cmake_minimum_required(VERSION 3.20)
project(DemonsSoulsNativeTests LANGUAGES C CXX)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
if(NOT EXISTS "${PS3RECOMP_DIR}/runtime/ppu/ppu_context.h")
    message(FATAL_ERROR "Pass -DPS3RECOMP_DIR=<SDK with patches/ps3recomp-local.patch applied>")
endif()
enable_testing()
option(DES_TEST_SANITIZERS "Check native tests with AddressSanitizer and UndefinedBehaviorSanitizer" OFF)
if(DES_TEST_SANITIZERS)
    add_compile_options(-fsanitize=address,undefined -fno-omit-frame-pointer)
    add_link_options(-fsanitize=address,undefined)
endif()
find_package(Threads REQUIRED)
add_executable(test_guest_setjmp test_guest_setjmp.cpp ../../project/ds_guest_setjmp.cpp)
target_include_directories(test_guest_setjmp PRIVATE "${PS3RECOMP_DIR}/runtime/ppu")
target_compile_options(test_guest_setjmp PRIVATE -UNDEBUG -fno-omit-frame-pointer)
target_link_libraries(test_guest_setjmp PRIVATE Threads::Threads)
add_test(NAME guest_setjmp COMMAND test_guest_setjmp)

set(VIDEO "${PS3RECOMP_DIR}/libs/video")
set(VERTEX "${VIDEO}/rsx_vertex_compact.c" "${VIDEO}/rsx_dispatch.c")
add_executable(test_vertex_fetch test_vertex.c ${VERTEX})
target_link_libraries(test_vertex_fetch PRIVATE Threads::Threads)
add_executable(test_subchannels test_subchannels.c engine_host_stubs.c ${VERTEX}
    "${VIDEO}/rsx_draw_engine.c" "${VIDEO}/rsx_texture_layout.c"
    "${VIDEO}/rsx_vp_decompiler.c" "${VIDEO}/rsx_fp_decompiler.c")
foreach(target test_vertex_fetch test_subchannels)
    target_include_directories(${target} PRIVATE "${PS3RECOMP_DIR}/include" "${VIDEO}" "${VIDEO}/tests")
    target_compile_options(${target} PRIVATE -UNDEBUG -fno-strict-aliasing -ffp-contract=off)
    add_test(NAME ${target} COMMAND ${target})
endforeach()
find_package(Python3 3.10 COMPONENTS Interpreter REQUIRED)
add_test(NAME interrupted_lift COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/test_patch_sync.py")
add_test(NAME play_configuration COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/test_play.py")
add_test(NAME spu_job_import COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/test_spu_jobs.py" "${PS3RECOMP_DIR}")

set(SPU "${PS3RECOMP_DIR}/runtime/spu")
add_executable(test_spu_elf test_spu_elf.c "${SPU}/spu_workload.c")
add_executable(test_spu_job_bounds test_spu_job_bounds.c "${SPU}/spurs_job.c")
foreach(target test_spu_elf test_spu_job_bounds)
    target_include_directories(${target} PRIVATE "${SPU}" "${PS3RECOMP_DIR}/include")
    target_compile_options(${target} PRIVATE -UNDEBUG -ffunction-sections -fdata-sections)
    target_link_libraries(${target} PRIVATE Threads::Threads)
    if(APPLE)
        target_link_options(${target} PRIVATE -Wl,-dead_strip)
    else()
        target_link_options(${target} PRIVATE -Wl,--gc-sections)
    endif()
    add_test(NAME ${target} COMMAND ${target})
endforeach()

foreach(mode neon scalar scalar_int)
    add_executable(test_spu_int_float_${mode} test_spu_int_float.c)
    target_include_directories(test_spu_int_float_${mode} PRIVATE "${SPU}")
    target_compile_options(test_spu_int_float_${mode} PRIVATE -UNDEBUG)
    target_link_libraries(test_spu_int_float_${mode} PRIVATE m)
    add_test(NAME spu_int_float_${mode} COMMAND test_spu_int_float_${mode})
endforeach()
target_compile_definitions(test_spu_int_float_scalar PRIVATE SPU_SCALAR_HELPERS=1)
target_compile_definitions(test_spu_int_float_scalar_int PRIVATE SPU_SCALAR_INT=1)

add_executable(test_taskset_lifecycle test_taskset_lifecycle.c
    "${PS3RECOMP_DIR}/runtime/platform/win32_compat.c")
add_executable(test_spu_thread_group test_spu_thread_group.c
    "${SPU}/spu_workload.c" "${SPU}/spu_lifted_thread.c"
    "${PS3RECOMP_DIR}/runtime/syscalls/spu_fallback.c"
    "${PS3RECOMP_DIR}/runtime/syscalls/sdk_version_weak.c"
    "${PS3RECOMP_DIR}/runtime/platform/win32_compat.c")
foreach(unit spu_channels spu_drain spu_lockstep spu_coherency spurs_policy spurs_job
             spu_tsp_weak spu_vm_pagemap spurs_policy_blob_weak spu_interp)
    target_sources(test_taskset_lifecycle PRIVATE "${SPU}/${unit}.c")
    target_sources(test_spu_thread_group PRIVATE "${SPU}/${unit}.c")
endforeach()
foreach(target test_taskset_lifecycle test_spu_thread_group)
    target_include_directories(${target} PRIVATE "${PS3RECOMP_DIR}/include"
        "${PS3RECOMP_DIR}/tests" "${SPU}/tests" "${SPU}" "${PS3RECOMP_DIR}/runtime/platform")
    target_compile_options(${target} PRIVATE -UNDEBUG -ffunction-sections -fdata-sections)
    target_link_libraries(${target} PRIVATE Threads::Threads m)
    if(APPLE)
        target_link_options(${target} PRIVATE -Wl,-dead_strip)
    else()
        target_link_options(${target} PRIVATE -Wl,--gc-sections)
    endif()
endforeach()
add_test(NAME spu_thread_group COMMAND test_spu_thread_group)
set_tests_properties(spu_thread_group PROPERTIES TIMEOUT 30)
foreach(scenario shutdown queued rejected exit_codes join_wait pending_bits
                 wake_latches wake_handshake parked_registry signal_wait)
    add_test(NAME taskset_${scenario} COMMAND test_taskset_lifecycle ${scenario})
    set_tests_properties(taskset_${scenario} PROPERTIES TIMEOUT 15)
endforeach()

foreach(scenario stop_plain stop_code stop_nested stop_policy stop_resident
                 stop_receive stop_receive_try stop_receive_empty)
    add_test(NAME spu_${scenario} COMMAND test_taskset_lifecycle ${scenario})
    set_tests_properties(spu_${scenario} PROPERTIES TIMEOUT 15)
endforeach()
