ninja backend: fix cleandead deleting files that meson implicitly creates

Specifically, when those files can be created by a build rule with one
version of meson.build, and created as e.g. a shared_library alias
symlink in another version of meson.build, the cleandead command will
delete important files just because they don't happen to be created by
ninja itself.

Work around this by making a dummy rule that exists solely to insert the
files into the build graph to trick ninja into not deleting them.

Closes #11861
pull/11863/head
Eli Schwartz 1 year ago
parent f2f42318ed
commit 82d0d1755d
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 16
      mesonbuild/backend/ninjabackend.py

@ -488,6 +488,7 @@ class NinjaBackend(backends.Backend):
self.introspection_data = {}
self.created_llvm_ir_rule = PerMachine(False, False)
self.rust_crates: T.Dict[str, RustCrate] = {}
self.implicit_meson_outs = []
def create_phony_target(self, all_outputs, dummy_outfile, rulename, phony_infilename, implicit_outs=None):
'''
@ -3439,17 +3440,20 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
def generate_shlib_aliases(self, target, outdir):
for alias, to, tag in target.get_aliases():
aliasfile = os.path.join(self.environment.get_build_dir(), outdir, alias)
aliasfile = os.path.join(outdir, alias)
abs_aliasfile = os.path.join(self.environment.get_build_dir(), outdir, alias)
try:
os.remove(aliasfile)
os.remove(abs_aliasfile)
except Exception:
pass
try:
os.symlink(to, aliasfile)
os.symlink(to, abs_aliasfile)
except NotImplementedError:
mlog.debug("Library versioning disabled because symlinks are not supported.")
except OSError:
mlog.debug("Library versioning disabled because we do not have symlink creation privileges.")
else:
self.implicit_meson_outs.append(aliasfile)
def generate_custom_target_clean(self, trees: T.List[str]) -> str:
e = self.create_phony_target(self.all_outputs, 'clean-ctlist', 'CUSTOM_COMMAND', 'PHONY')
@ -3611,6 +3615,12 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
elem.add_item('pool', 'console')
self.add_build(elem)
# If these files used to be explicitly created, they need to appear on the build graph somehow,
# otherwise cleandead deletes them. See https://github.com/ninja-build/ninja/issues/2299
if self.implicit_meson_outs:
elem = NinjaBuildElement(self.all_outputs, 'meson-implicit-outs', 'phony', self.implicit_meson_outs)
self.add_build(elem)
elem = NinjaBuildElement(self.all_outputs, 'reconfigure', 'REGENERATE_BUILD', 'PHONY')
elem.add_item('pool', 'console')
self.add_build(elem)

Loading…
Cancel
Save