The Meson Build System
http://mesonbuild.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1584 lines
70 KiB
1584 lines
70 KiB
8 years ago
|
# Copyright 2016-2017 The Meson development team
|
||
8 years ago
|
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
|
|
||
|
# This class contains the basic functionality needed to run any interpreter
|
||
|
# or an interpreter-based tool.
|
||
|
|
||
4 years ago
|
from .. import mparser, mesonlib, mlog
|
||
|
from .. import environment, dependencies
|
||
8 years ago
|
|
||
4 years ago
|
from functools import wraps
|
||
5 years ago
|
import abc
|
||
5 years ago
|
import collections.abc
|
||
4 years ago
|
import itertools
|
||
|
import os, copy, re
|
||
5 years ago
|
import typing as T
|
||
5 years ago
|
|
||
5 years ago
|
TV_fw_var = T.Union[str, int, float, bool, list, dict, 'InterpreterObject', 'ObjectHolder']
|
||
|
TV_fw_args = T.List[T.Union[mparser.BaseNode, TV_fw_var]]
|
||
|
TV_fw_kwargs = T.Dict[str, T.Union[mparser.BaseNode, TV_fw_var]]
|
||
|
|
||
|
TV_func = T.TypeVar('TV_func', bound=T.Callable[..., T.Any])
|
||
|
|
||
|
TYPE_elementary = T.Union[str, int, float, bool]
|
||
|
TYPE_var = T.Union[TYPE_elementary, T.List[T.Any], T.Dict[str, T.Any], 'InterpreterObject', 'ObjectHolder']
|
||
|
TYPE_nvar = T.Union[TYPE_var, mparser.BaseNode]
|
||
|
TYPE_nkwargs = T.Dict[str, TYPE_nvar]
|
||
|
TYPE_key_resolver = T.Callable[[mparser.BaseNode], str]
|
||
|
|
||
5 years ago
|
class InterpreterObject:
|
||
5 years ago
|
def __init__(self) -> None:
|
||
|
self.methods = {} # type: T.Dict[str, T.Callable[[T.List[TYPE_nvar], TYPE_nkwargs], TYPE_var]]
|
||
5 years ago
|
# Current node set during a method call. This can be used as location
|
||
|
# when printing a warning message during a method call.
|
||
|
self.current_node = None # type: mparser.BaseNode
|
||
|
|
||
5 years ago
|
def method_call(
|
||
|
self,
|
||
|
method_name: str,
|
||
|
args: TV_fw_args,
|
||
|
kwargs: TV_fw_kwargs
|
||
|
) -> TYPE_var:
|
||
5 years ago
|
if method_name in self.methods:
|
||
|
method = self.methods[method_name]
|
||
|
if not getattr(method, 'no-args-flattening', False):
|
||
|
args = flatten(args)
|
||
|
return method(args, kwargs)
|
||
|
raise InvalidCode('Unknown method "%s" in object.' % method_name)
|
||
8 years ago
|
|
||
5 years ago
|
TV_InterpreterObject = T.TypeVar('TV_InterpreterObject')
|
||
5 years ago
|
|
||
5 years ago
|
class ObjectHolder(T.Generic[TV_InterpreterObject]):
|
||
4 years ago
|
def __init__(self, obj: TV_InterpreterObject, subproject: str = '') -> None:
|
||
|
self.held_object = obj
|
||
|
self.subproject = subproject
|
||
7 years ago
|
|
||
5 years ago
|
def __repr__(self) -> str:
|
||
4 years ago
|
return f'<Holder: {self.held_object!r}>'
|
||
7 years ago
|
|
||
5 years ago
|
class MesonVersionString(str):
|
||
|
pass
|
||
|
|
||
4 years ago
|
class RangeHolder(InterpreterObject):
|
||
|
def __init__(self, start: int, stop: int, step: int) -> None:
|
||
|
super().__init__()
|
||
|
self.range = range(start, stop, step)
|
||
|
|
||
|
def __iter__(self) -> T.Iterator[int]:
|
||
|
return iter(self.range)
|
||
|
|
||
|
def __getitem__(self, key: int) -> int:
|
||
|
return self.range[key]
|
||
|
|
||
|
def __len__(self) -> int:
|
||
|
return len(self.range)
|
||
|
|
||
8 years ago
|
# Decorators for method calls.
|
||
|
|
||
5 years ago
|
def check_stringlist(a: T.Any, msg: str = 'Arguments must be strings.') -> None:
|
||
8 years ago
|
if not isinstance(a, list):
|
||
|
mlog.debug('Not a list:', str(a))
|
||
|
raise InvalidArguments('Argument not a list.')
|
||
|
if not all(isinstance(s, str) for s in a):
|
||
|
mlog.debug('Element not a string:', str(a))
|
||
|
raise InvalidArguments(msg)
|
||
|
|
||
5 years ago
|
def _get_callee_args(wrapped_args: T.Sequence[T.Any], want_subproject: bool = False) -> T.Tuple[T.Any, mparser.BaseNode, TV_fw_args, TV_fw_kwargs, T.Optional[str]]:
|
||
7 years ago
|
s = wrapped_args[0]
|
||
7 years ago
|
n = len(wrapped_args)
|
||
7 years ago
|
# Raise an error if the codepaths are not there
|
||
5 years ago
|
subproject = None # type: T.Optional[str]
|
||
7 years ago
|
if want_subproject and n == 2:
|
||
|
if hasattr(s, 'subproject'):
|
||
|
# Interpreter base types have 2 args: self, node
|
||
6 years ago
|
node = wrapped_args[1]
|
||
7 years ago
|
# args and kwargs are inside the node
|
||
|
args = None
|
||
|
kwargs = None
|
||
|
subproject = s.subproject
|
||
|
elif hasattr(wrapped_args[1], 'subproject'):
|
||
|
# Module objects have 2 args: self, interpreter
|
||
6 years ago
|
node = wrapped_args[1].current_node
|
||
7 years ago
|
# args and kwargs are inside the node
|
||
|
args = None
|
||
|
kwargs = None
|
||
|
subproject = wrapped_args[1].subproject
|
||
|
else:
|
||
4 years ago
|
raise AssertionError(f'Unknown args: {wrapped_args!r}')
|
||
7 years ago
|
elif n == 3:
|
||
|
# Methods on objects (*Holder, MesonMain, etc) have 3 args: self, args, kwargs
|
||
6 years ago
|
node = s.current_node
|
||
7 years ago
|
args = wrapped_args[1]
|
||
|
kwargs = wrapped_args[2]
|
||
7 years ago
|
if want_subproject:
|
||
|
if hasattr(s, 'subproject'):
|
||
|
subproject = s.subproject
|
||
|
elif hasattr(s, 'interpreter'):
|
||
|
subproject = s.interpreter.subproject
|
||
7 years ago
|
elif n == 4:
|
||
|
# Meson functions have 4 args: self, node, args, kwargs
|
||
6 years ago
|
# Module functions have 4 args: self, state, args, kwargs
|
||
|
if isinstance(s, InterpreterBase):
|
||
|
node = wrapped_args[1]
|
||
|
else:
|
||
|
node = wrapped_args[1].current_node
|
||
7 years ago
|
args = wrapped_args[2]
|
||
|
kwargs = wrapped_args[3]
|
||
7 years ago
|
if want_subproject:
|
||
|
if isinstance(s, InterpreterBase):
|
||
|
subproject = s.subproject
|
||
|
else:
|
||
6 years ago
|
subproject = wrapped_args[1].subproject
|
||
7 years ago
|
else:
|
||
4 years ago
|
raise AssertionError(f'Unknown args: {wrapped_args!r}')
|
||
7 years ago
|
# Sometimes interpreter methods are called internally with None instead of
|
||
|
# empty list/dict
|
||
|
args = args if args is not None else []
|
||
|
kwargs = kwargs if kwargs is not None else {}
|
||
6 years ago
|
return s, node, args, kwargs, subproject
|
||
7 years ago
|
|
||
5 years ago
|
def flatten(args: T.Union[TYPE_nvar, T.List[TYPE_nvar]]) -> T.List[TYPE_nvar]:
|
||
7 years ago
|
if isinstance(args, mparser.StringNode):
|
||
5 years ago
|
assert isinstance(args.value, str)
|
||
|
return [args.value]
|
||
5 years ago
|
if not isinstance(args, collections.abc.Sequence):
|
||
5 years ago
|
return [args]
|
||
5 years ago
|
result = [] # type: T.List[TYPE_nvar]
|
||
7 years ago
|
for a in args:
|
||
|
if isinstance(a, list):
|
||
|
rest = flatten(a)
|
||
|
result = result + rest
|
||
|
elif isinstance(a, mparser.StringNode):
|
||
|
result.append(a.value)
|
||
|
else:
|
||
|
result.append(a)
|
||
|
return result
|
||
|
|
||
5 years ago
|
def noPosargs(f: TV_func) -> TV_func:
|
||
8 years ago
|
@wraps(f)
|
||
5 years ago
|
def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
7 years ago
|
args = _get_callee_args(wrapped_args)[2]
|
||
8 years ago
|
if args:
|
||
8 years ago
|
raise InvalidArguments('Function does not take positional arguments.')
|
||
7 years ago
|
return f(*wrapped_args, **wrapped_kwargs)
|
||
5 years ago
|
return T.cast(TV_func, wrapped)
|
||
8 years ago
|
|
||
5 years ago
|
def builtinMethodNoKwargs(f: TV_func) -> TV_func:
|
||
5 years ago
|
@wraps(f)
|
||
5 years ago
|
def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
5 years ago
|
node = wrapped_args[0].current_node
|
||
|
method_name = wrapped_args[2]
|
||
|
kwargs = wrapped_args[4]
|
||
|
if kwargs:
|
||
4 years ago
|
mlog.warning(f'Method {method_name!r} does not take keyword arguments.',
|
||
5 years ago
|
'This will become a hard error in the future',
|
||
|
location=node)
|
||
|
return f(*wrapped_args, **wrapped_kwargs)
|
||
5 years ago
|
return T.cast(TV_func, wrapped)
|
||
5 years ago
|
|
||
5 years ago
|
def noKwargs(f: TV_func) -> TV_func:
|
||
8 years ago
|
@wraps(f)
|
||
5 years ago
|
def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
7 years ago
|
kwargs = _get_callee_args(wrapped_args)[3]
|
||
8 years ago
|
if kwargs:
|
||
8 years ago
|
raise InvalidArguments('Function does not take keyword arguments.')
|
||
7 years ago
|
return f(*wrapped_args, **wrapped_kwargs)
|
||
5 years ago
|
return T.cast(TV_func, wrapped)
|
||
8 years ago
|
|
||
5 years ago
|
def stringArgs(f: TV_func) -> TV_func:
|
||
8 years ago
|
@wraps(f)
|
||
5 years ago
|
def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
7 years ago
|
args = _get_callee_args(wrapped_args)[2]
|
||
8 years ago
|
assert(isinstance(args, list))
|
||
|
check_stringlist(args)
|
||
7 years ago
|
return f(*wrapped_args, **wrapped_kwargs)
|
||
5 years ago
|
return T.cast(TV_func, wrapped)
|
||
8 years ago
|
|
||
5 years ago
|
def noArgsFlattening(f: TV_func) -> TV_func:
|
||
6 years ago
|
setattr(f, 'no-args-flattening', True) # noqa: B010
|
||
7 years ago
|
return f
|
||
|
|
||
5 years ago
|
def disablerIfNotFound(f: TV_func) -> TV_func:
|
||
7 years ago
|
@wraps(f)
|
||
5 years ago
|
def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
7 years ago
|
kwargs = _get_callee_args(wrapped_args)[3]
|
||
|
disabler = kwargs.pop('disabler', False)
|
||
|
ret = f(*wrapped_args, **wrapped_kwargs)
|
||
|
if disabler and not ret.held_object.found():
|
||
|
return Disabler()
|
||
|
return ret
|
||
5 years ago
|
return T.cast(TV_func, wrapped)
|
||
7 years ago
|
|
||
8 years ago
|
class permittedKwargs:
|
||
|
|
||
5 years ago
|
def __init__(self, permitted: T.Set[str]):
|
||
5 years ago
|
self.permitted = permitted # type: T.Set[str]
|
||
8 years ago
|
|
||
5 years ago
|
def __call__(self, f: TV_func) -> TV_func:
|
||
8 years ago
|
@wraps(f)
|
||
5 years ago
|
def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
6 years ago
|
s, node, args, kwargs, _ = _get_callee_args(wrapped_args)
|
||
8 years ago
|
for k in kwargs:
|
||
|
if k not in self.permitted:
|
||
4 years ago
|
mlog.warning(f'''Passed invalid keyword argument "{k}".''', location=node)
|
||
7 years ago
|
mlog.warning('This will become a hard error in the future.')
|
||
7 years ago
|
return f(*wrapped_args, **wrapped_kwargs)
|
||
5 years ago
|
return T.cast(TV_func, wrapped)
|
||
7 years ago
|
|
||
4 years ago
|
|
||
4 years ago
|
def typed_pos_args(name: str, *types: T.Union[T.Type, T.Tuple[T.Type, ...]],
|
||
4 years ago
|
varargs: T.Optional[T.Union[T.Type, T.Tuple[T.Type, ...]]] = None,
|
||
|
optargs: T.Optional[T.List[T.Union[T.Type, T.Tuple[T.Type, ...]]]] = None,
|
||
4 years ago
|
min_varargs: int = 0, max_varargs: int = 0) -> T.Callable[..., T.Any]:
|
||
4 years ago
|
"""Decorator that types type checking of positional arguments.
|
||
|
|
||
4 years ago
|
This supports two different models of optional aguments, the first is the
|
||
|
variadic argument model. Variadic arguments are a possibly bounded,
|
||
|
possibly unbounded number of arguments of the same type (unions are
|
||
|
supported). The second is the standard default value model, in this case
|
||
|
a number of optional arguments may be provided, but they are still
|
||
|
ordered, and they may have different types.
|
||
|
|
||
|
This function does not support mixing variadic and default arguments.
|
||
|
|
||
|
:name: The name of the decorated function (as displayed in error messages)
|
||
|
:varargs: They type(s) of any variadic arguments the function takes. If
|
||
|
None the function takes no variadic args
|
||
|
:min_varargs: the minimum number of variadic arguments taken
|
||
|
:max_varargs: the maximum number of variadic arguments taken. 0 means unlimited
|
||
|
:optargs: The types of any optional arguments parameters taken. If None
|
||
|
then no optional paramters are taken.
|
||
|
|
||
4 years ago
|
Some examples of usage blow:
|
||
|
>>> @typed_pos_args('mod.func', str, (str, int))
|
||
|
... def func(self, state: ModuleState, args: T.Tuple[str, T.Union[str, int]], kwargs: T.Dict[str, T.Any]) -> T.Any:
|
||
|
... pass
|
||
|
|
||
|
>>> @typed_pos_args('method', str, varargs=str)
|
||
|
... def method(self, node: BaseNode, args: T.Tuple[str, T.List[str]], kwargs: T.Dict[str, T.Any]) -> T.Any:
|
||
|
... pass
|
||
|
|
||
|
>>> @typed_pos_args('method', varargs=str, min_varargs=1)
|
||
|
... def method(self, node: BaseNode, args: T.Tuple[T.List[str]], kwargs: T.Dict[str, T.Any]) -> T.Any:
|
||
|
... pass
|
||
|
|
||
|
>>> @typed_pos_args('method', str, optargs=[(str, int), str])
|
||
|
... def method(self, node: BaseNode, args: T.Tuple[str, T.Optional[T.Union[str, int]], T.Optional[str]], kwargs: T.Dict[str, T.Any]) -> T.Any:
|
||
|
... pass
|
||
|
|
||
|
When should you chose `typed_pos_args('name', varargs=str,
|
||
|
min_varargs=1)` vs `typed_pos_args('name', str, varargs=str)`?
|
||
|
|
||
|
The answer has to do with the semantics of the function, if all of the
|
||
|
inputs are the same type (such as with `files()`) then the former is
|
||
|
correct, all of the arguments are string names of files. If the first
|
||
|
argument is something else the it should be separated.
|
||
4 years ago
|
"""
|
||
|
def inner(f: TV_func) -> TV_func:
|
||
|
|
||
|
@wraps(f)
|
||
|
def wrapper(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
|
args = _get_callee_args(wrapped_args)[2]
|
||
4 years ago
|
|
||
|
# These are implementation programming errors, end users should never see them.
|
||
4 years ago
|
assert isinstance(args, list), args
|
||
4 years ago
|
assert max_varargs >= 0, 'max_varags cannot be negative'
|
||
|
assert min_varargs >= 0, 'min_varags cannot be negative'
|
||
4 years ago
|
assert optargs is None or varargs is None, \
|
||
|
'varargs and optargs not supported together as this would be ambiguous'
|
||
4 years ago
|
|
||
|
num_args = len(args)
|
||
|
num_types = len(types)
|
||
4 years ago
|
a_types = types
|
||
4 years ago
|
|
||
|
if varargs:
|
||
4 years ago
|
min_args = num_types + min_varargs
|
||
|
max_args = num_types + max_varargs
|
||
|
if max_varargs == 0 and num_args < min_args:
|
||
|
raise InvalidArguments(f'{name} takes at least {min_args} arguments, but got {num_args}.')
|
||
|
elif max_varargs != 0 and (num_args < min_args or num_args > max_args):
|
||
|
raise InvalidArguments(f'{name} takes between {min_args} and {max_args} arguments, but got {num_args}.')
|
||
4 years ago
|
elif optargs:
|
||
|
if num_args < num_types:
|
||
|
raise InvalidArguments(f'{name} takes at least {num_types} arguments, but got {num_args}.')
|
||
|
elif num_args > num_types + len(optargs):
|
||
|
raise InvalidArguments(f'{name} takes at most {num_types + len(optargs)} arguments, but got {num_args}.')
|
||
|
# Add the number of positional arguments required
|
||
|
if num_args > num_types:
|
||
|
diff = num_args - num_types
|
||
|
a_types = tuple(list(types) + list(optargs[:diff]))
|
||
4 years ago
|
elif num_args != num_types:
|
||
|
raise InvalidArguments(f'{name} takes exactly {num_types} arguments, but got {num_args}.')
|
||
|
|
||
4 years ago
|
for i, (arg, type_) in enumerate(itertools.zip_longest(args, a_types, fillvalue=varargs), start=1):
|
||
4 years ago
|
if not isinstance(arg, type_):
|
||
|
if isinstance(type_, tuple):
|
||
|
shouldbe = 'one of: {}'.format(", ".join(f'"{t.__name__}"' for t in type_))
|
||
|
else:
|
||
|
shouldbe = f'"{type_.__name__}"'
|
||
|
raise InvalidArguments(f'{name} argument {i} was of type "{type(arg).__name__}" but should have been {shouldbe}')
|
||
|
|
||
|
# Ensure that we're actually passing a tuple.
|
||
|
# Depending on what kind of function we're calling the length of
|
||
|
# wrapped_args can vary.
|
||
|
nargs = list(wrapped_args)
|
||
|
i = nargs.index(args)
|
||
4 years ago
|
if varargs:
|
||
|
# if we have varargs we need to split them into a separate
|
||
|
# tuple, as python's typing doesn't understand tuples with
|
||
|
# fixed elements and variadic elements, only one or the other.
|
||
4 years ago
|
# so in that case we need T.Tuple[int, str, float, T.Tuple[str, ...]]
|
||
4 years ago
|
pos = args[:len(types)]
|
||
|
var = list(args[len(types):])
|
||
|
pos.append(var)
|
||
|
nargs[i] = tuple(pos)
|
||
4 years ago
|
elif optargs:
|
||
|
if num_args < num_types + len(optargs):
|
||
|
diff = num_types + len(optargs) - num_args
|
||
|
nargs[i] = tuple(list(args) + [None] * diff)
|
||
|
else:
|
||
|
nargs[i] = args
|
||
4 years ago
|
else:
|
||
|
nargs[i] = tuple(args)
|
||
4 years ago
|
return f(*nargs, **wrapped_kwargs)
|
||
|
|
||
|
return T.cast(TV_func, wrapper)
|
||
|
return inner
|
||
|
|
||
|
|
||
4 years ago
|
class ContainerTypeInfo:
|
||
|
|
||
|
"""Container information for keyword arguments.
|
||
|
|
||
|
For keyword arguments that are containers (list or dict), this class encodes
|
||
|
that information.
|
||
|
|
||
|
:param container: the type of container
|
||
|
:param contains: the types the container holds
|
||
|
:param pairs: if the container is supposed to be of even length.
|
||
|
This is mainly used for interfaces that predate the addition of dictionaries, and use
|
||
|
`[key, value, key2, value2]` format.
|
||
|
:param allow_empty: Whether this container is allowed to be empty
|
||
|
There are some cases where containers not only must be passed, but must
|
||
|
not be empty, and other cases where an empty container is allowed.
|
||
|
"""
|
||
|
|
||
|
def __init__(self, container: T.Type, contains: T.Union[T.Type, T.Tuple[T.Type, ...]], *,
|
||
|
pairs: bool = False, allow_empty: bool = True) :
|
||
|
self.container = container
|
||
|
self.contains = contains
|
||
|
self.pairs = pairs
|
||
|
self.allow_empty = allow_empty
|
||
|
|
||
|
def check(self, value: T.Any) -> T.Optional[str]:
|
||
|
"""Check that a value is valid.
|
||
|
|
||
|
:param value: A value to check
|
||
|
:return: If there is an error then a string message, otherwise None
|
||
|
"""
|
||
|
if not isinstance(value, self.container):
|
||
|
return f'container type was "{type(value).__name__}", but should have been "{self.container.__name__}"'
|
||
|
iter_ = iter(value.values()) if isinstance(value, dict) else iter(value)
|
||
|
for each in iter_:
|
||
|
if not isinstance(each, self.contains):
|
||
|
if isinstance(self.contains, tuple):
|
||
|
shouldbe = 'one of: {}'.format(", ".join(f'"{t.__name__}"' for t in self.contains))
|
||
|
else:
|
||
|
shouldbe = f'"{self.contains.__name__}"'
|
||
|
return f'contained a value of type "{type(each).__name__}" but should have been {shouldbe}'
|
||
|
if self.pairs and len(value) % 2 != 0:
|
||
|
return 'container should be of even length, but is not'
|
||
|
if not value and not self.allow_empty:
|
||
|
return 'container is empty, but not allowed to be'
|
||
|
return None
|
||
|
|
||
|
|
||
|
_T = T.TypeVar('_T')
|
||
|
|
||
|
|
||
|
class KwargInfo(T.Generic[_T]):
|
||
|
|
||
|
"""A description of a keyword argument to a meson function
|
||
|
|
||
|
This is used to describe a value to the :func:typed_kwargs function.
|
||
|
|
||
|
:param name: the name of the parameter
|
||
|
:param types: A type or tuple of types that are allowed, or a :class:ContainerType
|
||
|
:param required: Whether this is a required keyword argument. defaults to False
|
||
|
:param listify: If true, then the argument will be listified before being
|
||
|
checked. This is useful for cases where the Meson DSL allows a scalar or
|
||
|
a container, but internally we only want to work with containers
|
||
4 years ago
|
:param default: A default value to use if this isn't set. defaults to None,
|
||
|
this may be safely set to a mutable type, as long as that type does not
|
||
|
itself contain mutable types, typed_kwargs will copy the default
|
||
4 years ago
|
:param since: Meson version in which this argument has been added. defaults to None
|
||
|
:param deprecated: Meson version in which this argument has been deprecated. defaults to None
|
||
4 years ago
|
:param validator: A callable that does additional validation. This is mainly
|
||
|
intended for cases where a string is expected, but only a few specific
|
||
|
values are accepted. Must return None if the input is valid, or a
|
||
|
message if the input is invalid
|
||
4 years ago
|
:param convertor: A callable that converts the raw input value into a
|
||
|
different type. This is intended for cases such as the meson DSL using a
|
||
|
string, but the implementation using an Enum. This should not do
|
||
|
validation, just converstion.
|
||
4 years ago
|
"""
|
||
|
|
||
|
def __init__(self, name: str, types: T.Union[T.Type[_T], T.Tuple[T.Type[_T], ...], ContainerTypeInfo],
|
||
4 years ago
|
*, required: bool = False, listify: bool = False,
|
||
|
default: T.Optional[_T] = None,
|
||
4 years ago
|
since: T.Optional[str] = None,
|
||
|
deprecated: T.Optional[str] = None,
|
||
4 years ago
|
validator: T.Optional[T.Callable[[_T], T.Optional[str]]] = None,
|
||
|
convertor: T.Optional[T.Callable[[_T], TYPE_nvar]] = None):
|
||
4 years ago
|
self.name = name
|
||
|
self.types = types
|
||
|
self.required = required
|
||
|
self.listify = listify
|
||
|
self.default = default
|
||
4 years ago
|
self.since = since
|
||
|
self.deprecated = deprecated
|
||
4 years ago
|
self.validator = validator
|
||
4 years ago
|
self.convertor = convertor
|
||
4 years ago
|
|
||
|
|
||
|
def typed_kwargs(name: str, *types: KwargInfo) -> T.Callable[..., T.Any]:
|
||
|
"""Decorator for type checking keyword arguments.
|
||
|
|
||
|
Used to wrap a meson DSL implementation function, where it checks various
|
||
|
things about keyword arguments, including the type, and various other
|
||
|
information. For non-required values it sets the value to a default, which
|
||
|
means the value will always be provided.
|
||
|
|
||
4 years ago
|
If type tyhpe is a :class:ContainerTypeInfo, then the default value will be
|
||
|
passed as an argument to the container initializer, making a shallow copy
|
||
|
|
||
4 years ago
|
:param name: the name of the function, including the object it's attached ot
|
||
|
(if applicable)
|
||
|
:param *types: KwargInfo entries for each keyword argument.
|
||
|
"""
|
||
|
def inner(f: TV_func) -> TV_func:
|
||
|
|
||
|
@wraps(f)
|
||
|
def wrapper(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
4 years ago
|
kwargs, subproject = _get_callee_args(wrapped_args, want_subproject=True)[3:5]
|
||
4 years ago
|
|
||
|
all_names = {t.name for t in types}
|
||
|
unknowns = set(kwargs).difference(all_names)
|
||
|
if unknowns:
|
||
|
# Warn about unknown argumnts, delete them and continue. This
|
||
|
# keeps current behavior
|
||
|
ustr = ', '.join([f'"{u}"' for u in sorted(unknowns)])
|
||
|
mlog.warning(f'{name} got unknown keyword arguments {ustr}')
|
||
|
for u in unknowns:
|
||
|
del kwargs[u]
|
||
|
|
||
|
for info in types:
|
||
4 years ago
|
value = kwargs.get(info.name)
|
||
|
if value is not None:
|
||
|
if info.since:
|
||
|
feature_name = info.name + ' arg in ' + name
|
||
|
FeatureNew.single_use(feature_name, info.since, subproject)
|
||
|
if info.deprecated:
|
||
|
feature_name = info.name + ' arg in ' + name
|
||
|
FeatureDeprecated.single_use(feature_name, info.deprecated, subproject)
|
||
4 years ago
|
if info.listify:
|
||
|
kwargs[info.name] = value = mesonlib.listify(value)
|
||
|
if isinstance(info.types, ContainerTypeInfo):
|
||
|
msg = info.types.check(value)
|
||
|
if msg is not None:
|
||
|
raise InvalidArguments(f'{name} keyword argument "{info.name}" {msg}')
|
||
|
else:
|
||
|
if not isinstance(value, info.types):
|
||
|
if isinstance(info.types, tuple):
|
||
|
shouldbe = 'one of: {}'.format(", ".join(f'"{t.__name__}"' for t in info.types))
|
||
|
else:
|
||
|
shouldbe = f'"{info.types.__name__}"'
|
||
|
raise InvalidArguments(f'{name} keyword argument "{info.name}"" was of type "{type(value).__name__}" but should have been {shouldbe}')
|
||
4 years ago
|
|
||
|
if info.validator is not None:
|
||
|
msg = info.validator(value)
|
||
|
if msg is not None:
|
||
|
raise InvalidArguments(f'{name} keyword argument "{info.name}" {msg}')
|
||
4 years ago
|
elif info.required:
|
||
|
raise InvalidArguments(f'{name} is missing required keyword argument "{info.name}"')
|
||
|
else:
|
||
|
# set the value to the default, this ensuring all kwargs are present
|
||
|
# This both simplifies the typing checking and the usage
|
||
4 years ago
|
# Create a shallow copy of the container (and do a type
|
||
|
# conversion if necessary). This allows mutable types to
|
||
|
# be used safely as default values
|
||
|
if isinstance(info.types, ContainerTypeInfo):
|
||
|
kwargs[info.name] = info.types.container(info.default)
|
||
|
else:
|
||
|
kwargs[info.name] = info.default
|
||
4 years ago
|
|
||
4 years ago
|
if info.convertor:
|
||
|
kwargs[info.name] = info.convertor(kwargs[info.name])
|
||
|
|
||
4 years ago
|
return f(*wrapped_args, **wrapped_kwargs)
|
||
|
return T.cast(TV_func, wrapper)
|
||
|
return inner
|
||
|
|
||
|
|
||
5 years ago
|
class FeatureCheckBase(metaclass=abc.ABCMeta):
|
||
7 years ago
|
"Base class for feature version checks"
|
||
7 years ago
|
|
||
5 years ago
|
# In python 3.6 we can just forward declare this, but in 3.5 we can't
|
||
|
# This will be overwritten by the subclasses by necessity
|
||
5 years ago
|
feature_registry = {} # type: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[str]]]]
|
||
5 years ago
|
|
||
5 years ago
|
def __init__(self, feature_name: str, version: str, extra_message: T.Optional[str] = None):
|
||
5 years ago
|
self.feature_name = feature_name # type: str
|
||
|
self.feature_version = version # type: str
|
||
5 years ago
|
self.extra_message = extra_message or '' # type: str
|
||
7 years ago
|
|
||
7 years ago
|
@staticmethod
|
||
5 years ago
|
def get_target_version(subproject: str) -> str:
|
||
7 years ago
|
# Don't do any checks if project() has not been parsed yet
|
||
|
if subproject not in mesonlib.project_meson_versions:
|
||
|
return ''
|
||
5 years ago
|
return mesonlib.project_meson_versions[subproject]
|
||
7 years ago
|
|
||
5 years ago
|
@staticmethod
|
||
|
@abc.abstractmethod
|
||
|
def check_version(target_version: str, feature_Version: str) -> bool:
|
||
|
pass
|
||
|
|
||
5 years ago
|
def use(self, subproject: str) -> None:
|
||
7 years ago
|
tv = self.get_target_version(subproject)
|
||
|
# No target version
|
||
7 years ago
|
if tv == '':
|
||
|
return
|
||
7 years ago
|
# Target version is new enough
|
||
5 years ago
|
if self.check_version(tv, self.feature_version):
|
||
7 years ago
|
return
|
||
7 years ago
|
# Feature is too new for target version, register it
|
||
|
if subproject not in self.feature_registry:
|
||
|
self.feature_registry[subproject] = {self.feature_version: set()}
|
||
|
register = self.feature_registry[subproject]
|
||
|
if self.feature_version not in register:
|
||
|
register[self.feature_version] = set()
|
||
|
if self.feature_name in register[self.feature_version]:
|
||
|
# Don't warn about the same feature multiple times
|
||
|
# FIXME: This is needed to prevent duplicate warnings, but also
|
||
|
# means we won't warn about a feature used in multiple places.
|
||
7 years ago
|
return
|
||
7 years ago
|
register[self.feature_version].add(self.feature_name)
|
||
|
self.log_usage_warning(tv)
|
||
|
|
||
|
@classmethod
|
||
5 years ago
|
def report(cls, subproject: str) -> None:
|
||
7 years ago
|
if subproject not in cls.feature_registry:
|
||
|
return
|
||
|
warning_str = cls.get_warning_str_prefix(cls.get_target_version(subproject))
|
||
|
fv = cls.feature_registry[subproject]
|
||
|
for version in sorted(fv.keys()):
|
||
|
warning_str += '\n * {}: {}'.format(version, fv[version])
|
||
|
mlog.warning(warning_str)
|
||
7 years ago
|
|
||
5 years ago
|
def log_usage_warning(self, tv: str) -> None:
|
||
|
raise InterpreterException('log_usage_warning not implemented')
|
||
|
|
||
|
@staticmethod
|
||
|
def get_warning_str_prefix(tv: str) -> str:
|
||
|
raise InterpreterException('get_warning_str_prefix not implemented')
|
||
|
|
||
5 years ago
|
def __call__(self, f: TV_func) -> TV_func:
|
||
7 years ago
|
@wraps(f)
|
||
5 years ago
|
def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
7 years ago
|
subproject = _get_callee_args(wrapped_args, want_subproject=True)[4]
|
||
|
if subproject is None:
|
||
4 years ago
|
raise AssertionError(f'{wrapped_args!r}')
|
||
7 years ago
|
self.use(subproject)
|
||
7 years ago
|
return f(*wrapped_args, **wrapped_kwargs)
|
||
5 years ago
|
return T.cast(TV_func, wrapped)
|
||
7 years ago
|
|
||
5 years ago
|
@classmethod
|
||
|
def single_use(cls, feature_name: str, version: str, subproject: str,
|
||
|
extra_message: T.Optional[str] = None) -> None:
|
||
|
"""Oneline version that instantiates and calls use()."""
|
||
|
cls(feature_name, version, extra_message).use(subproject)
|
||
|
|
||
|
|
||
7 years ago
|
class FeatureNew(FeatureCheckBase):
|
||
|
"""Checks for new features"""
|
||
7 years ago
|
|
||
5 years ago
|
# Class variable, shared across all instances
|
||
|
#
|
||
|
# Format: {subproject: {feature_version: set(feature_names)}}
|
||
|
feature_registry = {} # type: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[str]]]]
|
||
|
|
||
5 years ago
|
@staticmethod
|
||
|
def check_version(target_version: str, feature_version: str) -> bool:
|
||
5 years ago
|
return mesonlib.version_compare_condition_with_min(target_version, feature_version)
|
||
5 years ago
|
|
||
7 years ago
|
@staticmethod
|
||
5 years ago
|
def get_warning_str_prefix(tv: str) -> str:
|
||
4 years ago
|
return f'Project specifies a minimum meson_version \'{tv}\' but uses features which were added in newer versions:'
|
||
7 years ago
|
|
||
5 years ago
|
def log_usage_warning(self, tv: str) -> None:
|
||
5 years ago
|
args = [
|
||
4 years ago
|
'Project targeting', f"'{tv}'",
|
||
5 years ago
|
'but tried to use feature introduced in',
|
||
4 years ago
|
f"'{self.feature_version}':",
|
||
|
f'{self.feature_name}.',
|
||
5 years ago
|
]
|
||
|
if self.extra_message:
|
||
|
args.append(self.extra_message)
|
||
|
mlog.warning(*args)
|
||
7 years ago
|
|
||
7 years ago
|
class FeatureDeprecated(FeatureCheckBase):
|
||
|
"""Checks for deprecated features"""
|
||
7 years ago
|
|
||
5 years ago
|
# Class variable, shared across all instances
|
||
|
#
|
||
|
# Format: {subproject: {feature_version: set(feature_names)}}
|
||
|
feature_registry = {} # type: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[str]]]]
|
||
|
|
||
5 years ago
|
@staticmethod
|
||
|
def check_version(target_version: str, feature_version: str) -> bool:
|
||
5 years ago
|
# For deprecation checks we need to return the inverse of FeatureNew checks
|
||
5 years ago
|
return not mesonlib.version_compare_condition_with_min(target_version, feature_version)
|
||
|
|
||
7 years ago
|
@staticmethod
|
||
5 years ago
|
def get_warning_str_prefix(tv: str) -> str:
|
||
7 years ago
|
return 'Deprecated features used:'
|
||
|
|
||
5 years ago
|
def log_usage_warning(self, tv: str) -> None:
|
||
5 years ago
|
args = [
|
||
4 years ago
|
'Project targeting', f"'{tv}'",
|
||
5 years ago
|
'but tried to use feature deprecated since',
|
||
4 years ago
|
f"'{self.feature_version}':",
|
||
|
f'{self.feature_name}.',
|
||
5 years ago
|
]
|
||
|
if self.extra_message:
|
||
|
args.append(self.extra_message)
|
||
|
mlog.warning(*args)
|
||
7 years ago
|
|
||
7 years ago
|
|
||
5 years ago
|
class FeatureCheckKwargsBase(metaclass=abc.ABCMeta):
|
||
|
|
||
|
@property
|
||
|
@abc.abstractmethod
|
||
|
def feature_check_class(self) -> T.Type[FeatureCheckBase]:
|
||
|
pass
|
||
|
|
||
5 years ago
|
def __init__(self, feature_name: str, feature_version: str,
|
||
|
kwargs: T.List[str], extra_message: T.Optional[str] = None):
|
||
7 years ago
|
self.feature_name = feature_name
|
||
|
self.feature_version = feature_version
|
||
|
self.kwargs = kwargs
|
||
5 years ago
|
self.extra_message = extra_message
|
||
7 years ago
|
|
||
5 years ago
|
def __call__(self, f: TV_func) -> TV_func:
|
||
7 years ago
|
@wraps(f)
|
||
5 years ago
|
def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
|
||
7 years ago
|
kwargs, subproject = _get_callee_args(wrapped_args, want_subproject=True)[3:5]
|
||
|
if subproject is None:
|
||
4 years ago
|
raise AssertionError(f'{wrapped_args!r}')
|
||
7 years ago
|
for arg in self.kwargs:
|
||
7 years ago
|
if arg not in kwargs:
|
||
|
continue
|
||
7 years ago
|
name = arg + ' arg in ' + self.feature_name
|
||
5 years ago
|
self.feature_check_class.single_use(
|
||
|
name, self.feature_version, subproject, self.extra_message)
|
||
7 years ago
|
return f(*wrapped_args, **wrapped_kwargs)
|
||
5 years ago
|
return T.cast(TV_func, wrapped)
|
||
7 years ago
|
|
||
7 years ago
|
class FeatureNewKwargs(FeatureCheckKwargsBase):
|
||
|
feature_check_class = FeatureNew
|
||
7 years ago
|
|
||
7 years ago
|
class FeatureDeprecatedKwargs(FeatureCheckKwargsBase):
|
||
|
feature_check_class = FeatureDeprecated
|
||
7 years ago
|
|
||
|
|
||
8 years ago
|
class InterpreterException(mesonlib.MesonException):
|
||
|
pass
|
||
|
|
||
|
class InvalidCode(InterpreterException):
|
||
|
pass
|
||
|
|
||
|
class InvalidArguments(InterpreterException):
|
||
|
pass
|
||
|
|
||
7 years ago
|
class SubdirDoneRequest(BaseException):
|
||
|
pass
|
||
|
|
||
7 years ago
|
class ContinueRequest(BaseException):
|
||
|
pass
|
||
|
|
||
|
class BreakRequest(BaseException):
|
||
|
pass
|
||
|
|
||
8 years ago
|
class MutableInterpreterObject(InterpreterObject):
|
||
5 years ago
|
def __init__(self) -> None:
|
||
8 years ago
|
super().__init__()
|
||
8 years ago
|
|
||
8 years ago
|
class Disabler(InterpreterObject):
|
||
5 years ago
|
def __init__(self) -> None:
|
||
8 years ago
|
super().__init__()
|
||
|
self.methods.update({'found': self.found_method})
|
||
|
|
||
5 years ago
|
def found_method(self, args: T.Sequence[T.Any], kwargs: T.Dict[str, T.Any]) -> bool:
|
||
8 years ago
|
return False
|
||
|
|
||
5 years ago
|
def is_disabler(i: T.Any) -> bool:
|
||
8 years ago
|
return isinstance(i, Disabler)
|
||
|
|
||
5 years ago
|
def is_arg_disabled(arg: T.Any) -> bool:
|
||
6 years ago
|
if is_disabler(arg):
|
||
|
return True
|
||
|
if isinstance(arg, list):
|
||
|
for i in arg:
|
||
|
if is_arg_disabled(i):
|
||
|
return True
|
||
|
return False
|
||
|
|
||
5 years ago
|
def is_disabled(args: T.Sequence[T.Any], kwargs: T.Dict[str, T.Any]) -> bool:
|
||
8 years ago
|
for i in args:
|
||
6 years ago
|
if is_arg_disabled(i):
|
||
8 years ago
|
return True
|
||
|
for i in kwargs.values():
|
||
6 years ago
|
if is_arg_disabled(i):
|
||
8 years ago
|
return True
|
||
|
return False
|
||
8 years ago
|
|
||
5 years ago
|
def default_resolve_key(key: mparser.BaseNode) -> str:
|
||
|
if not isinstance(key, mparser.IdNode):
|
||
|
raise InterpreterException('Invalid kwargs format.')
|
||
5 years ago
|
return key.value
|
||
5 years ago
|
|
||
8 years ago
|
class InterpreterBase:
|
||
5 years ago
|
elementary_types = (int, float, str, bool, list)
|
||
|
|
||
5 years ago
|
def __init__(self, source_root: str, subdir: str, subproject: str):
|
||
8 years ago
|
self.source_root = source_root
|
||
5 years ago
|
self.funcs = {} # type: T.Dict[str, T.Callable[[mparser.BaseNode, T.List[TYPE_nvar], T.Dict[str, TYPE_nvar]], TYPE_var]]
|
||
|
self.builtin = {} # type: T.Dict[str, InterpreterObject]
|
||
8 years ago
|
self.subdir = subdir
|
||
4 years ago
|
self.root_subdir = subdir
|
||
5 years ago
|
self.subproject = subproject
|
||
5 years ago
|
self.variables = {} # type: T.Dict[str, TYPE_var]
|
||
8 years ago
|
self.argument_depth = 0
|
||
8 years ago
|
self.current_lineno = -1
|
||
6 years ago
|
# Current node set during a function call. This can be used as location
|
||
|
# when printing a warning message during a method call.
|
||
5 years ago
|
self.current_node = None # type: mparser.BaseNode
|
||
5 years ago
|
# This is set to `version_string` when this statement is evaluated:
|
||
|
# meson.version().compare_version(version_string)
|
||
|
# If it was part of a if-clause, it is used to temporally override the
|
||
|
# current meson version target within that if-block.
|
||
5 years ago
|
self.tmp_meson_version = None # type: T.Optional[str]
|
||
8 years ago
|
|
||
5 years ago
|
def load_root_meson_file(self) -> None:
|
||
8 years ago
|
mesonfile = os.path.join(self.source_root, self.subdir, environment.build_filename)
|
||
|
if not os.path.isfile(mesonfile):
|
||
|
raise InvalidArguments('Missing Meson file in %s' % mesonfile)
|
||
|
with open(mesonfile, encoding='utf8') as mf:
|
||
|
code = mf.read()
|
||
8 years ago
|
if code.isspace():
|
||
8 years ago
|
raise InvalidCode('Builder file is empty.')
|
||
|
assert(isinstance(code, str))
|
||
|
try:
|
||
5 years ago
|
self.ast = mparser.Parser(code, mesonfile).parse()
|
||
8 years ago
|
except mesonlib.MesonException as me:
|
||
5 years ago
|
me.file = mesonfile
|
||
8 years ago
|
raise me
|
||
8 years ago
|
|
||
5 years ago
|
def join_path_strings(self, args: T.Sequence[str]) -> str:
|
||
6 years ago
|
return os.path.join(*args).replace('\\', '/')
|
||
|
|
||
5 years ago
|
def parse_project(self) -> None:
|
||
8 years ago
|
"""
|
||
|
Parses project() and initializes languages, compilers etc. Do this
|
||
|
early because we need this before we parse the rest of the AST.
|
||
|
"""
|
||
|
self.evaluate_codeblock(self.ast, end=1)
|
||
|
|
||
5 years ago
|
def sanity_check_ast(self) -> None:
|
||
8 years ago
|
if not isinstance(self.ast, mparser.CodeBlockNode):
|
||
|
raise InvalidCode('AST is of invalid type. Possibly a bug in the parser.')
|
||
8 years ago
|
if not self.ast.lines:
|
||
8 years ago
|
raise InvalidCode('No statements in code.')
|
||
|
first = self.ast.lines[0]
|
||
|
if not isinstance(first, mparser.FunctionNode) or first.func_name != 'project':
|
||
|
raise InvalidCode('First statement must be a call to project')
|
||
|
|
||
5 years ago
|
def run(self) -> None:
|
||
8 years ago
|
# Evaluate everything after the first line, which is project() because
|
||
|
# we already parsed that in self.parse_project()
|
||
7 years ago
|
try:
|
||
|
self.evaluate_codeblock(self.ast, start=1)
|
||
|
except SubdirDoneRequest:
|
||
|
pass
|
||
8 years ago
|
|
||
5 years ago
|
def evaluate_codeblock(self, node: mparser.CodeBlockNode, start: int = 0, end: T.Optional[int] = None) -> None:
|
||
8 years ago
|
if node is None:
|
||
|
return
|
||
|
if not isinstance(node, mparser.CodeBlockNode):
|
||
|
e = InvalidCode('Tried to execute a non-codeblock. Possibly a bug in the parser.')
|
||
|
e.lineno = node.lineno
|
||
|
e.colno = node.colno
|
||
|
raise e
|
||
|
statements = node.lines[start:end]
|
||
|
i = 0
|
||
|
while i < len(statements):
|
||
|
cur = statements[i]
|
||
|
try:
|
||
8 years ago
|
self.current_lineno = cur.lineno
|
||
8 years ago
|
self.evaluate_statement(cur)
|
||
|
except Exception as e:
|
||
5 years ago
|
if getattr(e, 'lineno', None) is None:
|
||
5 years ago
|
# We are doing the equivalent to setattr here and mypy does not like it
|
||
|
e.lineno = cur.lineno # type: ignore
|
||
|
e.colno = cur.colno # type: ignore
|
||
|
e.file = os.path.join(self.source_root, self.subdir, environment.build_filename) # type: ignore
|
||
8 years ago
|
raise e
|
||
|
i += 1 # In THE FUTURE jump over blocks and stuff.
|
||
|
|
||
5 years ago
|
def evaluate_statement(self, cur: mparser.BaseNode) -> T.Optional[TYPE_var]:
|
||
5 years ago
|
self.current_node = cur
|
||
8 years ago
|
if isinstance(cur, mparser.FunctionNode):
|
||
|
return self.function_call(cur)
|
||
|
elif isinstance(cur, mparser.AssignmentNode):
|
||
5 years ago
|
self.assignment(cur)
|
||
8 years ago
|
elif isinstance(cur, mparser.MethodNode):
|
||
|
return self.method_call(cur)
|
||
|
elif isinstance(cur, mparser.StringNode):
|
||
5 years ago
|
return cur.value
|
||
8 years ago
|
elif isinstance(cur, mparser.BooleanNode):
|
||
5 years ago
|
return cur.value
|
||
8 years ago
|
elif isinstance(cur, mparser.IfClauseNode):
|
||
|
return self.evaluate_if(cur)
|
||
|
elif isinstance(cur, mparser.IdNode):
|
||
|
return self.get_variable(cur.value)
|
||
|
elif isinstance(cur, mparser.ComparisonNode):
|
||
|
return self.evaluate_comparison(cur)
|
||
|
elif isinstance(cur, mparser.ArrayNode):
|
||
|
return self.evaluate_arraystatement(cur)
|
||
7 years ago
|
elif isinstance(cur, mparser.DictNode):
|
||
|
return self.evaluate_dictstatement(cur)
|
||
8 years ago
|
elif isinstance(cur, mparser.NumberNode):
|
||
5 years ago
|
return cur.value
|
||
8 years ago
|
elif isinstance(cur, mparser.AndNode):
|
||
|
return self.evaluate_andstatement(cur)
|
||
|
elif isinstance(cur, mparser.OrNode):
|
||
|
return self.evaluate_orstatement(cur)
|
||
|
elif isinstance(cur, mparser.NotNode):
|
||
|
return self.evaluate_notstatement(cur)
|
||
|
elif isinstance(cur, mparser.UMinusNode):
|
||
|
return self.evaluate_uminusstatement(cur)
|
||
|
elif isinstance(cur, mparser.ArithmeticNode):
|
||
|
return self.evaluate_arithmeticstatement(cur)
|
||
|
elif isinstance(cur, mparser.ForeachClauseNode):
|
||
5 years ago
|
self.evaluate_foreach(cur)
|
||
8 years ago
|
elif isinstance(cur, mparser.PlusAssignmentNode):
|
||
5 years ago
|
self.evaluate_plusassign(cur)
|
||
8 years ago
|
elif isinstance(cur, mparser.IndexNode):
|
||
|
return self.evaluate_indexing(cur)
|
||
|
elif isinstance(cur, mparser.TernaryNode):
|
||
|
return self.evaluate_ternary(cur)
|
||
4 years ago
|
elif isinstance(cur, mparser.FormatStringNode):
|
||
|
return self.evaluate_fstring(cur)
|
||
7 years ago
|
elif isinstance(cur, mparser.ContinueNode):
|
||
|
raise ContinueRequest()
|
||
|
elif isinstance(cur, mparser.BreakNode):
|
||
|
raise BreakRequest()
|
||
5 years ago
|
elif isinstance(cur, self.elementary_types):
|
||
8 years ago
|
return cur
|
||
|
else:
|
||
|
raise InvalidCode("Unknown statement.")
|
||
5 years ago
|
return None
|
||
8 years ago
|
|
||
5 years ago
|
def evaluate_arraystatement(self, cur: mparser.ArrayNode) -> list:
|
||
8 years ago
|
(arguments, kwargs) = self.reduce_arguments(cur.args)
|
||
|
if len(kwargs) > 0:
|
||
|
raise InvalidCode('Keyword arguments are invalid in array construction.')
|
||
|
return arguments
|
||
|
|
||
7 years ago
|
@FeatureNew('dict', '0.47.0')
|
||
5 years ago
|
def evaluate_dictstatement(self, cur: mparser.DictNode) -> TYPE_nkwargs:
|
||
|
def resolve_key(key: mparser.BaseNode) -> str:
|
||
5 years ago
|
if not isinstance(key, mparser.StringNode):
|
||
5 years ago
|
FeatureNew.single_use('Dictionary entry using non literal key', '0.53.0', self.subproject)
|
||
5 years ago
|
str_key = self.evaluate_statement(key)
|
||
|
if not isinstance(str_key, str):
|
||
5 years ago
|
raise InvalidArguments('Key must be a string')
|
||
5 years ago
|
return str_key
|
||
|
arguments, kwargs = self.reduce_arguments(cur.args, key_resolver=resolve_key, duplicate_key_error='Duplicate dictionary key: {}')
|
||
|
assert not arguments
|
||
|
return kwargs
|
||
7 years ago
|
|
||
5 years ago
|
def evaluate_notstatement(self, cur: mparser.NotNode) -> T.Union[bool, Disabler]:
|
||
8 years ago
|
v = self.evaluate_statement(cur.value)
|
||
5 years ago
|
if isinstance(v, Disabler):
|
||
5 years ago
|
return v
|
||
8 years ago
|
if not isinstance(v, bool):
|
||
|
raise InterpreterException('Argument to "not" is not a boolean.')
|
||
|
return not v
|
||
|
|
||
5 years ago
|
def evaluate_if(self, node: mparser.IfClauseNode) -> T.Optional[Disabler]:
|
||
8 years ago
|
assert(isinstance(node, mparser.IfClauseNode))
|
||
|
for i in node.ifs:
|
||
5 years ago
|
# Reset self.tmp_meson_version to know if it gets set during this
|
||
|
# statement evaluation.
|
||
|
self.tmp_meson_version = None
|
||
8 years ago
|
result = self.evaluate_statement(i.condition)
|
||
5 years ago
|
if isinstance(result, Disabler):
|
||
8 years ago
|
return result
|
||
8 years ago
|
if not(isinstance(result, bool)):
|
||
4 years ago
|
raise InvalidCode(f'If clause {result!r} does not evaluate to true or false.')
|
||
8 years ago
|
if result:
|
||
5 years ago
|
prev_meson_version = mesonlib.project_meson_versions[self.subproject]
|
||
|
if self.tmp_meson_version:
|
||
|
mesonlib.project_meson_versions[self.subproject] = self.tmp_meson_version
|
||
|
try:
|
||
|
self.evaluate_codeblock(i.block)
|
||
|
finally:
|
||
|
mesonlib.project_meson_versions[self.subproject] = prev_meson_version
|
||
5 years ago
|
return None
|
||
8 years ago
|
if not isinstance(node.elseblock, mparser.EmptyNode):
|
||
|
self.evaluate_codeblock(node.elseblock)
|
||
5 years ago
|
return None
|
||
8 years ago
|
|
||
5 years ago
|
def validate_comparison_types(self, val1: T.Any, val2: T.Any) -> bool:
|
||
7 years ago
|
if type(val1) != type(val2):
|
||
7 years ago
|
return False
|
||
|
return True
|
||
7 years ago
|
|
||
5 years ago
|
def evaluate_in(self, val1: T.Any, val2: T.Any) -> bool:
|
||
7 years ago
|
if not isinstance(val1, (str, int, float, ObjectHolder)):
|
||
|
raise InvalidArguments('lvalue of "in" operator must be a string, integer, float, or object')
|
||
|
if not isinstance(val2, (list, dict)):
|
||
|
raise InvalidArguments('rvalue of "in" operator must be an array or a dict')
|
||
|
return val1 in val2
|
||
|
|
||
5 years ago
|
def evaluate_comparison(self, node: mparser.ComparisonNode) -> T.Union[bool, Disabler]:
|
||
8 years ago
|
val1 = self.evaluate_statement(node.left)
|
||
5 years ago
|
if isinstance(val1, Disabler):
|
||
8 years ago
|
return val1
|
||
8 years ago
|
val2 = self.evaluate_statement(node.right)
|
||
5 years ago
|
if isinstance(val2, Disabler):
|
||
8 years ago
|
return val2
|
||
7 years ago
|
if node.ctype == 'in':
|
||
|
return self.evaluate_in(val1, val2)
|
||
|
elif node.ctype == 'notin':
|
||
|
return not self.evaluate_in(val1, val2)
|
||
7 years ago
|
valid = self.validate_comparison_types(val1, val2)
|
||
|
# Ordering comparisons of different types isn't allowed since PR #1810
|
||
|
# (0.41.0). Since PR #2884 we also warn about equality comparisons of
|
||
|
# different types, which will one day become an error.
|
||
|
if not valid and (node.ctype == '==' or node.ctype == '!='):
|
||
|
mlog.warning('''Trying to compare values of different types ({}, {}) using {}.
|
||
7 years ago
|
The result of this is undefined and will become a hard error in a future Meson release.'''
|
||
|
.format(type(val1).__name__, type(val2).__name__, node.ctype), location=node)
|
||
8 years ago
|
if node.ctype == '==':
|
||
|
return val1 == val2
|
||
|
elif node.ctype == '!=':
|
||
|
return val1 != val2
|
||
7 years ago
|
elif not valid:
|
||
|
raise InterpreterException(
|
||
|
'Values of different types ({}, {}) cannot be compared using {}.'.format(type(val1).__name__,
|
||
|
type(val2).__name__,
|
||
|
node.ctype))
|
||
5 years ago
|
elif not isinstance(val1, self.elementary_types):
|
||
|
raise InterpreterException('{} can only be compared for equality.'.format(getattr(node.left, 'value', '<ERROR>')))
|
||
|
elif not isinstance(val2, self.elementary_types):
|
||
|
raise InterpreterException('{} can only be compared for equality.'.format(getattr(node.right, 'value', '<ERROR>')))
|
||
|
# Use type: ignore because mypy will complain that we are comparing two Unions,
|
||
|
# but we actually guarantee earlier that both types are the same
|
||
8 years ago
|
elif node.ctype == '<':
|
||
5 years ago
|
return val1 < val2 # type: ignore
|
||
8 years ago
|
elif node.ctype == '<=':
|
||
5 years ago
|
return val1 <= val2 # type: ignore
|
||
8 years ago
|
elif node.ctype == '>':
|
||
5 years ago
|
return val1 > val2 # type: ignore
|
||
8 years ago
|
elif node.ctype == '>=':
|
||
5 years ago
|
return val1 >= val2 # type: ignore
|
||
8 years ago
|
else:
|
||
|
raise InvalidCode('You broke my compare eval.')
|
||
|
|
||
5 years ago
|
def evaluate_andstatement(self, cur: mparser.AndNode) -> T.Union[bool, Disabler]:
|
||
8 years ago
|
l = self.evaluate_statement(cur.left)
|
||
5 years ago
|
if isinstance(l, Disabler):
|
||
8 years ago
|
return l
|
||
8 years ago
|
if not isinstance(l, bool):
|
||
|
raise InterpreterException('First argument to "and" is not a boolean.')
|
||
|
if not l:
|
||
|
return False
|
||
|
r = self.evaluate_statement(cur.right)
|
||
5 years ago
|
if isinstance(r, Disabler):
|
||
8 years ago
|
return r
|
||
8 years ago
|
if not isinstance(r, bool):
|
||
|
raise InterpreterException('Second argument to "and" is not a boolean.')
|
||
|
return r
|
||
|
|
||
5 years ago
|
def evaluate_orstatement(self, cur: mparser.OrNode) -> T.Union[bool, Disabler]:
|
||
8 years ago
|
l = self.evaluate_statement(cur.left)
|
||
5 years ago
|
if isinstance(l, Disabler):
|
||
8 years ago
|
return l
|
||
8 years ago
|
if not isinstance(l, bool):
|
||
|
raise InterpreterException('First argument to "or" is not a boolean.')
|
||
|
if l:
|
||
|
return True
|
||
|
r = self.evaluate_statement(cur.right)
|
||
5 years ago
|
if isinstance(r, Disabler):
|
||
8 years ago
|
return r
|
||
8 years ago
|
if not isinstance(r, bool):
|
||
|
raise InterpreterException('Second argument to "or" is not a boolean.')
|
||
|
return r
|
||
|
|
||
5 years ago
|
def evaluate_uminusstatement(self, cur: mparser.UMinusNode) -> T.Union[int, Disabler]:
|
||
8 years ago
|
v = self.evaluate_statement(cur.value)
|
||
5 years ago
|
if isinstance(v, Disabler):
|
||
8 years ago
|
return v
|
||
8 years ago
|
if not isinstance(v, int):
|
||
|
raise InterpreterException('Argument to negation is not an integer.')
|
||
|
return -v
|
||
|
|
||
6 years ago
|
@FeatureNew('/ with string arguments', '0.49.0')
|
||
5 years ago
|
def evaluate_path_join(self, l: str, r: str) -> str:
|
||
6 years ago
|
if not isinstance(l, str):
|
||
|
raise InvalidCode('The division operator can only append to a string.')
|
||
|
if not isinstance(r, str):
|
||
|
raise InvalidCode('The division operator can only append a string.')
|
||
|
return self.join_path_strings((l, r))
|
||
|
|
||
5 years ago
|
def evaluate_division(self, l: T.Any, r: T.Any) -> T.Union[int, str]:
|
||
6 years ago
|
if isinstance(l, str) or isinstance(r, str):
|
||
|
return self.evaluate_path_join(l, r)
|
||
|
if isinstance(l, int) and isinstance(r, int):
|
||
|
if r == 0:
|
||
|
raise InvalidCode('Division by zero.')
|
||
|
return l // r
|
||
|
raise InvalidCode('Division works only with strings or integers.')
|
||
|
|
||
5 years ago
|
def evaluate_arithmeticstatement(self, cur: mparser.ArithmeticNode) -> T.Union[int, str, dict, list, Disabler]:
|
||
8 years ago
|
l = self.evaluate_statement(cur.left)
|
||
5 years ago
|
if isinstance(l, Disabler):
|
||
8 years ago
|
return l
|
||
8 years ago
|
r = self.evaluate_statement(cur.right)
|
||
5 years ago
|
if isinstance(r, Disabler):
|
||
8 years ago
|
return r
|
||
8 years ago
|
|
||
|
if cur.operation == 'add':
|
||
7 years ago
|
if isinstance(l, dict) and isinstance(r, dict):
|
||
|
return {**l, **r}
|
||
8 years ago
|
try:
|
||
5 years ago
|
# MyPy error due to handling two Unions (we are catching all exceptions anyway)
|
||
|
return l + r # type: ignore
|
||
8 years ago
|
except Exception as e:
|
||
|
raise InvalidCode('Invalid use of addition: ' + str(e))
|
||
|
elif cur.operation == 'sub':
|
||
|
if not isinstance(l, int) or not isinstance(r, int):
|
||
|
raise InvalidCode('Subtraction works only with integers.')
|
||
|
return l - r
|
||
|
elif cur.operation == 'mul':
|
||
|
if not isinstance(l, int) or not isinstance(r, int):
|
||
|
raise InvalidCode('Multiplication works only with integers.')
|
||
|
return l * r
|
||
|
elif cur.operation == 'div':
|
||
6 years ago
|
return self.evaluate_division(l, r)
|
||
8 years ago
|
elif cur.operation == 'mod':
|
||
|
if not isinstance(l, int) or not isinstance(r, int):
|
||
|
raise InvalidCode('Modulo works only with integers.')
|
||
|
return l % r
|
||
|
else:
|
||
|
raise InvalidCode('You broke me.')
|
||
|
|
||
5 years ago
|
def evaluate_ternary(self, node: mparser.TernaryNode) -> TYPE_var:
|
||
8 years ago
|
assert(isinstance(node, mparser.TernaryNode))
|
||
|
result = self.evaluate_statement(node.condition)
|
||
5 years ago
|
if isinstance(result, Disabler):
|
||
8 years ago
|
return result
|
||
8 years ago
|
if not isinstance(result, bool):
|
||
|
raise InterpreterException('Ternary condition is not boolean.')
|
||
|
if result:
|
||
|
return self.evaluate_statement(node.trueblock)
|
||
|
else:
|
||
|
return self.evaluate_statement(node.falseblock)
|
||
|
|
||
4 years ago
|
@FeatureNew('format strings', '0.58.0')
|
||
4 years ago
|
def evaluate_fstring(self, node: mparser.FormatStringNode) -> TYPE_var:
|
||
|
assert(isinstance(node, mparser.FormatStringNode))
|
||
|
|
||
|
def replace(match: T.Match[str]) -> str:
|
||
|
var = str(match.group(1))
|
||
|
try:
|
||
4 years ago
|
val = self.variables[var]
|
||
|
if not isinstance(val, (str, int, float, bool)):
|
||
4 years ago
|
raise InvalidCode(f'Identifier "{var}" does not name a formattable variable ' +
|
||
|
'(has to be an integer, a string, a floating point number or a boolean).')
|
||
4 years ago
|
|
||
|
return str(val)
|
||
4 years ago
|
except KeyError:
|
||
4 years ago
|
raise InvalidCode(f'Identifier "{var}" does not name a variable.')
|
||
4 years ago
|
|
||
4 years ago
|
return re.sub(r'@([_a-zA-Z][_0-9a-zA-Z]*)@', replace, node.value)
|
||
4 years ago
|
|
||
5 years ago
|
def evaluate_foreach(self, node: mparser.ForeachClauseNode) -> None:
|
||
8 years ago
|
assert(isinstance(node, mparser.ForeachClauseNode))
|
||
|
items = self.evaluate_statement(node.items)
|
||
7 years ago
|
|
||
4 years ago
|
if isinstance(items, (list, RangeHolder)):
|
||
7 years ago
|
if len(node.varnames) != 1:
|
||
|
raise InvalidArguments('Foreach on array does not unpack')
|
||
5 years ago
|
varname = node.varnames[0]
|
||
7 years ago
|
for item in items:
|
||
|
self.set_variable(varname, item)
|
||
7 years ago
|
try:
|
||
|
self.evaluate_codeblock(node.block)
|
||
|
except ContinueRequest:
|
||
|
continue
|
||
|
except BreakRequest:
|
||
|
break
|
||
7 years ago
|
elif isinstance(items, dict):
|
||
|
if len(node.varnames) != 2:
|
||
|
raise InvalidArguments('Foreach on dict unpacks key and value')
|
||
4 years ago
|
for key, value in sorted(items.items()):
|
||
5 years ago
|
self.set_variable(node.varnames[0], key)
|
||
|
self.set_variable(node.varnames[1], value)
|
||
7 years ago
|
try:
|
||
|
self.evaluate_codeblock(node.block)
|
||
|
except ContinueRequest:
|
||
|
continue
|
||
|
except BreakRequest:
|
||
|
break
|
||
7 years ago
|
else:
|
||
|
raise InvalidArguments('Items of foreach loop must be an array or a dict')
|
||
8 years ago
|
|
||
5 years ago
|
def evaluate_plusassign(self, node: mparser.PlusAssignmentNode) -> None:
|
||
8 years ago
|
assert(isinstance(node, mparser.PlusAssignmentNode))
|
||
|
varname = node.var_name
|
||
|
addition = self.evaluate_statement(node.value)
|
||
5 years ago
|
|
||
8 years ago
|
# Remember that all variables are immutable. We must always create a
|
||
|
# full new variable and then assign it.
|
||
|
old_variable = self.get_variable(varname)
|
||
5 years ago
|
new_value = None # type: T.Union[str, int, float, bool, dict, list]
|
||
8 years ago
|
if isinstance(old_variable, str):
|
||
|
if not isinstance(addition, str):
|
||
|
raise InvalidArguments('The += operator requires a string on the right hand side if the variable on the left is a string')
|
||
|
new_value = old_variable + addition
|
||
|
elif isinstance(old_variable, int):
|
||
|
if not isinstance(addition, int):
|
||
|
raise InvalidArguments('The += operator requires an int on the right hand side if the variable on the left is an int')
|
||
|
new_value = old_variable + addition
|
||
7 years ago
|
elif isinstance(old_variable, list):
|
||
8 years ago
|
if isinstance(addition, list):
|
||
|
new_value = old_variable + addition
|
||
|
else:
|
||
|
new_value = old_variable + [addition]
|
||
7 years ago
|
elif isinstance(old_variable, dict):
|
||
|
if not isinstance(addition, dict):
|
||
|
raise InvalidArguments('The += operator requires a dict on the right hand side if the variable on the left is a dict')
|
||
5 years ago
|
new_value = {**old_variable, **addition}
|
||
7 years ago
|
# Add other data types here.
|
||
|
else:
|
||
5 years ago
|
raise InvalidArguments('The += operator currently only works with arrays, dicts, strings or ints')
|
||
8 years ago
|
self.set_variable(varname, new_value)
|
||
|
|
||
5 years ago
|
def evaluate_indexing(self, node: mparser.IndexNode) -> TYPE_var:
|
||
8 years ago
|
assert(isinstance(node, mparser.IndexNode))
|
||
|
iobject = self.evaluate_statement(node.iobject)
|
||
5 years ago
|
if isinstance(iobject, Disabler):
|
||
8 years ago
|
return iobject
|
||
8 years ago
|
if not hasattr(iobject, '__getitem__'):
|
||
|
raise InterpreterException(
|
||
|
'Tried to index an object that doesn\'t support indexing.')
|
||
8 years ago
|
index = self.evaluate_statement(node.index)
|
||
7 years ago
|
|
||
|
if isinstance(iobject, dict):
|
||
|
if not isinstance(index, str):
|
||
|
raise InterpreterException('Key is not a string')
|
||
|
try:
|
||
5 years ago
|
# The cast is required because we don't have recursive types...
|
||
5 years ago
|
return T.cast(TYPE_var, iobject[index])
|
||
7 years ago
|
except KeyError:
|
||
|
raise InterpreterException('Key %s is not in dict' % index)
|
||
|
else:
|
||
|
if not isinstance(index, int):
|
||
|
raise InterpreterException('Index value is not an integer.')
|
||
|
try:
|
||
5 years ago
|
# Ignore the MyPy error, since we don't know all indexable types here
|
||
|
# and we handle non indexable types with an exception
|
||
|
# TODO maybe find a better solution
|
||
|
return iobject[index] # type: ignore
|
||
7 years ago
|
except IndexError:
|
||
4 years ago
|
# We are already checking for the existence of __getitem__, so this should be save
|
||
5 years ago
|
raise InterpreterException('Index %d out of bounds of array of size %d.' % (index, len(iobject))) # type: ignore
|
||
8 years ago
|
|
||
5 years ago
|
def function_call(self, node: mparser.FunctionNode) -> T.Optional[TYPE_var]:
|
||
8 years ago
|
func_name = node.func_name
|
||
|
(posargs, kwargs) = self.reduce_arguments(node.args)
|
||
5 years ago
|
if is_disabled(posargs, kwargs) and func_name not in {'get_variable', 'set_variable', 'is_disabler'}:
|
||
8 years ago
|
return Disabler()
|
||
8 years ago
|
if func_name in self.funcs:
|
||
7 years ago
|
func = self.funcs[func_name]
|
||
5 years ago
|
func_args = posargs # type: T.Any
|
||
7 years ago
|
if not getattr(func, 'no-args-flattening', False):
|
||
5 years ago
|
func_args = flatten(posargs)
|
||
5 years ago
|
return func(node, func_args, kwargs)
|
||
8 years ago
|
else:
|
||
|
self.unknown_function_called(func_name)
|
||
5 years ago
|
return None
|
||
8 years ago
|
|
||
5 years ago
|
def method_call(self, node: mparser.MethodNode) -> TYPE_var:
|
||
8 years ago
|
invokable = node.source_object
|
||
|
if isinstance(invokable, mparser.IdNode):
|
||
|
object_name = invokable.value
|
||
|
obj = self.get_variable(object_name)
|
||
|
else:
|
||
|
obj = self.evaluate_statement(invokable)
|
||
|
method_name = node.name
|
||
5 years ago
|
(args, kwargs) = self.reduce_arguments(node.args)
|
||
|
if is_disabled(args, kwargs):
|
||
|
return Disabler()
|
||
8 years ago
|
if isinstance(obj, str):
|
||
5 years ago
|
return self.string_method_call(obj, method_name, args, kwargs)
|
||
8 years ago
|
if isinstance(obj, bool):
|
||
5 years ago
|
return self.bool_method_call(obj, method_name, args, kwargs)
|
||
8 years ago
|
if isinstance(obj, int):
|
||
5 years ago
|
return self.int_method_call(obj, method_name, args, kwargs)
|
||
8 years ago
|
if isinstance(obj, list):
|
||
5 years ago
|
return self.array_method_call(obj, method_name, args, kwargs)
|
||
7 years ago
|
if isinstance(obj, dict):
|
||
5 years ago
|
return self.dict_method_call(obj, method_name, args, kwargs)
|
||
8 years ago
|
if isinstance(obj, mesonlib.File):
|
||
|
raise InvalidArguments('File object "%s" is not callable.' % obj)
|
||
8 years ago
|
if not isinstance(obj, InterpreterObject):
|
||
|
raise InvalidArguments('Variable "%s" is not callable.' % object_name)
|
||
8 years ago
|
# Special case. This is the only thing you can do with a disabler
|
||
|
# object. Every other use immediately returns the disabler object.
|
||
5 years ago
|
if isinstance(obj, Disabler):
|
||
|
if method_name == 'found':
|
||
|
return False
|
||
|
else:
|
||
|
return Disabler()
|
||
8 years ago
|
if method_name == 'extract_objects':
|
||
5 years ago
|
if not isinstance(obj, ObjectHolder):
|
||
4 years ago
|
raise InvalidArguments(f'Invalid operation "extract_objects" on variable "{object_name}"')
|
||
8 years ago
|
self.validate_extraction(obj.held_object)
|
||
6 years ago
|
obj.current_node = node
|
||
5 years ago
|
return obj.method_call(method_name, args, kwargs)
|
||
8 years ago
|
|
||
5 years ago
|
@builtinMethodNoKwargs
|
||
|
def bool_method_call(self, obj: bool, method_name: str, posargs: T.List[TYPE_nvar], kwargs: T.Dict[str, T.Any]) -> T.Union[str, int]:
|
||
8 years ago
|
if method_name == 'to_string':
|
||
8 years ago
|
if not posargs:
|
||
8 years ago
|
if obj:
|
||
8 years ago
|
return 'true'
|
||
|
else:
|
||
|
return 'false'
|
||
|
elif len(posargs) == 2 and isinstance(posargs[0], str) and isinstance(posargs[1], str):
|
||
8 years ago
|
if obj:
|
||
8 years ago
|
return posargs[0]
|
||
|
else:
|
||
|
return posargs[1]
|
||
|
else:
|
||
|
raise InterpreterException('bool.to_string() must have either no arguments or exactly two string arguments that signify what values to return for true and false.')
|
||
|
elif method_name == 'to_int':
|
||
8 years ago
|
if obj:
|
||
8 years ago
|
return 1
|
||
|
else:
|
||
|
return 0
|
||
|
else:
|
||
|
raise InterpreterException('Unknown method "%s" for a boolean.' % method_name)
|
||
|
|
||
5 years ago
|
@builtinMethodNoKwargs
|
||
|
def int_method_call(self, obj: int, method_name: str, posargs: T.List[TYPE_nvar], kwargs: T.Dict[str, T.Any]) -> T.Union[str, bool]:
|
||
8 years ago
|
if method_name == 'is_even':
|
||
8 years ago
|
if not posargs:
|
||
8 years ago
|
return obj % 2 == 0
|
||
|
else:
|
||
|
raise InterpreterException('int.is_even() must have no arguments.')
|
||
|
elif method_name == 'is_odd':
|
||
8 years ago
|
if not posargs:
|
||
8 years ago
|
return obj % 2 != 0
|
||
|
else:
|
||
|
raise InterpreterException('int.is_odd() must have no arguments.')
|
||
7 years ago
|
elif method_name == 'to_string':
|
||
|
if not posargs:
|
||
|
return str(obj)
|
||
|
else:
|
||
7 years ago
|
raise InterpreterException('int.to_string() must have no arguments.')
|
||
8 years ago
|
else:
|
||
|
raise InterpreterException('Unknown method "%s" for an integer.' % method_name)
|
||
|
|
||
8 years ago
|
@staticmethod
|
||
5 years ago
|
def _get_one_string_posarg(posargs: T.List[TYPE_nvar], method_name: str) -> str:
|
||
8 years ago
|
if len(posargs) > 1:
|
||
|
m = '{}() must have zero or one arguments'
|
||
|
raise InterpreterException(m.format(method_name))
|
||
|
elif len(posargs) == 1:
|
||
|
s = posargs[0]
|
||
|
if not isinstance(s, str):
|
||
|
m = '{}() argument must be a string'
|
||
|
raise InterpreterException(m.format(method_name))
|
||
|
return s
|
||
|
return None
|
||
|
|
||
5 years ago
|
@builtinMethodNoKwargs
|
||
|
def string_method_call(self, obj: str, method_name: str, posargs: T.List[TYPE_nvar], kwargs: T.Dict[str, T.Any]) -> T.Union[str, int, bool, T.List[str]]:
|
||
8 years ago
|
if method_name == 'strip':
|
||
5 years ago
|
s1 = self._get_one_string_posarg(posargs, 'strip')
|
||
|
if s1 is not None:
|
||
|
return obj.strip(s1)
|
||
8 years ago
|
return obj.strip()
|
||
|
elif method_name == 'format':
|
||
5 years ago
|
return self.format_string(obj, posargs)
|
||
8 years ago
|
elif method_name == 'to_upper':
|
||
|
return obj.upper()
|
||
|
elif method_name == 'to_lower':
|
||
|
return obj.lower()
|
||
|
elif method_name == 'underscorify':
|
||
|
return re.sub(r'[^a-zA-Z0-9]', '_', obj)
|
||
|
elif method_name == 'split':
|
||
5 years ago
|
s2 = self._get_one_string_posarg(posargs, 'split')
|
||
|
if s2 is not None:
|
||
|
return obj.split(s2)
|
||
8 years ago
|
return obj.split()
|
||
8 years ago
|
elif method_name == 'startswith' or method_name == 'contains' or method_name == 'endswith':
|
||
5 years ago
|
s3 = posargs[0]
|
||
|
if not isinstance(s3, str):
|
||
8 years ago
|
raise InterpreterException('Argument must be a string.')
|
||
|
if method_name == 'startswith':
|
||
5 years ago
|
return obj.startswith(s3)
|
||
8 years ago
|
elif method_name == 'contains':
|
||
5 years ago
|
return obj.find(s3) >= 0
|
||
|
return obj.endswith(s3)
|
||
8 years ago
|
elif method_name == 'to_int':
|
||
|
try:
|
||
|
return int(obj)
|
||
|
except Exception:
|
||
4 years ago
|
raise InterpreterException(f'String {obj!r} cannot be converted to int')
|
||
8 years ago
|
elif method_name == 'join':
|
||
|
if len(posargs) != 1:
|
||
|
raise InterpreterException('Join() takes exactly one argument.')
|
||
|
strlist = posargs[0]
|
||
|
check_stringlist(strlist)
|
||
5 years ago
|
assert isinstance(strlist, list) # Required for mypy
|
||
8 years ago
|
return obj.join(strlist)
|
||
|
elif method_name == 'version_compare':
|
||
|
if len(posargs) != 1:
|
||
|
raise InterpreterException('Version_compare() takes exactly one argument.')
|
||
|
cmpr = posargs[0]
|
||
|
if not isinstance(cmpr, str):
|
||
|
raise InterpreterException('Version_compare() argument must be a string.')
|
||
5 years ago
|
if isinstance(obj, MesonVersionString):
|
||
|
self.tmp_meson_version = cmpr
|
||
8 years ago
|
return mesonlib.version_compare(obj, cmpr)
|
||
5 years ago
|
elif method_name == 'substring':
|
||
|
if len(posargs) > 2:
|
||
|
raise InterpreterException('substring() takes maximum two arguments.')
|
||
|
start = 0
|
||
|
end = len(obj)
|
||
|
if len (posargs) > 0:
|
||
|
if not isinstance(posargs[0], int):
|
||
|
raise InterpreterException('substring() argument must be an int')
|
||
|
start = posargs[0]
|
||
|
if len (posargs) > 1:
|
||
|
if not isinstance(posargs[1], int):
|
||
|
raise InterpreterException('substring() argument must be an int')
|
||
|
end = posargs[1]
|
||
|
return obj[start:end]
|
||
4 years ago
|
elif method_name == 'replace':
|
||
|
FeatureNew.single_use('str.replace', '0.58.0', self.subproject)
|
||
|
if len(posargs) != 2:
|
||
|
raise InterpreterException('replace() takes exactly two arguments.')
|
||
|
if not isinstance(posargs[0], str) or not isinstance(posargs[1], str):
|
||
|
raise InterpreterException('replace() requires that both arguments be strings')
|
||
|
return obj.replace(posargs[0], posargs[1])
|
||
8 years ago
|
raise InterpreterException('Unknown method "%s" for a string.' % method_name)
|
||
|
|
||
5 years ago
|
def format_string(self, templ: str, args: T.List[TYPE_nvar]) -> str:
|
||
6 years ago
|
arg_strings = []
|
||
|
for arg in args:
|
||
5 years ago
|
if isinstance(arg, mparser.BaseNode):
|
||
|
arg = self.evaluate_statement(arg)
|
||
6 years ago
|
if isinstance(arg, bool): # Python boolean is upper case.
|
||
|
arg = str(arg).lower()
|
||
|
arg_strings.append(str(arg))
|
||
|
|
||
5 years ago
|
def arg_replace(match: T.Match[str]) -> str:
|
||
6 years ago
|
idx = int(match.group(1))
|
||
|
if idx >= len(arg_strings):
|
||
4 years ago
|
raise InterpreterException(f'Format placeholder @{idx}@ out of range.')
|
||
6 years ago
|
return arg_strings[idx]
|
||
|
|
||
|
return re.sub(r'@(\d+)@', arg_replace, templ)
|
||
|
|
||
5 years ago
|
def unknown_function_called(self, func_name: str) -> None:
|
||
8 years ago
|
raise InvalidCode('Unknown function "%s".' % func_name)
|
||
8 years ago
|
|
||
5 years ago
|
@builtinMethodNoKwargs
|
||
5 years ago
|
def array_method_call(self, obj: T.List[TYPE_var], method_name: str, posargs: T.List[TYPE_nvar], kwargs: T.Dict[str, T.Any]) -> TYPE_var:
|
||
8 years ago
|
if method_name == 'contains':
|
||
5 years ago
|
def check_contains(el: list) -> bool:
|
||
|
if len(posargs) != 1:
|
||
|
raise InterpreterException('Contains method takes exactly one argument.')
|
||
|
item = posargs[0]
|
||
|
for element in el:
|
||
|
if isinstance(element, list):
|
||
|
found = check_contains(element)
|
||
|
if found:
|
||
|
return True
|
||
|
if element == item:
|
||
|
return True
|
||
|
return False
|
||
|
return check_contains(obj)
|
||
8 years ago
|
elif method_name == 'length':
|
||
|
return len(obj)
|
||
|
elif method_name == 'get':
|
||
8 years ago
|
index = posargs[0]
|
||
8 years ago
|
fallback = None
|
||
8 years ago
|
if len(posargs) == 2:
|
||
|
fallback = posargs[1]
|
||
|
elif len(posargs) > 2:
|
||
8 years ago
|
m = 'Array method \'get()\' only takes two arguments: the ' \
|
||
|
'index and an optional fallback value if the index is ' \
|
||
|
'out of range.'
|
||
|
raise InvalidArguments(m)
|
||
8 years ago
|
if not isinstance(index, int):
|
||
|
raise InvalidArguments('Array index must be a number.')
|
||
|
if index < -len(obj) or index >= len(obj):
|
||
8 years ago
|
if fallback is None:
|
||
|
m = 'Array index {!r} is out of bounds for array of size {!r}.'
|
||
|
raise InvalidArguments(m.format(index, len(obj)))
|
||
5 years ago
|
if isinstance(fallback, mparser.BaseNode):
|
||
|
return self.evaluate_statement(fallback)
|
||
8 years ago
|
return fallback
|
||
5 years ago
|
return obj[index]
|
||
8 years ago
|
m = 'Arrays do not have a method called {!r}.'
|
||
|
raise InterpreterException(m.format(method_name))
|
||
8 years ago
|
|
||
5 years ago
|
@builtinMethodNoKwargs
|
||
5 years ago
|
def dict_method_call(self, obj: T.Dict[str, TYPE_var], method_name: str, posargs: T.List[TYPE_nvar], kwargs: T.Dict[str, T.Any]) -> TYPE_var:
|
||
7 years ago
|
if method_name in ('has_key', 'get'):
|
||
|
if method_name == 'has_key':
|
||
|
if len(posargs) != 1:
|
||
|
raise InterpreterException('has_key() takes exactly one argument.')
|
||
|
else:
|
||
|
if len(posargs) not in (1, 2):
|
||
|
raise InterpreterException('get() takes one or two arguments.')
|
||
|
|
||
|
key = posargs[0]
|
||
|
if not isinstance(key, (str)):
|
||
|
raise InvalidArguments('Dictionary key must be a string.')
|
||
|
|
||
|
has_key = key in obj
|
||
|
|
||
|
if method_name == 'has_key':
|
||
|
return has_key
|
||
|
|
||
|
if has_key:
|
||
|
return obj[key]
|
||
|
|
||
|
if len(posargs) == 2:
|
||
5 years ago
|
fallback = posargs[1]
|
||
|
if isinstance(fallback, mparser.BaseNode):
|
||
|
return self.evaluate_statement(fallback)
|
||
|
return fallback
|
||
7 years ago
|
|
||
4 years ago
|
raise InterpreterException(f'Key {key!r} is not in the dictionary.')
|
||
7 years ago
|
|
||
|
if method_name == 'keys':
|
||
|
if len(posargs) != 0:
|
||
|
raise InterpreterException('keys() takes no arguments.')
|
||
4 years ago
|
return sorted(obj.keys())
|
||
7 years ago
|
|
||
|
raise InterpreterException('Dictionaries do not have a method called "%s".' % method_name)
|
||
|
|
||
5 years ago
|
def reduce_arguments(
|
||
|
self,
|
||
|
args: mparser.ArgumentNode,
|
||
|
key_resolver: T.Callable[[mparser.BaseNode], str] = default_resolve_key,
|
||
|
duplicate_key_error: T.Optional[str] = None,
|
||
|
) -> T.Tuple[T.List[TYPE_nvar], TYPE_nkwargs]:
|
||
8 years ago
|
assert(isinstance(args, mparser.ArgumentNode))
|
||
|
if args.incorrect_order():
|
||
|
raise InvalidArguments('All keyword arguments must be after positional arguments.')
|
||
8 years ago
|
self.argument_depth += 1
|
||
5 years ago
|
reduced_pos = [self.evaluate_statement(arg) for arg in args.arguments] # type: T.List[TYPE_nvar]
|
||
5 years ago
|
reduced_kw = {} # type: TYPE_nkwargs
|
||
|
for key, val in args.kwargs.items():
|
||
5 years ago
|
reduced_key = key_resolver(key)
|
||
5 years ago
|
reduced_val = val # type: TYPE_nvar
|
||
|
if isinstance(reduced_val, mparser.BaseNode):
|
||
|
reduced_val = self.evaluate_statement(reduced_val)
|
||
5 years ago
|
if duplicate_key_error and reduced_key in reduced_kw:
|
||
|
raise InvalidArguments(duplicate_key_error.format(reduced_key))
|
||
5 years ago
|
reduced_kw[reduced_key] = reduced_val
|
||
8 years ago
|
self.argument_depth -= 1
|
||
6 years ago
|
final_kw = self.expand_default_kwargs(reduced_kw)
|
||
|
return reduced_pos, final_kw
|
||
|
|
||
5 years ago
|
def expand_default_kwargs(self, kwargs: TYPE_nkwargs) -> TYPE_nkwargs:
|
||
6 years ago
|
if 'kwargs' not in kwargs:
|
||
|
return kwargs
|
||
|
to_expand = kwargs.pop('kwargs')
|
||
|
if not isinstance(to_expand, dict):
|
||
6 years ago
|
raise InterpreterException('Value of "kwargs" must be dictionary.')
|
||
|
if 'kwargs' in to_expand:
|
||
|
raise InterpreterException('Kwargs argument must not contain a "kwargs" entry. Points for thinking meta, though. :P')
|
||
6 years ago
|
for k, v in to_expand.items():
|
||
|
if k in kwargs:
|
||
4 years ago
|
raise InterpreterException(f'Entry "{k}" defined both as a keyword argument and in a "kwarg" entry.')
|
||
6 years ago
|
kwargs[k] = v
|
||
|
return kwargs
|
||
8 years ago
|
|
||
5 years ago
|
def assignment(self, node: mparser.AssignmentNode) -> None:
|
||
8 years ago
|
assert(isinstance(node, mparser.AssignmentNode))
|
||
8 years ago
|
if self.argument_depth != 0:
|
||
|
raise InvalidArguments('''Tried to assign values inside an argument list.
|
||
|
To specify a keyword argument, use : instead of =.''')
|
||
8 years ago
|
var_name = node.var_name
|
||
|
if not isinstance(var_name, str):
|
||
|
raise InvalidArguments('Tried to assign value to a non-variable.')
|
||
|
value = self.evaluate_statement(node.value)
|
||
|
if not self.is_assignable(value):
|
||
|
raise InvalidCode('Tried to assign an invalid value to variable.')
|
||
|
# For mutable objects we need to make a copy on assignment
|
||
|
if isinstance(value, MutableInterpreterObject):
|
||
|
value = copy.deepcopy(value)
|
||
|
self.set_variable(var_name, value)
|
||
8 years ago
|
return None
|
||
8 years ago
|
|
||
5 years ago
|
def set_variable(self, varname: str, variable: TYPE_var) -> None:
|
||
8 years ago
|
if variable is None:
|
||
|
raise InvalidCode('Can not assign None to variable.')
|
||
|
if not isinstance(varname, str):
|
||
|
raise InvalidCode('First argument to set_variable must be a string.')
|
||
|
if not self.is_assignable(variable):
|
||
|
raise InvalidCode('Assigned value not of assignable type.')
|
||
|
if re.match('[_a-zA-Z][_0-9a-zA-Z]*$', varname) is None:
|
||
|
raise InvalidCode('Invalid variable name: ' + varname)
|
||
|
if varname in self.builtin:
|
||
|
raise InvalidCode('Tried to overwrite internal variable "%s"' % varname)
|
||
|
self.variables[varname] = variable
|
||
|
|
||
5 years ago
|
def get_variable(self, varname: str) -> TYPE_var:
|
||
8 years ago
|
if varname in self.builtin:
|
||
|
return self.builtin[varname]
|
||
|
if varname in self.variables:
|
||
|
return self.variables[varname]
|
||
|
raise InvalidCode('Unknown variable "%s".' % varname)
|
||
|
|
||
5 years ago
|
def is_assignable(self, value: T.Any) -> bool:
|
||
8 years ago
|
return isinstance(value, (InterpreterObject, dependencies.Dependency,
|
||
7 years ago
|
str, int, list, dict, mesonlib.File))
|
||
8 years ago
|
|
||
5 years ago
|
def validate_extraction(self, buildtarget: InterpreterObject) -> None:
|
||
|
raise InterpreterException('validate_extraction is not implemented in this context (please file a bug)')
|