Merge libdexfile_external into libdexfile (reland 2).

To reduce the number of DSO's. libdexfile_external only adds a few
small functions on top of libdexfile, and it's still only those
functions that are available in the APEX stubs.

Also rename libdexfile_external_static to libdexfile_static, for
consistency.

Since libdexfile now has stubs, we need to add test_for properties to
avoid linking against the stubs in tests.

This relands https://r.android.com/1666119 that got submitted out of
order from https://r.android.com/1664026 - prerequisite changes now
submitted with https://r.android.com/1671709.

Test: Flash and boot with userdebug and eng to try both release and
  debug modules.
Test: art/tools/buildbot-build.sh {--target,--host}
Test: art/build/apex/runtests.sh
Test: mmm art
Bug: 143978909
Change-Id: I6de99052d6d8a9d01d748baabbbcecfe1f4509dc
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 2e6bf1e..3e1f5be 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -111,6 +111,10 @@
 # Remove symbols/apex/com.android.art symlink (b/171406631)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/symbols/apex/com.android.art)
 
+# art/tools/build_linux_bionic_tests.sh uses find here and can encounter
+# libdexfile_external.so in incremental builds.
+$(call add-clean-step, rm -rf $(HOST_OUT))
+
 # ************************************************
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
 # ************************************************
diff --git a/build/apex/Android.bp b/build/apex/Android.bp
index ba1d430..1f11f2e 100644
--- a/build/apex/Android.bp
+++ b/build/apex/Android.bp
@@ -50,7 +50,7 @@
 // the ART APEX.
 art_runtime_base_native_shared_libs = [
     // External API (having APEX stubs).
-    "libdexfile_external",
+    "libdexfile",
     "libnativebridge",
     "libnativehelper",
     "libnativeloader",
@@ -104,7 +104,7 @@
     "libadbconnectiond",
     "libartd",
     "libartd-compiler",
-    "libdexfiled_external",
+    "libdexfiled",
     "libopenjdkjvmd",
     "libopenjdkjvmtid",
 ]
diff --git a/build/apex/art_apex_test.py b/build/apex/art_apex_test.py
index 110553b..581e20d 100755
--- a/build/apex/art_apex_test.py
+++ b/build/apex/art_apex_test.py
@@ -527,7 +527,7 @@
     self._checker.check_symlinked_multilib_executable('dalvikvm')
 
     # Check exported libraries for ART.
-    self._checker.check_native_library('libdexfile_external')
+    self._checker.check_native_library('libdexfile')
     self._checker.check_native_library('libnativebridge')
     self._checker.check_native_library('libnativehelper')
     self._checker.check_native_library('libnativeloader')
@@ -542,7 +542,6 @@
     self._checker.check_native_library('libartpalette')
     self._checker.check_native_library('libartservice')
     self._checker.check_native_library('libarttools')
-    self._checker.check_native_library('libdexfile')
     self._checker.check_native_library('libdexfile_support')
     self._checker.check_native_library('libdt_fd_forward')
     self._checker.check_native_library('libopenjdkjvm')
@@ -677,6 +676,9 @@
     self._checker.check_symlinked_multilib_executable('imgdiagd')
     self._checker.check_executable('profmand')
 
+    # Check exported libraries for ART.
+    self._checker.check_native_library('libdexfiled')
+
     # Check internal libraries for ART.
     self._checker.check_native_library('libadbconnectiond')
     self._checker.check_native_library('libartbased')
@@ -684,7 +686,6 @@
     self._checker.check_native_library('libartd-compiler')
     self._checker.check_native_library('libartd-dexlayout')
     self._checker.check_native_library('libartd-disassembler')
-    self._checker.check_native_library('libdexfiled')
     self._checker.check_native_library('libopenjdkjvmd')
     self._checker.check_native_library('libopenjdkjvmtid')
     self._checker.check_native_library('libprofiled')
@@ -707,7 +708,6 @@
     self._checker.check_executable('oatdumpd')
 
     # Check ART internal libraries.
-    self._checker.check_native_library('libdexfiled_external')
     self._checker.check_native_library('libperfetto_hprofd')
 
     # Check internal native library dependencies.
diff --git a/build/sdk/Android.bp b/build/sdk/Android.bp
index 386663f..2dadee0 100644
--- a/build/sdk/Android.bp
+++ b/build/sdk/Android.bp
@@ -65,7 +65,7 @@
 
             native_shared_libs: [
                 "libandroidio",
-                "libdexfile_external",
+                "libdexfile",
                 "libnativebridge",
                 "libnativehelper",
                 "libnativeloader",
@@ -77,7 +77,7 @@
             ],
 
             native_static_libs: [
-                "libdexfile_external_static",
+                "libdexfile_static",
                 "libnativehelper_lazy",
             ],
         },
diff --git a/compiler/Android.bp b/compiler/Android.bp
index 935e255..b90b7e8 100644
--- a/compiler/Android.bp
+++ b/compiler/Android.bp
@@ -341,6 +341,10 @@
 
     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.
+        "com.android.art",
     ],
 }
 
diff --git a/dexlayout/Android.bp b/dexlayout/Android.bp
index f628f75..922e134 100644
--- a/dexlayout/Android.bp
+++ b/dexlayout/Android.bp
@@ -151,6 +151,10 @@
     },
     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.
+        "com.android.art",
     ],
 }
 
diff --git a/libdexfile/Android.bp b/libdexfile/Android.bp
index 3a6cc0d..e627ad0 100644
--- a/libdexfile/Android.bp
+++ b/libdexfile/Android.bp
@@ -46,10 +46,19 @@
         "dex/type_lookup_table.cc",
         "dex/utf.cc",
     ],
-    header_libs: ["jni_headers"],
-    export_header_lib_headers: ["jni_headers"],
+    header_libs: [
+        "jni_headers",
+        "libdexfile_external_headers",
+    ],
+    export_header_lib_headers: [
+        "jni_headers",
+        "libdexfile_external_headers",
+    ],
     target: {
         android: {
+            srcs: [
+                "external/dex_file_ext.cc",
+            ],
             static_libs: [
                 "libziparchive",
             ],
@@ -67,6 +76,9 @@
             ],
         },
         not_windows: {
+            srcs: [
+                "external/dex_file_ext.cc",
+            ],
             shared_libs: [
                 "libziparchive",
                 "libz",
@@ -188,6 +200,11 @@
         "com.android.art",
         "com.android.art.debug",
     ],
+    stubs: {
+        // TODO(b/143978909): Rename and move this file to this directory.
+        symbol_file: "external/libdexfile_external.map.txt",
+        versions: ["1"],
+    },
 }
 
 art_cc_library {
@@ -233,6 +250,11 @@
         // stubs, and libartd depends on this.
         "com.android.art",
     ],
+    stubs: {
+        // TODO(b/143978909): Rename and move this file to this directory.
+        symbol_file: "external/libdexfile_external.map.txt",
+        versions: ["1"],
+    },
 }
 
 art_cc_test {
@@ -308,69 +330,6 @@
     min_sdk_version: "S",
 }
 
-cc_defaults {
-    name: "libdexfile_external-defaults",
-    host_supported: true,
-    srcs: [
-        "external/dex_file_ext.cc",
-    ],
-    header_libs: [
-        "jni_headers",
-        "libdexfile_external_headers",
-    ],
-    shared_libs: [
-        "libbase",
-    ],
-    stubs: {
-        symbol_file: "external/libdexfile_external.map.txt",
-        versions: ["1"],
-    },
-    export_header_lib_headers: [
-        "jni_headers",
-        "libdexfile_external_headers",
-    ],
-}
-
-art_cc_library {
-    name: "libdexfile_external",
-    defaults: [
-        "art_defaults",
-        "libdexfile_external-defaults",
-    ],
-    visibility: ["//visibility:public"],
-    target: {
-        darwin: {
-            enabled: true,
-        },
-    },
-    shared_libs: [
-        "libdexfile",
-    ],
-    apex_available: [
-        "com.android.art",
-        "com.android.art.debug",
-    ],
-}
-
-art_cc_library {
-    name: "libdexfiled_external",
-    defaults: [
-        "art_debug_defaults",
-        "libdexfile_external-defaults",
-    ],
-    target: {
-        darwin: {
-            enabled: true,
-        },
-    },
-    shared_libs: [
-        "libdexfiled",
-    ],
-    apex_available: [
-        "com.android.art.debug",
-    ],
-}
-
 art_cc_test {
     name: "art_libdexfile_external_tests",
     host_supported: true,
@@ -398,7 +357,7 @@
     srcs: [
         "external/dex_file_supp.cc",
     ],
-    runtime_libs: ["libdexfile_external"],
+    runtime_libs: ["libdexfile"],
     shared_libs: [
         "liblog",
         "libbase",
@@ -430,7 +389,7 @@
     shared_libs: [
         "libartbase",
         "libbase",
-        "libdexfile_external",
+        "libdexfile",
         "libdexfile_support",
     ],
 }
@@ -452,7 +411,7 @@
         "libdexfile_static_defaults",
     ],
     whole_static_libs: [
-        "libdexfile_external",
+        "libdexfile",
         "libdexfile_support_static",
     ],
 }
@@ -463,8 +422,8 @@
         "libdexfiled_static_defaults",
     ],
     whole_static_libs: [
+        "libdexfiled",
         "libdexfile_support_static",
-        "libdexfiled_external",
     ],
 }
 
@@ -482,7 +441,7 @@
 // This is not allowed in any module that may end up in an APEX or platform
 // image, so visibility is restrictive.
 cc_library_static {
-    name: "libdexfile_external_static",
+    name: "libdexfile_static",
     host_supported: true,
     visibility: [
         // Required for simpleperf, libsimpleperf_record, and libsimpleperf_report
@@ -501,14 +460,14 @@
 }
 
 art_cc_test {
-    name: "art_libdexfile_external_static_tests",
+    name: "art_libdexfile_static_tests",
     host_supported: true,
     test_suites: ["general-tests"],
     srcs: [
         "external/dex_file_supp_test.cc",
     ],
     static_libs: [
-        "libdexfile_external_static",
+        "libdexfile_static",
     ],
     enabled: false,
     target: {
@@ -518,7 +477,7 @@
     },
     // TODO(b/181740144): Enable a ubsan check to create a dependency on
     // ubsan_minimal. It's needed to be able to link with the prebuilt
-    // libdexfile_external_static.a, which contains libziparchive.a, which is
+    // libdexfile_static.a, which contains libziparchive.a, which is
     // built with some ubsan checks
     // (https://cs.android.com/android/platform/superproject/+/master:system/libziparchive/Android.bp;l=47-59;drc=c7b498fdf2002194709e40ea58ce39f43684fc14)
     // that the SDK snapshots currently don't propagate properly for static
diff --git a/libdexfile/external/dex_file_ext.cc b/libdexfile/external/dex_file_ext.cc
index 4432f6a..302e188 100644
--- a/libdexfile/external/dex_file_ext.cc
+++ b/libdexfile/external/dex_file_ext.cc
@@ -18,7 +18,6 @@
 
 #include <inttypes.h>
 #include <stdint.h>
-#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -43,6 +42,7 @@
 #include <dex/dex_file_loader.h>
 
 extern "C" {
+
 // Wraps DexFile to add the caching needed by the external interface. This is
 // what gets passed over as ExtDexFile*.
 struct ExtDexFile {
diff --git a/libdexfile/external/dex_file_supp.cc b/libdexfile/external/dex_file_supp.cc
index 63dec1c..6313afe 100644
--- a/libdexfile/external/dex_file_supp.cc
+++ b/libdexfile/external/dex_file_supp.cc
@@ -65,10 +65,10 @@
     // Check which version is already loaded to avoid loading both debug and
     // release builds. We might also be backtracing from separate process, in
     // which case neither is loaded.
-    const char* so_name = "libdexfiled_external.so";
+    const char* so_name = "libdexfiled.so";
     void* handle = dlopen(so_name, RTLD_NOLOAD | RTLD_NOW | RTLD_NODELETE);
     if (handle == nullptr) {
-      so_name = "libdexfile_external.so";
+      so_name = "libdexfile.so";
       handle = dlopen(so_name, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
     }
     if (handle == nullptr) {
diff --git a/odrefresh/Android.bp b/odrefresh/Android.bp
index a3fa07c..8e39edf 100644
--- a/odrefresh/Android.bp
+++ b/odrefresh/Android.bp
@@ -113,6 +113,10 @@
     ],
     apex_available: [
         "com.android.art.debug",
+        // TODO(b/183882457): This binary 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 libartd.
+        "com.android.art",
     ],
 }
 
diff --git a/runtime/Android.bp b/runtime/Android.bp
index 33f4040..97f46a4 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -548,8 +548,6 @@
     shared_libs: [
         "libartbase",
         "libdexfile",
-        // We need to eagerly load it so libdexfile_support used from libunwindstack can find it.
-        "libdexfile_external",
         "libprofile",
     ],
     export_shared_lib_headers: [
@@ -582,9 +580,6 @@
     shared_libs: [
         "libartbased",
         "libdexfiled",
-        // We need to eagerly preload it, so that libunwindstack can find it.
-        // Otherwise, it would try to load the non-debug version with dlopen.
-        "libdexfiled_external",
         "libprofiled",
     ],
     export_shared_lib_headers: [
diff --git a/test/Android.bp b/test/Android.bp
index 7df6ead..bc340bd 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -29,6 +29,12 @@
     name: "art_test_defaults",
     host_supported: true,
     target: {
+        android: {
+            test_for: [
+                "com.android.art",
+                "com.android.art.debug",
+            ],
+        },
         android_arm: {
             relative_install_path: "art/arm",
         },
@@ -48,6 +54,8 @@
     cflags: [
         "-Wno-frame-larger-than=",
     ],
+    // Tests aren't generally included in any APEX, but this is necessary to
+    // avoid duplicate install rules for them by making them unavailable to platform.
     apex_available: [
         "com.android.art.debug",
     ],
@@ -151,6 +159,10 @@
     },
     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",
     ],
 }
 
@@ -187,6 +199,10 @@
     },
     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",
     ],
 }
 
@@ -1035,6 +1051,10 @@
             suffix: "64",
         },
     },
+    test_for: [
+        "com.android.art",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {
@@ -1051,6 +1071,10 @@
     ],
     test_suites: ["general-tests"],
     test_config: "art-gtests-target-chroot.xml",
+    test_for: [
+        "com.android.art",
+        "com.android.art.debug",
+    ],
 }
 
 filegroup {
diff --git a/tools/pylibdexfile.py b/tools/pylibdexfile.py
index 30c0b26..953cf0d 100644
--- a/tools/pylibdexfile.py
+++ b/tools/pylibdexfile.py
@@ -15,7 +15,7 @@
 # limitations under the License.
 
 #
-# This script can get info out of dexfiles using libdexfile_external
+# This script can get info out of dexfiles using libdexfile.so external API
 #
 
 from abc import ABC
@@ -24,8 +24,8 @@
 import functools
 import zipfile
 
-libdexfile_external = CDLL(
-    os.path.expandvars("$ANDROID_HOST_OUT/lib64/libdexfile_external.so"))
+libdexfile = CDLL(
+    os.path.expandvars("$ANDROID_HOST_OUT/lib64/libdexfile.so"))
 
 DexFileStr = c_void_p
 ExtDexFile = c_void_p
@@ -37,28 +37,28 @@
 
 
 AllMethodsCallback = CFUNCTYPE(c_int, POINTER(ExtMethodInfo), c_void_p)
-libdexfile_external.ExtDexFileOpenFromFd.argtypes = [
+libdexfile.ExtDexFileOpenFromFd.argtypes = [
     c_int, c_size_t, c_char_p,
     POINTER(DexFileStr),
     POINTER(ExtDexFile)
 ]
-libdexfile_external.ExtDexFileOpenFromFd.restype = c_int
-libdexfile_external.ExtDexFileOpenFromMemory.argtypes = [
+libdexfile.ExtDexFileOpenFromFd.restype = c_int
+libdexfile.ExtDexFileOpenFromMemory.argtypes = [
     c_void_p,
     POINTER(c_size_t), c_char_p,
     POINTER(DexFileStr),
     POINTER(ExtDexFile)
 ]
-libdexfile_external.ExtDexFileOpenFromMemory.restype = c_int
-libdexfile_external.ExtDexFileFree.argtypes = [ExtDexFile]
-libdexfile_external.ExtDexFileGetAllMethodInfos.argtypes = [
+libdexfile.ExtDexFileOpenFromMemory.restype = c_int
+libdexfile.ExtDexFileFree.argtypes = [ExtDexFile]
+libdexfile.ExtDexFileGetAllMethodInfos.argtypes = [
     ExtDexFile, c_int, AllMethodsCallback, c_void_p
 ]
-libdexfile_external.ExtDexFileGetString.argtypes = [
+libdexfile.ExtDexFileGetString.argtypes = [
     DexFileStr, POINTER(c_size_t)
 ]
-libdexfile_external.ExtDexFileGetString.restype = c_char_p
-libdexfile_external.ExtDexFileFreeString.argtypes = [DexFileStr]
+libdexfile.ExtDexFileGetString.restype = c_char_p
+libdexfile.ExtDexFileFreeString.argtypes = [DexFileStr]
 
 
 class DexClass(object):
@@ -105,9 +105,9 @@
   def __init__(self, mi):
     self.offset = mi.offset
     self.len = mi.len
-    self.name = libdexfile_external.ExtDexFileGetString(
+    self.name = libdexfile.ExtDexFileGetString(
         mi.name, byref(c_size_t(0))).decode("utf-8")
-    libdexfile_external.ExtDexFileFreeString(mi.name)
+    libdexfile.ExtDexFileFreeString(mi.name)
 
   def __repr__(self):
     return "(" + self.name + ")"
@@ -164,8 +164,8 @@
       meths.append(Method(info[0]))
       return 0
 
-    libdexfile_external.ExtDexFileGetAllMethodInfos(self.ext_dex_file_,
-                                                    c_int(1), my_cb, c_void_p())
+    libdexfile.ExtDexFileGetAllMethodInfos(self.ext_dex_file_,
+                                           c_int(1), my_cb, c_void_p())
     return meths
 
 
@@ -176,14 +176,14 @@
     super().__init__()
     res_fle_ptr = pointer(c_void_p())
     err_ptr = pointer(c_void_p())
-    res = libdexfile_external.ExtDexFileOpenFromFd(
+    res = libdexfile.ExtDexFileOpenFromFd(
         c_int(fd), 0, create_string_buffer(bytes(loc, "utf-8")), err_ptr,
         res_fle_ptr)
     if res == 0:
-      err = libdexfile_external.ExtDexFileGetString(err_ptr.contents,
-                                                    byref(c_size_t()))
+      err = libdexfile.ExtDexFileGetString(err_ptr.contents,
+                                           byref(c_size_t()))
       out = Exception("Failed to open file: {}. Error was: {}".format(loc, err))
-      libdexfile_external.ExtDexFileFreeString(err_ptr.contents)
+      libdexfile.ExtDexFileFreeString(err_ptr.contents)
       raise out
     self.ext_dex_file_ = res_fle_ptr.contents
 
@@ -209,14 +209,14 @@
     self.mem_ref = (c_byte * len(dat)).from_buffer_copy(dat)
     res_fle_ptr = pointer(c_void_p())
     err_ptr = pointer(c_void_p())
-    res = libdexfile_external.ExtDexFileOpenFromMemory(
+    res = libdexfile.ExtDexFileOpenFromMemory(
         self.mem_ref, byref(c_size_t(len(dat))),
         create_string_buffer(bytes(loc, "utf-8")), err_ptr, res_fle_ptr)
     if res == 0:
-      err = libdexfile_external.ExtDexFileGetString(err_ptr.contents,
-                                                    byref(c_size_t()))
+      err = libdexfile.ExtDexFileGetString(err_ptr.contents,
+                                           byref(c_size_t()))
       out = Exception("Failed to open file: {}. Error was: {}".format(loc, err))
-      libdexfile_external.ExtDexFileFreeString(err_ptr.contents)
+      libdexfile.ExtDexFileFreeString(err_ptr.contents)
       raise out
     self.ext_dex_file_ = res_fle_ptr.contents