[PATCH][Solaris] Clang/Driver, stop hardcoding GCC paths in crt/ld.so lookup

Xan López via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 31 10:30:00 PDT 2015


Oops, missed this. Here is the updated patch.

And yes please, commit the patch for me.

Cheers,

Xan

On Mon, Aug 31, 2015 at 01:07:35PM -0400, Rafael Espíndola wrote:
> Do you have a version with the last suggestions? I lgtmed it conditional on
> it.   Do you need someone to commit it for you?
> On Aug 23, 2015 7:07 PM, "Rafael Espíndola" <rafael.espindola at gmail.com>
> wrote:
> 
> > SolarisScanLibDirForGCCTriple should start with a lower case. Starting
> > it with "scan" would probably also be more in line with the code
> > style.
> >
> > LGTM
> >
> > On 11 August 2015 at 16:33, Xan López <xan at igalia.com> wrote:
> > > Hi,
> > >
> > > thanks for the review, I was not even aware that this could be
> > > tested. Adding a test helped to fix me a couple extra issues (plus the
> > > one you already mentioned). New patch attached.
> > >
> > > Xan
> > >
> > > On Wed, Aug 05, 2015 at 09:14:30AM -0400, Rafael Espíndola wrote:
> > >> Please git-clang-format this patch.
> > >>
> > >> +  // /usr/gcc/<major>.<minor>/lib/gcc/<major>.<minor>.<patch>/,
> > >>
> > >> The code appends a triple after the "/lib/gcc". Is the comment missing
> > it?
> > >>
> > >> The inner loop has no version comparison. Are you depending on the
> > >> directory iteration order?
> > >>
> > >> Can you add a testcase?
> > >>
> > >>
> > >> On 28 July 2015 at 12:35, Xan López <xan at igalia.com> wrote:
> > >> > Here it is.
> > >> >
> > >> > On Tue, Jul 28, 2015 at 01:21:06PM +0200, Xan López wrote:
> > >> >> On Tue, Jul 28, 2015 at 01:55:23PM +0300, Yaron Keren wrote:
> > >> >> > +cfe-commits
> > >> >> >
> > >> >> > This is a very large Solaris special case in
> > ScanLibDirForGCCTriple which
> > >> >> > shares almost no code with the function.
> > >> >> > How about splitting it out to a helper function or
> > >> >> > making ScanLibDirForGCCTriple virtual and overriding on Solaris?
> > >> >>
> > >> >> Yep, at least a helper function makes sense, you are right. I'll send
> > >> >> another patch with either of those suggestions later today.
> > >> >>
> > >> >>
> > >> >> Xan
> > >> >> _______________________________________________
> > >> >> llvm-commits mailing list
> > >> >> llvm-commits at cs.uiuc.edu
> > >> >> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> > >> >
> > >> > _______________________________________________
> > >> > llvm-commits mailing list
> > >> > llvm-commits at cs.uiuc.edu
> > >> > http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> > >> >
> >
-------------- next part --------------
>From 750f40d3ae1c00e9eea1f44c5f0d4c32dd21d659 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Xan=20L=C3=B3pez?= <xan at igalia.com>
Date: Fri, 24 Jul 2015 19:17:51 +0200
Subject: [PATCH 1/4] [Solaris] Stop hardcoding the location of the c runtime
 files

This refactors the GCC installation detection to work properly on
Solaris (which is a bit of a special case compared to Linux
distributions), and adds the proper /usr/lib/... logic in the
Driver. That way we can just use ToolChain::GetFilePath to locate all
crt*.o files instead of hardcoding their location.
---
 lib/Driver/ToolChains.cpp                          | 106 +++++++++++++++++++--
 lib/Driver/ToolChains.h                            |   6 ++
 lib/Driver/Tools.cpp                               |  54 ++++-------
 .../4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crt1.o |   0
 .../lib/gcc/sparc-sun-solaris2.11/4.8.2/crtbegin.o |   0
 .../lib/gcc/sparc-sun-solaris2.11/4.8.2/crtend.o   |   0
 .../Inputs/sparc-sun-solaris2.11/usr/lib/crti.o    |   0
 .../Inputs/sparc-sun-solaris2.11/usr/lib/crtn.o    |   0
 .../Inputs/sparc-sun-solaris2.11/usr/lib/ld.so.1   |   0
 test/Driver/solaris-ld.c                           |  16 ++++
 10 files changed, 139 insertions(+), 43 deletions(-)
 create mode 100644 test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crt1.o
 create mode 100644 test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtbegin.o
 create mode 100644 test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtend.o
 create mode 100644 test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/crti.o
 create mode 100644 test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/crtn.o
 create mode 100644 test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/ld.so.1
 create mode 100644 test/Driver/solaris-ld.c

diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp
index 54201aa..a703a92 100644
--- a/lib/Driver/ToolChains.cpp
+++ b/lib/Driver/ToolChains.cpp
@@ -1340,9 +1340,21 @@ bool Generic_GCC::GCCInstallationDetector::getBiarchSibling(Multilib &M) const {
       "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
       "s390x-suse-linux", "s390x-redhat-linux"};
 
+  // Solaris.
+  static const char *const SolarisSPARCLibDirs[] = {"/gcc"};
+  static const char *const SolarisSPARCTriples[] = {"sparc-sun-solaris2.11",
+                                                    "i386-pc-solaris2.11"};
+
   using std::begin;
   using std::end;
 
+  if (TargetTriple.getOS() == llvm::Triple::Solaris) {
+    LibDirs.append(begin(SolarisSPARCLibDirs), end(SolarisSPARCLibDirs));
+    TripleAliases.append(begin(SolarisSPARCTriples), end(SolarisSPARCTriples));
+
+    return;
+  }
+
   switch (TargetTriple.getArch()) {
   case llvm::Triple::aarch64:
     LibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs));
@@ -1907,6 +1919,54 @@ static bool findBiarchMultilibs(const llvm::Triple &TargetTriple,
   return true;
 }
 
+void Generic_GCC::GCCInstallationDetector::scanLibDirForGCCTripleSolaris(
+    const llvm::Triple &TargetArch, const llvm::opt::ArgList &Args,
+    const std::string &LibDir, StringRef CandidateTriple,
+    bool NeedsBiarchSuffix) {
+  // Solaris is a special case. The GCC installation is under
+  // /usr/gcc/<major>.<minor>/lib/gcc/<triple>/<major>.<minor>.<patch>/, so we
+  // need to iterate twice.
+  std::error_code EC;
+  for (llvm::sys::fs::directory_iterator LI(LibDir, EC), LE; !EC && LI != LE;
+       LI = LI.increment(EC)) {
+    StringRef VersionText = llvm::sys::path::filename(LI->path());
+    GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
+
+    if (CandidateVersion.Major != -1) // Filter obviously bad entries.
+      if (!CandidateGCCInstallPaths.insert(LI->path()).second)
+        continue; // Saw this path before; no need to look at it again.
+    if (CandidateVersion.isOlderThan(4, 1, 1))
+      continue;
+    if (CandidateVersion <= Version)
+      continue;
+
+    GCCInstallPath =
+        LibDir + "/" + VersionText.str() + "/lib/gcc/" + CandidateTriple.str();
+    if (!llvm::sys::fs::exists(GCCInstallPath))
+      continue;
+
+    // If we make it here there has to be at least one GCC version, let's just
+    // use the latest one.
+    std::error_code EEC;
+    for (llvm::sys::fs::directory_iterator LLI(GCCInstallPath, EEC), LLE;
+         !EEC && LLI != LLE; LLI = LLI.increment(EEC)) {
+
+      StringRef SubVersionText = llvm::sys::path::filename(LLI->path());
+      GCCVersion CandidateSubVersion = GCCVersion::Parse(SubVersionText);
+
+      if (CandidateSubVersion > Version)
+        Version = CandidateSubVersion;
+    }
+
+    GCCTriple.setTriple(CandidateTriple);
+
+    GCCInstallPath += "/" + Version.Text;
+    GCCParentLibPath = GCCInstallPath + "/../../../../";
+
+    IsValid = true;
+  }
+}
+
 void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
     const llvm::Triple &TargetTriple, const ArgList &Args,
     const std::string &LibDir, StringRef CandidateTriple,
@@ -1936,6 +1996,12 @@ void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
       // triple.
       {"/i386-linux-gnu/gcc/" + CandidateTriple.str(), "/../../../.."}};
 
+  if (TargetTriple.getOS() == llvm::Triple::Solaris) {
+    scanLibDirForGCCTripleSolaris(TargetTriple, Args, LibDir, CandidateTriple,
+                                  NeedsBiarchSuffix);
+    return;
+  }
+
   // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
   const unsigned NumLibSuffixes = (llvm::array_lengthof(LibAndInstallSuffixes) -
                                    (TargetArch != llvm::Triple::x86));
@@ -2849,18 +2915,45 @@ Tool *Minix::buildAssembler() const {
 
 Tool *Minix::buildLinker() const { return new tools::minix::Linker(*this); }
 
+static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
+  if (llvm::sys::fs::exists(Path))
+    Paths.push_back(Path.str());
+}
+
 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
 
 Solaris::Solaris(const Driver &D, const llvm::Triple &Triple,
                  const ArgList &Args)
     : Generic_GCC(D, Triple, Args) {
 
-  getProgramPaths().push_back(getDriver().getInstalledDir());
+  GCCInstallation.init(D, Triple, Args);
+
+  path_list &Paths = getFilePaths();
+  if (GCCInstallation.isValid())
+    addPathIfExists(GCCInstallation.getInstallPath(), Paths);
+
+  addPathIfExists(getDriver().getInstalledDir(), Paths);
   if (getDriver().getInstalledDir() != getDriver().Dir)
-    getProgramPaths().push_back(getDriver().Dir);
+    addPathIfExists(getDriver().Dir, Paths);
 
-  getFilePaths().push_back(getDriver().Dir + "/../lib");
-  getFilePaths().push_back("/usr/lib");
+  addPathIfExists(getDriver().SysRoot + getDriver().Dir + "/../lib", Paths);
+
+  std::string LibPath = "/usr/lib/";
+  switch (Triple.getArch()) {
+  case llvm::Triple::x86:
+  case llvm::Triple::sparc:
+    break;
+  case llvm::Triple::x86_64:
+    LibPath += "amd64/";
+    break;
+  case llvm::Triple::sparcv9:
+    LibPath += "sparcv9/";
+    break;
+  default:
+    llvm_unreachable("Unsupported architecture");
+  }
+
+  addPathIfExists(getDriver().SysRoot + LibPath, Paths);
 }
 
 Tool *Solaris::buildAssembler() const {
@@ -3107,11 +3200,6 @@ static std::string getMultiarchTriple(const llvm::Triple &TargetTriple,
   return TargetTriple.str();
 }
 
-static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
-  if (llvm::sys::fs::exists(Path))
-    Paths.push_back(Path.str());
-}
-
 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
   if (isMipsArch(Triple.getArch())) {
     // lib32 directory has a special meaning on MIPS targets.
diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h
index fd941e1..cc6b4c1 100644
--- a/lib/Driver/ToolChains.h
+++ b/lib/Driver/ToolChains.h
@@ -145,6 +145,12 @@ public:
                                 const std::string &LibDir,
                                 StringRef CandidateTriple,
                                 bool NeedsBiarchSuffix = false);
+
+    void scanLibDirForGCCTripleSolaris(const llvm::Triple &TargetArch,
+                                       const llvm::opt::ArgList &Args,
+                                       const std::string &LibDir,
+                                       StringRef CandidateTriple,
+                                       bool NeedsBiarchSuffix = false);
   };
 
 protected:
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 099cab1..53c3c1b 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -6836,25 +6836,6 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                    const InputInfoList &Inputs,
                                    const ArgList &Args,
                                    const char *LinkingOutput) const {
-  // FIXME: Find a real GCC, don't hard-code versions here
-  std::string GCCLibPath = "/usr/gcc/4.5/lib/gcc/";
-  const llvm::Triple &T = getToolChain().getTriple();
-  std::string LibPath = "/usr/lib/";
-  const llvm::Triple::ArchType Arch = T.getArch();
-  switch (Arch) {
-  case llvm::Triple::x86:
-    GCCLibPath +=
-        ("i386-" + T.getVendorName() + "-" + T.getOSName()).str() + "/4.5.2/";
-    break;
-  case llvm::Triple::x86_64:
-    GCCLibPath += ("i386-" + T.getVendorName() + "-" + T.getOSName()).str();
-    GCCLibPath += "/4.5.2/amd64/";
-    LibPath += "amd64/";
-    break;
-  default:
-    llvm_unreachable("Unsupported architecture");
-  }
-
   ArgStringList CmdArgs;
 
   // Demangle C++ names in errors
@@ -6875,7 +6856,8 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
       CmdArgs.push_back("-shared");
     } else {
       CmdArgs.push_back("--dynamic-linker");
-      CmdArgs.push_back(Args.MakeArgString(LibPath + "ld.so.1"));
+      CmdArgs.push_back(
+          Args.MakeArgString(getToolChain().GetFilePath("ld.so.1")));
     }
   }
 
@@ -6888,21 +6870,24 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
   if (!Args.hasArg(options::OPT_nostdlib) &&
       !Args.hasArg(options::OPT_nostartfiles)) {
-    if (!Args.hasArg(options::OPT_shared)) {
-      CmdArgs.push_back(Args.MakeArgString(LibPath + "crt1.o"));
-      CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
-      CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
-      CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
-    } else {
-      CmdArgs.push_back(Args.MakeArgString(LibPath + "crti.o"));
-      CmdArgs.push_back(Args.MakeArgString(LibPath + "values-Xa.o"));
-      CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtbegin.o"));
-    }
+    if (!Args.hasArg(options::OPT_shared))
+      CmdArgs.push_back(
+          Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
+
+    CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
+    CmdArgs.push_back(
+        Args.MakeArgString(getToolChain().GetFilePath("values-Xa.o")));
+    CmdArgs.push_back(
+        Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
+
     if (getToolChain().getDriver().CCCIsCXX())
-      CmdArgs.push_back(Args.MakeArgString(LibPath + "cxa_finalize.o"));
+      CmdArgs.push_back(
+          Args.MakeArgString(getToolChain().GetFilePath("cxa_finalize.o")));
   }
 
-  CmdArgs.push_back(Args.MakeArgString("-L" + GCCLibPath));
+  const ToolChain::path_list &Paths = getToolChain().getFilePaths();
+  for (const auto &Path : Paths)
+    CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + Path));
 
   Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
                             options::OPT_e, options::OPT_r});
@@ -6923,9 +6908,10 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
   if (!Args.hasArg(options::OPT_nostdlib) &&
       !Args.hasArg(options::OPT_nostartfiles)) {
-    CmdArgs.push_back(Args.MakeArgString(GCCLibPath + "crtend.o"));
+    CmdArgs.push_back(
+        Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
   }
-  CmdArgs.push_back(Args.MakeArgString(LibPath + "crtn.o"));
+  CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
 
   addProfileRT(getToolChain(), Args, CmdArgs);
 
diff --git a/test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crt1.o b/test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crt1.o
new file mode 100644
index 0000000..e69de29
diff --git a/test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtbegin.o b/test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtbegin.o
new file mode 100644
index 0000000..e69de29
diff --git a/test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtend.o b/test/Driver/Inputs/sparc-sun-solaris2.11/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtend.o
new file mode 100644
index 0000000..e69de29
diff --git a/test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/crti.o b/test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/crti.o
new file mode 100644
index 0000000..e69de29
diff --git a/test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/crtn.o b/test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/crtn.o
new file mode 100644
index 0000000..e69de29
diff --git a/test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/ld.so.1 b/test/Driver/Inputs/sparc-sun-solaris2.11/usr/lib/ld.so.1
new file mode 100644
index 0000000..e69de29
diff --git a/test/Driver/solaris-ld.c b/test/Driver/solaris-ld.c
new file mode 100644
index 0000000..9590115
--- /dev/null
+++ b/test/Driver/solaris-ld.c
@@ -0,0 +1,16 @@
+// Test ld invocation on Solaris targets.
+
+// Check sparc-sun-solaris2.1
+// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
+// RUN:     --target=sparc-sun-solaris2.11 \
+// RUN:     --gcc-toolchain="" \
+// RUN:     --sysroot=%S/Inputs/sparc-sun-solaris2.11 \
+// RUN:   | FileCheck %s
+// CHECK: "-cc1" "-triple" "sparc-sun-solaris2.11"
+// CHECK: ld{{.*}}"
+// CHECK: "--dynamic-linker" "{{.*}}/usr/lib/ld.so.1"
+// CHECK: "{{.*}}/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crt1.o"
+// CHECK: "{{.*}}/usr/lib/crti.o"
+// CHECK: "{{.*}}/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtbegin.o"
+// CHECK: "{{.*}}/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2/crtend.o"
+// CHECK: "{{.*}}/usr/lib/crtn.o"
-- 
2.4.3



More information about the cfe-commits mailing list