cmake_minimum_required(VERSION 3.20)
# Keep in step with the changelog in README.md. This sat at 0.3.0 until August
# 2026 while the project shipped 0.6.x through 0.8.0, so every build printed
# "ps3recomp runtime v0.3.0" and was believed. v0.9.0 is the first TAGGED one.
project(ps3recomp_runtime
    VERSION 0.18.0
    LANGUAGES C CXX
    DESCRIPTION "ps3recomp - Static recompilation runtime for PS3 games"
)

# ---------------------------------------------------------------------------
# Standards
# ---------------------------------------------------------------------------
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ---------------------------------------------------------------------------
# Options
# ---------------------------------------------------------------------------
option(PS3RECOMP_BUILD_TESTS "Build unit tests" OFF)

# ---------------------------------------------------------------------------
# Collect source files
# ---------------------------------------------------------------------------

# HLE library sources
file(GLOB_RECURSE HLE_SOURCES
    "libs/system/*.c"
    "libs/filesystem/*.c"
    "libs/input/*.c"
    "libs/audio/*.c"
    "libs/video/*.c"
    "libs/network/*.c"
    "libs/spurs/*.c"
    "libs/sync/*.c"
    "libs/codec/*.c"
    "libs/font/*.c"
    "libs/misc/*.c"
    "libs/hardware/*.c"
    "runtime/diag/*.c"      # yz_frontier_trace, used by the live draw engine
)
# Exclude test harnesses (e.g. libs/video/tests/*): they define their own main().
list(FILTER HLE_SOURCES EXCLUDE REGEX "/tests/")

# The Vulkan RSX backend is opt-in while it grows (stage V0: clear + present).
# It loads libvulkan at run time, so building needs only the Vulkan headers --
# no target-architecture libvulkan, which keeps cross-compiling simple.
option(PS3RECOMP_RSX_VULKAN "Build the Vulkan RSX backend (Linux)" OFF)
list(FILTER HLE_SOURCES EXCLUDE REGEX "rsx_vulkan_backend\\.c$")
if(PS3RECOMP_RSX_VULKAN)
    if(APPLE)
        message(FATAL_ERROR "PS3RECOMP_RSX_VULKAN is Linux and Windows only")
    endif()
    find_path(PS3RECOMP_VULKAN_INCLUDE_DIR vulkan/vulkan.h)
    if(NOT PS3RECOMP_VULKAN_INCLUDE_DIR)
        message(FATAL_ERROR "Vulkan headers not found. Debian/Ubuntu: "
            "libvulkan-dev, Fedora: vulkan-headers -- or pass "
            "-DPS3RECOMP_VULKAN_INCLUDE_DIR=<dir containing vulkan/vulkan.h>")
    endif()
    list(APPEND HLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_vulkan_backend.c")
    # HLSL -> SPIR-V for guest programs: a stub unless glslang is found below.
    list(APPEND HLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_shader_spirv.cpp")
    # DLSS through NGX: a stub unless PS3RECOMP_DLSS_SDK (below) names the SDK.
    list(APPEND HLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_vk_dlss.cpp")
    # FSR 3 through the FidelityFX SDK: a stub unless PS3RECOMP_FFX_DIR (below).
    list(APPEND HLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_vk_fsr3.cpp")
endif()

# The Metal backend is Objective-C and Apple-only, so it is added explicitly
# rather than globbed. It needs no platform #ifdef of its own as a result.
if(APPLE)
    enable_language(OBJC)
    list(APPEND HLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_metal_backend.m")
endif()
# The HLSL -> MSL translator is C++ (it drives glslang's C++ API) and compiles
# to a stub wherever the shader tools are absent, so it is part of the
# library everywhere. See the "Guest shaders on Metal" section below.
list(APPEND HLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_shader_msl.cpp")
# cellAtrac decodes with FFmpeg's ATRAC3plus decoder as extracted by PPSSPP
# (LGPL-2.1, kept separate in third_party/at3_standalone -- see its README).
# PS3RECOMP_ATRAC_LGPL=OFF swaps the decoder for a stub that fails every open, which keeps the library and any
# binary linked from it free of LGPL code (third_party/at3_stub); the title then sees an ATRAC error.
option(PS3RECOMP_ATRAC_LGPL "Link the LGPL-2.1 ATRAC3plus decoder (cellAtrac)" ON)
if(PS3RECOMP_ATRAC_LGPL)
    file(GLOB AT3_SOURCES "third_party/at3_standalone/*.cpp")
    set_source_files_properties(${AT3_SOURCES} PROPERTIES COMPILE_OPTIONS "-w")  # upstream code
    list(APPEND HLE_SOURCES ${AT3_SOURCES})
else()
    list(APPEND HLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/third_party/at3_stub/at3_stub.c")
endif()

# Runtime core (syscalls, PPU, SPU, memory)
file(GLOB_RECURSE RUNTIME_SOURCES
    "runtime/*.c"
    "runtime/*.cpp"
)
# Exclude:
#  - test harnesses (runtime/*/tests/*): they have their own main() and #include
#    per-game generated headers (spu_recomp.h) that a plain library build lacks.
#  - the PPU boot scaffold (runtime/ppu/, runtime/host/): these are compiled
#    per-game against the lifter-generated ppu_recomp.h (and the game's RSX/D3D12
#    backend), not as part of the game-agnostic runtime library. See
#    docs/PPU_RECOMP.md.
#  - spu_dispatch_mt.c: the musttail SPU indirect-branch fast path. It uses
#    __attribute__((musttail)), which MSVC cannot parse, and it must be compiled
#    *inside the game build* by clang so the tail call is guaranteed -- a plain
#    call leaks one resolver frame per guest loop iteration. A port adds it to
#    its own target; see templates/project/CMakeLists.txt.
list(FILTER RUNTIME_SOURCES EXCLUDE REGEX "/tests/|/runtime/ppu/|/runtime/host/|/spu_dispatch_mt\.c$")

# ---------------------------------------------------------------------------
# Static library
# ---------------------------------------------------------------------------
add_library(ps3recomp_runtime STATIC
    ${HLE_SOURCES}
    ${RUNTIME_SOURCES}
)

target_include_directories(ps3recomp_runtime PUBLIC
    "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

# HLE modules include their own headers
target_include_directories(ps3recomp_runtime PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/system"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/filesystem"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/input"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/audio"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/video"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/network"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/spurs"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/sync"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/codec"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/font"
    "${CMAKE_CURRENT_SOURCE_DIR}/libs/misc"
    "${CMAKE_CURRENT_SOURCE_DIR}/runtime/spu"
    "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

# Platform-specific link libraries
if(WIN32)
    target_link_libraries(ps3recomp_runtime PRIVATE
        ws2_32      # Winsock for network modules
        ole32       # COM for WASAPI audio
        bcrypt      # BCryptGenRandom for SSL RNG
    )
else()
    # cellPad and cellAudio select their SDL2 backends unconditionally off
    # Windows (see PAD_BACKEND_SDL2 / AUDIO_BACKEND_SDL2), so SDL2 is required
    # here -- docs/BUILDING.md already lists it as the Linux/macOS dependency.
    find_package(SDL2 QUIET)
    if(SDL2_FOUND)
        if(TARGET SDL2::SDL2)
            target_link_libraries(ps3recomp_runtime PRIVATE SDL2::SDL2)
        else()
            target_include_directories(ps3recomp_runtime PRIVATE ${SDL2_INCLUDE_DIRS})
            target_link_libraries(ps3recomp_runtime PRIVATE ${SDL2_LIBRARIES})
        endif()
        message(STATUS "  SDL2:         found (input + audio backends enabled)")
    else()
        message(FATAL_ERROR
            "SDL2 not found. It provides the cellPad and cellAudio backends on "
            "non-Windows hosts.\n"
            "  macOS:  brew install sdl2\n"
            "  Debian: apt install libsdl2-dev\n"
            "If SDL2 is installed in a non-standard prefix, pass "
            "-DCMAKE_PREFIX_PATH=<prefix>.")
    endif()

    find_package(Threads REQUIRED)
    target_link_libraries(ps3recomp_runtime PRIVATE Threads::Threads)
endif()

if(APPLE)
    target_link_libraries(ps3recomp_runtime PUBLIC
        "-framework Metal"
        "-framework QuartzCore"
        "-framework Foundation"
        "-framework AppKit"
        "-framework ApplicationServices"
    )
    set_source_files_properties(
        "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_metal_backend.m"
        PROPERTIES COMPILE_OPTIONS "-fobjc-arc")
    message(STATUS "  Metal:        RSX backend enabled (rsx_metal_backend.m)")
endif()

if(PS3RECOMP_RSX_VULKAN)
    target_include_directories(ps3recomp_runtime PRIVATE "${PS3RECOMP_VULKAN_INCLUDE_DIR}")
    # PUBLIC: ps3recomp_host selects its backend on this definition.
    target_compile_definitions(ps3recomp_runtime PUBLIC PS3RECOMP_RSX_VULKAN)
    if(WIN32)
        # The backend's window and its input are SDL2's on Windows as well:
        # cellPad takes its SDL2 backend (pads, keyboard, mouse), and winpthreads
        # serves the backend's pthreads
        find_package(SDL2 REQUIRED)
        target_compile_definitions(ps3recomp_runtime PRIVATE PS3RECOMP_PAD_USE_SDL2)
        find_package(Threads REQUIRED)
        target_link_libraries(ps3recomp_runtime PUBLIC Threads::Threads d3d12 dxgi d3dcompiler_47)
        if(TARGET SDL2::SDL2)
            target_link_libraries(ps3recomp_runtime PUBLIC SDL2::SDL2)
        else()
            target_include_directories(ps3recomp_runtime PRIVATE ${SDL2_INCLUDE_DIRS})
            target_link_libraries(ps3recomp_runtime PUBLIC ${SDL2_LIBRARIES})
        endif()
    endif()
    target_link_libraries(ps3recomp_runtime PUBLIC ${CMAKE_DL_LIBS})
    message(STATUS "  Vulkan:       RSX backend enabled (rsx_vulkan_backend.c, headers in ${PS3RECOMP_VULKAN_INCLUDE_DIR})")
    # NVIDIA DLSS: the NGX SDK (github.com/NVIDIA/DLSS) is not part of this
    # repository. Point PS3RECOMP_DLSS_SDK at it to build the DLSS path; the
    # DLSS library (libnvidia-ngx-dlss.so.*, nvngx_dlss.dll) then goes next
    # to the executable.
    set(PS3RECOMP_DLSS_SDK "" CACHE PATH "NVIDIA DLSS SDK folder (include/, lib/)")
    if(PS3RECOMP_DLSS_SDK)
        if(WIN32)
            set(_ngx_lib "${PS3RECOMP_DLSS_SDK}/lib/Windows_x86_64/x64/nvsdk_ngx_s.lib")
        else()
            set(_ngx_lib "${PS3RECOMP_DLSS_SDK}/lib/Linux_x86_64/libnvsdk_ngx.a")
        endif()
        if(NOT EXISTS "${_ngx_lib}")
            message(FATAL_ERROR "PS3RECOMP_DLSS_SDK: ${_ngx_lib} not found")
        endif()
        target_include_directories(ps3recomp_runtime PRIVATE "${PS3RECOMP_DLSS_SDK}/include")
        target_compile_definitions(ps3recomp_runtime PRIVATE PS3RECOMP_DLSS)
        target_link_libraries(ps3recomp_runtime PUBLIC "${_ngx_lib}" ${CMAKE_DL_LIBS})
        if(MINGW)
            # NVIDIA ships the NGX loader for MSVC only: its objects ask for the
            # MSVC static runtime, and the few MSVC runtime calls it makes are
            # answered by rsx_vk_ngx_mingw.cpp (the C library ones by the UCRT).
            target_sources(ps3recomp_runtime PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_vk_ngx_mingw.cpp")
            target_link_options(ps3recomp_runtime PUBLIC "LINKER:--Xlink=-nodefaultlib:libcmt"
                                "LINKER:--Xlink=-nodefaultlib:libcpmt" "LINKER:--Xlink=-nodefaultlib:oldnames")
        endif()
        message(STATUS "  DLSS:         NGX from ${PS3RECOMP_DLSS_SDK}")
    endif()
    # AMD FSR 3.1: the FidelityFX SDK's FSR 3 upscaler and VK backend, built
    # into one static library with its shaders (the SDK's shader compiler is a
    # Windows program), next to the SDK's include/ folder:
    # PS3RECOMP_FFX_DIR/include, PS3RECOMP_FFX_DIR/lib/libffx_fsr3_vk.a.
    # Or PS3RECOMP_FFX_TARGET: a library target the including project builds
    # from the same sources, whose interface carries the include folder.
    set(PS3RECOMP_FFX_DIR "" CACHE PATH "FidelityFX FSR 3 build (include/, lib/libffx_fsr3_vk.a)")
    set(PS3RECOMP_FFX_TARGET "" CACHE STRING "CMake target of the FidelityFX FSR 3 library (instead of PS3RECOMP_FFX_DIR)")
    if(PS3RECOMP_FFX_TARGET)
        if(NOT TARGET ${PS3RECOMP_FFX_TARGET})
            message(FATAL_ERROR "PS3RECOMP_FFX_TARGET: no target ${PS3RECOMP_FFX_TARGET}")
        endif()
        target_link_libraries(ps3recomp_runtime PUBLIC ${PS3RECOMP_FFX_TARGET})
        set(_ffx_from "target ${PS3RECOMP_FFX_TARGET}")
    elseif(PS3RECOMP_FFX_DIR)
        if(NOT EXISTS "${PS3RECOMP_FFX_DIR}/lib/libffx_fsr3_vk.a")
            message(FATAL_ERROR "PS3RECOMP_FFX_DIR: ${PS3RECOMP_FFX_DIR}/lib/libffx_fsr3_vk.a not found")
        endif()
        target_include_directories(ps3recomp_runtime PRIVATE "${PS3RECOMP_FFX_DIR}/include")
        target_link_libraries(ps3recomp_runtime PUBLIC "${PS3RECOMP_FFX_DIR}/lib/libffx_fsr3_vk.a")
        set(_ffx_from "${PS3RECOMP_FFX_DIR}")
    endif()
    if(_ffx_from)
        target_compile_definitions(ps3recomp_runtime PRIVATE PS3RECOMP_FSR3 FFX_GCC)
        # the SDK's headers use C++20 (std::popcount)
        set_source_files_properties("${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_vk_fsr3.cpp"
                                    TARGET_DIRECTORY ps3recomp_runtime PROPERTIES COMPILE_OPTIONS "-std=c++20")
        message(STATUS "  FSR 3:        FidelityFX from ${_ffx_from}")
    endif()
endif()

# cellVdec decodes H.264 / MPEG-2 through FFmpeg when this is on. libavcodec
# is loaded at run time (dlopen / LoadLibrary), never linked, so building needs
# only the FFmpeg headers, and a binary run where FFmpeg is absent -- or with
# PS3RECOMP_FFMPEG=0 -- keeps the callback-only decoders. FFmpeg stays a
# separate, user-supplied LGPL library. See libs/codec/ffmpeg_runtime.h.
option(PS3RECOMP_FFMPEG_RUNTIME "Decode media through FFmpeg loaded at run time (needs the FFmpeg headers)" OFF)
if(PS3RECOMP_FFMPEG_RUNTIME)
    find_path(PS3RECOMP_FFMPEG_INCLUDE_DIR libavcodec/avcodec.h)
    if(NOT PS3RECOMP_FFMPEG_INCLUDE_DIR)
        message(FATAL_ERROR "FFmpeg headers not found. Debian/Ubuntu: "
            "libavcodec-dev, Fedora: ffmpeg-free-devel, MSYS2: mingw-w64-x86_64-ffmpeg "
            "-- or pass -DPS3RECOMP_FFMPEG_INCLUDE_DIR=<dir containing libavcodec/avcodec.h>")
    endif()
    target_include_directories(ps3recomp_runtime PRIVATE "${PS3RECOMP_FFMPEG_INCLUDE_DIR}")
    target_compile_definitions(ps3recomp_runtime PRIVATE PS3RECOMP_FFMPEG_RUNTIME)
    target_link_libraries(ps3recomp_runtime PUBLIC ${CMAKE_DL_LIBS})
    message(STATUS "  FFmpeg:       run-time loaded (headers in ${PS3RECOMP_FFMPEG_INCLUDE_DIR})")
endif()

# ---------------------------------------------------------------------------
# Guest shaders on Metal: HLSL -> SPIR-V -> MSL
# ---------------------------------------------------------------------------
# The RSX decompilers emit HLSL for the D3D12 backend. Metal cannot compile
# that, so libs/video/rsx_shader_msl.cpp lowers it with glslang (HLSL ->
# SPIR-V) and spirv-cross (SPIR-V -> MSL). Both are optional: without them the
# module compiles to a stub, rsx_hlsl_to_msl_available() says so, and the
# Metal backend stays on its fixed-function path. The translation has no GPU
# in it, so it is built and tested on Linux as well -- that is where the
# decompilers' HLSL meets glslang's front end on every push.
#
#   macOS:  brew install glslang spirv-cross
#   Debian: apt install glslang-dev glslang-tools spirv-tools \
#                       libspirv-cross-c-shared-dev
#           (glslang-tools only because the packaged CMake config declares
#           the standalone binary as an imported target and fails to load
#           without it)
set(PS3RECOMP_MSL_TRANSLATION OFF)
if(UNIX)
    find_package(glslang CONFIG QUIET)

    # spirv-cross's C API. Debian ships it as a shared library and Homebrew as
    # a static one, under different pkg-config names -- and pkg-config itself
    # is not a given on a Mac, so fall back to a plain header + library search.
    set(SPVC_OK OFF)
    find_package(PkgConfig QUIET)
    if(PkgConfig_FOUND)
        pkg_check_modules(SPVC QUIET spirv-cross-c-shared)
        if(NOT SPVC_FOUND)
            pkg_check_modules(SPVC QUIET spirv-cross-c)
        endif()
        if(SPVC_FOUND)
            set(SPVC_INC     ${SPVC_INCLUDE_DIRS})
            set(SPVC_LIBS    ${SPVC_LINK_LIBRARIES})
            set(SPVC_LIBDIRS ${SPVC_LIBRARY_DIRS})
            set(SPVC_OK ON)
        endif()
    endif()
    if(NOT SPVC_OK)
        find_path(SPVC_INC_DIR spirv_cross_c.h PATH_SUFFIXES spirv_cross)
        find_library(SPVC_LIB NAMES spirv-cross-c-shared spirv-cross-c)
        if(SPVC_INC_DIR AND SPVC_LIB)
            set(SPVC_INC  ${SPVC_INC_DIR})
            set(SPVC_LIBS ${SPVC_LIB})
            get_filename_component(SPVC_LIBDIRS "${SPVC_LIB}" DIRECTORY)
            set(SPVC_OK ON)
        endif()
    endif()

    if(glslang_FOUND AND SPVC_OK
       AND TARGET glslang::glslang AND TARGET glslang::SPIRV)
        add_library(ps3recomp_msl_translation INTERFACE)
        target_compile_definitions(ps3recomp_msl_translation INTERFACE
            PS3RECOMP_HAVE_MSL_TRANSLATION=1)
        target_include_directories(ps3recomp_msl_translation INTERFACE ${SPVC_INC})
        target_link_libraries(ps3recomp_msl_translation INTERFACE
            glslang::glslang glslang::SPIRV ${SPVC_LIBS})
        if(TARGET glslang::glslang-default-resource-limits)
            target_link_libraries(ps3recomp_msl_translation INTERFACE
                glslang::glslang-default-resource-limits)
        endif()
        # A static spirv-cross C API (Homebrew's) needs the backends it was
        # built from, and neither pkg-config nor the library name says so:
        # pick up the static siblings next to it. The shared one carries
        # its own.
        if(SPVC_LIBS MATCHES "\\.a$")
            foreach(part msl glsl hlsl cpp reflect util core)
                find_library(SPVC_PART_${part} NAMES spirv-cross-${part}
                    HINTS ${SPVC_LIBDIRS} NO_DEFAULT_PATH)
                if(SPVC_PART_${part})
                    target_link_libraries(ps3recomp_msl_translation INTERFACE
                        ${SPVC_PART_${part}})
                endif()
            endforeach()
        endif()
        # The translator's identity in the MSL translation cache: a hash of its
        # sources, so a rebuild that changes nothing keeps the cached files and
        # an edit to the translator retires them. (Its build time did the
        # latter too, and threw the whole cache away on every rebuild.)
        set(_msl_sources libs/video/rsx_shader_msl.cpp libs/video/rsx_shader_msl.h)
        set(_msl_text "")
        foreach(f IN LISTS _msl_sources)
            file(SHA256 "${CMAKE_CURRENT_SOURCE_DIR}/${f}" _h)
            string(APPEND _msl_text "${_h}")
        endforeach()
        string(SHA256 _msl_id "${_msl_text}")
        string(SUBSTRING "${_msl_id}" 0 23 _msl_id)
        set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${_msl_sources})
        target_compile_definitions(ps3recomp_msl_translation INTERFACE
            "PS3RECOMP_MSL_TRANSLATOR_ID=\"${_msl_id}\"")
        target_link_libraries(ps3recomp_runtime PRIVATE ps3recomp_msl_translation)
        set(PS3RECOMP_MSL_TRANSLATION ON)
        message(STATUS "  Shaders:      guest programs translated to MSL (glslang ${glslang_VERSION}, ${SPVC_LIBS})")
    else()
        message(STATUS "  Shaders:      glslang/spirv-cross not found; Metal backend stays fixed-function")
    endif()
endif()

# Guest shaders on Vulkan: HLSL -> SPIR-V. glslang only -- Vulkan consumes
# SPIR-V, so spirv-cross is not needed. Without glslang
# libs/video/rsx_shader_spirv.cpp is a stub and the Vulkan backend stays on
# its fixed-function path.
if(PS3RECOMP_RSX_VULKAN)
    if(NOT glslang_FOUND)
        find_package(glslang CONFIG QUIET)
    endif()
    if(glslang_FOUND AND TARGET glslang::glslang AND TARGET glslang::SPIRV)
        # The translator's id (cmake/RsxSpirvTranslatorId.cmake): caches of
        # its SPIR-V on disk key their files by it.
        include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/RsxSpirvTranslatorId.cmake")
        rsx_spirv_translator_id(_rsx_spirv_id
            "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_shader_spirv.cpp"
            "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_shader_spirv.h")
        set_source_files_properties(
            "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/rsx_shader_spirv.cpp"
            PROPERTIES COMPILE_DEFINITIONS
            "PS3RECOMP_HAVE_SPIRV_TRANSLATION=1;RSX_SPIRV_TRANSLATOR_ID=\"${_rsx_spirv_id}\"")
        target_link_libraries(ps3recomp_runtime PRIVATE glslang::glslang glslang::SPIRV)
        if(TARGET glslang::glslang-default-resource-limits)
            target_link_libraries(ps3recomp_runtime PRIVATE
                glslang::glslang-default-resource-limits)
        endif()
        set(PS3RECOMP_SPIRV_TRANSLATION ON)
        message(STATUS "  Vulkan:       guest programs available (glslang ${glslang_VERSION}); "
                       "opt in with PS3RECOMP_VK_GUEST_PROGRAMS=1")
    else()
        message(STATUS "  Vulkan:       glslang not found; guest programs unavailable")
    endif()
endif()

# ---------------------------------------------------------------------------
# Compiler flags
# ---------------------------------------------------------------------------
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
    target_compile_options(ps3recomp_runtime PRIVATE
        -Wall -Wextra -Wno-unused-parameter
        -fno-strict-aliasing
        # No spontaneous fused multiply-add. MSVC never contracts, and x86-64
        # without -mfma has nothing to contract into, so every validated
        # result came from separate multiplies and adds; arm64 has FMA in
        # the base ISA and Clang fuses by default there. A PPC fmadd is
        # spelt as one where the lifter means one.
        -ffp-contract=off
        # Explicit, because the per-config defaults cannot be trusted: this
        # tree's caches carry CMAKE_C_FLAGS_RELEASE="" and CMAKE_CXX_FLAGS_RELEASE="",
        # and clang-cl with no /O flag compiles at /Od -- a "Release" runtime
        # was unoptimised without a word. Duplicating a default -O2 is harmless.
        # Every configuration but Debug, including none: a port configured
        # without CMAKE_BUILD_TYPE matched neither name and compiled the whole
        # runtime -- the FIFO walker, the draw engine, the MFC -- at -O0.
        $<$<NOT:$<CONFIG:Debug>>:-O2>
    )
    # SSE4.1 (any x86-64 since ~2008): pshufb/blendv for spu_shufb, the
    # hottest SPU op in lifted audio code. Probed rather than assumed: recent
    # Apple clang rejects the flag outright for arm64 ("unsupported option
    # '-msse4.1'"), which stops the whole runtime from building there.
    include(CheckCCompilerFlag)
    check_c_compiler_flag(-msse4.1 PS3RECOMP_HAS_MSSE41)
    if(PS3RECOMP_HAS_MSSE41)
        # Lifted consumers compile the same static-inline SPU helpers. Export
        # the runtime's existing ISA requirement so their fast path agrees.
        target_compile_options(ps3recomp_runtime PUBLIC -msse4.1)
    endif()
elseif(MSVC)
    # Real cl.exe -- clang-cl reports its id as Clang and is handled above.
    #
    # This does not currently build. The runtime and the lifted output use GCC/
    # Clang builtins that cl has no equivalent for, and the failure lands as a
    # wall of errors in whichever file the build happens to reach first rather
    # than as anything that names the cause. Say it here instead.
    message(WARNING
        "Building with cl.exe (MSVC). This is not currently supported.\n"
        "  The runtime uses __atomic_* builtins, __int128 and __builtin_bswap*, "
        "and the lifted output uses __int128 for the PPC 64x64 multiplies. "
        "cl has none of them.\n"
        "  Use clang-cl instead -- it consumes the MSVC headers and libraries, "
        "so nothing else about a Visual Studio build changes:\n"
        "    cmake -S . -B build -G Ninja "
        "-DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl\n"
        "  See docs/BUILDING.md. Porting the intrinsics to cl is a welcome "
        "contribution; nothing else stands in the way.")
    target_compile_options(ps3recomp_runtime PRIVATE
        /W3 /wd4100 /wd4244 /wd4267 /wd4996
        /Zc:__cplusplus
        /experimental:c11atomics   # Enable C11 <stdatomic.h> support
    )
    target_compile_definitions(ps3recomp_runtime PRIVATE
        _CRT_SECURE_NO_WARNINGS
    )
endif()

if(NOT MSVC AND NOT WIN32)
    # dladdr/Dl_info (runtime/platform/win32_backtrace.h) are GNU extensions
    # that glibc hides unless _GNU_SOURCE is set. g++ defines it implicitly,
    # so the C++ boot scaffold never noticed; a C file including the same
    # shim does not get it for free.
    target_compile_definitions(ps3recomp_runtime PRIVATE _GNU_SOURCE)
endif()

# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
if(PS3RECOMP_BUILD_TESTS)
    include(CTest)
    # sync_stress is written directly against the Win32 threading API
    # (WaitForMultipleObjects, joinable HANDLEs, Interlocked*, CRITICAL_SECTION).
    # Off Windows all of that comes from runtime/platform/win32_compat.h, so
    # the suite runs everywhere -- and is the arm64 memory-model regression
    # net for the lv2 sync primitives.
    add_subdirectory(tests/sync_stress)
    # cellFiber switches contexts with Windows fibers on Windows and ucontext
    # everywhere else. Apple deprecated the ucontext routines and gates both
    # them and the shape of ucontext_t on _XOPEN_SOURCE, so whether that path
    # works on arm64 is a question to answer rather than assume.
    add_subdirectory(tests/fiber_switch)
    # Syscall 14's address/flags classification as a plain unit test: the
    # check is pure, so the test links one runtime translation unit
    # (lv2_lock_line.c) and nothing else.
    add_subdirectory(tests/syscalls)
    # cellVdec through its guest API: the callback-only decoder always, and
    # real H.264 / MPEG-2 decode when FFmpeg is on (libs/codec/tests).
    add_subdirectory(libs/codec/tests)
    # cellGcm's FIFO walker without a backend: where GET_REPORT writes for
    # each report DMA context. Links the runtime like the POSIX host below.
    if(UNIX)
        add_subdirectory(tests/gcm_reports)
        # The per-call tile and zcull log lines, gated on PS3_VERBOSE.
        add_subdirectory(tests/gcm_tile_log)
        # The same walker's JUMP decode: the old and the new form.
        add_subdirectory(tests/gcm_fifo_jump)
    endif()
    # PS3_LOG_FIRST, the saturating counter behind the "first n times" log lines.
    add_executable(test_log_first tests/test_log_first.c)
    target_include_directories(test_log_first PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
    add_test(NAME log_first COMMAND test_log_first)

    if(NOT WIN32)
        # The vector single-precision ops against the exact per-lane references.
        add_executable(test_spu_float_exact
            runtime/spu/tests/test_spu_float_exact.c)
        target_include_directories(test_spu_float_exact PRIVATE include runtime/spu)
        target_compile_options(test_spu_float_exact PRIVATE -UNDEBUG)
        target_link_libraries(test_spu_float_exact PRIVATE m)
        add_test(NAME spu_float_exact COMMAND test_spu_float_exact)
        set_tests_properties(spu_float_exact PROPERTIES TIMEOUT 300)
        # The vector forms against the scalar per-lane forms, directly.
        add_executable(test_spu_float_vector_scalar
            runtime/spu/tests/test_spu_float_vector_scalar.c)
        target_include_directories(test_spu_float_vector_scalar PRIVATE include runtime/spu)
        target_compile_options(test_spu_float_vector_scalar PRIVATE -UNDEBUG)
        target_link_libraries(test_spu_float_vector_scalar PRIVATE m)
        add_test(NAME spu_float_vector_scalar COMMAND test_spu_float_vector_scalar 4)
        set_tests_properties(spu_float_vector_scalar PROPERTIES TIMEOUT 300)
        # On x86 both take the ISA the runtime and lifted code are built for, so the SSE4.1
        # vector form is the one under test; no contraction, as in the runtime.
        if(PS3RECOMP_HAS_MSSE41)
            target_compile_options(test_spu_float_exact PRIVATE -msse4.1 -ffp-contract=off)
            target_compile_options(test_spu_float_vector_scalar PRIVATE -msse4.1 -ffp-contract=off)
        endif()

        # The interpreter's op/immediate decode at the opcode-width and
        # immediate-form boundaries, against ISA-layout encodings.
        add_executable(test_spu_decode
            runtime/spu/tests/test_spu_decode.c)
        target_include_directories(test_spu_decode PRIVATE include runtime/spu)
        target_compile_options(test_spu_decode PRIVATE -UNDEBUG)
        target_link_libraries(test_spu_decode PRIVATE m)
        add_test(NAME spu_decode COMMAND test_spu_decode)
        set_tests_properties(spu_decode PROPERTIES TIMEOUT 30)

        # fi and cflts (NEON on ARM64) against their scalar references, in the
        # default FPCR and under round-toward-zero plus flush-to-zero.
        add_executable(test_spu_fi_cflts_neon
            runtime/spu/tests/test_spu_fi_cflts_neon.c)
        target_include_directories(test_spu_fi_cflts_neon PRIVATE include runtime/spu)
        target_compile_options(test_spu_fi_cflts_neon PRIVATE -UNDEBUG)
        target_link_libraries(test_spu_fi_cflts_neon PRIVATE m)
        add_test(NAME spu_fi_cflts_neon COMMAND test_spu_fi_cflts_neon)
        set_tests_properties(spu_fi_cflts_neon PROPERTIES TIMEOUT 300)

        add_executable(test_spu_lockline_wait
            runtime/spu/tests/test_spu_lockline_wait.c)
        target_include_directories(test_spu_lockline_wait PRIVATE include)
        target_compile_options(test_spu_lockline_wait PRIVATE -UNDEBUG)
        target_link_libraries(test_spu_lockline_wait PRIVATE Threads::Threads)
        add_test(NAME spu_lockline_wait COMMAND test_spu_lockline_wait)
        set_tests_properties(spu_lockline_wait PROPERTIES TIMEOUT 15)

    endif()
    add_executable(test_rsx_present_policy tests/test_rsx_present_policy.c)
    target_include_directories(test_rsx_present_policy PRIVATE
        "${CMAKE_CURRENT_SOURCE_DIR}/libs/video")
    add_test(NAME rsx_present_policy COMMAND test_rsx_present_policy)

    # vmx_fmaf (fused multiply-add without a libm call per lane) against fmaf.
    # Without -mfma it takes the double-rounding path; with it, the builtin.
    add_executable(test_vmx_fmaf tests/test_vmx_fmaf.c)
    target_compile_options(test_vmx_fmaf PRIVATE -UNDEBUG)
    if(NOT MSVC AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
        target_compile_options(test_vmx_fmaf PRIVATE -mno-fma)
    endif()
    if(NOT WIN32)
        target_link_libraries(test_vmx_fmaf PRIVATE m)
    endif()
    add_test(NAME vmx_fmaf COMMAND test_vmx_fmaf)
    set_tests_properties(vmx_fmaf PROPERTIES TIMEOUT 120)

    # Current colors are explicit shader inputs even when they equal the
    # hardware default; exercise both compact fetch entry points without a GPU.
    add_executable(test_vertex_current_color
        libs/video/tests/test_vertex_current_color.c
        libs/video/rsx_vertex_compact.c
        libs/video/rsx_dispatch.c)
    target_include_directories(test_vertex_current_color PRIVATE include libs/video)
    add_test(NAME vertex_current_color COMMAND test_vertex_current_color)

    # The converted-vertex cache's bookkeeping, without a GPU.
    add_executable(test_vertex_cache
        libs/video/tests/test_vertex_cache.c
        libs/video/rsx_vertex_compact.c
        libs/video/rsx_dispatch.c)
    target_include_directories(test_vertex_cache PRIVATE include libs/video)
    add_test(NAME vertex_cache COMMAND test_vertex_cache)


    find_package(Python3 3.10 COMPONENTS Interpreter QUIET)
    if(Python3_Interpreter_FOUND)
        add_test(NAME spinlock_word_width
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_spinlock_word_width.py")
        add_test(NAME spurs_task_exit_code_initialize
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_spurs_task_exit_code_initialize.py")
        add_test(NAME spurs_taskset_size
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_spurs_taskset_size.py")
        add_test(NAME spu_sdcrz
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_spu_sdcrz.py")
        add_test(NAME spu_dma_repeat
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_spu_dma_repeat.py")
    endif()
    if(NOT WIN32)
        find_package(Python3 3.10 COMPONENTS Interpreter QUIET)
        if(Python3_Interpreter_FOUND)
            add_test(NAME spu_fnms_zero_extended
                COMMAND "${Python3_EXECUTABLE}"
                    "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_spu_fnms_zero_extended.py")
        endif()
    endif()
    find_package(Python3 3.10 COMPONENTS Interpreter QUIET)
    if(Python3_Interpreter_FOUND)
        add_test(NAME loaded_image_memory
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_loaded_image_memory.py")
        add_test(NAME loader_memory_domain
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_loader_memory_domain.py")
        add_test(NAME mmapper_hle_heap
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_mmapper_hle_heap.py")
        add_test(NAME user_memory_domain
            COMMAND "${Python3_EXECUTABLE}"
                "${CMAKE_CURRENT_SOURCE_DIR}/tools/test_user_memory_domain.py")
    endif()
endif()

# ---------------------------------------------------------------------------
# POSIX host harness
# ---------------------------------------------------------------------------
# Drives the cellGcm -> RSX -> backend bridge with no lifted game, so a backend
# can be brought up and regression-tested without a game binary or a recompiler
# run. Builds on any POSIX host: it picks Metal on Apple and the null backend's
# headless software path everywhere else, so Linux -- which has no real backend
# yet -- can still run the whole guest command stream end to end in CI.
if(UNIX)
    add_executable(ps3recomp_host
        runtime/host/host_posix.c
        runtime/host/host_stubs.c
    )
    target_link_libraries(ps3recomp_host PRIVATE ps3recomp_runtime)
    if(NOT WIN32)
        # glibc keeps the math functions in a separate library; MSVC does not.
        target_link_libraries(ps3recomp_host PRIVATE m)
    endif()
    target_include_directories(ps3recomp_host PRIVATE
        "${CMAKE_CURRENT_SOURCE_DIR}/libs/video"
        "${CMAKE_CURRENT_SOURCE_DIR}/include"
    )
    # The runtime carries C++ (and, with translation on, spirv-cross), so the
    # C++ driver must do the final link or the standard library goes missing.
    set_target_properties(ps3recomp_host PROPERTIES LINKER_LANGUAGE CXX)

    # Guest shader translation, decompilers -> HLSL -> MSL, with no GPU: the
    # regression net for the Metal backend's shader path on every platform.
    if(PS3RECOMP_MSL_TRANSLATION)
        add_executable(test_shader_msl
            libs/video/tests/test_shader_msl.c
            libs/video/rsx_shader_msl.cpp
            libs/video/rsx_vp_decompiler.c
            libs/video/rsx_fp_decompiler.c
        )
        target_include_directories(test_shader_msl PRIVATE
            "${CMAKE_CURRENT_SOURCE_DIR}/libs/video"
            "${CMAKE_CURRENT_SOURCE_DIR}/include"
        )
        target_link_libraries(test_shader_msl PRIVATE ps3recomp_msl_translation)

        # The polygon offset through the engine and the Metal backend on the
        # GPU; the guest programs need the translation above.
        if(APPLE)
            add_executable(test_metal_poly_offset
                libs/video/tests/test_metal_poly_offset.m
                libs/video/rsx_tiling.c
                libs/video/rsx_shader_msl.cpp
                libs/video/rsx_dispatch.c
                libs/video/rsx_vertex_compact.c
                libs/video/rsx_texture_layout.c
                libs/video/rsx_vp_decompiler.c
                libs/video/rsx_fp_decompiler.c
            )
            set_source_files_properties(
                "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/tests/test_metal_poly_offset.m"
                PROPERTIES COMPILE_OPTIONS "-fobjc-arc")
            target_include_directories(test_metal_poly_offset PRIVATE
                "${CMAKE_CURRENT_SOURCE_DIR}/libs/video"
                "${CMAKE_CURRENT_SOURCE_DIR}/include"
            )
            target_link_libraries(test_metal_poly_offset PRIVATE
                ps3recomp_msl_translation
                "-framework Metal" "-framework QuartzCore" "-framework AppKit")
            # Only what the engine and the backend reach; the rest of the
            # runtime is not linked in.
            target_link_options(test_metal_poly_offset PRIVATE "-Wl,-dead_strip")

            # The depth-bounds test, which Metal does not have, emulated in
            # the fragment program; the same way.
            add_executable(test_metal_depth_bounds
                libs/video/tests/test_metal_depth_bounds.m
                libs/video/rsx_shader_msl.cpp
                libs/video/rsx_dispatch.c
                libs/video/rsx_vertex_compact.c
                libs/video/rsx_texture_layout.c
                libs/video/rsx_vp_decompiler.c
                libs/video/rsx_fp_decompiler.c
                libs/video/rsx_tiling.c
            )
            set_source_files_properties(
                "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/tests/test_metal_depth_bounds.m"
                PROPERTIES COMPILE_OPTIONS "-fobjc-arc")
            target_include_directories(test_metal_depth_bounds PRIVATE
                "${CMAKE_CURRENT_SOURCE_DIR}/libs/video"
                "${CMAKE_CURRENT_SOURCE_DIR}/include"
            )
            target_link_libraries(test_metal_depth_bounds PRIVATE
                ps3recomp_msl_translation
                "-framework Metal" "-framework QuartzCore" "-framework AppKit")
            target_link_options(test_metal_depth_bounds PRIVATE "-Wl,-dead_strip")

            # SET_ZMIN_MAX_CONTROL's depth clamp, the same way.
            add_executable(test_metal_depth_clamp
                libs/video/tests/test_metal_depth_clamp.m
                libs/video/rsx_shader_msl.cpp
                libs/video/rsx_dispatch.c
                libs/video/rsx_vertex_compact.c
                libs/video/rsx_texture_layout.c
                libs/video/rsx_vp_decompiler.c
                libs/video/rsx_fp_decompiler.c
                libs/video/rsx_tiling.c
            )
            set_source_files_properties(
                "${CMAKE_CURRENT_SOURCE_DIR}/libs/video/tests/test_metal_depth_clamp.m"
                PROPERTIES COMPILE_OPTIONS "-fobjc-arc")
            target_include_directories(test_metal_depth_clamp PRIVATE
                "${CMAKE_CURRENT_SOURCE_DIR}/libs/video"
                "${CMAKE_CURRENT_SOURCE_DIR}/include"
            )
            target_link_libraries(test_metal_depth_clamp PRIVATE
                ps3recomp_msl_translation
                "-framework Metal" "-framework QuartzCore" "-framework AppKit")
            target_link_options(test_metal_depth_clamp PRIVATE "-Wl,-dead_strip")
        endif()
    endif()

    # The HLSL -> SPIR-V translation cache, memory and disk. Needs glslang
    # only, no GPU.
    if(PS3RECOMP_SPIRV_TRANSLATION)
        add_executable(test_shader_spirv
            libs/video/tests/test_shader_spirv.c
            libs/video/rsx_shader_spirv.cpp
        )
        target_include_directories(test_shader_spirv PRIVATE
            "${CMAKE_CURRENT_SOURCE_DIR}/libs/video"
            "${CMAKE_CURRENT_SOURCE_DIR}/include"
        )
        target_link_libraries(test_shader_spirv PRIVATE glslang::glslang glslang::SPIRV)
        if(TARGET glslang::glslang-default-resource-limits)
            target_link_libraries(test_shader_spirv PRIVATE
                glslang::glslang-default-resource-limits)
        endif()
        set_target_properties(test_shader_spirv PROPERTIES LINKER_LANGUAGE CXX)
    endif()
endif()

# ---------------------------------------------------------------------------
# PPU boot smoke title
# ---------------------------------------------------------------------------
# The one target that LINKS AND RUNS the PPU boot scaffold. runtime/ppu/ is the
# game-agnostic boot path every port is built on, and nothing here has ever
# built it: it #includes the lifter's per-game ppu_recomp.h, so
# tools/check_ppu_scaffold.py compiles it against a stub header and stops
# there. Compiling is not running, and on POSIX the loader, the NID bridge, the
# lv2 syscalls, guest threads, the frame clock and the RSX backend had never
# once run together.
#
# runtime/ppu/tests/smoke/ supplies what was missing: a hand-written program in
# the lifter's ABI, and tools/make_smoke_elf.py writes both the PPU ELF it is
# loaded from and the ppu_recomp.h it compiles against -- the header from
# ppu_lifter.py's own HEADER_PREAMBLE, so the synthetic title cannot drift away
# from the ABI a real one gets.
#
# The HLE NID table is generated exactly as a port generates it, so the smoke
# title's firmware imports resolve through the same bridge a game's do.
if(UNIX)
    find_package(Python3 COMPONENTS Interpreter QUIET)
    if(NOT Python3_Interpreter_FOUND)
        message(STATUS "  Boot smoke:   no python3; ps3recomp_boot_smoke not built")
    else()
        set(SMOKE_GEN "${CMAKE_CURRENT_BINARY_DIR}/smoke")

        add_custom_command(
            OUTPUT "${SMOKE_GEN}/smoke.elf" "${SMOKE_GEN}/ppu_recomp.h"
            COMMAND "${Python3_EXECUTABLE}"
                    "${CMAKE_CURRENT_SOURCE_DIR}/tools/make_smoke_elf.py"
                    --out    "${SMOKE_GEN}/smoke.elf"
                    --header "${SMOKE_GEN}/ppu_recomp.h"
            DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tools/make_smoke_elf.py"
                    "${CMAKE_CURRENT_SOURCE_DIR}/tools/ppu_lifter.py"
            COMMENT "Generating the smoke title's image and ppu_recomp.h")

        add_custom_command(
            OUTPUT "${SMOKE_GEN}/ppu_hle_nids.cpp"
            COMMAND "${Python3_EXECUTABLE}"
                    "${CMAKE_CURRENT_SOURCE_DIR}/tools/gen_hle_nids.py"
                    --out "${SMOKE_GEN}/ppu_hle_nids.cpp" --all
            DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/tools/gen_hle_nids.py"
                    "${CMAKE_CURRENT_SOURCE_DIR}/tools/nid_database.py"
            COMMENT "Generating the HLE NID -> handler table")

        add_executable(ps3recomp_boot_smoke
            runtime/ppu/tests/boot_main.cpp
            runtime/ppu/tests/smoke/smoke_recomp.c
            runtime/ppu/ppu_loader.cpp
            runtime/ppu/ppu_hle.cpp
            runtime/ppu/ppu_fs.cpp
            runtime/ppu/ppu_sysprx.cpp
            "${SMOKE_GEN}/ppu_hle_nids.cpp"
            "${SMOKE_GEN}/ppu_recomp.h"
        )
        target_include_directories(ps3recomp_boot_smoke PRIVATE
            "${SMOKE_GEN}"                                  # the generated ppu_recomp.h
            "${CMAKE_CURRENT_SOURCE_DIR}/include"
            "${CMAKE_CURRENT_SOURCE_DIR}/runtime/ppu"
            "${CMAKE_CURRENT_SOURCE_DIR}/runtime/spu"
            "${CMAKE_CURRENT_SOURCE_DIR}/runtime/memory"
            "${CMAKE_CURRENT_SOURCE_DIR}/runtime/syscalls"
            "${CMAKE_CURRENT_SOURCE_DIR}/runtime/platform"
            "${CMAKE_CURRENT_SOURCE_DIR}/libs/video"
        )
        target_link_libraries(ps3recomp_boot_smoke PRIVATE ps3recomp_runtime)
        set_target_properties(ps3recomp_boot_smoke PROPERTIES LINKER_LANGUAGE CXX)

        # Where the runnable image lands, so CI and a developer both say
        #   ./build/ps3recomp_boot_smoke build/smoke/smoke.elf
        message(STATUS "  Boot smoke:   ps3recomp_boot_smoke + smoke/smoke.elf")

        # The same lift again, arranged the way a port's own tree arranges one:
        # the generated ppu_recomp.h and the lifted source in one directory,
        # which is what templates/project wants for -DRECOMP_DIR. The header is
        # generated into the build tree and the source lives in the repository,
        # so a directory holding both has to be made rather than pointed at.
        #
        # This is what lets CI build the project template against a real lift
        # and run it -- the check that a port started from templates/project
        # still boots, on a platform where it can be run.
        set(SMOKE_LIFT "${SMOKE_GEN}/lift")
        set(SMOKE_LIFT_SRC
            "${CMAKE_CURRENT_SOURCE_DIR}/runtime/ppu/tests/smoke/smoke_recomp.c")
        add_custom_command(
            OUTPUT "${SMOKE_LIFT}/ppu_recomp.h" "${SMOKE_LIFT}/smoke_recomp.c"
            COMMAND "${CMAKE_COMMAND}" -E make_directory "${SMOKE_LIFT}"
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "${SMOKE_GEN}/ppu_recomp.h" "${SMOKE_LIFT}/ppu_recomp.h"
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "${SMOKE_LIFT_SRC}" "${SMOKE_LIFT}/smoke_recomp.c"
            DEPENDS "${SMOKE_GEN}/ppu_recomp.h" "${SMOKE_LIFT_SRC}"
            COMMENT "Staging the smoke title's lift as a RECOMP_DIR")
        add_custom_target(ps3recomp_smoke_lift ALL
            DEPENDS "${SMOKE_LIFT}/ppu_recomp.h" "${SMOKE_LIFT}/smoke_recomp.c")
        message(STATUS "  Smoke lift:   smoke/lift (a RECOMP_DIR for templates/project)")
    endif()
endif()

# ---------------------------------------------------------------------------
# Install
# ---------------------------------------------------------------------------
install(TARGETS ps3recomp_runtime
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
)

install(DIRECTORY include/ps3emu
    DESTINATION include
    FILES_MATCHING PATTERN "*.h"
)

# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
message(STATUS "")
message(STATUS "ps3recomp runtime v${PROJECT_VERSION}")
message(STATUS "  C compiler:   ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}")
message(STATUS "  C++ compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "  Build type:   ${CMAKE_BUILD_TYPE}")
message(STATUS "")
