Make dexlayout and profman build without libart

Use libprofile and libartbase to remove the dependencies on libart for
dexlayout and profman.  dexdiag remains connected to libart because of
vdex file APIs.

Bug: 78652467
Test: make -j 40 test-art-host-gtest
Change-Id: Ie4d58e7e75aa725a6d453a9d4c7fefd868aa7b2d
diff --git a/dexlayout/Android.bp b/dexlayout/Android.bp
index b009774..5285c08 100644
--- a/dexlayout/Android.bp
+++ b/dexlayout/Android.bp
@@ -27,6 +27,7 @@
     ],
     export_include_dirs: ["."],
     shared_libs: [
+        "libartbase",
         "libbase",
     ],
     static_libs: ["libz"],
@@ -39,7 +40,6 @@
         "dex2oat-pgo-defaults",
     ],
     shared_libs: [
-        "libart",
         "libdexfile",
         "libprofile",
     ],
@@ -60,7 +60,6 @@
       "art_debug_defaults",
     ],
     shared_libs: [
-        "libartd",
         "libdexfiled",
         "libprofiled",
     ],
@@ -80,8 +79,9 @@
     name: "dexlayout",
     defaults: ["dexlayout-defaults"],
     shared_libs: [
+        "libartbase",
+        "libdexfile",
         "libprofile",
-        "libart",
         "libart-dexlayout",
     ],
 }
@@ -93,8 +93,9 @@
         "dexlayout-defaults",
     ],
     shared_libs: [
+        "libartbased",
+        "libdexfiled",
         "libprofiled",
-        "libartd",
         "libartd-dexlayout",
     ],
 }
diff --git a/dexlayout/dexlayout.cc b/dexlayout/dexlayout.cc
index 62dd1a9..03dfee3 100644
--- a/dexlayout/dexlayout.cc
+++ b/dexlayout/dexlayout.cc
@@ -36,8 +36,8 @@
 #include "base/logging.h"  // For VLOG_IS_ON.
 #include "base/mem_map.h"
 #include "base/os.h"
+#include "base/unix_file/fd_file.h"
 #include "base/utils.h"
-#include "dex/art_dex_file_loader.h"
 #include "dex/descriptors_names.h"
 #include "dex/dex_file-inl.h"
 #include "dex/dex_file_layout.h"
@@ -1930,7 +1930,7 @@
       std::string location = "memory mapped file for " + std::string(file_name);
       // Dex file verifier cannot handle compact dex.
       bool verify = options_.compact_dex_level_ == CompactDexLevel::kCompactDexLevelNone;
-      const ArtDexFileLoader dex_file_loader;
+      const DexFileLoader dex_file_loader;
       DexContainer::Section* const main_section = (*dex_container)->GetMainSection();
       DexContainer::Section* const data_section = (*dex_container)->GetDataSection();
       DCHECK_EQ(file_size, main_section->Size())
@@ -1980,10 +1980,32 @@
   // all of which are Zip archives with "classes.dex" inside.
   const bool verify_checksum = !options_.ignore_bad_checksum_;
   std::string error_msg;
-  const ArtDexFileLoader dex_file_loader;
+  std::unique_ptr<File> input_file(OS::OpenFileForReading(file_name));
+  if (input_file == nullptr) {
+    LOG(ERROR) << "Could not open file " << file_name << " for reading";
+    return -1;
+  }
+  std::unique_ptr<MemMap> mmap(MemMap::MapFile(input_file->GetLength(),
+                                               PROT_READ,
+                                               MAP_PRIVATE,
+                                               input_file->Fd(),
+                                               /*start*/0,
+                                               /*low_4gb*/false,
+                                               file_name,
+                                               &error_msg));
+  if (mmap == nullptr) {
+    LOG(ERROR) << "MemMap failed for '" << file_name << "' " << error_msg;
+    return -1;
+  }
+  const DexFileLoader dex_file_loader;
   std::vector<std::unique_ptr<const DexFile>> dex_files;
-  if (!dex_file_loader.Open(
-        file_name, file_name, /* verify */ true, verify_checksum, &error_msg, &dex_files)) {
+  if (!dex_file_loader.OpenAll(mmap->Begin(),
+                               mmap->Size(),
+                               file_name,
+                               /*verify*/true,
+                               verify_checksum,
+                               &error_msg,
+                               &dex_files)) {
     // Display returned error message to user. Note that this error behavior
     // differs from the error messages shown by the original Dalvik dexdump.
     LOG(ERROR) << error_msg;
diff --git a/dexlayout/dexlayout_main.cc b/dexlayout/dexlayout_main.cc
index 185c142..3f92d50 100644
--- a/dexlayout/dexlayout_main.cc
+++ b/dexlayout/dexlayout_main.cc
@@ -34,7 +34,6 @@
 #include "base/logging.h"  // For InitLogging.
 #include "base/mem_map.h"
 #include "profile/profile_compilation_info.h"
-#include "runtime.h"
 
 namespace art {
 
@@ -66,12 +65,17 @@
   LOG(ERROR) << " -x : compact dex generation level, either 'none' or 'fast'";
 }
 
+NO_RETURN static void Abort(const char* msg) {
+  LOG(ERROR) << "Aborted: " << msg;
+  exit(1);
+}
+
 /*
  * Main driver of the dexlayout utility.
  */
 int DexlayoutDriver(int argc, char** argv) {
   // Art specific set up.
-  InitLogging(argv, Runtime::Abort);
+  InitLogging(argv, Abort);
   MemMap::Init();
 
   Options options;
diff --git a/libprofile/Android.bp b/libprofile/Android.bp
index bcb90cb..5afe73b 100644
--- a/libprofile/Android.bp
+++ b/libprofile/Android.bp
@@ -40,7 +40,6 @@
             ],
         },
     },
-    //generated_sources: ["art_libartbase_operator_srcs"],
     cflags: ["-DBUILDING_LIBART=1"],
     shared_libs: [
         "libartbase",
diff --git a/profman/Android.bp b/profman/Android.bp
index 3c8c72c..c9c92e6 100644
--- a/profman/Android.bp
+++ b/profman/Android.bp
@@ -39,7 +39,7 @@
     name: "profman",
     defaults: ["profman-defaults"],
     shared_libs: [
-        "libart",
+        "libartbase",
         "libprofile",
         "libdexfile",
     ],
@@ -52,7 +52,7 @@
         "profman-defaults",
     ],
     shared_libs: [
-        "libartd",
+        "libartbased",
         "libprofiled",
         "libdexfiled",
     ],
diff --git a/profman/profman.cc b/profman/profman.cc
index cd88d03..c16fadd 100644
--- a/profman/profman.cc
+++ b/profman/profman.cc
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/file.h>
+#include <sys/mman.h>
 #include <sys/param.h>
 #include <unistd.h>
 
@@ -40,7 +41,6 @@
 #include "base/utils.h"
 #include "base/zip_archive.h"
 #include "boot_image_profile.h"
-#include "dex/art_dex_file_loader.h"
 #include "dex/bytecode_utils.h"
 #include "dex/code_item_accessors-inl.h"
 #include "dex/dex_file.h"
@@ -49,7 +49,6 @@
 #include "dex/type_reference.h"
 #include "profile/profile_compilation_info.h"
 #include "profile_assistant.h"
-#include "runtime.h"
 
 namespace art {
 
@@ -177,6 +176,11 @@
 static constexpr char kMethodFlagStringStartup = 'S';
 static constexpr char kMethodFlagStringPostStartup = 'P';
 
+NO_RETURN static void Abort(const char* msg) {
+  LOG(ERROR) << "Aborted: " << msg;
+  exit(1);
+}
+
 // TODO(calin): This class has grown too much from its initial design. Split the functionality
 // into smaller, more contained pieces.
 class ProfMan FINAL {
@@ -202,8 +206,8 @@
     original_argc = argc;
     original_argv = argv;
 
-    Locks::Init();
-    InitLogging(argv, Runtime::Abort);
+    MemMap::Init();
+    InitLogging(argv, Abort);
 
     // Skip over the command name.
     argv++;
@@ -413,36 +417,49 @@
     }
     static constexpr bool kVerifyChecksum = true;
     for (size_t i = 0; i < dex_locations_.size(); ++i) {
-      std::string error_msg;
-      const ArtDexFileLoader dex_file_loader;
-      std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
+      std::unique_ptr<File> apk_file;
       // We do not need to verify the apk for processing profiles.
       if (use_apk_fd_list) {
-        if (dex_file_loader.OpenZip(apks_fd_[i],
-                                    dex_locations_[i],
-                                    /* verify */ false,
-                                    kVerifyChecksum,
-                                    &error_msg,
-                                    &dex_files_for_location)) {
-        } else {
-          LOG(ERROR) << "OpenZip failed for '" << dex_locations_[i] << "' " << error_msg;
-          return false;
-        }
+        apk_file.reset(new File(apks_fd_[i], false/*checkUsage*/));
       } else {
-        if (dex_file_loader.Open(apk_files_[i].c_str(),
-                                 dex_locations_[i],
-                                 /* verify */ false,
-                                 kVerifyChecksum,
-                                 &error_msg,
-                                 &dex_files_for_location)) {
-        } else {
-          LOG(ERROR) << "Open failed for '" << dex_locations_[i] << "' " << error_msg;
+        apk_file.reset(new File(apk_files_[i], O_RDONLY, false/*checkUsage*/));
+        if (apk_file == nullptr) {
+          LOG(ERROR) << "Open failed for '" << dex_locations_[i] << "' ";
           return false;
         }
       }
+      std::string error_msg;
+      std::unique_ptr<MemMap> mmap(MemMap::MapFile(apk_file->GetLength(),
+                                                   PROT_READ,
+                                                   MAP_PRIVATE,
+                                                   apk_file->Fd(),
+                                                   /*start*/0,
+                                                   /*low_4gb*/false,
+                                                   dex_locations_[i].c_str(),
+                                                   &error_msg));
+      if (mmap == nullptr) {
+        LOG(ERROR) << "MemMap failed for '" << dex_locations_[i] << "' " << error_msg;
+        return false;
+      }
+      const DexFileLoader dex_file_loader;
+      std::vector<std::unique_ptr<const DexFile>> dex_files_for_location;
+      if (!dex_file_loader.OpenAll(mmap->Begin(),
+                                   mmap->Size(),
+                                   dex_locations_[i],
+                                   /* verify */ false,
+                                   kVerifyChecksum,
+                                   &error_msg,
+                                   &dex_files_for_location)) {
+        LOG(ERROR) << "OpenAll failed for '" << dex_locations_[i] << "' " << error_msg;
+        return false;
+      }
       for (std::unique_ptr<const DexFile>& dex_file : dex_files_for_location) {
         process_fn(std::move(dex_file));
       }
+      // Leak apk_file and mmap for now.
+      // TODO: close fds, etc.
+      apk_file.release();
+      mmap.release();
     }
     return true;
   }