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

@ -133,11 +133,11 @@ def ninja_quote(text: str, is_build_line=False) -> str:
if not quote_re.search(text):
return 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)
return quote_re.sub(r'$\g<0>', text)
@ -437,8 +437,8 @@ class NinjaBackend(backends.Backend):
# 'benchmark', etc, and also for RunTargets.
# https://github.com/mesonbuild/meson/issues/1644
if not to_target.startswith('meson-'):
m = 'Invalid usage of create_target_alias with {!r}'
raise AssertionError(m.format(to_target))
m = f'Invalid usage of create_target_alias with {to_target!r}'
raise AssertionError(m)
from_target = to_target[len('meson-'):]
elem = NinjaBuildElement(self.all_outputs, from_target, 'phony', to_target)
self.add_build(elem)
@ -530,10 +530,10 @@ int dummy;
num_pools = self.environment.coredata.options[OptionKey('backend_max_links')].value
if num_pools > 0:
outfile.write('''pool link_pool
depth = {}
outfile.write(f'''pool link_pool
depth = {num_pools}
'''.format(num_pools))
''')
with self.detect_vs_dep_prefix(tempfilename) as outfile:
self.generate_rules()
@ -776,9 +776,8 @@ int dummy;
if langs_cant:
langs_are = langs = ', '.join(langs_cant).upper()
langs_are += ' are' if len(langs_cant) > 1 else ' is'
msg = '{} not supported in Unity builds yet, so {} ' \
'sources in the {!r} target will be compiled normally' \
''.format(langs_are, langs, target.name)
msg = f'{langs_are} not supported in Unity builds yet, so {langs} ' \
f'sources in the {target.name!r} target will be compiled normally'
mlog.log(mlog.red('FIXME'), msg)
# 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)
deps = self.unwrap_dep_list(target)
deps += self.get_custom_target_depend_files(target)
desc = 'Generating {0} with a custom command{1}'
if target.build_always_stale:
deps.append('PHONY')
if target.depfile is None:
@ -990,7 +988,7 @@ int dummy;
if target.console:
elem.add_item('pool', 'console')
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.processed_targets.add(target.get_id())
@ -1010,7 +1008,6 @@ int dummy;
else:
target_env = self.get_run_target_env(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:],
force_serialize=True, env=target_env,
verbose=True)
@ -1018,7 +1015,7 @@ int dummy;
internal_target_name = f'meson-{target_name}'
elem = NinjaBuildElement(self.all_outputs, internal_target_name, 'CUSTOM_COMMAND', [])
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')
# Alias that runs the target defined above with the name the user specified
self.create_target_alias(internal_target_name)
@ -1393,8 +1390,7 @@ int dummy;
# either in the source root, or generated with configure_file and
# in the build root
if not isinstance(s, File):
msg = 'All sources in target {!r} must be of type ' \
'mesonlib.File, not {!r}'.format(t, s)
msg = f'All sources in target {t!r} must be of type mesonlib.File, not {s!r}'
raise InvalidArguments(msg)
f = s.rel_to_builddir(self.build_to_src)
if s.endswith(('.vala', '.gs')):
@ -1433,8 +1429,8 @@ int dummy;
(vala_src, vapi_src, other_src) = self.split_vala_sources(target)
extra_dep_files = []
if not vala_src:
msg = 'Vala library {!r} has no Vala or Genie source files.'
raise InvalidArguments(msg.format(target.name))
msg = f'Vala library {target.name!r} has no Vala or Genie source files.'
raise InvalidArguments(msg)
valac = target.compilers['vala']
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()
if modname in module_files:
raise InvalidArguments(
'Namespace collision: module {} defined in '
'two files {} and {}.'.format(modname, module_files[modname], s))
f'Namespace collision: module {modname} defined in '
'two files {module_files[modname]} and {s}.')
module_files[modname] = s
else:
submodmatch = submodre.match(line)
@ -2275,8 +2271,8 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
if submodname in submodule_files:
raise InvalidArguments(
'Namespace collision: submodule {} defined in '
'two files {} and {}.'.format(submodname, submodule_files[submodname], s))
'Namespace collision: submodule {submodname} defined in '
'two files {submodule_files[submodname]} and {s}.')
submodule_files[submodname] = s
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:
continue
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 ' \
'directory as source, please put it in a subdirectory.' \
''.format(target.get_basename())
msg = f'Precompiled header of {target.get_basename()!r} must not be in the same ' \
'directory as source, please put it in a subdirectory.'
raise InvalidArguments(msg)
compiler = target.compilers[lang]
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(':')
assert len(parents) in (1, 2), (
'submodule ancestry must be specified as'
' ancestor:parent but Meson found {}'.format(parents))
f' ancestor:parent but Meson found {parents}')
ancestor_child = '_'.join(parents)
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):
if has_path_sep(name):
# 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
a hard error in the future.'''.format(name))
a hard error in the future.''')
self.name = name
self.subdir = subdir
self.subproject = subproject
@ -475,7 +475,7 @@ a hard error in the future.'''.format(name))
self.option_overrides_compiler: T.Dict[OptionKey, str] = {}
self.extra_files = [] # type: T.List[File]
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:
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.'
raise InvalidArguments(msg)
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)
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
all_flags = self.extra_args['c'] + self.extra_args['cpp']
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
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'
'You probably wanted to use a dependency declared in it instead.\n'
'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 '
'dependency()) or an internal dependency (returned by '
'declare_dependency()).'.format(type(dep).__name__))
'declare_dependency()).')
self.added_deps.add(dep)
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(t, (CustomTarget, CustomTargetIndex)):
if not t.should_install():
mlog.warning('Try to link an installed static library target {} with a custom target '
'that is not installed, this might cause problems when you try to use '
'this static library'.format(self.name))
mlog.warning(f'Try to link an installed static library target {self.name} with a'
'custom target that is not installed, this might cause problems'
'when you try to use this static library')
elif t.is_internal():
# When we're a static library and we link_with to an
# internal/convenience library, promote to link_whole.
@ -1308,7 +1308,7 @@ You probably should put it in link_with instead.''')
return
elif len(pchlist) == 1:
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:
if environment.is_header(pchlist[0]):
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.')
pchlist = [pchlist[1], pchlist[0]]
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])):
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]
except KeyError:
raise MesonException(
'Could not get a prelinker linker for build target {!r}. '
'Requires a compiler for language "{}", but that is not '
'a project language.'.format(self.name, l))
f'Could not get a prelinker linker for build target {self.name!r}. '
f'Requires a compiler for language "{l}", but that is not '
'a project language.')
return prelinker
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]
except KeyError:
raise MesonException(
'Could not get a dynamic linker for build target {!r}. '
'Requires a linker for language "{}", but that is not '
'a project language.'.format(self.name, l))
f'Could not get a dynamic linker for build target {self.name!r}. '
f'Requires a linker for language "{l}", but that is not '
'a project language.')
stdlib_args = []
added_languages = set()
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
return linker, stdlib_args
m = 'Could not get a dynamic linker for build target {!r}'
raise AssertionError(m.format(self.name))
m = f'Could not get a dynamic linker for build target {self.name!r}'
raise AssertionError(m)
def uses_rust(self) -> bool:
"""Is this target a rust target."""
@ -2022,7 +2022,7 @@ class SharedLibrary(BuildTarget):
darwin_versions = 2 * [darwin_versions]
if not isinstance(darwin_versions, list):
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:
raise InvalidArguments('Shared library darwin_versions: list must contain 2 or fewer elements')
if len(darwin_versions) == 1:
@ -2032,7 +2032,7 @@ class SharedLibrary(BuildTarget):
v = str(v)
if not isinstance(v, str):
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):
raise InvalidArguments('Shared library darwin_versions: must be X.Y.Z where '
'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']]
for ed in unholder(extra_deps):
if not isinstance(ed, (CustomTarget, BuildTarget)):
raise InvalidArguments('Can only depend on toplevel targets: custom_target or build_target (executable or a library) got: {}({})'
.format(type(ed), ed))
raise InvalidArguments('Can only depend on toplevel targets: custom_target or build_target '
f'(executable or a library) got: {type(ed)}({ed})')
self.extra_depends.append(ed)
for i in depend_files:
if isinstance(i, (File, str)):
self.depend_files.append(i)
else:
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')
def get_dependencies(self):
@ -2689,10 +2689,10 @@ def load(build_dir: str) -> Build:
raise MesonException(load_fail_msg)
except AttributeError:
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 "
"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):
raise MesonException(load_fail_msg)
return obj

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

Loading…
Cancel
Save