diff --git a/BUILD.bazel b/BUILD.bazel
index be0b1a5332ff..9bad6838d784 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -1,11 +1,14 @@
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2021 The Android Open Source Project
+load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//build/bazel_common_rules/dist:dist.bzl", "copy_to_dist_dir")
+load("//build/bazel_common_rules/zip:zip.bzl", "zip_archive")
load("//build/kernel/kleaf:common_kernels.bzl", "define_common_kernels")
load(
"//build/kernel/kleaf:kernel.bzl",
+ "android_filegroup",
"checkpatch",
"ddk_headers",
"kernel_build",
@@ -13,6 +16,7 @@ load(
"kernel_modules_install",
"merged_kernel_uapi_headers",
)
+load(":abi.bzl", "cc_binary_with_abi")
load(":modules.bzl", "get_gki_modules_list")
package(
@@ -827,3 +831,99 @@ ddk_headers(
includes = [],
visibility = ["//visibility:private"],
)
+
+_KSELFTEST_DIR = "testcases/selftests"
+
+config_setting(
+ name = "x86_64",
+ values = {"platforms": "//build/kernel/kleaf/impl:android_x86_64"},
+ visibility = ["//visibility:private"],
+)
+
+config_setting(
+ name = "arm64",
+ values = {"platforms": "//build/kernel/kleaf/impl:android_arm64"},
+ visibility = ["//visibility:private"],
+)
+
+cc_library(
+ name = "kselftest_headers_lib",
+ hdrs = glob(["tools/testing/selftests/*.h"]),
+ visibility = ["//visibility:private"],
+)
+
+cc_binary_with_abi(
+ name = "kselftest_binderfs_test",
+ srcs = ["tools/testing/selftests/filesystems/binderfs/binderfs_test.c"],
+ path_prefix = _KSELFTEST_DIR,
+ target_compatible_with = ["@platforms//os:android"],
+ visibility = ["//visibility:private"],
+ deps = [":kselftest_headers_lib"],
+)
+
+cc_binary_with_abi(
+ name = "kselftest_breakpoints_tests_step_after_suspend_test",
+ srcs = ["tools/testing/selftests/breakpoints/step_after_suspend_test.c"],
+ path_prefix = _KSELFTEST_DIR,
+ target_compatible_with = ["@platforms//os:android"],
+ visibility = ["//visibility:private"],
+ deps = [":kselftest_headers_lib"],
+)
+
+cc_binary_with_abi(
+ name = "kselftest_breakpoints_tests_breakpoint_test",
+ srcs = select({
+ ":x86_64": ["tools/testing/selftests/breakpoints/breakpoint_test.c"],
+ ":arm64": ["tools/testing/selftests/breakpoints/breakpoint_test_arm64.c"],
+ }),
+ path_prefix = _KSELFTEST_DIR,
+ target_compatible_with = ["@platforms//os:android"],
+ visibility = ["//visibility:private"],
+ deps = [":kselftest_headers_lib"],
+)
+
+copy_file(
+ name = "kselftest_gen_config",
+ src = select({
+ ":x86_64": "tools/testing/selftests/android/config_x86_64.xml",
+ ":arm64": "tools/testing/selftests/android/config_arm64.xml",
+ }),
+ out = _KSELFTEST_DIR + "/selftests.config",
+ visibility = ["//visibility:private"],
+)
+
+android_filegroup(
+ name = "kselftest_tests_x86_64",
+ srcs = [
+ ":kselftest_binderfs_test_x86_64",
+ ":kselftest_breakpoints_tests_breakpoint_test_x86_64",
+ ":kselftest_breakpoints_tests_step_after_suspend_test_x86_64",
+ ":kselftest_gen_config",
+ ],
+ cpu = "x86_64",
+ visibility = ["//visibility:private"],
+)
+
+android_filegroup(
+ name = "kselftest_tests_arm64",
+ srcs = [
+ ":kselftest_binderfs_test_arm64",
+ ":kselftest_breakpoints_tests_breakpoint_test_arm64",
+ ":kselftest_breakpoints_tests_step_after_suspend_test_arm64",
+ ":kselftest_gen_config",
+ ],
+ cpu = "arm64",
+ visibility = ["//visibility:private"],
+)
+
+zip_archive(
+ name = "tests_zip_x86_64",
+ srcs = [":kselftest_tests_x86_64"],
+ out = "tests.zip",
+)
+
+zip_archive(
+ name = "tests_zip_arm64",
+ srcs = [":kselftest_tests_arm64"],
+ out = "tests.zip",
+)
diff --git a/abi.bzl b/abi.bzl
new file mode 100644
index 000000000000..0d57a05dcfcb
--- /dev/null
+++ b/abi.bzl
@@ -0,0 +1,74 @@
+# Copyright (C) 2023 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.
+"""
+ABI aware build rules.
+"""
+
+load("@bazel_skylib//lib:paths.bzl", "paths")
+load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
+
+visibility("private")
+
+_ALL_ABIS = ["arm64", "x86_64"]
+
+def _build_with_abi(name, build_rule, path_prefix, abis, visibility, **kwargs):
+ build_rule(name = name, visibility = visibility, **kwargs)
+
+ for abi in abis:
+ copy_file(
+ name = "{name}_{abi}".format(name = name, abi = abi),
+ src = ":{name}".format(name = name),
+ out = paths.join(path_prefix, abi, name),
+ allow_symlink = True,
+ visibility = visibility,
+ )
+
+def cc_binary_with_abi(name, path_prefix = None, abis = None, visibility = None, **kwargs):
+ """A cc_binary replacement that generates output in each subdirectory named by abi.
+
+ For example:
+ ```
+ cc_binary_with_abi(
+ name = "a_binary",
+ abis = ["x86_64", "arm64"],
+ path_prefix = "my/path",
+ )
+ ```
+ generates 2 rules:
+ * Rule a_binary_x86_64: Builds the cc_binary and put output in my/path/x86_64/a_binary.
+ * Rule a_binary_arm64: Builds the cc_binary and put output in my/path/arm64/a_binary.
+
+ Args:
+ name: the name of the build rule.
+ path_prefix: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
+ The path prefix to attach to output.
+ abis: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
+ The intended abis to generate.
+ visibility: [Nonconfigurable](https://bazel.build/reference/be/common-definitions#configurable-attributes).
+ The visibility attribute on a target controls whether the target can be used in other packages.
+ **kwargs: the rest args that cc_binary uses.
+ """
+ if not path_prefix:
+ path_prefix = ""
+ if not abis:
+ abis = _ALL_ABIS
+
+ _build_with_abi(
+ name = name,
+ build_rule = native.cc_binary,
+ path_prefix = path_prefix,
+ abis = abis,
+ visibility = visibility,
+ **kwargs
+ )
diff --git a/tools/testing/selftests/android/config_arm64.xml b/tools/testing/selftests/android/config_arm64.xml
new file mode 100644
index 000000000000..687f7c109526
--- /dev/null
+++ b/tools/testing/selftests/android/config_arm64.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tools/testing/selftests/android/config_x86_64.xml b/tools/testing/selftests/android/config_x86_64.xml
new file mode 100644
index 000000000000..08c1e18854f0
--- /dev/null
+++ b/tools/testing/selftests/android/config_x86_64.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+