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 InventoryExports.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_options_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttp2Options.cpp)
    target_include_directories(guest_http2_options_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http2_options_tests PRIVATE libSceHttp2 libkernel libc)
    configure_windows_unwind(guest_http2_options_tests)
    add_test(NAME guest_http2_options COMMAND guest_http2_options_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(http2SystemRootsRun ${CMAKE_CURRENT_BINARY_DIR}/guest_http2_system_roots)
    file(MAKE_DIRECTORY ${http2SystemRootsRun})
    add_test(NAME guest_http2_system_roots COMMAND guest_http2_transport_tests --system-roots WORKING_DIRECTORY ${http2SystemRootsRun})
    set_tests_properties(guest_http2_transport guest_http2_early_response guest_http2_system_roots 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_options guest_http2_transport guest_http2_early_response guest_http2_dns_first guest_http2_system_roots PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp2>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_http2_teardown_tests ${LIBS_INCLUDE_DIR}/tests/Http2TeardownFailure.cpp Export.cpp Cookies.cpp)
    target_include_directories(guest_http2_teardown_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http2_teardown_tests PRIVATE libkernel libc anyps5_http2_transport)
    configure_windows_unwind(guest_http2_teardown_tests)
    add_test(NAME guest_http2_teardown COMMAND guest_http2_teardown_tests)
    set_tests_properties(guest_http2_teardown PROPERTIES TIMEOUT 30)
    if(WIN32)
        target_link_libraries(guest_http2_teardown_tests PRIVATE ws2_32)
        set_tests_properties(guest_http2_teardown PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
    endif()
endif()
