# FNC Reborn - builds FNCReborn.exe from the ReXGlue SDK, the code generated from
# your own copy of the game (see scripts/setup.ps1) and the sources in src/.
cmake_minimum_required(VERSION 3.25)
project(fnc LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(DEFINED ENV{REXSDK})
    list(PREPEND CMAKE_PREFIX_PATH $ENV{REXSDK})
endif()
find_package(rexglue 0.10.0 REQUIRED CONFIG)

set(FNC_GENERATED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../build/generated" CACHE PATH
    "Directory containing the code generated by 'rexglue codegen'")
if(NOT EXISTS "${FNC_GENERATED_DIR}/sources.cmake")
    message(FATAL_ERROR "Generated code not found in ${FNC_GENERATED_DIR}. Run setup.bat first.")
endif()

# ---------------------------------------------------------------------------
# Recompiled game code (an object library with a precompiled header)
# ---------------------------------------------------------------------------
include(${FNC_GENERATED_DIR}/sources.cmake)

set(FNC_RECOMP_OPTIONS "")
if(WIN32)
    # Guest SEH scopes emit __try/__except, which traps hardware faults only
    # under asynchronous exception handling.
    list(APPEND FNC_RECOMP_OPTIONS -fasync-exceptions)
endif()
list(APPEND FNC_RECOMP_OPTIONS -gline-tables-only)
set_source_files_properties(${GENERATED_SOURCES} PROPERTIES COMPILE_OPTIONS "${FNC_RECOMP_OPTIONS}")

add_library(fnc_recomp OBJECT ${GENERATED_SOURCES})
target_include_directories(fnc_recomp PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${FNC_GENERATED_DIR})
target_link_libraries(fnc_recomp PRIVATE rex::runtime)
rexglue_apply_target_settings(fnc_recomp)
# The options go on the target as well: CMake compiles the precompiled header
# from its own source, which carries none of the source properties above.
target_compile_options(fnc_recomp PRIVATE ${FNC_RECOMP_OPTIONS})

# ---------------------------------------------------------------------------
# FNCReborn.exe
# ---------------------------------------------------------------------------
add_executable(fnc WIN32
    src/main.cpp
    src/fnc_hooks.cpp
    src/fnc_sampler.cpp
    src/fnc_fps_overlay.cpp
    src/fnc_settings_menu.cpp
    src/player_camera.cpp
)
if(WIN32)
    target_sources(fnc PRIVATE res/FNCReborn.rc)
endif()
# The target keeps the recompiler's project name; the program is FNCReborn.exe.
set_target_properties(fnc PROPERTIES OUTPUT_NAME FNCReborn)
target_include_directories(fnc PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${FNC_GENERATED_DIR})
target_link_libraries(fnc PRIVATE fnc_recomp rex::runtime)
# Adds the SDK's app entry point and copies the SDK's DLLs next to the exe.
rexglue_configure_target(fnc)

# ---------------------------------------------------------------------------
# CPU level of the recompiled code
# ---------------------------------------------------------------------------
# x86-64-v3 = AVX2 + FMA3 + BMI + MOVBE (Haswell / Zen and newer). Without FMA3,
# each of the game's ~4,000 fused multiply-adds is a call to a software fma.
# auto (default) = what the CPU doing the build supports: players build the game
# on their own PC, so an older CPU gets the v2 build.
# -ffp-contract=off keeps the game's separate multiplies and adds unfused, so
# results stay bit-exact.
set(FNC_CPU_LEVEL "auto" CACHE STRING "Recompiled code CPU level: auto, v3, v2 or sse41")
set(FNC_CPU_LEVEL_USED "${FNC_CPU_LEVEL}")
if(FNC_CPU_LEVEL STREQUAL "auto")
    try_run(FNC_CPU_PROBE_RUN FNC_CPU_PROBE_COMPILED
        ${CMAKE_BINARY_DIR}/cpu_level_probe
        ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cpu_level_probe.c
        COMPILE_DEFINITIONS -mxsave
        RUN_OUTPUT_VARIABLE FNC_CPU_PROBE_OUT)
    if(FNC_CPU_PROBE_COMPILED AND FNC_CPU_PROBE_OUT STREQUAL "v2")
        set(FNC_CPU_LEVEL_USED "v2")
    else()
        set(FNC_CPU_LEVEL_USED "v3")
    endif()
    message(STATUS "Recompiled code CPU level (auto): ${FNC_CPU_LEVEL_USED}")
endif()
foreach(_t fnc fnc_recomp)
    if(FNC_CPU_LEVEL_USED STREQUAL "v3")
        target_compile_options(${_t} PRIVATE -march=x86-64-v3 -ffp-contract=off)
    elseif(FNC_CPU_LEVEL_USED STREQUAL "v2")
        target_compile_options(${_t} PRIVATE -march=x86-64-v2 -ffp-contract=off)
    else()
        # Very old CPUs (SSE4.1, before ~2009).
        target_compile_options(${_t} PRIVATE -msse4.1 -ffp-contract=off)
    endif()
endforeach()
if(NOT FNC_CPU_LEVEL_USED STREQUAL "v3")
    # No FMA3: replace the runtime's slow software std::fma in the recompiled
    # code with an exact fast path (first in the precompiled header).
    target_precompile_headers(fnc_recomp PRIVATE
        "${CMAKE_CURRENT_SOURCE_DIR}/src/fast_fma.h" "${FNC_GENERATED_DIR}/fnc_pch.h")
else()
    target_precompile_headers(fnc_recomp PRIVATE "${FNC_GENERATED_DIR}/fnc_pch.h")
endif()
