diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2021-02-25 14:24:17 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2021-02-26 15:31:15 -0800 |
commit | 219535aad426e9678fcab4887c1fdce321d2ba05 (patch) | |
tree | f017acad35a3a365e8d5e3585dd1e2a671ee269b /mesonbuild | |
parent | 2d8d21d5bc9a914a81e91437ff3fda45582b588c (diff) | |
download | meson-219535aad426e9678fcab4887c1fdce321d2ba05.zip meson-219535aad426e9678fcab4887c1fdce321d2ba05.tar.gz meson-219535aad426e9678fcab4887c1fdce321d2ba05.tar.bz2 |
interpreter: Clean up and simplify/optimize get_(external|cross)_property
These are both way more compilcated than they needed to be, this is a
much simpler implementation, and is shared between cross and external
properies.
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/interpreter.py | 59 |
1 files changed, 20 insertions, 39 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index ea65bc6..0225de4 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -25,7 +25,7 @@ from .mesonlib import FileMode, MachineChoice, OptionKey, Popen_safe, listify, e from .dependencies import ExternalProgram from .dependencies import InternalDependency, Dependency, NotFoundDependency, DependencyException from .depfile import DepFile -from .interpreterbase import InterpreterBase +from .interpreterbase import InterpreterBase, typed_pos_args from .interpreterbase import check_stringlist, flatten, noPosargs, noKwargs, stringArgs, permittedKwargs, noArgsFlattening from .interpreterbase import InterpreterException, InvalidArguments, InvalidCode, SubdirDoneRequest from .interpreterbase import InterpreterObject, MutableInterpreterObject, Disabler, disablerIfNotFound @@ -1922,7 +1922,7 @@ class Summary: mlog.log(*line, sep=list_sep) class MesonMain(InterpreterObject): - def __init__(self, build, interpreter): + def __init__(self, build: 'build.Build', interpreter: 'Interpreter'): InterpreterObject.__init__(self) self.build = build self.interpreter = interpreter @@ -2244,51 +2244,32 @@ class MesonMain(InterpreterObject): def project_name_method(self, args, kwargs): return self.interpreter.active_projectname + def __get_external_property_impl(self, propname: str, fallback: T.Optional[object], machine: MachineChoice) -> object: + """Shared implementation for get_cross_property and get_external_property.""" + try: + return self.interpreter.environment.properties[machine][propname] + except KeyError: + if fallback is not None: + return fallback + raise InterpreterException(f'Unknown property for {machine.get_lower_case_name()} machine: {propname}') + @noArgsFlattening @permittedKwargs({}) @FeatureDeprecated('meson.get_cross_property', '0.58.0', 'Use meson.get_external_property() instead') - def get_cross_property_method(self, args, kwargs) -> str: - if len(args) < 1 or len(args) > 2: - raise InterpreterException('Must have one or two arguments.') - propname = args[0] - if not isinstance(propname, str): - raise InterpreterException('Property name must be string.') - try: - props = self.interpreter.environment.properties.host - return props[propname] - except Exception: - if len(args) == 2: - return args[1] - raise InterpreterException('Unknown cross property: %s.' % propname) + @typed_pos_args('meson.get_cross_property', str, optargs=[object]) + def get_cross_property_method(self, args: T.Tuple[str, T.Optional[object]], kwargs: T.Dict[str, T.Any]) -> object: + propname, fallback = args + return self.__get_external_property_impl(propname, fallback, MachineChoice.HOST) @noArgsFlattening @permittedKwargs({'native'}) @FeatureNew('meson.get_external_property', '0.54.0') - def get_external_property_method(self, args: T.Sequence[str], kwargs: dict) -> str: - if len(args) < 1 or len(args) > 2: - raise InterpreterException('Must have one or two positional arguments.') - propname = args[0] - if not isinstance(propname, str): - raise InterpreterException('Property name must be string.') + @typed_pos_args('meson.get_external_property', str, optargs=[object]) + def get_external_property_method(self, args: T.Tuple[str, T.Optional[object]], kwargs: T.Dict[str, T.Any]) -> object: + propname, fallback = args + machine = self.interpreter.machine_from_native_kwarg(kwargs) + return self.__get_external_property_impl(propname, fallback, machine) - def _get_native() -> str: - try: - props = self.interpreter.environment.properties.build - return props[propname] - except Exception: - if len(args) == 2: - return args[1] - raise InterpreterException('Unknown native property: %s.' % propname) - if 'native' in kwargs: - if kwargs['native']: - return _get_native() - else: - return self.get_cross_property_method(args, {}) - else: # native: not specified - if self.build.environment.is_cross_build(): - return self.get_cross_property_method(args, kwargs) - else: - return _get_native() @permittedKwargs({'native'}) @FeatureNew('meson.has_external_property', '0.58.0') |