add_library(libSceLibreSsl SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Digest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Runtime.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Aes.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Error.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Ocsp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Stack.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Ui.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Rsa.cpp)
target_include_directories(libSceLibreSsl PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(libSceLibreSsl PRIVATE mbedcrypto)
configure_windows_unwind(libSceLibreSsl)
target_sources(libSceLibreSsl PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/Asn1.cpp)

if(BUILD_TESTING)
    add_test_executable(guest_libressl_x509_errors_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslX509Errors.cpp)
    target_include_directories(guest_libressl_x509_errors_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_x509_errors_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_x509_errors_tests)
    add_test(NAME guest_libressl_x509_errors COMMAND guest_libressl_x509_errors_tests)
    set_tests_properties(guest_libressl_x509_errors PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_x509_errors PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_digest_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslDigest.cpp)
    target_include_directories(guest_libressl_digest_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_digest_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_digest_tests)
    add_test(NAME guest_libressl_digest COMMAND guest_libressl_digest_tests)
    set_tests_properties(guest_libressl_digest PROPERTIES TIMEOUT 30)
    add_test_executable(guest_libressl_asn1_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/Asn1.cpp)
    target_include_directories(guest_libressl_asn1_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_asn1_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_asn1_tests)
    add_test(NAME guest_libressl_asn1 COMMAND guest_libressl_asn1_tests)
    add_test(NAME guest_libressl_asn1_allocator COMMAND guest_libressl_asn1_tests allocator)
    set_tests_properties(guest_libressl_asn1 guest_libressl_asn1_allocator PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_digest guest_libressl_asn1 guest_libressl_asn1_allocator PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_des_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslDes.cpp)
    target_include_directories(guest_libressl_des_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_des_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_des_tests)
    add_test(NAME guest_libressl_des COMMAND guest_libressl_des_tests)
    set_tests_properties(guest_libressl_des PROPERTIES TIMEOUT 20)
    if(WIN32)
        set_tests_properties(guest_libressl_des PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_runtime_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslRuntime.cpp)
    target_include_directories(guest_libressl_runtime_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_runtime_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_runtime_tests)
    add_test(NAME guest_libressl_runtime COMMAND guest_libressl_runtime_tests)
    set_tests_properties(guest_libressl_runtime PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_runtime PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_aes_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslAes.cpp)
    target_include_directories(guest_libressl_aes_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_aes_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_aes_tests)
    add_test(NAME guest_libressl_aes COMMAND guest_libressl_aes_tests)
    set_tests_properties(guest_libressl_aes PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_aes PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_error_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslError.cpp)
    target_include_directories(guest_libressl_error_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_error_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_error_tests)
    add_test(NAME guest_libressl_error COMMAND guest_libressl_error_tests)
    set_tests_properties(guest_libressl_error PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_error PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_ocsp_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslOcsp.cpp)
    target_include_directories(guest_libressl_ocsp_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_ocsp_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_ocsp_tests)
    add_test(NAME guest_libressl_ocsp COMMAND guest_libressl_ocsp_tests)
    set_tests_properties(guest_libressl_ocsp PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_ocsp PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_stack_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslStack.cpp)
    target_include_directories(guest_libressl_stack_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_stack_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_stack_tests)
    add_test(NAME guest_libressl_stack COMMAND guest_libressl_stack_tests)
    set_tests_properties(guest_libressl_stack PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_stack PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_ui_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslUi.cpp)
    target_include_directories(guest_libressl_ui_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_ui_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_ui_tests)
    add_test(NAME guest_libressl_ui COMMAND guest_libressl_ui_tests)
    set_tests_properties(guest_libressl_ui PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_ui PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()

    add_test_executable(guest_libressl_rsa_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslRsa.cpp)
    target_include_directories(guest_libressl_rsa_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_rsa_tests PRIVATE libSceLibreSsl)
    configure_windows_unwind(guest_libressl_rsa_tests)
    add_test(NAME guest_libressl_rsa COMMAND guest_libressl_rsa_tests)
    set_tests_properties(guest_libressl_rsa PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_rsa PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()
endif()

target_sources(libSceLibreSsl PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/Cipher.cpp)

if(BUILD_TESTING)
    add_test_executable(guest_libressl_cipher_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslCipher.cpp)
    target_include_directories(guest_libressl_cipher_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_cipher_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_cipher_tests)
    add_test(NAME guest_libressl_cipher COMMAND guest_libressl_cipher_tests)
    set_tests_properties(guest_libressl_cipher PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_cipher PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()
endif()

target_sources(libSceLibreSsl PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Alert.cpp)

if(BUILD_TESTING)
    add_test_executable(guest_libressl_alert_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslAlert.cpp)
    target_include_directories(guest_libressl_alert_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_alert_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_alert_tests)
    add_test(NAME guest_libressl_alert COMMAND guest_libressl_alert_tests)
    set_tests_properties(guest_libressl_alert PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_alert PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_libressl_version_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslVersion.cpp)
    target_include_directories(guest_libressl_version_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_version_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_version_tests)
    add_test(NAME guest_libressl_version COMMAND guest_libressl_version_tests)
    set_tests_properties(guest_libressl_version PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_libressl_version PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_libressl_crypto_locks_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslCryptoLocks.cpp)
    target_include_directories(guest_libressl_crypto_locks_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_libressl_crypto_locks_tests PRIVATE libSceLibreSsl libc)
    configure_windows_unwind(guest_libressl_crypto_locks_tests)
    add_test(NAME guest_libressl_crypto_locks COMMAND guest_libressl_crypto_locks_tests)
    set_tests_properties(guest_libressl_crypto_locks PROPERTIES TIMEOUT 20)
    if(WIN32)
        set_tests_properties(guest_libressl_crypto_locks PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
    endif()
endif()
