# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2025 ai-ad4 and the FreePDFEditor contributors
# spike/CMakeLists.txt
#
# M0 feasibility spikes (§14). Each spike is its own executable so it can be run
# independently and its results reported separately by CI. The spikes link only
# against the leaf libraries they exercise (QPDF here), not against Qt.

# Find QPDF. System packages (Debian libqpdf-dev) install the config under the
# multiarch lib dir; vcpkg installs it under its installed tree. Search both.
find_package(QPDF CONFIG QUIET
    HINTS /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} /usr/local /usr)

# Fall back to a pkg-config probe if the CMake config wasn't installed.
if(NOT QPDF_FOUND)
    find_package(PkgConfig QUIET)
    if(PkgConfig_FOUND)
        pkg_check_modules(QPDF libqpdf IMPORTED_TARGET)
    endif()
endif()

if(NOT QPDF_FOUND AND NOT TARGET PkgConfig::QPDF)
    message(STATUS
        "QPDF not found — Spike A (verbatim round-trip) will not be built. "
        "Install qpdf (vcpkg, system package, or from source) to enable it.")
    return()
endif()

# The QPDF CMake config exports `qpdf::libqpdf` (system packages, e.g. Debian's
# libqpdf-dev) and/or `QPDF::qpdf` (vcpkg). pkg-config yields PkgConfig::QPDF.
# Accept any of them.
if(TARGET qpdf::libqpdf)
    set(_fpe_qpdf_target qpdf::libqpdf)
elseif(TARGET QPDF::qpdf)
    set(_fpe_qpdf_target QPDF::qpdf)
elseif(TARGET PkgConfig::QPDF)
    set(_fpe_qpdf_target PkgConfig::QPDF)
else()
    message(WARNING "QPDF found but no known imported target — Spike A skipped.")
    return()
endif()

add_executable(spike_a_verbatim_roundtrip
    common/SpikeRunner.cpp
    common/SpikeRunner.h
    A_verbatim_roundtrip/main.cpp
    A_verbatim_roundtrip/Roundtrip.cpp
    A_verbatim_roundtrip/Roundtrip.h
)
target_include_directories(spike_a_verbatim_roundtrip PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(spike_a_verbatim_roundtrip PRIVATE ${_fpe_qpdf_target})
target_compile_features(spike_a_verbatim_roundtrip PRIVATE cxx_std_20)
freepdfeditor_apply_warnings(spike_a_verbatim_roundtrip)
freepdfeditor_apply_hardening(spike_a_verbatim_roundtrip)

# Spike C (hb-subset growth of an existing subset font) is left as a placeholder
# until HarfBuzz is wired in; it is exercised from a standalone harness in CI.
if(FALSE)
    find_package(harfbuzz CONFIG QUIET)
    if(harfbuzz_FOUND)
        add_executable(spike_c_subset_growth
            common/SpikeRunner.cpp
            common/SpikeRunner.h
            C_subset_growth/main.cpp
        )
        target_link_libraries(spike_c_subset_growth PRIVATE harfbuzz::harfbuzz harfbuzz::harfbuzz-subset)
    endif()
endif()