cmake_minimum_required(VERSION 3.20)
project(DisgaeaD2Recomp
    VERSION 0.1.0
    LANGUAGES C CXX
    DESCRIPTION "Recompiled PS3 game project (ps3recomp)"
)

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

option(D2_ENABLE_ISO_VFS "Experimental direct UDF/ISO9660 ISO assets (decrypted images)" OFF)

# Prefer Ninja if available
if(NOT CMAKE_GENERATOR MATCHES "Ninja" AND NOT DEFINED ENV{CMAKE_GENERATOR})
    message(STATUS "Tip: use -G Ninja for faster builds")
endif()

# ---------------------------------------------------------------------------
# The two directories this project is built from
# ---------------------------------------------------------------------------
# PS3RECOMP_DIR is the toolkit checkout: include/, runtime/, libs/, tools/.
# RECOMP_DIR is where the lifter's output for THIS game lives. They are
# separate on purpose -- a port keeps its generated code in its own tree and
# points at whatever toolkit checkout it is building against:
#
#   cmake -B build -G Ninja \
#         -DPS3RECOMP_DIR=/path/to/ps3recomp \
#         -DRECOMP_DIR=/path/to/my_game/recompiled
#
if(NOT DEFINED PS3RECOMP_DIR)
    set(PS3RECOMP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../ps3recomp")
endif()
get_filename_component(PS3RECOMP_DIR "${PS3RECOMP_DIR}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")

set(D2_GAME_VERSION "140" CACHE STRING "D2 executable version (100 or 140)")
set_property(CACHE D2_GAME_VERSION PROPERTY STRINGS 100 140)
if(D2_GAME_VERSION STREQUAL "140")
    set(D2_DEFAULT_RECOMP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/recomp-140")
    set(D2_DEFAULT_D2_OUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/out-140")
    set(D2_DEFAULT_EBOOT "work/v140/EBOOT.elf")
    set(D2_EXPECTED_ENTRY_OPD "0x00461000")
    set(D2_EXPECTED_TOC "0x0047DF98")
    set(D2_DEFAULT_SPU_DIR "${CMAKE_CURRENT_SOURCE_DIR}/spu-140")
    set(D2_MOVIE_OVERRIDES 001686AC 00168304 00167FC8)
    set(D2_NATIVE_OVERRIDES 002FE4AC 0033D91C)   # VP upload -> d2_shader.cpp, memset -> d2_fill.cpp (lift file order)
elseif(D2_GAME_VERSION STREQUAL "100")
    set(D2_DEFAULT_RECOMP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/recomp")
    set(D2_DEFAULT_D2_OUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/out")
    set(D2_DEFAULT_EBOOT "work/EBOOT.elf")
    set(D2_EXPECTED_ENTRY_OPD "0x003E0EE8")
    set(D2_EXPECTED_TOC "0x003FDE60")
    set(D2_DEFAULT_SPU_DIR "${CMAKE_CURRENT_SOURCE_DIR}/spu")
    set(D2_MOVIE_OVERRIDES 0015FD14 0015F9F4 0015F680)
else()
    message(FATAL_ERROR "D2_GAME_VERSION must be 100 or 140")
endif()
# Explicit -D paths take precedence; relative paths are relative to port/.
foreach(path IN ITEMS RECOMP_DIR D2_OUT_DIR SPU_DIR)
    if(NOT DEFINED ${path})
        set(${path} "${D2_DEFAULT_${path}}")
    endif()
    get_filename_component(${path} "${${path}}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
endforeach()
message(STATUS "Loader metadata: ${D2_OUT_DIR}")
message(STATUS "SPU output:      ${SPU_DIR}")
message(STATUS "Disgaea D2:     ${D2_GAME_VERSION}")

message(STATUS "ps3recomp SDK:   ${PS3RECOMP_DIR}")
message(STATUS "Lifted output:   ${RECOMP_DIR}")

if(NOT EXISTS "${RECOMP_DIR}/ppu_recomp.h")
    message(FATAL_ERROR
        "No ppu_recomp.h in ${RECOMP_DIR}.\n"
        "That header and the lifted sources beside it are what "
        "tools/ppu_lifter.py writes. Run the lifter, drop its output there, "
        "or point -DRECOMP_DIR= at wherever it already is.")
endif()

# The lifted translation units. A game's lift is hundreds of generated files,
# so they are globbed rather than listed; re-run CMake after a fresh lift.
file(GLOB RECOMP_SOURCES CONFIGURE_DEPENDS
    "${RECOMP_DIR}/*.c"
    "${RECOMP_DIR}/*.cpp"
)
if(NOT RECOMP_SOURCES)
    message(FATAL_ERROR
        "${RECOMP_DIR} has a ppu_recomp.h but no .c or .cpp beside it. The "
        "lifted functions and the function table live in those; without them "
        "there is nothing to run.")
endif()
list(LENGTH RECOMP_SOURCES RECOMP_SOURCE_COUNT)
message(STATUS "Lifted sources:  ${RECOMP_SOURCE_COUNT} file(s)")

# D2's movie facade has direct lifted calls as well as OPD calls. Rename only
# its three definitions in a BUILD copy; src/recomp and the dump stay intact.
# The replacements live in d2_movie.cpp. Re-lifts trigger reconfiguration.
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${RECOMP_SOURCES})
include(src/d2_cheats_lift.cmake)
include(src/d2_appearance_lift.cmake)
foreach(source IN LISTS RECOMP_SOURCES)
    file(READ "${source}" text)
    set(changed FALSE)
    foreach(address IN LISTS D2_MOVIE_OVERRIDES D2_NATIVE_OVERRIDES)
        set(signature "void func_${address}(ppu_context* ctx) {")
        string(FIND "${text}" "${signature}" found)
        if(NOT found EQUAL -1)
            string(REPLACE "${signature}" "void d2_original_${address}(ppu_context* ctx) {" text "${text}")
            set(changed TRUE)
            if(address IN_LIST D2_NATIVE_OVERRIDES)
                list(APPEND D2_NATIVE_FOUND ${address})
            else()
                list(APPEND D2_MOVIE_FOUND ${address})
            endif()
        endif()
    endforeach()


    d2_cheats_patch("${text}" text cheat_changed)
    if(cheat_changed)
        set(changed TRUE)
    endif()
    d2_appearance_patch("${text}" text appearance_changed)
    if(appearance_changed)
        set(changed TRUE)
    endif()
    if(changed)
        get_filename_component(name "${source}" NAME)
        set(copy "${CMAKE_CURRENT_BINARY_DIR}/d2_${name}")
        file(WRITE "${copy}" "${text}")
        list(REMOVE_ITEM RECOMP_SOURCES "${source}")
        list(APPEND RECOMP_SOURCES "${copy}")
    endif()
endforeach()
if(D2_APPEARANCE_WRAPPERS)
    get_property(D2_APPEARANCE_FOUND GLOBAL PROPERTY D2_APPEARANCE_FOUND)
    foreach(kind IN ITEMS unit_face unit_face_large unit_panel_face class_queued_face class_queued_face_alt ui_face_record ui_face_draw unit_list_face unit_list_face_alt unit_small_face unit_small_face_alt face_coordinates illustration select update write_body read_body record visual request category body_kind motion file_size allocate file_read load_update model_select texture_bind)
        if(NOT kind IN_LIST D2_APPEARANCE_FOUND)
            message(FATAL_ERROR "D2 appearance boundary '${kind}' missing from this lift")
        endif()
    endforeach()
endif()
get_property(D2_CHEAT_FOUND GLOBAL PROPERTY D2_CHEAT_FOUND)
foreach(kind IN ITEMS exp sp shop damage)
    if(NOT kind IN_LIST D2_CHEAT_FOUND)
        message(FATAL_ERROR "D2 cheat hook '${kind}' missing for ${D2_GAME_VERSION}; re-resolve this lift")
    endif()
endforeach()
list(LENGTH D2_MOVIE_FOUND D2_MOVIE_COUNT)
if(NOT D2_MOVIE_COUNT EQUAL 3)
    message(FATAL_ERROR "D2 movie facade changed; expected three BLUS31313 definitions")
endif()
if(NOT "${D2_NATIVE_FOUND}" STREQUAL "${D2_NATIVE_OVERRIDES}")
    message(FATAL_ERROR "D2 native overrides '${D2_NATIVE_OVERRIDES}' not all found in this lift")
endif()
set(D2_NATIVE_SOURCES)
if(D2_NATIVE_OVERRIDES)
    set(D2_NATIVE_SOURCES src/d2_fill.cpp src/d2_shader.cpp)
endif()

# ---------------------------------------------------------------------------
# ps3recomp runtime library
# ---------------------------------------------------------------------------
# Built from source as a subdirectory rather than imported as a prebuilt
# archive. A static library carries none of its own link requirements, so an
# imported one arrives without SDL2, pthreads, the Metal frameworks or the
# Winsock/XInput imports -- the link then fails on every platform for a
# different reason. add_subdirectory hands all of that over with the target.
add_subdirectory("${PS3RECOMP_DIR}" "${CMAKE_BINARY_DIR}/ps3recomp_sdk"
                 EXCLUDE_FROM_ALL)
include(graphics/integrate.cmake)

# ---------------------------------------------------------------------------
# The HLE NID table
# ---------------------------------------------------------------------------
# Maps each firmware import NID onto the handler in the runtime that answers
# it. A port may keep a hand-curated one beside its lift; otherwise generate
# the canonical every-module table, which is what the toolkit's own boot
# harness uses.
if(EXISTS "${RECOMP_DIR}/ppu_hle_nids.cpp")
    set(HLE_NID_TABLE "${RECOMP_DIR}/ppu_hle_nids.cpp")
    message(STATUS "HLE NID table:   ${HLE_NID_TABLE}")
else()
    find_package(Python3 COMPONENTS Interpreter REQUIRED)
    set(HLE_NID_TABLE "${CMAKE_CURRENT_BINARY_DIR}/ppu_hle_nids.cpp")
    file(GLOB_RECURSE HLE_MODULE_SOURCES CONFIGURE_DEPENDS
         "${PS3RECOMP_DIR}/libs/*.c")
    add_custom_command(
        OUTPUT "${HLE_NID_TABLE}"
        COMMAND "${Python3_EXECUTABLE}"
                "${PS3RECOMP_DIR}/tools/gen_hle_nids.py"
                --out "${HLE_NID_TABLE}" --all
        DEPENDS "${PS3RECOMP_DIR}/tools/gen_hle_nids.py"
                "${PS3RECOMP_DIR}/tools/nid_database.py"
                ${HLE_MODULE_SOURCES}
        COMMENT "Generating the HLE NID -> handler table")
    message(STATUS "HLE NID table:   generated (every module)")
endif()

# ---------------------------------------------------------------------------
# The game executable
# ---------------------------------------------------------------------------
# The PPU boot scaffold is compiled here rather than taken from the runtime
# library: every one of these files #includes the lifter's per-game
# ppu_recomp.h, so they can only be built against a particular lift.
set(PS3RECOMP_SCAFFOLD
    "${PS3RECOMP_DIR}/runtime/ppu/ppu_loader.cpp"
    "${PS3RECOMP_DIR}/runtime/ppu/ppu_hle.cpp"
    "${PS3RECOMP_DIR}/runtime/ppu/ppu_fs.cpp"
    "${PS3RECOMP_DIR}/runtime/ppu/ppu_sysprx.cpp"
)
if(D2_ENABLE_ISO_VFS)
    include(vfs/integrate.cmake)
endif()

# ---------------------------------------------------------------------------
# Lifted SPU images (optional)
# ---------------------------------------------------------------------------
# A title that runs SPU work gets its images lifted into <SPU_DIR>/<image>/ by
# tools/spu_lifter.py, with a registration TU generated by
# tools/build_spu_workloads.py. cellSpurs dispatches them by content
# fingerprint, so the generated table is keyed to the images THIS build ships:
# a table generated from another release registers signatures that can never
# match, which is worse than shipping none. Both the table and the job-module
# registry self-register from constructors and are therefore optional -- a tree
# that has not lifted its SPU images yet configures and links without them.

option(PORT_ENABLE_SPU "Compile and register the title's lifted SPU images" ON)

set(SPU_SOURCES "")
if(PORT_ENABLE_SPU)
    file(GLOB SPU_LIFTED CONFIGURE_DEPENDS "${SPU_DIR}/*/spu_recomp.c")
    if(SPU_LIFTED)
        list(LENGTH SPU_LIFTED SPU_IMAGE_COUNT)
        message(STATUS "Lifted SPU:      ${SPU_IMAGE_COUNT} image(s)")
        # The musttail indirect-branch fast path. Excluded from the runtime
        # library on purpose (see the SDK root CMakeLists): it must be built
        # here, by clang, or the tail call degrades to a plain call and a guest
        # loop that branches indirectly walks the thread stack off its guard
        # page a few thousand iterations in.
        list(APPEND SPU_SOURCES ${SPU_LIFTED}
             "${PS3RECOMP_DIR}/runtime/spu/spu_dispatch_mt.c")
        # Generated beside the lift; each is present only once its generator
        # has been run, so each is included only if it is actually here.
        foreach(gen IN ITEMS spu_workloads.c jobmods_register.c taskset_policy_bytes.c)
            if(EXISTS "${SPU_DIR}/${gen}")
                list(APPEND SPU_SOURCES "${SPU_DIR}/${gen}")
            else()
                message(STATUS "  (no ${gen} -- not registered)")
            endif()
        endforeach()
    else()
        message(STATUS "Lifted SPU:      none found in ${SPU_DIR}")
    endif()
endif()

add_executable(${PROJECT_NAME}
    main.cpp
    stubs.cpp
    src/d2_psn.cpp
    src/d2_movie.cpp
    src/d2_shader_trace.cpp
    src/d2_draw_trace.cpp
    src/d2_audio.cpp
    src/d2_debug_warp.cpp
    src/d2_cheats.cpp
    src/d2_items.cpp
    ${D2_NATIVE_SOURCES}
    ${PS3RECOMP_SCAFFOLD}
    ${HLE_NID_TABLE}
    ${RECOMP_SOURCES}
    ${SPU_SOURCES}
)
if(D2_APPEARANCE_WRAPPERS)
    target_sources(${PROJECT_NAME} PRIVATE src/d2_appearance_trace.cpp)
endif()

# Embed the asset-free, versioned editor schema so app bundles do not depend on cwd.
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/d2_cheats.v1.json" D2_CHEATS_JSON)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/d2_cheats.v1.json")
find_path(D2_JSON_INCLUDE_DIR nlohmann/json.hpp REQUIRED)
configure_file(src/d2_cheats_data.h.in d2_cheats_data.h @ONLY)
set_source_files_properties(src/d2_cheats.cpp PROPERTIES INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR};${D2_JSON_INCLUDE_DIR}" COMPILE_DEFINITIONS "D2_CHEATS_VERSION=${D2_GAME_VERSION}")
if(D2_APPEARANCE_IMPORTER)
    set_source_files_properties(src/d2_appearance_trace.cpp PROPERTIES
        INCLUDE_DIRECTORIES "${D2_JSON_INCLUDE_DIR}" COMPILE_DEFINITIONS D2_APPEARANCE_IMPORTER=1)
endif()

# The lifted SPU sources use bare #include "spu_context.h". Scope that include
# directory (and their flags) to those TUs alone: adding it to the whole target
# changes the compile command of every lifted PPU TU, and those are large enough
# that the needless rebuild is measured in tens of minutes.
#   SPU_USE_DISPATCH_MT -- route SPU_IB_DISPATCH through the musttail path.
#   -O2                 -- the target-wide -O1 exists for the huge PPU TUs; the
#                          SPU sources are small and decode-hot, so they get
#                          full optimization. The last -O on the line wins.
if(SPU_SOURCES)
    set_source_files_properties(${SPU_SOURCES} PROPERTIES
        INCLUDE_DIRECTORIES "${PS3RECOMP_DIR}/runtime/spu"
        COMPILE_DEFINITIONS SPU_USE_DISPATCH_MT)
    if(NOT MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        set_source_files_properties(${SPU_SOURCES} PROPERTIES COMPILE_OPTIONS "-O2")
    endif()
endif()

# app.rc carries the icon, the version block and the manifest, and needs a
# resource compiler. Listing it as a source on a host that has none is not a
# link error, it is a "no rule to make" for a file nothing can consume.
if(WIN32 AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/app.rc")
    target_sources(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/app.rc")
endif()

target_compile_definitions(${PROJECT_NAME} PRIVATE D2_GAME_VERSION=${D2_GAME_VERSION})
if(D2_ENABLE_ISO_VFS)
    target_compile_definitions(${PROJECT_NAME} PRIVATE D2_ENABLE_ISO_VFS=1)
    target_link_libraries(${PROJECT_NAME} PRIVATE d2_iso_vfs)
    if(WIN32)
        target_link_libraries(${PROJECT_NAME} PRIVATE shell32)
    endif()
endif()

target_include_directories(${PROJECT_NAME} PRIVATE
    "${RECOMP_DIR}"                            # the generated ppu_recomp.h
    "${PS3RECOMP_DIR}/include"
    "${PS3RECOMP_DIR}/runtime/ppu"
    "${PS3RECOMP_DIR}/runtime/spu"
    "${PS3RECOMP_DIR}/runtime/memory"
    "${PS3RECOMP_DIR}/runtime/syscalls"
    "${PS3RECOMP_DIR}/runtime/platform"        # win32_compat.h and friends
    "${PS3RECOMP_DIR}/libs/video"
    "${PS3RECOMP_DIR}/libs/system"
    "${CMAKE_CURRENT_SOURCE_DIR}"
)

target_link_libraries(${PROJECT_NAME} PRIVATE
    ps3recomp_runtime
)

# The runtime is C++, and on Apple also Objective-C. Letting CMake infer the
# link language from the generated C would leave the C++ standard library out.
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)

# Windows system import libraries. dbghelp symbolizes crash-handler addresses
# against the PDB, user32 and gdi32 are what the backend's window and its GDI
# present are made of, and winmm is timeBeginPeriod. None of the four names
# anything a POSIX linker can find; the D3D12 backend's own imports come in
# through the #pragma comment(lib) in libs/video/rsx_d3d12_backend.c.
if(WIN32)
    target_link_libraries(${PROJECT_NAME} PRIVATE
        dbghelp
        user32
        gdi32
        winmm
    )
endif()

# ---------------------------------------------------------------------------
# Compiler-specific flags
# ---------------------------------------------------------------------------
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    target_compile_options(${PROJECT_NAME} PRIVATE
        -Wall -Wextra -Wno-unused-parameter
        -fno-strict-aliasing
    )
elseif(MSVC)
    target_compile_options(${PROJECT_NAME} PRIVATE
        /W3 /wd4100 /wd4244 /wd4267
        /Zc:__cplusplus         # report correct __cplusplus value
        # ppu_recomp.h defines PPU_FENCE, the lowering of the guest's
        # sync/lwsync/eieio/isync, and in C that is <stdatomic.h>. The lifter
        # writes C++ by default, but --single-file writes ppu_recomp.c, and a
        # port that took it would not compile without this. The runtime's own
        # build already carries the same flag for the same reason.
        /experimental:c11atomics
    )
    target_compile_definitions(${PROJECT_NAME} PRIVATE
        _CRT_SECURE_NO_WARNINGS
    )
endif()

# The lifted translation units are machine-generated and can run to hundreds of
# thousands of lines each, so they get their own flags:
#
#   - warnings off. They are about generated code and nobody will act on them,
#     and at this volume they bury the diagnostics that matter.
#   - no floating-point contraction. arm64 fuses a multiply and an add by
#     default; a PPC fmadd is spelt as one where the lifter means one.
#   - /bigobj on MSVC. A single lifted file routinely passes the 65,536-section
#     limit of the default object format.
if(MSVC)
    set_source_files_properties(${RECOMP_SOURCES} PROPERTIES
        COMPILE_OPTIONS "/w;/bigobj")
else()
    set_source_files_properties(${RECOMP_SOURCES} PROPERTIES
        COMPILE_OPTIONS "-w;-ffp-contract=off")
endif()

# Instrument the host runtime/scaffold, keeping generated guest code out of
# the instrumentation overhead. PPU memory accesses go through the scaffold.
option(D2_HOST_SANITIZERS "Enable host AddressSanitizer and UndefinedBehaviorSanitizer" OFF)
if(D2_HOST_SANITIZERS)
    foreach(target IN ITEMS ps3recomp_runtime ${PROJECT_NAME})
        target_compile_options(${target} PRIVATE
            -fsanitize=address,undefined -fno-omit-frame-pointer -gline-tables-only)
    endforeach()
    target_link_options(${PROJECT_NAME} PRIVATE -fsanitize=address,undefined)
    set_property(SOURCE ${RECOMP_SOURCES} ${SPU_LIFTED} APPEND PROPERTY
        COMPILE_OPTIONS -fno-sanitize=address,undefined)
endif()

# ---------------------------------------------------------------------------
# Install
# ---------------------------------------------------------------------------
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)

if(APPLE)
    include(src/d2_bundle.cmake)
    target_sources(${PROJECT_NAME} PRIVATE src/d2_flags.cpp src/d2_flags.m)
    # AT: native Item Editor window (bridge in src/d2_items.cpp).
    target_sources(${PROJECT_NAME} PRIVATE src/d2_item_editor.m)
    set_source_files_properties(src/d2_item_editor.m PROPERTIES COMPILE_OPTIONS "-fobjc-arc")
    set_source_files_properties(src/d2_flags.m PROPERTIES COMPILE_OPTIONS "-fobjc-arc"
        INCLUDE_DIRECTORIES "${PS3RECOMP_DIR}/libs/video")
endif()

include(CTest)
if(BUILD_TESTING)
    add_subdirectory(tests)
endif()

# D2_LINK_MAP_V1: symbol report only; keep all compile/optimization flags.
if(WIN32 AND MSVC AND TARGET DisgaeaD2Recomp)
    target_link_options(DisgaeaD2Recomp PRIVATE "/MAP:${CMAKE_BINARY_DIR}/DisgaeaD2Recomp.map")
endif()

# D2_SPU_SSE41_V1: enable the existing SIMD helpers in lifted SPU TUs only.
option(D2_SPU_SIMD_SSE41 "Use existing SSE4.1 helpers for Disgaea 1.40 SPU code" OFF)
if(D2_SPU_SIMD_SSE41)
    if(NOT WIN32 OR NOT CMAKE_C_COMPILER_ID MATCHES "Clang" OR NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
        message(FATAL_ERROR "D2_SPU_SIMD_SSE41 expects Windows x64 clang-cl")
    endif()
    get_target_property(d2_spu_target_sources DisgaeaD2Recomp SOURCES)
    set(d2_spu_simd_count 0)
    foreach(d2_spu_source IN LISTS d2_spu_target_sources)
        string(REPLACE "\\" "/" d2_spu_norm "${d2_spu_source}")
        if(d2_spu_norm MATCHES "/spu-140/.*\\.(c|cpp)$")
            set_property(SOURCE "${d2_spu_source}" APPEND PROPERTY COMPILE_OPTIONS "-msse4.1")
            math(EXPR d2_spu_simd_count "${d2_spu_simd_count}+1")
        endif()
    endforeach()
    if(d2_spu_simd_count EQUAL 0)
        message(FATAL_ERROR "No lifted spu-140 C/C++ source found; use D2_SPU_SIMD_SSE41=OFF to restore")
    endif()
    message(STATUS "D2 SPU SSE4.1: ${d2_spu_simd_count} lifted source(s); PPU/runtime flags preserved")
endif()
