aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/ToolchainLookup.java
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2016-11-22 16:18:48 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2016-11-22 18:15:08 +0000
commit8bb7b8fa364639c09dc4a50b5c960e1867b8e283 (patch)
tree9b1bb1f83f3a7cf4612195554e4dd449247ef8cd /src/main/java/com/google/devtools/build/lib/rules/ToolchainLookup.java
parent0edd35414dbe916457c5e98d7c08e1b4cfcf8d57 (diff)
Initial checkin of the toolchain_lookup() rule for expressing explicit dependencies on Make variables and toolchains.
I'm not particularly happy with the fact that it needs to depend on every configuration fragment that defines Make variables, but this seemed more reasonable than having a rule class for every fragment, just to have a lonely single rule of that class. -- MOS_MIGRATED_REVID=139910229
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/ToolchainLookup.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/ToolchainLookup.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/ToolchainLookup.java b/src/main/java/com/google/devtools/build/lib/rules/ToolchainLookup.java
new file mode 100644
index 0000000000..a536fdc679
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/ToolchainLookup.java
@@ -0,0 +1,68 @@
+// Copyright 2016 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
+import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.Runfiles;
+import com.google.devtools.build.lib.analysis.RunfilesProvider;
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.cmdline.Label;
+import java.util.TreeMap;
+
+/**
+ * Abstract base class for {@code toolchain_lookup}.
+ */
+public class ToolchainLookup implements RuleConfiguredTargetFactory {
+ private final ImmutableMap<Label, Class<? extends BuildConfiguration.Fragment>> fragmentMap;
+ private final ImmutableMap<Label, ImmutableMap<String, String>> hardcodedVariableMap;
+
+ protected ToolchainLookup(
+ ImmutableMap<Label, Class<? extends BuildConfiguration.Fragment>> fragmentMap,
+ ImmutableMap<Label, ImmutableMap<String, String>> hardcodedVariableMap) {
+ this.fragmentMap = fragmentMap;
+ this.hardcodedVariableMap = hardcodedVariableMap;
+ }
+
+ @Override
+ public ConfiguredTarget create(RuleContext ruleContext)
+ throws InterruptedException, RuleErrorException {
+ // This cannot be an ImmutableMap.Builder because that asserts when a key is duplicated
+ TreeMap<String, String> makeVariables = new TreeMap<>();
+ Class<? extends BuildConfiguration.Fragment> fragmentClass =
+ fragmentMap.get(ruleContext.getLabel());
+ if (fragmentClass != null) {
+ BuildConfiguration.Fragment fragment = ruleContext.getFragment(fragmentClass);
+ ImmutableMap.Builder<String, String> fragmentBuilder = ImmutableMap.builder();
+ fragment.addGlobalMakeVariables(fragmentBuilder);
+ makeVariables.putAll(fragmentBuilder.build());
+ }
+
+ ImmutableMap<String, String> hardcodedVariables =
+ hardcodedVariableMap.get(ruleContext.getLabel());
+ if (hardcodedVariables != null) {
+ makeVariables.putAll(hardcodedVariables);
+ }
+ // This will eventually go to Skyframe using getAnalysisEnvironment.getSkyframeEnv() to figure
+ // out the lookup rule -> toolchain rule mapping. For now, it only provides Make variables that
+ // come from BuildConfiguration so no need to ask Skyframe.
+ return new RuleConfiguredTargetBuilder(ruleContext)
+ .addProvider(new ToolchainProvider(ImmutableMap.copyOf(makeVariables)))
+ .addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
+ .build();
+ }
+}