aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
diff options
context:
space:
mode:
authorGravatar Carmi Grushko <carmi@google.com>2017-02-01 23:48:13 +0000
committerGravatar Yun Peng <pcloudy@google.com>2017-02-02 10:13:47 +0000
commit094fb73adaa37a28e4d41396de4bfb5f300660a5 (patch)
tree9cedbd55f85957bc3b9c81890914a073ff31b1ec /src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
parentec6c613768e675d1e14d76a2fdcaa2e522add1c9 (diff)
proto_library: saner descriptor sets
1. proto_library exposes a direct descriptor set (built from its 'srcs') and a nested set of transitive descriptor (from all of its dependencies). 2. Alias libraries (=no 'srcs') produce empty files as their descriptor sets. 3. The direct descriptor set depends on the transitive ones, ensuring that building a top-most proto validates all of its dependencies are also valid protos. 4. The wire format of protos allows to concatenate the outputs to get a valid serialized proto that contains all of the descriptor sets in the proto tree. RELNOTES: proto_library: alias libraries produce empty files for descriptor sets. -- PiperOrigin-RevId: 146300520 MOS_MIGRATED_REVID=146300520
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java78
1 files changed, 62 insertions, 16 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
index f6b515f912..060fa0894c 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
@@ -35,11 +35,13 @@ import com.google.devtools.build.lib.analysis.MakeVariableExpander;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
+import com.google.devtools.build.lib.analysis.actions.FileWriteAction;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.util.LazyString;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@@ -371,21 +373,37 @@ public class ProtoCompileActionBuilder {
public static void writeDescriptorSet(
RuleContext ruleContext,
final CharSequence outReplacement,
- Iterable<Artifact> protosToCompile,
+ Collection<Artifact> protosToCompile,
NestedSet<Artifact> transitiveSources,
NestedSet<Artifact> protosInDirectDeps,
- Iterable<Artifact> outputs,
- boolean allowServices) {
- registerActions(
- ruleContext,
- ImmutableList.of(createDescriptorSetToolchain(outReplacement)),
- protosToCompile,
- transitiveSources,
- protosInDirectDeps,
- ruleContext.getLabel().getCanonicalForm(),
- outputs,
- "Descriptor Set",
- allowServices);
+ Artifact output,
+ boolean allowServices,
+ NestedSet<Artifact> transitiveDescriptorSets) {
+ if (protosToCompile.isEmpty()) {
+ ruleContext.registerAction(
+ FileWriteAction.createEmptyWithInputs(
+ ruleContext.getActionOwner(), transitiveDescriptorSets, output));
+ return;
+ }
+
+ SpawnAction.Builder actions =
+ createActions(
+ ruleContext,
+ ImmutableList.of(createDescriptorSetToolchain(outReplacement)),
+ protosToCompile,
+ transitiveSources,
+ protosInDirectDeps,
+ ruleContext.getLabel().getCanonicalForm(),
+ ImmutableList.of(output),
+ "Descriptor Set",
+ allowServices);
+ if (actions == null) {
+ return;
+ }
+
+ actions.setMnemonic("GenProtoDescriptorSet");
+ actions.addTransitiveInputs(transitiveDescriptorSets);
+ ruleContext.registerAction(actions.build(ruleContext));
}
private static ToolchainInvocation createDescriptorSetToolchain(CharSequence outReplacement) {
@@ -421,8 +439,36 @@ public class ProtoCompileActionBuilder {
Iterable<Artifact> outputs,
String flavorName,
boolean allowServices) {
+ SpawnAction.Builder actions =
+ createActions(
+ ruleContext,
+ toolchainInvocations,
+ protosToCompile,
+ transitiveSources,
+ protosInDirectDeps,
+ ruleLabel,
+ outputs,
+ flavorName,
+ allowServices);
+ if (actions != null) {
+ ruleContext.registerAction(actions.build(ruleContext));
+ }
+ }
+
+ @Nullable
+ private static SpawnAction.Builder createActions(
+ RuleContext ruleContext,
+ List<ToolchainInvocation> toolchainInvocations,
+ Iterable<Artifact> protosToCompile,
+ NestedSet<Artifact> transitiveSources,
+ @Nullable NestedSet<Artifact> protosInDirectDeps,
+ String ruleLabel,
+ Iterable<Artifact> outputs,
+ String flavorName,
+ boolean allowServices) {
+
if (isEmpty(outputs)) {
- return;
+ return null;
}
SpawnAction.Builder result = new SpawnAction.Builder().addTransitiveInputs(transitiveSources);
@@ -437,7 +483,7 @@ public class ProtoCompileActionBuilder {
FilesToRunProvider compilerTarget =
ruleContext.getExecutablePrerequisite(":proto_compiler", RuleConfiguredTarget.Mode.HOST);
if (compilerTarget == null) {
- return;
+ return null;
}
result
@@ -458,7 +504,7 @@ public class ProtoCompileActionBuilder {
.setProgressMessage("Generating " + flavorName + " proto_library " + ruleContext.getLabel())
.setMnemonic(MNEMONIC);
- ruleContext.registerAction(result.build(ruleContext));
+ return result;
}
/**