Blame SOURCES/0004-PATCH-clang-Prefer-gcc-toolchains-with-libgcc_s.so-w.patch

e3fa52
From d8af49687765744efaae7ba0f0c4c0fcd58a0e31 Mon Sep 17 00:00:00 2001
292815
From: serge-sans-paille <sguelton@redhat.com>
292815
Date: Wed, 23 Sep 2020 12:47:30 +0000
e3fa52
Subject: [PATCH 4/6] [PATCH][clang] Prefer gcc toolchains with libgcc_s.so
e3fa52
 when not static linking libgcc
292815
292815
Fedora ships cross-compilers on all platforms, so a user could end up
292815
with a gcc x86_64 cross-compiler installed on an x86_64 system. clang
292815
maintains a list of supported triples for each target and when all
292815
else is equal will prefer toolchains with triples that appear earlier
292815
in the list.
292815
292815
The cross-compiler triple on Fedora is x86_64-linux-gnu and this comes
292815
before the Fedora system compiler's triple: x86_64-redhat-linux in
292815
the triples list, so the cross compiler is always preferred. This
292815
is a problem, because the cross compiler is missing libraries, like
292815
libgcc_s.so, that clang expects to be there so linker invocations
292815
will fail.
292815
292815
This patch fixes this by checking for the existence of libgcc_s.so
292815
when it is required and taking that into account when selecting a
292815
toolchain.
292815
---
e3fa52
 clang/lib/Driver/ToolChains/Gnu.cpp                      | 16 ++++++++++++++--
e3fa52
 clang/lib/Driver/ToolChains/Gnu.h                        |  4 +++-
e3fa52
 .../usr/lib/gcc/x86_64-linux-gnu/7/crtbegin.o            |  0
e3fa52
 .../usr/lib/gcc/x86_64-redhat-linux/7/crtbegin.o         |  0
e3fa52
 .../usr/lib/gcc/x86_64-redhat-linux/7/libgcc_s.so        |  0
e3fa52
 clang/test/Driver/linux-ld.c                             | 12 ++++++++++++
292815
 6 files changed, 29 insertions(+), 3 deletions(-)
e3fa52
 create mode 100644 clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-linux-gnu/7/crtbegin.o
e3fa52
 create mode 100644 clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-redhat-linux/7/crtbegin.o
e3fa52
 create mode 100644 clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-redhat-linux/7/libgcc_s.so
292815
e3fa52
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
e3fa52
index 5deeb10..5d51517 100644
e3fa52
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
e3fa52
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
e3fa52
@@ -2539,6 +2539,8 @@ void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
292815
        (TargetArch == llvm::Triple::x86 &&
292815
         TargetTriple.getOS() != llvm::Triple::Solaris)}};
292815
 
292815
+  bool NeedLibgccShared = !Args.hasArg(options::OPT_static_libgcc) &&
292815
+                          !Args.hasArg(options::OPT_static);
292815
   for (auto &Suffix : Suffixes) {
292815
     if (!Suffix.Active)
292815
       continue;
e3fa52
@@ -2556,8 +2558,17 @@ void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
292815
           continue; // Saw this path before; no need to look at it again.
292815
       if (CandidateVersion.isOlderThan(4, 1, 1))
292815
         continue;
292815
-      if (CandidateVersion <= Version)
292815
-        continue;
292815
+
292815
+      bool CandidateHasLibGccShared = false;
292815
+      if (CandidateVersion <= Version) {
292815
+        if (NeedLibgccShared && !HasLibGccShared) {
292815
+          CandidateHasLibGccShared =
292815
+                D.getVFS().exists(LI->path() + "/libgcc_s.so");
292815
+
292815
+        }
292815
+        if (HasLibGccShared || !CandidateHasLibGccShared)
292815
+          continue;
292815
+      }
292815
 
292815
       if (!ScanGCCForMultilibs(TargetTriple, Args, LI->path(),
292815
                                NeedsBiarchSuffix))
e3fa52
@@ -2571,6 +2582,7 @@ void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
292815
       GCCInstallPath = (LibDir + "/" + LibSuffix + "/" + VersionText).str();
292815
       GCCParentLibPath = (GCCInstallPath + "/../" + Suffix.ReversePath).str();
292815
       IsValid = true;
292815
+      HasLibGccShared = CandidateHasLibGccShared;
292815
     }
292815
   }
292815
 }
e3fa52
diff --git a/clang/lib/Driver/ToolChains/Gnu.h b/clang/lib/Driver/ToolChains/Gnu.h
e3fa52
index 90d3baf..9d0cea2 100644
e3fa52
--- a/clang/lib/Driver/ToolChains/Gnu.h
e3fa52
+++ b/clang/lib/Driver/ToolChains/Gnu.h
292815
@@ -190,6 +190,7 @@ public:
292815
   /// Driver, and has logic for fuzzing that where appropriate.
292815
   class GCCInstallationDetector {
292815
     bool IsValid;
292815
+    bool HasLibGccShared;
292815
     llvm::Triple GCCTriple;
292815
     const Driver &D;
292815
 
e3fa52
@@ -216,7 +217,8 @@ public:
e3fa52
     const std::string GentooConfigDir = "/etc/env.d/gcc";
292815
 
292815
   public:
292815
-    explicit GCCInstallationDetector(const Driver &D) : IsValid(false), D(D) {}
292815
+    explicit GCCInstallationDetector(const Driver &D)
292815
+        : IsValid(false), HasLibGccShared(false), D(D) {}
292815
     void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args,
292815
               ArrayRef<std::string> ExtraTripleAliases = None);
292815
 
e3fa52
diff --git a/clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-linux-gnu/7/crtbegin.o b/clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-linux-gnu/7/crtbegin.o
292815
new file mode 100644
e3fa52
index 0000000..e69de29
e3fa52
diff --git a/clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-redhat-linux/7/crtbegin.o b/clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-redhat-linux/7/crtbegin.o
292815
new file mode 100644
e3fa52
index 0000000..e69de29
e3fa52
diff --git a/clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-redhat-linux/7/libgcc_s.so b/clang/test/Driver/Inputs/fedora_28_tree/usr/lib/gcc/x86_64-redhat-linux/7/libgcc_s.so
292815
new file mode 100644
e3fa52
index 0000000..e69de29
e3fa52
diff --git a/clang/test/Driver/linux-ld.c b/clang/test/Driver/linux-ld.c
e3fa52
index 24d3c78..071bb9b 100644
e3fa52
--- a/clang/test/Driver/linux-ld.c
e3fa52
+++ b/clang/test/Driver/linux-ld.c
292815
@@ -784,6 +784,18 @@
292815
 // CHECK-FEDORA-31-RISCV64: "{{.*}}/usr/lib/gcc/riscv64-redhat-linux/9{{/|\\\\}}crtend.o"
292815
 // CHECK-FEDORA-31-RISCV64: "{{.*}}/usr/lib/gcc/riscv64-redhat-linux/9{{/|\\\\}}crtn.o"
292815
 //
292815
+// Check that clang does not select the cross compiler by default on Fedora 28.
292815
+//
292815
+// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
292815
+// RUN:     --target=x86_64-unknown-linux-gnu \
292815
+// RUN:     --gcc-toolchain="" \
292815
+// RUN:     --sysroot=%S/Inputs/fedora_28_tree \
292815
+// RUN:   | FileCheck --check-prefix=CHECK-FEDORA-28-X86_64 %s
292815
+//
292815
+// CHECK-FEDORA-28-X86_64: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
292815
+// CHECK-FEDORA-28-X86_64: "[[SYSROOT]]/usr/lib/gcc/x86_64-redhat-linux/7/crtbegin.o"
292815
+// CHECK-FEDORA-28-X86_64: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-redhat-linux/7"
292815
+//
292815
 // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
292815
 // RUN:     --target=arm-unknown-linux-gnueabi -rtlib=platform \
292815
 // RUN:     --gcc-toolchain="" \
292815
-- 
e3fa52
1.8.3.1
292815