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/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;
   }