Skip to content

Commit

Permalink
Expose ProtoSourcesProvider.transitive_proto_path_flags to Skylark.
Browse files Browse the repository at this point in the history
Progress on #4544.

RELNOTES: None.
PiperOrigin-RevId: 187179454
  • Loading branch information
iirina authored and Copybara-Service committed Feb 27, 2018
1 parent 19c6428 commit 98ed9af
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,28 @@ public static NestedSet<Artifact> collectDependenciesDescriptorSets(RuleContext
}

/**
* Returns all proto source roots in this lib and in its transitive dependencies, each prefixed
* by {@code --proto_path}.
* Returns all proto source roots in this lib and in its transitive dependencies.
*
* Build will fail if the {@code proto_source_root} of the current lib is different than the
* package name.
*/
public static NestedSet<String> collectTransitiveProtoPathFlags(RuleContext ruleContext) {
NestedSetBuilder<String> protoPathFlags = NestedSetBuilder.stableOrder();
NestedSetBuilder<String> protoPath = NestedSetBuilder.stableOrder();

// first add the protoSourceRoot of the current target, if any
String protoSourceRoot =
ruleContext.attributes().get("proto_source_root", Type.STRING);
if (protoSourceRoot != null && !protoSourceRoot.isEmpty()) {
checkProtoSourceRootIsTheSameAsPackage(protoSourceRoot, ruleContext);
protoPathFlags.add("--proto_path=" + protoSourceRoot);
protoPath.add(protoSourceRoot);
}

for (ProtoSourcesProvider provider : ruleContext.getPrerequisites(
"deps", Mode.TARGET, ProtoSourcesProvider.class)) {
protoPathFlags.addTransitive(provider.getTransitiveProtoPathFlags());
protoPath.addTransitive(provider.getTransitiveProtoPathFlags());
}

return protoPathFlags.build();
return protoPath.build();
}

private static void checkProtoSourceRootIsTheSameAsPackage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ static CustomCommandLine createCommandLineFromToolchains(
ImmutableList<String> protocOpts) {
CustomCommandLine.Builder cmdLine = CustomCommandLine.builder();

cmdLine.addAll(transitiveProtoPathFlags);
cmdLine.addAll(
VectorArg.of(transitiveProtoPathFlags)
.mapped(ProtoCompileActionBuilder::expandTransitiveProtoPathFlags));

// A set to check if there are multiple invocations with the same name.
HashSet<String> invocationNames = new HashSet<>();
Expand Down Expand Up @@ -604,6 +606,10 @@ static void addIncludeMapArguments(
}
}

private static void expandTransitiveProtoPathFlags(String flag, Consumer<String> args) {
args.accept("--proto_path=" + flag);
}

private static void expandTransitiveImportArg(Artifact artifact, Consumer<String> args) {
args.accept("-I" + getPathIgnoringRepository(artifact) + "=" + artifact.getExecPathString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ public static ProtoSourcesProvider create(
public abstract NestedSet<Artifact> transitiveDescriptorSets();

/**
* Directories of .proto sources collected from the transitive closure, each prefixed with
* {@code --proto_path}. These flags will be passed to {@code protoc} in the specified oreder.
* Directories of .proto sources collected from the transitive closure. These flags will be passed
* to {@code protoc} in the specified order, via the {@code --proto_path} flag.
*/
@SkylarkCallable(
name = "transitive_proto_path",
doc = "A set of proto source roots collected from the transitive closure of this rule.",
structField = true
)
public abstract NestedSet<String> getTransitiveProtoPathFlags();

ProtoSourcesProvider() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public static SupportData create(
public abstract NestedSet<Artifact> getProtosInDirectDeps();

/**
* Directories of .proto sources collected from the transitive closure, each prefixed with
* {@code --proto_path}. These flags will be passed to {@code protoc} in the specified oreder.
* Directories of .proto sources collected from the transitive closure. These flags will be passed
* to {@code protoc} in the specified order, via the {@code --proto_path} flag.
*/
public abstract NestedSet<String> getTransitiveProtoPathFlags();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ public void testProtoSourceRootWithoutDeps() throws Exception {
);
ConfiguredTarget protoTarget = getConfiguredTarget("//x/foo:nodeps");
ProtoSourcesProvider sourcesProvider = protoTarget.getProvider(ProtoSourcesProvider.class);
assertThat(sourcesProvider.getTransitiveProtoPathFlags()).containsExactly("--proto_path=x/foo");
assertThat(sourcesProvider.getTransitiveProtoPathFlags()).containsExactly("x/foo");

SupportData supportData =
protoTarget.getProvider(ProtoSupportDataProvider.class).getSupportData();
assertThat(supportData.getTransitiveProtoPathFlags()).containsExactly("--proto_path=x/foo");
assertThat(supportData.getTransitiveProtoPathFlags()).containsExactly("x/foo");

assertThat(getGeneratingSpawnAction(getDescriptorOutput("//x/foo:nodeps"))
.getRemainingArguments())
Expand Down Expand Up @@ -304,11 +304,11 @@ public void testProtoSourceRootWithDepsDuplicate() throws Exception {
);
ConfiguredTarget protoTarget = getConfiguredTarget("//x/foo:withdeps");
ProtoSourcesProvider sourcesProvider = protoTarget.getProvider(ProtoSourcesProvider.class);
assertThat(sourcesProvider.getTransitiveProtoPathFlags()).containsExactly("--proto_path=x/foo");
assertThat(sourcesProvider.getTransitiveProtoPathFlags()).containsExactly("x/foo");

SupportData supportData =
protoTarget.getProvider(ProtoSupportDataProvider.class).getSupportData();
assertThat(supportData.getTransitiveProtoPathFlags()).containsExactly("--proto_path=x/foo");
assertThat(supportData.getTransitiveProtoPathFlags()).containsExactly("x/foo");

assertThat(getGeneratingSpawnAction(getDescriptorOutput("//x/foo:withdeps"))
.getRemainingArguments())
Expand Down Expand Up @@ -342,12 +342,12 @@ public void testProtoSourceRootWithDeps() throws Exception {
ConfiguredTarget protoTarget = getConfiguredTarget("//x/foo:withdeps");
ProtoSourcesProvider sourcesProvider = protoTarget.getProvider(ProtoSourcesProvider.class);
assertThat(sourcesProvider.getTransitiveProtoPathFlags())
.containsExactly("--proto_path=x/foo", "--proto_path=x/bar");
.containsExactly("x/foo", "x/bar");

SupportData supportData =
protoTarget.getProvider(ProtoSupportDataProvider.class).getSupportData();
assertThat(supportData.getTransitiveProtoPathFlags())
.containsExactly("--proto_path=x/foo", "--proto_path=x/bar");
.containsExactly("x/foo", "x/bar");

assertThat(getGeneratingSpawnAction(getDescriptorOutput("//x/foo:withdeps"))
.getRemainingArguments())
Expand Down

0 comments on commit 98ed9af

Please sign in to comment.