Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make coredata and environment fully type safe #13737

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f42f773
options: fix the annotations of _to_tuple
dcbaker Aug 28, 2024
13a4b19
options: Add a printable_choices method to UserOption
dcbaker Aug 29, 2024
c89c599
options: Get rid of the invalid _U type, and use UserOption[_T]
dcbaker Aug 29, 2024
0d4ed70
compilers: remove Compiler.create_option
dcbaker Aug 29, 2024
aee5c19
options: use dataclasses for UserOption
dcbaker Aug 29, 2024
9c48b1c
options: Add an EnumeratedUserOption class
dcbaker Aug 29, 2024
1127964
options: fix typing of add_to_argparse
dcbaker Aug 30, 2024
b6425df
options: split UserIntegerOption and UserUmaskOption
dcbaker Aug 30, 2024
58c4327
options: Add a function to compare different option choices
dcbaker Sep 3, 2024
4a69ef0
options: Replace uses of `UserOption[T.Any]` with a Union of UserOpti…
dcbaker Sep 6, 2024
45266a3
coredata: import default_prefix from mesonlib instead of options
dcbaker Sep 4, 2024
b97d4af
coredata: Add missing type annotations
dcbaker Sep 4, 2024
0c0f880
coredata: use a TypeAlias for option values union
dcbaker Sep 4, 2024
e8e2877
coredata: get completely typesafe
dcbaker Sep 4, 2024
438b5a7
environment: fix missing argument and return type annotations
dcbaker Oct 1, 2024
06db047
environment: fix minor typing issues
dcbaker Oct 1, 2024
4fe5d75
machinefile: ensure that arrays are single deep arrays for strings
dcbaker Oct 1, 2024
15de453
environment: make fully type safe
dcbaker Oct 1, 2024
c973ab4
modules/cmake: fix mypy issues
dcbaker Oct 1, 2024
bd136fd
fixup! environment: fix minor typing issues
dcbaker Oct 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mesonbuild/cmake/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def __init__(self, environment: 'Environment', version: str, for_machine: Machin
self.cmakebin = None
return

self.prefix_paths = self.environment.coredata.optstore.get_value(OptionKey('cmake_prefix_path', machine=self.for_machine))
pp = self.environment.coredata.optstore.get_value(OptionKey('cmake_prefix_path', machine=self.for_machine))
assert isinstance(pp, list), 'for mypy'
self.prefix_paths = pp
if self.prefix_paths:
self.extra_cmake_args += ['-DCMAKE_PREFIX_PATH={}'.format(';'.join(self.prefix_paths))]

Expand Down
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
82 changes: 38 additions & 44 deletions mesonbuild/compilers/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,18 @@ def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_
def get_options(self) -> 'MutableKeyedOptionDictType':
opts = super().get_options()
if self.info.is_windows() or self.info.is_cygwin():
self.update_options(
opts,
self.create_option(options.UserArrayOption,
self.form_compileropt_key('winlibs'),
'Standard Win libraries to link against',
gnu_winlibs),
)
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
gnu_winlibs)
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('-std=' + std)
return args
Expand All @@ -175,11 +174,9 @@ def get_option_link_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
if self.info.is_windows() or self.info.is_cygwin():
# without a typedict mypy can't understand this.
key = self.form_compileropt_key('winlibs')
libs = options.get_value(key).copy()
assert isinstance(libs, list)
for l in libs:
assert isinstance(l, str)
return libs
libs = options.get_value(key)
assert isinstance(libs, list), 'for mypy'
return libs.copy()
return []


Expand Down Expand Up @@ -260,6 +257,7 @@ def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('-std=' + std)
return args
Expand Down Expand Up @@ -307,19 +305,18 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
assert isinstance(std_opt, options.UserStdOption), 'for mypy'
std_opt.set_versions(stds, gnu=True)
if self.info.is_windows() or self.info.is_cygwin():
self.update_options(
opts,
self.create_option(options.UserArrayOption,
key.evolve('c_winlibs'),
'Standard Win libraries to link against',
gnu_winlibs),
)
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
gnu_winlibs)
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('-std=' + std)
return args
Expand All @@ -328,11 +325,9 @@ def get_option_link_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
if self.info.is_windows() or self.info.is_cygwin():
# without a typeddict mypy can't figure this out
key = self.form_compileropt_key('winlibs')
libs: T.List[str] = options.get_value(key).copy()
assert isinstance(libs, list)
for l in libs:
assert isinstance(l, str)
return libs
libs = options.get_value(key)
assert isinstance(libs, list), 'fpr mypy'
return libs.copy()
return []

def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:
Expand Down Expand Up @@ -434,6 +429,7 @@ def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('-std=' + std)
return args
Expand All @@ -449,24 +445,20 @@ class VisualStudioLikeCCompilerMixin(CompilerMixinBase):
"""Shared methods that apply to MSVC-like C compilers."""

def get_options(self) -> MutableKeyedOptionDictType:
return self.update_options(
super().get_options(),
self.create_option(
options.UserArrayOption,
self.form_compileropt_key('winlibs'),
'Windows libs to link against.',
msvc_winlibs,
),
)
opts = super().get_options()
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserStringArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
msvc_winlibs)
return opts

def get_option_link_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
# need a TypeDict to make this work
key = self.form_compileropt_key('winlibs')
libs = options.get_value(key).copy()
assert isinstance(libs, list)
for l in libs:
assert isinstance(l, str)
return libs
libs = options.get_value(key)
assert isinstance(libs, list), 'for mypy'
return libs.copy()


class VisualStudioCCompiler(MSVCCompiler, VisualStudioLikeCCompilerMixin, CCompiler):
Expand Down Expand Up @@ -554,6 +546,7 @@ def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std == 'c89':
mlog.log("ICL doesn't explicitly implement c89, setting the standard to 'none', which is close.", once=True)
elif std != 'none':
Expand Down Expand Up @@ -588,6 +581,7 @@ def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('--' + std)
return args
Expand Down Expand Up @@ -669,6 +663,7 @@ def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('-ansi')
args.append('-std=' + std)
Expand Down Expand Up @@ -753,6 +748,7 @@ def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('--' + std)
return args
Expand Down Expand Up @@ -780,15 +776,14 @@ 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]:
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('-lang')
args.append(std)
Expand All @@ -810,15 +805,14 @@ 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]:
args = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
assert isinstance(std, str), 'for mypy'
if std != 'none':
args.append('-lang ' + std)
return args
Loading
Loading