blob: c0c9c504b6d1387242e6a919779fcba572dbfec1 [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"
11
12#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"
John McCall9f084a32011-07-06 00:26:06 +000017#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000018#include "clang/Driver/Options.h"
John McCall9f084a32011-07-06 00:26:06 +000019#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000020using namespace clang::driver;
Chris Lattner5f9e2722011-07-23 10:55:15 +000021using namespace clang;
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000022
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000023ToolChain::ToolChain(const Driver &D, const llvm::Triple &T)
24 : D(D), Triple(T) {
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000025}
26
27ToolChain::~ToolChain() {
28}
29
Daniel Dunbaree788e72009-12-21 18:54:17 +000030const Driver &ToolChain::getDriver() const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000031 return D;
Daniel Dunbaree788e72009-12-21 18:54:17 +000032}
33
Daniel Dunbar4a7e8892010-07-14 18:46:23 +000034std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000035 return D.GetFilePath(Name, *this);
Mike Stump1eb44332009-09-09 15:08:12 +000036
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000037}
38
Daniel Dunbar4a7e8892010-07-14 18:46:23 +000039std::string ToolChain::GetProgramPath(const char *Name, bool WantFile) const {
Chandler Carruth4d7ff6e2012-01-25 09:12:06 +000040 return D.GetProgramPath(Name, *this, WantFile);
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000041}
Daniel Dunbar41800112010-08-02 05:43:56 +000042
43types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
44 return types::lookupTypeForExtension(Ext);
45}
Daniel Dunbar00577ad2010-08-23 22:35:37 +000046
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000047bool ToolChain::HasNativeLLVMSupport() const {
48 return false;
49}
50
John McCall9f084a32011-07-06 00:26:06 +000051void ToolChain::configureObjCRuntime(ObjCRuntime &runtime) const {
52 switch (runtime.getKind()) {
53 case ObjCRuntime::NeXT:
54 // Assume a minimal NeXT runtime.
55 runtime.HasARC = false;
56 runtime.HasWeak = false;
John McCall256a76e2011-07-06 01:22:26 +000057 runtime.HasTerminate = false;
John McCall9f084a32011-07-06 00:26:06 +000058 return;
59
60 case ObjCRuntime::GNU:
61 // Assume a maximal GNU runtime.
62 runtime.HasARC = true;
63 runtime.HasWeak = true;
John McCall256a76e2011-07-06 01:22:26 +000064 runtime.HasTerminate = false; // to be added
John McCall9f084a32011-07-06 00:26:06 +000065 return;
66 }
67 llvm_unreachable("invalid runtime kind!");
68}
69
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000070/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar00577ad2010-08-23 22:35:37 +000071//
72// FIXME: tblgen this.
73static const char *getARMTargetCPU(const ArgList &Args,
74 const llvm::Triple &Triple) {
75 // FIXME: Warn on inconsistent use of -mcpu and -march.
76
77 // If we have -mcpu=, use that.
78 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
79 return A->getValue(Args);
80
Chris Lattner5f9e2722011-07-23 10:55:15 +000081 StringRef MArch;
Daniel Dunbar00577ad2010-08-23 22:35:37 +000082 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
83 // Otherwise, if we have -march= choose the base CPU for that arch.
84 MArch = A->getValue(Args);
85 } else {
86 // Otherwise, use the Arch from the triple.
87 MArch = Triple.getArchName();
88 }
89
90 if (MArch == "armv2" || MArch == "armv2a")
91 return "arm2";
92 if (MArch == "armv3")
93 return "arm6";
94 if (MArch == "armv3m")
95 return "arm7m";
96 if (MArch == "armv4" || MArch == "armv4t")
97 return "arm7tdmi";
98 if (MArch == "armv5" || MArch == "armv5t")
99 return "arm10tdmi";
100 if (MArch == "armv5e" || MArch == "armv5te")
101 return "arm1026ejs";
102 if (MArch == "armv5tej")
103 return "arm926ej-s";
104 if (MArch == "armv6" || MArch == "armv6k")
105 return "arm1136jf-s";
106 if (MArch == "armv6j")
107 return "arm1136j-s";
108 if (MArch == "armv6z" || MArch == "armv6zk")
109 return "arm1176jzf-s";
110 if (MArch == "armv6t2")
111 return "arm1156t2-s";
112 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
113 return "cortex-a8";
114 if (MArch == "armv7r" || MArch == "armv7-r")
115 return "cortex-r4";
116 if (MArch == "armv7m" || MArch == "armv7-m")
117 return "cortex-m3";
118 if (MArch == "ep9312")
119 return "ep9312";
120 if (MArch == "iwmmxt")
121 return "iwmmxt";
122 if (MArch == "xscale")
123 return "xscale";
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000124 if (MArch == "armv6m" || MArch == "armv6-m")
125 return "cortex-m0";
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000126
127 // If all else failed, return the most base CPU LLVM supports.
128 return "arm7tdmi";
129}
130
131/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
132/// CPU.
133//
134// FIXME: This is redundant with -mcpu, why does LLVM use this.
135// FIXME: tblgen this, or kill it!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000136static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000137 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
138 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
139 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
140 CPU == "arm940t" || CPU == "ep9312")
141 return "v4t";
142
143 if (CPU == "arm10tdmi" || CPU == "arm1020t")
144 return "v5";
145
146 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
147 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
148 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
149 CPU == "iwmmxt")
150 return "v5e";
151
152 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
153 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
154 return "v6";
155
156 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
157 return "v6t2";
158
159 if (CPU == "cortex-a8" || CPU == "cortex-a9")
160 return "v7";
161
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000162 if (CPU == "cortex-m3")
163 return "v7m";
164
165 if (CPU == "cortex-m0")
166 return "v6m";
167
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000168 return "";
169}
170
Chad Rosier61ab80a2011-09-20 20:44:06 +0000171std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
172 types::ID InputType) const {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000173 switch (getTriple().getArch()) {
174 default:
175 return getTripleString();
176
177 case llvm::Triple::arm:
178 case llvm::Triple::thumb: {
179 // FIXME: Factor into subclasses.
180 llvm::Triple Triple = getTriple();
181
182 // Thumb2 is the default for V7 on Darwin.
183 //
184 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000185 StringRef Suffix =
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000186 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Bob Wilson905c45f2011-10-14 05:03:44 +0000187 bool ThumbDefault = (Suffix == "v7" && getTriple().isOSDarwin());
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000188 std::string ArchName = "arm";
Chad Rosier61ab80a2011-09-20 20:44:06 +0000189
190 // Assembly files should start in ARM mode.
191 if (InputType != types::TY_PP_Asm &&
192 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000193 ArchName = "thumb";
194 Triple.setArchName(ArchName + Suffix.str());
195
196 return Triple.getTriple();
197 }
198 }
199}
200
Chad Rosier61ab80a2011-09-20 20:44:06 +0000201std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
202 types::ID InputType) const {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000203 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000204 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000205 options::OPT_miphoneos_version_min_EQ,
206 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000208 << A->getAsString(Args);
209
Chad Rosier61ab80a2011-09-20 20:44:06 +0000210 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000211}
212
Chandler Carruth88491fc2011-11-04 07:12:53 +0000213void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
214 ArgStringList &CC1Args) const {
215 // Each toolchain should provide the appropriate include flags.
216}
217
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000218ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
219 const ArgList &Args) const
220{
221 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
222 StringRef Value = A->getValue(Args);
223 if (Value == "compiler-rt")
224 return ToolChain::RLT_CompilerRT;
225 if (Value == "libgcc")
226 return ToolChain::RLT_Libgcc;
227 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
228 << A->getAsString(Args);
229 }
230
231 return GetDefaultRuntimeLibType();
232}
233
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000234ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000235 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000236 StringRef Value = A->getValue(Args);
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000237 if (Value == "libc++")
238 return ToolChain::CST_Libcxx;
239 if (Value == "libstdc++")
240 return ToolChain::CST_Libstdcxx;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000242 << A->getAsString(Args);
243 }
244
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000245 return ToolChain::CST_Libstdcxx;
246}
247
Chandler Carruth79cbbdc2011-12-17 23:10:01 +0000248/// \brief Utility function to add a system include directory to CC1 arguments.
249/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
250 ArgStringList &CC1Args,
251 const Twine &Path) {
252 CC1Args.push_back("-internal-isystem");
253 CC1Args.push_back(DriverArgs.MakeArgString(Path));
254}
255
256/// \brief Utility function to add a system include directory with extern "C"
257/// semantics to CC1 arguments.
258///
259/// Note that this should be used rarely, and only for directories that
260/// historically and for legacy reasons are treated as having implicit extern
261/// "C" semantics. These semantics are *ignored* by and large today, but its
262/// important to preserve the preprocessor changes resulting from the
263/// classification.
264/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
265 ArgStringList &CC1Args,
266 const Twine &Path) {
267 CC1Args.push_back("-internal-externc-isystem");
268 CC1Args.push_back(DriverArgs.MakeArgString(Path));
269}
270
271/// \brief Utility function to add a list of system include directories to CC1.
272/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
273 ArgStringList &CC1Args,
274 ArrayRef<StringRef> Paths) {
275 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
276 I != E; ++I) {
277 CC1Args.push_back("-internal-isystem");
278 CC1Args.push_back(DriverArgs.MakeArgString(*I));
279 }
280}
281
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000282void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
283 ArgStringList &CC1Args) const {
Chandler Carrutha4614422011-11-04 07:43:33 +0000284 // Header search paths should be handled by each of the subclasses.
285 // Historically, they have not been, and instead have been handled inside of
286 // the CC1-layer frontend. As the logic is hoisted out, this generic function
287 // will slowly stop being called.
288 //
289 // While it is being called, replicate a bit of a hack to propagate the
290 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
291 // header search paths with it. Once all systems are overriding this
292 // function, the CC1 flag and this line can be removed.
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000293 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000294}
295
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000296void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
297 ArgStringList &CmdArgs) const {
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000298 CXXStdlibType Type = GetCXXStdlibType(Args);
299
300 switch (Type) {
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000301 case ToolChain::CST_Libcxx:
302 CmdArgs.push_back("-lc++");
303 break;
304
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000305 case ToolChain::CST_Libstdcxx:
306 CmdArgs.push_back("-lstdc++");
307 break;
308 }
309}
Shantonu Sen7433fed2010-09-17 18:39:08 +0000310
311void ToolChain::AddCCKextLibArgs(const ArgList &Args,
312 ArgStringList &CmdArgs) const {
313 CmdArgs.push_back("-lcc_kext");
314}