diff options
author | elenairina <elenairina@google.com> | 2018-02-20 06:26:33 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-20 06:27:57 -0800 |
commit | 5deca4cf88f5568771f2c836a9b8c693b88bd749 (patch) | |
tree | 1ab038e4bf10301e0e8878c21e9693091b78e304 /src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java | |
parent | 9ac10696da052d1327b3f1cd276b2ab50fe1fee1 (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/ProtoCompileActionBuilder.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java | 37 |
1 files changed, 34 insertions, 3 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 374bf9b9fe..015c02c3f0 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 @@ -322,6 +322,7 @@ public class ProtoCompileActionBuilder { addIncludeMapArguments( result, areDepsStrict ? supportData.getProtosInDirectDeps() : null, + supportData.getDirectProtoSourceRoots(), supportData.getTransitiveImports()); if (areDepsStrict) { @@ -360,7 +361,8 @@ public class ProtoCompileActionBuilder { Artifact output, boolean allowServices, NestedSet<Artifact> transitiveDescriptorSets, - NestedSet<String> protoSourceRoots) { + NestedSet<String> protoSourceRoots, + NestedSet<String> directProtoSourceRoots) { if (protosToCompile.isEmpty()) { ruleContext.registerAction( FileWriteAction.createEmptyWithInputs( @@ -376,6 +378,7 @@ public class ProtoCompileActionBuilder { transitiveSources, protosInDirectDeps, protoSourceRoots, + directProtoSourceRoots, ruleContext.getLabel(), ImmutableList.of(output), "Descriptor Set", @@ -423,6 +426,7 @@ public class ProtoCompileActionBuilder { NestedSet<Artifact> transitiveSources, NestedSet<Artifact> protosInDirectDeps, NestedSet<String> protoSourceRoots, + NestedSet<String> directProtoSourceRoots, Label ruleLabel, Iterable<Artifact> outputs, String flavorName, @@ -435,6 +439,7 @@ public class ProtoCompileActionBuilder { transitiveSources, protosInDirectDeps, protoSourceRoots, + directProtoSourceRoots, ruleLabel, outputs, flavorName, @@ -452,6 +457,7 @@ public class ProtoCompileActionBuilder { NestedSet<Artifact> transitiveSources, @Nullable NestedSet<Artifact> protosInDirectDeps, NestedSet<String> protoSourceRoots, + NestedSet<String> directProtoSourceRoots, Label ruleLabel, Iterable<Artifact> outputs, String flavorName, @@ -487,6 +493,7 @@ public class ProtoCompileActionBuilder { protosToCompile, transitiveSources, protoSourceRoots, + directProtoSourceRoots, areDepsStrict(ruleContext) ? protosInDirectDeps : null, ruleLabel, allowServices, @@ -524,11 +531,13 @@ public class ProtoCompileActionBuilder { Iterable<Artifact> protosToCompile, NestedSet<Artifact> transitiveSources, NestedSet<String> transitiveProtoPathFlags, + NestedSet<String> directProtoSourceRoots, @Nullable NestedSet<Artifact> protosInDirectDeps, Label ruleLabel, boolean allowServices, ImmutableList<String> protocOpts) { CustomCommandLine.Builder cmdLine = CustomCommandLine.builder(); + cmdLine.addAll(transitiveProtoPathFlags); cmdLine.addAll(transitiveProtoPathFlags); @@ -565,7 +574,7 @@ public class ProtoCompileActionBuilder { cmdLine.addAll(protocOpts); // Add include maps - addIncludeMapArguments(cmdLine, protosInDirectDeps, transitiveSources); + addIncludeMapArguments(cmdLine, protosInDirectDeps, directProtoSourceRoots, transitiveSources); if (protosInDirectDeps != null) { cmdLine.addFormatted(STRICT_DEPS_FLAG_TEMPLATE, ruleLabel); @@ -586,6 +595,7 @@ public class ProtoCompileActionBuilder { static void addIncludeMapArguments( CustomCommandLine.Builder commandLine, @Nullable NestedSet<Artifact> protosInDirectDependencies, + NestedSet<String> directProtoSourceRoots, NestedSet<Artifact> transitiveImports) { commandLine.addAll( VectorArg.of(transitiveImports) @@ -596,7 +606,14 @@ public class ProtoCompileActionBuilder { "--direct_dependencies", VectorArg.join(":") .each(protosInDirectDependencies) - .mapped(ProtoCompileActionBuilder::expandToPathIgnoringRepository)); + .mapped((Artifact proto, Consumer<String> args) -> { + for (String directProtoSourceRoot : directProtoSourceRoots) { + expandToPathIgnoringSourceRoot(proto, directProtoSourceRoot, args); + } + expandToPathIgnoringRepository(proto, args); + }) + ); + } else { // The proto compiler requires an empty list to turn on strict deps checking commandLine.add("--direct_dependencies="); @@ -627,6 +644,20 @@ public class ProtoCompileActionBuilder { .toString(); } + private static void expandToPathIgnoringSourceRoot( + Artifact artifact, String directProtoSourceRoot, Consumer<String> args) { + try { + String relativePath = artifact.getRootRelativePath() + .relativeTo( + artifact.getOwnerLabel().getPackageIdentifier().getRepository().getPathUnderExecRoot()) + .relativeTo(directProtoSourceRoot) + .toString(); + args.accept(relativePath); + } catch (IllegalArgumentException exception) { + // do nothing + } + } + /** * Describes a toolchain and the value to replace for a $(OUT) that might appear in its * commandLine() (e.g., "bazel-out/foo.srcjar"). |