blob: 074f1a348132e91eb8277aacb1e0bee0bdb38b74 [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"
Daniel Dunbarfa0cda42009-03-17 21:21:26 +000017#include "clang/Driver/HostInfo.h"
John McCall9f084a32011-07-06 00:26:06 +000018#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000019#include "clang/Driver/Options.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
Chris Lattner5f9e2722011-07-23 10:55:15 +000024ToolChain::ToolChain(const HostInfo &H, const llvm::Triple &T)
25 : Host(H), 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 {
32 return Host.getDriver();
33}
34
Daniel Dunbar4a7e8892010-07-14 18:46:23 +000035std::string ToolChain::GetFilePath(const char *Name) const {
Daniel Dunbar21549232009-03-18 02:55:38 +000036 return Host.getDriver().GetFilePath(Name, *this);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000038}
39
Daniel Dunbar4a7e8892010-07-14 18:46:23 +000040std::string ToolChain::GetProgramPath(const char *Name, bool WantFile) const {
Mike Stump950bedd2009-03-27 00:40:20 +000041 return Host.getDriver().GetProgramPath(Name, *this, WantFile);
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +000042}
Daniel Dunbar41800112010-08-02 05:43:56 +000043
44types::ID ToolChain::LookupTypeForExtension(const char *Ext) const {
45 return types::lookupTypeForExtension(Ext);
46}
Daniel Dunbar00577ad2010-08-23 22:35:37 +000047
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000048bool ToolChain::HasNativeLLVMSupport() const {
49 return false;
50}
51
John McCall9f084a32011-07-06 00:26:06 +000052void ToolChain::configureObjCRuntime(ObjCRuntime &runtime) const {
53 switch (runtime.getKind()) {
54 case ObjCRuntime::NeXT:
55 // Assume a minimal NeXT runtime.
56 runtime.HasARC = false;
57 runtime.HasWeak = 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;
John McCall256a76e2011-07-06 01:22:26 +000065 runtime.HasTerminate = false; // to be added
John McCall9f084a32011-07-06 00:26:06 +000066 return;
67 }
68 llvm_unreachable("invalid runtime kind!");
69}
70
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000071/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
Daniel Dunbar00577ad2010-08-23 22:35:37 +000072//
73// FIXME: tblgen this.
74static const char *getARMTargetCPU(const ArgList &Args,
75 const llvm::Triple &Triple) {
76 // FIXME: Warn on inconsistent use of -mcpu and -march.
77
78 // If we have -mcpu=, use that.
79 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
80 return A->getValue(Args);
81
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 StringRef MArch;
Daniel Dunbar00577ad2010-08-23 22:35:37 +000083 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
84 // Otherwise, if we have -march= choose the base CPU for that arch.
85 MArch = A->getValue(Args);
86 } else {
87 // Otherwise, use the Arch from the triple.
88 MArch = Triple.getArchName();
89 }
90
91 if (MArch == "armv2" || MArch == "armv2a")
92 return "arm2";
93 if (MArch == "armv3")
94 return "arm6";
95 if (MArch == "armv3m")
96 return "arm7m";
97 if (MArch == "armv4" || MArch == "armv4t")
98 return "arm7tdmi";
99 if (MArch == "armv5" || MArch == "armv5t")
100 return "arm10tdmi";
101 if (MArch == "armv5e" || MArch == "armv5te")
102 return "arm1026ejs";
103 if (MArch == "armv5tej")
104 return "arm926ej-s";
105 if (MArch == "armv6" || MArch == "armv6k")
106 return "arm1136jf-s";
107 if (MArch == "armv6j")
108 return "arm1136j-s";
109 if (MArch == "armv6z" || MArch == "armv6zk")
110 return "arm1176jzf-s";
111 if (MArch == "armv6t2")
112 return "arm1156t2-s";
113 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
114 return "cortex-a8";
115 if (MArch == "armv7r" || MArch == "armv7-r")
116 return "cortex-r4";
117 if (MArch == "armv7m" || MArch == "armv7-m")
118 return "cortex-m3";
119 if (MArch == "ep9312")
120 return "ep9312";
121 if (MArch == "iwmmxt")
122 return "iwmmxt";
123 if (MArch == "xscale")
124 return "xscale";
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000125 if (MArch == "armv6m" || MArch == "armv6-m")
126 return "cortex-m0";
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000127
128 // If all else failed, return the most base CPU LLVM supports.
129 return "arm7tdmi";
130}
131
132/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
133/// CPU.
134//
135// FIXME: This is redundant with -mcpu, why does LLVM use this.
136// FIXME: tblgen this, or kill it!
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137static const char *getLLVMArchSuffixForARM(StringRef CPU) {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000138 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
139 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
140 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
141 CPU == "arm940t" || CPU == "ep9312")
142 return "v4t";
143
144 if (CPU == "arm10tdmi" || CPU == "arm1020t")
145 return "v5";
146
147 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
148 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
149 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
150 CPU == "iwmmxt")
151 return "v5e";
152
153 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
154 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
155 return "v6";
156
157 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
158 return "v6t2";
159
160 if (CPU == "cortex-a8" || CPU == "cortex-a9")
161 return "v7";
162
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000163 if (CPU == "cortex-m3")
164 return "v7m";
165
166 if (CPU == "cortex-m0")
167 return "v6m";
168
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000169 return "";
170}
171
Chad Rosier61ab80a2011-09-20 20:44:06 +0000172std::string ToolChain::ComputeLLVMTriple(const ArgList &Args,
173 types::ID InputType) const {
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000174 switch (getTriple().getArch()) {
175 default:
176 return getTripleString();
177
178 case llvm::Triple::arm:
179 case llvm::Triple::thumb: {
180 // FIXME: Factor into subclasses.
181 llvm::Triple Triple = getTriple();
182
183 // Thumb2 is the default for V7 on Darwin.
184 //
185 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000186 StringRef Suffix =
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000187 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
188 bool ThumbDefault =
189 (Suffix == "v7" && getTriple().getOS() == llvm::Triple::Darwin);
190 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
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000215ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000216 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000217 StringRef Value = A->getValue(Args);
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000218 if (Value == "libc++")
219 return ToolChain::CST_Libcxx;
220 if (Value == "libstdc++")
221 return ToolChain::CST_Libstdcxx;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000222 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000223 << A->getAsString(Args);
224 }
225
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000226 return ToolChain::CST_Libstdcxx;
227}
228
229void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
John McCallf85e1932011-06-15 23:02:42 +0000230 ArgStringList &CmdArgs,
231 bool ObjCXXAutoRefCount) const {
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000232 CXXStdlibType Type = GetCXXStdlibType(Args);
233
Bob Wilson13c4f212011-06-21 21:12:29 +0000234 // Header search paths are handled by the mass of goop in InitHeaderSearch.
235
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000236 switch (Type) {
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000237 case ToolChain::CST_Libcxx:
John McCallf85e1932011-06-15 23:02:42 +0000238 if (ObjCXXAutoRefCount)
239 CmdArgs.push_back("-fobjc-arc-cxxlib=libc++");
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000240 break;
241
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000242 case ToolChain::CST_Libstdcxx:
John McCallf85e1932011-06-15 23:02:42 +0000243 if (ObjCXXAutoRefCount)
244 CmdArgs.push_back("-fobjc-arc-cxxlib=libstdc++");
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000245 break;
246 }
247}
248
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000249void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
250 ArgStringList &CmdArgs) const {
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000251 CXXStdlibType Type = GetCXXStdlibType(Args);
252
253 switch (Type) {
Daniel Dunbar3f16c952010-09-14 23:12:40 +0000254 case ToolChain::CST_Libcxx:
255 CmdArgs.push_back("-lc++");
256 break;
257
Daniel Dunbar641b98b2010-09-14 23:12:35 +0000258 case ToolChain::CST_Libstdcxx:
259 CmdArgs.push_back("-lstdc++");
260 break;
261 }
262}
Shantonu Sen7433fed2010-09-17 18:39:08 +0000263
264void ToolChain::AddCCKextLibArgs(const ArgList &Args,
265 ArgStringList &CmdArgs) const {
266 CmdArgs.push_back("-lcc_kext");
267}