summaryrefslogtreecommitdiff
path: root/cmake
diff options
context:
space:
mode:
authorAlex Richardson <arichardson.kde@gmail.com>2022-03-19 14:58:59 +0000
committerRalf Habacker <ralf.habacker@freenet.de>2022-03-29 12:21:46 +0000
commitd35554c111787064aa773495f4b14107b742e340 (patch)
treeefd405ca044d1996109a4da30cdb1e4b6d9620df /cmake
parent0a22950288488226c3f322151962be55aed06508 (diff)
downloaddbus-d35554c111787064aa773495f4b14107b742e340.tar.gz
cmake: Only add warning flags if the compiler supports them
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.
Diffstat (limited to 'cmake')
-rw-r--r--cmake/modules/Macros.cmake52
1 files changed, 40 insertions, 12 deletions
diff --git a/cmake/modules/Macros.cmake b/cmake/modules/Macros.cmake
index 92fe9d81..8d021d37 100644
--- a/cmake/modules/Macros.cmake
+++ b/cmake/modules/Macros.cmake
@@ -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()
#