blob: b4f5d0b421080c245d91aca8c75e62ce78a0a3bd [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;
Ted Kremenek30660a82012-03-06 20:06:33 +000057 runtime.HasSubscripting = false;
John McCall256a76e2011-07-06 01:22:26 +000058 runtime.HasTerminate = false;
John McCall9f084a32011-07-06 00:26:06 +000059 return;
60
61 case ObjCRuntime::GNU:
62 // Assume a maximal GNU runtime.
63 runtime.HasARC = true;
64 runtime.HasWeak = true;
Ted Kremenek30660a82012-03-06 20:06:33 +000065 runtime.HasSubscripting = false; // to be added
John McCall256a76e2011-07-06 01:22:26 +000066 runtime.HasTerminate = false; // to be added
John McCall9f084a32011-07-06 00:26:06 +000067 return;
68 }
69 llvm_unreachable("invalid runtime kind!");
70}
71
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000072/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar00577ad2010-08-23 22:35:37 +000073//
74// FIXME: tblgen this.
75static const char *getARMTargetCPU(const ArgList &Args,
76 const llvm::Triple &Triple) {
77 // FIXME: Warn on inconsistent use of -mcpu and -march.
78
79 // If we have -mcpu=, use that.
80 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
81 return A->getValue(Args);
82
Chris Lattner5f9e2722011-07-23 10:55:15 +000083 StringRef MArch;
Daniel Dunbar00577ad2010-08-23 22:35:37 +000084 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
85 // Otherwise, if we have -march= choose the base CPU for that arch.
86 MArch = A->getValue(Args);
87 } else {
88 // Otherwise, use the Arch from the triple.
89 MArch = Triple.getArchName();
90 }
91
92 if (MArch == "armv2" || MArch == "armv2a")
93 return "arm2";
94 if (MArch == "armv3")
95 return "arm6";
96 if (MArch == "armv3m")
97 return "arm7m";
98 if (MArch == "armv4" || MArch == "armv4t")
99 return "arm7tdmi";
100 if (MArch == "armv5" || MArch == "armv5t")
101 return "arm10tdmi";
102 if (MArch == "armv5e" || MArch == "armv5te")
103 return "arm1026ejs";
104 if (MArch == "armv5tej")
105 return "arm926ej-s";
106 if (MArch == "armv6" || MArch == "armv6k")
107 return "arm1136jf-s";
108 if (MArch == "armv6j")
109 return "arm1136j-s";
110 if (MArch == "armv6z" || MArch == "armv6zk")
111 return "arm1176jzf-s";
112 if (MArch == "armv6t2")
113 return "arm1156t2-s";
114 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
115 return "cortex-a8";
116 if (MArch == "armv7r" || MArch == "armv7-r")
117 return "cortex-r4";
118 if (MArch == "armv7m" || MArch == "armv7-m")
119 return "cortex-m3";
120 if (MArch == "ep9312")
121 return "ep9312";
122 if (MArch == "iwmmxt")
123 return "iwmmxt";
124 if (MArch == "xscale")
125 return "xscale";
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000126 if (MArch == "armv6m" || MArch == "armv6-m")
127 return "cortex-m0";
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000128
129 // If all else failed, return the most base CPU LLVM supports.
130 return "arm7tdmi";
131}
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) {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000139 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
140 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
141 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
142 CPU == "arm940t" || CPU == "ep9312")
143 return "v4t";
144
145 if (CPU == "arm10tdmi" || CPU == "arm1020t")
146 return "v5";
147
148 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
149 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
150 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
151 CPU == "iwmmxt")
152 return "v5e";
153
154 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
155 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
156 return "v6";
157
158 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
159 return "v6t2";
160
161 if (CPU == "cortex-a8" || CPU == "cortex-a9")
162 return "v7";
163
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000164 if (CPU == "cortex-m3")
165 return "v7m";
166
167 if (CPU == "cortex-m0")
168 return "v6m";
169
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000170 return "";
171}
172
Chad Rosier61ab80a2011-09-20 20:44:06 +0000173std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
174 types::ID InputType) const {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000175 switch (getTriple().getArch()) {
176 default:
177 return getTripleString();
178
179 case llvm::Triple::arm:
180 case llvm::Triple::thumb: {
181 // FIXME: Factor into subclasses.
182 llvm::Triple Triple = getTriple();
183
184 // Thumb2 is the default for V7 on Darwin.
185 //
186 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187 StringRef Suffix =
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000188 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Bob Wilson905c45f2011-10-14 05:03:44 +0000189 bool ThumbDefault = (Suffix == "v7" && getTriple().isOSDarwin());
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000190 std::string ArchName = "arm";
Chad Rosier61ab80a2011-09-20 20:44:06 +0000191
192 // Assembly files should start in ARM mode.
193 if (InputType != types::TY_PP_Asm &&
194 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000195 ArchName = "thumb";
196 Triple.setArchName(ArchName + Suffix.str());
197
198 return Triple.getTriple();
199 }
200 }
201}
202
Chad Rosier61ab80a2011-09-20 20:44:06 +0000203std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
204 types::ID InputType) const {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000205 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000206 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000207 options::OPT_miphoneos_version_min_EQ,
208 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000210 << A->getAsString(Args);
211
Chad Rosier61ab80a2011-09-20 20:44:06 +0000212 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000213}
214
Chandler Carruth88491fc2011-11-04 07:12:53 +0000215void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
216 ArgStringList &CC1Args) const {
217 // Each toolchain should provide the appropriate include flags.
218}
219
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000220ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
221 const ArgList &Args) const
222{
223 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
224 StringRef Value = A->getValue(Args);
225 if (Value == "compiler-rt")
226 return ToolChain::RLT_CompilerRT;
227 if (Value == "libgcc")
228 return ToolChain::RLT_Libgcc;
229 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
230 << A->getAsString(Args);
231 }
232
233 return GetDefaultRuntimeLibType();
234}
235
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000236ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000237 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000238 StringRef Value = A->getValue(Args);
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000239 if (Value == "libc++")
240 return ToolChain::CST_Libcxx;
241 if (Value == "libstdc++")
242 return ToolChain::CST_Libstdcxx;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000243 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000244 << A->getAsString(Args);
245 }
246
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000247 return ToolChain::CST_Libstdcxx;
248}
249
Chandler Carruth79cbbdc2011-12-17 23:10:01 +0000250/// \brief Utility function to add a system include directory to CC1 arguments.
251/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
252 ArgStringList &CC1Args,
253 const Twine &Path) {
254 CC1Args.push_back("-internal-isystem");
255 CC1Args.push_back(DriverArgs.MakeArgString(Path));
256}
257
258/// \brief Utility function to add a system include directory with extern "C"
259/// semantics to CC1 arguments.
260///
261/// Note that this should be used rarely, and only for directories that
262/// historically and for legacy reasons are treated as having implicit extern
263/// "C" semantics. These semantics are *ignored* by and large today, but its
264/// important to preserve the preprocessor changes resulting from the
265/// classification.
266/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
267 ArgStringList &CC1Args,
268 const Twine &Path) {
269 CC1Args.push_back("-internal-externc-isystem");
270 CC1Args.push_back(DriverArgs.MakeArgString(Path));
271}
272
273/// \brief Utility function to add a list of system include directories to CC1.
274/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
275 ArgStringList &CC1Args,
276 ArrayRef<StringRef> Paths) {
277 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
278 I != E; ++I) {
279 CC1Args.push_back("-internal-isystem");
280 CC1Args.push_back(DriverArgs.MakeArgString(*I));
281 }
282}
283
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000284void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
285 ArgStringList &CC1Args) const {
Chandler Carrutha4614422011-11-04 07:43:33 +0000286 // Header search paths should be handled by each of the subclasses.
287 // Historically, they have not been, and instead have been handled inside of
288 // the CC1-layer frontend. As the logic is hoisted out, this generic function
289 // will slowly stop being called.
290 //
291 // While it is being called, replicate a bit of a hack to propagate the
292 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
293 // header search paths with it. Once all systems are overriding this
294 // function, the CC1 flag and this line can be removed.
Chandler Carruthab9fcd02011-11-04 23:49:01 +0000295 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000296}
297
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000298void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
299 ArgStringList &CmdArgs) const {
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000300 CXXStdlibType Type = GetCXXStdlibType(Args);
301
302 switch (Type) {
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000303 case ToolChain::CST_Libcxx:
304 CmdArgs.push_back("-lc++");
305 break;
306
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000307 case ToolChain::CST_Libstdcxx:
308 CmdArgs.push_back("-lstdc++");
309 break;
310 }
311}
Shantonu Sen7433fed2010-09-17 18:39:08 +0000312
313void ToolChain::AddCCKextLibArgs(const ArgList &Args,
314 ArgStringList &CmdArgs) const {
315 CmdArgs.push_back("-lcc_kext");
316}