##############################################################################
#
#  Copyright (C) 2015-2016  Richard Hacker (lerichi at gmx dot net)
#                2021-2024  Bjarne von Horn (vh at igh dot de)
#                2021-2024  Florian Pose <fp@igh.de>
#
#  This file is part of the PdCom library.
#
#  The PdCom 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 PdCom 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 PdCom library. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
#
#  This is the main cmake file for building process data communications client
#
#  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_INCLUDEDIR=<${CMAKE_INSTALL_PREFIX}/include>
#       -DCMAKE_INSTALL_BINDIR=<${CMAKE_INSTALL_PREFIX}/bin>
#       -DCMAKE_INSTALL_LIBDIR=<${CMAKE_INSTALL_PREFIX}/lib>
#
#  To build the python bindings, navigate to the python directory
#  and run pip3 install .
#
#  To build example, use
#       -DEXAMPLE=1
#
#  For debugging, use
#
#       -DCMAKE_BUILD_TYPE=Debug|Release|RelWithDebInfo|MinSizeRel
#       -DDEBUG=1               # prints debug messages in terminal
#                                 when CMAKE_BUILD_TYPE=Debug
#
#  If you want to build the tests, use
#
#       -DBUILD_TESTING=1
#
#  The Doxygen documentation is built by default, if the doxygen executable is
#  found. It is necessary to update the submodules with the doxygen template
#  with 'git submodule update --init'. If you don't want to build the
#  documentation, use
#
#       -DBUILD_DOCS=0
#
#  The git commit hash is included in the library and updated on every `make`
#  invocation. To supply your own hash, use
#       -DVERSION_HASH=c0ffee
#
##############################################################################

CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

#
# Release Instructions
#
# - Update VERSION, SOVERSION and PDCOM_PY_EXTRA_VERSION below
# - Update the NEWS file
# - Commit
# - Add version tag x.x.x (simply the version without pre/postfix)
#

# Project and version
PROJECT(pdcom VERSION 5.3.2)

# Library version
SET(SOVERSION 2)

# Stuff to add to python package version
SET(PDCOM_PY_EXTRA_VERSION "")

SET(LIBNAME "${PROJECT_NAME}${PROJECT_VERSION_MAJOR}")

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

INCLUDE(GNUInstallDirs)
INCLUDE(CMakeDependentOption)
INCLUDE(CMakePackageConfigHelpers)
INCLUDE(GenerateExportHeader)

OPTION(BUILD_SHARED_LIBS "Build using shared libraries" ON)
OPTION(BUILD_POSITION_INDEPENDENT "Build position-independent code" ON)
OPTION(DEBUG "Debugging output when CMAKE_BUILD_TYPE=Debug" OFF)
IF(("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") AND (DEBUG))
    SET_DIRECTORY_PROPERTIES(PROPERTIES COMPILE_DEFINITIONS "PDC_DEBUG")
ENDIF()
OPTION(USE_SYMBOL_VERSIONING "Use ELF symbol versioning" OFF)
cmake_dependent_option(
    CHECK_SYMBOL_VERSIONING
    "Verify symbol versioning after build"
    ON
    "USE_SYMBOL_VERSIONING"
    OFF
)

FIND_PACKAGE(EXPAT REQUIRED)

# Test for XML_SetReparseDeferralEnabled() in expat, introduced in 2.6.0
TRY_COMPILE(HAVE_XML_SetReparseDeferralEnabled
    ${PROJECT_BINARY_DIR}
    ${CMAKE_SOURCE_DIR}/feature_test/test_XML_SetReparseDeferralEnabled.cpp
    LINK_LIBRARIES EXPAT::EXPAT
    )
MESSAGE(STATUS "Have XML_SetReparseDeferralEnabled(): ${HAVE_XML_SetReparseDeferralEnabled}")

IF(USE_SYMBOL_VERSIONING)
    FIND_PACKAGE(PythonInterp 3 REQUIRED)
ENDIF()


## build documentation by default, if doxygen is found

FIND_PACKAGE(Doxygen)
OPTION(BUILD_DOCS "Build documentation" ${DOXYGEN_FOUND})
IF(BUILD_DOCS)
    ADD_SUBDIRECTORY(doc)
ENDIF()

ADD_SUBDIRECTORY(src)

## install headers

IF(WIN32)
    INSTALL(DIRECTORY include/
        DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
        PATTERN "PosixProcess.h" EXCLUDE
    )
ELSE()
    INSTALL(DIRECTORY include/
        DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
    )
ENDIF()

OPTION(USE_SASL "Build SASL Components" ON)
IF (USE_SASL)
    ADD_SUBDIRECTORY(cyrus_sasl)
ENDIF()

FIND_PACKAGE(GnuTLS)
IF(GNUTLS_FOUND)
    ADD_SUBDIRECTORY(gnutls)
ENDIF()

# Python library (build and install with `cd python && pip3 install .`)
ADD_SUBDIRECTORY(python)

OPTION(BUILD_TESTING "Build unit tests" OFF)
IF(BUILD_TESTING)
    INCLUDE(CTest)
    ADD_SUBDIRECTORY(test)
ENDIF()

OPTION(EXAMPLE "Build example" OFF)
IF(EXAMPLE)
    ADD_SUBDIRECTORY(example)
ENDIF()

string(LENGTH "${PROJECT_SOURCE_DIR}/src/" SRC_PATH_LENGTH)
CONFIGURE_FILE( pdcom.h.in ${LIBNAME}.h)
CONFIGURE_FILE(libpdcom.pc.in libpdcom${PROJECT_VERSION_MAJOR}.pc @ONLY)

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

INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/libpdcom${PROJECT_VERSION_MAJOR}.pc"
    DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
