diff options
author | Googler <noreply@google.com> | 2018-07-31 08:53:47 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-31 08:54:56 -0700 |
commit | 04d8a923ffd81b35eca52e34accbab930571d626 (patch) | |
tree | 91ea58897f9e97626be0e2964f4d88099a4d628a | |
parent | 504a6d20845c65a346e6f376611aa7fdafb65950 (diff) |
Remove output jar from runfiles and the ijar from the OutputJar when sources
are not provided.
RELNOTES: none.
PiperOrigin-RevId: 206768891
3 files changed, 60 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java index fed792eb5f..4ff6468940 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java @@ -536,6 +536,12 @@ final class JavaInfoBuildHelper { sourceJarsP -> transitiveSourceJars.addTransitive(sourceJarsP.getTransitiveSourceJars())); + // When sources are not provided, the subsequent output Jar will be empty. As such, the output + // Jar is omitted from the set of Runtime Jars. + if (!sourceJars.isEmpty() || !sourceFiles.isEmpty()) { + javaInfoBuilder.setRuntimeJars(ImmutableList.of(outputJar)); + } + return javaInfoBuilder .addProvider(JavaCompilationArgsProvider.class, javaCompilationArgsProvider) .addProvider( @@ -545,7 +551,6 @@ final class JavaInfoBuildHelper { .addProvider(JavaRunfilesProvider.class, new JavaRunfilesProvider(runfiles)) .addProvider(JavaPluginInfoProvider.class, transitivePluginsProvider) .setNeverlink(neverlink) - .setRuntimeJars(ImmutableList.of(outputJar)) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java index c035f5c433..37929cb8b9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java @@ -267,8 +267,11 @@ public final class JavaLibraryHelper { /* instrumentationMetadataJar= */ null, nativeHeaderOutput); - artifactsBuilder.addRuntimeJar(output); - Artifact iJar = helper.createCompileTimeJarAction(output, artifactsBuilder); + Artifact iJar = null; + if (!sourceJars.isEmpty() || !sourceFiles.isEmpty()) { + artifactsBuilder.addRuntimeJar(output); + iJar = helper.createCompileTimeJarAction(output, artifactsBuilder); + } if (createOutputSourceJar) { helper.createSourceJarAction( diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java index 3e2c655639..109787b499 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java @@ -2014,4 +2014,53 @@ public class JavaSkylarkApiTest extends BuildViewTestCase { assertThat(artifactFilesNames(info.getTransitiveCompileTimeJars().toCollection(Artifact.class))) .containsExactly("liba-hjar.jar", "libb-hjar.jar"); } + + @Test + public void testCompileOutputJarNotInRuntimePathWithoutAnySourcesDefined() throws Exception { + writeBuildFileForJavaToolchain(); + scratch.file( + "foo/java_custom_library.bzl", + "def _impl(ctx):", + " output_jar = ctx.actions.declare_file('lib%s.jar' % ctx.label.name)", + " exports = [export[JavaInfo] for export in ctx.attr.exports]", + " compilation_provider = java_common.compile(", + " ctx,", + " source_files = ctx.files.srcs,", + " output = output_jar,", + " exports = exports,", + " java_toolchain = ctx.attr._java_toolchain,", + " host_javabase = ctx.attr._host_javabase", + " )", + " return struct(", + " providers = [compilation_provider]", + " )", + "java_custom_library = rule(", + " implementation = _impl,", + " attrs = {", + " 'srcs': attr.label_list(allow_files=['.java']),", + " 'exports': attr.label_list(),", + " '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),", + " '_host_javabase': attr.label(", + " default = Label('" + HOST_JAVA_RUNTIME_LABEL + "'))", + " },", + " fragments = ['java'],", + ")"); + scratch.file( + "foo/BUILD", + "load(':java_custom_library.bzl', 'java_custom_library')", + "java_library(name = 'b', srcs = ['java/B.java'])", + "java_custom_library(name = 'c', srcs = [], exports = [':b'])"); + + ConfiguredTarget configuredTarget = getConfiguredTarget("//foo:c"); + JavaInfo info = configuredTarget.get(JavaInfo.PROVIDER); + assertThat(artifactFilesNames(info.getTransitiveRuntimeJars().toCollection(Artifact.class))) + .containsExactly("libb.jar"); + assertThat(artifactFilesNames(info.getTransitiveCompileTimeJars().toCollection(Artifact.class))) + .containsExactly("libb-hjar.jar"); + JavaRuleOutputJarsProvider outputs = info.getOutputJars(); + assertThat(outputs.getOutputJars()).hasSize(1); + OutputJar output = outputs.getOutputJars().get(0); + assertThat(output.getClassJar().getFilename()).isEqualTo("libc.jar"); + assertThat(output.getIJar()).isNull(); + } } |