From ef8b6f68cc8ffd2e6523a894034ae383e87ec74c Mon Sep 17 00:00:00 2001 From: elenairina Date: Tue, 13 Aug 2019 07:33:24 -0700 Subject: [PATCH] Return JavaInfo from java proto aspects. RELNOTES: None. PiperOrigin-RevId: 263133003 --- .../rules/java/proto/JavaLiteProtoAspect.java | 10 ++++ .../lib/rules/java/proto/JavaProtoAspect.java | 10 ++++ .../shell/bazel/bazel_proto_library_test.sh | 59 +++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java index 0b01e84e375369..da899079159f1d 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java @@ -41,8 +41,10 @@ import com.google.devtools.build.lib.packages.AspectParameters; import com.google.devtools.build.lib.packages.Attribute.LabelLateBoundDefault; import com.google.devtools.build.lib.packages.NativeAspectClass; +import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider; import com.google.devtools.build.lib.rules.java.JavaConfiguration; +import com.google.devtools.build.lib.rules.java.JavaInfo; import com.google.devtools.build.lib.rules.java.JavaRuleClasses; import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider; import com.google.devtools.build.lib.rules.java.JavaSemantics; @@ -111,6 +113,8 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) { JavaConfiguration.class, ProtoConfiguration.class, PlatformConfiguration.class) .requireSkylarkProviders(ProtoInfo.PROVIDER.id()) .advertiseProvider(JavaProtoLibraryAspectProvider.class) + .advertiseProvider( + ImmutableList.of(SkylarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey()))) .advertiseProvider(ImmutableList.of(JavaSkylarkApiProvider.SKYLARK_NAME)) .add( attr(JavaProtoAspectCommon.LITE_PROTO_TOOLCHAIN_ATTR, LABEL) @@ -169,6 +173,7 @@ private static class Impl { } void addProviders(ConfiguredAspect.Builder aspect) { + JavaInfo.Builder javaInfo = JavaInfo.Builder.create(); // Represents the result of compiling the code generated for this proto, including all of its // dependencies. JavaCompilationArgsProvider generatedCompilationArgsProvider; @@ -209,11 +214,14 @@ void addProviders(ConfiguredAspect.Builder aspect) { NestedSetBuilder.emptySet(Order.STABLE_ORDER), javaSourceJars); aspect.addProvider(ruleOutputJarsProvider).addProvider(sourceJarsProvider); + javaInfo.addProvider(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider); + javaInfo.addProvider(JavaSourceJarsProvider.class, sourceJarsProvider); } else { // No sources - this proto_library is an alias library, which exports its dependencies. // Simply propagate the compilation-args from its dependencies. generatedCompilationArgsProvider = dependencyCompilationArgs; aspect.addProvider(JavaRuleOutputJarsProvider.EMPTY); + javaInfo.addProvider(JavaRuleOutputJarsProvider.class, JavaRuleOutputJarsProvider.EMPTY); } generatedCompilationArgsProvider = @@ -221,12 +229,14 @@ void addProviders(ConfiguredAspect.Builder aspect) { ImmutableList.of(generatedCompilationArgsProvider, exportsCompilationArgs)); aspect.addProvider(generatedCompilationArgsProvider); + javaInfo.addProvider(JavaCompilationArgsProvider.class, generatedCompilationArgsProvider); aspect.addNativeDeclaredProvider( createCcLinkingInfo(ruleContext, aspectCommon.getProtoRuntimeDeps())); JavaSkylarkApiProvider skylarkApiProvider = JavaSkylarkApiProvider.fromRuleContext(); aspect .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider) + .addNativeDeclaredProvider(javaInfo.build()) .addProvider( new JavaProtoLibraryAspectProvider( transitiveOutputJars.build(), diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java index 787ea7415cc8f9..93ac592dd93965 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java @@ -41,8 +41,10 @@ import com.google.devtools.build.lib.packages.AspectParameters; import com.google.devtools.build.lib.packages.Attribute.LabelLateBoundDefault; import com.google.devtools.build.lib.packages.NativeAspectClass; +import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider; import com.google.devtools.build.lib.rules.java.JavaConfiguration; +import com.google.devtools.build.lib.rules.java.JavaInfo; import com.google.devtools.build.lib.rules.java.JavaRuleClasses; import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider; import com.google.devtools.build.lib.rules.java.JavaSemantics; @@ -120,6 +122,8 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) { JavaConfiguration.class, ProtoConfiguration.class, PlatformConfiguration.class) .requireSkylarkProviders(ProtoInfo.PROVIDER.id()) .advertiseProvider(JavaProtoLibraryAspectProvider.class) + .advertiseProvider( + ImmutableList.of(SkylarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey()))) .advertiseProvider(ImmutableList.of(JavaSkylarkApiProvider.SKYLARK_NAME)) .add( attr(JavaProtoAspectCommon.SPEED_PROTO_TOOLCHAIN_ATTR, LABEL) @@ -191,6 +195,7 @@ private static class Impl { void addProviders(ConfiguredAspect.Builder aspect) { // Represents the result of compiling the code generated for this proto, including all of its // dependencies. + JavaInfo.Builder javaInfo = JavaInfo.Builder.create(); JavaCompilationArgsProvider generatedCompilationArgsProvider; // The jars that this proto and its dependencies produce. Used to roll-up jars up to the @@ -229,11 +234,14 @@ void addProviders(ConfiguredAspect.Builder aspect) { NestedSetBuilder.emptySet(Order.STABLE_ORDER), javaSourceJars); aspect.addProvider(ruleOutputJarsProvider).addProvider(sourceJarsProvider); + javaInfo.addProvider(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider); + javaInfo.addProvider(JavaSourceJarsProvider.class, sourceJarsProvider); } else { // No sources - this proto_library is an alias library, which exports its dependencies. // Simply propagate the compilation-args from its dependencies. generatedCompilationArgsProvider = dependencyCompilationArgs; aspect.addProvider(JavaRuleOutputJarsProvider.EMPTY); + javaInfo.addProvider(JavaRuleOutputJarsProvider.class, JavaRuleOutputJarsProvider.EMPTY); } generatedCompilationArgsProvider = @@ -241,11 +249,13 @@ void addProviders(ConfiguredAspect.Builder aspect) { ImmutableList.of(generatedCompilationArgsProvider, exportsCompilationArgs)); aspect.addProvider(generatedCompilationArgsProvider); + javaInfo.addProvider(JavaCompilationArgsProvider.class, generatedCompilationArgsProvider); aspect.addNativeDeclaredProvider( createCcLinkingInfo(ruleContext, aspectCommon.getProtoRuntimeDeps())); JavaSkylarkApiProvider javaSkylarkApiProvider = JavaSkylarkApiProvider.fromRuleContext(); aspect .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, javaSkylarkApiProvider) + .addNativeDeclaredProvider(javaInfo.build()) .addProvider( new JavaProtoLibraryAspectProvider( transitiveOutputJars.build(), diff --git a/src/test/shell/bazel/bazel_proto_library_test.sh b/src/test/shell/bazel/bazel_proto_library_test.sh index 1a3d796429816e..88a6dabe68f386 100755 --- a/src/test/shell/bazel/bazel_proto_library_test.sh +++ b/src/test/shell/bazel/bazel_proto_library_test.sh @@ -384,6 +384,65 @@ EOF ############# TESTS ############# +function test_javainfo_proto_aspect() { + write_workspace "" + + mkdir -p java/proto/ + touch java/proto/my.proto + cat > java/proto/BUILD << EOF +load(':my_rule_with_aspect.bzl', 'my_rule_with_aspect') +my_rule_with_aspect( + name = 'my_rule', + deps = [':my_java_proto'] +) + +java_proto_library( + name = 'my_java_proto', + deps = [':my_proto'], +) + +proto_library( + name = 'my_proto', + srcs = ['my.proto'], +) +EOF + + cat > java/proto/my_rule_with_aspect.bzl < "$TEST_log" || fail "build failed" + expect_log "Transitive runtime jar " +} + function test_strip_import_prefix() { write_workspace "" write_setup "proto_library" "strip_import_prefix = '/x/person'" ""