Re-enable and fix bazel_apexer_wrapper_test.

This test was accidentally skipped by bp2build-incremental because its
dependency, //external/avb:avbtool, is a python binary that is target
incompatible with Android targets. Bazel automatically skips tests in a
target pattern like `...` if the test or any of its dependencies are not
compatible.

This CL makes it such that this test only runs when --platforms is the
linux host, and also add it to bp2build.sh for CI coverage.

Test: CI/bp2build-incremental
Test: b test //build/bazel/rules/apex:bazel_apexer_wrapper_test --platforms=//build/bazel/platforms:linux_x86_64
Change-Id: I7f0bec8b10c114fc4fb8b609921eaa636ca74763
diff --git a/ci/bp2build.sh b/ci/bp2build.sh
index 8f9e7ac..cc2c251 100755
--- a/ci/bp2build.sh
+++ b/ci/bp2build.sh
@@ -33,11 +33,14 @@
 FLAGS_LIST=(
   --config=bp2build
   --config=ci
+
+  # Ensure that a test command will also build non-test targets.
+  --build_tests_only=false
 )
 FLAGS="${FLAGS_LIST[@]}"
 
 ###############
-# Build targets
+# Build and test targets for device target platform.
 ###############
 BUILD_TARGETS_LIST=(
   //art/...
@@ -62,11 +65,31 @@
   -//external/e2fsprogs/e2fsck:all
 )
 BUILD_TARGETS="${BUILD_TARGETS_LIST[@]}"
-# Iterate over various architectures supported in the platform build.
-tools/bazel --max_idle_secs=5 build ${FLAGS} --platforms //build/bazel/platforms:android_x86 -k -- ${BUILD_TARGETS}
-tools/bazel --max_idle_secs=5 build ${FLAGS} --platforms //build/bazel/platforms:android_x86_64 -k -- ${BUILD_TARGETS}
-tools/bazel --max_idle_secs=5 build ${FLAGS} --platforms //build/bazel/platforms:android_arm -k -- ${BUILD_TARGETS}
-tools/bazel --max_idle_secs=5 build ${FLAGS} --platforms //build/bazel/platforms:android_arm64 -k -- ${BUILD_TARGETS}
+
+TEST_TARGETS_LIST=(
+  //build/bazel/tests/...
+  //build/bazel/rules/apex/...
+  //build/bazel/scripts/...
+)
+TEST_TARGETS="${TEST_TARGETS_LIST[@]}"
+
+###########
+# Iterate over various architectures supported in the platform build and perform:
+# 1) builds for all build and test targets
+# 2) tests
+# 3) dist for mainline modules
+###########
+
+for platform in android_x86 android_x86_64 android_arm android_arm64; do
+  # Use a loop to prevent unnecessarily switching --platforms because that drops the Bazel analysis cache.
+  tools/bazel --max_idle_secs=5 test ${FLAGS} --platforms //build/bazel/platforms:${platform} -k -- ${BUILD_TARGETS} ${TEST_TARGETS}
+  tools/bazel --max_idle_secs=5 run //build/bazel/ci/dist:mainline_modules ${FLAGS} --platforms=//build/bazel/platforms:${platform} -- --dist_dir="${DIST_DIR}/mainline_modules_${platform}"
+done
+
+
+#########
+# Host-only builds and tests
+#########
 
 HOST_INCOMPATIBLE_TARGETS=(
   # TODO(b/217756861): Apex toolchain is incompatible with host arches but apex modules do
@@ -88,23 +111,10 @@
   -//packages/modules/adb/pairing_connection:all
 )
 
-# build for host
-tools/bazel --max_idle_secs=5 build ${FLAGS} \
+# Host-only builds and tests, relying on incompatible target skipping.
+tools/bazel --max_idle_secs=5 test ${FLAGS} \
   --platforms //build/bazel/platforms:linux_x86_64 \
-  -- ${BUILD_TARGETS} "${HOST_INCOMPATIBLE_TARGETS[@]}"
-
-###########
-# Run tests
-###########
-tools/bazel --max_idle_secs=5 test ${FLAGS} //build/bazel/tests/... //build/bazel/rules/apex/... //build/bazel/scripts/...
-
-###########
-# Dist mainline modules
-###########
-tools/bazel --max_idle_secs=5 run //build/bazel/ci/dist:mainline_modules ${FLAGS} --platforms=//build/bazel/platforms:android_x86 -- --dist_dir="${DIST_DIR}/mainline_modules_x86"
-tools/bazel --max_idle_secs=5 run //build/bazel/ci/dist:mainline_modules ${FLAGS} --platforms=//build/bazel/platforms:android_x86_64 -- --dist_dir="${DIST_DIR}/mainline_modules_x86_64"
-tools/bazel --max_idle_secs=5 run //build/bazel/ci/dist:mainline_modules ${FLAGS} --platforms=//build/bazel/platforms:android_arm -- --dist_dir="${DIST_DIR}/mainline_modules_arm"
-tools/bazel --max_idle_secs=5 run //build/bazel/ci/dist:mainline_modules ${FLAGS} --platforms=//build/bazel/platforms:android_arm64 -- --dist_dir="${DIST_DIR}/mainline_modules_arm64"
+  -- ${BUILD_TARGETS} ${TEST_TARGETS} "${HOST_INCOMPATIBLE_TARGETS[@]}"
 
 ###################
 # bp2build-progress
diff --git a/rules/apex/BUILD b/rules/apex/BUILD
index ef3998c..c724d4a 100644
--- a/rules/apex/BUILD
+++ b/rules/apex/BUILD
@@ -75,5 +75,10 @@
         "@make_injection//:host/linux-x86/bin/deapexer",
         "@make_injection//:host/linux-x86/bin/sefcontext_compile",
         "//prebuilts/sdk/current:public/android.jar",
-    ]
+    ],
+    # This is a host test.
+    target_compatible_with = select({
+        "//build/bazel/platforms/os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
 )
diff --git a/rules/apex/bazel_apexer_wrapper_test.sh b/rules/apex/bazel_apexer_wrapper_test.sh
index 1cf03fa..24e26d0 100755
--- a/rules/apex/bazel_apexer_wrapper_test.sh
+++ b/rules/apex/bazel_apexer_wrapper_test.sh
@@ -17,8 +17,15 @@
 
 set -xeuo pipefail
 
-apexer_tool_path="${RUNFILES_DIR}/__main__/external/make_injection/host/linux-x86/bin"
+prebuilt_tool_path="${RUNFILES_DIR}/__main__/external/make_injection/host/linux-x86/bin"
+apexer_tool_path="${RUNFILES_DIR}/__main__/external/make_injection/host/linux-x86/bin/apexer"
 avb_tool_path="${RUNFILES_DIR}/__main__/external/avb"
+e2fsdroid_path="${RUNFILES_DIR}/__main__/external/e2fsprogs/contrib/android"
+mke2fs_path="${RUNFILES_DIR}/__main__/external/e2fsprogs/misc"
+resize2fs_path="${RUNFILES_DIR}/__main__/external/e2fsprogs/resize"
+debugfs_path="${RUNFILES_DIR}/__main__/external/e2fsprogs/debugfs"
+soong_zip_path="${RUNFILES_DIR}/__main__/prebuilts/build-tools/linux-x86/bin"
+aapt2_path="${RUNFILES_DIR}/__main__/prebuilts/sdk/tools/linux/bin"
 android_jar="${RUNFILES_DIR}/__main__/prebuilts/sdk/current/public/android.jar"
 
 input_dir=$(mktemp -d)
@@ -53,7 +60,7 @@
 manifest_dir=$(mktemp -d)
 manifest_file="${manifest_dir}/apex_manifest.pb"
 echo '{"name": "com.android.example.apex", "version": 1}' > "${manifest_dir}/apex_manifest.json"
-"${apexer_tool_path}/conv_apex_manifest" proto "${manifest_dir}/apex_manifest.json" -o ${manifest_file}
+"${prebuilt_tool_path}/conv_apex_manifest" proto "${manifest_dir}/apex_manifest.json" -o ${manifest_file}
 
 # Create the file_contexts file
 file_contexts_file=$(mktemp)
@@ -75,6 +82,8 @@
 dir7,three_level_sym_in_execroot,"${input_dir}/three_level_sym_in_execroot"
 " > ${bazel_apexer_wrapper_manifest_file}
 
+apexer_tool_paths=${prebuilt_tool_path}:${avb_tool_path}:${avb_tool_path}:${e2fsdroid_path}:${mke2fs_path}:${resize2fs_path}:${debugfs_path}:${soong_zip_path}:${aapt2_path}
+
 #############################################
 # run bazel_apexer_wrapper
 #############################################
@@ -83,7 +92,7 @@
   --file_contexts ${file_contexts_file} \
   --key "${RUNFILES_DIR}/__main__/build/bazel/rules/apex/test.pem" \
   --apexer_path ${apexer_tool_path} \
-  --apexer_tool_paths ${apexer_tool_path}:${avb_tool_path} \
+  --apexer_tool_paths "${apexer_tool_paths}" \
   --apex_output_file ${output_file} \
   --bazel_apexer_wrapper_manifest ${bazel_apexer_wrapper_manifest_file} \
   --android_jar_path ${android_jar}
@@ -91,7 +100,7 @@
 #############################################
 # check the result
 #############################################
-"${apexer_tool_path}/deapexer" --debugfs_path="${apexer_tool_path}/debugfs" extract ${output_file} ${output_dir}
+"${prebuilt_tool_path}/deapexer" --debugfs_path="${debugfs_path}/debugfs" extract ${output_file} ${output_dir}
 
 # The expected mounted tree should be something like this:
 # /tmp/tmp.9u7ViPlMr7
diff --git a/tests/apex/BUILD b/tests/apex/BUILD
index 560d4b8..815bcd0 100644
--- a/tests/apex/BUILD
+++ b/tests/apex/BUILD
@@ -12,18 +12,33 @@
     name = "build.bazel.examples.apex.minimal",
     apex1 = "//build/bazel/examples/apex/minimal:build.bazel.examples.apex.minimal.apex",
     apex2 = "@make_injection//:target/product/generic/system/product/apex/build.bazel.examples.apex.minimal.apex",
+    # arch dependent test
+    target_compatible_with = [
+        "//build/bazel/platforms/os:android",
+        "//build/bazel/platforms/arch:arm",
+    ],
 )
 
 apex_diff_test(
     name = "com.android.adbd_uncompressed",
     apex1 = "//packages/modules/adb/apex:com.android.adbd.apex",
     apex2 = "@make_injection//:target/product/generic/system/apex/com.android.adbd.capex",
+    # arch dependent test
+    target_compatible_with = [
+        "//build/bazel/platforms/os:android",
+        "//build/bazel/platforms/arch:arm",
+    ],
 )
 
 apex_diff_test(
     name = "com.android.adbd_compressed",
     apex1 = "//packages/modules/adb/apex:com.android.adbd.capex",
     apex2 = "@make_injection//:target/product/generic/system/apex/com.android.adbd.capex",
+    # arch dependent test
+    target_compatible_with = [
+        "//build/bazel/platforms/os:android",
+        "//build/bazel/platforms/arch:arm",
+    ],
 )
 
 apex_compression_test(
@@ -41,4 +56,4 @@
 apex_aab_test(
     name = "build.bazel.examples.apex.minimal_mainline-module",
     apex = "//build/bazel/examples/apex/minimal:build.bazel.examples.apex.minimal",
-)
\ No newline at end of file
+)
diff --git a/tests/apex/apex_diff_test.bzl b/tests/apex/apex_diff_test.bzl
index e809f94..5c0d548 100644
--- a/tests/apex/apex_diff_test.bzl
+++ b/tests/apex/apex_diff_test.bzl
@@ -1,6 +1,11 @@
 load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
 
-def apex_diff_test(name, apex1, apex2, expected_diff=None, **kwargs):
+def apex_diff_test(
+    name,
+    apex1,
+    apex2,
+    target_compatible_with = None,
+    expected_diff = None):
     """A test that compares the content list of two APEXes, determined by `deapexer`."""
 
     native.genrule(
@@ -30,6 +35,7 @@
             name = name + "_content_diff_test",
             file1 = name + ".apex1.txt",
             file2 = name + ".apex2.txt",
+            target_compatible_with = target_compatible_with,
         )
     else:
         # Make our own diff to compare against the expected one
@@ -47,4 +53,5 @@
             name = name + "_content_diff_test",
             file1 = name + ".apex1.apex2.diff.txt",
             file2 = expected_diff,
+            target_compatible_with = target_compatible_with,
         )