]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
cmake: Only add warning flags if the compiler supports them
authorAlex Richardson <arichardson.kde@gmail.com>
Sat, 19 Mar 2022 14:58:59 +0000 (14:58 +0000)
committerRalf Habacker <ralf.habacker@freenet.de>
Tue, 29 Mar 2022 12:21:46 +0000 (12:21 +0000)
I am compiling for FreeBSD where the compiler is Clang and doesn't accept
all the GCC warning flags. This breaks the -Werror build:
```
error: unknown warning option '-Wduplicated-branches' [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wduplicated-cond' [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wjump-misses-init' [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wlogical-op'; did you mean '-Wlong-long'? [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wrestrict' [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wunused-but-set-variable'; did you mean '-Wunused-const-variable'? [-Werror,-Wunknown-warning-option]
```

With this change we use check_{c,cxx}_compiler_flag to check if the flag
is supported before adding it. In the future this will allow adding
clang-specific warning flags to the list of warnings as well since they
will be ignored for GCC.

CMakeLists.txt
cmake/modules/Macros.cmake

index cc4926e3a0b602399cc3a5743b74b1ecf69fbc28..890618dc74c6344da88c4c894c6efe5075f9b7e4 100644 (file)
@@ -356,6 +356,7 @@ generate_compiler_warning_flags(
         ${WARNINGS_ERRORS}
 )
 generate_compiler_warning_flags(
+    CXX
     RESULTVAR
         WARNINGS_CXXFLAGS
     WARNINGS
index 92fe9d816779c731e52d65cecd39ae7607bcd614..8d021d37245bacbfc22a13e77615787ad079838c 100644 (file)
@@ -120,17 +120,31 @@ macro(add_session_test_executable _target _source)
     )
 endmacro()
 
+include(CheckCCompilerFlag)
+include(CheckCXXCompilerFlag)
+function(check_compiler_warning_flag _flag _result _cxx)
+    string(MAKE_C_IDENTIFIER "${_flag}" _varname)
+    if (_cxx)
+        check_cxx_compiler_flag("${_flag}" HAVE_CXX_FLAG${_varname})
+        set(${_result} ${HAVE_CXX_FLAG${_varname}} PARENT_SCOPE)
+    else()
+        check_c_compiler_flag("${_flag}" HAVE_C_FLAG${_varname})
+        set(${_result} ${HAVE_C_FLAG${_varname}} PARENT_SCOPE)
+    endif()
+endfunction()
+
 # generate compiler flags from MSVC warning identifiers (e.g. '4114') or gcc warning keys (e.g. 'pointer-sign')
 # Options:
 #   [DISABLED <list>] list of warnings to disable
 #   [ERRORS <list>] list of warnings to report as error
 #   RESULTVAR <var> variable name to get results
 #   WARNINGS <list> list of warnings to add
+#   [CXX] Check if the flag is supported using the C++ compiler instead of the C compiler
 #
 macro(generate_compiler_warning_flags)
     # Support if() IN_LIST operator
     cmake_policy(SET CMP0057 NEW)
-    set(options)
+    set(options CXX)
     set(oneValueArgs RESULTVAR)
     set(multiValueArgs WARNINGS DISABLED ERRORS)
     cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
@@ -152,18 +166,29 @@ macro(generate_compiler_warning_flags)
 
     set(temp)
     foreach(warning ${ARGS_ERRORS})
-        set(temp "${temp} ${error_prefix}${warning}")
-        list(APPEND USED ${warning})
+        check_compiler_warning_flag("${error_prefix}${warning}" _flag_supported "${ARGS_CXX}")
+        if(_flag_supported)
+            set(temp "${temp} ${error_prefix}${warning}")
+            list(APPEND USED ${warning})
+        else()
+            list(APPEND USED_UNSUPPORTED ${warning})
+        endif()
     endforeach()
+
     foreach(warning ${ARGS_WARNINGS})
         if(warning IN_LIST ARGS_ERRORS)
             message(WARNING "warning '${warning}' already specified as error, ignored")
         elseif(warning IN_LIST ARGS_DISABLED)
             message(WARNING "warning '${warning}' already specified as disabled, ignored")
         elseif(NOT warning IN_LIST USED)
-            set(temp "${temp} ${enabled_prefix}${warning}")
-            list(APPEND USED_WARNINGS ${warning})
-            list(APPEND USED ${warning})
+            check_compiler_warning_flag("${enabled_prefix}${warning}" _flag_supported "${ARGS_CXX}")
+            if(_flag_supported)
+                set(temp "${temp} ${enabled_prefix}${warning}")
+                list(APPEND USED_WARNINGS ${warning})
+                list(APPEND USED ${warning})
+            else()
+                list(APPEND USED_UNSUPPORTED ${warning})
+            endif()
         endif()
     endforeach()
 
@@ -173,18 +198,21 @@ macro(generate_compiler_warning_flags)
         elseif(warning IN_LIST ARGS_WARNINGS)
             message(WARNING "disabled warning '${warning}' already specified as warning, ignored")
         elseif(NOT warning IN_LIST USED)
-            set(temp "${temp} ${disabled_prefix}${warning}")
-            list(APPEND USED_DISABLED ${warning})
-            list(APPEND USED ${warning})
+            check_compiler_warning_flag("${disabled_prefix}${warning}" _flag_supported "${ARGS_CXX}")
+            if(_flag_supported)
+                set(temp "${temp} ${disabled_prefix}${warning}")
+                list(APPEND USED_DISABLED ${warning})
+                list(APPEND USED ${warning})
+            else()
+                list(APPEND USED_UNSUPPORTED ${warning})
+            endif()
         endif()
     endforeach()
 
-    foreach(warning ${ARGS_ERRORS})
-        set(temp "${temp} ${error_prefix}${warning}")
-    endforeach()
     set(${ARGS_RESULTVAR} "${temp}")
     message(STATUS "effectively used warnings for '${ARGS_RESULTVAR}': ${USED_WARNINGS}")
     message(STATUS "effectively used disabled warnings for '${ARGS_RESULTVAR}': ${USED_DISABLED}")
+    message(STATUS "unsupported warnings for '${ARGS_RESULTVAR}': ${USED_UNSUPPORTED}")
 endmacro()
 
 #