[PATCH] D45504: [MinGW] Look for a cross sysroot relative to the clang binary

2018-04-18 Thread Martin Storsjö via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL330244: [MinGW] Look for a cross sysroot relative to the 
clang binary (authored by mstorsjo, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D45504?vs=141909=142897#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D45504

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


Index: cfe/trunk/lib/Driver/ToolChains/MinGW.h
===
--- cfe/trunk/lib/Driver/ToolChains/MinGW.h
+++ cfe/trunk/lib/Driver/ToolChains/MinGW.h
@@ -96,6 +96,7 @@
   mutable std::unique_ptr Compiler;
   void findGccLibDir();
   llvm::ErrorOr findGcc();
+  llvm::ErrorOr findClangRelativeSysroot();
 };
 
 } // end namespace toolchains
Index: cfe/trunk/lib/Driver/ToolChains/MinGW.cpp
===
--- cfe/trunk/lib/Driver/ToolChains/MinGW.cpp
+++ cfe/trunk/lib/Driver/ToolChains/MinGW.cpp
@@ -275,7 +275,8 @@
   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,13 +303,35 @@
   return make_error_code(std::errc::no_such_file_or_directory);
 }
 
+llvm::ErrorOr toolchains::MinGW::findClangRelativeSysroot() {
+  llvm::SmallVector, 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 , const llvm::Triple ,
  const ArgList )
 : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) {
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
   if (getDriver().SysRoot.size())
 Base = getDriver().SysRoot;
+  // Look for /../; if found, use /.. as the
+  // base as it could still be a base for a gcc setup with libgcc.
+  else if (llvm::ErrorOr TargetSubdir = 
findClangRelativeSysroot())
+Base = llvm::sys::path::parent_path(TargetSubdir.get());
   else if (llvm::ErrorOr GPPName = findGcc())
 Base = llvm::sys::path::parent_path(
 llvm::sys::path::parent_path(GPPName.get()));


Index: cfe/trunk/lib/Driver/ToolChains/MinGW.h
===
--- cfe/trunk/lib/Driver/ToolChains/MinGW.h
+++ cfe/trunk/lib/Driver/ToolChains/MinGW.h
@@ -96,6 +96,7 @@
   mutable std::unique_ptr Compiler;
   void findGccLibDir();
   llvm::ErrorOr findGcc();
+  llvm::ErrorOr findClangRelativeSysroot();
 };
 
 } // end namespace toolchains
Index: cfe/trunk/lib/Driver/ToolChains/MinGW.cpp
===
--- cfe/trunk/lib/Driver/ToolChains/MinGW.cpp
+++ cfe/trunk/lib/Driver/ToolChains/MinGW.cpp
@@ -275,7 +275,8 @@
   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,13 +303,35 @@
   return make_error_code(std::errc::no_such_file_or_directory);
 }
 
+llvm::ErrorOr toolchains::MinGW::findClangRelativeSysroot() {
+  llvm::SmallVector, 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 , const llvm::Triple ,
  const ArgList )
 : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) {
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
   if (getDriver().SysRoot.size())
 Base = getDriver().SysRoot;
+  // Look for /../; if found, use /.. as the
+  // base as it could still be a base for a gcc setup with libgcc.
+  else if 

[PATCH] D45504: [MinGW] Look for a cross sysroot relative to the clang binary

2018-04-17 Thread Martin Storsjö via Phabricator via cfe-commits
mstorsjo added a comment.

In https://reviews.llvm.org/D45504#1070198, @rnk wrote:

> Sorry, I skipped over the message and looked at the code, which seems pretty 
> straightforward.
>
> > Tests still are TBD, but posting this early to see if there's comments. 
> > (How do I easily do a test that checks something relative to the clang 
> > binary, since I don't control the location of the tested binary when 
> > running tests?)
>
> As much fun as it is to create sysroot subtrees in the clang test input 
> directories and test them with -###, I don't feel like they have that much 
> value. I've seen people write tests that do things like `mkdir -p %t/bin ... 
> cp %clang %t/bin/clang && %t/bin/clang -###`, but it's pretty expensive, and 
> the tests are hard to debug and modify.


Yeah, I also agree that's kinda overkill. Proceeding without tests for this 
then.


https://reviews.llvm.org/D45504



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D45504: [MinGW] Look for a cross sysroot relative to the clang binary

2018-04-17 Thread Reid Kleckner via Phabricator via cfe-commits
rnk added a comment.

Sorry, I skipped over the message and looked at the code, which seems pretty 
straightforward.

> Tests still are TBD, but posting this early to see if there's comments. (How 
> do I easily do a test that checks something relative to the clang binary, 
> since I don't control the location of the tested binary when running tests?)

As much fun as it is to create sysroot subtrees in the clang test input 
directories and test them with -###, I don't feel like they have that much 
value. I've seen people write tests that do things like `mkdir -p %t/bin ... cp 
%clang %t/bin/clang && %t/bin/clang -###`, but it's pretty expensive, and the 
tests are hard to debug and modify.


https://reviews.llvm.org/D45504



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D45504: [MinGW] Look for a cross sysroot relative to the clang binary

2018-04-17 Thread Reid Kleckner via Phabricator via cfe-commits
rnk accepted this revision.
rnk added a comment.
This revision is now accepted and ready to land.

lgtm


https://reviews.llvm.org/D45504



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D45504: [MinGW] Look for a cross sysroot relative to the clang binary

2018-04-17 Thread Martin Storsjö via Phabricator via cfe-commits
mstorsjo added a comment.

Ping


https://reviews.llvm.org/D45504



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D45504: [MinGW] Look for a cross sysroot relative to the clang binary

2018-04-12 Thread Martin Storsjö via Phabricator via cfe-commits
mstorsjo added a comment.

Ping, any comments on the change itself, or tips on how to create a test for it?


https://reviews.llvm.org/D45504



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D45504: [MinGW] Look for a cross sysroot relative to the clang binary

2018-04-10 Thread Martin Storsjö via Phabricator via cfe-commits
mstorsjo updated this revision to Diff 141909.
mstorsjo added a comment.

Fixed the hardcoded triplet suffix, previously I erroneously had a "-gcc" 
suffix there.


https://reviews.llvm.org/D45504

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


Index: lib/Driver/ToolChains/MinGW.h
===
--- lib/Driver/ToolChains/MinGW.h
+++ lib/Driver/ToolChains/MinGW.h
@@ -96,6 +96,7 @@
   mutable std::unique_ptr Compiler;
   void findGccLibDir();
   llvm::ErrorOr findGcc();
+  llvm::ErrorOr findClangRelativeSysroot();
 };
 
 } // end namespace toolchains
Index: lib/Driver/ToolChains/MinGW.cpp
===
--- lib/Driver/ToolChains/MinGW.cpp
+++ lib/Driver/ToolChains/MinGW.cpp
@@ -275,7 +275,8 @@
   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,13 +303,34 @@
   return make_error_code(std::errc::no_such_file_or_directory);
 }
 
+llvm::ErrorOr toolchains::MinGW::findClangRelativeSysroot() {
+  llvm::SmallVector, 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 , const llvm::Triple ,
  const ArgList )
 : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) {
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
   if (getDriver().SysRoot.size())
 Base = getDriver().SysRoot;
+  // Look for /../; if found, use /.. as the
+  // base as it could still be a base for a gcc setup with libgcc.
+  else if (llvm::ErrorOr TargetSubdir = 
findClangRelativeSysroot())
+Base = llvm::sys::path::parent_path(TargetSubdir.get());
   else if (llvm::ErrorOr GPPName = findGcc())
 Base = llvm::sys::path::parent_path(
 llvm::sys::path::parent_path(GPPName.get()));


Index: lib/Driver/ToolChains/MinGW.h
===
--- lib/Driver/ToolChains/MinGW.h
+++ lib/Driver/ToolChains/MinGW.h
@@ -96,6 +96,7 @@
   mutable std::unique_ptr Compiler;
   void findGccLibDir();
   llvm::ErrorOr findGcc();
+  llvm::ErrorOr findClangRelativeSysroot();
 };
 
 } // end namespace toolchains
Index: lib/Driver/ToolChains/MinGW.cpp
===
--- lib/Driver/ToolChains/MinGW.cpp
+++ lib/Driver/ToolChains/MinGW.cpp
@@ -275,7 +275,8 @@
   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,13 +303,34 @@
   return make_error_code(std::errc::no_such_file_or_directory);
 }
 
+llvm::ErrorOr toolchains::MinGW::findClangRelativeSysroot() {
+  llvm::SmallVector, 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 , const llvm::Triple ,
  const ArgList )
 : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) {
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
   if (getDriver().SysRoot.size())
 Base = getDriver().SysRoot;
+  // Look for /../; if found, use /.. as the
+  // base as it could still be a base for a gcc setup with libgcc.
+  else if (llvm::ErrorOr TargetSubdir = findClangRelativeSysroot())
+Base = llvm::sys::path::parent_path(TargetSubdir.get());
   else if (llvm::ErrorOr GPPName = findGcc())
 Base = llvm::sys::path::parent_path(
 llvm::sys::path::parent_path(GPPName.get()));
___
cfe-commits mailing list

[PATCH] D45504: [MinGW] Look for a cross sysroot relative to the clang binary

2018-04-10 Thread Martin Storsjö via Phabricator via cfe-commits
mstorsjo created this revision.
mstorsjo added reviewers: martell, rnk, compnerd, mati865, ismail, yaron.keren.

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

This implements what @martell suggested in https://reviews.llvm.org/D45152 in a 
much neater way.

Tests still are TBD, but posting this early to see if there's comments. (How do 
I easily do a test that checks something relative to the clang binary, since I 
don't control the location of the tested binary when running tests?)


Repository:
  rC Clang

https://reviews.llvm.org/D45504

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


Index: lib/Driver/ToolChains/MinGW.h
===
--- lib/Driver/ToolChains/MinGW.h
+++ lib/Driver/ToolChains/MinGW.h
@@ -96,6 +96,7 @@
   mutable std::unique_ptr Compiler;
   void findGccLibDir();
   llvm::ErrorOr findGcc();
+  llvm::ErrorOr findClangRelativeSysroot();
 };
 
 } // end namespace toolchains
Index: lib/Driver/ToolChains/MinGW.cpp
===
--- lib/Driver/ToolChains/MinGW.cpp
+++ lib/Driver/ToolChains/MinGW.cpp
@@ -275,7 +275,8 @@
   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,13 +303,34 @@
   return make_error_code(std::errc::no_such_file_or_directory);
 }
 
+llvm::ErrorOr toolchains::MinGW::findClangRelativeSysroot() {
+  llvm::SmallVector, 2> Subdirs;
+  Subdirs.emplace_back(getTriple().str());
+  Subdirs.emplace_back(getTriple().getArchName());
+  Subdirs[1] += "-w64-mingw32-gcc";
+  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 , const llvm::Triple ,
  const ArgList )
 : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) {
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
   if (getDriver().SysRoot.size())
 Base = getDriver().SysRoot;
+  // Look for /../; if found, use /.. as the
+  // base as it could still be a base for a gcc setup with libgcc.
+  else if (llvm::ErrorOr TargetSubdir = 
findClangRelativeSysroot())
+Base = llvm::sys::path::parent_path(TargetSubdir.get());
   else if (llvm::ErrorOr GPPName = findGcc())
 Base = llvm::sys::path::parent_path(
 llvm::sys::path::parent_path(GPPName.get()));


Index: lib/Driver/ToolChains/MinGW.h
===
--- lib/Driver/ToolChains/MinGW.h
+++ lib/Driver/ToolChains/MinGW.h
@@ -96,6 +96,7 @@
   mutable std::unique_ptr Compiler;
   void findGccLibDir();
   llvm::ErrorOr findGcc();
+  llvm::ErrorOr findClangRelativeSysroot();
 };
 
 } // end namespace toolchains
Index: lib/Driver/ToolChains/MinGW.cpp
===
--- lib/Driver/ToolChains/MinGW.cpp
+++ lib/Driver/ToolChains/MinGW.cpp
@@ -275,7 +275,8 @@
   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,13 +303,34 @@
   return make_error_code(std::errc::no_such_file_or_directory);
 }
 
+llvm::ErrorOr toolchains::MinGW::findClangRelativeSysroot() {
+  llvm::SmallVector, 2> Subdirs;
+  Subdirs.emplace_back(getTriple().str());
+  Subdirs.emplace_back(getTriple().getArchName());
+  Subdirs[1] += "-w64-mingw32-gcc";
+  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 , const llvm::Triple ,
  const ArgList )
 : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) {
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
   if (getDriver().SysRoot.size())
 Base = getDriver().SysRoot;
+  // Look for /../; if found, use /.. as the
+  // base as it could still be a base