cmake_minimum_required(VERSION 3.20)
project(xboxrecomp VERSION 0.1.0 LANGUAGES C)

# Xbox Static Recompilation Runtime Library
# Provides link-time libraries for statically recompiled Xbox games.
# Pattern: extract emulator subsystems (xemu) into reusable static libraries.
#
# Libraries:
#   xbox_kernel    - Xbox kernel replacement layer (147 imports → Win32)
#   xbox_d3d8      - D3D8 → D3D11 graphics compatibility
#   xbox_dsound    - DirectSound → software mixer compatibility
#   xbox_input     - Xbox gamepad → XInput compatibility
#   xbox_apu       - MCPX APU audio emulation (extracted from xemu)
#   xbox_nv2a      - NV2A GPU emulation (extracted from xemu)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Common definitions for all targets
add_compile_definitions(
    WIN32_LEAN_AND_MEAN
    NOMINMAX
)

# Option to build examples

# ── Runtime Libraries ─────────────────────────────────────────

# Platform: Win32/NT type vocabulary + POSIX implementation of generic
# Win32 host primitives. Carries no Xbox semantics; provides the foundation
# the kernel HLE and the graphics/audio/input layers build on.
if(NOT WIN32)
    option(XBOX_POSIX_GPU "Build the NV2A GPU (Vulkan renderer) on POSIX hosts" ON)
    # windows.h & co. for every target on POSIX hosts (ARM port, docs Update 677)
    include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/posix_include
                        ${CMAKE_CURRENT_SOURCE_DIR}/src)
    # 16-bit wchar_t, so L"..." matches the runtime's WCHAR (win32_compat.h
    # routes the wcs* calls to 16-bit implementations)
    add_compile_options($<$<COMPILE_LANGUAGE:C>:-fshort-wchar>)
endif()
add_subdirectory(src/platform)

# Core: Xbox kernel replacement (memory layout, file I/O, threading, sync)
add_subdirectory(src/kernel)

# Graphics: D3D8 → D3D11 compatibility layer
add_subdirectory(src/d3d)

# Audio: DirectSound → software mixer compatibility
add_subdirectory(src/audio)

# Audio: MCPX APU hardware emulation (voice processor, DSP)
add_subdirectory(src/apu)

# GPU: NV2A hardware emulation (register handlers, MMIO, push buffer)
add_subdirectory(src/nv2a)

# GPU: xemu's NV2A PFIFO/PGRAPH core + OpenGL renderer (RECOMP_NV2A_XEMU=1)
# (on Linux too since the ARM port, docs Update 677: Vulkan renderer + EGL)
if(WIN32 OR XBOX_POSIX_GPU)
    add_subdirectory(src/gpu)
endif()

# Input: Xbox gamepad → XInput
add_subdirectory(src/input)

# Shared FMV player (Media Foundation -> D3D8 texture)
add_subdirectory(src/video)

# ── Umbrella target ───────────────────────────────────────────

# Convenience target that links all runtime libraries
add_library(xboxrecomp INTERFACE)
target_link_libraries(xboxrecomp INTERFACE
    xbox_kernel
    xbox_d3d8
    xbox_dsound
    xbox_input
    xbox_apu
    xbox_nv2a
    xbox_video
)
if(TARGET xbox_gpu)
    target_link_libraries(xboxrecomp INTERFACE xbox_gpu)
endif()

