cmake_minimum_required(VERSION 3.20)
project("CoD3RecompLib" CXX)

# The recompiled PowerPC code. Generated by XenonRecomp from default.xex into
# ppc/ -- see config/CoD3.toml and ../scripts/recompile.ps1. Nothing in ppc/ is
# hand written, so never edit it: regenerate instead.

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    message(FATAL_ERROR
        "The recompiled code requires Clang. It relies on Clang-specific "
        "intrinsics, the alias attribute for function hooking, and Clang's "
        "code generation for the register-as-local optimizations. "
        "Current compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()

file(GLOB PPC_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/ppc/*.cpp")
list(LENGTH PPC_SOURCES PPC_SOURCE_COUNT)
if (PPC_SOURCE_COUNT EQUAL 0)
    message(FATAL_ERROR
        "No recompiled sources in ppc/. Run scripts/recompile.ps1 first.")
endif()
message(STATUS "CoD3RecompLib: ${PPC_SOURCE_COUNT} recompiled translation units")

add_library(CoD3RecompLib STATIC ${PPC_SOURCES})

target_include_directories(CoD3RecompLib PUBLIC
    "${CMAKE_CURRENT_SOURCE_DIR}/include"
    "${CMAKE_CURRENT_SOURCE_DIR}/ppc"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tools/XenonRecomp/thirdparty/simde")

# The translated code reinterprets guest memory constantly and reorders nothing
# on its own; strict aliasing assumptions would be unsound here.
target_compile_options(CoD3RecompLib PRIVATE
    -fno-strict-aliasing
    -Wno-unused-variable
    -Wno-unused-but-set-variable)

# Memory mapped I/O. The recompiler translates the graphics driver's register
# accesses as plain memory operations, which would land in guest RAM and leave
# the GPU deaf. Force including this ahead of ppc_context.h replaces the memory
# macros with ones that route the register aperture to the GPU instead. It has
# to be a forced include because the macros are consumed by generated code that
# must not be edited.
target_compile_options(CoD3RecompLib PRIVATE -FIcod3_mmio.h)

# The translated vector code leans on SSE4.1 (simde_mm_round_ps,
# packus_epi32, min_epu16, blendv_ps, insert_ps). Without a baseline that
# includes it, simde emulates each one in scalar C, which is both slow and
# drags in libm symbols the MSVC runtime does not have, such as roundevenf.
# x86-64-v2 is SSE4.2 and POPCNT: Intel since 2008, AMD since 2011.
target_compile_options(CoD3RecompLib PUBLIC -march=x86-64-v2)

# Every guest instruction the recompiler could not translate becomes a
# PPC_UNIMPLEMENTED_INSN. The default in ppc_context.h traps. A runtime can
# define PPC_UNIMPLEMENTED_INSN itself to log and continue while the remaining
# instructions are being implemented in XenonRecomp.
if (COD3_TRAP_ON_UNIMPLEMENTED)
    target_compile_definitions(CoD3RecompLib PUBLIC COD3_TRAP_ON_UNIMPLEMENTED)
endif()

# The levels' code, one library each. COD3_LEVEL_LIBRARIES names the ones
# that were recompiled; the host links whatever is there.
add_subdirectory(levels)
set(COD3_LEVEL_LIBRARIES ${COD3_LEVEL_LIBRARIES} PARENT_SCOPE)
