aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
diff options
context:
space:
mode:
authorGravatar dannark <dannark@google.com>2018-06-21 11:55:44 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-06-21 11:57:27 -0700
commitf24479d495df540bae634b5ff2dde3177585c2a3 (patch)
treeab16e9bf0c46f1f2b1ad70d6502003de553abd81 /src/main/java/com/google/devtools/build/lib/cmdline/Label.java
parent9594d5e8dc465bb4526a2f15a4b547de7e1324a7 (diff)
Take into account repository mapping when processing labels inside BUILD files within external repositories.
For example: a/BUILD genrule( name = "a", srcs = ["@x//:x.txt"], outs = ["result.txt"], cmd = "echo hello > \$(location result.txt)" ) If the main workspace file references that repository with a rule: local_repository( name = "other_repo", path = "../a", repo_mapping = {"@x" : "@y"} ) Then when a/BUILD is evaluated, the string "@x//:x.txt" will be turned into a Label "@y//:x.txt" RELNOTES: None PiperOrigin-RevId: 201562148
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/cmdline/Label.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/Label.java22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
index 6f238429eb..91106b272c 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
@@ -533,12 +533,32 @@ public final class Label
}
)
public Label getRelative(String relName) throws LabelSyntaxException {
+ return getRelativeWithRemapping(relName, /* repositoryMapping= */ ImmutableMap.of());
+ }
+
+ /**
+ * Resolves a relative or absolute label name. If given name is absolute, then this method calls
+ * {@link #parseAbsolute}. Otherwise, it calls {@link #getLocalTargetLabel}.
+ *
+ * <p>For example: {@code :quux} relative to {@code //foo/bar:baz} is {@code //foo/bar:quux};
+ * {@code //wiz:quux} relative to {@code //foo/bar:baz} is {@code //wiz:quux};
+ * {@code @repo//foo:bar} relative to anything will be {@code @repo//foo:bar} if {@code @repo} is
+ * not in {@code repositoryMapping} but will be {@code @other_repo//foo:bar} if there is an entry
+ * {@code @repo -> @other_repo} in {@code repositoryMapping}
+ *
+ * @param relName the relative label name; must be non-empty
+ * @param repositoryMapping the map of local repository names in external repository to global
+ * repository names in main repo; can be empty, but not null
+ */
+ public Label getRelativeWithRemapping(
+ String relName, ImmutableMap<RepositoryName, RepositoryName> repositoryMapping)
+ throws LabelSyntaxException {
if (relName.length() == 0) {
throw new LabelSyntaxException("empty package-relative label");
}
if (LabelValidator.isAbsolute(relName)) {
- return resolveRepositoryRelative(parseAbsolute(relName, false));
+ return resolveRepositoryRelative(parseAbsolute(relName, false, repositoryMapping));
} else if (relName.equals(":")) {
throw new LabelSyntaxException("':' is not a valid package-relative label");
} else if (relName.charAt(0) == ':') {