Skip to content

Commit

Permalink
Expose JavaRuleOutputJarsProvider in java_common.provider, when avail…
Browse files Browse the repository at this point in the history
…able.

I'm not attempting to fix b/65618333 here, just handling one case
currently breaking users (JavaInfo created via java_common.compile).

My temporary workaround attempt to expose this information in the
soy custom rule failed (unknown commit) -- to fix users we really
need java_common changes.

RELNOTES: Expose output jars and jdeps in java_common.provider, when available.
PiperOrigin-RevId: 170236096
  • Loading branch information
Googler authored and katre committed Sep 28, 2017
1 parent 42ac567 commit e398b25
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ public SkylarkList<Artifact> getSourceJars() {
providers.getProvider(JavaSourceJarsProvider.class).getSourceJars());
}

@SkylarkCallable(
name = "outputs",
doc = "Returns information about outputs of this Java target.",
structField = true,
allowReturnNones = true
)
public JavaRuleOutputJarsProvider getOutputJars() {
return getProvider(JavaRuleOutputJarsProvider.class);
}

@Override
public boolean equals(Object otherObject) {
if (this == otherObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.rules.java.JavaConfiguration.JavaClasspathMode;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.util.Preconditions;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -154,12 +155,16 @@ public JavaLibraryHelper setCompilationStrictDepsMode(StrictDepsMode strictDepsM
return this;
}

/** Creates the compile actions. */
/**
* Creates the compile actions. Also fills in the {@link JavaRuleOutputJarsProvider.Builder} with
* the corresponding compilation outputs.
*/
public JavaCompilationArtifacts build(
JavaSemantics semantics,
JavaToolchainProvider javaToolchainProvider,
NestedSet<Artifact> hostJavabase,
Iterable<Artifact> jacocoInstrumental) {
Iterable<Artifact> jacocoInstrumental,
JavaRuleOutputJarsProvider.Builder outputJarsBuilder) {
Preconditions.checkState(output != null, "must have an output file; use setOutput()");
JavaTargetAttributes.Builder attributes = new JavaTargetAttributes.Builder(semantics);
attributes.addSourceJars(sourceJars);
Expand Down Expand Up @@ -189,9 +194,14 @@ public JavaCompilationArtifacts build(
Artifact outputDepsProto = helper.createOutputDepsProtoArtifact(output, artifactsBuilder);
helper.createCompileAction(
output, null /* manifestProtoOutput */, null /* gensrcOutputJar */, outputDepsProto);
helper.createCompileTimeJarAction(output, artifactsBuilder);
Artifact iJar = helper.createCompileTimeJarAction(output, artifactsBuilder);

artifactsBuilder.addRuntimeJar(output);

outputJarsBuilder
.addOutputJar(new OutputJar(output, iJar, sourceJars))
.setJdeps(outputDepsProto);

return artifactsBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public Builder addOutputJar(
return this;
}

public Builder addOutputJars(Iterable<OutputJar> outputJars) {
this.outputJars.addAll(outputJars);
return this;
}

public Builder addOutputJar(OutputJar outputJar) {
outputJars.add(outputJar);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,19 +428,17 @@ public JavaInfo createJavaCompileAction(

NestedSet<Artifact> hostJavabaseArtifacts =
hostJavabaseProvider == null
? NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)
? NestedSetBuilder.emptySet(Order.STABLE_ORDER)
: hostJavabaseProvider.getMiddlemanArtifact();
JavaToolchainProvider javaToolchainProvider = getJavaToolchainProvider(javaToolchain);
JavaRuleOutputJarsProvider.Builder outputJarsBuilder = JavaRuleOutputJarsProvider.builder();
JavaCompilationArtifacts artifacts =
helper.build(
javaSemantics,
javaToolchainProvider,
hostJavabaseArtifacts,
SkylarkList.createImmutable(ImmutableList.<Artifact>of()));
JavaRuleOutputJarsProvider javaRuleOutputJarsProvider =
JavaRuleOutputJarsProvider.builder().addOutputJar(
new JavaRuleOutputJarsProvider.OutputJar(outputJar, /* ijar */ null, sourceJars))
.build();
SkylarkList.createImmutable(ImmutableList.of()),
outputJarsBuilder);
JavaCompilationArgsProvider javaCompilationArgsProvider =
helper.buildCompilationArgsProvider(artifacts, true);
Runfiles runfiles =
Expand All @@ -458,12 +456,12 @@ public JavaInfo createJavaCompileAction(
));

return JavaInfo.Builder.create()
.addProvider(JavaCompilationArgsProvider.class, javaCompilationArgsProvider)
.addProvider(JavaSourceJarsProvider.class, createJavaSourceJarsProvider(sourceJars))
.addProvider(JavaRuleOutputJarsProvider.class, javaRuleOutputJarsProvider)
.addProvider(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles))
.addProvider(JavaPluginInfoProvider.class, transitivePluginsProvider)
.build();
.addProvider(JavaCompilationArgsProvider.class, javaCompilationArgsProvider)
.addProvider(JavaSourceJarsProvider.class, createJavaSourceJarsProvider(sourceJars))
.addProvider(JavaRuleOutputJarsProvider.class, outputJarsBuilder.build())
.addProvider(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles))
.addProvider(JavaPluginInfoProvider.class, transitivePluginsProvider)
.build();
}

private static Artifact buildIjar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ private JavaCompilationArgsProvider createJavaCompileAction(
javaSemantics,
JavaCompilationHelper.getJavaToolchainProvider(ruleContext),
JavaHelper.getHostJavabaseInputs(ruleContext),
JavaCompilationHelper.getInstrumentationJars(ruleContext));
JavaCompilationHelper.getInstrumentationJars(ruleContext),
JavaRuleOutputJarsProvider.builder());
return helper.buildCompilationArgsProvider(artifacts, true /* isReportedAsStrict */);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ private JavaCompilationArgsProvider createJavaCompileAction(
javaSemantics,
JavaCompilationHelper.getJavaToolchainProvider(ruleContext),
JavaHelper.getHostJavabaseInputs(ruleContext),
JavaCompilationHelper.getInstrumentationJars(ruleContext)),
JavaCompilationHelper.getInstrumentationJars(ruleContext),
JavaRuleOutputJarsProvider.builder()),
true /* isReportedAsStrict */);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,145 @@ public void testJavaRuntimeProvider() throws Exception {
assertThat(javaExecutable.getPathString()).startsWith("/foo/bar/bin/java");
}

@Test
public void testExposesJavaCommonProvider() throws Exception {
scratch.file(
"java/test/BUILD",
"load(':extension.bzl', 'my_rule')",
"java_library(",
" name = 'dep',",
" srcs = [ 'Dep.java'],",
")",
"my_rule(",
" name = 'my',",
" dep = ':dep',",
")");
scratch.file(
"java/test/extension.bzl",
"result = provider()",
"def impl(ctx):",
" depj = ctx.attr.dep[java_common.provider]",
" return [result(",
" transitive_runtime_jars = depj.transitive_runtime_jars,",
" transitive_compile_time_jars = depj.transitive_compile_time_jars,",
" compile_jars = depj.compile_jars,",
" full_compile_jars = depj.full_compile_jars,",
" source_jars = depj.source_jars,",
" outputs = depj.outputs,",
" )]",
"my_rule = rule(impl, attrs = { 'dep' : attr.label() })");

ConfiguredTarget configuredTarget = getConfiguredTarget("//java/test:my");
Info info =
configuredTarget.get(
new SkylarkKey(Label.parseAbsolute("//java/test:extension.bzl"), "result"));

SkylarkNestedSet transitiveRuntimeJars =
((SkylarkNestedSet) info.getValue("transitive_runtime_jars"));
SkylarkNestedSet transitiveCompileTimeJars =
((SkylarkNestedSet) info.getValue("transitive_compile_time_jars"));
SkylarkNestedSet compileJars = ((SkylarkNestedSet) info.getValue("compile_jars"));
SkylarkNestedSet fullCompileJars = ((SkylarkNestedSet) info.getValue("full_compile_jars"));
SkylarkList<Artifact> sourceJars = ((SkylarkList<Artifact>) info.getValue("source_jars"));
JavaRuleOutputJarsProvider outputs = ((JavaRuleOutputJarsProvider) info.getValue("outputs"));

assertThat(artifactFilesNames(transitiveRuntimeJars.toCollection(Artifact.class)))
.containsExactly("libdep.jar");
assertThat(artifactFilesNames(transitiveCompileTimeJars.toCollection(Artifact.class)))
.containsExactly("libdep-hjar.jar");
assertThat(transitiveCompileTimeJars.toCollection()).isEqualTo(compileJars.toCollection());
assertThat(artifactFilesNames(fullCompileJars.toCollection(Artifact.class)))
.containsExactly("libdep.jar");
assertThat(artifactFilesNames(sourceJars)).containsExactly("libdep-src.jar");

assertThat(outputs.getOutputJars()).hasSize(1);
OutputJar output = outputs.getOutputJars().get(0);
assertThat(output.getClassJar().getFilename()).isEqualTo("libdep.jar");
assertThat(output.getIJar().getFilename()).isEqualTo("libdep-hjar.jar");
assertThat(artifactFilesNames(output.getSrcJars())).containsExactly("libdep-src.jar");
assertThat(outputs.getJdeps().getFilename()).isEqualTo("libdep.jdeps");
}

@Test
public void testJavaCommonCompileExposesOutputJarProvider() throws Exception {
writeBuildFileForJavaToolchain();
scratch.file("java/test/B.jar");
scratch.file(
"java/test/BUILD",
"load(':extension.bzl', 'my_rule')",
"load(':custom_rule.bzl', 'java_custom_library')",
"java_custom_library(",
"name = 'dep',",
"srcs = ['Main.java'],",
"sourcepath = [':B.jar']",
")",
"my_rule(",
" name = 'my',",
" dep = ':dep',",
")");
scratch.file(
"java/test/extension.bzl",
"result = provider()",
"def impl(ctx):",
" depj = ctx.attr.dep[java_common.provider]",
" return [result(",
" transitive_runtime_jars = depj.transitive_runtime_jars,",
" transitive_compile_time_jars = depj.transitive_compile_time_jars,",
" compile_jars = depj.compile_jars,",
" full_compile_jars = depj.full_compile_jars,",
" source_jars = depj.source_jars,",
" outputs = depj.outputs,",
" )]",
"my_rule = rule(impl, attrs = { 'dep' : attr.label() })");
scratch.file(
"java/test/custom_rule.bzl",
"def _impl(ctx):",
" output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
" compilation_provider = java_common.compile(",
" ctx,",
" source_files = ctx.files.srcs,",
" output = output_jar,",
" javac_opts = java_common.default_javac_opts(",
" ctx, java_toolchain_attr = '_java_toolchain'),",
" deps = [],",
" sourcepath = ctx.files.sourcepath,",
" strict_deps = 'ERROR',",
" java_toolchain = ctx.attr._java_toolchain,",
" host_javabase = ctx.attr._host_javabase",
" )",
" return struct(",
" files = depset([output_jar]),",
" providers = [compilation_provider]",
" )",
"java_custom_library = rule(",
" implementation = _impl,",
" outputs = {",
" 'my_output': 'lib%{name}.jar'",
" },",
" attrs = {",
" 'srcs': attr.label_list(allow_files=['.java']),",
" 'sourcepath': attr.label_list(allow_files=['.jar']),",
" '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
" '_host_javabase': attr.label(default = Label('//tools/defaults:jdk'))",
" },",
" fragments = ['java']",
")");

ConfiguredTarget configuredTarget = getConfiguredTarget("//java/test:my");
Info info =
configuredTarget.get(
new SkylarkKey(Label.parseAbsolute("//java/test:extension.bzl"), "result"));

JavaRuleOutputJarsProvider outputs = ((JavaRuleOutputJarsProvider) info.getValue("outputs"));
assertThat(outputs.getOutputJars()).hasSize(1);

OutputJar outputJar = outputs.getOutputJars().get(0);
assertThat(outputJar.getClassJar().getFilename()).isEqualTo("libdep.jar");
assertThat(outputJar.getIJar().getFilename()).isEqualTo("libdep-hjar.jar");
assertThat(outputJar.getSrcJars()).isEmpty();
assertThat(outputs.getJdeps().getFilename()).isEqualTo("libdep.jdeps");
}

@Test
public void testExposesJavaSkylarkApiProvider() throws Exception {
scratch.file(
Expand Down Expand Up @@ -120,7 +259,7 @@ public void testExposesJavaSkylarkApiProvider() throws Exception {
assertThat(outputJars.get(0).getClassJar().getFilename()).isEqualTo("libdep.jar");
}

private static Collection<String> artifactFilesNames(Collection<Artifact> artifacts) {
private static Collection<String> artifactFilesNames(Iterable<Artifact> artifacts) {
List<String> result = new ArrayList<>();
for (Artifact artifact : artifacts) {
result.add(artifact.getFilename());
Expand Down

0 comments on commit e398b25

Please sign in to comment.