interpreter: Move argument checks from add_*_arguments to compiler.get_supported_arguments

pull/8898/head
Laurin-Luis Lehning 4 years ago
parent 377adfb6fb
commit d5ed8f61a5
  1. 6
      docs/markdown/snippets/compiler_argument_checking.md
  2. 8
      docs/markdown/snippets/required_project_arguments.md
  3. 19
      mesonbuild/interpreter/compiler.py
  4. 21
      mesonbuild/interpreter/interpreter.py
  5. 1
      mesonbuild/interpreter/kwargs.py
  6. 4
      test cases/failing/115 compiler argument checking/meson.build
  7. 7
      test cases/failing/115 compiler argument checking/test.json
  8. 3
      test cases/failing/115 required project arguments/meson.build
  9. 7
      test cases/failing/115 required project arguments/test.json

@ -0,0 +1,6 @@
## Compiler argument checking for `get_supported_arguments`
The compiler method `get_supported_arguments` now supports
a new keyword argument named `checked` that can be set to
one of `warn`, `require` or `off` (defaults to `off`) to
enforce argument checks.

@ -1,8 +0,0 @@
## Required project & global arguments
The `add_global_arguments` & `add_project_arguments` functions now
support an optional keyword argument named `required`. Setting it
to `true` will cause Meson to perform compiler support checks on
each of the passed arguments as if by calling `has_argument`, which
means manual checks can now be avoided and instead expressed more
tersely.

@ -1,4 +1,5 @@
import functools
from mesonbuild.interpreterbase.decorators import typed_kwargs, KwargInfo
from .interpreterobjects import (extract_required_kwarg, extract_search_dirs)
@ -684,12 +685,24 @@ class CompilerHolder(ObjectHolder['Compiler']):
return result
@FeatureNew('compiler.get_supported_arguments', '0.43.0')
@permittedKwargs({})
def get_supported_arguments_method(self, args, kwargs):
@FeatureNewKwargs('compiler.get_supported_arguments', '0.59.0', ['checked'])
@typed_kwargs('compiler.get_supported_arguments', KwargInfo('checked', str, default='off'))
def get_supported_arguments_method(self, args: T.Sequence[str], kwargs: T.Dict[str, T.Any]):
args = mesonlib.stringlistify(args)
supported_args = []
checked = kwargs.pop('checked')
if checked not in ['warn', 'require', 'off']:
raise mesonlib.MesonException('"checked" kwarg must be one of "warn", "require" or "off"')
for arg in args:
if self.has_argument_method(arg, kwargs):
if not self.has_argument_method(arg, kwargs):
msg = f'Compiler for {self.compiler.get_display_language()} does not support "{arg}"'
if checked == 'warn':
mlog.warning(msg)
elif checked == 'require':
raise mesonlib.MesonException(msg)
else:
supported_args.append(arg)
return supported_args

@ -2321,9 +2321,8 @@ This will become a hard error in the future.''' % kwargs['input'], location=self
self.build.test_setups[setup_name] = build.TestSetup(exe_wrapper, gdb, timeout_multiplier, env,
exclude_suites)
@FeatureNewKwargs('add_global_arguments', '0.59.0', ['required'])
@typed_pos_args('add_global_arguments', varargs=str)
@typed_kwargs('add_global_arguments', _NATIVE_KW, _LANGUAGE_KW, KwargInfo('required', bool, default=False))
@typed_kwargs('add_global_arguments', _NATIVE_KW, _LANGUAGE_KW)
def func_add_global_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'kwargs.FuncAddProjectArgs') -> None:
self._add_global_arguments(node, self.build.global_args[kwargs['native']], args[0], kwargs)
@ -2332,9 +2331,8 @@ This will become a hard error in the future.''' % kwargs['input'], location=self
def func_add_global_link_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'kwargs.FuncAddProjectArgs') -> None:
self._add_global_arguments(node, self.build.global_link_args[kwargs['native']], args[0], kwargs)
@FeatureNewKwargs('add_project_arguments', '0.59.0', ['required'])
@typed_pos_args('add_project_arguments', varargs=str)
@typed_kwargs('add_project_arguments', _NATIVE_KW, _LANGUAGE_KW, KwargInfo('required', bool, default=False))
@typed_kwargs('add_project_arguments', _NATIVE_KW, _LANGUAGE_KW)
def func_add_project_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'kwargs.FuncAddProjectArgs') -> None:
self._add_project_arguments(node, self.build.projects_args[kwargs['native']], args[0], kwargs)
@ -2400,21 +2398,6 @@ This will become a hard error in the future.''' % kwargs['input'], location=self
self._warn_about_builtin_args(args)
try:
if kwargs['required']:
compilers = self.coredata.compilers.build.values() if kwargs['native'] == MachineChoice.BUILD \
else self.coredata.compilers.host.values()
for c in compilers:
if not c.language in kwargs['language']:
continue
for arg in args:
if not c.has_multi_arguments([arg], self.environment)[0]:
raise mesonlib.MesonException(f'C compiler does not support "{arg}"')
except KeyError:
pass
for lang in kwargs['language']:
argsdict[lang] = argsdict.get(lang, []) + args

@ -27,7 +27,6 @@ class FuncAddProjectArgs(TypedDict):
native: MachineChoice
language: T.List[str]
required: bool
class BaseTest(TypedDict):

@ -0,0 +1,4 @@
project('compiler argument checking test', 'c')
cc = meson.get_compiler('c')
add_project_arguments(cc.get_supported_arguments('-meson-goober-arg-for-testing', checked : 'require'), language : 'c')

@ -0,0 +1,7 @@
{
"stdout": [
{
"line": "test cases/failing/115 compiler argument checking/meson.build:4:0: ERROR: Compiler for C does not support \"-meson-goober-arg-for-testing\""
}
]
}

@ -1,3 +0,0 @@
project('add_project_arguments test', 'c')
add_project_arguments('-meson-goober-arg-for-testing', language : 'c', required : true)

@ -1,7 +0,0 @@
{
"stdout": [
{
"line": "test cases/failing/115 required project arguments/meson.build:3:0: ERROR: C compiler does not support \"-meson-goober-arg-for-testing\""
}
]
}
Loading…
Cancel
Save