diff --git a/CMakeLists.txt b/CMakeLists.txt index 82be4a091..b0ec2ae86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,8 +42,6 @@ option(ICINGA2_WITH_OPENTELEMETRY "Build the OpenTelemetry integration module" $ 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) @@ -234,8 +232,11 @@ include_directories( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/lib ) -if(HAVE_SYSTEMD) - list(APPEND base_DEPS systemd) +if(USE_SYSTEMD) + find_package(Systemd REQUIRED) + list(APPEND base_DEPS Systemd) + include_directories(SYSTEM ${Systemd_INCLUDE_DIRS}) + add_compile_definitions(HAVE_SYSTEMD) endif() if(EDITLINE_FOUND) diff --git a/cmake/FindSystemd.cmake b/cmake/FindSystemd.cmake new file mode 100644 index 000000000..0aa60eef4 --- /dev/null +++ b/cmake/FindSystemd.cmake @@ -0,0 +1,35 @@ +# SPDX-FileCopyrightText: 2026 Icinga GmbH +# SPDX-License-Identifier: GPL-2.0-or-later + +find_path(Systemd_INCLUDE_DIRS + NAMES + systemd/sd-daemon.h + HINTS ${SYSTEMD_ROOT_DIR} +) + +find_library(Systemd_LIBRARIES + NAMES systemd + HINTS ${SYSTEMD_LIBRARY_DIR} +) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Systemd + REQUIRED_VARS Systemd_LIBRARIES Systemd_INCLUDE_DIRS +) + +if(Systemd_FOUND) + message(STATUS "Systemd_LIBRARIES: ${Systemd_LIBRARIES}") + message(STATUS "Systemd_INCLUDE_DIRS: ${Systemd_INCLUDE_DIRS}") + add_library(Systemd UNKNOWN IMPORTED) + set_target_properties(Systemd PROPERTIES + IMPORTED_LOCATION "${Systemd_LIBRARIES}" + INTERFACE_INCLUDE_DIRECTORIES "${Systemd_INCLUDE_DIRS}" + INTERFACE_COMPILE_DEFINITIONS "HAVE_SYSTEMD" + INTERFACE_LINK_LIBRARIES "${Systemd_LIBRARIES}" + ) +else() + set(SYSTEMD_LIBRARIES) + set(SYSTEMD_INCLUDE_DIRS) +endif() + +mark_as_advanced(Systemd_LIBRARIES Systemd_INCLUDE_DIRS) diff --git a/config.h.cmake b/config.h.cmake index b3b36207a..8daab077d 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -13,7 +13,6 @@ #cmakedefine HAVE_PTHREAD_SET_NAME_NP #cmakedefine HAVE_PTHREAD_SETNAME_NP #cmakedefine HAVE_EDITLINE -#cmakedefine HAVE_SYSTEMD #cmakedefine ICINGA2_UNITY_BUILD #cmakedefine ICINGA2_STACKTRACE_USE_BACKTRACE_SYMBOLS diff --git a/etc/CMakeLists.txt b/etc/CMakeLists.txt index bee39464b..b72c0cd55 100644 --- a/etc/CMakeLists.txt +++ b/etc/CMakeLists.txt @@ -40,7 +40,7 @@ install_if_not_exists(icinga2/features-available/debuglog.conf ${ICINGA2_CONFIGD install_if_not_exists(icinga2/features-available/mainlog.conf ${ICINGA2_CONFIGDIR}/features-available) if(NOT WIN32) install_if_not_exists(icinga2/features-available/syslog.conf ${ICINGA2_CONFIGDIR}/features-available) - if(HAVE_SYSTEMD) + if(USE_SYSTEMD) install_if_not_exists(icinga2/features-available/journald.conf ${ICINGA2_CONFIGDIR}/features-available) endif() else() diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index 6f80914e4..3f41b8bfb 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -133,11 +133,7 @@ if(ICINGA2_UNITY_BUILD) mkunity_target(base base base_SOURCES) endif() -if(HAVE_SYSTEMD) - find_path(SYSTEMD_INCLUDE_DIR - NAMES systemd/sd-daemon.h - HINTS ${SYSTEMD_ROOT_DIR}) - include_directories(SYSTEM ${SYSTEMD_INCLUDE_DIR}) +if(USE_SYSTEMD) set_property( SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/journaldlogger.cpp APPEND PROPERTY COMPILE_DEFINITIONS