Fail to recognize "Wno" flags in GCC


CMake’s “check_c(xx)_compiler_flag” fails to recognize unsupported “Wno” fails in GCC. The reason is that GCC displays this note:

note: unrecognized command-line option ‘-Wno-unused-command-line-argument’ may have been intended to silence earlier diagnostics

but ONLY if some warning was produced. This is explained in the manpage

When an unrecognized warning option is requested (e.g., -Wunknown-warning), GCC emits a diagnostic stating that the option is not recognized. However, if the -Wno- form is used, the behavior is slightly different: no diagnostic is produced for -Wno-unknown-warning unless other diagnostics are being produced. This allows the use of new -Wno- options with old compilers, but if something goes wrong, the compiler warns that an unrecognized option is present.

I’ve found no way to fix this except for chanching _lang_src in “Internal/CheckCompilerFlag.cmake” so it purposely produces some warning.

Does someone have some other idea or knows what source to use so it produces a warning reliably?


This hotfix works for me

string(APPEND CMAKE_REQUIRED_FLAGS " -Wno-error=cpp")
		#warning warning
		int main() { return 0; }
	FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+"

Here’s another fix, I think it’s more reliable

check_cxx_compiler_flag(${flag} ${test})
if (${test} AND (flag MATCHES "^-Wno-") AND NOT (flag MATCHES "^-Wno-error(=|$)"))
	string(REGEX REPLACE "^-Wno-" "-W" flag_warning ${flag})
	unset(${test} CACHE)
	check_cxx_compiler_flag(${flag_warning} ${test})
endif ()