aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2018-05-29 07:41:10 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-29 07:42:14 -0700
commit7fcbc8ffdead028d19606fefa2fa3be13781da98 (patch)
tree803a2275982ce67dff4251e5a41a1c8dfd186cf8 /src/main/java/com
parent26c86f88be0ddd9d59f4fd3f3afdd769af4ecefc (diff)
shell toolchain: genrule now uses it
genrule() now uses the shell toolchain (as well as the ShellConfiguration config fragment) to look up the shell interpreter's path. Also the CommandHelper no longer looks up the shell interpreter's path itself. Instead its ctor takes the path as an argument. This allows supporting rules that already use the shell toolchain and rules that still only consider the configuration fragment. With these changes genrule() is now able to use the shell interpreter registered as a toolchain, even if there's no `--shell_executable` flag specified (or its value is empty). Subsequent commits will migrate more and more rules to depend on the shell toolchain. This commit takes us closer to: 1. be able to detect the local shell interpreter's location, especially when it's not the default /bin/bash 2. be able to define different shell toolchains (different interpreter paths) for remote builds 3. gracefully fail the build if the machine has no shell installed but the action graph included a shell action See https://github.com/bazelbuild/bazel/issues/4319 Change-Id: I0674c7e2d5917643d87b48b19a1cb43e606ad2f7 Closes #5282. Change-Id: I0674c7e2d5917643d87b48b19a1cb43e606ad2f7 PiperOrigin-RevId: 198394021
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/ShToolchain.java90
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/genrule/BazelGenRuleRule.java3
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java2
3 files changed, 91 insertions, 4 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ShToolchain.java b/src/main/java/com/google/devtools/build/lib/analysis/ShToolchain.java
index c785da410e..482ba88380 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/ShToolchain.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/ShToolchain.java
@@ -14,12 +14,40 @@
package com.google.devtools.build.lib.analysis;
+import static com.google.devtools.build.lib.packages.Attribute.attr;
+import static com.google.devtools.build.lib.packages.BuildType.LABEL;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.devtools.build.lib.analysis.ToolchainContext.ResolvedToolchainProviders;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.packages.Attribute;
+import com.google.devtools.build.lib.packages.RuleClass;
+import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.vfs.PathFragment;
/** Class to work with the shell toolchain, e.g. get the shell interpreter's path. */
public final class ShToolchain {
+ private static final String TOOLCHAIN_TYPE_ATTR = "$sh_toolchain_type";
+ private static final String TOOLCHAIN_TYPE_LABEL = "//tools/sh:toolchain_type";
+ private static final String TOOLCHAIN_TYPE_PACKAGE = "tools/sh";
+
+ public static RuleClass.Builder addDependency(
+ RuleClass.Builder builder, RuleDefinitionEnvironment env) {
+ return addDependency(builder, env.getToolsLabel(TOOLCHAIN_TYPE_LABEL));
+ }
+
+ private static RuleClass.Builder addDependency(RuleClass.Builder builder, Label toolchainType) {
+ return builder.addRequiredToolchains(toolchainType).add(createAttribute(toolchainType));
+ }
+
+ private static Attribute.Builder createAttribute(Label toolchainType) {
+ return attr(TOOLCHAIN_TYPE_ATTR, LABEL).value(toolchainType);
+ }
+
/**
* Returns the shell executable's path, or an empty path if not set.
*
@@ -43,9 +71,11 @@ public final class ShToolchain {
/**
* Returns the shell executable's path, or reports a rule error if the path is empty.
*
- * <p>This method checks the rule's configuration's {@link ShellConfiguration} fragment for the
- * shell executable's path. If null or empty, the method reports an error against the rule.
+ * <p>DO NOT USE THIS METHOD, use {@link #getToolchainPathOrError} instead. This method exists to
+ * allow incremental migration to {@link #getToolchainPathOrError}.
*/
+ // TODO(laszlocsomor): update every rule that calls getPathOrError to depend on the shell
+ // toolchain, and change their use of getPathOrError to getToolchainPathOrError.
public static PathFragment getPathOrError(RuleContext ctx) {
PathFragment result = getPath(ctx.getConfiguration());
@@ -58,5 +88,61 @@ public final class ShToolchain {
return result;
}
+ /**
+ * Returns the shell executable's path, or reports a rule error if the path is empty.
+ *
+ * <p>This method checks the rule's configuration's {@link ShellConfiguration} fragment for the
+ * shell executable's path. If null or empty, this method gets the path from the selected shell
+ * toolchain. If the path is still null or empty, the method reports an error against the rule.
+ *
+ * @throws IllegalArgumentException if the rule does not depend on the shell toolchain
+ */
+ public static PathFragment getToolchainPathOrError(RuleContext ctx) {
+ // TODO(laszlocsomor): update every rule that calls getPathOrError to depend on the shell
+ // toolchain, and change their use of getPathOrError to getToolchainPathOrError.
+ Preconditions.checkState(ctx.attributes().has(TOOLCHAIN_TYPE_ATTR, LABEL));
+
+ PathFragment result = getPath(ctx.getConfiguration());
+
+ if (result.isEmpty()) {
+ ResolvedToolchainProviders toolchains =
+ (ResolvedToolchainProviders) ctx.getToolchainContext().getResolvedToolchainProviders();
+
+ ToolchainInfo activeToolchain =
+ toolchains.getForToolchainType(ctx.attributes().get(TOOLCHAIN_TYPE_ATTR, LABEL));
+
+ if (activeToolchain != null) {
+ String path = null;
+ try {
+ path = (String) activeToolchain.getValue("path");
+ } catch (EvalException e) {
+ throw new IllegalStateException(e);
+ }
+
+ if (path != null && !path.isEmpty()) {
+ result = PathFragment.create(path);
+ }
+ }
+ }
+
+ if (result.isEmpty()) {
+ ctx.ruleError(
+ "This rule needs a shell interpreter. Use the --shell_executable=<path> flag to specify"
+ + " the interpreter's path, e.g. --shell_executable=/usr/local/bin/bash");
+ }
+
+ return result;
+ }
+
private ShToolchain() {}
+
+ @VisibleForTesting
+ public static String getToolchainTypeLabelForTesting() {
+ return TOOLCHAIN_TYPE_LABEL;
+ }
+
+ @VisibleForTesting
+ public static String getToolchainTypePackageForTesting() {
+ return TOOLCHAIN_TYPE_PACKAGE;
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/genrule/BazelGenRuleRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/genrule/BazelGenRuleRule.java
index 964cfafce1..bda085ec41 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/genrule/BazelGenRuleRule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/genrule/BazelGenRuleRule.java
@@ -20,6 +20,7 @@ import static com.google.devtools.build.lib.syntax.Type.BOOLEAN;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
+import com.google.devtools.build.lib.analysis.ShToolchain;
import com.google.devtools.build.lib.analysis.config.HostTransition;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.rules.cpp.CcToolchain;
@@ -44,7 +45,7 @@ public final class BazelGenRuleRule implements RuleDefinition {
rules. If the rule generates source files, you should use the
<code>srcs</code> attribute.
<!-- #END_BLAZE_RULE.NAME --> */
- return builder
+ return ShToolchain.addDependency(builder, env)
.setOutputToGenfiles()
.add(
attr("$genrule_setup", LABEL)
diff --git a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java
index 52efdf15de..ab57723303 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java
@@ -196,7 +196,7 @@ public abstract class GenRuleBase implements RuleConfiguredTargetFactory {
FilesToRunProvider genruleSetup =
ruleContext.getPrerequisite("$genrule_setup", Mode.HOST, FilesToRunProvider.class);
inputs.addTransitive(genruleSetup.getFilesToRun());
- PathFragment shExecutable = ShToolchain.getPathOrError(ruleContext);
+ PathFragment shExecutable = ShToolchain.getToolchainPathOrError(ruleContext);
if (ruleContext.hasErrors()) {
return null;
}