blob: eea460bd33e944ba6f2f9a1e9f3dd553e0ea13fe [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"
John McCall24fc0de2011-07-06 00:26:06 +000017#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000018#include "clang/Driver/Options.h"
John McCall24fc0de2011-07-06 00:26:06 +000019#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000020using namespace clang::driver;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000021using namespace clang;
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000022
Chandler Carruthd7fa2e02012-01-31 02:21:20 +000023ToolChain::ToolChain(const Driver &D, const llvm::Triple &T)
24 : D(D), Triple(T) {
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000025}
26
27ToolChain::~ToolChain() {
28}
29
Daniel Dunbar083edf72009-12-21 18:54:17 +000030const Driver &ToolChain::getDriver() const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000031 return D;
Daniel Dunbar083edf72009-12-21 18:54:17 +000032}
33
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +000034std::string ToolChain::GetFilePath(const char *Name) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000035 return D.GetFilePath(Name, *this);
Mike Stump11289f42009-09-09 15:08:12 +000036
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000037}
38
Daniel Dunbar9c3ed5f2010-07-14 18:46:23 +000039std::string ToolChain::GetProgramPath(const char *Name, bool WantFile) const {
Chandler Carruthb65b1112012-01-25 09:12:06 +000040 return D.GetProgramPath(Name, *this, WantFile);
Daniel Dunbar9e2136d2009-03-16 05:25:36 +000041}
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +000042
43types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
44 return types::lookupTypeForExtension(Ext);
45}
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000046
Daniel Dunbar62123a12010-09-17 00:24:52 +000047bool ToolChain::HasNativeLLVMSupport() const {
48 return false;
49}
50
John McCall24fc0de2011-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 Kremenekf7639e12012-03-06 20:06:33 +000057 runtime.HasSubscripting = false;
John McCall9de19782011-07-06 01:22:26 +000058 runtime.HasTerminate = false;
John McCall24fc0de2011-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 Kremenekf7639e12012-03-06 20:06:33 +000065 runtime.HasSubscripting = false; // to be added
John McCall9de19782011-07-06 01:22:26 +000066 runtime.HasTerminate = false; // to be added
John McCall24fc0de2011-07-06 00:26:06 +000067 return;
68 }
69 llvm_unreachable("invalid runtime kind!");
70}
71
Chris Lattner57540c52011-04-15 05:22:18 +000072/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000073//
74// FIXME: tblgen this.
75static const char *getARMTargetCPU(const ArgList &Args,
76 const llvm::Triple &Triple) {
Bob Wilsoncc4ab9d2012-03-21 16:31:37 +000077 // For Darwin targets, the -arch option (which is translated to a
78 // corresponding -march option) should determine the architecture
79 // (and the Mach-O slice) regardless of any -mcpu options.
80 if (!Triple.isOSDarwin()) {
81 // FIXME: Warn on inconsistent use of -mcpu and -march.
82 // If we have -mcpu=, use that.
83 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
84 return A->getValue(Args);
85 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000086
Chris Lattner0e62c1c2011-07-23 10:55:15 +000087 StringRef MArch;
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000088 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
89 // Otherwise, if we have -march= choose the base CPU for that arch.
90 MArch = A->getValue(Args);
91 } else {
92 // Otherwise, use the Arch from the triple.
93 MArch = Triple.getArchName();
94 }
95
96 if (MArch == "armv2" || MArch == "armv2a")
97 return "arm2";
98 if (MArch == "armv3")
99 return "arm6";
100 if (MArch == "armv3m")
101 return "arm7m";
102 if (MArch == "armv4" || MArch == "armv4t")
103 return "arm7tdmi";
104 if (MArch == "armv5" || MArch == "armv5t")
105 return "arm10tdmi";
106 if (MArch == "armv5e" || MArch == "armv5te")
107 return "arm1026ejs";
108 if (MArch == "armv5tej")
109 return "arm926ej-s";
110 if (MArch == "armv6" || MArch == "armv6k")
111 return "arm1136jf-s";
112 if (MArch == "armv6j")
113 return "arm1136j-s";
114 if (MArch == "armv6z" || MArch == "armv6zk")
115 return "arm1176jzf-s";
116 if (MArch == "armv6t2")
117 return "arm1156t2-s";
118 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
119 return "cortex-a8";
120 if (MArch == "armv7r" || MArch == "armv7-r")
121 return "cortex-r4";
122 if (MArch == "armv7m" || MArch == "armv7-m")
123 return "cortex-m3";
124 if (MArch == "ep9312")
125 return "ep9312";
126 if (MArch == "iwmmxt")
127 return "iwmmxt";
128 if (MArch == "xscale")
129 return "xscale";
Bob Wilsond9249412011-03-21 20:40:05 +0000130 if (MArch == "armv6m" || MArch == "armv6-m")
131 return "cortex-m0";
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000132
133 // If all else failed, return the most base CPU LLVM supports.
134 return "arm7tdmi";
135}
136
137/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
138/// CPU.
139//
140// FIXME: This is redundant with -mcpu, why does LLVM use this.
141// FIXME: tblgen this, or kill it!
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000142static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000143 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
144 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
145 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
146 CPU == "arm940t" || CPU == "ep9312")
147 return "v4t";
148
149 if (CPU == "arm10tdmi" || CPU == "arm1020t")
150 return "v5";
151
152 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
153 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
154 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
155 CPU == "iwmmxt")
156 return "v5e";
157
158 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
159 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
160 return "v6";
161
162 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
163 return "v6t2";
164
165 if (CPU == "cortex-a8" || CPU == "cortex-a9")
166 return "v7";
167
Bob Wilsond9249412011-03-21 20:40:05 +0000168 if (CPU == "cortex-m3")
169 return "v7m";
170
171 if (CPU == "cortex-m0")
172 return "v6m";
173
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000174 return "";
175}
176
Chad Rosierd3a0f952011-09-20 20:44:06 +0000177std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
178 types::ID InputType) const {
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000179 switch (getTriple().getArch()) {
180 default:
181 return getTripleString();
182
183 case llvm::Triple::arm:
184 case llvm::Triple::thumb: {
185 // FIXME: Factor into subclasses.
186 llvm::Triple Triple = getTriple();
187
188 // Thumb2 is the default for V7 on Darwin.
189 //
190 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000191 StringRef Suffix =
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000192 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Bob Wilson6524dd32011-10-14 05:03:44 +0000193 bool ThumbDefault = (Suffix == "v7" && getTriple().isOSDarwin());
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000194 std::string ArchName = "arm";
Chad Rosierd3a0f952011-09-20 20:44:06 +0000195
196 // Assembly files should start in ARM mode.
197 if (InputType != types::TY_PP_Asm &&
198 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000199 ArchName = "thumb";
200 Triple.setArchName(ArchName + Suffix.str());
201
202 return Triple.getTriple();
203 }
204 }
205}
206
Chad Rosierd3a0f952011-09-20 20:44:06 +0000207std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
208 types::ID InputType) const {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000209 // Diagnose use of Darwin OS deployment target arguments on non-Darwin.
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000210 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000211 options::OPT_miphoneos_version_min_EQ,
212 options::OPT_mios_simulator_version_min_EQ))
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000213 getDriver().Diag(diag::err_drv_clang_unsupported)
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000214 << A->getAsString(Args);
215
Chad Rosierd3a0f952011-09-20 20:44:06 +0000216 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000217}
218
Chandler Carruth6bfd84f2011-11-04 07:12:53 +0000219void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
220 ArgStringList &CC1Args) const {
221 // Each toolchain should provide the appropriate include flags.
222}
223
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000224ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType(
225 const ArgList &Args) const
226{
227 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) {
228 StringRef Value = A->getValue(Args);
229 if (Value == "compiler-rt")
230 return ToolChain::RLT_CompilerRT;
231 if (Value == "libgcc")
232 return ToolChain::RLT_Libgcc;
233 getDriver().Diag(diag::err_drv_invalid_rtlib_name)
234 << A->getAsString(Args);
235 }
236
237 return GetDefaultRuntimeLibType();
238}
239
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000240ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000241 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000242 StringRef Value = A->getValue(Args);
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000243 if (Value == "libc++")
244 return ToolChain::CST_Libcxx;
245 if (Value == "libstdc++")
246 return ToolChain::CST_Libstdcxx;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000247 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000248 << A->getAsString(Args);
249 }
250
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000251 return ToolChain::CST_Libstdcxx;
252}
253
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000254/// \brief Utility function to add a system include directory to CC1 arguments.
255/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
256 ArgStringList &CC1Args,
257 const Twine &Path) {
258 CC1Args.push_back("-internal-isystem");
259 CC1Args.push_back(DriverArgs.MakeArgString(Path));
260}
261
262/// \brief Utility function to add a system include directory with extern "C"
263/// semantics to CC1 arguments.
264///
265/// Note that this should be used rarely, and only for directories that
266/// historically and for legacy reasons are treated as having implicit extern
267/// "C" semantics. These semantics are *ignored* by and large today, but its
268/// important to preserve the preprocessor changes resulting from the
269/// classification.
270/*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
271 ArgStringList &CC1Args,
272 const Twine &Path) {
273 CC1Args.push_back("-internal-externc-isystem");
274 CC1Args.push_back(DriverArgs.MakeArgString(Path));
275}
276
277/// \brief Utility function to add a list of system include directories to CC1.
278/*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
279 ArgStringList &CC1Args,
280 ArrayRef<StringRef> Paths) {
281 for (ArrayRef<StringRef>::iterator I = Paths.begin(), E = Paths.end();
282 I != E; ++I) {
283 CC1Args.push_back("-internal-isystem");
284 CC1Args.push_back(DriverArgs.MakeArgString(*I));
285 }
286}
287
Chandler Carruth814db372011-11-04 23:49:01 +0000288void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
289 ArgStringList &CC1Args) const {
Chandler Carruth4c81dfa2011-11-04 07:43:33 +0000290 // Header search paths should be handled by each of the subclasses.
291 // Historically, they have not been, and instead have been handled inside of
292 // the CC1-layer frontend. As the logic is hoisted out, this generic function
293 // will slowly stop being called.
294 //
295 // While it is being called, replicate a bit of a hack to propagate the
296 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++
297 // header search paths with it. Once all systems are overriding this
298 // function, the CC1 flag and this line can be removed.
Chandler Carruth814db372011-11-04 23:49:01 +0000299 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ);
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000300}
301
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000302void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
303 ArgStringList &CmdArgs) const {
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000304 CXXStdlibType Type = GetCXXStdlibType(Args);
305
306 switch (Type) {
Daniel Dunbar092b6fb2010-09-14 23:12:40 +0000307 case ToolChain::CST_Libcxx:
308 CmdArgs.push_back("-lc++");
309 break;
310
Daniel Dunbarbf11f792010-09-14 23:12:35 +0000311 case ToolChain::CST_Libstdcxx:
312 CmdArgs.push_back("-lstdc++");
313 break;
314 }
315}
Shantonu Senafeb03b2010-09-17 18:39:08 +0000316
317void ToolChain::AddCCKextLibArgs(const ArgList &Args,
318 ArgStringList &CmdArgs) const {
319 CmdArgs.push_back("-lcc_kext");
320}