cmake_minimum_required(VERSION 3.20)
project(tomba2_source_profile LANGUAGES NONE)

# Configure-only wiring check: no compiler, assets, generation or runtime.
# Exercise the real title CMakeLists and shared implementation helper while
# replacing only heavyweight runtime target creation. Never build this probe.
if(NOT PSXRECOMP_ROOT)
    message(FATAL_ERROR "Pass -DPSXRECOMP_ROOT=<campaign framework checkout>")
endif()
include("${PSXRECOMP_ROOT}/cmake/psx_execution_profile.cmake")
set(CMAKE_C_COMPILE_OBJECT "${CMAKE_COMMAND} -E false")
set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_COMMAND} -E false")

function(project)
    _project(${ARGV0} LANGUAGES NONE)
endfunction()
set(BUILD_TESTING OFF CACHE BOOL "Probe does not compile tests" FORCE)
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/framework/runtime")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/framework/runtime/runtime.cmake" [=[
function(psxrecomp_add_runtime_target target)
    cmake_parse_arguments(P ""
        "GAME_GENERATED_DISPATCH_C;PRELOADED_MODS_DIR;DEBUG_PORT;WINDOW_TITLE;DEFAULT_GAME_CONFIG_PATH;LAUNCHER_BOXART"
        "EXTRAS_SOURCES;GAME_GENERATED_FULL_C" ${ARGN})
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${target}.c" "/* configure only */\n")
    add_executable(${target} "${CMAKE_CURRENT_BINARY_DIR}/${target}.c" ${P_EXTRAS_SOURCES})
    set_target_properties(${target} PROPERTIES LINKER_LANGUAGE C)
    psxrecomp_execution_profile(${target})
endfunction()
function(recomp_target_launcher_language)
endfunction()
]=])
set(PSXRECOMP_ROOT "${CMAKE_CURRENT_BINARY_DIR}/framework" CACHE PATH "Probe runtime" FORCE)
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../.." title)

foreach(_target psx-runtime psx-runtime-ita)
    get_target_property(_sources ${_target} SOURCES)
    set(_adapter 0)
    set(_prepare 0)
    set(_reference 0)
    foreach(_source IN LISTS _sources)
        if(_source MATCHES "tomba2_seamless[.]c$")
            math(EXPR _adapter "${_adapter}+1")
        elseif(_source MATCHES "tomba2_seamless_prepare[.]cpp$")
            math(EXPR _prepare "${_prepare}+1")
        elseif(_source MATCHES "tomba2_seamless_reference[.]c$")
            math(EXPR _reference "${_reference}+1")
        endif()
    endforeach()
    if(PSX_EXECUTION_PROFILE STREQUAL "ENHANCED")
        if(NOT _adapter EQUAL 1 OR NOT _prepare EQUAL 1 OR NOT _reference EQUAL 0)
            message(FATAL_ERROR "${_target}: enhanced family missing or linked twice")
        endif()
    elseif(NOT _adapter EQUAL 0 OR NOT _prepare EQUAL 0 OR NOT _reference EQUAL 1)
        message(FATAL_ERROR "${_target}: reference links a resident implementation")
    endif()
    get_target_property(_manifest ${_target} PSX_EXECUTION_MANIFEST)
    file(READ "${_manifest}" _json)
    string(JSON _name GET "${_json}" implementations 0 name)
    string(JSON _profile GET "${_json}" profile)
    if(NOT _name STREQUAL "tomba2-resident-loader" OR NOT _profile STREQUAL PSX_EXECUTION_PROFILE)
        message(FATAL_ERROR "${_target}: missing execution contract")
    endif()
endforeach()
