Add defaults to link libz and liblog correctly in static libraries.

This should fix ODR issues on host while keeping NDK dependencies
accurate on target.

The two defaults have a "_static_defaults" suffix to reflect their
usage pattern: To be inherited into other *_static_defaults to collect
dependencies that eventually gets used in a "top level" shared library
or binary (except in special cases like libdex2oat(d)_static and
libprofman(d)_static where we cannot do that).

Test: art/test/testrunner/run_build_test_target.py -j64 art-gtest-asan
Test: art/test/testrunner/run_build_test_target.py -j64 art-asan
Test: mmm art/ system/extras/simpleperf/ cts/tests/tests/simpleperf/
Bug: 347036796
Flag: TEST_ONLY
Change-Id: Id0d14dc500b88ac8a50f840e9675d160e73d7465
diff --git a/build/Android.bp b/build/Android.bp
index 17f6891..834a7d5 100644
--- a/build/Android.bp
+++ b/build/Android.bp
@@ -303,6 +303,40 @@
     },
 }
 
+// Add a dependency on liblog suitable for use in *_static_defaults.
+cc_defaults {
+    name: "art_liblog_static_defaults",
+    target: {
+        android: {
+            shared_libs: ["liblog"],
+        },
+        not_windows: {
+            whole_static_libs: ["liblog"],
+        },
+        windows: {
+            whole_static_libs: ["liblog"],
+        },
+    },
+}
+
+// Add a dependency on libz suitable for use in *_static_defaults.
+cc_defaults {
+    name: "art_libz_static_defaults",
+    target: {
+        android: {
+            shared_libs: ["libz"],
+        },
+        // Link libz dynamically on host to avoid odr-violation errors in ASAN
+        // builds, because the LLVM runtime may load libz-host.so dynamically.
+        not_windows: {
+            shared_libs: ["libz"],
+        },
+        windows: {
+            whole_static_libs: ["libz"],
+        },
+    },
+}
+
 // A version of conscrypt only for enabling the "-hostdex" version to test ART on host.
 java_library {
     // We need our own name to not clash with the conscrypt library.
diff --git a/dex2oat/Android.bp b/dex2oat/Android.bp
index bee34cc..8f77b7c 100644
--- a/dex2oat/Android.bp
+++ b/dex2oat/Android.bp
@@ -102,11 +102,13 @@
 
 cc_defaults {
     name: "libart-dex2oat_static_base_defaults",
+    defaults: [
+        "art_liblog_static_defaults",
+        "art_libz_static_defaults",
+    ],
     whole_static_libs: [
         "libbase",
-        "liblog",
         "liblz4",
-        "libz",
     ],
 }
 
@@ -302,6 +304,9 @@
             // it'd get overridden by the load hook even when it uses
             // PrependProperties.
             compile_multilib: "64",
+            // libz is a libdex2oat_static transitive dependency from art_libz_static_defaults,
+            // duplicated here because we cannot import *_static_defaults only for host.
+            shared_libs: ["libz"],
         },
     },
 
@@ -346,12 +351,13 @@
             ],
         },
         host: {
-            // Comments for host in dex2oat apply here too.
+            // Comments for host in the dex2oat binary apply here too.
             static_libs: [
                 "libdex2oatd_static",
             ],
             stl: "c++_static",
             compile_multilib: "64",
+            shared_libs: ["libz"],
         },
     },
 
@@ -368,6 +374,11 @@
         "dex2oat-defaults",
     ],
     target: {
+        host: {
+            // libz is normally linked dynamically on host in
+            // art_libz_static_defaults, so need to bring it in statically here.
+            static_libs: ["libz"],
+        },
         darwin: {
             enabled: false,
         },
diff --git a/libartbase/Android.bp b/libartbase/Android.bp
index 0512dca6..e979067 100644
--- a/libartbase/Android.bp
+++ b/libartbase/Android.bp
@@ -167,11 +167,13 @@
 
 cc_defaults {
     name: "libartbase_static_base_defaults",
+    defaults: [
+        "art_liblog_static_defaults",
+        "art_libz_static_defaults",
+    ],
     whole_static_libs: [
         "libbase",
         "libartpalette",
-        "liblog",
-        "libz",
         "libziparchive",
     ],
     target: {
diff --git a/libdexfile/Android.bp b/libdexfile/Android.bp
index ab05378..ec25095 100644
--- a/libdexfile/Android.bp
+++ b/libdexfile/Android.bp
@@ -124,10 +124,12 @@
 
 cc_defaults {
     name: "libdexfile_static_base_defaults",
+    defaults: [
+        "art_liblog_static_defaults",
+        "art_libz_static_defaults",
+    ],
     whole_static_libs: [
         "libbase",
-        "liblog",
-        "libz",
         "libziparchive",
     ],
 }
@@ -554,8 +556,8 @@
         "//system/extras/simpleperf",
     ],
     // Using libdexfile_support_static_defaults will link in external libs like
-    // libbase and libz statically as well, which are likely to cause duplicate
-    // copies in the depending module.
+    // libbase statically as well, which are likely to cause duplicate copies in
+    // the depending module.
     // TODO(b/169885605): Avoid exposing symbols from those libs.
     defaults: [
         "art_defaults",
@@ -568,7 +570,21 @@
     defaults_visibility: [
         "//system/extras/simpleperf",
     ],
-    // Transitive deps to be added in a later CL.
+    // Take the relevant bits from art_liblog_static_defaults and
+    // art_libz_static_defaults.
+    target: {
+        android: {
+            shared_libs: [
+                "liblog",
+                "libz",
+            ],
+        },
+        not_windows: {
+            shared_libs: [
+                "libz",
+            ],
+        },
+    },
 }
 
 art_cc_test {
diff --git a/libprofile/Android.bp b/libprofile/Android.bp
index 91c5cbc..9120e57 100644
--- a/libprofile/Android.bp
+++ b/libprofile/Android.bp
@@ -76,9 +76,11 @@
 
 cc_defaults {
     name: "libprofile_static_base_defaults",
+    defaults: [
+        "art_libz_static_defaults",
+    ],
     whole_static_libs: [
         "libbase",
-        "libz",
         "libziparchive",
     ],
 }
diff --git a/oatdump/Android.bp b/oatdump/Android.bp
index 066ad1b..01ec25d 100644
--- a/oatdump/Android.bp
+++ b/oatdump/Android.bp
@@ -75,6 +75,9 @@
             // Make the host binary static, except for system libraries.
             static_libs: ["liboatdump_static"],
             stl: "c++_static",
+            // libz is a libdex2oat_static transitive dependency from art_libz_static_defaults,
+            // duplicated here because we cannot import *_static_defaults only for host.
+            shared_libs: ["libz"],
         },
     },
 
@@ -126,9 +129,10 @@
             ],
         },
         host: {
-            // Make the host binary static, except for system libraries.
+            // Comments for host in the oatdump binary apply here too.
             static_libs: ["liboatdumpd_static"],
             stl: "c++_static",
+            shared_libs: ["libz"],
         },
     },
 
@@ -145,6 +149,11 @@
         "oatdump-defaults",
     ],
     target: {
+        host: {
+            // libz is normally linked dynamically on host in
+            // art_libz_static_defaults, so need to bring it in statically here.
+            static_libs: ["libz"],
+        },
         darwin: {
             enabled: false,
         },
diff --git a/profman/Android.bp b/profman/Android.bp
index b3dcb6d..77184b9 100644
--- a/profman/Android.bp
+++ b/profman/Android.bp
@@ -106,7 +106,9 @@
             // avoids having to bundle host dynamic libs in prebuilts.
             static_libs: ["libprofman_static"],
             stl: "c++_static",
-
+            // libz is a libprofman_static transitive dependency from art_libz_static_defaults,
+            // duplicated here because we cannot import *_static_defaults only for host.
+            shared_libs: ["libz"],
         },
     },
     apex_available: [
@@ -131,11 +133,10 @@
             ],
         },
         host: {
-            // Make the host binary static, except for system libraries. This
-            // avoids having to bundle host dynamic libs in prebuilts.
+            // Comments for host in the profman binary apply here too.
             static_libs: ["libprofmand_static"],
             stl: "c++_static",
-
+            shared_libs: ["libz"],
         },
     },
     apex_available: [
diff --git a/runtime/Android.bp b/runtime/Android.bp
index 0974ce8..5a9ee52 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -656,25 +656,25 @@
 // where all non-NDK dependencies are static as well.
 cc_defaults {
     name: "art_libunwindstack_static_defaults",
+    defaults: [
+        "art_liblog_static_defaults",
+    ],
     whole_static_libs: [
         "libunwindstack",
         "libbase",
         "liblzma",
     ],
-    shared_libs: [
-        "liblog",
-    ],
 }
 
 cc_defaults {
     name: "libart_static_base_defaults",
     defaults: [
+        "art_liblog_static_defaults",
         "art_libunwindstack_static_defaults",
     ],
     whole_static_libs: [
         "libartpalette",
         "libbase",
-        "liblog",
         "liblz4",
         "liblzma", // libelffile dependency; must be repeated here since it's a static lib.
         "libnativebridge",
diff --git a/simulator/Android.bp b/simulator/Android.bp
index d99b78a..96f4067 100644
--- a/simulator/Android.bp
+++ b/simulator/Android.bp
@@ -51,11 +51,16 @@
 
 cc_defaults {
     name: "libart_simulator_static_base_defaults",
+    defaults: [
+        // Ideally this defaults should only be added for host, but Soong
+        // doesn't support that. This is good enough - in the worst case it adds
+        // some unnecessary dependencies.
+        "art_liblog_static_defaults",
+    ],
     target: {
         host: {
             whole_static_libs: [
                 "libbase",
-                "liblog",
             ],
         },
     },
@@ -64,9 +69,7 @@
 cc_defaults {
     name: "libart-simulator-for-test_static_defaults",
     defaults: [
-        // Ideally these defaults should only be added for host, but Soong
-        // doesn't support that. This is good enough - in the worst case it adds
-        // some unnecessary dependencies.
+        // See comment in libart_simulator_static_base_defaults.
         "libart-for-test_static_defaults",
         "libart_simulator_static_base_defaults",
         "libartbase_static_defaults",
@@ -81,7 +84,7 @@
 cc_defaults {
     name: "libartd-simulator-for-test_static_defaults",
     defaults: [
-        // See comment in libart-simulator-for-test_static_defaults.
+        // See comment in libart_simulator_static_base_defaults.
         "libartd-for-test_static_defaults",
         "libart_simulator_static_base_defaults",
         "libartbased_static_defaults",