From 81d999d40a42393ad5ddc8d889563fd3c431e409 Mon Sep 17 00:00:00 2001 From: elenairina Date: Mon, 16 Apr 2018 06:27:56 -0700 Subject: [PATCH] Create gen source jar in java_common.compile(). PiperOrigin-RevId: 193029216 --- .../build/lib/rules/java/JavaCommon.java | 4 ++++ .../lib/rules/java/JavaCompilationHelper.java | 16 +++++++++++++--- .../lib/rules/java/JavaLibraryHelper.java | 18 +++++++++++++++--- .../lib/rules/java/JavaSkylarkApiTest.java | 7 +++++-- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java index 3c37112911ba4a..2a86144f48bfd5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java @@ -467,6 +467,10 @@ public static PathFragment getHostJavaExecutable(RuleContext ruleContext) { return JavaRuntimeInfo.forHost(ruleContext).javaBinaryExecPath(); } + public static PathFragment getHostJavaExecutable(JavaRuntimeInfo javaRuntime) { + return javaRuntime.javaBinaryExecPath(); + } + public static PathFragment getJavaExecutable(RuleContext ruleContext) { return JavaRuntimeInfo.from(ruleContext).javaBinaryExecPath(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java index faab225a5e07c6..fdd0c260820154 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java @@ -479,8 +479,18 @@ public Artifact createManifestProtoOutput(Artifact outputJar) { * @param manifestProto The artifact for the manifest proto emitted from JavaBuilder * @param genClassJar The artifact for the gen jar to output */ - public void createGenJarAction(Artifact classJar, Artifact manifestProto, + public void createGenJarAction( + Artifact classJar, + Artifact manifestProto, Artifact genClassJar) { + createGenJarAction( + classJar, manifestProto, genClassJar, JavaRuntimeInfo.forHost(getRuleContext())); + } + + public void createGenJarAction(Artifact classJar, + Artifact manifestProto, + Artifact genClassJar, + JavaRuntimeInfo hostJavabase) { getRuleContext() .registerAction( new SpawnAction.Builder() @@ -488,9 +498,9 @@ public void createGenJarAction(Artifact classJar, Artifact manifestProto, .addInput(classJar) .addOutput(genClassJar) .addTransitiveInputs( - JavaRuntimeInfo.forHost(getRuleContext()).javaBaseInputsMiddleman()) + hostJavabase.javaBaseInputsMiddleman()) .setJarExecutable( - JavaCommon.getHostJavaExecutable(ruleContext), + JavaCommon.getHostJavaExecutable(hostJavabase), getGenClassJar(ruleContext), javaToolchain.getJvmOptions()) .addCommandLine( 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 93197acb9a1d88..47a214d1afb7bd 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 @@ -214,10 +214,21 @@ public JavaCompilationArtifacts build( hostJavabase, jacocoInstrumental); Artifact outputDepsProto = helper.createOutputDepsProtoArtifact(output, artifactsBuilder); + + Artifact manifestProtoOutput = helper.createManifestProtoOutput(output); + + Artifact genSourceJar = null; + Artifact genClassJar = null; + if (helper.usesAnnotationProcessing()) { + genClassJar = helper.createGenJar(output); + genSourceJar = helper.createGensrcJar(output); + helper.createGenJarAction(output, manifestProtoOutput, genClassJar, hostJavabase); + } + helper.createCompileAction( output, - /* manifestProtoOutput= */ null, - /* gensrcOutputJar= */ null, + manifestProtoOutput, + genSourceJar, outputDepsProto, /* instrumentationMetadataJar= */ null, /* nativeHeaderOutput= */ null); @@ -226,7 +237,8 @@ public JavaCompilationArtifacts build( Artifact iJar = helper.createCompileTimeJarAction(output, artifactsBuilder); if (createOutputSourceJar) { - helper.createSourceJarAction(outputSourceJar, null, javaToolchainProvider, hostJavabase); + helper.createSourceJarAction( + outputSourceJar, genSourceJar, javaToolchainProvider, hostJavabase); } ImmutableList outputSourceJars = outputSourceJar == null ? ImmutableList.of() : ImmutableList.of(outputSourceJar); 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 83423c0b97dea9..fecfad9e6357ff 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 @@ -340,13 +340,14 @@ public void testJavaCommonCompileTransitiveSourceJars() throws Exception { " host_javabase = ctx.attr._host_javabase", " )", " return struct(", - " files = depset([output_jar]),", + " files = depset([output_jar] + compilation_provider.source_jars),", " providers = [compilation_provider]", " )", "java_custom_library = rule(", " implementation = _impl,", " outputs = {", - " 'my_output': 'lib%{name}.jar'", + " 'my_output': 'lib%{name}.jar',", + " 'my_src_output': 'lib%{name}-src.jar'", " },", " attrs = {", " 'srcs': attr.label_list(allow_files=['.java']),", @@ -365,6 +366,8 @@ public void testJavaCommonCompileTransitiveSourceJars() throws Exception { assertThat(artifactFilesNames(sourceJars)).containsExactly("libcustom-src.jar"); assertThat(artifactFilesNames(transitiveSourceJars)) .containsExactly("libdep-src.jar", "libcustom-src.jar"); + + assertThat(getGeneratingAction(configuredTarget, "java/test/libcustom-src.jar")).isNotNull(); } @Test