r330244 - [MinGW] Look for a cross sysroot relative to the clang binary

Martin Storsjo via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 18 01:47:26 PDT 2018


Author: mstorsjo
Date: Wed Apr 18 01:47:26 2018
New Revision: 330244

URL: http://llvm.org/viewvc/llvm-project?rev=330244&view=rev
Log:
[MinGW] Look for a cross sysroot relative to the clang binary

If found, prefer this over looking for a similar gcc later in the
system path.

Differential Revision: https://reviews.llvm.org/D45504

Modified:
    cfe/trunk/lib/Driver/ToolChains/MinGW.cpp
    cfe/trunk/lib/Driver/ToolChains/MinGW.h

Modified: cfe/trunk/lib/Driver/ToolChains/MinGW.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/MinGW.cpp?rev=330244&r1=330243&r2=330244&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains/MinGW.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains/MinGW.cpp Wed Apr 18 01:47:26 2018
@@ -275,7 +275,8 @@ void toolchains::MinGW::findGccLibDir()
   Archs.emplace_back(getTriple().getArchName());
   Archs[0] += "-w64-mingw32";
   Archs.emplace_back("mingw32");
-  Arch = Archs[0].str();
+  if (Arch.empty())
+    Arch = Archs[0].str();
   // lib: Arch Linux, Ubuntu, Windows
   // lib64: openSUSE Linux
   for (StringRef CandidateLib : {"lib", "lib64"}) {
@@ -302,6 +303,24 @@ llvm::ErrorOr<std::string> toolchains::M
   return make_error_code(std::errc::no_such_file_or_directory);
 }
 
+llvm::ErrorOr<std::string> toolchains::MinGW::findClangRelativeSysroot() {
+  llvm::SmallVector<llvm::SmallString<32>, 2> Subdirs;
+  Subdirs.emplace_back(getTriple().str());
+  Subdirs.emplace_back(getTriple().getArchName());
+  Subdirs[1] += "-w64-mingw32";
+  Twine ClangRoot =
+      llvm::sys::path::parent_path(getDriver().getInstalledDir()) +
+      llvm::sys::path::get_separator();
+  for (StringRef CandidateSubdir : Subdirs) {
+    Twine Subdir = ClangRoot + CandidateSubdir;
+    if (llvm::sys::fs::is_directory(Subdir)) {
+      Arch = CandidateSubdir;
+      return Subdir.str();
+    }
+  }
+  return make_error_code(std::errc::no_such_file_or_directory);
+}
+
 toolchains::MinGW::MinGW(const Driver &D, const llvm::Triple &Triple,
                          const ArgList &Args)
     : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) {
@@ -309,6 +328,10 @@ toolchains::MinGW::MinGW(const Driver &D
 
   if (getDriver().SysRoot.size())
     Base = getDriver().SysRoot;
+  // Look for <clang-bin>/../<triplet>; if found, use <clang-bin>/.. as the
+  // base as it could still be a base for a gcc setup with libgcc.
+  else if (llvm::ErrorOr<std::string> TargetSubdir = findClangRelativeSysroot())
+    Base = llvm::sys::path::parent_path(TargetSubdir.get());
   else if (llvm::ErrorOr<std::string> GPPName = findGcc())
     Base = llvm::sys::path::parent_path(
         llvm::sys::path::parent_path(GPPName.get()));

Modified: cfe/trunk/lib/Driver/ToolChains/MinGW.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/MinGW.h?rev=330244&r1=330243&r2=330244&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains/MinGW.h (original)
+++ cfe/trunk/lib/Driver/ToolChains/MinGW.h Wed Apr 18 01:47:26 2018
@@ -96,6 +96,7 @@ private:
   mutable std::unique_ptr<tools::gcc::Compiler> Compiler;
   void findGccLibDir();
   llvm::ErrorOr<std::string> findGcc();
+  llvm::ErrorOr<std::string> findClangRelativeSysroot();
 };
 
 } // end namespace toolchains




More information about the cfe-commits mailing list