From d3b79c2950d590cb272648f1c6a9dd7646f57beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Tue, 22 Nov 2022 14:39:39 +0200 Subject: [PATCH 2/2] [clang] [MinGW] Improve detection of libstdc++ headers on Fedora There's some variation in where different toolchain distributions (and linux distributions) package the mingw sysroots - this is so far handled by adding specific known subdirectory paths to the include and lib directory lists. There are multiple degrees of combinatorics involved here though; the distros may use different locations such as /usr/x86_64-w64-mingw32/include or /usr/x86_64-w64-mingw32/sys-root/mingw/include. So far, this setup has been treated as base=/usr, subdir=x86_64-w64-mingw32, and the driver tries to add further subdirectories such as //include, //sys-root/mingw/include. When it comes to libstdc++ (and libc++), each of these come with a large number of potential subdirectories. Instead of further exploding the combinatorics another step by adding all combinations of all paths, check whether //sys-root/mingw/include exists, and if it does, append that subpath into the subdir variable. This allows finding libstdc++ headers in e.g. /usr/x86_64-w64-mingw32/sys-root/mingw/include/c++/x86_64-w64-mingw32 on Fedora. The same logic (where everything belonging to this target fits under one expanded path, with just /include and /lib under it) doesn't seem to apply on Gentoo, where the includes are found in //usr/include while the libraries are in //mingw/lib (see 8e218026f8d5eabfdef9141ae5e26aa91d1933e6). But apparently the libstdc++ headers aren't installed under //usr/include, so that path hierarchy quirk doesn't need to be taken into account in AddClangCXXStdlibIncludeArgs. Differential Revision: https://reviews.llvm.org/D138693 --- clang/lib/Driver/ToolChains/MinGW.cpp | 19 +++++++++---------- clang/lib/Driver/ToolChains/MinGW.h | 1 + clang/test/Driver/mingw.cpp | 5 ++++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/clang/lib/Driver/ToolChains/MinGW.cpp b/clang/lib/Driver/ToolChains/MinGW.cpp index 0ebacea6e87f..2a9bf4b18f19 100644 --- a/clang/lib/Driver/ToolChains/MinGW.cpp +++ b/clang/lib/Driver/ToolChains/MinGW.cpp @@ -376,7 +376,7 @@ void toolchains::MinGW::findGccLibDir(const llvm::Triple &LiteralTriple) { SubdirNames.back() += "-w64-mingw32ucrt"; SubdirNames.emplace_back("mingw32"); if (SubdirName.empty()) { - SubdirName = getTriple().getArchName(); + SubdirName = getTriple().getArchName().str(); SubdirName += "-w64-mingw32"; } // lib: Arch Linux, Ubuntu, Windows @@ -459,9 +459,16 @@ toolchains::MinGW::MinGW(const Driver &D, const llvm::Triple &Triple, Base += llvm::sys::path::get_separator(); findGccLibDir(LiteralTriple); + TripleDirName = SubdirName; // GccLibDir must precede Base/lib so that the // correct crtbegin.o ,cetend.o would be found. getFilePaths().push_back(GccLibDir); + + // openSUSE/Fedora + std::string CandidateSubdir = SubdirName + "/sys-root/mingw"; + if (getDriver().getVFS().exists(Base + CandidateSubdir)) + SubdirName = CandidateSubdir; + getFilePaths().push_back( (Base + SubdirName + llvm::sys::path::get_separator() + "lib").str()); @@ -470,8 +477,6 @@ toolchains::MinGW::MinGW(const Driver &D, const llvm::Triple &Triple, (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"); NativeLLVMSupport = Args.getLastArgValue(options::OPT_fuse_ld_EQ, CLANG_DEFAULT_LINKER) @@ -621,12 +626,6 @@ void toolchains::MinGW::AddClangSystemIncludeArgs(const ArgList &DriverArgs, if (DriverArgs.hasArg(options::OPT_nostdlibinc)) return; - if (GetRuntimeLibType(DriverArgs) == ToolChain::RLT_Libgcc) { - // openSUSE - addSystemInclude(DriverArgs, CC1Args, - Base + SubdirName + "/sys-root/mingw/include"); - } - addSystemInclude(DriverArgs, CC1Args, Base + SubdirName + llvm::sys::path::get_separator() + "include"); @@ -684,7 +683,7 @@ void toolchains::MinGW::AddClangCXXStdlibIncludeArgs( for (auto &CppIncludeBase : CppIncludeBases) { addSystemInclude(DriverArgs, CC1Args, CppIncludeBase); CppIncludeBase += Slash; - addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + SubdirName); + addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + TripleDirName); addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + "backward"); } break; diff --git a/clang/lib/Driver/ToolChains/MinGW.h b/clang/lib/Driver/ToolChains/MinGW.h index bc17d273e776..ef9bfb29e175 100644 --- a/clang/lib/Driver/ToolChains/MinGW.h +++ b/clang/lib/Driver/ToolChains/MinGW.h @@ -106,6 +106,7 @@ private: clang::driver::toolchains::Generic_GCC::GCCVersion GccVer; std::string Ver; std::string SubdirName; + std::string TripleDirName; mutable std::unique_ptr Preprocessor; mutable std::unique_ptr Compiler; void findGccLibDir(const llvm::Triple &LiteralTriple); diff --git a/clang/test/Driver/mingw.cpp b/clang/test/Driver/mingw.cpp index bde952fb3565..46ea55b9500d 100644 --- a/clang/test/Driver/mingw.cpp +++ b/clang/test/Driver/mingw.cpp @@ -40,7 +40,10 @@ // RUN: %clang -target x86_64-pc-windows-gnu -rtlib=platform -stdlib=libstdc++ -c -### --sysroot=%S/Inputs/mingw_fedora_tree/usr %s 2>&1 | FileCheck -check-prefix=CHECK_MINGW_FEDORA_TREE %s -// CHECK_MINGW_FEDORA_TREE: "[[BASE:[^"]+]]/Inputs/mingw_fedora_tree/usr{{/|\\\\}}x86_64-w64-mingw32ucrt/sys-root/mingw/include" +// CHECK_MINGW_FEDORA_TREE: "[[BASE:[^"]+]]/Inputs/mingw_fedora_tree/usr{{/|\\\\}}x86_64-w64-mingw32ucrt/sys-root/mingw/include/c++" +// CHECK_MINGW_FEDORA_TREE: "[[BASE]]/Inputs/mingw_fedora_tree/usr{{/|\\\\}}x86_64-w64-mingw32ucrt/sys-root/mingw/include/c++/x86_64-w64-mingw32ucrt" +// CHECK_MINGW_FEDORA_TREE: "[[BASE]]/Inputs/mingw_fedora_tree/usr{{/|\\\\}}x86_64-w64-mingw32ucrt/sys-root/mingw/include/c++/backward" +// CHECK_MINGW_FEDORA_TREE: "[[BASE]]/Inputs/mingw_fedora_tree/usr{{/|\\\\}}x86_64-w64-mingw32ucrt/sys-root/mingw/include" // RUN: %clang -target i686-pc-windows-gnu -rtlib=platform -stdlib=libstdc++ -c -### --sysroot=%S/Inputs/mingw_arch_tree/usr %s 2>&1 | FileCheck -check-prefix=CHECK_MINGW_ARCH_TREE %s -- 2.38.1