diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AarImport.java b/src/main/java/com/google/devtools/build/lib/rules/android/AarImport.java index d70439041c161e..789090b2157d75 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AarImport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AarImport.java @@ -13,6 +13,7 @@ // limitations under the License. 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.ImmutableSet; @@ -33,8 +34,11 @@ import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; import com.google.devtools.build.lib.analysis.actions.CustomCommandLine; import com.google.devtools.build.lib.analysis.actions.SpawnAction; +import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; +import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; +import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; import com.google.devtools.build.lib.rules.android.databinding.DataBinding; import com.google.devtools.build.lib.rules.android.databinding.DataBindingV2Provider; import com.google.devtools.build.lib.rules.java.ImportDepsCheckActionBuilder; @@ -54,6 +58,7 @@ import com.google.devtools.build.lib.starlarkbuildapi.android.DataBindingV2ProviderApi; import com.google.devtools.build.lib.vfs.PathFragment; import java.util.List; +import java.util.Map; import javax.annotation.Nullable; /** @@ -466,7 +471,48 @@ private static SpawnAction createAarJarsMergingActions( } private static SpawnAction createAarNativeLibsFilterActions( - RuleContext ruleContext, Artifact aar, Artifact outputZip) { + RuleContext ruleContext, Artifact aar, Artifact outputZip) throws RuleErrorException { + + BuildConfigurationValue configuration = ruleContext.getConfiguration(); + + String cpu = null; + AndroidConfiguration androidConfiguration = + configuration.getFragment(AndroidConfiguration.class); + + if (androidConfiguration.incompatibleUseToolchainResolution()) { + + // Maps a CPU name as used in an AAR to the corresponding CPU constraint. + ImmutableMap aarCpuToConstraint = + ImmutableMap.of( + "arm64-v8a", + ruleContext.getPrerequisite("$constraint_arm64", ConstraintValueInfo.PROVIDER), + "armeabi-v7a", + ruleContext.getPrerequisite("$constraint_armv7", ConstraintValueInfo.PROVIDER), + "x86", ruleContext.getPrerequisite("$constraint_x86", ConstraintValueInfo.PROVIDER), + "x86_64", + ruleContext.getPrerequisite("$constraint_x86_64", ConstraintValueInfo.PROVIDER)); + + for (Map.Entry e : aarCpuToConstraint.entrySet()) { + if (ruleContext.targetPlatformHasConstraint(e.getValue())) { + cpu = e.getKey(); + break; + } + } + + if (cpu == null) { + throw ruleContext.throwWithRuleError( + String.format( + "Target platform %s does not match one of the applicable CPU constraints for" + + " aar_import %s. Applicable CPU constraints are listed in" + + " https://blog.bazel.build/2023/11/15/android-platforms.html", + ruleContext.getToolchainContexts().getTargetPlatform().label(), + ruleContext.getLabel())); + } + + } else { + cpu = configuration.getCpu(); + } + SpawnAction.Builder actionBuilder = new SpawnAction.Builder(); ParamFileInfo paramFileInfo = getParamFileInfo(ruleContext); modifyExecutionInfo(ruleContext, actionBuilder); @@ -481,7 +527,7 @@ private static SpawnAction createAarNativeLibsFilterActions( .addCommandLine( CustomCommandLine.builder() .addExecPath("--input_aar", aar) - .add("--cpu", ruleContext.getConfiguration().getCpu()) + .add("--cpu", cpu) .addExecPath("--output_zip", outputZip) .build(), paramFileInfo) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AarImportBaseRule.java b/src/main/java/com/google/devtools/build/lib/rules/android/AarImportBaseRule.java index fa7901137db777..9ce13e5578f951 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AarImportBaseRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AarImportBaseRule.java @@ -21,6 +21,7 @@ import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; import com.google.devtools.build.lib.analysis.config.ExecutionTransitionFactory; +import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType; import com.google.devtools.build.lib.packages.StarlarkProviderIdentifier; @@ -92,6 +93,18 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) .cfg(ExecutionTransitionFactory.createFactory()) .exec() .value(env.getToolsLabel("//tools/zip:zipper"))) + .add( + attr("$constraint_arm64", LABEL) + .value(Label.parseCanonicalUnchecked("@platforms//cpu:arm64"))) + .add( + attr("$constraint_armv7", LABEL) + .value(Label.parseCanonicalUnchecked("@platforms//cpu:armv7"))) + .add( + attr("$constraint_x86", LABEL) + .value(Label.parseCanonicalUnchecked("@platforms//cpu:x86_32"))) + .add( + attr("$constraint_x86_64", LABEL) + .value(Label.parseCanonicalUnchecked("@platforms//cpu:x86_64"))) .advertiseStarlarkProvider(StarlarkProviderIdentifier.forKey(JavaInfo.PROVIDER.getKey())) .requiresConfigurationFragments(AndroidConfiguration.class, JavaConfiguration.class) .build(); diff --git a/src/test/shell/bazel/android/aar_integration_test.sh b/src/test/shell/bazel/android/aar_integration_test.sh index e52bd77a53a83b..eb72ad12b8cc9b 100755 --- a/src/test/shell/bazel/android/aar_integration_test.sh +++ b/src/test/shell/bazel/android/aar_integration_test.sh @@ -83,6 +83,8 @@ EOF function test_android_binary_depends_on_aar() { create_new_workspace setup_android_sdk_support + setup_android_platforms + cat > AndroidManifest.xml < EOF @@ -91,9 +93,15 @@ EOF EOF + mkdir assets echo "some asset" > assets/a - zip example.aar AndroidManifest.xml res/layout/mylayout.xml assets/a + + mkdir -p jni/armeabi-v7a + echo "an armeabi-v7a so" > jni/armeabi-v7a/libjni.so + + zip example.aar AndroidManifest.xml res/layout/mylayout.xml assets/a jni/armeabi-v7a/libjni.so + cat > BUILD <> $(create_workspace_with_default_repos WORKSPACE) <