From e875e0351b8d1aa636332c125f15669176695fbc Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Wed, 15 Apr 2026 14:23:10 -0700 Subject: [PATCH] compilers: force clang to error on unknown warning options gcc seems to error by default if an unknown warning option is passed to it: CC=gcc meson setup build -Dc_args=-Wbad gcc will fail in the project() call during the sanity check. clang does not currently doing that: CC=clang meson setup build -Dc_args=-Wbad ...will continue until an actual compilation like cc.compiles() from meson-log.txt... Sanity check compile stderr: warning: unknown warning option '-Wbad' [-Wunknown-warning-option] We should error in the sanity check to notify the user as soon as possible that they have errors in their environment. Signed-off-by: Tristan Partin --- mesonbuild/compilers/mixins/clang.py | 6 ++++++ unittests/internaltests.py | 9 +++++++++ unittests/linuxliketests.py | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mesonbuild/compilers/mixins/clang.py b/mesonbuild/compilers/mixins/clang.py index 69d29b70f1bc..89eb099c183e 100644 --- a/mesonbuild/compilers/mixins/clang.py +++ b/mesonbuild/compilers/mixins/clang.py @@ -90,6 +90,12 @@ def __init__(self, defines: T.Optional[T.Dict[str, str]]): # All Clang backends can also do LLVM IR self.can_compile_suffixes.add('ll') + def get_always_args(self) -> T.List[str]: + # Force clang to fail in sanity checks if an unknwon warning option is + # user + myargs: T.List[str] = ['-Werror=unknown-warning-option'] + return super().get_always_args() + myargs + def get_crt_compile_args(self, crt_val: str) -> T.List[str]: if not isinstance(self.linker, VisualStudioLikeLinkerMixin): return [] diff --git a/unittests/internaltests.py b/unittests/internaltests.py index fb0e4a2fd0ad..9f7bfece4433 100644 --- a/unittests/internaltests.py +++ b/unittests/internaltests.py @@ -238,6 +238,15 @@ def test_compiler_args_class_visualstudio(self): self.assertEqual(a.to_native(copy=True), ['/nologo', '/showIncludes', '/Zc:__cplusplus', '/validate-charset-']) + def test_clang_always_args_contain_werror_unknown_warning(self): + env = get_fake_env() + linker = linkers.MoldDynamicLinker([], env, MachineChoice.HOST, '-Wl,', []) + cc = ClangCCompiler([], [], '14.0.0', MachineChoice.HOST, env, linker=linker) + + always_args = cc.get_always_args() + self.assertIn('-Werror=unknown-warning-option', always_args) + + def test_msvc_unix_args_to_native(self): # joined self.assertEqual(MSVCCompiler.unix_args_to_native(['-isystemfoo']), ['/Ifoo']) diff --git a/unittests/linuxliketests.py b/unittests/linuxliketests.py index a79bd6d767ee..10401e304242 100644 --- a/unittests/linuxliketests.py +++ b/unittests/linuxliketests.py @@ -797,8 +797,8 @@ def test_cpp_std_override(self): self.assertNotIn('-std=c++98', plain_comp) self.assertNotIn('-std=c++11', plain_comp) # Now werror - self.assertIn('-Werror', plain_comp) - self.assertNotIn('-Werror', c98_comp) + self.assertIn('-Werror', plain_comp.split()) + self.assertNotIn('-Werror', c98_comp.split()) def test_run_installed(self): if is_cygwin() or is_osx():