cmake_minimum_required(VERSION 3.16)
project(gamekdm LANGUAGES C CXX)

option(BUILD_PURE "Build without linking or loading the original binary" OFF)
option(PURE_SLICE "Build the verified T11 slice with explicit remainder stubs" OFF)
set(PURE_REQUIRE_RVA "" CACHE STRING
    "Additional semicolon-separated RVAs that the PURE coverage gate must verify")

# 32-bit Windows DLL, built with the pinned i686-w64-mingw32 cross toolchain
# (see cmake/toolchain-mingw-i686.cmake, toolchain.lock). The real shipping
# gamekdm.dll's reconstructed source lands in src/* over subsequent tasks;
# today's target is a minimal stub so the layered tree + build + export
# table shape is proven before any real logic is reconstructed.

if(BUILD_PURE)
    find_package(Python3 REQUIRED COMPONENTS Interpreter)
    set(_pure_gate_args
        "${CMAKE_SOURCE_DIR}/cmake/check_pure_coverage.py"
        "${CMAKE_SOURCE_DIR}/re/rvadb.json"
    )
    if(PURE_SLICE)
        list(APPEND _pure_gate_args
            --mode slice
            --slice-status "${CMAKE_SOURCE_DIR}/re/pure-slice-status.json"
        )
    else()
        list(APPEND _pure_gate_args --mode full)
    endif()
    foreach(_pure_rva IN LISTS PURE_REQUIRE_RVA)
        list(APPEND _pure_gate_args --require-rva "${_pure_rva}")
    endforeach()
    execute_process(
        COMMAND "${Python3_EXECUTABLE}" ${_pure_gate_args}
        RESULT_VARIABLE _pure_gate_result
    )
    if(NOT _pure_gate_result EQUAL 0)
        message(FATAL_ERROR "PURE coverage/reachability gate failed")
    endif()
    add_custom_target(pure_coverage_gate ALL
        COMMAND "${Python3_EXECUTABLE}" ${_pure_gate_args}
        VERBATIM
    )

    if(NOT PURE_SLICE)
        message(FATAL_ERROR
            "PURE coverage passed, but the complete reconstructed source graph "
            "and entry point are not integrated yet")
    endif()

    add_library(gamekdm_pure_slice SHARED
        src/pure/slice.cpp
        src/game/box_distance.cpp
        src/game/validity.cpp
        src/game/nui_elevation.cpp
    )
    add_dependencies(gamekdm_pure_slice pure_coverage_gate)
    target_include_directories(gamekdm_pure_slice PRIVATE
        ${CMAKE_SOURCE_DIR}/include
        ${CMAKE_SOURCE_DIR}/src
    )
    target_compile_features(gamekdm_pure_slice PRIVATE cxx_std_17)
    target_compile_options(gamekdm_pure_slice PRIVATE
        -Wall -Wextra -Werror -mfpmath=387 -ffp-contract=off -fno-fast-math
    )
    set_target_properties(gamekdm_pure_slice PROPERTIES
        OUTPUT_NAME "gamekdm_pure_slice"
        PREFIX ""
        SUFFIX ".dll"
    )

    add_executable(gamekdm_pure_host src/pure/mock_host.cpp)
    add_dependencies(gamekdm_pure_host gamekdm_pure_slice)
    target_compile_options(gamekdm_pure_host PRIVATE -Wall -Wextra -Werror)
    return()
endif()

add_library(gamekdm SHARED src/entry/gamemain.cpp)

target_include_directories(gamekdm PRIVATE
    ${CMAKE_SOURCE_DIR}/include
)

set_target_properties(gamekdm PROPERTIES
    OUTPUT_NAME "gamekdm"
    PREFIX ""
    SUFFIX ".dll"
)

# Link with the hand-authored .def so ONLY gameMain is exported, at ordinal 1,
# matching the original DLL's export table (no CRT/C++ name-mangled exports).
target_link_options(gamekdm PRIVATE
    "${CMAKE_SOURCE_DIR}/export/gamekdm.def"
)
set_target_properties(gamekdm PROPERTIES
    LINK_DEPENDS "${CMAKE_SOURCE_DIR}/export/gamekdm.def"
)

# T12: explicit startup installer, linked by a host before invoking gameMain.
# Only the audited POD slice crosses into the separately built MinGW CRT.
add_library(gamekdm_detours STATIC
    src/hybrid/detours.cpp
    src/game/box_distance.cpp
)
target_include_directories(gamekdm_detours PUBLIC
    ${CMAKE_SOURCE_DIR}/include
    ${CMAKE_SOURCE_DIR}/src/hybrid
)
target_compile_features(gamekdm_detours PUBLIC cxx_std_17)
target_compile_options(gamekdm_detours PRIVATE
    -Wall -Wextra -Werror -mfpmath=387 -ffp-contract=off -fno-fast-math
    -fno-exceptions -fno-rtti
)
add_executable(gamekdm_detour_test src/hybrid/test_host.cpp)
target_link_libraries(gamekdm_detour_test PRIVATE gamekdm_detours)
target_compile_options(gamekdm_detour_test PRIVATE
    -Wall -Wextra -Werror -mfpmath=387 -ffp-contract=off -fno-fast-math
)
