Author: aemerson Date: Fri May 10 16:24:20 2019 New Revision: 360483 URL: http://llvm.org/viewvc/llvm-project?rev=360483&view=rev Log: [Darwin] Introduce a new flag, -fapple-link-rtlib that forces linking of the builtins library.
This driver flag is useful when users want to link against the compiler's builtins, but nothing else, and so use flags like -nostdlib. Darwin can't use -nolibc & nostdlib++ like other platforms on because we disable all runtime lib linking with -static, which we still want to have an option to link with the builtins. Differential Revision: https://reviews.llvm.org/D58320 Added: cfe/trunk/test/Driver/darwin-fapple-link-rtlib.c Modified: cfe/trunk/include/clang/Driver/Options.td cfe/trunk/lib/Driver/ToolChains/Darwin.cpp cfe/trunk/lib/Driver/ToolChains/Darwin.h Modified: cfe/trunk/include/clang/Driver/Options.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=360483&r1=360482&r2=360483&view=diff ============================================================================== --- cfe/trunk/include/clang/Driver/Options.td (original) +++ cfe/trunk/include/clang/Driver/Options.td Fri May 10 16:24:20 2019 @@ -1248,6 +1248,8 @@ def fno_fine_grained_bitfield_accesses : 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 fapple_link_rtlib : Flag<["-"], "fapple-link-rtlib">, 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>, Modified: cfe/trunk/lib/Driver/ToolChains/Darwin.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/Darwin.cpp?rev=360483&r1=360482&r2=360483&view=diff ============================================================================== --- cfe/trunk/lib/Driver/ToolChains/Darwin.cpp (original) +++ cfe/trunk/lib/Driver/ToolChains/Darwin.cpp Fri May 10 16:24:20 2019 @@ -593,15 +593,26 @@ void darwin::Linker::ConstructJob(Compil 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 @@ ToolChain::RuntimeLibType DarwinClang::G } 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 @@ void DarwinClang::AddLinkRuntimeLibArgs( // 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 @@ DerivedArgList *MachO::TranslateArgs(con } 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 } Modified: cfe/trunk/lib/Driver/ToolChains/Darwin.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/Darwin.h?rev=360483&r1=360482&r2=360483&view=diff ============================================================================== --- cfe/trunk/lib/Driver/ToolChains/Darwin.h (original) +++ cfe/trunk/lib/Driver/ToolChains/Darwin.h Fri May 10 16:24:20 2019 @@ -157,7 +157,8 @@ public: /// 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 @@ public: 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, Added: cfe/trunk/test/Driver/darwin-fapple-link-rtlib.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/darwin-fapple-link-rtlib.c?rev=360483&view=auto ============================================================================== --- cfe/trunk/test/Driver/darwin-fapple-link-rtlib.c (added) +++ cfe/trunk/test/Driver/darwin-fapple-link-rtlib.c Fri May 10 16:24:20 2019 @@ -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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits