aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/cmdline
diff options
context:
space:
mode:
authorGravatar dannark <dannark@google.com>2018-06-12 09:28:45 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-06-12 09:30:48 -0700
commitf4b9ff40b8f1612571cc711560177af240d034d0 (patch)
tree2b2bf5415d7b4a0ab5db78d916cebfec0775f810 /src/main/java/com/google/devtools/build/lib/cmdline
parent1b94e64ee8a1d385e92a8d9f4c9a06e2d99c82bf (diff)
Remap repository names inside load statements in BUILD files if the repository name is remapped.
For example if main/WORKSPACE contains: local_repository( name = "a", path = "../a", repo_mapping = {"@x" : "@y"}, ) a/BUILD load("@x//:sample.bzl", "sample") Then the load in a/BUILD will be resolved as "@y//:sample.bzl" RELNOTES: None PiperOrigin-RevId: 200227431
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/cmdline')
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/Label.java40
1 files changed, 39 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 61805ca5ae..9d14e6d7d8 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
@@ -15,6 +15,7 @@ package com.google.devtools.build.lib.cmdline;
import com.google.common.base.Preconditions;
import com.google.common.collect.ComparisonChain;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Interner;
import com.google.devtools.build.lib.actions.CommandLineItem;
@@ -115,8 +116,36 @@ public final class Label
*
* @param defaultToMain Treat labels in the default repository as being in the main one instead.
*/
+ @Deprecated
+ // TODO(dannark): Remove usages of this method, use other parseAbsolute() instead
public static Label parseAbsolute(String absName, boolean defaultToMain)
throws LabelSyntaxException {
+ return parseAbsolute(absName, defaultToMain, /* repositoryMapping= */ ImmutableMap.of());
+ }
+
+ /**
+ * Factory for Labels from absolute string form. e.g.
+ *
+ * <pre>
+ * //foo/bar
+ * //foo/bar:quux
+ * {@literal @}foo
+ * {@literal @}foo//bar
+ * {@literal @}foo//bar:baz
+ * </pre>
+ *
+ * <p>Labels that begin with a repository name may have the repository name remapped to a
+ * different name if it appears in {@code repositoryMapping}. This happens if the current
+ * repository being evaluated is external to the main repository and the main repository set the
+ * {@code repo_mapping} attribute when declaring this repository.
+ *
+ * @param defaultToMain Treat labels in the default repository as being in the main one instead.
+ */
+ public static Label parseAbsolute(
+ String absName,
+ boolean defaultToMain,
+ ImmutableMap<RepositoryName, RepositoryName> repositoryMapping)
+ throws LabelSyntaxException {
String repo = defaultToMain ? "@" : RepositoryName.DEFAULT_REPOSITORY;
int packageStartPos = absName.indexOf("//");
if (packageStartPos > 0) {
@@ -134,12 +163,21 @@ public final class Label
if (repo.isEmpty() && ABSOLUTE_PACKAGE_NAMES.contains(packageFragment)) {
repo = "@";
}
- return create(PackageIdentifier.create(repo, packageFragment), labelParts.getTargetName());
+ RepositoryName globalRepoName = getGlobalRepoName(repo, repositoryMapping);
+ return create(
+ PackageIdentifier.create(globalRepoName, packageFragment), labelParts.getTargetName());
} catch (BadLabelException e) {
throw new LabelSyntaxException(e.getMessage());
}
}
+ private static RepositoryName getGlobalRepoName(
+ String repo, ImmutableMap<RepositoryName, RepositoryName> repositoryMapping)
+ throws LabelSyntaxException {
+ RepositoryName repoName = RepositoryName.create(repo);
+ return repositoryMapping.getOrDefault(repoName, repoName);
+ }
+
/**
* Alternate factory method for Labels from absolute strings. This is a convenience method for
* cases when a Label needs to be initialized statically, so the declared exception is