Check for GCC paths that have the target triple in them.  This is required for a lot of cross-compile toolchains.  Also add some slightly better support for -B.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140645 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp
index 8cfe859..8b1c59a 100644
--- a/lib/Driver/ToolChains.cpp
+++ b/lib/Driver/ToolChains.cpp
@@ -1477,7 +1477,8 @@
   return UnknownDistro;
 }
 
-static std::string findGCCBaseLibDir(const std::string &GccTriple) {
+static std::string findGCCBaseLibDir(const Driver &D,
+    const std::string &GccTriple) {
   // FIXME: Using CXX_INCLUDE_ROOT is here is a bit of a hack, but
   // avoids adding yet another option to configure/cmake.
   // It would probably be cleaner to break it in two variables
@@ -1510,24 +1511,33 @@
                                       "4.2.4", "4.2.3", "4.2.2", "4.2.1",
                                       "4.2", "4.1.1"};
   bool Exists;
-  for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
-    std::string Suffix = GccTriple + "/" + GccVersions[i];
-    std::string t1 = "/usr/lib/gcc/" + Suffix;
-    if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists)
-      return t1;
-    std::string t2 = "/usr/lib64/gcc/" + Suffix;
-    if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists)
-      return t2;
-    std::string t3 = "/usr/lib/" + GccTriple + "/gcc/" + Suffix;
-    if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists)
-      return t3;
-    if (GccTriple == "i386-linux-gnu") {
-      // Ubuntu 11.04 uses an unusual path.
-      std::string t4 =
-          std::string("/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/") +
-          GccVersions[i];
-      if (!llvm::sys::fs::exists(t4 + "/crtbegin.o", Exists) && Exists)
-        return t4;
+  llvm::SmallVector<std::string, 8> Paths(D.PrefixDirs.begin(),
+      D.PrefixDirs.end());
+  Paths.push_back("/usr/");
+  const std::string *Triples[] = {&GccTriple, &D.DefaultHostTriple};
+  for (llvm::SmallVector<std::string, 8>::const_iterator it = Paths.begin(),
+       ie = Paths.end(); it != ie; ++it) {
+    for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
+      for (unsigned j = 0; j < sizeof(Triples)/sizeof(Triples[0]); ++j) {
+        std::string Suffix = *Triples[j] + "/" + GccVersions[i];
+        std::string t1 = *it + "gcc/" + Suffix;
+        if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists)
+          return t1;
+        std::string t2 = *it + "lib64/gcc/" + Suffix;
+        if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists)
+          return t2;
+        std::string t3 = *it + "lib/" + GccTriple + "/gcc/" + Suffix;
+        if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists)
+          return t3;
+        if (GccTriple == "i386-linux-gnu") {
+          // Ubuntu 11.04 uses an unusual path.
+          std::string t4 =
+              std::string(*it + "lib/i386-linux-gnu/gcc/i686-linux-gnu/") +
+              GccVersions[i];
+          if (!llvm::sys::fs::exists(t4 + "/crtbegin.o", Exists) && Exists)
+            return t4;
+        }
+      }
     }
   }
   return "";
@@ -1628,7 +1638,7 @@
       GccTriple = "powerpc64-unknown-linux-gnu";
   }
 
-  std::string Base = findGCCBaseLibDir(GccTriple);
+  std::string Base = findGCCBaseLibDir(getDriver(), GccTriple);
   path_list &Paths = getFilePaths();
   bool Is32Bits = (getArch() == llvm::Triple::x86 ||
                    getArch() == llvm::Triple::ppc);