##############################################################################
#
#  Copyright (C) 2015-2016  Richard Hacker (lerichi at gmx dot net)
#
#  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
#
##############################################################################

CMAKE_MINIMUM_REQUIRED(VERSION 3.2)

PROJECT(pdcom VERSION 5.0.0)

# Release Instructions
#
# - Update version numbers below
# - Update SOVERSION below
# - Update ChangeLog
# - Update NEWS file
# - commit
# - make dist
# - add tag release-x.x.x
#

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

# library version
SET (SOVERSION 1)

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

INCLUDE (GNUInstallDirs)
INCLUDE (CMakePackageConfigHelpers)

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()

FIND_PACKAGE(EXPAT REQUIRED)

FIND_PACKAGE (Doxygen)
IF (DOXYGEN_FOUND)
    ADD_SUBDIRECTORY (doc)
ENDIF()

IF (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
    ADD_DEFINITIONS(-Wall -Wextra)
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)

INCLUDE(CTest)
IF(BUILD_TESTING)
    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")
