aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
diff options
context:
space:
mode:
authorGravatar elenairina <elenairina@google.com>2018-02-20 06:26:33 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-02-20 06:27:57 -0800
commit5deca4cf88f5568771f2c836a9b8c693b88bd749 (patch)
tree1ab038e4bf10301e0e8878c21e9693091b78e304 /src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
parent9ac10696da052d1327b3f1cd276b2ab50fe1fee1 (diff)
Accept proto paths relative to proto_source_root as direct dependencies.
This will make protoc see as direct dependencies the .proto files that were included using the proto_source_root flag. Until now, Bazel passed to protoc the direct dependencies of a target as the path relative to the WORKSPACE, which made it fail when a shorter path, relative to the package was used. Progress on #4544. RELNOTES: None. PiperOrigin-RevId: 186294997
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java55
1 files changed, 46 insertions, 9 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
index 374b534cd9..2b6f88e2e6 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
@@ -100,29 +100,66 @@ public class ProtoCommon {
}
/**
- * 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 ({@code currentProtoSourceRoot}) and in its
+ * transitive dependencies, each prefixed by {@code --proto_path}.
*
- * Build will fail if the {@code proto_source_root} of the current lib is different than the
- * package name.
+ * Assumes {@code currentProtoSourceRoot} is the same as the package name.
*/
- public static NestedSet<String> collectTransitiveProtoPathFlags(RuleContext ruleContext) {
+ public static NestedSet<String> collectTransitiveProtoPathFlags(
+ RuleContext ruleContext, String currentProtoSourceRoot) {
NestedSetBuilder<String> protoPathFlags = NestedSetBuilder.stableOrder();
// first add the protoSourceRoot of the current target, if any
+ if (currentProtoSourceRoot != null && !currentProtoSourceRoot.isEmpty()) {
+ protoPathFlags.add("--proto_path=" + currentProtoSourceRoot);
+ }
+
+ for (ProtoSourcesProvider provider : ruleContext.getPrerequisites(
+ "deps", Mode.TARGET, ProtoSourcesProvider.class)) {
+ protoPathFlags.addTransitive(provider.getTransitiveProtoPathFlags());
+ }
+
+ return protoPathFlags.build();
+ }
+
+ /**
+ * Returns the {@code proto_source_root} of the current library or null if none is specified.
+ *
+ * Build will fail if the {@code proto_source_root} of the current library is different than the
+ * package name.
+ */
+ @Nullable
+ public static String getProtoSourceRoot(RuleContext ruleContext) {
String protoSourceRoot =
ruleContext.attributes().get("proto_source_root", Type.STRING);
if (protoSourceRoot != null && !protoSourceRoot.isEmpty()) {
checkProtoSourceRootIsTheSameAsPackage(protoSourceRoot, ruleContext);
- protoPathFlags.add("--proto_path=" + protoSourceRoot);
+ }
+ return protoSourceRoot;
+ }
+
+ /**
+ * Returns a set of the {@code proto_source_root} collected from the current library and the
+ * direct dependencies.
+ *
+ * Assumes {@code currentProtoSourceRoot} is the same as the package name.
+ */
+ public static NestedSet<String> getProtoSourceRootsOfDirectDependencies(
+ RuleContext ruleContext, String currentProtoSourceRoot) {
+ NestedSetBuilder<String> protoSourceRoots = NestedSetBuilder.stableOrder();
+ if (currentProtoSourceRoot != null && !currentProtoSourceRoot.isEmpty()) {
+ protoSourceRoots.add(currentProtoSourceRoot);
}
for (ProtoSourcesProvider provider : ruleContext.getPrerequisites(
- "deps", Mode.TARGET, ProtoSourcesProvider.class)) {
- protoPathFlags.addTransitive(provider.getTransitiveProtoPathFlags());
+ "deps", Mode.TARGET, ProtoSourcesProvider.class)) {
+ String protoSourceRoot = provider.getProtoSourceRoot();
+ if (protoSourceRoot != null && !protoSourceRoot.isEmpty()) {
+ protoSourceRoots.add(provider.getProtoSourceRoot());
+ }
}
- return protoPathFlags.build();
+ return protoSourceRoots.build();
}
private static void checkProtoSourceRootIsTheSameAsPackage(