cmake_minimum_required(VERSION 3.25)

# ---------------------------------------------------------------------------
# Dependency-pinning guard (Rule: pin all dependency versions on Day 1).
# We refuse to configure until vcpkg.json carries a 'builtin-baseline', which
# scripts/bootstrap adds via `vcpkg x-update-baseline --add-initial-baseline`.
# ---------------------------------------------------------------------------
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg.json" _pdfengine_vcpkg_json)
string(JSON _pdfengine_baseline ERROR_VARIABLE _pdfengine_baseline_err
       GET "${_pdfengine_vcpkg_json}" "builtin-baseline")
if(_pdfengine_baseline_err OR NOT _pdfengine_baseline)
    message(FATAL_ERROR
        "vcpkg.json has no 'builtin-baseline' — the dependency registry is not "
        "pinned.\n"
        "Run the bootstrap script first:\n"
        "    Windows:  pwsh scripts/bootstrap.ps1\n"
        "    Unix:     ./scripts/bootstrap.sh\n"
        "It installs vcpkg and runs 'vcpkg x-update-baseline --add-initial-baseline'.")
endif()

project(PdfEngine
    VERSION 0.1.0
    DESCRIPTION "Cross-platform PDF rendering and editing SDK"
    HOMEPAGE_URL "https://example.invalid/pdf-engine"
    LANGUAGES CXX)

# ---------------------------------------------------------------------------
# Global C++ settings — C++23, no compiler extensions (engine blueprint §1.3).
# ---------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# compile_commands.json — consumed by clang-tidy and editors.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Single output dirs so test binaries find imported shared libs at runtime.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# Only the top-level project drives testing/install defaults.
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
    message(FATAL_ERROR "In-source builds are not allowed. Use a preset: "
                        "cmake --preset <platform>-debug")
endif()

# ---------------------------------------------------------------------------
# Build options.
# ---------------------------------------------------------------------------
option(PDFENGINE_BUILD_TESTS      "Build engine unit/smoke tests"                ON)
option(PDFENGINE_ENABLE_SANITIZERS "Build with AddressSanitizer/UBSan"           OFF)
option(PDFENGINE_WARNINGS_AS_ERRORS "Treat compiler warnings as errors"          OFF)
option(PDFENGINE_WITH_PDFIUM      "Link the PDFium static lib (build it first)"  OFF)

include(CompilerWarnings)
include(Sanitizers)
include(pdfium)        # defines pdfium::pdfium when PDFENGINE_WITH_PDFIUM is ON

# ---------------------------------------------------------------------------
# Third-party dependencies (resolved by vcpkg via the manifest).
# ---------------------------------------------------------------------------
find_package(freetype  CONFIG REQUIRED)
find_package(harfbuzz  CONFIG REQUIRED)
find_package(spdlog    CONFIG REQUIRED)

if(PDFENGINE_BUILD_TESTS)
    find_package(GTest CONFIG REQUIRED)
    enable_testing()
    include(GoogleTest)
endif()

# ---------------------------------------------------------------------------
# Subprojects.
# ---------------------------------------------------------------------------
add_subdirectory(engine)

# bindings/, gateway/, frontend/, wasm/ are placeholders in Phase 0 and are not
# wired into the build yet.

message(STATUS "PdfEngine ${PROJECT_VERSION} configured")
message(STATUS "  C++ standard ......... ${CMAKE_CXX_STANDARD}")
message(STATUS "  Build tests .......... ${PDFENGINE_BUILD_TESTS}")
message(STATUS "  Sanitizers ........... ${PDFENGINE_ENABLE_SANITIZERS}")
message(STATUS "  Warnings as errors ... ${PDFENGINE_WARNINGS_AS_ERRORS}")
message(STATUS "  Link PDFium .......... ${PDFENGINE_WITH_PDFIUM}")
