Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linux.bash
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ case "$DISTRO" in
*suse*)
zypper in -y --allow-downgrade bison ccache cmake flex gcc-c++ ninja rpm-config-SUSE \
{lib{edit,mariadb,openssl},ncurses,postgresql,systemd,protobuf}-devel \
libboost_{context,coroutine,filesystem,iostreams,program_options,regex,system,test,thread}-devel
libboost_{atomic,context,coroutine,filesystem,iostreams,program_options,regex,system,test,thread}-devel
;;

*rockylinux:*)
Expand Down
186 changes: 46 additions & 140 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,25 @@ option(ICINGA2_WITH_PERFDATA "Build the perfdata module" ${ICINGA2_MASTER})
option(ICINGA2_WITH_ICINGADB "Build the IcingaDB module" ${ICINGA2_MASTER})
option(ICINGA2_WITH_OPENTELEMETRY "Build the OpenTelemetry integration module" ${ICINGA2_MASTER})

if(ICINGA2_WITH_OPENTELEMETRY)
# Newer Protobuf versions provide a CMake config package that we should prefer, since it implicitly
# links against all its dependencies (like absl, etc.) that would otherwise need to be linked manually.
# Thus, first try to find Protobuf in config mode and only fall back to module mode if that fails.
find_package(Protobuf CONFIG)
if(NOT Protobuf_FOUND)
# FindProtobuf.cmake in CMake versions < 3.31.0 is just broken and mixes up the Protobuf output directories
# and it doesn't even support to pass any PLUGIN_OPTIONS like "lite" to the protobuf_generate() function in
# order to generate code for the lite runtime without having to modify the proto files directly.
if(CMAKE_VERSION VERSION_LESS 3.31.0)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/third-party/cmake/protobuf")
endif()
find_package(Protobuf REQUIRED)
endif()
endif()

option (USE_SYSTEMD
"Configure icinga as native systemd service instead of a SysV initscript" OFF)

set(HAVE_SYSTEMD ${USE_SYSTEMD})

include(GNUInstallDirs)
include(InstallConfig)
include(SetFullDir)
Expand All @@ -67,6 +81,11 @@ set(ICINGA2_INITRUNDIR "${ICINGA2_RUNDIR}/icinga2" CACHE FILEPATH "Runtime data
set(ICINGA2_PKGDATADIR "${CMAKE_INSTALL_DATADIR}/icinga2" CACHE FILEPATH "Installed data, e.g. /usr/share/icinga2")
set(ICINGA2_INCLUDEDIR "${ICINGA2_PKGDATADIR}/include" CACHE FILEPATH "Include directory for the ITL, e.g. /usr/share/icinga2/include")

# TODO: Remove when presets are done and applied in CI etc.
if(ICINGA2_UNITY_BUILD)
set(CMAKE_UNITY_BUILD ON)
endif()

# ensure absolute paths
set_full_dir(ICINGA2_FULL_CONFIGDIR "${ICINGA2_CONFIGDIR}")
set_full_dir(ICINGA2_FULL_CACHEDIR "${ICINGA2_CACHEDIR}")
Expand Down Expand Up @@ -141,8 +160,8 @@ if(WIN32)
# Disable optimization for Boost::context
# https://www.boost.org/doc/libs/1_69_0/libs/context/doc/html/context/overview.html
# https://docs.microsoft.com/en-us/cpp/build/reference/gl-whole-program-optimization?view=vs-2017
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj /GL- /EHs")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /GL- /EHs")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj /GL- /EHs")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /GL- /EHs")

# detect if 32-bit target
if(CMAKE_VS_PLATFORM_NAME STREQUAL "Win32")
Expand Down Expand Up @@ -182,117 +201,33 @@ add_compile_definitions(OPENSSL_SUPPRESS_DEPRECATED)
# Boost.Coroutine2 (the successor of Boost.Coroutine)
# (1) doesn't even exist in old Boost versions and
# (2) isn't supported by ASIO, yet.
add_definitions(-DBOOST_COROUTINES_NO_DEPRECATION_WARNING)
add_compile_definitions(BOOST_COROUTINES_NO_DEPRECATION_WARNING)

add_definitions(-DBOOST_FILESYSTEM_NO_DEPRECATED)
add_compile_definitions(BOOST_FILESYSTEM_NO_DEPRECATED)

# Required for Boost v1.74+
add_definitions(-DBOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
add_compile_definitions(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)

link_directories(${Boost_LIBRARY_DIRS})
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
find_package(Threads REQUIRED)

find_package(OpenSSL REQUIRED)
include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})

set(base_DEPS ${CMAKE_DL_LIBS} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES})
set(base_OBJS $<TARGET_OBJECTS:mmatch> $<TARGET_OBJECTS:socketpair> $<TARGET_OBJECTS:base>)

# JSON
find_package(JSON)
include_directories(SYSTEM ${JSON_INCLUDE})

# UTF8CPP
find_package(UTF8CPP)
include_directories(SYSTEM ${UTF8CPP_INCLUDE})
if(USE_SYSTEMD)
find_package(Systemd REQUIRED)
endif()

find_package(Editline)
set(HAVE_EDITLINE "${EDITLINE_FOUND}")

# TODO: Where is this ever used?
find_package(Termcap)
set(HAVE_TERMCAP "${TERMCAP_FOUND}")

if(ICINGA2_WITH_OPENTELEMETRY)
# Newer Protobuf versions provide a CMake config package that we should prefer, since it implicitly
# links against all its dependencies (like absl, etc.) that would otherwise need to be linked manually.
# Thus, first try to find Protobuf in config mode and only fall back to module mode if that fails.
find_package(Protobuf CONFIG)
if(NOT Protobuf_FOUND)
# FindProtobuf.cmake in CMake versions < 3.31.0 is just broken and mixes up the Protobuf output directories
# and it doesn't even support to pass any PLUGIN_OPTIONS like "lite" to the protobuf_generate() function in
# order to generate code for the lite runtime without having to modify the proto files directly.
if(CMAKE_VERSION VERSION_LESS 3.31.0)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/third-party/cmake/protobuf")
endif()
find_package(Protobuf REQUIRED)
endif()
list(APPEND base_DEPS protobuf::libprotobuf-lite)
endif()

include_directories(
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/lib
)

if(HAVE_SYSTEMD)
list(APPEND base_DEPS systemd)
endif()

if(EDITLINE_FOUND)
list(APPEND base_DEPS ${EDITLINE_LIBRARIES})
include_directories(SYSTEM ${EDITLINE_INCLUDE_DIR})
endif()

if(TERMCAP_FOUND)
list(APPEND base_DEPS ${TERMCAP_LIBRARIES})
include_directories(SYSTEM ${TERMCAP_INCLUDE_DIR})
endif()

if(WIN32)
list(APPEND base_DEPS ws2_32 dbghelp shlwapi msi)
endif()

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH};${CMAKE_INSTALL_FULL_LIBDIR}/icinga2")

# TODO: Move all of this into CMake Presets
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Winconsistent-missing-override -Wrange-loop-construct")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Qunused-arguments -fcolor-diagnostics -fno-limit-debug-info")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments -fcolor-diagnostics -fno-limit-debug-info")

# Clang on Fedora requires -pthread, Apple Clang does not
# AppleClang is available since CMake 3.0.0
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()
endif()

if(CMAKE_C_COMPILER_ID STREQUAL "SunPro")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mt")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mt -library=stlport4")
endif()

if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsuggest-override")

if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_GREATER_EQUAL "11.0.0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wrange-loop-construct")
endif()

if(CMAKE_SYSTEM_NAME MATCHES AIX)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -lpthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -lpthread")
elseif(CMAKE_SYSTEM_NAME MATCHES "kOpenBSD.*|OpenBSD.*")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lpthread")
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -lpthread")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread")
endif()
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Qunused-arguments -fcolor-diagnostics ")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments")
endif()

include(CheckCXXCompilerFlag)
Expand Down Expand Up @@ -339,33 +274,12 @@ if(LD_DYNAMIC_LIST_DATA)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--dynamic-list-data")
endif()

check_cxx_compiler_flag("-Winvalid-pch" CXX_INVALID_PCH)

if(CXX_INVALID_PCH)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winvalid-pch")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Winvalid-pch")
endif()

if(ICINGA2_LTO_BUILD)
check_cxx_compiler_flag("-flto" CXX_FLAG_LTO)

if(NOT CXX_FLAG_LTO)
message(WARNING "Compiler does not support LTO, falling back to non-LTO build")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto")

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.0) AND NOT OPENBSD)
set(CMAKE_AR "gcc-ar")
set(CMAKE_RANLIB "gcc-ranlib")
endif()
endif()
endif()

if(MSVC)
add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS)
add_compile_definitions(
_CRT_SECURE_NO_DEPRECATE
_CRT_NONSTDC_NO_DEPRECATE
_SCL_SECURE_NO_WARNINGS
)
endif()

set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/Bin/${CMAKE_BUILD_TYPE} CACHE PATH "Library output path")
Expand All @@ -382,8 +296,7 @@ if(NOT HAVE_COUNTER_MACRO)
message(FATAL_ERROR "Your C/C++ compiler does not support the __COUNTER__ macro.")
endif()

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DI2_DEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DI2_DEBUG")
add_compile_definitions($<$<CONFIG:Debug>:I2_DEBUG>)

check_function_exists(vfork HAVE_VFORK)
check_function_exists(backtrace_symbols HAVE_BACKTRACE_SYMBOLS)
Expand All @@ -397,14 +310,15 @@ check_library_exists(dl dladdr "dlfcn.h" HAVE_DLADDR)
check_library_exists(execinfo backtrace_symbols "" HAVE_LIBEXECINFO)
check_include_file_cxx(cxxabi.h HAVE_CXXABI_H)

if(HAVE_LIBEXECINFO)
set(HAVE_BACKTRACE_SYMBOLS TRUE)
list(APPEND base_DEPS execinfo)
endif()
# TODO:
# if(HAVE_LIBEXECINFO)
# set(HAVE_BACKTRACE_SYMBOLS TRUE)
# list(APPEND base_DEPS execinfo)
# endif()

if(NOT WIN32)
# boost::stacktrace uses _Unwind_Backtrace which is only exposed if _GNU_SOURCE is defined on most systems
add_definitions(-D_GNU_SOURCE)
add_compile_definitions(_GNU_SOURCE)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
Expand Down Expand Up @@ -432,14 +346,6 @@ if(MSVC)
endif()
endif()

if(NOT MSVC)
check_cxx_source_compiles("class Base { public: virtual void test(void) { } }; class Derived : public Base { virtual void test(void) override { } }; int main(){}" CXX_FEATURE_OVERRIDE)

if(NOT CXX_FEATURE_OVERRIDE)
add_definitions("-Doverride=")
endif()
endif()

# Architecture specifics
# - Log the target architecture
# - ARM needs to link against atomic
Expand Down
Loading
Loading