#############################################################################
#
#  Copyright 2010 - 2016  Richard Hacker (lerichi at gmx dot net)
#            2024 - 2025  Florian Pose <fp@igh.de>
#
#  This file is part of the pdserv library.
#
#  The pdserv library is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or (at your
#  option) any later version.
#
#  The pdserv library is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
#  License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with the pdserv library. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
#
#  This is the main cmake file for building process data communications server
#
#  The following options can be specified on the command line of cmake to
#  change the installation paths (Defaults are in <>)
#
#       -DCMAKE_INSTALL_PREFIX=</usr/local>
#       -DCMAKE_INSTALL_SYSCONFDIR=<${CMAKE_INSTALL_PREFIX}/etc>
#       -DCMAKE_INSTALL_INCLUDEDIR=<${CMAKE_INSTALL_PREFIX}/include>
#       -DCMAKE_INSTALL_BINDIR=<${CMAKE_INSTALL_PREFIX}/bin>
#       -DCMAKE_INSTALL_LIBDIR=<${CMAKE_INSTALL_PREFIX}/lib>
#       -DPAM_SERVICE_DIR=<unset> # optional, for installing SASL pam service
#
#       -DENABLE_EXAMPLE=<0>      # Compile examples
#       -DENABLE_DOC=<1>          # Enable documentattion if Doxygen is found
#
#  For debugging, use
#
#       -DCMAKE_BUILD_TYPE=Debug|Release|RelWithDebInfo|MinSizeRel
#       -DDEBUG=1               # prints debug messages in terminal
#       -DENABLE_TEST=1         # Compile tests (Requires
#                               #     -DCMAKE_BUILD_TYPE=Debug)
#
#  The git commit hash is included in the library and updated on every `make`
#  invocation. To supply your own hash, use
#
#       -DVERSION_HASH=3.2.3.10.gc0ffee+
#
##############################################################################

CMAKE_MINIMUM_REQUIRED (VERSION 3.2)

#
# Release Instructions
#
# - Update semantic version below
# - Update NEWS.md file
# - commit
# - add tag x.x.x
#
PROJECT (pdserv VERSION 3.4.1 LANGUAGES C CXX)

SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)

# Create compile_commands.json file to support modern editors
SET (CMAKE_EXPORT_COMPILE_COMMANDS ON)

LIST (APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/")

INCLUDE (GNUInstallDirs)
INCLUDE (CMakeDependentOption)
INCLUDE (InstallRequiredSystemLibraries)
INCLUDE (CMakePackageConfigHelpers)

ENABLE_TESTING()

# Set up pthreads
SET (CMAKE_THREAD_PREFER_PTHREAD TRUE)
FIND_PACKAGE (Threads REQUIRED)

# Require CommonC++
FIND_PACKAGE (Log4cplus REQUIRED)
FIND_PACKAGE (Yaml REQUIRED)
FIND_PACKAGE (BerkeleyDB REQUIRED)
FIND_PACKAGE (CyrusSASL REQUIRED)
FIND_PACKAGE (GnuTLS)

IF (UNIX)
    ADD_CUSTOM_TARGET (tags ctags -R ${CMAKE_CURRENT_SOURCE_DIR})
    ADD_CUSTOM_TARGET (ctags DEPENDS tags)
ENDIF ()

IF (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
        AND NOT ${CMAKE_VERSION} VERSION_LESS "3")
    ADD_COMPILE_OPTIONS (-Wall -Wextra)
ENDIF ()

ADD_SUBDIRECTORY (src)
FIND_PACKAGE (Doxygen)

OPTION (ENABLE_DOC "Generate documentation" ON)
IF (DOXYGEN_FOUND AND ENABLE_DOC)
    ADD_SUBDIRECTORY (doc)
ENDIF()

STRING (COMPARE EQUAL "${CMAKE_BUILD_TYPE}" "Debug" DEBUGON)
CMAKE_DEPENDENT_OPTION (ENABLE_TEST "Compile test subdirectory" ON
    "DEBUGON" OFF)
IF (ENABLE_TEST)
    ADD_SUBDIRECTORY (test)
ENDIF ()

OPTION (ENABLE_EXAMPLE "Compile examples subdirectory" OFF)
IF (ENABLE_EXAMPLE)
    ADD_SUBDIRECTORY (example)
ENDIF ()

OPTION ( DEBUG "Debugging output" OFF )
SET  (PDS_DEBUG ${DEBUG})

if (DEBUG)
    string(LENGTH "${PROJECT_SOURCE_DIR}/src/" SRC_PATH_LENGTH)
endif (DEBUG)

CONFIGURE_FILE (
    "${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h"
    )

CONFIGURE_FILE (
    "${PROJECT_SOURCE_DIR}/include/pdserv.h.in"
    "${PROJECT_BINARY_DIR}/include/pdserv.h"
    )

INSTALL (FILES "${PROJECT_BINARY_DIR}/include/pdserv.h"
    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
    )

set (CONFFILE pdserv.conf)
IF (NOT EXISTS ${CMAKE_INSTALL_FULL_SYSCONFDIR}/${CONFFILE})
    INSTALL (FILES "${CMAKE_CURRENT_SOURCE_DIR}/${CONFFILE}"
        DESTINATION "${CMAKE_INSTALL_FULL_SYSCONFDIR}"
        )
ENDIF ()

IF (PAM_SERVICE_DIR)
    INSTALL (FILES "${CMAKE_CURRENT_SOURCE_DIR}/pam/pdserv"
        DESTINATION "${PAM_SERVICE_DIR}"
        )
ENDIF ()

# Custom release make target
IF (EXISTS "${PROJECT_SOURCE_DIR}/scripts")
    CONFIGURE_FILE (
        "${PROJECT_SOURCE_DIR}/scripts/release.sh.in"
        "${PROJECT_BINARY_DIR}/release.sh"
        )
    ADD_CUSTOM_TARGET (dist COMMAND sh ./release.sh)
ENDIF ()
