# The game, built from source as a library the host runtime loads.
#
# A separate project because it needs GCC even when the host uses another compiler
# (see ../cmake/mingw-w64-x86_64.cmake). The host talks to it through one exported entry point and
# two tables of function pointers, so nothing here links against the host and the host links against
# nothing here.
cmake_minimum_required(VERSION 3.25)
project(melee_game C CXX)

enable_testing()

if(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_VERSION VERSION_LESS 14)
  message(FATAL_ERROR "melee_game needs GCC 14 or newer: scalar_storage_order, and -ffp-contract=on meaning what the standard says")
endif()
if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8 OR NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64)$")
  message(FATAL_ERROR "melee_game requires an x86-64 target")
endif()
if(NOT WIN32 AND NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
  message(FATAL_ERROR "melee_game supports Windows x64 and native Linux x86-64")
endif()

get_filename_component(REPO "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE)
# Assertions and the debug file name their source file. Without this they carry the full path of
# the folder the game was built in, which then shows up in every player's crash report.
add_compile_options("-ffile-prefix-map=${REPO}=.")
get_filename_component(MU_TOOLCHAIN_ROOT "${CMAKE_C_COMPILER}" DIRECTORY)
get_filename_component(MU_TOOLCHAIN_ROOT "${MU_TOOLCHAIN_ROOT}/.." ABSOLUTE)
add_compile_options("-ffile-prefix-map=${MU_TOOLCHAIN_ROOT}=toolchain")
set(MELEE_DECOMP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../extern/melee" CACHE PATH "checkout of the game sources")
set(D "${MELEE_DECOMP_DIR}")
if(NOT EXISTS "${D}/src/melee/gm/gmmain.c")
  message(FATAL_ERROR "melee_game: no game sources at ${D}")
endif()
find_package(Python3 COMPONENTS Interpreter REQUIRED)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  # LP64 must never widen the console's explicit long fields or its audio sample
  # buffers. Keep the original source/Windows build intact; compile a lexical
  # overlay that preserves 64-bit integers and host ABI shim declarations.
  file(GLOB_RECURSE MU_CONSOLE_INPUTS CONFIGURE_DEPENDS
    "${D}/src/*" "${D}/libs/dolphin/*")
  set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MU_CONSOLE_INPUTS}
    "${CMAKE_CURRENT_SOURCE_DIR}/../tools/console_long_overlay.py")
  set(MU_CONSOLE_OVERLAY "${CMAKE_CURRENT_BINARY_DIR}/console-lp64")
  execute_process(COMMAND "${Python3_EXECUTABLE}"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tools/console_long_overlay.py" "${D}" "${MU_CONSOLE_OVERLAY}"
    COMMAND_ERROR_IS_FATAL ANY)
  set(D "${MU_CONSOLE_OVERLAY}")
endif()
set(SDK "${D}/libs/dolphin/src/dolphin")
# ---- how game code is compiled: one definition, shared by the game and by the parity library ----
add_library(game_flags INTERFACE)
target_include_directories(game_flags INTERFACE
  "${CMAKE_CURRENT_SOURCE_DIR}/include" "${D}/src" "${D}/src/MSL" "${D}/libs/dolphin/include" "${SDK}"
  "${CMAKE_CURRENT_SOURCE_DIR}/include_stub" "${REPO}/port/runtime/abi")
target_compile_definitions(game_flags INTERFACE MU_NATIVE VERSION_GALE01 BUILD_VERSION=0)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  # GCC 14's scalar replacement loses scalar_storage_order on nested DISC_PTR
  # unions (native_disc_layout returns byte-swapped pointers with -O2). Preserve
  # the actual loads/stores until that optimizer regression is resolved upstream.
  target_compile_options(game_flags INTERFACE -fPIC -fvisibility=hidden -fno-tree-sra)
  add_executable(mu_linux_sdk_layout_test tests/linux_sdk_layout.c)
  target_link_libraries(mu_linux_sdk_layout_test PRIVATE game_flags)
  set_target_properties(mu_linux_sdk_layout_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
  add_test(NAME native_linux_sdk_layout COMMAND mu_linux_sdk_layout_test)
  add_test(NAME native_console_long_overlay COMMAND "${Python3_EXECUTABLE}"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tools/test_console_long_overlay.py")
  add_test(NAME native_game_image_bounds COMMAND "${Python3_EXECUTABLE}"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tools/test_check_game_image.py")
  add_test(NAME native_audio_map_ranges COMMAND "${Python3_EXECUTABLE}"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tools/test_audio_ranges.py")
endif()
target_compile_options(game_flags INTERFACE
  -O2 -g
  -include "${CMAKE_CURRENT_SOURCE_DIR}/include/mu_native.h"   # only C targets link game_flags
  -mno-ms-bitfields          # the console compiler packs a u8 and a u16 bitfield into one run; the MS layout does not
  -fno-strict-aliasing       # the sources read floats through integer pointers throughout
  -fwrapv
  -fno-aggressive-loop-optimizations   # the sources read one entry past a table on a loop's last pass; GCC took that as proof the pass never runs and dropped it (Peach's third down special item)
  -fno-builtin               # no folding sin() at compile time or sqrt() into an instruction: the game's own maths decides
  -ffp-contract=on           # fuse a*b+c only inside one expression, as the console compiler did
  -fexec-charset=CP932       # the game's string literals are Shift-JIS at run time
  -w)
# M0 evidence build only: counts non-finite inputs to the maths routines (include/mu_math_audit.h).
option(MU_MATH_AUDIT "Count non-finite inputs to audited maths routines" OFF)
if(MU_MATH_AUDIT)
  target_compile_definitions(game_flags INTERFACE MU_MATH_AUDIT)
endif()
set(MELEE_CPU_BASELINE "AVX2" CACHE STRING "Instruction set baseline, matching the host: AVX2, AVX or SSE2")
if(MELEE_CPU_BASELINE STREQUAL "AVX2")
  target_compile_options(game_flags INTERFACE -mavx2 -mfma)
elseif(MELEE_CPU_BASELINE STREQUAL "AVX")
  target_compile_options(game_flags INTERFACE -mavx)
endif()
# Disc pointers are 32-bit slots holding real addresses, so everything the game can point at has to
# sit below 4 GB: the image at a fixed base with relocation off, MEM1 placed by the host.
if(WIN32)
  set(LOW_IMAGE_LINK_OPTIONS -static-libgcc -static-libstdc++ -Wl,--disable-dynamicbase -Wl,--disable-high-entropy-va)
  set(LOW_TEST_LINK_OPTIONS ${LOW_IMAGE_LINK_OPTIONS} -Wl,--image-base,0x90000000)
else()
  # A low non-PIE executable is sufficient for pointer-slot unit tests. The game
  # itself remains an ELF shared object at its console-compatible preferred base.
  set(LOW_IMAGE_LINK_OPTIONS -Wl,-Bsymbolic -Wl,-z,defs)
  set(LOW_TEST_LINK_OPTIONS -no-pie -Wl,-Ttext-segment=0x40000000)
endif()

add_executable(mu_disc_layout_test EXCLUDE_FROM_ALL tests/disc_layout.c shim/mu_disc_layout.c)
set_target_properties(mu_disc_layout_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_disc_layout_test PRIVATE game_flags)
add_test(NAME native_disc_layout COMMAND mu_disc_layout_test)

add_executable(mu_command_test EXCLUDE_FROM_ALL tests/command.c "${D}/src/melee/lb/lbcommand.c")
set_target_properties(mu_command_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_command_test PRIVATE game_flags)
add_test(NAME native_command_loop COMMAND mu_command_test)

add_executable(mu_match_test EXCLUDE_FROM_ALL tests/match_override.c shim/mu_match.c)
set_target_properties(mu_match_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_match_test PRIVATE game_flags)
target_include_directories(mu_match_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" "${REPO}/port/runtime/abi")
add_test(NAME native_match_override COMMAND mu_match_test)

# Run the exact production major runner with scene stubs, plus the content hook's real cache calls.
set(CONTENT_MAJOR_RUNNER "${CMAKE_CURRENT_BINARY_DIR}/content_major_runner.h")
if(MELEE_PYTHON)
  set(CONTENT_TEST_PYTHON "${MELEE_PYTHON}")
else()
  find_package(Python3 COMPONENTS Interpreter REQUIRED)
  set(CONTENT_TEST_PYTHON "${Python3_EXECUTABLE}")
endif()
add_custom_command(OUTPUT "${CONTENT_MAJOR_RUNNER}"
  COMMAND "${CONTENT_TEST_PYTHON}" "${REPO}/tools/native_major_fixture.py"
    "${D}/src/melee/gm/gm_1A3F.c" "${CONTENT_MAJOR_RUNNER}"
  DEPENDS "${REPO}/tools/native_major_fixture.py" "${D}/src/melee/gm/gm_1A3F.c"
  VERBATIM)
add_executable(mu_content_transition_test EXCLUDE_FROM_ALL
  tests/content_transition.c shim/mu_content.c "${CONTENT_MAJOR_RUNNER}")
set_target_properties(mu_content_transition_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_content_transition_test PRIVATE game_flags)
target_include_directories(mu_content_transition_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
add_test(NAME native_content_transition COMMAND mu_content_transition_test)

set(CREDITS_PARTICLE_FUNCTION "${CMAKE_CURRENT_BINARY_DIR}/credits_particle_function.h")
add_custom_command(OUTPUT "${CREDITS_PARTICLE_FUNCTION}"
  COMMAND "${CONTENT_TEST_PYTHON}" "${REPO}/tools/native_major_fixture.py"
    "${D}/src/melee/gm/gmstaffroll.c" "${CREDITS_PARTICLE_FUNCTION}" --function gm_801AB200_ptcl
  DEPENDS "${REPO}/tools/native_major_fixture.py" "${D}/src/melee/gm/gmstaffroll.c"
  VERBATIM)
foreach(credits_variant native console)
  add_executable(mu_credits_${credits_variant}_test EXCLUDE_FROM_ALL
    tests/credits_particle.c "${CREDITS_PARTICLE_FUNCTION}")
  set_target_properties(mu_credits_${credits_variant}_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
  target_include_directories(mu_credits_${credits_variant}_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
  if(credits_variant STREQUAL "native")
    target_compile_definitions(mu_credits_${credits_variant}_test PRIVATE MU_NATIVE)
  endif()
  add_test(NAME credits_particle_${credits_variant} COMMAND mu_credits_${credits_variant}_test)
endforeach()

if(NOT MU_NO_SLIPPI)   # tests the replay recording, which a MU_NO_SLIPPI tree does not have (option: see below)
add_executable(mu_record_start_test tests/record_start.c)
set_target_properties(mu_record_start_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_record_start_test PRIVATE game_flags)
target_compile_options(mu_record_start_test PRIVATE -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables)
target_link_options(mu_record_start_test PRIVATE -Wl,--gc-sections)
add_test(NAME native_record_start COMMAND mu_record_start_test)
endif()

add_executable(mu_setjmp_test EXCLUDE_FROM_ALL tests/setjmp.c shim/mu_setjmp.c)
set_target_properties(mu_setjmp_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_setjmp_test PRIVATE game_flags)
add_test(NAME native_setjmp COMMAND mu_setjmp_test)

add_executable(mu_alarm_test EXCLUDE_FROM_ALL tests/alarm.c shim/mu_os.c)
set_target_properties(mu_alarm_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_alarm_test PRIVATE game_flags)
add_test(NAME native_alarm COMMAND mu_alarm_test)

add_executable(mu_preload_compaction_test EXCLUDE_FROM_ALL
  tests/preload_compaction.c "${D}/src/sysdolphin/baselib/archive.c")
set_target_properties(mu_preload_compaction_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_preload_compaction_test PRIVATE game_flags)
target_compile_options(mu_preload_compaction_test PRIVATE
  -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables)
target_link_options(mu_preload_compaction_test PRIVATE ${LOW_TEST_LINK_OPTIONS} -Wl,--gc-sections)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  target_link_options(mu_preload_compaction_test PRIVATE -Wl,--section-start=.test_arena=0x90000000)
endif()
add_test(NAME native_preload_compaction COMMAND mu_preload_compaction_test)

# Exercise the actual private sound-bank bookkeeping without disc/renderer dependencies.
add_executable(mu_synth_bank_test EXCLUDE_FROM_ALL tests/synth_bank.c)
set_target_properties(mu_synth_bank_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_synth_bank_test PRIVATE game_flags)
target_compile_options(mu_synth_bank_test PRIVATE -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables)
target_link_options(mu_synth_bank_test PRIVATE -Wl,--gc-sections)
add_test(NAME native_synth_bank COMMAND mu_synth_bank_test)

# Exercise the production AX voice service path when full and partial address
# updates are coalesced before the DSP copy runs.
add_executable(mu_ax_addr_sync_test tests/ax_addr_sync.c)
set_target_properties(mu_ax_addr_sync_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_ax_addr_sync_test PRIVATE game_flags)
target_compile_options(mu_ax_addr_sync_test PRIVATE -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables)
target_link_options(mu_ax_addr_sync_test PRIVATE -Wl,--gc-sections)
add_test(NAME native_ax_addr_sync COMMAND mu_ax_addr_sync_test)

add_executable(mu_ax_commands_test tests/ax_commands.c)
set_target_properties(mu_ax_commands_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_ax_commands_test PRIVATE game_flags)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  target_link_libraries(mu_ax_commands_test PRIVATE m)
endif()
target_compile_options(mu_ax_commands_test PRIVATE -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables)
target_link_options(mu_ax_commands_test PRIVATE -Wl,--gc-sections)
add_test(NAME native_ax_commands COMMAND mu_ax_commands_test)

add_executable(mu_synth_ratio_test tests/synth_ratio.c)
set_target_properties(mu_synth_ratio_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_synth_ratio_test PRIVATE game_flags)
target_compile_options(mu_synth_ratio_test PRIVATE -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables)
target_link_options(mu_synth_ratio_test PRIVATE -Wl,--gc-sections)
add_test(NAME native_synth_ratio COMMAND mu_synth_ratio_test)
# Wobbuffet's decaying counter damage converts below zero as the console does (B-281).
add_executable(mu_sonans_damage_test EXCLUDE_FROM_ALL tests/sonans_damage.c tests/sonans_damage_stubs.c)
set_target_properties(mu_sonans_damage_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_sonans_damage_test PRIVATE game_flags)
target_compile_options(mu_sonans_damage_test PRIVATE -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables)
target_link_options(mu_sonans_damage_test PRIVATE -Wl,--gc-sections)
add_test(NAME native_sonans_damage COMMAND mu_sonans_damage_test)

# Verify field-aware card payload byte-order conversion for save and name-tag data.
add_executable(mu_card_endian_test tests/card_endian.c "${D}/src/melee/lb/lbcardserial.c")
set_target_properties(mu_card_endian_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_card_endian_test PRIVATE game_flags)
add_test(NAME native_card_endian COMMAND mu_card_endian_test)

# Every annotated DISC_STRUCT field sits at its console offset in the host layout (tools/disc_offset_audit.py).
# An 8-byte pointer field in a disc struct shifted Link's attributes from +0x94 and went unnoticed.
add_test(NAME native_disc_offsets COMMAND "${CONTENT_TEST_PYTHON}" "${REPO}/tools/disc_offset_audit.py"
  --build "${CMAKE_CURRENT_BINARY_DIR}" --decomp "${D}" --ninja "${CMAKE_MAKE_PROGRAM}")

# ---- source implementations of assembly-only SDK maths ----
# Native game builds never consume generated PPC translations or a retail DOL.
# The retail comparison belongs exclusively to the host's parity test oracle.
add_library(sdk_math OBJECT math/sdk_math.c math/estimates.c)
target_link_libraries(sdk_math PRIVATE game_flags)
target_compile_options(sdk_math PRIVATE -ffp-contract=off)
set_target_properties(sdk_math PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)

# ---- what is compiled ----
file(GLOB_RECURSE GAME_SOURCES CONFIGURE_DEPENDS "${D}/src/melee/*.c" "${D}/src/sysdolphin/*.c")
# Define the CPU's hitlag decision in a private input, preserving the user's decomp checkout.
set(MU_CPU_SOURCE "${D}/src/melee/ft/kinds/ftCommon/ftCo_0A01.c")
set(MU_CPU_FIXED "${CMAKE_CURRENT_BINARY_DIR}/native-cpu/ftCo_0A01.c")
add_custom_command(OUTPUT "${MU_CPU_FIXED}"
  COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../tools/native_cpu_overlay.py"
    "${MU_CPU_SOURCE}" "${MU_CPU_FIXED}"
  DEPENDS "${MU_CPU_SOURCE}" "${CMAKE_CURRENT_SOURCE_DIR}/../tools/native_cpu_overlay.py"
  VERBATIM)
list(REMOVE_ITEM GAME_SOURCES "${MU_CPU_SOURCE}")
list(APPEND GAME_SOURCES "${MU_CPU_FIXED}")
set_source_files_properties("${MU_CPU_FIXED}" PROPERTIES
  INCLUDE_DIRECTORIES "${D}/src/melee/ft/kinds/ftCommon")
set(MU_CPU_HITLAG_FIXTURE "${CMAKE_CURRENT_BINARY_DIR}/native_cpu_hitlag_branch.h")
add_custom_command(OUTPUT "${MU_CPU_HITLAG_FIXTURE}"
  COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../tools/native_cpu_overlay.py"
    "${MU_CPU_SOURCE}" "${MU_CPU_HITLAG_FIXTURE}" --hitlag-fixture
  DEPENDS "${MU_CPU_SOURCE}" "${CMAKE_CURRENT_SOURCE_DIR}/../tools/native_cpu_overlay.py"
  VERBATIM)
add_executable(mu_cpu_hitlag_test EXCLUDE_FROM_ALL tests/cpu_hitlag.c "${MU_CPU_HITLAG_FIXTURE}")
set_target_properties(mu_cpu_hitlag_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
target_include_directories(mu_cpu_hitlag_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
add_test(NAME native_cpu_hitlag COMMAND mu_cpu_hitlag_test)
# The console's C library is kept only where its results reach the simulation: the maths, and rand.
set(LIBC_SOURCES "${D}/src/MSL/math.c" "${D}/src/MSL/math_1.c" "${D}/src/MSL/math_data.c" "${D}/src/MSL/trigf.c" "${D}/src/MSL/float.c" "${D}/src/MSL/rand.c")
# The halves of the SDK that sit above the hardware. What sat below it is the host's job.
file(GLOB SDK_SOURCES CONFIGURE_DEPENDS "${SDK}/gx/*.c" "${SDK}/ax/*.c" "${SDK}/axfx/*.c" "${SDK}/mtx/*.c"
  "${SDK}/thp/*.c")   # THP is a video decoder: computation, no hardware. MCC/FIO: see shim/mu_devkit.c
list(APPEND SDK_SOURCES "${SDK}/os/OSAlloc.c" "${SDK}/os/OSArena.c" "${SDK}/pad/PadClamp.c")
# Written for the hardware itself, or in its assembly; the native build has its own versions of these.
list(REMOVE_ITEM SDK_SOURCES "${SDK}/gx/GXFifo.c" "${SDK}/mtx/psmtx.c")
file(GLOB_RECURSE SHIM_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/shim/*.c")
# A game library with no Slippi code in it (-DMU_NO_SLIPPI=ON, its own build directory): the online
# menus, replay playback and recording, practice matchmaking and the online rule codes are left out.
# Three small files stand in for them: the entry points the game sources call (shim/mu_noslippi.c),
# the match block copy a network match starts from (shim/mu_match_info.c) and the menu-less entry
# into such a match (shim/mu_p2p_entry.c). The rollback engine itself (shim/mu_online*.c) stays.
# The library keeps its names: melee_game.dll, .dbg and .snapexcl.
option(MU_NO_SLIPPI "Build the game without the Slippi menus, replays and online codes" OFF)
set(MU_NO_SLIPPI_ONLY mu_noslippi.c mu_match_info.c mu_p2p_entry.c)
if(MU_NO_SLIPPI)
  foreach(removed mu_slippi_menu.c mu_slippi_css.c mu_slippi_chat.c mu_slippi_splash.c mu_slippi_sss.c
                  mu_replay.c mu_replay_abi.c mu_practice.c mu_online_rules.c mu_framedata.c
                  mu_replay_defend.c)
    list(REMOVE_ITEM SHIM_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/shim/${removed}")
  endforeach()
  target_compile_definitions(game_flags INTERFACE MU_NO_SLIPPI)
else()
  foreach(removed ${MU_NO_SLIPPI_ONLY})
    list(REMOVE_ITEM SHIM_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/shim/${removed}")
  endforeach()
endif()
# Replacements for SDK files written against the hardware itself (see the REMOVE_ITEM above).
file(GLOB GXNATIVE_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/gxnative/*.c")
list(APPEND SHIM_SOURCES ${GXNATIVE_SOURCES})
# The SDK's AXOut.c with the Faster sound effects switch (axnative/AXOut.c).
list(REMOVE_ITEM SDK_SOURCES "${SDK}/ax/AXOut.c")
list(APPEND SHIM_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/axnative/AXOut.c")
# Akaneia's added fighters, native (akaneia/INTEGRATION.md): the registry and each fighter's own C.
# A fighter is offered on the character select only when its descriptor carries MU_AK_READY
# (mu_ak_fighters.c); the others stay locked. ACE's fighters (ace/) build only with
# -DMU_ACE_FIGHTERS=ON until their own checks are done.
option(MU_AKANEIA_FIGHTERS "Build the native Akaneia fighters" ON)
option(MU_ACE_FIGHTERS "Build the native ACE fighters as well" ON)
list(APPEND SHIM_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/akaneia/mu_ak_fighters.c")
if(MU_AKANEIA_FIGHTERS)
  target_compile_definitions(game_flags INTERFACE MU_AKANEIA_FIGHTERS=1)
  add_executable(mu_ak_character_kind_test EXCLUDE_FROM_ALL tests/ak_character_kind.c)
  set_target_properties(mu_ak_character_kind_test PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
  target_link_libraries(mu_ak_character_kind_test PRIVATE game_flags)
  target_compile_options(mu_ak_character_kind_test PRIVATE -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables)
  target_link_options(mu_ak_character_kind_test PRIVATE -Wl,--gc-sections)
  add_test(NAME native_ak_character_kind COMMAND mu_ak_character_kind_test)
  file(GLOB_RECURSE AKANEIA_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/akaneia/*/*.c")
  if(MU_ACE_FIGHTERS)
    file(GLOB_RECURSE ACE_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/ace/*/*.c")
    list(APPEND AKANEIA_SOURCES ${ACE_SOURCES})
  endif()
  list(APPEND SHIM_SOURCES ${AKANEIA_SOURCES})
  # The registry list: one line per fighter descriptor in those sources (a line that starts
  # "const MuAkFighter mu_ak_<name> = "), so a new fighter folder under akaneia/ or ace/ needs no
  # edit here or in mu_ak_fighters.c. Track contents as well as added/removed files so a
  # descriptor added to an existing source refreshes the registry on the next build.
  set(MU_AK_REGISTRY "")
  foreach(ak_source ${AKANEIA_SOURCES})
    set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${ak_source}")
    file(STRINGS "${ak_source}" ak_lines REGEX "^const MuAkFighter mu_ak_[A-Za-z0-9_]+ = ")
    foreach(ak_line ${ak_lines})
      string(REGEX REPLACE "^const MuAkFighter (mu_ak_[A-Za-z0-9_]+) = .*$" "\\1" ak_symbol "${ak_line}")
      string(APPEND MU_AK_REGISTRY "MU_AK_FIGHTER(${ak_symbol})\n")
    endforeach()
  endforeach()
  file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mu_ak/mu_ak_registry.inc" CONTENT "${MU_AK_REGISTRY}")
  set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/akaneia/mu_ak_fighters.c" APPEND PROPERTY
    COMPILE_DEFINITIONS MU_AK_REGISTRY_GENERATED)
  set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/akaneia/mu_ak_fighters.c" APPEND PROPERTY
    INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/mu_ak")
endif()
# A fighter joins the registry once akaneia/<fighter>/<fighter>.c exists (that file defines
# mu_ak_<fighter>); until then the registry leaves it out and the fighter stays locked.
foreach(ak_fighter wolf diddy charizard lucas sonic dedede tails)
  if(MU_AKANEIA_FIGHTERS AND (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/akaneia/${ak_fighter}/${ak_fighter}.c"
      OR EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/akaneia/${ak_fighter}/ft${ak_fighter}.c"))
    string(TOUPPER "${ak_fighter}" ak_upper)
    set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/akaneia/mu_ak_fighters.c" APPEND PROPERTY
      COMPILE_DEFINITIONS "MU_AK_HAVE_${ak_upper}")
  endif()
endforeach()

# m-ex stages with their own code, native (shim/mu_mex_stage.h, run-source/rel09-stages/LAYER.md
# phase B): each stage's C under mexstages/<stage>/, helpers in mexstages/common/. The registry is
# one line per "const MuGrStage mu_gr_<name> = " line in those sources, so a new stage folder needs
# no edit here. A stage is offered only when its descriptor carries MU_GR_READY; without this option
# every row that needs its own code stays locked (the data only rows play either way).
option(MU_MEX_STAGES "Build the native m-ex stage code (mexstages/)" ON)
if(MU_MEX_STAGES)
  file(GLOB_RECURSE MEX_STAGE_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/mexstages/*/*.c")
  list(APPEND SHIM_SOURCES ${MEX_STAGE_SOURCES})
  set(MU_GR_REGISTRY "")
  foreach(gr_source ${MEX_STAGE_SOURCES})
    set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${gr_source}")
    file(STRINGS "${gr_source}" gr_lines REGEX "^const MuGrStage mu_gr_[A-Za-z0-9_]+ = ")
    foreach(gr_line ${gr_lines})
      string(REGEX REPLACE "^const MuGrStage (mu_gr_[A-Za-z0-9_]+) = .*$" "\\1" gr_symbol "${gr_line}")
      string(APPEND MU_GR_REGISTRY "MU_GR_STAGE(${gr_symbol})\n")
    endforeach()
  endforeach()
  file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/mu_gr/mu_gr_registry.inc" CONTENT "${MU_GR_REGISTRY}")
  set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/shim/mu_mex_stage.c" APPEND PROPERTY
    COMPILE_DEFINITIONS MU_GR_REGISTRY_GENERATED)
  set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/shim/mu_mex_stage.c" APPEND PROPERTY
    INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/mu_gr")
endif()

# rogueMelee's Rogue Mode, native (rogue/): its own C under rogue/melee/rogue (the include root is
# rogue/, so the decomp's <melee/rogue/...> includes find it), and its edits to the game's source in
# melee-native.patch, each under #if MU_ROGUE. Offered only with its disc, offline (shim side:
# rogue/mu_rogue.c). The flags are the release values of its own build (tools/lib/integration.py).
option(MU_ROGUE "Build rogueMelee's Rogue Mode natively (rogue/)" ON)
if(MU_ROGUE)
  target_compile_definitions(game_flags INTERFACE MU_ROGUE=1 ROGUE_ENABLE_AERIALS=1 ROGUE_DEBUG=0
    ROGUE_QA_EVENT_RELEASE=0 ROGUE_QA_CYCLES=0 ROGUE_QA_MODE=0 ROGUE_QA_MATCHES=20 ROGUE_QA_SETTLEMENT=0
    ROGUE_QA_RECOVERY=0 ROGUE_QA_LIFECYCLE=0 ROGUE_QA_AERIAL_STRESS=0 ROGUE_QA_SPECIAL_START=0
    ROGUE_QA_SPECIAL_COUNT=104)
  target_include_directories(game_flags INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/rogue")
  file(GLOB_RECURSE ROGUE_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/rogue/*.c")
  list(APPEND SHIM_SOURCES ${ROGUE_SOURCES})
  # Melee Maker's borrowed-move engine, native (maker/, include root for its <engine/...>, <bam/...>,
  # <platform/...>). Its edits share rogueMelee's lines in the game's source (include/mu_bm.h picks the
  # engine of the loaded disc), so it builds with MU_ROGUE. Shim side: maker/mu_maker.c.
  target_compile_definitions(game_flags INTERFACE MU_MAKER=1 BAM_ENABLE_AERIALS=1 BAM_DEBUG=0
    BAM_VERSION="1.3.8")
  target_include_directories(game_flags INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/maker")
  file(GLOB_RECURSE MAKER_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/maker/*.c")
  list(APPEND SHIM_SOURCES ${MAKER_SOURCES})
endif()

if(NOT Python3_EXECUTABLE)
  find_package(Python3 COMPONENTS Interpreter REQUIRED)
endif()

# ---- Training Mode CE, compiled natively (tmce/) ----
# TM-CE's own C, against MexTK headers regenerated for the native layouts (tools/tmce/gen_mextk.py).
# Each module is linked on its own as on the console, where each is a separate .dat file: its
# globals stay private, its exports get the module's prefix, and its MexTK calls are renamed to the
# decomp functions at the same console addresses (tools/tmce/link_module.py).
set(TMCE "${CMAKE_CURRENT_SOURCE_DIR}/tmce")
get_filename_component(TMCE_TOOLCHAIN_BIN "${CMAKE_C_COMPILER}" DIRECTORY)
# No debug info: TM-CE declares types with the decomp's names (ColorOverlay, CollData...), and the
# layout tools (tools/tmce/gdb_decomp_info.py) must find only the decomp's in melee_game.dbg.
set(TMCE_FLAGS -O2 -g0 -funsigned-char -fno-strict-aliasing -fwrapv -mno-ms-bitfields -mcmodel=small
  -fno-builtin -ffp-contract=on -w)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  list(APPEND TMCE_FLAGS -fPIC -fvisibility=hidden -fno-tree-sra)
endif()
if(MELEE_CPU_BASELINE STREQUAL "AVX2")
  list(APPEND TMCE_FLAGS -mavx2 -mfma)
elseif(MELEE_CPU_BASELINE STREQUAL "AVX")
  list(APPEND TMCE_FLAGS -mavx)
endif()
set(TMCE_OBJECTS)
function(tmce_module name exports)
  cmake_parse_arguments(M "" "" "SOURCES;KEEP" ${ARGN})
  add_library(tmce_${name}_objs OBJECT ${M_SOURCES})
  target_compile_definitions(tmce_${name}_objs PRIVATE MU_NATIVE)
  target_include_directories(tmce_${name}_objs PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")  # mu_gxpipe.h
  target_compile_options(tmce_${name}_objs PRIVATE ${TMCE_FLAGS})
  set_target_properties(tmce_${name}_objs PROPERTIES C_STANDARD 11 C_EXTENSIONS ON)
  set(keep)
  foreach(k ${M_KEEP})
    list(APPEND keep --keep "${k}")
  endforeach()
  set(out "${CMAKE_CURRENT_BINARY_DIR}/tmce_${name}.o")
  add_custom_command(OUTPUT "${out}"
    COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../../tools/tmce/link_module.py"
            --ld "${CMAKE_LINKER}" --nm "${CMAKE_NM}" --objcopy "${CMAKE_OBJCOPY}" --name ${name} --exports "${exports}"
            ${keep} --redefine "${TMCE}/MexTK/mextk_redefine.txt" --out "${out}" $<TARGET_OBJECTS:tmce_${name}_objs>
    DEPENDS tmce_${name}_objs $<TARGET_OBJECTS:tmce_${name}_objs> "${TMCE}/MexTK/mextk_redefine.txt"
            "${CMAKE_CURRENT_SOURCE_DIR}/../../tools/tmce/link_module.py"
    COMMAND_EXPAND_LISTS VERBATIM)
  set(TMCE_OBJECTS ${TMCE_OBJECTS} "${out}" PARENT_SCOPE)
endfunction()
# The runtime: the loader, the C library shims, and the native parts written against TM-CE's own
# types (the original UnclePunch events in C, the on-screen displays).
file(GLOB TMCE_NATIVE_SOURCES CONFIGURE_DEPENDS "${TMCE}/native/*.c")
tmce_module(runtime - SOURCES ${TMCE_NATIVE_SOURCES} KEEP "mu_tmce_*" "mex_*" "MEX_*")
tmce_module(eventMenu "${TMCE}/MexTK/tmFunction.txt" SOURCES
  "${TMCE}/src/events.c" "${TMCE}/src/menu.c" "${TMCE}/src/osds.c" "${TMCE}/src/savestate_v1.c")
tmce_module(labCSS "${TMCE}/MexTK/cssFunction.txt" SOURCES "${TMCE}/src/lab_css.c")
foreach(ev lab lcancel ledgedash wavedash powershield dthrowknee edgeguard fc sweetspot laserland eggs techchase slalom)
  tmce_module(${ev} "${TMCE}/MexTK/evFunction.txt" SOURCES "${TMCE}/src/${ev}.c")
endforeach()
set_source_files_properties(${TMCE_OBJECTS} PROPERTIES EXTERNAL_OBJECT TRUE GENERATED TRUE)

add_library(melee_game SHARED ${GAME_SOURCES} ${LIBC_SOURCES} ${SDK_SOURCES} ${SHIM_SOURCES} $<TARGET_OBJECTS:sdk_math>
  ${TMCE_OBJECTS})
set_target_properties(melee_game PROPERTIES PREFIX "" C_STANDARD 11 C_EXTENSIONS ON EXCLUDE_FROM_ALL ON)
target_link_libraries(melee_game PRIVATE game_flags)
# Right after MEM1 (see port/app/source_host.cpp): the image's statics then have physical addresses
# GX can hold. The debug information goes to melee_game.dbg so the loaded image stays small enough
# to fit below 64 MB physical; addr2line reads the .dbg.
target_link_options(melee_game PRIVATE ${LOW_IMAGE_LINK_OPTIONS}
  "-Wl,-Map,${CMAKE_CURRENT_BINARY_DIR}/melee_game.map")
if(WIN32)
  target_link_options(melee_game PRIVATE -Wl,--image-base,0x82800000)
else()
  target_link_libraries(melee_game PRIVATE m)
  set(MU_ELF_LAYOUT "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/game-linux-layout.ld")
  target_link_options(melee_game PRIVATE -Wl,-Ttext-segment=0x82800000 "-Wl,-T,${MU_ELF_LAYOUT}")
  set_property(TARGET melee_game APPEND PROPERTY LINK_DEPENDS "${MU_ELF_LAYOUT}")
  add_test(NAME native_elf_game_load COMMAND "${Python3_EXECUTABLE}"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tools/check_elf_load.py" $<TARGET_FILE:melee_game>)
endif()
add_custom_command(TARGET melee_game POST_BUILD
  COMMAND "${CMAKE_OBJCOPY}" --only-keep-debug $<TARGET_FILE:melee_game> $<TARGET_FILE_DIR:melee_game>/melee_game.dbg
  COMMAND "${CMAKE_OBJCOPY}" --strip-debug --add-gnu-debuglink=$<TARGET_FILE_DIR:melee_game>/melee_game.dbg $<TARGET_FILE:melee_game>
  COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../tools/check_game_image.py"
          $<TARGET_FILE:melee_game> --base 0x82800000
  # The sound driver's whole state, left out of rollback snapshots as a console savestate does.
  COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../../tools/snapshot_audio_ranges.py"
          "${CMAKE_CURRENT_BINARY_DIR}/melee_game.map" 82800000 $<TARGET_FILE_DIR:melee_game>/melee_game.snapexcl
          $<TARGET_FILE:melee_game>
  VERBATIM)

# ---- parity library ----
# Native routines that are meant to agree bit for bit with the translated game, offered to the MSVC
# harness (port/tests/native_parity.cpp), which runs each against its twin with the same inputs.
# Pure collision routines copied out of lbcollision.c (which needs the whole game to link).
if(NOT Python3_EXECUTABLE)
  find_package(Python3 COMPONENTS Interpreter REQUIRED)
endif()
set(MU_TOOLS "${CMAKE_CURRENT_SOURCE_DIR}/../../tools")
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/extracted_lbcoll.c"
  COMMAND "${Python3_EXECUTABLE}" "${MU_TOOLS}/fma_exact/extract_funcs.py"
          "${D}/src/melee/lb/lbcollision.c" "${CMAKE_CURRENT_BINARY_DIR}/extracted_lbcoll.c" lbColl_80005EBC lbColl_80006094 lbColl_80006E58
  DEPENDS "${D}/src/melee/lb/lbcollision.c" "${MU_TOOLS}/fma_exact/extract_funcs.py" VERBATIM)
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/extracted_mplib.c"
  COMMAND "${Python3_EXECUTABLE}" "${MU_TOOLS}/fma_exact/extract_funcs.py"
          "${D}/src/melee/mp/mplib.c" "${CMAKE_CURRENT_BINARY_DIR}/extracted_mplib.c" mpLineIntersection mpLineIntersectionH
  DEPENDS "${D}/src/melee/mp/mplib.c" "${MU_TOOLS}/fma_exact/extract_funcs.py" VERBATIM)
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/extracted_spline.c"
  COMMAND "${Python3_EXECUTABLE}" "${MU_TOOLS}/fma_exact/extract_funcs.py"
          "${D}/src/sysdolphin/baselib/spline.c" "${CMAKE_CURRENT_BINARY_DIR}/extracted_spline.c" splGetHelmite
  DEPENDS "${D}/src/sysdolphin/baselib/spline.c" "${MU_TOOLS}/fma_exact/extract_funcs.py" VERBATIM)
add_library(mu_parity SHARED parity/parity_table.c parity/parity_stubs.c shim/mu_math.c $<TARGET_OBJECTS:sdk_math>
  "${CMAKE_CURRENT_BINARY_DIR}/extracted_lbcoll.c" "${CMAKE_CURRENT_BINARY_DIR}/extracted_mplib.c"
  "${CMAKE_CURRENT_BINARY_DIR}/extracted_spline.c"
  "${D}/src/MSL/trigf.c" "${D}/src/MSL/math_data.c" "${D}/src/MSL/math_1.c" "${D}/src/MSL/math.c" "${D}/src/MSL/float.c"
  "${D}/src/melee/lb/lbvector.c" "${D}/src/melee/lb/lbtrigf.c" "${D}/src/melee/lb/lb_00CE.c"
  "${D}/src/sysdolphin/baselib/mtx.c" "${D}/src/sysdolphin/baselib/quatlib.c")
set_target_properties(mu_parity PROPERTIES PREFIX "" C_STANDARD 11 C_EXTENSIONS ON)
target_link_libraries(mu_parity PRIVATE game_flags)
target_link_options(mu_parity PRIVATE ${LOW_IMAGE_LINK_OPTIONS})
if(WIN32)
  target_link_options(mu_parity PRIVATE -Wl,--image-base,0x58000000)
else()
  target_link_libraries(mu_parity PRIVATE m)
  target_link_options(mu_parity PRIVATE -Wl,-Ttext-segment=0x58000000)
endif()

# Record the actual configured game inputs for the source-only package audit.
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/native-game-sources.txt"
  CONTENT "$<JOIN:$<TARGET_PROPERTY:melee_game,SOURCES>,\n>\n$<JOIN:$<TARGET_PROPERTY:sdk_math,SOURCES>,\n>\n")

# Extract the actual Training exit routine, so an absent CPU exercises the same code that ships.
set(MU_TRAINING_FIXTURE "${CMAKE_CURRENT_BINARY_DIR}/training_select_body.h")
add_custom_command(OUTPUT "${MU_TRAINING_FIXTURE}"
  COMMAND "${Python3_EXECUTABLE}" "${REPO}/tools/native_major_fixture.py"
          "${D}/src/melee/gm/gmtrainingmode.c" "${MU_TRAINING_FIXTURE}" --function gm_801B1C24
  DEPENDS "${D}/src/melee/gm/gmtrainingmode.c" "${REPO}/tools/native_major_fixture.py" VERBATIM)
add_executable(mu_training_select_test EXCLUDE_FROM_ALL tests/training_select.c "${MU_TRAINING_FIXTURE}")
target_include_directories(mu_training_select_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_compile_options(mu_training_select_test PRIVATE -UNDEBUG)
add_test(NAME native_training_select COMMAND mu_training_select_test)

set(MU_FIGHTER_SCREEN_FIXTURE "${CMAKE_CURRENT_BINARY_DIR}/fighter_screen_position_body.h")
add_custom_command(OUTPUT "${MU_FIGHTER_SCREEN_FIXTURE}"
  COMMAND "${Python3_EXECUTABLE}" "${REPO}/tools/native_major_fixture.py"
          "${D}/src/melee/if/ifnametag.c" "${MU_FIGHTER_SCREEN_FIXTURE}" --function mu_fighter_screen_position
  DEPENDS "${D}/src/melee/if/ifnametag.c" "${REPO}/tools/native_major_fixture.py" VERBATIM)
add_executable(mu_fighter_screen_position_test EXCLUDE_FROM_ALL tests/fighter_screen_position.c "${MU_FIGHTER_SCREEN_FIXTURE}")
target_include_directories(mu_fighter_screen_position_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_compile_options(mu_fighter_screen_position_test PRIVATE -UNDEBUG)
add_test(NAME native_fighter_screen_position COMMAND mu_fighter_screen_position_test)

set(MU_SIS_STACK_FIXTURES)
foreach(MU_SIS_STACK_FUNCTION HSD_SisLib_803A7684 HSD_SisLib_803A7F0C sisRejectInvalidCursor)
  set(MU_SIS_STACK_BODY "${CMAKE_CURRENT_BINARY_DIR}/${MU_SIS_STACK_FUNCTION}_body.h")
  add_custom_command(OUTPUT "${MU_SIS_STACK_BODY}"
    COMMAND "${Python3_EXECUTABLE}" "${REPO}/tools/native_major_fixture.py"
            "${D}/src/sysdolphin/baselib/hsd_3A76.c" "${MU_SIS_STACK_BODY}" --function "${MU_SIS_STACK_FUNCTION}"
    DEPENDS "${D}/src/sysdolphin/baselib/hsd_3A76.c" "${REPO}/tools/native_major_fixture.py" VERBATIM)
  list(APPEND MU_SIS_STACK_FIXTURES "${MU_SIS_STACK_BODY}")
endforeach()
add_executable(mu_sis_state_stack_test EXCLUDE_FROM_ALL tests/sis_state_stack.c ${MU_SIS_STACK_FIXTURES})
target_include_directories(mu_sis_state_stack_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_compile_options(mu_sis_state_stack_test PRIVATE -UNDEBUG)
add_test(NAME native_sis_state_stack COMMAND mu_sis_state_stack_test)

function(mu_boundary_fixture name source)
  set(bodies)
  foreach(fn IN LISTS ARGN)
    set(body "${CMAKE_CURRENT_BINARY_DIR}/${fn}_body.h")
    add_custom_command(OUTPUT "${body}"
      COMMAND "${Python3_EXECUTABLE}" "${REPO}/tools/native_major_fixture.py" "${source}" "${body}" --function "${fn}"
      DEPENDS "${source}" "${REPO}/tools/native_major_fixture.py" VERBATIM)
    list(APPEND bodies "${body}")
  endforeach()
  add_executable(mu_${name}_test EXCLUDE_FROM_ALL tests/${name}.c ${bodies})
  target_include_directories(mu_${name}_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
  target_compile_options(mu_${name}_test PRIVATE -UNDEBUG)
  add_test(NAME native_${name} COMMAND mu_${name}_test)
endfunction()
mu_boundary_fixture(hud_nana "${CMAKE_CURRENT_SOURCE_DIR}/shim/mu_hud_scale.c" mu_live_fighter mu_nana_damage)
mu_boundary_fixture(practice_destination "${CMAKE_CURRENT_SOURCE_DIR}/shim/mu_practice.c"
  mu_practice_resolve_pending_mode mu_practice_enter_mode)
mu_boundary_fixture(skin_cycle_bridge "${CMAKE_CURRENT_SOURCE_DIR}/shim/mu_content.c" mu_skin_cycle)
mu_boundary_fixture(costume_parts "${D}/src/melee/ft/ftparts.c" ftParts_80074D7C ftParts_80074CA0)
mu_boundary_fixture(stage_item_link "${D}/src/melee/it/kinds/itwhitebea.c" mu_whitebea_block_alive mu_whitebea_validate_block)
mu_boundary_fixture(envelope_ready "${D}/src/sysdolphin/baselib/pobj.c" PObjNativeEnvelopeReady)
mu_boundary_fixture(fod_scenery "${D}/src/melee/gr/grizumi.c"
  mu_fod_joint_uses_image mu_fod_scenery_plan mu_fod_draw_scenery mu_fod_render_reflection_scene)

add_test(NAME native_collision_geometry
  COMMAND "${Python3_EXECUTABLE}" "${REPO}/tools/check_collision_geometry.py" "${D}/src/melee/lb/lbcollision.c")

# Build every registered executable fixture, including normally excluded targets, before CTest.
get_property(MU_GAME_DIRECTORY_TARGETS DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
set(MU_GAME_TEST_TARGETS)
foreach(mu_target IN LISTS MU_GAME_DIRECTORY_TARGETS)
  get_target_property(mu_target_type "${mu_target}" TYPE)
  if(mu_target_type STREQUAL "EXECUTABLE" AND mu_target MATCHES "^mu_.*_test$")
    list(APPEND MU_GAME_TEST_TARGETS "${mu_target}")
  endif()
endforeach()
add_custom_target(mu_game_tests DEPENDS melee_game ${MU_GAME_TEST_TARGETS})
