@cryptotaxi247 / netdata-1 / commits / cc33fbe67

Clean up handling of compiler flags in CMake. (#17532)

* Move all handling of compilation flags inot compiler flags module. Also, make including the module do all the required compiler flag changes. * Switch compiler flag handling to (mostly) use properties. This makes it easier to override individual flags on a per-target basis and also results in slightly simpler CMake code. * Fix typos in compiler flag handling. * Fix missing quotes.

Austin S. Hemmelgarn committed May 1, 2024 at 07:04 UTC cc33fbe673ed4e593b6debb126d08fee77f1b018
3 files changed +51 -71
CMakeLists.txt
+1 -48
@@ -82,15 +82,7 @@ if(NOT CMAKE_BUILD_TYPE)
82 set(CMAKE_BUILD_TYPE "Release")
83 endif()
84
85 -option(ENABLE_ADDRESS_SANITIZER "Build with address sanitizer enabled" False)
86 -mark_as_advanced(ENABLE_ADDRESS_SANITIZER)
87 -
88 -if(ENABLE_ADDRESS_SANITIZER)
89 - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
90 -endif()
91 -
92 -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexceptions")
93 -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")
85 +include(NetdataCompilerFlags)
86
87 set(CMAKE_EXPORT_COMPILE_COMMANDS On)
88
@@ -197,45 +189,6 @@ if(NEED_PROTOBUF)
189 endif()
190 endif()
191
200 -#
201 -# handling of extra compiler flags
202 -#
203 -
204 -include(NetdataCompilerFlags)
205 -
206 -# Disable hardening for debug builds by default.
207 -if(CMAKE_BUILD_TYPE STREQUAL "Debug")
208 - option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE)
209 -else()
210 - option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" FALSE)
211 -endif()
212 -
213 -set(EXTRA_HARDENING_C_FLAGS "")
214 -set(EXTRA_HARDENING_CXX_FLAGS "")
215 -
216 -set(EXTRA_OPT_C_FLAGS "")
217 -set(EXTRA_OPT_CXX_FLAGS "")
218 -
219 -if(NOT ${DISABLE_HARDENING})
220 - add_double_extra_compiler_flag("stack-protector" "-fstack-protector-strong" "-fstack-protector" EXTRA_HARDENING)
221 - add_double_extra_compiler_flag("_FORTIFY_SOURCE" "-D_FORTIFY_SOURCE=3" "-D_FORTIFY_SOURCE=2" EXTRA_HARDENING)
222 - add_simple_extra_compiler_flag("stack-clash-protection" "-fstack-clash-protection" EXTRA_HARDENING)
223 - add_simple_extra_compiler_flag("-fcf-protection" "-fcf-protection=full" EXTRA_HARDENING)
224 - add_simple_extra_compiler_flag("branch-protection" "-mbranch-protection=standard" EXTRA_HARDENING)
225 -endif()
226 -
227 -foreach(FLAG function-sections data-sections)
228 - add_simple_extra_compiler_flag("${FLAG}" "-f${FLAG}" EXTRA_OPT)
229 -endforeach()
230 -
231 -add_simple_extra_compiler_flag("-Wbuiltin-macro-redefined" "-Wno-builtin-macro-redefined" EXTRA_OPT)
232 -
233 -foreach(RELTYP RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
234 - foreach(L C CXX)
235 - set(CMAKE_${L}_FLAGS_${RELTYP} "${CMAKE_${L}_FLAGS_${RELTYP}} ${EXTRA_HARDENING_C_FLAGS} ${EXTRA_OPT_C_FLAGS}")
236 - endforeach()
237 -endforeach()
238 -
192 #
193 # detect OS
194 #
packaging/cmake/Modules/NetdataCompilerFlags.cmake
+48 -21
@@ -20,30 +20,29 @@ endfunction()
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)
23 +# the compiler flags for the run.
24 +function(add_simple_extra_compiler_flag match flag)
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()
31 endif()
32
33 if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
34 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()
35 + endif()
36 +
37 + if(HAVE_C_${flag_name} AND HAVE_CXX_${flag_name})
38 + add_compile_options("${flag}")
39 + add_link_options("${flag}")
40 endif()
41 endfunction()
42
43 # Same as add_simple_extra_compiler_flag, but check for a second flag if the
44 # first one is unsupported.
46 -function(add_double_extra_compiler_flag match flag1 flag2 target)
45 +function(add_double_extra_compiler_flag match flag1 flag2)
46 set(CMAKE_REQUIRED_FLAGS "-Werror")
47
48 make_cpp_safe_name("${flag1}" flag1_name)
@@ -51,25 +50,53 @@ function(add_double_extra_compiler_flag match flag1 flag2 target)
50
51 if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
52 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()
53 + if(NOT HAVE_C_${flag1_name})
54 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()
55 endif()
56 endif()
57
58 if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
59 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()
60 + if(NOT HAVE_CXX_${flag1_name})
61 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()
62 endif()
63 endif()
64 +
65 + if(HAVE_C_${flag1_name} AND HAVE_CXX_${flag1_name})
66 + add_compile_options("${flag1}")
67 + add_link_options("${flag1}")
68 + elseif(HAVE_C_${flag2_name} AND HAVE_CXX${flag2_name})
69 + add_compile_options("${flag2}")
70 + add_link_options("${flag2}")
71 + endif()
72 endfunction()
73 +
74 +if(CMAKE_BUILD_TYPE STREQUAL "Debug")
75 + option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE)
76 +else()
77 + option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" FALSE)
78 +endif()
79 +
80 +option(ENABLE_ADDRESS_SANITIZER "Build with address sanitizer enabled" False)
81 +mark_as_advanced(ENABLE_ADDRESS_SANITIZER)
82 +
83 +if(ENABLE_ADDRESS_SANITIZER)
84 + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
85 +endif()
86 +
87 +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")
88 +
89 +if(NOT ${DISABLE_HARDENING})
90 + add_double_extra_compiler_flag("stack-protector" "-fstack-protector-strong" "-fstack-protector")
91 + add_double_extra_compiler_flag("_FORTIFY_SOURCE" "-D_FORTIFY_SOURCE=3" "-D_FORTIFY_SOURCE=2")
92 + add_simple_extra_compiler_flag("stack-clash-protection" "-fstack-clash-protection")
93 + add_simple_extra_compiler_flag("-fcf-protection" "-fcf-protection=full")
94 + add_simple_extra_compiler_flag("branch-protection" "-mbranch-protection=standard")
95 +endif()
96 +
97 +foreach(FLAG function-sections data-sections)
98 + add_simple_extra_compiler_flag("${FLAG}" "-f${FLAG}")
99 +endforeach()
100 +
101 +add_simple_extra_compiler_flag("-Wbuiltin-macro-redefined" "-Wno-builtin-macro-redefined")
102 +add_simple_extra_compiler_flag("-fexecptions" "-fexceptions")
packaging/cmake/Modules/NetdataProtobuf.cmake
+2 -2
@@ -47,8 +47,8 @@ function(netdata_bundle_protobuf)
47
48 set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER)
49
50 - string(REPLACE "-fsanitize=address" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
51 - string(REPLACE "-fsanitize=address" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
50 + string(REPLACE "-fsanitize=address" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
51 + string(REPLACE "-fsanitize=address" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
52
53 # ignore debhelper
54 set(FETCHCONTENT_FULLY_DISCONNECTED Off)