Skip to content

Commit b50e8c5

Browse files
committed
[Driver] Introduce -stdlib++-isystem
There are times when we wish to explicitly control the C++ standard library search paths used by the driver. For example, when we're building against the Android NDK, we might want to use the NDK's C++ headers (which have a custom inline namespace) even if we have C++ headers installed next to the driver. We might also be building against a non-standard directory layout and wanting to specify the C++ standard library include directories explicitly. We could accomplish this by passing -nostdinc++ and adding an explicit -isystem for our custom search directories. However, users of our toolchain may themselves want to use -nostdinc++ and a custom C++ search path (libc++'s build does this, for example), and our added -isystem won't respect the -nostdinc++, leading to multiple C++ header directories on the search path, which causes build failures. Add a new driver option -stdlib++-isystem to support this use case. Passing this option suppresses adding the default C++ library include paths in the driver, and it also respects -nostdinc++ to allow users to still override the C++ library paths themselves. It's a bit unfortunate that we end up with both -stdlib++-isystem and -cxx-isystem, but their semantics differ significantly. -cxx-isystem is unaffected by -nostdinc++ and is added to the end of the search path (which is not appropriate for C++ standard library headers, since they often #include_next into other system headers), while -stdlib++-isystem respects -nostdinc++, is added to the beginning of the search path, and suppresses the default C++ library include paths. Differential Revision: https://reviews.llvm.org/D64089 llvm-svn: 367982
1 parent c37022b commit b50e8c5

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,10 @@ def std_EQ : Joined<["-", "--"], "std=">, Flags<[CC1Option]>,
26422642
}]>;
26432643
def stdlib_EQ : Joined<["-", "--"], "stdlib=">, Flags<[CC1Option]>,
26442644
HelpText<"C++ standard library to use">, Values<"libc++,libstdc++,platform">;
2645+
def stdlibxx_isystem : JoinedOrSeparate<["-"], "stdlib++-isystem">,
2646+
Group<clang_i_Group>,
2647+
HelpText<"Use directory as the C++ standard library include path">,
2648+
Flags<[DriverOption]>, MetaVarName<"<directory>">;
26452649
def unwindlib_EQ : Joined<["-", "--"], "unwindlib=">, Flags<[CC1Option]>,
26462650
HelpText<"Unwind library to use">, Values<"libgcc,unwindlib,platform">;
26472651
def sub__library : JoinedOrSeparate<["-"], "sub_library">;

clang/include/clang/Driver/ToolChain.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ class ToolChain {
542542
AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
543543
llvm::opt::ArgStringList &CC1Args) const;
544544

545+
/// AddClangCXXStdlibIsystemArgs - Add the clang -cc1 level arguments to set
546+
/// the specified include paths for the C++ standard library.
547+
void AddClangCXXStdlibIsystemArgs(const llvm::opt::ArgList &DriverArgs,
548+
llvm::opt::ArgStringList &CC1Args) const;
549+
545550
/// Returns if the C++ standard library should be linked in.
546551
/// Note that e.g. -lm should still be linked even if this returns false.
547552
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const;

clang/lib/Driver/ToolChain.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,16 @@ void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
832832
DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
833833
}
834834

835+
void ToolChain::AddClangCXXStdlibIsystemArgs(
836+
const llvm::opt::ArgList &DriverArgs,
837+
llvm::opt::ArgStringList &CC1Args) const {
838+
DriverArgs.ClaimAllArgs(options::OPT_stdlibxx_isystem);
839+
if (!DriverArgs.hasArg(options::OPT_nostdincxx))
840+
for (const auto &P :
841+
DriverArgs.getAllArgValues(options::OPT_stdlibxx_isystem))
842+
addSystemInclude(DriverArgs, CC1Args, P);
843+
}
844+
835845
bool ToolChain::ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const {
836846
return getDriver().CCCIsCXX() &&
837847
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,9 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
12351235
// Do not claim the argument so that the use of the argument does not
12361236
// silently go unnoticed on toolchains which do not honour the option.
12371237
continue;
1238+
} else if (A->getOption().matches(options::OPT_stdlibxx_isystem)) {
1239+
// Translated to -internal-isystem by the driver, no need to pass to cc1.
1240+
continue;
12381241
}
12391242

12401243
// Not translated, render as usual.
@@ -1289,11 +1292,15 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
12891292
// of an offloading programming model.
12901293

12911294
// Add C++ include arguments, if needed.
1292-
if (types::isCXX(Inputs[0].getType()))
1293-
forAllAssociatedToolChains(C, JA, getToolChain(),
1294-
[&Args, &CmdArgs](const ToolChain &TC) {
1295-
TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
1296-
});
1295+
if (types::isCXX(Inputs[0].getType())) {
1296+
bool HasStdlibxxIsystem = Args.hasArg(options::OPT_stdlibxx_isystem);
1297+
forAllAssociatedToolChains(
1298+
C, JA, getToolChain(),
1299+
[&Args, &CmdArgs, HasStdlibxxIsystem](const ToolChain &TC) {
1300+
HasStdlibxxIsystem ? TC.AddClangCXXStdlibIsystemArgs(Args, CmdArgs)
1301+
: TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
1302+
});
1303+
}
12971304

12981305
// Add system include arguments for all targets but IAMCU.
12991306
if (!IsIAMCU)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Backslash escaping makes matching against the installation directory fail on
2+
// Windows. Temporarily disable the test there until we add an option to print
3+
// the installation directory unescaped.
4+
// UNSUPPORTED: system-windows
5+
6+
// By default, we should search for libc++ next to the driver.
7+
// RUN: mkdir -p %t/bin
8+
// RUN: mkdir -p %t/include/c++/v1
9+
// RUN: %clang -target aarch64-linux-android -ccc-install-dir %t/bin \
10+
// RUN: -stdlib=libc++ -fsyntax-only %s -### 2>&1 | \
11+
// RUN: FileCheck -check-prefix=LIBCXX %s
12+
// RUN: %clang -target x86_64-apple-darwin -ccc-install-dir %t/bin \
13+
// RUN: -stdlib=libc++ -fsyntax-only %s -### 2>&1 | \
14+
// RUN: FileCheck -check-prefix=LIBCXX %s
15+
// LIBCXX: InstalledDir: [[INSTALLDIR:.+$]]
16+
// LIBCXX: "-internal-isystem" "[[INSTALLDIR]]/../include/c++/v1"
17+
18+
// Passing -stdlib++-isystem should suppress the default search.
19+
// RUN: %clang -target aarch64-linux-android -ccc-install-dir %t/bin \
20+
// RUN: -stdlib++-isystem /tmp/foo -stdlib++-isystem /tmp/bar -stdlib=libc++ \
21+
// RUN: -fsyntax-only %s -### 2>&1 | FileCheck -check-prefix=NODEFAULT %s
22+
// RUN: %clang -target x86_64-apple-darwin -ccc-install-dir %t/bin \
23+
// RUN: -stdlib++-isystem /tmp/foo -stdlib++-isystem /tmp/bar -stdlib=libc++ \
24+
// RUN: -fsyntax-only %s -### 2>&1 | FileCheck -check-prefix=NODEFAULT %s
25+
// NODEFAULT: InstalledDir: [[INSTALLDIR:.+$]]
26+
// NODEFAULT-NOT: "-internal-isystem" "[[INSTALLDIR]]/../include/c++/v1"
27+
28+
// And we should add it as an -internal-isystem.
29+
// RUN: %clang -target aarch64-linux-android -ccc-install-dir %t/bin \
30+
// RUN: -stdlib++-isystem /tmp/foo -stdlib++-isystem /tmp/bar -stdlib=libc++ \
31+
// RUN: -fsyntax-only %s -### 2>&1 | FileCheck -check-prefix=INCPATH %s
32+
// RUN: %clang -target x86_64-apple-darwin -ccc-install-dir %t/bin \
33+
// RUN: -stdlib++-isystem /tmp/foo -stdlib++-isystem /tmp/bar -stdlib=libc++ \
34+
// RUN: -fsyntax-only %s -### 2>&1 | FileCheck -check-prefix=INCPATH %s
35+
// INCPATH: "-internal-isystem" "/tmp/foo" "-internal-isystem" "/tmp/bar"
36+
37+
// We shouldn't pass the -stdlib++-isystem to cc1.
38+
// RUN: %clang -target aarch64-linux-android -ccc-install-dir %t/bin \
39+
// RUN: -stdlib++-isystem /tmp -stdlib=libc++ -fsyntax-only %s -### 2>&1 | \
40+
// RUN: FileCheck -check-prefix=NOCC1 %s
41+
// RUN: %clang -target x86_64-apple-darwin -ccc-install-dir %t/bin \
42+
// RUN: -stdlib++-isystem /tmp -stdlib=libc++ -fsyntax-only %s -### 2>&1 | \
43+
// RUN: FileCheck -check-prefix=NOCC1 %s
44+
// NOCC1-NOT: "-stdlib++-isystem" "/tmp"
45+
46+
// It should respect -nostdinc++.
47+
// RUN: %clang -target aarch64-linux-android -ccc-install-dir %t/bin \
48+
// RUN: -stdlib++-isystem /tmp/foo -stdlib++-isystem /tmp/bar -nostdinc++ \
49+
// RUN: -fsyntax-only %s -### 2>&1 | FileCheck -check-prefix=NOSTDINCXX %s
50+
// RUN: %clang -target x86_64-apple-darwin -ccc-install-dir %t/bin \
51+
// RUN: -stdlib++-isystem /tmp/foo -stdlib++-isystem /tmp/bar -nostdinc++ \
52+
// RUN: -fsyntax-only %s -### 2>&1 | FileCheck -check-prefix=NOSTDINCXX %s
53+
// NOSTDINCXX-NOT: "-internal-isystem" "/tmp/foo" "-internal-isystem" "/tmp/bar"

0 commit comments

Comments
 (0)