Only add link arguments when needed in Compiler object methods

Currently, ComplierHolder.determine_args() unconditionally adds the link
arguments to the commmand, even if we aren't linking, because it doesn't
have access to the mode (preprocess, compile, link) that
_get_compiler_check_args() will use.

This leads to command lines like:

'cl testfile.c /nologo /showIncludes /c /Fooutput.obj /Od kernel32.lib
user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib
uuid.lib comdlg32.lib advapi32.lib'

which clang-cl considers invalid; MSVS cl accepts this, ignoring the
unneeded libraries

Change from passing extra_args down to _get_compiler_check_args(), to
passing down a callback to CompilerHolder.determine_args() (with a bound
kwargs argument), so it can consult mode and kwargs to determine the args to
use.
pull/4250/head
Jon Turney 6 years ago
parent e57fd01830
commit bb31a8c1c7
No known key found for this signature in database
GPG Key ID: C7C86F0370285C81
  1. 2
      mesonbuild/compilers/c.py
  2. 2
      mesonbuild/compilers/d.py
  3. 30
      mesonbuild/interpreter.py

@ -392,6 +392,8 @@ class CCompiler(Compiler):
return self.compiles(t.format(**fargs), env, extra_args, dependencies) return self.compiles(t.format(**fargs), env, extra_args, dependencies)
def _get_compiler_check_args(self, env, extra_args, dependencies, mode='compile'): def _get_compiler_check_args(self, env, extra_args, dependencies, mode='compile'):
if callable(extra_args):
extra_args = extra_args(mode)
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
elif isinstance(extra_args, str): elif isinstance(extra_args, str):

@ -274,6 +274,8 @@ class DCompiler(Compiler):
return ['-Wl,-rpath,{}'.format(paths)] return ['-Wl,-rpath,{}'.format(paths)]
def _get_compiler_check_args(self, env, extra_args, dependencies, mode='compile'): def _get_compiler_check_args(self, env, extra_args, dependencies, mode='compile'):
if callable(extra_args):
extra_args = extra_args(mode)
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
elif isinstance(extra_args, str): elif isinstance(extra_args, str):

@ -38,6 +38,7 @@ import subprocess
from collections import namedtuple from collections import namedtuple
from pathlib import PurePath from pathlib import PurePath
import traceback import traceback
import functools
import importlib import importlib
@ -965,7 +966,7 @@ class CompilerHolder(InterpreterObject):
def cmd_array_method(self, args, kwargs): def cmd_array_method(self, args, kwargs):
return self.compiler.exelist return self.compiler.exelist
def determine_args(self, kwargs): def determine_args(self, kwargs, mode='link'):
nobuiltins = kwargs.get('no_builtin_args', False) nobuiltins = kwargs.get('no_builtin_args', False)
if not isinstance(nobuiltins, bool): if not isinstance(nobuiltins, bool):
raise InterpreterException('Type of no_builtin_args not a boolean.') raise InterpreterException('Type of no_builtin_args not a boolean.')
@ -981,7 +982,8 @@ class CompilerHolder(InterpreterObject):
if not nobuiltins: if not nobuiltins:
opts = self.environment.coredata.compiler_options opts = self.environment.coredata.compiler_options
args += self.compiler.get_option_compile_args(opts) args += self.compiler.get_option_compile_args(opts)
args += self.compiler.get_option_link_args(opts) if mode == 'link':
args += self.compiler.get_option_link_args(opts)
args += mesonlib.stringlistify(kwargs.get('args', [])) args += mesonlib.stringlistify(kwargs.get('args', []))
return args return args
@ -1039,7 +1041,7 @@ class CompilerHolder(InterpreterObject):
testname = kwargs.get('name', '') testname = kwargs.get('name', '')
if not isinstance(testname, str): if not isinstance(testname, str):
raise InterpreterException('Testname argument must be a string.') raise InterpreterException('Testname argument must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs, endl=None) deps, msg = self.determine_dependencies(kwargs, endl=None)
result = self.compiler.run(code, self.environment, extra_args, deps) result = self.compiler.run(code, self.environment, extra_args, deps)
if len(testname) > 0: if len(testname) > 0:
@ -1094,7 +1096,7 @@ class CompilerHolder(InterpreterObject):
prefix = kwargs.get('prefix', '') prefix = kwargs.get('prefix', '')
if not isinstance(prefix, str): if not isinstance(prefix, str):
raise InterpreterException('Prefix argument of has_member must be a string.') raise InterpreterException('Prefix argument of has_member must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
had = self.compiler.has_members(typename, [membername], prefix, had = self.compiler.has_members(typename, [membername], prefix,
self.environment, extra_args, deps) self.environment, extra_args, deps)
@ -1122,7 +1124,7 @@ class CompilerHolder(InterpreterObject):
prefix = kwargs.get('prefix', '') prefix = kwargs.get('prefix', '')
if not isinstance(prefix, str): if not isinstance(prefix, str):
raise InterpreterException('Prefix argument of has_members must be a string.') raise InterpreterException('Prefix argument of has_members must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
had = self.compiler.has_members(typename, membernames, prefix, had = self.compiler.has_members(typename, membernames, prefix,
self.environment, extra_args, deps) self.environment, extra_args, deps)
@ -1175,7 +1177,7 @@ class CompilerHolder(InterpreterObject):
prefix = kwargs.get('prefix', '') prefix = kwargs.get('prefix', '')
if not isinstance(prefix, str): if not isinstance(prefix, str):
raise InterpreterException('Prefix argument of has_type must be a string.') raise InterpreterException('Prefix argument of has_type must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
had = self.compiler.has_type(typename, prefix, self.environment, extra_args, deps) had = self.compiler.has_type(typename, prefix, self.environment, extra_args, deps)
if had: if had:
@ -1213,7 +1215,7 @@ class CompilerHolder(InterpreterObject):
raise InterpreterException('High argument of compute_int must be an int.') raise InterpreterException('High argument of compute_int must be an int.')
if guess is not None and not isinstance(guess, int): if guess is not None and not isinstance(guess, int):
raise InterpreterException('Guess argument of compute_int must be an int.') raise InterpreterException('Guess argument of compute_int must be an int.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
res = self.compiler.compute_int(expression, low, high, guess, prefix, self.environment, extra_args, deps) res = self.compiler.compute_int(expression, low, high, guess, prefix, self.environment, extra_args, deps)
mlog.log('Computing int of', mlog.bold(expression, True), msg, res) mlog.log('Computing int of', mlog.bold(expression, True), msg, res)
@ -1234,7 +1236,7 @@ class CompilerHolder(InterpreterObject):
prefix = kwargs.get('prefix', '') prefix = kwargs.get('prefix', '')
if not isinstance(prefix, str): if not isinstance(prefix, str):
raise InterpreterException('Prefix argument of sizeof must be a string.') raise InterpreterException('Prefix argument of sizeof must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
esize = self.compiler.sizeof(element, prefix, self.environment, extra_args, deps) esize = self.compiler.sizeof(element, prefix, self.environment, extra_args, deps)
mlog.log('Checking for size of', mlog.bold(element, True), msg, esize) mlog.log('Checking for size of', mlog.bold(element, True), msg, esize)
@ -1256,7 +1258,7 @@ class CompilerHolder(InterpreterObject):
prefix = kwargs.get('prefix', '') prefix = kwargs.get('prefix', '')
if not isinstance(prefix, str): if not isinstance(prefix, str):
raise InterpreterException('Prefix argument of get_define() must be a string.') raise InterpreterException('Prefix argument of get_define() must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
value = self.compiler.get_define(element, prefix, self.environment, extra_args, deps) value = self.compiler.get_define(element, prefix, self.environment, extra_args, deps)
mlog.log('Fetching value of define', mlog.bold(element, True), msg, value) mlog.log('Fetching value of define', mlog.bold(element, True), msg, value)
@ -1281,7 +1283,7 @@ class CompilerHolder(InterpreterObject):
testname = kwargs.get('name', '') testname = kwargs.get('name', '')
if not isinstance(testname, str): if not isinstance(testname, str):
raise InterpreterException('Testname argument must be a string.') raise InterpreterException('Testname argument must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs, endl=None) deps, msg = self.determine_dependencies(kwargs, endl=None)
result = self.compiler.compiles(code, self.environment, extra_args, deps) result = self.compiler.compiles(code, self.environment, extra_args, deps)
if len(testname) > 0: if len(testname) > 0:
@ -1311,7 +1313,7 @@ class CompilerHolder(InterpreterObject):
testname = kwargs.get('name', '') testname = kwargs.get('name', '')
if not isinstance(testname, str): if not isinstance(testname, str):
raise InterpreterException('Testname argument must be a string.') raise InterpreterException('Testname argument must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs, endl=None) deps, msg = self.determine_dependencies(kwargs, endl=None)
result = self.compiler.links(code, self.environment, extra_args, deps) result = self.compiler.links(code, self.environment, extra_args, deps)
if len(testname) > 0: if len(testname) > 0:
@ -1338,7 +1340,7 @@ class CompilerHolder(InterpreterObject):
prefix = kwargs.get('prefix', '') prefix = kwargs.get('prefix', '')
if not isinstance(prefix, str): if not isinstance(prefix, str):
raise InterpreterException('Prefix argument of has_header must be a string.') raise InterpreterException('Prefix argument of has_header must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
haz = self.compiler.check_header(hname, prefix, self.environment, extra_args, deps) haz = self.compiler.check_header(hname, prefix, self.environment, extra_args, deps)
if haz: if haz:
@ -1363,7 +1365,7 @@ class CompilerHolder(InterpreterObject):
prefix = kwargs.get('prefix', '') prefix = kwargs.get('prefix', '')
if not isinstance(prefix, str): if not isinstance(prefix, str):
raise InterpreterException('Prefix argument of has_header must be a string.') raise InterpreterException('Prefix argument of has_header must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
haz = self.compiler.has_header(hname, prefix, self.environment, extra_args, deps) haz = self.compiler.has_header(hname, prefix, self.environment, extra_args, deps)
if haz: if haz:
@ -1389,7 +1391,7 @@ class CompilerHolder(InterpreterObject):
prefix = kwargs.get('prefix', '') prefix = kwargs.get('prefix', '')
if not isinstance(prefix, str): if not isinstance(prefix, str):
raise InterpreterException('Prefix argument of has_header_symbol must be a string.') raise InterpreterException('Prefix argument of has_header_symbol must be a string.')
extra_args = self.determine_args(kwargs) extra_args = functools.partial(self.determine_args, kwargs)
deps, msg = self.determine_dependencies(kwargs) deps, msg = self.determine_dependencies(kwargs)
haz = self.compiler.has_header_symbol(hname, symbol, prefix, self.environment, extra_args, deps) haz = self.compiler.has_header_symbol(hname, symbol, prefix, self.environment, extra_args, deps)
if haz: if haz:

Loading…
Cancel
Save