# port/platform/CMakeLists.txt: the platform sources ico_platform takes
# beyond the top-level CMakeLists.txt's, and the platform tests.

target_sources(ico_platform PRIVATE
    app_icon.c
    app_icon_ico.c
    app_icon_image.c
    arena.c
    backup.c
    bones_ladder.c
    code_hash.c
    diag_host.c
    fiber.c
    fps_counter.c
    frame_mailbox.c
    game_data_range.c
    gpu_hang.c
    gpu_sysfs.c
    thermal_text.c
    hotkeys.c
    kernel_host.c
    pace_policy.c
    memory_floor.c
    display_rate.c
    present_hist.c
    perf_block.c
    pipeline_skip.c
    present_fallback.c
    pump_watch.c
    report.c
    robust_persist.c
    sched.c
    startup_retry.c
    state_check.c
    state_file.c
    state_hash.c
    state_registry.c
    state_reloc.c
    state_syms.c
    state_xbuild.c
    symmap.c
    vm_reserve.c
    vsync_hooks.c
    window_lifecycle.c
    window_scene.c)
# diag_host.c's heartbeat and watchdog thread (pthreads on POSIX).
find_package(Threads REQUIRED)
target_link_libraries(ico_platform PUBLIC Threads::Threads)
# report.c deflates the report file's entries (tdefl) and symmap.c reads a
# gzip symbol table (tinfl), both into static buffers
target_link_libraries(ico_platform PUBLIC ico_miniz)
if(TARGET ico_pc)
    # A link map beside the program, shipped with test builds, to turn the
    # crash report's executable offsets into symbols.
    if(APPLE)
        target_link_options(ico_pc PRIVATE "LINKER:-map,$<TARGET_FILE_DIR:ico_pc>/ico_pc.map")
    else()
        target_link_options(ico_pc PRIVATE "LINKER:-Map=$<TARGET_FILE_DIR:ico_pc>/ico_pc.map")
    endif()
endif()
# host_loop.c calls into the game (its main, ico_vsync, systemStatus), so it
# goes into the program beside main_host.c, ahead of the game library on the
# link line, rather than into ico_platform, which the game itself needs.
if(TARGET ico_pc)
    target_sources(ico_pc PRIVATE host_loop.c)
endif()
# game_data_verify.c names the game's globals (the save states' start-up
# check of the game data ranges, game_data_range.h): the program only
if(TARGET ico_pc)
    target_sources(ico_pc PRIVATE game_data_verify.c)
endif()
# state_port.c registers the port's save state blocks (state_port.h), which
# live in libraries the platform layer does not link: the program only
if(TARGET ico_pc)
    target_sources(ico_pc PRIVATE state_port.c)
endif()
# The commit the program was built from (ICO_BUILD_COMMIT in the
# generated ico_build_commit.h, "git describe --always --dirty"), for the pad
# recording's header (main_host.c). Checked at every build; the header only
# changes when the commit does.
if(TARGET ico_pc)
    set(_ico_commit_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
    add_custom_target(ico_build_commit
        COMMAND "${CMAKE_COMMAND}" -DSRC=${CMAKE_SOURCE_DIR}
                -DOUT=${_ico_commit_dir}/ico_build_commit.h
                -P "${CMAKE_CURRENT_SOURCE_DIR}/build_commit.cmake"
        BYPRODUCTS "${_ico_commit_dir}/ico_build_commit.h"
        COMMENT "Build commit"
        VERBATIM)
    add_dependencies(ico_pc ico_build_commit)
    target_include_directories(ico_pc PRIVATE "${_ico_commit_dir}")
endif()
# port/null/gfx_null.c (the graphics floor under the game's packet code, in
# both builds) is linked into ico_pc by the top-level CMakeLists.txt.

# ICO_HEAP_STATS: memory.c reports allocations to arena.c, which logs each
# partition's high-water mark.
option(ICO_HEAP_STATS "Log the game heap's per-partition high-water marks" OFF)
if(ICO_HEAP_STATS)
    target_compile_definitions(ico_fumi PRIVATE ICO_HEAP_STATS=1)
endif()

# --- Tests ------------------------------------------------------------------
# startup_retry.c: the steps after a graphics driver crash while building a
# pipeline, and the relaunch's environment
add_executable(startup_retry_test test/startup_retry_test.c)
target_compile_options(startup_retry_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(startup_retry_test PRIVATE ico_platform)
if(ICO_RUN_TESTS)
    add_test(NAME startup_retry COMMAND startup_retry_test)
endif()
# memory_floor: the start-up set's free-memory floor (memory_floor.c, issue
# 161): the default floor, the reporter's figures, a small device, the setting
# pipeline_skip: the record of a pipeline family a device's driver stopped in
# (pipeline_skip.c, issue 161): the note's family, the value written and read
# back, the device and driver match, the [dev] clear
# robust_persist: the port's own [dev] gpu_robust turning itself off (robust_persist.c):
# the rule over sequences of starts, the mark written and read back through the
# config text, the key removal, the log lines word for word
# gpu_hang: the GPU hang limit's arithmetic and its watch (gpu_hang.c, issue 123)
# state_fiber: save states' fiber pool, a suspended fiber saved and restored
# into a second pool, the dead frames under its stack pointer kept, and the
# relocation words (fiber.c, state_reloc.c)
# state_guard: save states' two process values (the thread pointer and the
# stack guard) rewritten old -> new by exact match in words, a scan and a
# real fiber's registers and frame, and the per-platform refusal of a state
# without them (state_reloc.c, fiber.c)
# state_port: save states across versions (state_syms.c, code_hash.c,
# state_xbuild.c, state_reloc.c, state_file.c): two synthetic builds' symbol
# files and code, the masked code hashes, the translation of every word and
# the refusal naming the first blocker (a changed function, the data
# layout, a block), the bounds' fallback, the format 1 refusal; nothing of
# the game starts
# state_registry: the save state registry's table (state_registry.c)
# display_rate: the refresh rate's log lines and the Android frame rate pin's
# decisions (display_rate.c, issue 123)
# present_hist: the 10 s block's histogram of the driver's present call
# (present_hist.c, issue 123)
# gpu_sysfs: the kernel GPU driver's nodes, the pure part (gpu_sysfs.c, issue
# 123): the parser over the nodes' texts, the ring, the block and around
# reductions, the three lines word for word
# present_fallback: the FIFO fallback's decision over a log's present blocks
# under mailbox, the driver's eligibility, the [dev] value and the lines
# (present_fallback.c)
# frame_mailbox: the game thread's and the render thread's hand-over
# (frame_mailbox.c): the latest frame wins, the slot pins, park and quit with
# a job queued, the submit's handshake, the heartbeat and the block's
# numbers under a fake clock, the [dev] render_thread mode
# bones_ladder: issue 123's ladder across starts (bones_ladder.c): the step
# for each run's evidence over sequences of starts (step 1, step 2, healed,
# both tried, a port race, step 2 out of reach), the mark written and read
# back, the start and action lines word for word
foreach(t sched fiber fps_counter arena diag hotkeys pace_policy pace_loop perf_block lifecycle pump_watch ico_thread
          gpu_hang state_fiber state_guard state_port state_registry pipeline_skip memory_floor
          present_fallback frame_mailbox display_rate present_hist gpu_sysfs
          thermal_text window_scene bones_ladder)
    add_executable(${t}_test test/${t}_test.c)
    target_include_directories(${t}_test PRIVATE "${ICO_COMPAT_DIR}")
    target_compile_options(${t}_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
    target_link_libraries(${t}_test PRIVATE ico_platform)
    if(ICO_RUN_TESTS)
        add_test(NAME ${t} COMMAND ${t}_test)
    endif()
endforeach()
# diag_test's own link map beside it: its crash cases check the symbol
# lines against it, as a player's ico_pc.map is read beside ico_pc
if(NOT APPLE AND NOT WIN32)
    target_link_options(diag_test PRIVATE "LINKER:-Map=$<TARGET_FILE_DIR:diag_test>/diag_test.map")
endif()
if(ICO_RUN_TESTS)
    set_tests_properties(diag PROPERTIES SKIP_RETURN_CODE 77)
    # the switch's step lines (ico_diag_step) and its watch
    # (ico_diag_watchdog_switch): a killed child's log names the step, a
    # hung step ends the run with the report naming it, steady progress does
    # not (diag_test --switch; POSIX only, 77 elsewhere)
    add_test(NAME diag_switch COMMAND diag_test --switch)
    set_tests_properties(diag_switch PROPERTIES SKIP_RETURN_CODE 77)
endif()

# cpu_place: the cores a thread ran on most, the cores' speeds from a fake
# sysfs under the build folder (Linux and Android only), the 10 s part's
# words, the masks' text, the calling thread's priority and allowed cores,
# its CPU time, the threads line from a fake table and a fake
# /proc/self/task
add_executable(cpu_place_test test/cpu_place_test.c)
target_compile_options(cpu_place_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(cpu_place_test PRIVATE ico_thread)
if(ICO_RUN_TESTS)
    add_test(NAME cpu_place COMMAND cpu_place_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# thread_stat: a thread's time on a core and waiting for one (thread_stat.c,
# issue 179): the kernel's schedstat text, a fake /proc/self/task under the
# build folder (Linux and Android), the block's difference, the words
add_executable(thread_stat_test test/thread_stat_test.c)
target_compile_options(thread_stat_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(thread_stat_test PRIVATE ico_thread)
if(ICO_RUN_TESTS)
    add_test(NAME thread_stat COMMAND thread_stat_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# The Android build's file layout (android/android_paths.c), pure C, so it
# is tested here on the native builds.
add_executable(android_paths_test test/android_paths_test.c android/android_paths.c)
target_include_directories(android_paths_test PRIVATE android)
target_compile_options(android_paths_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
if(ICO_RUN_TESTS)
    add_test(NAME android_paths COMMAND android_paths_test)
endif()

# The Android build's graphics driver packages (gpu_driver.c),
# pure C over host_fs, miniz and json.h, so it is tested on the native builds;
# it writes under the build folder
add_executable(gpu_driver_test test/gpu_driver_test.c gpu_driver.c)
target_compile_options(gpu_driver_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(gpu_driver_test PRIVATE ico_host_fs ico_json ico_miniz)
if(ICO_RUN_TESTS)
    add_test(NAME gpu_driver COMMAND gpu_driver_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# Extras > Add to Steam (steam_shortcut.c): the binary VDF reader and writer,
# the Steam folder search and the running-Steam check, over a fake Steam
# folder under the build folder
add_executable(steam_shortcut_test test/steam_shortcut_test.c steam_shortcut.c)
target_include_directories(steam_shortcut_test PRIVATE .)
target_compile_options(steam_shortcut_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(steam_shortcut_test PRIVATE ico_host_fs)
if(ICO_RUN_TESTS)
    add_test(NAME steam_shortcut COMMAND steam_shortcut_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# The application icon (app_icon.h): the image maths on synthetic frames, the
# .ico writer parsed back, and the files and stamp over a fake user folder
# under the build folder
add_executable(app_icon_image_test test/app_icon_image_test.c app_icon_image.c)
target_include_directories(app_icon_image_test PRIVATE .)
target_compile_options(app_icon_image_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
if(UNIX)
    target_link_libraries(app_icon_image_test PRIVATE m)
endif()
add_executable(ico_icon_test test/ico_icon_test.c app_icon_ico.c app_icon_image.c)
target_include_directories(ico_icon_test PRIVATE .)
target_compile_options(ico_icon_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(ico_icon_test PRIVATE ico_host_fs)
add_executable(app_icon_files_test test/app_icon_files_test.c app_icon.c app_icon_ico.c
                                   app_icon_image.c)
target_include_directories(app_icon_files_test PRIVATE .)
target_compile_options(app_icon_files_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(app_icon_files_test PRIVATE ico_host_fs)
if(ICO_RUN_TESTS)
    add_test(NAME app_icon_image COMMAND app_icon_image_test)
    add_test(NAME ico_icon COMMAND ico_icon_test "${CMAKE_CURRENT_BINARY_DIR}")
    add_test(NAME app_icon_files COMMAND app_icon_files_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# The exe's own icon (icon_stamp.h): the RT_GROUP_ICON directory and the
# RT_ICON images built from an .ico the writer made, the stamp text and the
# command-line quoting. The resource update itself (tools/ico_icon_stamp.c,
# Windows only) is checked on Windows by hand (docs/port/APP_ICON.md).
add_executable(icon_stamp_test test/icon_stamp_test.c icon_stamp.c app_icon_ico.c app_icon_image.c)
target_include_directories(icon_stamp_test PRIVATE .)
target_compile_options(icon_stamp_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(icon_stamp_test PRIVATE ico_host_fs)
if(ICO_RUN_TESTS)
    add_test(NAME icon_stamp COMMAND icon_stamp_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()
# tools\ico_icon_stamp.exe, shipped by tools/package_win.sh: gives the game's
# exe the captured icon after the game exits (app_icon_exe.c starts it).
# A program with no window (GUI subsystem); kernel32 and shell32 only.
if(WIN32)
    add_executable(ico_icon_stamp "${CMAKE_SOURCE_DIR}/tools/ico_icon_stamp.c" icon_stamp.c)
    target_include_directories(ico_icon_stamp PRIVATE .)
    target_compile_options(ico_icon_stamp PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
    target_link_options(ico_icon_stamp PRIVATE -mwindows)
    if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
        target_link_options(ico_icon_stamp PRIVATE -static-libgcc)
    endif()
    target_link_libraries(ico_icon_stamp PRIVATE shell32)
endif()

# Extras > Add to the applications menu (desktop_entry.c): the entry's text
# and the install over a fake data folder under the build folder
# (Linux and the other Unixes: the test sets environment variables with setenv)
if(UNIX)
    add_executable(desktop_entry_test test/desktop_entry_test.c desktop_entry.c)
    target_include_directories(desktop_entry_test PRIVATE .)
    target_compile_options(desktop_entry_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
    target_link_libraries(desktop_entry_test PRIVATE ico_host_fs)
    if(ICO_RUN_TESTS)
        add_test(NAME desktop_entry COMMAND desktop_entry_test "${CMAKE_CURRENT_BINARY_DIR}")
    endif()
endif()

# The bug report's zip (report.c): the stored loss dump deflated into it,
# the slow-scene zip stored, read back with miniz's reader; it writes under
# the build folder
add_executable(report_test test/report_test.c)
target_compile_options(report_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(report_test PRIVATE ico_platform ico_host_fs ico_miniz)
if(ICO_RUN_TESTS)
    add_test(NAME report COMMAND report_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# F12's zip (dump_zip.c): the dump and the log in one archive, read back
# with miniz's reader; it writes under the build folder
add_executable(dump_zip_test test/dump_zip_test.c dump_zip.c)
target_compile_options(dump_zip_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(dump_zip_test PRIVATE ico_host_fs ico_miniz)
if(ICO_RUN_TESTS)
    add_test(NAME dump_zip COMMAND dump_zip_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# Options > Extras > Back up your data and Restore from a backup (backup.c):
# a user folder under the build folder backed up, inspected, unpacked and
# put in place, byte for byte, and the refusals (not a backup, a path that
# climbs out, past the bounds, a busy card or save state) and a failed move
# undone
add_executable(backup_test test/backup_test.c backup.c)
target_compile_options(backup_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(backup_test PRIVATE ico_host_fs ico_miniz)
if(ICO_RUN_TESTS)
    add_test(NAME backup COMMAND backup_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# An rc build's Share a bug report's files (share_report.c, compiled into
# the program only with ICO_RC_TOOLS on Android), over a fake logs and dumps
# folder under the build folder; tested in every build
add_executable(share_report_test test/share_report_test.c share_report.c)
target_compile_options(share_report_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(share_report_test PRIVATE ico_host_fs)
if(ICO_RUN_TESTS)
    add_test(NAME share_report COMMAND share_report_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# game_data_range: the game data ranges (game_data_range.c) over a stand-in
# game library that gets ico_game's section treatment
# (cmake/IcoStateSections.cmake) and the game's semantics options: the
# ranges hold its writable globals and nothing of the program's, the renamed
# sections keep their contents and relocations, and a copy of the ranges
# written back restores them
add_library(game_data_range_fixture STATIC EXCLUDE_FROM_ALL test/game_data_fixture.c)
target_compile_options(game_data_range_fixture PRIVATE ${ICO_SEMANTIC_OPTIONS}
                       ${ICO_GAME_LAYOUT_OPTIONS})
ico_state_sections(game_data_range_fixture)
add_executable(game_data_range_test test/game_data_range_test.c game_data_range.c)
target_include_directories(game_data_range_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_options(game_data_range_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(game_data_range_test PRIVATE game_data_range_fixture)
if(ICO_RUN_TESTS)
    add_test(NAME game_data_range COMMAND game_data_range_test)
endif()

# state_format: the save state file (state_file.c) written on its thread and
# read back, every header refusal, bad and truncated files, the build id; it
# writes under the build folder
add_executable(state_format_test test/state_format_test.c)
target_compile_options(state_format_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(state_format_test PRIVATE ico_platform)
if(ICO_RUN_TESTS)
    add_test(NAME state_format COMMAND state_format_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()

# host_fs.h on a non-ASCII (UTF-8) path in the build tree
add_executable(host_fs_test test/host_fs_test.c)
target_compile_options(host_fs_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
target_link_libraries(host_fs_test PRIVATE ico_platform)
if(ICO_RUN_TESTS)
    add_test(NAME host_fs COMMAND host_fs_test "${CMAKE_CURRENT_BINARY_DIR}")
endif()
if(ICO_RUN_TESTS AND NOT WIN32)
    # a stack overflow in a fiber must hit the guard page
    add_test(NAME fiber_guard COMMAND fiber_test guard)
    set_tests_properties(fiber_guard PROPERTIES SKIP_RETURN_CODE 77)
    # and in a fiber restored from a save state into another pool
    add_test(NAME state_fiber_guard COMMAND state_fiber_test guard)
    set_tests_properties(state_fiber_guard PROPERTIES SKIP_RETURN_CODE 77)
endif()

# The game's allocator (fumi/ios/memory.c) over the arena, compiled as the
# game compiles it; the test checks the partition layout for the host's
# record sizes.
add_executable(memory_test test/memory_test.c "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/memory.c")
ico_include_dirs(fumi _ico_fumi_incs)
target_include_directories(memory_test PRIVATE ${_ico_fumi_incs})
target_compile_definitions(memory_test PRIVATE ${ICO_GAME_DEFINITIONS} ICO_HEAP_STATS=1)
target_compile_options(memory_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})
target_link_libraries(memory_test PRIVATE ico_platform)
if(ICO_RUN_TESTS)
    add_test(NAME memory COMMAND memory_test)
endif()

# Issue 151: the game's inflate (fumi/ios/inflate.c) over its allocator and
# its code-table blocks (memory.c, mblock.c), compiled as the game compiles
# them: fixed-code deflate blocks decode and their tables are freed at the
# close without an IOSFREE() assert, and a bad free's assert names the block's
# allocation.  `synth` runs everywhere on streams the test holds; the disc
# runs inflate the packs that hold fixed-code blocks (the Asian disc's
# STGST13C.DF, stage 4, and STGST02A.DF; the Korean disc's STGSTAFF1.DF; the
# August 2001 preview's STGST13C.DF) to their end and are skipped (77) without
# the image.
add_executable(inflate_test test/inflate_test.c
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/inflate.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/mblock.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/memory.c")
target_include_directories(inflate_test PRIVATE ${_ico_fumi_incs})
target_compile_definitions(inflate_test PRIVATE ${ICO_GAME_DEFINITIONS} ICO_HEAP_STATS=1)
target_compile_options(inflate_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})
target_link_libraries(inflate_test PRIVATE ico_port_data ico_platform)
# The test frees a pointer 8 bytes into a block on purpose (the EE walk's
# mistake on the host); iosFree reads the 16-byte-aligned header in front of
# it, which UBSan's alignment check reports, and its assert message then
# reads the shifted node's neighbours: the shifted `next` lands on the block's
# free_prev, NULL for a block in use, so the message's "%s" of that node's
# tag (at offset 0) is a NULL member access UBSan reports (glibc prints
# "(null)").  Only this target drops those two checks.
if(ICO_SANITIZE)
    target_compile_options(inflate_test PRIVATE $<$<COMPILE_LANGUAGE:C>:-fno-sanitize=alignment,null>)
endif()
if(ICO_RUN_TESTS)
    add_test(NAME inflate COMMAND inflate_test synth)
    add_test(NAME inflate_disc_cn COMMAND inflate_test disc "${ICO_DISC_IMAGE_CN}" STGST13C.DF
                                          9831100 483)
    add_test(NAME inflate_disc_cn_st02a COMMAND inflate_test disc "${ICO_DISC_IMAGE_CN}"
                                                STGST02A.DF 9486676 497)
    add_test(NAME inflate_disc_kor COMMAND inflate_test disc "${ICO_DISC_IMAGE_KOR}"
                                           STGSTAFF1.DF 7154080 232)
    add_test(NAME inflate_disc_aug6 COMMAND inflate_test disc "${ICO_DISC_IMAGE_AUG6}"
                                            STGST13C.DF 9656788 461)
    set_tests_properties(inflate_disc_cn inflate_disc_cn_st02a inflate_disc_kor inflate_disc_aug6
                         PROPERTIES SKIP_RETURN_CODE 77)
endif()

# Save states' relocation (state_reloc.c, state_check.c): the scan over
# synthetic regions with pointers into each moved range, near misses,
# floats and EE word pairs; the program's image range; the game heap's
# typed walk over a real partition (fumi/ios/memory.c, compiled as the game
# compiles it) copied to another address and relocated; the thread and
# handler checks; the masked hash of the game's state (state_hash.c): typed
# fields and words into noted host blocks left out, everything else counted.
add_executable(state_reloc_test test/state_reloc_test.c
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/memory.c")
target_include_directories(state_reloc_test PRIVATE ${_ico_fumi_incs})
target_compile_definitions(state_reloc_test PRIVATE ${ICO_GAME_DEFINITIONS})
target_compile_options(state_reloc_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})
target_link_libraries(state_reloc_test PRIVATE ico_platform)
if(ICO_RUN_TESTS)
    add_test(NAME state_reloc COMMAND state_reloc_test)
endif()

# The game's thread, message and memory layers on the scheduler, driven by
# simulated vsyncs.
add_executable(ios_chain_test test/ios_chain_test.c
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/thread.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/message.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/memory.c")
target_include_directories(ios_chain_test PRIVATE ${_ico_fumi_incs})
target_compile_definitions(ios_chain_test PRIVATE ${ICO_GAME_DEFINITIONS})
target_compile_options(ios_chain_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})
target_link_libraries(ios_chain_test PRIVATE ico_platform)
if(ICO_RUN_TESTS)
    add_test(NAME ios_chain COMMAND ios_chain_test)
endif()

# The pad as a movie's skip poll sees it (main.c movie_abort_check): the game's
# keyInput.c, pad.c and shockdriver.c over the thread and message layers and
# port/input's libpad and bindings, on the scheduler, a thread spinning to the
# vsync and polling, fed a gamepad Start and a key bound to Start.
add_executable(movie_pad_test test/movie_pad_test.c host_config.c
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/thread.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/message.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/memory.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/pad.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/shockdriver.c"
    "${CMAKE_SOURCE_DIR}/ico2/sugipon/src/keyInput.c")
target_include_directories(movie_pad_test PRIVATE ${_ico_fumi_incs})
target_compile_definitions(movie_pad_test PRIVATE ${ICO_GAME_DEFINITIONS})
target_compile_options(movie_pad_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})
target_link_libraries(movie_pad_test PRIVATE ico_port_null ico_platform)
if(ICO_RUN_TESTS)
    add_test(NAME movie_pad COMMAND movie_pad_test)
endif()

# Issue 144: Mirror mode and the d-pad.  The game's iosPadGetStick (pad.c) over
# a centred stick and the d-pad bits, mirror off and on, both disc copies: the
# synthesised x flips with the option, y and a deflected stick do not.
add_executable(pad_mirror_test test/pad_mirror_test.c host_config.c
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/thread.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/message.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/memory.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/pad.c"
    "${CMAKE_SOURCE_DIR}/ico2/fumi/ios/shockdriver.c")
target_include_directories(pad_mirror_test PRIVATE ${_ico_fumi_incs})
target_compile_definitions(pad_mirror_test PRIVATE ${ICO_GAME_DEFINITIONS})
target_compile_options(pad_mirror_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})
target_link_libraries(pad_mirror_test PRIVATE ico_port_null ico_platform)
if(ICO_RUN_TESTS)
    add_test(NAME pad_mirror COMMAND pad_mirror_test)
endif()

# The window mode requests' readback (window_video.c) on SDL's
# offscreen video driver. SDL3 is found by port/rhi, configured after this
# directory, so the test is defined once the top level is done; exit 77
# (skipped) without the offscreen driver.
function(_ico_window_video_test)
    if(NOT TARGET SDL3::SDL3-shared)
        return()
    endif()
    set(_dir "${CMAKE_SOURCE_DIR}/port/platform")
    add_executable(window_video_test "${_dir}/test/window_video_test.c" "${_dir}/window_video.c")
    target_compile_options(window_video_test PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_PORT_WARNINGS})
    target_link_libraries(window_video_test PRIVATE SDL3::SDL3-shared)
    ico_copy_sdl3_dll(window_video_test)
    if(ICO_RUN_TESTS)
        add_test(NAME window_video COMMAND window_video_test)
        set_tests_properties(window_video PROPERTIES SKIP_RETURN_CODE 77)
    endif()
endfunction()
cmake_language(DEFER DIRECTORY "${CMAKE_SOURCE_DIR}" CALL _ico_window_video_test)

# backend_switch_window: the window build's Options > Graphics > Apply,
# Vulkan (lavapipe) -> OpenGL (llvmpipe) -> Vulkan through
# ico_window_switch_backend and ico_window_reopen, on SDL's offscreen
# driver, driven by [dev] backend_switch_test (test/backend_switch.cmake);
# Linux, both backends built, the disc image (skipped without it). A game
# run with two renderer starts on software drivers: alone, label window.
# Deferred: port/rhi (the OpenGL backend's target) is configured after this
# directory.
function(_ico_backend_switch_test)
    if(NOT TARGET ico_pc OR ICO_HEADLESS OR NOT ICO_RUN_TESTS OR NOT TARGET ico_rhi_opengl
       OR NOT TARGET ico_rhi_vulkan OR NOT CMAKE_SYSTEM_NAME STREQUAL "Linux" OR ANDROID)
        return()
    endif()
    set(_dir "${CMAKE_SOURCE_DIR}/port/platform")
    add_test(NAME backend_switch_window
             COMMAND "${CMAKE_COMMAND}" "-DICO_PC=$<TARGET_FILE:ico_pc>" "-DISO=${ICO_DISC_IMAGE}"
                     "-DDIR=${CMAKE_BINARY_DIR}/port/platform/backend_switch_run"
                     "-DLVP=/usr/share/vulkan/icd.d/lvp_icd.json"
                     -P "${_dir}/test/backend_switch.cmake")
    set_tests_properties(backend_switch_window PROPERTIES RUN_SERIAL TRUE TIMEOUT 1500
                         SKIP_REGULAR_EXPRESSION "backend_switch_window: skipped"
                         LABELS "platform;window")
endfunction()
cmake_language(DEFER DIRECTORY "${CMAKE_SOURCE_DIR}" CALL _ico_backend_switch_test)
