Host gtests: Use the apex boot image instead of core.art.

Closely mirror the behaviour of target tests.

Test: m test-art-host-gtest
Bug: 147817558
Change-Id: If9a1d34cef022fa93c9c09c8f31adc26ac71afdc
diff --git a/build/Android.common_path.mk b/build/Android.common_path.mk
index 42d64b4..42460ad 100644
--- a/build/Android.common_path.mk
+++ b/build/Android.common_path.mk
@@ -99,6 +99,15 @@
 HOST_CORE_DEX_FILES   := $(foreach jar,$(HOST_TEST_CORE_JARS),  $(call intermediates-dir-for,JAVA_LIBRARIES,$(jar),t,COMMON)/javalib.jar)
 TARGET_CORE_DEX_FILES := $(foreach jar,$(TARGET_CORE_IMG_JARS), $(call intermediates-dir-for,JAVA_LIBRARIES,$(jar).com.android.art.testing, ,COMMON)/javalib.jar)
 TARGET_CORE_DEX_FILES += $(call intermediates-dir-for,JAVA_LIBRARIES,conscrypt.com.android.conscrypt, ,COMMON)/javalib.jar
+
+# Also copy the jar files next to host boot.art image.
+HOST_BOOT_IMAGE_JARS := $(foreach jar,$(CORE_IMG_JARS),$(HOST_OUT)/apex/com.android.art/javalib/$(jar).jar)
+$(HOST_BOOT_IMAGE_JARS): $(HOST_OUT)/apex/com.android.art/javalib/%.jar : $(HOST_OUT_JAVA_LIBRARIES)/%-hostdex.jar
+	$(copy-file-to-target)
+HOST_BOOT_IMAGE_JARS += $(HOST_OUT)/apex/com.android.conscrypt/javalib/conscrypt.jar
+$(HOST_OUT)/apex/com.android.conscrypt/javalib/conscrypt.jar : $(HOST_OUT_JAVA_LIBRARIES)/conscrypt-hostdex.jar
+	$(copy-file-to-target)
+
 ART_HOST_DEX_DEPENDENCIES := $(foreach jar,$(HOST_TEST_CORE_JARS),$(HOST_OUT_JAVA_LIBRARIES)/$(jar).jar)
 ART_TARGET_DEX_DEPENDENCIES := com.android.art.testing com.android.conscrypt
 
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index ab8a8c8..a40fc9e 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -488,6 +488,8 @@
   gtest_exe := $(2)
   # Dependencies for all host gtests.
   gtest_deps := $$(HOST_CORE_DEX_LOCATIONS) \
+    $$($(3)HOST_BOOT_IMAGE) \
+    $$(HOST_BOOT_IMAGE_JARS) \
     $$($(3)ART_HOST_OUT_SHARED_LIBRARIES)/libicu_jni$$(ART_HOST_SHLIB_EXTENSION) \
     $$($(3)ART_HOST_OUT_SHARED_LIBRARIES)/libjavacore$$(ART_HOST_SHLIB_EXTENSION) \
     $$($(3)ART_HOST_OUT_SHARED_LIBRARIES)/libopenjdkd$$(ART_HOST_SHLIB_EXTENSION) \
diff --git a/libartbase/base/common_art_test.cc b/libartbase/base/common_art_test.cc
index 978f69c..a7675f4 100644
--- a/libartbase/base/common_art_test.cc
+++ b/libartbase/base/common_art_test.cc
@@ -338,14 +338,19 @@
   MemMap::Init();
   static constexpr bool kVerifyChecksum = true;
   const ArtDexFileLoader dex_file_loader;
-  if (!dex_file_loader.Open(
-        location, location, /* verify= */ true, kVerifyChecksum, &error_msg, &dex_files)) {
-    LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
+  std::string prefix = IsHost() ? GetAndroidRoot() : "";
+  std::string filename = prefix + location;
+  if (!dex_file_loader.Open(filename.c_str(),
+                            std::string(location),
+                            /* verify= */ true,
+                            kVerifyChecksum,
+                            &error_msg,
+                            &dex_files)) {
+    LOG(FATAL) << "Could not open .dex file '" << filename << "': " << error_msg << "\n";
     UNREACHABLE();
-  } else {
-    CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
-    return std::move(dex_files[0]);
   }
+  CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << filename;
+  return std::move(dex_files[0]);
 }
 
 void CommonArtTestImpl::ClearDirectory(const char* dirpath, bool recursive) {
@@ -388,13 +393,11 @@
 }
 
 static std::string GetDexFileName(const std::string& jar_prefix, bool host) {
-  if (host) {
-    std::string path = GetAndroidRoot();
-    return StringPrintf("%s/framework/%s-hostdex.jar", path.c_str(), jar_prefix.c_str());
-  } else {
-    const char* apex = (jar_prefix == "conscrypt") ? "com.android.conscrypt" : "com.android.art";
-    return StringPrintf("/apex/%s/javalib/%s.jar", apex, jar_prefix.c_str());
-  }
+  std::string prefix(host ? GetAndroidRoot() : "");
+  const char* apexPath = (jar_prefix == "conscrypt")
+    ? kAndroidConscryptApexDefaultPath
+    : kAndroidArtApexDefaultPath;
+  return StringPrintf("%s%s/javalib/%s.jar", prefix.c_str(), apexPath, jar_prefix.c_str());
 }
 
 std::vector<std::string> CommonArtTestImpl::GetLibCoreModuleNames() const {
@@ -433,14 +436,8 @@
     const std::vector<std::string>& modules) const {
   std::vector<std::string> result = GetLibCoreDexFileNames(modules);
   if (IsHost()) {
-    // Strip the ANDROID_BUILD_TOP directory including the directory separator '/'.
-    const char* host_dir = getenv("ANDROID_BUILD_TOP");
-    CHECK(host_dir != nullptr);
-    std::string prefix = host_dir;
-    CHECK(!prefix.empty());
-    if (prefix.back() != '/') {
-      prefix += '/';
-    }
+    // Strip the build directory prefix (e.g. .../host/linux-x86)
+    std::string prefix = GetAndroidRoot();
     for (std::string& location : result) {
       CHECK_GT(location.size(), prefix.size());
       CHECK_EQ(location.compare(0u, prefix.size(), prefix), 0);
@@ -522,16 +519,8 @@
 
 std::string CommonArtTestImpl::GetCoreFileLocation(const char* suffix) {
   CHECK(suffix != nullptr);
-
-  std::string location;
-  if (IsHost()) {
-    std::string host_dir = GetAndroidRoot();
-    location = StringPrintf("%s/framework/core.%s", host_dir.c_str(), suffix);
-  } else {
-    location = StringPrintf("/apex/com.android.art/javalib/boot.%s", suffix);
-  }
-
-  return location;
+  std::string prefix(IsHost() ? GetAndroidRoot() : "");
+  return StringPrintf("%s%s/javalib/boot.%s", prefix.c_str(), kAndroidArtApexDefaultPath, suffix);
 }
 
 std::string CommonArtTestImpl::CreateClassPath(
diff --git a/libartbase/base/file_utils.cc b/libartbase/base/file_utils.cc
index a899b86..98b69f3 100644
--- a/libartbase/base/file_utils.cc
+++ b/libartbase/base/file_utils.cc
@@ -71,9 +71,7 @@
 static constexpr const char* kAndroidDataEnvVar = "ANDROID_DATA";
 static constexpr const char* kAndroidDataDefaultPath = "/data";
 static constexpr const char* kAndroidArtRootEnvVar = "ANDROID_ART_ROOT";
-static constexpr const char* kAndroidArtApexDefaultPath = "/apex/com.android.art";
 static constexpr const char* kAndroidConscryptRootEnvVar = "ANDROID_CONSCRYPT_ROOT";
-static constexpr const char* kAndroidConscryptApexDefaultPath = "/apex/com.android.conscrypt";
 
 // Get the "root" directory containing the "lib" directory where this instance
 // of the libartbase library (which contains `GetRootContainingLibartbase`) is
diff --git a/libartbase/base/file_utils.h b/libartbase/base/file_utils.h
index 6a57fbd..b1d044a 100644
--- a/libartbase/base/file_utils.h
+++ b/libartbase/base/file_utils.h
@@ -27,6 +27,9 @@
 
 namespace art {
 
+static constexpr const char* kAndroidArtApexDefaultPath = "/apex/com.android.art";
+static constexpr const char* kAndroidConscryptApexDefaultPath = "/apex/com.android.conscrypt";
+
 // These methods return the Android Root, which is the historical location of
 // the Android "system" directory, containing the built Android artifacts. On
 // target, this is normally "/system". On host this is usually a directory under
diff --git a/oatdump/oatdump_test.h b/oatdump/oatdump_test.h
index 00672b8..f47c4d2 100644
--- a/oatdump/oatdump_test.h
+++ b/oatdump/oatdump_test.h
@@ -224,6 +224,7 @@
         exec_argv.push_back("--app-image=" + GetAppImageName());
       } else if (mode == kModeCoreOat) {
         exec_argv.push_back("--oat-file=" + core_oat_location_);
+        exec_argv.push_back("--dex-file=" + GetLibCoreDexFileNames()[0]);
       } else {
         CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
         exec_argv.push_back("--oat-file=" + GetAppOdexName());
diff --git a/runtime/common_runtime_test.cc b/runtime/common_runtime_test.cc
index ed8a024..640e182 100644
--- a/runtime/common_runtime_test.cc
+++ b/runtime/common_runtime_test.cc
@@ -523,22 +523,22 @@
 }
 
 std::string CommonRuntimeTestImpl::GetImageDirectory() {
+  std::string prefix;
   if (IsHost()) {
     const char* host_dir = getenv("ANDROID_HOST_OUT");
     CHECK(host_dir != nullptr);
-    return std::string(host_dir) + "/framework";
-  } else {
-    return std::string("/apex/com.android.art/javalib");
+    prefix = host_dir;
   }
+  return prefix + kAndroidArtApexDefaultPath + "/javalib";
 }
 
 std::string CommonRuntimeTestImpl::GetImageLocation() {
-  return GetImageDirectory() + (IsHost() ? "/core.art" : "/boot.art");
+  return GetImageDirectory() + "/boot.art";
 }
 
 std::string CommonRuntimeTestImpl::GetSystemImageFile() {
   std::string isa = GetInstructionSetString(kRuntimeISA);
-  return GetImageDirectory() + "/" + isa + (IsHost() ? "/core.art" : "/boot.art");
+  return GetImageDirectory() + "/" + isa + "/boot.art";
 }
 
 void CommonRuntimeTestImpl::EnterTransactionMode() {
diff --git a/tools/build_linux_bionic_tests.sh b/tools/build_linux_bionic_tests.sh
index 76dabd4..c834dd5 100755
--- a/tools/build_linux_bionic_tests.sh
+++ b/tools/build_linux_bionic_tests.sh
@@ -82,7 +82,7 @@
   $soong_out/bin/hprof-conv
   $soong_out/bin/signal_dumper
   $soong_out/lib64/libclang_rt.ubsan_standalone-x86_64-android.so
-  $(find $host_out/apex -type f | sed "s:$host_out:$soong_out:g")
+  $(find $host_out/apex/com.android.art.host.zipapex -type f | sed "s:$host_out:$soong_out:g")
   $(find $host_out/lib64 -type f | sed "s:$host_out:$soong_out:g")
   $(find $host_out/nativetest64 -type f | sed "s:$host_out:$soong_out:g"))