]> granicus.if.org Git - clang/commitdiff
[MinGW] Look for a cross sysroot relative to the clang binary
authorMartin Storsjo <martin@martin.st>
Wed, 18 Apr 2018 08:47:26 +0000 (08:47 +0000)
committerMartin Storsjo <martin@martin.st>
Wed, 18 Apr 2018 08:47:26 +0000 (08:47 +0000)
If found, prefer this over looking for a similar gcc later in the
system path.

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@330244 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Driver/ToolChains/MinGW.cpp
lib/Driver/ToolChains/MinGW.h

index 8e59fd126a06353e17d94c781b182b7379b25a38..312acde3bc1e7750130498542866ae0f40afbe3d 100644 (file)
@@ -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::MinGW::findGcc() {
   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, const llvm::Triple &Triple,
 
   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()));
index f8dbcae62756500acccecf59ef17a4b8994d45bd..0c3919d29f77bb1321f57c8e0653d6040688b05d 100644 (file)
@@ -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