diff options
author | Xavier Claessens <xavier.claessens@collabora.com> | 2023-09-03 12:07:32 -0400 |
---|---|---|
committer | Xavier Claessens <xclaesse@gmail.com> | 2023-09-07 06:55:07 -0400 |
commit | d5f17bc9ffea9537057eb249fc68776eb53d5f58 (patch) | |
tree | 06ba89b450c0c22f762555d5cc90ead4dbfb32b1 /mesonbuild/coredata.py | |
parent | 3fc16f05b513f26aa5da614673116074f5d60396 (diff) | |
download | meson-d5f17bc9ffea9537057eb249fc68776eb53d5f58.zip meson-d5f17bc9ffea9537057eb249fc68776eb53d5f58.tar.gz meson-d5f17bc9ffea9537057eb249fc68776eb53d5f58.tar.bz2 |
Rename OptionOverrideProxy to OptionsView and move to coredata
Coredata is where all option handling is done so it makes sense there.
It is a view on a list of options for a given subproject and with
optional overrides. This change prepare for using that view in a more
generic way in the future.
Diffstat (limited to 'mesonbuild/coredata.py')
-rw-r--r-- | mesonbuild/coredata.py | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 4b0f9af..f151c7b 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -19,7 +19,9 @@ import pickle, os, uuid import sys from itertools import chain from pathlib import PurePath -from collections import OrderedDict +from collections import OrderedDict, abc +from dataclasses import dataclass + from .mesonlib import ( HoldableObject, MesonException, EnvironmentException, MachineChoice, PerMachine, @@ -42,12 +44,12 @@ if T.TYPE_CHECKING: from .compilers.compilers import Compiler, CompileResult, RunResult, CompileCheckMode from .dependencies.detect import TV_DepID from .environment import Environment - from .mesonlib import OptionOverrideProxy, FileOrString + from .mesonlib import FileOrString from .cmake.traceparser import CMakeCacheEntry - OptionDictType = T.Union[T.Dict[str, 'UserOption[T.Any]'], OptionOverrideProxy] + OptionDictType = T.Union[T.Dict[str, 'UserOption[T.Any]'], 'OptionsView'] MutableKeyedOptionDictType = T.Dict['OptionKey', 'UserOption[T.Any]'] - KeyedOptionDictType = T.Union[MutableKeyedOptionDictType, OptionOverrideProxy] + KeyedOptionDictType = T.Union[MutableKeyedOptionDictType, 'OptionsView'] CompilerCheckCacheKey = T.Tuple[T.Tuple[str, ...], str, FileOrString, T.Tuple[str, ...], CompileCheckMode] # code, args RunCheckCacheKey = T.Tuple[str, T.Tuple[str, ...]] @@ -375,6 +377,42 @@ class UserStdOption(UserComboOption): raise MesonException(f'None of values {candidates} are supported by the {self.lang.upper()} compiler. ' + f'Possible values are {self.choices}') +@dataclass +class OptionsView(abc.Mapping): + '''A view on an options dictionary for a given subproject and with overrides. + ''' + + # TODO: the typing here could be made more explicit using a TypeDict from + # python 3.8 or typing_extensions + options: KeyedOptionDictType + subproject: T.Optional[str] = None + overrides: T.Optional[T.Mapping[OptionKey, T.Union[str, int, bool, T.List[str]]]] = None + + def __getitem__(self, key: OptionKey) -> UserOption: + # FIXME: This is fundamentally the same algorithm than interpreter.get_option_internal(). + # We should try to share the code somehow. + key = key.evolve(subproject=self.subproject) + if not key.is_project(): + opt = self.options.get(key) + if opt is None or opt.yielding: + opt = self.options[key.as_root()] + else: + opt = self.options[key] + if opt.yielding: + opt = self.options.get(key.as_root(), opt) + if self.overrides: + override_value = self.overrides.get(key.as_root()) + if override_value is not None: + opt = copy.copy(opt) + opt.set_value(override_value) + return opt + + def __iter__(self) -> T.Iterator[OptionKey]: + return iter(self.options) + + def __len__(self) -> int: + return len(self.options) + class DependencyCacheType(enum.Enum): OTHER = 0 |