From d778a371ac3eb0bdb2bfde84d753722b576d5f14 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Thu, 4 Apr 2019 13:32:55 -0700 Subject: [PATCH] coredata: Add an object to abstract builtins Currently the builtins are stored as lists, then there are a mess of functions which take said lists, check what the type of the first thing is, and then extract data from various points in the list based on that. This is stupid. This is what structs/classes are for. --- mesonbuild/coredata.py | 115 ++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 48 deletions(-) diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 01db6358b..9002fd931 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -26,6 +26,7 @@ from .wrap import WrapMode import ast import argparse import configparser +from typing import Optional, Any, TypeVar, Generic, Type version = '0.50.999' backendlist = ['ninja', 'vs', 'vs2010', 'vs2015', 'vs2017', 'xcode'] @@ -335,11 +336,8 @@ class CoreData: def init_builtins(self): # Create builtin options with default values self.builtins = {} - prefix = get_builtin_option_default('prefix') - for key in get_builtin_options(): - value = get_builtin_option_default(key, prefix) - args = [key] + builtin_options[key][1:-1] + [value] - self.builtins[key] = builtin_options[key][0](*args) + for key, opt in builtin_options.items(): + self.builtins[key] = opt.init_option(key) def init_backend_options(self, backend_name): if backend_name == 'ninja': @@ -664,25 +662,24 @@ def is_builtin_option(optname): def get_builtin_option_choices(optname): if is_builtin_option(optname): - if builtin_options[optname][0] == UserComboOption: - return builtin_options[optname][2] - elif builtin_options[optname][0] == UserBooleanOption: + b = builtin_options[optname] + if b.opt_type is UserBooleanOption: return [True, False] - elif builtin_options[optname][0] == UserFeatureOption: + elif b.opt_type is UserFeatureOption: return UserFeatureOption.static_choices else: - return None + return b.choices else: raise RuntimeError('Tried to get the supported values for an unknown builtin option \'%s\'.' % optname) def get_builtin_option_description(optname): if is_builtin_option(optname): - return builtin_options[optname][1] + return builtin_options[optname].description else: raise RuntimeError('Tried to get the description for an unknown builtin option \'%s\'.' % optname) def get_builtin_option_action(optname): - default = builtin_options[optname][2] + default = builtin_options[optname].default if default is True: return 'store_false' elif default is False: @@ -692,15 +689,13 @@ def get_builtin_option_action(optname): def get_builtin_option_default(optname, prefix=''): if is_builtin_option(optname): o = builtin_options[optname] - if o[0] == UserComboOption: - return o[3] - if o[0] == UserIntegerOption: - return o[4] + if o.opt_type in [UserComboOption, UserIntegerOption]: + return o.default try: return builtin_dir_noprefix_options[optname][prefix] except KeyError: pass - return o[2] + return o.default else: raise RuntimeError('Tried to get the default value for an unknown builtin option \'%s\'.' % optname) @@ -757,38 +752,62 @@ def parse_cmd_line_options(args): args.cmd_line_options[name] = value delattr(args, name) + +_U = TypeVar('_U', bound=UserOption) + +class BuiltinOption(Generic[_U]): + + """Class for a builtin option type. + + Currently doesn't support UserIntegerOption, or a few other cases. + """ + + def __init__(self, opt_type: Type[_U], description: str, default: Any, yielding: Optional[bool] = None, *, + choices: Any = None): + self.opt_type = opt_type + self.description = description + self.default = default + self.choices = choices + self.yielding = yielding + + def init_option(self, name: str) -> _U: + """Create an instance of opt_type and return it.""" + keywords = {'yielding': self.yielding, 'value': self.default} + if self.choices: + keywords['choices'] = self.choices + return self.opt_type(name, self.description, **keywords) + + builtin_options = { - 'buildtype': [UserComboOption, 'Build type to use', ['plain', 'debug', 'debugoptimized', 'release', 'minsize', 'custom'], 'debug'], - 'strip': [UserBooleanOption, 'Strip targets on install', False], - 'unity': [UserComboOption, 'Unity build', ['on', 'off', 'subprojects'], 'off'], - 'prefix': [UserStringOption, 'Installation prefix', default_prefix()], - 'libdir': [UserStringOption, 'Library directory', default_libdir()], - 'libexecdir': [UserStringOption, 'Library executable directory', default_libexecdir()], - 'bindir': [UserStringOption, 'Executable directory', 'bin'], - 'sbindir': [UserStringOption, 'System executable directory', 'sbin'], - 'includedir': [UserStringOption, 'Header file directory', 'include'], - 'datadir': [UserStringOption, 'Data file directory', 'share'], - 'mandir': [UserStringOption, 'Manual page directory', 'share/man'], - 'infodir': [UserStringOption, 'Info page directory', 'share/info'], - 'localedir': [UserStringOption, 'Locale data directory', 'share/locale'], - 'sysconfdir': [UserStringOption, 'Sysconf data directory', 'etc'], - 'localstatedir': [UserStringOption, 'Localstate data directory', 'var'], - 'sharedstatedir': [UserStringOption, 'Architecture-independent data directory', 'com'], - 'werror': [UserBooleanOption, 'Treat warnings as errors', False], - 'warning_level': [UserComboOption, 'Compiler warning level to use', ['0', '1', '2', '3'], '1'], - 'layout': [UserComboOption, 'Build directory layout', ['mirror', 'flat'], 'mirror'], - 'default_library': [UserComboOption, 'Default library type', ['shared', 'static', 'both'], 'shared'], - 'backend': [UserComboOption, 'Backend to use', backendlist, 'ninja'], - 'stdsplit': [UserBooleanOption, 'Split stdout and stderr in test logs', True], - 'errorlogs': [UserBooleanOption, "Whether to print the logs from failing tests", True], - 'install_umask': [UserUmaskOption, 'Default umask to apply on permissions of installed files', '022'], - 'auto_features': [UserFeatureOption, "Override value of all 'auto' features", 'auto'], - 'optimization': [UserComboOption, 'Optimization level', ['0', 'g', '1', '2', '3', 's'], '0'], - 'debug': [UserBooleanOption, 'Debug', True], - 'wrap_mode': [UserComboOption, 'Wrap mode', ['default', - 'nofallback', - 'nodownload', - 'forcefallback'], 'default'], + 'buildtype': BuiltinOption(UserComboOption, 'Build type to use', 'debug', + choices=['plain', 'debug', 'debugoptimized', 'release', 'minsize', 'custom']), + 'strip': BuiltinOption(UserBooleanOption, 'Strip targets on install', False), + 'unity': BuiltinOption(UserComboOption, 'Unity build', 'off', choices=['on', 'off', 'subprojects']), + 'prefix': BuiltinOption(UserStringOption, 'Installation prefix', default_prefix()), + 'libdir': BuiltinOption(UserStringOption, 'Library directory', default_libdir()), + 'libexecdir': BuiltinOption(UserStringOption, 'Library executable directory', default_libexecdir()), + 'bindir': BuiltinOption(UserStringOption, 'Executable directory', 'bin'), + 'sbindir': BuiltinOption(UserStringOption, 'System executable directory', 'sbin'), + 'includedir': BuiltinOption(UserStringOption, 'Header file directory', 'include'), + 'datadir': BuiltinOption(UserStringOption, 'Data file directory', 'share'), + 'mandir': BuiltinOption(UserStringOption, 'Manual page directory', 'share/man'), + 'infodir': BuiltinOption(UserStringOption, 'Info page directory', 'share/info'), + 'localedir': BuiltinOption(UserStringOption, 'Locale data directory', 'share/locale'), + 'sysconfdir': BuiltinOption(UserStringOption, 'Sysconf data directory', 'etc'), + 'localstatedir': BuiltinOption(UserStringOption, 'Localstate data directory', 'var'), + 'sharedstatedir': BuiltinOption(UserStringOption, 'Architecture-independent data directory', 'com'), + 'werror': BuiltinOption(UserBooleanOption, 'Treat warnings as errors', False), + 'warning_level': BuiltinOption(UserComboOption, 'Compiler warning level to use', '1', choices=['0', '1', '2', '3']), + 'layout': BuiltinOption(UserComboOption, 'Build directory layout', 'mirror', choices=['mirror', 'flat']), + 'default_library': BuiltinOption(UserComboOption, 'Default library type', 'shared', choices=['shared', 'static', 'both']), + 'backend': BuiltinOption(UserComboOption, 'Backend to use', 'ninja', choices=backendlist), + 'stdsplit': BuiltinOption(UserBooleanOption, 'Split stdout and stderr in test logs', True), + 'errorlogs': BuiltinOption(UserBooleanOption, "Whether to print the logs from failing tests", True), + 'install_umask': BuiltinOption(UserUmaskOption, 'Default umask to apply on permissions of installed files', '022'), + 'auto_features': BuiltinOption(UserFeatureOption, "Override value of all 'auto' features", 'auto'), + 'optimization': BuiltinOption(UserComboOption, 'Optimization level', '0', choices=['0', 'g', '1', '2', '3', 's']), + 'debug': BuiltinOption(UserBooleanOption, 'Debug', True), + 'wrap_mode': BuiltinOption(UserComboOption, 'Wrap mode', 'default', choices=['default', 'nofallback', 'nodownload', 'forcefallback']), } # Special prefix-dependent defaults for installation directories that reside in