Skip to content

Commit

Permalink
options: Add an EnumeratedUserOption class
Browse files Browse the repository at this point in the history
This will allow us to take choices out of the UserOption class, which
doesn't actually use this attribute.
  • Loading branch information
dcbaker committed Aug 30, 2024
1 parent 199e179 commit 2244eff
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 82 deletions.
12 changes: 4 additions & 8 deletions mesonbuild/cmake/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .traceparser import CMakeTraceParser
from .tracetargets import resolve_cmake_trace_targets
from .. import mlog, mesonlib
from .. import options
from ..mesonlib import MachineChoice, OrderedSet, path_is_in_root, relative_to_if_possible
from ..options import OptionKey
from ..mesondata import DataFile
Expand Down Expand Up @@ -533,17 +534,12 @@ def _all_source_suffixes(self) -> 'ImmutableListProtocol[str]':
@lru_cache(maxsize=None)
def _all_lang_stds(self, lang: str) -> 'ImmutableListProtocol[str]':
try:
res = self.env.coredata.optstore.get_value_object(OptionKey(f'{lang}_std', machine=MachineChoice.BUILD)).choices
opt = self.env.coredata.optstore.get_value_object(OptionKey(f'{lang}_std', machine=MachineChoice.BUILD))
assert isinstance(opt, (options.UserStdOption, options.UserComboOption)), 'for mypy'
return opt.choices or []
except KeyError:
return []

# TODO: Get rid of this once we have proper typing for options
assert isinstance(res, list)
for i in res:
assert isinstance(i, str)

return res

def process_inter_target_dependencies(self) -> None:
# Move the dependencies from all TRANSFER_DEPENDENCIES_FROM to the target
to_process = list(self.depends)
Expand Down
14 changes: 5 additions & 9 deletions mesonbuild/compilers/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
opts = super().get_options()
if self.info.is_windows() or self.info.is_cygwin():
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
gnu_winlibs)
Expand Down Expand Up @@ -306,7 +306,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
std_opt.set_versions(stds, gnu=True)
if self.info.is_windows() or self.info.is_cygwin():
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
gnu_winlibs)
Expand Down Expand Up @@ -447,7 +447,7 @@ class VisualStudioLikeCCompilerMixin(CompilerMixinBase):
def get_options(self) -> MutableKeyedOptionDictType:
opts = super().get_options()
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
msvc_winlibs)
Expand Down Expand Up @@ -774,9 +774,7 @@ def get_instruction_set_args(self, instruction_set: str) -> T.Optional[T.List[st

def get_options(self) -> 'MutableKeyedOptionDictType':
opts = CCompiler.get_options(self)
c_stds = ['c99']
key = self.form_compileropt_key('std')
opts[key].choices = ['none'] + c_stds
self._update_language_stds(opts, ['c99'])
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
Expand Down Expand Up @@ -804,9 +802,7 @@ def get_instruction_set_args(self, instruction_set: str) -> T.Optional[T.List[st

def get_options(self) -> 'MutableKeyedOptionDictType':
opts = CCompiler.get_options(self)
c_stds = ['c99']
key = self.form_compileropt_key('std')
opts[key].choices = ['none'] + c_stds
self._update_language_stds(opts, ['c99'])
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
Expand Down
13 changes: 11 additions & 2 deletions mesonbuild/compilers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,15 @@ def get_preprocessor(self) -> Compiler:
def form_compileropt_key(self, basename: str) -> OptionKey:
return OptionKey(f'{self.language}_{basename}', machine=self.for_machine)

def _update_language_stds(self, opts: MutableKeyedOptionDictType, value: T.List[str]) -> None:
key = self.form_compileropt_key('std')
std = opts[key]
assert isinstance(std, (options.UserStdOption, options.UserComboOption)), 'for mypy'
if 'none' not in value:
value = ['none'] + value
std.choices = value


def get_global_options(lang: str,
comp: T.Type[Compiler],
for_machine: MachineChoice,
Expand All @@ -1366,12 +1375,12 @@ def get_global_options(lang: str,
comp_options = env.options.get(comp_key, [])
link_options = env.options.get(largkey, [])

cargs = options.UserArrayOption(
cargs = options.UserStringArrayOption(
f'{lang}_{argkey.name}',
description + ' compiler',
comp_options, split_args=True, allow_dups=True)

largs = options.UserArrayOption(
largs = options.UserStringArrayOption(
f'{lang}_{largkey.name}',
description + ' linker',
link_options, split_args=True, allow_dups=True)
Expand Down
28 changes: 12 additions & 16 deletions mesonbuild/compilers/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.make_option_name(key),
'C++ exception handling type.',
'default',
['none', 'default', 'a', 's', 'sc'])
choices=['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
Expand All @@ -272,7 +272,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
std_opt.set_versions(cppstd_choices, gnu=True)
if self.info.is_windows() or self.info.is_cygwin():
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
gnu_winlibs)
Expand Down Expand Up @@ -401,7 +401,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.make_option_name(key),
'C++ exception handling type.',
'default',
['none', 'default', 'a', 's', 'sc'])
choices=['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('std')
std_opt = opts[key]
Expand Down Expand Up @@ -451,7 +451,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.make_option_name(key),
'C++ exception handling type.',
'default',
['none', 'default', 'a', 's', 'sc'])
choices=['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
Expand Down Expand Up @@ -480,7 +480,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':

if self.info.is_windows() or self.info.is_cygwin():
key = key.evolve(name='cpp_winlibs')
opts[key] = options.UserArrayOption(
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
gnu_winlibs)
Expand Down Expand Up @@ -582,7 +582,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.make_option_name(key),
'C++ exception handling type.',
'default',
['none', 'default', 'a', 's', 'sc'])
choices=['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('debugstl')
opts[key] = options.UserBooleanOption(
Expand Down Expand Up @@ -665,7 +665,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.make_option_name(key),
'C++ exception handling type.',
'default',
['none', 'default', 'a', 's', 'sc'])
choices=['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
Expand Down Expand Up @@ -695,9 +695,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
c_stds += ['c++2a']
g_stds += ['gnu++2a']

std_opt = opts[self.form_compileropt_key('std')]
assert isinstance(std_opt, options.UserStdOption), 'for mypy'
std_opt.set_versions(c_stds + g_stds)
self._update_language_stds(opts, c_stds + g_stds)
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
Expand Down Expand Up @@ -758,7 +756,7 @@ def _get_options_impl(self, opts: 'MutableKeyedOptionDictType', cpp_stds: T.List
self.make_option_name(key),
'C++ exception handling type.',
'default',
['none', 'default', 'a', 's', 'sc'])
choices=['none', 'default', 'a', 's', 'sc'])

key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
Expand All @@ -767,7 +765,7 @@ def _get_options_impl(self, opts: 'MutableKeyedOptionDictType', cpp_stds: T.List
True)

key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Windows libs to link against.',
msvc_winlibs)
Expand Down Expand Up @@ -1039,8 +1037,7 @@ def get_instruction_set_args(self, instruction_set: str) -> T.Optional[T.List[st

def get_options(self) -> 'MutableKeyedOptionDictType':
opts = CPPCompiler.get_options(self)
key = self.form_compileropt_key('std')
opts[key].choices = ['none']
self._update_language_stds(opts, [])
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
Expand Down Expand Up @@ -1068,8 +1065,7 @@ def get_instruction_set_args(self, instruction_set: str) -> T.Optional[T.List[st

def get_options(self) -> 'MutableKeyedOptionDictType':
opts = CPPCompiler.get_options(self)
key = self.form_compileropt_key('std')
opts[key].choices = ['none']
self._update_language_stds(opts, [])
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
Expand Down
3 changes: 1 addition & 2 deletions mesonbuild/compilers/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,12 @@ def get_options(self) -> 'MutableKeyedOptionDictType':

opts = super().get_options()

# XXX: cpp_std is correct, the annotations are wrong
key = self.form_compileropt_key('std')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ language standard to use with CUDA',
'none',
cpp_stds)
choices=cpp_stds)

key = self.form_compileropt_key('ccbindir')
opts[key] = options.UserStringOption(
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.make_option_name(key),
'Python version to target',
'3',
['2', '3'])
choices=['2', '3'])

key = self.form_compileropt_key('language')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Output C or C++ files',
'c',
['c', 'cpp'])
choices=['c', 'cpp'])

return opts

Expand Down
15 changes: 5 additions & 10 deletions mesonbuild/compilers/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.make_option_name(key),
'Fortran language standard to use',
'none',
['none'])
choices=['none'])

return opts

Expand Down Expand Up @@ -149,8 +149,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
fortran_stds += ['f2008']
if version_compare(self.version, '>=8.0.0'):
fortran_stds += ['f2018']
key = self.form_compileropt_key('std')
opts[key].choices = ['none'] + fortran_stds
self._update_language_stds(opts, fortran_stds)
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
Expand Down Expand Up @@ -206,9 +205,7 @@ def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoic

def get_options(self) -> 'MutableKeyedOptionDictType':
opts = FortranCompiler.get_options(self)
fortran_stds = ['f95', 'f2003', 'f2008', 'gnu', 'legacy', 'f2008ts']
key = self.form_compileropt_key('std')
opts[key].choices = ['none'] + fortran_stds
self._update_language_stds(opts, ['f95', 'f2003', 'f2008', 'gnu', 'legacy', 'f2008ts'])
return opts

def get_module_outdir_args(self, path: str) -> T.List[str]:
Expand Down Expand Up @@ -286,8 +283,7 @@ def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoic

def get_options(self) -> 'MutableKeyedOptionDictType':
opts = FortranCompiler.get_options(self)
key = self.form_compileropt_key('std')
opts[key].choices = ['none', 'legacy', 'f95', 'f2003', 'f2008', 'f2018']
self._update_language_stds(opts, ['none', 'legacy', 'f95', 'f2003', 'f2008', 'f2018'])
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
Expand Down Expand Up @@ -341,8 +337,7 @@ def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoic

def get_options(self) -> 'MutableKeyedOptionDictType':
opts = FortranCompiler.get_options(self)
key = self.form_compileropt_key('std')
opts[key].choices = ['none', 'legacy', 'f95', 'f2003', 'f2008', 'f2018']
self._update_language_stds(opts, ['none', 'legacy', 'f95', 'f2003', 'f2008', 'f2018'])
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/objc.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_options(self) -> 'coredata.MutableKeyedOptionDictType':
self.make_option_name(key),
'C language standard to use',
'none',
['none', 'c89', 'c99', 'c11', 'c17', 'gnu89', 'gnu99', 'gnu11', 'gnu17'])
choices=['none', 'c89', 'c99', 'c11', 'c17', 'gnu89', 'gnu99', 'gnu11', 'gnu17'])

return opts

Expand Down
8 changes: 5 additions & 3 deletions mesonbuild/compilers/objcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ def get_options(self) -> coredata.MutableKeyedOptionDictType:
self.make_option_name(key),
'C++ language standard to use',
'none',
['none', 'c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++2b',
'gnu++98', 'gnu++11', 'gnu++14', 'gnu++17', 'gnu++20',
'gnu++2b'])
choices=[
'none', 'c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++2b',
'gnu++98', 'gnu++11', 'gnu++14', 'gnu++17', 'gnu++20',
'gnu++2b',
])

return opts

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def get_options(self) -> MutableKeyedOptionDictType:
self.make_option_name(key),
'Rust edition to use',
'none',
['none', '2015', '2018', '2021'])
choices=['none', '2015', '2018', '2021'])

return opts

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def update_project_options(self, project_options: 'MutableKeyedOptionDictType',
oldval = self.optstore.get_value_object(key)
if type(oldval) is not type(value):
self.optstore.set_value(key, value.value)
elif oldval.choices != value.choices:
elif oldval.printable_choices() != value.printable_choices():
# If the choices have changed, use the new value, but attempt
# to keep the old options. If they are not valid keep the new
# defaults but warn.
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def print_options(self, title: str, opts: 'T.Union[dict[OptionKey, UserOption[An
printable_value = '<inherited from main project>'
if isinstance(o, options.UserFeatureOption) and o.is_auto():
printable_value = auto.printable_value()
self.add_option(str(root), o.description, printable_value, o.choices)
self.add_option(str(root), o.description, printable_value, o.printable_choices())

def print_conf(self, pager: bool) -> None:
if pager:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/mintro.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def add_keys(opts: 'T.Union[dict[OptionKey, UserOption[Any]], cdata.KeyedOptionD
typestr = 'combo'
elif isinstance(opt, options.UserIntegerOption):
typestr = 'integer'
elif isinstance(opt, options.UserArrayOption):
elif isinstance(opt, options.UserStringArrayOption):
typestr = 'array'
c = opt.printable_choices()
if c:
Expand Down
Loading

0 comments on commit 2244eff

Please sign in to comment.