diff options
| author | Dylan Baker <dylan@pnwbakers.com> | 2025-02-12 09:30:04 -0800 |
|---|---|---|
| committer | Dylan Baker <dylan@pnwbakers.com> | 2025-03-03 11:26:23 -0800 |
| commit | 71304684024bcdec102212117ff1ae3880c0318c (patch) | |
| tree | f7fda13c827649d916eff67bab65168c3834b731 /mesonbuild/compilers/compilers.py | |
| parent | daf4774cea27e223ca338310912734c05d0f4d54 (diff) | |
| download | meson-71304684024bcdec102212117ff1ae3880c0318c.tar.gz | |
compilers: Remove the BaseOption type
This class only served one purpose, to avoid typing the name of the
option twice. Unfortunately the way it was implemented made getting the
type checking right difficult, and required storing the same data twice.
This patch replaces this approach with a dictionary comprehension that
creates the OptionKey from the UserOption. This allows us to initialize
a single dictionary once, avoid typing the name twice, delete lines of
code, and get better type safety.
As an added bonus, it means that the exported data from the module can
be marked module constant, ie, ALL_CAPS.
Diffstat (limited to 'mesonbuild/compilers/compilers.py')
| -rw-r--r-- | mesonbuild/compilers/compilers.py | 67 |
1 files changed, 27 insertions, 40 deletions
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 18535be42..5b640781e 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -214,48 +214,35 @@ clike_debug_args: T.Dict[bool, T.List[str]] = { MSCRT_VALS = ['none', 'md', 'mdd', 'mt', 'mtd'] - -@dataclass -class BaseOption(T.Generic[_T]): - opt_type: T.Type[options.UserOption[_T]] - description: str - default: T.Any = None - choices: T.Any = None - - def init_option(self, name: OptionKey) -> options.UserOption[_T]: - keywords = {} - if self.choices: - keywords['choices'] = self.choices - return self.opt_type(name.name, self.description, self.default, **keywords) - - -BASE_OPTIONS: T.Mapping[OptionKey, BaseOption] = { - OptionKey('b_pch'): BaseOption(options.UserBooleanOption, 'Use precompiled headers', True), - OptionKey('b_lto'): BaseOption(options.UserBooleanOption, 'Use link time optimization', False), - OptionKey('b_lto_threads'): BaseOption(options.UserIntegerOption, 'Use multiple threads for Link Time Optimization', 0), - OptionKey('b_lto_mode'): BaseOption(options.UserComboOption, 'Select between different LTO modes.', 'default', - choices=['default', 'thin']), - OptionKey('b_thinlto_cache'): BaseOption(options.UserBooleanOption, 'Use LLVM ThinLTO caching for faster incremental builds', False), - OptionKey('b_thinlto_cache_dir'): BaseOption(options.UserStringOption, 'Directory to store ThinLTO cache objects', ''), - OptionKey('b_sanitize'): BaseOption(options.UserComboOption, 'Code sanitizer to use', 'none', - choices=['none', 'address', 'thread', 'undefined', 'memory', 'leak', 'address,undefined']), - OptionKey('b_lundef'): BaseOption(options.UserBooleanOption, 'Use -Wl,--no-undefined when linking', True), - OptionKey('b_asneeded'): BaseOption(options.UserBooleanOption, 'Use -Wl,--as-needed when linking', True), - OptionKey('b_pgo'): BaseOption(options.UserComboOption, 'Use profile guided optimization', 'off', - choices=['off', 'generate', 'use']), - OptionKey('b_coverage'): BaseOption(options.UserBooleanOption, 'Enable coverage tracking.', False), - OptionKey('b_colorout'): BaseOption(options.UserComboOption, 'Use colored output', 'always', - choices=['auto', 'always', 'never']), - OptionKey('b_ndebug'): BaseOption(options.UserComboOption, 'Disable asserts', 'false', choices=['true', 'false', 'if-release']), - OptionKey('b_staticpic'): BaseOption(options.UserBooleanOption, 'Build static libraries as position independent', True), - OptionKey('b_pie'): BaseOption(options.UserBooleanOption, 'Build executables as position independent', False), - OptionKey('b_bitcode'): BaseOption(options.UserBooleanOption, 'Generate and embed bitcode (only macOS/iOS/tvOS)', False), - OptionKey('b_vscrt'): BaseOption(options.UserComboOption, 'VS run-time library type to use.', 'from_buildtype', - choices=MSCRT_VALS + ['from_buildtype', 'static_from_buildtype']), +BASE_OPTIONS: T.Mapping[OptionKey, options.AnyOptionType] = { + OptionKey(o.name): o for o in T.cast('T.List[options.AnyOptionType]', [ + options.UserBooleanOption('b_pch', 'Use precompiled headers', True), + options.UserBooleanOption('b_lto', 'Use link time optimization', False), + options.UserIntegerOption('b_lto_threads', 'Use multiple threads for Link Time Optimization', 0), + options.UserComboOption('b_lto_mode', 'Select between different LTO modes.', 'default', choices=['default', 'thin']), + options.UserBooleanOption('b_thinlto_cache', 'Use LLVM ThinLTO caching for faster incremental builds', False), + options.UserStringOption('b_thinlto_cache_dir', 'Directory to store ThinLTO cache objects', ''), + options.UserComboOption( + 'b_sanitize', 'Code sanitizer to use', 'none', + choices=['none', 'address', 'thread', 'undefined', 'memory', 'leak', 'address,undefined']), + options.UserBooleanOption('b_lundef', 'Use -Wl,--no-undefined when linking', True), + options.UserBooleanOption('b_asneeded', 'Use -Wl,--as-needed when linking', True), + options.UserComboOption( + 'b_pgo', 'Use profile guided optimization', 'off', choices=['off', 'generate', 'use']), + options.UserBooleanOption('b_coverage', 'Enable coverage tracking.', False), + options.UserComboOption( + 'b_colorout', 'Use colored output', 'always', choices=['auto', 'always', 'never']), + options.UserComboOption( + 'b_ndebug', 'Disable asserts', 'false', choices=['true', 'false', 'if-release']), + options.UserBooleanOption('b_staticpic', 'Build static libraries as position independent', True), + options.UserBooleanOption('b_pie', 'Build executables as position independent', False), + options.UserBooleanOption('b_bitcode', 'Generate and embed bitcode (only macOS/iOS/tvOS)', False), + options.UserComboOption( + 'b_vscrt', 'VS run-time library type to use.', 'from_buildtype', + choices=MSCRT_VALS + ['from_buildtype', 'static_from_buildtype']), + ]) } -base_options = {key: base_opt.init_option(key) for key, base_opt in BASE_OPTIONS.items()} - def option_enabled(boptions: T.Set[OptionKey], target: 'BuildTarget', env: 'Environment', |
