blob: 29aecf2e2cb04840b42c9dcd5d10d641195ae730 [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
Rafael Espindola5470cd22013-03-18 17:52:57 +000035bool ToolChain::useIntegratedAs(const ArgList &Args) const {
36 return Args.hasFlag(options::OPT_integrated_as,
37 options::OPT_no_integrated_as,
38 IsIntegratedAssemblerDefault());
39}
40
Daniel Dunbard2a527e2012-11-08 03:38:26 +000041std::string ToolChain::getDefaultUniversalArchName() const {
42 // In universal driver terms, the arch name accepted by -arch isn't exactly
43 // the same as the ones that appear in the triple. Roughly speaking, this is
44 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the
45 // only interesting special case is powerpc.
46 switch (Triple.getArch()) {
47 case llvm::Triple::ppc:
48 return "ppc";
49 case llvm::Triple::ppc64:
50 return "ppc64";
51 default:
52 return Triple.getArchName();
53 }
54}
55
Rafael Espindola03a86382012-09-23 03:05:41 +000056bool ToolChain::IsUnwindTablesDefault() const {
57 return false;
58}
59
Daniel Dunbar4a7e8892010-07-14 18:46:23 +000060std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000061 return D.GetFilePath(Name, *this);
Mike Stump1eb44332009-09-09 15:08:12 +000062
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000063}
64
Simon Atanasyanfc44e882012-10-03 19:52:37 +000065std::string ToolChain::GetProgramPath(const char *Name) const {
66 return D.GetProgramPath(Name, *this);
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000067}
Daniel Dunbar41800112010-08-02 05:43:56 +000068
69types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
70 return types::lookupTypeForExtension(Ext);
71}
Daniel Dunbar00577ad2010-08-23 22:35:37 +000072
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000073bool ToolChain::HasNativeLLVMSupport() const {
74 return false;
75}
76
John McCall260611a2012-06-20 06:18:46 +000077ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnall11d3f4c2012-07-03 20:49:52 +000078 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall260611a2012-06-20 06:18:46 +000079 VersionTuple());
John McCall9f084a32011-07-06 00:26:06 +000080}
81
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000082/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar00577ad2010-08-23 22:35:37 +000083//
84// FIXME: tblgen this.
85static const char *getARMTargetCPU(const ArgList &Args,
86 const llvm::Triple &Triple) {
Bob Wilson677a35b2012-03-21 16:31:37 +000087 // For Darwin targets, the -arch option (which is translated to a
88 // corresponding -march option) should determine the architecture
89 // (and the Mach-O slice) regardless of any -mcpu options.
90 if (!Triple.isOSDarwin()) {
91 // FIXME: Warn on inconsistent use of -mcpu and -march.
92 // If we have -mcpu=, use that.
93 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +000094 return A->getValue();
Bob Wilson677a35b2012-03-21 16:31:37 +000095 }
Daniel Dunbar00577ad2010-08-23 22:35:37 +000096
Chris Lattner5f9e2722011-07-23 10:55:15 +000097 StringRef MArch;
Daniel Dunbar00577ad2010-08-23 22:35:37 +000098 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
99 // Otherwise, if we have -march= choose the base CPU for that arch.
Richard Smith1d489cf2012-11-01 04:30:05 +0000100 MArch = A->getValue();
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000101 } else {
102 // Otherwise, use the Arch from the triple.
103 MArch = Triple.getArchName();
104 }
105
Bob Wilson57f6d192012-03-21 17:19:12 +0000106 return llvm::StringSwitch<const char *>(MArch)
107 .Cases("armv2", "armv2a","arm2")
108 .Case("armv3", "arm6")
109 .Case("armv3m", "arm7m")
110 .Cases("armv4", "armv4t", "arm7tdmi")
111 .Cases("armv5", "armv5t", "arm10tdmi")
112 .Cases("armv5e", "armv5te", "arm1026ejs")
113 .Case("armv5tej", "arm926ej-s")
114 .Cases("armv6", "armv6k", "arm1136jf-s")
115 .Case("armv6j", "arm1136j-s")
116 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
117 .Case("armv6t2", "arm1156t2-s")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000118 .Cases("armv6m", "armv6-m", "cortex-m0")
Bob Wilson57f6d192012-03-21 17:19:12 +0000119 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
Renato Golin40a94e22013-02-05 23:42:01 +0000120 .Cases("armv7l", "armv7-l", "cortex-a8")
Bob Wilson336bfa32012-09-29 23:52:50 +0000121 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
122 .Cases("armv7s", "armv7-s", "swift")
Bob Wilson532f5a92013-03-04 22:37:43 +0000123 .Cases("armv7r", "armv7-r", "cortex-r4")
Bob Wilson57f6d192012-03-21 17:19:12 +0000124 .Cases("armv7m", "armv7-m", "cortex-m3")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000125 .Cases("armv7em", "armv7e-m", "cortex-m4")
Bob Wilson57f6d192012-03-21 17:19:12 +0000126 .Case("ep9312", "ep9312")
127 .Case("iwmmxt", "iwmmxt")
128 .Case("xscale", "xscale")
Bob Wilson57f6d192012-03-21 17:19:12 +0000129 // If all else failed, return the most base CPU LLVM supports.
130 .Default("arm7tdmi");
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000131}
132
133/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
134/// CPU.
135//
136// FIXME: This is redundant with -mcpu, why does LLVM use this.
137// FIXME: tblgen this, or kill it!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000138static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Bob Wilson57f6d192012-03-21 17:19:12 +0000139 return llvm::StringSwitch<const char *>(CPU)
140 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
141 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
142 .Cases("arm920", "arm920t", "arm922t", "v4t")
143 .Cases("arm940t", "ep9312","v4t")
144 .Cases("arm10tdmi", "arm1020t", "v5")
145 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
146 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
147 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
148 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
149 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
150 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Bob Wilsonfc553452013-03-04 22:37:46 +0000151 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "v7")
152 .Cases("cortex-a9", "cortex-a15", "v7")
153 .Case("cortex-r5", "v7r")
Bob Wilson57f6d192012-03-21 17:19:12 +0000154 .Case("cortex-m0", "v6m")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000155 .Case("cortex-m3", "v7m")
156 .Case("cortex-m4", "v7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000157 .Case("cortex-a9-mp", "v7f")
158 .Case("swift", "v7s")
Bob Wilson57f6d192012-03-21 17:19:12 +0000159 .Default("");
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000160}
161
Chad Rosier61ab80a2011-09-20 20:44:06 +0000162std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
163 types::ID InputType) const {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000164 switch (getTriple().getArch()) {
165 default:
166 return getTripleString();
167
168 case llvm::Triple::arm:
169 case llvm::Triple::thumb: {
170 // FIXME: Factor into subclasses.
171 llvm::Triple Triple = getTriple();
172
173 // Thumb2 is the default for V7 on Darwin.
174 //
175 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000176 StringRef Suffix =
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000177 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Bob Wilson2503ebd2013-03-04 22:37:49 +0000178 bool ThumbDefault = Suffix.startswith("v6m") ||
179 (Suffix.startswith("v7") && getTriple().isOSDarwin());
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000180 std::string ArchName = "arm";
Chad Rosier61ab80a2011-09-20 20:44:06 +0000181
182 // Assembly files should start in ARM mode.
183 if (InputType != types::TY_PP_Asm &&
184 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000185 ArchName = "thumb";
186 Triple.setArchName(ArchName + Suffix.str());
187
188 return Triple.getTriple();
189 }
190 }
191}
192
Chad Rosier61ab80a2011-09-20 20:44:06 +0000193std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
194 types::ID InputType) const {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000195 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000196 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000197 options::OPT_miphoneos_version_min_EQ,
198 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000200 << A->getAsString(Args);
201
Chad Rosier61ab80a2011-09-20 20:44:06 +0000202 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000203}
204
Chandler Carruth88491fc2011-11-04 07:12:53 +0000205void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
206 ArgStringList &CC1Args) const {
207 // Each toolchain should provide the appropriate include flags.
208}
209
Chandler Carrutha6b25812012-11-21 23:40:23 +0000210void ToolChain::addClangTargetOptions(const ArgList &DriverArgs,
211 ArgStringList &CC1Args) const {
Rafael Espindola8af669f2012-06-19 01:26:10 +0000212}
213
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000214ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
215 const ArgList &Args) const
216{
217 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000218 StringRef Value = A->getValue();
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000219 if (Value == "compiler-rt")
220 return ToolChain::RLT_CompilerRT;
221 if (Value == "libgcc")
222 return ToolChain::RLT_Libgcc;
223 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
224 << A->getAsString(Args);
225 }
226
227 return GetDefaultRuntimeLibType();
228}
229
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000230ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000231 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000232 StringRef Value = A->getValue();
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000233 if (Value == "libc++")
234 return ToolChain::CST_Libcxx;
235 if (Value == "libstdc++")
236 return ToolChain::CST_Libstdcxx;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000238 << A->getAsString(Args);
239 }
240
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000241 return ToolChain::CST_Libstdcxx;
242}
243
Chandler Carruth79cbbdc2011-12-17 23:10:01 +0000244/// \brief Utility function to add a system include directory to CC1 arguments.
245/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
246 ArgStringList &CC1Args,
247 const Twine &Path) {
248 CC1Args.push_back("-internal-isystem");
249 CC1Args.push_back(DriverArgs.MakeArgString(Path));
250}
251
252/// \brief Utility function to add a system include directory with extern "C"
253/// semantics to CC1 arguments.
254///
255/// Note that this should be used rarely, and only for directories that
256/// historically and for legacy reasons are treated as having implicit extern
257/// "C" semantics. These semantics are *ignored* by and large today, but its
258/// important to preserve the preprocessor changes resulting from the
259/// classification.
260/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
261 ArgStringList &CC1Args,
262 const Twine &Path) {
263 CC1Args.push_back("-internal-externc-isystem");
264 CC1Args.push_back(DriverArgs.MakeArgString(Path));
265}
266
267/// \brief Utility function to add a list of system include directories to CC1.
268/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
269 ArgStringList &CC1Args,
270 ArrayRef<StringRef> Paths) {
271 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
272 I != E; ++I) {
273 CC1Args.push_back("-internal-isystem");
274 CC1Args.push_back(DriverArgs.MakeArgString(*I));
275 }
276}
277
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000278void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
279 ArgStringList &CC1Args) const {
Chandler Carrutha4614422011-11-04 07:43:33 +0000280 // Header search paths should be handled by each of the subclasses.
281 // Historically, they have not been, and instead have been handled inside of
282 // the CC1-layer frontend. As the logic is hoisted out, this generic function
283 // will slowly stop being called.
284 //
285 // While it is being called, replicate a bit of a hack to propagate the
286 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
287 // header search paths with it. Once all systems are overriding this
288 // function, the CC1 flag and this line can be removed.
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000289 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000290}
291
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000292void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
293 ArgStringList &CmdArgs) const {
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000294 CXXStdlibType Type = GetCXXStdlibType(Args);
295
296 switch (Type) {
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000297 case ToolChain::CST_Libcxx:
298 CmdArgs.push_back("-lc++");
299 break;
300
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000301 case ToolChain::CST_Libstdcxx:
302 CmdArgs.push_back("-lstdc++");
303 break;
304 }
305}
Shantonu Sen7433fed2010-09-17 18:39:08 +0000306
307void ToolChain::AddCCKextLibArgs(const ArgList &Args,
308 ArgStringList &CmdArgs) const {
309 CmdArgs.push_back("-lcc_kext");
310}
Benjamin Kramere20e5082012-10-04 19:42:20 +0000311
312bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
313 ArgStringList &CmdArgs) const {
314 // Check if -ffast-math or -funsafe-math is enabled.
315 Arg *A = Args.getLastArg(options::OPT_ffast_math,
316 options::OPT_fno_fast_math,
317 options::OPT_funsafe_math_optimizations,
318 options::OPT_fno_unsafe_math_optimizations);
319
320 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
321 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
322 return false;
323
324 // If crtfastmath.o exists add it to the arguments.
325 std::string Path = GetFilePath("crtfastmath.o");
326 if (Path == "crtfastmath.o") // Not found.
327 return false;
328
329 CmdArgs.push_back(Args.MakeArgString(Path));
330 return true;
331}