Skip to content

Commit

Permalink
Add a source_jars variable to OutputJar.
Browse files Browse the repository at this point in the history
Having a correlation between an output jar and a source jar is not enough.
There may be situations when an output jar is generated from more source jars,
not just one. We need this flexibility especially in Skylark for the java
sandwich, when the user can compile multiple source jars.

--
PiperOrigin-RevId: 149510534
MOS_MIGRATED_REVID=149510534
  • Loading branch information
iirina authored and vladmos committed Mar 8, 2017
1 parent 4950111 commit ef65171
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.google.devtools.build.lib.rules.cpp.CppConfiguration;
import com.google.devtools.build.lib.rules.java.JavaExportsProvider;
import com.google.devtools.build.lib.rules.java.JavaGenJarsProvider;
import com.google.devtools.build.lib.rules.java.JavaProvider;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.rules.java.JavaSourceInfoProvider;
Expand Down Expand Up @@ -335,7 +336,7 @@ private AndroidStudioInfoFilesProvider createIdeBuildArtifact(

// Java rules
JavaRuleOutputJarsProvider outputJarsProvider =
base.getProvider(JavaRuleOutputJarsProvider.class);
JavaProvider.getProvider(JavaRuleOutputJarsProvider.class, base);
if (outputJarsProvider != null && !androidStudioInfoSemantics.suppressJavaRuleInfo(base)) {
JavaIdeInfo javaIdeInfo =
makeJavaIdeInfo(base, ruleContext, outputJarsProvider, providerBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider;
import com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts;
import com.google.devtools.build.lib.rules.java.JavaHelper;
import com.google.devtools.build.lib.rules.java.JavaProvider;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.rules.java.JavaRuntimeJarProvider;
Expand Down Expand Up @@ -121,9 +122,10 @@ public ConfiguredTarget create(RuleContext ruleContext)
ruleContext.registerAction(createAarNativeLibsFilterActions(ruleContext, aar, nativeLibs));

JavaRuleOutputJarsProvider.Builder jarProviderBuilder = new JavaRuleOutputJarsProvider.Builder()
.addOutputJar(mergedJar, null, null);
.addOutputJar(mergedJar, null, ImmutableList.<Artifact>of());
for (TransitiveInfoCollection export : ruleContext.getPrerequisites("exports", Mode.TARGET)) {
for (OutputJar jar : export.getProvider(JavaRuleOutputJarsProvider.class).getOutputJars()) {
for (OutputJar jar :
JavaProvider.getProvider(JavaRuleOutputJarsProvider.class, export).getOutputJars()) {
jarProviderBuilder.addOutputJar(jar);
filesToBuildBuilder.add(jar.getClassJar());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,13 +746,13 @@ public RuleConfiguredTargetBuilder addTransitiveInfoProviders(
}
OutputJar resourceJar = null;
if (resourceClassJar != null && resourceSourceJar != null) {
resourceJar = new OutputJar(resourceClassJar, null, resourceSourceJar);
resourceJar = new OutputJar(resourceClassJar, null, ImmutableList.of(resourceSourceJar));
javaRuleOutputJarsProviderBuilder.addOutputJar(resourceJar);
}

JavaRuleOutputJarsProvider ruleOutputJarsProvider =
javaRuleOutputJarsProviderBuilder
.addOutputJar(classJar, iJar, srcJar)
.addOutputJar(classJar, iJar, ImmutableList.of(srcJar))
.setJdeps(outputDepsProto)
.build();
JavaSourceJarsProvider sourceJarsProvider = javaSourceJarsProviderBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.common.base.Function;
import com.google.common.collect.ImmutableCollection;
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.collect.nestedset.NestedSet;
Expand Down Expand Up @@ -215,10 +216,11 @@ public JavaRuleOutputJarsProvider.OutputJar getIdlOutput() {
return null;
}

Artifact idlSourceJar = getIdeInfoProvider().getIdlSourceJar();
return new OutputJar(
getIdeInfoProvider().getIdlClassJar(),
null,
getIdeInfoProvider().getIdlSourceJar()
idlSourceJar == null ? ImmutableList.<Artifact>of() : ImmutableList.of(idlSourceJar)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
.addAllTransitiveSourceJars(common.collectTransitiveSourceJars(srcJar));
Artifact classJar = ruleContext.getImplicitOutputArtifact(JavaSemantics.JAVA_BINARY_CLASS_JAR);
JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder =
JavaRuleOutputJarsProvider.builder().addOutputJar(classJar, null /* iJar */, srcJar);
JavaRuleOutputJarsProvider.builder().addOutputJar(
classJar, null /* iJar */, ImmutableList.of(srcJar));

CppConfiguration cppConfiguration = ruleContext.getConfiguration().getFragment(
CppConfiguration.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected void collect(CcLinkParams.Builder builder, boolean linkingStatically,
JavaRuleOutputJarsProvider.builder();
for (Artifact jar : jars) {
ruleOutputJarsProviderBuilder.addOutputJar(
jar, compilationToRuntimeJarMap.inverse().get(jar), srcJar);
jar, compilationToRuntimeJarMap.inverse().get(jar), srcJars);
}

NestedSet<Artifact> proguardSpecs = new ProguardLibrary(ruleContext).collectProguardSpecs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected void collect(CcLinkParams.Builder builder, boolean linkingStatically,

JavaRuleOutputJarsProvider ruleOutputJarsProvider =
JavaRuleOutputJarsProvider.builder()
.addOutputJar(classJar, iJar, srcJar)
.addOutputJar(classJar, iJar, ImmutableList.of(srcJar))
.setJdeps(outputDepsProto)
.build();
JavaCompilationArgsProvider compilationArgsProvider =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public final class JavaProvider extends SkylarkClassObject implements Transitive
ImmutableSet.of(
JavaCompilationArgsProvider.class,
JavaSourceJarsProvider.class,
ProtoJavaApiInfoAspectProvider.class);
ProtoJavaApiInfoAspectProvider.class,
JavaRuleOutputJarsProvider.class
);

private final TransitiveInfoProviderMap providers;

Expand Down Expand Up @@ -77,6 +79,9 @@ public static JavaProvider merge(List<JavaProvider> providers) {
.addProvider(
ProtoJavaApiInfoAspectProvider.class,
ProtoJavaApiInfoAspectProvider.merge(protoJavaApiInfoAspectProviders))
// When a rule merges multiple JavaProviders, its purpose is to pass on information, so
// it doesn't have any output jars.
.addProvider(JavaRuleOutputJarsProvider.class, JavaRuleOutputJarsProvider.builder().build())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
package com.google.devtools.build.lib.rules.java;

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.TransitiveInfoProvider;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.util.Preconditions;
import javax.annotation.Nullable;

Expand All @@ -46,13 +48,15 @@ public final class JavaRuleOutputJarsProvider implements TransitiveInfoProvider
public static class OutputJar {
@Nullable private final Artifact classJar;
@Nullable private final Artifact iJar;
@Nullable private final Artifact srcJar;
@Nullable private final ImmutableList<Artifact> srcJars;

public OutputJar(
@Nullable Artifact classJar, @Nullable Artifact iJar, @Nullable Artifact srcJar) {
@Nullable Artifact classJar,
@Nullable Artifact iJar,
@Nullable Iterable<Artifact> srcJars) {
this.classJar = classJar;
this.iJar = iJar;
this.srcJar = srcJar;
this.srcJars = ImmutableList.copyOf(srcJars);
}

@Nullable
Expand Down Expand Up @@ -80,12 +84,25 @@ public Artifact getIJar() {
@Nullable
@SkylarkCallable(
name = "source_jar",
doc = "A sources archive file.",
doc = "A sources archive file. Deprecated. Kept for migration reasons. "
+ "Please use source_jars instead.",
allowReturnNones = true,
structField = true
)
@Deprecated
public Artifact getSrcJar() {
return srcJar;
return Iterables.getOnlyElement(srcJars, null);
}

@Nullable
@SkylarkCallable(
name = "source_jars",
doc = "A list of sources archive files.",
allowReturnNones = true,
structField = true
)
public SkylarkList<Artifact> getSrcJars() {
return SkylarkList.createImmutable(srcJars);
}
}

Expand Down Expand Up @@ -128,9 +145,9 @@ public static class Builder {
public Builder addOutputJar(
@Nullable Artifact classJar,
@Nullable Artifact iJar,
@Nullable Artifact sourceJar) {
Preconditions.checkState(classJar != null || iJar != null || sourceJar != null);
outputJars.add(new OutputJar(classJar, iJar, sourceJar));
@Nullable ImmutableList<Artifact> sourceJars) {
Preconditions.checkState(classJar != null || iJar != null || !sourceJars.isEmpty());
outputJars.add(new OutputJar(classJar, iJar, sourceJars));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public JavaProvider createJavaCompileAction(
MiddlemanProvider hostJavabaseProvider = hostJavabase.getProvider(MiddlemanProvider.class);

NestedSet<Artifact> hostJavabaseArtifacts =
hostJavabase == null
hostJavabaseProvider == null
? NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)
: hostJavabaseProvider.getMiddlemanArtifact();
JavaToolchainProvider javaToolchainProvider =
Expand All @@ -144,11 +144,16 @@ public JavaProvider createJavaCompileAction(
javaToolchainProvider,
hostJavabaseArtifacts,
SkylarkList.createImmutable(ImmutableList.<Artifact>of()));
JavaRuleOutputJarsProvider javaRuleOutputJarsProvider =
JavaRuleOutputJarsProvider.builder().addOutputJar(
new JavaRuleOutputJarsProvider.OutputJar(outputJar, /* ijar */ null, sourceJars))
.build();
return JavaProvider.Builder.create()
.addProvider(
JavaCompilationArgsProvider.class,
helper.buildCompilationArgsProvider(artifacts, true))
.addProvider(JavaSourceJarsProvider.class, createJavaSourceJarsProvider(sourceJars))
.addProvider(JavaRuleOutputJarsProvider.class, javaRuleOutputJarsProvider)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void addProviders(ConfiguredAspect.Builder aspect) {
// TODO(carmi): Expose to native rules
JavaRuleOutputJarsProvider ruleOutputJarsProvider =
JavaRuleOutputJarsProvider.builder()
.addOutputJar(outputJar, compileTimeJar, sourceJar)
.addOutputJar(outputJar, compileTimeJar, ImmutableList.of(sourceJar))
.build();
JavaSourceJarsProvider sourceJarsProvider =
JavaSourceJarsProvider.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void addProviders(ConfiguredAspect.Builder aspect) {
// TODO(carmi): Expose to native rules
JavaRuleOutputJarsProvider ruleOutputJarsProvider =
JavaRuleOutputJarsProvider.builder()
.addOutputJar(outputJar, compileTimeJar, sourceJar)
.addOutputJar(outputJar, compileTimeJar, ImmutableList.of(sourceJar))
.build();
JavaSourceJarsProvider sourceJarsProvider =
JavaSourceJarsProvider.create(
Expand Down

0 comments on commit ef65171

Please sign in to comment.