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()
foreach(property LINK_LIBRARIES INTERFACE_LINK_LIBRARIES)
    get_target_property(libraries nghttp2_static ${property})
    if(libraries)
        list(REMOVE_ITEM libraries windows_seh_personalities)
        set_property(TARGET nghttp2_static PROPERTY ${property} "${libraries}")
    endif()
endforeach()
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)

set(http2ZlibDir ${CMAKE_SOURCE_DIR}/3rdparty/libjpeg-turbo/src/spng/zlib)
add_library(anyps5_http2_transport STATIC EXCLUDE_FROM_ALL Transport.cpp
    ${http2ZlibDir}/adler32.c ${http2ZlibDir}/crc32.c ${http2ZlibDir}/inffast.c
    ${http2ZlibDir}/inflate.c ${http2ZlibDir}/inftrees.c ${http2ZlibDir}/zutil.c)
target_include_directories(anyps5_http2_transport PRIVATE ${LIBS_INCLUDE_DIR} ${http2ZlibDir})
target_link_libraries(anyps5_http2_transport PRIVATE libSceHttp libSceNet libSceSsl libkernel libkernel_sys libc nghttp2_static)
set_target_properties(anyps5_http2_transport PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_VISIBILITY_PRESET hidden C_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 libSceHttp 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_guest_pointer_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttp2GuestPointers.cpp)
    target_include_directories(guest_http2_guest_pointer_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http2_guest_pointer_tests PRIVATE libSceHttp2 libc)
    configure_windows_unwind(guest_http2_guest_pointer_tests)
    add_test(NAME guest_http2_guest_pointers COMMAND guest_http2_guest_pointer_tests)
    set_tests_properties(guest_http2_guest_pointers PROPERTIES TIMEOUT 30)
    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)
    set_tests_properties(guest_http2 PROPERTIES TIMEOUT 30)
    add_test(NAME guest_http2_offline_sync COMMAND guest_http2_tests --offline-sync)
    add_test(NAME guest_http2_offline_async COMMAND guest_http2_tests --offline-async)
    add_test(NAME guest_http2_offline_queued COMMAND guest_http2_tests --offline-queued)
    add_test(NAME guest_http2_offline_declared_length COMMAND guest_http2_tests --offline-declared-length)
    set_tests_properties(guest_http2_offline_sync guest_http2_offline_async guest_http2_offline_queued guest_http2_offline_declared_length PROPERTIES TIMEOUT 30)
    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)
    set_tests_properties(guest_http2_cookies PROPERTIES TIMEOUT 30)
    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)
    set_tests_properties(guest_http2_options PROPERTIES TIMEOUT 30)
    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 libSceHttp libSceNet libSceSsl libkernel libc nghttp2_static mbedtls mbedx509 mbedcrypto)
    configure_windows_unwind(guest_http2_transport_tests)
    add_test(NAME guest_http2_gzip_members COMMAND guest_http2_transport_tests --gzip-members)
    add_test(NAME guest_http2_gzip_headers COMMAND guest_http2_transport_tests --gzip-headers)
    set_tests_properties(guest_http2_gzip_members guest_http2_gzip_headers PROPERTIES TIMEOUT 30)
    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_gzip_members guest_http2_gzip_headers guest_http2_guest_pointers guest_http2 guest_http2_offline_sync guest_http2_offline_async guest_http2_offline_queued guest_http2_offline_declared_length 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>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp>")
    endif()
    add_test(NAME guest_http2_http1_gzip COMMAND guest_http2_transport_tests --http1-gzip)
    set_tests_properties(guest_http2_http1_gzip PROPERTIES TIMEOUT 30)
    add_test(NAME guest_http2_http1 COMMAND guest_http2_transport_tests --http1)
    add_test(NAME guest_http2_http1_cookies COMMAND guest_http2_transport_tests --http1-cookies)
    set_tests_properties(guest_http2_http1 guest_http2_http1_cookies PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_http2_http1 guest_http2_http1_cookies guest_http2_http1_gzip PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp2>")
    endif()
    add_test(NAME guest_http2_strip_gzip_headers COMMAND guest_http2_transport_tests --strip-gzip-headers)
    add_test(NAME guest_http2_gzip_content_length COMMAND guest_http2_transport_tests --gzip-content-length)
    add_test(NAME guest_http2_gzip_default COMMAND guest_http2_transport_tests --gzip-default)
    set_tests_properties(guest_http2_strip_gzip_headers guest_http2_gzip_content_length guest_http2_gzip_default PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_http2_strip_gzip_headers guest_http2_gzip_content_length guest_http2_gzip_default PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp2>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp>")
    endif()
    add_test(NAME guest_http2_ssl_callbacks COMMAND guest_http2_transport_tests --ssl-callbacks)
    set_tests_properties(guest_http2_ssl_callbacks PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_http2_ssl_callbacks PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp2>")
    endif()
    add_test(NAME guest_http2_redirects COMMAND guest_http2_transport_tests --redirects)
    set_tests_properties(guest_http2_redirects PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_http2_redirects 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 libSceHttp 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()

if(BUILD_TESTING)
    add_test(NAME guest_http2_auth_status COMMAND guest_http2_transport_tests --auth-status)
    set_tests_properties(guest_http2_auth_status PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_http2_auth_status PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp2>")
    endif()
endif()
