@cryptotaxi247 / netdata-1 / commits / b22a400d2

Minor improvements to CMake code. (#21146)

* Use CMake’s built-in functionality for making CPP-safe values. This makes our code slightly tidier, and also covers a number of cases that our initial implementation did not. * Switch to using CMakePushCheckState to manage check state. This helps ensure that additional configuration required for specific individual checks doesn’t spill over to other checks.

Austin S. Hemmelgarn committed Jan 7, 2026 at 11:12 UTC b22a400d21658bf642a10bf8d5447b98509436e8
3 files changed +48 -47
CMakeLists.txt
+5
@@ -17,6 +17,7 @@ project(netdata
17 HOMEPAGE_URL "https://www.netdata.cloud"
18 LANGUAGES C CXX)
19 include(CMakeDependentOption)
20 +include(CMakePushCheckState)
21 include(NetdataUtil)
22 netdata_fixup_system_processor()
23
@@ -255,6 +256,7 @@ if(ENABLE_JEMALLOC)
256 pkg_check_modules(JEMALLOC QUIET jemalloc)
257 if(JEMALLOC_FOUND)
258 # Check if jemalloc has arena API
259 + cmake_push_check_state()
260 set(CMAKE_REQUIRED_INCLUDES ${JEMALLOC_INCLUDE_DIRS})
261 set(CMAKE_REQUIRED_LIBRARIES ${JEMALLOC_LIBRARIES})
262 check_c_source_compiles("
@@ -266,6 +268,7 @@ if(ENABLE_JEMALLOC)
268 return 0;
269 }
270 " HAVE_JEMALLOC_ARENA_API)
271 + cmake_pop_check_state()
272
273 if(HAVE_JEMALLOC_ARENA_API)
274 set(ENABLE_JEMALLOC ON CACHE BOOL "Enable jemalloc allocator" FORCE)
@@ -503,6 +506,7 @@ int main(void) {
506 }
507 " HAVE_TM_GMTOFF)
508
509 +cmake_push_check_state()
510 set(CMAKE_REQUIRED_LIBRARIES pthread)
511 check_c_source_compiles("
512 #define _GNU_SOURCE
@@ -513,6 +517,7 @@ int main() {
517 return pthread_getname_np(thread, name, sizeof(name));
518 }
519 " HAVE_PTHREAD_GETNAME_NP)
520 +cmake_pop_check_state()
521
522 check_c_source_compiles("
523 #include <stdio.h>
packaging/cmake/Modules/NetdataCompilerFlags.cmake
+7 -12
@@ -3,16 +3,7 @@
3
4 include(CheckCCompilerFlag)
5 include(CheckCXXCompilerFlag)
6 -
7 -# Construct a pre-processor safe name
8 -#
9 -# This takes a specified value, and assigns the generated name to the
10 -# specified target.
11 -function(make_cpp_safe_name value target)
12 - string(REPLACE "-" "_" tmp "${value}")
13 - string(REPLACE "=" "_" tmp "${tmp}")
14 - set(${target} "${tmp}" PARENT_SCOPE)
15 -endfunction()
6 +include(CMakePushCheckState)
7
8 # Conditionally add an extra compiler flag to C and C++ flags.
9 #
@@ -21,9 +12,10 @@ endfunction()
12 # the compiler flags for the run. Also sets `result` to MATCHED/ADDED/UNSUPPORTED
13 # depending on whether the flag was added or not.
14 function(add_extra_compiler_flag match flag result)
15 + cmake_push_check_state()
16 set(CMAKE_REQUIRED_FLAGS "-Werror")
17
26 - make_cpp_safe_name("${flag}" flag_name)
18 + string(MAKE_C_IDENTIFIER "${flag}" flag_name)
19
20 if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
21 check_c_compiler_flag("${flag}" HAVE_C_${flag_name})
@@ -36,6 +28,7 @@ function(add_extra_compiler_flag match flag result)
28 else()
29 set(matched_cxx TRUE)
30 endif()
31 + cmake_pop_check_state()
32
33 if(HAVE_C_${flag_name} AND HAVE_CXX_${flag_name})
34 add_compile_options("${flag}")
@@ -66,12 +59,14 @@ endfunction()
59 # Similar logic to add_extra_compiler_flag, but ignores existing
60 # instances and throws an error if the flag is not supported.
61 function(add_required_compiler_flag flag)
62 + cmake_push_check_state()
63 set(CMAKE_REQUIRED_FLAGS "-Werror")
64
71 - make_cpp_safe_name("${flag}" flag_name)
65 + string(MAKE_C_IDENTIFIER "${flag}" flag_name)
66
67 check_c_compiler_flag("${flag}" HAVE_C_${flag_name})
68 check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name})
69 + cmake_pop_check_state()
70
71 if(HAVE_C_${flag_name} AND HAVE_CXX_${flag_name})
72 add_compile_options("${flag}")
packaging/cmake/Modules/NetdataDetectSystemd.cmake
+36 -35
@@ -1,40 +1,41 @@
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # CMake Module to handle all the systemd-related checks for Netdata.
3
4 -macro(detect_systemd)
5 - find_library(SYSTEMD_LIBRARY NAMES systemd)
6 -
7 - set(ENABLE_DSYSTEMD_DBUS NO)
8 - pkg_check_modules(SYSTEMD libsystemd)
9 -
10 - if(SYSTEMD_FOUND)
11 - set(CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD "${CMAKE_REQUIRED_LIBRARIES}")
12 - set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};${SYSTEMD_LIBRARIES}")
13 -
14 - check_c_source_compiles("
15 - #include <systemd/sd-journal.h>
16 -
17 - int main() {
18 - int x = SD_JOURNAL_OS_ROOT;
19 - return 0;
20 - }" HAVE_SD_JOURNAL_OS_ROOT)
4 +include(CMakePushCheckState)
5
22 - check_symbol_exists(SD_JOURNAL_OS_ROOT "systemd/sd-journal.h" HAVE_SD_JOURNAL_OS_ROOT)
23 - check_symbol_exists(sd_journal_open_files_fd "systemd/sd-journal.h" HAVE_SD_JOURNAL_OPEN_FILES_FD)
24 - check_symbol_exists(sd_journal_restart_fields "systemd/sd-journal.h" HAVE_SD_JOURNAL_RESTART_FIELDS)
25 - check_symbol_exists(sd_journal_get_seqnum "systemd/sd-journal.h" HAVE_SD_JOURNAL_GET_SEQNUM)
26 -
27 - check_symbol_exists(sd_bus_default_system "systemd/sd-bus.h" HAVE_SD_BUS_DEFAULT_SYSTEM)
28 - check_symbol_exists(sd_bus_call_method "systemd/sd-bus.h" HAVE_SD_BUS_CALL_METHOD)
29 - check_symbol_exists(sd_bus_message_enter_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER)
30 - check_symbol_exists(sd_bus_message_read "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_READ)
31 - check_symbol_exists(sd_bus_message_exit_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER)
32 -
33 - set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD}")
34 -
35 - set(HAVE_SYSTEMD True)
36 - 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)
37 - set(ENABLE_SYSTEMD_DBUS YES)
38 - endif()
39 - endif()
6 +macro(detect_systemd)
7 + find_library(SYSTEMD_LIBRARY NAMES systemd)
8 +
9 + set(ENABLE_DSYSTEMD_DBUS NO)
10 + pkg_check_modules(SYSTEMD libsystemd)
11 +
12 + if(SYSTEMD_FOUND)
13 + cmake_push_check_state()
14 + set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};${SYSTEMD_LIBRARIES}")
15 +
16 + check_c_source_compiles("
17 + #include <systemd/sd-journal.h>
18 +
19 + int main() {
20 + int x = SD_JOURNAL_OS_ROOT;
21 + return 0;
22 + }" HAVE_SD_JOURNAL_OS_ROOT)
23 +
24 + check_symbol_exists(SD_JOURNAL_OS_ROOT "systemd/sd-journal.h" HAVE_SD_JOURNAL_OS_ROOT)
25 + check_symbol_exists(sd_journal_open_files_fd "systemd/sd-journal.h" HAVE_SD_JOURNAL_OPEN_FILES_FD)
26 + check_symbol_exists(sd_journal_restart_fields "systemd/sd-journal.h" HAVE_SD_JOURNAL_RESTART_FIELDS)
27 + check_symbol_exists(sd_journal_get_seqnum "systemd/sd-journal.h" HAVE_SD_JOURNAL_GET_SEQNUM)
28 +
29 + check_symbol_exists(sd_bus_default_system "systemd/sd-bus.h" HAVE_SD_BUS_DEFAULT_SYSTEM)
30 + check_symbol_exists(sd_bus_call_method "systemd/sd-bus.h" HAVE_SD_BUS_CALL_METHOD)
31 + check_symbol_exists(sd_bus_message_enter_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER)
32 + check_symbol_exists(sd_bus_message_read "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_READ)
33 + check_symbol_exists(sd_bus_message_exit_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER)
34 +
35 + cmake_pop_check_state()
36 + set(HAVE_SYSTEMD True)
37 + 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)
38 + set(ENABLE_SYSTEMD_DBUS YES)
39 + endif()
40 + endif()
41 endmacro()