# CMake Config
cmake_minimum_required(VERSION 3.5.1)
project(sh4lt)

#
# CONFIGURATION
#

# Project Variables
set(SH4LT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(SH4LT_VERSION_MAJOR 0)
set(SH4LT_VERSION_MINOR 1)
set(SH4LT_VERSION_PATCH 8)
set(SH4LT_VERSION_STRING ${SH4LT_VERSION_MAJOR}.${SH4LT_VERSION_MINOR}.${SH4LT_VERSION_PATCH})
set(SH4LT_API_VERSION ${SH4LT_VERSION_MAJOR}.${SH4LT_VERSION_MINOR})
set(SH4LT_LIBRARY sh4lt-${SH4LT_API_VERSION})

# Package (pkg-config) Information
set(PACKAGE_NAME sh4lt)
set(PACKAGE_DESCRIPTION "Shares data flows between applications")
set(PACKAGE_URL "https://gitlab.com/sh4lt/sh4lt")
set(PACKAGE_VERSION ${SH4LT_VERSION_STRING})

# Compilation
set(CMAKE_CXX_STANDARD 20)


#
# Code coverage
#

set(TEST_COVERAGE OFF CACHE BOOL "Enable test coverage")
if (TEST_COVERAGE)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
  add_custom_command(OUTPUT test_coverage
    COMMAND mkdir -p ${CMAKE_BINARY_DIR}/coverage
    COMMAND lcov --no-external --capture --initial
                 --directory ${CMAKE_SOURCE_DIR}
                 --exclude '${CMAKE_SOURCE_DIR}/tests/*'
                 --exclude '${CMAKE_SOURCE_DIR}/build/*'
                 --output-file coverage/sh4lt_base.info
    COMMAND make test
    COMMAND lcov --no-external --capture
                 --directory ${CMAKE_SOURCE_DIR}
                 --exclude '${CMAKE_SOURCE_DIR}/tests/*'
                 --exclude '${CMAKE_SOURCE_DIR}/build/*'
                 --output-file coverage/sh4lt.info
    COMMAND lcov --add-tracefile ${CMAKE_BINARY_DIR}/coverage/sh4lt_base.info
                 --add-tracefile ${CMAKE_BINARY_DIR}/coverage/sh4lt.info
                 --output-file ${CMAKE_BINARY_DIR}/coverage/sh4lt_total.info
    COMMAND genhtml --output-directory ${CMAKE_BINARY_DIR}/coverage ${CMAKE_BINARY_DIR}/coverage/sh4lt_total.info
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    )
  add_custom_target(check_coverage DEPENDS test_coverage)
endif()  # TEST_COVERAGE 

# CPack - General
set(CPACK_PACKAGE_NAME ${PACKAGE_NAME})
set(CPACK_PACKAGE_VENDOR "Nicolas Bouillot")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PACKAGE_DESCRIPTION})
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md")
set(CPACK_PACKAGE_VERSION_MAJOR "${SH4LT_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${SH4LT_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${SH4LT_VERSION_PATCH}")
set(CPACK_GENERATOR DEB)
set(CPACK_SOURCE_GENERATOR TGZ)
set(CPACK_COMPONENTS_ALL lib dev dbg)

# CPack - Debian Package
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_DEBIAN_PACKAGE_NAME "${CPACK_PACKAGE_NAME}")
set(CPACK_DEBIAN_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}")
set(CPACK_DEBIAN_PROJECT_HOMEPAGE "https://gitlab.com/nicobou/sh4lt/")
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${CPACK_PACKAGE_VENDOR} <nbou7110t@gmail.com>")
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "Library to share streams of framed data between processes via shared memory. sh4lt is server less: it requires applications to link data streams using socket path (e.g. \"/tmp/my_sh4lt_stream\"). Sh4lt is very fast and allows processes to access data streams without the need of extra copy. The communication paradigm is 1 to many, i.e., one writer is making available data frames to several followers. Followers and writers can hot connect & disconnect.")
set(CPACK_DEBIAN_PACKAGE_SECTION "libs")
set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "")
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/README.md ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md ${CMAKE_CURRENT_SOURCE_DIR}/NEWS.md
  COMPONENT lib
  DESTINATION share/doc/${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}/)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/README.md ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md ${CMAKE_CURRENT_SOURCE_DIR}/NEWS.md
  COMPONENT dev
  DESTINATION share/doc/${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}/)

# CPack - Libraries
set(CPACK_COMPONENT_LIB_DISPLAY_NAME "Sh4lt libraries")
set(CPACK_COMPONENT_LIB_DESCRIPTION "Shared libraries, monitoring tool (sh4lt-flow)")

# CPack - Development
set(CPACK_COMPONENT_DEV_DISPLAY_NAME "Sh4lt development files and documentation")
set(CPACK_COMPONENT_DEV_DESCRIPTION "C++ Headers, utils and libraries")
set(CPACK_DEBIAN_DEV_PACKAGE_REPLACES lib)

# CPack - Debug
set(CPACK_COMPONENT_DBG_DISPLAY_NAME "Sh4lt development files and documentation")
set(CPACK_COMPONENT_DBG_DESCRIPTION "C++ Headers, utils and libraries with debug symbols")
set(CPACK_DEBIAN_DBG_PACKAGE_REPLACES dev)

# CPack - Source
set(CPACK_SOURCE_IGNORE_FILES "/\.hooks/;/build/;/html/;${CPACK_SOURCE_IGNORE_FILES}")

# Add local cmake directory to cmake module path in case we need it
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")

#
# INCLUDES
#

include(FeatureSummary)
include(CPack)
include(PackageSourceTest)

#
# PROJECT
#

project(Sh4lt)
enable_testing()

# Prerequisites
find_package(PkgConfig REQUIRED)

# Global Stuff

link_libraries(
    pthread
)

include_directories(
    ${SH4LT_DIR} # Sh4lt needs to include from itself using "sh4lt/*"
)
include_directories(
    ${SH4LT_DIR}/wrappers/python/pybind11/
)

# BUILD TYPE
if(CMAKE_BUILD_TYPE EQUAL "Debug")
    add_definitions(-DDEBUG)
endif()

# COLOR OUTPUT
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-fdiagnostics-color CAN_DIAGNOSTICS_COLOR)
if(CAN_DIAGNOSTICS_COLOR)
  add_definitions(-fdiagnostics-color)
endif()

# WARNINGS
add_definitions(-Wall)
add_definitions(-Wextra)
add_definitions(-Werror)
add_definitions(-Wno-error=unused-parameter)
check_cxx_compiler_flag(-Wno-error=maybe-uninitialized CAN_MAYBE_UNINITIALIZED)
if(CAN_MAYBE_UNINITIALIZED)
  add_definitions(-Wno-error=maybe-uninitialized)
endif()
# disable warning in jsoncpp with clang18:
check_cxx_compiler_flag(-Wno-error=unneeded-internal-declaration CAN_UNNEEDED_INTERNAL_DECLARATION)
if(CAN_UNNEEDED_INTERNAL_DECLARATION)
  add_definitions(-Wno-error=unneeded-internal-declaration)
endif()
# disable warning in jsoncpp with release build in ubuntu 22.04:
check_cxx_compiler_flag(-Wno-error=stringop-overflow CAN_STRINGOP_OVERFLOW)
if(CAN_STRINGOP_OVERFLOW)
  add_definitions(-Wno-error=stringop-overflow)
endif()

# OSX
if(APPLE AND ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
    add_definitions(-DOSX)
endif ()

#
# MODULES
#

add_subdirectory(sh4lt)
add_subdirectory(utils)
add_subdirectory(tests)
add_subdirectory(wrappers/python)

#
# OTHER TARGETS
#

# Documentation
find_package(Doxygen)
if (DOXYGEN_FOUND AND NOT SKBUILD) # if SKBUILD is set, then sh4lt is installed through pip/scikit-build-core
    configure_file("doxyfile.in" "${CMAKE_SOURCE_DIR}/doxyfile")
    add_custom_target(doc COMMAND ${DOXYGEN_EXECUTABLE} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
    install(DIRECTORY ${CMAKE_SOURCE_DIR}/html/
        DESTINATION  share/doc/${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}/html/
        COMPONENT dev
        )
    install(DIRECTORY ${CMAKE_SOURCE_DIR}/html/
        DESTINATION  share/doc/${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}/html/
        COMPONENT dbg
        )
endif ()

# Uninstall
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/CMakeUninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/CMakeUninstall.cmake"
    IMMEDIATE @ONLY)

add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/CMakeUninstall.cmake)

#
# POST
#

# Report
feature_summary(WHAT ALL)
