mirror of
https://github.com/llvm/llvm-project.git
synced 2025-05-13 12:36:07 +00:00
Introduce -nostdlib++ flag to disable linking the C++ standard library.
Projects that want to statically link their own C++ standard library currently need to pass -nostdlib or -nodefaultlibs, which also disables linking of the builtins library, -lm, and so on. Alternatively, they could use `clang` instead of `clang++`, but that already disables implicit addition of -lm on some toolchains. Add a dedicated flag -nostdlib++ that disables just linking of libc++ / libstdc++. This is analogous to -nostdinc++. https://reviews.llvm.org/D35780 llvm-svn: 308997
This commit is contained in:
parent
f5ecb5e1b4
commit
0ee47d92b2
@ -2144,6 +2144,7 @@ def nostdlibinc : Flag<["-"], "nostdlibinc">;
|
||||
def nostdincxx : Flag<["-"], "nostdinc++">, Flags<[CC1Option]>,
|
||||
HelpText<"Disable standard #include directories for the C++ standard library">;
|
||||
def nostdlib : Flag<["-"], "nostdlib">;
|
||||
def nostdlibxx : Flag<["-"], "nostdlib++">;
|
||||
def object : Flag<["-"], "object">;
|
||||
def o : JoinedOrSeparate<["-"], "o">, Flags<[DriverOption, RenderAsInput, CC1Option, CC1AsOption]>,
|
||||
HelpText<"Write output to <file>">, MetaVarName<"<file>">;
|
||||
|
@ -432,6 +432,10 @@ public:
|
||||
AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
|
||||
llvm::opt::ArgStringList &CC1Args) const;
|
||||
|
||||
/// Returns if the C++ standard library should be linked in.
|
||||
/// Note that e.g. -lm should still be linked even if this returns false.
|
||||
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const;
|
||||
|
||||
/// AddCXXStdlibLibArgs - Add the system specific linker arguments to use
|
||||
/// for the given C++ standard library type.
|
||||
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
|
||||
|
@ -648,8 +648,16 @@ void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
|
||||
DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
|
||||
}
|
||||
|
||||
bool ToolChain::ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const {
|
||||
return getDriver().CCCIsCXX() &&
|
||||
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
|
||||
options::OPT_nostdlibxx);
|
||||
}
|
||||
|
||||
void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
|
||||
ArgStringList &CmdArgs) const {
|
||||
assert(!Args.hasArg(options::OPT_nostdlibxx) &&
|
||||
"should not have called this");
|
||||
CXXStdlibType Type = GetCXXStdlibType(Args);
|
||||
|
||||
switch (Type) {
|
||||
|
@ -91,11 +91,10 @@ void ananas::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
|
||||
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
|
||||
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (D.CCCIsCXX())
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (ToolChain.ShouldLinkCXXStdlib(Args))
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
|
||||
CmdArgs.push_back("-lc");
|
||||
}
|
||||
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
|
||||
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
|
||||
|
@ -192,10 +192,9 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
options::OPT_e, options::OPT_s, options::OPT_t,
|
||||
options::OPT_Z_Flag, options::OPT_r});
|
||||
|
||||
if (TC.ShouldLinkCXXStdlib(Args))
|
||||
TC.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (C.getDriver().CCCIsCXX())
|
||||
TC.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
|
||||
CmdArgs.push_back("-lc");
|
||||
CmdArgs.push_back("-lm");
|
||||
|
||||
|
@ -80,9 +80,9 @@ void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
|
||||
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
|
||||
|
||||
if (ToolChain.ShouldLinkCXXStdlib(Args))
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (D.CCCIsCXX())
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
CmdArgs.push_back("-lc");
|
||||
CmdArgs.push_back("-lcompiler_rt");
|
||||
}
|
||||
|
@ -600,10 +600,12 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
|
||||
static void addLibFuzzerRuntime(const ToolChain &TC,
|
||||
const ArgList &Args,
|
||||
ArgStringList &CmdArgs) {
|
||||
StringRef ParentDir = llvm::sys::path::parent_path(TC.getDriver().InstalledDir);
|
||||
SmallString<128> P(ParentDir);
|
||||
llvm::sys::path::append(P, "lib", "libLLVMFuzzer.a");
|
||||
CmdArgs.push_back(Args.MakeArgString(P));
|
||||
StringRef ParentDir =
|
||||
llvm::sys::path::parent_path(TC.getDriver().InstalledDir);
|
||||
SmallString<128> P(ParentDir);
|
||||
llvm::sys::path::append(P, "lib", "libLLVMFuzzer.a");
|
||||
CmdArgs.push_back(Args.MakeArgString(P));
|
||||
if (!Args.hasArg(clang::driver::options::OPT_nostdlibxx))
|
||||
TC.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
}
|
||||
|
||||
|
@ -160,8 +160,7 @@ void tools::CrossWindows::Linker::ConstructJob(
|
||||
TC.AddFilePathLibArgs(Args, CmdArgs);
|
||||
AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
|
||||
|
||||
if (D.CCCIsCXX() && !Args.hasArg(options::OPT_nostdlib) &&
|
||||
!Args.hasArg(options::OPT_nodefaultlibs)) {
|
||||
if (TC.ShouldLinkCXXStdlib(Args)) {
|
||||
bool StaticCXX = Args.hasArg(options::OPT_static_libstdcxx) &&
|
||||
!Args.hasArg(options::OPT_static);
|
||||
if (StaticCXX)
|
||||
|
@ -549,10 +549,9 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
Args.MakeArgString(Twine("-threads=") + llvm::to_string(Parallelism)));
|
||||
}
|
||||
|
||||
if (getToolChain().ShouldLinkCXXStdlib(Args))
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (getToolChain().getDriver().CCCIsCXX())
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
|
||||
// link_ssp spec is empty.
|
||||
|
||||
// Let the tool chain choose which runtime library to link.
|
||||
|
@ -127,7 +127,8 @@ void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
}
|
||||
|
||||
if (D.CCCIsCXX()) {
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (getToolChain().ShouldLinkCXXStdlib(Args))
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
CmdArgs.push_back("-lm");
|
||||
}
|
||||
|
||||
|
@ -240,7 +240,8 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
addOpenMPRuntime(CmdArgs, ToolChain, Args);
|
||||
if (D.CCCIsCXX()) {
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (ToolChain.ShouldLinkCXXStdlib(Args))
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (Args.hasArg(options::OPT_pg))
|
||||
CmdArgs.push_back("-lm_p");
|
||||
else
|
||||
|
@ -107,13 +107,15 @@ void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
CmdArgs.push_back("-Bdynamic");
|
||||
|
||||
if (D.CCCIsCXX()) {
|
||||
bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
|
||||
!Args.hasArg(options::OPT_static);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bstatic");
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bdynamic");
|
||||
if (ToolChain.ShouldLinkCXXStdlib(Args)) {
|
||||
bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
|
||||
!Args.hasArg(options::OPT_static);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bstatic");
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bdynamic");
|
||||
}
|
||||
CmdArgs.push_back("-lm");
|
||||
}
|
||||
|
||||
|
@ -560,13 +560,15 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
|
||||
if (D.CCCIsCXX() &&
|
||||
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
|
||||
!Args.hasArg(options::OPT_static);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bstatic");
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bdynamic");
|
||||
if (ToolChain.ShouldLinkCXXStdlib(Args)) {
|
||||
bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
|
||||
!Args.hasArg(options::OPT_static);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bstatic");
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bdynamic");
|
||||
}
|
||||
CmdArgs.push_back("-lm");
|
||||
}
|
||||
// Silence warnings when linking C code with a C++ '-stdlib' argument.
|
||||
|
@ -248,7 +248,8 @@ constructHexagonLinkArgs(Compilation &C, const JobAction &JA,
|
||||
//----------------------------------------------------------------------------
|
||||
if (IncStdLib && IncDefLibs) {
|
||||
if (D.CCCIsCXX()) {
|
||||
HTC.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (HTC.ShouldLinkCXXStdlib(Args))
|
||||
HTC.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
CmdArgs.push_back("-lm");
|
||||
}
|
||||
|
||||
|
@ -185,8 +185,7 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
|
||||
// TODO: Add profile stuff here
|
||||
|
||||
if (D.CCCIsCXX() &&
|
||||
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (TC.ShouldLinkCXXStdlib(Args)) {
|
||||
bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
|
||||
!Args.hasArg(options::OPT_static);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
|
@ -72,7 +72,8 @@ void tools::minix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (D.CCCIsCXX()) {
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (getToolChain().ShouldLinkCXXStdlib(Args))
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
CmdArgs.push_back("-lm");
|
||||
}
|
||||
}
|
||||
|
@ -133,13 +133,15 @@ void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
|
||||
if (D.CCCIsCXX() &&
|
||||
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
bool OnlyLibstdcxxStatic =
|
||||
Args.hasArg(options::OPT_static_libstdcxx) && !IsStatic;
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bstatic");
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bdynamic");
|
||||
if (ToolChain.ShouldLinkCXXStdlib(Args)) {
|
||||
bool OnlyLibstdcxxStatic =
|
||||
Args.hasArg(options::OPT_static_libstdcxx) && !IsStatic;
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bstatic");
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (OnlyLibstdcxxStatic)
|
||||
CmdArgs.push_back("-Bdynamic");
|
||||
}
|
||||
CmdArgs.push_back("-lm");
|
||||
}
|
||||
|
||||
|
@ -278,7 +278,8 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
addOpenMPRuntime(CmdArgs, getToolChain(), Args);
|
||||
if (D.CCCIsCXX()) {
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (getToolChain().ShouldLinkCXXStdlib(Args))
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
CmdArgs.push_back("-lm");
|
||||
}
|
||||
if (NeedsSanitizerDeps)
|
||||
|
@ -177,7 +177,8 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (D.CCCIsCXX()) {
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (getToolChain().ShouldLinkCXXStdlib(Args))
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (Args.hasArg(options::OPT_pg))
|
||||
CmdArgs.push_back("-lm_p");
|
||||
else
|
||||
|
@ -227,7 +227,8 @@ static void ConstructGoldLinkJob(const Tool &T, Compilation &C,
|
||||
// libraries for both C and C++ compilations.
|
||||
CmdArgs.push_back("-lkernel");
|
||||
if (D.CCCIsCXX()) {
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (ToolChain.ShouldLinkCXXStdlib(Args))
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
if (Args.hasArg(options::OPT_pg))
|
||||
CmdArgs.push_back("-lm_p");
|
||||
else
|
||||
|
@ -100,7 +100,7 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
|
||||
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (getToolChain().getDriver().CCCIsCXX())
|
||||
if (getToolChain().ShouldLinkCXXStdlib(Args))
|
||||
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
CmdArgs.push_back("-lgcc_s");
|
||||
CmdArgs.push_back("-lc");
|
||||
|
@ -38,7 +38,6 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
const char *LinkingOutput) const {
|
||||
|
||||
const ToolChain &ToolChain = getToolChain();
|
||||
const Driver &D = ToolChain.getDriver();
|
||||
const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath());
|
||||
ArgStringList CmdArgs;
|
||||
CmdArgs.push_back("-flavor");
|
||||
@ -77,7 +76,7 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
||||
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
|
||||
|
||||
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
|
||||
if (D.CCCIsCXX())
|
||||
if (ToolChain.ShouldLinkCXXStdlib(Args))
|
||||
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
|
||||
|
||||
if (Args.hasArg(options::OPT_pthread))
|
||||
|
8
clang/test/Driver/nostdlibxx.cpp
Normal file
8
clang/test/Driver/nostdlibxx.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
// RUN: %clangxx -target i686-pc-linux-gnu -### -nostdlib++ %s 2> %t
|
||||
// RUN: FileCheck < %t %s
|
||||
|
||||
// We should still have -lm and the C standard libraries, but not -lstdc++.
|
||||
|
||||
// CHECK-NOT: -lstdc++
|
||||
// CHECK-NOT: -lc++
|
||||
// CHECK: -lm
|
Loading…
x
Reference in New Issue
Block a user