add_library(jxr_lookup_native STATIC src/NativeLookup.c src/NativeQuant.c src/NativeBitIo.c src/NativePredict.c)
configure_jxr_native(jxr_lookup_native)
target_include_directories(jxr_lookup_native PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(jxr_lookup_native PRIVATE jxr)
add_library(libSceJxr SHARED EXCLUDE_FROM_ALL Export.cpp src/Allocation.cpp src/GlueHelpers.cpp src/PixelFormatLookup.cpp src/GuidExports.cpp src/Quantizer.cpp src/BitIo.cpp src/Predict.cpp src/SimpleBitIo.cpp src/Transcode.cpp src/WriteSource.cpp)
add_library(jxr_glue_guest STATIC ${JXR_SOURCE_DIR}/jxrgluelib/JXRGlue.c src/NativeGlue.c)
configure_jxr_native(jxr_glue_guest)
target_include_directories(jxr_glue_guest PRIVATE ${LIBS_INCLUDE_DIR})
set_source_files_properties(${JXR_SOURCE_DIR}/jxrgluelib/JXRGlue.c TARGET_DIRECTORY jxr_glue_guest
    PROPERTIES COMPILE_OPTIONS "-include;${CMAKE_CURRENT_SOURCE_DIR}/src/GuestHeap.h")
target_link_libraries(jxr_glue_guest PRIVATE jxr)
target_include_directories(jxr_glue_guest PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(libSceJxr PRIVATE jxr_glue_guest jxr_lookup_native)
if(NOT APPLE)
    target_link_options(libSceJxr PRIVATE LINKER:--exclude-libs,ALL)
endif()
include(Rows.cmake)
include(PostProc.cmake)
target_include_directories(libSceJxr PRIVATE ${LIBS_INCLUDE_DIR})
target_sources(libSceJxr PRIVATE src/ListStream.cpp src/ImageEncode.cpp src/FileStream.cpp)
configure_windows_unwind(libSceJxr)

include(Metadata.cmake)
include(Wmp.cmake)

if(BUILD_TESTING)
    add_library(jxr_lookup_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrLookupReference.c)
    configure_jxr_native(jxr_lookup_reference)
    target_link_libraries(jxr_lookup_reference PRIVATE jxr)
    add_test_executable(guest_jxr_lookup_tests NO_RUNTIME_DLL_STAGING
        ${LIBS_INCLUDE_DIR}/tests/GuestJxrLookup.cpp)
    target_include_directories(guest_jxr_lookup_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_lookup_tests PRIVATE jxr_lookup_reference ${CMAKE_DL_LIBS})
    if(WIN32)
        target_link_libraries(guest_jxr_lookup_tests PRIVATE libc)
    endif()
    configure_windows_unwind(guest_jxr_lookup_tests)
    add_custom_target(jxr_lookup_patched DEPENDS ${PATCHED_DIR}/libSceJxr.prx ${PATCHED_DIR}/libkernel.prx ${LIBC_PATCHED})
    add_dependencies(guest_jxr_lookup_tests jxr_lookup_patched)
    add_test(NAME guest_jxr_lookup_reference COMMAND guest_jxr_lookup_tests reference)
    add_test(NAME guest_jxr_lookup COMMAND guest_jxr_lookup_tests $<TARGET_FILE:libSceJxr> raw)
    add_test(NAME guest_jxr_lookup_patched COMMAND guest_jxr_lookup_tests ${PATCHED_DIR}/libSceJxr.prx patched)
    set_tests_properties(guest_jxr_lookup_reference guest_jxr_lookup guest_jxr_lookup_patched PROPERTIES TIMEOUT 20)
    foreach(direction IN ITEMS forward reverse)
        add_test(NAME guest_jxr_lookup_padding_${direction}
            COMMAND guest_jxr_lookup_tests $<TARGET_FILE:libSceJxr> raw ${direction})
        add_test(NAME guest_jxr_lookup_padding_${direction}_patched
            COMMAND guest_jxr_lookup_tests ${PATCHED_DIR}/libSceJxr.prx patched ${direction})
        set_tests_properties(guest_jxr_lookup_padding_${direction} guest_jxr_lookup_padding_${direction}_patched PROPERTIES TIMEOUT 20)
        if(WIN32)
            set_tests_properties(guest_jxr_lookup_padding_${direction} PROPERTIES
                ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
            set_tests_properties(guest_jxr_lookup_padding_${direction}_patched PROPERTIES
                ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:${PATCHED_DIR}")
        endif()
    endforeach()
    if(WIN32)
        set_tests_properties(guest_jxr_lookup_reference guest_jxr_lookup PROPERTIES
            ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
        set_tests_properties(guest_jxr_lookup_patched PROPERTIES
            ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:${PATCHED_DIR}")
    endif()
    add_library(jxr_tile_info_fixture STATIC ${LIBS_INCLUDE_DIR}/tests/JxrTileInfoFixture.c)
    configure_jxr_native(jxr_tile_info_fixture)

    add_library(jxr_free_tile_fixture STATIC ${LIBS_INCLUDE_DIR}/tests/JxrFreeTileFixture.c)
    configure_jxr_native(jxr_free_tile_fixture)
    add_library(jxr_bitio_pointers_fixture STATIC ${LIBS_INCLUDE_DIR}/tests/JxrBitIoPointersFixture.c)
    configure_jxr_native(jxr_bitio_pointers_fixture)

    add_library(jxr_decode_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrDecodeReference.c)
    configure_jxr_native(jxr_decode_reference)
    target_link_libraries(jxr_decode_reference PRIVATE jxr)
    add_test_executable(guest_jxr_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxr.cpp)
    target_link_libraries(guest_jxr_tests PRIVATE jxr_tile_info_fixture)
    target_link_libraries(guest_jxr_tests PRIVATE jxr_free_tile_fixture)
    target_link_libraries(guest_jxr_tests PRIVATE jxr_bitio_pointers_fixture)
    target_include_directories(guest_jxr_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_tests PRIVATE libSceJxr libc jxr_decode_reference)
    configure_windows_unwind(guest_jxr_tests)
    add_test(NAME guest_jxr COMMAND guest_jxr_tests)
    if(WIN32)
        set_tests_properties(guest_jxr PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_jxr_stream_tests ${LIBS_INCLUDE_DIR}/tests/JxrStream.cpp)
    target_include_directories(guest_jxr_stream_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_stream_tests PRIVATE libSceJxr libc libkernel)
    configure_windows_unwind(guest_jxr_stream_tests)
    add_test(NAME guest_jxr_stream COMMAND guest_jxr_stream_tests)
    set_tests_properties(guest_jxr_stream PROPERTIES TIMEOUT 30)
    add_test_executable(guest_jxr_file_stream_tests ${LIBS_INCLUDE_DIR}/tests/JxrFileStream.cpp)
    target_include_directories(guest_jxr_file_stream_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_file_stream_tests PRIVATE libSceJxr libc libkernel)
    configure_windows_unwind(guest_jxr_file_stream_tests)
    add_test(NAME guest_jxr_file_stream COMMAND guest_jxr_file_stream_tests)
    set_tests_properties(guest_jxr_file_stream PROPERTIES TIMEOUT 20)
    if(WIN32)
        set_tests_properties(guest_jxr_file_stream PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
    add_test_executable(guest_jxr_list_stream_tests ${LIBS_INCLUDE_DIR}/tests/JxrListStream.cpp)
    target_include_directories(guest_jxr_list_stream_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_list_stream_tests PRIVATE libSceJxr libc)
    configure_windows_unwind(guest_jxr_list_stream_tests)
    add_test(NAME guest_jxr_list_stream COMMAND guest_jxr_list_stream_tests)
    set_tests_properties(guest_jxr_list_stream PROPERTIES TIMEOUT 20)
    if(WIN32)
        set_tests_properties(guest_jxr_list_stream PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
    add_test_executable(guest_jxr_image_encode_tests ${LIBS_INCLUDE_DIR}/tests/JxrImageEncode.cpp)
    target_include_directories(guest_jxr_image_encode_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_image_encode_tests PRIVATE libSceJxr libc)
    configure_windows_unwind(guest_jxr_image_encode_tests)
    add_test(NAME guest_jxr_image_encode COMMAND guest_jxr_image_encode_tests)
    set_tests_properties(guest_jxr_image_encode PROPERTIES TIMEOUT 20)
    if(WIN32)
        set_tests_properties(guest_jxr_image_encode PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
    if(WIN32)
        set_tests_properties(guest_jxr_stream PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

if(BUILD_TESTING)
    add_library(jxr_transcode_fixture STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrTranscodeLayout.c)
    configure_jxr_native(jxr_transcode_fixture)
    add_test_executable(guest_jxr_glue_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrGlue.cpp)
    target_include_directories(guest_jxr_glue_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_glue_tests PRIVATE libSceJxr libc jxr jxr_transcode_fixture)
    configure_windows_unwind(guest_jxr_glue_tests)
    add_test(NAME guest_jxr_glue COMMAND guest_jxr_glue_tests)
    set_tests_properties(guest_jxr_glue PROPERTIES TIMEOUT 20)
    if(WIN32)
        set_tests_properties(guest_jxr_glue PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

if(BUILD_TESTING)
    add_library(jxr_quantizer_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrQuantizerReference.c)
    configure_jxr_native(jxr_quantizer_reference)
    target_link_libraries(jxr_quantizer_reference PRIVATE jxr)
    add_test_executable(guest_jxr_quantizer_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrQuantizer.cpp)
    target_include_directories(guest_jxr_quantizer_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_quantizer_tests PRIVATE libSceJxr libc jxr_quantizer_reference)
    configure_windows_unwind(guest_jxr_quantizer_tests)
    add_test(NAME guest_jxr_quantizer COMMAND guest_jxr_quantizer_tests)
    set_tests_properties(guest_jxr_quantizer PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_jxr_quantizer PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

if(BUILD_TESTING)
    add_library(jxr_bitio_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrBitIoReference.c)
    configure_jxr_native(jxr_bitio_reference)
    target_link_libraries(jxr_bitio_reference PRIVATE jxr)
    add_test_executable(guest_jxr_bitio_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrBitIo.cpp)
    target_include_directories(guest_jxr_bitio_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_bitio_tests PRIVATE libSceJxr libc jxr_bitio_reference)
    configure_windows_unwind(guest_jxr_bitio_tests)
    add_test(NAME guest_jxr_bitio COMMAND guest_jxr_bitio_tests)
    set_tests_properties(guest_jxr_bitio PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_jxr_bitio PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

if(BUILD_TESTING)
    add_library(jxr_predict_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrPredictReference.c)
    configure_jxr_native(jxr_predict_reference)
    target_link_libraries(jxr_predict_reference PRIVATE jxr)
    add_test_executable(guest_jxr_predict_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrPredict.cpp)
    target_include_directories(guest_jxr_predict_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_predict_tests PRIVATE libSceJxr libc jxr_predict_reference)
    configure_windows_unwind(guest_jxr_predict_tests)
    add_test(NAME guest_jxr_predict COMMAND guest_jxr_predict_tests)
    set_tests_properties(guest_jxr_predict PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_jxr_predict PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

if(BUILD_TESTING)
    add_library(jxr_wmp_roundtrip_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrWmpRoundtripReference.c)
    configure_jxr_native(jxr_wmp_roundtrip_reference)
    target_include_directories(jxr_wmp_roundtrip_reference PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(jxr_wmp_roundtrip_reference PRIVATE jxr)
    add_test_executable(guest_jxr_wmp_roundtrip_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrWmpRoundtrip.cpp)
    target_include_directories(guest_jxr_wmp_roundtrip_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_wmp_roundtrip_tests PRIVATE libSceJxr libc jxr_wmp_roundtrip_reference)
    configure_windows_unwind(guest_jxr_wmp_roundtrip_tests)
    add_test(NAME guest_jxr_wmp_roundtrip COMMAND guest_jxr_wmp_roundtrip_tests)
    set_tests_properties(guest_jxr_wmp_roundtrip PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_jxr_wmp_roundtrip PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

if(BUILD_TESTING)
    add_library(jxr_wmp_lossy_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrWmpLossyFormatsReference.c)
    configure_jxr_native(jxr_wmp_lossy_reference)
    target_include_directories(jxr_wmp_lossy_reference PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(jxr_wmp_lossy_reference PRIVATE jxr)
    add_test_executable(guest_jxr_wmp_lossy_formats_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrWmpLossyFormats.cpp)
    target_include_directories(guest_jxr_wmp_lossy_formats_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_wmp_lossy_formats_tests PRIVATE libSceJxr libc jxr_wmp_lossy_reference)
    configure_windows_unwind(guest_jxr_wmp_lossy_formats_tests)
    add_test(NAME guest_jxr_wmp_lossy_formats COMMAND guest_jxr_wmp_lossy_formats_tests)
    set_tests_properties(guest_jxr_wmp_lossy_formats PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_jxr_wmp_lossy_formats PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

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

if(BUILD_TESTING)
    add_library(jxr_simple_bit_io_reference STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrSimpleBitIoReference.c)
    configure_jxr_native(jxr_simple_bit_io_reference)
    target_link_libraries(jxr_simple_bit_io_reference PRIVATE jxr)
    add_test_executable(guest_jxr_simple_bit_io_tests ${LIBS_INCLUDE_DIR}/tests/JxrSimpleBitIo.cpp)
    target_include_directories(guest_jxr_simple_bit_io_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_simple_bit_io_tests PRIVATE libSceJxr libc jxr_simple_bit_io_reference)
    configure_windows_unwind(guest_jxr_simple_bit_io_tests)
    add_test(NAME guest_jxr_simple_bit_io COMMAND guest_jxr_simple_bit_io_tests)
    set_tests_properties(guest_jxr_simple_bit_io PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_jxr_simple_bit_io PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()

if(BUILD_TESTING)
    add_library(jxr_bitioinfo_fixture STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrBitIOInfoFixture.c)
    configure_jxr_native(jxr_bitioinfo_fixture)
    add_test_executable(guest_jxr_bitioinfo_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrBitIOInfo.cpp)
    target_include_directories(guest_jxr_bitioinfo_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_bitioinfo_tests PRIVATE libSceJxr libc jxr_bitioinfo_fixture)
    configure_windows_unwind(guest_jxr_bitioinfo_tests)
    add_test(NAME guest_jxr_bitioinfo COMMAND guest_jxr_bitioinfo_tests)
    set_tests_properties(guest_jxr_bitioinfo PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_jxr_bitioinfo PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()

    add_library(jxr_decode_fixture STATIC ${LIBS_INCLUDE_DIR}/tests/GuestJxrDecodeCreateFixture.c)
    configure_jxr_native(jxr_decode_fixture)
    target_include_directories(jxr_decode_fixture PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(jxr_decode_fixture PRIVATE jxr)
    add_test_executable(guest_jxr_decode_create_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxrDecodeCreate.cpp)
    target_include_directories(guest_jxr_decode_create_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_decode_create_tests PRIVATE libSceJxr libc jxr_decode_fixture)
    configure_windows_unwind(guest_jxr_decode_create_tests)
    add_test(NAME guest_jxr_decode_create COMMAND guest_jxr_decode_create_tests)
    set_tests_properties(guest_jxr_decode_create PROPERTIES TIMEOUT 30)
    if(WIN32)
        set_tests_properties(guest_jxr_decode_create PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()
