set(UC_GENERATED_DIR ${CMAKE_SOURCE_DIR}/generated CACHE PATH "translator output")
add_library(uc_options STATIC options.cpp window_mode.cpp)
target_link_libraries(uc_options PUBLIC uc_path_io)
target_include_directories(uc_options PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/renderer)
target_link_libraries(uc_options PUBLIC SDL3::SDL3 uc_input_prompts uc_netplay uc_gamepad_config uc_wiimote_config uc_coop_config)
add_library(uc_options_config STATIC options_config.cpp)
target_include_directories(uc_options_config PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_options_config PUBLIC uc_options uc_input_config)
add_library(uc_live_settings STATIC live_settings.cpp)   # in-game settings menu: apply a change everywhere and save it
target_include_directories(uc_live_settings PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_live_settings PUBLIC uc_options_config)
add_library(uc_intro STATIC intro.cpp)
target_include_directories(uc_intro PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_intro PUBLIC uc_runtime)
add_library(uc_game_pacing STATIC game_pacing.cpp game_loop.cpp pacing_fixes.cpp cpu_sampler.cpp counter_probe.cpp write_trap.cpp state_dump.cpp)
target_include_directories(uc_game_pacing PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_game_pacing PUBLIC uc_runtime uc_mods)
add_library(uc_crash_bundle STATIC crash_bundle.cpp)
target_include_directories(uc_crash_bundle PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_crash_bundle PUBLIC uc_runtime)
add_library(uc_input_probe STATIC input_probe.cpp entry_probe.cpp audio_startup.cpp)
target_include_directories(uc_input_probe PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_input_probe PUBLIC uc_runtime uc_mods)   # entry_probe.h includes the mod hook points (modding/hooks.h)
add_library(uc_prompt_bridge STATIC prompt_bridge.cpp)
target_include_directories(uc_prompt_bridge PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_prompt_bridge PUBLIC uc_input_prompts uc_gx)
add_library(uc_host_input STATIC host_input.cpp)
target_include_directories(uc_host_input PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_host_input PUBLIC uc_runtime SDL3::SDL3)
add_library(uc_pointer_feed STATIC pointer_feed.cpp)   # a second player's mouse or light gun (read as a device of its own) as the SDL mouse events HostInput reads
target_include_directories(uc_pointer_feed PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/common)
target_link_libraries(uc_pointer_feed PUBLIC uc_host_input)
add_library(uc_coop_session STATIC coop_session.cpp)   # local two-player play: who joins, and whose input each device feeds
target_include_directories(uc_coop_session PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_library(uc_gamepad_input STATIC gamepad_input.cpp)   # SDL gamepads: events in, NativeInput sticks, buttons and gyro out
target_include_directories(uc_gamepad_input PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_gamepad_input PUBLIC uc_input uc_gamepad_math SDL3::SDL3)   # no runtime: the launcher reads gamepads too
add_library(uc_wiimote_input STATIC wiimote_input.cpp $<IF:$<BOOL:${WIN32}>,wiimote_hid.cpp,wiimote_evdev.cpp>)   # the Wii Remote over Windows HID, on Linux through the kernel's hid-wiimote nodes: a thread reads it, NativeInput's WiimoteTarget gets its buttons, pointer, stick and swings
target_include_directories(uc_wiimote_input PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_wiimote_input PUBLIC uc_wiimote_protocol uc_wiimote_math uc_wiimote_config uc_input)
if(WIN32)
  target_link_libraries(uc_wiimote_input PUBLIC setupapi hid)
  add_library(uc_raw_input_backend STATIC raw_input_backend.cpp)   # mice and light guns as devices of their own through Raw Input (local two-player play)
  target_include_directories(uc_raw_input_backend PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/common)
  target_link_libraries(uc_raw_input_backend PUBLIC SDL3::SDL3 user32)
else()
  target_sources(uc_wiimote_input PRIVATE evdev_scan.cpp)
  target_link_libraries(uc_wiimote_input PUBLIC uc_evdev_logic)
  add_library(uc_evdev_backend STATIC evdev_backend.cpp evdev_scan.cpp)   # mice and light guns as devices of their own (local two-player play)
  target_include_directories(uc_evdev_backend PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
  target_link_libraries(uc_evdev_backend PUBLIC uc_evdev_logic)
  add_executable(uc_evdev_probe evdev_probe.cpp)   # developer tool: what the evdev layer sees (scripts/linux/uinput_fakes.py makes virtual devices to look at)
  target_link_libraries(uc_evdev_probe PRIVATE uc_evdev_backend uc_wiimote_input)
endif()   # no runtime: the launcher reads the remote too
target_link_libraries(uc_coop_session PUBLIC uc_coop_roster uc_pointer_feed uc_gamepad_input uc_wiimote_input)
add_library(uc_game_mods STATIC game_mods.cpp)   # the port's side of the mod platform (modding/)
target_include_directories(uc_game_mods PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_game_mods PUBLIC uc_mods uc_diagnostics PRIVATE uc_public_release)
add_library(uc_game_input STATIC game_input.cpp test_state.cpp)
add_library(uc_netplay_game STATIC netplay_game.cpp netplay_saves.cpp game_checkpoint.cpp)
target_include_directories(uc_netplay_game PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_netplay_game PUBLIC uc_game_input uc_game_pacing uc_options uc_gx nlohmann_json::nlohmann_json)
target_include_directories(uc_game_input PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_game_input PUBLIC uc_input_probe uc_runtime uc_mods)
add_library(uc_test_server STATIC test_protocol.cpp $<IF:$<BOOL:${WIN32}>,test_pipe_win.cpp,test_pipe_posix.cpp> test_server.cpp test_draws.cpp)
target_include_directories(uc_test_server PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(uc_test_server PUBLIC ${CMAKE_SOURCE_DIR}/renderer)
target_link_libraries(uc_test_server PUBLIC uc_game_input uc_runtime)
add_library(uc_prompt_hooks STATIC prompt_hooks.cpp)
target_include_directories(uc_prompt_hooks PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uc_prompt_hooks PUBLIC uc_prompt_bridge uc_game_input)
target_link_libraries(uc_netplay_game PUBLIC uc_prompt_hooks)
if(EXISTS ${UC_GENERATED_DIR}/game_sources.cmake)
  include(${UC_GENERATED_DIR}/game_sources.cmake)
endif()
if(DEFINED UC_GENERATED_INCLUDE_DIR AND EXISTS ${UC_GENERATED_INCLUDE_DIR}/game_table.cpp)
  option(UC_TRACE "record guest function entries (slower; for bring-up)" OFF)
  add_library(uc_game STATIC ${UC_GENERATED_SOURCES})
  target_include_directories(uc_game PUBLIC ${UC_GENERATED_INCLUDE_DIR})
  target_link_libraries(uc_game PUBLIC uc_runtime uc_intro uc_game_pacing uc_game_input uc_prompt_hooks uc_mods)
  if(WIN32)
    target_compile_options(uc_game PRIVATE /O2)
  else()
    target_compile_options(uc_game PRIVATE -O2)
  endif()
  target_compile_options(uc_game PRIVATE -Wno-unused-label -Wno-unused-variable -Wno-unused-but-set-variable -Wno-parentheses-equality)
  set(UC_GAME_EXTRA_FLAGS "" CACHE STRING "extra compile flags for the translated game code only (code-generation experiments)")
  if(UC_GAME_EXTRA_FLAGS)
    separate_arguments(uc_game_extra_flags NATIVE_COMMAND "${UC_GAME_EXTRA_FLAGS}")
    target_compile_options(uc_game PRIVATE ${uc_game_extra_flags})
  endif()
  if(WIN32)
    target_compile_options(uc_game PRIVATE "/FI${CMAKE_CURRENT_SOURCE_DIR}/entry_probe.h")
  else()
    target_compile_options(uc_game PRIVATE -include "${CMAKE_CURRENT_SOURCE_DIR}/entry_probe.h")
  endif()
  option(UC_FN_COUNT "count the entries of every translated function (UC_FN_COUNT=<prefix>, see state_dump.h; for diagnostics)" OFF)
  if(UC_FN_COUNT)
    target_compile_definitions(uc_game PUBLIC UC_FN_COUNT=1)
  endif()
  if(UC_TRACE)
    target_compile_definitions(uc_game PUBLIC UC_TRACE=1)
    target_compile_definitions(uc_runtime PRIVATE UC_TRACE=1)
  endif()
  add_executable(uc_smoke smoke.cpp)
  target_link_libraries(uc_smoke PRIVATE uc_game uc_common test_support)
  install(TARGETS uc_smoke RUNTIME DESTINATION tools)
  add_executable(step_probe ${CMAKE_SOURCE_DIR}/tools/step_probe/step_probe.cpp)   # offline proof of tools/step_audit fix records
  target_link_libraries(step_probe PRIVATE uc_game uc_common test_support)
  add_test(NAME test_step_probe_selfcheck COMMAND step_probe --selfcheck)
  include(${CMAKE_SOURCE_DIR}/cmake/mimalloc.cmake)
  add_executable(uc allocator.cpp uc_main.cpp audio_output.cpp compile_screen.cpp det_trace.cpp frame_log.cpp settings_menu.cpp deck_menu.cpp steam_overlay.cpp $<IF:$<BOOL:${WIN32}>,system_report_win.cpp,system_report_posix.cpp> $<$<NOT:$<BOOL:${WIN32}>>:huge_text_posix.cpp>)
  target_link_libraries(uc PRIVATE mimalloc-static uc_public_release uc_path_io uc_ui_mode)
  target_link_libraries(uc PRIVATE uc_game uc_common uc_cpu_topology uc_platform uc_options_config uc_intro uc_vulkan uc_input_probe uc_host_input uc_gamepad_input uc_wiimote_input uc_coop_session $<IF:$<BOOL:${WIN32}>,uc_raw_input_backend,uc_evdev_backend> uc_test_server uc_crash_bundle
    uc_live_settings uc_settings_registry uc_ui_style uc_launcher_locale uc_binding_editor uc_gamepad_panel uc_game_mods uc_mod_menu uc_deck_pages uc_netplay_game)
  include(${CMAKE_SOURCE_DIR}/cmake/stb_image.cmake)   # the header the compile screen's picture is decoded with (the launcher's folder fetches it too)
  if(WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set_source_files_properties(${CMAKE_SOURCE_DIR}/launcher/graphite_posix.cpp compile_screen.cpp ${CMAKE_SOURCE_DIR}/tools/compile_screen_snapshot/main.cpp PROPERTIES COMPILE_OPTIONS /FIintrin.h)   # SDL3-3.2.0's prefetch workaround needs the intrinsics header first
  endif()
  target_sources(uc PRIVATE ${CMAKE_SOURCE_DIR}/launcher/graphite_posix.cpp)   # load_banner: the compile screen's background picture (stb_image decodes the PNG; the file also holds the Linux file dialogs, unused here)
  target_include_directories(uc PRIVATE ${stb_image_SOURCE_DIR})
  # Draws the compile screen in its states and keeps each as a BMP (UC_COMPILE_SCREEN_PNG): `uc_compile_screen_snapshot <language> <folder>`; built on request only.
  add_executable(uc_compile_screen_snapshot EXCLUDE_FROM_ALL ${CMAKE_SOURCE_DIR}/tools/compile_screen_snapshot/main.cpp compile_screen.cpp ${CMAKE_SOURCE_DIR}/launcher/graphite_posix.cpp)
  target_include_directories(uc_compile_screen_snapshot PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${stb_image_SOURCE_DIR})
  target_link_libraries(uc_compile_screen_snapshot PRIVATE uc_path_io uc_ui_style uc_launcher_locale uc_imgui SDL3::SDL3)
  if(NOT WIN32)
    target_sources(uc PRIVATE ${CMAKE_SOURCE_DIR}/launcher/launch_posix.cpp)   # query_features (the GPU probe), without the Windows launcher
    target_link_libraries(uc PRIVATE ${CMAKE_DL_LIBS})   # steam_overlay.cpp loads libX11 at run time
    if(TARGET uc_profile_rt)
      target_link_options(uc PRIVATE -Wl,-u,__llvm_profile_runtime)   # what clang's driver adds for -fprofile-instr-generate: pulls the runtime out of its archive
      target_link_libraries(uc PRIVATE uc_profile_rt)   # the instrumentation's runtime (UC_PGO_GENERATE, runtime/linux_profile_rt.c); last, after the objects that need it
    endif()
  endif()
  if(WIN32)
    target_sources(uc PRIVATE ${CMAKE_SOURCE_DIR}/assets/app.rc)
    target_link_libraries(uc PRIVATE uc_client_update_lock dxgi uc_directx winmm shell32 uc_launch)
  endif()
  add_custom_command(TARGET uc POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:uc>/data/shaders
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${UC_SHADER_FILES} $<TARGET_FILE_DIR:uc>/data/shaders
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/project/pipeline_keys.seed $<TARGET_FILE_DIR:uc>/data
    COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:SDL3-shared> $<TARGET_FILE_DIR:uc> VERBATIM)
  if(WIN32)
    add_custom_command(TARGET uc POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${UC_DIRECTX_SHADER_FILES} $<TARGET_FILE_DIR:uc>/data/shaders
      COMMAND ${CMAKE_COMMAND} -E copy_if_different ${UC_UPSCALER_RUNTIME} ${UC_DLSS5_RUNTIME_FILES} $<TARGET_FILE_DIR:uc> VERBATIM)
    # Linker map lets crash reports (exe offsets) be resolved to runtime function names offline.
    target_link_options(uc PRIVATE "/MAP:$<TARGET_FILE_DIR:uc>/uc.map")
    install(FILES $<TARGET_FILE_DIR:uc>/uc.map DESTINATION data)
  endif()
  install(TARGETS uc RUNTIME DESTINATION .)
  if(WIN32)   # the Deck uses its own SDL3 (3.4, with Wayland): the one built here has no Wayland backend
    install(FILES $<TARGET_FILE:SDL3-shared> DESTINATION .)
  endif()
  install(FILES ${UC_SHADER_FILES} DESTINATION data/shaders)
  install(FILES ${UC_UPSCALER_RUNTIME} DESTINATION .)
  # nvngx_dlssnr.dll is not installed into dist: players supply their own, and a deploy must never overwrite it.
  install(FILES ${CMAKE_SOURCE_DIR}/third_party/smaa/LICENSE.txt DESTINATION licenses/SMAA)
  install(FILES ${mimalloc_SOURCE_DIR}/LICENSE DESTINATION licenses/mimalloc)
  install(FILES ${CMAKE_SOURCE_DIR}/THIRD_PARTY_NOTICES.md DESTINATION .)   # credits and trademark line; the licence texts go to licenses/ (renderer, cmake/upscaler_sdks.cmake)
  install(FILES ${CMAKE_SOURCE_DIR}/project/identified_sdk.txt ${CMAKE_SOURCE_DIR}/project/symbols.txt ${CMAKE_SOURCE_DIR}/project/gx_symbols.txt ${CMAKE_SOURCE_DIR}/project/pipeline_keys.seed DESTINATION data)
else()
  message(STATUS "active generation manifest not found: skipping uc_game (build.bat runs the translator first)")
endif()
