Skip to content

Commit

Permalink
[7.1.0] Enable aar_import JNI libs to work with --android_platforms. (#…
Browse files Browse the repository at this point in the history
…21502)

Fixes #21225

Commit
1e77944

PiperOrigin-RevId: 610551472
Change-Id: I7e62919dd41d0d4e2b923df1942920162f554809

Co-authored-by: Googler <ahumesky@google.com>
  • Loading branch information
bazel-io and ahumesky authored Feb 27, 2024
1 parent e57b6cc commit 4995a20
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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<String, ConstraintValueInfo> 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<String, ConstraintValueInfo> 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);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions src/test/shell/bazel/android/aar_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
<manifest package="com.example"/>
EOF
Expand All @@ -91,9 +93,15 @@ EOF
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" />
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 <<EOF
aar_import(
name = "example",
Expand All @@ -106,10 +114,12 @@ android_binary(
deps = [":example"],
)
EOF
assert_build :app

assert_build :app --android_platforms=//test_android_platforms:simple
apk_contents="$(zipinfo -1 bazel-bin/app.apk)"
assert_one_of $apk_contents "assets/a"
assert_one_of $apk_contents "res/layout/mylayout.xml"
assert_one_of $apk_contents "lib/armeabi-v7a/libjni.so"
}

function test_android_binary_fat_apk_contains_all_shared_libraries() {
Expand Down
3 changes: 3 additions & 0 deletions src/test/shell/bazel/android/android_sdk_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ function test_specifying_android_sdk_flag() {
create_new_workspace
setup_android_sdk_support
create_android_binary
setup_android_platforms

cat >> $(create_workspace_with_default_repos WORKSPACE) <<EOF
android_sdk_repository(
name = "a",
)
EOF
ANDROID_HOME=$ANDROID_SDK bazel build --android_sdk=@a//:sdk-24 \
--android_platforms=//test_android_platforms:simple \
//java/bazel:bin || fail "build with --android_sdk failed"
}

Expand Down

0 comments on commit 4995a20

Please sign in to comment.