cmake_minimum_required( VERSION 3.20 )

project(fson VERSION 1.0.0.0 LANGUAGES CXX)

# ── Standalone vs embedded detection ─────────────────────────────────────────
# FSON_STANDALONE is ON when this CMakeLists is the top-level project and OFF
# when fson is consumed via add_subdirectory() from a host project (e.g. FFS).
# In embedded mode tests/examples default OFF, the build type is left to the
# host, and the CPack section is skipped so the host's packaging is untouched.
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
    set(FSON_STANDALONE ON)
else()
    set(FSON_STANDALONE OFF)
endif()

option(TESTING          "Compile Test Targets"                    ${FSON_STANDALONE})
option(EXAMPLE          "Compile Example Targets"                 ${FSON_STANDALONE})
option(FSON_BUILD_TOOLS "Build the fson-check command-line tool"  ON)

# Install component for the runtime artefacts (libfson, libcparse, fson-check).
# A host project may override this so the libraries are staged and deployed
# together with its own component set (FFS sets it to "ffs-sdk").
set(FSON_INSTALL_COMPONENT "fson" CACHE STRING
    "CMake install component for fson runtime libraries and tools")

if(FSON_STANDALONE)
    find_program(CCACHE_PROGRAM ccache)
    if(CCACHE_PROGRAM)
        set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
    endif()
endif()

include(GNUInstallDirs)

set(CMAKE_VERBOSE_MAKEFILE OFF)
set(CMAKE_CXX_STANDARD 23)
if(FSON_STANDALONE AND NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()

# Setup gTest
if (${TESTING})
find_package(GTest QUIET)

if(${GTest_FOUND})
  enable_testing()
  include_directories(${GTEST_INCLUDE_DIR})
else()
  message(STATUS "GTest not found, disabling tests")
  set(TESTING OFF)
endif()
endif()

# ── cparse — Conan package (was the libs/external/cparse git submodule) ──────
# Resolved here, at a scope visible to every subdirectory. The fson tree links
# the bare target name `cparse`, so expose a GLOBAL alias over cparse::cparse.
find_package(cparse CONFIG REQUIRED)
if(NOT TARGET cparse)
    add_library(cparse INTERFACE IMPORTED GLOBAL)
    target_link_libraries(cparse INTERFACE cparse::cparse)
endif()

# The project

add_subdirectory(libs)
if(FSON_BUILD_TOOLS)
    add_subdirectory(apps)
endif()

if (${TESTING})
add_subdirectory(tests)
endif()

# API documentation (Doxygen). Standalone-only: the `docs` target name would
# collide inside an embedding host (FFS). No-op when Doxygen is absent.
if(FSON_STANDALONE)
    add_subdirectory(docs)
endif()


#
# installation package
#
configure_file(fson.pc.in ${CMAKE_CURRENT_BINARY_DIR}/fson.pc @ONLY)

# Runtime library — libfson.so. Since the conan migration, libcparse.so is a
# separate Conan package (cparse/*), deployed by Conan alongside libfson.so;
# fson installs only its own target here.
install(TARGETS fson
        LIBRARY
            COMPONENT   ${FSON_INSTALL_COMPONENT}
            DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/fson.pc
        COMPONENT ${FSON_INSTALL_COMPONENT}
        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

if(FSON_STANDALONE)
## create packages
set(CPACK_COMPONENTS_ALL ${CPACK_COMPONENTS_ALL} fson)

set(CPACK_PACKAGE_NAME fson)
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
set(CPACK_PACKAGE_RELEASE "1")
set(CPACK_PACKAGE_DESCRIPTION "A FSON parser utility")
set(CPACK_PACKAGE_VENDOR "Fehmi Demiralp (FEDEM)")
set(CPACK_PACKAGE_CONTACT "Fehmi Demiralp <demiralp@fedem.eu>")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_RELEASE}.${CMAKE_SYSTEM_PROCESSOR}")
set(CPACK_PACKAGING_INSTALL_DIRECTORY ${CMAKE_INSTALL_PREFIX})
set(CPACK_PACKAGE_HOMEPAGE_URL "https://fson.fedem.eu")
set(CPACK_PACKAGE_LICENSE "MIT")
set(CPACK_GENERATOR 7Z TGZ ZIP DEB RPM)

# RPM-specific settings
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_COMPONENT_CPARSE_DISPLAY_NAME "FSON Package")
set(CPACK_COMPONENT_CPARSE_DESCRIPTION "${CPACK_PACKAGE_DESCRIPTION}")

set(CPACK_RPM_CPARSE_PACKAGE_GROUP "Development/Tools")
set(CPACK_RPM_CPARSE_PACKAGE_LICENSE "MIT")
set(CPACK_RPM_CPARSE_PACKAGE_URL "https://fson.fedem.eu")
set(CPACK_RPM_CPARSE_PACKAGE_ARCHITECTURE "x86_64")
set(CPACK_RPM_CPARSE_PACKAGE_RELEASE ${CPACK_PACKAGE_RELEASE})
set(CPACK_RPM_CPARSE_PACKAGE_REQUIRES "gcc >= 12.0")

set(CPACK_ARCHIVE_CPARSE_FILE_NAME "C${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_RELEASE}.${CMAKE_SYSTEM_PROCESSOR}")
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)

# Debian-specific settings
set(CPACK_DEBIAN_PACKAGE_DEPENDS "")

include(CPack)
endif()  # FSON_STANDALONE
