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/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) {