Skip to content

Commit

Permalink
Merge pull request #8080 from dcbaker/submit/option-key-type
Browse files Browse the repository at this point in the history
Use an object for option keys
  • Loading branch information
jpakkane authored Jan 4, 2021
2 parents 6b515c4 + f14bf8b commit d47a5c8
Show file tree
Hide file tree
Showing 54 changed files with 1,341 additions and 1,082 deletions.
12 changes: 6 additions & 6 deletions mesonbuild/ast/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .visitor import AstVisitor
from .. import compilers, environment, mesonlib, optinterpreter
from .. import coredata as cdata
from ..mesonlib import MachineChoice
from ..mesonlib import MachineChoice, OptionKey
from ..interpreterbase import InvalidArguments, TYPE_nvar
from ..build import BuildTarget, Executable, Jar, SharedLibrary, SharedModule, StaticLibrary
from ..mparser import BaseNode, ArithmeticNode, ArrayNode, ElementaryNode, IdNode, FunctionNode, StringNode
Expand Down Expand Up @@ -65,7 +65,7 @@ def __init__(self,
self.coredata = self.environment.get_coredata()
self.option_file = os.path.join(self.source_root, self.subdir, 'meson_options.txt')
self.backend = backend
self.default_options = {'backend': self.backend}
self.default_options = {OptionKey('backend'): self.backend}
self.project_data = {} # type: T.Dict[str, T.Any]
self.targets = [] # type: T.List[T.Dict[str, T.Any]]
self.dependencies = [] # type: T.List[T.Dict[str, T.Any]]
Expand Down Expand Up @@ -103,11 +103,11 @@ def func_project(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[s
if os.path.exists(self.option_file):
oi = optinterpreter.OptionInterpreter(self.subproject)
oi.process(self.option_file)
self.coredata.merge_user_options(oi.options)
self.coredata.update_project_options(oi.options)

def_opts = self.flatten_args(kwargs.get('default_options', []))
_project_default_options = mesonlib.stringlistify(def_opts)
self.project_default_options = cdata.create_options_dict(_project_default_options)
self.project_default_options = cdata.create_options_dict(_project_default_options, self.subproject)
self.default_options.update(self.project_default_options)
self.coredata.set_default_options(self.default_options, self.subproject, self.environment)

Expand All @@ -125,7 +125,7 @@ def func_project(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[s
self.do_subproject(i)

self.coredata.init_backend_options(self.backend)
options = {k: v for k, v in self.environment.raw_options.items() if k.startswith('backend_')}
options = {k: v for k, v in self.environment.options.items() if k.is_backend()}

self.coredata.set_options(options)
self._add_languages(proj_langs, MachineChoice.HOST)
Expand Down Expand Up @@ -269,7 +269,7 @@ def traverse_nodes(inqueue: T.List[BaseNode]) -> T.List[BaseNode]:
return new_target

def build_library(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
default_library = self.coredata.get_builtin_option('default_library')
default_library = self.coredata.get_option(OptionKey('default_library'))
if default_library == 'shared':
return self.build_target(node, args, kwargs, SharedLibrary)
elif default_library == 'static':
Expand Down
54 changes: 27 additions & 27 deletions mesonbuild/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
from .. import mlog
from ..compilers import languages_using_ldflags
from ..mesonlib import (
File, MachineChoice, MesonException, OrderedSet, OptionOverrideProxy,
classify_unity_sources, unholder
File, MachineChoice, MesonException, OptionType, OrderedSet, OptionOverrideProxy,
classify_unity_sources, unholder, OptionKey
)

if T.TYPE_CHECKING:
from ..arglist import CompilerArgs
from ..compilers import Compiler
from ..interpreter import Interpreter, Test


Expand Down Expand Up @@ -209,24 +211,21 @@ def get_target_filename(self, t, *, warn_multi_output: bool = True):
def get_target_filename_abs(self, target):
return os.path.join(self.environment.get_build_dir(), self.get_target_filename(target))

def get_base_options_for_target(self, target):
def get_base_options_for_target(self, target: build.BuildTarget) -> OptionOverrideProxy:
return OptionOverrideProxy(target.option_overrides_base,
self.environment.coredata.builtins,
self.environment.coredata.base_options)
{k: v for k, v in self.environment.coredata.options.items()
if k.type in {OptionType.BASE, OptionType.BUILTIN}})

def get_compiler_options_for_target(self, target):
comp_reg = self.environment.coredata.compiler_options[target.for_machine]
def get_compiler_options_for_target(self, target: build.BuildTarget) -> OptionOverrideProxy:
comp_reg = {k: v for k, v in self.environment.coredata.options.items() if k.is_compiler()}
comp_override = target.option_overrides_compiler
return {
lang: OptionOverrideProxy(comp_override[lang], comp_reg[lang])
for lang in set(comp_reg.keys()) | set(comp_override.keys())
}
return OptionOverrideProxy(comp_override, comp_reg)

def get_option_for_target(self, option_name, target):
def get_option_for_target(self, option_name: 'OptionKey', target: build.BuildTarget):
if option_name in target.option_overrides_base:
override = target.option_overrides_base[option_name]
return self.environment.coredata.validate_option_value(option_name, override)
return self.environment.coredata.get_builtin_option(option_name, target.subproject)
return self.environment.coredata.get_option(option_name.evolve(subproject=target.subproject))

def get_target_filename_for_linking(self, target):
# On some platforms (msvc for instance), the file that is used for
Expand All @@ -251,7 +250,7 @@ def get_target_filename_for_linking(self, target):

@lru_cache(maxsize=None)
def get_target_dir(self, target):
if self.environment.coredata.get_builtin_option('layout') == 'mirror':
if self.environment.coredata.get_option(OptionKey('layout')) == 'mirror':
dirname = target.get_subdir()
else:
dirname = 'meson-out'
Expand Down Expand Up @@ -300,7 +299,7 @@ def generate_unity_files(self, target, unity_src):
abs_files = []
result = []
compsrcs = classify_unity_sources(target.compilers.values(), unity_src)
unity_size = self.get_option_for_target('unity_size', target)
unity_size = self.get_option_for_target(OptionKey('unity_size'), target)

def init_language_file(suffix, unity_file_number):
unity_src = self.get_unity_source_file(target, suffix, unity_file_number)
Expand Down Expand Up @@ -542,7 +541,7 @@ def rpaths_for_bundled_shared_libraries(self, target, exclude_system=True):
return paths

def determine_rpath_dirs(self, target):
if self.environment.coredata.get_builtin_option('layout') == 'mirror':
if self.environment.coredata.get_option(OptionKey('layout')) == 'mirror':
result = target.get_link_dep_subdirs()
else:
result = OrderedSet()
Expand Down Expand Up @@ -621,7 +620,8 @@ def determine_ext_objs(self, extobj, proj_dir_to_build_root):
if self.is_unity(extobj.target):
compsrcs = classify_unity_sources(extobj.target.compilers.values(), sources)
sources = []
unity_size = self.get_option_for_target('unity_size', extobj.target)
unity_size = self.get_option_for_target(OptionKey('unity_size'), extobj.target)

for comp, srcs in compsrcs.items():
for i in range(len(srcs) // unity_size + 1):
osrc = self.get_unity_source_file(extobj.target,
Expand Down Expand Up @@ -672,13 +672,13 @@ def escape_extra_args(compiler, args):

return extra_args

def generate_basic_compiler_args(self, target, compiler, no_warn_args=False):
def generate_basic_compiler_args(self, target: build.BuildTarget, compiler: 'Compiler', no_warn_args: bool = False) -> 'CompilerArgs':
# Create an empty commands list, and start adding arguments from
# various sources in the order in which they must override each other
# starting from hard-coded defaults followed by build options and so on.
commands = compiler.compiler_args()

copt_proxy = self.get_compiler_options_for_target(target)[compiler.language]
copt_proxy = self.get_compiler_options_for_target(target)
# First, the trivial ones that are impossible to override.
#
# Add -nostdinc/-nostdinc++ if needed; can't be overridden
Expand All @@ -690,20 +690,20 @@ def generate_basic_compiler_args(self, target, compiler, no_warn_args=False):
if no_warn_args:
commands += compiler.get_no_warn_args()
else:
commands += compiler.get_warn_args(self.get_option_for_target('warning_level', target))
commands += compiler.get_warn_args(self.get_option_for_target(OptionKey('warning_level'), target))
# Add -Werror if werror=true is set in the build options set on the
# command-line or default_options inside project(). This only sets the
# action to be done for warnings if/when they are emitted, so it's ok
# to set it after get_no_warn_args() or get_warn_args().
if self.get_option_for_target('werror', target):
if self.get_option_for_target(OptionKey('werror'), target):
commands += compiler.get_werror_args()
# Add compile args for c_* or cpp_* build options set on the
# command-line or default_options inside project().
commands += compiler.get_option_compile_args(copt_proxy)
# Add buildtype args: optimization level, debugging, etc.
commands += compiler.get_buildtype_args(self.get_option_for_target('buildtype', target))
commands += compiler.get_optimization_args(self.get_option_for_target('optimization', target))
commands += compiler.get_debug_args(self.get_option_for_target('debug', target))
commands += compiler.get_buildtype_args(self.get_option_for_target(OptionKey('buildtype'), target))
commands += compiler.get_optimization_args(self.get_option_for_target(OptionKey('optimization'), target))
commands += compiler.get_debug_args(self.get_option_for_target(OptionKey('debug'), target))
# MSVC debug builds have /ZI argument by default and /Zi is added with debug flag
# /ZI needs to be removed in that case to avoid cl's warning to that effect (D9025 : overriding '/ZI' with '/Zi')
if ('/ZI' in commands) and ('/Zi' in commands):
Expand Down Expand Up @@ -1022,7 +1022,7 @@ def get_custom_target_provided_libraries(self, target):
return libs

def is_unity(self, target):
optval = self.get_option_for_target('unity', target)
optval = self.get_option_for_target(OptionKey('unity'), target)
if optval == 'on' or (optval == 'subprojects' and target.subproject != ''):
return True
return False
Expand Down Expand Up @@ -1183,7 +1183,7 @@ def create_install_data(self) -> InstallData:
self.environment.get_build_dir(),
self.environment.get_prefix(),
strip_bin,
self.environment.coredata.get_builtin_option('install_umask'),
self.environment.coredata.get_option(OptionKey('install_umask')),
self.environment.get_build_command() + ['introspect'],
self.environment.coredata.version)
self.generate_depmf_install(d)
Expand Down Expand Up @@ -1228,7 +1228,7 @@ def generate_target_install(self, d):
#
# TODO: Create GNUStrip/AppleStrip/etc. hierarchy for more
# fine-grained stripping of static archives.
should_strip = not isinstance(t, build.StaticLibrary) and self.get_option_for_target('strip', t)
should_strip = not isinstance(t, build.StaticLibrary) and self.get_option_for_target(OptionKey('strip'), t)
# Install primary build output (library/executable/jar, etc)
# Done separately because of strip/aliases/rpath
if outdirs[0] is not False:
Expand Down
Loading

0 comments on commit d47a5c8

Please sign in to comment.