cmake_minimum_required(VERSION 3.25)
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt" TEKKEN6_VERSION LIMIT_COUNT 1)
project(tekken6 VERSION ${TEKKEN6_VERSION} LANGUAGES CXX)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt")

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
option(TEKKEN6_ENABLE_FMA3
    "Compile generated guest code for CPUs with FMA3" ON)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(REXSDK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/rexglue-sdk"
    CACHE PATH "Pinned RexGlue SDK source tree")

# This file is generated by the pinned SDK and owns the codegen dependency,
# generated source list, PCH settings, runtime linkage, and plugin staging.
option(TEKKEN6_USE_PREGENERATED_CODE
    "Build existing guest sources without invoking target-platform codegen" OFF)
set(REXGLUE_USE_PREGENERATED_CODE ${TEKKEN6_USE_PREGENERATED_CODE})
include(generated/rexglue.cmake)

# Small ISO inspection/extraction utility; game data remains local to the user.
add_subdirectory(tools/xdvdfs)

if(MINGW AND TARGET rexgpu-xenos)
    # Use the unprefixed module filename expected by the Windows plugin loader.
    set_target_properties(rexgpu-xenos PROPERTIES PREFIX "")
endif()

if(MINGW AND TARGET rexruntime)
    # MinGW auto-export skips __imp_* names, but these are actual guest ABI
    # functions, not Windows import-address-table pointers. Export the title's
    # required functions explicitly while retaining normal C++ auto-exports.
    set(tekken6_import_header "${CMAKE_CURRENT_SOURCE_DIR}/generated/default/tekken6_funcs.h")
    if(EXISTS "${tekken6_import_header}")
        set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${tekken6_import_header}")
        file(STRINGS "${tekken6_import_header}" tekken6_imports
            REGEX "^DECLARE_REX_FUNC\\(__imp_[A-Za-z0-9_]+\\);")
        set(tekken6_exports "EXPORTS\n")
        foreach(declaration IN LISTS tekken6_imports)
            string(REGEX REPLACE "^DECLARE_REX_FUNC\\(([^)]+)\\).*" "\\1" symbol "${declaration}")
            string(APPEND tekken6_exports "    ${symbol}\n")
        endforeach()
        file(CONFIGURE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/tekken6-runtime-exports.def"
            CONTENT "${tekken6_exports}" @ONLY)
        target_sources(rexruntime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/tekken6-runtime-exports.def")
    endif()
    target_link_options(rexruntime PRIVATE -Wl,--export-all-symbols)
endif()

add_executable(tekken6 src/main.cpp src/tekken6_guest_hooks.cpp src/internal_resolution.cpp)
target_compile_options(tekken6 PRIVATE -mssse3)
if(WIN32)
    target_sources(tekken6 PRIVATE src/tekken6_crash_win.cpp)
    target_link_libraries(tekken6 PRIVATE dbghelp)
    # The SDK's SDL entry point is wWinMain; select the Unicode MinGW CRT
    # startup rather than expecting the ANSI WinMain symbol.
    target_link_options(tekken6 PRIVATE -municode)
endif()
rexglue_setup_target(tekken6 GPU_PLUGINS xenos)
# Stage the dynamically loaded GPU plugin whenever the host target is built.
add_custom_target(tekken6_stage_gpu_plugin
    COMMAND "${CMAKE_COMMAND}" -E copy_if_different
        "$<TARGET_FILE:rexgpu-xenos>"
        "${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_NAME:rexgpu-xenos>"
    DEPENDS rexgpu-xenos
    VERBATIM
)
add_dependencies(tekken6 tekken6_stage_gpu_plugin)
# Generated PPC code makes frequent explicit fused-multiply-add calls. FMA3
# lowers these to hardware instructions while keeping implicit contraction off.
if(TEKKEN6_ENABLE_FMA3)
    include(CheckCXXCompilerFlag)
    check_cxx_compiler_flag("-mfma" TEKKEN6_COMPILER_HAS_MFMA)
    if(NOT TEKKEN6_COMPILER_HAS_MFMA)
        message(FATAL_ERROR "TEKKEN6_ENABLE_FMA3 requires compiler support for -mfma")
    endif()
    target_compile_options(tekken6_recomp PRIVATE -mfma -ffp-contract=off)
endif()
# The app sources include UI headers (including imgui.h) and instantiate UI
# classes, so carry the UI target's public include path and object code here.
target_link_libraries(tekken6 PRIVATE rex::ui)

if(WIN32)
    configure_file(tools/portable_builder.rc.in
        "${CMAKE_CURRENT_BINARY_DIR}/portable_builder.rc" @ONLY)
    add_executable(tekken6_builder WIN32 tools/portable_builder.cpp
        "${CMAKE_CURRENT_BINARY_DIR}/portable_builder.rc")
    target_compile_definitions(tekken6_builder PRIVATE
        TEKKEN6_VERSION="${PROJECT_VERSION}")
    target_link_libraries(tekken6_builder PRIVATE comctl32 comdlg32 shell32 ole32)
    if(MINGW)
        target_link_options(tekken6_builder PRIVATE -municode)
    endif()

    function(tekken6_add_windows_launcher target_name)
        add_executable(${target_name} WIN32
            tools/portable_launcher.cpp
            tools/tekken6_launcher.rc
        )
        target_include_directories(${target_name} PRIVATE
            "${CMAKE_CURRENT_SOURCE_DIR}/tools")
        target_link_libraries(${target_name} PRIVATE user32 comctl32)
        if(MINGW)
            target_link_options(${target_name} PRIVATE -municode)
        endif()
    endfunction()

    # Standard Windows launcher with persistent caches.
    tekken6_add_windows_launcher(tekken6_low_overhead)
    target_compile_definitions(tekken6_low_overhead PRIVATE TEKKEN6_LOW_OVERHEAD=1)
    # Optional D3D12 ROV rendering paths.
    tekken6_add_windows_launcher(tekken6_late_depth)
    target_compile_definitions(tekken6_late_depth PRIVATE
        TEKKEN6_LOW_OVERHEAD=1 TEKKEN6_LATE_DEPTH=1)
    tekken6_add_windows_launcher(tekken6_fused_resolve)
    target_compile_definitions(tekken6_fused_resolve PRIVATE
        TEKKEN6_LOW_OVERHEAD=1 TEKKEN6_FUSED_RESOLVE=1)
endif()

if(WIN32 AND REXGLUE_USE_VULKAN)
    tekken6_add_windows_launcher(tekken6_vulkan)
    target_compile_definitions(tekken6_vulkan PRIVATE TEKKEN6_LOW_OVERHEAD=1 TEKKEN6_VULKAN=1)
    tekken6_add_windows_launcher(tekken6_vulkan_fbo)
    target_compile_definitions(tekken6_vulkan_fbo PRIVATE
        TEKKEN6_LOW_OVERHEAD=1 TEKKEN6_VULKAN=1 TEKKEN6_VULKAN_FBO=1)
endif()
