Actually handle the `-fexceptions` requirement correctly in our build system. (#19534)
Austin S. Hemmelgarn committed
Jan 31, 2025 at 08:20 UTC
34236bffa4bec1952eb796e22f65900b82269866
3 files changed
+22
-10
CMakeLists.txt
-5
@@ -83,11 +83,6 @@ if(USE_MOLD)
83
endif()
84
endif()
85
86
-check_c_compiler_flag("-fexceptions" HAVE_FEXCEPTIONS)
87
-if (NOT HAVE_FEXCEPTIONS)
88
- message(FATAL_ERROR "Missing required compiler flag: -fexceptions.")
89
-endif()
90
-
86
set(CONFIG_H_DIR ${CMAKE_BINARY_DIR})
87
set(CONFIG_H ${CONFIG_H_DIR}/config.h)
88
packaging/cmake/Modules/NetdataCompilerFlags.cmake
+22
@@ -69,6 +69,26 @@ function(add_double_extra_compiler_flag match flag1 flag2)
69
endif()
70
endfunction()
71
72
+# Add a required extra compiler flag to C and C++ flags.
73
+#
74
+# Similar logic as add_simple_extra_compiler_flag, but ignores existing
75
+# instances and throws an error if the flag is not supported.
76
+function(add_required_compiler_flag flag)
77
+ set(CMAKE_REQUIRED_FLAGS "-Werror")
78
+
79
+ make_cpp_safe_name("${flag}" flag_name)
80
+
81
+ check_c_compiler_flag("${flag}" HAVE_C_${flag_name})
82
+ check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name})
83
+
84
+ if(HAVE_C_${flag_name} AND HAVE_CXX_${flag_name})
85
+ add_compile_options("${flag}")
86
+ add_link_options("${flag}")
87
+ else()
88
+ message(FATAL_ERROR "${flag} support is required to build Netdata")
89
+ endif()
90
+endfunction()
91
+
92
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
93
option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE)
94
else()
@@ -84,6 +104,8 @@ endif()
104
105
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")
106
107
+add_required_compiler_flag("-fexceptions")
108
+
109
if(NOT ${DISABLE_HARDENING})
110
add_double_extra_compiler_flag("stack-protector" "-fstack-protector-strong" "-fstack-protector")
111
add_double_extra_compiler_flag("_FORTIFY_SOURCE" "-D_FORTIFY_SOURCE=3" "-D_FORTIFY_SOURCE=2")
src/daemon/main.c
-5
@@ -228,11 +228,6 @@ int unittest_prepare_rrd(const char **user) {
228
}
229
230
int netdata_main(int argc, char **argv) {
231
-#if !defined(HAVE_FEXCEPTIONS)
232
- fprintf(stderr, "Netdata has been compiled without these required compiler flags: -fexceptions.\n");
233
- exit(1);
234
-#endif
235
-
231
string_init();
232
analytics_init();
233