# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2025 ai-ad4 and the FreePDFEditor contributors
cmake_minimum_required(VERSION 3.28)

project(FreePDFEditor
    VERSION 0.1.0.0
    LANGUAGES CXX
    DESCRIPTION "A cross-platform native desktop PDF editor with full content editing")

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

# --- Toolchain requirements -------------------------------------------------
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_SCAN_FOR_MODULES OFF) # we use CMake 3.28+ but no C++20 modules yet

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# --- Output layout ----------------------------------------------------------
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")

# Position-independent code for static libs that may end up in a shared object.
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Version string used by the UI process for the About box and --version.
set(FREEPDFEDITOR_VERSION_STRING
    "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}"
    CACHE STRING "Human-readable application version" FORCE)
add_compile_definitions(FREEPDFEDITOR_VERSION_STRING="${FREEPDFEDITOR_VERSION_STRING}")

# --- Hardening (applied to all targets via FREEPDFEDITOR_TARGET_OPTIONS) ----
include(FreePDFEditorHardening)

# --- vcpkg manifest mode -----------------------------------------------------
# The vcpkg manifest (vcpkg.json) and the pinned baseline (vcpkg-configuration.json)
# drive all third-party dependencies. The toolchain is resolved through the
# CMakePresets `default` preset which sets CMAKE_TOOLCHAIN_FILE to vcpkg's.
find_package(Threads REQUIRED)

# --- Options ----------------------------------------------------------------
option(FREEPDFEDITOR_BUILD_TESTS "Build the unit and gate tests" ON)
option(FREEPDFEDITOR_BUILD_SPIKES "Build the M0 feasibility spike harnesses" ON)
option(FREEPDFEDITOR_ENABLE_LTO "Enable link-time optimization on Release builds" OFF)
option(FREEPDFEDITOR_TREAT_WARNINGS_AS_ERRORS "Treat compiler warnings as errors on CI" OFF)

if(FREEPDFEDITOR_ENABLE_LTO)
    include(CheckIPOSupported)
    check_ipo_supported(RESULT lpo_supported OUTPUT lpo_error)
    if(lpo_supported)
        set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
    else()
        message(WARNING "LTO requested but not supported: ${lpo_error}")
    endif()
endif()

# --- Source tree ------------------------------------------------------------
add_subdirectory(src)

if(FREEPDFEDITOR_BUILD_SPIKES)
    add_subdirectory(spike)
endif()

if(FREEPDFEDITOR_BUILD_TESTS)
    enable_testing()
    add_subdirectory(test)
endif()

# --- CPack packaging (skeleton) ---------------------------------------------
include(FreePDFEditorPackaging)

message(STATUS "FreePDFEditor ${PROJECT_VERSION} — ${CMAKE_BUILD_TYPE} — ${CMAKE_SYSTEM_NAME}")