Move GetAndroidToolsDir to common location.

Move the code which looks for the prebuilts directory
to CommonRuntimeTest and add test for it.

Change-Id: Id804de31c466656957fdd4b6a470f80a00477aed
diff --git a/compiler/utils/assembler_thumb_test.cc b/compiler/utils/assembler_thumb_test.cc
index a171e59..772fa9a 100644
--- a/compiler/utils/assembler_thumb_test.cc
+++ b/compiler/utils/assembler_thumb_test.cc
@@ -43,8 +43,6 @@
 static constexpr bool kPrintResults = false;
 #endif
 
-static const char* TOOL_PREFIX = "arm-linux-androideabi-";
-
 void SetAndroidData() {
   const char* data = getenv("ANDROID_DATA");
   if (data == nullptr) {
@@ -65,87 +63,6 @@
   return *s1 - *s2;
 }
 
-std::string GetAndroidToolsDir() {
-  std::string root;
-  const char* android_build_top = getenv("ANDROID_BUILD_TOP");
-  if (android_build_top != nullptr) {
-    root += android_build_top;
-  } else {
-    // Not set by build server, so default to current directory
-    char* cwd = getcwd(nullptr, 0);
-    setenv("ANDROID_BUILD_TOP", cwd, 1);
-    root += cwd;
-    free(cwd);
-  }
-
-  // Look for "prebuilts"
-  std::string toolsdir = root;
-  struct stat st;
-  while (toolsdir != "") {
-    std::string prebuilts = toolsdir + "/prebuilts";
-    if (stat(prebuilts.c_str(), &st) == 0) {
-       // Found prebuilts.
-       toolsdir += "/prebuilts/gcc/linux-x86/arm";
-       break;
-    }
-    // Not present, move up one dir.
-    size_t slash = toolsdir.rfind('/');
-    if (slash == std::string::npos) {
-      toolsdir = "";
-    } else {
-      toolsdir = toolsdir.substr(0, slash-1);
-    }
-  }
-  bool statok = stat(toolsdir.c_str(), &st) == 0;
-  if (!statok) {
-    return "";      // Use path.
-  }
-
-  DIR* dir = opendir(toolsdir.c_str());
-  if (dir == nullptr) {
-    return "";      // Use path.
-  }
-
-  struct dirent* entry;
-  std::string founddir;
-  double maxversion  = 0;
-
-  // Find the latest version of the arm-eabi tools (biggest version number).
-  // Suffix on toolsdir will be something like "arm-eabi-4.8"
-  while ((entry = readdir(dir)) != nullptr) {
-    std::string subdir = toolsdir + std::string("/") + std::string(entry->d_name);
-    size_t eabi = subdir.find(TOOL_PREFIX);
-    if (eabi != std::string::npos) {
-      // Check if "bin/{as,objcopy,objdump}" exist under this folder.
-      struct stat exec_st;
-      std::string exec_path;
-      exec_path = subdir + "/bin/" + TOOL_PREFIX + "as";
-      if (stat(exec_path.c_str(), &exec_st) != 0)
-        continue;
-      exec_path = subdir + "/bin/" + TOOL_PREFIX + "objcopy";
-      if (stat(exec_path.c_str(), &exec_st) != 0)
-        continue;
-      exec_path = subdir + "/bin/" + TOOL_PREFIX + "objdump";
-      if (stat(exec_path.c_str(), &exec_st) != 0)
-        continue;
-
-      std::string suffix = subdir.substr(eabi + strlen(TOOL_PREFIX));
-      double version = strtod(suffix.c_str(), nullptr);
-      if (version > maxversion) {
-        maxversion = version;
-        founddir = subdir;
-      }
-    }
-  }
-  closedir(dir);
-  bool found = founddir != "";
-  if (!found) {
-    return "";      // Use path.
-  }
-
-  return founddir + "/bin/";
-}
-
 void dump(std::vector<uint8_t>& code, const char* testname) {
   // This will only work on the host.  There is no as, objcopy or objdump on the
   // device.
@@ -155,7 +72,7 @@
 
   if (!results_ok) {
     setup_results();
-    toolsdir = GetAndroidToolsDir();
+    toolsdir = CommonRuntimeTest::GetAndroidTargetToolsDir(kThumb2);
     SetAndroidData();
     results_ok = true;
   }
@@ -187,19 +104,18 @@
   char cmd[1024];
 
   // Assemble the .S
-  snprintf(cmd, sizeof(cmd), "%s%sas %s -o %s.o", toolsdir.c_str(), TOOL_PREFIX, filename, filename);
+  snprintf(cmd, sizeof(cmd), "%sas %s -o %s.o", toolsdir.c_str(), filename, filename);
   system(cmd);
 
   // Remove the $d symbols to prevent the disassembler dumping the instructions
   // as .word
-  snprintf(cmd, sizeof(cmd), "%s%sobjcopy -N '$d' %s.o %s.oo", toolsdir.c_str(), TOOL_PREFIX,
-    filename, filename);
+  snprintf(cmd, sizeof(cmd), "%sobjcopy -N '$d' %s.o %s.oo", toolsdir.c_str(), filename, filename);
   system(cmd);
 
   // Disassemble.
 
-  snprintf(cmd, sizeof(cmd), "%s%sobjdump -d %s.oo | grep '^  *[0-9a-f][0-9a-f]*:'",
-    toolsdir.c_str(), TOOL_PREFIX, filename);
+  snprintf(cmd, sizeof(cmd), "%sobjdump -d %s.oo | grep '^  *[0-9a-f][0-9a-f]*:'",
+    toolsdir.c_str(), filename);
   if (kPrintResults) {
     // Print the results only, don't check. This is used to generate new output for inserting
     // into the .inc file.