Skip to content

Commit

Permalink
Improve Java-like rulesets support.
Browse files Browse the repository at this point in the history
JavaInfo constructor (class JavaInfoApi) is extended with parameters compile_jdeps, native_headers, generated_class_jar, generated_source_jar.

Class OutputJarApi exposes all the added data and additionally jdeps and manifest_file. Field ijar is renamed to compile_jar (old ijar getter is left in for compatibility).

Class JavaRuleOutptJarsProviderApi: outputs.jdeps and outputs.manifest_file is deprecated in favour of fields in JarOutputs.

Design document: https://docs.google.com/document/d/10isTEK5W9iCPp4BIyGBrLY5iti3Waaam6EeGVSjq3r8/edit#bookmark=id.vta5s12rfsxt
Related issue bazelbuild#11928

PiperOrigin-RevId: 364291642
  • Loading branch information
comius authored and copybara-github committed Mar 22, 2021
1 parent de75b42 commit 72b1c2c
Show file tree
Hide file tree
Showing 21 changed files with 625 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.devtools.build.lib.rules.java.JavaConfiguration.ImportDepsCheckingLevel;
import com.google.devtools.build.lib.rules.java.JavaInfo;
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.JavaRuntimeInfo;
import com.google.devtools.build.lib.rules.java.JavaSemantics;
import com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider;
Expand Down Expand Up @@ -137,7 +138,7 @@ public ConfiguredTarget create(RuleContext ruleContext)

JavaRuleOutputJarsProvider.Builder jarProviderBuilder =
new JavaRuleOutputJarsProvider.Builder()
.addOutputJar(mergedJar, null /* ijar */, null /* manifestProto */, ImmutableList.of());
.addOutputJar(OutputJar.builder().setClassJar(mergedJar).build());

ImmutableList<TransitiveInfoCollection> targets =
ImmutableList.<TransitiveInfoCollection>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,19 +651,23 @@ public RuleConfiguredTargetBuilder addTransitiveInfoProviders(
OutputJar resourceJar = null;
if (resourceApk.getResourceJavaClassJar() != null && resourceSourceJar != null) {
resourceJar =
new OutputJar(
resourceApk.getResourceJavaClassJar(),
null /* ijar */,
outputs.manifestProto(),
ImmutableList.of(resourceSourceJar));
OutputJar.builder()
.setClassJar(resourceApk.getResourceJavaClassJar())
.addSourceJar(resourceSourceJar)
.build();
javaRuleOutputJarsProviderBuilder.addOutputJar(resourceJar);
}

JavaRuleOutputJarsProvider ruleOutputJarsProvider =
javaRuleOutputJarsProviderBuilder
.addOutputJar(classJar, iJar, outputs.manifestProto(), ImmutableList.of(srcJar))
.setJdeps(outputs.depsProto())
.setNativeHeaders(outputs.nativeHeader())
.addOutputJar(
OutputJar.builder()
.fromJavaCompileOutputs(outputs)
.setCompileJar(iJar)
.setCompileJdeps(
javaCommon.getJavaCompilationArtifacts().getCompileTimeDependencyArtifact())
.addSourceJar(srcJar)
.build())
.build();
JavaSourceJarsProvider sourceJarsProvider = javaSourceJarsProviderBuilder.build();
JavaCompilationArgsProvider compilationArgsProvider = javaCompilationArgs;
Expand Down Expand Up @@ -879,10 +883,8 @@ static JavaCommon createJavaCommonWithAndroidDataBinding(
ImmutableList<Artifact> dataBindingSources =
dataBindingContext.getAnnotationSourceFiles(ruleContext);

ImmutableList<Artifact> srcs = ImmutableList.<Artifact>builder()
.addAll(ruleSources)
.addAll(dataBindingSources)
.build();
ImmutableList<Artifact> srcs =
ImmutableList.<Artifact>builder().addAll(ruleSources).addAll(dataBindingSources).build();

ImmutableList<TransitiveInfoCollection> compileDeps;
ImmutableList<TransitiveInfoCollection> runtimeDeps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.google.devtools.build.lib.rules.java.JavaInfo;
import com.google.devtools.build.lib.rules.java.JavaPrimaryClassProvider;
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.JavaRuntimeClasspathProvider;
import com.google.devtools.build.lib.rules.java.JavaRuntimeInfo;
import com.google.devtools.build.lib.rules.java.JavaSemantics;
Expand Down Expand Up @@ -239,10 +240,13 @@ public ConfiguredTarget create(RuleContext ruleContext)
JavaRuleOutputJarsProvider.Builder javaRuleOutputJarsProviderBuilder =
JavaRuleOutputJarsProvider.builder()
.addOutputJar(
classJar,
classJar,
outputs.manifestProto(),
srcJar == null ? ImmutableList.<Artifact>of() : ImmutableList.of(srcJar));
OutputJar.builder()
.fromJavaCompileOutputs(outputs)
.setCompileJar(classJar)
.setCompileJdeps(
javaCommon.getJavaCompilationArtifacts().getCompileTimeDependencyArtifact())
.addSourceJar(srcJar)
.build());

javaArtifactsBuilder.setCompileTimeDependencies(outputs.depsProto());

Expand All @@ -261,7 +265,6 @@ public ConfiguredTarget create(RuleContext ruleContext)
JavaTargetAttributes attributes = attributesBuilder.build();
addJavaClassJarToArtifactsBuilder(javaArtifactsBuilder, attributes, classJar);

javaRuleOutputJarsProviderBuilder.setJdeps(outputs.depsProto());
helper.createCompileAction(outputs);
helper.createSourceJarAction(srcJar, outputs.genSource());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package com.google.devtools.build.lib.rules.android;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
Expand Down Expand Up @@ -160,11 +159,10 @@ public JavaRuleOutputJarsProvider.OutputJar getIdlOutput() {
}

Artifact idlSourceJar = getIdeInfoProvider().getIdlSourceJar();
return new OutputJar(
getIdeInfoProvider().getIdlClassJar(),
null,
null,
idlSourceJar == null ? ImmutableList.<Artifact>of() : ImmutableList.of(idlSourceJar));
return OutputJar.builder()
.setClassJar(getIdeInfoProvider().getIdlClassJar())
.addSourceJar(idlSourceJar)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.devtools.build.lib.rules.java.JavaCompilationInfoProvider;
import com.google.devtools.build.lib.rules.java.JavaInfo;
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.JavaSourceJarsProvider;
import com.google.devtools.build.lib.rules.java.ProguardSpecProvider;
import com.google.devtools.build.lib.starlarkbuildapi.android.AndroidBinaryDataSettingsApi;
Expand Down Expand Up @@ -585,7 +586,8 @@ private static JavaInfo getJavaInfoForRClassJar(Artifact rClassJar, Artifact rCl
.addProvider(
JavaRuleOutputJarsProvider.class,
JavaRuleOutputJarsProvider.builder()
.addOutputJar(rClassJar, null, null, ImmutableList.of(rClassSrcJar))
.addOutputJar(
OutputJar.builder().setClassJar(rClassJar).addSourceJar(rClassSrcJar).build())
.build())
.addProvider(
JavaCompilationArgsProvider.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.google.devtools.build.lib.rules.cpp.LibraryToLink;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider.ClasspathType;
import com.google.devtools.build.lib.rules.java.JavaConfiguration.OneVersionEnforcementLevel;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.rules.java.proto.GeneratedExtensionRegistryProvider;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.util.Pair;
Expand Down Expand Up @@ -204,10 +205,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder =
JavaRuleOutputJarsProvider.builder()
.addOutputJar(
/* classJar= */ classJar,
/* iJar= */ null,
/* manifestProto= */ outputs.manifestProto(),
/* sourceJars= */ ImmutableList.of(srcJar));
OutputJar.builder().fromJavaCompileOutputs(outputs).addSourceJar(srcJar).build());

JavaTargetAttributes attributes = attributesBuilder.build();
List<Artifact> nativeLibraries = attributes.getNativeLibraries();
Expand Down Expand Up @@ -238,7 +236,6 @@ public ConfiguredTarget create(RuleContext ruleContext)
ruleOutputJarsProviderBuilder,
javaSourceJarsProviderBuilder);
javaArtifactsBuilder.setCompileTimeDependencies(outputs.depsProto());
ruleOutputJarsProviderBuilder.setJdeps(outputs.depsProto());

JavaCompilationArtifacts javaArtifacts = javaArtifactsBuilder.build();
common.setJavaCompilationArtifacts(javaArtifacts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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.cpp.LibraryToLink;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down Expand Up @@ -117,7 +118,11 @@ public ConfiguredTarget create(RuleContext ruleContext)
JavaRuleOutputJarsProvider.builder();
for (Artifact jar : jars) {
ruleOutputJarsProviderBuilder.addOutputJar(
jar, compilationToRuntimeJarMap.inverse().get(jar), null /* manifestProto */, srcJars);
OutputJar.builder()
.setClassJar(jar)
.setCompileJar(compilationToRuntimeJarMap.inverse().get(jar))
.addSourceJar(srcJar)
.build());
}

NestedSet<Artifact> proguardSpecs = new ProguardLibrary(ruleContext).collectProguardSpecs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.rules.java.JavaPluginInfoProvider.JavaPluginInfo;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
Expand Down Expand Up @@ -424,6 +425,11 @@ public JavaInfo javaInfo(
FileApi outputJarApi,
Object compileJarApi,
Object sourceJarApi,
Object compileJdepsApi,
Object generatedClassJarApi,
Object generatedSourceJarApi,
Object nativeHeadersJarApi,
Object manifestProtoApi,
Boolean neverlink,
Sequence<?> deps,
Sequence<?> runtimeDeps,
Expand All @@ -434,20 +440,32 @@ public JavaInfo javaInfo(
Artifact outputJar = (Artifact) outputJarApi;
@Nullable Artifact compileJar = nullIfNone(compileJarApi, Artifact.class);
@Nullable Artifact sourceJar = nullIfNone(sourceJarApi, Artifact.class);
@Nullable Artifact compileJdeps = nullIfNone(compileJdepsApi, Artifact.class);
@Nullable Artifact generatedClassJar = nullIfNone(generatedClassJarApi, Artifact.class);
@Nullable Artifact generatedSourceJar = nullIfNone(generatedSourceJarApi, Artifact.class);
@Nullable Artifact nativeHeadersJar = nullIfNone(nativeHeadersJarApi, Artifact.class);
@Nullable Artifact manifestProto = nullIfNone(manifestProtoApi, Artifact.class);
@Nullable Artifact jdeps = nullIfNone(jdepsApi, Artifact.class);
checkSequenceOfJavaInfo(deps, "deps");
checkSequenceOfJavaInfo(runtimeDeps, "runtime_deps");
checkSequenceOfJavaInfo(exports, "exports");
return JavaInfoBuildHelper.getInstance()
.createJavaInfo(
outputJar,
compileJar,
sourceJar,
OutputJar.builder()
.setClassJar(outputJar)
.setCompileJar(compileJar)
.setCompileJdeps(compileJdeps)
.setGeneratedClassJar(generatedClassJar)
.setGeneratedSourceJar(generatedSourceJar)
.setNativeHeadersJar(nativeHeadersJar)
.setManifestProto(manifestProto)
.setJdeps(jdeps)
.addSourceJar(sourceJar)
.build(),
neverlink,
(Sequence<JavaInfo>) deps,
(Sequence<JavaInfo>) runtimeDeps,
(Sequence<JavaInfo>) exports,
jdeps,
thread.getCallerLocation());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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.JavaCompilationArgsProvider.ClasspathType;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.shell.ShellUtils;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import java.util.ArrayList;
Expand All @@ -62,51 +63,39 @@ public static JavaInfoBuildHelper getInstance() {
/**
* Creates JavaInfo instance from outputJar.
*
* @param outputJar the jar that was created as a result of a compilation (e.g. javac, scalac,
* etc)
* @param compileJar Jar added as a compile-time dependency to other rules. Typically produced by
* ijar.
* @param sourceJar the source jar that was used to create the output jar
* @param outputs the artifacts that were created as a result of a compilation (e.g. javac,
* scalac, etc)
* @param neverlink if true only use this library for compilation and not at runtime
* @param compileTimeDeps compile time dependencies that were used to create the output jar
* @param runtimeDeps runtime dependencies that are needed for this library
* @param exports libraries to make available for users of this library. <a
* href="https://docs.bazel.build/versions/master/be/java.html#java_library"
* target="_top">java_library.exports</a>
* @param jdeps optional jdeps information for outputJar
* @return new created JavaInfo instance
*/
JavaInfo createJavaInfo(
Artifact outputJar,
@Nullable Artifact compileJar,
@Nullable Artifact sourceJar,
OutputJar outputs,
Boolean neverlink,
Sequence<JavaInfo> compileTimeDeps,
Sequence<JavaInfo> runtimeDeps,
Sequence<JavaInfo> exports,
@Nullable Artifact jdeps,
Location location) {
ImmutableList<Artifact> sourceJars =
sourceJar != null ? ImmutableList.of(sourceJar) : ImmutableList.of();
JavaInfo.Builder javaInfoBuilder = JavaInfo.Builder.create();
javaInfoBuilder.setLocation(location);

JavaCompilationArgsProvider.Builder javaCompilationArgsBuilder =
JavaCompilationArgsProvider.builder();

if (!neverlink) {
javaCompilationArgsBuilder.addRuntimeJar(outputJar);
javaCompilationArgsBuilder.addRuntimeJar(outputs.getClassJar());
}
if (compileJar != null) {
if (outputs.getCompileJar() != null) {
javaCompilationArgsBuilder.addDirectCompileTimeJar(
/* interfaceJar= */ compileJar, /* fullJar= */ outputJar);
/* interfaceJar= */ outputs.getCompileJar(), /* fullJar= */ outputs.getClassJar());
}

JavaRuleOutputJarsProvider javaRuleOutputJarsProvider =
JavaRuleOutputJarsProvider.builder()
.addOutputJar(outputJar, compileJar, null /* manifestProto */, sourceJars)
.setJdeps(jdeps)
.build();
JavaRuleOutputJarsProvider.builder().addOutputJar(outputs).build();
javaInfoBuilder.addProvider(JavaRuleOutputJarsProvider.class, javaRuleOutputJarsProvider);

ClasspathType type = neverlink ? COMPILE_ONLY : BOTH;
Expand All @@ -130,19 +119,20 @@ JavaInfo createJavaInfo(

javaInfoBuilder.addProvider(
JavaSourceJarsProvider.class,
createJavaSourceJarsProvider(sourceJars, concat(compileTimeDeps, runtimeDeps, exports)));
createJavaSourceJarsProvider(
outputs.getSourceJars(), concat(compileTimeDeps, runtimeDeps, exports)));

javaInfoBuilder.addProvider(
JavaGenJarsProvider.class,
JavaGenJarsProvider.create(
false,
null,
null,
outputs.getGeneratedClassJar(),
outputs.getGeneratedSourceJar(),
JavaPluginInfoProvider.empty(),
JavaInfo.fetchProvidersFromList(
concat(compileTimeDeps, exports), JavaGenJarsProvider.class)));

javaInfoBuilder.setRuntimeJars(ImmutableList.of(outputJar));
javaInfoBuilder.setRuntimeJars(ImmutableList.of(outputs.getClassJar()));

return javaInfoBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import static com.google.devtools.build.lib.collect.nestedset.Order.STABLE_ORDER;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
Expand All @@ -29,6 +28,7 @@
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.rules.cpp.LibraryToLink;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider.ClasspathType;
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider.OutputJar;
import com.google.devtools.build.lib.rules.java.proto.GeneratedExtensionRegistryProvider;

/** Implementation for the java_library rule. */
Expand Down Expand Up @@ -117,11 +117,17 @@ final ConfiguredTarget init(
if (attributes.hasSources() && jar != null) {
iJar = helper.createCompileTimeJarAction(jar, javaArtifactsBuilder);
}
JavaCompilationArtifacts javaArtifacts = javaArtifactsBuilder.build();

JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder =
JavaRuleOutputJarsProvider.builder()
.addOutputJar(classJar, iJar, outputs.manifestProto(), ImmutableList.of(srcJar))
.setJdeps(outputs.depsProto())
.setNativeHeaders(outputs.nativeHeader());
.addOutputJar(
OutputJar.builder()
.fromJavaCompileOutputs(outputs)
.setCompileJar(iJar)
.setCompileJdeps(javaArtifacts.getCompileTimeDependencyArtifact())
.addSourceJar(srcJar)
.build());

GeneratedExtensionRegistryProvider generatedExtensionRegistryProvider = null;
if (includeGeneratedExtensionRegistry) {
Expand All @@ -136,7 +142,7 @@ final ConfiguredTarget init(
}

boolean neverLink = JavaCommon.isNeverLink(ruleContext);
JavaCompilationArtifacts javaArtifacts = javaArtifactsBuilder.build();

common.setJavaCompilationArtifacts(javaArtifacts);
common.setClassPathFragment(
new ClasspathConfiguredFragment(
Expand Down
Loading

0 comments on commit 72b1c2c

Please sign in to comment.