Merge libdexfile_external into libdexfile.

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.

Test: Flash and boot with userdebug and eng to try both release and
  debug modules.
Test: art/build/apex/runtests.sh
Bug: 143978909
Change-Id: Ia011fa3a86509839ea0d80ad5fcad67ee5ce68d9
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 4e9dca8..0b07395 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 91af3db..30f8e1f 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/libdexfile/Android.bp b/libdexfile/Android.bp
index 8aa287f..8c7114e 100644
--- a/libdexfile/Android.bp
+++ b/libdexfile/Android.bp
@@ -45,9 +45,16 @@
         "dex/standard_dex_file.cc",
         "dex/type_lookup_table.cc",
         "dex/utf.cc",
+        "external/dex_file_ext.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: {
             static_libs: [
@@ -188,6 +195,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 {
@@ -229,6 +241,11 @@
     apex_available: [
         "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_test {
@@ -304,69 +321,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,
@@ -394,7 +348,7 @@
     srcs: [
         "external/dex_file_supp.cc",
     ],
-    runtime_libs: ["libdexfile_external"],
+    runtime_libs: ["libdexfile"],
     shared_libs: [
         "liblog",
         "libbase",
@@ -426,7 +380,7 @@
     shared_libs: [
         "libartbase",
         "libbase",
-        "libdexfile_external",
+        "libdexfile",
         "libdexfile_support",
     ],
 }
@@ -448,7 +402,7 @@
         "libdexfile_static_defaults",
     ],
     whole_static_libs: [
-        "libdexfile_external",
+        "libdexfile",
         "libdexfile_support_static",
     ],
 }
@@ -459,8 +413,8 @@
         "libdexfiled_static_defaults",
     ],
     whole_static_libs: [
+        "libdexfiled",
         "libdexfile_support_static",
-        "libdexfiled_external",
     ],
 }
 
@@ -478,7 +432,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
@@ -497,14 +451,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: {
@@ -514,7 +468,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_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/libdexfile/external/libdexfile_external.map.txt b/libdexfile/external/libdexfile_external.map.txt
index e9d12fb..174a546 100644
--- a/libdexfile/external/libdexfile_external.map.txt
+++ b/libdexfile/external/libdexfile_external.map.txt
@@ -1,4 +1,4 @@
-LIBDEXFILE_EXTERNAL_1 {
+LIBDEXFILE_1 {
   global:
     ExtDexFileClose;
     ExtDexFileGetAllMethodInfos;
diff --git a/runtime/Android.bp b/runtime/Android.bp
index 3de88d7..ca2fb36 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -547,8 +547,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: [
@@ -581,9 +579,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 af3aa21..44f84cd 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",
         },
@@ -1034,6 +1040,10 @@
             suffix: "64",
         },
     },
+    test_for: [
+        "com.android.art",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {
@@ -1050,6 +1060,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