diff --git a/mesonbuild/compilers/mixins/visualstudio.py b/mesonbuild/compilers/mixins/visualstudio.py index 365421e100f8..dd6924a26563 100644 --- a/mesonbuild/compilers/mixins/visualstudio.py +++ b/mesonbuild/compilers/mixins/visualstudio.py @@ -127,6 +127,14 @@ def get_always_args(self) -> T.List[str]: # TODO: use ImmutableListProtocol[str] here instead return self.always_args.copy() + def get_compiler_args_for_mode(self, mode: CompileCheckMode) -> T.List[str]: + # Linker always-args are intended for link.exe, which Meson invokes as + # a separate build step. Compiler checks link through cl.exe instead, + # where unwrapped linker options such as /release are compiler errors. + if mode is CompileCheckMode.LINK: + return self.get_always_args() + return super().get_compiler_args_for_mode(mode) + def get_no_stdinc_args(self) -> T.List[str]: return ['/X'] diff --git a/unittests/internaltests.py b/unittests/internaltests.py index 5647a0e68a43..5b4e6c92e539 100644 --- a/unittests/internaltests.py +++ b/unittests/internaltests.py @@ -28,7 +28,7 @@ import mesonbuild.scripts.env2mfile from mesonbuild import coredata from mesonbuild.compilers.c import ClangCCompiler, GnuCCompiler -from mesonbuild.compilers.compilers import ManyInOneLinkerOptionStyle +from mesonbuild.compilers.compilers import CompileCheckMode, ManyInOneLinkerOptionStyle from mesonbuild.compilers.cpp import VisualStudioCPPCompiler from mesonbuild.compilers.d import DmdDCompiler from mesonbuild.compilers.detect import detect_c_compiler @@ -293,6 +293,13 @@ def test_compiler_args_class_visualstudio(self): a = cc.compiler_args(cc.get_always_args()) self.assertEqual(a.to_native(copy=True), ['/nologo', '/utf-8', '/Zc:__cplusplus']) + # Linker always-args must not leak into cl.exe compiler checks. In + # particular, link.exe accepts /release while cl.exe rejects it. + self.assertEqual( + cc.get_compiler_args_for_mode(CompileCheckMode.LINK), + ['/nologo', '/utf-8', '/Zc:__cplusplus'], + ) + # Ensure /source-charset: removes /utf-8 a.append('/source-charset:utf-8') self.assertEqual(a.to_native(copy=True), ['/nologo', '/Zc:__cplusplus', '/source-charset:utf-8'])