aemerson created this revision.
aemerson added reviewers: peter.smith, dexonsmith, ab.
aemerson added a project: clang.
Herald added subscribers: jdoerfert, jfb.
This driver flag is useful when users want to link against the compiler's
builtins, but nothing else, and so use flags like -nostdlib.
Implemented only for Darwin at the moment.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D58320
Files:
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Darwin.cpp
clang/lib/Driver/ToolChains/Darwin.h
clang/test/Driver/darwin-flink-builtins-rt.c
Index: clang/test/Driver/darwin-flink-builtins-rt.c
===================================================================
--- /dev/null
+++ clang/test/Driver/darwin-flink-builtins-rt.c
@@ -0,0 +1,6 @@
+// RUN: %clang -target arm64-apple-ios12.0 %s -nostdlib -flink-builtins-rt -### 2>&1 | FileCheck %s
+// RUN: %clang -target arm64-apple-ios12.0 %s -static -flink-builtins-rt -### 2>&1 | FileCheck %s
+// RUN: %clang -target arm64-apple-ios12.0 %s -flink-builtins-rt -### 2>&1 | FileCheck %s --check-prefix=DEFAULT
+// CHECK-NOT: "-lSystem"
+// DEFAULT: "-lSystem"
+// CHECK: libclang_rt.ios.a
Index: clang/lib/Driver/ToolChains/Darwin.h
===================================================================
--- clang/lib/Driver/ToolChains/Darwin.h
+++ clang/lib/Driver/ToolChains/Darwin.h
@@ -157,7 +157,8 @@
/// FIXME: This API is intended for use with embedded libraries only, and is
/// misleadingly named.
virtual void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args,
- llvm::opt::ArgStringList &CmdArgs) const;
+ llvm::opt::ArgStringList &CmdArgs,
+ bool ForceLinkBuiltinRT = false) const;
virtual void addStartObjectFileArgs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const {
@@ -495,7 +496,8 @@
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override;
void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args,
- llvm::opt::ArgStringList &CmdArgs) const override;
+ llvm::opt::ArgStringList &CmdArgs,
+ bool ForceLinkBuiltinRT = false) const override;
void AddClangCXXStdlibIncludeArgs(
const llvm::opt::ArgList &DriverArgs,
Index: clang/lib/Driver/ToolChains/Darwin.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Darwin.cpp
+++ clang/lib/Driver/ToolChains/Darwin.cpp
@@ -568,15 +568,26 @@
if (getToolChain().ShouldLinkCXXStdlib(Args))
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
- if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
+
+ bool NoStdOrDefaultLibs =
+ Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs);
+ bool ForceLinkBuiltins = Args.hasArg(options::OPT_flink_builtins_rt);
+ if (!NoStdOrDefaultLibs || ForceLinkBuiltins) {
// link_ssp spec is empty.
- // Let the tool chain choose which runtime library to link.
- getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
+ // If we have both -nostdlib/nodefaultlibs and -flink-builtins-rt then
+ // we just want to link the builtins, not the other libs like libSystem.
+ if (NoStdOrDefaultLibs && ForceLinkBuiltins) {
+ getMachOToolChain().AddLinkRuntimeLib(Args, CmdArgs, "builtins");
+ } else {
+ // Let the tool chain choose which runtime library to link.
+ getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs,
+ ForceLinkBuiltins);
- // No need to do anything for pthreads. Claim argument to avoid warning.
- Args.ClaimAllArgs(options::OPT_pthread);
- Args.ClaimAllArgs(options::OPT_pthreads);
+ // No need to do anything for pthreads. Claim argument to avoid warning.
+ Args.ClaimAllArgs(options::OPT_pthread);
+ Args.ClaimAllArgs(options::OPT_pthreads);
+ }
}
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
@@ -1074,7 +1085,8 @@
}
void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
- ArgStringList &CmdArgs) const {
+ ArgStringList &CmdArgs,
+ bool ForceLinkBuiltinRT) const {
// Call once to ensure diagnostic is printed if wrong value was specified
GetRuntimeLibType(Args);
@@ -1082,8 +1094,11 @@
// libraries with -static.
if (Args.hasArg(options::OPT_static) ||
Args.hasArg(options::OPT_fapple_kext) ||
- Args.hasArg(options::OPT_mkernel))
+ Args.hasArg(options::OPT_mkernel)) {
+ if (ForceLinkBuiltinRT)
+ AddLinkRuntimeLib(Args, CmdArgs, "builtins");
return;
+ }
// Reject -static-libgcc for now, we can deal with this when and if someone
// cares. This is useful in situations where someone wants to statically link
@@ -2054,7 +2069,8 @@
}
void MachO::AddLinkRuntimeLibArgs(const ArgList &Args,
- ArgStringList &CmdArgs) const {
+ ArgStringList &CmdArgs,
+ bool ForceLinkBuiltinRT) const {
// Embedded targets are simple at the moment, not supporting sanitizers and
// with different libraries for each member of the product { static, PIC } x
// { hard-float, soft-float }
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1239,6 +1239,8 @@
def flat__namespace : Flag<["-"], "flat_namespace">;
def flax_vector_conversions : Flag<["-"], "flax-vector-conversions">, Group<f_Group>;
def flimited_precision_EQ : Joined<["-"], "flimited-precision=">, Group<f_Group>;
+def flink_builtins_rt : Flag<["-"], "flink-builtins-rt">, Group<f_Group>,
+ HelpText<"Force linking the clang builtins runtime library">;
def flto_EQ : Joined<["-"], "flto=">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
HelpText<"Set LTO mode to either 'full' or 'thin'">, Values<"thin,full">;
def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group<f_Group>,
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits