add_library(jxr_lookup_native STATIC src/NativeLookup.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/PixelFormatLookup.cpp)
target_link_libraries(libSceJxr PRIVATE jxr_lookup_native)
if(NOT APPLE)
    target_link_options(libSceJxr PRIVATE LINKER:--exclude-libs,ALL)
endif()
target_include_directories(libSceJxr PRIVATE ${LIBS_INCLUDE_DIR})
configure_windows_unwind(libSceJxr)

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 ${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_test_executable(guest_jxr_tests ${LIBS_INCLUDE_DIR}/tests/GuestJxr.cpp)
    target_include_directories(guest_jxr_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_jxr_tests PRIVATE libSceJxr libc)
    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)
    if(WIN32)
        set_tests_properties(guest_jxr_stream PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceJxr>")
    endif()
endif()
