diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2024-06-08 21:31:10 +0300 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2024-06-08 22:58:06 +0300 |
commit | 2b1510d7068747541f8d49ba876570d62fde5d0e (patch) | |
tree | 57672ec9b21c3381fa40c5ed26530f672d4ce6af | |
parent | 374fa7f0da278d46a4c3adb587f4b43089f5d7db (diff) | |
download | meson-2b1510d7068747541f8d49ba876570d62fde5d0e.zip meson-2b1510d7068747541f8d49ba876570d62fde5d0e.tar.gz meson-2b1510d7068747541f8d49ba876570d62fde5d0e.tar.bz2 |
Convert option from a plain dictionary into a named class.
-rw-r--r-- | mesonbuild/coredata.py | 16 | ||||
-rw-r--r-- | mesonbuild/environment.py | 16 | ||||
-rw-r--r-- | mesonbuild/options.py | 41 |
3 files changed, 54 insertions, 19 deletions
diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 782e770..e4081e3 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -58,7 +58,7 @@ if T.TYPE_CHECKING: OptionDictType = T.Union[T.Dict[str, 'options.UserOption[T.Any]'], 'OptionsView'] MutableKeyedOptionDictType = T.Dict['OptionKey', 'options.UserOption[T.Any]'] - KeyedOptionDictType = T.Union[MutableKeyedOptionDictType, 'OptionsView'] + KeyedOptionDictType = T.Union['options.OptionStore', MutableKeyedOptionDictType, 'OptionsView'] CompilerCheckCacheKey = T.Tuple[T.Tuple[str, ...], str, FileOrString, T.Tuple[str, ...], CompileCheckMode] # code, args RunCheckCacheKey = T.Tuple[str, T.Tuple[str, ...]] @@ -241,7 +241,7 @@ _V = T.TypeVar('_V') class CoreData: - def __init__(self, options: SharedCMDOptions, scratch_dir: str, meson_command: T.List[str]): + def __init__(self, cmd_options: SharedCMDOptions, scratch_dir: str, meson_command: T.List[str]): self.lang_guids = { 'default': '8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942', 'c': '8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942', @@ -255,8 +255,8 @@ class CoreData: self.meson_command = meson_command self.target_guids = {} self.version = version - self.options: 'MutableKeyedOptionDictType' = {} - self.cross_files = self.__load_config_files(options, scratch_dir, 'cross') + self.options = options.OptionStore() + self.cross_files = self.__load_config_files(cmd_options, scratch_dir, 'cross') self.compilers: PerMachine[T.Dict[str, Compiler]] = PerMachine(OrderedDict(), OrderedDict()) # Stores the (name, hash) of the options file, The name will be either @@ -282,18 +282,18 @@ class CoreData: self.cmake_cache: PerMachine[CMakeStateCache] = PerMachine(CMakeStateCache(), CMakeStateCache()) # Only to print a warning if it changes between Meson invocations. - self.config_files = self.__load_config_files(options, scratch_dir, 'native') + self.config_files = self.__load_config_files(cmd_options, scratch_dir, 'native') self.builtin_options_libdir_cross_fixup() self.init_builtins('') @staticmethod - def __load_config_files(options: SharedCMDOptions, scratch_dir: str, ftype: str) -> T.List[str]: + def __load_config_files(cmd_options: SharedCMDOptions, scratch_dir: str, ftype: str) -> T.List[str]: # Need to try and make the passed filenames absolute because when the # files are parsed later we'll have chdir()d. if ftype == 'cross': - filenames = options.cross_file + filenames = cmd_options.cross_file else: - filenames = options.native_file + filenames = cmd_options.native_file if not filenames: return [] diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index 1607c32..be40dbc 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -520,7 +520,7 @@ class Environment: log_dir = 'meson-logs' info_dir = 'meson-info' - def __init__(self, source_dir: str, build_dir: str, options: coredata.SharedCMDOptions) -> None: + def __init__(self, source_dir: str, build_dir: str, cmd_options: coredata.SharedCMDOptions) -> None: self.source_dir = source_dir self.build_dir = build_dir # Do not try to create build directories when build_dir is none. @@ -536,26 +536,26 @@ class Environment: self.coredata: coredata.CoreData = coredata.load(self.get_build_dir(), suggest_reconfigure=False) self.first_invocation = False except FileNotFoundError: - self.create_new_coredata(options) + self.create_new_coredata(cmd_options) except coredata.MesonVersionMismatchException as e: # This is routine, but tell the user the update happened mlog.log('Regenerating configuration from scratch:', str(e)) - coredata.read_cmd_line_file(self.build_dir, options) - self.create_new_coredata(options) + coredata.read_cmd_line_file(self.build_dir, cmd_options) + self.create_new_coredata(cmd_options) except MesonException as e: # If we stored previous command line options, we can recover from # a broken/outdated coredata. if os.path.isfile(coredata.get_cmd_line_file(self.build_dir)): mlog.warning('Regenerating configuration from scratch.', fatal=False) mlog.log('Reason:', mlog.red(str(e))) - coredata.read_cmd_line_file(self.build_dir, options) - self.create_new_coredata(options) + coredata.read_cmd_line_file(self.build_dir, cmd_options) + self.create_new_coredata(cmd_options) else: raise MesonException(f'{str(e)} Try regenerating using "meson setup --wipe".') else: # Just create a fresh coredata in this case self.scratch_dir = '' - self.create_new_coredata(options) + self.create_new_coredata(cmd_options) ## locally bind some unfrozen configuration @@ -627,7 +627,7 @@ class Environment: self.cmakevars = cmakevars.default_missing() # Command line options override those from cross/native files - self.options.update(options.cmd_line_options) + self.options.update(cmd_options.cmd_line_options) # Take default value from env if not set in cross/native files or command line. self._set_default_options_from_env() diff --git a/mesonbuild/options.py b/mesonbuild/options.py index 2699dd5..6ffcf1a 100644 --- a/mesonbuild/options.py +++ b/mesonbuild/options.py @@ -25,6 +25,7 @@ from .mesonlib import ( from . import mlog import typing as T +from typing import ItemsView DEFAULT_YIELDING = False @@ -473,8 +474,42 @@ BUILTIN_DIR_NOPREFIX_OPTIONS: T.Dict[OptionKey, T.Dict[str, str]] = { OptionKey('purelibdir', module='python'): {}, } - class OptionStore: def __init__(self): - # This class will hold all options for a given build directory - self.dummy = None + self.d: T.Dict['OptionKey', 'UserOption[T.Any]'] = {} + + def __len__(self): + return len(self.d) + + def __setitem__(self, key, value): + self.d[key] = value + + def __getitem__(self, key): + return self.d[key] + + def __delitem__(self, key): + del self.d[key] + + def __contains__(self, key): + return key in self.d + + def __repr__(self): + return repr(self.d) + + def keys(self): + return self.d.keys() + + def values(self): + return self.d.values() + + def items(self) -> ItemsView['OptionKey', 'USerOption[T.Any]']: + return self.d.items() + + def update(self, *args, **kwargs): + return self.d.update(*args, **kwargs) + + def setdefault(self, k, o): + return self.d.setdefault(k, o) + + def get(self, *args, **kwargs)-> UserOption: + return self.d.get(*args, **kwargs) |