function(configure_http2_backend)
    set(ENABLE_LIB_ONLY ON)
    set(ENABLE_DOC OFF)
    set(BUILD_SHARED_LIBS OFF)
    set(BUILD_STATIC_LIBS ON)
    foreach(package OpenSSL Libngtcp2 Libnghttp3 Systemd Jansson Libevent LibXml2 Jemalloc)
        set(CMAKE_DISABLE_FIND_PACKAGE_${package} TRUE)
    endforeach()
    add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty/nghttp2 ${CMAKE_BINARY_DIR}/3rdparty/nghttp2 EXCLUDE_FROM_ALL)
endfunction()
configure_http2_backend()
set_target_properties(nghttp2_static PROPERTIES POSITION_INDEPENDENT_CODE ON C_VISIBILITY_PRESET hidden)
if(MINGW OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_link_options(nghttp2_static INTERFACE LINKER:--exclude-libs,libnghttp2.a)
endif()
install(FILES ${CMAKE_SOURCE_DIR}/3rdparty/nghttp2/COPYING DESTINATION licenses/nghttp2)

add_library(anyps5_http2_transport STATIC EXCLUDE_FROM_ALL Transport.cpp)
target_include_directories(anyps5_http2_transport PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(anyps5_http2_transport PRIVATE libSceNet libSceSsl libkernel libc nghttp2_static)
set_target_properties(anyps5_http2_transport PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_VISIBILITY_PRESET hidden)
if(MINGW OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_link_options(anyps5_http2_transport INTERFACE LINKER:--exclude-libs,libanyps5_http2_transport.a)
endif()
add_library(libSceHttp2 SHARED EXCLUDE_FROM_ALL Export.cpp Cookies.cpp)
target_include_directories(libSceHttp2 PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(libSceHttp2 PRIVATE libc libkernel anyps5_http2_transport)
if(WIN32)
    target_link_libraries(libSceHttp2 PRIVATE ws2_32)
endif()
configure_windows_unwind(libSceHttp2)

if(BUILD_TESTING)
    add_test_executable(guest_http2_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttp2.cpp)
    target_include_directories(guest_http2_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http2_tests PRIVATE libSceHttp2 libkernel libc)
    configure_windows_unwind(guest_http2_tests)
    add_test(NAME guest_http2 COMMAND guest_http2_tests)
    add_test_executable(guest_http2_cookie_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttp2Cookies.cpp)
    target_include_directories(guest_http2_cookie_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http2_cookie_tests PRIVATE libSceHttp2 libkernel libc)
    configure_windows_unwind(guest_http2_cookie_tests)
    add_test(NAME guest_http2_cookies COMMAND guest_http2_cookie_tests)
    add_test_executable(guest_http2_transport_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttp2Transport.cpp)
    target_include_directories(guest_http2_transport_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http2_transport_tests PRIVATE libSceHttp2 libSceNet libSceSsl libkernel libc nghttp2_static mbedtls mbedx509 mbedcrypto)
    configure_windows_unwind(guest_http2_transport_tests)
    add_test(NAME guest_http2_transport COMMAND guest_http2_transport_tests)
    add_test(NAME guest_http2_early_response COMMAND guest_http2_transport_tests --early)
    set_tests_properties(guest_http2_transport guest_http2_early_response PROPERTIES TIMEOUT 30)
    if(WIN32)
        target_link_libraries(guest_http2_transport_tests PRIVATE ws2_32 bcrypt)
        add_test(NAME guest_http2_dns_first COMMAND guest_http2_transport_tests --dns-first)
        set_tests_properties(guest_http2_dns_first PROPERTIES TIMEOUT 30)
        set_tests_properties(guest_http2 guest_http2_cookies guest_http2_transport guest_http2_early_response guest_http2_dns_first PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp2>")
    endif()
endif()
