Add test_mapping rule.

The test_mapping target defines a target that,
when `bazel run <target>`, generates a
under {out_dir}/dist/test_mapping.zip, where
{out_dir} is an argument given to the rule.

This is used as a workaround for Bug 201687176
until test_mapping.zip is generated properly.

Bug: 201687176
Bug: 228560650
Test: build for CF

Change-Id: If225dbb320974657713b680dcf8ceac0f85a6830
diff --git a/test_mapping/BUILD b/test_mapping/BUILD
new file mode 100644
index 0000000..1f48815
--- /dev/null
+++ b/test_mapping/BUILD
@@ -0,0 +1,17 @@
+# Copyright (C) 2022 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+exports_files([
+    "test_mapping.sh",
+])
diff --git a/test_mapping/test_mapping.bzl b/test_mapping/test_mapping.bzl
new file mode 100644
index 0000000..e523fc3
--- /dev/null
+++ b/test_mapping/test_mapping.bzl
@@ -0,0 +1,61 @@
+# Copyright (C) 2022 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+load("//build/bazel_common_rules/exec:embedded_exec.bzl", "embedded_exec")
+
+def test_mapping_dist(
+        name,
+        dist_dir = None,
+        **kwargs):
+    """Run this target to generate test mapping archive to the location given
+    by `--dist_dir` command-line argument. If `--dist_dir` command-line argument
+    is not specified, default to the `dist_dir` argument of this rule.
+
+    For example:
+
+    ```
+    test_mapping(
+        name = "my_test_mapping",
+        args = ["--dist_dir", "out/dist"],
+    )
+    ```
+
+    ```
+    # generate to <workspace_root>/out/dist
+    $ bazel run my_test_mapping
+
+    # generate to <workspace_root>/path
+    $ bazel run my_test_mapping -- --dist_dir=path
+
+    # generate to /tmp/path
+    $ bazel run my_test_mapping -- --dist_dir=/tmp/path
+    ```
+
+    Args:
+        name: name of this target.
+        kwargs: Additional arguments to the internal rule, e.g. `visibility`.
+    """
+
+    native.sh_binary(
+        name = name + "_internal",
+        srcs = ["//build/bazel_common_rules/test_mapping:test_mapping.sh"],
+        data = ["//prebuilts/build-tools:linux-x86"],
+        args = ["--dist_dir", dist_dir] if dist_dir else None,
+        **kwargs
+    )
+
+    embedded_exec(
+        name = name,
+        actual = name + "_internal",
+    )
diff --git a/test_mapping/test_mapping.sh b/test_mapping/test_mapping.sh
new file mode 100755
index 0000000..672ac83
--- /dev/null
+++ b/test_mapping/test_mapping.sh
@@ -0,0 +1,73 @@
+#!/bin/bash -e
+
+# Copyright (C) 2022 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Ensure hermeticity.
+PATH="$PWD/prebuilts/build-tools/path/linux-x86/:$PWD/prebuilts/build-tools/linux-x86/bin/"
+
+while [[ $# -gt 0 ]]; do
+  case $1 in
+    --dist_dir)
+      DIST_DIR="$2"
+      shift # past argument
+      shift # past value
+      ;;
+    --dist_dir=*)
+      DIST_DIR=$1
+      DIST_DIR="${DIST_DIR#*=}"
+      shift # past argument=value
+      ;;
+    -*|--*)
+      # There may be additional arguments passed to copy_to_dist_dir. Ignore them.
+      shift
+      ;;
+    *)
+      # There may be additional arguments passed to copy_to_dist_dir. Ignore them.
+      shift
+      ;;
+  esac
+done
+
+# BUILD_WORKSPACE_DIRECTORY is the root of the Bazel workspace containing
+# this binary target.
+# https://docs.bazel.build/versions/main/user-manual.html#run
+ROOT_DIR=$BUILD_WORKSPACE_DIRECTORY
+if [[ -z "$ROOT_DIR" ]]; then
+  echo "ERROR: Only execute this script with bazel run." >&2
+  exit 1
+fi
+
+if [[ -z "$DIST_DIR" ]]; then
+  echo "ERROR: --dist_dir is not specified." >&2
+  exit 1
+fi
+
+if [[ ! "$DIST_DIR" == /* ]]; then
+  DIST_DIR=${ROOT_DIR}/${DIST_DIR}
+fi
+mkdir -p ${DIST_DIR}
+
+OUTPUT_FILE=${DIST_DIR}/test_mapping.zip
+echo "Generating ${OUTPUT_FILE}"
+
+trap 'rm -f "$TMPFILE"' EXIT
+TEST_MAPPING_FILES=$(mktemp)
+find ${ROOT_DIR} -name TEST_MAPPING \
+  -not -path "${ROOT_DIR}/\.git*" \
+  -not -path "${ROOT_DIR}/\.repo*" \
+  -not -path "${ROOT_DIR}/out*" \
+  > ${TEST_MAPPING_FILES}
+soong_zip -o ${OUTPUT_FILE} -C ${ROOT_DIR} -l ${TEST_MAPPING_FILES}
+rm -f ${TEST_MAPPING_FILES}