# The recompiled game executable.
#
# Inputs:
#   - ps2recomp runtime + IOP (external/ps2recomp, with our host hooks)
#   - C++ generated by ps2_recomp from the USER'S OWN ELF (GOW2_GENERATED_DIR)
#   - the port layer in port/src (input, presentation, settings)

if(NOT EXISTS "${GOW2_GENERATED_DIR}/register_functions.cpp")
    message(FATAL_ERROR
        "No recompiled code in GOW2_GENERATED_DIR='${GOW2_GENERATED_DIR}'.\n"
        "Run gow2-setup with your ISO, then ps2_recomp (scripts/build.ps1 does both).")
endif()

include(FetchContent)

# --- ps2recomp runtime -------------------------------------------------------
# Quiet, fast runtime defaults for players; flip these when debugging.
set(PS2X_ENABLE_AGRESSIVE_LOGS OFF CACHE BOOL "" FORCE)
set(PS2X_ENABLE_IOP_RPC_TRACE OFF CACHE BOOL "" FORCE)
set(PS2X_ENABLE_DEBUG_UI OFF CACHE BOOL "")
set(PS2X_ENABLE_SCCACHE ON CACHE BOOL "")

set(GOW2_PS2RECOMP_DIR "${CMAKE_SOURCE_DIR}/external/ps2recomp")
# EXCLUDE_FROM_ALL: only build the libraries we link, not the sample runner.
add_subdirectory("${GOW2_PS2RECOMP_DIR}/ps2xIOP" "${CMAKE_BINARY_DIR}/ps2xIOP" EXCLUDE_FROM_ALL)
add_subdirectory("${GOW2_PS2RECOMP_DIR}/ps2xRuntime" "${CMAKE_BINARY_DIR}/ps2xRuntime" EXCLUDE_FROM_ALL)

# --- SDL3 (gamepads only) ----------------------------------------------------
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
set(SDL_EXAMPLES OFF CACHE BOOL "" FORCE)
FetchContent_Declare(SDL3
    GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
    GIT_TAG release-3.4.20
    GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(SDL3)

# --- GPU renderer (optional) -------------------------------------------------
# A separate library, so the recompiled sources never see the Vulkan headers.
option(GOW2_PARALLEL_GS "Build the GPU renderer (paraLLEl-GS, LGPL-3.0+; see tools/pgs_probe/README.md)" OFF)
set(GOW2_PGS_DIR "${CMAKE_SOURCE_DIR}/build/third_party/parallel-gs" CACHE PATH "paraLLEl-GS checkout")
add_library(gow2_gpu STATIC src/gs_gpu.cpp)
target_include_directories(gow2_gpu PUBLIC src)
target_link_libraries(gow2_gpu PUBLIC ps2_runtime)
if(GOW2_PARALLEL_GS)
    if(NOT EXISTS "${GOW2_PGS_DIR}/gs/gs_interface.hpp")
        message(FATAL_ERROR "GOW2_PARALLEL_GS: paraLLEl-GS not found in ${GOW2_PGS_DIR} (see tools/pgs_probe/README.md)")
    endif()
    # paraLLEl-GS picks the static C runtime unless told otherwise; the game uses the DLL runtime.
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
    set(PARALLEL_GS_STANDALONE ON CACHE BOOL "" FORCE)
    add_subdirectory("${GOW2_PGS_DIR}" "${CMAKE_BINARY_DIR}/parallel-gs" EXCLUDE_FROM_ALL)
    target_link_libraries(gow2_gpu PRIVATE parallel-gs)
    target_compile_definitions(gow2_gpu PRIVATE GOW2_HAS_PARALLEL_GS=1)
endif()

# --- Game executable ---------------------------------------------------------
file(GLOB GOW2_GENERATED_SOURCES CONFIGURE_DEPENDS "${GOW2_GENERATED_DIR}/*.cpp")
list(LENGTH GOW2_GENERATED_SOURCES GOW2_GENERATED_COUNT)
message(STATUS "GoW2: ${GOW2_GENERATED_COUNT} recompiled source files")

add_executable(GoW2Recomp
    src/main.cpp
    src/input_sdl.cpp
    src/keyboard_names.cpp
    src/platform.cpp
    src/video.cpp
    src/gow2_overrides.cpp
    src/profiler.cpp
    src/hwwatch.cpp
    src/overlay.cpp
    src/perf_stats.cpp
    src/frame_gen.cpp
    ${GOW2_GENERATED_SOURCES}
)

target_include_directories(GoW2Recomp PRIVATE "${GOW2_GENERATED_DIR}" src)
target_link_libraries(GoW2Recomp PRIVATE gow2_gpu ps2_runtime gow2_common SDL3::SDL3-static)
if(WIN32)
    target_link_libraries(GoW2Recomp PRIVATE dbghelp winmm dxgi psapi)
endif()

# 6500+ small generated files: batch them, and precompile the runtime headers.
set_source_files_properties(src/main.cpp src/input_sdl.cpp src/keyboard_names.cpp src/platform.cpp src/video.cpp src/gow2_overrides.cpp src/profiler.cpp src/hwwatch.cpp src/overlay.cpp src/perf_stats.cpp src/frame_gen.cpp
    PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON SKIP_PRECOMPILE_HEADERS ON)
set_target_properties(GoW2Recomp PROPERTIES
    UNITY_BUILD ON
    UNITY_BUILD_BATCH_SIZE 48
    OUTPUT_NAME "GoW2Recomp")
target_precompile_headers(GoW2Recomp PRIVATE
    "${GOW2_PS2RECOMP_DIR}/ps2xRuntime/include/ps2_runtime_macros.h"
    "${GOW2_PS2RECOMP_DIR}/ps2xRuntime/include/ps2_runtime.h"
    "${GOW2_GENERATED_DIR}/ps2_recompiled_functions.h"
    "${GOW2_GENERATED_DIR}/ps2_recompiled_stubs.h"
    "${GOW2_PS2RECOMP_DIR}/ps2xRuntime/include/ps2_syscalls.h"
    "${GOW2_PS2RECOMP_DIR}/ps2xRuntime/include/ps2_stubs.h")

if(MSVC)
    target_compile_options(GoW2Recomp PRIVATE /FS /bigobj)
    # Same raylib/WinAPI CloseWindow clash the upstream runner works around.
    target_link_options(GoW2Recomp PRIVATE "/FORCE:MULTIPLE")
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
    EnableFastReleaseMode(GoW2Recomp)
endif()

ps2x_stage_ffmpeg_runtime_dlls(GoW2Recomp)
