blob: 16c5ae6d9ddd7e552e03664bff8fee911e73f945 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- ToolChain.cpp - Collections of tools for one platform ------------===//
Daniel Dunbar9e2136d2009-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"
11
12#include "clang/Driver/Action.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000016#include "clang/Driver/DriverDiagnostic.h"
Benjamin Kramer058666a2012-10-04 19:42:20 +000017#include "clang/Driver/Option.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000018#include "clang/Driver/Options.h"
Bob Wilson7f05ca32012-03-21 17:19:12 +000019#include "llvm/ADT/StringSwitch.h"
John McCall24fc0de2011-07-06 00:26:06 +000020#include "llvm/Support/ErrorHandling.h"
John McCall5fb5df92012-06-20 06:18:46 +000021#include "clang/Basic/ObjCRuntime.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000022using namespace clang::driver;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000023using namespace clang;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000024
Chandler Carruthd7fa2e02012-01-31 02:21:20 +000025ToolChain::ToolChain(const Driver &D, const llvm::Triple &T)
26 : D(D), Triple(T) {
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000027}
28
29ToolChain::~ToolChain() {
30}
31
Daniel Dunbar083edf72009-12-21 18:54:17 +000032const Driver &ToolChain::getDriver() const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000033 return D;
Daniel Dunbar083edf72009-12-21 18:54:17 +000034}
35
Rafael Espindola151a9572012-09-23 03:05:41 +000036bool ToolChain::IsUnwindTablesDefault() const {
37 return false;
38}
39
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +000040std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000041 return D.GetFilePath(Name, *this);
Mike Stump11289f42009-09-09 15:08:12 +000042
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000043}
44
Simon Atanasyanb16488c2012-10-03 19:52:37 +000045std::string ToolChain::GetProgramPath(const char *Name) const {
46 return D.GetProgramPath(Name, *this);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000047}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +000048
49types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
50 return types::lookupTypeForExtension(Ext);
51}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000052
Daniel Dunbar62123a12010-09-17 00:24:52 +000053bool ToolChain::HasNativeLLVMSupport() const {
54 return false;
55}
56
John McCall5fb5df92012-06-20 06:18:46 +000057ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const {
David Chisnallb601c962012-07-03 20:49:52 +000058 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC,
John McCall5fb5df92012-06-20 06:18:46 +000059 VersionTuple());
John McCall24fc0de2011-07-06 00:26:06 +000060}
61
Chris Lattner57540c52011-04-15 05:22:18 +000062/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000063//
64// FIXME: tblgen this.
65static const char *getARMTargetCPU(const ArgList &Args,
66 const llvm::Triple &Triple) {
Bob Wilsoncc4ab9d2012-03-21 16:31:37 +000067 // For Darwin targets, the -arch option (which is translated to a
68 // corresponding -march option) should determine the architecture
69 // (and the Mach-O slice) regardless of any -mcpu options.
70 if (!Triple.isOSDarwin()) {
71 // FIXME: Warn on inconsistent use of -mcpu and -march.
72 // If we have -mcpu=, use that.
73 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
74 return A->getValue(Args);
75 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000076
Chris Lattner0e62c1c2011-07-23 10:55:15 +000077 StringRef MArch;
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000078 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
79 // Otherwise, if we have -march= choose the base CPU for that arch.
80 MArch = A->getValue(Args);
81 } else {
82 // Otherwise, use the Arch from the triple.
83 MArch = Triple.getArchName();
84 }
85
Bob Wilson7f05ca32012-03-21 17:19:12 +000086 return llvm::StringSwitch<const char *>(MArch)
87 .Cases("armv2", "armv2a","arm2")
88 .Case("armv3", "arm6")
89 .Case("armv3m", "arm7m")
90 .Cases("armv4", "armv4t", "arm7tdmi")
91 .Cases("armv5", "armv5t", "arm10tdmi")
92 .Cases("armv5e", "armv5te", "arm1026ejs")
93 .Case("armv5tej", "arm926ej-s")
94 .Cases("armv6", "armv6k", "arm1136jf-s")
95 .Case("armv6j", "arm1136j-s")
96 .Cases("armv6z", "armv6zk", "arm1176jzf-s")
97 .Case("armv6t2", "arm1156t2-s")
98 .Cases("armv7", "armv7a", "armv7-a", "cortex-a8")
Bob Wilsond7cf1042012-09-29 23:52:50 +000099 .Cases("armv7f", "armv7-f", "cortex-a9-mp")
100 .Cases("armv7s", "armv7-s", "swift")
Bob Wilson7f05ca32012-03-21 17:19:12 +0000101 .Cases("armv7r", "armv7-r", "cortex-r4")
102 .Cases("armv7m", "armv7-m", "cortex-m3")
103 .Case("ep9312", "ep9312")
104 .Case("iwmmxt", "iwmmxt")
105 .Case("xscale", "xscale")
106 .Cases("armv6m", "armv6-m", "cortex-m0")
107 // If all else failed, return the most base CPU LLVM supports.
108 .Default("arm7tdmi");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000109}
110
111/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
112/// CPU.
113//
114// FIXME: This is redundant with -mcpu, why does LLVM use this.
115// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000116static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Bob Wilson7f05ca32012-03-21 17:19:12 +0000117 return llvm::StringSwitch<const char *>(CPU)
118 .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "v4t")
119 .Cases("arm720t", "arm9", "arm9tdmi", "v4t")
120 .Cases("arm920", "arm920t", "arm922t", "v4t")
121 .Cases("arm940t", "ep9312","v4t")
122 .Cases("arm10tdmi", "arm1020t", "v5")
123 .Cases("arm9e", "arm926ej-s", "arm946e-s", "v5e")
124 .Cases("arm966e-s", "arm968e-s", "arm10e", "v5e")
125 .Cases("arm1020e", "arm1022e", "xscale", "iwmmxt", "v5e")
126 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "v6")
127 .Cases("arm1176jzf-s", "mpcorenovfp", "mpcore", "v6")
128 .Cases("arm1156t2-s", "arm1156t2f-s", "v6t2")
Silviu Baranga157f7c62012-09-13 15:06:00 +0000129 .Cases("cortex-a8", "cortex-a9", "cortex-a15", "v7")
Bob Wilson7f05ca32012-03-21 17:19:12 +0000130 .Case("cortex-m3", "v7m")
Jim Grosbach903e63f2012-03-29 19:53:34 +0000131 .Case("cortex-m4", "v7m")
Bob Wilson7f05ca32012-03-21 17:19:12 +0000132 .Case("cortex-m0", "v6m")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000133 .Case("cortex-a9-mp", "v7f")
134 .Case("swift", "v7s")
Bob Wilson7f05ca32012-03-21 17:19:12 +0000135 .Default("");
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000136}
137
Chad Rosierd3a0f952011-09-20 20:44:06 +0000138std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
139 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000140 switch (getTriple().getArch()) {
141 default:
142 return getTripleString();
143
144 case llvm::Triple::arm:
145 case llvm::Triple::thumb: {
146 // FIXME: Factor into subclasses.
147 llvm::Triple Triple = getTriple();
148
149 // Thumb2 is the default for V7 on Darwin.
150 //
151 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000152 StringRef Suffix =
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000153 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Bob Wilsond7cf1042012-09-29 23:52:50 +0000154 bool ThumbDefault = (Suffix.startswith("v7") && getTriple().isOSDarwin());
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000155 std::string ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000156
157 // Assembly files should start in ARM mode.
158 if (InputType != types::TY_PP_Asm &&
159 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000160 ArchName = "thumb";
161 Triple.setArchName(ArchName + Suffix.str());
162
163 return Triple.getTriple();
164 }
165 }
166}
167
Chad Rosierd3a0f952011-09-20 20:44:06 +0000168std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
169 types::ID InputType) const {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000170 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000171 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000172 options::OPT_miphoneos_version_min_EQ,
173 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000174 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000175 << A->getAsString(Args);
176
Chad Rosierd3a0f952011-09-20 20:44:06 +0000177 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000178}
179
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000180void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
181 ArgStringList &CC1Args) const {
182 // Each toolchain should provide the appropriate include flags.
183}
184
Rafael Espindola66aa0452012-06-19 01:26:10 +0000185void ToolChain::addClangTargetOptions(ArgStringList &CC1Args) const {
186}
187
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000188ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
189 const ArgList &Args) const
190{
191 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
192 StringRef Value = A->getValue(Args);
193 if (Value == "compiler-rt")
194 return ToolChain::RLT_CompilerRT;
195 if (Value == "libgcc")
196 return ToolChain::RLT_Libgcc;
197 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
198 << A->getAsString(Args);
199 }
200
201 return GetDefaultRuntimeLibType();
202}
203
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000204ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000205 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000206 StringRef Value = A->getValue(Args);
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000207 if (Value == "libc++")
208 return ToolChain::CST_Libcxx;
209 if (Value == "libstdc++")
210 return ToolChain::CST_Libstdcxx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000211 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000212 << A->getAsString(Args);
213 }
214
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000215 return ToolChain::CST_Libstdcxx;
216}
217
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000218/// \brief Utility function to add a system include directory to CC1 arguments.
219/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
220 ArgStringList &CC1Args,
221 const Twine &Path) {
222 CC1Args.push_back("-internal-isystem");
223 CC1Args.push_back(DriverArgs.MakeArgString(Path));
224}
225
226/// \brief Utility function to add a system include directory with extern "C"
227/// semantics to CC1 arguments.
228///
229/// Note that this should be used rarely, and only for directories that
230/// historically and for legacy reasons are treated as having implicit extern
231/// "C" semantics. These semantics are *ignored* by and large today, but its
232/// important to preserve the preprocessor changes resulting from the
233/// classification.
234/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
235 ArgStringList &CC1Args,
236 const Twine &Path) {
237 CC1Args.push_back("-internal-externc-isystem");
238 CC1Args.push_back(DriverArgs.MakeArgString(Path));
239}
240
241/// \brief Utility function to add a list of system include directories to CC1.
242/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
243 ArgStringList &CC1Args,
244 ArrayRef<StringRef> Paths) {
245 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
246 I != E; ++I) {
247 CC1Args.push_back("-internal-isystem");
248 CC1Args.push_back(DriverArgs.MakeArgString(*I));
249 }
250}
251
Chandler Carruth814db372011-11-04 23:49:01 +0000252void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
253 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000254 // Header search paths should be handled by each of the subclasses.
255 // Historically, they have not been, and instead have been handled inside of
256 // the CC1-layer frontend. As the logic is hoisted out, this generic function
257 // will slowly stop being called.
258 //
259 // While it is being called, replicate a bit of a hack to propagate the
260 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
261 // header search paths with it. Once all systems are overriding this
262 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000263 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000264}
265
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000266void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
267 ArgStringList &CmdArgs) const {
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000268 CXXStdlibType Type = GetCXXStdlibType(Args);
269
270 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000271 case ToolChain::CST_Libcxx:
272 CmdArgs.push_back("-lc++");
273 break;
274
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000275 case ToolChain::CST_Libstdcxx:
276 CmdArgs.push_back("-lstdc++");
277 break;
278 }
279}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000280
281void ToolChain::AddCCKextLibArgs(const ArgList &Args,
282 ArgStringList &CmdArgs) const {
283 CmdArgs.push_back("-lcc_kext");
284}
Benjamin Kramer058666a2012-10-04 19:42:20 +0000285
286bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args,
287 ArgStringList &CmdArgs) const {
288 // Check if -ffast-math or -funsafe-math is enabled.
289 Arg *A = Args.getLastArg(options::OPT_ffast_math,
290 options::OPT_fno_fast_math,
291 options::OPT_funsafe_math_optimizations,
292 options::OPT_fno_unsafe_math_optimizations);
293
294 if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
295 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
296 return false;
297
298 // If crtfastmath.o exists add it to the arguments.
299 std::string Path = GetFilePath("crtfastmath.o");
300 if (Path == "crtfastmath.o") // Not found.
301 return false;
302
303 CmdArgs.push_back(Args.MakeArgString(Path));
304 return true;
305}