Skip to content

Commit

Permalink
Make it possible to retrieve JVM related artifacts from application_r…
Browse files Browse the repository at this point in the history
…esources.

When the AndroidApplicationResourceInfo sets shouldCompileJavaSrcs to False, the
android_binary rule will no longer compile its Java sources and expect to receive the compiled artifacts from the "application_resources" attribute.

RELNOTES: no
PiperOrigin-RevId: 355800362
  • Loading branch information
Googler authored and copybara-github committed Feb 5, 2021
1 parent 9b13391 commit bebbace
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void addCoverageSupport(
public ImmutableList<String> getAttributesWithJavaRuntimeDeps(RuleContext ruleContext) {
switch (ruleContext.getRule().getRuleClass()) {
case "android_binary":
return ImmutableList.of("deps");
return ImmutableList.of("application_resources", "deps");
default:
throw new UnsupportedOperationException("Only supported for top-level binaries");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class AndroidApplicationResourceInfo extends NativeInfo
private final Artifact rTxt;
private final Artifact resourcesZip;
private final Artifact databindingLayoutInfoZip;
private final Artifact buildStampJar;
private final boolean shouldCompileJavaSrcs;

AndroidApplicationResourceInfo(
Artifact resourceApk,
Expand All @@ -50,7 +52,9 @@ public class AndroidApplicationResourceInfo extends NativeInfo
Artifact mainDexProguardConfig,
Artifact rTxt,
Artifact resourcesZip,
Artifact databindingLayoutInfoZip) {
Artifact databindingLayoutInfoZip,
Artifact buildStampJar,
boolean shouldCompileJavaSrcs) {
this.resourceApk = resourceApk;
this.resourceJavaSrcJar = resourceJavaSrcJar;
this.resourceJavaClassJar = resourceJavaClassJar;
Expand All @@ -60,6 +64,8 @@ public class AndroidApplicationResourceInfo extends NativeInfo
this.rTxt = rTxt;
this.resourcesZip = resourcesZip;
this.databindingLayoutInfoZip = databindingLayoutInfoZip;
this.buildStampJar = buildStampJar;
this.shouldCompileJavaSrcs = shouldCompileJavaSrcs;
}

@Override
Expand Down Expand Up @@ -112,6 +118,22 @@ public Artifact getDatabindingLayoutInfoZip() {
return databindingLayoutInfoZip;
}

@Override
public Artifact getBuildStampJar() {
return buildStampJar;
}

/**
* A signal that indicates whether the android_binary rule should compile its Java sources in
* android_binary.srcs. When false, android_binary.application_resources will provide a JavaInfo
* that contains the compiled sources of the android_binary target. This step allows
* android_binary Java compilation to be offloaded to a Starlark rule.
*/
@Override
public boolean shouldCompileJavaSrcs() {
return shouldCompileJavaSrcs;
}

/** Provider for {@link AndroidApplicationResourceInfo}. */
public static class AndroidApplicationResourceInfoProvider
extends BuiltinProvider<AndroidApplicationResourceInfo>
Expand All @@ -131,7 +153,9 @@ public AndroidApplicationResourceInfoApi<Artifact> createInfo(
Object mainDexProguardConfig,
Object rTxt,
Object resourcesZip,
Object databindingLayoutInfoZip)
Object databindingLayoutInfoZip,
Object buildStampJar,
boolean shouldCompileJavaSrcs)
throws EvalException {

return new AndroidApplicationResourceInfo(
Expand All @@ -143,7 +167,9 @@ public AndroidApplicationResourceInfoApi<Artifact> createInfo(
fromNoneable(mainDexProguardConfig, Artifact.class),
fromNoneable(rTxt, Artifact.class),
fromNoneable(resourcesZip, Artifact.class),
fromNoneable(databindingLayoutInfoZip, Artifact.class));
fromNoneable(databindingLayoutInfoZip, Artifact.class),
fromNoneable(buildStampJar, Artifact.class),
shouldCompileJavaSrcs);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static RuleConfiguredTargetBuilder init(
NativeLibs nativeLibs =
NativeLibs.fromLinkedNativeDeps(
ruleContext,
ImmutableList.of("deps"),
ImmutableList.of("application_resources", "deps"),
androidSemantics.getNativeDepsFileName(),
cppSemantics);

Expand Down Expand Up @@ -268,6 +268,7 @@ private static RuleConfiguredTargetBuilder init(
AndroidApplicationResourceInfo.PROVIDER);

final ResourceApk resourceApk;
boolean shouldCompileJavaSrcs = true;
if (androidApplicationResourceInfo == null) {
resourceApk =
new RClassGeneratorActionBuilder()
Expand All @@ -280,6 +281,7 @@ private static RuleConfiguredTargetBuilder init(
resourceApk =
ResourceApk.fromAndroidApplicationResourceInfo(
ruleContext, dataContext.getAndroidConfig(), androidApplicationResourceInfo);
shouldCompileJavaSrcs = androidApplicationResourceInfo.shouldCompileJavaSrcs();
}

if (dataContext.useResourcePathShortening()) {
Expand All @@ -292,7 +294,11 @@ private static RuleConfiguredTargetBuilder init(

JavaCommon javaCommon =
AndroidCommon.createJavaCommonWithAndroidDataBinding(
ruleContext, javaSemantics, resourceApk.asDataBindingContext(), /* isLibrary */ false);
ruleContext,
javaSemantics,
resourceApk.asDataBindingContext(),
/* isLibrary */ false,
shouldCompileJavaSrcs);
javaSemantics.checkRule(ruleContext, javaCommon);
javaSemantics.checkForProtoLibraryAndJavaProtoLibraryOnSameProto(ruleContext, javaCommon);

Expand Down Expand Up @@ -408,6 +414,9 @@ public static RuleConfiguredTargetBuilder createAndroidBinary(
List<ProguardSpecProvider> proguardDeps = new ArrayList<>();
Iterables.addAll(
proguardDeps, ruleContext.getPrerequisites("deps", ProguardSpecProvider.PROVIDER));
Iterables.addAll(
proguardDeps,
ruleContext.getPrerequisites("application_resources", ProguardSpecProvider.PROVIDER));
if (ruleContext.getConfiguration().isCodeCoverageEnabled()
&& ruleContext.attributes().has("$jacoco_runtime", BuildType.LABEL)) {
proguardDeps.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ public class AndroidCommon {

public static final InstrumentationSpec ANDROID_COLLECTION_SPEC =
JavaCommon.JAVA_COLLECTION_SPEC.withDependencyAttributes(
"deps", "data", "exports", "instruments", "runtime_deps", "binary_under_test");
"application_resources",
"deps",
"data",
"exports",
"instruments",
"runtime_deps",
"binary_under_test");

private static final ImmutableSet<String> TRANSITIVE_ATTRIBUTES =
ImmutableSet.of("deps", "exports");
ImmutableSet.of("application_resources", "deps", "exports");

private static final int DEX_THREADS = 5;
private static final ResourceSet DEX_RESOURCE_SET =
Expand Down Expand Up @@ -846,7 +852,24 @@ static JavaCommon createJavaCommonWithAndroidDataBinding(
RuleContext ruleContext,
JavaSemantics semantics,
DataBindingContext dataBindingContext,
boolean isLibrary) {
boolean isLibrary,
boolean shouldCompileJavaSrcs) {

/**
* When within the context of an android_binary rule and shouldCompileJavaSrcs is False, the
* Java compilation happens within the Starlark rule.
*/
if (!isLibrary && !shouldCompileJavaSrcs) {
ImmutableList<TransitiveInfoCollection> runtimeDeps =
ImmutableList.copyOf(ruleContext.getPrerequisites("application_resources"));
return new JavaCommon(
ruleContext,
semantics,
ImmutableList.of(),
runtimeDeps, /* compileDeps */
runtimeDeps,
runtimeDeps); /* bothDeps */
}

ImmutableList<Artifact> ruleSources = ruleContext.getPrerequisiteArtifacts("srcs").list();

Expand All @@ -869,7 +892,11 @@ static JavaCommon createJavaCommonWithAndroidDataBinding(
bothDeps = JavaCommon.defaultDeps(ruleContext, semantics, ClasspathType.BOTH);
} else {
// Binary:
compileDeps = ImmutableList.copyOf(ruleContext.getPrerequisites("deps"));
compileDeps =
ImmutableList.<TransitiveInfoCollection>builder()
.addAll(ruleContext.getPrerequisites("application_resources"))
.addAll(ruleContext.getPrerequisites("deps"))
.build();
runtimeDeps = compileDeps;
bothDeps = compileDeps;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ public ConfiguredTarget create(RuleContext ruleContext)
// rules respectively.
JavaCommon javaCommon =
AndroidCommon.createJavaCommonWithAndroidDataBinding(
ruleContext, javaSemantics, resourceApk.asDataBindingContext(), /* isLibrary */ true);
ruleContext,
javaSemantics,
resourceApk.asDataBindingContext(),
/* isLibrary */ true,
/* shouldCompileJavaSrcs */ true);
javaSemantics.checkRule(ruleContext, javaCommon);
AndroidCommon androidCommon = new AndroidCommon(javaCommon);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public ConfiguredTarget create(RuleContext ruleContext)

JavaCommon javaCommon =
AndroidCommon.createJavaCommonWithAndroidDataBinding(
ruleContext, javaSemantics, resourceApk.asDataBindingContext(), /* isLibrary= */ false);
ruleContext,
javaSemantics,
resourceApk.asDataBindingContext(),
/* isLibrary */ false,
/* shouldCompileJavaSrcs */ true);
javaSemantics.checkRule(ruleContext, javaCommon);

// Use the regular Java javacopts, plus any extra needed for databinding. Enforcing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,9 @@ public Object getDefault(AttributeMap rule) {
.allowedFileTypes(NO_FILE)
.undocumented(
"Do not use this attribute. It's for the migration of "
+ "Android resource processing to Starlark only."))
+ "Android resource processing to Starlark only.")
.aspect(androidNeverlinkAspect)
.aspect(dexArchiveAspect, DexArchiveAspect.PARAM_EXTRACTOR))
// Nothing in the native rule reads this. This is only for facilitating the Starlark
// migration of the android rules.
.add(attr("stamp", TRISTATE).value(TriState.AUTO))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ private Function<Artifact, Artifact> desugarJarsIfRequested(
jars.add(rJar);
}

Artifact buildStampJar = getAndroidBuildStampJar(base);
if (buildStampJar != null) {
jars.add(buildStampJar);
}

// For android_* targets we need to honor their bootclasspath (nicer in general to do so)
NestedSet<Artifact> bootclasspath = getBootclasspath(base, ruleContext);

Expand Down Expand Up @@ -321,6 +326,11 @@ private static Iterable<Artifact> getProducedRuntimeJars(
jars.add(rJar);
}

Artifact buildStampJar = getAndroidBuildStampJar(base);
if (buildStampJar != null) {
jars.add(buildStampJar);
}

return jars.build();
}
return null;
Expand Down Expand Up @@ -349,6 +359,15 @@ private static Artifact getAndroidLibraryRJar(ConfiguredTarget base) {
return null;
}

private static Artifact getAndroidBuildStampJar(ConfiguredTarget base) {
AndroidApplicationResourceInfo provider =
(AndroidApplicationResourceInfo) base.get(AndroidApplicationResourceInfo.PROVIDER.getKey());
if (provider != null && provider.getBuildStampJar() != null) {
return provider.getBuildStampJar();
}
return null;
}

private static boolean checkBasenameClash(Iterable<Artifact> artifacts) {
HashSet<String> seen = new HashSet<>();
for (Artifact artifact : artifacts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ public interface AndroidApplicationResourceInfoApi<FileT extends FileApi> extend
@Nullable
FileT getDatabindingLayoutInfoZip();

/** The build stamp jar file */
@StarlarkMethod(
name = "build_stamp_jar",
doc = "The build stamp jar file.",
documented = false,
allowReturnNones = true,
structField = true)
@Nullable
FileT getBuildStampJar();

/** Whether to compile Java Srcs within the android_binary rule */
@StarlarkMethod(
name = "should_compile_java_srcs",
doc = "Whether to compile Java Srcs within the android_binary rule.",
documented = false,
allowReturnNones = false,
structField = true)
boolean shouldCompileJavaSrcs();

/** Provider for {@link AndroidApplicationResourceInfoApi}. */
@StarlarkBuiltin(
name = "Provider",
Expand Down Expand Up @@ -207,6 +226,16 @@ interface AndroidApplicationResourceInfoApiProvider<FileT extends FileApi> exten
named = true,
doc = "",
defaultValue = "None"),
@Param(
name = "build_stamp_jar",
allowedTypes = {
@ParamType(type = FileApi.class),
@ParamType(type = NoneType.class),
},
named = true,
doc = "",
defaultValue = "None"),
@Param(name = "should_compile_java_srcs", named = true, doc = "", defaultValue = "True"),
},
selfCall = true)
@StarlarkConstructor
Expand All @@ -219,7 +248,9 @@ AndroidApplicationResourceInfoApi<FileT> createInfo(
Object mainDexProguardConfig,
Object rTxt,
Object resourcesZip,
Object databindingLayoutInfoZip)
Object databindingLayoutInfoZip,
Object buildStampJar,
boolean shouldCompileJava)
throws EvalException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public AndroidApplicationResourceInfoApi<FileApi> createInfo(
Object mainDexProguardConfig,
Object rTxt,
Object resourcesZip,
Object databindingLayoutInfoZip)
Object databindingLayoutInfoZip,
Object buildStampJar,
boolean shouldCompileJavaSrcs)
throws EvalException {
return null;
}
Expand Down
Loading

0 comments on commit bebbace

Please sign in to comment.