cmake_minimum_required(VERSION 3.25)
project(MeleeNative LANGUAGES C CXX)
if(APPLE)
  enable_language(OBJCXX)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  add_compile_definitions(_GNU_SOURCE)
  link_libraries(m pthread)
  # Use Aurora's pinned static SQLite on every Linux host, as validated on the
  # ThinkPad. A runner's preinstalled SQLite must not change the bundle's ABI.
  set(CMAKE_DISABLE_FIND_PACKAGE_SQLite3 TRUE CACHE BOOL "Use pinned SQLite" FORCE)
endif()
include(CheckCCompilerFlag)
check_c_compiler_flag("-Wno-return-mismatch" MELEE_HAS_RETURN_MISMATCH_WARNING)
if(MELEE_HAS_RETURN_MISMATCH_WARNING)
  set(MELEE_RETURN_WARNING -Wno-return-mismatch)
else()
  set(MELEE_RETURN_WARNING -Wno-return-type)
endif()
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
get_filename_component(MELEE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tools/prepare_sdk.py")
execute_process(COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tools/prepare_sdk.py"
  "${CMAKE_CURRENT_BINARY_DIR}/sdk" COMMAND_ERROR_IS_FATAL ANY)
file(GLOB_RECURSE GAME_SOURCES CONFIGURE_DEPENDS
  "${MELEE_ROOT}/src/melee/*.c" "${MELEE_ROOT}/src/sysdolphin/*.c")
add_library(melee_game OBJECT ${GAME_SOURCES})
target_include_directories(melee_game PRIVATE include "${MELEE_ROOT}/src" "${CMAKE_CURRENT_BINARY_DIR}/sdk")
target_compile_definitions(melee_game PRIVATE TARGET_PC MELEE_NATIVE BUILD_VERSION=0 VERSION_GALE01)
target_compile_options(melee_game PRIVATE -include "${CMAKE_CURRENT_SOURCE_DIR}/include/compat.h"
  -fno-strict-aliasing -fgnu89-inline -ffp-contract=off -Wno-incompatible-pointer-types -Wno-int-conversion
  -Wno-deprecated-non-prototype -Wno-macro-redefined -Wno-pointer-sign
  ${MELEE_RETURN_WARNING})

# Aurora is pinned because its native SDK ABI is part of the port.
set(MELEE_AURORA_DIR "${MELEE_ROOT}/build/native-deps/aurora" CACHE PATH "Aurora source checkout")
find_package(Git REQUIRED)
execute_process(COMMAND "${GIT_EXECUTABLE}" -C "${MELEE_AURORA_DIR}" rev-parse HEAD
  OUTPUT_VARIABLE MELEE_AURORA_REV OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ERROR_IS_FATAL ANY)
if(NOT MELEE_AURORA_REV STREQUAL "749d6ee7a22bdfab78c8ece9047bca5d79aa72ca")
  message(FATAL_ERROR "Unexpected Aurora revision. Run native/tools/bootstrap.py in a fresh dependency directory.")
endif()
set(AURORA_DAWN_PROVIDER package CACHE STRING "" FORCE)
set(AURORA_SDL3_PROVIDER vendor CACHE STRING "" FORCE)
set(AURORA_NOD_PROVIDER package CACHE STRING "" FORCE)
add_subdirectory("${MELEE_AURORA_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/aurora" EXCLUDE_FROM_ALL)
# Record the actual dependency sources, including FetchContent source overrides.
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/package-licenses.txt" "")
foreach(entry "SDL|SDL|LICENSE.txt" "ImGui|imgui|LICENSE.txt" "Abseil|abseil-cpp|LICENSE"
              "Tracy|tracy|LICENSE" "xxHash|xxhash|LICENSE")
  string(REPLACE "|" ";" fields "${entry}")
  list(GET fields 0 notice)
  list(GET fields 1 dependency)
  list(GET fields 2 filename)
  FetchContent_GetProperties(${dependency} SOURCE_DIR dependency_source)
  file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/package-licenses.txt" "${notice}.txt\t${dependency_source}/${filename}\n")
endforeach()
# Keep the game debuggable while avoiding unoptimized texture-cache iteration
# and GX translation in the renderer. Debug assertions remain enabled.
foreach(backend aurora_core aurora_gx)
  target_compile_options(${backend} PRIVATE $<$<CONFIG:Debug>:-O2>)
endforeach()
set_source_files_properties("${MELEE_ROOT}/src/melee/gm/gmmain.c" PROPERTIES COMPILE_DEFINITIONS main=melee_game_main)
add_library(melee_os_runtime OBJECT os_runtime.c)
target_include_directories(melee_os_runtime PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(melee_os_runtime PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(melee_os_runtime PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS>)
add_executable(melee_native EXCLUDE_FROM_ALL runtime_main.cpp disc_fonts.cpp gx_bridge.cpp vi_runtime.cpp pad_bridge.cpp "${MELEE_ROOT}/src/MSL/float.c" $<TARGET_OBJECTS:melee_game> $<TARGET_OBJECTS:melee_os_runtime>)
target_link_libraries(melee_native PRIVATE aurora::core aurora::gx aurora::os aurora::dvd
  aurora::card aurora::mtx aurora::pad aurora::vi aurora::thp)
if(APPLE)
  target_link_options(melee_native PRIVATE -Wl,-dead_strip)
  set_target_properties(melee_native PROPERTIES OUTPUT_NAME melee_mac)
  add_custom_target(melee_mac DEPENDS melee_native)
else()
  target_link_options(melee_native PRIVATE -Wl,--gc-sections)
endif()

enable_testing()
add_executable(native_core_test tests/core_test.c
  "${MELEE_ROOT}/src/sysdolphin/baselib/util.c"
  "${MELEE_ROOT}/src/sysdolphin/baselib/id.c"
  "${MELEE_ROOT}/src/sysdolphin/baselib/objalloc.c"
  "${MELEE_ROOT}/src/sysdolphin/baselib/random.c")
target_include_directories(native_core_test PRIVATE
  $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_core_test PRIVATE
  $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_core_test PRIVATE
  $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_core_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_core COMMAND native_core_test)
add_executable(native_command_test tests/command_test.c "${MELEE_ROOT}/src/melee/lb/lbcommand.c")
target_include_directories(native_command_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_command_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_command_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_command_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_command COMMAND native_command_test)
set_tests_properties(native_command PROPERTIES LABELS melee)
add_executable(native_particle_tev_test tests/particle_tev_test.c "${MELEE_ROOT}/src/sysdolphin/baselib/psdisptev.c")
target_include_directories(native_particle_tev_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_particle_tev_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_particle_tev_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_particle_tev_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_particle_tev COMMAND native_particle_tev_test)
set_tests_properties(native_particle_tev PROPERTIES LABELS melee)
add_executable(native_bytecode_test tests/bytecode_test.c
  "${MELEE_ROOT}/src/sysdolphin/baselib/bytecode.c"
  "${MELEE_ROOT}/src/sysdolphin/baselib/random.c")
target_include_directories(native_bytecode_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_bytecode_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_bytecode_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_bytecode_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_bytecode COMMAND native_bytecode_test)
set_tests_properties(native_bytecode PROPERTIES LABELS melee)
add_library(melee_assets STATIC asset_archive.cpp)
target_include_directories(melee_assets PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
add_executable(native_archive_test tests/archive_test.cpp asset_archive.cpp)
target_include_directories(native_archive_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_options(native_archive_test PRIVATE -fsanitize=address,undefined)
target_link_options(native_archive_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_archive COMMAND native_archive_test)
add_executable(melee_asset_probe asset_probe.cpp)
target_link_libraries(melee_asset_probe PRIVATE melee_assets aurora::core aurora::gx aurora::mtx aurora::vi)

set_tests_properties(native_core native_archive PROPERTIES LABELS melee)

if(APPLE)
set_target_properties(melee_asset_probe PROPERTIES MACOSX_BUNDLE TRUE
  MACOSX_BUNDLE_GUI_IDENTIFIER "org.melee.native.asset-probe"
  MACOSX_BUNDLE_BUNDLE_NAME "Melee Asset Diagnostic")
endif()
add_test(NAME native_disc COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_disc.py")
set_tests_properties(native_disc PROPERTIES LABELS melee)
add_executable(native_os_test tests/os_test.c os_runtime.c)
target_include_directories(native_os_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_os_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_os_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_os_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_os COMMAND native_os_test)
set_tests_properties(native_os PROPERTIES LABELS melee)

add_executable(native_devcom_test tests/devcom_test.c os_runtime.c
  "${MELEE_ROOT}/src/sysdolphin/baselib/devcom.c")
target_include_directories(native_devcom_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_devcom_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_devcom_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_devcom_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_devcom COMMAND native_devcom_test)
set_tests_properties(native_devcom PROPERTIES LABELS melee)

add_library(melee_gx_probe OBJECT gx_port_probe.c)
target_include_directories(melee_gx_probe PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(melee_gx_probe PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(melee_gx_probe PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS>)
target_sources(melee_asset_probe PRIVATE $<TARGET_OBJECTS:melee_gx_probe>)

# Developer-only loader probe. Unimplemented functions still fail at their first
# call; the actual melee_native target continues requiring a complete static link.
add_executable(melee_boot_probe EXCLUDE_FROM_ALL runtime_main.cpp disc_fonts.cpp gx_bridge.cpp vi_runtime.cpp pad_bridge.cpp
  "${MELEE_ROOT}/src/MSL/float.c" $<TARGET_OBJECTS:melee_game> $<TARGET_OBJECTS:melee_os_runtime>)
target_link_libraries(melee_boot_probe PRIVATE aurora::core aurora::gx aurora::os aurora::dvd
  aurora::card aurora::mtx aurora::pad aurora::vi aurora::thp)
if(APPLE)
  target_link_options(melee_boot_probe PRIVATE -Wl,-dead_strip -Wl,-undefined,dynamic_lookup)
endif()
add_library(melee_pad_abi_test OBJECT tests/pad_abi_test.c)
target_include_directories(melee_pad_abi_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(melee_pad_abi_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(melee_pad_abi_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
add_executable(native_pad_test pad_bridge.cpp tests/pad_backend_fixture.cpp $<TARGET_OBJECTS:melee_pad_abi_test>)
target_include_directories(native_pad_test PRIVATE "${MELEE_AURORA_DIR}/include")
target_compile_definitions(native_pad_test PRIVATE TARGET_PC)
target_compile_options(native_pad_test PRIVATE -fsanitize=address,undefined)
target_link_options(native_pad_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_pad COMMAND native_pad_test)
set_tests_properties(native_pad PROPERTIES LABELS melee)
target_include_directories(melee_native PRIVATE "${MELEE_AURORA_DIR}/lib/dolphin/gx")
target_include_directories(melee_boot_probe PRIVATE "${MELEE_AURORA_DIR}/lib/dolphin/gx")

add_library(melee_audio_runtime OBJECT audio_runtime.c audio_decode.c arq_runtime.c audio_effects.c audio_chorus.c sfx_bank.c archive_bridge.c asset_schema.c movie_runtime.c dev_hardware.c matrix_runtime.c)
target_include_directories(melee_audio_runtime PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(melee_audio_runtime PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(melee_audio_runtime PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS>)
foreach(target melee_native melee_boot_probe)
  if(APPLE)
    target_sources(${target} PRIVATE mac_launcher.mm)
    target_link_libraries(${target} PRIVATE "-framework AppKit" "-framework UniformTypeIdentifiers")
  else()
    target_sources(${target} PRIVATE linux_launcher.cpp)
  endif()
  target_sources(${target} PRIVATE render_check.cpp)
  target_include_directories(${target} PRIVATE "${MELEE_AURORA_DIR}/lib")
  target_sources(${target} PRIVATE audio_host.cpp movie_host.cpp dvd_bridge.cpp card_bridge.cpp keyboard_input.cpp archive_runtime.cpp asset_archive.cpp $<TARGET_OBJECTS:melee_audio_runtime>)
  target_link_libraries(${target} PRIVATE SDL3::SDL3)
endforeach()
if(APPLE)
  set_source_files_properties(mac_launcher.mm PROPERTIES COMPILE_OPTIONS "-fobjc-arc")
  set(MELEE_APP_ARTWORK resources/MeleeNative.icns resources/SSBM.png)
  set_source_files_properties(${MELEE_APP_ARTWORK} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
  target_sources(melee_native PRIVATE ${MELEE_APP_ARTWORK})
endif()
add_executable(native_audio_test tests/audio_test.c audio_decode.c audio_runtime.c audio_effects.c audio_chorus.c arq_runtime.c os_runtime.c)
target_include_directories(native_audio_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_audio_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_audio_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_audio_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_audio COMMAND native_audio_test)
set_tests_properties(native_audio PROPERTIES LABELS melee)

execute_process(COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tools/prepare_audio.py"
  "${CMAKE_CURRENT_BINARY_DIR}/audio_coefficients.inc" COMMAND_ERROR_IS_FATAL ANY)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tools/prepare_audio.py")
target_include_directories(melee_audio_runtime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_include_directories(native_audio_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
add_executable(native_sfx_test tests/sfx_test.c sfx_bank.c)
target_include_directories(native_sfx_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_sfx_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_sfx_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_sfx_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_sfx COMMAND native_sfx_test)
set_tests_properties(native_sfx PROPERTIES LABELS melee)
add_library(melee_audio_device_test OBJECT tests/audio_device_test.c audio_decode.c audio_runtime.c audio_effects.c audio_chorus.c sfx_bank.c os_runtime.c)
target_include_directories(melee_audio_device_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}" $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(melee_audio_device_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(melee_audio_device_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
add_executable(native_audio_device_test EXCLUDE_FROM_ALL audio_host.cpp tests/audio_device_main.cpp $<TARGET_OBJECTS:melee_audio_device_test>)
target_include_directories(native_audio_device_test PRIVATE "${MELEE_AURORA_DIR}/include")
target_compile_definitions(native_audio_device_test PRIVATE TARGET_PC)
target_compile_options(native_audio_device_test PRIVATE -fsanitize=address,undefined)
target_link_options(native_audio_device_test PRIVATE -fsanitize=address,undefined)
target_link_libraries(native_audio_device_test PRIVATE SDL3::SDL3)
add_library(melee_schema_test OBJECT asset_schema.c tests/scene_layout_test.c)
target_include_directories(melee_schema_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(melee_schema_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(melee_schema_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
add_executable(native_runtime_archive_test tests/runtime_archive_test.cpp archive_runtime.cpp asset_archive.cpp $<TARGET_OBJECTS:melee_schema_test>)
target_include_directories(native_runtime_archive_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_options(native_runtime_archive_test PRIVATE -fsanitize=address,undefined)
target_link_options(native_runtime_archive_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_runtime_archive COMMAND native_runtime_archive_test)
set_tests_properties(native_runtime_archive PROPERTIES LABELS melee)

if(APPLE)
foreach(target melee_native melee_boot_probe)
  set_target_properties(${target} PROPERTIES MACOSX_BUNDLE TRUE
    MACOSX_BUNDLE_GUI_IDENTIFIER "org.melee.native.${target}"
    MACOSX_BUNDLE_BUNDLE_NAME "${target}")
endforeach()
set_target_properties(melee_native PROPERTIES
  MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist.in")
endif()
add_executable(native_card_test card_bridge.cpp tests/card_test.cpp)
target_include_directories(native_card_test PRIVATE "${MELEE_AURORA_DIR}/include")
target_compile_definitions(native_card_test PRIVATE TARGET_PC)
target_compile_options(native_card_test PRIVATE -fsanitize=address,undefined)
target_link_options(native_card_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_card COMMAND native_card_test)
set_tests_properties(native_card PROPERTIES LABELS melee)
add_library(melee_movie_test OBJECT movie_runtime.c tests/movie_test.c)
target_include_directories(melee_movie_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(melee_movie_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(melee_movie_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
add_executable(native_movie_test movie_host.cpp $<TARGET_OBJECTS:melee_movie_test>)
target_compile_options(native_movie_test PRIVATE -fsanitize=address,undefined)
target_link_options(native_movie_test PRIVATE -fsanitize=address,undefined)
target_link_libraries(native_movie_test PRIVATE aurora::thp)
add_test(NAME native_movie COMMAND native_movie_test)
set_tests_properties(native_movie PROPERTIES LABELS melee)

# Disc-backed integration tests are registered only when the local assets exist.
set(native_menu_assets)
foreach(asset MnSlChr.usd MnSlChr.dat MnSlMap.usd MnSlMap.dat LbAd.dat GmEvent.dat LbRf.dat LbMcSnap.usd MnMaAll.usd GmTtAll.usd TyDatai.usd NtMemAc.usd NtMsgWin.dat SdMsgBox.usd)
  if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/assets/${asset}")
    list(APPEND native_menu_assets "${CMAKE_CURRENT_BINARY_DIR}/assets/${asset}")
  endif()
endforeach()
if(native_menu_assets)
  add_test(NAME native_menu_assets COMMAND native_runtime_archive_test ${native_menu_assets})
  set_tests_properties(native_menu_assets PROPERTIES LABELS melee)
endif()
file(GLOB native_effect_assets "${CMAKE_CURRENT_BINARY_DIR}/assets/Ef*Data.dat")
if(native_effect_assets)
  add_test(NAME native_effect_assets COMMAND native_runtime_archive_test ${native_effect_assets})
  set_tests_properties(native_effect_assets PROPERTIES LABELS melee)
endif()
set(native_match_assets)
foreach(asset PlCo.dat PdPm.dat ItCo.dat ItCo.usd GrOt.dat GrOt.usd IfAll.dat IfAll.usd LbBf.dat PlKbCpFx.dat PlKbCpMr.dat PlKbCpDr.dat PlKbCpLg.dat PlKbCpYs.dat PlFx.dat GmPause.dat GmPause.usd GmTrain.dat GmTrain.usd GmRst.dat GmRst.usd)
  if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/assets/${asset}")
    list(APPEND native_match_assets "${CMAKE_CURRENT_BINARY_DIR}/assets/${asset}")
  endif()
endforeach()
if(native_match_assets)
  add_test(NAME native_match_assets COMMAND native_runtime_archive_test ${native_match_assets})
  set_tests_properties(native_match_assets PROPERTIES LABELS melee)
endif()
file(GLOB native_fighter_animations "${CMAKE_CURRENT_BINARY_DIR}/assets/Pl??AJ.dat")
if(native_fighter_animations)
  add_test(NAME native_fighter_animations COMMAND native_runtime_archive_test --animation-bundle ${native_fighter_animations})
  set_tests_properties(native_fighter_animations PROPERTIES LABELS melee)
endif()
file(GLOB native_fighter_assets "${CMAKE_CURRENT_BINARY_DIR}/assets/Pl??.dat")
if(native_fighter_assets)
  add_test(NAME native_fighter_assets COMMAND native_runtime_archive_test ${native_fighter_assets})
  set_tests_properties(native_fighter_assets PROPERTIES LABELS melee)
endif()
file(GLOB native_stage_assets "${CMAKE_CURRENT_BINARY_DIR}/assets/Gr*.dat")
if(native_stage_assets)
  add_test(NAME native_stage_core_assets COMMAND native_runtime_archive_test --stage-core ${native_stage_assets})
  set_tests_properties(native_stage_core_assets PROPERTIES LABELS melee)
endif()
if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/assets/GrIz.dat")
  set(native_vs_stage_assets)
  foreach(stage Bb Cn Cs Fs Fz Gb Gd Gr I1 I2 Im Iz Kg Kr Mc NBa NLa Ok Op Ot Oy Ps Ps1 Ps2 Ps3 Ps4 Pu Rc Sh St Ve Yt Ze)
    list(APPEND native_vs_stage_assets "${CMAKE_CURRENT_BINARY_DIR}/assets/Gr${stage}.dat")
  endforeach()
  add_test(NAME native_vs_stage_assets COMMAND native_runtime_archive_test ${native_vs_stage_assets})
  set_tests_properties(native_vs_stage_assets PROPERTIES LABELS melee)
endif()
if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/assets/MvOpen.mth")
  add_test(NAME native_opening_movie COMMAND native_movie_test "${CMAKE_CURRENT_BINARY_DIR}/assets/MvOpen.mth")
  set_tests_properties(native_opening_movie PROPERTIES LABELS melee)
endif()

option(MELEE_SANITIZE_GAME "Instrument the native game and boundary for runtime diagnosis" OFF)
if(MELEE_SANITIZE_GAME)
  foreach(target melee_game melee_audio_runtime melee_native)
    target_compile_options(${target} PRIVATE -fsanitize=address,undefined -fno-omit-frame-pointer)
  endforeach()
  target_link_options(melee_native PRIVATE -fsanitize=address,undefined)
endif()

add_executable(native_fobj_stream_test tests/fobj_stream_test.c "${MELEE_ROOT}/src/sysdolphin/baselib/fobj.c")
target_include_directories(native_fobj_stream_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_fobj_stream_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_fobj_stream_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS> -fsanitize=address,undefined)
target_link_options(native_fobj_stream_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_fobj_stream COMMAND native_fobj_stream_test)
set_tests_properties(native_fobj_stream PROPERTIES LABELS melee)

add_executable(native_jump_test tests/jump_test.c)
target_include_directories(native_jump_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_compile_options(native_jump_test PRIVATE -fsanitize=address,undefined)
target_link_options(native_jump_test PRIVATE -fsanitize=address,undefined)
add_test(NAME native_jump COMMAND native_jump_test)
set_tests_properties(native_jump PROPERTIES LABELS melee)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  add_executable(native_linux_platform_test tests/linux_platform_test.cpp linux_launcher.cpp)
  target_include_directories(native_linux_platform_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
  target_link_libraries(native_linux_platform_test PRIVATE SDL3::SDL3)
  add_test(NAME native_linux_platform COMMAND native_linux_platform_test)
  set_tests_properties(native_linux_platform PROPERTIES LABELS melee)
endif()
# Component assertions remain active in Release CI/package builds as well.
get_property(native_targets DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
foreach(target IN LISTS native_targets)
  if(target MATCHES "^native_.*_test$")
    target_compile_options(${target} PRIVATE -UNDEBUG)
  endif()
endforeach()

add_executable(native_item_pick_test tests/item_pick_test.c "${MELEE_ROOT}/src/melee/it/itspawn.c")
target_include_directories(native_item_pick_test PRIVATE $<TARGET_PROPERTY:melee_game,INCLUDE_DIRECTORIES>)
target_compile_definitions(native_item_pick_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_DEFINITIONS>)
target_compile_options(native_item_pick_test PRIVATE $<TARGET_PROPERTY:melee_game,COMPILE_OPTIONS>
  -UNDEBUG -ffunction-sections -fdata-sections -fsanitize=address,undefined)
target_link_options(native_item_pick_test PRIVATE -fsanitize=address,undefined
  $<$<PLATFORM_ID:Darwin>:-Wl,-dead_strip> $<$<PLATFORM_ID:Linux>:-Wl,--gc-sections>)
add_test(NAME native_item_pick COMMAND native_item_pick_test)
set_tests_properties(native_item_pick PROPERTIES LABELS melee)
