blob: 751c9f805eafb4b56bbe0211c015441b35d58f7b [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChain.cpp - Collections of tools for one platform ------------===//
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Driver/ToolChain.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000011#include "clang/Basic/ObjCRuntime.h"
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000012#include "clang/Driver/Action.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000016#include "clang/Driver/DriverDiagnostic.h"
Benjamin Kramere20e5082012-10-04 19:42:20 +000017#include "clang/Driver/Option.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000018#include "clang/Driver/Options.h"
Bob Wilson57f6d192012-03-21 17:19:12 +000019#include "llvm/ADT/StringSwitch.h"
John McCall9f084a32011-07-06 00:26:06 +000020#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000021using namespace clang::driver;
Chris Lattner5f9e2722011-07-23 10:55:15 +000022using namespace clang;
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000023
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000024ToolChain::ToolChain(const Driver &D, const llvm::Triple &T)
25 : D(D), Triple(T) {
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000026}
27
28ToolChain::~ToolChain() {
29}
30
Daniel Dunbaree788e72009-12-21 18:54:17 +000031const Driver &ToolChain::getDriver() const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000032 return D;
Daniel Dunbaree788e72009-12-21 18:54:17 +000033}
34
Daniel Dunbard2a527e2012-11-08 03:38:26 +000035std::string ToolChain::getDefaultUniversalArchName() const {
36 // In universal driver terms, the arch name accepted by -arch isn't exactly
37 // the same as the ones that appear in the triple. Roughly speaking, this is
38 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
39 // only interesting special case is powerpc.
40 switch (Triple.getArch()) {
41 case llvm::Triple::ppc:
42 return "ppc";
43 case llvm::Triple::ppc64:
44 return "ppc64";
45 default:
46 return Triple.getArchName();
47 }
48}
49
Rafael Espindola03a86382012-09-23 03:05:41 +000050bool ToolChain::IsUnwindTablesDefault() const {
51 return false;
52}
53
Daniel Dunbar4a7e8892010-07-14 18:46:23 +000054std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000055 return D.GetFilePath(Name, *this);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000057}
58
Simon Atanasyanfc44e882012-10-03 19:52:37 +000059std::string ToolChain::GetProgramPath(const char *Name) const {
60 return D.GetProgramPath(Name, *this);
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000061}
Daniel Dunbar41800112010-08-02 05:43:56 +000062
63types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
64 return types::lookupTypeForExtension(Ext);
65}
Daniel Dunbar00577ad2010-08-23 22:35:37 +000066
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000067bool ToolChain::HasNativeLLVMSupport() const {
68 return false;
69}
70
John McCall260611a2012-06-20 06:18:46 +000071ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnall11d3f4c2012-07-03 20:49:52 +000072 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall260611a2012-06-20 06:18:46 +000073 VersionTuple());
John McCall9f084a32011-07-06 00:26:06 +000074}
75
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000076/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar00577ad2010-08-23 22:35:37 +000077//
78// FIXME: tblgen this.
79static const char *getARMTargetCPU(const ArgList &Args,
80 const llvm::Triple &Triple) {
Bob Wilson677a35b2012-03-21 16:31:37 +000081 // For Darwin targets, the -arch option (which is translated to a
82 // corresponding -march option) should determine the architecture
83 // (and the Mach-O slice) regardless of any -mcpu options.
84 if (!Triple.isOSDarwin()) {
85 // FIXME: Warn on inconsistent use of -mcpu and -march.
86 // If we have -mcpu=, use that.
87 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +000088 return A->getValue();
Bob Wilson677a35b2012-03-21 16:31:37 +000089 }
Daniel Dunbar00577ad2010-08-23 22:35:37 +000090
Chris Lattner5f9e2722011-07-23 10:55:15 +000091 StringRef MArch;
Daniel Dunbar00577ad2010-08-23 22:35:37 +000092 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
93 // Otherwise, if we have -march= choose the base CPU for that arch.
Richard Smith1d489cf2012-11-01 04:30:05 +000094 MArch = A->getValue();
Daniel Dunbar00577ad2010-08-23 22:35:37 +000095 } else {
96 // Otherwise, use the Arch from the triple.
97 MArch = Triple.getArchName();
98 }
99
Bob Wilson57f6d192012-03-21 17:19:12 +0000100 return llvm::StringSwitch<const char *>(MArch)
101 .Cases("armv2", "armv2a","arm2")
102 .Case("armv3", "arm6")
103 .Case("armv3m", "arm7m")
104 .Cases("armv4", "armv4t", "arm7tdmi")
105 .Cases("armv5", "armv5t", "arm10tdmi")
106 .Cases("armv5e", "armv5te", "arm1026ejs")
107 .Case("armv5tej", "arm926ej-s")
108 .Cases("armv6", "armv6k", "arm1136jf-s")
109 .Case("armv6j", "arm1136j-s")
110 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
111 .Case("armv6t2", "arm1156t2-s")
112 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
Renato Golin40a94e22013-02-05 23:42:01 +0000113 .Cases("armv7l", "armv7-l", "cortex-a8")
Bob Wilson336bfa32012-09-29 23:52:50 +0000114 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
115 .Cases("armv7s", "armv7-s", "swift")
Quentin Colombetab137512012-12-21 17:57:47 +0000116 .Cases("armv7r", "armv7-r", "cortex-r4", "cortex-r5")
Bob Wilson57f6d192012-03-21 17:19:12 +0000117 .Cases("armv7m", "armv7-m", "cortex-m3")
118 .Case("ep9312", "ep9312")
119 .Case("iwmmxt", "iwmmxt")
120 .Case("xscale", "xscale")
121 .Cases("armv6m", "armv6-m", "cortex-m0")
122 // If all else failed, return the most base CPU LLVM supports.
123 .Default("arm7tdmi");
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000124}
125
126/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
127/// CPU.
128//
129// FIXME: This is redundant with -mcpu, why does LLVM use this.
130// FIXME: tblgen this, or kill it!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Bob Wilson57f6d192012-03-21 17:19:12 +0000132 return llvm::StringSwitch<const char *>(CPU)
133 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
134 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
135 .Cases("arm920", "arm920t", "arm922t", "v4t")
136 .Cases("arm940t", "ep9312","v4t")
137 .Cases("arm10tdmi", "arm1020t", "v5")
138 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
139 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
140 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
141 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
142 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
143 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000144 .Cases("cortex-a8", "cortex-a9", "cortex-a15", "v7")
Bob Wilson57f6d192012-03-21 17:19:12 +0000145 .Case("cortex-m3", "v7m")
Jim Grosbach69033132012-03-29 19:53:34 +0000146 .Case("cortex-m4", "v7m")
Bob Wilson57f6d192012-03-21 17:19:12 +0000147 .Case("cortex-m0", "v6m")
Bob Wilson336bfa32012-09-29 23:52:50 +0000148 .Case("cortex-a9-mp", "v7f")
149 .Case("swift", "v7s")
Bob Wilson57f6d192012-03-21 17:19:12 +0000150 .Default("");
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000151}
152
Chad Rosier61ab80a2011-09-20 20:44:06 +0000153std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
154 types::ID InputType) const {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000155 switch (getTriple().getArch()) {
156 default:
157 return getTripleString();
158
159 case llvm::Triple::arm:
160 case llvm::Triple::thumb: {
161 // FIXME: Factor into subclasses.
162 llvm::Triple Triple = getTriple();
163
164 // Thumb2 is the default for V7 on Darwin.
165 //
166 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000167 StringRef Suffix =
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000168 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Bob Wilson336bfa32012-09-29 23:52:50 +0000169 bool ThumbDefault = (Suffix.startswith("v7") && getTriple().isOSDarwin());
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000170 std::string ArchName = "arm";
Chad Rosier61ab80a2011-09-20 20:44:06 +0000171
172 // Assembly files should start in ARM mode.
173 if (InputType != types::TY_PP_Asm &&
174 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000175 ArchName = "thumb";
176 Triple.setArchName(ArchName + Suffix.str());
177
178 return Triple.getTriple();
179 }
180 }
181}
182
Chad Rosier61ab80a2011-09-20 20:44:06 +0000183std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
184 types::ID InputType) const {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000185 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000186 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000187 options::OPT_miphoneos_version_min_EQ,
188 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000189 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000190 << A->getAsString(Args);
191
Chad Rosier61ab80a2011-09-20 20:44:06 +0000192 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000193}
194
Chandler Carruth88491fc2011-11-04 07:12:53 +0000195void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
196 ArgStringList &CC1Args) const {
197 // Each toolchain should provide the appropriate include flags.
198}
199
Chandler Carrutha6b25812012-11-21 23:40:23 +0000200void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
201 ArgStringList &CC1Args) const {
Rafael Espindola8af669f2012-06-19 01:26:10 +0000202}
203
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000204ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
205 const ArgList &Args) const
206{
207 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000208 StringRef Value = A->getValue();
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000209 if (Value == "compiler-rt")
210 return ToolChain::RLT_CompilerRT;
211 if (Value == "libgcc")
212 return ToolChain::RLT_Libgcc;
213 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
214 << A->getAsString(Args);
215 }
216
217 return GetDefaultRuntimeLibType();
218}
219
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000220ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000221 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000222 StringRef Value = A->getValue();
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000223 if (Value == "libc++")
224 return ToolChain::CST_Libcxx;
225 if (Value == "libstdc++")
226 return ToolChain::CST_Libstdcxx;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000227 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000228 << A->getAsString(Args);
229 }
230
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000231 return ToolChain::CST_Libstdcxx;
232}
233
Chandler Carruth79cbbdc2011-12-17 23:10:01 +0000234/// \brief Utility function to add a system include directory to CC1 arguments.
235/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
236 ArgStringList &CC1Args,
237 const Twine &Path) {
238 CC1Args.push_back("-internal-isystem");
239 CC1Args.push_back(DriverArgs.MakeArgString(Path));
240}
241
242/// \brief Utility function to add a system include directory with extern "C"
243/// semantics to CC1 arguments.
244///
245/// Note that this should be used rarely, and only for directories that
246/// historically and for legacy reasons are treated as having implicit extern
247/// "C" semantics. These semantics are *ignored* by and large today, but its
248/// important to preserve the preprocessor changes resulting from the
249/// classification.
250/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
251 ArgStringList &CC1Args,
252 const Twine &Path) {
253 CC1Args.push_back("-internal-externc-isystem");
254 CC1Args.push_back(DriverArgs.MakeArgString(Path));
255}
256
257/// \brief Utility function to add a list of system include directories to CC1.
258/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
259 ArgStringList &CC1Args,
260 ArrayRef<StringRef> Paths) {
261 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
262 I != E; ++I) {
263 CC1Args.push_back("-internal-isystem");
264 CC1Args.push_back(DriverArgs.MakeArgString(*I));
265 }
266}
267
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000268void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
269 ArgStringList &CC1Args) const {
Chandler Carrutha4614422011-11-04 07:43:33 +0000270 // Header search paths should be handled by each of the subclasses.
271 // Historically, they have not been, and instead have been handled inside of
272 // the CC1-layer frontend. As the logic is hoisted out, this generic function
273 // will slowly stop being called.
274 //
275 // While it is being called, replicate a bit of a hack to propagate the
276 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
277 // header search paths with it. Once all systems are overriding this
278 // function, the CC1 flag and this line can be removed.
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000279 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000280}
281
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000282void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
283 ArgStringList &CmdArgs) const {
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000284 CXXStdlibType Type = GetCXXStdlibType(Args);
285
286 switch (Type) {
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000287 case ToolChain::CST_Libcxx:
288 CmdArgs.push_back("-lc++");
289 break;
290
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000291 case ToolChain::CST_Libstdcxx:
292 CmdArgs.push_back("-lstdc++");
293 break;
294 }
295}
Shantonu Sen7433fed2010-09-17 18:39:08 +0000296
297void ToolChain::AddCCKextLibArgs(const ArgList &Args,
298 ArgStringList &CmdArgs) const {
299 CmdArgs.push_back("-lcc_kext");
300}
Benjamin Kramere20e5082012-10-04 19:42:20 +0000301
302bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
303 ArgStringList &CmdArgs) const {
304 // Check if -ffast-math or -funsafe-math is enabled.
305 Arg *A = Args.getLastArg(options::OPT_ffast_math,
306 options::OPT_fno_fast_math,
307 options::OPT_funsafe_math_optimizations,
308 options::OPT_fno_unsafe_math_optimizations);
309
310 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
311 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
312 return false;
313
314 // If crtfastmath.o exists add it to the arguments.
315 std::string Path = GetFilePath("crtfastmath.o");
316 if (Path == "crtfastmath.o") // Not found.
317 return false;
318
319 CmdArgs.push_back(Args.MakeArgString(Path));
320 return true;
321}