diff --git a/CMakeLists.txt b/CMakeLists.txt index 320a294e84..d6e9536bbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ project(netdata DESCRIPTION "Netdata real-time monitoring" HOMEPAGE_URL "https://www.netdata.cloud" LANGUAGES C CXX) +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/packaging/cmake/Modules") find_package(PkgConfig REQUIRED) @@ -165,8 +166,7 @@ endif() # handling of extra compiler flags # -include(CheckCCompilerFlag) -include(CheckCXXCompilerFlag) +include(NetdataCompilerFlags) # Disable hardening for debug builds by default. if(CMAKE_BUILD_TYPE STREQUAL "Debug") @@ -175,71 +175,6 @@ else() option(DISABLE_HARDENING "disable adding extra compiler flags for hardening" FALSE) endif() -# Construct a pre-processor safe name -function(make_cpp_safe_name value target) - string(REPLACE "-" "_" tmp "${value}") - string(REPLACE "=" "_" tmp "${tmp}") - set(${target} "${tmp}" PARENT_SCOPE) -endfunction() - -# Conditionally add an extra compiler flag to C and C++ flags. -# -# If the language flags already match the `match` argument, skip this flag. -# Otherwise, check for support for `flag` and if support is found, add it to -# the language-specific `target` flag group. -function(add_simple_extra_compiler_flag match flag target) - set(CMAKE_REQUIRED_FLAGS "-Werror") - - make_cpp_safe_name("${flag}" flag_name) - - if(NOT ${CMAKE_C_FLAGS} MATCHES ${match}) - check_c_compiler_flag("${flag}" HAVE_C_${flag_name}) - if(HAVE_C_${flag_name}) - set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag}" PARENT_SCOPE) - endif() - endif() - - if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match}) - check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name}) - if(HAVE_CXX_${flag_name}) - set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag}" PARENT_SCOPE) - endif() - endif() -endfunction() - -# Same as add_simple_extra_compiler_flag, but check for a second flag if the -# first one is unsupported. -function(add_double_extra_compiler_flag match flag1 flag2 target) - set(CMAKE_REQUIRED_FLAGS "-Werror") - - make_cpp_safe_name("${flag1}" flag1_name) - make_cpp_safe_name("${flag2}" flag2_name) - - if(NOT ${CMAKE_C_FLAGS} MATCHES ${match}) - check_c_compiler_flag("${flag1}" HAVE_C_${flag1_name}) - if(HAVE_C_${flag1_name}) - set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag1}" PARENT_SCOPE) - else() - check_c_compiler_flag("${flag2}" HAVE_C_${flag2_name}) - if(HAVE_C_${flag2_name}) - set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag2}" PARENT_SCOPE) - endif() - endif() - endif() - - if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match}) - check_cxx_compiler_flag("${flag1}" HAVE_CXX_${flag1_name}) - if(HAVE_CXX_${flag1_name}) - set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag1}" PARENT_SCOPE) - else() - check_cxx_compiler_flag("${flag2}" HAVE_CXX_${flag2_name}) - if(HAVE_CXX_${flag2_name}) - set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag2}" PARENT_SCOPE) - endif() - endif() - endif() -endfunction() - set(EXTRA_HARDENING_C_FLAGS "") set(EXTRA_HARDENING_CXX_FLAGS "") @@ -1524,7 +1459,8 @@ set_source_files_properties(JudyLTables.c PROPERTIES COMPILE_OPTIONS "-I${CMAKE_ # build libnetdata # -include(packaging/cmake/systemd.cmake) +include(NetdataDetectSystemd) +detect_systemd() add_library(libnetdata STATIC ${LIBNETDATA_FILES}) diff --git a/packaging/cmake/Modules/NetdataCompilerFlags.cmake b/packaging/cmake/Modules/NetdataCompilerFlags.cmake new file mode 100644 index 0000000000..894e244ce6 --- /dev/null +++ b/packaging/cmake/Modules/NetdataCompilerFlags.cmake @@ -0,0 +1,75 @@ +# Functions to simplify handling of extra compiler flags. +# +# Copyright (c) 2024 Netdata Inc. +# SPDX-License-Identifier: GPL-3.0-or-later + +include(CheckCCompilerFlag) +include(CheckCXXCompilerFlag) + +# Construct a pre-processor safe name +# +# This takes a specified value, and assigns the generated name to the +# specified target. +function(make_cpp_safe_name value target) + string(REPLACE "-" "_" tmp "${value}") + string(REPLACE "=" "_" tmp "${tmp}") + set(${target} "${tmp}" PARENT_SCOPE) +endfunction() + +# Conditionally add an extra compiler flag to C and C++ flags. +# +# If the language flags already match the `match` argument, skip this flag. +# Otherwise, check for support for `flag` and if support is found, add it to +# the language-specific `target` flag group. +function(add_simple_extra_compiler_flag match flag target) + set(CMAKE_REQUIRED_FLAGS "-Werror") + + make_cpp_safe_name("${flag}" flag_name) + + if(NOT ${CMAKE_C_FLAGS} MATCHES ${match}) + check_c_compiler_flag("${flag}" HAVE_C_${flag_name}) + if(HAVE_C_${flag_name}) + set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag}" PARENT_SCOPE) + endif() + endif() + + if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match}) + check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name}) + if(HAVE_CXX_${flag_name}) + set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag}" PARENT_SCOPE) + endif() + endif() +endfunction() + +# Same as add_simple_extra_compiler_flag, but check for a second flag if the +# first one is unsupported. +function(add_double_extra_compiler_flag match flag1 flag2 target) + set(CMAKE_REQUIRED_FLAGS "-Werror") + + make_cpp_safe_name("${flag1}" flag1_name) + make_cpp_safe_name("${flag2}" flag2_name) + + if(NOT ${CMAKE_C_FLAGS} MATCHES ${match}) + check_c_compiler_flag("${flag1}" HAVE_C_${flag1_name}) + if(HAVE_C_${flag1_name}) + set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag1}" PARENT_SCOPE) + else() + check_c_compiler_flag("${flag2}" HAVE_C_${flag2_name}) + if(HAVE_C_${flag2_name}) + set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag2}" PARENT_SCOPE) + endif() + endif() + endif() + + if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match}) + check_cxx_compiler_flag("${flag1}" HAVE_CXX_${flag1_name}) + if(HAVE_CXX_${flag1_name}) + set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag1}" PARENT_SCOPE) + else() + check_cxx_compiler_flag("${flag2}" HAVE_CXX_${flag2_name}) + if(HAVE_CXX_${flag2_name}) + set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag2}" PARENT_SCOPE) + endif() + endif() + endif() +endfunction() diff --git a/packaging/cmake/Modules/NetdataDetectSystemd.cmake b/packaging/cmake/Modules/NetdataDetectSystemd.cmake new file mode 100644 index 0000000000..5c0e6e09e5 --- /dev/null +++ b/packaging/cmake/Modules/NetdataDetectSystemd.cmake @@ -0,0 +1,42 @@ +# CMake Module to handle all the systemd-related checks for Netdata. +# +# Copyright (c) 2024 Netdata Inc. +# SPDX-License-Identifier: GPL-3.0-or-later + +macro(detect_systemd) + find_library(SYSTEMD_LIBRARY NAMES systemd) + + set(ENABLE_DSYSTEMD_DBUS NO) + pkg_check_modules(SYSTEMD libsystemd) + + if(SYSTEMD_FOUND) + set(CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD "${CMAKE_REQUIRED_LIBRARIES}") + set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};${SYSTEMD_LIBRARIES}") + + check_c_source_compiles(" + #include <systemd/sd-journal.h> + + int main() { + int x = SD_JOURNAL_OS_ROOT; + return 0; + }" HAVE_SD_JOURNAL_OS_ROOT) + + check_symbol_exists(SD_JOURNAL_OS_ROOT "systemd/sd-journal.h" HAVE_SD_JOURNAL_OS_ROOT) + check_symbol_exists(sd_journal_open_files_fd "systemd/sd-journal.h" HAVE_SD_JOURNAL_OPEN_FILES_FD) + check_symbol_exists(sd_journal_restart_fields "systemd/sd-journal.h" HAVE_SD_JOURNAL_RESTART_FIELDS) + check_symbol_exists(sd_journal_get_seqnum "systemd/sd-journal.h" HAVE_SD_JOURNAL_GET_SEQNUM) + + check_symbol_exists(sd_bus_default_system "systemd/sd-bus.h" HAVE_SD_BUS_DEFAULT_SYSTEM) + check_symbol_exists(sd_bus_call_method "systemd/sd-bus.h" HAVE_SD_BUS_CALL_METHOD) + check_symbol_exists(sd_bus_message_enter_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER) + check_symbol_exists(sd_bus_message_read "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_READ) + check_symbol_exists(sd_bus_message_exit_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER) + + set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD}") + + set(HAVE_SYSTEMD True) + if(HAVE_SD_BUS_DEFAULT_SYSTEM AND HAVE_SD_BUS_CALL_METHOD AND HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER AND HAVE_SD_BUS_MESSAGE_READ AND HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER) + set(ENABLE_SYSTEMD_DBUS YES) + endif() + endif() +endmacro() diff --git a/packaging/cmake/systemd.cmake b/packaging/cmake/systemd.cmake deleted file mode 100644 index b61cbb2a42..0000000000 --- a/packaging/cmake/systemd.cmake +++ /dev/null @@ -1,37 +0,0 @@ -find_library(SYSTEMD_LIBRARY NAMES systemd) - -include(CheckFunctionExists) - -set(ENABLE_DSYSTEMD_DBUS NO) -pkg_check_modules(SYSTEMD libsystemd) - -if(SYSTEMD_FOUND) - set(CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD "${CMAKE_REQUIRED_LIBRARIES}") - set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};${SYSTEMD_LIBRARIES}") - - check_c_source_compiles(" - #include <systemd/sd-journal.h> - - int main() { - int x = SD_JOURNAL_OS_ROOT; - return 0; - }" HAVE_SD_JOURNAL_OS_ROOT) - - check_symbol_exists(SD_JOURNAL_OS_ROOT "systemd/sd-journal.h" HAVE_SD_JOURNAL_OS_ROOT) - check_symbol_exists(sd_journal_open_files_fd "systemd/sd-journal.h" HAVE_SD_JOURNAL_OPEN_FILES_FD) - check_symbol_exists(sd_journal_restart_fields "systemd/sd-journal.h" HAVE_SD_JOURNAL_RESTART_FIELDS) - check_symbol_exists(sd_journal_get_seqnum "systemd/sd-journal.h" HAVE_SD_JOURNAL_GET_SEQNUM) - - check_symbol_exists(sd_bus_default_system "systemd/sd-bus.h" HAVE_SD_BUS_DEFAULT_SYSTEM) - check_symbol_exists(sd_bus_call_method "systemd/sd-bus.h" HAVE_SD_BUS_CALL_METHOD) - check_symbol_exists(sd_bus_message_enter_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER) - check_symbol_exists(sd_bus_message_read "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_READ) - check_symbol_exists(sd_bus_message_exit_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER) - - set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD}") - - set(HAVE_SYSTEMD True) - if(HAVE_SD_BUS_DEFAULT_SYSTEM AND HAVE_SD_BUS_CALL_METHOD AND HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER AND HAVE_SD_BUS_MESSAGE_READ AND HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER) - set(ENABLE_SYSTEMD_DBUS YES) - endif() -endif()