Skip to content

Commit a594fd2

Browse files
committed
[Driver] Move detectLibcxxIncludePath to ToolChain
This helper method is useful even outside of Gnu toolchains, so move it to ToolChain so it can be reused in other toolchains such as Fuchsia. Differential Revision: https://reviews.llvm.org/D88452
1 parent f7e91e6 commit a594fd2

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

clang/include/clang/Driver/ToolChain.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,9 @@ class ToolChain {
575575
// given compilation arguments.
576576
virtual UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const;
577577

578+
// Detect the highest available version of libc++ in base path.
579+
virtual std::string detectLibcxxIncludePath(StringRef Base) const;
580+
578581
/// AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set
579582
/// the include paths to use for the given C++ standard library type.
580583
virtual void

clang/lib/Driver/ToolChain.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,29 @@ void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
924924
}
925925
}
926926

927+
std::string ToolChain::detectLibcxxIncludePath(StringRef Base) const {
928+
std::error_code EC;
929+
int MaxVersion = 0;
930+
std::string MaxVersionString;
931+
for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Base, EC), LE;
932+
!EC && LI != LE; LI = LI.increment(EC)) {
933+
StringRef VersionText = llvm::sys::path::filename(LI->path());
934+
int Version;
935+
if (VersionText[0] == 'v' &&
936+
!VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
937+
if (Version > MaxVersion) {
938+
MaxVersion = Version;
939+
MaxVersionString = std::string(VersionText);
940+
}
941+
}
942+
}
943+
if (!MaxVersion)
944+
return "";
945+
SmallString<128> P(Base);
946+
llvm::sys::path::append(P, MaxVersionString);
947+
return std::string(P.str());
948+
}
949+
927950
void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
928951
ArgStringList &CC1Args) const {
929952
// Header search paths should be handled by each of the subclasses.

clang/lib/Driver/ToolChains/Fuchsia.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
319319
switch (GetCXXStdlibType(DriverArgs)) {
320320
case ToolChain::CST_Libcxx: {
321321
SmallString<128> P(getDriver().Dir);
322-
llvm::sys::path::append(P, "..", "include", "c++", "v1");
323-
addSystemInclude(DriverArgs, CC1Args, P.str());
322+
llvm::sys::path::append(P, "..", "include", "c++");
323+
addSystemInclude(DriverArgs, CC1Args, detectLibcxxIncludePath(P.str()));
324324
break;
325325
}
326326

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,31 +2877,11 @@ void Generic_GCC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
28772877
}
28782878
}
28792879

2880-
static std::string DetectLibcxxIncludePath(llvm::vfs::FileSystem &vfs,
2881-
StringRef base) {
2882-
std::error_code EC;
2883-
int MaxVersion = 0;
2884-
std::string MaxVersionString;
2885-
for (llvm::vfs::directory_iterator LI = vfs.dir_begin(base, EC), LE;
2886-
!EC && LI != LE; LI = LI.increment(EC)) {
2887-
StringRef VersionText = llvm::sys::path::filename(LI->path());
2888-
int Version;
2889-
if (VersionText[0] == 'v' &&
2890-
!VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) {
2891-
if (Version > MaxVersion) {
2892-
MaxVersion = Version;
2893-
MaxVersionString = std::string(VersionText);
2894-
}
2895-
}
2896-
}
2897-
return MaxVersion ? (base + "/" + MaxVersionString).str() : "";
2898-
}
2899-
29002880
void
29012881
Generic_GCC::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
29022882
llvm::opt::ArgStringList &CC1Args) const {
29032883
auto AddIncludePath = [&](std::string Path) {
2904-
std::string IncludePath = DetectLibcxxIncludePath(getVFS(), Path);
2884+
std::string IncludePath = detectLibcxxIncludePath(Path);
29052885
if (IncludePath.empty() || !getVFS().exists(IncludePath))
29062886
return false;
29072887
addSystemInclude(DriverArgs, CC1Args, IncludePath);

0 commit comments

Comments
 (0)