Skip to content

Commit

Permalink
Remove language (AKA compiler) type from OptionKey.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpakkane committed Jul 17, 2024
1 parent de8e3d6 commit 61c742f
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 77 deletions.
4 changes: 2 additions & 2 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ def should_use_dyndeps_for_target(self, target: 'build.BuildTarget') -> bool:
cpp = target.compilers['cpp']
if cpp.get_id() != 'msvc':
return False
cppversion = target.get_option(OptionKey('std', machine=target.for_machine, lang='cpp'))
cppversion = target.get_option(OptionKey('cpp_std', machine=target.for_machine))
if cppversion not in ('latest', 'c++latest', 'vc++latest'):
return False
if not mesonlib.current_vs_supports_modules():
Expand Down Expand Up @@ -1783,7 +1783,7 @@ def generate_cython_transpile(self, target: build.BuildTarget) -> \
args += self.build.get_project_args(cython, target.subproject, target.for_machine)
args += target.get_extra_args('cython')

ext = target.get_option(OptionKey('language', machine=target.for_machine, lang='cython'))
ext = target.get_option(OptionKey('cython_language', machine=target.for_machine))

pyx_sources = [] # Keep track of sources we're adding to build

Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,8 @@ def get_args_defines_and_inc_dirs(self, target, compiler, generated_files_includ
file_args[l] += args
# Compile args added from the env or cross file: CFLAGS/CXXFLAGS, etc. We want these
# to override all the defaults, but not the per-target compile args.
for l in file_args.keys():
file_args[l] += target.get_option(OptionKey('args', machine=target.for_machine, lang=l))
for lang in file_args.keys():
file_args[lang] += target.get_option(OptionKey(f'{lang}_args', machine=target.for_machine))
for args in file_args.values():
# This is where Visual Studio will insert target_args, target_defines,
# etc, which are added later from external deps (see below).
Expand Down Expand Up @@ -1340,7 +1340,7 @@ def add_non_makefile_vcxproj_elements(
# Exception handling has to be set in the xml in addition to the "AdditionalOptions" because otherwise
# cl will give warning D9025: overriding '/Ehs' with cpp_eh value
if 'cpp' in target.compilers:
eh = target.get_option(OptionKey('eh', machine=target.for_machine, lang='cpp'))
eh = target.get_option(OptionKey('cpp_eh', machine=target.for_machine))
if eh == 'a':
ET.SubElement(clconf, 'ExceptionHandling').text = 'Async'
elif eh == 's':
Expand Down
14 changes: 12 additions & 2 deletions mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,20 @@ def process_kwargs_base(self, kwargs: T.Dict[str, T.Any]) -> None:

self.set_option_overrides(self.parse_overrides(kwargs))

def is_compiler_option_hack(self, key):
# FIXME this method must be deleted when OptionsView goes away.
# At that point the build target only stores the original string.
# The decision on how to use those pieces of data is done elsewhere.
from .compilers import all_languages
if '_' not in key.name:
return False
prefix = key.name.split('_')[0]
return prefix in all_languages

def set_option_overrides(self, option_overrides: T.Dict[OptionKey, str]) -> None:
self.options.overrides = {}
for k, v in option_overrides.items():
if k.lang:
if self.is_compiler_option_hack(k):
self.options.overrides[k.evolve(machine=self.for_machine)] = v
else:
self.options.overrides[k] = v
Expand Down Expand Up @@ -1002,7 +1012,7 @@ def process_compilers(self) -> T.List[str]:
if 'vala' in self.compilers and 'c' not in self.compilers:
self.compilers['c'] = self.all_compilers['c']
if 'cython' in self.compilers:
key = OptionKey('language', machine=self.for_machine, lang='cython')
key = OptionKey('cython_language', machine=self.for_machine)
value = self.get_option(key)

try:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/cmake/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ 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('std', machine=MachineChoice.BUILD, lang=lang)).choices
res = self.env.coredata.optstore.get_value_object(OptionKey(f'{lang}_std', machine=MachineChoice.BUILD)).choices
except KeyError:
return []

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.update_options(
opts,
self.create_option(options.UserArrayOption,
key.evolve('winlibs'),
key.evolve('c_winlibs'),
'Standard Win libraries to link against',
gnu_winlibs),
)
Expand Down
8 changes: 4 additions & 4 deletions mesonbuild/compilers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,17 +1355,17 @@ def get_preprocessor(self) -> Compiler:
raise EnvironmentException(f'{self.get_id()} does not support preprocessor')

def form_compileropt_key(self, basename: str) -> OptionKey:
return OptionKey(basename, machine=self.for_machine, lang=self.language)
return OptionKey(f'{self.language}_{basename}', machine=self.for_machine)

def get_global_options(lang: str,
comp: T.Type[Compiler],
for_machine: MachineChoice,
env: 'Environment') -> 'dict[OptionKey, options.UserOption[Any]]':
"""Retrieve options that apply to all compilers for a given language."""
description = f'Extra arguments passed to the {lang}'
argkey = OptionKey('args', lang=lang, machine=for_machine)
largkey = argkey.evolve('link_args')
envkey = argkey.evolve('env_args')
argkey = OptionKey(f'{lang}_args', machine=for_machine)
largkey = argkey.evolve(f'{lang}_link_args')
envkey = argkey.evolve(f'{lang}_env_args')

comp_key = argkey if argkey in env.options else envkey

Expand Down
16 changes: 10 additions & 6 deletions mesonbuild/compilers/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,25 +475,29 @@ def get_options(self) -> 'MutableKeyedOptionDictType':
self.update_options(
opts,
self.create_option(options.UserArrayOption,
key.evolve('winlibs'),
key.evolve('cpp_winlibs'),
'Standard Win libraries to link against',
gnu_winlibs),
)
return opts

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
args: T.List[str] = []
key = self.form_compileropt_key('std')
std = options.get_value(key)
stdkey = self.form_compileropt_key('std')
ehkey = self.form_compileropt_key('eh')
rttikey = self.form_compileropt_key('rtti')
debugstlkey = self.form_compileropt_key('debugstl')

std = options.get_value(stdkey)
if std != 'none':
args.append(self._find_best_cpp_std(std))

non_msvc_eh_options(options.get_value(key.evolve('eh')), args)
non_msvc_eh_options(options.get_value(ehkey), args)

if not options.get_value(key.evolve('rtti')):
if not options.get_value(rttikey):
args.append('-fno-rtti')

if options.get_value(key.evolve('debugstl')):
if options.get_value(debugstlkey):
args.append('-D_GLIBCXX_DEBUG=1')
return args

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ def _to_host_compiler_options(self, master_options: 'KeyedOptionDictType') -> 'K
# We must strip the -std option from the host compiler option set, as NVCC has
# its own -std flag that may not agree with the host compiler's.
host_options = {key: master_options.get(key, opt) for key, opt in self.host_compiler.get_options().items()}
std_key = OptionKey('std', machine=self.for_machine, lang=self.host_compiler.language)
std_key = OptionKey(f'{self.host_compiler.language}_std', machine=self.for_machine)
overrides = {std_key: 'none'}
# To shut up mypy.
return coredata.OptionsView(host_options, overrides=overrides)
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/mixins/elbrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def get_pch_suffix(self) -> str:

def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
args: T.List[str] = []
std = options.get_value(OptionKey('std', lang=self.language, machine=self.for_machine))
std = options.get_value(OptionKey(f'{self.language}_std', machine=self.for_machine))
if std != 'none':
args.append('-std=' + std)
return args
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/mixins/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _get_compile_output(self, dirname: str, mode: CompileCheckMode) -> str:

def thread_link_flags(self, env: 'Environment') -> T.List[str]:
args = ['-pthread']
count: int = env.coredata.optstore.get_value(OptionKey('thread_count', lang=self.language, machine=self.for_machine))
count: int = env.coredata.optstore.get_value(OptionKey(f'{self.language}_thread_count', machine=self.for_machine))
if count:
args.append(f'-sPTHREAD_POOL_SIZE={count}')
return args
Expand All @@ -61,7 +61,7 @@ def get_options(self) -> coredata.MutableKeyedOptionDictType:
super().get_options(),
self.create_option(
options.UserIntegerOption,
OptionKey('thread_count', machine=self.for_machine, lang=self.language),
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
),
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/objc.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ def get_options(self) -> 'coredata.MutableKeyedOptionDictType':
return self.update_options(
super().get_options(),
self.create_option(options.UserComboOption,
OptionKey('std', machine=self.for_machine, lang='c'),
OptionKey('c_std', machine=self.for_machine),
'C language standard to use',
['none', 'c89', 'c99', 'c11', 'c17', 'gnu89', 'gnu99', 'gnu11', 'gnu17'],
'none'),
)

def get_option_compile_args(self, options: 'coredata.KeyedOptionDictType') -> T.List[str]:
args = []
std = options.get_value(OptionKey('std', machine=self.for_machine, lang='c'))
std = options.get_value(OptionKey('c_std', machine=self.for_machine))
if std != 'none':
args.append('-std=' + std)
return args
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/objcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def get_options(self) -> coredata.MutableKeyedOptionDictType:
return self.update_options(
super().get_options(),
self.create_option(options.UserComboOption,
OptionKey('std', machine=self.for_machine, lang='cpp'),
OptionKey('cpp_std', machine=self.for_machine),
'C++ language standard to use',
['none', 'c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++2b',
'gnu++98', 'gnu++11', 'gnu++14', 'gnu++17', 'gnu++20',
Expand All @@ -92,7 +92,7 @@ def get_options(self) -> coredata.MutableKeyedOptionDictType:

def get_option_compile_args(self, options: 'coredata.KeyedOptionDictType') -> T.List[str]:
args = []
std = options.get_value(OptionKey('std', machine=self.for_machine, lang='cpp'))
std = options.get_value(OptionKey('cpp_std', machine=self.for_machine))
if std != 'none':
args.append('-std=' + std)
return args
Expand Down
14 changes: 7 additions & 7 deletions mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,20 +568,19 @@ def _set_others_from_buildtype(self, value: str) -> bool:

return dirty

@staticmethod
def is_per_machine_option(optname: OptionKey) -> bool:
def is_per_machine_option(self, optname: OptionKey) -> bool:
if optname.as_host() in options.BUILTIN_OPTIONS_PER_MACHINE:
return True
return optname.lang is not None
return self.optstore.is_compiler_option(optname)

def get_external_args(self, for_machine: MachineChoice, lang: str) -> T.List[str]:
# mypy cannot analyze type of OptionKey
key = OptionKey('args', machine=for_machine, lang=lang)
key = OptionKey(f'{lang}_args', machine=for_machine)
return T.cast('T.List[str]', self.optstore.get_value(key))

def get_external_link_args(self, for_machine: MachineChoice, lang: str) -> T.List[str]:
# mypy cannot analyze type of OptionKey
key = OptionKey('link_args', machine=for_machine, lang=lang)
key = OptionKey(f'{lang}_link_args', machine=for_machine)
return T.cast('T.List[str]', self.optstore.get_value(key))

def update_project_options(self, project_options: 'MutableKeyedOptionDictType', subproject: SubProject) -> None:
Expand Down Expand Up @@ -732,7 +731,8 @@ def add_lang_args(self, lang: str, comp: T.Type['Compiler'],
# These options are all new at this point, because the compiler is
# responsible for adding its own options, thus calling
# `self.optstore.update()`` is perfectly safe.
self.optstore.update(compilers.get_global_options(lang, comp, for_machine, env))
for gopt_key, gopt_valobj in compilers.get_global_options(lang, comp, for_machine, env).items():
self.optstore.add_compiler_option(lang, gopt_key, gopt_valobj)

def process_compiler_options(self, lang: str, comp: Compiler, env: Environment, subproject: str) -> None:
from . import compilers
Expand Down Expand Up @@ -924,7 +924,7 @@ def __getitem__(self, key: OptionKey) -> options.UserOption:
# to hold overrides.
if isinstance(self.original_options, options.OptionStore):
if key2 not in self.original_options:
raise KeyError
raise KeyError(f'{key} {key2}')
opt = self.original_options.get_value_object(key2)
else:
opt = self.original_options[key2]
Expand Down
9 changes: 4 additions & 5 deletions mesonbuild/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,14 +743,12 @@ def _set_default_options_from_env(self) -> None:
# if it changes on future invocations.
if self.first_invocation:
if keyname == 'ldflags':
key = OptionKey('link_args', machine=for_machine, lang='c') # needs a language to initialize properly
for lang in compilers.compilers.LANGUAGES_USING_LDFLAGS:
key = key.evolve(lang=lang)
key = OptionKey(name=f'{lang}_link_args', machine=for_machine)
env_opts[key].extend(p_list)
elif keyname == 'cppflags':
key = OptionKey('env_args', machine=for_machine, lang='c')
for lang in compilers.compilers.LANGUAGES_USING_CPPFLAGS:
key = key.evolve(lang=lang)
key = OptionKey(f'{lang}_env_args', machine=for_machine)
env_opts[key].extend(p_list)
else:
key = OptionKey.from_string(keyname).evolve(machine=for_machine)
Expand All @@ -770,7 +768,8 @@ def _set_default_options_from_env(self) -> None:
# We still use the original key as the base here, as
# we want to inherit the machine and the compiler
# language
key = key.evolve('env_args')
lang = key.name.split('_', 1)[0]
key = key.evolve(f'{lang}_env_args')
env_opts[key].extend(p_list)

# Only store options that are not already in self.options,
Expand Down
5 changes: 2 additions & 3 deletions mesonbuild/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,14 @@ def test(self, args: T.Tuple[str, T.Union[build.Executable, build.Jar, 'External

def get_option(self, name: str, subproject: str = '',
machine: MachineChoice = MachineChoice.HOST,
lang: T.Optional[str] = None,
module: T.Optional[str] = None) -> T.Union[T.List[str], str, int, bool]:
return self.environment.coredata.get_option(OptionKey(name, subproject, machine, lang, module))
return self.environment.coredata.get_option(OptionKey(name, subproject, machine, module))

def is_user_defined_option(self, name: str, subproject: str = '',
machine: MachineChoice = MachineChoice.HOST,
lang: T.Optional[str] = None,
module: T.Optional[str] = None) -> bool:
key = OptionKey(name, subproject, machine, lang, module)
key = OptionKey(name, subproject, machine, module)
return key in self._interpreter.user_defined_options.cmd_line_options

def process_include_dirs(self, dirs: T.Iterable[T.Union[str, IncludeDirs]]) -> T.Iterable[IncludeDirs]:
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/modules/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
raise InterpreterException(f'Unknown file type extension for: {name}')

# We only want include directories and defines, other things may not be valid
cargs = state.get_option('args', state.subproject, lang=language)
cargs = state.get_option(f'{language}_args', state.subproject)
assert isinstance(cargs, list), 'for mypy'
for a in itertools.chain(state.global_args.get(language, []), state.project_args.get(language, []), cargs):
if a.startswith(('-I', '/I', '-D', '/D', '-U', '/U')):
Expand All @@ -280,7 +280,7 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu

# Add the C++ standard to the clang arguments. Attempt to translate VS
# extension versions into the nearest standard version
std = state.get_option('std', lang=language)
std = state.get_option(f'{language}_std')
assert isinstance(std, str), 'for mypy'
if std.startswith('vc++'):
if std.endswith('latest'):
Expand Down
Loading

0 comments on commit 61c742f

Please sign in to comment.