More concretely utilize local modules in our CMake code. (#17022)
* Properly handle systemd.cmake as a module. * Split CMake compiler flag handling functions to their own module. * Add include guards to modules. * Prefix module names with Netdata. This ensures that we end up using our local modules instead of possibly using a system module with the same name. * Drop include guards. And shift systemd detection code to a macro so it’s less dangerous to import the module multiple times.
Austin S. Hemmelgarn committed
Feb 26, 2024 at 08:47 UTC
ca016406c3072cf5f7d2a28c36f4593ff33d67dc
4 files changed
+121
-105
CMakeLists.txt
+4
-68
@@ -56,6 +56,7 @@ project(netdata
56
DESCRIPTION "Netdata real-time monitoring"
57
HOMEPAGE_URL "https://www.netdata.cloud"
58
LANGUAGES C CXX)
59
+list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/packaging/cmake/Modules")
60
61
find_package(PkgConfig REQUIRED)
62
@@ -165,8 +166,7 @@ endif()
166
# handling of extra compiler flags
167
#
168
168
-include(CheckCCompilerFlag)
169
-include(CheckCXXCompilerFlag)
169
+include(NetdataCompilerFlags)
170
171
# Disable hardening for debug builds by default.
172
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
@@ -175,71 +175,6 @@ else()
175
option(DISABLE_HARDENING "disable adding extra compiler flags for hardening" FALSE)
176
endif()
177
178
-# Construct a pre-processor safe name
179
-function(make_cpp_safe_name value target)
180
- string(REPLACE "-" "_" tmp "${value}")
181
- string(REPLACE "=" "_" tmp "${tmp}")
182
- set(${target} "${tmp}" PARENT_SCOPE)
183
-endfunction()
184
-
185
-# Conditionally add an extra compiler flag to C and C++ flags.
186
-#
187
-# If the language flags already match the `match` argument, skip this flag.
188
-# Otherwise, check for support for `flag` and if support is found, add it to
189
-# the language-specific `target` flag group.
190
-function(add_simple_extra_compiler_flag match flag target)
191
- set(CMAKE_REQUIRED_FLAGS "-Werror")
192
-
193
- make_cpp_safe_name("${flag}" flag_name)
194
-
195
- if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
196
- check_c_compiler_flag("${flag}" HAVE_C_${flag_name})
197
- if(HAVE_C_${flag_name})
198
- set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag}" PARENT_SCOPE)
199
- endif()
200
- endif()
201
-
202
- if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
203
- check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name})
204
- if(HAVE_CXX_${flag_name})
205
- set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag}" PARENT_SCOPE)
206
- endif()
207
- endif()
208
-endfunction()
209
-
210
-# Same as add_simple_extra_compiler_flag, but check for a second flag if the
211
-# first one is unsupported.
212
-function(add_double_extra_compiler_flag match flag1 flag2 target)
213
- set(CMAKE_REQUIRED_FLAGS "-Werror")
214
-
215
- make_cpp_safe_name("${flag1}" flag1_name)
216
- make_cpp_safe_name("${flag2}" flag2_name)
217
-
218
- if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
219
- check_c_compiler_flag("${flag1}" HAVE_C_${flag1_name})
220
- if(HAVE_C_${flag1_name})
221
- set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag1}" PARENT_SCOPE)
222
- else()
223
- check_c_compiler_flag("${flag2}" HAVE_C_${flag2_name})
224
- if(HAVE_C_${flag2_name})
225
- set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag2}" PARENT_SCOPE)
226
- endif()
227
- endif()
228
- endif()
229
-
230
- if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
231
- check_cxx_compiler_flag("${flag1}" HAVE_CXX_${flag1_name})
232
- if(HAVE_CXX_${flag1_name})
233
- set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag1}" PARENT_SCOPE)
234
- else()
235
- check_cxx_compiler_flag("${flag2}" HAVE_CXX_${flag2_name})
236
- if(HAVE_CXX_${flag2_name})
237
- set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag2}" PARENT_SCOPE)
238
- endif()
239
- endif()
240
- endif()
241
-endfunction()
242
-
178
set(EXTRA_HARDENING_C_FLAGS "")
179
set(EXTRA_HARDENING_CXX_FLAGS "")
180
@@ -1524,7 +1459,8 @@ set_source_files_properties(JudyLTables.c PROPERTIES COMPILE_OPTIONS "-I${CMAKE_
1459
# build libnetdata
1460
#
1461
1527
-include(packaging/cmake/systemd.cmake)
1462
+include(NetdataDetectSystemd)
1463
+detect_systemd()
1464
1465
add_library(libnetdata STATIC ${LIBNETDATA_FILES})
1466
packaging/cmake/Modules/NetdataCompilerFlags.cmake
new
+75
@@ -0,0 +1,75 @@
1
+# Functions to simplify handling of extra compiler flags.
2
+#
3
+# Copyright (c) 2024 Netdata Inc.
4
+# SPDX-License-Identifier: GPL-3.0-or-later
5
+
6
+include(CheckCCompilerFlag)
7
+include(CheckCXXCompilerFlag)
8
+
9
+# Construct a pre-processor safe name
10
+#
11
+# This takes a specified value, and assigns the generated name to the
12
+# specified target.
13
+function(make_cpp_safe_name value target)
14
+ string(REPLACE "-" "_" tmp "${value}")
15
+ string(REPLACE "=" "_" tmp "${tmp}")
16
+ set(${target} "${tmp}" PARENT_SCOPE)
17
+endfunction()
18
+
19
+# Conditionally add an extra compiler flag to C and C++ flags.
20
+#
21
+# If the language flags already match the `match` argument, skip this flag.
22
+# Otherwise, check for support for `flag` and if support is found, add it to
23
+# the language-specific `target` flag group.
24
+function(add_simple_extra_compiler_flag match flag target)
25
+ set(CMAKE_REQUIRED_FLAGS "-Werror")
26
+
27
+ make_cpp_safe_name("${flag}" flag_name)
28
+
29
+ if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
30
+ check_c_compiler_flag("${flag}" HAVE_C_${flag_name})
31
+ if(HAVE_C_${flag_name})
32
+ set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag}" PARENT_SCOPE)
33
+ endif()
34
+ endif()
35
+
36
+ if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
37
+ check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name})
38
+ if(HAVE_CXX_${flag_name})
39
+ set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag}" PARENT_SCOPE)
40
+ endif()
41
+ endif()
42
+endfunction()
43
+
44
+# Same as add_simple_extra_compiler_flag, but check for a second flag if the
45
+# first one is unsupported.
46
+function(add_double_extra_compiler_flag match flag1 flag2 target)
47
+ set(CMAKE_REQUIRED_FLAGS "-Werror")
48
+
49
+ make_cpp_safe_name("${flag1}" flag1_name)
50
+ make_cpp_safe_name("${flag2}" flag2_name)
51
+
52
+ if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
53
+ check_c_compiler_flag("${flag1}" HAVE_C_${flag1_name})
54
+ if(HAVE_C_${flag1_name})
55
+ set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag1}" PARENT_SCOPE)
56
+ else()
57
+ check_c_compiler_flag("${flag2}" HAVE_C_${flag2_name})
58
+ if(HAVE_C_${flag2_name})
59
+ set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag2}" PARENT_SCOPE)
60
+ endif()
61
+ endif()
62
+ endif()
63
+
64
+ if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
65
+ check_cxx_compiler_flag("${flag1}" HAVE_CXX_${flag1_name})
66
+ if(HAVE_CXX_${flag1_name})
67
+ set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag1}" PARENT_SCOPE)
68
+ else()
69
+ check_cxx_compiler_flag("${flag2}" HAVE_CXX_${flag2_name})
70
+ if(HAVE_CXX_${flag2_name})
71
+ set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag2}" PARENT_SCOPE)
72
+ endif()
73
+ endif()
74
+ endif()
75
+endfunction()
packaging/cmake/Modules/NetdataDetectSystemd.cmake
new
+42
@@ -0,0 +1,42 @@
1
+# CMake Module to handle all the systemd-related checks for Netdata.
2
+#
3
+# Copyright (c) 2024 Netdata Inc.
4
+# SPDX-License-Identifier: GPL-3.0-or-later
5
+
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
+ set(CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD "${CMAKE_REQUIRED_LIBRARIES}")
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
+ set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD}")
36
+
37
+ set(HAVE_SYSTEMD True)
38
+ 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)
39
+ set(ENABLE_SYSTEMD_DBUS YES)
40
+ endif()
41
+ endif()
42
+endmacro()
packaging/cmake/systemd.cmake
deleted
-37
@@ -1,37 +0,0 @@
1
-find_library(SYSTEMD_LIBRARY NAMES systemd)
2
-
3
-include(CheckFunctionExists)
4
-
5
-set(ENABLE_DSYSTEMD_DBUS NO)
6
-pkg_check_modules(SYSTEMD libsystemd)
7
-
8
-if(SYSTEMD_FOUND)
9
- set(CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD "${CMAKE_REQUIRED_LIBRARIES}")
10
- set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES};${SYSTEMD_LIBRARIES}")
11
-
12
- check_c_source_compiles("
13
- #include <systemd/sd-journal.h>
14
-
15
- int main() {
16
- int x = SD_JOURNAL_OS_ROOT;
17
- return 0;
18
- }" HAVE_SD_JOURNAL_OS_ROOT)
19
-
20
- check_symbol_exists(SD_JOURNAL_OS_ROOT "systemd/sd-journal.h" HAVE_SD_JOURNAL_OS_ROOT)
21
- check_symbol_exists(sd_journal_open_files_fd "systemd/sd-journal.h" HAVE_SD_JOURNAL_OPEN_FILES_FD)
22
- check_symbol_exists(sd_journal_restart_fields "systemd/sd-journal.h" HAVE_SD_JOURNAL_RESTART_FIELDS)
23
- check_symbol_exists(sd_journal_get_seqnum "systemd/sd-journal.h" HAVE_SD_JOURNAL_GET_SEQNUM)
24
-
25
- check_symbol_exists(sd_bus_default_system "systemd/sd-bus.h" HAVE_SD_BUS_DEFAULT_SYSTEM)
26
- check_symbol_exists(sd_bus_call_method "systemd/sd-bus.h" HAVE_SD_BUS_CALL_METHOD)
27
- check_symbol_exists(sd_bus_message_enter_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_ENTER_CONTAINER)
28
- check_symbol_exists(sd_bus_message_read "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_READ)
29
- check_symbol_exists(sd_bus_message_exit_container "systemd/sd-bus.h" HAVE_SD_BUS_MESSAGE_EXIT_CONTAINER)
30
-
31
- set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES_BEFORE_SYSTEMD}")
32
-
33
- set(HAVE_SYSTEMD True)
34
- 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)
35
- set(ENABLE_SYSTEMD_DBUS YES)
36
- endif()
37
-endif()