aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java
diff options
context:
space:
mode:
authorGravatar elenairina <elenairina@google.com>2017-11-24 07:23:55 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-11-24 07:25:22 -0800
commita6ed4c01d374bcf98bf39c1efc12a0f4f20fe313 (patch)
treeb0a6f8ffb7d9efd8b9f1ca3f3224366319f1a3fa /src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java
parent263c5e07dd2860a866fd361cd29c03afaf3e019f (diff)
Create the output source jar in java_common.compile
and expose transitive source jars to Skylark. Slightly refactor java classes to take in specific host javabase inputs and host java executable for creating the source jar, instead of always relying on fetching them from native java rules specific attributes. Creating the output source jar in java_common.compile makes the behavior more similar to java_library. Exposing the transitive_source_jars to Skylark helps with the Skylark migration from the old 'java' provider to JavaInfo. Progress on #2614. RELNOTES: transitive_source_jars is now exposed on JavaInfo. PiperOrigin-RevId: 176844750
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java47
1 files changed, 39 insertions, 8 deletions
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 941e7b6e98..ae0e58a437 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
@@ -22,7 +22,9 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.analysis.MiddlemanProvider;
import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration.StrictDepsMode;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
@@ -32,6 +34,7 @@ import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.Outpu
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import javax.annotation.Nullable;
/**
* A class to create Java compile actions in a way that is consistent with java_library. Rules that
@@ -156,15 +159,24 @@ public final class JavaLibraryHelper {
}
/**
- * Creates the compile actions. Also fills in the {@link JavaRuleOutputJarsProvider.Builder} with
- * the corresponding compilation outputs.
+ * Creates the compile actions (including the ones for ijar and source jar). Also fills in the
+ * {@link JavaRuleOutputJarsProvider.Builder} with the corresponding compilation outputs.
+ *
+ * @param semantics implementation specific java rules semantics
+ * @param javaToolchainProvider used for retrieving misc java tools
+ * @param hostJavabase the target of the host javabase used to retrieve the java executable and
+ * its necessary inputs
+ * @param jacocoInstrumental jacoco jars needed when running coverage
+ * @param outputJarsBuilder populated with the outputs of the created actions
+ * @param outputSourceJar if not-null, the output of an source jar action that will be created
*/
public JavaCompilationArtifacts build(
JavaSemantics semantics,
JavaToolchainProvider javaToolchainProvider,
- NestedSet<Artifact> hostJavabase,
+ TransitiveInfoCollection hostJavabase,
Iterable<Artifact> jacocoInstrumental,
- JavaRuleOutputJarsProvider.Builder outputJarsBuilder) {
+ JavaRuleOutputJarsProvider.Builder outputJarsBuilder,
+ @Nullable Artifact outputSourceJar) {
Preconditions.checkState(output != null, "must have an output file; use setOutput()");
JavaTargetAttributes.Builder attributes = new JavaTargetAttributes.Builder(semantics);
attributes.addSourceJars(sourceJars);
@@ -185,11 +197,12 @@ public final class JavaLibraryHelper {
attributes, deps);
}
+ NestedSet<Artifact> hostJavabaseArtifacts = getMiddleManFor(hostJavabase);
JavaCompilationArtifacts.Builder artifactsBuilder = new JavaCompilationArtifacts.Builder();
JavaCompilationHelper helper =
new JavaCompilationHelper(ruleContext, semantics, javacOpts, attributes,
javaToolchainProvider,
- hostJavabase,
+ hostJavabaseArtifacts,
jacocoInstrumental);
Artifact outputDepsProto = helper.createOutputDepsProtoArtifact(output, artifactsBuilder);
helper.createCompileAction(
@@ -198,17 +211,35 @@ public final class JavaLibraryHelper {
null /* gensrcOutputJar */,
outputDepsProto,
null /* outputMetadata */);
+
Artifact iJar = helper.createCompileTimeJarAction(output, artifactsBuilder);
artifactsBuilder.addRuntimeJar(output);
- outputJarsBuilder
- .addOutputJar(new OutputJar(output, iJar, sourceJars))
- .setJdeps(outputDepsProto);
+ if (outputSourceJar != null) {
+ helper.createSourceJarAction(outputSourceJar, null,
+ javaToolchainProvider, hostJavabaseArtifacts,
+ JavaCommon.getHostJavaExecutable(ruleContext, hostJavabase));
+ ImmutableList<Artifact> outputSourceJars = ImmutableList.of(outputSourceJar);
+ outputJarsBuilder
+ .addOutputJar(new OutputJar(output, iJar, outputSourceJars))
+ .setJdeps(outputDepsProto);
+ }
return artifactsBuilder.build();
}
+ private static NestedSet<Artifact> getMiddleManFor(TransitiveInfoCollection prereq) {
+ if (prereq == null) {
+ return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
+ }
+ MiddlemanProvider provider = prereq.getProvider(MiddlemanProvider.class);
+ if (provider == null) {
+ return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
+ }
+ return provider.getMiddlemanArtifact();
+ }
+
/**
* Returns a JavaCompilationArgsProvider that fully encapsulates this compilation, based on the
* result of a call to build(). (that is, it contains the compile-time and runtime jars, separated