cmake_minimum_required(VERSION 3.21)
file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/VERSION" DC_RELEASE_VERSION LIMIT_COUNT 1)
project(DC_Recomp VERSION ${DC_RELEASE_VERSION} LANGUAGES CXX)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
option(DC_GUI_SUBSYSTEM "Build the Windows player without a console" OFF)
set(DC_GAME_DIR "${CMAKE_SOURCE_DIR}/game" CACHE PATH "User-owned extracted disc files")
# BozoEngine: the release cmake/bozo_engine.json pins, fetched into BozoEngine/ (cmake/bozo_engine_fetch.cmake), unless
# BOZO_ENGINE_DIR names another tree (development: a BozoEngine checkout).
set(BOZO_ENGINE_DIR "" CACHE PATH "BozoEngine's source tree (default: the release cmake/bozo_engine.json pins, in BozoEngine/)")
set(dc_engine_given "")
if(BOZO_ENGINE_DIR)
  file(REAL_PATH "${BOZO_ENGINE_DIR}" dc_engine_given)
endif()
file(REAL_PATH "${CMAKE_SOURCE_DIR}/BozoEngine" dc_engine_in_repo)
if(NOT BOZO_ENGINE_DIR OR dc_engine_given STREQUAL dc_engine_in_repo)
  include("${CMAKE_SOURCE_DIR}/cmake/bozo_engine_fetch.cmake")
  set(BOZO_ENGINE_DIR "${bozo_engine_tree}")
  set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/cmake/bozo_engine.json")
endif()
option(DC_WINDOW "Build the SDL3 native game window" ON)
option(DC_AUDIT_INDIRECT "Instrument generated register transfers for local audit traces" OFF)
set(BOZO_APP ${DC_WINDOW} CACHE BOOL "" FORCE)
set(BOZO_BUILD_TOOLS OFF CACHE BOOL "" FORCE)
set(BOZO_REQUIRE_FFMPEG ON CACHE BOOL "Native movie decoding is required by this port" FORCE)
set(DC_ISO "${CMAKE_SOURCE_DIR}/Dark Cloud (USA).iso" CACHE FILEPATH "User-owned NTSC-U disc image")
add_subdirectory("${BOZO_ENGINE_DIR}" BozoEngine)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
add_executable(dc-indirect-facts tools/dc_indirect_facts.cpp)
target_link_libraries(dc-indirect-facts PRIVATE bozo_recomp_lib)
if(MINGW)
  target_link_options(dc-indirect-facts PRIVATE -static)
endif()
set(dc_gen ${CMAKE_BINARY_DIR}/dc_gen)
file(MAKE_DIRECTORY ${dc_gen})
set(dc_images ${dc_gen}/dc.elf ${dc_gen}/title.elf ${dc_gen}/dun.elf
              ${dc_gen}/title.identity ${dc_gen}/dun.identity)
add_custom_command(OUTPUT ${dc_images}
  COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tools/dc_bozo_prepare.py ${CMAKE_SOURCE_DIR} ${dc_gen} ${DC_GAME_DIR}
  DEPENDS tools/dc_bozo_prepare.py tools/wrap_elf.py tools/symtab_map.py
          ${DC_GAME_DIR}/SCUS_971.11 ${DC_GAME_DIR}/TITLE.BIN ${DC_GAME_DIR}/DUN.BIN recomp/maps/dc_title.csv recomp/maps/dc_dun.csv
  COMMENT "Preparing the resident and overlay ELF images offline")
set(dc_externals ${dc_gen}/dc_externals.txt)
set(dc_overlay_externals ${dc_gen}/dc_overlay_externals.txt)
add_custom_command(OUTPUT ${dc_externals} ${dc_overlay_externals}
  COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tools/dc_bozo_externals.py --overrides ${CMAKE_SOURCE_DIR} ${dc_gen}/dc_overrides.txt
  COMMAND bozo-externals ${DC_GAME_DIR}/SCUS_971.11 ${dc_gen}/native_externals.txt --overrides ${dc_gen}/dc_overrides.txt
  COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tools/dc_bozo_externals.py ${dc_gen}/native_externals.txt ${dc_externals} ${dc_overlay_externals}
  DEPENDS bozo-externals ${CMAKE_SOURCE_DIR}/tools/dc_bozo_externals.py ${CMAKE_SOURCE_DIR}/runtime/dc_native.cpp
          ${CMAKE_SOURCE_DIR}/runtime/dc_port.cpp ${CMAKE_SOURCE_DIR}/runtime/dc_cheats.cpp
          ${CMAKE_SOURCE_DIR}/runtime/dc_ingame_cheats.cpp ${CMAKE_SOURCE_DIR}/runtime/dc_warp.cpp ${CMAKE_SOURCE_DIR}/runtime/dc_pad.cpp ${CMAKE_SOURCE_DIR}/runtime/dc_sound.cpp ${DC_GAME_DIR}/SCUS_971.11)
set(dc_sources)
foreach(image dc title dun)
  set(chunks 32)
  if(NOT image STREQUAL "dc")
    set(chunks 4)
  endif()
  set(files ${dc_gen}/${image}_functions.h ${dc_gen}/${image}_table.cpp)
  math(EXPR last "${chunks} - 1")
  foreach(i RANGE ${last})
    string(LENGTH "${i}" len)
    if(len EQUAL 1)
      set(n "00${i}")
    else()
      set(n "0${i}")
    endif()
    list(APPEND files ${dc_gen}/${image}_${n}.cpp)
  endforeach()
  # Patches belong to an image's compiled table. TITLE and DUN reuse virtual
  # addresses, so their adaptations must never become global runtime overrides.
  set(native_adaptation_inputs ${CMAKE_SOURCE_DIR}/recomp/${image}_native.patches)
  set(native_adaptations --patches ${native_adaptation_inputs})
  set(image_externals ${dc_overlay_externals})
  if(image STREQUAL "dc")
    set(image_externals ${dc_externals})
  endif()
  list(APPEND files ${dc_gen}/${image}.indirect.json)
  set(indirect_instrumentation)
  if(DC_AUDIT_INDIRECT)
    set(indirect_instrumentation COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tools/dc_instrument_indirect.py ${dc_gen} ${image})
  endif()
  add_custom_command(OUTPUT ${files}
    COMMAND bozo-recomp ${dc_gen}/${image}.elf ${dc_gen} --prefix ${image} --symbol-prefix ${image}_ --files ${chunks} --no-comments --externals ${image_externals} ${native_adaptations}
    COMMAND dc-indirect-facts ${dc_gen}/${image}.elf ${native_adaptation_inputs} ${dc_gen}/${image}.indirect.json
    ${indirect_instrumentation}
    DEPENDS bozo-recomp dc-indirect-facts ${dc_gen}/${image}.elf ${image_externals} ${native_adaptation_inputs}
            tools/dc_instrument_indirect.py)
  list(APPEND dc_sources ${files})
endforeach()
add_custom_target(dc_generate DEPENDS ${dc_sources} ${dc_images})
add_library(dc_game STATIC ${dc_sources})
target_link_libraries(dc_game PUBLIC bozo_runtime_headers)
target_include_directories(dc_game PUBLIC ${dc_gen})
if(DC_AUDIT_INDIRECT)
  target_sources(dc_game PRIVATE runtime/dc_indirect_trace.cpp)
  target_include_directories(dc_game PRIVATE ${CMAKE_SOURCE_DIR}/runtime)
  target_compile_definitions(dc_game PUBLIC DC_AUDIT_INDIRECT)
  target_compile_definitions(dc_game PRIVATE DC_TRACE_GAME)
endif()
target_compile_options(dc_game PRIVATE -O1 -w -ffp-contract=off)
add_library(dc_cheats STATIC runtime/dc_cheats.cpp)
target_link_libraries(dc_cheats PUBLIC bozo_host)
add_library(dc_warp STATIC runtime/dc_warp.cpp)
target_link_libraries(dc_warp PUBLIC dc_cheats dc_pad bozo_host)
add_library(dc_ingame_cheats STATIC runtime/dc_ingame_cheats.cpp)
target_link_libraries(dc_ingame_cheats PUBLIC dc_cheats dc_warp dc_language bozo_host)
add_library(dc_pad STATIC runtime/dc_pad.cpp)
target_link_libraries(dc_pad PUBLIC bozo_host)
add_library(dc_language STATIC runtime/dc_language.cpp)
target_include_directories(dc_language PUBLIC ${CMAKE_SOURCE_DIR}/runtime)
add_library(dc_sound STATIC runtime/dc_sound.cpp)
target_link_libraries(dc_sound PUBLIC bozo_host)
add_executable(dc-headless runtime/dc_main.cpp runtime/dc_state.cpp runtime/dc_port.cpp runtime/dc_sound.cpp runtime/dc_native.cpp)
target_link_libraries(dc-headless PRIVATE dc_game bozo_host dc_cheats dc_ingame_cheats dc_warp dc_pad dc_language)
if(MINGW)
  target_link_options(dc-headless PRIVATE -static)
endif()
if(WIN32)
  enable_language(RC)
  target_sources(dc-headless PRIVATE runtime/app/utf8.rc)
  target_include_directories(dc-headless PRIVATE runtime/app)
  set_source_files_properties(runtime/app/utf8.rc PROPERTIES OBJECT_DEPENDS "${CMAKE_SOURCE_DIR}/runtime/app/dc.manifest")
endif()
target_compile_definitions(dc-headless PRIVATE DC_IMAGE_DIR="${dc_gen}")

set(dc_audio_gen ${CMAKE_BINARY_DIR}/dc_audio_gen)
set(dc_audio_outputs ${dc_audio_gen}/dc_audio_modules.h)
set(dc_audio_inputs)
foreach(module LIBSD SDRDRV MODMIDI MODHSYN EZMIDI)
  string(TOLOWER "${module}" lower)
  list(APPEND dc_audio_outputs ${dc_audio_gen}/${lower}_iop.cpp)
  list(APPEND dc_audio_inputs ${DC_GAME_DIR}/MODULES/${module}.IRX)
endforeach()
add_custom_command(OUTPUT ${dc_audio_outputs}
  COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tools/dc_bozo_audio.py ${CMAKE_SOURCE_DIR} ${dc_audio_gen} ${DC_GAME_DIR} ${BOZO_ENGINE_DIR}
  DEPENDS ${dc_audio_inputs} tools/dc_bozo_audio.py
          ${BOZO_ENGINE_DIR}/tools/native_irx/relocate.py ${BOZO_ENGINE_DIR}/tools/native_irx/recompile.py
  COMMENT "Statically recompiling the native sound libraries (strict mode)")
add_custom_target(dc_native_audio DEPENDS ${dc_audio_outputs})
add_library(dc_audio_code OBJECT ${dc_audio_gen}/libsd_iop.cpp ${dc_audio_gen}/sdrdrv_iop.cpp ${dc_audio_gen}/modmidi_iop.cpp ${dc_audio_gen}/modhsyn_iop.cpp ${dc_audio_gen}/ezmidi_iop.cpp)
target_link_libraries(dc_audio_code PRIVATE bozo_runtime_headers)
target_compile_options(dc_audio_code PRIVATE -O1 -w)
target_sources(dc-headless PRIVATE $<TARGET_OBJECTS:dc_audio_code> runtime/dc_audio.cpp)
target_include_directories(dc-headless PRIVATE ${dc_audio_gen})
add_dependencies(dc-headless dc_native_audio)

if(DC_WINDOW)
  add_executable(dc runtime/dc_main.cpp runtime/dc_state.cpp runtime/dc_port.cpp runtime/dc_sound.cpp runtime/dc_native.cpp runtime/dc_audio.cpp runtime/dc_cheats_ui.cpp $<TARGET_OBJECTS:dc_audio_code>)
  target_link_libraries(dc PRIVATE dc_game bozo_host bozo_app dc_cheats dc_ingame_cheats dc_warp dc_pad dc_language)
  if(WIN32)
    enable_language(RC)
    string(REPLACE "." "," DC_FILEVERSION "${PROJECT_VERSION}.0")
    configure_file(runtime/app/dc.rc.in "${CMAKE_CURRENT_BINARY_DIR}/dc.rc" @ONLY)
    set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/dc.rc" PROPERTIES OBJECT_DEPENDS "${CMAKE_SOURCE_DIR}/runtime/app/dc.manifest")
    target_sources(dc PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/dc.rc")
    if(DC_GUI_SUBSYSTEM)
      set_target_properties(dc PROPERTIES WIN32_EXECUTABLE TRUE)
    endif()
    target_include_directories(dc PRIVATE runtime/app)
  endif()
  target_include_directories(dc PRIVATE ${dc_audio_gen})
  add_dependencies(dc dc_native_audio)
  target_compile_definitions(dc PRIVATE DC_WITH_WINDOW DC_IMAGE_DIR="${dc_gen}")
  if(MINGW)
    target_link_options(dc PRIVATE -static)
  endif()
endif()

# Offline texture conversion is the only place that knows the disc's console texture layout.
set(dc_textures ${CMAKE_BINARY_DIR}/textures/dc_textures.bin)
add_custom_command(OUTPUT ${dc_textures}
  COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/textures
  COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tools/dc_texconv.py ${DC_GAME_DIR} ${dc_textures} --progress
  DEPENDS tools/dc_texconv.py tools/gs_layout.py ${DC_GAME_DIR}/DATA.HD2 ${DC_GAME_DIR}/DATA.DAT
  USES_TERMINAL
  COMMENT "Converting the user-owned disc textures offline")
add_custom_target(dc_assets DEPENDS ${dc_textures})
foreach(target dc-headless dc)
  if(TARGET ${target})
    add_dependencies(${target} dc_assets)
    target_compile_definitions(${target} PRIVATE
      DC_DEFAULT_TEXTURES="${dc_textures}" DC_DEFAULT_ELF="${DC_GAME_DIR}/SCUS_971.11"
      DC_DEFAULT_ISO="${DC_ISO}")
  endif()
endforeach()

option(DC_ENGINE_TESTS "Build BozoEngine's regression suites alongside the port" OFF)
if(DC_ENGINE_TESTS)
  enable_testing()
  set(BOZO_TEST_ELF "${dc_gen}/dc.elf")
  add_subdirectory("${BOZO_ENGINE_DIR}/tests" engine_tests)
  add_executable(dc-cheat-tests tests/dc_cheat_tests.cpp)
  target_link_libraries(dc-cheat-tests PRIVATE dc_game dc_cheats bozo_host)
  if(MINGW)
    target_link_options(dc-cheat-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_cheats COMMAND dc-cheat-tests)
  add_executable(dc-cheat-extra-tests tests/dc_cheat_extra_tests.cpp)
  target_link_libraries(dc-cheat-extra-tests PRIVATE dc_cheats bozo_host)
  if(MINGW)
    target_link_options(dc-cheat-extra-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_cheat_extras COMMAND dc-cheat-extra-tests)
  add_executable(dc-ingame-cheat-menu-tests tests/dc_ingame_cheat_menu_tests.cpp)
  target_link_libraries(dc-ingame-cheat-menu-tests PRIVATE dc_ingame_cheats bozo_host)
  if(MINGW)
    target_link_options(dc-ingame-cheat-menu-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_ingame_cheats COMMAND dc-ingame-cheat-menu-tests)
  add_executable(dc-pad-tests tests/dc_pad_tests.cpp)
  target_link_libraries(dc-pad-tests PRIVATE dc_pad bozo_host)
  if(MINGW)
    target_link_options(dc-pad-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_pad COMMAND dc-pad-tests)
  add_executable(dc-language-tests tests/dc_language_tests.cpp)
  target_link_libraries(dc-language-tests PRIVATE dc_language)
  if(MINGW)
    target_link_options(dc-language-tests PRIVATE -static)
  endif()
  add_test(NAME dc_language COMMAND dc-language-tests)
  add_executable(dc-sound-tests tests/dc_sound_tests.cpp)
  target_link_libraries(dc-sound-tests PRIVATE dc_sound bozo_host)
  if(MINGW)
    target_link_options(dc-sound-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_sound_transport COMMAND dc-sound-tests)
  add_executable(dc-cd-cancel-tests tests/dc_cd_cancel_tests.cpp)
  target_link_libraries(dc-cd-cancel-tests PRIVATE dc_game bozo_host)
  if(MINGW)
    target_link_options(dc-cd-cancel-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_cd_cancel COMMAND dc-cd-cancel-tests)
  set_tests_properties(dc_native_cd_cancel PROPERTIES TIMEOUT 15)
  if(DC_WINDOW)
    # Local diagnostic: the caller supplies a RAM capture; no assets are embedded.
    add_executable(dc-sky-replay tools/dc_sky_replay.cpp runtime/dc_native.cpp runtime/dc_port.cpp runtime/dc_sound.cpp)
    target_link_libraries(dc-sky-replay PRIVATE dc_game bozo_host bozo_app)
    target_compile_definitions(dc-sky-replay PRIVATE DC_DEFAULT_TEXTURES="${dc_textures}")
    if(MINGW)
      target_link_options(dc-sky-replay PRIVATE -static)
    endif()
  endif()
  add_executable(dc-native-tests tests/dc_native_tests.cpp runtime/dc_native.cpp runtime/dc_port.cpp runtime/dc_sound.cpp)
  target_link_libraries(dc-native-tests PRIVATE bozo_host)
  target_compile_definitions(dc-native-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-native-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_packet_boundary COMMAND dc-native-tests)
  add_executable(dc-model-builders-tests tests/dc_model_builders_tests.cpp runtime/dc_native.cpp)
  target_link_libraries(dc-model-builders-tests PRIVATE dc_game bozo_host)
  target_compile_definitions(dc-model-builders-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-model-builders-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_asset_builders COMMAND dc-model-builders-tests)
  add_executable(dc-model-lifetime-tests tests/dc_model_lifetime_tests.cpp runtime/dc_native.cpp)
  target_link_libraries(dc-model-lifetime-tests PRIVATE bozo_host)
  target_compile_definitions(dc-model-lifetime-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-model-lifetime-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_model_lifetime COMMAND dc-model-lifetime-tests)
  add_executable(dc-water-refraction-tests tests/dc_water_refraction_tests.cpp runtime/dc_native.cpp)
  target_link_libraries(dc-water-refraction-tests PRIVATE bozo_host)
  target_compile_definitions(dc-water-refraction-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-water-refraction-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_water_refraction COMMAND dc-water-refraction-tests)
  if(DC_WINDOW)
    target_link_libraries(dc-water-refraction-tests PRIVATE bozo_app)
    target_compile_definitions(dc-water-refraction-tests PRIVATE DC_WATER_GPU)
    add_test(NAME dc_native_water_refraction_pixels COMMAND dc-water-refraction-tests --gpu)
    set_tests_properties(dc_native_water_refraction_pixels PROPERTIES LABELS "gpu")
  endif()
  add_executable(dc-fishing-line-tests tests/dc_fishing_line_tests.cpp runtime/dc_native.cpp runtime/dc_port.cpp runtime/dc_sound.cpp)
  target_link_libraries(dc-fishing-line-tests PRIVATE bozo_host)
  target_compile_definitions(dc-fishing-line-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-fishing-line-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_fishing_line COMMAND dc-fishing-line-tests)
  add_executable(dc-debug-font-tests tests/dc_debug_font_tests.cpp runtime/dc_native.cpp runtime/dc_port.cpp runtime/dc_sound.cpp)
  target_link_libraries(dc-debug-font-tests PRIVATE bozo_host)
  target_compile_definitions(dc-debug-font-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-debug-font-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_debug_font COMMAND dc-debug-font-tests)
  if(DC_WINDOW)
    add_executable(dc-balloon-tests tests/dc_balloon_tests.cpp runtime/dc_native.cpp)
    target_link_libraries(dc-balloon-tests PRIVATE bozo_host bozo_app)
    target_compile_definitions(dc-balloon-tests PRIVATE DC_DEFAULT_TEXTURES="")
    if(MINGW)
      target_link_options(dc-balloon-tests PRIVATE -static)
    endif()
    add_test(NAME dc_native_balloon_mask COMMAND dc-balloon-tests)
    set_tests_properties(dc_native_balloon_mask PROPERTIES LABELS "gpu")
    add_executable(dc-generated-texture-tests tests/dc_generated_texture_tests.cpp runtime/dc_native.cpp)
    target_link_libraries(dc-generated-texture-tests PRIVATE bozo_host bozo_app)
    target_compile_definitions(dc-generated-texture-tests PRIVATE DC_DEFAULT_TEXTURES="")
    if(MINGW)
      target_link_options(dc-generated-texture-tests PRIVATE -static)
    endif()
    add_test(NAME dc_native_generated_texture_alpha COMMAND dc-generated-texture-tests)
    set_tests_properties(dc_native_generated_texture_alpha PROPERTIES LABELS "gpu")
    add_executable(dc-sprite-seam-tests tests/dc_sprite_seam_tests.cpp runtime/dc_native.cpp)
    target_link_libraries(dc-sprite-seam-tests PRIVATE bozo_host bozo_app)
    target_compile_definitions(dc-sprite-seam-tests PRIVATE DC_DEFAULT_TEXTURES="")
    if(MINGW)
      target_link_options(dc-sprite-seam-tests PRIVATE -static)
    endif()
    add_test(NAME dc_native_sprite_seams COMMAND dc-sprite-seam-tests)
    set_tests_properties(dc_native_sprite_seams PROPERTIES LABELS "gpu")
    add_executable(dc-model-sampler-tests tests/dc_model_sampler_tests.cpp runtime/dc_native.cpp)
    target_link_libraries(dc-model-sampler-tests PRIVATE bozo_host bozo_app)
    target_compile_definitions(dc-model-sampler-tests PRIVATE DC_DEFAULT_TEXTURES="")
    if(MINGW)
      target_link_options(dc-model-sampler-tests PRIVATE -static)
    endif()
    add_test(NAME dc_native_model_sampler COMMAND dc-model-sampler-tests)
    set_tests_properties(dc_native_model_sampler PROPERTIES LABELS "gpu")
    add_executable(dc-shadow-mask-tests tests/dc_shadow_mask_tests.cpp runtime/dc_native.cpp)
    target_link_libraries(dc-shadow-mask-tests PRIVATE bozo_host bozo_app)
    target_compile_definitions(dc-shadow-mask-tests PRIVATE DC_DEFAULT_TEXTURES="")
    if(MINGW)
      target_link_options(dc-shadow-mask-tests PRIVATE -static)
    endif()
    add_test(NAME dc_native_shadow_mask COMMAND dc-shadow-mask-tests)
    set_tests_properties(dc_native_shadow_mask PROPERTIES LABELS "gpu")
  endif()
  add_executable(dc-texture-services-tests tests/dc_texture_services_tests.cpp runtime/dc_native.cpp)
  target_link_libraries(dc-texture-services-tests PRIVATE dc_game bozo_host)
  target_compile_definitions(dc-texture-services-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-texture-services-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_texture_services COMMAND dc-texture-services-tests)
  add_executable(dc-window-services-tests tests/dc_window_services_tests.cpp runtime/dc_native.cpp)
  target_link_libraries(dc-window-services-tests PRIVATE dc_game bozo_host)
  target_compile_definitions(dc-window-services-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-window-services-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_window_services COMMAND dc-window-services-tests)
  set(dc_visibility_gen ${CMAKE_BINARY_DIR}/visibility_reference)
  set(dc_visibility_outputs ${dc_visibility_gen}/dcvis_000.cpp ${dc_visibility_gen}/dcvis_table.cpp
                            ${dc_visibility_gen}/dcvis_functions.h)
  add_custom_command(OUTPUT ${dc_visibility_outputs}
    COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/make_visibility_fixture.py
      ${DC_GAME_DIR}/SCUS_971.11 ${dc_visibility_gen}/reference.elf
    COMMAND bozo-recomp ${dc_visibility_gen}/reference.elf ${dc_visibility_gen} --prefix dcvis
      --symbol-prefix dcvis_ --files 1 --no-comments --externals ${dc_visibility_gen}/reference.externals
    DEPENDS bozo-recomp tests/make_visibility_fixture.py ${DC_GAME_DIR}/SCUS_971.11)
  add_executable(dc-visibility-tests tests/dc_visibility_tests.cpp runtime/dc_native.cpp runtime/dc_port.cpp runtime/dc_sound.cpp
    ${dc_visibility_outputs})
  target_link_libraries(dc-visibility-tests PRIVATE dc_game bozo_host)
  target_include_directories(dc-visibility-tests PRIVATE runtime ${dc_visibility_gen})
  target_compile_definitions(dc-visibility-tests PRIVATE DC_DEFAULT_TEXTURES="")
  if(MINGW)
    target_link_options(dc-visibility-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_visibility COMMAND dc-visibility-tests)
  function(dc_packet_wrapper_fixture kind)
    set(fixture_dir ${CMAKE_BINARY_DIR}/${kind}_reference)
    set(outputs)
    foreach(variant ${kind}_ref ${kind}_native)
      list(APPEND outputs ${fixture_dir}/${variant}_000.cpp ${fixture_dir}/${variant}_table.cpp
                          ${fixture_dir}/${variant}_functions.h)
    endforeach()
    add_custom_command(OUTPUT ${outputs}
      COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/make_packet_wrapper_fixture.py ${kind}
        ${DC_GAME_DIR}/SCUS_971.11 ${CMAKE_SOURCE_DIR}/recomp/dc_native.patches ${fixture_dir}/reference.elf
      COMMAND bozo-recomp ${fixture_dir}/reference.elf ${fixture_dir} --prefix ${kind}_ref
        --symbol-prefix ${kind}_ref_ --files 1 --no-comments --externals ${fixture_dir}/reference.externals
        --patches ${fixture_dir}/reference.baseline.patches
      COMMAND bozo-recomp ${fixture_dir}/reference.elf ${fixture_dir} --prefix ${kind}_native
        --symbol-prefix ${kind}_native_ --files 1 --no-comments --externals ${fixture_dir}/reference.native.externals
        --patches ${fixture_dir}/reference.patches
      DEPENDS bozo-recomp tests/make_packet_wrapper_fixture.py ${DC_GAME_DIR}/SCUS_971.11 recomp/dc_native.patches)
    set(wrapper_test tests/dc_${kind}_wrapper_tests.cpp)
    if(kind STREQUAL "texanim_state" OR kind STREQUAL "item_state")
      set(wrapper_test tests/dc_copy_state_wrapper_tests.cpp)
    endif()
    if(kind STREQUAL "shadow_fast" OR kind STREQUAL "shadow_fast2" OR kind STREQUAL "shadow")
      set(wrapper_test tests/dc_shadow_wrapper_tests.cpp)
    endif()
    if(kind MATCHES "^(packet_init|begin_frame|resident_chain|loading_init)$")
      set(wrapper_test tests/dc_resident_packet_tests.cpp)
    endif()
    if(kind MATCHES "^(packet_reserve|buffer_clear|packet_handle|packet_get)$")
      set(wrapper_test tests/dc_packet_storage_tests.cpp)
    endif()
    add_executable(dc-${kind}-wrapper-tests ${wrapper_test} ${outputs})
    if(kind MATCHES "^(packet_init|begin_frame|resident_chain|loading_init|packet_reserve|buffer_clear|packet_handle|packet_get)$")
      string(TOUPPER ${kind} resident_case)
      target_compile_definitions(dc-${kind}-wrapper-tests PRIVATE DC_${resident_case})
    endif()
    if(kind STREQUAL "item_state")
      target_compile_definitions(dc-${kind}-wrapper-tests PRIVATE DC_ITEM_COPY)
    endif()
    if(kind STREQUAL "shadow_fast")
      target_compile_definitions(dc-${kind}-wrapper-tests PRIVATE DC_SHADOW_FAST)
    elseif(kind STREQUAL "shadow_fast2")
      target_compile_definitions(dc-${kind}-wrapper-tests PRIVATE DC_SHADOW_FAST2)
    endif()
    if(kind STREQUAL "frame_draw" OR kind STREQUAL "frame_member")
      target_sources(dc-${kind}-wrapper-tests PRIVATE runtime/dc_native.cpp)
      target_compile_definitions(dc-${kind}-wrapper-tests PRIVATE DC_DEFAULT_TEXTURES="")
    endif()
    if(kind STREQUAL "loading" OR kind STREQUAL "fishing" OR kind STREQUAL "debug_font")
      target_sources(dc-${kind}-wrapper-tests PRIVATE runtime/dc_native.cpp runtime/dc_port.cpp runtime/dc_sound.cpp)
      target_compile_definitions(dc-${kind}-wrapper-tests PRIVATE DC_DEFAULT_TEXTURES="")
    endif()
    target_link_libraries(dc-${kind}-wrapper-tests PRIVATE dc_game bozo_host)
    target_include_directories(dc-${kind}-wrapper-tests PRIVATE ${fixture_dir})
    if(MINGW)
      target_link_options(dc-${kind}-wrapper-tests PRIVATE -static)
    endif()
    if(kind MATCHES "^(packet_init|begin_frame|resident_chain|loading_init)$")
      add_test(NAME dc_native_${kind}_wrapper COMMAND dc-${kind}-wrapper-tests ${DC_GAME_DIR}/SCUS_971.11)
    else()
      add_test(NAME dc_native_${kind}_wrapper COMMAND dc-${kind}-wrapper-tests)
    endif()
  endfunction()
  dc_packet_wrapper_fixture(water)
  dc_packet_wrapper_fixture(effect)
  dc_packet_wrapper_fixture(frame_close)
  dc_packet_wrapper_fixture(frame_submit)
  dc_packet_wrapper_fixture(frame_draw)
  dc_packet_wrapper_fixture(frame_member)
  dc_packet_wrapper_fixture(mdt_draw)
  dc_packet_wrapper_fixture(message)
  dc_packet_wrapper_fixture(edit_state)
  dc_packet_wrapper_fixture(menu_state)
  dc_packet_wrapper_fixture(loading)
  dc_packet_wrapper_fixture(texanime)
  dc_packet_wrapper_fixture(fire)
  dc_packet_wrapper_fixture(texanim_state)
  dc_packet_wrapper_fixture(item_state)
  dc_packet_wrapper_fixture(language)
  dc_packet_wrapper_fixture(fishing)
  dc_packet_wrapper_fixture(shadow_fast)
  dc_packet_wrapper_fixture(shadow_fast2)
  dc_packet_wrapper_fixture(shadow)
  dc_packet_wrapper_fixture(debug_font)
  dc_packet_wrapper_fixture(packet_init)
  dc_packet_wrapper_fixture(begin_frame)
  dc_packet_wrapper_fixture(resident_chain)
  dc_packet_wrapper_fixture(loading_init)
  dc_packet_wrapper_fixture(packet_reserve)
  dc_packet_wrapper_fixture(buffer_clear)
  dc_packet_wrapper_fixture(packet_handle)
  dc_packet_wrapper_fixture(packet_get)
  function(dc_overlay_packet_fixture kind image)
    set(fixture_dir ${CMAKE_BINARY_DIR}/${kind}_reference)
    string(TOUPPER ${image} raw_image)
    set(outputs ${fixture_dir}/overlay_case.h)
    foreach(variant ${kind}_ref ${kind}_native)
      list(APPEND outputs ${fixture_dir}/${variant}_000.cpp ${fixture_dir}/${variant}_table.cpp
                          ${fixture_dir}/${variant}_functions.h)
    endforeach()
    add_custom_command(OUTPUT ${outputs}
      COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/make_overlay_packet_fixture.py ${kind}
        ${dc_gen}/${image}.elf ${DC_GAME_DIR}/${raw_image}.BIN
        ${CMAKE_SOURCE_DIR}/recomp/${image}_native.patches ${fixture_dir}/reference.elf
      COMMAND bozo-recomp ${fixture_dir}/reference.elf ${fixture_dir} --prefix ${kind}_ref
        --symbol-prefix ${kind}_ref_ --files 1 --no-comments --externals ${fixture_dir}/reference.externals
      COMMAND bozo-recomp ${fixture_dir}/reference.elf ${fixture_dir} --prefix ${kind}_native
        --symbol-prefix ${kind}_native_ --files 1 --no-comments --externals ${fixture_dir}/reference.externals
        --patches ${fixture_dir}/reference.patches
      DEPENDS bozo-recomp ${dc_gen}/${image}.elf game/${raw_image}.BIN recomp/${image}_native.patches
              tests/make_overlay_packet_fixture.py tests/overlay_packet_cases.py)
    add_executable(dc-${kind}-packet-tests tests/dc_overlay_packet_tests.cpp ${outputs})
    target_link_libraries(dc-${kind}-packet-tests PRIVATE dc_game bozo_host)
    target_include_directories(dc-${kind}-packet-tests PRIVATE ${fixture_dir})
    if(MINGW)
      target_link_options(dc-${kind}-packet-tests PRIVATE -static)
    endif()
    add_test(NAME dc_native_${kind}_packets COMMAND dc-${kind}-packet-tests ${dc_gen}/${image}.elf)
  endfunction()
  foreach(address 1db0780 1db82d0 1db8550 1db87d0 1dbb8a0 1dbba90 1dc3ad0 1dc3c90 1dc8930 1dc9e70 1dd2220)
    dc_overlay_packet_fixture(title_${address} title)
  endforeach()
  foreach(address 1dae2a0 1dc1510)
    dc_overlay_packet_fixture(dun_${address} dun)
  endforeach()
  add_executable(dc-packet-audit-tests tests/dc_packet_audit_tests.cpp runtime/dc_native.cpp runtime/dc_port.cpp runtime/dc_sound.cpp)
  target_link_libraries(dc-packet-audit-tests PRIVATE dc_game bozo_host)
  target_compile_definitions(dc-packet-audit-tests PRIVATE DC_DEFAULT_TEXTURES="" DC_PACKET_AUDIT_GAME)
  if(MINGW)
    target_link_options(dc-packet-audit-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_packet_audit COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tools/dc_packet_audit.py
    --root ${CMAKE_SOURCE_DIR} --registry $<TARGET_FILE:dc-packet-audit-tests> --images ${dc_gen})
  set(dun_dispatch_dir ${CMAKE_BINARY_DIR}/dun_dispatch_fixture)
  set(dun_dispatch_outputs ${dun_dispatch_dir}/dun_dispatch_000.cpp ${dun_dispatch_dir}/dun_dispatch_table.cpp
      ${dun_dispatch_dir}/dun_dispatch_functions.h ${dun_dispatch_dir}/dispatch_cases.h)
  add_custom_command(OUTPUT ${dun_dispatch_outputs}
    COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/make_dun_dispatch_fixture.py
      ${dc_gen}/dun.elf ${DC_GAME_DIR}/DUN.BIN ${CMAKE_SOURCE_DIR}/recomp/dun_native.patches
      ${dc_gen}/dun.indirect.json ${dun_dispatch_dir}
    COMMAND bozo-recomp ${dun_dispatch_dir}/fixture.elf ${dun_dispatch_dir} --prefix dun_dispatch
      --symbol-prefix dun_dispatch_ --files 1 --no-comments --externals ${dun_dispatch_dir}/fixture.externals
    DEPENDS bozo-recomp tests/make_dun_dispatch_fixture.py tools/dc_packet_audit.py tools/dc_alias_audit.py tools/symtab_map.py
      ${dc_gen}/dun.elf ${dc_gen}/dun.indirect.json ${DC_GAME_DIR}/DUN.BIN recomp/dun_native.patches)
  add_executable(dc-dun-dispatch-tests tests/dc_dun_dispatch_tests.cpp ${dun_dispatch_outputs})
  target_link_libraries(dc-dun-dispatch-tests PRIVATE bozo_host)
  target_include_directories(dc-dun-dispatch-tests PRIVATE ${dun_dispatch_dir})
  if(MINGW)
    target_link_options(dc-dun-dispatch-tests PRIVATE -static)
  endif()
  add_test(NAME dc_dun_remaining_dispatch COMMAND dc-dun-dispatch-tests ${dun_dispatch_dir}/fixture.elf)
  add_executable(dc-dispatch-assignment-tests tests/dc_dispatch_assignment_tests.cpp)
  target_link_libraries(dc-dispatch-assignment-tests PRIVATE dc_game bozo_host)
  if(MINGW)
    target_link_options(dc-dispatch-assignment-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_dispatch_assignment COMMAND dc-dispatch-assignment-tests)
  add_executable(dc-bulk-dispatch-tests tests/dc_bulk_dispatch_tests.cpp)
  target_link_libraries(dc-bulk-dispatch-tests PRIVATE dc_game bozo_host)
  if(MINGW)
    target_link_options(dc-bulk-dispatch-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_bulk_dispatch COMMAND dc-bulk-dispatch-tests)
  add_executable(dc-loadchara-bounds-tests tests/dc_loadchara_bounds_tests.cpp)
  target_link_libraries(dc-loadchara-bounds-tests PRIVATE dc_game bozo_host)
  if(MINGW)
    target_link_options(dc-loadchara-bounds-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_loadchara_bounds COMMAND dc-loadchara-bounds-tests)
  add_executable(dc-readinfo-tests tests/dc_readinfo_tests.cpp)
  target_link_libraries(dc-readinfo-tests PRIVATE dc_game bozo_host)
  if(MINGW)
    target_link_options(dc-readinfo-tests PRIVATE -static)
  endif()
  add_test(NAME dc_native_readinfo_effects COMMAND dc-readinfo-tests)
  add_executable(dc-indirect-trace-tests tests/dc_indirect_trace_tests.cpp runtime/dc_indirect_trace.cpp)
  target_link_libraries(dc-indirect-trace-tests PRIVATE dc_game bozo_host)
  target_compile_definitions(dc-indirect-trace-tests PRIVATE DC_TRACE_GAME)
  target_include_directories(dc-indirect-trace-tests PRIVATE runtime)
  if(MINGW)
    target_link_options(dc-indirect-trace-tests PRIVATE -static)
  endif()
  add_test(NAME dc_indirect_tools COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/test_indirect_audit.py
    $<TARGET_FILE:dc-indirect-facts> $<TARGET_FILE:dc-indirect-trace-tests> --game-modules)
  add_test(NAME dc_external_image_boundaries COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/test_externals.py)
  add_test(NAME dc_pointer_alias_tools COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/test_alias_audit.py)
  add_test(NAME dc_callee_effect_tools COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/test_callee_audit.py)
  add_test(NAME dc_loadchara_bounds_tools COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/test_loadchara_audit.py)
  add_test(NAME dc_readinfo_effect_tools COMMAND ${Python3_EXECUTABLE} -I ${CMAKE_SOURCE_DIR}/tests/test_readinfo_audit.py)
endif()

if(BUILD_TESTING)
  add_test(NAME dc_release_tools COMMAND ${Python3_EXECUTABLE} -I "${CMAKE_SOURCE_DIR}/tests/test_release_tools.py")
endif()
