cmake_minimum_required( VERSION 3.20 )

project( mstring VERSION 1.3.1.2
         DESCRIPTION "mstring - generates C++ message functions and classes from localized message definitions"
         LANGUAGES CXX )

# The message-definition grammar is a hand-written cparse parser (it was a
# flex/bison front end up to 1.0): the only build dependency is the cparse
# Conan package — `conan build .` (or `conan install .` before a plain CMake
# configure) provides it.

option(TESTING "Build and register the tests (ctest)" ON)
option(EXAMPLE "Build the examples (and their ctest smoke tests)" ON)
option(BENCH "Build the benchmark of the generated code (bench/)" OFF)

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

include(GNUInstallDirs)

# C++17 is enough for mstring and for the code it generates; a newer
# standard from the toolchain (Conan profile) is kept.
if(NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS 17)
    set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Debug)
endif()

find_package(cparse CONFIG REQUIRED)

add_subdirectory(src)

# mstring_generate(): run mstring at build time. The same module is installed
# for consumers (see README.md, "Using mstring from CMake").
include(cmake/mstring-generate.cmake)

# The tests and the example smoke tests are plain add_test() + a CMake diff
# script; enable_testing() must come before the add_subdirectory() calls.
if(TESTING OR EXAMPLE)
    enable_testing()
endif()

if(EXAMPLE AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/examples")
    add_subdirectory(examples)
endif()

if(TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests")
    add_subdirectory(tests)
endif()

if(BENCH AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/bench")
    add_subdirectory(bench)
endif()

# Doxygen `docs` target (README + docs/syntax.md + the sources); absent when
# Doxygen is not installed. ws-release.sh builds it for the site's /api.
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/docs/CMakeLists.txt")
    add_subdirectory(docs)
endif()

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

install(TARGETS mstring
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        COMPONENT mstring)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mstring.pc
        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig
        COMPONENT mstring)
# find_package(mstring) without Conan: a package config + version file next
# to the module (CMakeDeps generates its own for a Conan consumer).
include(CMakePackageConfigHelpers)
configure_package_config_file(cmake/mstringConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/mstringConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mstring
    PATH_VARS CMAKE_INSTALL_BINDIR)
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/mstringConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion)

install(FILES cmake/mstring-generate.cmake
              ${CMAKE_CURRENT_BINARY_DIR}/mstringConfig.cmake
              ${CMAKE_CURRENT_BINARY_DIR}/mstringConfigVersion.cmake
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mstring
        COMPONENT mstring)
install(FILES LICENSE README.md docs/syntax.md
        DESTINATION ${CMAKE_INSTALL_DOCDIR}
        COMPONENT mstring)

#
# packages
#
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
set(CPACK_PACKAGE_RELEASE "1")
set(CPACK_PACKAGE_DESCRIPTION "${PROJECT_DESCRIPTION}")
set(CPACK_PACKAGE_VENDOR "Fedem Software")
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_PACKAGE_LICENSE "MIT")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_GENERATOR TGZ RPM DEB)

set(CPACK_RPM_PACKAGE_LICENSE "MIT")
set(CPACK_RPM_PACKAGE_GROUP "Development/Tools")
set(CPACK_RPM_PACKAGE_RELEASE ${CPACK_PACKAGE_RELEASE})
set(CPACK_RPM_PACKAGE_REQUIRES "cparse >= 1.2")
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
    /usr/local /usr/local/bin /usr/local/lib64 /usr/local/lib64/cmake
    /usr/local/share /usr/local/share/doc /usr/local/share/pkgconfig)

set(CPACK_DEBIAN_PACKAGE_DEPENDS "cparse (>= 1.2)")

include(CPack)
