Skip to content

Commit

Permalink
[clang] [MinGW] Fix paths on Gentoo
Browse files Browse the repository at this point in the history
There's code in clang/lib/Driver/ToolChains/Gnu.cpp for Clang to use Gentoo's include and lib paths, but this is missing for mingw, meaning that any C++ programs using the STL will fail to compile.

See https://bugs.gentoo.org/788430

Differential Revision: https://reviews.llvm.org/D111081
  • Loading branch information
maharmstone authored and mstorsjo committed Jul 7, 2022
1 parent 5812f9f commit 8e21802
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
28 changes: 24 additions & 4 deletions clang/lib/Driver/ToolChains/MinGW.cpp
Expand Up @@ -339,8 +339,9 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,

// Simplified from Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple.
static bool findGccVersion(StringRef LibDir, std::string &GccLibDir,
std::string &Ver) {
auto Version = toolchains::Generic_GCC::GCCVersion::Parse("0.0.0");
std::string &Ver,
toolchains::Generic_GCC::GCCVersion &Version) {
Version = toolchains::Generic_GCC::GCCVersion::Parse("0.0.0");
std::error_code EC;
for (llvm::sys::fs::directory_iterator LI(LibDir, EC), LE; !EC && LI != LE;
LI = LI.increment(EC)) {
Expand Down Expand Up @@ -371,7 +372,7 @@ void toolchains::MinGW::findGccLibDir() {
for (StringRef CandidateSysroot : SubdirNames) {
llvm::SmallString<1024> LibDir(Base);
llvm::sys::path::append(LibDir, CandidateLib, "gcc", CandidateSysroot);
if (findGccVersion(LibDir, GccLibDir, Ver)) {
if (findGccVersion(LibDir, GccLibDir, Ver, GccVer)) {
SubdirName = std::string(CandidateSysroot);
return;
}
Expand Down Expand Up @@ -438,6 +439,11 @@ toolchains::MinGW::MinGW(const Driver &D, const llvm::Triple &Triple,
getFilePaths().push_back(GccLibDir);
getFilePaths().push_back(
(Base + SubdirName + llvm::sys::path::get_separator() + "lib").str());

// Gentoo
getFilePaths().push_back(
(Base + SubdirName + llvm::sys::path::get_separator() + "mingw/lib").str());

getFilePaths().push_back(Base + "lib");
// openSUSE
getFilePaths().push_back(Base + SubdirName + "/sys-root/mingw/lib");
Expand Down Expand Up @@ -593,6 +599,11 @@ void toolchains::MinGW::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
addSystemInclude(DriverArgs, CC1Args,
Base + SubdirName + llvm::sys::path::get_separator() +
"include");

// Gentoo
addSystemInclude(DriverArgs, CC1Args,
Base + SubdirName + llvm::sys::path::get_separator() + "usr/include");

addSystemInclude(DriverArgs, CC1Args, Base + "include");
}

Expand Down Expand Up @@ -620,7 +631,7 @@ void toolchains::MinGW::AddClangCXXStdlibIncludeArgs(
}

case ToolChain::CST_Libstdcxx:
llvm::SmallVector<llvm::SmallString<1024>, 4> CppIncludeBases;
llvm::SmallVector<llvm::SmallString<1024>, 7> CppIncludeBases;
CppIncludeBases.emplace_back(Base);
llvm::sys::path::append(CppIncludeBases[0], SubdirName, "include", "c++");
CppIncludeBases.emplace_back(Base);
Expand All @@ -630,6 +641,15 @@ void toolchains::MinGW::AddClangCXXStdlibIncludeArgs(
llvm::sys::path::append(CppIncludeBases[2], "include", "c++", Ver);
CppIncludeBases.emplace_back(GccLibDir);
llvm::sys::path::append(CppIncludeBases[3], "include", "c++");
CppIncludeBases.emplace_back(GccLibDir);
llvm::sys::path::append(CppIncludeBases[4], "include",
"g++-v" + GccVer.Text);
CppIncludeBases.emplace_back(GccLibDir);
llvm::sys::path::append(CppIncludeBases[5], "include",
"g++-v" + GccVer.MajorStr + "." + GccVer.MinorStr);
CppIncludeBases.emplace_back(GccLibDir);
llvm::sys::path::append(CppIncludeBases[6], "include",
"g++-v" + GccVer.MajorStr);
for (auto &CppIncludeBase : CppIncludeBases) {
addSystemInclude(DriverArgs, CC1Args, CppIncludeBase);
CppIncludeBase += Slash;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Driver/ToolChains/MinGW.h
Expand Up @@ -103,6 +103,7 @@ class LLVM_LIBRARY_VISIBILITY MinGW : public ToolChain {

std::string Base;
std::string GccLibDir;
clang::driver::toolchains::Generic_GCC::GCCVersion GccVer;
std::string Ver;
std::string SubdirName;
mutable std::unique_ptr<tools::gcc::Preprocessor> Preprocessor;
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/mingw-sysroot.cpp
Expand Up @@ -22,6 +22,9 @@
// CHECK_TESTROOT_GCC: "-internal-isystem" "[[BASE:[^"]+]]/testroot-gcc{{/|\\\\}}lib{{/|\\\\}}gcc{{/|\\\\}}x86_64-w64-mingw32{{/|\\\\}}10.2-posix{{/|\\\\}}include{{/|\\\\}}c++"
// CHECK_TESTROOT_GCC-SAME: {{^}} "-internal-isystem" "[[BASE]]/testroot-gcc{{/|\\\\}}lib{{/|\\\\}}gcc{{/|\\\\}}x86_64-w64-mingw32{{/|\\\\}}10.2-posix{{/|\\\\}}include{{/|\\\\}}c++{{/|\\\\}}x86_64-w64-mingw32"
// CHECK_TESTROOT_GCC-SAME: {{^}} "-internal-isystem" "[[BASE]]/testroot-gcc{{/|\\\\}}lib{{/|\\\\}}gcc{{/|\\\\}}x86_64-w64-mingw32{{/|\\\\}}10.2-posix{{/|\\\\}}include{{/|\\\\}}c++{{/|\\\\}}backward"
// CHECK_TESTROOT_GCC: "-internal-isystem" "[[BASE]]/testroot-gcc{{/|\\\\}}lib{{/|\\\\}}gcc{{/|\\\\}}x86_64-w64-mingw32{{/|\\\\}}10.2-posix{{/|\\\\}}include{{/|\\\\}}g++-v10.2-posix"
// CHECK_TESTROOT_GCC: "-internal-isystem" "[[BASE]]/testroot-gcc{{/|\\\\}}lib{{/|\\\\}}gcc{{/|\\\\}}x86_64-w64-mingw32{{/|\\\\}}10.2-posix{{/|\\\\}}include{{/|\\\\}}g++-v10.2"
// CHECK_TESTROOT_GCC: "-internal-isystem" "[[BASE]]/testroot-gcc{{/|\\\\}}lib{{/|\\\\}}gcc{{/|\\\\}}x86_64-w64-mingw32{{/|\\\\}}10.2-posix{{/|\\\\}}include{{/|\\\\}}g++-v10"
// CHECK_TESTROOT_GCC: "-internal-isystem" "[[BASE]]/testroot-gcc{{/|\\\\}}x86_64-w64-mingw32{{/|\\\\}}include"


Expand Down
1 change: 1 addition & 0 deletions clang/test/Driver/mingw.cpp
@@ -1,5 +1,6 @@
// RUN: %clang -target i686-windows-gnu -rtlib=platform -c -### --sysroot=%S/Inputs/mingw_clang_tree/mingw32 %s 2>&1 | FileCheck -check-prefix=CHECK_MINGW_CLANG_TREE %s
// CHECK_MINGW_CLANG_TREE: "[[BASE:[^"]+]]/Inputs/mingw_clang_tree/mingw32{{/|\\\\}}i686-w64-mingw32{{/|\\\\}}include"
// CHECK_MINGW_CLANG_TREE: "[[BASE:[^"]+]]/Inputs/mingw_clang_tree/mingw32{{/|\\\\}}i686-w64-mingw32{{/|\\\\}}usr{{/|\\\\}}include"
// CHECK_MINGW_CLANG_TREE: "[[BASE]]/Inputs/mingw_clang_tree/mingw32{{/|\\\\}}include"


Expand Down

0 comments on commit 8e21802

Please sign in to comment.