# PC port build.
#
#   recvx_game     : the original game + platform code from the decomp, compiled
#                    unmodified except for '#ifdef TARGET_PC' asm replacements.
#   recvx_platform : the new PC abstraction layer (PS2 hardware/IOP emulation at
#                    library boundaries, renderer, audio, input, overlay).
#   recvx          : the executable.

set(DECOMP ${RECVX_DECOMP_DIR})
# Floating point and char as on the reference build (Linux i686) on every architecture (docs/ARCHITECTURE.md
# "Determinism across architectures"): no fused multiply-add (GCC and Clang fuse a * b + c where the target has FMA,
# e.g. arm64), char signed (arm64's is unsigned); on x86 SSE arithmetic, not the x87's extended precision.
# Every build computes the libm's float functions with pc/src/host/pc_math.c (pc_math.h). RECVX_VU_PORTABLE: the VU's
# plain C path in place of SSE2 / NEON (a test of that path: identity_check against the i686 build).
include(CheckSymbolExists)
check_symbol_exists(__i386__ "" RECVX_X86_32)
check_symbol_exists(__x86_64__ "" RECVX_X86_64)
set(RECVX_FP_FLAGS -ffp-contract=off -fsigned-char)
if(RECVX_X86_32 OR RECVX_X86_64)
    list(APPEND RECVX_FP_FLAGS -msse2 -mfpmath=sse)
endif()
option(RECVX_VU_PORTABLE "the VU's plain C path in place of SSE2 / NEON (test)" OFF)
if(RECVX_VU_PORTABLE)
    add_compile_definitions(RECVX_VU_PORTABLE)
endif()
add_compile_definitions(_FILE_OFFSET_BITS=64)   # disc images over 2 GB (cdvd.c fseeko); mingw-w64 honours it too
if(WIN32)
    # the crash log walks the EBP frame chain (host/log.c); measured on Linux: +1..5% game logic
    add_compile_options(-fno-omit-frame-pointer)
endif()
set(PROG ${DECOMP}/src/ps2/veronica/prog)
set(KATANA_INC ${DECOMP}/include/recvx-decomp-katana/KATANA/Include)

# SDL3 is built from source and linked statically by default, so the binary does not
# depend on a (32-bit) SDL3 package being installed. SDL still dlopen()s the system's
# X11/Wayland/audio libraries at runtime.
option(RECVX_VENDORED_SDL "Build SDL3 from source and link it statically" ON)
if(RECVX_VENDORED_SDL)
    include(FetchContent)
    set(SDL_SHARED OFF CACHE BOOL "" FORCE)
    set(SDL_STATIC ON CACHE BOOL "" FORCE)
    set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
    set(SDL_EXAMPLES OFF CACHE BOOL "" FORCE)
    FetchContent_Declare(SDL3 GIT_REPOSITORY https://github.com/libsdl-org/SDL.git GIT_TAG release-3.4.16 GIT_SHALLOW TRUE)
    FetchContent_MakeAvailable(SDL3)
else()
    find_package(SDL3 REQUIRED CONFIG)
endif()

# Dear ImGui (MIT) for the F1 overlay, with its SDL3 + OpenGL3 backends.
include(FetchContent)
FetchContent_Declare(imgui GIT_REPOSITORY https://github.com/ocornut/imgui.git GIT_TAG v1.92.9b GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(imgui)
add_library(imgui STATIC
    ${imgui_SOURCE_DIR}/imgui.cpp ${imgui_SOURCE_DIR}/imgui_draw.cpp
    ${imgui_SOURCE_DIR}/imgui_tables.cpp ${imgui_SOURCE_DIR}/imgui_widgets.cpp
    ${imgui_SOURCE_DIR}/backends/imgui_impl_sdl3.cpp ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp)
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends)
target_link_libraries(imgui PUBLIC SDL3::SDL3)
target_compile_options(imgui PRIVATE -ffp-contract=off)   # (the menus' layout as on x86)

# Co-op host port mapping (src/net/portmap.c): miniupnpc (UPnP IGD) and libnatpmp (NAT-PMP),
# both BSD, static, Winsock on Windows.
set(UPNPC_BUILD_SHARED OFF CACHE BOOL "" FORCE)
set(UPNPC_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(UPNPC_BUILD_SAMPLE OFF CACHE BOOL "" FORCE)
set(UPNPC_NO_INSTALL ON CACHE BOOL "" FORCE)
FetchContent_Declare(miniupnpc GIT_REPOSITORY https://github.com/miniupnp/miniupnp.git GIT_TAG miniupnpc_2_3_3
                     GIT_SHALLOW TRUE SOURCE_SUBDIR miniupnpc EXCLUDE_FROM_ALL)
FetchContent_Declare(libnatpmp GIT_REPOSITORY https://github.com/miniupnp/libnatpmp.git
                     GIT_TAG 134fc89e2781e154e40042641f4d8bcbe42579f1 EXCLUDE_FROM_ALL)
FetchContent_MakeAvailable(miniupnpc libnatpmp)

# zstd (BSD-3-Clause): only its decompressor, for Wii disc images in Dolphin's RVZ format (src/import/rvz.c)
FetchContent_Declare(zstd GIT_REPOSITORY https://github.com/facebook/zstd.git GIT_TAG v1.5.7 GIT_SHALLOW TRUE SOURCE_SUBDIR none)
FetchContent_MakeAvailable(zstd)
add_library(zstd_dec STATIC
    ${zstd_SOURCE_DIR}/lib/common/debug.c ${zstd_SOURCE_DIR}/lib/common/entropy_common.c ${zstd_SOURCE_DIR}/lib/common/error_private.c
    ${zstd_SOURCE_DIR}/lib/common/fse_decompress.c ${zstd_SOURCE_DIR}/lib/common/xxhash.c ${zstd_SOURCE_DIR}/lib/common/zstd_common.c
    ${zstd_SOURCE_DIR}/lib/decompress/huf_decompress.c ${zstd_SOURCE_DIR}/lib/decompress/zstd_ddict.c
    ${zstd_SOURCE_DIR}/lib/decompress/zstd_decompress.c ${zstd_SOURCE_DIR}/lib/decompress/zstd_decompress_block.c)
target_include_directories(zstd_dec PUBLIC ${zstd_SOURCE_DIR}/lib)
target_compile_definitions(zstd_dec PRIVATE ZSTD_DISABLE_ASM ZSTD_LEGACY_SUPPORT=0 XXH_NAMESPACE=ZSTD_)

# Co-op server (pc/server: recvx-server) and Monocypher, which the game shares for the
# end-to-end encryption of server sessions (src/net/rdv_proto.c)
add_subdirectory(server)

# everything from here on (the game, the platform layer, the tests): RECVX_FP_FLAGS
add_compile_options(${RECVX_FP_FLAGS})
# OpenGL ES 3.0 in place of desktop OpenGL 3.3 core by default (Android: always): host.c asks SDL for an ES context;
# elsewhere RECVX_GLES=0/1 or --gles choose at run time (gs_gl.c gs_gl_es_wanted, docs/ARCHITECTURE.md "OpenGL ES")
option(RECVX_GLES "OpenGL ES 3.0 (Android)" ${ANDROID})
if(RECVX_GLES)
    add_compile_definitions(RECVX_GLES)
endif()
if(ANDROID)
    add_compile_options(-Wno-error=format-security)   # (the NDK's default -Werror: our texts' translations are formats, tr())
endif()

# ---------------------------------------------------------------- headers
# The Katana SDK headers are included with inconsistent case (SG_MWPLY.H vs
# sg_mwply.h). Mirror them under both spellings for case-sensitive filesystems.
set(CASEFOLD_DIR ${CMAKE_BINARY_DIR}/katana_casefold)
file(GLOB KATANA_HEADERS RELATIVE ${KATANA_INC} ${KATANA_INC}/*)
foreach(h ${KATANA_HEADERS})
    string(TOLOWER ${h} lo)
    string(TOUPPER ${h} up)
    foreach(n ${lo} ${up})
        if(NOT n STREQUAL h AND NOT EXISTS ${KATANA_INC}/${n} AND NOT EXISTS ${CASEFOLD_DIR}/${n})
            file(MAKE_DIRECTORY ${CASEFOLD_DIR})
            file(CREATE_LINK ${KATANA_INC}/${h} ${CASEFOLD_DIR}/${n} COPY_ON_ERROR SYMBOLIC)
        endif()
    endforeach()
endforeach()

# ---------------------------------------------------------------- game (decomp)
file(GLOB GAME_SOURCES ${PROG}/*.c)
set(GAME_EXCLUDE
    gcc_wrapper.c        # ee-gcc soft-float/int64 runtime helpers
    ps2_MovieFunc.c      # libmpeg/IPU movie player -> pc/src/movie
    ps2_MovieWork.c
    ps2_Vu1Scissor2.c    # #included by ps2_Vu1Strip.c
)
foreach(x ${GAME_EXCLUDE})
    list(REMOVE_ITEM GAME_SOURCES ${PROG}/${x})
endforeach()

# PC implementations of decomp-declared APIs that need the decomp's types
file(GLOB GLUE_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/glue/*.c)

add_library(recvx_game OBJECT ${GAME_SOURCES} ${GLUE_SOURCES})
target_compile_definitions(recvx_game PRIVATE main=recvx_game_main)
target_include_directories(recvx_game PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/include/sdk
    ${CMAKE_CURRENT_SOURCE_DIR}/src/ps2
    ${DECOMP}/src/cri/mwlib/ee/lib/libadxe
    ${PROG}
)
# Katana/CRI dirs ship libc-named headers (math.h, stddef.h, ...); search them
# after the host's system headers.
target_compile_options(recvx_game PRIVATE
    "SHELL:-idirafter ${KATANA_INC}"
    "SHELL:-idirafter ${CASEFOLD_DIR}"
    "SHELL:-idirafter ${DECOMP}/include/recvx-decomp-cri/cri/mwlib/include"
    "SHELL:-idirafter ${DECOMP}/include/recvx-decomp-cri/cri/mwlib/ee/include"
    "SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/include/pc_prelude.h"
    -std=gnu11
    -fno-strict-aliasing
    -fwrapv
    ${RECVX_FP_FLAGS}         # EE char is signed; no FMA fusion: the PS2's operation order; x86: IEEE single precision, no x87 extended intermediates
    -fno-tree-vectorize
    -ftrivial-auto-var-init=zero  # deterministic stand-in for MWCC's stale registers (docs/dev/uninitialized-locals.txt)
)
# the decomp is compiled as is, without warnings (-w), but a call to an undeclared function stays an
# error: without a prototype a float argument goes as a double and a char/float return is read as an
# int. -w also hides -Werror=..., so this relies on GCC 14's permerror (no -fpermissive here; the other
# permerrors the decomp needs are switched off one by one). GCC 13 (CI) only warns: the ctest
# decomp_declarations checks it there. Missing returns and uninitialized locals are listed by
# pc/tools/decomp_warnings.py. The port's own glue gets warnings.
set_source_files_properties(${GAME_SOURCES} PROPERTIES COMPILE_OPTIONS
    "-w;-Wno-incompatible-pointer-types;-Wno-int-conversion;-Wno-return-mismatch")
set_source_files_properties(${GLUE_SOURCES} PROPERTIES COMPILE_OPTIONS "-Wall;-Wno-unused-function")
if(WIN32)
    # GCC (not MSVC) bitfield layout, as on Linux and the EE
    target_compile_options(recvx_game PRIVATE -mno-ms-bitfields)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT RECVX_64BIT)
    # the executable is non-PIE (fixed address window); position-independent code only costs
    # get_pc_thunk calls on x86-32. 64-bit: no window, the default PIE
    target_compile_options(recvx_game PRIVATE -fno-pie)
endif()

# ---------------------------------------------------------------- platform layer
file(GLOB_RECURSE PLATFORM_SOURCES CONFIGURE_DEPENDS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
if(NOT ANDROID)   # (the Android app's glue: lifecycle, the disc image through the system's file picker, logcat)
    list(FILTER PLATFORM_SOURCES EXCLUDE REGEX "/src/host/(android|biorand_zip)\\.c$")   # (and Import BioRand Pack: the desktop generates packs)
endif()
add_library(recvx_platform OBJECT ${PLATFORM_SOURCES})
# xxHash (BSD-2-Clause, header only): XXH3, the hash PCSX2 names its texture replacements by (src/gs/hdpack.c)
include(FetchContent)
FetchContent_Declare(xxhash GIT_REPOSITORY https://github.com/Cyan4973/xxHash.git GIT_TAG v0.8.3 GIT_SHALLOW TRUE SOURCE_SUBDIR none)
# bcdec (MIT / Unlicense, header only): BC1-BC7 decoding for the replacements' content check
FetchContent_Declare(bcdec GIT_REPOSITORY https://github.com/iOrange/bcdec.git GIT_TAG 80859ed3b7afb1c527a2a99d70c61457bea72d0c SOURCE_SUBDIR none)
FetchContent_MakeAvailable(xxhash bcdec)
target_include_directories(recvx_platform PRIVATE ${xxhash_SOURCE_DIR} ${bcdec_SOURCE_DIR})
# the render thread's per-vertex loops (GS emulation, native strips, interpolation): -O3 (IEEE as at
# -O2: no fast-math, -ffp-contract=off)
set_property(SOURCE src/gs/gs_gl.c src/gs/stream.c src/gs/gs_mem.c src/gs/textures.c APPEND PROPERTY COMPILE_OPTIONS -O3)
# Android: ImGui's GL backend without its desktop loader (imgl3w opens libGL/libEGL.so.1): it calls libGLESv3
if(ANDROID)
    target_compile_definitions(imgui PUBLIC IMGUI_IMPL_OPENGL_ES3)
    target_link_libraries(imgui PUBLIC GLESv3)
endif()
set_property(SOURCE src/host/host.c src/host/overlay.cpp src/net/update.c APPEND PROPERTY COMPILE_DEFINITIONS "RECVX_VERSION=\"${RECVX_VERSION}\"")
# our texts' table: its text goes into the executable as it is, outside the address window (tr.c, res/recvx.rc)
set_property(SOURCE src/host/tr.c APPEND PROPERTY COMPILE_DEFINITIONS "TR_TABLE_INC=\"${CMAKE_CURRENT_SOURCE_DIR}/src/host/tr_table.inc\"")
set_property(SOURCE src/host/tr.c APPEND PROPERTY OBJECT_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/host/tr_table.inc)
# the changelog the build carries (WHAT'S NEW at the title: changelog.c beside the table; Windows: res/recvx.rc)
set_property(SOURCE src/host/changelog.c APPEND PROPERTY COMPILE_DEFINITIONS "CHANGELOG_MD=\"${PROJECT_SOURCE_DIR}/CHANGELOG.md\"")
set_property(SOURCE src/host/changelog.c APPEND PROPERTY OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/CHANGELOG.md)
target_include_directories(recvx_platform PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/include/sdk
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/src/ps2
)
target_compile_options(recvx_platform PRIVATE
    $<$<COMPILE_LANGUAGE:C>:-std=gnu11>
    -fno-strict-aliasing -fwrapv ${RECVX_FP_FLAGS}
    -Wall -Wno-unused-function
)
target_link_libraries(recvx_platform PRIVATE SDL3::SDL3 imgui miniupnpc::miniupnpc natpmp monocypher zstd_dec)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT RECVX_64BIT)
    target_compile_options(recvx_platform PRIVATE -fno-pie)
endif()
if(WIN32)
    target_compile_options(recvx_platform PRIVATE $<$<COMPILE_LANGUAGE:C>:-mno-ms-bitfields>)
endif()

# ---------------------------------------------------------------- executable
set(RECVX_LAYOUT_PAD 0 CACHE STRING "test aid: bytes of padding before all code, data and bss (pc/tools/layout_check.sh)")
if(RECVX_LAYOUT_PAD)
    set(LAYOUT_PAD_SRC tests/layout_pad.c)
    set_source_files_properties(tests/layout_pad.c PROPERTIES COMPILE_DEFINITIONS RECVX_LAYOUT_PAD=${RECVX_LAYOUT_PAD})
endif()
if(WIN32)
    enable_language(RC)
    set(RECVX_RES res/recvx.rc)   # the program icon, our texts' table, the changelog
    set_source_files_properties(res/recvx.rc PROPERTIES OBJECT_DEPENDS
                                "${CMAKE_CURRENT_SOURCE_DIR}/res/recvx-enhanced.ico;${CMAKE_CURRENT_SOURCE_DIR}/src/host/tr_table.inc;${PROJECT_SOURCE_DIR}/CHANGELOG.md")
endif()
if(ANDROID)   # the game is libmain.so, which SDL's Java activity loads and calls SDL_main in (android/, docs/ANDROID.md)
    add_library(recvx SHARED $<TARGET_OBJECTS:recvx_game> $<TARGET_OBJECTS:recvx_platform>)
    set_target_properties(recvx PROPERTIES OUTPUT_NAME main)
    target_link_libraries(recvx PRIVATE log android GLESv3)   # logcat (log.c), the window (SDL), the overlay's GL (ImGui: ES 3 on Android)
    # 16 KB pages (Android 15 devices): every segment aligned for them (NDK r28's default, stated). The read-only data
    # in a segment of its own, which memory.c makes writable (the game writes into it): the NDK links below API 30
    # with --no-rosegment (read-only data in the code's segment; only the system unwinder of Android 10 cared)
    target_link_options(recvx PRIVATE -Wl,-z,max-page-size=16384 -Wl,-z,common-page-size=16384 -Wl,--rosegment)
else()
add_executable(recvx ${LAYOUT_PAD_SRC} ${RECVX_RES} $<TARGET_OBJECTS:recvx_game> $<TARGET_OBJECTS:recvx_platform>
               $<$<TARGET_EXISTS:glibc_compat>:$<TARGET_OBJECTS:glibc_compat>>)
endif()
target_link_libraries(recvx PRIVATE imgui SDL3::SDL3 miniupnpc::miniupnpc natpmp monocypher zstd_dec m)
if(WIN32)
    target_link_libraries(recvx PRIVATE ws2_32 pthread shell32 dwmapi)   # co-op UDP (src/net); clock_gettime (winpthreads); borderless corners (settings.c)
    target_link_libraries(recvx PRIVATE winhttp)   # the BioRand CLI download (src/net/biorand_dl.c), a system DLL
    # Same address window as Linux: fixed image base, no ASLR relocation; large-address-aware
    # gives the 32-bit exe 4 GB on 64-bit Windows (Linux already uses addresses above 2 GB). -static: libstdc++,
    # libgcc and winpthread go into the exe, only system DLLs (UCRT, opengl32, ...) remain.
    if(RECVX_64BIT)   # no window on 64-bit: a relocatable image anywhere
        target_link_options(recvx PRIVATE -static -Wl,--stack,8388608)
    else()
        target_link_options(recvx PRIVATE -static -Wl,--image-base,0x30000000 -Wl,--disable-dynamicbase -Wl,--large-address-aware
                            -Wl,--stack,8388608)
    endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND RECVX_64BIT)
    # no window on 64-bit (docs/ARCHITECTURE.md "64-bit: pointers in the game's data"): a PIE anywhere
    target_link_options(recvx PRIVATE -static-libgcc -static-libstdc++)
    if(NOT RECVX_X86_32 AND NOT RECVX_X86_64)   # the read-only data in a segment of its own (binutils' default on x86 only): memory.c makes it writable
        target_link_options(recvx PRIVATE -Wl,-z,separate-code)
    endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    # Place the image inside the PS2 address window (see docs/ARCHITECTURE.md, "Data model and memory").
    # Our texts' table (tr.c, section .trtab) right above the window: not game memory.
    target_link_options(recvx PRIVATE -no-pie -Wl,-Ttext-segment=0x30000000 -Wl,--section-start=.trtab=0x40000000 -static-libgcc -static-libstdc++)
    set_target_properties(recvx PROPERTIES POSITION_INDEPENDENT_CODE OFF)
endif()

# ---------------------------------------------------------------- tests
add_executable(test_vu tests/test_vu.c src/ps2/vu.c)
target_compile_options(test_vu PRIVATE -std=gnu11 ${RECVX_FP_FLAGS})
target_link_libraries(test_vu PRIVATE m)
add_test(NAME vu COMMAND test_vu)
# the same against the plain C (non-SSE) native paths, as an arm64 build would compile them
add_executable(test_vu_portable tests/test_vu.c src/ps2/vu.c)
target_compile_definitions(test_vu_portable PRIVATE RECVX_VU_PORTABLE)
target_compile_options(test_vu_portable PRIVATE -std=gnu11 ${RECVX_FP_FLAGS})
target_link_libraries(test_vu_portable PRIVATE m)
add_test(NAME vu_portable COMMAND test_vu_portable)

# pc/src/host/pc_math.c, the libm's float functions in plain C: the i686 libm's bits on any architecture
# (docs/ARCHITECTURE.md "Determinism across architectures"; also run under the CMAKE_CROSSCOMPILING_EMULATOR)
add_executable(test_pc_math tests/test_pc_math.c src/host/pc_math.c)
target_include_directories(test_pc_math PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_options(test_pc_math PRIVATE -std=gnu11 -fno-builtin)
target_link_libraries(test_pc_math PRIVATE m)
add_test(NAME pc_math COMMAND test_pc_math)

# decodes every movie on the disc (needs RECVX_ISO; skipped otherwise)
add_executable(test_movies tests/test_movies.c src/movie/mpeg2.c src/movie/pss.c src/host/pc_math.c)
target_compile_options(test_movies PRIVATE -std=gnu11 -O2)
target_link_libraries(test_movies PRIVATE m)
add_test(NAME movies COMMAND test_movies)
set_tests_properties(movies PROPERTIES SKIP_RETURN_CODE 77)

# calls to undeclared functions in the decomp: GCC 14+ stops the build on them, GCC 13 only warns (and -w hides it)
find_package(Python3 COMPONENTS Interpreter)
if(Python3_FOUND AND CMAKE_GENERATOR STREQUAL "Ninja")
    add_test(NAME decomp_declarations COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/decomp_warnings.py --declarations ${CMAKE_BINARY_DIR})
endif()
# asm2c's operand registers as C pointers (tools/asm2c_ptr.py)
if(Python3_FOUND)
    add_test(NAME asm2c_ptr COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/asm2c_ptr.py --selftest)
endif()

# control bindings (pc/src/input/bindings.c): defaults, rebinding, ini, a virtual SDL gamepad
add_executable(test_bindings tests/test_bindings.c src/input/bindings.c src/input/weapons.c)
target_compile_options(test_bindings PRIVATE $<$<COMPILE_LANGUAGE:C>:-std=gnu11>)
target_link_libraries(test_bindings PRIVATE SDL3::SDL3)
add_test(NAME bindings COMMAND test_bindings)

# modern controls' direction math (pc/src/input/modern.c): camera + stick -> angle, the cut hold
add_executable(test_modern tests/test_modern.c src/input/modern.c src/host/pc_math.c)
target_compile_options(test_modern PRIVATE -std=gnu11)
target_link_libraries(test_modern PRIVATE m)
add_test(NAME modern COMMAND test_modern)

# pure rules of the PC hooks (pc/include/pc_rules.h, src/gs/native.h): added enemies' helper code and holders,
# dual weapons' second target, hor+ margin meshes and clipping, warp music, "Unlock all", co-op inventory blocks;
# with the game's flags and types
add_executable(test_rules tests/test_rules.c)
target_include_directories(test_rules PRIVATE $<TARGET_PROPERTY:recvx_game,INCLUDE_DIRECTORIES>)
target_compile_options(test_rules PRIVATE $<TARGET_PROPERTY:recvx_game,COMPILE_OPTIONS>)
target_link_libraries(test_rules PRIVATE m)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT RECVX_64BIT)
    target_link_options(test_rules PRIVATE -no-pie)   # (the game's flags include -fno-pie)
endif()
add_test(NAME rules COMMAND test_rules)

# the new menu's character grid for text fields (src/host/textgrid.h): its keys, moves, field rules, the lobby's READY note
add_executable(test_textgrid tests/test_textgrid.c)
add_test(NAME textgrid COMMAND test_textgrid)

# every recvx.ini key has a row in the settings table (F1 and OPTION > ENHANCED SETTINGS) or is listed
add_test(NAME settings COMMAND ${CMAKE_COMMAND} -DSRC=${CMAKE_CURRENT_SOURCE_DIR} -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/check_settings.cmake)
# the new menu's tree (src/host/menu_layout.c): every settings-table row placed exactly once; and that the check catches a broken one
add_test(NAME menu_layout COMMAND ${CMAKE_COMMAND} -DSRC=${CMAKE_CURRENT_SOURCE_DIR} -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/check_menu_layout.cmake)
add_test(NAME menu_layout_selftest COMMAND ${CMAKE_COMMAND} -DSRC=${CMAKE_CURRENT_SOURCE_DIR} -DSELFTEST=1 -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/check_menu_layout.cmake)

# native texture queue (pc/src/gs/textures.c): superseded uploads never written, the rest only when read
add_executable(test_ntex tests/test_ntex.c src/gs/textures.c)
target_compile_options(test_ntex PRIVATE -std=gnu11)
add_test(NAME ntex COMMAND test_ntex)

# GS memory (pc/src/gs/gs_mem.c): texture decodes and CLUT loads against per-pixel reads
# (it #includes gs_mem.c: built with the game's flags for it, -O3 as above)
add_executable(test_gsmem tests/test_gsmem.c)
target_compile_options(test_gsmem PRIVATE -std=gnu11 -fno-strict-aliasing -fwrapv ${RECVX_FP_FLAGS} -O3)
target_include_directories(test_gsmem PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/sdk ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_test(NAME gsmem COMMAND test_gsmem)

# SMAA's lookup textures computed at start (src/gs/smaa_tables.h): the bytes of SMAA's own tables
add_executable(test_aa_tables tests/test_aa_tables.c)
target_compile_options(test_aa_tables PRIVATE -std=gnu11 ${RECVX_FP_FLAGS})
target_link_libraries(test_aa_tables PRIVATE m)
add_test(NAME aa_tables COMMAND test_aa_tables)

# "You appear as" list (pc/src/host/actors.c): event doubles hidden, indices kept
add_executable(test_appear tests/test_appear.c src/host/actors.c)
add_test(NAME appear COMMAND test_appear)

# imports (src/import): SLD, TIM2, the glTF and .cvxm writers on a synthetic model
add_executable(test_import tests/test_import.c src/import/impmodel.c src/import/gdrom.c src/import/dc.c src/import/ob2.c src/import/uc.c src/import/uc_voice.c src/import/rvz.c src/import/import.c src/import/lang.c src/host/glyphs.c src/host/actors.c src/host/fileio.c src/host/parse.c src/host/pc_math.c)
target_link_libraries(test_import PRIVATE SDL3::SDL3 zstd_dec m)
add_test(NAME import COMMAND test_import ${CMAKE_CURRENT_BINARY_DIR})

# our translations (src/host/tr_table.inc) against their languages' game fonts (src/host/glyphs.c): no missing letter
# ... and tr() reads them from the executable as the game does (src/host/tr.c: the same rows)
add_executable(test_lang tests/test_lang.c src/host/glyphs.c src/host/tr.c ${RECVX_RES})
add_test(NAME lang COMMAND test_lang)

# checked file reading (include/pc_parse.h): PRS against the game's decoder, crafted AFS tables, .cvxm
# models, DDS / PNG headers and BioRand start-kit scripts
add_executable(test_parsers tests/test_parsers.c src/host/parse.c src/net/coop_proto.c src/net/rdv_proto.c)
target_compile_options(test_parsers PRIVATE -std=gnu11)
target_link_libraries(test_parsers PRIVATE monocypher m)
add_test(NAME parsers COMMAND test_parsers)

# enemy variants (pc/src/host/variants.c): the built-in table, the mods/enemies.ini reader
add_executable(test_variants tests/test_variants.c src/host/variants.c src/host/actors.c src/host/fileio.c)
add_test(NAME variants COMMAND test_variants)
# the cutscene cast randomizer's picks (pc/src/host/cast.c): deterministic, never the model itself, distinct per room; the voice lines (voices.c)
# "Enemy Models" (pc/src/host/enemymdl.c): categories, choices, picks, recvx.ini keys
add_executable(test_emodel tests/test_emodel.c src/host/enemymdl.c src/host/actors.c)
add_test(NAME emodel COMMAND test_emodel)
add_executable(test_cast tests/test_cast.c src/host/cast.c src/host/actors.c src/host/voices.c src/net/coop_proto.c src/net/rdv_proto.c src/host/pc_math.c)
target_link_libraries(test_cast PRIVATE monocypher m)
add_test(NAME cast COMMAND test_cast)
# pc/tools/voice_curation.py (the voice tool's JSON -> voice_curation.inc) on a made-up file: flags, groups (lines said as one)
if(Python3_FOUND)
    add_test(NAME voice_curation COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_voice_curation.py)
    # recvx --voice-plan (cast_glue.c voice_plan_export): refused without a disc; with RECVX_ISO the JSON's shape (skipped otherwise)
    add_test(NAME voice_plan COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_voice_plan.py ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:recvx>)
    set_tests_properties(voice_plan PROPERTIES SKIP_RETURN_CODE 77)
    # ... against the game itself: a few scenes run headless with the cast and the voice lines on, each line as the plan has it
    add_test(NAME voice_plan_game COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_voice_plan_game.py ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:recvx>)
    set_tests_properties(voice_plan_game PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 1200)
    # the new menu (src/host/menu_layout.c): every row it shows has a help line
    add_test(NAME menu_help COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_menu_help.py ${CMAKE_CURRENT_SOURCE_DIR})
    # ... and every text it shows is translated into every language (src/host/tr_table.inc)
    add_test(NAME menu_tr COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_menu_tr.py ${CMAKE_CURRENT_SOURCE_DIR})
endif()
# the --dev object placer's file (pc/src/host/placer.c): written and read back
add_executable(test_placer tests/test_placer.c src/host/placer.c src/host/extras.c src/host/fileio.c src/host/pc_math.c)
target_link_libraries(test_placer PRIVATE m)
add_test(NAME placer COMMAND test_placer ${CMAKE_CURRENT_BINARY_DIR})
# the --dev cutscene log's list (pc/src/host/devcine.c): repeats, the oldest dropped, clear, text and export; the scene list's room reading
add_executable(test_devcine tests/test_devcine.c src/host/devcine.c src/host/fileio.c src/net/coop_proto.c src/net/rdv_proto.c)
target_link_libraries(test_devcine PRIVATE monocypher m)
add_test(NAME devcine COMMAND test_devcine ${CMAKE_CURRENT_BINARY_DIR})

# co-op networking (pc/src/net): a busy port, the port mapping started and stopped again
add_executable(test_net tests/test_net.c src/net/net.c src/net/portmap.c)
target_compile_options(test_net PRIVATE $<$<COMPILE_LANGUAGE:C>:-std=gnu11>)
target_link_libraries(test_net PRIVATE SDL3::SDL3 miniupnpc::miniupnpc natpmp $<$<BOOL:${WIN32}>:ws2_32>)
add_test(NAME net COMMAND test_net)

# update check (pc/src/net/update.h): the URL, the server's answer (status, body, junk), version comparison
add_executable(test_update tests/test_update.c)
add_test(NAME update COMMAND test_update)

# BioRand CLI download (pc/src/net/biorand_dl.h): SHA-256 vectors, the release JSON scan, the URL allowlist;
# biorand_dl_install (Linux, curl and tar): the whole install from a made-up release over file://
add_executable(test_biorand_dl tests/test_biorand_dl.c src/net/biorand_dl.c src/net/sha256.c)
target_compile_options(test_biorand_dl PRIVATE -std=gnu11 -Wall)
target_link_libraries(test_biorand_dl PRIVATE SDL3::SDL3 $<$<BOOL:${WIN32}>:winhttp>)
add_test(NAME biorand_dl COMMAND test_biorand_dl)
# a BioRand pack from a ZIP (pc/src/host/biorand_zip.c, Android's Import BioRand Pack): made-up ZIPs imported and refused
add_executable(test_biorand_zip tests/test_biorand_zip.c src/host/biorand_zip.c src/import/gdrom.c src/host/fileio.c src/net/biorand_dl.c src/net/sha256.c)
target_compile_options(test_biorand_zip PRIVATE -std=gnu11 -Wall)
target_link_libraries(test_biorand_zip PRIVATE SDL3::SDL3 $<$<BOOL:${WIN32}>:winhttp>)
add_test(NAME biorand_zip COMMAND test_biorand_zip ${CMAKE_CURRENT_BINARY_DIR}/biorand_zip_test)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    add_test(NAME biorand_dl_install COMMAND test_biorand_dl ${CMAKE_CURRENT_BINARY_DIR}/biorand_dl_test)
    set_tests_properties(biorand_dl_install PROPERTIES SKIP_RETURN_CODE 77)   # no curl or tar
endif()

# BioRand's CVX options (pc/src/host/biorand_opts.c): the golden seed strings, every row round-trips,
# malformed strings rejected, the item and enemy distributions, the ini key
add_executable(test_biorand_opts tests/test_biorand_opts.c src/host/biorand_opts.c)
target_compile_options(test_biorand_opts PRIVATE -std=gnu11 -Wall)
target_link_libraries(test_biorand_opts PRIVATE m)
add_test(NAME biorand_opts COMMAND test_biorand_opts)
# BioRand's spoiler log (pc/src/host/spoiler.c): a made-up log's segments, keys, spots and start items; odd input
add_executable(test_spoiler tests/test_spoiler.c src/host/spoiler.c)
target_compile_options(test_spoiler PRIVATE -std=gnu11 -Wall)
add_test(NAME spoiler COMMAND test_spoiler)
# WHAT'S NEW's lines (pc/src/host/changelog.c): a made-up changelog and the real one, nothing the game's font cannot draw
add_executable(test_changelog tests/test_changelog.c src/host/changelog.c)
target_compile_options(test_changelog PRIVATE -std=gnu11 -Wall)
add_test(NAME changelog COMMAND test_changelog ${PROJECT_SOURCE_DIR}/CHANGELOG.md)

# co-op packets (pc/src/net/coop_proto.c): malformed events of every type, transfers, the event
# ring, the join code, world event actions, and a fuzz loop
add_executable(test_coop tests/test_coop.c src/net/coop_proto.c src/net/rdv_proto.c)
target_compile_options(test_coop PRIVATE -std=gnu11)
target_link_libraries(test_coop PRIVATE monocypher m)
add_test(NAME coop COMMAND test_coop)
add_test(NAME coop_playout COMMAND test_coop playout)   # the enemy playout on synthetic jitter, loss, drift, stall traces

# UTF-8 file paths and replace-on-rename saving (pc/src/host/fileio.c)
add_executable(test_fileio tests/test_fileio.c src/host/fileio.c)
target_compile_options(test_fileio PRIVATE -std=gnu11)
add_test(NAME fileio COMMAND test_fileio)

# native save files (pc/src/host/savestore.c)
add_executable(test_savestore tests/test_savestore.c src/host/savestore.c src/host/fileio.c)
target_compile_options(test_savestore PRIVATE -std=gnu11)
target_link_libraries(test_savestore PRIVATE SDL3::SDL3)
add_test(NAME savestore COMMAND test_savestore)

# co-op server protocol and server core (src/net/rdv_proto.c, server/server.c): messages, malformed
# input and fuzz, the end-to-end encryption, room codes (unique, collisions, grace period), hijacks,
# CREATE retries, passwords, relay limits
add_executable(test_rdv tests/test_rdv.c)
target_compile_options(test_rdv PRIVATE -std=gnu11)
target_link_libraries(test_rdv PRIVATE recvx_server_core)
target_compile_definitions(test_rdv PRIVATE RECVX_TEST_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests")
add_test(NAME rdv COMMAND test_rdv)
# the rendezvous client state machine against the server core on a fake network (deterministic)
add_executable(test_rdv_client tests/test_rdv_client.c src/net/rendezvous.c)
target_compile_options(test_rdv_client PRIVATE -std=gnu11)
target_link_libraries(test_rdv_client PRIVATE recvx_server_core)
if(WIN32)
    target_link_libraries(test_rdv_client PRIVATE ws2_32)
endif()
add_test(NAME rdv_client COMMAND test_rdv_client)
# fuzzing under AddressSanitizer + UBSan: parser, server and client state machines (Linux, GCC/Clang)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(RDV_SAN -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer)
    add_executable(fuzz_rdv tests/fuzz_rdv.c src/net/rendezvous.c ${CMAKE_CURRENT_SOURCE_DIR}/server/server.c
                   ${CMAKE_CURRENT_SOURCE_DIR}/server/config.c src/net/rdv_proto.c)
    target_compile_options(fuzz_rdv PRIVATE -std=gnu11 -O1 -g ${RDV_SAN})
    target_link_options(fuzz_rdv PRIVATE ${RDV_SAN})
    target_link_libraries(fuzz_rdv PRIVATE monocypher)
    add_test(NAME rdv_fuzz COMMAND fuzz_rdv 300000)
endif()
