From 50cb6a49ad984e72b68a6031086db42d35bd05ec Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Wed, 1 Nov 2023 21:49:42 -0700 Subject: [PATCH] Expose repo mapping manifest to Starlark The new `repo_mapping_manifest` field on `FilesToRunProvider` allows Starlark rule implementations to access the `File` containing the repo mapping manifest for an executable target, e.g. to include the manifest into a synthetic runfiles structure for packaging purposes. Fixes #19937 Closes #19944. PiperOrigin-RevId: 578735848 Change-Id: Ida78778af5aef4ba0c216815563aef4b54c9d1db --- .../lib/analysis/FilesToRunProvider.java | 7 +++++ .../FilesToRunProviderApi.java | 8 ++++++ .../RunfilesRepoMappingManifestTest.java | 26 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/FilesToRunProvider.java b/src/main/java/com/google/devtools/build/lib/analysis/FilesToRunProvider.java index ee5a12ae32a568..f07c201aede21a 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/FilesToRunProvider.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/FilesToRunProvider.java @@ -95,6 +95,13 @@ public final Artifact getRunfilesManifest() { return runfilesSupport != null ? runfilesSupport.getRunfilesManifest() : null; } + @Nullable + @Override + public Artifact getRepoMappingManifest() { + var runfilesSupport = getRunfilesSupport(); + return runfilesSupport != null ? runfilesSupport.getRepoMappingManifest() : null; + } + /** Returns a {@link RunfilesSupplier} encapsulating runfiles for this tool. */ public final RunfilesSupplier getRunfilesSupplier() { return firstNonNull(getRunfilesSupport(), EmptyRunfilesSupplier.INSTANCE); diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/FilesToRunProviderApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/FilesToRunProviderApi.java index 7dab417d98e9e4..5c0006bf053905 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/FilesToRunProviderApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/FilesToRunProviderApi.java @@ -39,4 +39,12 @@ public interface FilesToRunProviderApi extends StarlarkVa allowReturnNones = true) @Nullable FileT getRunfilesManifest(); + + @StarlarkMethod( + name = "repo_mapping_manifest", + doc = "The repo mapping manifest or None if it does not exist.", + structField = true, + allowReturnNones = true) + @Nullable + FileT getRepoMappingManifest(); } diff --git a/src/test/java/com/google/devtools/build/lib/analysis/RunfilesRepoMappingManifestTest.java b/src/test/java/com/google/devtools/build/lib/analysis/RunfilesRepoMappingManifestTest.java index e7949ebee49215..21cd1832c3ba50 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/RunfilesRepoMappingManifestTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/RunfilesRepoMappingManifestTest.java @@ -391,6 +391,32 @@ public void hasMappingForSymlinks() throws Exception { .inOrder(); } + @Test + public void repoMappingOnFilesToRunProvider() throws Exception { + scratch.overwriteFile("MODULE.bazel", "bazel_dep(name='bare_rule',version='1.0')"); + scratch.overwriteFile( + "defs.bzl", + "def _get_repo_mapping_impl(ctx):", + " files_to_run = ctx.attr.bin[DefaultInfo].files_to_run", + " return [", + " DefaultInfo(files = depset([files_to_run.repo_mapping_manifest])),", + " ]", + "get_repo_mapping = rule(", + " implementation = _get_repo_mapping_impl,", + " attrs = {'bin':attr.label(cfg='target',executable=True)}", + ")"); + scratch.overwriteFile( + "BUILD", + "load('@bare_rule//:defs.bzl', 'bare_binary')", + "load('//:defs.bzl', 'get_repo_mapping')", + "bare_binary(name='aaa')", + "get_repo_mapping(name='get_repo_mapping', bin=':aaa')"); + invalidatePackages(); + + assertThat(getFilesToBuild(getConfiguredTarget("//:get_repo_mapping")).toList()) + .containsExactly(getRunfilesSupport("//:aaa").getRepoMappingManifest()); + } + /** * Similar to {@link BuildViewTestCase#rewriteWorkspace(String...)}, but does not call {@link * BuildViewTestCase#invalidatePackages()}.