From 8e74722a0a9f3b06dbcfe7551f8145900a182368 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Wed, 29 Mar 2023 11:10:27 +0100 Subject: [PATCH] cmake: Fix `-Wno-` handling in add_cxx_flag_if_supported (#1656) The `-Wno-...` compiler flags only result in a diagnostic if another diagnostic is emitted. When passing such flags to `add_cxx_flag_if_supported` (and `check_cxx_compiler_flag`), the test would always succeed. For such cases, test the `-W...` flag instead of the `-Wno-...` flag. Signed-off-by: Sven van Haastregt --- CMakeLists.txt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f20e346e..401b13dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,9 +82,15 @@ endif() macro(add_cxx_flag_if_supported flag) string(REGEX REPLACE "[-=+]" "" FLAG_NO_SIGNS ${flag}) - check_cxx_compiler_flag(${flag} COMPILER_SUPPORTS_${FLAG_NO_SIGNS}) + set(FLAG_TO_TEST ${flag}) + if((${flag} MATCHES "^-Wno-") AND NOT (${flag} MATCHES "^-Wno-error=")) + # -Wno-... only causes a diagnostic if another diagnostic is emitted. + # Change such flags into a -W... flag to test if the warning is known. + string(REGEX REPLACE "^-Wno-" "-W" FLAG_TO_TEST ${flag}) + endif() + check_cxx_compiler_flag(${FLAG_TO_TEST} COMPILER_SUPPORTS_${FLAG_NO_SIGNS}) if(COMPILER_SUPPORTS_${FLAG_NO_SIGNS}) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") endif() endmacro(add_cxx_flag_if_supported)