diff options
author | Carmi Grushko <carmi@google.com> | 2016-11-23 23:34:22 +0000 |
---|---|---|
committer | Dmitry Lomov <dslomov@google.com> | 2016-11-24 10:33:00 +0000 |
commit | 0c5c3484d3b717cdafe4a2983375cedae32989c0 (patch) | |
tree | 661093d6344b0db21c3f067452f16e3d1e4d076d /src/main/java/com/google | |
parent | 64de4b555a93aa9779cbf54aa1202193a4ed76a5 (diff) |
Strict deps for proto_library.
In other words, all imported protos must be declared as BUILD dependencies.
RELNOTES: proto_library supports strict proto deps.
--
MOS_MIGRATED_REVID=140078632
Diffstat (limited to 'src/main/java/com/google')
3 files changed, 38 insertions, 9 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java index 6b107fe655..b81e6e0a70 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java @@ -14,13 +14,14 @@ package com.google.devtools.build.lib.rules.proto; +import static com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode.TARGET; import static com.google.devtools.build.lib.collect.nestedset.Order.STABLE_ORDER; +import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.ConfiguredTarget; -import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.Runfiles; @@ -37,7 +38,7 @@ public class BazelProtoLibrary implements RuleConfiguredTargetFactory { public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException { ImmutableList<Artifact> protoSources = - ruleContext.getPrerequisiteArtifacts("srcs", Mode.TARGET).list(); + ruleContext.getPrerequisiteArtifacts("srcs", TARGET).list(); ImmutableList<Artifact> checkDepsProtoSources = ProtoCommon.getCheckDepsProtoSources(ruleContext, protoSources); ProtoCommon.checkSourceFilesAreInSamePackage(ruleContext); @@ -50,11 +51,16 @@ public class BazelProtoLibrary implements RuleConfiguredTargetFactory { ProtoSourcesProvider.create( transitiveImports, transitiveImports, protoSources, checkDepsProtoSources); + NestedSet<Artifact> protosInDirectDeps = + ruleContext.attributes().get("strict_proto_deps", BOOLEAN) + ? ProtoCommon.computeProtosInDirectDeps(ruleContext) + : null; + final SupportData supportData = SupportData.create( Predicates.<TransitiveInfoCollection>alwaysTrue() /* nonWeakDepsPredicate */, protoSources, - null /* protosInDirectDeps */, + protosInDirectDeps, transitiveImports, !protoSources.isEmpty()); @@ -74,7 +80,7 @@ public class BazelProtoLibrary implements RuleConfiguredTargetFactory { descriptorSetOutput.getExecPathString(), checkDepsProtoSources, transitiveImports, - null /* protosInDirectDeps */, + protosInDirectDeps, ImmutableList.of(descriptorSetOutput), true /* allowServices */); diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibraryRule.java b/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibraryRule.java index c005efec9f..ee9a23daa3 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibraryRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibraryRule.java @@ -18,6 +18,7 @@ import static com.google.devtools.build.lib.packages.Attribute.ConfigurationTran import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL; import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; +import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; import com.google.devtools.build.lib.analysis.BaseRuleClasses; import com.google.devtools.build.lib.analysis.RuleDefinition; @@ -56,11 +57,7 @@ public final class BazelProtoLibraryRule implements RuleDefinition { // but these are still 'experimental' according to the documentation. .setUndocumented() .setOutputToGenfiles() - .add( - attr(":proto_compiler", LABEL) - .cfg(HOST) - .exec() - .value(PROTO_COMPILER)) + .add(attr(":proto_compiler", LABEL).cfg(HOST).exec().value(PROTO_COMPILER)) /* <!-- #BLAZE_RULE(proto_library).ATTRIBUTE(deps) --> The list of other <code>proto_library</code> rules that the target depends upon. A <code>proto_library</code> may only depend on other @@ -79,6 +76,7 @@ public final class BazelProtoLibraryRule implements RuleDefinition { attr("srcs", LABEL_LIST) .direct_compile_time_input() .allowedFileTypes(FileType.of(".proto"), FileType.of(".protodevel"))) + .add(attr("strict_proto_deps", BOOLEAN).value(true).undocumented("for migration only")) .advertiseProvider(ProtoSourcesProvider.class) .build(); } 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 41778d8383..626d7136ed 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 @@ -14,6 +14,8 @@ package com.google.devtools.build.lib.rules.proto; +import static com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode.TARGET; + import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.Root; @@ -27,6 +29,7 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.PathFragment; +import javax.annotation.Nullable; /** * Utility functions for proto_library and proto aspect implementations. @@ -155,4 +158,26 @@ public class ProtoCommon { ImmutableList<Artifact> protoSources, String extension) { return getGeneratedOutputs(ruleContext, protoSources, extension, false); } + + /** + * Returns the .proto files that are the direct srcs of the direct-dependencies of this rule. If + * the current rule is an alias proto_library (=no srcs), we use the direct srcs of the + * direct-dependencies of our direct-dependencies. + */ + @Nullable + public static NestedSet<Artifact> computeProtosInDirectDeps(RuleContext ruleContext) { + NestedSetBuilder<Artifact> result = NestedSetBuilder.stableOrder(); + if (ruleContext.getPrerequisiteArtifacts("srcs", TARGET).list().isEmpty()) { + for (ProtoSupportDataProvider provider : + ruleContext.getPrerequisites("deps", TARGET, ProtoSupportDataProvider.class)) { + result.addTransitive(provider.getSupportData().getProtosInDirectDeps()); + } + } else { + for (ProtoSourcesProvider provider : + ruleContext.getPrerequisites("deps", TARGET, ProtoSourcesProvider.class)) { + result.addAll(provider.getCheckDepsProtoSources()); + } + } + return result.build(); + } } |