Skip to content

Commit

Permalink
Return JavaInfo from java proto aspects.
Browse files Browse the repository at this point in the history
RELNOTES: None.
PiperOrigin-RevId: 263133003
  • Loading branch information
iirina authored and copybara-github committed Aug 13, 2019
1 parent 522099d commit ef8b6f6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -209,24 +214,29 @@ void addProviders(ConfiguredAspect.Builder aspect) {
NestedSetBuilder.<Artifact>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 =
JavaCompilationArgsProvider.merge(
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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -229,23 +234,28 @@ void addProviders(ConfiguredAspect.Builder aspect) {
NestedSetBuilder.<Artifact>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 =
JavaCompilationArgsProvider.merge(
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(),
Expand Down
59 changes: 59 additions & 0 deletions src/test/shell/bazel/bazel_proto_library_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
def _my_rule_impl(ctx):
aspect_java_infos = []
for dep in ctx.attr.deps:
aspect_java_infos += dep.my_aspect_providers
merged_java_info = java_common.merge(aspect_java_infos)
for jar in merged_java_info.transitive_runtime_jars.to_list():
print('Transitive runtime jar', jar)
def _my_aspect_impl(target, ctx):
aspect_java_infos = []
for dep in ctx.rule.attr.deps:
aspect_java_infos += dep.my_aspect_providers
aspect_java_infos.append(target[JavaInfo])
return struct(
my_aspect_providers = aspect_java_infos
)
my_aspect = aspect(
attr_aspects = ['deps'],
fragments = ['java'],
implementation = _my_aspect_impl,
required_aspect_providers = [[JavaInfo]]
)
my_rule_with_aspect = rule(
implementation = _my_rule_impl,
attrs = {
'deps': attr.label_list(aspects = [my_aspect]),
}
)
EOF
bazel build java/proto:my_rule &> "$TEST_log" || fail "build failed"
expect_log "Transitive runtime jar <generated file java/proto/libmy_proto-speed.jar>"
}

function test_strip_import_prefix() {
write_workspace ""
write_setup "proto_library" "strip_import_prefix = '/x/person'" ""
Expand Down

0 comments on commit ef8b6f6

Please sign in to comment.