aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java
diff options
context:
space:
mode:
authorGravatar dannark <dannark@google.com>2018-06-04 13:53:22 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-06-04 13:54:52 -0700
commitc3495858f097451e06044778bc806e94e06d6da7 (patch)
tree6350516aaddd9bf93643c86d936a92fc9256d385 /src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java
parentba25d49ce2c6bb86e93e19cfe9bb8da1399a3c43 (diff)
Process 'repo_mapping' attribute from WORKSPACE rules. 'repo_mapping' is a way to remap references to repositories within an external repository by another name. This CL only adds the mappings to the Package object, but it does not actually enable the reassignments.
Example usage (in a WORKSPACE file): local_repository( name = ?a?, path = ?../a?, repo_mapping = {?@x? : ?@y?} ) This change also creates a new SkyKey which represents the mappings. This is to prevent all packages from depending on the external package, and instead depending just on the mappings. i.e. a change to the WORKSPACE file that does not touch the mappings shouldn't cause a reload of the package. RELNOTES:None PiperOrigin-RevId: 199187963
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java35
1 files changed, 32 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java b/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java
index b9d86ad461..132c511c04 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactory.java
@@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.LabelValidator;
+import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.events.NullEventHandler;
@@ -55,6 +56,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import java.util.stream.Collectors;
import javax.annotation.Nullable;
/**
@@ -278,6 +280,7 @@ public class WorkspaceFactory {
}
builder.addRegisteredExecutionPlatforms(aPackage.getRegisteredExecutionPlatforms());
builder.addRegisteredToolchains(aPackage.getRegisteredToolchains());
+ builder.addRepositoryMappings(aPackage);
for (Rule rule : aPackage.getTargets(Rule.class)) {
try {
// The old rule references another Package instance and we wan't to keep the invariant that
@@ -466,8 +469,8 @@ public class WorkspaceFactory {
};
/**
- * Returns a function-value implementing the build rule "ruleClass" (e.g. cc_library) in the
- * specified package context.
+ * Returns a function-value implementing the build or workspace rule "ruleClass" (e.g. cc_library)
+ * in the specified package context.
*/
private static BuiltinFunction newRuleFunction(
final RuleFactory ruleFactory, final String ruleClassName, final boolean allowOverride) {
@@ -487,11 +490,29 @@ public class WorkspaceFactory {
+ kwargs.get("name")
+ "')");
}
+ if (kwargs.containsKey("repo_mapping")) {
+ if (!(kwargs.get("repo_mapping") instanceof Map)) {
+ throw new EvalException(
+ ast.getLocation(),
+ "Invalid value for 'repo_mapping': '" + kwargs.get("repo_mapping")
+ + "'. Value must be a map."
+ );
+ }
+ @SuppressWarnings("unchecked")
+ Map<String, String> map = (Map<String, String>) kwargs.get("repo_mapping");
+ String externalRepoName = (String) kwargs.get("name");
+ for (Map.Entry<String, String> e : map.entrySet()) {
+ builder.addRepositoryMappingEntry(
+ RepositoryName.createFromValidStrippedName(externalRepoName),
+ RepositoryName.create((String) e.getKey()),
+ RepositoryName.create((String) e.getValue()));
+ }
+ }
RuleClass ruleClass = ruleFactory.getRuleClass(ruleClassName);
RuleClass bindRuleClass = ruleFactory.getRuleClass("bind");
Rule rule =
WorkspaceFactoryHelper.createAndAddRepositoryRule(
- builder, ruleClass, bindRuleClass, kwargs, ast);
+ builder, ruleClass, bindRuleClass, getFinalKwargs(kwargs), ast);
if (!isLegalWorkspaceName(rule.getName())) {
throw new EvalException(
ast.getLocation(), rule + "'s name field must be a legal workspace name");
@@ -506,6 +527,14 @@ public class WorkspaceFactory {
};
}
+ private static Map<String, Object> getFinalKwargs(Map<String, Object> kwargs) {
+ // 'repo_mapping' is not an explicit attribute of any rule and so it would
+ // result in a rule error if propagated to the rule factory.
+ return kwargs.entrySet().stream()
+ .filter(x -> !x.getKey().equals("repo_mapping"))
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ }
+
private static ImmutableMap<String, BaseFunction> createWorkspaceFunctions(
boolean allowOverride, RuleFactory ruleFactory) {
Map<String, BaseFunction> map = new HashMap<>();