Static link gtests with ART libraries

Add new build targets `libart-for-test` and `libart-compiler-for-test`,
because building tests with LTO takes too much RAM and there is no way to
override LTO enabled in libart(-compiler). See: b/277207452
Add new *-for-test_static_defaults which use libart(-compiler)-for-test.
After this change, gtests will have access to all libart(-compiler)
symbols, which will allow merging libart and libart-compiler together
and further reducing the amount of public symbols.

Bug: 261703065
Bug: 247108425
Test: atest art_standalone_*_tests
Test: m aapt mts && mts-tradefed run commandAndExit mts-art-shard-03
Test: m test-art-host-gtest
Test: m art-check-testing-apex-gen
Change-Id: Ide03609e78af5441b6822e5640925e812bec4e11
diff --git a/build/apex/art_apex_test.py b/build/apex/art_apex_test.py
index 891fd0f..0c813d1 100755
--- a/build/apex/art_apex_test.py
+++ b/build/apex/art_apex_test.py
@@ -697,11 +697,6 @@
     self._checker.check_art_test_executable('art_runtime_tests')
     self._checker.check_art_test_executable('art_sigchain_tests')
 
-    # Check ART test (internal) libraries.
-    self._checker.check_native_library('libartd-gtest')
-    self._checker.check_native_library('libartd-simulator-container')
-    self._checker.check_native_library('libartbased-testing')
-
     # Check ART test tools.
     self._checker.check_executable('signal_dumper')
 
diff --git a/compiler/Android.bp b/compiler/Android.bp
index 00e1bb9..3e279e4 100644
--- a/compiler/Android.bp
+++ b/compiler/Android.bp
@@ -28,6 +28,89 @@
     default_applicable_licenses: ["art_license"],
 }
 
+// Common dependencies for libart-compiler_deps and libartd-compiler_deps.
+cc_defaults {
+    name: "libart-compiler_common_deps",
+    shared_libs: [
+        "libbase",
+        "liblzma", // libelffile(d) dependency; must be repeated here since it's a static lib.
+        "libartpalette",
+    ],
+    header_libs: [
+        "libart_generated_headers",
+    ],
+}
+
+// Dependencies of libart-compiler, used to propagate libart-compiler deps when static linking.
+art_cc_defaults {
+    name: "libart-compiler_deps",
+    defaults: ["libart-compiler_common_deps"],
+    shared_libs: [
+        "libartbase",
+        "libprofile",
+        "libdexfile",
+    ],
+    static_libs: ["libelffile"],
+    codegen: {
+        arm: {
+            // VIXL assembly support for ARM targets.
+            static_libs: [
+                "libvixl",
+            ],
+        },
+        arm64: {
+            // VIXL assembly support for ARM64 targets.
+            static_libs: [
+                "libvixl",
+            ],
+        },
+    },
+    runtime_libs: [
+        // `art::HGraphVisualizerDisassembler::HGraphVisualizerDisassembler` may dynamically load
+        // `libart-disassembler.so`.
+        "libart-disassembler",
+    ],
+}
+
+// Dependencies of libartd-compiler, used to propagate libartd-compiler deps when static linking.
+art_cc_defaults {
+    name: "libartd-compiler_deps",
+    defaults: ["libart-compiler_common_deps"],
+    shared_libs: [
+        "libartbased",
+        "libprofiled",
+        "libdexfiled",
+    ],
+    static_libs: ["libelffiled"],
+    codegen: {
+        arm: {
+            // VIXL assembly support for ARM targets.
+            static_libs: [
+                "libvixld",
+            ],
+            // Export vixl headers as they are included in this library's headers used by tests.
+            export_static_lib_headers: [
+                "libvixld",
+            ],
+        },
+        arm64: {
+            // VIXL assembly support for ARM64 targets.
+            static_libs: [
+                "libvixld",
+            ],
+            // Export vixl headers as they are included in this library's headers used by tests.
+            export_static_lib_headers: [
+                "libvixld",
+            ],
+        },
+    },
+    runtime_libs: [
+        // `art::HGraphVisualizerDisassembler::HGraphVisualizerDisassembler` may dynamically load
+        // `libartd-disassembler.so`.
+        "libartd-disassembler",
+    ],
+}
+
 art_cc_defaults {
     name: "libart-compiler-defaults",
     defaults: ["art_defaults"],
@@ -211,35 +294,13 @@
         "libart-compiler-defaults",
         "dex2oat-pgo-defaults",
         "art_hugepage_defaults",
+        "libart-compiler_deps",
     ],
-    codegen: {
-        arm: {
-            // VIXL assembly support for ARM targets.
-            static_libs: [
-                "libvixl",
-            ],
-        },
-        arm64: {
-            // VIXL assembly support for ARM64 targets.
-            static_libs: [
-                "libvixl",
-            ],
-        },
-    },
     shared_libs: [
+        // libart is not included in libart-compiler_deps to allow libart-compiler(-for-test)
+        // select suitable libart library (either with or without LTO).
         "libart",
-        "libartbase",
-        "libartpalette",
-        "libprofile",
-        "libdexfile",
     ],
-    static_libs: ["libelffile"],
-    runtime_libs: [
-        // `art::HGraphVisualizerDisassembler::HGraphVisualizerDisassembler` may dynamically load
-        // `libart-disassembler.so`.
-        "libart-disassembler",
-    ],
-
     target: {
         android: {
             lto: {
@@ -254,6 +315,22 @@
     ],
 }
 
+// For static linking with gtests. Same as `libart-compiler`, but without LTO.
+// When gtests static link a library with LTO enabled, they are also built with LTO.
+// This makes the build process use a lot of memory. b/277207452
+art_cc_library_static {
+    name: "libart-compiler-for-test",
+    defaults: [
+        "libart-compiler-defaults",
+        "dex2oat-pgo-defaults",
+        "art_hugepage_defaults",
+        "libart-compiler_deps",
+    ],
+    header_libs: [
+        "libart_headers",
+    ],
+}
+
 cc_defaults {
     name: "libart-compiler_static_defaults",
     defaults: [
@@ -267,48 +344,32 @@
     whole_static_libs: ["libart-compiler"],
 }
 
+// libart-compiler_static_defaults for standalone gtests.
+// Uses libart-for-test_static_defaults instead of libart_static_defaults.
+// Uses libart-compiler-for-test instead of libart-compiler.
+cc_defaults {
+    name: "libart-compiler-for-test_static_defaults",
+    defaults: [
+        "libart-compiler_static_base_defaults",
+        "libart-disassembler_static_defaults",
+        "libart-for-test_static_defaults",
+        "libartbase_static_defaults",
+        "libdexfile_static_defaults",
+        "libprofile_static_defaults",
+    ],
+    whole_static_libs: ["libart-compiler-for-test"],
+}
+
 art_cc_library {
     name: "libartd-compiler",
     defaults: [
         "art_debug_defaults",
         "libart-compiler-defaults",
+        "libartd-compiler_deps",
     ],
-    codegen: {
-        arm: {
-            // VIXL assembly support for ARM targets.
-            static_libs: [
-                "libvixld",
-            ],
-            // Export vixl headers as they are included in this library's headers used by tests.
-            export_static_lib_headers: [
-                "libvixld",
-            ],
-        },
-        arm64: {
-            // VIXL assembly support for ARM64 targets.
-            static_libs: [
-                "libvixld",
-            ],
-            // Export vixl headers as they are included in this library's headers used by tests.
-            export_static_lib_headers: [
-                "libvixld",
-            ],
-        },
-    },
     shared_libs: [
-        "libartbased",
         "libartd",
-        "libartpalette",
-        "libprofiled",
-        "libdexfiled",
     ],
-    static_libs: ["libelffiled"],
-    runtime_libs: [
-        // `art::HGraphVisualizerDisassembler::HGraphVisualizerDisassembler` may dynamically load
-        // `libartd-disassembler.so`.
-        "libartd-disassembler",
-    ],
-
     apex_available: [
         "com.android.art.debug",
         // TODO(b/183882457): This lib doesn't go into com.android.art, but
@@ -331,42 +392,45 @@
     whole_static_libs: ["libartd-compiler"],
 }
 
+// libartd-compiler_static_defaults for standalone gtests.
+// Uses libartd-for-test_static_defaults instead of libart_static_defaults.
+cc_defaults {
+    name: "libartd-compiler-for-test_static_defaults",
+    defaults: [
+        "libart-compiler_static_base_defaults",
+        "libartbased_static_defaults",
+        "libartd-disassembler_static_defaults",
+        "libartd-for-test_static_defaults",
+        "libdexfiled_static_defaults",
+        "libprofiled_static_defaults",
+    ],
+    whole_static_libs: ["libartd-compiler"],
+}
+
 // Properties common to `libart-compiler-gtest` and `libartd-compiler-gtest`.
 art_cc_defaults {
     name: "libart-compiler-gtest-common",
     srcs: [
         "common_compiler_test.cc",
     ],
-    shared_libs: [
-        "libbase",
-    ],
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libart-compiler-gtest",
     defaults: [
         "libart-gtest-defaults",
         "libart-compiler-gtest-common",
-    ],
-    shared_libs: [
-        "libart-compiler",
-        "libart-disassembler",
-        "libartbase-art-gtest",
-        "libart-runtime-gtest",
+        "libart-compiler-for-test_static_defaults",
     ],
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libartd-compiler-gtest",
     defaults: [
-        "libartd-gtest-defaults",
+        "art_debug_defaults",
+        "libart-gtest-defaults",
         "libart-compiler-gtest-common",
-    ],
-    shared_libs: [
-        "libartd-compiler",
-        "libartd-disassembler",
-        "libartbased-art-gtest",
-        "libartd-runtime-gtest",
+        "libartd-compiler-for-test_static_defaults",
     ],
 }
 
@@ -475,11 +539,6 @@
         "libnativehelper_header_only",
     ],
 
-    shared_libs: [
-        "libnativeloader",
-        "libunwindstack",
-    ],
-
     target: {
         host: {
             shared_libs: [
@@ -497,15 +556,8 @@
         "art_gtest_defaults",
         "art_compiler_tests_defaults",
     ],
-    shared_libs: [
-        "libprofiled",
-        "libartd-simulator-container",
-        "liblzma",
-    ],
     static_libs: [
-        "libartd-compiler",
-        "libelffiled",
-        "libvixld",
+        "libartd-simulator-container",
     ],
 }
 
@@ -517,21 +569,8 @@
         "art_compiler_tests_defaults",
     ],
     data: [":generate-boot-image"],
-    shared_libs: [
-        "libprofile",
-        "liblzma",
-        "libartpalette",
-    ],
     static_libs: [
-        // For now, link `libart-simulator-container` statically for simplicity,
-        // to save the added complexity to package it in test suites (along with
-        // other test artifacts) and install it on device during tests.
-        // TODO(b/192070541): Consider linking `libart-simulator-container`
-        // dynamically.
         "libart-simulator-container",
-        "libart-compiler",
-        "libelffile",
-        "libvixl",
     ],
     test_config: "art_standalone_compiler_tests.xml",
 }
@@ -552,7 +591,7 @@
                 "utils/assembler_thumb_test.cc",
             ],
         },
-        riscv64 : {
+        riscv64: {
             srcs: [
                 "utils/riscv64/assembler_riscv64_test.cc",
             ],
@@ -568,12 +607,7 @@
             ],
         },
     },
-    shared_libs: [
-        "liblzma",
-    ],
     static_libs: [
-        "libartd-compiler",
-        "libelffiled",
         "libvixld",
     ],
 }
diff --git a/dex2oat/Android.bp b/dex2oat/Android.bp
index 7341774..761b073 100644
--- a/dex2oat/Android.bp
+++ b/dex2oat/Android.bp
@@ -440,37 +440,24 @@
     srcs: [
         "common_compiler_driver_test.cc",
     ],
-    shared_libs: [
-        "libart-compiler-gtest",
-        "libart-runtime-gtest",
-        "libart-compiler",
-        "libart-disassembler",
-        "libbase",
-        "liblz4", // libart-dex2oat dependency; must be repeated here since it's a static lib.
-        "liblog",
-    ],
     static_libs: [
         "libart-dex2oat",
+        "libart-gtest",
     ],
 }
 
 art_cc_library_static {
     name: "libartd-dex2oat-gtest",
-    defaults: ["libartd-gtest-defaults"],
+    defaults: [
+        "art_debug_defaults",
+        "libart-gtest-defaults",
+    ],
     srcs: [
         "common_compiler_driver_test.cc",
     ],
-    shared_libs: [
-        "libartd-compiler-gtest",
-        "libartd-runtime-gtest",
-        "libartd-compiler",
-        "libartd-disassembler",
-        "libbase",
-        "liblz4", // libartd-dex2oat dependency; must be repeated here since it's a static lib.
-        "liblog",
-    ],
     static_libs: [
         "libartd-dex2oat",
+        "libartd-gtest",
     ],
 }
 
@@ -556,17 +543,10 @@
         },
     },
 
-    static_libs: [
-        "libziparchive",
-    ],
     shared_libs: [
-        "libartpalette",
-        "libbase",
         "libcrypto",
         "liblz4", // libart(d)-dex2oat dependency; must be repeated here since it's a static lib.
         "liblog",
-        "libsigchain",
-        "libz", // libziparchive dependency; must be repeated here since it's a static lib.
     ],
 }
 
@@ -578,18 +558,11 @@
         "art_gtest_defaults",
         "art_dex2oat_tests_defaults",
     ],
-    shared_libs: [
-        "libartbased",
-        "libartd-dexlayout",
-        "liblzma",
-        "libprofiled",
-    ],
     static_libs: [
-        "libartd-compiler",
         "libartd-dex2oat",
         "libartd-dex2oat-gtest",
-        "libelffiled",
         "libvixld",
+        "libartd-dexlayout",
     ],
 }
 
@@ -601,18 +574,11 @@
         "art_dex2oat_tests_defaults",
     ],
     data: [":generate-boot-image"],
-    shared_libs: [
-        "libart-dexlayout",
-        "libartbase",
-        "liblzma",
-        "libprofile",
-    ],
     static_libs: [
-        "libart-compiler",
         "libart-dex2oat",
         "libart-dex2oat-gtest",
-        "libelffile",
         "libvixl",
+        "libart-dexlayout",
     ],
     test_config: "art_standalone_dex2oat_tests.xml",
 }
@@ -631,12 +597,6 @@
         ":art-gtest-jars-Nested",
         ":generate-boot-image",
     ],
-    shared_libs: [
-        "libz", // libziparchive dependency; must be repeated here since it's a static lib.
-    ],
-    static_libs: [
-        "libziparchive",
-    ],
     test_config: "art_standalone_dex2oat_cts_tests.xml",
     test_suites: ["cts"],
 }
diff --git a/dexoptanalyzer/Android.bp b/dexoptanalyzer/Android.bp
index 82f0c1a..43953da 100644
--- a/dexoptanalyzer/Android.bp
+++ b/dexoptanalyzer/Android.bp
@@ -83,12 +83,8 @@
 
 art_cc_defaults {
     name: "art_dexoptanalyzer_tests_defaults",
-    static_libs: [
-        "libziparchive",
-    ],
     shared_libs: [
         "libunwindstack",
-        "libz", // libziparchive dependency; must be repeated here since it's a static lib.
     ],
     data: [
         ":art-gtest-jars-LinkageTest",
diff --git a/libartbase/Android.bp b/libartbase/Android.bp
index 951599d..7cd1532 100644
--- a/libartbase/Android.bp
+++ b/libartbase/Android.bp
@@ -254,12 +254,10 @@
     srcs: [
         "base/common_art_test.cc",
     ],
-    shared_libs: [
-        "libbase",
-        "libunwindstack",
-    ],
     header_libs: [
         "libnativehelper_header_only",
+        // Required for "base/mutex.h" in common_art_test.cc
+        "libart_headers",
     ],
     static: {
         whole_static_libs: [
@@ -275,34 +273,29 @@
     },
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libartbase-art-gtest",
     defaults: [
         "libart-gtest-defaults",
         "libartbase-art-gtest-defaults",
-    ],
-    shared_libs: [
-        "libartbase",
-        "libartbase-testing",
-        "libdexfile",
+        "libartbase_static_defaults",
+        "libdexfile_static_defaults",
     ],
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libartbased-art-gtest",
     defaults: [
-        "libartd-gtest-defaults",
+        "art_debug_defaults",
+        "libart-gtest-defaults",
         "libartbase-art-gtest-defaults",
-    ],
-    shared_libs: [
-        "libartbased",
-        "libartbased-testing",
-        "libdexfiled",
+        "libartbased_static_defaults",
+        "libdexfiled_static_defaults",
     ],
 }
 
-art_cc_defaults {
-    name: "libartbase-testing-defaults",
+art_cc_library_static {
+    name: "libartbase-testing",
     defaults: [
         "art_defaults",
     ],
@@ -310,35 +303,9 @@
     srcs: [
         "base/testing.cc",
     ],
-    shared_libs: [
-        "libbase",
-    ],
-    apex_available: [
-        "com.android.art.debug",
-        // TODO(b/183882457): This lib doesn't go into com.android.art, but
-        // apex_available lists need to be the same for internal libs to avoid
-        // stubs, and this depends on libdexfiled and others.
-        "com.android.art",
-    ],
-}
-
-art_cc_library {
-    name: "libartbase-testing",
-    defaults: [
-        "libartbase-testing-defaults",
-    ],
-    shared_libs: [
-        "libartbase",
-    ],
-}
-
-art_cc_library {
-    name: "libartbased-testing",
-    defaults: [
-        "libartbase-testing-defaults",
-    ],
-    shared_libs: [
-        "libartbased",
+    header_libs: [
+        "libbase_headers",
+        "art_libartbase_headers",
     ],
 }
 
@@ -384,9 +351,6 @@
         "base/variant_map_test.cc",
         "base/zip_archive_test.cc",
     ],
-    shared_libs: [
-        "libbase",
-    ],
     static_libs: [
         "libgmock",
     ],
diff --git a/libartpalette/Android.bp b/libartpalette/Android.bp
index 9ac9091..301ef89 100644
--- a/libartpalette/Android.bp
+++ b/libartpalette/Android.bp
@@ -111,10 +111,6 @@
 art_cc_defaults {
     name: "art_libartpalette_tests_defaults",
     srcs: ["apex/palette_test.cc"],
-    shared_libs: [
-        "libartpalette",
-        "libnativehelper",
-    ],
     target: {
         android: {
             static_libs: ["libmodules-utils-build"],
diff --git a/libdexfile/Android.bp b/libdexfile/Android.bp
index 9c2acac..9d2a219 100644
--- a/libdexfile/Android.bp
+++ b/libdexfile/Android.bp
@@ -306,12 +306,8 @@
         ":art-gtest-jars-VerifierDeps",
     ],
     header_libs: ["jni_headers"],
-    static_libs: [
-        "libziparchive",
-    ],
     shared_libs: [
         "libunwindstack",
-        "libz", // libziparchive dependency; must be repeated here since it's a static lib.
     ],
 }
 
diff --git a/libprofile/Android.bp b/libprofile/Android.bp
index beae8a9..c7e3cb8 100644
--- a/libprofile/Android.bp
+++ b/libprofile/Android.bp
@@ -188,12 +188,6 @@
         "profile/profile_boot_info_test.cc",
         "profile/profile_compilation_info_test.cc",
     ],
-    static_libs: [
-        "libziparchive",
-    ],
-    shared_libs: [
-        "libz", // libziparchive dependency; must be repeated here since it's a static lib.
-    ],
 }
 
 // Version of ART gtest `art_libprofile_tests` bundled with the ART APEX on target.
@@ -204,10 +198,6 @@
         "art_gtest_defaults",
         "art_libprofile_tests_defaults",
     ],
-    shared_libs: [
-        "libartbased",
-        "libdexfiled",
-    ],
 }
 
 // Standalone version of ART gtest `art_libprofile_tests`, not bundled with the ART APEX on target.
@@ -217,9 +207,5 @@
         "art_standalone_gtest_defaults",
         "art_libprofile_tests_defaults",
     ],
-    shared_libs: [
-        "libartbase",
-        "libdexfile",
-    ],
     test_config: "art_standalone_libprofile_tests.xml",
 }
diff --git a/odrefresh/Android.bp b/odrefresh/Android.bp
index 809e18d..97fec58 100644
--- a/odrefresh/Android.bp
+++ b/odrefresh/Android.bp
@@ -207,9 +207,6 @@
         "art_odrefresh_tests_defaults",
     ],
     host_supported: false,
-    shared_libs: [
-        "libdexfiled",
-    ],
     // The test config template is needed even though it's not used by the test
     // runner. Otherwise, Soong will generate a test config, which is adding
     // `art-host-test` as a test tag, while this test does not support running
@@ -225,9 +222,6 @@
         "art_standalone_gtest_defaults",
         "art_odrefresh_tests_defaults",
     ],
-    shared_libs: [
-        "libdexfile",
-    ],
 }
 
 genrule {
diff --git a/profman/Android.bp b/profman/Android.bp
index ac80641..7eff771 100644
--- a/profman/Android.bp
+++ b/profman/Android.bp
@@ -199,9 +199,6 @@
         "art_gtest_defaults",
         "art_profman_tests_defaults",
     ],
-    shared_libs: [
-        "libprofiled",
-    ],
     target: {
         host: {
             required: ["profmand"],
@@ -217,9 +214,6 @@
         "art_profman_tests_defaults",
     ],
     data: [":generate-boot-image"],
-    shared_libs: [
-        "libprofile",
-    ],
     target: {
         host: {
             required: ["profman"],
diff --git a/runtime/Android.bp b/runtime/Android.bp
index 1d42ccd..657076b 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -87,8 +87,14 @@
     // ART's macros.h depends on libbase's macros.h.
     // Note: runtime_options.h depends on cmdline. But we don't really want to export this
     //       generically. dex2oat takes care of it itself.
-    header_libs: ["art_libartbase_headers"],
-    export_header_lib_headers: ["art_libartbase_headers"],
+    header_libs: [
+        "art_libartbase_headers",
+        "dlmalloc",
+    ],
+    export_header_lib_headers: [
+        "art_libartbase_headers",
+        "dlmalloc",
+    ],
 
     // We optimize Thread::Current() with a direct TLS access. This requires
     // access to a platform specific Bionic header.
@@ -109,6 +115,23 @@
     ],
 }
 
+// Generated headers target required by libart.
+cc_library_headers {
+    name: "libart_generated_headers",
+    defaults: ["art_defaults"],
+    host_supported: true,
+
+    // asm_support_gen.h (used by asm_support.h) is generated with cpp-define-generator
+    generated_headers: ["cpp-define-generator-asm-support"],
+    // export our headers so the libart(d)-gtest targets can use it as well.
+    export_generated_headers: ["cpp-define-generator-asm-support"],
+
+    apex_available: [
+        "com.android.art",
+        "com.android.art.debug",
+    ],
+}
+
 cc_defaults {
     name: "libart_defaults",
     defaults: ["art_defaults"],
@@ -479,26 +502,23 @@
             ],
         },
     },
-
+    static: {
+        cflags: ["-DART_STATIC_LIBART"],
+    },
     generated_sources: [
         "art_operator_srcs",
     ],
-    // asm_support_gen.h (used by asm_support.h) is generated with cpp-define-generator
-    generated_headers: ["cpp-define-generator-asm-support"],
-    // export our headers so the libart(d)-gtest targets can use it as well.
-    export_generated_headers: ["cpp-define-generator-asm-support"],
-
     header_libs: [
         "art_cmdlineparser_headers",
         "cpp-define-generator-definitions",
-        "dlmalloc",
         "jni_platform_headers",
         "libart_headers",
         "libnativehelper_header_only",
+        "libart_generated_headers",
     ],
     export_header_lib_headers: [
-        "dlmalloc",
         "libart_headers",
+        "libart_generated_headers",
     ],
     whole_static_libs: [
         "libcpu_features",
@@ -539,9 +559,7 @@
         "liblz4",
         "liblzma", // libelffile dependency; must be repeated here since it's a static lib.
         "libnativebridge",
-        "libnativeloader",
         "libodrstatslog",
-        "libsigchain_fake",
         "libunwindstack",
         "libz",
     ],
@@ -572,6 +590,8 @@
     whole_static_libs: [
         "libart",
         "libelffile",
+        "libsigchain_fake",
+        "libnativeloader",
     ],
 }
 
@@ -587,6 +607,43 @@
     whole_static_libs: [
         "libartd",
         "libelffiled",
+        "libsigchain_fake",
+        "libnativeloader",
+    ],
+}
+
+// libart_static_defaults for standalone gtests.
+// Doesn't link libsigchain_fake.
+// Uses libart-for-test instead of libart.
+cc_defaults {
+    name: "libart-for-test_static_defaults",
+    defaults: [
+        "libart_static_base_defaults",
+        "libartbase_static_defaults",
+        "libdexfile_static_defaults",
+        "libdexfile_support_static_defaults",
+        "libprofile_static_defaults",
+    ],
+    whole_static_libs: [
+        "libart-for-test",
+        "libelffile",
+    ],
+}
+
+// libartd_static_defaults for gtests.
+// Doesn't link libsigchain_fake.
+cc_defaults {
+    name: "libartd-for-test_static_defaults",
+    defaults: [
+        "libart_static_base_defaults",
+        "libartbased_static_defaults",
+        "libdexfiled_static_defaults",
+        "libdexfiled_support_static_defaults",
+        "libprofiled_static_defaults",
+    ],
+    whole_static_libs: [
+        "libartd",
+        "libelffiled",
     ],
 }
 
@@ -653,13 +710,6 @@
     export_shared_lib_headers: [
         "libdexfile",
     ],
-    target: {
-        android: {
-            lto: {
-                thin: true,
-            },
-        },
-    },
 }
 
 // Release version of the ART runtime library.
@@ -677,6 +727,13 @@
         "test_broken_com.android.art",
     ],
     afdo: true,
+    target: {
+        android: {
+            lto: {
+                thin: true,
+            },
+        },
+    },
 }
 
 // "Broken" version of the ART runtime library, used only for testing.
@@ -696,6 +753,19 @@
     ],
 }
 
+// For static linking with gtests. Same as `libart`, but without LTO.
+// When gtests static link a library with LTO enabled, they are also built with LTO.
+// This makes the build process use a lot of memory. b/277207452
+art_cc_library_static {
+    name: "libart-for-test",
+    defaults: ["libart_common_defaults"],
+    shared_libs: [
+        "libartbase",
+        "libdexfile",
+        "libprofile",
+    ],
+}
+
 // Debug version of the ART runtime library.
 art_cc_library {
     name: "libartd",
@@ -743,40 +813,30 @@
         "common_runtime_test.cc",
         "dexopt_test.cc",
     ],
-    shared_libs: [
-        "libbase",
-        "libz", // libziparchive dependency; must be repeated here since it's a static lib.
-    ],
     static_libs: [
         "libprocinfo",
-        "libziparchive",
     ],
     header_libs: [
         "libnativehelper_header_only",
     ],
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libart-runtime-gtest",
     defaults: [
         "libart-runtime-gtest-defaults",
         "libart-gtest-defaults",
-    ],
-    shared_libs: [
-        "libart",
-        "libartbase-art-gtest",
+        "libart-for-test_static_defaults",
     ],
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libartd-runtime-gtest",
     defaults: [
+        "art_debug_defaults",
         "libart-runtime-gtest-defaults",
-        "libartd-gtest-defaults",
-    ],
-    shared_libs: [
-        "libartd",
-        "libartbased-art-gtest",
+        "libart-gtest-defaults",
+        "libartd-for-test_static_defaults",
     ],
 }
 
@@ -932,11 +992,9 @@
     ],
     shared_libs: [
         "libunwindstack",
-        "libz", // libziparchive dependency; must be repeated here since it's a static lib.
     ],
     static_libs: [
         "libgmock",
-        "libziparchive",
     ],
     header_libs: [
         "art_cmdlineparser_headers", // For parsed_options_test.
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index 3d7ba53..ecaf679 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -242,7 +242,16 @@
   return true;
 }
 
+#ifdef ART_STATIC_LIBART
+extern "C" JitCompilerInterface* jit_load();
+#endif
+
 bool Jit::LoadCompilerLibrary(std::string* error_msg) {
+#ifdef ART_STATIC_LIBART
+  (void)error_msg;
+  jit_load_ = &jit_load;
+  return true;
+#else
   jit_library_handle_ = dlopen(
       kIsDebugBuild ? "libartd-compiler.so" : "libart-compiler.so", RTLD_NOW);
   if (jit_library_handle_ == nullptr) {
@@ -256,6 +265,7 @@
     return false;
   }
   return true;
+#endif
 }
 
 bool Jit::CompileMethodInternal(ArtMethod* method,
diff --git a/runtime/monitor-inl.h b/runtime/monitor-inl.h
index f7e31a0..3b1a998 100644
--- a/runtime/monitor-inl.h
+++ b/runtime/monitor-inl.h
@@ -30,7 +30,7 @@
 }
 
 // Check for request to set lock owner info.
-void Monitor::CheckLockOwnerRequest(Thread* self) {
+inline void Monitor::CheckLockOwnerRequest(Thread* self) {
   DCHECK(self != nullptr);
   Thread* request_thread = lock_owner_request_.load(std::memory_order_relaxed);
   if (request_thread == self) {
@@ -40,13 +40,13 @@
   }
 }
 
-uintptr_t Monitor::LockOwnerInfoChecksum(ArtMethod* m, uint32_t dex_pc, Thread* t) {
+inline uintptr_t Monitor::LockOwnerInfoChecksum(ArtMethod* m, uint32_t dex_pc, Thread* t) {
   uintptr_t dpc_and_thread = static_cast<uintptr_t>(dex_pc << 8) ^ reinterpret_cast<uintptr_t>(t);
   return reinterpret_cast<uintptr_t>(m) ^ dpc_and_thread
       ^ (dpc_and_thread << (/* ptr_size / 2 */ (sizeof m) << 2));
 }
 
-void Monitor::SetLockOwnerInfo(ArtMethod* method, uint32_t dex_pc, Thread* t) {
+inline void Monitor::SetLockOwnerInfo(ArtMethod* method, uint32_t dex_pc, Thread* t) {
   lock_owner_method_.store(method, std::memory_order_relaxed);
   lock_owner_dex_pc_.store(dex_pc, std::memory_order_relaxed);
   lock_owner_.store(t, std::memory_order_relaxed);
@@ -54,8 +54,9 @@
   lock_owner_sum_.store(sum, std::memory_order_relaxed);
 }
 
-void Monitor::GetLockOwnerInfo(/*out*/ArtMethod** method, /*out*/uint32_t* dex_pc,
-                               Thread* t) {
+inline void Monitor::GetLockOwnerInfo(/*out*/ ArtMethod** method,
+                                      /*out*/ uint32_t* dex_pc,
+                                      Thread* t) {
   ArtMethod* owners_method;
   uint32_t owners_dex_pc;
   Thread* owner;
@@ -79,7 +80,6 @@
   }
 }
 
-
 }  // namespace art
 
 #endif  // ART_RUNTIME_MONITOR_INL_H_
diff --git a/simulator/Android.bp b/simulator/Android.bp
index d29319a..043a1bc 100644
--- a/simulator/Android.bp
+++ b/simulator/Android.bp
@@ -83,34 +83,24 @@
     srcs: [
         "code_simulator_container.cc",
     ],
-    shared_libs: [
-        "libbase",
+    header_libs: [
+        "libart_simulator_headers",
+        "libbase_headers",
+        "libart_headers",
+        "art_libartbase_headers",
     ],
-
-    header_libs: ["libart_simulator_headers"],
     export_include_dirs: ["."], // TODO: Consider a proper separation.
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libart-simulator-container",
     defaults: ["libart_simulator_container_defaults"],
-    shared_libs: [
-        "libartbase",
-        "libart",
-    ],
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libartd-simulator-container",
     defaults: [
         "art_debug_defaults",
         "libart_simulator_container_defaults",
     ],
-    shared_libs: [
-        "libartbased",
-        "libartd",
-    ],
-    apex_available: [
-        "com.android.art.debug",
-    ],
 }
diff --git a/test/Android.bp b/test/Android.bp
index 836beb8..efff429 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -65,6 +65,10 @@
             ],
         },
     },
+    // Reduce test executable size by disabling automatic export of static lib symbols.
+    ldflags: [
+        "-Wl,--exclude-libs=ALL",
+    ],
 }
 
 art_module_cc_defaults {
@@ -204,12 +208,33 @@
     gtest: false,
 
     shared_libs: [
-        "libbase",
-        "liblog",
-        "libz",
-    ],
+        // `libsigchain` must be shared for art_standalone_libsigchain_tests to work.
+        "libsigchain",
+        // `libnativeloader` must be shared, otherwise host gtests can't load libraries from
+        // "art_common/out/host", which is present in `libnativeloader` RUNPATH.
+        // TODO(b/247108425): modify gtests RUNPATH so that `libnativeloader` can be static linked.
+        "libnativeloader",
 
+        // `libart(d)` (`art/runtime/jni/java_vm_ext.cc`) and `libnativehelper`
+        // (`libnativehelper/JniInvocation.c`) define symbols with the same name
+        // (e.g. `JNI_GetDefaultJavaVMInitArgs`).
+        // `JavaVmExtTest#*` tests require `libart(d)` implementation of these symbols.
+        // At the moment `libart(d)` is linked statically (through `libart(d)-gtest`)
+        // and these symbols are correctly resolved to `libart(d)`.
+        // If `libnativehelper` and `libart(d)` are both linked dynamically,
+        // `libart(d)` must be specified in shared_libs list before `libnativehelper`,
+        // so that its symbols have precedence over `libnativehelper`.
+        "libnativehelper",
+    ],
     target: {
+        android: {
+            shared_libs: [
+                // Dependencies of `libart(d)`, that are not included in *static_defaults.
+                "libdl_android",
+                "libstatssocket",
+                "heapprofd_client_api",
+            ],
+        },
         linux: {
             ldflags: [
                 // Allow jni_compiler_test to find Java_MyClassNatives_bar
@@ -227,15 +252,15 @@
             ],
         },
         host: {
-            shared_libs: [
-                "libziparchive",
-            ],
             cflags: [
                 "-fsanitize-address-use-after-return=never",
                 "-Wno-unused-command-line-argument",
             ],
         },
     },
+    static_libs: [
+        "libartbase-testing",
+    ],
 }
 
 art_cc_defaults {
@@ -245,33 +270,12 @@
         "art_gtest_common_defaults",
         "art_debug_defaults",
     ],
-
     test_suites: ["art-host-tests"],
     test_options: {
         test_suite_tag: ["art-host-gtest"],
     },
-
-    shared_libs: [
-        "libartd",
-        "libartd-disassembler",
+    static_libs: [
         "libartd-gtest",
-        "libdexfiled",
-        "libprofiled",
-        "libartbased",
-        "libartbased-testing",
-
-        // Library `libnativehelper` needs to appear after `libartd` here,
-        // otherwise the following tests from module `libartd-runtime-gtest`
-        // will fail because `art/runtime/jni/java_vm_ext.cc` and
-        // `libnativehelper/JniInvocation.c` define symbols with the same name
-        // (e.g. `JNI_GetDefaultJavaVMInitArgs`) and the link order does matter
-        // for these tests:
-        // - JavaVmExtTest#JNI_GetDefaultJavaVMInitArgs
-        // - JavaVmExtTest#JNI_GetCreatedJavaVMs
-        // - JavaVmExtTest#AttachCurrentThread
-        // - JavaVmExtTest#AttachCurrentThreadAsDaemon
-        // - JavaVmExtTest#AttachCurrentThread_SmallStack
-        "libnativehelper",
     ],
 }
 
@@ -285,12 +289,10 @@
         "art_standalone_test_defaults",
         "art_gtest_common_defaults",
     ],
-
     test_suites: [
         "general-tests",
         "mts-art",
     ],
-
     // Support multilib variants (using different suffix per sub-architecture), which is needed on
     // build targets with secondary architectures, as the MTS test suite packaging logic flattens
     // all test artifacts into a single `testcases` directory.
@@ -303,49 +305,17 @@
             suffix: "64",
         },
     },
-
-    // We use the "non-d" variants of libraries, as the Release ART APEX does
-    // not contain the "d" (debug) variants.
-    shared_libs: [
-        "libart",
-        "libart-compiler",
-        "libart-disassembler",
-        "libdexfile",
-        "libprofile",
-        "libartbase",
-
-        // Library `libnativehelper` needs to appear after `libart` here,
-        // otherwise the following tests from module `libart-runtime-gtest`
-        // will fail because `art/runtime/jni/java_vm_ext.cc` and
-        // `libnativehelper/JniInvocation.c` define symbols with the same name
-        // (e.g. `JNI_GetDefaultJavaVMInitArgs`) and the link order does matter
-        // for these tests:
-        // - JavaVmExtTest#JNI_GetDefaultJavaVMInitArgs
-        // - JavaVmExtTest#JNI_GetCreatedJavaVMs
-        // - JavaVmExtTest#AttachCurrentThread
-        // - JavaVmExtTest#AttachCurrentThreadAsDaemon
-        // - JavaVmExtTest#AttachCurrentThread_SmallStack
-        "libnativehelper",
-    ],
     static_libs: [
-        // For now, link `libart-gtest` and `libartbase-testing` statically for
-        // simplicity, to save the added complexity to package it in test suites
-        // (along with other test artifacts) and install it on device during
-        // tests.
-        // TODO(b/192070541): Consider linking `libart-gtest` dynamically.
         "libart-gtest",
-        "libartbase-testing",
     ],
-
     test_for: [
         "com.android.art",
         "com.android.art.debug",
     ],
 }
 
-// Properties common to `libart-gtest-defaults` and `libartd-gtest-defaults`.
 art_cc_defaults {
-    name: "libart-gtest-common-defaults",
+    name: "libart-gtest-defaults",
     defaults: [
         "art_defaults",
     ],
@@ -381,43 +351,6 @@
             enabled: false,
         },
     },
-    apex_available: [
-        "com.android.art.debug",
-        // TODO(b/183882457): This lib doesn't go into com.android.art, but
-        // apex_available lists need to be the same for internal libs to avoid
-        // stubs, and this depends on libdexfiled and others.
-        "com.android.art",
-        "test_broken_com.android.art",
-    ],
-}
-
-art_cc_defaults {
-    name: "libart-gtest-defaults",
-    defaults: [
-        "libart-gtest-common-defaults",
-    ],
-    shared_libs: [
-        "libart",
-        "libart-compiler",
-        "libdexfile",
-        "libprofile",
-        "libartbase",
-    ],
-}
-
-art_cc_defaults {
-    name: "libartd-gtest-defaults",
-    defaults: [
-        "art_debug_defaults",
-        "libart-gtest-common-defaults",
-    ],
-    shared_libs: [
-        "libartd",
-        "libartd-compiler",
-        "libdexfiled",
-        "libprofiled",
-        "libartbased",
-    ],
 }
 
 // Properties common to `libart-gtest` and `libartd-gtest`.
@@ -434,7 +367,6 @@
         "libgtest_isolated",
     ],
     shared_libs: [
-        "libbase",
         "liblog",
     ],
     target: {
@@ -448,17 +380,9 @@
             enabled: false,
         },
     },
-    apex_available: [
-        "com.android.art.debug",
-        // TODO(b/183882457): This lib doesn't go into com.android.art, but
-        // apex_available lists need to be the same for internal libs to avoid
-        // stubs, and this depends on libdexfiled and others.
-        "com.android.art",
-        "test_broken_com.android.art",
-    ],
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libart-gtest",
     defaults: [
         "libart-gtest-common",
@@ -468,17 +392,9 @@
         "libart-runtime-gtest",
         "libartbase-art-gtest",
     ],
-    shared_libs: [
-        "libart",
-        "libart-compiler",
-        "libdexfile",
-        "libprofile",
-        "libartbase",
-        "libartbase-testing",
-    ],
 }
 
-art_cc_library {
+art_cc_library_static {
     name: "libartd-gtest",
     defaults: [
         "art_debug_defaults",
@@ -489,14 +405,6 @@
         "libartd-runtime-gtest",
         "libartbased-art-gtest",
     ],
-    shared_libs: [
-        "libartd",
-        "libartd-compiler",
-        "libdexfiled",
-        "libprofiled",
-        "libartbased",
-        "libartbased-testing",
-    ],
 }
 
 // ART run-tests.
@@ -752,7 +660,7 @@
         "2005-pause-all-redefine-multithreaded/pause-all.cc",
         "2009-structural-local-ref/local-ref.cc",
         "2035-structural-native-method/structural-native.cc",
-	"2243-single-step-default/single_step_helper.cc"
+        "2243-single-step-default/single_step_helper.cc",
     ],
     // Use NDK-compatible headers for ctstiagent.
     header_libs: [
@@ -991,7 +899,7 @@
         "2040-huge-native-alloc/huge_native_buf.cc",
         "2048-bad-native-registry/native_finalizer.cc",
         "2235-JdkUnsafeTest/unsafe_test.cc",
-	"2262-miranda-methods/jni_invoke.cc",
+        "2262-miranda-methods/jni_invoke.cc",
         "common/runtime_state.cc",
         "common/stack_inspect.cc",
     ],
diff --git a/tools/art_verifier/Android.bp b/tools/art_verifier/Android.bp
index 5d19215..19498e0 100644
--- a/tools/art_verifier/Android.bp
+++ b/tools/art_verifier/Android.bp
@@ -59,6 +59,8 @@
     defaults: [
         "art_verifier-defaults",
         "libart_static_defaults",
+        // TODO(b/186902856): remove once libart-compiler is merged into libart
+        "libart-compiler_static_defaults",
     ],
 }
 
@@ -68,5 +70,7 @@
         "art_debug_defaults",
         "art_verifier-defaults",
         "libartd_static_defaults",
+        // TODO(b/186902856): remove once libart-compiler is merged into libart
+        "libartd-compiler_static_defaults",
     ],
 }
diff --git a/tools/fuzzer/Android.bp b/tools/fuzzer/Android.bp
index 0d99943..c3f35d2 100644
--- a/tools/fuzzer/Android.bp
+++ b/tools/fuzzer/Android.bp
@@ -30,6 +30,8 @@
     defaults: [
         // Run in release mode since debug is too slow.
         "libart_static_defaults",
+        // TODO(b/186902856): remove once libart-compiler is merged into libart
+        "libart-compiler_static_defaults",
         // To allow the ART module to build correctly.
         "art_module_source_build_defaults",
     ],
@@ -50,7 +52,10 @@
     dictionary: "dex.dict",
     fuzz_config: {
         triage_assignee: "art-perf-team@google.com",
-        cc: ["solanes@google.com", "art-bugs@google.com"],
+        cc: [
+            "solanes@google.com",
+            "art-bugs@google.com",
+        ],
         componentid: 86431,
         acknowledgement: [
             "Santiago Aboy Solanes of Google",