aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar John Cater <jcater@google.com>2017-01-31 18:50:14 +0000
committerGravatar Yun Peng <pcloudy@google.com>2017-02-01 08:55:13 +0000
commitecf3197a225b14cbc62ec2e043ffa6889918cf86 (patch)
tree4820b606945177f37f74fc04d2208e427f20a967 /src
parent2f6fede1828b30eae787fee02d6c92ca44d49cb7 (diff)
--
PiperOrigin-RevId: 146138214 MOS_MIGRATED_REVID=146138214
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleAction.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java146
2 files changed, 89 insertions, 59 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleAction.java b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleAction.java
index a45b382f49..55b4e60192 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleAction.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleAction.java
@@ -46,7 +46,7 @@ public class GenRuleAction extends SpawnAction {
ImmutableMap<String, String> environment,
ImmutableSet<String> clientEnvironmentVariables,
ImmutableMap<String, String> executionInfo,
- RunfilesSupplier runfilesSupplier,
+ RunfilesSupplier runfilesSupplier,
String progressMessage) {
super(
owner,
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 932d58040e..3da9cc2048 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
@@ -241,68 +241,98 @@ public abstract class GenRuleBase implements RuleConfiguredTargetFactory {
return ruleContext.expandMakeVariables(
"cmd",
command,
- new ConfigurationMakeVariableContext(
- ruleContext.getRule().getPackage(), ruleContext.getConfiguration(),
- ToolchainProvider.getToolchainMakeVariables(ruleContext, "toolchains")) {
- @Override
- public String lookupMakeVariable(String name) throws ExpansionException {
- if (name.equals("SRCS")) {
- return Artifact.joinExecPaths(" ", resolvedSrcs);
- } else if (name.equals("<")) {
- return expandSingletonArtifact(resolvedSrcs, "$<", "input file");
- } else if (name.equals("OUTS")) {
- return Artifact.joinExecPaths(" ", filesToBuild);
- } else if (name.equals("@")) {
- return expandSingletonArtifact(filesToBuild, "$@", "output file");
- } else if (name.equals("@D")) {
- // The output directory. If there is only one filename in outs,
- // this expands to the directory containing that file. If there are
- // multiple filenames, this variable instead expands to the
- // package's root directory in the genfiles tree, even if all the
- // generated files belong to the same subdirectory!
- if (Iterables.size(filesToBuild) == 1) {
- Artifact outputFile = Iterables.getOnlyElement(filesToBuild);
- PathFragment relativeOutputFile = outputFile.getExecPath();
- if (relativeOutputFile.segmentCount() <= 1) {
- // This should never happen, since the path should contain at
- // least a package name and a file name.
- throw new IllegalStateException(
- "$(@D) for genrule " + ruleContext.getLabel() + " has less than one segment");
- }
- return relativeOutputFile.getParentDirectory().getPathString();
- } else {
- PathFragment dir;
- if (ruleContext.getRule().hasBinaryOutput()) {
- dir = ruleContext.getConfiguration().getBinFragment();
- } else {
- dir = ruleContext.getConfiguration().getGenfilesFragment();
- }
- PathFragment relPath =
- ruleContext.getRule().getLabel().getPackageIdentifier().getSourceRoot();
- return dir.getRelative(relPath).getPathString();
- }
- } else {
- return super.lookupMakeVariable(name);
- }
- }
- });
+ createCommandResolverContext(ruleContext, resolvedSrcs, filesToBuild));
+ }
+
+ /**
+ * Creates a new {@link CommandResolverContext} instance to use in {@link #resolveCommand}.
+ */
+ protected CommandResolverContext createCommandResolverContext(RuleContext ruleContext,
+ NestedSet<Artifact> resolvedSrcs, NestedSet<Artifact> filesToBuild) {
+ return new CommandResolverContext(ruleContext, resolvedSrcs, filesToBuild);
}
/**
- * Returns the path of the sole element "artifacts", generating an exception with an informative
- * error message iff the set is not a singleton. Used to expand "$<", "$@".
+ * Implementation of {@link ConfigurationMakeVariableContext} used to expand variables in a
+ * genrule command string.
*/
- private String expandSingletonArtifact(Iterable<Artifact> artifacts,
- String variable,
- String artifactName)
- throws ExpansionException {
- if (Iterables.isEmpty(artifacts)) {
- throw new ExpansionException("variable '" + variable
- + "' : no " + artifactName);
- } else if (Iterables.size(artifacts) > 1) {
- throw new ExpansionException("variable '" + variable
- + "' : more than one " + artifactName);
+ protected static class CommandResolverContext extends ConfigurationMakeVariableContext {
+
+ private final RuleContext ruleContext;
+ private final NestedSet<Artifact> resolvedSrcs;
+ private final NestedSet<Artifact> filesToBuild;
+
+ public CommandResolverContext(RuleContext ruleContext, NestedSet<Artifact> resolvedSrcs,
+ NestedSet<Artifact> filesToBuild) {
+ super(ruleContext.getRule().getPackage(), ruleContext.getConfiguration(),
+ ToolchainProvider.getToolchainMakeVariables(ruleContext, "toolchains"));
+ this.ruleContext = ruleContext;
+ this.resolvedSrcs = resolvedSrcs;
+ this.filesToBuild = filesToBuild;
+ }
+
+ public RuleContext getRuleContext() {
+ return ruleContext;
+ }
+
+ @Override
+ public String lookupMakeVariable(String name) throws ExpansionException {
+ if (name.equals("SRCS")) {
+ return Artifact.joinExecPaths(" ", resolvedSrcs);
+ } else if (name.equals("<")) {
+ return expandSingletonArtifact(resolvedSrcs, "$<", "input file");
+ } else if (name.equals("OUTS")) {
+ return Artifact.joinExecPaths(" ", filesToBuild);
+ } else if (name.equals("@")) {
+ return expandSingletonArtifact(filesToBuild, "$@", "output file");
+ } else if (name.equals("@D")) {
+ // The output directory. If there is only one filename in outs,
+ // this expands to the directory containing that file. If there are
+ // multiple filenames, this variable instead expands to the
+ // package's root directory in the genfiles tree, even if all the
+ // generated files belong to the same subdirectory!
+ if (Iterables.size(filesToBuild) == 1) {
+ Artifact outputFile = Iterables.getOnlyElement(filesToBuild);
+ PathFragment relativeOutputFile = outputFile.getExecPath();
+ if (relativeOutputFile.segmentCount() <= 1) {
+ // This should never happen, since the path should contain at
+ // least a package name and a file name.
+ throw new IllegalStateException(
+ "$(@D) for genrule " + ruleContext.getLabel() + " has less than one segment");
+ }
+ return relativeOutputFile.getParentDirectory().getPathString();
+ } else {
+ PathFragment dir;
+ if (ruleContext.getRule().hasBinaryOutput()) {
+ dir = ruleContext.getConfiguration().getBinFragment();
+ } else {
+ dir = ruleContext.getConfiguration().getGenfilesFragment();
+ }
+ PathFragment relPath =
+ ruleContext.getRule().getLabel().getPackageIdentifier().getSourceRoot();
+ return dir.getRelative(relPath).getPathString();
+ }
+ } else {
+ return super.lookupMakeVariable(name);
+ }
+ }
+
+ /**
+ * Returns the path of the sole element "artifacts", generating an exception with an informative
+ * error message iff the set is not a singleton. Used to expand "$<", "$@".
+ */
+ private final String expandSingletonArtifact(Iterable<Artifact> artifacts,
+ String variable,
+ String artifactName)
+ throws ExpansionException {
+ if (Iterables.isEmpty(artifacts)) {
+ throw new ExpansionException("variable '" + variable
+ + "' : no " + artifactName);
+ } else if (Iterables.size(artifacts) > 1) {
+ throw new ExpansionException("variable '" + variable
+ + "' : more than one " + artifactName);
+ }
+ return Iterables.getOnlyElement(artifacts).getExecPathString();
}
- return Iterables.getOnlyElement(artifacts).getExecPathString();
}
}