Skip to content

Commit

Permalink
Propogate tags for the AndroidBinary actions (bazelbuild#9)
Browse files Browse the repository at this point in the history
Tags are not propagated from targets to actions for Android rules. bazelbuild#8830

This PR adds basic propagation of tags from the android_binary target it's actions.

Testing with aquery with the Android repo:

./bazelw aquery 'mnemonic(RClassGenerator, //apps/mushroom:mushroom-gms)'
./bazelw aquery 'mnemonic(JavaDeployJar, //apps/mushroom:mushroom-gms)'
./bazelw aquery 'mnemonic(ApkBuilder, //apps/mushroom:mushroom-gms)'
etc.
  • Loading branch information
antonlopyrevsc authored and nkorostelev-sc committed Jul 14, 2020
1 parent 7cfe416 commit 8e2e050
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
.bazelversion
# User-specific .bazelrc
user.bazelrc

# Intellij
.aswb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,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.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleErrorConsumer;
import com.google.devtools.build.lib.packages.TriState;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.packages.*;
import com.google.devtools.build.lib.rules.android.AndroidBinaryMobileInstall.MobileInstallResourceApks;
import com.google.devtools.build.lib.rules.android.AndroidRuleClasses.MultidexMode;
import com.google.devtools.build.lib.rules.android.ProguardHelper.ProguardOutput;
Expand Down Expand Up @@ -262,7 +258,9 @@ private static RuleConfiguredTargetBuilder init(
ResourceFilterFactory.fromRuleContextAndAttrs(ruleContext),
ruleContext.getExpander().withDataLocations().tokenized("nocompress_extensions"),
ruleContext.attributes().get("crunch_png", Type.BOOLEAN),
DataBinding.contextFrom(ruleContext, dataContext.getAndroidConfig()));
DataBinding.contextFrom(ruleContext, dataContext.getAndroidConfig()),
TargetUtils.getExecutionInfo(
ruleContext.getRule(), ruleContext.isAllowTagsPropagation()));

AndroidApplicationResourceInfo androidApplicationResourceInfo =
ruleContext.getPrerequisite(
Expand All @@ -276,6 +274,8 @@ private static RuleConfiguredTargetBuilder init(
new RClassGeneratorActionBuilder()
.withDependencies(resourceDeps)
.finalFields(!shrinkResourceCycles)
.setExecutionInfo(TargetUtils.getExecutionInfo(
ruleContext.getRule(), ruleContext.isAllowTagsPropagation()))
.setClassJarOut(
dataContext.createOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR))
.build(dataContext, processedAndroidData);
Expand Down Expand Up @@ -525,7 +525,7 @@ public static RuleConfiguredTargetBuilder createAndroidBinary(
java8LegacyDex = getDxArtifact(ruleContext, "_java8_legacy.dex.zip");
Artifact androidJar = AndroidSdkProvider.fromRuleContext(ruleContext).getAndroidJar();
ruleContext.registerAction(
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setExecutable(
ruleContext.getExecutablePrerequisite(
"$build_java8_legacy_dex", TransitionMode.HOST))
Expand All @@ -546,7 +546,7 @@ public static RuleConfiguredTargetBuilder createAndroidBinary(
// Append legacy .dex library to app's .dex files
finalClassesDex = getDxArtifact(ruleContext, "_final_classes.dex.zip");
ruleContext.registerAction(
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.useDefaultShellEnvironment()
.setMnemonic("AppendJava8LegacyDex")
.setProgressMessage("Adding Java 8 legacy library for %s", ruleContext.getLabel())
Expand Down Expand Up @@ -616,7 +616,7 @@ public static RuleConfiguredTargetBuilder createAndroidBinary(
AndroidRuleClasses.INSTRUMENTATION_TEST_CHECK_RESULTS);

SpawnAction.Builder checkAction =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setExecutable(checker)
.addInput(targetManifest)
.addInput(instrumentationManifest)
Expand Down Expand Up @@ -1157,7 +1157,7 @@ private static DexingOutput dex(
.addExecPath("--output_zip", classesDex)
.build();
ruleContext.registerAction(
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.useDefaultShellEnvironment()
.setMnemonic("MergeDexZips")
.setProgressMessage("Merging dex shards for %s", ruleContext.getLabel())
Expand Down Expand Up @@ -1367,20 +1367,20 @@ private static SpecialArtifact createSharderAction(
ImmutableList<Artifact> dexArchives,
@Nullable Artifact mainDexList,
Collection<String> dexopts,
@Nullable Artifact inclusionFilterJar) {
@Nullable Artifact inclusionFilterJar) throws InterruptedException {
SpecialArtifact outputTree =
ruleContext.getTreeArtifact(
ruleContext.getUniqueDirectory("dexsplits"), ruleContext.getBinOrGenfilesDirectory());

SpawnAction.Builder shardAction =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.useDefaultShellEnvironment()
.setMnemonic("ShardForMultidex")
.setProgressMessage(
"Assembling dex files for %s", ruleContext.getLabel().getCanonicalForm())
.setExecutable(
ruleContext.getExecutablePrerequisite("$dexsharder", TransitionMode.HOST))
.addInputs(dexArchives)
.addInputs(dexArchives)
.addOutput(outputTree);

CustomCommandLine.Builder shardCommandLine =
Expand Down Expand Up @@ -1442,7 +1442,7 @@ private static Artifact createTemplatedMergerActions(
}

private static void createZipMergeAction(
RuleContext ruleContext, Artifact inputTree, Artifact outputZip) {
RuleContext ruleContext, Artifact inputTree, Artifact outputZip) throws InterruptedException {
CustomCommandLine args =
CustomCommandLine.builder()
.add("--normalize")
Expand Down Expand Up @@ -1481,9 +1481,9 @@ private static void createDexMergerAction(
ImmutableList<Artifact> dexArchives,
Artifact classesDex,
@Nullable Artifact mainDexList,
Collection<String> dexopts) {
Collection<String> dexopts) throws InterruptedException {
SpawnAction.Builder dexmerger =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.useDefaultShellEnvironment()
.setExecutable(ruleContext.getExecutablePrerequisite("$dexmerger", TransitionMode.HOST))
.setMnemonic("DexMerger")
Expand Down Expand Up @@ -1563,7 +1563,7 @@ private static Map<Artifact, Artifact> collectDexArchives(
AndroidCommon common,
List<String> dexopts,
AndroidSemantics semantics,
Function<Artifact, Artifact> derivedJarFunction) {
Function<Artifact, Artifact> derivedJarFunction) throws InterruptedException {
DexArchiveProvider.Builder result = new DexArchiveProvider.Builder();
for (String attr : semantics.getAttributesWithJavaRuntimeDeps(ruleContext)) {
// Use all available DexArchiveProviders from attributes that carry runtime dependencies
Expand Down Expand Up @@ -1621,7 +1621,7 @@ private static Artifact createShuffleJarActions(
}

SpawnAction.Builder shardAction =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.useDefaultShellEnvironment()
.setMnemonic("ShardClassesToDex")
.setProgressMessage("Sharding classes for dexing for %s", ruleContext.getLabel())
Expand Down Expand Up @@ -1724,10 +1724,17 @@ private static ImmutableList<Artifact> toDexedClasspath(
return dexedClasspath.build();
}

// Adds execution info by propagating tags from the target
private static SpawnAction.Builder createSpawnActionBuilder(RuleContext ruleContext) throws InterruptedException {
return new SpawnAction.Builder()
.setExecutionInfo(TargetUtils.getExecutionInfo(
ruleContext.getRule(), ruleContext.isAllowTagsPropagation()));
}

// Adds the appropriate SpawnAction options depending on if SingleJar is a jar or not.
private static SpawnAction.Builder singleJarSpawnActionBuilder(RuleContext ruleContext) {
private static SpawnAction.Builder singleJarSpawnActionBuilder(RuleContext ruleContext) throws InterruptedException {
Artifact singleJar = JavaToolchainProvider.from(ruleContext).getSingleJar();
SpawnAction.Builder builder = new SpawnAction.Builder().useDefaultShellEnvironment();
SpawnAction.Builder builder = createSpawnActionBuilder(ruleContext).useDefaultShellEnvironment();
if (singleJar.getFilename().endsWith(".jar")) {
builder
.setJarExecutable(
Expand All @@ -1746,7 +1753,7 @@ private static SpawnAction.Builder singleJarSpawnActionBuilder(RuleContext ruleC
* of the output.
*/
static void createCleanDexZipAction(
RuleContext ruleContext, Artifact inputZip, Artifact outputZip) {
RuleContext ruleContext, Artifact inputZip, Artifact outputZip) throws InterruptedException {
ruleContext.registerAction(
singleJarSpawnActionBuilder(ruleContext)
.setProgressMessage("Trimming %s", inputZip.getExecPath().getBaseName())
Expand Down Expand Up @@ -1802,7 +1809,7 @@ static Artifact createMainDexListAction(
// Process the input jar through Proguard into an intermediate, streamlined jar.
Artifact strippedJar = AndroidBinary.getDxArtifact(ruleContext, "main_dex_intermediate.jar");
SpawnAction.Builder streamlinedBuilder =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.useDefaultShellEnvironment()
.addOutput(strippedJar)
.setExecutable(sdk.getProguard())
Expand Down Expand Up @@ -1833,7 +1840,7 @@ static Artifact createMainDexListAction(
ruleContext.registerAction(streamlinedBuilder.build(ruleContext));

SpawnAction.Builder builder =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setMnemonic("MainDexClasses")
.setProgressMessage("Generating main dex classes list");

Expand Down Expand Up @@ -1861,7 +1868,7 @@ static Artifact createMainDexListAction(
":legacy_main_dex_list_generator", TransitionMode.HOST);
// Use the newer legacy multidex main-dex list generation.
SpawnAction.Builder actionBuilder =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setMnemonic("MainDexClasses")
.setProgressMessage("Generating main dex classes list");

Expand Down Expand Up @@ -1910,7 +1917,7 @@ static Artifact transformDexListThroughProguardMapAction(
Artifact obfuscatedMainDexList =
AndroidBinary.getDxArtifact(ruleContext, "main_dex_list_obfuscated.txt");
SpawnAction.Builder actionBuilder =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setMnemonic("MainDexProguardClasses")
.setProgressMessage("Obfuscating main dex classes list")
.setExecutable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.devtools.build.lib.rules.android;

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;
import com.google.devtools.build.lib.rules.android.AndroidDataConverter.JoinerType;
Expand Down Expand Up @@ -63,6 +64,8 @@ public class AndroidResourcesProcessorBuilder {
private String packageUnderTest;
private boolean isTestWithResources = false;

private ImmutableMap<String, String> executionInfo;

/**
* The output zip for resource-processed data binding expressions (i.e. a zip of .xml files).
*
Expand Down Expand Up @@ -163,6 +166,14 @@ public AndroidResourcesProcessorBuilder setThrowOnResourceConflict(
return this;
}

/**
* Sets the map of execution info.
*/
public AndroidResourcesProcessorBuilder setExecutionInfo(ImmutableMap<String, String> info) {
this.executionInfo = info;
return this;
}

/**
* Creates and registers an action that processes only transitive data.
*
Expand Down Expand Up @@ -278,6 +289,10 @@ private void createAapt2ApkAction(
BusyBoxActionBuilder builder =
BusyBoxActionBuilder.create(dataContext, "AAPT2_PACKAGE").addAapt();

if (executionInfo != null && !executionInfo.isEmpty()) {
builder.addExecutionInfo(executionInfo);
}

if (resourceDependencies != null) {
builder
.addTransitiveFlag(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ public AndroidBinaryDataInfo processBinaryData(
DataBinding.contextFrom(
dataBindingEnabled,
ctx.getActionConstructionContext(),
ctx.getAndroidConfig()))
ctx.getAndroidConfig()),
ImmutableMap.of())
.generateRClass(ctx);

return AndroidBinaryDataInfo.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import com.google.devtools.build.lib.analysis.RunfilesSupplierImpl;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.rules.android.AndroidConfiguration.ApkSigningMethod;
import com.google.devtools.build.lib.rules.java.JavaCommon;
import com.google.devtools.build.lib.rules.java.JavaRuntimeInfo;
import com.google.devtools.build.lib.rules.java.JavaToolchainProvider;
import com.google.devtools.build.lib.vfs.PathFragment;

import java.beans.IntrospectionException;
import java.util.List;

/**
Expand Down Expand Up @@ -142,7 +145,7 @@ public ApkActionsBuilder setArtifactLocationDirectory(String artifactLocation) {
}

/** Registers the actions needed to build the requested APKs in the rule context. */
public void registerActions(RuleContext ruleContext) {
public void registerActions(RuleContext ruleContext) throws InterruptedException {
boolean useSingleJarApkBuilder =
ruleContext.getFragment(AndroidConfiguration.class).useSingleJarApkBuilder();

Expand Down Expand Up @@ -176,9 +179,9 @@ public void registerActions(RuleContext ruleContext) {
* <p>If {@code signingKey} is not null, the apk will be signed with it using the V1 signature
* scheme.
*/
private void legacyBuildApk(RuleContext ruleContext, Artifact outApk) {
private void legacyBuildApk(RuleContext ruleContext, Artifact outApk) throws InterruptedException {
SpawnAction.Builder actionBuilder =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setExecutable(AndroidSdkProvider.fromRuleContext(ruleContext).getApkBuilder())
.setProgressMessage("Generating unsigned %s", apkName)
.setMnemonic("AndroidApkBuilder")
Expand Down Expand Up @@ -253,11 +256,11 @@ private void legacyBuildApk(RuleContext ruleContext, Artifact outApk) {
}

/** Registers generating actions for {@code outApk} that build an unsigned APK using SingleJar. */
private void buildApk(RuleContext ruleContext, Artifact outApk) {
private void buildApk(RuleContext ruleContext, Artifact outApk) throws InterruptedException {
Artifact compressedApk = getApkArtifact(ruleContext, "compressed_" + outApk.getFilename());

SpawnAction.Builder compressedApkActionBuilder =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setMnemonic("ApkBuilder")
.setProgressMessage("Generating unsigned %s", apkName)
.addOutput(compressedApk);
Expand Down Expand Up @@ -297,7 +300,7 @@ private void buildApk(RuleContext ruleContext, Artifact outApk) {
}

SpawnAction.Builder singleJarActionBuilder =
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setMnemonic("ApkBuilder")
.setProgressMessage("Generating unsigned %s", apkName)
.addInput(compressedApk)
Expand All @@ -316,7 +319,7 @@ private void buildApk(RuleContext ruleContext, Artifact outApk) {
Artifact extractedJavaResourceZip =
getApkArtifact(ruleContext, "extracted_" + javaResourceZip.getFilename());
ruleContext.registerAction(
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setExecutable(resourceExtractor)
.setMnemonic("ResourceExtractor")
.setProgressMessage("Extracting Java resources from deploy jar for %s", apkName)
Expand Down Expand Up @@ -376,9 +379,10 @@ private void buildApk(RuleContext ruleContext, Artifact outApk) {
}

/** Uses the zipalign tool to align the zip boundaries for uncompressed resources by 4 bytes. */
private void zipalignApk(RuleContext ruleContext, Artifact inputApk, Artifact zipAlignedApk) {
private void zipalignApk(RuleContext ruleContext, Artifact inputApk, Artifact zipAlignedApk)
throws InterruptedException {
ruleContext.registerAction(
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.addInput(inputApk)
.addOutput(zipAlignedApk)
.setExecutable(AndroidSdkProvider.fromRuleContext(ruleContext).getZipalign())
Expand All @@ -402,11 +406,11 @@ private void zipalignApk(RuleContext ruleContext, Artifact inputApk, Artifact zi
* alignment cannot be performed after v2 signing without invalidating the signature.
*/
private void signApk(
RuleContext ruleContext, Artifact unsignedApk, Artifact signedAndZipalignedApk) {
RuleContext ruleContext, Artifact unsignedApk, Artifact signedAndZipalignedApk) throws InterruptedException {
ApkSigningMethod signingMethod =
ruleContext.getFragment(AndroidConfiguration.class).getApkSigningMethod();
ruleContext.registerAction(
new SpawnAction.Builder()
createSpawnActionBuilder(ruleContext)
.setExecutable(AndroidSdkProvider.fromRuleContext(ruleContext).getApkSigner())
.setProgressMessage("Signing %s", apkName)
.setMnemonic("ApkSignerTool")
Expand Down Expand Up @@ -453,4 +457,12 @@ private Artifact getApkArtifact(RuleContext ruleContext, String baseName) {
return AndroidBinary.getDxArtifact(ruleContext, baseName);
}
}

// Adds execution info by propagating tags from the target
private static SpawnAction.Builder createSpawnActionBuilder(RuleContext ruleContext) throws InterruptedException {
return new SpawnAction.Builder()
.setExecutionInfo(TargetUtils.getExecutionInfo(
ruleContext.getRule(), ruleContext.isAllowTagsPropagation()));
}

}
Loading

0 comments on commit 8e2e050

Please sign in to comment.