lewis-revill created this revision.
lewis-revill added reviewers: asb, simoncook, edward-jones.
Herald added subscribers: cfe-commits, rkruppe, the_o, brucehoult, 
MartinMosbeck, rogfer01, mgrang, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, 
sabuasal, apazos, johnrusso, rbar.

Extends r338385 to allow the driver to compute the sysroot when an explicit 
path is not provided. This allows the linker to find C runtime files and the 
correct include directory for header files.


Repository:
  rC Clang

https://reviews.llvm.org/D50246

Files:
  lib/Driver/ToolChains/RISCV.cpp
  lib/Driver/ToolChains/RISCV.h


Index: lib/Driver/ToolChains/RISCV.h
===================================================================
--- lib/Driver/ToolChains/RISCV.h
+++ lib/Driver/ToolChains/RISCV.h
@@ -32,6 +32,9 @@
 
 protected:
   Tool *buildLinker() const override;
+
+private:
+  std::string computeSysRoot() const;
 };
 
 } // end namespace toolchains
Index: lib/Driver/ToolChains/RISCV.cpp
===================================================================
--- lib/Driver/ToolChains/RISCV.cpp
+++ lib/Driver/ToolChains/RISCV.cpp
@@ -13,6 +13,7 @@
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Options.h"
 #include "llvm/Option/ArgList.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -27,7 +28,7 @@
                                const ArgList &Args)
     : Generic_ELF(D, Triple, Args) {
   GCCInstallation.init(Triple, Args);
-  getFilePaths().push_back(D.SysRoot + "/lib");
+  getFilePaths().push_back(computeSysRoot() + "/lib");
   if (GCCInstallation.isValid()) {
     getFilePaths().push_back(GCCInstallation.getInstallPath().str());
     getProgramPaths().push_back(
@@ -45,24 +46,39 @@
     return;
 
   if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
-    SmallString<128> Dir(getDriver().SysRoot);
+    SmallString<128> Dir(computeSysRoot());
     llvm::sys::path::append(Dir, "include");
     addSystemInclude(DriverArgs, CC1Args, Dir.str());
   }
 }
 
 void RISCVToolChain::addLibStdCxxIncludePaths(
     const llvm::opt::ArgList &DriverArgs,
     llvm::opt::ArgStringList &CC1Args) const {
-  StringRef LibDir = GCCInstallation.getParentLibPath();
   const GCCVersion &Version = GCCInstallation.getVersion();
   StringRef TripleStr = GCCInstallation.getTriple().str();
   const Multilib &Multilib = GCCInstallation.getMultilib();
-  addLibStdCXXIncludePaths(
-      LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
+  addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
       "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
 }
 
+std::string RISCVToolChain::computeSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+    return getDriver().SysRoot;
+
+  if (!GCCInstallation.isValid())
+    return std::string();
+
+  StringRef LibDir = GCCInstallation.getParentLibPath();
+  StringRef TripleStr = GCCInstallation.getTriple().str();
+  std::string SysRootDir = LibDir.str() + "/../" + TripleStr.str();
+
+  if (!llvm::sys::fs::exists(SysRootDir))
+    return std::string();
+
+  return SysRootDir;
+}
+
 void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                  const InputInfo &Output,
                                  const InputInfoList &Inputs,


Index: lib/Driver/ToolChains/RISCV.h
===================================================================
--- lib/Driver/ToolChains/RISCV.h
+++ lib/Driver/ToolChains/RISCV.h
@@ -32,6 +32,9 @@
 
 protected:
   Tool *buildLinker() const override;
+
+private:
+  std::string computeSysRoot() const;
 };
 
 } // end namespace toolchains
Index: lib/Driver/ToolChains/RISCV.cpp
===================================================================
--- lib/Driver/ToolChains/RISCV.cpp
+++ lib/Driver/ToolChains/RISCV.cpp
@@ -13,6 +13,7 @@
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Options.h"
 #include "llvm/Option/ArgList.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -27,7 +28,7 @@
                                const ArgList &Args)
     : Generic_ELF(D, Triple, Args) {
   GCCInstallation.init(Triple, Args);
-  getFilePaths().push_back(D.SysRoot + "/lib");
+  getFilePaths().push_back(computeSysRoot() + "/lib");
   if (GCCInstallation.isValid()) {
     getFilePaths().push_back(GCCInstallation.getInstallPath().str());
     getProgramPaths().push_back(
@@ -45,24 +46,39 @@
     return;
 
   if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) {
-    SmallString<128> Dir(getDriver().SysRoot);
+    SmallString<128> Dir(computeSysRoot());
     llvm::sys::path::append(Dir, "include");
     addSystemInclude(DriverArgs, CC1Args, Dir.str());
   }
 }
 
 void RISCVToolChain::addLibStdCxxIncludePaths(
     const llvm::opt::ArgList &DriverArgs,
     llvm::opt::ArgStringList &CC1Args) const {
-  StringRef LibDir = GCCInstallation.getParentLibPath();
   const GCCVersion &Version = GCCInstallation.getVersion();
   StringRef TripleStr = GCCInstallation.getTriple().str();
   const Multilib &Multilib = GCCInstallation.getMultilib();
-  addLibStdCXXIncludePaths(
-      LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
+  addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
       "", TripleStr, "", "", Multilib.includeSuffix(), DriverArgs, CC1Args);
 }
 
+std::string RISCVToolChain::computeSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+    return getDriver().SysRoot;
+
+  if (!GCCInstallation.isValid())
+    return std::string();
+
+  StringRef LibDir = GCCInstallation.getParentLibPath();
+  StringRef TripleStr = GCCInstallation.getTriple().str();
+  std::string SysRootDir = LibDir.str() + "/../" + TripleStr.str();
+
+  if (!llvm::sys::fs::exists(SysRootDir))
+    return std::string();
+
+  return SysRootDir;
+}
+
 void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                  const InputInfo &Output,
                                  const InputInfoList &Inputs,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to