Convert option from a plain dictionary into a named class.

pull/13307/head
Jussi Pakkanen 8 months ago
parent 374fa7f0da
commit 2b1510d706
  1. 16
      mesonbuild/coredata.py
  2. 16
      mesonbuild/environment.py
  3. 41
      mesonbuild/options.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 []

@ -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()

@ -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)

Loading…
Cancel
Save