cmake_minimum_required(VERSION 3.20)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# No C++20 modules here; skip CMake's dependency scanning of every source.
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

project(DAORecomp-ALL C CXX)

if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    message(FATAL_ERROR "The recompiled code needs Clang (clang-cl on Windows).")
endif()

set(DAO_TOOLS_ROOT ${CMAKE_SOURCE_DIR}/tools)
set(XENON_ROOT ${DAO_TOOLS_ROOT}/XenonRecomp)
# Xenia's third-party libraries (xenia-canary checkout with its submodules).
set(DAO_XENIA_THIRD_PARTY ${DAO_TOOLS_ROOT}/xenia-canary/third_party)
# The recompiled game code (generated by XenonRecomp, never committed).
set(DAO_PPC_DIR ${CMAKE_SOURCE_DIR}/ppc CACHE PATH "XenonRecomp output directory")
# Title Update 6's recompiled code (optional: built in when ppc_tu6/ was generated;
# needed by Awakening and the DLC campaigns).
set(DAO_PPC_TU6_DIR ${CMAKE_SOURCE_DIR}/ppc_tu6 CACHE PATH "XenonRecomp output directory for Title Update 6")

# The processor the build is for. The installer picks x86-64-v3 or sandybridge for the
# game from the PC's own (installer/pipeline.py target_cpu); this default (any AVX
# processor, UnleashedRecomp's baseline) is for direct builds and the downloaded
# launcher. The exe checks for it when it starts (DAORecomp/cpu_check.cpp).
set(DAO_MARCH "sandybridge" CACHE STRING "Target CPU (-march): sandybridge or x86-64-v3")
add_compile_options(-march=${DAO_MARCH})
# No fused multiply-adds where the code has none (the same float results on
# every target).
add_compile_options(/clang:-ffp-contract=off)

# Xenia's GPU/UI/base (vendored, patched). Comes first so its fmt and xxhash
# targets are the ones XenonUtils picks up.
add_subdirectory(xenia)

# SDL3 (static): gamepads incl. DualSense, haptics, audio output, and the
# launcher window (2D renderer, Direct3D 11/12 backends 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_TESTS OFF CACHE BOOL "" FORCE)
set(SDL_EXAMPLES OFF CACHE BOOL "" FORCE)
set(SDL_INSTALL OFF CACHE BOOL "" FORCE)
set(SDL_CAMERA OFF CACHE BOOL "" FORCE)
set(SDL_GPU OFF CACHE BOOL "" FORCE)
set(SDL_RENDER ON CACHE BOOL "" FORCE)
set(SDL_RENDER_VULKAN OFF CACHE BOOL "" FORCE)
set(SDL_OPENGL OFF CACHE BOOL "" FORCE)
set(SDL_OPENGLES OFF CACHE BOOL "" FORCE)
add_subdirectory(thirdparty/SDL EXCLUDE_FROM_ALL)

# XenonUtils (XEX structures, byteswap helpers).
add_subdirectory(${XENON_ROOT} ${CMAKE_BINARY_DIR}/XenonRecomp EXCLUDE_FROM_ALL)

# What players download: DAORecomp.exe with the launcher and installer only. It has no
# game code (no ppc/): installing recompiles the player's game into the full one.
option(DAO_LAUNCHER_ONLY "Build the launcher and installer without the game" OFF)
if (DAO_LAUNCHER_ONLY)
    add_subdirectory(DAORecomp)
    return()
endif()

add_subdirectory(DAORecompLib)
add_subdirectory(DAORecomp)
