compilers: remove Compiler.create_option

This saves a *tiny* bit of typing, but at the cost of requiring either
the current solution of throwing up our hands and saying "typing is too
hard, better to have bugs!" or an extensive amount of `TypedDict`s,
`overloads`, and a very new version of mypy. Let's get our type safety
back, even if it means writing a little bit more code.
pull/14099/head
Dylan Baker 8 months ago committed by Jussi Pakkanen
parent f0a6ba3809
commit fe9f8de1ab
  1. 40
      mesonbuild/compilers/c.py
  2. 4
      mesonbuild/compilers/compilers.py
  3. 218
      mesonbuild/compilers/cpp.py
  4. 28
      mesonbuild/compilers/cuda.py
  5. 32
      mesonbuild/compilers/cython.py
  6. 18
      mesonbuild/compilers/fortran.py
  7. 18
      mesonbuild/compilers/mixins/emscripten.py
  8. 10
      mesonbuild/compilers/objc.py
  9. 15
      mesonbuild/compilers/objcpp.py
  10. 15
      mesonbuild/compilers/rust.py

@ -123,13 +123,11 @@ class ClangCCompiler(ClangCStds, ClangCompiler, CCompiler):
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 Windows libs to link against',
gnu_winlibs),
)
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
self.make_option_name(key),
'Standard Windows libraries to link against',
gnu_winlibs)
return opts
def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
@ -258,13 +256,11 @@ class GnuCCompiler(GnuCStds, GnuCompiler, CCompiler):
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 Windows libs to link against',
gnu_winlibs),
)
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
self.make_option_name(key),
'Standard Windows libraries to link against',
gnu_winlibs)
return opts
def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
@ -408,15 +404,13 @@ 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'),
'Standard Windows libs to link against',
msvc_winlibs,
),
)
opts = super().get_options()
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
self.make_option_name(key),
'Standard Windows 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

@ -588,8 +588,8 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta):
"""
return []
def create_option(self, option_type: T.Type[UserOptionType], option_key: OptionKey, *args: T.Any, **kwargs: T.Any) -> T.Tuple[OptionKey, UserOptionType]:
return option_key, option_type(f'{self.language}_{option_key.name}', *args, **kwargs)
def make_option_name(self, key: OptionKey) -> str:
return f'{self.language}_{key.name}'
@staticmethod
def update_options(options: MutableKeyedOptionDictType, *args: T.Tuple[OptionKey, UserOptionType]) -> MutableKeyedOptionDictType:

@ -237,30 +237,32 @@ class ClangCPPCompiler(_StdCPPLibMixin, ClangCPPStds, ClangCompiler, CPPCompiler
def get_options(self) -> 'MutableKeyedOptionDictType':
opts = super().get_options()
self.update_options(
opts,
self.create_option(options.UserComboOption,
self.form_compileropt_key('eh'),
'C++ exception handling type',
['none', 'default', 'a', 's', 'sc'],
'default'),
self.create_option(options.UserBooleanOption,
self.form_compileropt_key('rtti'),
'Enable RTTI',
True),
self.create_option(options.UserBooleanOption,
self.form_compileropt_key('debugstl'),
'STL debug mode',
False),
)
key = self.form_compileropt_key('eh')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
self.make_option_name(key),
'Enable RTTI',
True)
key = self.form_compileropt_key('debugstl')
opts[key] = options.UserBooleanOption(
self.make_option_name(key),
'STL debug mode',
False)
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 Windows libs to link against',
gnu_winlibs),
)
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
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]:
@ -391,15 +393,15 @@ class ArmclangCPPCompiler(ArmclangCompiler, CPPCompiler):
def get_options(self) -> 'MutableKeyedOptionDictType':
opts = super().get_options()
key = self.form_compileropt_key('eh')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
key = self.form_compileropt_key('std')
self.update_options(
opts,
self.create_option(options.UserComboOption,
key.evolve('eh'),
'C++ exception handling type',
['none', 'default', 'a', 's', 'sc'],
'default'),
)
std_opt = opts[key]
assert isinstance(std_opt, options.UserStdOption), 'for mypy'
std_opt.set_versions(['c++98', 'c++03', 'c++11', 'c++14', 'c++17'], gnu=True)
@ -440,32 +442,34 @@ class GnuCPPCompiler(_StdCPPLibMixin, GnuCPPStds, GnuCompiler, CPPCompiler):
self.supported_warn_args(gnu_cpp_warning_args))}
def get_options(self) -> 'MutableKeyedOptionDictType':
key = self.form_compileropt_key('std')
opts = super().get_options()
self.update_options(
opts,
self.create_option(options.UserComboOption,
self.form_compileropt_key('eh'),
'C++ exception handling type',
['none', 'default', 'a', 's', 'sc'],
'default'),
self.create_option(options.UserBooleanOption,
self.form_compileropt_key('rtti'),
'Enable RTTI',
True),
self.create_option(options.UserBooleanOption,
self.form_compileropt_key('debugstl'),
'STL debug mode',
False),
)
key = self.form_compileropt_key('eh')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
self.make_option_name(key),
'Enable RTTI',
True)
key = self.form_compileropt_key('debugstl')
opts[key] = options.UserBooleanOption(
self.make_option_name(key),
'STL debug mode',
False)
if self.info.is_windows() or self.info.is_cygwin():
self.update_options(
opts,
self.create_option(options.UserArrayOption,
key.evolve('cpp_winlibs'),
'Standard Windows libs to link against',
gnu_winlibs),
)
key = key.evolve(name='cpp_winlibs')
opts[key] = options.UserArrayOption(
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]:
@ -569,6 +573,19 @@ class ElbrusCPPCompiler(ElbrusCompiler, CPPCompiler):
def get_options(self) -> 'MutableKeyedOptionDictType':
opts = super().get_options()
key = self.form_compileropt_key('eh')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
key = self.form_compileropt_key('debugstl')
opts[key] = options.UserBooleanOption(
self.make_option_name(key),
'STL debug mode',
False)
cpp_stds = ['c++98']
if version_compare(self.version, '>=1.20.00'):
cpp_stds += ['c++03', 'c++0x', 'c++11']
@ -586,18 +603,6 @@ class ElbrusCPPCompiler(ElbrusCompiler, CPPCompiler):
cpp_stds += ['c++20']
key = self.form_compileropt_key('std')
self.update_options(
opts,
self.create_option(options.UserComboOption,
self.form_compileropt_key('eh'),
'C++ exception handling type',
['none', 'default', 'a', 's', 'sc'],
'default'),
self.create_option(options.UserBooleanOption,
self.form_compileropt_key('debugstl'),
'STL debug mode',
False),
)
std_opt = opts[key]
assert isinstance(std_opt, options.UserStdOption), 'for mypy'
std_opt.set_versions(cpp_stds, gnu=True)
@ -650,6 +655,26 @@ class IntelCPPCompiler(IntelGnuLikeCompiler, CPPCompiler):
def get_options(self) -> 'MutableKeyedOptionDictType':
opts = super().get_options()
key = self.form_compileropt_key('eh')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
self.make_option_name(key),
'Enable RTTI',
True)
key = self.form_compileropt_key('debugstl')
opts[key] = options.UserBooleanOption(
self.make_option_name(key),
'STL debug mode',
False)
# Every Unix compiler under the sun seems to accept -std=c++03,
# with the exception of ICC. Instead of preventing the user from
# globally requesting C++03, we transparently remap it to C++98
@ -666,24 +691,7 @@ class IntelCPPCompiler(IntelGnuLikeCompiler, CPPCompiler):
c_stds += ['c++2a']
g_stds += ['gnu++2a']
key = self.form_compileropt_key('std')
self.update_options(
opts,
self.create_option(options.UserComboOption,
self.form_compileropt_key('eh'),
'C++ exception handling type',
['none', 'default', 'a', 's', 'sc'],
'default'),
self.create_option(options.UserBooleanOption,
self.form_compileropt_key('rtti'),
'Enable RTTI',
True),
self.create_option(options.UserBooleanOption,
self.form_compileropt_key('debugstl'),
'STL debug mode',
False),
)
std_opt = opts[key]
std_opt = opts[self.form_compileropt_key('std')]
assert isinstance(std_opt, options.UserStdOption), 'for mypy'
std_opt.set_versions(c_stds + g_stds)
return opts
@ -739,24 +747,28 @@ class VisualStudioLikeCPPCompilerMixin(CompilerMixinBase):
return T.cast('T.List[str]', options.get_value(key)[:])
def _get_options_impl(self, opts: 'MutableKeyedOptionDictType', cpp_stds: T.List[str]) -> 'MutableKeyedOptionDictType':
key = self.form_compileropt_key('std')
self.update_options(
opts,
self.create_option(options.UserComboOption,
self.form_compileropt_key('eh'),
'C++ exception handling type',
['none', 'default', 'a', 's', 'sc'],
'default'),
self.create_option(options.UserBooleanOption,
self.form_compileropt_key('rtti'),
'Enable RTTI',
True),
self.create_option(options.UserArrayOption,
self.form_compileropt_key('winlibs'),
'Standard Windows libs to link against',
msvc_winlibs),
)
std_opt = opts[key]
opts = super().get_options()
key = self.form_compileropt_key('eh')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ exception handling type.',
['none', 'default', 'a', 's', 'sc'],
'default')
key = self.form_compileropt_key('rtti')
opts[key] = options.UserBooleanOption(
self.make_option_name(key),
'Enable RTTI',
True)
key = self.form_compileropt_key('winlibs')
opts[key] = options.UserArrayOption(
self.make_option_name(key),
'Standard Win libraries to link against',
msvc_winlibs)
std_opt = opts[self.form_compileropt_key('std')]
assert isinstance(std_opt, options.UserStdOption), 'for mypy'
std_opt.set_versions(cpp_stds)
return opts

@ -646,18 +646,22 @@ class CudaCompiler(Compiler):
if version_compare(self.version, self._CPP20_VERSION):
cpp_stds += ['c++20']
return self.update_options(
super().get_options(),
self.create_option(options.UserComboOption,
self.form_compileropt_key('std'),
'C++ language standard to use with CUDA',
cpp_stds,
'none'),
self.create_option(options.UserStringOption,
self.form_compileropt_key('ccbindir'),
'CUDA non-default toolchain directory to use (-ccbin)',
''),
)
opts = super().get_options()
key = self.form_compileropt_key('std')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'C++ language standard to use with CUDA',
cpp_stds,
'none')
key = self.form_compileropt_key('ccbindir')
opts[key] = options.UserStringOption(
self.make_option_name(key),
'CUDA non-default toolchain directory to use (-ccbin)',
'')
return opts
def _to_host_compiler_options(self, master_options: 'KeyedOptionDictType') -> 'KeyedOptionDictType':
"""

@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2021 Intel Corporation
# Copyright © 2021-2024 Intel Corporation
from __future__ import annotations
"""Abstraction for Cython language compilers."""
@ -67,19 +67,23 @@ class CythonCompiler(Compiler):
return new
def get_options(self) -> 'MutableKeyedOptionDictType':
return self.update_options(
super().get_options(),
self.create_option(options.UserComboOption,
self.form_compileropt_key('version'),
'Python version to target',
['2', '3'],
'3'),
self.create_option(options.UserComboOption,
self.form_compileropt_key('language'),
'Output C or C++ files',
['c', 'cpp'],
'c'),
)
opts = super().get_options()
key = self.form_compileropt_key('version')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Python version to target',
['2', '3'],
'3')
key = self.form_compileropt_key('language')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Output C or C++ files',
['c', 'cpp'],
'c')
return opts
def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
args: T.List[str] = []

@ -114,14 +114,16 @@ class FortranCompiler(CLikeCompiler, Compiler):
return self._has_multi_link_arguments(args, env, 'stop; end program')
def get_options(self) -> 'MutableKeyedOptionDictType':
return self.update_options(
super().get_options(),
self.create_option(options.UserComboOption,
self.form_compileropt_key('std'),
'Fortran language standard to use',
['none'],
'none'),
)
opts = super().get_options()
key = self.form_compileropt_key('std')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Fortran language standard to use',
['none'],
'none')
return opts
def _compile_int(self, expression: str, prefix: str, env: 'Environment',
extra_args: T.Union[None, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]],

@ -57,15 +57,15 @@ class EmscriptenMixin(Compiler):
return args
def get_options(self) -> coredata.MutableKeyedOptionDictType:
return self.update_options(
super().get_options(),
self.create_option(
options.UserIntegerOption,
OptionKey(f'{self.language}_thread_count', machine=self.for_machine),
'Number of threads to use in web assembly, set to 0 to disable',
(0, None, 4), # Default was picked at random
),
)
opts = super().get_options()
key = OptionKey(f'{self.language}_thread_count', machine=self.for_machine)
opts[key] = options.UserIntegerOption(
self.make_option_name(key),
'Number of threads to use in web assembly, set to 0 to disable',
(0, None, 4)) # Default was picked at random
return opts
@classmethod
def native_args_to_unix(cls, args: T.List[str]) -> T.List[str]:

@ -99,6 +99,16 @@ class ClangObjCCompiler(ClangCStds, ClangCompiler, ObjCCompiler):
'3': default_warn_args + ['-Wextra', '-Wpedantic'],
'everything': ['-Weverything']}
def form_compileropt_key(self, basename: str) -> OptionKey:
if basename == 'std':
return OptionKey('c_std', machine=self.for_machine)
return super().form_compileropt_key(basename)
def make_option_name(self, key: OptionKey) -> str:
if key.name == 'std':
return 'c_std'
return super().make_option_name(key)
def get_option_compile_args(self, options: 'coredata.KeyedOptionDictType') -> T.List[str]:
args = []
std = options.get_value(self.form_compileropt_key('std'))

@ -35,6 +35,16 @@ class ObjCPPCompiler(CLikeCompiler, Compiler):
linker=linker)
CLikeCompiler.__init__(self)
def form_compileropt_key(self, basename: str) -> OptionKey:
if basename == 'std':
return OptionKey('cpp_std', machine=self.for_machine)
return super().form_compileropt_key(basename)
def make_option_name(self, key: OptionKey) -> str:
if key.name == 'std':
return 'cpp_std'
return super().make_option_name(key)
@staticmethod
def get_display_language() -> str:
return 'Objective-C++'
@ -43,11 +53,6 @@ class ObjCPPCompiler(CLikeCompiler, Compiler):
code = '#import<stdio.h>\nclass MyClass;int main(void) { return 0; }\n'
return self._sanity_check_impl(work_dir, environment, 'sanitycheckobjcpp.mm', code)
def form_compileropt_key(self, basename: str) -> OptionKey:
if basename == 'std':
return OptionKey(f'cpp_{basename}', machine=self.for_machine)
return super().form_compileropt_key(basename)
def get_options(self) -> coredata.MutableKeyedOptionDictType:
opts = super().get_options()
key = self.form_compileropt_key('std')

@ -234,11 +234,16 @@ class RustCompiler(Compiler):
# use_linker_args method instead.
def get_options(self) -> MutableKeyedOptionDictType:
return dict((self.create_option(options.UserComboOption,
self.form_compileropt_key('std'),
'Rust edition to use',
['none', '2015', '2018', '2021', '2024'],
'none'),))
opts = super().get_options()
key = self.form_compileropt_key('std')
opts[key] = options.UserComboOption(
self.make_option_name(key),
'Rust edition to use',
['none', '2015', '2018', '2021', '2024'],
'none')
return opts
def get_dependency_compile_args(self, dep: 'Dependency') -> T.List[str]:
# Rust doesn't have dependency compile arguments so simply return

Loading…
Cancel
Save