cmake_minimum_required(VERSION 3.20)
project(linux_runtime_acceptance LANGUAGES C CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
enable_testing()

# Instrument every linked helper as well as the production archive and probes.
# Configure-only ABI symbol objects remain metadata, outside this runtime claim.
add_compile_options(-UNDEBUG -fsanitize=address,undefined -fno-omit-frame-pointer -fno-pie)
add_link_options(-fsanitize=address,undefined -no-pie)

# The operator explicitly opts into these acceptance companions. They use the
# single production runtime created by linux_ratchets below.
option(F02_BUILD_SENDER "Build the actual delivery-postage sender companion" OFF)
option(TRAIL_MATE_INCOMING_WORKER_TESTS "Build the actual incoming worker companion" OFF)

add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../linux_ratchets" linux_ratchets)
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../linux_delivery_postage" delivery_postage)
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../linux_incoming_durability" incoming_durability)
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../linux_ticket_vertical" ticket_vertical)

# Reuse the single runtime archive for the existing radio/Worker fixture. This
# finite selection exercises real carrier ownership and per-radio Link MTU.
option(TRAIL_MATE_RADIO_IFAC_TESTS "Build the existing radio IFAC/Worker companion" OFF)
if(TRAIL_MATE_RADIO_IFAC_TESTS)
    get_filename_component(RADIO_IFAC_REPO "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE)
    set(RADIO_ANNOUNCE_FIXTURE "${RADIO_IFAC_REPO}/tests/reticulum_conformance/fixtures/microreticulum_announce_vectors.json")
    set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${RADIO_ANNOUNCE_FIXTURE}")
    file(READ "${RADIO_ANNOUNCE_FIXTURE}" RADIO_ANNOUNCE_JSON)
    string(JSON RADIO_FIRST_ANNOUNCE GET "${RADIO_ANNOUNCE_JSON}" vectors 0 raw_hex)
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/reticulum_announce_first.inc" "\"${RADIO_FIRST_ANNOUNCE}\"\n")
    string(JSON RADIO_ANNOUNCE_COUNT LENGTH "${RADIO_ANNOUNCE_JSON}" vectors)
    math(EXPR RADIO_ANNOUNCE_LAST "${RADIO_ANNOUNCE_COUNT} - 1")
    foreach(index RANGE 0 ${RADIO_ANNOUNCE_LAST})
        string(JSON RADIO_ANNOUNCE_NAME GET "${RADIO_ANNOUNCE_JSON}" vectors ${index} name)
        if(RADIO_ANNOUNCE_NAME STREQUAL "microreticulum_direct_ratchet_announce")
            string(JSON RADIO_CONTACT_ANNOUNCE GET "${RADIO_ANNOUNCE_JSON}" vectors ${index} raw_hex)
            file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/reticulum_announce_contact.inc" "\"${RADIO_CONTACT_ANNOUNCE}\"\n")
        endif()
    endforeach()
    if(NOT RADIO_CONTACT_ANNOUNCE)
        message(FATAL_ERROR "Missing original contact announce fixture")
    endif()
    add_executable(trailmate_linux_mesh_worker_test
        "${CMAKE_CURRENT_LIST_DIR}/../../../apps/linux_lvgl/tests/linux_mesh_worker_test.cpp")
    target_link_libraries(trailmate_linux_mesh_worker_test PRIVATE linux_ratchet_runtime)
    target_include_directories(trailmate_linux_mesh_worker_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
    add_executable(trailmate_linux_tms_document_test
        "${CMAKE_CURRENT_LIST_DIR}/../../../apps/linux_lvgl/tests/linux_tms_document_test.cpp")
    target_link_libraries(trailmate_linux_tms_document_test PRIVATE linux_ratchet_runtime)
endif()
