Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion mesonbuild/compilers/mixins/clike.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,17 @@ def _sanity_check_compile_args(self, sourcename: str, binname: str
# Cross-compiling is hard. For example, you might need -nostdlib, or to pass --target, etc.
mode = CompileCheckMode.COMPILE if self.is_cross and not self.environment.has_exe_wrapper() else CompileCheckMode.LINK
cargs, b_largs = self._get_basic_compiler_args(mode)
largs = self.linker_to_compiler_args(b_largs)
s_args, s_largs = super()._sanity_check_compile_args(sourcename, binname)
if mode is CompileCheckMode.COMPILE:
# We aren't linking in this invocation (and can't run the result
# without an exe wrapper anyway), so don't pass any link-only
# arguments to a compile-only command. Some compilers add fixed
# flags (e.g. MSVC-style compilers always prepend /link) even
# when there is nothing to link, which would otherwise end up
# unused/misplaced in a compile-only invocation.
return s_args + cargs, []

largs = self.linker_to_compiler_args(b_largs)
return s_args + cargs, s_largs + largs

def check_header(self, hname: str, prefix: str, *,
Expand Down
21 changes: 20 additions & 1 deletion unittests/internaltests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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 Compiler, ManyInOneLinkerOptionStyle
from mesonbuild.compilers.cpp import VisualStudioCPPCompiler
from mesonbuild.compilers.d import DmdDCompiler
from mesonbuild.compilers.detect import detect_c_compiler
Expand Down Expand Up @@ -416,6 +416,25 @@ def test_compiler_args_class_visualstudio(self):
self.assertEqual(a.to_native(copy=True), ['/showIncludes'])


def test_clike_sanity_check_drops_link_only_args_when_compile_only(self):
# When cross-compiling without an exe wrapper, CLikeCompiler's sanity
# check only compiles and never links because we can't run the
# executable. Therefore, it doesn't make sense for link-only arguments
# to be added on the command line. Some compilers warn about unused
# command line arguments.
env = get_fake_env()
linker = linkers.MoldDynamicLinker([], env, MachineChoice.HOST, '-Wl,', [])
cc = ClangCCompiler([], [], '14.0.0', MachineChoice.HOST, env, linker=linker)
cc.is_cross = True

with mock.patch.object(env, 'has_exe_wrapper', return_value=False), \
mock.patch.object(cc, '_get_basic_compiler_args', return_value=([], ['-fake-cross-link-arg'])), \
mock.patch.object(Compiler, '_sanity_check_compile_args', return_value=([], ['-Lfake-ldflags-arg'])):
_, largs = cc._sanity_check_compile_args('foo.c', 'foo.exe')

self.assertEqual(largs, [])


def test_msvc_unix_args_to_native(self):
# joined
self.assertEqual(MSVCCompiler.unix_args_to_native(['-isystemfoo']), ['/Ifoo'])
Expand Down
Loading