Take into account custom OUT_DIR in gtests

Custom OUT_DIR settings need to be taken into account when checking
the ANDROID_HOST_OUT and gtest locations.

Test: m test-art-host-gtest
Change-Id: Iba0d11b6ad85ea32731fa6afe54608bacd66dcb9
diff --git a/libartbase/base/common_art_test.cc b/libartbase/base/common_art_test.cc
index 6c19138..c372e10 100644
--- a/libartbase/base/common_art_test.cc
+++ b/libartbase/base/common_art_test.cc
@@ -169,8 +169,11 @@
 
   // Check that the expected directory matches the environment variable.
   const char* android_build_top_from_env = getenv("ANDROID_BUILD_TOP");
+  android_build_top = std::filesystem::path(android_build_top).string();
+  CHECK(!android_build_top.empty());
   if (android_build_top_from_env != nullptr) {
-    CHECK_EQ(android_build_top, android_build_top_from_env);
+    CHECK_EQ(std::filesystem::weakly_canonical(android_build_top).string(),
+             std::filesystem::weakly_canonical(android_build_top_from_env).string());
   } else {
     setenv("ANDROID_BUILD_TOP", android_build_top.c_str(), /*overwrite=*/0);
   }
@@ -182,16 +185,33 @@
 
 std::string CommonArtTestImpl::GetAndroidHostOut() {
   CHECK(IsHost());
-  std::string android_host_out = GetAndroidBuildTop() + "out/host/linux-x86";
 
   // Check that the expected directory matches the environment variable.
+  // ANDROID_HOST_OUT is set by envsetup or unset and is the full path to host binaries/libs
   const char* android_host_out_from_env = getenv("ANDROID_HOST_OUT");
+  // OUT_DIR is a user-settable ENV_VAR that controls where soong puts build artifacts. It can
+  // either be relative to ANDROID_BUILD_TOP or a concrete path.
+  const char* android_out_dir = getenv("OUT_DIR");
+  // Take account of OUT_DIR setting.
+  if (android_out_dir == nullptr) {
+    android_out_dir = "out";
+  }
+  std::string android_host_out;
+  if (android_out_dir[0] == '/') {
+    android_host_out = (std::filesystem::path(android_out_dir) / "host" / "linux-x86").string();
+  } else {
+    android_host_out =
+        (std::filesystem::path(GetAndroidBuildTop()) / android_out_dir / "host" / "linux-x86")
+            .string();
+  }
+  std::filesystem::path expected(android_host_out);
   if (android_host_out_from_env != nullptr) {
-    CHECK_EQ(android_host_out, android_host_out_from_env);
+    std::filesystem::path from_env(std::filesystem::weakly_canonical(android_host_out_from_env));
+    CHECK_EQ(std::filesystem::weakly_canonical(expected).string(), from_env.string());
   } else {
     setenv("ANDROID_HOST_OUT", android_host_out.c_str(), /*overwrite=*/0);
   }
-  return android_host_out;
+  return expected.string();
 }
 
 void CommonArtTestImpl::SetUpAndroidRootEnvVars() {