CMAKE_MINIMUM_REQUIRED (VERSION 2.8.5...3.26)
PROJECT (librepo C)

OPTION (ENABLE_TESTS "Build test?" ON)
OPTION (ENABLE_DOCS "Build docs?" ON)
OPTION (ENABLE_EXAMPLES "Build examples?" ON)
OPTION (WITH_ZCHUNK "Build with zchunk support" ON)
OPTION (ENABLE_PYTHON "Build Python bindings" ON)
OPTION (USE_GPGME "Use GpgMe (instead of rpm library) for OpenPGP key support" ON)
OPTION (USE_RUN_GNUPG_USER_SOCKET "Create a directory for gpg-agent socket in /run/gnugp/user (instead of /run/user)" OFF)
OPTION (ENABLE_SELINUX "Restore SELinux labels on GnuPG directories" ON)
OPTION (WITH_SANITIZERS "Build with address, leak and undefined sanitizers (DEBUG ONLY)" OFF)

INCLUDE (${CMAKE_SOURCE_DIR}/VERSION.cmake)
SET (VERSION "${LIBREPO_MAJOR}.${LIBREPO_MINOR}.${LIBREPO_PATCH}")

SET (CMAKE_C_FLAGS          "${CMAKE_C_FLAGS} -std=c99 -Wall")
SET (CMAKE_C_FLAGS_DEBUG    "${CMAKE_C_FLAGS} -ggdb -O0 -Wall -Wextra -DDEBUG")

IF(CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR CMAKE_BUILD_TYPE STREQUAL "debug"))
    MESSAGE("Build type is set do DEBUG!")
ENDIF()

# Set librepo log domain

add_definitions( -DG_LOG_DOMAIN="librepo" )

# Add path with own cmake modules

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

# Find necessare libraries

FIND_PACKAGE(PkgConfig)
PKG_CHECK_MODULES(GLIB2 glib-2.0>=2.66 gio-2.0 REQUIRED)
PKG_SEARCH_MODULE(LIBCRYPTO REQUIRED libcrypto openssl)
PKG_CHECK_MODULES(LIBXML2 libxml-2.0 REQUIRED)
FIND_PACKAGE(CURL 7.52.0 REQUIRED)

IF (USE_GPGME)
    PKG_SEARCH_MODULE(GPGME gpgme)
    IF (NOT GPGME_FOUND)
        FIND_PACKAGE(Gpgme REQUIRED)
        SET (GPGME_LIBRARIES "${GPGME_VANILLA_LIBRARIES}")
    ENDIF(NOT GPGME_FOUND)
    IF (ENABLE_SELINUX)
        PKG_CHECK_MODULES(SELINUX REQUIRED libselinux)
    ENDIF(ENABLE_SELINUX)
ELSE (USE_GPGME)
    PKG_CHECK_MODULES(RPM REQUIRED rpm>=4.18.0)

    INCLUDE(CheckSymbolExists)
    SET(CMAKE_REQUIRED_INCLUDES "${RPM_INCLUDE_DIRS}")
    SET(CMAKE_REQUIRED_LIBRARIES "${RPM_LIBRARIES}")

    # pgpPrtParams2 added after rpm 4.19.0-alpha
    CHECK_SYMBOL_EXISTS(pgpPrtParams2 rpm/rpmpgp.h HAVE_PGPPRTPARAMS2)
    IF (HAVE_PGPPRTPARAMS2)
        ADD_DEFINITIONS(-DHAVE_PGPPRTPARAMS2)
    ENDIF()

    # pgpVerifySignature2 added after rpm 4.19.0-alpha
    CHECK_SYMBOL_EXISTS(pgpVerifySignature2 rpm/rpmpgp.h HAVE_PGPVERIFYSIGNATURE2)
    IF (HAVE_PGPVERIFYSIGNATURE2)
        ADD_DEFINITIONS(-DHAVE_PGPVERIFYSIGNATURE2)
    ENDIF()

    UNSET(CMAKE_REQUIRED_INCLUDES)
    UNSET(CMAKE_REQUIRED_LIBRARIES)
ENDIF (USE_GPGME)

IF (USE_RUN_GNUPG_USER_SOCKET)
    SET (CMAKE_C_FLAGS          "${CMAKE_C_FLAGS} -DUSE_RUN_GNUPG_USER_SOCKET")
    SET (CMAKE_C_FLAGS_DEBUG    "${CMAKE_C_FLAGS_DEBUG} -DUSE_RUN_GNUPG_USER_SOCKET")
ENDIF (USE_RUN_GNUPG_USER_SOCKET)

IF (WITH_ZCHUNK)
    PKG_CHECK_MODULES(ZCHUNKLIB zck>=0.9.11 REQUIRED)
    SET (CMAKE_C_FLAGS          "${CMAKE_C_FLAGS} -DWITH_ZCHUNK")
    SET (CMAKE_C_FLAGS_DEBUG    "${CMAKE_C_FLAGS_DEBUG} -DWITH_ZCHUNK")
    SET (LIBREPO_ZCHUNK_ENABLED "1")
ELSE (WITH_ZCHUNK)
    SET (LIBREPO_ZCHUNK_ENABLED "0")
ENDIF (WITH_ZCHUNK)

INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})

# Enable large file support

ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE)

# Check libraries

IF (NOT LIBXML2_FOUND)
    MESSAGE(FATAL_ERROR "No libxml-2.0 library installed")
ENDIF (NOT LIBXML2_FOUND)

IF (NOT CURL_FOUND)
    MESSAGE(FATAL_ERROR "No CURL library installed")
ENDIF (NOT CURL_FOUND)

IF (WITH_SANITIZERS)
    MESSAGE(WARNING "Building with sanitizers enabled!")
    ADD_COMPILE_OPTIONS(-fsanitize=address -fsanitize=undefined -fsanitize=leak)
    LINK_LIBRARIES(asan ubsan)
ENDIF()

# Generate header files with the configured ABI (e.g. with/without zchunk).

configure_file("librepo/downloadtarget.h.in" "librepo/downloadtarget.h" @ONLY)
configure_file("librepo/handle.h.in" "librepo/handle.h" @ONLY)
configure_file("librepo/util.h.in" "librepo/util.h" @ONLY)
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/librepo")

# Add include dirs

INCLUDE_DIRECTORIES(${LIBXML2_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIR})
#INCLUDE_DIRECTORIES(${CHECK_INCLUDE_DIR})
IF (USE_GPGME AND ENABLE_SELINUX)
    INCLUDE_DIRECTORIES(${SELINUX_INCLUDE_DIRS})
    ADD_DEFINITIONS(-DENABLE_SELINUX=1)
ENDIF (USE_GPGME AND ENABLE_SELINUX)

include (GNUInstallDirs)
# Python stuff

IF (ENABLE_PYTHON)
    FIND_PACKAGE (PythonInterp 3 EXACT REQUIRED)
    FIND_PACKAGE (PythonLibs REQUIRED)
ENDIF (ENABLE_PYTHON)

# Subdirs

ADD_SUBDIRECTORY (librepo)

IF (ENABLE_TESTS)
  PKG_CHECK_MODULES(LIBCHECK REQUIRED check)
  ENABLE_TESTING()
  ADD_SUBDIRECTORY (tests)
ENDIF (ENABLE_TESTS)

IF (ENABLE_DOCS)
  ADD_SUBDIRECTORY (doc)
ENDIF (ENABLE_DOCS)

IF (ENABLE_EXAMPLES)
  ADD_SUBDIRECTORY (examples/c)
ENDIF (ENABLE_EXAMPLES)
