cmake_minimum_required(VERSION 3.20)
project(SMRecomp C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

if (NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    message(FATAL_ERROR "The recompiled code requires clang (clang-cl).")
endif()

set(SMR_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
set(SMR_PPC_DIR ${SMR_ROOT}/ppc)
set(SMR_SIMDE ${SMR_ROOT}/tools/XenonRecomp/thirdparty/simde)

# XenonUtils: XEX decryption/decompression and the kernel export name tables.
add_subdirectory(${SMR_ROOT}/tools/XenonRecomp ${CMAKE_BINARY_DIR}/xenonrecomp EXCLUDE_FROM_ALL)

# Xenia's Direct3D 12 GPU backend (see xenia/CMakeLists.txt).
add_subdirectory(xenia)

# ---------------------------------------------------------------- guest code
# The recompiled game (generated by XenonRecomp into ../ppc). Nothing here is
# written by hand; regenerate it instead of editing it.
file(GLOB SMR_PPC_SOURCES CONFIGURE_DEPENDS ${SMR_PPC_DIR}/ppc_recomp.*.cpp ${SMR_PPC_DIR}/ppc_func_mapping.cpp)
add_library(SMRGuest STATIC ${SMR_PPC_SOURCES})
target_include_directories(SMRGuest PUBLIC ${SMR_PPC_DIR} ${SMR_SIMDE})
target_compile_options(SMRGuest PRIVATE
    -march=sandybridge          # AVX: matches what the generated SIMD code is tuned for
    /bigobj
    /EHsc                       # guest threads exit by throwing through guest frames
    -Wno-everything)
# Guest code is huge; optimise it even in Debug so the game is playable.
target_compile_options(SMRGuest PRIVATE $<$<CONFIG:Debug>:/O1>)

# ---------------------------------------------------------------- runtime
add_executable(ShootManyRobots
    src/main.cpp
    src/memory.cpp
    src/loader.cpp
    src/kernel/objects.cpp
    src/kernel/threads.cpp
    src/kernel/memory_imports.cpp
    src/kernel/files.cpp
    src/kernel/rtl.cpp
    src/kernel/format.cpp
    src/kernel/system.cpp
    src/kernel/xam.cpp
    src/kernel/stubs_generated.cpp
    src/gpu/xenia_bridge.cpp
    src/audio/audio.cpp
    src/audio/xma.cpp
    src/audio/xma_backend.cpp
    src/settings.cpp
    src/input/input.cpp
    src/patches/frame_rate.cpp
    src/patches/skip_intro.cpp
    src/debug/profiler.cpp
    src/ui/overlay.cpp
)
# Icon + version info. Compiled on its own so rc.exe gets a short command line without the
# runtime's ~20 include paths and defines (with those, some setups fail with RC1107
# "invalid usage", e.g. when building from a folder with spaces), and without CMake's
# cmcldeps wrapper, which the resource does not need (it only includes the .ico).
set(CMAKE_NINJA_CMCLDEPS_RC OFF)
add_library(ShootManyRobotsRes OBJECT res/ShootManyRobots.rc)
target_sources(ShootManyRobots PRIVATE $<TARGET_OBJECTS:ShootManyRobotsRes>)
target_include_directories(ShootManyRobots PRIVATE src)
target_link_libraries(ShootManyRobots PRIVATE SMRGuest XenonUtils xenia_gpu_d3d12 xenia_apu)
target_compile_definitions(ShootManyRobots PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN _CRT_SECURE_NO_WARNINGS)
target_compile_options(ShootManyRobots PRIVATE -march=sandybridge /EHsc -Wno-unused-parameter -Wno-missing-field-initializers)
# Guest call chains are deep and the generated functions have big frames.
target_link_options(ShootManyRobots PRIVATE /STACK:16777216)
