Convert bionic benchmarks and tests to Android.bp

The compile-time tests and a few custom libraries for dynamic linker
testing are still compiled in make.

Also converts the make rules to run tests on the host to shell scripts
in tests/run-on-host.sh and benchmarks/run-on-host.sh

Change-Id: I6f174b3a69d58c4ed74d29f4e79332d483681534
diff --git a/benchmarks/Android.bp b/benchmarks/Android.bp
new file mode 100644
index 0000000..12f4940
--- /dev/null
+++ b/benchmarks/Android.bp
@@ -0,0 +1,64 @@
+//
+// Copyright (C) 2013 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.
+//
+
+cc_defaults {
+    name: "bionic-benchmarks-defaults",
+    cflags: [
+        "-O2",
+        "-fno-builtin",
+        "-Wall",
+        "-Wextra",
+        "-Werror",
+        "-Wunused",
+    ],
+    srcs: [
+        "math_benchmark.cpp",
+        "property_benchmark.cpp",
+        "pthread_benchmark.cpp",
+        "semaphore_benchmark.cpp",
+        "stdio_benchmark.cpp",
+        "string_benchmark.cpp",
+        "time_benchmark.cpp",
+        "unistd_benchmark.cpp",
+    ],
+}
+
+// Build benchmarks for the device (with bionic's .so). Run with:
+//   adb shell bionic-benchmarks32
+//   adb shell bionic-benchmarks64
+cc_benchmark {
+    name: "bionic-benchmarks",
+    defaults: ["bionic-benchmarks-defaults"],
+}
+
+// We don't build a static benchmark executable because it's not usually
+// useful. If you're trying to run the current benchmarks on an older
+// release, it's (so far at least) always because you want to measure the
+// performance of the old release's libc, and a static benchmark isn't
+// going to let you do that.
+
+// Build benchmarks for the host (against glibc!). Run with:
+cc_benchmark_host {
+    name: "bionic-benchmarks-glibc",
+    defaults: ["bionic-benchmarks-defaults"],
+    host_ldlibs: ["-lrt"],
+    target: {
+        darwin: {
+            // Only supported on linux systems.
+            enabled: false,
+        },
+    },
+}
diff --git a/benchmarks/Android.mk b/benchmarks/Android.mk
deleted file mode 100644
index 6f2651e..0000000
--- a/benchmarks/Android.mk
+++ /dev/null
@@ -1,94 +0,0 @@
-#
-# Copyright (C) 2013 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.
-#
-
-LOCAL_PATH := $(call my-dir)
-
-benchmark_cflags := \
-    -O2 \
-    -fno-builtin \
-    -Wall \
-    -Wextra \
-    -Werror \
-    -Wunused \
-
-benchmark_cppflags := \
-
-benchmark_src_files := \
-    math_benchmark.cpp \
-    property_benchmark.cpp \
-    pthread_benchmark.cpp \
-    semaphore_benchmark.cpp \
-    stdio_benchmark.cpp \
-    string_benchmark.cpp \
-    time_benchmark.cpp \
-    unistd_benchmark.cpp \
-
-# Build benchmarks for the device (with bionic's .so). Run with:
-#   adb shell bionic-benchmarks32
-#   adb shell bionic-benchmarks64
-include $(CLEAR_VARS)
-LOCAL_MODULE := bionic-benchmarks
-LOCAL_MODULE_STEM_32 := bionic-benchmarks32
-LOCAL_MODULE_STEM_64 := bionic-benchmarks64
-LOCAL_MULTILIB := both
-LOCAL_CFLAGS := $(benchmark_cflags)
-LOCAL_CPPFLAGS := $(benchmark_cppflags)
-LOCAL_SRC_FILES := $(benchmark_src_files)
-include $(BUILD_NATIVE_BENCHMARK)
-
-# We don't build a static benchmark executable because it's not usually
-# useful. If you're trying to run the current benchmarks on an older
-# release, it's (so far at least) always because you want to measure the
-# performance of the old release's libc, and a static benchmark isn't
-# going to let you do that.
-
-# Only supported on linux systems.
-ifeq ($(HOST_OS),linux)
-
-# Build benchmarks for the host (against glibc!). Run with:
-include $(CLEAR_VARS)
-LOCAL_MODULE := bionic-benchmarks-glibc
-LOCAL_MODULE_STEM_32 := bionic-benchmarks-glibc32
-LOCAL_MODULE_STEM_64 := bionic-benchmarks-glibc64
-LOCAL_MULTILIB := both
-LOCAL_CFLAGS := $(benchmark_cflags)
-LOCAL_CPPFLAGS := $(benchmark_cppflags)
-LOCAL_LDFLAGS := -lrt
-LOCAL_SRC_FILES := $(benchmark_src_files)
-LOCAL_STATIC_LIBRARIES := libgoogle-benchmark
-# TODO: BUILD_HOST_NATIVE_BENCHMARK
-include $(BUILD_HOST_EXECUTABLE)
-
-endif
-
-ifeq ($(HOST_OS)-$(HOST_ARCH),$(filter $(HOST_OS)-$(HOST_ARCH),linux-x86 linux-x86_64))
-include $(LOCAL_PATH)/../build/run-on-host.mk
-
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86 x86_64))
-bionic-benchmarks-run-on-host32: bionic-benchmarks bionic-prepare-run-on-host
-	ANDROID_DATA=$(TARGET_OUT_DATA) \
-	ANDROID_ROOT=$(TARGET_OUT) \
-		$(TARGET_OUT_EXECUTABLES)/bionic-benchmarks32 $(BIONIC_BENCHMARKS_FLAGS)
-endif
-
-ifeq ($(TARGET_IS_64_BIT),true)
-bionic-benchmarks-run-on-host64: bionic-benchmarks bionic-prepare-run-on-host
-	ANDROID_DATA=$(TARGET_OUT_DATA) \
-	ANDROID_ROOT=$(TARGET_OUT) \
-		$(TARGET_OUT_EXECUTABLES)/bionic-benchmarks64 $(BIONIC_BENCHMARKS_FLAGS)
-endif
-
-endif
diff --git a/benchmarks/run-on-host.sh b/benchmarks/run-on-host.sh
new file mode 100755
index 0000000..bc63628
--- /dev/null
+++ b/benchmarks/run-on-host.sh
@@ -0,0 +1,33 @@
+#!/bin/bash -e
+
+. $(dirname $0)/../build/run-on-host.sh
+
+if [ "$1" = glibc ]; then
+    m -j bionic-benchmarks-glibc
+    (
+        cd ${ANDROID_BUILD_TOP}
+        export ANDROID_DATA=${TARGET_OUT_DATA}
+        export ANDROID_ROOT=${TARGET_OUT}
+        ${HOST_OUT}/nativetest64/bionic-benchmarks-glibc/bionic-benchmarks-glibc $@
+    )
+    exit 0
+elif [ "$1" != 32 -a "$1" != 64 ]; then
+    echo "Usage: $0 [ 32 | 64 | glibc ] [gtest flags]"
+    exit 1
+fi
+
+if [ ${HOST_OS}-${HOST_ARCH} = linux-x86 -o ${HOST_OS}-${HOST_ARCH} = linux-x86_64 ]; then
+
+    prepare $1 bionic-benchmarks
+
+    if [ ${TARGET_ARCH} = x86 -o ${TARGET_ARCH} = x86_64 ]; then
+        (
+            cd ${ANDROID_BUILD_TOP}
+            export ANDROID_DATA=${TARGET_OUT_DATA}
+            export ANDROID_ROOT=${TARGET_OUT}
+            ${NATIVETEST}/bionic-benchmarks/bionic-benchmarks $@
+        )
+    else
+        echo "$0 not supported on TARGET_ARCH=$TARGET_ARCH"
+    fi
+fi
diff --git a/build/run-on-host.mk b/build/run-on-host.mk
deleted file mode 100644
index dc7e5d5..0000000
--- a/build/run-on-host.mk
+++ /dev/null
@@ -1,44 +0,0 @@
-#
-# Copyright (C) 2014 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.
-#
-
-# Include once
-ifneq ($(bionic_run_on_host_mk_included),true)
-bionic_run_on_host_mk_included:=true
-
-ifneq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),arm mips x86))
-LINKER = linker64
-else
-LINKER = linker
-endif
-
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86 x86_64))
-# gtest needs ANDROID_DATA/local/tmp for death test output.
-# Make sure to create ANDROID_DATA/local/tmp if doesn't exist.
-# bionic itself should always work relative to ANDROID_DATA or ANDROID_ROOT.
-bionic-prepare-run-on-host: $(TARGET_OUT_EXECUTABLES)/$(LINKER) $(TARGET_OUT)/etc/hosts $(TARGET_OUT_EXECUTABLES)/sh
-	if [ ! -d /system ]; then \
-	  echo "Attempting to create /system"; \
-	  sudo mkdir -p -m 0777 /system; \
-	fi
-	mkdir -p $(TARGET_OUT_DATA)/local/tmp
-	ln -fs `realpath $(TARGET_OUT)/bin` /system/
-	ln -fs `realpath $(TARGET_OUT)/etc` /system/
-	ln -fs `realpath $(TARGET_OUT)/lib` /system/
-	if [ -d "$(TARGET_OUT)/lib64" ]; then \
-	  ln -fs `realpath $(TARGET_OUT)/lib64` /system/; \
-	fi
-endif
-endif
diff --git a/build/run-on-host.sh b/build/run-on-host.sh
new file mode 100644
index 0000000..c3a2751
--- /dev/null
+++ b/build/run-on-host.sh
@@ -0,0 +1,47 @@
+#!/bin/bash -e
+
+source ${ANDROID_BUILD_TOP}/build/envsetup.sh
+
+TARGET_ARCH=$(get_build_var TARGET_ARCH)
+TARGET_OUT=$(get_build_var TARGET_OUT)
+TARGET_OUT_EXECUTABLES=$(get_build_var TARGET_OUT_EXECUTABLES)
+TARGET_OUT_DATA=$(get_build_var TARGET_OUT_DATA)
+HOST_OS=$(get_build_var HOST_OS)
+HOST_ARCH=$(get_build_var HOST_ARCH)
+HOST_OUT=$(get_build_var HOST_OUT)
+
+function prepare()
+{
+    BITS=$1
+    shift
+
+    NATIVETEST=${TARGET_OUT_DATA}/nativetest
+    if [ "${BITS}" = 64 ]; then
+        NATIVETEST=${NATIVETEST}64
+    fi
+
+    if [ ${TARGET_ARCH} = arm -o ${TARGET_ARCH} = mips -o ${TARGET_ARCH} = x86 ]; then
+        LINKER=${TARGET_OUT_EXECUTABLES}/linker
+    else
+        LINKER="${TARGET_OUT_EXECUTABLES}/linker64 ${TARGET_OUT_EXECUTABLES}/linker"
+    fi
+
+    if [ ${TARGET_ARCH} = x86 -o ${TARGET_ARCH} = x86_64 ]; then
+        m -j ${LINKER} ${TARGET_OUT}/etc/hosts ${TARGET_OUT_EXECUTABLES}/sh $@
+
+        if [ ! -d /system ]; then
+            echo "Attempting to create /system";
+            sudo mkdir -p -m 0777 /system;
+        fi
+        (
+            cd ${ANDROID_BUILD_TOP}
+            mkdir -p ${TARGET_OUT_DATA}/local/tmp
+            ln -fs `realpath ${TARGET_OUT}/bin` /system/
+            ln -fs `realpath ${TARGET_OUT}/etc` /system/
+            ln -fs `realpath ${TARGET_OUT}/lib` /system/
+            if [ -d "${TARGET_OUT}/lib64" ]; then
+                ln -fs `realpath ${TARGET_OUT}/lib64` /system/;
+            fi
+        )
+    fi
+}
diff --git a/tests/Android.bp b/tests/Android.bp
new file mode 100644
index 0000000..4ea1122
--- /dev/null
+++ b/tests/Android.bp
@@ -0,0 +1,411 @@
+//
+// Copyright (C) 2012 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.
+//
+
+cc_defaults {
+    name: "bionic_tests_defaults",
+    host_supported: true,
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+    cflags: [
+        "-fstack-protector-all",
+        "-g",
+        "-Wall",
+        "-Wextra",
+        "-Wunused",
+        "-Werror",
+        "-fno-builtin",
+
+        // We want to test deprecated API too.
+        "-Wno-deprecated-declarations",
+
+        // For glibc.
+        "-D__STDC_LIMIT_MACROS",
+    ],
+    stl: "libc++",
+    sanitize: {
+        never: true,
+    },
+}
+
+// -----------------------------------------------------------------------------
+// All standard tests.
+// -----------------------------------------------------------------------------
+
+cc_test_library {
+    name: "libBionicStandardTests",
+    defaults: ["bionic_tests_defaults"],
+    srcs: [
+        "arpa_inet_test.cpp",
+        "buffer_tests.cpp",
+        "bug_26110743_test.cpp",
+        "complex_test.cpp",
+        "ctype_test.cpp",
+        "dirent_test.cpp",
+        "error_test.cpp",
+        "eventfd_test.cpp",
+        "fcntl_test.cpp",
+        "fenv_test.cpp",
+        "ftw_test.cpp",
+        "getauxval_test.cpp",
+        "getcwd_test.cpp",
+        "grp_pwd_test.cpp",
+        "ifaddrs_test.cpp",
+        "inttypes_test.cpp",
+        "libc_logging_test.cpp",
+        "libgen_basename_test.cpp",
+        "libgen_test.cpp",
+        "locale_test.cpp",
+        "malloc_test.cpp",
+        "math_test.cpp",
+        "mntent_test.cpp",
+        "netdb_test.cpp",
+        "net_if_test.cpp",
+        "netinet_ether_test.cpp",
+        "netinet_in_test.cpp",
+        "netinet_udp_test.cpp",
+        "nl_types_test.cpp",
+        "pthread_test.cpp",
+        "pty_test.cpp",
+        "regex_test.cpp",
+        "resolv_test.cpp",
+        "sched_test.cpp",
+        "search_test.cpp",
+        "semaphore_test.cpp",
+        "setjmp_test.cpp",
+        "signal_test.cpp",
+        "stack_protector_test.cpp",
+        "stack_protector_test_helper.cpp",
+        "stack_unwinding_test.cpp",
+        "stdatomic_test.cpp",
+        "stdint_test.cpp",
+        "stdio_nofortify_test.cpp",
+        "stdio_test.cpp",
+        "stdio_ext_test.cpp",
+        "stdlib_test.cpp",
+        "string_nofortify_test.cpp",
+        "string_test.cpp",
+        "string_posix_strerror_r_test.cpp",
+        "strings_nofortify_test.cpp",
+        "strings_test.cpp",
+        "sstream_test.cpp",
+        "sys_epoll_test.cpp",
+        "sys_mman_test.cpp",
+        "sys_personality_test.cpp",
+        "sys_prctl_test.cpp",
+        "sys_procfs_test.cpp",
+        "sys_ptrace_test.cpp",
+        "sys_quota_test.cpp",
+        "sys_resource_test.cpp",
+        "sys_select_test.cpp",
+        "sys_sendfile_test.cpp",
+        "sys_socket_test.cpp",
+        "sys_stat_test.cpp",
+        "sys_statvfs_test.cpp",
+        "sys_syscall_test.cpp",
+        "sys_sysinfo_test.cpp",
+        "sys_sysmacros_test.cpp",
+        "sys_time_test.cpp",
+        "sys_timex_test.cpp",
+        "sys_types_test.cpp",
+        "sys_uio_test.cpp",
+        "sys_vfs_test.cpp",
+        "sys_xattr_test.cpp",
+        "system_properties_test.cpp",
+        "time_test.cpp",
+        "uchar_test.cpp",
+        "uniqueptr_test.cpp",
+        "unistd_nofortify_test.cpp",
+        "unistd_test.cpp",
+        "utmp_test.cpp",
+        "wchar_test.cpp",
+        "wctype_test.cpp",
+    ],
+
+    include_dirs: [
+        "bionic/libc",
+        "external/tinyxml2",
+    ],
+
+    static_libs: ["libbase"],
+    host_ldlibs: ["-lrt"],
+}
+
+// -----------------------------------------------------------------------------
+// Fortify tests.
+// -----------------------------------------------------------------------------
+
+cc_defaults {
+    name: "bionic_fortify_tests_defaults",
+    cflags: [
+        "-Wno-error",
+        "-U_FORTIFY_SOURCE",
+    ],
+    srcs: ["fortify_test_main.cpp"],
+    target: {
+        host: {
+            clang_cflags: ["-D__clang__"],
+        },
+    },
+}
+
+cc_test_library {
+    name: "libfortify1-tests-gcc",
+    defaults: ["bionic_fortify_tests_defaults", "bionic_tests_defaults"],
+    clang: false,
+    cflags: [
+        "-D_FORTIFY_SOURCE=1",
+        "-DTEST_NAME=Fortify1_gcc"
+    ],
+}
+
+cc_test_library {
+    name: "libfortify2-tests-gcc",
+    defaults: ["bionic_fortify_tests_defaults", "bionic_tests_defaults"],
+    clang: false,
+    cflags: [
+        "-D_FORTIFY_SOURCE=2",
+        "-DTEST_NAME=Fortify2_gcc"
+    ],
+}
+
+cc_test_library {
+    name: "libfortify1-tests-clang",
+    defaults: ["bionic_fortify_tests_defaults", "bionic_tests_defaults"],
+    clang: true,
+    cflags: [
+        "-D_FORTIFY_SOURCE=1",
+        "-DTEST_NAME=Fortify1_clang"
+    ],
+}
+
+cc_test_library {
+    name: "libfortify2-tests-clang",
+    defaults: ["bionic_fortify_tests_defaults", "bionic_tests_defaults"],
+    clang: true,
+    cflags: [
+        "-D_FORTIFY_SOURCE=2",
+        "-DTEST_NAME=Fortify2_clang"
+    ],
+}
+
+// -----------------------------------------------------------------------------
+// Library of all tests (excluding the dynamic linker tests).
+// -----------------------------------------------------------------------------
+cc_test_library {
+    name: "libBionicTests",
+    defaults: ["bionic_tests_defaults"],
+    whole_static_libs: [
+        "libBionicStandardTests",
+        "libfortify1-tests-gcc",
+        "libfortify2-tests-gcc",
+        "libfortify1-tests-clang",
+        "libfortify2-tests-clang",
+    ],
+}
+
+// -----------------------------------------------------------------------------
+// Library of bionic customized gtest main function, with simplified output format.
+// -----------------------------------------------------------------------------
+cc_test_library {
+    name: "libBionicGtestMain",
+    defaults: ["bionic_tests_defaults"],
+    srcs: ["gtest_main.cpp"],
+    target: {
+        darwin: {
+            enabled: true,
+        },
+    },
+}
+
+// -----------------------------------------------------------------------------
+// Library of bionic customized gtest main function, with normal gtest output format,
+// which is needed by bionic cts test.
+// -----------------------------------------------------------------------------
+cc_test_library {
+    name: "libBionicCtsGtestMain",
+    defaults: ["bionic_tests_defaults"],
+    srcs: ["gtest_main.cpp"],
+    cppflags: ["-DUSING_GTEST_OUTPUT_FORMAT"],
+}
+
+// -----------------------------------------------------------------------------
+// Tests for the device using bionic's .so. Run with:
+//   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests32
+//   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests64
+//   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests-gcc32
+//   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests-gcc64
+// -----------------------------------------------------------------------------
+cc_defaults {
+    name: "bionic_unit_tests_defaults",
+    host_supported: false,
+
+    whole_static_libs: [
+        "libBionicTests",
+        "libBionicGtestMain",
+    ],
+
+    static_libs: [
+        "libtinyxml2",
+        "liblog",
+        "libbase",
+    ],
+
+    srcs: [
+        // TODO: Include __cxa_thread_atexit_test.cpp to glibc tests once it is upgraded (glibc 2.18+)
+        "atexit_test.cpp",
+        "dl_test.cpp",
+        "dlext_test.cpp",
+        "__cxa_thread_atexit_test.cpp",
+        "dlfcn_test.cpp",
+        "libdl_test.cpp",
+        "pthread_dlfcn_test.cpp",
+        "thread_local_test.cpp",
+    ],
+
+    conlyflags: [
+        "-fexceptions",
+        "-fnon-call-exceptions",
+    ],
+
+    ldflags: ["-Wl,--export-dynamic"],
+
+    include_dirs: ["bionic/libc"],
+
+    target: {
+        android: {
+            shared_libs: [
+                "libdl",
+                "libpagemap",
+                "libdl_preempt_test_1",
+                "libdl_preempt_test_2",
+                "libdl_test_df_1_global",
+            ],
+            static_libs: [
+                // The order of these libraries matters, do not shuffle them.
+                "libbase",
+                "libziparchive",
+                "libz",
+                "libutils",
+            ],
+        },
+    }
+}
+
+cc_test {
+    name: "bionic-unit-tests",
+    defaults: ["bionic_unit_tests_defaults", "bionic_tests_defaults"],
+    clang: true,
+}
+
+cc_test {
+    name: "bionic-unit-tests-gcc",
+    defaults: ["bionic_unit_tests_defaults", "bionic_tests_defaults"],
+    clang: false,
+}
+
+// -----------------------------------------------------------------------------
+// Tests for the device linked against bionic's static library. Run with:
+//   adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static32
+//   adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static64
+// -----------------------------------------------------------------------------
+cc_test {
+    name: "bionic-unit-tests-static",
+    defaults: ["bionic_tests_defaults"],
+    host_supported: false,
+
+    whole_static_libs: [
+        "libBionicTests",
+        "libBionicGtestMain",
+    ],
+
+    static_libs: [
+        "libm",
+        "libc",
+        "libc++_static",
+        "libdl",
+        "libtinyxml2",
+        "liblog",
+        "libbase",
+    ],
+
+    static_executable: true,
+    stl: "libc++_static",
+
+    // libc and libc++ both define std::nothrow. libc's is a private symbol, but this
+    // still causes issues when linking libc.a and libc++.a, since private isn't
+    // effective until it has been linked. To fix this, just allow multiple symbol
+    // definitions for the static tests.
+    ldflags: ["-Wl,--allow-multiple-definition"],
+}
+
+// -----------------------------------------------------------------------------
+// Tests to run on the host and linked against glibc. Run with:
+//   cd bionic/tests; mm bionic-unit-tests-glibc-run
+// -----------------------------------------------------------------------------
+
+cc_test_host {
+    name: "bionic-unit-tests-glibc",
+    defaults: ["bionic_tests_defaults"],
+
+    srcs: [
+        "atexit_test.cpp",
+        "dlfcn_test.cpp",
+        "dl_test.cpp",
+        "pthread_dlfcn_test.cpp",
+    ],
+
+    shared_libs: [
+        "libdl_preempt_test_1",
+        "libdl_preempt_test_2",
+
+        "libdl_test_df_1_global",
+    ],
+
+    whole_static_libs: [
+        "libBionicStandardTests",
+        "libBionicGtestMain",
+        "libfortify1-tests-gcc",
+        "libfortify2-tests-gcc",
+        "libfortify1-tests-clang",
+        "libfortify2-tests-clang",
+    ],
+
+    static_libs: [
+        "libbase",
+        "liblog",
+        "libcutils",
+    ],
+
+    host_ldlibs: [
+        "-lresolv",
+        "-lrt",
+        "-ldl",
+        "-lutil",
+    ],
+
+    include_dirs: ["bionic/libc"],
+
+    ldflags: ["-Wl,--export-dynamic"],
+
+    sanitize: {
+        never: false,
+    },
+}
+
+subdirs = ["libs"]
diff --git a/tests/Android.mk b/tests/Android.mk
index b3ff5eb..0da3b88 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -16,426 +16,14 @@
 
 LOCAL_PATH := $(call my-dir)
 
-# -----------------------------------------------------------------------------
-# Unit tests.
-# -----------------------------------------------------------------------------
-
 ifeq ($(HOST_OS)-$(HOST_ARCH),$(filter $(HOST_OS)-$(HOST_ARCH),linux-x86 linux-x86_64))
 build_host := true
 else
 build_host := false
 endif
 
-common_additional_dependencies := $(LOCAL_PATH)/Android.mk $(LOCAL_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
-# All standard tests.
-# -----------------------------------------------------------------------------
-test_cflags = \
-    -fstack-protector-all \
-    -g \
-    -Wall -Wextra -Wunused \
-    -Werror \
-    -fno-builtin \
-
-# We want to test deprecated API too.
-test_cflags += \
-    -Wno-deprecated-declarations \
-
-test_cflags += -D__STDC_LIMIT_MACROS  # For glibc.
-
-test_cppflags := \
-
-libBionicStandardTests_src_files := \
-    arpa_inet_test.cpp \
-    buffer_tests.cpp \
-    bug_26110743_test.cpp \
-    complex_test.cpp \
-    ctype_test.cpp \
-    dirent_test.cpp \
-    error_test.cpp \
-    eventfd_test.cpp \
-    fcntl_test.cpp \
-    fenv_test.cpp \
-    ftw_test.cpp \
-    getauxval_test.cpp \
-    getcwd_test.cpp \
-    grp_pwd_test.cpp \
-    ifaddrs_test.cpp \
-    inttypes_test.cpp \
-    libc_logging_test.cpp \
-    libgen_basename_test.cpp \
-    libgen_test.cpp \
-    locale_test.cpp \
-    malloc_test.cpp \
-    math_test.cpp \
-    mntent_test.cpp \
-    netdb_test.cpp \
-    net_if_test.cpp \
-    netinet_ether_test.cpp \
-    netinet_in_test.cpp \
-    netinet_udp_test.cpp \
-    nl_types_test.cpp \
-    pthread_test.cpp \
-    pty_test.cpp \
-    regex_test.cpp \
-    resolv_test.cpp \
-    sched_test.cpp \
-    search_test.cpp \
-    semaphore_test.cpp \
-    setjmp_test.cpp \
-    signal_test.cpp \
-    stack_protector_test.cpp \
-    stack_protector_test_helper.cpp \
-    stack_unwinding_test.cpp \
-    stdatomic_test.cpp \
-    stdint_test.cpp \
-    stdio_nofortify_test.cpp \
-    stdio_test.cpp \
-    stdio_ext_test.cpp \
-    stdlib_test.cpp \
-    string_nofortify_test.cpp \
-    string_test.cpp \
-    string_posix_strerror_r_test.cpp \
-    strings_nofortify_test.cpp \
-    strings_test.cpp \
-    sstream_test.cpp \
-    sys_epoll_test.cpp \
-    sys_mman_test.cpp \
-    sys_personality_test.cpp \
-    sys_prctl_test.cpp \
-    sys_procfs_test.cpp \
-    sys_ptrace_test.cpp \
-    sys_quota_test.cpp \
-    sys_resource_test.cpp \
-    sys_select_test.cpp \
-    sys_sendfile_test.cpp \
-    sys_socket_test.cpp \
-    sys_stat_test.cpp \
-    sys_statvfs_test.cpp \
-    sys_syscall_test.cpp \
-    sys_sysinfo_test.cpp \
-    sys_sysmacros_test.cpp \
-    sys_time_test.cpp \
-    sys_timex_test.cpp \
-    sys_types_test.cpp \
-    sys_uio_test.cpp \
-    sys_vfs_test.cpp \
-    sys_xattr_test.cpp \
-    system_properties_test.cpp \
-    time_test.cpp \
-    uchar_test.cpp \
-    uniqueptr_test.cpp \
-    unistd_nofortify_test.cpp \
-    unistd_test.cpp \
-    utmp_test.cpp \
-    wchar_test.cpp \
-    wctype_test.cpp \
-
-libBionicStandardTests_cflags := \
-    $(test_cflags) \
-
-libBionicStandardTests_cppflags := \
-    $(test_cppflags) \
-
-libBionicStandardTests_c_includes := \
-    bionic/libc \
-    external/tinyxml2 \
-
-libBionicStandardTests_static_libraries := \
-    libbase \
-
-libBionicStandardTests_ldlibs_host := \
-    -lrt \
-
-module := libBionicStandardTests
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-include $(LOCAL_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
-# Fortify tests.
-# -----------------------------------------------------------------------------
-$(foreach compiler,gcc clang, \
-  $(foreach test,1 2, \
-    $(eval fortify$(test)-tests-$(compiler)_cflags := \
-      $(test_cflags) \
-      -Wno-error \
-      -U_FORTIFY_SOURCE \
-      -D_FORTIFY_SOURCE=$(test) \
-      -DTEST_NAME=Fortify$(test)_$(compiler)); \
-    $(eval fortify$(test)-tests-$(compiler)_src_files := \
-      fortify_test_main.cpp); \
-    $(eval fortify_libs += fortify$(test)-tests-$(compiler)); \
-  ) \
-)
-
-fortify1-tests-gcc_clang_target := false
-module := fortify1-tests-gcc
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-include $(LOCAL_PATH)/Android.build.mk
-
-fortify2-tests-gcc_clang_target := false
-module := fortify2-tests-gcc
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-include $(LOCAL_PATH)/Android.build.mk
-
-fortify1-tests-clang_clang_target := true
-fortify1-tests-clang_cflags_host := -D__clang__
-
-module := fortify1-tests-clang
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-include $(LOCAL_PATH)/Android.build.mk
-
-fortify2-tests-clang_clang_target := true
-
-fortify2-tests-clang_cflags_host := -D__clang__
-
-module := fortify2-tests-clang
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-include $(LOCAL_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
-# Library of all tests (excluding the dynamic linker tests).
-# -----------------------------------------------------------------------------
-libBionicTests_whole_static_libraries := \
-    libBionicStandardTests \
-    $(fortify_libs) \
-
-module := libBionicTests
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-include $(LOCAL_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
-# Library of bionic customized gtest main function, with simplified output format.
-# -----------------------------------------------------------------------------
-libBionicGtestMain_src_files := gtest_main.cpp
-
-libBionicGtestMain_cflags := $(test_cflags)
-
-libBionicGtestMain_cppflags := $(test_cppflags)
-
-module := libBionicGtestMain
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-
-ifeq ($(HOST_OS),$(filter $(HOST_OS),linux darwin))
-saved_build_host := $(build_host)
-build_host := true
-include $(LOCAL_PATH)/Android.build.mk
-build_host := $(saved_build_host)
-endif
-
-# -----------------------------------------------------------------------------
-# Library of bionic customized gtest main function, with normal gtest output format,
-# which is needed by bionic cts test.
-# -----------------------------------------------------------------------------
-libBionicCtsGtestMain_src_files := gtest_main.cpp
-
-libBionicCtsGtestMain_cflags := $(test_cflags)
-
-libBionicCtsGtestMain_cppflags := $(test_cppflags) -DUSING_GTEST_OUTPUT_FORMAT \
-
-module := libBionicCtsGtestMain
-module_tag := optional
-build_type := target
-build_target := STATIC_TEST_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-include $(LOCAL_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
-# Tests for the device using bionic's .so. Run with:
-#   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests32
-#   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests64
-#   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests-gcc32
-#   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests-gcc64
-# -----------------------------------------------------------------------------
-common_bionic-unit-tests_whole_static_libraries := \
-    libBionicTests \
-    libBionicGtestMain \
-
-common_bionic-unit-tests_static_libraries := \
-    libtinyxml2 \
-    liblog \
-    libbase \
-
-# TODO: Include __cxa_thread_atexit_test.cpp to glibc tests once it is upgraded (glibc 2.18+)
-common_bionic-unit-tests_src_files := \
-    atexit_test.cpp \
-    dl_test.cpp \
-    dlext_test.cpp \
-    __cxa_thread_atexit_test.cpp \
-    dlfcn_test.cpp \
-    libdl_test.cpp \
-    pthread_dlfcn_test.cpp \
-    thread_local_test.cpp \
-
-common_bionic-unit-tests_cflags := $(test_cflags)
-
-common_bionic-unit-tests_conlyflags := \
-    -fexceptions \
-    -fnon-call-exceptions \
-
-common_bionic-unit-tests_cppflags := $(test_cppflags)
-
-common_bionic-unit-tests_ldflags := \
-    -Wl,--export-dynamic
-
-common_bionic-unit-tests_c_includes := \
-    bionic/libc \
-
-common_bionic-unit-tests_shared_libraries_target := \
-    libdl \
-    libpagemap \
-    libdl_preempt_test_1 \
-    libdl_preempt_test_2 \
-    libdl_test_df_1_global \
-
-# The order of these libraries matters, do not shuffle them.
-common_bionic-unit-tests_static_libraries_target := \
-    libbase \
-    libziparchive \
-    libz \
-    libutils \
-
-module_tag := optional
-build_type := target
-build_target := NATIVE_TEST
-
-module := bionic-unit-tests
-bionic-unit-tests_clang_target := true
-bionic-unit-tests_whole_static_libraries := $(common_bionic-unit-tests_whole_static_libraries)
-bionic-unit-tests_static_libraries := $(common_bionic-unit-tests_static_libraries)
-bionic-unit-tests_src_files := $(common_bionic-unit-tests_src_files)
-bionic-unit-tests_cflags := $(common_bionic-unit-tests_cflags)
-bionic-unit-tests_conlyflags := $(common_bionic-unit-tests_conlyflags)
-bionic-unit-tests_cppflags := $(common_bionic-unit-tests_cppflags)
-bionic-unit-tests_ldflags := $(common_bionic-unit-tests_ldflags)
-bionic-unit-tests_c_includes := $(common_bionic-unit-tests_c_includes)
-bionic-unit-tests_shared_libraries_target := $(common_bionic-unit-tests_shared_libraries_target)
-bionic-unit-tests_static_libraries_target := $(common_bionic-unit-tests_static_libraries_target)
-include $(LOCAL_PATH)/Android.build.mk
-
-module := bionic-unit-tests-gcc
-bionic-unit-tests-gcc_clang_target := false
-bionic-unit-tests-gcc_whole_static_libraries := $(common_bionic-unit-tests_whole_static_libraries)
-bionic-unit-tests-gcc_static_libraries := $(common_bionic-unit-tests_static_libraries)
-bionic-unit-tests-gcc_src_files := $(common_bionic-unit-tests_src_files)
-bionic-unit-tests-gcc_cflags := $(common_bionic-unit-tests_cflags)
-bionic-unit-tests-gcc_conlyflags := $(common_bionic-unit-tests_conlyflags)
-bionic-unit-tests-gcc_cppflags := $(common_bionic-unit-tests_cppflags)
-bionic-unit-tests-gcc_ldflags := $(common_bionic-unit-tests_ldflags)
-bionic-unit-tests-gcc_c_includes := $(common_bionic-unit-tests_c_includes)
-bionic-unit-tests-gcc_shared_libraries_target := $(common_bionic-unit-tests_shared_libraries_target)
-bionic-unit-tests-gcc_static_libraries_target := $(common_bionic-unit-tests_static_libraries_target)
-include $(LOCAL_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
-# Tests for the device linked against bionic's static library. Run with:
-#   adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static32
-#   adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static64
-# -----------------------------------------------------------------------------
-bionic-unit-tests-static_whole_static_libraries := \
-    libBionicTests \
-    libBionicGtestMain \
-
-bionic-unit-tests-static_static_libraries := \
-    libm \
-    libc \
-    libc++_static \
-    libdl \
-    libtinyxml2 \
-    liblog \
-    libbase \
-
-bionic-unit-tests-static_force_static_executable := true
-
-# libc and libc++ both define std::nothrow. libc's is a private symbol, but this
-# still causes issues when linking libc.a and libc++.a, since private isn't
-# effective until it has been linked. To fix this, just allow multiple symbol
-# definitions for the static tests.
-bionic-unit-tests-static_ldflags := -Wl,--allow-multiple-definition
-
-module := bionic-unit-tests-static
-module_tag := optional
-build_type := target
-build_target := NATIVE_TEST
-include $(LOCAL_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
-# Tests to run on the host and linked against glibc. Run with:
-#   cd bionic/tests; mm bionic-unit-tests-glibc-run
-# -----------------------------------------------------------------------------
-
 ifeq ($(HOST_OS)-$(HOST_ARCH),$(filter $(HOST_OS)-$(HOST_ARCH),linux-x86 linux-x86_64))
 
-bionic-unit-tests-glibc_src_files := \
-    atexit_test.cpp \
-    dlfcn_test.cpp \
-    dl_test.cpp \
-    pthread_dlfcn_test.cpp \
-
-bionic-unit-tests-glibc_shared_libraries := \
-    libdl_preempt_test_1 \
-    libdl_preempt_test_2
-
-bionic-unit-tests-glibc_shared_libraries += libdl_test_df_1_global
-
-bionic-unit-tests-glibc_whole_static_libraries := \
-    libBionicStandardTests \
-    libBionicGtestMain \
-    $(fortify_libs) \
-
-bionic-unit-tests-glibc_static_libraries := \
-    libbase \
-    liblog \
-    libcutils \
-
-bionic-unit-tests-glibc_ldlibs := \
-    -lresolv -lrt -ldl -lutil \
-
-bionic-unit-tests-glibc_c_includes := \
-    bionic/libc \
-
-bionic-unit-tests-glibc_cflags := $(test_cflags)
-bionic-unit-tests-glibc_cppflags := $(test_cppflags)
-bionic-unit-tests-glibc_ldflags := -Wl,--export-dynamic
-
-bionic-unit-tests-glibc_allow_asan := true
-
-module := bionic-unit-tests-glibc
-module_tag := optional
-build_type := host
-build_target := NATIVE_TEST
-include $(LOCAL_PATH)/Android.build.mk
-
 # -----------------------------------------------------------------------------
 # Compile time tests.
 # -----------------------------------------------------------------------------
@@ -483,52 +71,6 @@
 LOCAL_SRC_FILES :=
 include $(BUILD_STATIC_LIBRARY)
 
-# -----------------------------------------------------------------------------
-# Host glibc tests.
-# -----------------------------------------------------------------------------
-
-# gtest needs ANDROID_DATA/local/tmp for death test output.
-# Make sure to create ANDROID_DATA/local/tmp if doesn't exist.
-# Use the current target out directory as ANDROID_DATA.
-# BIONIC_TEST_FLAGS is either empty or it comes from the user.
-.PHONY: bionic-unit-tests-glibc-run
-bionic-unit-tests-glibc-run: bionic-unit-tests-glibc
-	mkdir -p $(TARGET_OUT_DATA)/local/tmp
-	ANDROID_DATA=$(TARGET_OUT_DATA) \
-	ANDROID_ROOT=$(TARGET_OUT) \
-		$(HOST_OUT_EXECUTABLES)/bionic-unit-tests-glibc64 $(BIONIC_TEST_FLAGS)
-
-# -----------------------------------------------------------------------------
-# Run the unit tests built against x86 bionic on an x86 host.
-# -----------------------------------------------------------------------------
-
-include $(LOCAL_PATH)/../build/run-on-host.mk
-
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86 x86_64))
-
-TEST_TIMEOUT := 0
-
-# BIONIC_TEST_FLAGS is either empty or it comes from the user.
-.PHONY: bionic-unit-tests-run-on-host32
-bionic-unit-tests-run-on-host32: bionic-unit-tests bionic-prepare-run-on-host
-	ANDROID_DATA=$(TARGET_OUT_DATA) \
-	ANDROID_DNS_MODE=local \
-	ANDROID_ROOT=$(TARGET_OUT) \
-		timeout $(TEST_TIMEOUT) \
-		$(TARGET_OUT_DATA)/nativetest/bionic-unit-tests/bionic-unit-tests32 $(BIONIC_TEST_FLAGS)
-
-ifeq ($(TARGET_IS_64_BIT),true)
-# add target to run lp64 tests
-.PHONY: bionic-unit-tests-run-on-host64
-bionic-unit-tests-run-on-host64: bionic-unit-tests bionic-prepare-run-on-host
-	ANDROID_DATA=$(TARGET_OUT_DATA) \
-	ANDROID_DNS_MODE=local \
-	ANDROID_ROOT=$(TARGET_OUT) \
-		timeout $(TEST_TIMEOUT) \
-		$(TARGET_OUT_DATA)/nativetest64/bionic-unit-tests/bionic-unit-tests64 $(BIONIC_TEST_FLAGS)
-endif
-
-endif # x86 x86_64
 endif # linux-x86
 
 include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
new file mode 100644
index 0000000..0303138
--- /dev/null
+++ b/tests/libs/Android.bp
@@ -0,0 +1,443 @@
+//
+// Copyright (C) 2012 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.
+//
+
+cc_defaults {
+    name: "bionic_testlib_defaults",
+    host_supported: true,
+    sanitize: {
+        never: true,
+    },
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
+
+// -----------------------------------------------------------------------------
+// Library to test gnu-styled hash
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libgnu-hash-table-library",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlext_test_library.cpp"],
+    ldflags: ["-Wl,--hash-style=gnu"],
+    arch: {
+        mips: {
+            enabled: false,
+        },
+        mips64: {
+            enabled: false,
+        },
+    },
+}
+
+// -----------------------------------------------------------------------------
+// Library to test sysv-styled hash
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libsysv-hash-table-library",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlext_test_library.cpp"],
+    ldflags: ["-Wl,--hash-style=sysv"],
+}
+
+// -----------------------------------------------------------------------------
+// Library used by dlext tests - with GNU RELRO program header
+// -----------------------------------------------------------------------------
+// In Android.mk to support creating symlinks
+
+// -----------------------------------------------------------------------------
+// Library used by dlext tests - without GNU RELRO program header
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libdlext_test_norelro",
+    srcs: ["dlext_test_library.cpp"],
+    ldflags: ["-Wl,-z,norelro"],
+    shared_libs = ["libtest_simple"],
+}
+
+// -----------------------------------------------------------------------------
+// Library used by dlext tests - different name non-default location
+// -----------------------------------------------------------------------------
+// In Android.mk to support installing to /data
+
+// -----------------------------------------------------------------------------
+// Libraries used by dlext tests for open from a zip-file
+// -----------------------------------------------------------------------------
+// In Android.mk to support installing to /data
+
+// ----------------------------------------------------------------------------
+// Library with soname which does not match filename
+// ----------------------------------------------------------------------------
+// In Android.mk to support zipped and aligned tests
+
+// -----------------------------------------------------------------------------
+// Library used by dlext tests - zipped and aligned
+// -----------------------------------------------------------------------------
+// In Android.mk to support zipped and aligned tests
+
+// -----------------------------------------------------------------------------
+// Library used by dlfcn tests
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_simple",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_simple.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library used by dlfcn nodelete tests
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_nodelete_1",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_nodelete_1.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library used by dlfcn nodelete tests
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_nodelete_2",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_nodelete_2.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library used by dlfcn nodelete tests
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_nodelete_dt_flags_1",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_nodelete_dt_flags_1.cpp"],
+    ldflags: ["-Wl,-z,nodelete"],
+}
+
+// -----------------------------------------------------------------------------
+// Build test helper libraries for linker namespaces
+// -----------------------------------------------------------------------------
+// include $(LOCAL_PATH)/Android.build.linker_namespaces.mk
+
+// -----------------------------------------------------------------------------
+// Build DT_RUNPATH test helper libraries
+// -----------------------------------------------------------------------------
+// include $(LOCAL_PATH)/Android.build.dt_runpath.mk
+
+// -----------------------------------------------------------------------------
+// Build library with two parents
+// -----------------------------------------------------------------------------
+// include $(LOCAL_PATH)/Android.build.dlopen_2_parents_reloc.mk
+
+// -----------------------------------------------------------------------------
+// Build libtest_check_order_dlsym.so with its dependencies.
+// -----------------------------------------------------------------------------
+// include $(LOCAL_PATH)/Android.build.dlopen_check_order_dlsym.mk
+
+// -----------------------------------------------------------------------------
+// Build libtest_check_order_siblings.so with its dependencies.
+// -----------------------------------------------------------------------------
+// include $(LOCAL_PATH)/Android.build.dlopen_check_order_reloc_siblings.mk
+
+// -----------------------------------------------------------------------------
+// Build libtest_check_order_root.so with its dependencies.
+// -----------------------------------------------------------------------------
+// include $(LOCAL_PATH)/Android.build.dlopen_check_order_reloc_main_executable.mk
+
+// -----------------------------------------------------------------------------
+// Build libtest_versioned_lib.so with its dependencies.
+// -----------------------------------------------------------------------------
+// include $(LOCAL_PATH)/Android.build.versioned_lib.mk
+
+// -----------------------------------------------------------------------------
+// Build libraries needed by pthread_atfork tests
+// -----------------------------------------------------------------------------
+// include $(LOCAL_PATH)/Android.build.pthread_atfork.mk
+
+// -----------------------------------------------------------------------------
+// Library with dependency loop used by dlfcn tests
+//
+// libtest_with_dependency_loop -> a -> b -> c -> a
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_with_dependency_loop",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_loopy_root.cpp"],
+    shared_libs: ["libtest_with_dependency_loop_a"],
+}
+
+// -----------------------------------------------------------------------------
+// libtest_with_dependency_loop_a.so
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_with_dependency_loop_a",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_loopy_a.cpp"],
+    shared_libs: ["libtest_with_dependency_loop_b_tmp"],
+}
+
+// -----------------------------------------------------------------------------
+// libtest_with_dependency_loop_b.so
+//
+// this is temporary placeholder - will be removed
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_with_dependency_loop_b_tmp",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_loopy_invalid.cpp"],
+    ldflags: ["-Wl,-soname=libtest_with_dependency_loop_b.so"],
+}
+
+// -----------------------------------------------------------------------------
+// libtest_with_dependency_loop_b.so
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_with_dependency_loop_b",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_loopy_b.cpp"],
+    shared_libs: ["libtest_with_dependency_loop_c"],
+}
+
+// -----------------------------------------------------------------------------
+// libtest_with_dependency_loop_c.so
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_with_dependency_loop_c",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_loopy_c.cpp"],
+    shared_libs: ["libtest_with_dependency_loop_a"],
+}
+
+// -----------------------------------------------------------------------------
+// libtest_relo_check_dt_needed_order.so
+// |
+// +-> libtest_relo_check_dt_needed_order_1.so
+// |
+// +-> libtest_relo_check_dt_needed_order_2.so
+// -----------------------------------------------------------------------------
+
+
+cc_library {
+    name: "libtest_relo_check_dt_needed_order",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_relo_check_dt_needed_order.cpp"],
+    shared_libs: [
+        "libtest_relo_check_dt_needed_order_1",
+        "libtest_relo_check_dt_needed_order_2",
+    ],
+}
+
+cc_library {
+    name: "libtest_relo_check_dt_needed_order_1",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_relo_check_dt_needed_order_1.cpp"],
+}
+
+cc_library {
+    name: "libtest_relo_check_dt_needed_order_2",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_relo_check_dt_needed_order_2.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library with dependency used by dlfcn tests
+// -----------------------------------------------------------------------------
+// In Android.mk to support dependency on libdlext_test
+
+// -----------------------------------------------------------------------------
+// Library used by ifunc tests
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_ifunc",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_ifunc.c"],
+
+    // TODO(dimitry): clang does not support ifunc attribute
+    clang: false,
+    arch: {
+        mips: {
+            enabled: false,
+        },
+        mips64: {
+            enabled: false,
+        },
+    },
+}
+
+// -----------------------------------------------------------------------------
+// Library used by atexit tests
+// -----------------------------------------------------------------------------
+
+cc_library {
+    name: "libtest_atexit",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["atexit_testlib.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// This library is used by dl_load test to check symbol preempting
+// by main executable
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libdl_preempt_test_1",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dl_preempt_library_1.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// This library is used by dl_load test to check symbol preempting
+// by libdl_preempt_test_1.so
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libdl_preempt_test_2",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dl_preempt_library_2.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library with DF_1_GLOBAL
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libdl_test_df_1_global",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dl_df_1_global.cpp"],
+    ldflags: ["-Wl,-z,global"],
+
+    target: {
+        host: {
+            // TODO (dimitry): host ld.gold does not yet support -z global
+            // remove this line once it is updated.
+            ldflags: ["-fuse-ld=bfd"],
+        },
+    },
+}
+
+// -----------------------------------------------------------------------------
+// Library using symbol from libdl_test_df_1_global
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_dlsym_df_1_global",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dl_df_1_use_global.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library with weak function
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_dlsym_weak_func",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlsym_weak_function.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library to check RTLD_LOCAL with dlsym in 'this'
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_dlsym_from_this",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlsym_from_this_symbol.cpp"],
+    shared_libs: ["libtest_dlsym_from_this_child"],
+
+    target: {
+        android: {
+            shared_libs: ["libdl"],
+        },
+    },
+}
+
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_dlsym_from_this_child",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlsym_from_this_functions.cpp"],
+    shared_libs: ["libtest_dlsym_from_this_grandchild"],
+}
+
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_dlsym_from_this_grandchild",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlsym_from_this_symbol2.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Empty library
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_empty",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["empty.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library with weak undefined function
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_dlopen_weak_undefined_func",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_weak_undefined.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library with constructor that calls dlopen() b/7941716
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_dlopen_from_ctor",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dlopen_testlib_dlopen_from_ctor.cpp"],
+    target: {
+        android: {
+            shared_libs: ["libdl"],
+        },
+    },
+}
+
+// -----------------------------------------------------------------------------
+// Library that depends on the library with constructor that calls dlopen() b/7941716
+// -----------------------------------------------------------------------------
+cc_library {
+    name: "libtest_dlopen_from_ctor_main",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["empty.cpp"],
+    shared_libs: ["libtest_dlopen_from_ctor"],
+}
+
+// -----------------------------------------------------------------------------
+// Tool to use to align the shared libraries in a zip file.
+// -----------------------------------------------------------------------------
+cc_binary_host {
+    name: "bionic_tests_zipalign",
+    srcs: ["bionic_tests_zipalign.cpp"],
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+
+    static_libs: [
+        "libziparchive",
+        "liblog",
+        "libbase",
+        "libz",
+        "libutils",
+    ],
+
+    target: {
+        windows: {
+            enabled: true,
+        },
+    },
+}
diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk
index 82bfc05..aa70b86 100644
--- a/tests/libs/Android.mk
+++ b/tests/libs/Android.mk
@@ -33,34 +33,6 @@
     $(TEST_PATH)/Android.build.mk
 
 # -----------------------------------------------------------------------------
-# Library to test gnu-styled hash
-# -----------------------------------------------------------------------------
-ifneq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),mips mips64))
-libgnu-hash-table-library_src_files := \
-    dlext_test_library.cpp \
-
-libgnu-hash-table-library_ldflags := \
-    -Wl,--hash-style=gnu \
-
-module := libgnu-hash-table-library
-module_tag := optional
-include $(LOCAL_PATH)/Android.build.testlib.mk
-endif
-
-# -----------------------------------------------------------------------------
-# Library to test sysv-styled hash
-# -----------------------------------------------------------------------------
-libsysv-hash-table-library_src_files := \
-    dlext_test_library.cpp \
-
-libsysv-hash-table-library_ldflags := \
-    -Wl,--hash-style=sysv \
-
-module := libsysv-hash-table-library
-module_tag := optional
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
 # Library used by dlext tests - with GNU RELRO program header
 # -----------------------------------------------------------------------------
 libdlext_test_src_files := \
@@ -96,23 +68,6 @@
     $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so
 
 # -----------------------------------------------------------------------------
-# Library used by dlext tests - without GNU RELRO program header
-# -----------------------------------------------------------------------------
-libdlext_test_norelro_src_files := \
-    dlext_test_library.cpp \
-
-libdlext_test_norelro_ldflags := \
-    -Wl,-z,norelro \
-
-libdlext_test_norelro_shared_libraries := libtest_simple
-
-module := libdlext_test_norelro
-module_tag := optional
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
-
-# -----------------------------------------------------------------------------
 # Library used by dlext tests - different name non-default location
 # -----------------------------------------------------------------------------
 libdlext_test_fd_src_files := \
@@ -176,44 +131,6 @@
 endif
 
 # -----------------------------------------------------------------------------
-# Library used by dlfcn tests
-# -----------------------------------------------------------------------------
-libtest_simple_src_files := \
-    dlopen_testlib_simple.cpp
-
-module := libtest_simple
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library used by dlfcn nodelete tests
-# -----------------------------------------------------------------------------
-libtest_nodelete_1_src_files := \
-    dlopen_nodelete_1.cpp
-
-module := libtest_nodelete_1
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library used by dlfcn nodelete tests
-# -----------------------------------------------------------------------------
-libtest_nodelete_2_src_files := \
-    dlopen_nodelete_2.cpp
-
-module := libtest_nodelete_2
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library used by dlfcn nodelete tests
-# -----------------------------------------------------------------------------
-libtest_nodelete_dt_flags_1_src_files := \
-    dlopen_nodelete_dt_flags_1.cpp
-
-libtest_nodelete_dt_flags_1_ldflags := -Wl,-z,nodelete
-
-module := libtest_nodelete_dt_flags_1
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
 # Build test helper libraries for linker namespaces
 # -----------------------------------------------------------------------------
 include $(LOCAL_PATH)/Android.build.linker_namespaces.mk
@@ -254,82 +171,6 @@
 include $(LOCAL_PATH)/Android.build.pthread_atfork.mk
 
 # -----------------------------------------------------------------------------
-# Library with dependency loop used by dlfcn tests
-#
-# libtest_with_dependency_loop -> a -> b -> c -> a
-# -----------------------------------------------------------------------------
-libtest_with_dependency_loop_src_files := dlopen_testlib_loopy_root.cpp
-
-libtest_with_dependency_loop_shared_libraries := \
-    libtest_with_dependency_loop_a
-
-module := libtest_with_dependency_loop
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_with_dependency_loop_a.so
-# -----------------------------------------------------------------------------
-libtest_with_dependency_loop_a_src_files := dlopen_testlib_loopy_a.cpp
-
-libtest_with_dependency_loop_a_shared_libraries := \
-    libtest_with_dependency_loop_b_tmp
-
-module := libtest_with_dependency_loop_a
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_with_dependency_loop_b.so
-#
-# this is temporary placeholder - will be removed
-# -----------------------------------------------------------------------------
-libtest_with_dependency_loop_b_tmp_src_files := dlopen_testlib_loopy_invalid.cpp
-libtest_with_dependency_loop_b_tmp_ldflags := -Wl,-soname=libtest_with_dependency_loop_b.so
-
-module := libtest_with_dependency_loop_b_tmp
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_with_dependency_loop_b.so
-# -----------------------------------------------------------------------------
-libtest_with_dependency_loop_b_src_files := dlopen_testlib_loopy_b.cpp
-libtest_with_dependency_loop_b_shared_libraries := libtest_with_dependency_loop_c
-
-module := libtest_with_dependency_loop_b
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_with_dependency_loop_c.so
-# -----------------------------------------------------------------------------
-libtest_with_dependency_loop_c_src_files := dlopen_testlib_loopy_c.cpp
-
-libtest_with_dependency_loop_c_shared_libraries := \
-    libtest_with_dependency_loop_a
-
-module := libtest_with_dependency_loop_c
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_relo_check_dt_needed_order.so
-# |
-# +-> libtest_relo_check_dt_needed_order_1.so
-# |
-# +-> libtest_relo_check_dt_needed_order_2.so
-# -----------------------------------------------------------------------------
-libtest_relo_check_dt_needed_order_shared_libraries := \
-    libtest_relo_check_dt_needed_order_1 libtest_relo_check_dt_needed_order_2
-
-libtest_relo_check_dt_needed_order_src_files := dlopen_testlib_relo_check_dt_needed_order.cpp
-libtest_relo_check_dt_needed_order_1_src_files := dlopen_testlib_relo_check_dt_needed_order_1.cpp
-libtest_relo_check_dt_needed_order_2_src_files := dlopen_testlib_relo_check_dt_needed_order_2.cpp
-
-module := libtest_relo_check_dt_needed_order
-include $(LOCAL_PATH)/Android.build.testlib.mk
-module := libtest_relo_check_dt_needed_order_1
-include $(LOCAL_PATH)/Android.build.testlib.mk
-module := libtest_relo_check_dt_needed_order_2
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
 # Library with dependency used by dlfcn tests
 # -----------------------------------------------------------------------------
 libtest_with_dependency_src_files := \
@@ -339,161 +180,3 @@
 
 module := libtest_with_dependency
 include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library used by ifunc tests
-# -----------------------------------------------------------------------------
-libtest_ifunc_src_files := \
-    dlopen_testlib_ifunc.c
-
-# TODO(dimitry): clang does not support ifunc attribute
-libtest_ifunc_clang_host := false
-
-module := libtest_ifunc
-build_target := SHARED_LIBRARY
-
-build_type := host
-include $(TEST_PATH)/Android.build.mk
-
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),arm arm64 x86 x86_64))
-    build_type := target
-    libtest_ifunc_clang_target := false
-    include $(TEST_PATH)/Android.build.mk
-endif
-
-
-# -----------------------------------------------------------------------------
-# Library used by atexit tests
-# -----------------------------------------------------------------------------
-
-libtest_atexit_src_files := \
-    atexit_testlib.cpp
-
-module := libtest_atexit
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# This library is used by dl_load test to check symbol preempting
-# by main executable
-# -----------------------------------------------------------------------------
-libdl_preempt_test_1_src_files := dl_preempt_library_1.cpp
-
-module := libdl_preempt_test_1
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# This library is used by dl_load test to check symbol preempting
-# by libdl_preempt_test_1.so
-# -----------------------------------------------------------------------------
-libdl_preempt_test_2_src_files := dl_preempt_library_2.cpp
-
-module := libdl_preempt_test_2
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library with DF_1_GLOBAL
-# -----------------------------------------------------------------------------
-libdl_test_df_1_global_src_files := dl_df_1_global.cpp
-libdl_test_df_1_global_ldflags := -Wl,-z,global
-
-# TODO (dimitry): host ld.gold does not yet support -z global
-# remove this line once it is updated.
-libdl_test_df_1_global_ldflags_host := -fuse-ld=bfd
-
-module := libdl_test_df_1_global
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library using symbol from libdl_test_df_1_global
-# -----------------------------------------------------------------------------
-libtest_dlsym_df_1_global_src_files := dl_df_1_use_global.cpp
-module := libtest_dlsym_df_1_global
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library with weak function
-# -----------------------------------------------------------------------------
-libtest_dlsym_weak_func_src_files := \
-    dlsym_weak_function.cpp
-
-module := libtest_dlsym_weak_func
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library to check RTLD_LOCAL with dlsym in 'this'
-# -----------------------------------------------------------------------------
-libtest_dlsym_from_this_src_files := dlsym_from_this_symbol.cpp
-
-libtest_dlsym_from_this_shared_libraries_target := libdl
-libtest_dlsym_from_this_shared_libraries := libtest_dlsym_from_this_child
-
-module := libtest_dlsym_from_this
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-libtest_dlsym_from_this_child_src_files := dlsym_from_this_functions.cpp
-
-libtest_dlsym_from_this_child_shared_libraries := libtest_dlsym_from_this_grandchild
-
-module := libtest_dlsym_from_this_child
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-libtest_dlsym_from_this_grandchild_src_files := dlsym_from_this_symbol2.cpp
-
-module := libtest_dlsym_from_this_grandchild
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Empty library
-# -----------------------------------------------------------------------------
-libtest_empty_src_files := empty.cpp
-
-module := libtest_empty
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library with weak undefined function
-# -----------------------------------------------------------------------------
-libtest_dlopen_weak_undefined_func_src_files := \
-    dlopen_weak_undefined.cpp
-
-module := libtest_dlopen_weak_undefined_func
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library with constructor that calls dlopen() b/7941716
-# -----------------------------------------------------------------------------
-libtest_dlopen_from_ctor_src_files := \
-   dlopen_testlib_dlopen_from_ctor.cpp
-
-module := libtest_dlopen_from_ctor
-
-libtest_dlopen_from_ctor_shared_libraries_target := libdl
-
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Library that depends on the library with constructor that calls dlopen() b/7941716
-# -----------------------------------------------------------------------------
-
-libtest_dlopen_from_ctor_main_src_files := empty.cpp
-libtest_dlopen_from_ctor_main_shared_libraries := libtest_dlopen_from_ctor
-
-module := libtest_dlopen_from_ctor_main
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# Tool to use to align the shared libraries in a zip file.
-# -----------------------------------------------------------------------------
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := bionic_tests_zipalign.cpp
-LOCAL_MODULE := bionic_tests_zipalign
-LOCAL_CFLAGS := -Wall -Werror
-
-LOCAL_STATIC_LIBRARIES := libziparchive-host liblog libbase libz libutils
-
-LOCAL_MODULE_HOST_OS := darwin linux windows
-
-include $(BUILD_HOST_EXECUTABLE)
diff --git a/tests/run-on-host.sh b/tests/run-on-host.sh
new file mode 100755
index 0000000..c47bc4f
--- /dev/null
+++ b/tests/run-on-host.sh
@@ -0,0 +1,34 @@
+#!/bin/bash -e
+
+. $(dirname $0)/../build/run-on-host.sh
+
+if [ "$1" = glibc ]; then
+    m -j bionic-unit-tests-glibc
+    (
+        cd ${ANDROID_BUILD_TOP}
+        export ANDROID_DATA=${TARGET_OUT_DATA}
+        export ANDROID_ROOT=${TARGET_OUT}
+        ${HOST_OUT}/nativetest64/bionic-unit-tests-glibc/bionic-unit-tests-glibc $@
+    )
+    exit 0
+elif [ "$1" != 32 -a "$1" != 64 ]; then
+    echo "Usage: $0 [ 32 | 64 | glibc ] [gtest flags]"
+    exit 1
+fi
+
+if [ ${HOST_OS}-${HOST_ARCH} = linux-x86 -o ${HOST_OS}-${HOST_ARCH} = linux-x86_64 ]; then
+
+    prepare $1 bionic-unit-tests
+
+    if [ ${TARGET_ARCH} = x86 -o ${TARGET_ARCH} = x86_64 ]; then
+        (
+            cd ${ANDROID_BUILD_TOP}
+            export ANDROID_DATA=${TARGET_OUT_DATA}
+            export ANDROID_DNS_MODE=local
+            export ANDROID_ROOT=${TARGET_OUT}
+            ${NATIVETEST}/bionic-unit-tests/bionic-unit-tests $@
+        )
+    else
+        echo "$0 not supported on TARGET_ARCH=$TARGET_ARCH"
+    fi
+fi