From: Alex Richardson Date: Sat, 19 Mar 2022 14:58:59 +0000 (+0000) Subject: cmake: Only add warning flags if the compiler supports them X-Git-Tag: dbus-1.15.0~83^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d35554c111787064aa773495f4b14107b742e340;p=thirdparty%2Fdbus.git 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. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index cc4926e3a..890618dc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -356,6 +356,7 @@ generate_compiler_warning_flags( ${WARNINGS_ERRORS} ) generate_compiler_warning_flags( + CXX RESULTVAR WARNINGS_CXXFLAGS WARNINGS diff --git a/cmake/modules/Macros.cmake b/cmake/modules/Macros.cmake index 92fe9d816..8d021d372 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 of warnings to disable # [ERRORS ] list of warnings to report as error # RESULTVAR variable name to get results # WARNINGS 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() #