Skip to content

Commit

Permalink
when generating optional utility targets in ninja, skip existing alia…
Browse files Browse the repository at this point in the history
…ses too

When auto-generating e.g. a `clang-format` target, we first check to see
if the user has already defined one, and if so we don't bother creating
our own. We check for two things:

- if a ninja target already exists, skip
- if a run_target was defined, skip

The second check is *obviously* a duplicate of the first check. But the
first check never actually worked, because all_outputs was only
generated *after* generating all utility rules and actually writing out
the build.ninja file. The check itself compares against nothing, and
always evaluates to false no matter what.

Fix this by reordering the target creation logic so we track outputs
immediately, but only error about them later. Now, we no longer need to
special-case run_target at all, so we can drop that whole logic from
build.py and interpreter.py, and simplify the tracked state.

Fixes defining an `alias_target()` for a utility, which tried to
auto-generate another rule and errored out. Also fixes doing the same
thing with a `custom_target()` although I cannot imagine why anyone
would want to produce an output file named `clang-format` (unless clang
itself decided to migrate to Meson, which would be cool but feels
unlikely).
  • Loading branch information
eli-schwartz authored and dcbaker committed Dec 5, 2022
1 parent ce120ff commit 9e8a3b9
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 13 deletions.
13 changes: 6 additions & 7 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def __init__(self, all_outputs, outfilenames, rulename, infilenames, implicit_ou
self.orderdeps = OrderedSet()
self.elems = []
self.all_outputs = all_outputs
self.output_errors = ''

def add_dep(self, dep):
if isinstance(dep, list):
Expand Down Expand Up @@ -362,7 +363,8 @@ def count_rule_references(self):
self.rule.refcount += 1

def write(self, outfile):
self.check_outputs()
if self.output_errors:
raise MesonException(self.output_errors)
ins = ' '.join([ninja_quote(i, True) for i in self.infilenames])
outs = ' '.join([ninja_quote(i, True) for i in self.outfilenames])
implicit_outs = ' '.join([ninja_quote(i, True) for i in self.implicit_outfilenames])
Expand Down Expand Up @@ -421,7 +423,7 @@ def write(self, outfile):
def check_outputs(self):
for n in self.outfilenames:
if n in self.all_outputs:
raise MesonException(f'Multiple producers for Ninja target "{n}". Please rename your targets.')
self.output_errors = f'Multiple producers for Ninja target "{n}". Please rename your targets.'
self.all_outputs[n] = True

@dataclass
Expand Down Expand Up @@ -1283,6 +1285,7 @@ def add_rule(self, rule):
self.ruledict[rule.name] = rule

def add_build(self, build):
build.check_outputs()
self.build_elements.append(build)

if build.rulename != 'phony':
Expand Down Expand Up @@ -3331,7 +3334,7 @@ def generate_dist(self):
def generate_scanbuild(self):
if not environment.detect_scanbuild():
return
if ('', 'scan-build') in self.build.run_target_names:
if 'scan-build' in self.all_outputs:
return
cmd = self.environment.get_build_command() + \
['--internal', 'scanbuild', self.environment.source_dir, self.environment.build_dir] + \
Expand All @@ -3352,8 +3355,6 @@ def generate_clangtool(self, name, extra_arg=None):
return
if target_name in self.all_outputs:
return
if ('', target_name) in self.build.run_target_names:
return
cmd = self.environment.get_build_command() + \
['--internal', 'clang' + name, self.environment.source_dir, self.environment.build_dir] + \
extra_args
Expand All @@ -3378,8 +3379,6 @@ def generate_tags(self, tool, target_name):
import shutil
if not shutil.which(tool):
return
if ('', target_name) in self.build.run_target_names:
return
if target_name in self.all_outputs:
return
cmd = self.environment.get_build_command() + \
Expand Down
1 change: 0 additions & 1 deletion mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ def __init__(self, environment: environment.Environment):
self.environment = environment
self.projects = {}
self.targets: 'T.OrderedDict[str, T.Union[CustomTarget, BuildTarget]]' = OrderedDict()
self.run_target_names: T.Set[T.Tuple[str, str]] = set()
self.global_args: PerMachine[T.Dict[str, T.List[str]]] = PerMachine({}, {})
self.global_link_args: PerMachine[T.Dict[str, T.List[str]]] = PerMachine({}, {})
self.projects_args: PerMachine[T.Dict[str, T.Dict[str, T.List[str]]]] = PerMachine({}, {})
Expand Down
3 changes: 0 additions & 3 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1990,9 +1990,6 @@ def func_run_target(self, node: mparser.FunctionNode, args: T.Tuple[str],
tg = build.RunTarget(name, all_args, kwargs['depends'], self.subdir, self.subproject, self.environment,
kwargs['env'])
self.add_target(name, tg)
full_name = (self.subproject, name)
assert full_name not in self.build.run_target_names
self.build.run_target_names.add(full_name)
return tg

@FeatureNew('alias_target', '0.52.0')
Expand Down
1 change: 1 addition & 0 deletions test cases/common/51 run target/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BasedOnStyle: LLVM
Empty file.
9 changes: 7 additions & 2 deletions test cases/common/51 run target/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@ custom_target('configure_script_ct',
run_target('ctags',
command : converter)

run_target('clang-format',
command : converter)
clangf = run_target('clang-format',
command : [converter, files('.clang-format'), meson.current_build_dir() / 'clang-format'])
custom_target('clang-tidy',
input: '.clang-tidy',
output: 'clang-tidy',
command : [converter, '@INPUT@', '@OUTPUT@'])
alias_target('clang-format-check', clangf)

# Check we can pass env to the program. Also check some string substitutions
# that were added in 0.57.0 but not documented. This is documented behaviour
Expand Down

0 comments on commit 9e8a3b9

Please sign in to comment.