add_library(libSceJson2 SHARED EXCLUDE_FROM_ALL Export.cpp InventoryExports.cpp)
target_link_libraries(libSceJson2 PRIVATE libkernel libc)
target_include_directories(libSceJson2 PRIVATE ${LIBS_INCLUDE_DIR})
configure_windows_unwind(libSceJson2)

set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/Api.hpp")
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/Api.hpp" jsonDeclarations REGEX "_ZN")
set(jsonExports "")
set(jsonWindowsExports "")
foreach(declaration IN LISTS jsonDeclarations)
    string(REGEX MATCH "(_ZN[A-Za-z0-9_]+)" match "${declaration}")
    set(symbol "${CMAKE_MATCH_1}")
    list(APPEND jsonExports "${symbol}")
    if(declaration MATCHES "\\(")
        list(APPEND jsonWindowsExports "${symbol}")
    else()
        list(APPEND jsonWindowsExports "${symbol} DATA")
    endif()
endforeach()
if(APPLE)
    set(exportList "")
    foreach(symbol IN LISTS jsonExports)
        string(APPEND exportList "_${symbol}\n")
    endforeach()
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/json2.exports" "${exportList}")
    target_link_options(libSceJson2 PRIVATE "LINKER:-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/json2.exports")
elseif(WIN32)
    string(JOIN "\n" exportList ${jsonWindowsExports})
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/json2.def" "EXPORTS\n${exportList}\n")
    target_sources(libSceJson2 PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/json2.def")
else()
    string(JOIN ";\n" exportList ${jsonExports})
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/json2.exports" "{ global:\n${exportList};\nlocal: *; };\n")
    target_link_options(libSceJson2 PRIVATE "LINKER:--version-script=${CMAKE_CURRENT_BINARY_DIR}/json2.exports")
endif()

if(BUILD_TESTING)
    add_test_executable(guest_json_legacy_self_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonLegacySelf.cpp Export.cpp)
    target_include_directories(guest_json_legacy_self_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_legacy_self_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_legacy_self_tests)
    add_test(NAME guest_json_legacy_self COMMAND guest_json_legacy_self_tests)
    if(WIN32)
        set_tests_properties(guest_json_legacy_self PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
    add_test_executable(guest_json_termination_exceptions_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonTerminationExceptions.cpp)
    target_include_directories(guest_json_termination_exceptions_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_termination_exceptions_tests PRIVATE libSceJson2 libkernel libc)
    configure_windows_unwind(guest_json_termination_exceptions_tests)
    add_test_executable(guest_json_legacy_termination_exceptions_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonTerminationExceptions.cpp Export.cpp)
    target_compile_definitions(guest_json_legacy_termination_exceptions_tests PRIVATE JSON2_TERMINATION_LEGACY_SDK)
    target_include_directories(guest_json_legacy_termination_exceptions_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_legacy_termination_exceptions_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_legacy_termination_exceptions_tests)
    foreach(flavor modern legacy)
        if(flavor STREQUAL "legacy")
            set(exception_target guest_json_legacy_termination_exceptions_tests)
        else()
            set(exception_target guest_json_termination_exceptions_tests)
        endif()
        add_test(NAME guest_json_${flavor}_termination_healthy COMMAND ${exception_target} 0 before)
        foreach(position RANGE 1 12)
            foreach(boundary before after)
                add_test(NAME guest_json_${flavor}_termination_exception_${boundary}_${position} COMMAND ${exception_target} ${position} ${boundary})
                if(WIN32 AND flavor STREQUAL "legacy")
                    set_tests_properties(guest_json_${flavor}_termination_exception_${boundary}_${position} PROPERTIES ENVIRONMENT_MODIFICATION
                        "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
                endif()
            endforeach()
        endforeach()
        foreach(position 1 5 6 8 9 11 12)
            add_test(NAME guest_json_${flavor}_termination_retry_${position} COMMAND ${exception_target} ${position} before retry)
            if(WIN32 AND flavor STREQUAL "legacy")
                set_tests_properties(guest_json_${flavor}_termination_retry_${position} PROPERTIES ENVIRONMENT_MODIFICATION
                    "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
            endif()
        endforeach()
        if(flavor STREQUAL "legacy")
            add_test(NAME guest_json_legacy_termination_retry_2 COMMAND ${exception_target} 2 before retry)
            add_test(NAME guest_json_legacy_termination_retry_null_after COMMAND ${exception_target} 1 after retry)
            if(WIN32)
                set_tests_properties(guest_json_legacy_termination_retry_2 guest_json_legacy_termination_retry_null_after PROPERTIES ENVIRONMENT_MODIFICATION
                    "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
            endif()
        endif()
        if(WIN32 AND flavor STREQUAL "legacy")
            set_tests_properties(guest_json_${flavor}_termination_healthy PROPERTIES ENVIRONMENT_MODIFICATION
                "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
        endif()
    endforeach()
    add_test_executable(guest_json_initialization_exceptions_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonInitializationExceptions.cpp)
    target_include_directories(guest_json_initialization_exceptions_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_initialization_exceptions_tests PRIVATE libSceJson2 libkernel libc)
    configure_windows_unwind(guest_json_initialization_exceptions_tests)
    foreach(position RANGE 0 12)
        foreach(callback allocate notify)
            add_test(NAME guest_json_initialization_exception_${callback}_${position} COMMAND guest_json_initialization_exceptions_tests ${position} ${callback})
        endforeach()
    endforeach()
    add_test_executable(guest_json_legacy_initialization_exceptions_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonInitializationExceptions.cpp Export.cpp)
    target_compile_definitions(guest_json_legacy_initialization_exceptions_tests PRIVATE JSON2_EXCEPTION_LEGACY_SDK)
    target_include_directories(guest_json_legacy_initialization_exceptions_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_legacy_initialization_exceptions_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_legacy_initialization_exceptions_tests)
    foreach(position RANGE 0 11)
        foreach(callback allocate notify)
            add_test(NAME guest_json_legacy_initialization_exception_${callback}_${position} COMMAND guest_json_legacy_initialization_exceptions_tests ${position} ${callback})
            if(WIN32)
                set_tests_properties(guest_json_legacy_initialization_exception_${callback}_${position} PROPERTIES ENVIRONMENT_MODIFICATION
                    "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
            endif()
        endforeach()
    endforeach()
    add_test_executable(guest_json_legacy_composite_destruction_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonCompositeDestruction.cpp Export.cpp)
    target_compile_definitions(guest_json_legacy_composite_destruction_tests PRIVATE JSON2_DESTRUCTION_LEGACY_SDK)
    target_include_directories(guest_json_legacy_composite_destruction_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_legacy_composite_destruction_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_legacy_composite_destruction_tests)
    add_test(NAME guest_json_legacy_composite_destruction COMMAND guest_json_legacy_composite_destruction_tests)
    if(WIN32)
        set_tests_properties(guest_json_legacy_composite_destruction PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
    add_test_executable(guest_json_composite_destruction_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonCompositeDestruction.cpp)
    target_include_directories(guest_json_composite_destruction_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_composite_destruction_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_composite_destruction_tests)
    add_test(NAME guest_json_composite_destruction COMMAND guest_json_composite_destruction_tests)
    add_test_executable(guest_json_legacy_initialization_publication_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonInitializationPublication.cpp Export.cpp)
    target_compile_definitions(guest_json_legacy_initialization_publication_tests PRIVATE JSON2_PUBLICATION_LEGACY_SDK)
    target_include_directories(guest_json_legacy_initialization_publication_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_legacy_initialization_publication_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_legacy_initialization_publication_tests)
    add_test(NAME guest_json_legacy_initialization_publication COMMAND guest_json_legacy_initialization_publication_tests)
    if(WIN32)
        set_tests_properties(guest_json_legacy_initialization_publication PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
    add_test_executable(guest_json_initialization_publication_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonInitializationPublication.cpp)
    target_include_directories(guest_json_initialization_publication_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_initialization_publication_tests PRIVATE libSceJson2 libkernel libc)
    configure_windows_unwind(guest_json_initialization_publication_tests)
    add_test(NAME guest_json_initialization_publication COMMAND guest_json_initialization_publication_tests)
    add_test_executable(guest_json_callback_lifecycle_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonCallbackLifecycle.cpp)
    target_include_directories(guest_json_callback_lifecycle_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_callback_lifecycle_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_callback_lifecycle_tests)
    add_test(NAME guest_json_callback_lifecycle COMMAND guest_json_callback_lifecycle_tests)
    add_test_executable(guest_json_callback_copies_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonCallbackCopies.cpp)
    target_include_directories(guest_json_callback_copies_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_callback_copies_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_callback_copies_tests)
    add_test(NAME guest_json_callback_copies COMMAND guest_json_callback_copies_tests)
    add_test_executable(guest_json_pair_allocator_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonPairAllocator.cpp)
    target_include_directories(guest_json_pair_allocator_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_pair_allocator_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_pair_allocator_tests)
    add_test(NAME guest_json_pair_allocator COMMAND guest_json_pair_allocator_tests)
    add_test_executable(guest_json_pair_layout_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonPairLayout.cpp)
    target_include_directories(guest_json_pair_layout_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_pair_layout_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_pair_layout_tests)
    add_test(NAME guest_json_pair_layout COMMAND guest_json_pair_layout_tests)
    add_test_executable(guest_json_kernel_initialization_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonKernelInitialization.cpp Export.cpp)
    target_include_directories(guest_json_kernel_initialization_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_kernel_initialization_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_kernel_initialization_tests)
    add_test(NAME guest_json_kernel_initialization COMMAND guest_json_kernel_initialization_tests)
    if(WIN32)
        set_tests_properties(guest_json_kernel_initialization PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
    add_test_executable(guest_json_inherited_recovery_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonInheritedRecovery.cpp)
    target_include_directories(guest_json_inherited_recovery_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_inherited_recovery_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_inherited_recovery_tests)
    add_test(NAME guest_json_inherited_recovery COMMAND guest_json_inherited_recovery_tests)
    add_test_executable(guest_json_initialization_release_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonInitializationRelease.cpp)
    target_include_directories(guest_json_initialization_release_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_initialization_release_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_initialization_release_tests)
    add_test(NAME guest_json_initialization_release COMMAND guest_json_initialization_release_tests)
    add_test_executable(guest_json_legacy_initialization_release_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonInitializationRelease.cpp Export.cpp)
    target_compile_definitions(guest_json_legacy_initialization_release_tests PRIVATE JSON2_LEGACY_TEST)
    target_include_directories(guest_json_legacy_initialization_release_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_legacy_initialization_release_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_legacy_initialization_release_tests)
    add_test(NAME guest_json_legacy_initialization_release COMMAND guest_json_legacy_initialization_release_tests)
    if(WIN32)
        set_tests_properties(guest_json_initialization_release guest_json_legacy_initialization_release PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJson2>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
    add_test_executable(guest_json_recovery_allocator_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonRecoveryAllocator.cpp)
    target_include_directories(guest_json_recovery_allocator_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_recovery_allocator_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_recovery_allocator_tests)
    add_test(NAME guest_json_recovery_allocator COMMAND guest_json_recovery_allocator_tests)
    add_test_executable(guest_json_parser_metadata_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonParserMetadata.cpp)
    target_include_directories(guest_json_parser_metadata_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_parser_metadata_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_parser_metadata_tests)
    add_test(NAME guest_json_parser_metadata COMMAND guest_json_parser_metadata_tests)
    add_test_executable(guest_json_parser_guest_pointers_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonParserGuestPointers.cpp)
    target_include_directories(guest_json_parser_guest_pointers_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_parser_guest_pointers_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_parser_guest_pointers_tests)
    add_test(NAME guest_json_parser_guest_pointers COMMAND guest_json_parser_guest_pointers_tests)
    if(WIN32)
        set_tests_properties(guest_json_parser_guest_pointers PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJson2>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
    add_test_executable(guest_json_decimal_range_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonDecimalRange.cpp)
    target_include_directories(guest_json_decimal_range_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_decimal_range_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_decimal_range_tests)
    add_test(NAME guest_json_decimal_range COMMAND guest_json_decimal_range_tests)
    add_test_executable(guest_json_object_guest_pointers_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonObjectGuestPointers.cpp)
    target_include_directories(guest_json_object_guest_pointers_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_object_guest_pointers_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_object_guest_pointers_tests)
    add_test(NAME guest_json_object_guest_pointers COMMAND guest_json_object_guest_pointers_tests)
    if(WIN32)
        set_tests_properties(guest_json_object_guest_pointers PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJson2>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
    add_test_executable(guest_json_serialization_callback_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonSerializationCallback.cpp)
    target_include_directories(guest_json_serialization_callback_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_serialization_callback_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_serialization_callback_tests)
    add_test(NAME guest_json_serialization_callback COMMAND guest_json_serialization_callback_tests)
    add_test_executable(guest_json_key_lookup_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonKeyLookup.cpp)
    target_include_directories(guest_json_key_lookup_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_key_lookup_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_key_lookup_tests)
    add_test(NAME guest_json_key_lookup COMMAND guest_json_key_lookup_tests)
    add_test_executable(guest_json_array_insertion_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonArrayInsertion.cpp)
    target_include_directories(guest_json_array_insertion_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_array_insertion_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_array_insertion_tests)
    add_test(NAME guest_json_array_insertion COMMAND guest_json_array_insertion_tests)
    add_test_executable(guest_json_array_mutation_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonArrayMutation.cpp)
    target_include_directories(guest_json_array_mutation_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_array_mutation_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_array_mutation_tests)
    add_test(NAME guest_json_array_mutation COMMAND guest_json_array_mutation_tests)
    add_test_executable(guest_json_value_refer_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonValueRefer.cpp)
    target_include_directories(guest_json_value_refer_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_value_refer_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_value_refer_tests)
    add_test(NAME guest_json_value_refer COMMAND guest_json_value_refer_tests)
    add_test_executable(guest_json_internal_initializer_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonInternalInitializer.cpp)
    target_include_directories(guest_json_internal_initializer_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_internal_initializer_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_internal_initializer_tests)
    add_test(NAME guest_json_internal_initializer COMMAND guest_json_internal_initializer_tests)
    add_test_executable(guest_json_allocation_entry_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonAllocationEntry.cpp)
    target_include_directories(guest_json_allocation_entry_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_allocation_entry_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_allocation_entry_tests)
    add_test(NAME guest_json_allocation_entry COMMAND guest_json_allocation_entry_tests)
    add_test_executable(guest_json_string_search_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonStringSearch.cpp)
    target_include_directories(guest_json_string_search_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_string_search_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_string_search_tests)
    add_test(NAME guest_json_string_search COMMAND guest_json_string_search_tests)
    add_test_executable(guest_json_reference_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonReferences.cpp)
    target_include_directories(guest_json_reference_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_reference_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_reference_tests)
    add_test(NAME guest_json_references COMMAND guest_json_reference_tests)
    add_test_executable(guest_json_alias_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonAliases.cpp)
    target_include_directories(guest_json_alias_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_alias_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_alias_tests)
    add_test(NAME guest_json_aliases COMMAND guest_json_alias_tests)
    add_test_executable(guest_json_tests ${LIBS_INCLUDE_DIR}/tests/GuestJson.cpp)
    target_include_directories(guest_json_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_tests)
    add_test(NAME guest_json COMMAND guest_json_tests)
    add_test(NAME guest_json_numeric_locale COMMAND guest_json_tests --numeric-locale)
    set_tests_properties(guest_json_numeric_locale PROPERTIES SKIP_RETURN_CODE 77)
    add_test_executable(guest_json_float_settings_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonFloatSettings.cpp)
    target_include_directories(guest_json_float_settings_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_float_settings_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_float_settings_tests)
    add_test(NAME guest_json_float_settings COMMAND guest_json_float_settings_tests)
    add_test_executable(guest_json_allocator_settings_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonAllocatorSettings.cpp)
    target_include_directories(guest_json_allocator_settings_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_allocator_settings_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_allocator_settings_tests)
    add_test(NAME guest_json_allocator_settings COMMAND guest_json_allocator_settings_tests)
    add_test_executable(guest_json_parser_state_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonParserState.cpp)
    target_include_directories(guest_json_parser_state_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_parser_state_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_parser_state_tests)
    add_test(NAME guest_json_parser_state COMMAND guest_json_parser_state_tests)
    add_test_executable(guest_json_value_layout_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonValueLayout.cpp)
    target_include_directories(guest_json_value_layout_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_value_layout_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_value_layout_tests)
    add_test(NAME guest_json_value_layout COMMAND guest_json_value_layout_tests)
    add_test_executable(guest_json_value_ownership_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonValueOwnership.cpp)
    target_include_directories(guest_json_value_ownership_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_value_ownership_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_value_ownership_tests)
    add_test(NAME guest_json_value_ownership COMMAND guest_json_value_ownership_tests)
    add_test_executable(guest_json_special_float_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonSpecialFloat.cpp)
    target_include_directories(guest_json_special_float_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_special_float_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_special_float_tests)
    add_test(NAME guest_json_special_float COMMAND guest_json_special_float_tests)
    add_test_executable(guest_json_value_text_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonValueText.cpp)
    target_include_directories(guest_json_value_text_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_value_text_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_value_text_tests)
    add_test(NAME guest_json_value_text COMMAND guest_json_value_text_tests)
    add_test_executable(guest_json_element_recovery_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonElementRecovery.cpp)
    target_include_directories(guest_json_element_recovery_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_element_recovery_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_element_recovery_tests)
    add_test(NAME guest_json_element_recovery COMMAND guest_json_element_recovery_tests)
    add_test_executable(guest_json_null_recovery_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonNullRecovery.cpp)
    target_include_directories(guest_json_null_recovery_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_null_recovery_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_null_recovery_tests)
    add_test(NAME guest_json_null_recovery COMMAND guest_json_null_recovery_tests)
    add_test_executable(guest_json_global_recovery_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonGlobalRecovery.cpp)
    target_include_directories(guest_json_global_recovery_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_global_recovery_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_global_recovery_tests)
    add_test(NAME guest_json_global_recovery COMMAND guest_json_global_recovery_tests)
    add_test_executable(guest_json_string_allocator_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonStringAllocator.cpp)
    target_include_directories(guest_json_string_allocator_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_string_allocator_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_string_allocator_tests)
    add_test(NAME guest_json_string_allocator COMMAND guest_json_string_allocator_tests)
    add_test_executable(guest_json_array_allocator_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonArrayAllocator.cpp)
    target_include_directories(guest_json_array_allocator_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_array_allocator_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_array_allocator_tests)
    add_test(NAME guest_json_array_allocator COMMAND guest_json_array_allocator_tests)
    add_test_executable(guest_json_allocator_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonAllocator.cpp)
    target_include_directories(guest_json_allocator_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_allocator_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_allocator_tests)
    add_test(NAME guest_json_allocator COMMAND guest_json_allocator_tests)
    add_test_executable(guest_json_value_allocator_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonValueAllocator.cpp)
    target_include_directories(guest_json_value_allocator_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_value_allocator_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_value_allocator_tests)
    add_test(NAME guest_json_value_allocator COMMAND guest_json_value_allocator_tests)
    add_test_executable(guest_json_object_allocator_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonObjectAllocator.cpp)
    target_include_directories(guest_json_object_allocator_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_object_allocator_tests PRIVATE libSceJson2 libc)
    configure_windows_unwind(guest_json_object_allocator_tests)
    add_test(NAME guest_json_object_allocator COMMAND guest_json_object_allocator_tests)
    if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
        set(numeric_locale_dir ${CMAKE_CURRENT_BINARY_DIR}/numeric-locale)
        set(numeric_locale_data ${LIBS_INCLUDE_DIR}/tests/numeric-locale)
        add_test(NAME guest_json_numeric_locale_setup COMMAND sh -c
            "rm -rf '${numeric_locale_dir}' && mkdir -p '${numeric_locale_dir}' && I18NPATH='${numeric_locale_dir}' localedef -c -f '${numeric_locale_data}/comma.charmap' -i '${numeric_locale_data}/comma.locale' '${numeric_locale_dir}/xx_COMMA'; test -f '${numeric_locale_dir}/xx_COMMA/LC_NUMERIC'")
        set_tests_properties(guest_json_numeric_locale_setup PROPERTIES FIXTURES_SETUP json_numeric_locale)
        set_tests_properties(guest_json_numeric_locale PROPERTIES FIXTURES_REQUIRED json_numeric_locale
            ENVIRONMENT "LOCPATH=${numeric_locale_dir}")
    endif()
    if(WIN32)
        get_property(json2_module_exception_tests DIRECTORY PROPERTY TESTS)
        list(FILTER json2_module_exception_tests INCLUDE REGEX "^guest_json_(modern_termination_|initialization_exception_)")
        set_tests_properties(${json2_module_exception_tests} guest_json_composite_destruction PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJson2>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
        set_tests_properties(guest_json guest_json_initialization_publication guest_json_callback_lifecycle guest_json_callback_copies guest_json_pair_allocator guest_json_pair_layout guest_json_inherited_recovery guest_json_recovery_allocator guest_json_parser_metadata guest_json_decimal_range guest_json_serialization_callback guest_json_key_lookup guest_json_array_insertion guest_json_array_mutation guest_json_internal_initializer guest_json_allocation_entry guest_json_string_search guest_json_references guest_json_aliases guest_json_parser_state guest_json_numeric_locale guest_json_float_settings guest_json_allocator_settings guest_json_value_layout guest_json_value_ownership guest_json_special_float guest_json_element_recovery guest_json_null_recovery guest_json_global_recovery guest_json_string_allocator guest_json_array_allocator guest_json_object_allocator guest_json_value_allocator guest_json_allocator PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJson2>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_json_copy_failure_tests ${LIBS_INCLUDE_DIR}/tests/JsonCopyFailure.cpp Export.cpp)
    target_include_directories(guest_json_copy_failure_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_copy_failure_tests PRIVATE libkernel libc)
    target_compile_features(guest_json_copy_failure_tests PRIVATE cxx_std_23)
    if(WIN32)
        configure_windows_unwind(guest_json_copy_failure_tests)
    endif()
    add_test(NAME guest_json_copy_failure COMMAND guest_json_copy_failure_tests)
    set_tests_properties(guest_json_copy_failure PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_json_copy_failure PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_json_float_failure_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonFloatFailure.cpp Export.cpp)
    target_include_directories(guest_json_float_failure_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_float_failure_tests PRIVATE libkernel libc)
    if(WIN32)
        configure_windows_unwind(guest_json_float_failure_tests)
    endif()
    add_test(NAME guest_json_float_failure COMMAND guest_json_float_failure_tests)
    set_tests_properties(guest_json_float_failure PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_json_float_failure PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_json_file_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonFile.cpp Export.cpp)
    target_include_directories(guest_json_file_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_file_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_file_tests)
    add_test(NAME guest_json_file COMMAND guest_json_file_tests)
    set_tests_properties(guest_json_file PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_json_file PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_json_legacy_handlers_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonLegacyHandlers.cpp Export.cpp)
    target_include_directories(guest_json_legacy_handlers_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_legacy_handlers_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_legacy_handlers_tests)
    add_test(NAME guest_json_legacy_handlers COMMAND guest_json_legacy_handlers_tests)
    foreach(position RANGE 0 2)
        foreach(alias RANGE 0 1)
            foreach(wrapper direct wrapped)
                add_test(NAME guest_json_legacy_object_heap_exception_${position}_${alias}_${wrapper} COMMAND guest_json_legacy_handlers_tests object-heap ${position} ${alias} ${wrapper})
                if(WIN32)
                    set_tests_properties(guest_json_legacy_object_heap_exception_${position}_${alias}_${wrapper} PROPERTIES ENVIRONMENT_MODIFICATION
                        "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
                endif()
            endforeach()
        endforeach()
    endforeach()
    foreach(position RANGE 0 2)
        foreach(alias RANGE 0 1)
            add_test(NAME guest_json_legacy_array_heap_exception_${position}_${alias} COMMAND guest_json_legacy_handlers_tests array-heap ${position} ${alias})
            add_test(NAME guest_json_legacy_value_array_heap_exception_${position}_${alias} COMMAND guest_json_legacy_handlers_tests value-array-heap ${position} ${alias})
            if(WIN32)
                set_tests_properties(guest_json_legacy_array_heap_exception_${position}_${alias} guest_json_legacy_value_array_heap_exception_${position}_${alias} PROPERTIES ENVIRONMENT_MODIFICATION
                    "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
            endif()
        endforeach()
    endforeach()
    foreach(shape RANGE 0 3)
        foreach(alias RANGE 0 2)
            add_test(NAME guest_json_legacy_copy_heap_exception_${shape}_${alias} COMMAND guest_json_legacy_handlers_tests ${shape} ${alias})
            if(WIN32)
                set_tests_properties(guest_json_legacy_copy_heap_exception_${shape}_${alias} PROPERTIES ENVIRONMENT_MODIFICATION
                    "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
            endif()
        endforeach()
    endforeach()
    foreach(position RANGE 0 2)
        add_test(NAME guest_json_legacy_heap_exception_${position} COMMAND guest_json_legacy_handlers_tests ${position})
        if(WIN32)
            set_tests_properties(guest_json_legacy_heap_exception_${position} PROPERTIES ENVIRONMENT_MODIFICATION
                "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
        endif()
    endforeach()
    if(WIN32)
        set_tests_properties(guest_json_legacy_handlers PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_json_legacy_serialization_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonLegacySerialization.cpp Export.cpp)
    target_include_directories(guest_json_legacy_serialization_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_legacy_serialization_tests PRIVATE libkernel libc)
    configure_windows_unwind(guest_json_legacy_serialization_tests)
    add_test(NAME guest_json_legacy_serialization COMMAND guest_json_legacy_serialization_tests)
    if(WIN32)
        set_tests_properties(guest_json_legacy_serialization PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
endif()

if(BUILD_TESTING)
    find_package(Threads REQUIRED)
    add_test_executable(guest_json_callback_threads_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonCallbackThreads.cpp Export.cpp)
    target_include_directories(guest_json_callback_threads_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_callback_threads_tests PRIVATE libkernel libc Threads::Threads)
    configure_windows_unwind(guest_json_callback_threads_tests)
    add_test(NAME guest_json_callback_threads COMMAND guest_json_callback_threads_tests)
    set_tests_properties(guest_json_callback_threads PROPERTIES TIMEOUT 10)
    if(WIN32)
        set_tests_properties(guest_json_callback_threads PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
    add_test_executable(guest_json_terminate_lock_order_tests ${LIBS_INCLUDE_DIR}/tests/GuestJsonTerminateLockOrder.cpp Export.cpp)
    target_include_directories(guest_json_terminate_lock_order_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_json_terminate_lock_order_tests PRIVATE libkernel libc Threads::Threads)
    configure_windows_unwind(guest_json_terminate_lock_order_tests)
    add_test(NAME guest_json_terminate_lock_order COMMAND guest_json_terminate_lock_order_tests)
    set_tests_properties(guest_json_terminate_lock_order PROPERTIES TIMEOUT 10)
    if(WIN32)
        set_tests_properties(guest_json_terminate_lock_order PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
    endif()
endif()
