Skip to content

Commit e6bb7f2

Browse files
tristan957thesamesam
authored andcommitted
compilers: force clang{,-cl} to error on unknown warning options
gcc will 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 <tristan@partin.io> Part-of: mesonbuild#15714 Closes: mesonbuild#15714 Signed-off-by: Sam James <sam@gentoo.org>
1 parent 47d6802 commit e6bb7f2

5 files changed

Lines changed: 47 additions & 9 deletions

File tree

mesonbuild/compilers/compilers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,12 @@ def _sanity_check_compile_args(self, sourcename: str, binname: str
14491449
OptionKey(f'{self.language}_args', machine=self.for_machine))))
14501450
largs = list(T.cast('T.List[str]', optstore.get_value_for(
14511451
OptionKey(f'{self.language}_link_args', machine=self.for_machine))))
1452-
return self.exelist_no_ccache + self.get_always_args() + self.get_output_args(binname) + [sourcename] + cargs, largs
1452+
return self.exelist_no_ccache \
1453+
+ self.get_always_args() \
1454+
+ self.get_compiler_check_args(CompileCheckMode.COMPILE) \
1455+
+ self.get_output_args(binname) \
1456+
+ [sourcename] \
1457+
+ cargs, largs
14531458

14541459
@abc.abstractmethod
14551460
def _sanity_check_source_code(self) -> str:

mesonbuild/compilers/cpp.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,10 @@ def get_cpp_modules_args(self) -> T.List[str]:
975975
# clang-cl does not support /interface.
976976
return ['-fmodules', '-fmodules-ts']
977977

978+
def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]:
979+
# XXX: this is a hack because so much GnuLike stuff is in the base CPPCompiler class.
980+
return ClangClCompiler.get_compiler_check_args(self, mode)
981+
978982

979983
class IntelClCPPCompiler(VisualStudioLikeCPPCompilerMixin, IntelVisualStudioLikeCompiler, CPPCompiler):
980984

mesonbuild/compilers/mixins/visualstudio.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,14 +469,16 @@ def sanitizer_compile_args(self, target: T.Optional[BuildTarget], value: T.List[
469469
args.append('/clang:-fno-omit-frame-pointer')
470470
return args
471471

472-
def has_arguments(self, args: T.List[str], code: str, mode: CompileCheckMode) -> T.Tuple[bool, bool]:
472+
def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]:
473+
myargs: T.List[str] = []
473474
if mode != CompileCheckMode.LINK:
474-
args = args + [
475+
myargs.extend((
475476
'-Werror=unknown-argument',
476477
'-Werror=unknown-warning-option',
477478
'-Werror=unused-command-line-argument',
478-
]
479-
return super().has_arguments(args, code, mode)
479+
))
480+
481+
return super().get_compiler_check_args(mode) + myargs
480482

481483
def get_pch_base_name(self, header: str) -> str:
482484
return header

unittests/internaltests.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
import mesonbuild.scripts.depfixer
3030
import mesonbuild.scripts.env2mfile
3131
from mesonbuild import coredata
32-
from mesonbuild.compilers.c import ClangCCompiler, GnuCCompiler
33-
from mesonbuild.compilers.compilers import Compiler, ManyInOneLinkerOptionStyle
32+
from mesonbuild.compilers import Compiler
33+
from mesonbuild.compilers.c import ClangCCompiler, ClangClCCompiler, GnuCCompiler
34+
from mesonbuild.compilers.compilers import CompileCheckMode, ManyInOneLinkerOptionStyle
3435
from mesonbuild.compilers.cpp import VisualStudioCPPCompiler
3536
from mesonbuild.compilers.d import DmdDCompiler
3637
from mesonbuild.compilers.detect import detect_c_compiler
@@ -435,6 +436,27 @@ def test_clike_sanity_check_drops_link_only_args_when_compile_only(self):
435436
self.assertEqual(largs, [])
436437

437438

439+
def test_clang_family_compiler_check_args_contain_werror_unknown_warning(self):
440+
env = get_fake_env()
441+
mold = linkers.MoldDynamicLinker([], env, MachineChoice.HOST, '-Wl,', [])
442+
lld_link = linkers.ClangClDynamicLinker(env, MachineChoice.HOST, [])
443+
444+
compilers = {
445+
'clang': ClangCCompiler([], [], '14.0.0', MachineChoice.HOST, env, linker=mold),
446+
'clang-cl': ClangClCCompiler([], '14.0.0', MachineChoice.HOST, env, 'x64', linker=lld_link),
447+
}
448+
449+
for name, cc in compilers.items():
450+
with self.subTest(compiler=name):
451+
self.assertIn('-Werror=unknown-warning-option',
452+
cc.get_compiler_check_args(CompileCheckMode.COMPILE))
453+
# Both clang and clang-cl only apply this diagnostic when
454+
# actually compiling; it's intentionally omitted for LINK to
455+
# avoid failing on flags that are unused during linking.
456+
self.assertNotIn('-Werror=unknown-warning-option',
457+
cc.get_compiler_check_args(CompileCheckMode.LINK))
458+
459+
438460
def test_msvc_unix_args_to_native(self):
439461
# joined
440462
self.assertEqual(MSVCCompiler.unix_args_to_native(['-isystemfoo']), ['/Ifoo'])

unittests/linuxliketests.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,13 @@ def test_cpp_std_override(self):
845845
self.assertNotIn('-std=c++98', plain_comp)
846846
self.assertNotIn('-std=c++11', plain_comp)
847847
# Now werror
848-
self.assertIn('-Werror', plain_comp)
849-
self.assertNotIn('-Werror', c98_comp)
848+
self.assertIn('-Werror', plain_comp.split())
849+
self.assertNotIn('-Werror', c98_comp.split())
850+
851+
def test_sanity_check_fails_on_bad_c_args(self):
852+
testdir = os.path.join(self.common_test_dir, '1 trivial')
853+
with self.assertRaises((subprocess.CalledProcessError, RuntimeError)):
854+
self.init(testdir, extra_args=['-Dc_args=-Wbad-flag-does-not-exist'])
850855

851856
def test_run_installed(self):
852857
if is_cygwin() or is_osx():

0 commit comments

Comments
 (0)