cmake_minimum_required(VERSION 3.15)
project(MeleeUnlocked LANGUAGES CXX)

include(port/tests/embed_view.cmake)

# YouGame's rows show as Coming soon and nothing YouGame runs from the launcher, until YouGame opens.
option(MELEE_YOUGAME_COMING_SOON "Show YouGame's online rows as Coming soon (launcher YouGame off)" ON)
if(MELEE_YOUGAME_COMING_SOON)
  add_compile_definitions(MELEE_YOUGAME_COMING_SOON=1)
endif()

option(MELEE_BUILD_NATIVE_LINUX "Build the native Linux Source Port (ELF host and game)" OFF)
if(MELEE_BUILD_NATIVE_LINUX)
  if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux" OR NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(FATAL_ERROR "MELEE_BUILD_NATIVE_LINUX requires a 64-bit Linux toolchain")
  endif()
  enable_testing()
  include(port/tests/sysgen.cmake)
  include(port/tests/settings_mouse.cmake)
  include(port/tests/controller_profiles.cmake)
  add_subdirectory(port/linux)
  return()
endif()
enable_testing()
include(port/tests/sysgen.cmake)
include(port/tests/settings_mouse.cmake)
include(port/tests/controller_profiles.cmake)
if(WIN32 OR APPLE)
  include(port/tests/production_integrity.cmake)
endif()
find_package(Python3 COMPONENTS Interpreter REQUIRED)
if(WIN32)
  include(port/tests/yougame_agent_root_win.cmake)
endif()
add_test(NAME port_gc_adapter_guard COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/gc_adapter_guard_test.py")
add_test(NAME port_yougame_0956_merge COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/yougame_0956_merge_test.py")
add_test(NAME port_yougame_0962_merge COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/yougame_0962_merge_test.py")
add_test(NAME port_local_build_engine_wiring COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/local_build_engine_wiring_test.py")
add_test(NAME port_akaneia_probe_wiring COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/akaneia_probe_wiring_test.py")
add_test(NAME port_relay_pads_wiring COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/relay_pads_wiring_test.py")
add_test(NAME port_yougame_0965_merge COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/yougame_0965_merge_test.py")
add_test(NAME port_yougame_0968_merge COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/yougame_0968_merge_test.py")
add_test(NAME port_yougame_0969_merge COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/yougame_0969_merge_test.py")
add_test(NAME port_shader_auto_fit COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/shader_auto_fit_test.py")

# Window-free agent hook checks, built on macOS/Linux only (use the bundled JSON header). Kept out of the
# Windows build so these test programs can never break the shipped melee_source build.
if(NOT WIN32)
  add_executable(port_cosmetic_cli_test port/tests/cosmetic_cli_test.cpp)
  target_include_directories(port_cosmetic_cli_test PRIVATE port/runtime/host)
  target_compile_features(port_cosmetic_cli_test PRIVATE cxx_std_17)
  add_test(NAME port_cosmetic_cli COMMAND port_cosmetic_cli_test)
  add_executable(port_slippi_netplay_receive_test port/tests/slippi_netplay_receive_test.cpp)
  target_include_directories(port_slippi_netplay_receive_test PRIVATE port/runtime/hle port/runtime/host)
  target_compile_features(port_slippi_netplay_receive_test PRIVATE cxx_std_17)
  add_test(NAME port_slippi_netplay_receive COMMAND port_slippi_netplay_receive_test)
  add_executable(port_pad_mirror_posix_test port/tests/pad_mirror_posix_test.cpp)
  target_include_directories(port_pad_mirror_posix_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_pad_mirror_posix_test PRIVATE cxx_std_17)
  add_test(NAME port_pad_mirror_posix COMMAND port_pad_mirror_posix_test)
  add_executable(port_lcancel_policy_test port/tests/lcancel_policy_test.cpp)
  target_include_directories(port_lcancel_policy_test PRIVATE port/runtime/host)
  target_compile_features(port_lcancel_policy_test PRIVATE cxx_std_17)
  add_test(NAME port_lcancel_policy COMMAND port_lcancel_policy_test)
  add_executable(port_gc_content_sha1_test port/tests/gc_content_sha1_test.cpp
    port/runtime/host/sysgen.cpp port/runtime/host/vcdiff.cpp)
  target_include_directories(port_gc_content_sha1_test PRIVATE port/runtime/host port/third_party)
  target_compile_features(port_gc_content_sha1_test PRIVATE cxx_std_17)
  add_test(NAME port_gc_content_sha1 COMMAND port_gc_content_sha1_test)
  add_executable(port_yougame_net_test port/tests/yougame_net_test.cpp)
  target_include_directories(port_yougame_net_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_net_test PRIVATE cxx_std_17)
  target_compile_definitions(port_yougame_net_test PRIVATE YOUGAME_AGENT_TEST_HOOK)
  add_test(NAME port_yougame_net COMMAND port_yougame_net_test)
  add_executable(port_yougame_net_off_test port/tests/yougame_net_test.cpp)
  target_include_directories(port_yougame_net_off_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_net_off_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_net_off COMMAND sh
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent.sh" $<TARGET_FILE:port_yougame_net_off_test>)
  add_executable(port_rollback_perf_test port/tests/rollback_perf_test.cpp)
  target_include_directories(port_rollback_perf_test PRIVATE port/runtime/host)
  target_compile_features(port_rollback_perf_test PRIVATE cxx_std_17)
  add_test(NAME port_rollback_perf COMMAND port_rollback_perf_test)
  add_executable(port_sim_work_test port/tests/sim_work_test.cpp)
  target_include_directories(port_sim_work_test PRIVATE port/runtime/host)
  target_compile_features(port_sim_work_test PRIVATE cxx_std_17)
  add_test(NAME port_sim_work COMMAND port_sim_work_test)
  add_executable(port_frame_queue_test port/tests/frame_queue_test.cpp)
  target_include_directories(port_frame_queue_test PRIVATE port/runtime/gx port/runtime/host)
  target_compile_features(port_frame_queue_test PRIVATE cxx_std_17)
  add_test(NAME port_frame_queue COMMAND port_frame_queue_test)
  if(APPLE)
    add_test(NAME port_mac_digest COMMAND "${Python3_EXECUTABLE}" -B "${CMAKE_CURRENT_SOURCE_DIR}/port/mac/compare_digest_test.py")
    add_executable(port_mac_ws_frame_test port/linux/compat/tests/ws_frame_test.cpp)
    target_compile_features(port_mac_ws_frame_test PRIVATE cxx_std_17)
    add_test(NAME port_mac_ws_frames COMMAND port_mac_ws_frame_test)
  endif()
  add_executable(port_yougame_relay_pads_test port/tests/yougame_relay_pads_test.cpp)
  target_include_directories(port_yougame_relay_pads_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_relay_pads_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_relay_pads COMMAND port_yougame_relay_pads_test)
  if(APPLE)
    # The actual XInput compatibility types include the host's x86 intrinsics: build this test x86_64 only (an
    # extra -arch flag would make a two-slice build whose arm64 half cannot compile them).
    set_target_properties(port_yougame_relay_pads_test PROPERTIES OSX_ARCHITECTURES x86_64)
  endif()
  add_executable(port_yougame_friends_test port/tests/yougame_friends_test.cpp)
  target_include_directories(port_yougame_friends_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_friends_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_friends_test COMMAND port_yougame_friends_test)
  add_executable(port_yougame_ux_test port/tests/yougame_ux_test.cpp)
  target_include_directories(port_yougame_ux_test PRIVATE port/runtime/hle port/runtime/host port/third_party)
  target_compile_features(port_yougame_ux_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_ux COMMAND port_yougame_ux_test)
  add_executable(port_yougame_parity_test port/tests/yougame_parity_test.cpp)
  target_include_directories(port_yougame_parity_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_parity_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_parity_test COMMAND port_yougame_parity_test)
  find_package(Threads REQUIRED)
  add_executable(port_yougame_agent_present_test port/tests/yougame_agent_present_test.cpp)
  target_include_directories(port_yougame_agent_present_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_agent_present_test PRIVATE cxx_std_17)
  target_compile_definitions(port_yougame_agent_present_test PRIVATE YOUGAME_AGENT_TEST_HOOK)
  target_link_libraries(port_yougame_agent_present_test PRIVATE Threads::Threads)
  add_test(NAME port_yougame_agent_present COMMAND port_yougame_agent_present_test "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-present-test")
  add_executable(port_yougame_agent_present_off_test port/tests/yougame_agent_present_test.cpp)
  target_include_directories(port_yougame_agent_present_off_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_agent_present_off_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_agent_present_off COMMAND sh
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent.sh" $<TARGET_FILE:port_yougame_agent_present_off_test>)
  add_executable(port_yougame_agent_keys_test port/tests/yougame_agent_keys_test.cpp)
  target_include_directories(port_yougame_agent_keys_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_agent_keys_test PRIVATE cxx_std_17)
  target_compile_definitions(port_yougame_agent_keys_test PRIVATE YOUGAME_AGENT_TEST_HOOK)
  target_link_libraries(port_yougame_agent_keys_test PRIVATE Threads::Threads)
  add_test(NAME port_yougame_agent_keys COMMAND port_yougame_agent_keys_test "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-keys-test")
  add_executable(port_yougame_agent_keys_off_test port/tests/yougame_agent_keys_test.cpp)
  target_include_directories(port_yougame_agent_keys_off_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_agent_keys_off_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_agent_keys_off COMMAND sh
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent.sh" $<TARGET_FILE:port_yougame_agent_keys_off_test>)
  add_executable(port_yougame_agent_script_test port/tests/yougame_agent_script_test.cpp)
  target_include_directories(port_yougame_agent_script_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_agent_script_test PRIVATE cxx_std_17)
  target_compile_definitions(port_yougame_agent_script_test PRIVATE YOUGAME_AGENT_TEST_HOOK)
  target_link_libraries(port_yougame_agent_script_test PRIVATE Threads::Threads)
  add_test(NAME port_yougame_agent_script COMMAND port_yougame_agent_script_test "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-script-test")
  add_executable(port_yougame_agent_script_off_test port/tests/yougame_agent_script_test.cpp)
  target_include_directories(port_yougame_agent_script_off_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_agent_script_off_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_agent_script_off COMMAND sh
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent.sh" $<TARGET_FILE:port_yougame_agent_script_off_test>)
  add_test(NAME port_yougame_agent_script_absent COMMAND sh
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_script_absent_test.sh"
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent.sh")
  add_executable(port_native_online_policy_test port/tests/native_online_policy_test.cpp)
  target_include_directories(port_native_online_policy_test PRIVATE port/runtime/hle)
  target_compile_features(port_native_online_policy_test PRIVATE cxx_std_17)
  add_test(NAME port_native_online_policy COMMAND port_native_online_policy_test)
  add_executable(port_source_mod_view_test port/tests/source_mod_view_test.cpp)
  target_include_directories(port_source_mod_view_test PRIVATE port/runtime/hle)
  target_compile_features(port_source_mod_view_test PRIVATE cxx_std_17)
  add_test(NAME port_source_mod_view COMMAND port_source_mod_view_test)
  add_executable(port_yougame_mod_online_test port/tests/yougame_mod_online_test.cpp)
  target_include_directories(port_yougame_mod_online_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_mod_online_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_mod_online COMMAND port_yougame_mod_online_test)
  add_executable(port_yougame_mod_online_noslippi_test port/tests/yougame_mod_online_test.cpp)
  target_include_directories(port_yougame_mod_online_noslippi_test PRIVATE port/runtime/hle port/runtime/host)
  target_compile_features(port_yougame_mod_online_noslippi_test PRIVATE cxx_std_17)
  target_compile_definitions(port_yougame_mod_online_noslippi_test PRIVATE MELEE_NO_SLIPPI)
  add_test(NAME port_yougame_mod_online_noslippi COMMAND port_yougame_mod_online_noslippi_test)
  add_executable(port_yougame_agent_test port/tests/yougame_agent_test.cpp)
  target_include_directories(port_yougame_agent_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_agent_test PRIVATE cxx_std_17)
  file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-test")
  add_test(NAME port_yougame_agent COMMAND port_yougame_agent_test "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-test")
  add_executable(port_stage_read_pin_test port/tests/stage_read_pin_test.cpp)
  target_include_directories(port_stage_read_pin_test PRIVATE port/runtime/host)
  target_compile_features(port_stage_read_pin_test PRIVATE cxx_std_17)
  add_test(NAME port_stage_read_pin COMMAND port_stage_read_pin_test)
  add_executable(port_yougame_agent_fight_test port/tests/yougame_agent_fight_test.cpp)
  target_include_directories(port_yougame_agent_fight_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_agent_fight_test PRIVATE cxx_std_17)
  target_compile_definitions(port_yougame_agent_fight_test PRIVATE YOUGAME_AGENT_TEST_HOOK)
  target_link_libraries(port_yougame_agent_fight_test PRIVATE Threads::Threads)
  file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-fight-test")
  add_test(NAME port_yougame_agent_fight COMMAND port_yougame_agent_fight_test "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-fight-test")
  add_executable(port_yougame_agent_fight_off_test port/tests/yougame_agent_fight_test.cpp)
  target_include_directories(port_yougame_agent_fight_off_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_agent_fight_off_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_agent_fight_off COMMAND port_yougame_agent_fight_off_test)
  add_executable(port_yougame_agent_static_test port/tests/yougame_agent_static_test.cpp)
  target_include_directories(port_yougame_agent_static_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_agent_static_test PRIVATE cxx_std_17)
  target_compile_definitions(port_yougame_agent_static_test PRIVATE YOUGAME_AGENT_TEST_HOOK)
  target_link_libraries(port_yougame_agent_static_test PRIVATE Threads::Threads)
  file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-static-test")
  add_test(NAME port_yougame_agent_static COMMAND port_yougame_agent_static_test "${CMAKE_CURRENT_BINARY_DIR}/yougame-agent-static-test")
  add_executable(port_yougame_agent_static_off_test port/tests/yougame_agent_static_test.cpp)
  target_include_directories(port_yougame_agent_static_off_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_agent_static_off_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_agent_static_off COMMAND sh
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent.sh" $<TARGET_FILE:port_yougame_agent_static_off_test>)
  add_test(NAME port_yougame_agent_static_absent COMMAND sh
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_static_absent_test.sh"
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent.sh")
  add_executable(port_yougame_agent_pad_test port/tests/yougame_agent_pad_test.cpp)
  target_include_directories(port_yougame_agent_pad_test PRIVATE port/runtime/hle port/runtime/host)
  target_compile_features(port_yougame_agent_pad_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_agent_pad COMMAND port_yougame_agent_pad_test)
  add_executable(port_yougame_memcard_static_test port/tests/yougame_memcard_static_test.cpp
    port/runtime/hle/yougame_memcard.cpp)
  target_include_directories(port_yougame_memcard_static_test PRIVATE port/tests/memcard_stubs port/runtime/hle)
  target_compile_features(port_yougame_memcard_static_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_memcard_static COMMAND port_yougame_memcard_static_test)
  add_executable(port_yougame_memcard_test port/tests/yougame_memcard_test.cpp)
  target_include_directories(port_yougame_memcard_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_memcard_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_memcard COMMAND port_yougame_memcard_test)
  add_executable(port_yougame_mode_policy_test port/tests/yougame_mode_policy_test.cpp)
  target_include_directories(port_yougame_mode_policy_test PRIVATE port/runtime/hle)
  target_compile_features(port_yougame_mode_policy_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_mode_policy COMMAND port_yougame_mode_policy_test)
  add_executable(port_host_stub_policy_test port/tests/host_stub_policy_test.cpp)
  target_include_directories(port_host_stub_policy_test PRIVATE port/runtime/ppc)
  target_compile_features(port_host_stub_policy_test PRIVATE cxx_std_17)
  add_test(NAME port_host_stub_policy COMMAND port_host_stub_policy_test)
  add_executable(port_yougame_exit_reason_test port/tests/yougame_exit_reason_test.cpp
    port/runtime/host/source_mod_overlay.cpp)
  target_include_directories(port_yougame_exit_reason_test PRIVATE port/runtime/hle port/runtime/host port/third_party)
  target_compile_features(port_yougame_exit_reason_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_exit_reason COMMAND port_yougame_exit_reason_test)
  foreach(name duel_mapping teams room transport)
    add_executable(port_yougame_${name}_test port/tests/yougame_${name}_test.cpp)
    target_include_directories(port_yougame_${name}_test PRIVATE port/runtime/hle port/third_party)
    target_compile_features(port_yougame_${name}_test PRIVATE cxx_std_20)
    add_test(NAME port_yougame_${name} COMMAND port_yougame_${name}_test)
  endforeach()
  foreach(variant hook off)
    add_executable(port_yougame_drop_room_${variant}_test port/tests/yougame_drop_room_test.cpp)
    target_include_directories(port_yougame_drop_room_${variant}_test PRIVATE port/runtime/hle port/third_party)
    target_compile_features(port_yougame_drop_room_${variant}_test PRIVATE cxx_std_17)
    if(variant STREQUAL "hook")
      target_compile_definitions(port_yougame_drop_room_${variant}_test PRIVATE YOUGAME_AGENT_TEST_HOOK)
      target_link_libraries(port_yougame_drop_room_${variant}_test PRIVATE Threads::Threads)
    endif()
    add_test(NAME port_yougame_drop_room_${variant} COMMAND port_yougame_drop_room_${variant}_test
      "${CMAKE_CURRENT_BINARY_DIR}/yougame-drop-room-${variant}")
  endforeach()
  add_executable(port_yougame_ranked_test port/tests/yougame_ranked_test.cpp)
  target_include_directories(port_yougame_ranked_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_ranked_test PRIVATE cxx_std_20)
  add_test(NAME port_yougame_ranked COMMAND port_yougame_ranked_test)
  add_executable(port_yougame_ranked_rules_test port/tests/yougame_ranked_rules_test.cpp)
  target_include_directories(port_yougame_ranked_rules_test PRIVATE port/runtime/hle port/third_party)
  target_compile_features(port_yougame_ranked_rules_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_ranked_rules COMMAND port_yougame_ranked_rules_test)
  if(APPLE)
    add_executable(port_static_mod_online_test port/tests/static_mod_online_test.cpp
      port/runtime/host/mod_profile.cpp port/tests/identity_stubs/hash_apple.cpp)
    target_include_directories(port_static_mod_online_test PRIVATE port/tests/identity_stubs port/runtime/hle port/runtime/host)
    target_compile_features(port_static_mod_online_test PRIVATE cxx_std_17)
    add_test(NAME port_static_mod_online COMMAND port_static_mod_online_test)
  endif()
  add_executable(port_yougame_agent_pad_ui_test port/tests/yougame_agent_pad_ui_test.cpp
    port/third_party/imgui/imgui.cpp port/third_party/imgui/imgui_draw.cpp
    port/third_party/imgui/imgui_tables.cpp port/third_party/imgui/imgui_widgets.cpp)
  target_include_directories(port_yougame_agent_pad_ui_test PRIVATE
    port/runtime/hle port/runtime/host port/third_party/imgui)
  target_compile_features(port_yougame_agent_pad_ui_test PRIVATE cxx_std_17)
  add_test(NAME port_yougame_agent_pad_ui COMMAND port_yougame_agent_pad_ui_test)
  add_test(NAME port_agent_test_hook_absent COMMAND sh
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent_test.sh"
    "${CMAKE_CURRENT_SOURCE_DIR}/port/tests/agent_test_hook_absent.sh")
endif()

# The instruction set baseline for every target in this build, including native_animation below.
# Declared here (not in port/CMakeLists.txt) because native_animation is built before add_subdirectory
# (port) runs and links into melee_port_compat.exe same as everything else; without this it kept MSVC's
# default codegen regardless of MELEE_CPU_BASELINE, so a "compat" build could still fault on a
# pre-Haswell/pre-Ryzen CPU with an illegal instruction coming out of the animation/spline math that
# runs every frame, even though port/ itself was genuinely built at SSE2.
set(MELEE_CPU_BASELINE "AVX2" CACHE STRING "Instruction set baseline: AVX2, AVX or SSE2")
set_property(CACHE MELEE_CPU_BASELINE PROPERTY STRINGS AVX2 AVX SSE2)
if(MELEE_CPU_BASELINE STREQUAL "SSE2")
  set(MELEE_ARCH_FLAG "")          # the x64 default; naming it explicitly is an error in MSVC
else()
  set(MELEE_ARCH_FLAG "/arch:${MELEE_CPU_BASELINE}")
endif()

set(HOST_FOBJ "${CMAKE_CURRENT_BINARY_DIR}/generated/FObjHost.cpp")
add_custom_command(OUTPUT "${HOST_FOBJ}"
  COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_fobj_host.py" "${HOST_FOBJ}"
  DEPENDS tools/generate_fobj_host.py melee/src/sysdolphin/baselib/fobj.c
          melee/src/sysdolphin/baselib/fobj.h melee/src/sysdolphin/baselib/spline.c)
add_library(native_animation STATIC "${HOST_FOBJ}")
target_include_directories(native_animation PUBLIC native)
target_compile_features(native_animation PUBLIC cxx_std_14)
if(MSVC)
  target_compile_options(native_animation PRIVATE ${MELEE_ARCH_FLAG})
endif()
add_executable(native_asset_test tests/native_asset_test.cpp)
add_executable(packed_animation_test tests/packed_animation_test.cpp)
add_executable(native_practice_model_test
  tests/native_practice_model_test.cpp
  port/runtime/hle/native_practice_model.cpp)
target_include_directories(native_practice_model_test PRIVATE port/runtime/hle)
target_compile_features(native_practice_model_test PRIVATE cxx_std_17)
add_test(NAME native_practice_model COMMAND native_practice_model_test)
target_link_libraries(packed_animation_test PRIVATE native_animation)
add_test(NAME packed_animation COMMAND packed_animation_test)
target_link_libraries(native_asset_test PRIVATE native_animation)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/runtime/native-assets/PlFcAJ.dat")
  add_test(NAME native_assets COMMAND native_asset_test "${CMAKE_CURRENT_SOURCE_DIR}/runtime/native-assets")
endif()

option(MELEE_BUILD_EXPERIMENTAL_PORT "Build the incomplete native recompilation prototype" OFF)
if(MELEE_BUILD_EXPERIMENTAL_PORT)
  if(NOT WIN32 OR NOT MSVC OR NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(FATAL_ERROR "The experimental port requires Windows x64 and MSVC")
  endif()
  add_subdirectory(port)
endif()
add_executable(native_animation_test tests/native_animation_test.cpp)
target_include_directories(native_animation_test PRIVATE native)
target_compile_features(native_animation_test PRIVATE cxx_std_14)
add_test(NAME native_animation COMMAND native_animation_test)
if(WIN32)
  add_executable(native_viewer native/Viewer.cpp)
  target_link_libraries(native_viewer PRIVATE native_animation)
endif()
