Revert "Support running of host gtests in eng-prod"

This reverts commit 22872da653053bb4b86fbef67163a9f6b7aee25b.

Reason for revert: Checking if this CL is cause of b/160132136

Bug: 147817558
Bug: 160132136
Change-Id: Ifb6f2292292a7f69fbd0f2e9204fd37dbca84277
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index 7331033..083bb9e 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -25,13 +25,12 @@
 
 # Manually add system libraries that we need to run the host ART tools.
 my_files += \
-  $(foreach lib, libbacktrace libbase libc++ libicu_jni liblog libsigchain libunwindstack \
-    libziparchive libjavacore libandroidio libopenjdkd, \
-    $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST)/$(lib).so:lib64/$(lib).so \
-    $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST,,2ND)/$(lib).so:lib/$(lib).so) \
-  $(foreach lib, libcrypto libz libicuuc libicui18n libandroidicu libexpat, \
-    $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST)/$(lib).so:lib64/$(lib)-host.so \
-    $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST,,2ND)/$(lib).so:lib/$(lib)-host.so)
+  $(foreach lib, \
+    libbacktrace libbase libc++ libicu_jni liblog libsigchain libunwindstack libziparchive, \
+    $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST)/$(lib).so:lib64/$(lib).so) \
+  $(foreach lib, \
+    libcrypto libz, \
+    $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST)/$(lib).so:lib64/$(lib)-host.so)
 
 # Add apex directories for art, conscrypt and i18n.
 my_files += $(foreach infix,_ _VDEX_,$(foreach suffix,$(HOST_ARCH) $(HOST_2ND_ARCH), \
@@ -40,8 +39,7 @@
   $(foreach jar,$(CORE_IMG_JARS),\
     $(HOST_OUT_JAVA_LIBRARIES)/$(jar)-hostdex.jar:apex/com.android.art/javalib/$(jar).jar) \
   $(HOST_OUT_JAVA_LIBRARIES)/conscrypt-hostdex.jar:apex/com.android.conscrypt/javalib/conscrypt.jar\
-  $(HOST_OUT_JAVA_LIBRARIES)/core-icu4j-hostdex.jar:apex/com.android.i18n/javalib/core-icu4j.jar \
-  $(HOST_OUT)/com.android.i18n/etc/icu/icudt66l.dat:com.android.i18n/etc/icu/icudt66l.dat
+  $(HOST_OUT_JAVA_LIBRARIES)/core-icu4j-hostdex.jar:apex/com.android.i18n/javalib/core-icu4j.jar
 
 # Create dummy module that will copy all the data files into testcases directory.
 # For now, this copies everything to "out/host/linux-x86/" subdirectory, since it
@@ -53,7 +51,7 @@
 LOCAL_MODULE_CLASS := NATIVE_TESTS
 LOCAL_MODULE_SUFFIX := .txt
 LOCAL_COMPATIBILITY_SUITE := general-tests
-LOCAL_COMPATIBILITY_SUPPORT_FILES := $(ART_TESTCASES_PREBUILT_CONTENT) \
+LOCAL_COMPATIBILITY_SUPPORT_FILES := \
 	$(foreach f,$(my_files),$(call word-colon,1,$f):out/host/linux-x86/$(call word-colon,2,$f))
 include $(BUILD_SYSTEM)/base_rules.mk
 
diff --git a/build/art.go b/build/art.go
index af2a5de..8deea23 100644
--- a/build/art.go
+++ b/build/art.go
@@ -291,11 +291,10 @@
 	defer artTestMutex.Unlock()
 
 	if ctx.Os().Class == android.Host {
-		src := ctx.SrcPath().String()
-		path := strings.Split(ctx.Path().ToMakePath().String(), "/")
-		// Keep last two parts of the install path (e.g. bin/dex2oat).
-		dst := strings.Join(path[len(path)-2:], "/")
-		testcasesContent[dst] = src
+		path := ctx.Path().ToMakePath().String()
+		parts := strings.Split(path, "/")
+		// Keep last two parts of the path (e.g. bin/dex2oat).
+		testcasesContent[strings.Join(parts[len(parts)-2:], "/")] = path
 	}
 }
 
diff --git a/build/makevars.go b/build/makevars.go
index 22ef205..6e66966 100644
--- a/build/makevars.go
+++ b/build/makevars.go
@@ -15,22 +15,18 @@
 package art
 
 import (
-	"path/filepath"
 	"sort"
 	"strings"
 
 	"android/soong/android"
-	"android/soong/cc/config"
 )
 
 var (
-	pctx                  = android.NewPackageContext("android/soong/art")
-	prebuiltToolsForTests = []string{"as", "addr2line", "objdump"}
+	pctx = android.NewPackageContext("android/soong/art")
 )
 
 func init() {
 	android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
-	pctx.Import("android/soong/cc/config")
 }
 
 func makeVarsProvider(ctx android.MakeVarsContext) {
@@ -56,25 +52,4 @@
 		copy_cmds = append(copy_cmds, testcasesContent[key]+":"+key)
 	}
 	ctx.Strict("ART_TESTCASES_CONTENT", strings.Join(copy_cmds, " "))
-
-	// Add prebuilt tools.
-	copy_cmds = []string{}
-	for _, cmd := range prebuiltToolsForTests {
-		target := ctx.Config().Targets[android.BuildOs][0]
-		toolchain := config.FindToolchain(target.Os, target.Arch)
-		gccRoot, gccTriple := toolchain.GccRoot(), toolchain.GccTriple()
-		eval := func(path ...string) string {
-			result, err := ctx.Eval(filepath.Join(path...))
-			if err != nil {
-				panic(err)
-			}
-			return result
-		}
-		src := eval(gccRoot, "bin", gccTriple+"-"+cmd)
-		// Different tests use different paths, so we need to copy to two locations.
-		// TODO: Unify the test code so that this is no longer necessary.
-		copy_cmds = append(copy_cmds, src+":"+eval(gccRoot, "bin", gccTriple+"-"+cmd))
-		copy_cmds = append(copy_cmds, src+":"+eval(gccRoot, gccTriple, "bin", cmd))
-	}
-	ctx.Strict("ART_TESTCASES_PREBUILT_CONTENT", strings.Join(copy_cmds, " "))
 }
diff --git a/compiler/utils/assembler_test.h b/compiler/utils/assembler_test.h
index 268befb..9e23d11 100644
--- a/compiler/utils/assembler_test.h
+++ b/compiler/utils/assembler_test.h
@@ -738,7 +738,6 @@
   AssemblerTest() {}
 
   void SetUp() override {
-    CommonArtTest::SetUpAndroidRootEnvVars();
     allocator_.reset(new ArenaAllocator(&pool_));
     assembler_.reset(CreateAssembler(allocator_.get()));
     test_helper_.reset(
diff --git a/libartbase/base/common_art_test.cc b/libartbase/base/common_art_test.cc
index e327832..2497e77 100644
--- a/libartbase/base/common_art_test.cc
+++ b/libartbase/base/common_art_test.cc
@@ -141,40 +141,53 @@
 
 void CommonArtTestImpl::SetUpAndroidRootEnvVars() {
   if (IsHost()) {
-    // Look at how we were invoked to extract reasonable default paths.
-    std::string argv;
-    if (android::base::ReadFileToString("/proc/self/cmdline", &argv)) {
-      // /proc/self/cmdline is the programs 'argv' with elements delimited by '\0'.
-      std::filesystem::path path(argv.substr(0, argv.find('\0')));
-      path = std::filesystem::absolute(path);
-      // Walk up until we find the one of the well-known directories.
-      for (; path.parent_path() != path; path = path.parent_path()) {
-        // We are running tests from out/host/linux-x86 on developer machine.
-        if (path.filename() == std::filesystem::path("linux-x86")) {
-          char* cwd = getcwd(nullptr, 0);
-          setenv("ANDROID_BUILD_TOP", cwd, /*overwrite=*/ 0);  // No-op if already set.
-          free(cwd);
-          setenv("ANDROID_HOST_OUT", path.c_str(), /*overwrite=*/ 0);  // No-op if already set.
-          break;
+    // Make sure that ANDROID_BUILD_TOP is set. If not, set it from CWD.
+    const char* android_build_top_from_env = getenv("ANDROID_BUILD_TOP");
+    if (android_build_top_from_env == nullptr) {
+      // Not set by build server, so default to current directory.
+      char* cwd = getcwd(nullptr, 0);
+      setenv("ANDROID_BUILD_TOP", cwd, 1);
+      free(cwd);
+      android_build_top_from_env = getenv("ANDROID_BUILD_TOP");
+    }
+
+    const char* android_host_out_from_env = getenv("ANDROID_HOST_OUT");
+    if (android_host_out_from_env == nullptr) {
+      // Not set by build server, so default to the usual value of
+      // ANDROID_HOST_OUT.
+      std::string android_host_out;
+#if defined(__linux__)
+      // Fallback
+      android_host_out = std::string(android_build_top_from_env) + "/out/host/linux-x86";
+      // Look at how we were invoked
+      std::string argv;
+      if (android::base::ReadFileToString("/proc/self/cmdline", &argv)) {
+        // /proc/self/cmdline is the programs 'argv' with elements delimited by '\0'.
+        std::string cmdpath(argv.substr(0, argv.find('\0')));
+        std::filesystem::path path(cmdpath);
+        // If the path is relative then prepend the android_build_top_from_env to it
+        if (path.is_relative()) {
+          path = std::filesystem::path(android_build_top_from_env).append(cmdpath);
+          DCHECK(path.is_absolute()) << path;
         }
-        // We are running tests from testcases (extracted from zip) on tradefed.
-        if (path.filename() == std::filesystem::path("testcases")) {
-          path.append("art_common");
-          bool ok = chdir(path.c_str()) == 0;
-          CHECK(ok);
-          setenv("ANDROID_BUILD_TOP", path.c_str(), /*overwrite=*/ 0);  // No-op if already set.
-          path.append("out/host/linux-x86");
-          setenv("ANDROID_HOST_OUT", path.c_str(), /*overwrite=*/ 0);  // No-op if already set.
-          break;
+        // Walk up until we find the linux-x86 directory or we hit the root directory.
+        while (path.has_parent_path() && path.parent_path() != path &&
+               path.filename() != std::filesystem::path("linux-x86")) {
+          path = path.parent_path();
+        }
+        // If we found a linux-x86 directory path is now android_host_out
+        if (path.filename() == std::filesystem::path("linux-x86")) {
+          android_host_out = path.string();
         }
       }
+#elif defined(__APPLE__)
+      android_host_out = std::string(android_build_top_from_env) + "/out/host/darwin-x86";
+#else
+#error unsupported OS
+#endif
+      setenv("ANDROID_HOST_OUT", android_host_out.c_str(), 1);
+      android_host_out_from_env = getenv("ANDROID_HOST_OUT");
     }
-    const char* android_build_top_from_env = getenv("ANDROID_BUILD_TOP");
-    DCHECK(android_build_top_from_env != nullptr);
-    DCHECK(std::filesystem::exists(android_build_top_from_env)) << android_build_top_from_env;
-    const char* android_host_out_from_env = getenv("ANDROID_HOST_OUT");
-    DCHECK(android_host_out_from_env != nullptr);
-    DCHECK(std::filesystem::exists(android_host_out_from_env)) << android_host_out_from_env;
 
     // Environment variable ANDROID_ROOT is set on the device, but not
     // necessarily on the host.
diff --git a/libnativeloader/Android.bp b/libnativeloader/Android.bp
index 33a2c8f..5f29b4d 100644
--- a/libnativeloader/Android.bp
+++ b/libnativeloader/Android.bp
@@ -35,19 +35,6 @@
         "libbase",
     ],
     target: {
-        // Library search path needed for running host tests remotely (from testcases directory).
-        linux_glibc_x86: {
-            ldflags: [
-                "-Wl,-rpath,$ORIGIN/../art_common/out/host/linux-x86/lib",
-                "-Wl,--enable-new-dtags",
-            ],
-        },
-        linux_glibc_x86_64: {
-            ldflags: [
-                "-Wl,-rpath,$ORIGIN/../art_common/out/host/linux-x86/lib64",
-                "-Wl,--enable-new-dtags",
-            ],
-        },
         android: {
             srcs: [
                 "library_namespaces.cpp",
diff --git a/test/Android.bp b/test/Android.bp
index 6be4562..021a70f 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -54,11 +54,6 @@
         "art_defaults",
     ],
 
-    test_suites: ["general-tests"],
-    test_options: {
-        test_suite_tag: ["art-host-gtest"],
-    },
-
     shared_libs: [
         "libartd",
         "libartd-disassembler",