Index: cfe/trunk/include/clang/Driver/Options.td =================================================================== --- cfe/trunk/include/clang/Driver/Options.td +++ cfe/trunk/include/clang/Driver/Options.td @@ -1248,6 +1248,8 @@ def flat__namespace : Flag<["-"], "flat_namespace">; def flax_vector_conversions : Flag<["-"], "flax-vector-conversions">, Group; def flimited_precision_EQ : Joined<["-"], "flimited-precision=">, Group; +def fapple_link_rtlib : Flag<["-"], "fapple-link-rtlib">, Group, + HelpText<"Force linking the clang builtins runtime library">; def flto_EQ : Joined<["-"], "flto=">, Flags<[CoreOption, CC1Option]>, Group, HelpText<"Set LTO mode to either 'full' or 'thin'">, Values<"thin,full">; def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group, Index: cfe/trunk/lib/Driver/ToolChains/Darwin.h =================================================================== --- cfe/trunk/lib/Driver/ToolChains/Darwin.h +++ cfe/trunk/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: cfe/trunk/lib/Driver/ToolChains/Darwin.cpp =================================================================== --- cfe/trunk/lib/Driver/ToolChains/Darwin.cpp +++ cfe/trunk/lib/Driver/ToolChains/Darwin.cpp @@ -593,15 +593,26 @@ if (getToolChain().ShouldLinkCXXStdlib(Args)) getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); - if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { - // link_ssp spec is empty. - // Let the tool chain choose which runtime library to link. - getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs); + bool NoStdOrDefaultLibs = + Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs); + bool ForceLinkBuiltins = Args.hasArg(options::OPT_fapple_link_rtlib); + if (!NoStdOrDefaultLibs || ForceLinkBuiltins) { + // link_ssp spec is empty. - // No need to do anything for pthreads. Claim argument to avoid warning. - Args.ClaimAllArgs(options::OPT_pthread); - Args.ClaimAllArgs(options::OPT_pthreads); + // If we have both -nostdlib/nodefaultlibs and -fapple-link-rtlib 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); + } } if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { @@ -1128,7 +1139,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); @@ -1136,8 +1148,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 @@ -2106,7 +2121,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: cfe/trunk/test/Driver/darwin-fapple-link-rtlib.c =================================================================== --- cfe/trunk/test/Driver/darwin-fapple-link-rtlib.c +++ cfe/trunk/test/Driver/darwin-fapple-link-rtlib.c @@ -0,0 +1,6 @@ +// RUN: %clang -target arm64-apple-ios12.0 %s -nostdlib -fapple-link-rtlib -resource-dir=%S/Inputs/resource_dir -### 2>&1 | FileCheck %s +// RUN: %clang -target arm64-apple-ios12.0 %s -static -fapple-link-rtlib -resource-dir=%S/Inputs/resource_dir -### 2>&1 | FileCheck %s +// RUN: %clang -target arm64-apple-ios12.0 %s -fapple-link-rtlib -resource-dir=%S/Inputs/resource_dir -### 2>&1 | FileCheck %s --check-prefix=DEFAULT +// CHECK-NOT: "-lSystem" +// DEFAULT: "-lSystem" +// CHECK: libclang_rt.ios.a