more f-strings everywhere

pyupgrade didn't catch many .format() methods which were too complex
(e.g. multiline or applied to templates rather than string literals)
pull/8844/head
Eli Schwartz 4 years ago
parent 40e8a67a83
commit 2c71b63e77
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 22
      mesonbuild/backend/backends.py
  2. 49
      mesonbuild/backend/ninjabackend.py
  3. 54
      mesonbuild/build.py
  4. 130
      mesonbuild/compilers/mixins/clike.py

@ -235,13 +235,13 @@ class Backend:
self.environment.get_source_dir()) self.environment.get_source_dir())
def generate(self) -> None: def generate(self) -> None:
raise RuntimeError('generate is not implemented in {}'.format(type(self).__name__)) raise RuntimeError(f'generate is not implemented in {type(self).__name__}')
def get_target_filename(self, t, *, warn_multi_output: bool = True): def get_target_filename(self, t, *, warn_multi_output: bool = True):
if isinstance(t, build.CustomTarget): if isinstance(t, build.CustomTarget):
if warn_multi_output and len(t.get_outputs()) != 1: if warn_multi_output and len(t.get_outputs()) != 1:
mlog.warning('custom_target {!r} has more than one output! ' mlog.warning(f'custom_target {t.name!r} has more than one output! '
'Using the first one.'.format(t.name)) 'Using the first one.')
filename = t.get_outputs()[0] filename = t.get_outputs()[0]
elif isinstance(t, build.CustomTargetIndex): elif isinstance(t, build.CustomTargetIndex):
filename = t.get_outputs()[0] filename = t.get_outputs()[0]
@ -753,7 +753,7 @@ class Backend:
pch_file = os.path.join(self.build_dir, pch_rel_to_build) pch_file = os.path.join(self.build_dir, pch_rel_to_build)
os.makedirs(os.path.dirname(pch_file), exist_ok=True) os.makedirs(os.path.dirname(pch_file), exist_ok=True)
content = '#include "{}"'.format(os.path.basename(pch_header)) content = f'#include "{os.path.basename(pch_header)}"'
pch_file_tmp = pch_file + '.tmp' pch_file_tmp = pch_file + '.tmp'
with open(pch_file_tmp, 'w') as f: with open(pch_file_tmp, 'w') as f:
f.write(content) f.write(content)
@ -1245,8 +1245,8 @@ class Backend:
i = i.replace('@CURRENT_SOURCE_DIR@', os.path.join(source_root, target.subdir)) i = i.replace('@CURRENT_SOURCE_DIR@', os.path.join(source_root, target.subdir))
if '@DEPFILE@' in i: if '@DEPFILE@' in i:
if target.depfile is None: if target.depfile is None:
msg = 'Custom target {!r} has @DEPFILE@ but no depfile ' \ msg = f'Custom target {target.name!r} has @DEPFILE@ but no depfile ' \
'keyword argument.'.format(target.name) 'keyword argument.'
raise MesonException(msg) raise MesonException(msg)
dfilename = os.path.join(outdir, target.depfile) dfilename = os.path.join(outdir, target.depfile)
i = i.replace('@DEPFILE@', dfilename) i = i.replace('@DEPFILE@', dfilename)
@ -1257,8 +1257,8 @@ class Backend:
pdir = self.get_target_private_dir(target) pdir = self.get_target_private_dir(target)
i = i.replace('@PRIVATE_DIR@', pdir) i = i.replace('@PRIVATE_DIR@', pdir)
else: else:
err_msg = 'Argument {0} is of unknown type {1}' err_msg = f'Argument {i} is of unknown type {type(i)}'
raise RuntimeError(err_msg.format(str(i), str(type(i)))) raise RuntimeError(err_msg)
cmd.append(i) cmd.append(i)
# Substitute the rest of the template strings # Substitute the rest of the template strings
values = mesonlib.get_filenames_templates_dict(inputs, outputs) values = mesonlib.get_filenames_templates_dict(inputs, outputs)
@ -1449,8 +1449,8 @@ class Backend:
outdir = os.path.join(incroot, h.get_install_subdir()) outdir = os.path.join(incroot, h.get_install_subdir())
for f in h.get_sources(): for f in h.get_sources():
if not isinstance(f, File): if not isinstance(f, File):
msg = 'Invalid header type {!r} can\'t be installed' msg = f'Invalid header type {f!r} can\'t be installed'
raise MesonException(msg.format(f)) raise MesonException(msg)
abspath = f.absolute_path(srcdir, builddir) abspath = f.absolute_path(srcdir, builddir)
i = InstallDataBase(abspath, outdir, h.get_custom_install_mode(), h.subproject) i = InstallDataBase(abspath, outdir, h.get_custom_install_mode(), h.subproject)
d.headers.append(i) d.headers.append(i)
@ -1546,7 +1546,7 @@ class Backend:
elif isinstance(j, (build.BuildTarget, build.CustomTarget)): elif isinstance(j, (build.BuildTarget, build.CustomTarget)):
compiler += j.get_outputs() compiler += j.get_outputs()
else: else:
raise RuntimeError('Type "{}" is not supported in get_introspection_data. This is a bug'.format(type(j).__name__)) raise RuntimeError(f'Type "{type(j).__name__}" is not supported in get_introspection_data. This is a bug')
return [{ return [{
'language': 'unknown', 'language': 'unknown',

@ -133,11 +133,11 @@ def ninja_quote(text: str, is_build_line=False) -> str:
if not quote_re.search(text): if not quote_re.search(text):
return text return text
if '\n' in text: if '\n' in text:
errmsg = '''Ninja does not support newlines in rules. The content was: errmsg = f'''Ninja does not support newlines in rules. The content was:
{} {text}
Please report this error with a test case to the Meson bug tracker.'''.format(text) Please report this error with a test case to the Meson bug tracker.'''
raise MesonException(errmsg) raise MesonException(errmsg)
return quote_re.sub(r'$\g<0>', text) return quote_re.sub(r'$\g<0>', text)
@ -437,8 +437,8 @@ class NinjaBackend(backends.Backend):
# 'benchmark', etc, and also for RunTargets. # 'benchmark', etc, and also for RunTargets.
# https://github.com/mesonbuild/meson/issues/1644 # https://github.com/mesonbuild/meson/issues/1644
if not to_target.startswith('meson-'): if not to_target.startswith('meson-'):
m = 'Invalid usage of create_target_alias with {!r}' m = f'Invalid usage of create_target_alias with {to_target!r}'
raise AssertionError(m.format(to_target)) raise AssertionError(m)
from_target = to_target[len('meson-'):] from_target = to_target[len('meson-'):]
elem = NinjaBuildElement(self.all_outputs, from_target, 'phony', to_target) elem = NinjaBuildElement(self.all_outputs, from_target, 'phony', to_target)
self.add_build(elem) self.add_build(elem)
@ -530,10 +530,10 @@ int dummy;
num_pools = self.environment.coredata.options[OptionKey('backend_max_links')].value num_pools = self.environment.coredata.options[OptionKey('backend_max_links')].value
if num_pools > 0: if num_pools > 0:
outfile.write('''pool link_pool outfile.write(f'''pool link_pool
depth = {} depth = {num_pools}
'''.format(num_pools)) ''')
with self.detect_vs_dep_prefix(tempfilename) as outfile: with self.detect_vs_dep_prefix(tempfilename) as outfile:
self.generate_rules() self.generate_rules()
@ -776,9 +776,8 @@ int dummy;
if langs_cant: if langs_cant:
langs_are = langs = ', '.join(langs_cant).upper() langs_are = langs = ', '.join(langs_cant).upper()
langs_are += ' are' if len(langs_cant) > 1 else ' is' langs_are += ' are' if len(langs_cant) > 1 else ' is'
msg = '{} not supported in Unity builds yet, so {} ' \ msg = f'{langs_are} not supported in Unity builds yet, so {langs} ' \
'sources in the {!r} target will be compiled normally' \ f'sources in the {target.name!r} target will be compiled normally'
''.format(langs_are, langs, target.name)
mlog.log(mlog.red('FIXME'), msg) mlog.log(mlog.red('FIXME'), msg)
# Get a list of all generated headers that will be needed while building # Get a list of all generated headers that will be needed while building
@ -959,7 +958,6 @@ int dummy;
(srcs, ofilenames, cmd) = self.eval_custom_target_command(target) (srcs, ofilenames, cmd) = self.eval_custom_target_command(target)
deps = self.unwrap_dep_list(target) deps = self.unwrap_dep_list(target)
deps += self.get_custom_target_depend_files(target) deps += self.get_custom_target_depend_files(target)
desc = 'Generating {0} with a custom command{1}'
if target.build_always_stale: if target.build_always_stale:
deps.append('PHONY') deps.append('PHONY')
if target.depfile is None: if target.depfile is None:
@ -990,7 +988,7 @@ int dummy;
if target.console: if target.console:
elem.add_item('pool', 'console') elem.add_item('pool', 'console')
elem.add_item('COMMAND', cmd) elem.add_item('COMMAND', cmd)
elem.add_item('description', desc.format(target.name, cmd_type)) elem.add_item('description', f'Generating {target.name} with a custom command{cmd_type}')
self.add_build(elem) self.add_build(elem)
self.processed_targets.add(target.get_id()) self.processed_targets.add(target.get_id())
@ -1010,7 +1008,6 @@ int dummy;
else: else:
target_env = self.get_run_target_env(target) target_env = self.get_run_target_env(target)
_, _, cmd = self.eval_custom_target_command(target) _, _, cmd = self.eval_custom_target_command(target)
desc = 'Running external command {}{}'
meson_exe_cmd, reason = self.as_meson_exe_cmdline(target_name, target.command[0], cmd[1:], meson_exe_cmd, reason = self.as_meson_exe_cmdline(target_name, target.command[0], cmd[1:],
force_serialize=True, env=target_env, force_serialize=True, env=target_env,
verbose=True) verbose=True)
@ -1018,7 +1015,7 @@ int dummy;
internal_target_name = f'meson-{target_name}' internal_target_name = f'meson-{target_name}'
elem = NinjaBuildElement(self.all_outputs, internal_target_name, 'CUSTOM_COMMAND', []) elem = NinjaBuildElement(self.all_outputs, internal_target_name, 'CUSTOM_COMMAND', [])
elem.add_item('COMMAND', meson_exe_cmd) elem.add_item('COMMAND', meson_exe_cmd)
elem.add_item('description', desc.format(target.name, cmd_type)) elem.add_item('description', f'Running external command {target.name}{cmd_type}')
elem.add_item('pool', 'console') elem.add_item('pool', 'console')
# Alias that runs the target defined above with the name the user specified # Alias that runs the target defined above with the name the user specified
self.create_target_alias(internal_target_name) self.create_target_alias(internal_target_name)
@ -1393,8 +1390,7 @@ int dummy;
# either in the source root, or generated with configure_file and # either in the source root, or generated with configure_file and
# in the build root # in the build root
if not isinstance(s, File): if not isinstance(s, File):
msg = 'All sources in target {!r} must be of type ' \ msg = f'All sources in target {t!r} must be of type mesonlib.File, not {s!r}'
'mesonlib.File, not {!r}'.format(t, s)
raise InvalidArguments(msg) raise InvalidArguments(msg)
f = s.rel_to_builddir(self.build_to_src) f = s.rel_to_builddir(self.build_to_src)
if s.endswith(('.vala', '.gs')): if s.endswith(('.vala', '.gs')):
@ -1433,8 +1429,8 @@ int dummy;
(vala_src, vapi_src, other_src) = self.split_vala_sources(target) (vala_src, vapi_src, other_src) = self.split_vala_sources(target)
extra_dep_files = [] extra_dep_files = []
if not vala_src: if not vala_src:
msg = 'Vala library {!r} has no Vala or Genie source files.' msg = f'Vala library {target.name!r} has no Vala or Genie source files.'
raise InvalidArguments(msg.format(target.name)) raise InvalidArguments(msg)
valac = target.compilers['vala'] valac = target.compilers['vala']
c_out_dir = self.get_target_private_dir(target) c_out_dir = self.get_target_private_dir(target)
@ -2263,8 +2259,8 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
modname = modmatch.group(1).lower() modname = modmatch.group(1).lower()
if modname in module_files: if modname in module_files:
raise InvalidArguments( raise InvalidArguments(
'Namespace collision: module {} defined in ' f'Namespace collision: module {modname} defined in '
'two files {} and {}.'.format(modname, module_files[modname], s)) 'two files {module_files[modname]} and {s}.')
module_files[modname] = s module_files[modname] = s
else: else:
submodmatch = submodre.match(line) submodmatch = submodre.match(line)
@ -2275,8 +2271,8 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
if submodname in submodule_files: if submodname in submodule_files:
raise InvalidArguments( raise InvalidArguments(
'Namespace collision: submodule {} defined in ' 'Namespace collision: submodule {submodname} defined in '
'two files {} and {}.'.format(submodname, submodule_files[submodname], s)) 'two files {submodule_files[submodname]} and {s}.')
submodule_files[submodname] = s submodule_files[submodname] = s
self.fortran_deps[target.get_basename()] = {**module_files, **submodule_files} self.fortran_deps[target.get_basename()] = {**module_files, **submodule_files}
@ -2695,9 +2691,8 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
if not pch: if not pch:
continue continue
if not has_path_sep(pch[0]) or not has_path_sep(pch[-1]): if not has_path_sep(pch[0]) or not has_path_sep(pch[-1]):
msg = 'Precompiled header of {!r} must not be in the same ' \ msg = f'Precompiled header of {target.get_basename()!r} must not be in the same ' \
'directory as source, please put it in a subdirectory.' \ 'directory as source, please put it in a subdirectory.'
''.format(target.get_basename())
raise InvalidArguments(msg) raise InvalidArguments(msg)
compiler = target.compilers[lang] compiler = target.compilers[lang]
if isinstance(compiler, VisualStudioLikeCompiler): if isinstance(compiler, VisualStudioLikeCompiler):
@ -3331,7 +3326,7 @@ def _scan_fortran_file_deps(src: Path, srcdir: Path, dirname: Path, tdeps, compi
parents = submodmatch.group(1).lower().split(':') parents = submodmatch.group(1).lower().split(':')
assert len(parents) in (1, 2), ( assert len(parents) in (1, 2), (
'submodule ancestry must be specified as' 'submodule ancestry must be specified as'
' ancestor:parent but Meson found {}'.format(parents)) f' ancestor:parent but Meson found {parents}')
ancestor_child = '_'.join(parents) ancestor_child = '_'.join(parents)
if ancestor_child not in tdeps: if ancestor_child not in tdeps:

@ -461,9 +461,9 @@ class Target:
def __init__(self, name: str, subdir: str, subproject: str, build_by_default: bool, for_machine: MachineChoice): def __init__(self, name: str, subdir: str, subproject: str, build_by_default: bool, for_machine: MachineChoice):
if has_path_sep(name): if has_path_sep(name):
# Fix failing test 53 when this becomes an error. # Fix failing test 53 when this becomes an error.
mlog.warning('''Target "{}" has a path separator in its name. mlog.warning(f'''Target "{name}" has a path separator in its name.
This is not supported, it can cause unexpected failures and will become This is not supported, it can cause unexpected failures and will become
a hard error in the future.'''.format(name)) a hard error in the future.''')
self.name = name self.name = name
self.subdir = subdir self.subdir = subdir
self.subproject = subproject self.subproject = subproject
@ -475,7 +475,7 @@ a hard error in the future.'''.format(name))
self.option_overrides_compiler: T.Dict[OptionKey, str] = {} self.option_overrides_compiler: T.Dict[OptionKey, str] = {}
self.extra_files = [] # type: T.List[File] self.extra_files = [] # type: T.List[File]
if not hasattr(self, 'typename'): if not hasattr(self, 'typename'):
raise RuntimeError('Target type is not set for target class "{}". This is a bug'.format(type(self).__name__)) raise RuntimeError(f'Target type is not set for target class "{type(self).__name__}". This is a bug')
def __lt__(self, other: object) -> bool: def __lt__(self, other: object) -> bool:
if not hasattr(other, 'get_id') and not callable(other.get_id): if not hasattr(other, 'get_id') and not callable(other.get_id):
@ -684,7 +684,7 @@ class BuildTarget(Target):
'tree. Try adding it in the list of sources.' 'tree. Try adding it in the list of sources.'
raise InvalidArguments(msg) raise InvalidArguments(msg)
else: else:
msg = 'Bad object of type {!r} in target {!r}.'.format(type(s).__name__, self.name) msg = f'Bad object of type {type(s).__name__!r} in target {self.name!r}.'
raise InvalidArguments(msg) raise InvalidArguments(msg)
def process_sourcelist(self, sources: T.List['SourceOutputs']) -> None: def process_sourcelist(self, sources: T.List['SourceOutputs']) -> None:
@ -1126,7 +1126,7 @@ This will become a hard error in a future Meson release.''')
# Check if we have -fPIC, -fpic, -fPIE, or -fpie in cflags # Check if we have -fPIC, -fpic, -fPIE, or -fpie in cflags
all_flags = self.extra_args['c'] + self.extra_args['cpp'] all_flags = self.extra_args['c'] + self.extra_args['cpp']
if '-f' + arg.lower() in all_flags or '-f' + arg.upper() in all_flags: if '-f' + arg.lower() in all_flags or '-f' + arg.upper() in all_flags:
mlog.warning("Use the '{}' kwarg instead of passing '{}' manually to {!r}".format(arg, '-f' + arg, self.name)) mlog.warning(f"Use the '{arg}' kwarg instead of passing '-f{arg}' manually to {self.name!r}")
return True return True
k = OptionKey(option) k = OptionKey(option)
@ -1228,10 +1228,10 @@ You probably should put it in link_with instead.''')
raise InvalidArguments('Tried to use subproject object as a dependency.\n' raise InvalidArguments('Tried to use subproject object as a dependency.\n'
'You probably wanted to use a dependency declared in it instead.\n' 'You probably wanted to use a dependency declared in it instead.\n'
'Access it by calling get_variable() on the subproject object.') 'Access it by calling get_variable() on the subproject object.')
raise InvalidArguments('Argument is of an unacceptable type {!r}.\nMust be ' raise InvalidArguments(f'Argument is of an unacceptable type {type(dep).__name__!r}.\nMust be '
'either an external dependency (returned by find_library() or ' 'either an external dependency (returned by find_library() or '
'dependency()) or an internal dependency (returned by ' 'dependency()) or an internal dependency (returned by '
'declare_dependency()).'.format(type(dep).__name__)) 'declare_dependency()).')
self.added_deps.add(dep) self.added_deps.add(dep)
def get_external_deps(self): def get_external_deps(self):
@ -1245,9 +1245,9 @@ You probably should put it in link_with instead.''')
if isinstance(self, StaticLibrary) and self.need_install: if isinstance(self, StaticLibrary) and self.need_install:
if isinstance(t, (CustomTarget, CustomTargetIndex)): if isinstance(t, (CustomTarget, CustomTargetIndex)):
if not t.should_install(): if not t.should_install():
mlog.warning('Try to link an installed static library target {} with a custom target ' mlog.warning(f'Try to link an installed static library target {self.name} with a'
'that is not installed, this might cause problems when you try to use ' 'custom target that is not installed, this might cause problems'
'this static library'.format(self.name)) 'when you try to use this static library')
elif t.is_internal(): elif t.is_internal():
# When we're a static library and we link_with to an # When we're a static library and we link_with to an
# internal/convenience library, promote to link_whole. # internal/convenience library, promote to link_whole.
@ -1308,7 +1308,7 @@ You probably should put it in link_with instead.''')
return return
elif len(pchlist) == 1: elif len(pchlist) == 1:
if not environment.is_header(pchlist[0]): if not environment.is_header(pchlist[0]):
raise InvalidArguments('PCH argument {} is not a header.'.format(pchlist[0])) raise InvalidArguments(f'PCH argument {pchlist[0]} is not a header.')
elif len(pchlist) == 2: elif len(pchlist) == 2:
if environment.is_header(pchlist[0]): if environment.is_header(pchlist[0]):
if not environment.is_source(pchlist[1]): if not environment.is_source(pchlist[1]):
@ -1318,7 +1318,7 @@ You probably should put it in link_with instead.''')
raise InvalidArguments('PCH definition must contain one header and at most one source.') raise InvalidArguments('PCH definition must contain one header and at most one source.')
pchlist = [pchlist[1], pchlist[0]] pchlist = [pchlist[1], pchlist[0]]
else: else:
raise InvalidArguments('PCH argument {} is of unknown type.'.format(pchlist[0])) raise InvalidArguments(f'PCH argument {pchlist[0]} is of unknown type.')
if (os.path.dirname(pchlist[0]) != os.path.dirname(pchlist[1])): if (os.path.dirname(pchlist[0]) != os.path.dirname(pchlist[1])):
raise InvalidArguments('PCH files must be stored in the same folder.') raise InvalidArguments('PCH files must be stored in the same folder.')
@ -1398,9 +1398,9 @@ You probably should put it in link_with instead.''')
prelinker = all_compilers[l] prelinker = all_compilers[l]
except KeyError: except KeyError:
raise MesonException( raise MesonException(
'Could not get a prelinker linker for build target {!r}. ' f'Could not get a prelinker linker for build target {self.name!r}. '
'Requires a compiler for language "{}", but that is not ' f'Requires a compiler for language "{l}", but that is not '
'a project language.'.format(self.name, l)) 'a project language.')
return prelinker return prelinker
raise MesonException(f'Could not determine prelinker for {self.name!r}.') raise MesonException(f'Could not determine prelinker for {self.name!r}.')
@ -1432,9 +1432,9 @@ You probably should put it in link_with instead.''')
linker = all_compilers[l] linker = all_compilers[l]
except KeyError: except KeyError:
raise MesonException( raise MesonException(
'Could not get a dynamic linker for build target {!r}. ' f'Could not get a dynamic linker for build target {self.name!r}. '
'Requires a linker for language "{}", but that is not ' f'Requires a linker for language "{l}", but that is not '
'a project language.'.format(self.name, l)) 'a project language.')
stdlib_args = [] stdlib_args = []
added_languages = set() added_languages = set()
for dl in itertools.chain(self.compilers, dep_langs): for dl in itertools.chain(self.compilers, dep_langs):
@ -1445,8 +1445,8 @@ You probably should put it in link_with instead.''')
# Pretty hard to fix because the return value is passed everywhere # Pretty hard to fix because the return value is passed everywhere
return linker, stdlib_args return linker, stdlib_args
m = 'Could not get a dynamic linker for build target {!r}' m = f'Could not get a dynamic linker for build target {self.name!r}'
raise AssertionError(m.format(self.name)) raise AssertionError(m)
def uses_rust(self) -> bool: def uses_rust(self) -> bool:
"""Is this target a rust target.""" """Is this target a rust target."""
@ -2022,7 +2022,7 @@ class SharedLibrary(BuildTarget):
darwin_versions = 2 * [darwin_versions] darwin_versions = 2 * [darwin_versions]
if not isinstance(darwin_versions, list): if not isinstance(darwin_versions, list):
raise InvalidArguments('Shared library darwin_versions: must be a string, integer,' raise InvalidArguments('Shared library darwin_versions: must be a string, integer,'
'or a list, not {!r}'.format(darwin_versions)) f'or a list, not {darwin_versions!r}')
if len(darwin_versions) > 2: if len(darwin_versions) > 2:
raise InvalidArguments('Shared library darwin_versions: list must contain 2 or fewer elements') raise InvalidArguments('Shared library darwin_versions: list must contain 2 or fewer elements')
if len(darwin_versions) == 1: if len(darwin_versions) == 1:
@ -2032,7 +2032,7 @@ class SharedLibrary(BuildTarget):
v = str(v) v = str(v)
if not isinstance(v, str): if not isinstance(v, str):
raise InvalidArguments('Shared library darwin_versions: list elements ' raise InvalidArguments('Shared library darwin_versions: list elements '
'must be strings or integers, not {!r}'.format(v)) f'must be strings or integers, not {v!r}')
if not re.fullmatch(r'[0-9]+(\.[0-9]+){0,2}', v): if not re.fullmatch(r'[0-9]+(\.[0-9]+){0,2}', v):
raise InvalidArguments('Shared library darwin_versions: must be X.Y.Z where ' raise InvalidArguments('Shared library darwin_versions: must be X.Y.Z where '
'X, Y, Z are numbers, and Y and Z are optional') 'X, Y, Z are numbers, and Y and Z are optional')
@ -2374,15 +2374,15 @@ class CustomTarget(Target, CommandBase):
extra_deps, depend_files = [extract_as_list(kwargs, c, pop=False) for c in ['depends', 'depend_files']] extra_deps, depend_files = [extract_as_list(kwargs, c, pop=False) for c in ['depends', 'depend_files']]
for ed in unholder(extra_deps): for ed in unholder(extra_deps):
if not isinstance(ed, (CustomTarget, BuildTarget)): if not isinstance(ed, (CustomTarget, BuildTarget)):
raise InvalidArguments('Can only depend on toplevel targets: custom_target or build_target (executable or a library) got: {}({})' raise InvalidArguments('Can only depend on toplevel targets: custom_target or build_target '
.format(type(ed), ed)) f'(executable or a library) got: {type(ed)}({ed})')
self.extra_depends.append(ed) self.extra_depends.append(ed)
for i in depend_files: for i in depend_files:
if isinstance(i, (File, str)): if isinstance(i, (File, str)):
self.depend_files.append(i) self.depend_files.append(i)
else: else:
mlog.debug(i) mlog.debug(i)
raise InvalidArguments('Unknown type {!r} in depend_files.'.format(type(i).__name__)) raise InvalidArguments(f'Unknown type {type(i).__name__!r} in depend_files.')
self.env = kwargs.get('env') self.env = kwargs.get('env')
def get_dependencies(self): def get_dependencies(self):
@ -2689,10 +2689,10 @@ def load(build_dir: str) -> Build:
raise MesonException(load_fail_msg) raise MesonException(load_fail_msg)
except AttributeError: except AttributeError:
raise MesonException( raise MesonException(
"Build data file {!r} references functions or classes that don't " f"Build data file {filename!r} references functions or classes that don't "
"exist. This probably means that it was generated with an old " "exist. This probably means that it was generated with an old "
"version of meson. Try running from the source directory " "version of meson. Try running from the source directory "
"meson {} --wipe".format(filename, build_dir)) f"meson {build_dir} --wipe")
if not isinstance(obj, Build): if not isinstance(obj, Build):
raise MesonException(load_fail_msg) raise MesonException(load_fail_msg)
return obj return obj

@ -348,35 +348,32 @@ class CLikeCompiler(Compiler):
def check_header(self, hname: str, prefix: str, env: 'Environment', *, def check_header(self, hname: str, prefix: str, env: 'Environment', *,
extra_args: T.Optional[T.List[str]] = None, extra_args: T.Optional[T.List[str]] = None,
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]: dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]:
fargs = {'prefix': prefix, 'header': hname} code = f'''{prefix}
code = '''{prefix} #include <{hname}>'''
#include <{header}>''' return self.compiles(code, env, extra_args=extra_args,
return self.compiles(code.format(**fargs), env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
def has_header(self, hname: str, prefix: str, env: 'Environment', *, def has_header(self, hname: str, prefix: str, env: 'Environment', *,
extra_args: T.Optional[T.List[str]] = None, extra_args: T.Optional[T.List[str]] = None,
dependencies: T.Optional[T.List['Dependency']] = None, dependencies: T.Optional[T.List['Dependency']] = None,
disable_cache: bool = False) -> T.Tuple[bool, bool]: disable_cache: bool = False) -> T.Tuple[bool, bool]:
fargs = {'prefix': prefix, 'header': hname} code = f'''{prefix}
code = '''{prefix}
#ifdef __has_include #ifdef __has_include
#if !__has_include("{header}") #if !__has_include("{hname}")
#error "Header '{header}' could not be found" #error "Header '{hname}' could not be found"
#endif #endif
#else #else
#include <{header}> #include <{hname}>
#endif''' #endif'''
return self.compiles(code.format(**fargs), env, extra_args=extra_args, return self.compiles(code, env, extra_args=extra_args,
dependencies=dependencies, mode='preprocess', disable_cache=disable_cache) dependencies=dependencies, mode='preprocess', disable_cache=disable_cache)
def has_header_symbol(self, hname: str, symbol: str, prefix: str, def has_header_symbol(self, hname: str, symbol: str, prefix: str,
env: 'Environment', *, env: 'Environment', *,
extra_args: T.Optional[T.List[str]] = None, extra_args: T.Optional[T.List[str]] = None,
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]: dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]:
fargs = {'prefix': prefix, 'header': hname, 'symbol': symbol} t = f'''{prefix}
t = '''{prefix} #include <{hname}>
#include <{header}>
int main(void) {{ int main(void) {{
/* If it's not defined as a macro, try to use as a symbol */ /* If it's not defined as a macro, try to use as a symbol */
#ifndef {symbol} #ifndef {symbol}
@ -384,7 +381,7 @@ class CLikeCompiler(Compiler):
#endif #endif
return 0; return 0;
}}''' }}'''
return self.compiles(t.format(**fargs), env, extra_args=extra_args, return self.compiles(t, env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
def _get_basic_compiler_args(self, env: 'Environment', mode: CompileCheckMode) -> T.Tuple[T.List[str], T.List[str]]: def _get_basic_compiler_args(self, env: 'Environment', mode: CompileCheckMode) -> T.Tuple[T.List[str], T.List[str]]:
@ -499,11 +496,10 @@ class CLikeCompiler(Compiler):
def _compile_int(self, expression: str, prefix: str, env: 'Environment', def _compile_int(self, expression: str, prefix: str, env: 'Environment',
extra_args: T.Optional[T.List[str]], extra_args: T.Optional[T.List[str]],
dependencies: T.Optional[T.List['Dependency']]) -> bool: dependencies: T.Optional[T.List['Dependency']]) -> bool:
fargs = {'prefix': prefix, 'expression': expression} t = f'''#include <stdio.h>
t = '''#include <stdio.h>
{prefix} {prefix}
int main(void) {{ static int a[1-2*!({expression})]; a[0]=0; return 0; }}''' int main(void) {{ static int a[1-2*!({expression})]; a[0]=0; return 0; }}'''
return self.compiles(t.format(**fargs), env, extra_args=extra_args, return self.compiles(t, env, extra_args=extra_args,
dependencies=dependencies)[0] dependencies=dependencies)[0]
def cross_compute_int(self, expression: str, low: T.Optional[int], high: T.Optional[int], def cross_compute_int(self, expression: str, low: T.Optional[int], high: T.Optional[int],
@ -565,14 +561,13 @@ class CLikeCompiler(Compiler):
extra_args = [] extra_args = []
if self.is_cross: if self.is_cross:
return self.cross_compute_int(expression, low, high, guess, prefix, env, extra_args, dependencies) return self.cross_compute_int(expression, low, high, guess, prefix, env, extra_args, dependencies)
fargs = {'prefix': prefix, 'expression': expression} t = f'''#include<stdio.h>
t = '''#include<stdio.h>
{prefix} {prefix}
int main(void) {{ int main(void) {{
printf("%ld\\n", (long)({expression})); printf("%ld\\n", (long)({expression}));
return 0; return 0;
}};''' }};'''
res = self.run(t.format(**fargs), env, extra_args=extra_args, res = self.run(t, env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
if not res.compiled: if not res.compiled:
return -1 return -1
@ -585,14 +580,13 @@ class CLikeCompiler(Compiler):
dependencies: T.Optional[T.List['Dependency']] = None) -> int: dependencies: T.Optional[T.List['Dependency']] = None) -> int:
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
fargs = {'prefix': prefix, 'type': typename} t = f'''#include <stdio.h>
t = '''#include <stdio.h>
{prefix} {prefix}
int main(void) {{ int main(void) {{
{type} something; {typename} something;
return 0; return 0;
}}''' }}'''
if not self.compiles(t.format(**fargs), env, extra_args=extra_args, if not self.compiles(t, env, extra_args=extra_args,
dependencies=dependencies)[0]: dependencies=dependencies)[0]:
return -1 return -1
return self.cross_compute_int('sizeof(%s)' % typename, None, None, None, prefix, env, extra_args, dependencies) return self.cross_compute_int('sizeof(%s)' % typename, None, None, None, prefix, env, extra_args, dependencies)
@ -602,17 +596,16 @@ class CLikeCompiler(Compiler):
dependencies: T.Optional[T.List['Dependency']] = None) -> int: dependencies: T.Optional[T.List['Dependency']] = None) -> int:
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
fargs = {'prefix': prefix, 'type': typename}
if self.is_cross: if self.is_cross:
return self.cross_sizeof(typename, prefix, env, extra_args=extra_args, return self.cross_sizeof(typename, prefix, env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
t = '''#include<stdio.h> t = f'''#include<stdio.h>
{prefix} {prefix}
int main(void) {{ int main(void) {{
printf("%ld\\n", (long)(sizeof({type}))); printf("%ld\\n", (long)(sizeof({typename})));
return 0; return 0;
}};''' }};'''
res = self.run(t.format(**fargs), env, extra_args=extra_args, res = self.run(t, env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
if not res.compiled: if not res.compiled:
return -1 return -1
@ -625,23 +618,22 @@ class CLikeCompiler(Compiler):
dependencies: T.Optional[T.List['Dependency']] = None) -> int: dependencies: T.Optional[T.List['Dependency']] = None) -> int:
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
fargs = {'prefix': prefix, 'type': typename} t = f'''#include <stdio.h>
t = '''#include <stdio.h>
{prefix} {prefix}
int main(void) {{ int main(void) {{
{type} something; {typename} something;
return 0; return 0;
}}''' }}'''
if not self.compiles(t.format(**fargs), env, extra_args=extra_args, if not self.compiles(t, env, extra_args=extra_args,
dependencies=dependencies)[0]: dependencies=dependencies)[0]:
return -1 return -1
t = '''#include <stddef.h> t = f'''#include <stddef.h>
{prefix} {prefix}
struct tmp {{ struct tmp {{
char c; char c;
{type} target; {typename} target;
}};''' }};'''
return self.cross_compute_int('offsetof(struct tmp, target)', None, None, None, t.format(**fargs), env, extra_args, dependencies) return self.cross_compute_int('offsetof(struct tmp, target)', None, None, None, t, env, extra_args, dependencies)
def alignment(self, typename: str, prefix: str, env: 'Environment', *, def alignment(self, typename: str, prefix: str, env: 'Environment', *,
extra_args: T.Optional[T.List[str]] = None, extra_args: T.Optional[T.List[str]] = None,
@ -651,19 +643,18 @@ class CLikeCompiler(Compiler):
if self.is_cross: if self.is_cross:
return self.cross_alignment(typename, prefix, env, extra_args=extra_args, return self.cross_alignment(typename, prefix, env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
fargs = {'prefix': prefix, 'type': typename} t = f'''#include <stdio.h>
t = '''#include <stdio.h>
#include <stddef.h> #include <stddef.h>
{prefix} {prefix}
struct tmp {{ struct tmp {{
char c; char c;
{type} target; {typename} target;
}}; }};
int main(void) {{ int main(void) {{
printf("%d", (int)offsetof(struct tmp, target)); printf("%d", (int)offsetof(struct tmp, target));
return 0; return 0;
}}''' }}'''
res = self.run(t.format(**fargs), env, extra_args=extra_args, res = self.run(t, env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
if not res.compiled: if not res.compiled:
raise mesonlib.EnvironmentException('Could not compile alignment test.') raise mesonlib.EnvironmentException('Could not compile alignment test.')
@ -679,18 +670,17 @@ class CLikeCompiler(Compiler):
dependencies: T.Optional[T.List['Dependency']], dependencies: T.Optional[T.List['Dependency']],
disable_cache: bool = False) -> T.Tuple[str, bool]: disable_cache: bool = False) -> T.Tuple[str, bool]:
delim = '"MESON_GET_DEFINE_DELIMITER"' delim = '"MESON_GET_DEFINE_DELIMITER"'
fargs = {'prefix': prefix, 'define': dname, 'delim': delim} code = f'''
code = '''
{prefix} {prefix}
#ifndef {define} #ifndef {dname}
# define {define} # define {dname}
#endif #endif
{delim}\n{define}''' {delim}\n{dname}'''
args = self.build_wrapper_args(env, extra_args, dependencies, args = self.build_wrapper_args(env, extra_args, dependencies,
mode=CompileCheckMode.PREPROCESS).to_native() mode=CompileCheckMode.PREPROCESS).to_native()
func = functools.partial(self.cached_compile, code.format(**fargs), env.coredata, extra_args=args, mode='preprocess') func = functools.partial(self.cached_compile, code, env.coredata, extra_args=args, mode='preprocess')
if disable_cache: if disable_cache:
func = functools.partial(self.compile, code.format(**fargs), extra_args=args, mode='preprocess', temp_dir=env.scratch_dir) func = functools.partial(self.compile, code, extra_args=args, mode='preprocess', temp_dir=env.scratch_dir)
with func() as p: with func() as p:
cached = p.cached cached = p.cached
if p.returncode != 0: if p.returncode != 0:
@ -713,25 +703,24 @@ class CLikeCompiler(Compiler):
cast = '(long long int)' cast = '(long long int)'
else: else:
raise AssertionError(f'BUG: Unknown return type {rtype!r}') raise AssertionError(f'BUG: Unknown return type {rtype!r}')
fargs = {'prefix': prefix, 'f': fname, 'cast': cast, 'fmt': fmt} code = f'''{prefix}
code = '''{prefix}
#include <stdio.h> #include <stdio.h>
int main(void) {{ int main(void) {{
printf ("{fmt}", {cast} {f}()); printf ("{fmt}", {cast} {fname}());
return 0; return 0;
}}'''.format(**fargs) }}'''
res = self.run(code, env, extra_args=extra_args, dependencies=dependencies) res = self.run(code, env, extra_args=extra_args, dependencies=dependencies)
if not res.compiled: if not res.compiled:
m = 'Could not get return value of {}()' m = f'Could not get return value of {fname}()'
raise mesonlib.EnvironmentException(m.format(fname)) raise mesonlib.EnvironmentException(m)
if rtype == 'string': if rtype == 'string':
return res.stdout return res.stdout
elif rtype == 'int': elif rtype == 'int':
try: try:
return int(res.stdout.strip()) return int(res.stdout.strip())
except ValueError: except ValueError:
m = 'Return value of {}() is not an int' m = f'Return value of {fname}() is not an int'
raise mesonlib.EnvironmentException(m.format(fname)) raise mesonlib.EnvironmentException(m)
assert False, 'Unreachable' assert False, 'Unreachable'
@staticmethod @staticmethod
@ -896,28 +885,25 @@ class CLikeCompiler(Compiler):
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]: dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]:
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
fargs = {'prefix': prefix, 'type': typename, 'name': 'foo'}
# Create code that accesses all members # Create code that accesses all members
members = '' members = ''
for member in membernames: for member in membernames:
members += '{}.{};\n'.format(fargs['name'], member) members += f'foo.{member};\n'
fargs['members'] = members t = f'''{prefix}
t = '''{prefix}
void bar(void) {{ void bar(void) {{
{type} {name}; {typename} foo;
{members} {members}
}};''' }};'''
return self.compiles(t.format(**fargs), env, extra_args=extra_args, return self.compiles(t, env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
def has_type(self, typename: str, prefix: str, env: 'Environment', extra_args: T.List[str], def has_type(self, typename: str, prefix: str, env: 'Environment', extra_args: T.List[str],
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]: dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]:
fargs = {'prefix': prefix, 'type': typename} t = f'''{prefix}
t = '''{prefix}
void bar(void) {{ void bar(void) {{
sizeof({type}); sizeof({typename});
}};''' }};'''
return self.compiles(t.format(**fargs), env, extra_args=extra_args, return self.compiles(t, env, extra_args=extra_args,
dependencies=dependencies) dependencies=dependencies)
def symbols_have_underscore_prefix(self, env: 'Environment') -> bool: def symbols_have_underscore_prefix(self, env: 'Environment') -> bool:
@ -937,11 +923,11 @@ class CLikeCompiler(Compiler):
n = 'symbols_have_underscore_prefix' n = 'symbols_have_underscore_prefix'
with self._build_wrapper(code, env, extra_args=args, mode='compile', want_output=True, temp_dir=env.scratch_dir) as p: with self._build_wrapper(code, env, extra_args=args, mode='compile', want_output=True, temp_dir=env.scratch_dir) as p:
if p.returncode != 0: if p.returncode != 0:
m = 'BUG: Unable to compile {!r} check: {}' m = f'BUG: Unable to compile {n!r} check: {p.stdout}'
raise RuntimeError(m.format(n, p.stdout)) raise RuntimeError(m)
if not os.path.isfile(p.output_name): if not os.path.isfile(p.output_name):
m = 'BUG: Can\'t find compiled test code for {!r} check' m = f'BUG: Can\'t find compiled test code for {n!r} check'
raise RuntimeError(m.format(n)) raise RuntimeError(m)
with open(p.output_name, 'rb') as o: with open(p.output_name, 'rb') as o:
for line in o: for line in o:
# Check if the underscore form of the symbol is somewhere # Check if the underscore form of the symbol is somewhere
@ -1065,8 +1051,7 @@ class CLikeCompiler(Compiler):
if archs and env.machines.host.cpu_family in archs: if archs and env.machines.host.cpu_family in archs:
return p return p
else: else:
mlog.debug('Rejected {}, supports {} but need {}' mlog.debug(f'Rejected {p}, supports {archs} but need {env.machines.host.cpu_family}')
.format(p, archs, env.machines.host.cpu_family))
return None return None
@functools.lru_cache() @functools.lru_cache()
@ -1236,14 +1221,13 @@ class CLikeCompiler(Compiler):
if arg.startswith('-Wno-'): if arg.startswith('-Wno-'):
new_args.append('-W' + arg[5:]) new_args.append('-W' + arg[5:])
if arg.startswith('-Wl,'): if arg.startswith('-Wl,'):
mlog.warning('{} looks like a linker argument, ' mlog.warning(f'{arg} looks like a linker argument, '
'but has_argument and other similar methods only ' 'but has_argument and other similar methods only '
'support checking compiler arguments. Using them ' 'support checking compiler arguments. Using them '
'to check linker arguments are never supported, ' 'to check linker arguments are never supported, '
'and results are likely to be wrong regardless of ' 'and results are likely to be wrong regardless of '
'the compiler you are using. has_link_argument or ' 'the compiler you are using. has_link_argument or '
'other similar method can be used instead.' 'other similar method can be used instead.')
.format(arg))
new_args.append(arg) new_args.append(arg)
return self.has_arguments(new_args, env, code, mode='compile') return self.has_arguments(new_args, env, code, mode='compile')

Loading…
Cancel
Save