Build jemalloc for host, too.

We build both static and dynamic libjemalloc and libjemalloc_jet for
the host. For the target, only the static libraries are built, just
as before.

Disable sanitizer for the unit/integration tests on the host.

Bug: 113365582

Test: 'mmma external/jemalloc', verify that it builds the same target
libraries, verify it builds host libraries and they can be used to
build ckati.

Change-Id: I84212d0c51995aab25f83deb087d1670ddfb00a8
diff --git a/Android.bp b/Android.bp
index e7d9ea0..bc22a71 100644
--- a/Android.bp
+++ b/Android.bp
@@ -44,9 +44,10 @@
 //     usually decreases the amount of PSS used, but can increase
 //     fragmentation.
 
-// Default to a single arena for svelte configurations to minimize
-// PSS consumed by jemalloc.
-common_cflags += [
+android_common_cflags = [
+    // Default to a single arena for svelte configurations to minimize
+    // PSS. This will be overridden by android_product_variables for
+    // non-svelte configs.
     "-DANDROID_MAX_ARENAS=1",
     "-DANDROID_LG_TCACHE_MAXCLASS_DEFAULT=16",
 ]
@@ -56,7 +57,7 @@
     "include",
 ]
 
-common_product_variables = {
+android_product_variables = {
     // Only enable the tcache on non-svelte configurations, to save PSS.
     malloc_not_svelte: {
         cflags: [
@@ -72,9 +73,20 @@
 cc_defaults {
     name: "jemalloc_defaults",
     defaults: ["linux_bionic_supported"],
+    host_supported: true,
     cflags: common_cflags,
 
-    product_variables: common_product_variables,
+    target: {
+        android: {
+            cflags: android_common_cflags + [
+                "-include android/include/log.h",
+            ],
+            product_variables: android_product_variables,
+        },
+        linux_glibc: {
+            enabled: true,
+        },
+    },
 
     multilib: {
         lib32: {
@@ -132,13 +144,19 @@
 //-----------------------------------------------------------------------
 // jemalloc static library
 //-----------------------------------------------------------------------
-cc_library_static {
+cc_library {
     name: "libjemalloc",
     recovery_available: true,
 
     defaults: ["jemalloc_defaults"],
 
-    cflags: ["-include bionic/libc/async_safe/include/async_safe/log.h"],
+    target: {
+        android: {
+            shared: {
+                enabled: false,
+            },
+        },
+    },
 
     srcs: lib_src_files,
 }
@@ -153,7 +171,6 @@
 
     cflags: [
         "-DJEMALLOC_JET",
-        "-include android/include/log.h",
     ],
 
     srcs: lib_src_files,
@@ -180,10 +197,8 @@
     name: "libjemalloc_unittest",
 
     defaults: ["jemalloc_defaults"],
-
     cflags: [
         "-DJEMALLOC_UNIT_TEST",
-        "-include android/include/log.h",
     ],
 
     local_include_dirs: [
@@ -194,7 +209,6 @@
     srcs: jemalloc_testlib_srcs,
 
     whole_static_libs: ["libjemalloc_jet"],
-
 }
 
 //-----------------------------------------------------------------------
@@ -247,13 +261,12 @@
 cc_test {
     name: "jemalloc_unittests",
 
-    gtest: false,
+    defaults: ["jemalloc_defaults"],
 
-    product_variables: common_product_variables,
+    gtest: false,
 
     cflags: common_cflags + [
         "-DJEMALLOC_UNIT_TEST",
-        "-include android/include/log.h",
     ],
 
     local_include_dirs: common_c_local_includes + [
@@ -268,6 +281,15 @@
     shared_libs: ["liblog"],
 
     test_per_src: true,
+
+    target: {
+        linux_glibc: {
+            // The sanitizer does not work for these tests on the host.
+            sanitize: {
+                never: true,
+            },
+        },
+    },
 }
 
 //-----------------------------------------------------------------------
@@ -280,7 +302,6 @@
 
     cflags: [
         "-DJEMALLOC_INTEGRATION_TEST",
-        "-include android/include/log.h",
     ],
 
     local_include_dirs: [
@@ -299,7 +320,6 @@
     "test/integration/aligned_alloc.c",
     "test/integration/allocated.c",
     "test/integration/chunk.c",
-    "test/integration/iterate.c",
     "test/integration/MALLOCX_ARENA.c",
     "test/integration/mallocx.c",
     "test/integration/overflow.c",
@@ -311,17 +331,19 @@
     "test/integration/xallocx.c",
 ]
 
-cc_test {
+android_integration_tests = [
+    "test/integration/iterate.c",
+]
 
+cc_test {
     name: "jemalloc_integrationtests",
 
+    defaults: ["jemalloc_defaults"],
+
     gtest: false,
 
-    product_variables: common_product_variables,
-
     cflags: common_cflags + [
         "-DJEMALLOC_INTEGRATION_TEST",
-        "-include android/include/log.h",
     ],
 
     local_include_dirs: common_c_local_includes + [
@@ -330,6 +352,17 @@
     ],
 
     srcs: integration_tests,
+    target: {
+        android: {
+            srcs: android_integration_tests,
+        },
+        linux_glibc: {
+            // The sanitizer does not work for these tests on the host.
+            sanitize: {
+                never: true,
+            },
+        },
+    },
 
     static_libs: ["libjemalloc_integrationtest"],