cmake_minimum_required(VERSION 3.20)
project(wwhd_recomp C CXX)
# Renderers compiled into the executable:
#   BOTH   (macOS default) Metal and Vulkan (MoltenVK) in one app; Graphics > Renderer picks one at start
#   METAL  Metal only (macOS)
#   VULKAN Vulkan only, with the portable SDL3 host (default on other platforms)
#   DEKO3D deko3d (the Switch's own GPU API, docs/deko3d-plan.md) with the libnx host (Switch homebrew, the
#          only one there; the OpenGL renderer it replaced was removed after dev 207349b)
# Switch homebrew build: the devkitPro toolchain file (Switch.cmake) sets NX_ROOT
if(DEFINED NX_ROOT)
  set(WWHD_SWITCH ON)
else()
  set(WWHD_SWITCH OFF)
endif()
set(WWHD_RENDERER "" CACHE STRING "Graphics backends: BOTH, METAL, VULKAN or DEKO3D")
if(NOT WWHD_RENDERER)
  if(APPLE)
    set(WWHD_RENDERER BOTH)
  elseif(WWHD_SWITCH)
    set(WWHD_RENDERER DEKO3D)
  else()
    set(WWHD_RENDERER VULKAN)
  endif()
endif()
string(TOUPPER "${WWHD_RENDERER}" WWHD_RENDERER)
set_property(CACHE WWHD_RENDERER PROPERTY STRINGS BOTH METAL VULKAN DEKO3D)
if(NOT WWHD_RENDERER MATCHES "^(BOTH|METAL|VULKAN|DEKO3D)$")
  message(FATAL_ERROR "WWHD_RENDERER must be BOTH, METAL, VULKAN or DEKO3D")
endif()
set(WWHD_HAS_METAL OFF)
set(WWHD_HAS_VULKAN OFF)
set(WWHD_HAS_DEKO3D OFF)
if(WWHD_RENDERER STREQUAL "DEKO3D")
  if(NOT WWHD_SWITCH)
    message(FATAL_ERROR "deko3d is the Switch's own GPU API: WWHD_RENDERER=DEKO3D needs the devkitPro Switch toolchain")
  endif()
  set(WWHD_HAS_DEKO3D ON)
elseif(WWHD_SWITCH)
  message(FATAL_ERROR "The Switch build supports only WWHD_RENDERER=DEKO3D")
endif()
if(WWHD_RENDERER MATCHES "^(BOTH|METAL)$")
  if(NOT APPLE)
    message(FATAL_ERROR "Metal is available only on macOS; select VULKAN")
  endif()
  set(WWHD_HAS_METAL ON)
  enable_language(OBJCXX)
endif()
if(WWHD_RENDERER MATCHES "^(BOTH|VULKAN)$")
  set(WWHD_HAS_VULKAN ON)
endif()
# the SDL3 host (windows, input, audio) serves Vulkan-only builds; with Metal compiled in, the
# AppKit host (TV/GamePad windows, menus, controls window) serves both renderers
if(WWHD_HAS_VULKAN AND NOT WWHD_HAS_METAL)
  set(WWHD_SDL_HOST ON)
else()
  set(WWHD_SDL_HOST OFF)
endif()
message(STATUS "WWHD renderers: ${WWHD_RENDERER} (Metal ${WWHD_HAS_METAL}, Vulkan ${WWHD_HAS_VULKAN}, deko3d ${WWHD_HAS_DEKO3D}, SDL host ${WWHD_SDL_HOST})")
if(WWHD_SWITCH AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
  # devkitA64 is GCC; __attribute__((musttail)) needs GCC 15
  if(CMAKE_C_COMPILER_VERSION VERSION_LESS 15)
    message(FATAL_ERROR "The recompiled game needs GCC 15 or newer (musttail); update devkitA64")
  endif()
elseif(NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
  message(FATAL_ERROR "The recompiled game requires Clang (musttail support); select clang/clang++")
endif()

# Linux and Android on arm64: plain char is unsigned in that ABI, but signed on x86-64 and Apple
# arm64, where the runtime and the recompiled game code are tested. Keep it signed everywhere (also
# reaches the game code the installer compiles: its flags come from this build).
# The Switch (devkitA64, aarch64-none-elf) follows the same arm64 ABI: unsigned plain char.
if(WWHD_SWITCH OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Android)$" AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64)$"))
  add_compile_options(-fsigned-char)
endif()

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()

# Generated by tools/recomp/recomp.py from game/code/cking.rpx
set(GEN_DIR ${CMAKE_SOURCE_DIR}/build/gen CACHE PATH "recompiler output")
file(GLOB GEN_SOURCES CONFIGURE_DEPENDS ${GEN_DIR}/code_*.c)
if(NOT GEN_SOURCES)
  message(FATAL_ERROR "no generated code in ${GEN_DIR}; run tools/recomp/recomp.py first")
endif()

# WWHD_BUNDLED_DEPS: build glslang, zlib, LZ4 (and SDL3 if not found) from pinned sources with the
# project's own compiler. Release builds use it so the prebuilt runtime and its dependencies all
# come from the toolchain the installer later links with (llvm-mingw, zig).
option(WWHD_BUNDLED_DEPS "build missing dependencies from pinned sources" OFF)
if(WIN32 AND CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC" OR WWHD_BUNDLED_DEPS)
  include(cmake/WindowsDependencies.cmake)
endif()
if(WIN32 AND CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
  add_compile_definitions(NOMINMAX _CRT_SECURE_NO_WARNINGS)
endif()

add_library(gamecode STATIC ${GEN_SOURCES} ${GEN_DIR}/table.c ${GEN_DIR}/imports.c)
target_include_directories(gamecode PUBLIC runtime/include ${GEN_DIR})
# exact FP semantics: no contraction into FMA, no fast-math; PowerPC integer arithmetic wraps
target_compile_options(gamecode PRIVATE -O3 -ffp-contract=off -fno-strict-aliasing -fwrapv -w)
if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
  target_compile_options(gamecode PRIVATE -mcpu=apple-m1)
endif()
if(WWHD_SWITCH)
  # no frame-pointer setup in each of the ~40,000 recompiled functions (code size: the Switch's
  # caches are small and the game code is ~38 MB)
  target_compile_options(gamecode PRIVATE -fomit-frame-pointer)
endif()


# ---- vendored Cemu GPU pieces (MPL-2.0): address library + shader decompiler
set(CEMU_DIR ${CMAKE_SOURCE_DIR}/runtime/third_party/cemu)
file(GLOB_RECURSE CEMU_SOURCES CONFIGURE_DEPENDS ${CEMU_DIR}/*.cpp)
if(NOT WWHD_HAS_METAL)
  # the MSL emitter needs metal-cpp (Objective-C runtime); Vulkan uses the GLSL emitter
  list(FILTER CEMU_SOURCES EXCLUDE REGEX "LatteDecompilerEmitMSL[^/]*\\.cpp$")
endif()
add_library(cemu_latte STATIC ${CEMU_SOURCES})
target_include_directories(cemu_latte PUBLIC ${CEMU_DIR} ${CEMU_DIR}/Cafe ${CMAKE_SOURCE_DIR}/runtime/third_party/metal-cpp
                                             ${CMAKE_SOURCE_DIR}/runtime/third_party/fmt/include)
target_compile_options(cemu_latte PRIVATE "SHELL:-include ${CEMU_DIR}/cemu_shim.h")
if(WWHD_HAS_METAL)
  target_compile_definitions(cemu_latte PUBLIC ENABLE_METAL=1)
endif()
if(WWHD_HAS_VULKAN)
  target_compile_definitions(cemu_latte PUBLIC ENABLE_VULKAN=1)
endif()
if(WWHD_HAS_DEKO3D)
  # deko3d uses the decompiler's OpenGL mode (the same GLSL and shader cache keys; docs/deko3d-plan.md 1)
  target_compile_definitions(cemu_latte PUBLIC ENABLE_OPENGL=1)
endif()
target_compile_options(cemu_latte PRIVATE -w -O2)

# ---- Dear ImGui (MIT, runtime/third_party/imgui, pinned release): the in-game settings overlay
set(IMGUI_DIR ${CMAKE_SOURCE_DIR}/runtime/third_party/imgui)
add_library(imgui STATIC ${IMGUI_DIR}/imgui.cpp ${IMGUI_DIR}/imgui_draw.cpp ${IMGUI_DIR}/imgui_tables.cpp ${IMGUI_DIR}/imgui_widgets.cpp)
target_include_directories(imgui PUBLIC ${IMGUI_DIR})
target_compile_definitions(imgui PUBLIC IMGUI_DISABLE_DEMO_WINDOWS IMGUI_DISABLE_OBSOLETE_FUNCTIONS)
target_compile_options(imgui PRIVATE -w -O2)
if(WWHD_SWITCH)
  # no processes to open links with on the console (ImGui's default uses execvp/waitpid)
  target_compile_definitions(imgui PUBLIC IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS)
endif()

# ---- uam (devkitPro's deko3d shader compiler, MIT, runtime/third_party/uam, 1.1.0 + PATCHES.md): the
# 'uamlib' target (GLSL -> DKSH, uam_api.h) for the coming deko3d renderer; built only when something
# links it. -DWWHD_UAM_TEST=ON adds uam_dksh_test (desktop).
add_subdirectory(runtime/third_party/uam EXCLUDE_FROM_ALL)
# ---- deko3d (devkitPro's Switch GPU API, zlib, runtime/third_party/deko3d, 0.5.0 + PATCHES.md): built from
# source for its fixes, in place of devkitPro's libdeko3d / libdeko3dd
if(WWHD_RENDERER STREQUAL "DEKO3D")
  add_subdirectory(runtime/third_party/deko3d EXCLUDE_FROM_ALL)
endif()

# ---- build identification (runtime/src/build_info.h): version and commit, rewritten by
# cmake/BuildInfo.cmake on every build when they change. Release builds pass the tag.
set(WWHD_VERSION "" CACHE STRING "release version shown in reports and logs (default: from git describe)")
set(WWHD_BUILD_INFO ${CMAKE_BINARY_DIR}/generated/build_info.cpp)
add_custom_target(wwhd_build_info
  COMMAND ${CMAKE_COMMAND} -DSRC=${CMAKE_SOURCE_DIR} -DOUT=${WWHD_BUILD_INFO} "-DVERSION=${WWHD_VERSION}"
          -P ${CMAKE_SOURCE_DIR}/cmake/BuildInfo.cmake
  BYPRODUCTS ${WWHD_BUILD_INFO}
  COMMENT "Build info (version, commit)" VERBATIM)
# Windows: VERSIONINFO + application manifest (wwhd_windows_resources); release builds strip the shipped
# programs' debug information (WWHD_STRIP_RELEASE: -s for GNU-style linkers in Release/MinSizeRel)
include(cmake/WindowsResources.cmake)
if(WIN32 AND NOT MSVC AND NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
  set(WWHD_STRIP_RELEASE "$<$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>:-s>")
else()
  set(WWHD_STRIP_RELEASE "")
endif()

# ---- the game executable: runtime + native GX2 + the selected renderers
# GX2 sources are compiled into the executable (not a static library) so their
# strong HLE definitions replace the generated weak import stubs.
file(GLOB RUNTIME_SOURCES CONFIGURE_DEPENDS runtime/src/*.cpp runtime/src/*.c runtime/src/hle/*.cpp runtime/src/mods/*.cpp runtime/src/motion/*.cpp
                                            runtime/src/overlay/*.cpp)
list(APPEND RUNTIME_SOURCES runtime/src/platform/perf_hint.cpp)  # Android performance hints (no-op elsewhere)
if(WWHD_SWITCH)
  # desktop crash logs (module names through dladdr); the Switch has its own handler in main.cpp
  list(FILTER RUNTIME_SOURCES EXCLUDE REGEX "runtime/src/crash_addr\\.cpp$")
endif()
list(APPEND RUNTIME_SOURCES ${WWHD_BUILD_INFO})
file(GLOB GX2_SOURCES CONFIGURE_DEPENDS runtime/src/gx2/*.cpp)
# renderer selection / dispatch (runtime/src/gfx/renderer.cpp) and the GamePad screen modes / TV window
# layout both window hosts share (display_modes.cpp): every configuration
set(GFX_SOURCES runtime/src/gfx/renderer.cpp runtime/src/gfx/display_modes.cpp)
set(MOD_GFX_SOURCES)
if(WWHD_HAS_METAL)
  # Metal renderer and the AppKit host (windows, menus, input, controls window, display modes)
  file(GLOB METAL_SOURCES CONFIGURE_DEPENDS runtime/src/gfx/*.mm)
  file(GLOB MOD_GFX_SOURCES CONFIGURE_DEPENDS runtime/src/mods/*.mm)
  list(APPEND GFX_SOURCES ${METAL_SOURCES})
  set_source_files_properties(${METAL_SOURCES} ${MOD_GFX_SOURCES} PROPERTIES COMPILE_FLAGS "-fobjc-arc")
  # the settings overlay's Metal renderer (Dear ImGui's backend, unmodified)
  set(IMGUI_METAL ${IMGUI_DIR}/backends/imgui_impl_metal.mm)
  set_source_files_properties(${IMGUI_METAL} PROPERTIES COMPILE_FLAGS "-fobjc-arc -w")
endif()
if(WWHD_HAS_VULKAN)
  find_package(Vulkan 1.3 REQUIRED)
  if(NOT TARGET glslang::glslang)
    find_package(glslang CONFIG REQUIRED)
  endif()
  file(GLOB VULKAN_SOURCES CONFIGURE_DEPENDS runtime/src/gfx/vulkan/*.cpp)
  list(APPEND GFX_SOURCES ${VULKAN_SOURCES})
  if(APPLE)
    list(APPEND GFX_SOURCES runtime/src/platform/native.mm)
    set_source_files_properties(runtime/src/platform/native.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")
  endif()
endif()
if(WWHD_HAS_DEKO3D)
  # deko3d renderer and the libnx host
  file(GLOB DEKO3D_SOURCES CONFIGURE_DEPENDS runtime/src/gfx/deko/*.cpp)
  list(APPEND GFX_SOURCES ${DEKO3D_SOURCES} runtime/src/platform/input_switch.cpp runtime/src/platform/hostui_switch.cpp
                          runtime/src/platform/settings_switch.cpp runtime/src/platform/settings_ini.cpp
                          runtime/src/platform/debug_server.cpp
                          runtime/src/platform/debug_switch.cpp runtime/src/platform/posix_stubs_switch.cpp
                          runtime/src/platform/startup_checks_switch.cpp runtime/src/platform/prepare_graphics_switch.cpp runtime/src/platform/audio_guard_switch.cpp
                          runtime/src/platform/prepare_graphics_ui_switch.cpp runtime/src/platform/ui_text_switch.cpp runtime/src/platform/shader_scan_switch.cpp runtime/src/platform/shader_list_switch.cpp
                          runtime/src/platform/background_shaders_switch.cpp)
endif()
if(WWHD_SDL_HOST)
  if(NOT TARGET SDL3::SDL3)
    find_package(SDL3 CONFIG REQUIRED)
  endif()
  list(APPEND GFX_SOURCES runtime/src/platform/input_sdl.cpp runtime/src/platform/mouse_sdl.cpp runtime/src/platform/display_rate.cpp)
  if(APPLE)
    enable_language(OBJCXX)
    list(APPEND GFX_SOURCES runtime/src/gfx/vulkan/graphics_menu.mm)
    set_source_files_properties(runtime/src/gfx/vulkan/graphics_menu.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")
  endif()
endif()
set_source_files_properties(${GX2_SOURCES} ${GFX_SOURCES} PROPERTIES COMPILE_OPTIONS "-include;${CEMU_DIR}/cemu_shim.h")
if(ANDROID)
  # Android: SDLActivity loads the game as libmain.so and calls its SDL_main (android/)
  add_library(wwhd SHARED ${RUNTIME_SOURCES} ${GX2_SOURCES} ${GFX_SOURCES} ${MOD_GFX_SOURCES} ${IMGUI_METAL})
  set_target_properties(wwhd PROPERTIES OUTPUT_NAME main)
else()
  add_executable(wwhd ${RUNTIME_SOURCES} ${GX2_SOURCES} ${GFX_SOURCES} ${MOD_GFX_SOURCES} ${IMGUI_METAL})
endif()
# Switch: the guest memory base stays in the reserved register x28 (runtime/include/ppc.h, core.cpp
# guest_call). On by default since v0.4.4 (main thread ~2% faster in the performance test's busy scenes).
option(WWHD_SWITCH_BASE_REG "Switch: keep the guest memory base in x28" ON)
if(WWHD_SWITCH AND WWHD_SWITCH_BASE_REG)
  target_compile_options(gamecode PRIVATE -ffixed-x28)
  target_compile_definitions(gamecode PRIVATE PPC_BASE_REG=1)
  target_compile_options(wwhd PRIVATE -ffixed-x28)
  target_compile_definitions(wwhd PRIVATE PPC_BASE_REG=1)
endif()
# Testing (desktop): sanitizers for the runtime, GX2 and renderer code and Cemu's decompiler, e.g.
# -DWWHD_SANITIZE=address. The generated game code is left out (too large; it only touches guest memory).
set(WWHD_SANITIZE "" CACHE STRING "sanitizers for the runtime (desktop testing), e.g. address or address,undefined")
if(WWHD_SANITIZE AND NOT WWHD_SWITCH)
  foreach(t wwhd cemu_latte)
    target_compile_options(${t} PRIVATE -fsanitize=${WWHD_SANITIZE} -fno-omit-frame-pointer)
  endforeach()
  target_link_options(wwhd PRIVATE -fsanitize=${WWHD_SANITIZE})
endif()
target_include_directories(wwhd PRIVATE runtime/include runtime/src runtime/src/gx2)
add_dependencies(wwhd wwhd_build_info)
target_compile_options(wwhd PRIVATE -Wall -Wno-unused-function -Wno-unused-variable -ffp-contract=off)
if(NOT TARGET ZLIB::ZLIB)
  find_package(ZLIB REQUIRED)
endif()
find_package(Threads REQUIRED)
target_link_libraries(wwhd PRIVATE gamecode cemu_latte imgui ZLIB::ZLIB Threads::Threads)
if(ANDROID)
  target_link_libraries(wwhd PRIVATE log android)
endif()
if(WIN32)
  target_compile_definitions(wwhd PRIVATE _USE_MATH_DEFINES)
  wwhd_windows_resources(wwhd "The Wind Waker HD, native PC port" "wwhd.exe" gui)
  # advapi32: the Windows version for the performance report (report_header.cpp, RegGetValueW)
  target_link_libraries(wwhd PRIVATE winmm ws2_32 advapi32)  # timeBeginPeriod (main.cpp), Cemuhook client sockets (motion/dsu.cpp)
endif()
if(WWHD_HAS_METAL)
  target_compile_definitions(wwhd PRIVATE WWHD_HAS_METAL=1)
  target_link_libraries(wwhd PRIVATE "-framework Metal" "-framework QuartzCore" "-framework AppKit" "-framework Foundation" "-framework ImageIO" "-framework UniformTypeIdentifiers" "-framework GameController" "-framework Carbon" "-framework AudioToolbox" "-framework CoreAudio" compression)
endif()
if(WWHD_HAS_VULKAN)
  # VK_NO_PROTOTYPES: the renderer loads every Vulkan function at run time (gfx/vulkan/loader.h), so the
  # executable starts without a Vulkan loader or with an older one, and can say what is missing
  target_compile_definitions(wwhd PRIVATE WWHD_HAS_VULKAN=1 VK_NO_PROTOTYPES)
  if(WWHD_HAS_METAL)
    # One app, two renderers: the Vulkan loader and glslang are weak imports, so the game still
    # starts (with Metal) on a Mac without them; the renderer checks for them before using Vulkan.
    # SYSTEM: searched after the project's own -I paths, so a Homebrew boost/fmt in the same prefix
    # (/opt/homebrew/include) can't replace the vendored ones (cemu_latte is built with those; a
    # different boost changed LatteDecompilerShader's layout and crashed the first shader translation)
    target_include_directories(wwhd SYSTEM PRIVATE $<TARGET_PROPERTY:Vulkan::Vulkan,INTERFACE_INCLUDE_DIRECTORIES>
                                            $<TARGET_PROPERTY:glslang::glslang,INTERFACE_INCLUDE_DIRECTORIES>)
    foreach(lib Vulkan::Vulkan glslang::glslang glslang::SPIRV glslang::glslang-default-resource-limits)
      target_link_options(wwhd PRIVATE "LINKER:-weak_library,$<TARGET_FILE:${lib}>")
    endforeach()
  elseif(APPLE)
    # SDL host on a Mac: Homebrew's loader is linked; SDL and the renderer find it in the process
    target_link_libraries(wwhd PRIVATE Vulkan::Vulkan glslang::glslang glslang::SPIRV glslang::glslang-default-resource-limits)
  else()
    # Windows, Linux, Android: SDL loads the Vulkan loader at run time (SDL_Vulkan_LoadLibrary);
    # only its headers are used here
    target_include_directories(wwhd SYSTEM PRIVATE $<TARGET_PROPERTY:Vulkan::Vulkan,INTERFACE_INCLUDE_DIRECTORIES>)
    target_link_libraries(wwhd PRIVATE glslang::glslang glslang::SPIRV glslang::glslang-default-resource-limits)
  endif()
endif()
if(WWHD_HAS_DEKO3D)
  target_compile_definitions(wwhd PRIVATE WWHD_HAS_DEKO3D=1)
  find_path(LZ4_INCLUDE_DIR lz4.h REQUIRED)
  find_library(LZ4_LIBRARY NAMES lz4 REQUIRED)
  target_include_directories(wwhd PRIVATE ${LZ4_INCLUDE_DIR})
  # the renderer's own shaders (FPS counter, settings overlay, depth-only): GLSL compiled at build time
  # by devkitPro's uam into DKSH and embedded (bin2s); gfx/deko/shaders/*.glsl, named <name>_<vsh|fsh>.glsl
  file(GLOB DEKO3D_SHADERS CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/runtime/src/gfx/deko/shaders/*.glsl)
  set(_dksh)
  foreach(_glsl ${DEKO3D_SHADERS})
    get_filename_component(_name ${_glsl} NAME_WE)
    if(_name MATCHES "_vsh$")
      set(_stage vert)
    elseif(_name MATCHES "_fsh$")
      set(_stage frag)
    else()
      message(FATAL_ERROR "${_glsl}: deko3d shaders are named <name>_vsh.glsl or <name>_fsh.glsl")
    endif()
    nx_add_shader_program(dksh_${_name} ${_glsl} ${_stage} OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/dksh/${_name}.dksh)
    list(APPEND _dksh dksh_${_name})
  endforeach()
  dkp_add_embedded_binary_library(deko_shaders ${_dksh})
  # deko3d_debug (libdeko3dd): deko3d's checks of every call and its messages (gfx/deko/backend.cpp debug_message: a misuse
  # or failure is logged before the process ends). Release libdeko3d aborts with only a 2359-xxxx result.
  # On while the backend is new (also in Release builds); off for performance measurements.
  option(WWHD_DEKO3D_DEBUG_LIB "deko3d: link the debug library libdeko3dd (also in Release builds)" OFF)
  if(WWHD_DEKO3D_DEBUG_LIB OR CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_definitions(wwhd PRIVATE WWHD_DEKO3D_DEBUG_LIB=1)
    target_link_libraries(wwhd PRIVATE deko_shaders ${LZ4_LIBRARY} deko3d_debug)
  else()
    target_link_libraries(wwhd PRIVATE deko_shaders ${LZ4_LIBRARY} deko3d_release)
  endif()
  # uam on the console: the game shaders that no DKSH cache has (gfx/deko/shaders_dk.cpp, its worker thread)
  target_link_libraries(wwhd PRIVATE uamlib)
  # threads created without attributes get a large stack, newlib's C11 thrd_create results are fixed
  # (core.cpp), and abort() logs its caller first (main.cpp)
  target_link_options(wwhd PRIVATE -Wl,--wrap=pthread_create -Wl,--wrap=thrd_create -Wl,--wrap=abort)
  # the default build is wwhd.nro (what the HOME-menu forwarder starts); the debug-library build says so in
  # hbmenu and is named wwhd_dk_debug.nro, so it can sit in its own folder next to it
  if(WWHD_DEKO3D_DEBUG_LIB OR CMAKE_BUILD_TYPE STREQUAL "Debug")
    nx_generate_nacp(OUTPUT wwhd_dk.nacp NAME "SwitchWakerHD (deko3d debug)" AUTHOR "centollOS" VERSION "0.1.0")
    nx_create_nro(wwhd NACP wwhd_dk.nacp OUTPUT wwhd_dk_debug.nro)
  else()
    nx_generate_nacp(OUTPUT wwhd_dk.nacp NAME "SwitchWakerHD" AUTHOR "centollOS" VERSION "0.1.0")
    nx_create_nro(wwhd NACP wwhd_dk.nacp OUTPUT wwhd.nro)
  endif()
endif()
if(WWHD_SDL_HOST)
  target_compile_definitions(wwhd PRIVATE WWHD_SDL_HOST=1)
  target_link_libraries(wwhd PRIVATE SDL3::SDL3)
  if(APPLE)
    target_link_libraries(wwhd PRIVATE compression "-framework Foundation" "-framework AppKit")
  else()
    if(TARGET lz4_static)
      target_link_libraries(wwhd PRIVATE lz4_static)
    else()
      find_path(LZ4_INCLUDE_DIR lz4.h REQUIRED)
      find_library(LZ4_LIBRARY NAMES lz4 REQUIRED)
      target_include_directories(wwhd PRIVATE ${LZ4_INCLUDE_DIR})
      target_link_libraries(wwhd PRIVATE ${LZ4_LIBRARY} ${CMAKE_DL_LIBS})
    endif()
  endif()
endif()

if(WWHD_HAS_METAL)
add_executable(shadertest runtime/tools/shadertest.cpp runtime/src/gx2/gx2_shader_regs.cpp)
target_include_directories(shadertest PRIVATE runtime/include runtime/src runtime/src/gx2)
target_link_libraries(shadertest PRIVATE cemu_latte)
target_compile_options(shadertest PRIVATE "SHELL:-include ${CEMU_DIR}/cemu_shim.h")

endif()

if(WWHD_SWITCH)
  return()  # host test tools do not run on the console
endif()

option(WWHD_SETUP_GUI "build the graphical installer (wwhd-setup)" OFF)
if(WWHD_SETUP_GUI)
  include(cmake/SetupGui.cmake)
endif()

# recomp mod-manager preferences and catalogue (standalone host stubs)
add_executable(mod_manager_test runtime/tools/mod_manager_test.cpp runtime/src/mods/manager.cpp)
target_include_directories(mod_manager_test PRIVATE runtime/src)
target_compile_options(mod_manager_test PRIVATE -UNDEBUG)

# External native package fixture and isolated lifecycle/integration tests.
add_library(mod_fixture MODULE runtime/tools/mod_fixture.cpp)
target_include_directories(mod_fixture PRIVATE runtime/include)
add_executable(mod_package_test runtime/tools/mod_package_test.cpp runtime/src/mods/manager.cpp runtime/src/mods/packages.cpp runtime/src/mods/mod_archive.cpp runtime/src/mods/content.cpp runtime/src/mods/cemu_pack.cpp runtime/src/guest_addr_identity.c)
target_include_directories(mod_package_test PRIVATE runtime/src runtime/include)
target_compile_options(mod_package_test PRIVATE -UNDEBUG)
target_link_libraries(mod_package_test PRIVATE ZLIB::ZLIB Threads::Threads ${CMAKE_DL_LIBS})

# Synthetic integration tests call the real guest filesystem HLE handlers.
add_executable(mod_content_fs_test runtime/tools/mod_content_fs_test.cpp runtime/src/hle/fs.cpp runtime/src/mods/content.cpp runtime/src/mods/mod_archive.cpp runtime/src/game_languages.cpp runtime/src/guest_addr_identity.c)
target_include_directories(mod_content_fs_test PRIVATE runtime/src runtime/include)
target_compile_options(mod_content_fs_test PRIVATE -UNDEBUG)
target_link_libraries(mod_content_fs_test PRIVATE ZLIB::ZLIB Threads::Threads)

# language sources: synthetic European/Japanese packs read through the guest file system (game_languages.h)
add_executable(language_source_test runtime/tools/language_source_test.cpp runtime/src/hle/fs.cpp runtime/src/mods/content.cpp runtime/src/mods/mod_archive.cpp runtime/src/game_languages.cpp runtime/src/guest_addr_identity.c)
target_include_directories(language_source_test PRIVATE runtime/src runtime/include)
target_compile_options(language_source_test PRIVATE -UNDEBUG)
target_link_libraries(language_source_test PRIVATE ZLIB::ZLIB Threads::Threads)

# controls mapping unit tests (runtime/src/input_map.cpp)
# performance report header: driver version decoding, OS names, overrides list (report_header.h)
add_executable(report_header_test runtime/tools/report_header_test.cpp runtime/src/report_header.cpp ${WWHD_BUILD_INFO})
target_include_directories(report_header_test PRIVATE runtime/src)
target_compile_options(report_header_test PRIVATE -UNDEBUG)
add_dependencies(report_header_test wwhd_build_info)
if(WIN32)
  target_link_libraries(report_header_test PRIVATE advapi32)
endif()

foreach(test register_blocks scheduler_tick full_state_header)
  add_executable(${test}_test runtime/tools/${test}_test.cpp)
  target_include_directories(${test}_test PRIVATE runtime/src)
  target_compile_options(${test}_test PRIVATE -UNDEBUG)
endforeach()
target_link_libraries(scheduler_tick_test PRIVATE Threads::Threads)

add_executable(portable_state_test runtime/tools/portable_state_test.cpp runtime/src/portable_state.cpp)
target_include_directories(portable_state_test PRIVATE runtime/src)
target_compile_options(portable_state_test PRIVATE -UNDEBUG)
add_executable(input_map_test runtime/tools/input_map_test.cpp runtime/src/input_map.cpp)
target_include_directories(input_map_test PRIVATE runtime/src)
# screenshots: PNG writer, file names, binding, requests and the encoding thread (runtime/src/screenshot.cpp)
add_executable(screenshot_test runtime/tools/screenshot_test.cpp runtime/src/screenshot.cpp runtime/src/input_map.cpp)
target_include_directories(screenshot_test PRIVATE runtime/src runtime/include)
target_compile_options(screenshot_test PRIVATE -UNDEBUG)
target_link_libraries(screenshot_test PRIVATE ZLIB::ZLIB Threads::Threads)
# the text prompt's reader of the game's name font (runtime/src/overlay/game_font.cpp)
add_executable(rtl_text_test runtime/tools/rtl_text_test.cpp runtime/src/rtl_text.cpp)
target_include_directories(rtl_text_test PRIVATE runtime/src)
target_compile_options(rtl_text_test PRIVATE -UNDEBUG)
add_executable(game_font_test runtime/tools/game_font_test.cpp runtime/src/overlay/game_font.cpp runtime/src/game_languages.cpp)
target_include_directories(game_font_test PRIVATE runtime/src runtime/include)
target_compile_options(game_font_test PRIVATE -UNDEBUG)
target_link_libraries(game_font_test PRIVATE Threads::Threads)
# the game's languages from its language packs (runtime/src/game_languages.cpp)
add_executable(game_languages_test runtime/tools/game_languages_test.cpp runtime/src/game_languages.cpp)
target_include_directories(game_languages_test PRIVATE runtime/src runtime/include)
target_compile_options(game_languages_test PRIVATE -UNDEBUG)
target_link_libraries(game_languages_test PRIVATE Threads::Threads)
# frame interpolation: blend fractions, display cap and paced scheduling (runtime/src/interp_pacing.h)
add_executable(interp_pacing_test runtime/tools/interp_pacing_test.cpp)
target_compile_options(interp_pacing_test PRIVATE -UNDEBUG)
# quick doors: extra logic steps against the game's process deletion (runtime/src/mods/turbo_steps.h)
add_executable(turbo_steps_test runtime/tools/turbo_steps_test.cpp)
target_compile_options(turbo_steps_test PRIVATE -UNDEBUG)
# quit prompt decisions: when closing the TV window or Quit asks first (runtime/src/quit_prompt.h, issue #65)
add_executable(quit_prompt_test runtime/tools/quit_prompt_test.cpp)
target_compile_options(quit_prompt_test PRIVATE -UNDEBUG)
# texture change detection: page write tracking of guest memory (runtime/src/write_watch.cpp)
add_executable(write_watch_test runtime/tools/write_watch_test.cpp runtime/src/write_watch.cpp)
target_include_directories(write_watch_test PRIVATE runtime/src)
target_compile_options(write_watch_test PRIVATE -UNDEBUG)
target_link_libraries(write_watch_test PRIVATE Threads::Threads)
# recompiled spin waits see other threads' stores (PPC_LOOP in runtime/include/ppc.h, issue #62):
# built with the game code's flags
if(NOT WIN32)
  add_executable(spin_wait_test runtime/tools/spin_wait_test.c)
  target_include_directories(spin_wait_test PRIVATE runtime/include)
  target_compile_options(spin_wait_test PRIVATE -O3 -ffp-contract=off -fno-strict-aliasing -w)
  if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
    target_compile_options(spin_wait_test PRIVATE -mcpu=apple-m1)
  endif()
  target_link_libraries(spin_wait_test PRIVATE Threads::Threads)
  add_test(NAME spin_wait COMMAND spin_wait_test)
endif()
# host audio: TV mix, or TV + GamePad in Off-TV Play, from the game's faders (runtime/src/audio_output_mode.h)
add_executable(audio_output_test runtime/tools/audio_output_test.cpp)
target_include_directories(audio_output_test PRIVATE runtime/src)
target_compile_options(audio_output_test PRIVATE -UNDEBUG)
# Vulkan guest buffer cache: range keys, invalidation by write stamps and hints (runtime/src/gfx/vulkan/buffer_cache_core.h)
add_executable(buffer_cache_test runtime/tools/buffer_cache_test.cpp runtime/src/write_watch.cpp)
target_include_directories(buffer_cache_test PRIVATE runtime/src)
target_compile_options(buffer_cache_test PRIVATE -UNDEBUG)
target_link_libraries(buffer_cache_test PRIVATE Threads::Threads)
# Vulkan snapshot reuse caches never read mapped upload memory (runtime/src/gfx/vulkan/uniform_snapshot.h,
# vertex_snapshot_history.h)
add_executable(snapshot_cache_test runtime/tools/snapshot_cache_test.cpp)
target_include_directories(snapshot_cache_test PRIVATE runtime/src)
target_compile_options(snapshot_cache_test PRIVATE -UNDEBUG)
# gyro: orientation integration, axis mapping, Cemuhook (DSU) packets, mouse as a gyro, settings
# (runtime/src/motion/)
set(MOTION_SOURCES runtime/src/motion/motion.cpp runtime/src/motion/fusion.cpp runtime/src/motion/dsu.cpp)
add_executable(motion_test runtime/tools/motion_test.cpp ${MOTION_SOURCES})
target_include_directories(motion_test PRIVATE runtime/src)
target_compile_options(motion_test PRIVATE -UNDEBUG)
target_link_libraries(motion_test PRIVATE Threads::Threads)
if(WIN32)
  target_link_libraries(motion_test PRIVATE ws2_32)
endif()
# controller rumble: the game's motor requests and the host motor level (runtime/src/rumble.cpp)
add_executable(rumble_test runtime/tools/rumble_test.cpp runtime/src/rumble.cpp)
target_include_directories(rumble_test PRIVATE runtime/src runtime/include)
target_compile_options(rumble_test PRIVATE -UNDEBUG)
target_link_libraries(rumble_test PRIVATE Threads::Threads)

if(WWHD_SDL_HOST)
  add_executable(input_sdl_test runtime/tools/input_sdl_test.cpp runtime/src/input_map.cpp runtime/src/rumble.cpp runtime/src/platform/input_sdl.cpp runtime/src/platform/mouse_sdl.cpp ${MOTION_SOURCES})
  target_include_directories(input_sdl_test PRIVATE runtime/src runtime/include ${CEMU_DIR} ${CEMU_DIR}/Cafe ${CMAKE_SOURCE_DIR}/runtime/third_party/fmt/include)
  target_compile_definitions(input_sdl_test PRIVATE WWHD_HAS_VULKAN=1 WWHD_SDL_HOST=1)
  target_link_libraries(input_sdl_test PRIVATE SDL3::SDL3 Threads::Threads)
  if(WIN32)
    target_link_libraries(input_sdl_test PRIVATE ws2_32)
  endif()
  add_executable(platform_test runtime/tools/platform_test.cpp)
  target_include_directories(platform_test PRIVATE runtime/src)
  target_link_libraries(platform_test PRIVATE Threads::Threads)
  add_executable(sleep_test runtime/tools/sleep_test.cpp)
  target_include_directories(sleep_test PRIVATE runtime/src)
  target_compile_definitions(sleep_test PRIVATE WWHD_SDL_HOST=1)
  target_compile_options(sleep_test PRIVATE -UNDEBUG)
  target_link_libraries(sleep_test PRIVATE SDL3::SDL3 Threads::Threads)
  if(WIN32)
    target_link_libraries(sleep_test PRIVATE winmm)
  endif()
endif()
# These test executables use assertions as checks, including fixture setup.
target_compile_options(input_map_test PRIVATE -UNDEBUG)
if(WWHD_SDL_HOST)
  target_compile_options(input_sdl_test PRIVATE -UNDEBUG)
  target_compile_options(platform_test PRIVATE -UNDEBUG)
endif()

if(WWHD_SDL_HOST)
  add_executable(mouse_sdl_test runtime/tools/mouse_sdl_test.cpp runtime/src/platform/mouse_sdl.cpp)  # motion.h stubbed
  target_include_directories(mouse_sdl_test PRIVATE runtime/src runtime/include ${CEMU_DIR} ${CEMU_DIR}/Cafe ${CMAKE_SOURCE_DIR}/runtime/third_party/fmt/include)
  target_compile_options(mouse_sdl_test PRIVATE -UNDEBUG)
  target_link_libraries(mouse_sdl_test PRIVATE SDL3::Headers)
endif()

# disc image / Cemu archive extractor used by the installer (tools/wudextract; no Python or
# pycryptodome needed) and its tests; zstd (for .wua) from pinned source (cmake/Zstd.cmake)
include(cmake/Zstd.cmake)
add_library(wudcrypto STATIC tools/wudextract/crypto.cpp)
target_include_directories(wudcrypto PUBLIC tools/wudextract)
add_executable(wwhd-extract tools/wudextract/wwhd_extract.cpp tools/wudextract/zarchive.cpp)
target_link_libraries(wwhd-extract PRIVATE wudcrypto wwhd_zstd)
if(WIN32)
  target_link_libraries(wwhd-extract PRIVATE shell32)
  wwhd_windows_resources(wwhd-extract "Wind Waker HD game extractor (disc images, Cemu archives)" "wwhd-extract.exe" console)
  if(WWHD_STRIP_RELEASE)
    target_link_options(wwhd-extract PRIVATE ${WWHD_STRIP_RELEASE})
  endif()
endif()
add_executable(extract_crypto_test tools/wudextract/crypto_test.cpp)
target_link_libraries(extract_crypto_test PRIVATE wudcrypto)
add_executable(extract_test tools/wudextract/extract_test.cpp)
target_link_libraries(extract_test PRIVATE wudcrypto)
add_executable(wua_test tools/wudextract/wua_test.cpp)
target_link_libraries(wua_test PRIVATE wudcrypto wwhd_zstd)

enable_testing()
if(WIN32 AND WWHD_SDL_HOST)
  get_target_property(SDL3_LIBRARY_TYPE SDL3::SDL3 TYPE)
  if(SDL3_LIBRARY_TYPE STREQUAL "SHARED_LIBRARY")
    foreach(SDL3_APP wwhd input_sdl_test sleep_test)
      add_custom_command(TARGET ${SDL3_APP} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:SDL3::SDL3> $<TARGET_FILE_DIR:${SDL3_APP}>
        VERBATIM)
    endforeach()
  endif()
endif()
add_executable(mod_cemu_test runtime/tools/mod_cemu_test.cpp runtime/src/mods/cemu_pack.cpp runtime/src/guest_addr_identity.c)
target_include_directories(mod_cemu_test PRIVATE runtime/src runtime/include)
target_compile_options(mod_cemu_test PRIVATE -UNDEBUG)
add_test(NAME mod_cemu COMMAND mod_cemu_test)
add_test(NAME portable_state COMMAND portable_state_test)
# Verify the shipped EUR address tables without game code, plus the weak USA fallback.
find_package(Python3 COMPONENTS Interpreter QUIET)
if(Python3_Interpreter_FOUND)
  add_test(NAME build_coverage COMMAND "${Python3_EXECUTABLE}" -m unittest discover
    -s "${CMAKE_SOURCE_DIR}/tools/recomp" -p "test*.py")
  add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/guest_map_fixture.c"
    COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/recomp/write_map_fixture.py"
      "${CMAKE_BINARY_DIR}/guest_map_fixture.c"
    DEPENDS tools/recomp/write_map_fixture.py tools/recomp/builds.py tools/recomp/builds/eu.json tools/recomp/recomp.py)
  add_executable(guest_addr_test runtime/tools/guest_addr_test.cpp "${CMAKE_BINARY_DIR}/guest_map_fixture.c")
  target_include_directories(guest_addr_test PRIVATE runtime/include)
  target_compile_options(guest_addr_test PRIVATE -UNDEBUG)
  add_test(NAME guest_addr COMMAND guest_addr_test)
  add_executable(guest_addr_identity_test runtime/tools/guest_addr_test.cpp runtime/src/guest_addr_identity.c)
  target_include_directories(guest_addr_identity_test PRIVATE runtime/include)
  target_compile_definitions(guest_addr_identity_test PRIVATE TEST_IDENTITY)
  target_compile_options(guest_addr_identity_test PRIVATE -UNDEBUG)
  add_test(NAME guest_addr_identity COMMAND guest_addr_identity_test)
endif()

add_test(NAME register_blocks COMMAND register_blocks_test)
add_test(NAME scheduler_tick COMMAND scheduler_tick_test)
add_test(NAME full_state_header COMMAND full_state_header_test)
add_test(NAME mod_manager COMMAND mod_manager_test)
add_test(NAME mod_content_fs_off COMMAND mod_content_fs_test off)
add_test(NAME mod_content_fs_on COMMAND mod_content_fs_test on)
add_test(NAME mod_guest_startup COMMAND mod_package_test --guest-startup)
add_test(NAME mod_guest_metadata COMMAND mod_package_test --guest-metadata)
add_test(NAME mod_cemu_backend COMMAND mod_package_test --cemu-backend)
add_test(NAME mod_cemu_startup COMMAND mod_package_test --cemu-startup)
add_test(NAME mod_content_startup COMMAND mod_package_test --content-startup)
add_test(NAME mod_packages COMMAND mod_package_test ${CMAKE_BINARY_DIR}/package-tests $<TARGET_FILE:mod_fixture>)
add_test(NAME report_header COMMAND report_header_test)
add_test(NAME input_mapping COMMAND input_map_test)
add_test(NAME screenshot COMMAND screenshot_test)
add_test(NAME game_font COMMAND game_font_test)
add_test(NAME rtl_text COMMAND rtl_text_test)
add_test(NAME game_languages COMMAND game_languages_test ${CMAKE_BINARY_DIR}/game_languages_test_work)
add_test(NAME language_sources COMMAND language_source_test)
add_test(NAME interp_pacing COMMAND interp_pacing_test)
add_test(NAME turbo_steps COMMAND turbo_steps_test)
add_test(NAME quit_prompt COMMAND quit_prompt_test)
add_test(NAME write_watch COMMAND write_watch_test)
add_test(NAME buffer_cache COMMAND buffer_cache_test)
add_test(NAME snapshot_caches COMMAND snapshot_cache_test)
add_test(NAME rumble COMMAND rumble_test)
add_test(NAME motion COMMAND motion_test)
add_test(NAME audio_output COMMAND audio_output_test)
add_test(NAME extract_crypto COMMAND extract_crypto_test)
add_test(NAME extract_synthetic COMMAND extract_test $<TARGET_FILE:wwhd-extract> ${CMAKE_BINARY_DIR}/extract_test_work)
add_test(NAME extract_wua COMMAND wua_test $<TARGET_FILE:wwhd-extract> ${CMAKE_BINARY_DIR}/wua_test_work)
if(NOT ANDROID)
  # crash logs name the module of a host crash address (main.cpp, crash_addr.cpp)
  add_test(NAME crash_log_module COMMAND ${CMAKE_COMMAND} -DWWHD=$<TARGET_FILE:wwhd>
           -DWORK=${CMAKE_BINARY_DIR}/crash_log_test_work -P ${CMAKE_SOURCE_DIR}/runtime/tools/crash_log_test.cmake)
endif()
if(WWHD_SDL_HOST)
  add_test(NAME sdl_input COMMAND input_sdl_test)
  set_tests_properties(sdl_input PROPERTIES ENVIRONMENT "WWHD_CONTROLS=${CMAKE_BINARY_DIR}/test-controls.json")
  add_test(NAME host_platform COMMAND platform_test)
  add_test(NAME sdl_mouse COMMAND mouse_sdl_test)
  add_test(NAME host_sleep COMMAND sleep_test)
endif()

add_executable(bc_reference_test runtime/tools/bc_reference_test.cpp)
target_include_directories(bc_reference_test PRIVATE runtime/src)
target_compile_options(bc_reference_test PRIVATE -UNDEBUG)
add_test(NAME bc_reference COMMAND bc_reference_test)

if(ANDROID)
  include(FetchContent)
  # libadrenotools and its pinned linkernsbypass submodule are BSD-2-Clause.
  # The loader uses gipa directly; no volk dependency.
  FetchContent_Declare(wwhd_adrenotools
    GIT_REPOSITORY https://github.com/bylaws/libadrenotools.git
    GIT_TAG 8fae8ce254dfc1344527e05301e43f37dea2df80
    GIT_SUBMODULES lib/linkernsbypass)
  function(wwhd_add_adrenotools)
    set(BUILD_SHARED_LIBS OFF)
    FetchContent_MakeAvailable(wwhd_adrenotools)
  endfunction()
  wwhd_add_adrenotools()
  target_link_libraries(wwhd PRIVATE adrenotools)
  add_dependencies(wwhd main_hook hook_impl file_redirect_hook gsl_alloc_hook)
endif()

add_executable(driver_store_test runtime/tools/driver_store_test.cpp runtime/src/gfx/vulkan/driver_store.cpp runtime/src/mods/mod_archive.cpp)
target_include_directories(driver_store_test PRIVATE runtime/src runtime/third_party/fmt/include)
target_link_libraries(driver_store_test PRIVATE ZLIB::ZLIB)
target_compile_options(driver_store_test PRIVATE -UNDEBUG)
find_package(Python3 COMPONENTS Interpreter QUIET)
if(Python3_Interpreter_FOUND AND NOT ANDROID)
  add_test(NAME driver_store COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/runtime/tools/driver_store_test.py $<TARGET_FILE:driver_store_test>)
endif()

add_executable(perf_average_test runtime/tools/perf_average_test.cpp)
target_include_directories(perf_average_test PRIVATE runtime/src)
target_compile_options(perf_average_test PRIVATE -UNDEBUG)
add_test(NAME perf_average COMMAND perf_average_test)
add_executable(move_speed_test runtime/tools/move_speed_test.cpp)
target_include_directories(move_speed_test PRIVATE runtime/src)
target_compile_options(move_speed_test PRIVATE -UNDEBUG)
add_test(NAME move_speed COMMAND move_speed_test)
add_executable(crash_redact_test runtime/tools/crash_redact_test.cpp)
target_include_directories(crash_redact_test PRIVATE runtime/src)
target_compile_options(crash_redact_test PRIVATE -UNDEBUG)
add_test(NAME crash_redact COMMAND crash_redact_test)

# Cheats must preserve sword ownership: it controls Medli/Makar story placement (#85).
add_executable(cheats_test runtime/tools/cheats_test.cpp runtime/src/mods/cheats.cpp runtime/src/guest_addr_identity.c)
target_include_directories(cheats_test PRIVATE runtime/src runtime/include)
target_compile_options(cheats_test PRIVATE -UNDEBUG)
add_test(NAME cheats_story_progress COMMAND cheats_test)
if(Python3_Interpreter_FOUND)
  add_test(NAME repair_medli COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/savegame/repair_medli_test.py")
  add_test(NAME medli_scenario_evidence COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/runtime/tools/medli_scenario_test.py")
endif()
# Guest mod loader metadata validation; synthetic tables, no game files.
add_executable(guest_validation_test runtime/tools/guest_validation_test.cpp)
target_include_directories(guest_validation_test PRIVATE runtime/include runtime/src)
target_compile_options(guest_validation_test PRIVATE -UNDEBUG)
add_test(NAME guest_validation COMMAND guest_validation_test)

add_executable(guest_hooks_test runtime/tools/guest_hooks_test.cpp runtime/tools/guest_hooks_body.c)
# The entry musttail regression must reach the optimized backend even in Debug builds.
set_source_files_properties(runtime/tools/guest_hooks_body.c PROPERTIES COMPILE_OPTIONS "-O3")
target_include_directories(guest_hooks_test PRIVATE runtime/include runtime/src)
target_compile_options(guest_hooks_test PRIVATE -UNDEBUG)
target_link_libraries(guest_hooks_test PRIVATE ${CMAKE_DL_LIBS})
add_test(NAME guest_hooks COMMAND guest_hooks_test)
if(Python3_Interpreter_FOUND)
  add_test(NAME release_sdk_package COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/release/test_sdk_package.py")
endif()

add_executable(process_test runtime/tools/process_test.cpp)
target_include_directories(process_test PRIVATE runtime/src)
target_compile_options(process_test PRIVATE -UNDEBUG)
add_test(NAME process COMMAND process_test)

add_executable(guest_build_test runtime/tools/guest_build_test.cpp)
target_include_directories(guest_build_test PRIVATE runtime/src)
target_compile_options(guest_build_test PRIVATE -UNDEBUG)
add_test(NAME guest_build COMMAND guest_build_test)

add_executable(guest_heap_test runtime/tools/guest_heap_test.cpp)
target_include_directories(guest_heap_test PRIVATE runtime/src)
target_compile_options(guest_heap_test PRIVATE -UNDEBUG)
target_link_libraries(guest_heap_test PRIVATE Threads::Threads)
add_test(NAME guest_heap COMMAND guest_heap_test)

add_executable(guest_files_test runtime/tools/guest_files_test.cpp)
target_include_directories(guest_files_test PRIVATE runtime/src)
target_compile_options(guest_files_test PRIVATE -UNDEBUG)
target_link_libraries(guest_files_test PRIVATE Threads::Threads)
add_test(NAME guest_files COMMAND guest_files_test ${CMAKE_BINARY_DIR}/guest-files-work)

add_executable(guest_state_test runtime/tools/guest_state_test.cpp)
target_include_directories(guest_state_test PRIVATE runtime/src runtime/include)
target_compile_options(guest_state_test PRIVATE -UNDEBUG)
target_link_libraries(guest_state_test PRIVATE Threads::Threads)
add_test(NAME guest_state COMMAND guest_state_test)

add_executable(code_mods_test runtime/tools/code_mods_test.cpp runtime/src/mods/code_mods.cpp)
target_include_directories(code_mods_test PRIVATE runtime/include runtime/src)
target_compile_options(code_mods_test PRIVATE -UNDEBUG)
target_link_libraries(code_mods_test PRIVATE Threads::Threads)
if(Python3_Interpreter_FOUND)
  add_test(NAME code_mods_service COMMAND code_mods_test "${Python3_EXECUTABLE}")
endif()

add_executable(aspect_panes_test runtime/tools/aspect_panes_test.cpp)
target_include_directories(aspect_panes_test PRIVATE runtime/src)
add_test(NAME aspect_panes COMMAND aspect_panes_test)

if(WWHD_HAS_VULKAN)
  add_executable(barrier_state_test runtime/tools/barrier_state_test.cpp)
  target_include_directories(barrier_state_test PRIVATE runtime/src)
  target_include_directories(barrier_state_test SYSTEM PRIVATE $<TARGET_PROPERTY:Vulkan::Vulkan,INTERFACE_INCLUDE_DIRECTORIES>)
  target_compile_options(barrier_state_test PRIVATE -UNDEBUG)
  add_test(NAME barrier_state COMMAND barrier_state_test)
endif()
