From d84a971ba917569829b51fff6057e5fd0d85e402 Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar@redhat.com>
Date: Thu, 18 Jan 2018 02:57:51 +0000
Subject: [PATCH] Driver: Prefer vendor supplied gcc toolchain

Summary:
This patch fixes an issue on Fedora where if you had the x86_64 cross
compiler installed on your x86_64 system, then clang would use that compiler
as the default toolchain.  This was happening because the cross compiler
is installed to /usr/lib/gcc/x86_64-linux-gnu/ and this directory comes before
the default compiler directory (/usr/lib/gcc/x86_64-redhat-linux/) in the search
list.

This patch re-orders the search list so that vendor supplied gcc toolchains
are selected before toolchains with a generic target, which should prevent
these kind of issues on other OSes too.

Subscribers: srhines, cfe-commits

Differential Revision: https://reviews.llvm.org/D42608
---
 lib/Driver/ToolChains/Gnu.cpp | 47 ++++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/lib/Driver/ToolChains/Gnu.cpp b/lib/Driver/ToolChains/Gnu.cpp
index 3755673..5a49a6e 100644
--- a/lib/Driver/ToolChains/Gnu.cpp
+++ b/lib/Driver/ToolChains/Gnu.cpp
@@ -1811,18 +1811,19 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
   // lifetime or initialization issues.
   static const char *const AArch64LibDirs[] = {"/lib64", "/lib"};
   static const char *const AArch64Triples[] = {
-      "aarch64-none-linux-gnu", "aarch64-linux-gnu", "aarch64-redhat-linux",
-      "aarch64-suse-linux"};
+      "aarch64-redhat-linux", "aarch64-suse-linux",
+      "aarch64-none-linux-gnu", "aarch64-linux-gnu"};
   static const char *const AArch64beLibDirs[] = {"/lib"};
   static const char *const AArch64beTriples[] = {"aarch64_be-none-linux-gnu",
                                                  "aarch64_be-linux-gnu"};
 
   static const char *const ARMLibDirs[] = {"/lib"};
   static const char *const ARMTriples[] = {"arm-linux-gnueabi"};
-  static const char *const ARMHFTriples[] = {"arm-linux-gnueabihf",
-                                             "armv7hl-redhat-linux-gnueabi",
+  static const char *const ARMHFTriples[] = {"armv7hl-redhat-linux-gnueabi",
                                              "armv6hl-suse-linux-gnueabi",
-                                             "armv7hl-suse-linux-gnueabi"};
+                                             "armv7hl-suse-linux-gnueabi",
+                                             "arm-linux-gnueabihf",
+  };
   static const char *const ARMebLibDirs[] = {"/lib"};
   static const char *const ARMebTriples[] = {"armeb-linux-gnueabi"};
   static const char *const ARMebHFTriples[] = {
@@ -1830,19 +1831,19 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
 
   static const char *const X86_64LibDirs[] = {"/lib64", "/lib"};
   static const char *const X86_64Triples[] = {
-      "x86_64-linux-gnu",       "x86_64-unknown-linux-gnu",
-      "x86_64-pc-linux-gnu",    "x86_64-redhat-linux6E",
-      "x86_64-redhat-linux",    "x86_64-suse-linux",
-      "x86_64-manbo-linux-gnu", "x86_64-linux-gnu",
-      "x86_64-slackware-linux", "x86_64-unknown-linux",
-      "x86_64-amazon-linux"};
+      "x86_64-redhat-linux6E",    "x86_64-redhat-linux",
+      "x86_64-suse-linux",        "x86_64-slackware-linux",
+      "x86_64-manbo-linux-gnu",   "x86_64-amazon-linux",
+      "x86_64-linux-gnu",         "x86_64-unknown-linux-gnu",
+      "x86_64-pc-linux-gnu",      "x86_64-linux-gnu",         
+      "x86_64-unknown-linux"};
   static const char *const X32LibDirs[] = {"/libx32"};
   static const char *const X86LibDirs[] = {"/lib32", "/lib"};
   static const char *const X86Triples[] = {
-      "i686-linux-gnu",       "i686-pc-linux-gnu",     "i486-linux-gnu",
-      "i386-linux-gnu",       "i386-redhat-linux6E",   "i686-redhat-linux",
-      "i586-redhat-linux",    "i386-redhat-linux",     "i586-suse-linux",
-      "i486-slackware-linux", "i686-montavista-linux", "i586-linux-gnu"};
+      "i386-redhat-linux6E",   "i686-redhat-linux", "i586-redhat-linux",
+      "i386-redhat-linux",     "i586-suse-linux",   "i486-slackware-linux",
+      "i686-montavista-linux", "i686-linux-gnu",    "i686-pc-linux-gnu",
+      "i486-linux-gnu",        "i386-linux-gnu",    "i586-linux-gnu"};
 
   static const char *const MIPSLibDirs[] = {"/lib"};
   static const char *const MIPSTriples[] = {"mips-linux-gnu", "mips-mti-linux",
@@ -1864,16 +1865,16 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
 
   static const char *const PPCLibDirs[] = {"/lib32", "/lib"};
   static const char *const PPCTriples[] = {
-      "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
-      "powerpc-suse-linux", "powerpc-montavista-linuxspe"};
+      "powerpc-suse-linux", "powerpc-montavista-linuxspe",
+      "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe"};
   static const char *const PPC64LibDirs[] = {"/lib64", "/lib"};
   static const char *const PPC64Triples[] = {
-      "powerpc64-linux-gnu", "powerpc64-unknown-linux-gnu",
-      "powerpc64-suse-linux", "ppc64-redhat-linux"};
+      "powerpc64-suse-linux", "ppc64-redhat-linux",
+      "powerpc64-linux-gnu", "powerpc64-unknown-linux-gnu"};
   static const char *const PPC64LELibDirs[] = {"/lib64", "/lib"};
   static const char *const PPC64LETriples[] = {
-      "powerpc64le-linux-gnu", "powerpc64le-unknown-linux-gnu",
-      "powerpc64le-suse-linux", "ppc64le-redhat-linux"};
+      "powerpc64le-suse-linux", "ppc64le-redhat-linux",
+      "powerpc64le-linux-gnu", "powerpc64le-unknown-linux-gnu"};
 
   static const char *const RISCV32LibDirs[] = {"/lib", "/lib32"};
   static const char *const RISCVTriples[] = {"riscv32-unknown-linux-gnu",
@@ -1889,8 +1890,8 @@ void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes(
 
   static const char *const SystemZLibDirs[] = {"/lib64", "/lib"};
   static const char *const SystemZTriples[] = {
-      "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
-      "s390x-suse-linux", "s390x-redhat-linux"};
+      "s390x-ibm-linux-gnu", "s390x-suse-linux", "s390x-redhat-linux",
+      "s390x-linux-gnu", "s390x-unknown-linux-gnu"};
 
 
   using std::begin;
-- 
1.8.3.1

