blob: 0449711208eb4d184907f49c1c0445998a283691 [file] [log] [blame]
Daniel Dunbar39176082009-03-20 00:20:03 +00001//===--- ToolChains.cpp - ToolChain Implementations ---------------------*-===//
2//
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 "ToolChains.h"
11
Daniel Dunbarf3cad362009-03-25 04:13:45 +000012#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000014#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000018#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000019#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000020#include "clang/Driver/Options.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000021
22#include "llvm/ADT/StringExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000023#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000024#include "llvm/Support/raw_ostream.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000025#include "llvm/System/Path.h"
26
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000027#include <cstdlib> // ::getenv
28
Daniel Dunbar39176082009-03-20 00:20:03 +000029using namespace clang::driver;
30using namespace clang::driver::toolchains;
31
Daniel Dunbarf3955282009-09-04 18:34:51 +000032/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000033
Daniel Dunbarf3955282009-09-04 18:34:51 +000034Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple,
Daniel Dunbarcc8e1892010-01-27 00:56:44 +000035 const unsigned (&_DarwinVersion)[3])
36 : ToolChain(Host, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000037{
Daniel Dunbar02633b52009-03-26 16:23:12 +000038 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar5435fc92010-01-27 00:57:11 +000039 << "10." << std::max(0, (int)_DarwinVersion[0] - 4) << '.'
40 << _DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000041}
42
Daniel Dunbareeff4062010-01-22 02:04:58 +000043// FIXME: Can we tablegen this?
44static const char *GetArmArchForMArch(llvm::StringRef Value) {
45 if (Value == "armv6k")
46 return "armv6";
47
48 if (Value == "armv5tej")
49 return "armv5";
50
51 if (Value == "xscale")
52 return "xscale";
53
54 if (Value == "armv4t")
55 return "armv4t";
56
57 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
58 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
59 Value == "armv7m")
60 return "armv7";
61
62 return 0;
63}
64
65// FIXME: Can we tablegen this?
66static const char *GetArmArchForMCpu(llvm::StringRef Value) {
67 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
68 Value == "arm946e-s" || Value == "arm966e-s" ||
69 Value == "arm968e-s" || Value == "arm10e" ||
70 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
71 Value == "arm1026ej-s")
72 return "armv5";
73
74 if (Value == "xscale")
75 return "xscale";
76
77 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
78 Value == "arm1176jz-s" || Value == "arm1176jzf-s")
79 return "armv6";
80
81 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
82 return "armv7";
83
84 return 0;
85}
86
87llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
88 switch (getTriple().getArch()) {
89 default:
90 return getArchName();
91
92 case llvm::Triple::arm: {
93 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
94 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
95 return Arch;
96
97 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
98 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
99 return Arch;
100
101 return "arm";
102 }
103 }
104}
105
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000106DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
107 const unsigned (&DarwinVersion)[3],
Daniel Dunbarcc8e1892010-01-27 00:56:44 +0000108 const unsigned (&_GCCVersion)[3])
109 : Darwin(Host, Triple, DarwinVersion)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000110{
111 GCCVersion[0] = _GCCVersion[0];
112 GCCVersion[1] = _GCCVersion[1];
113 GCCVersion[2] = _GCCVersion[2];
114
115 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000116 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +0000117 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000118 ToolChainDir += "/";
119 ToolChainDir += llvm::utostr(GCCVersion[0]);
120 ToolChainDir += '.';
121 ToolChainDir += llvm::utostr(GCCVersion[1]);
122 ToolChainDir += '.';
123 ToolChainDir += llvm::utostr(GCCVersion[2]);
124
Daniel Dunbar74782b02009-10-20 19:25:43 +0000125 // Try the next major version if that tool chain dir is invalid.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000126 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
127 if (!llvm::sys::Path(Tmp).exists()) {
Daniel Dunbar74782b02009-10-20 19:25:43 +0000128 std::string Next = "i686-apple-darwin";
129 Next += llvm::utostr(DarwinVersion[0] + 1);
130 Next += "/";
131 Next += llvm::utostr(GCCVersion[0]);
132 Next += '.';
133 Next += llvm::utostr(GCCVersion[1]);
134 Next += '.';
135 Next += llvm::utostr(GCCVersion[2]);
136
137 // Use that if it exists, otherwise hope the user isn't linking.
138 //
139 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000140 Tmp = "/usr/lib/gcc/" + Next;
141 if (llvm::sys::Path(Tmp).exists())
Daniel Dunbar74782b02009-10-20 19:25:43 +0000142 ToolChainDir = Next;
143 }
144
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000145 std::string Path;
146 if (getArchName() == "x86_64") {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000147 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000148 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000149 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000150 Path += "/x86_64";
151 getFilePaths().push_back(Path);
152
153 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000154 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000155 Path += "/x86_64";
156 getFilePaths().push_back(Path);
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Daniel Dunbaree788e72009-12-21 18:54:17 +0000159 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000160 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000161 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000162 getFilePaths().push_back(Path);
163
164 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000165 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000166 getFilePaths().push_back(Path);
167
Daniel Dunbaree788e72009-12-21 18:54:17 +0000168 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000169 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000170 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000171 getProgramPaths().push_back(Path);
172
173 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000174 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000175 getProgramPaths().push_back(Path);
176
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000177 getProgramPaths().push_back(getDriver().getInstalledDir());
178 if (getDriver().getInstalledDir() != getDriver().Dir)
179 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000180}
181
Daniel Dunbarf3955282009-09-04 18:34:51 +0000182Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000183 // Free tool implementations.
184 for (llvm::DenseMap<unsigned, Tool*>::iterator
185 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
186 delete it->second;
187}
188
Daniel Dunbarf3955282009-09-04 18:34:51 +0000189Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000190 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000191 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000192 Key = Action::AnalyzeJobClass;
193 else
194 Key = JA.getKind();
195
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000196 // FIXME: This doesn't belong here, but ideally we will support static soon
197 // anyway.
198 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
199 C.getArgs().hasArg(options::OPT_static) ||
200 C.getArgs().hasArg(options::OPT_fapple_kext));
201 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
202 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
203 options::OPT_no_integrated_as,
204 IsIADefault);
205
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000206 Tool *&T = Tools[Key];
207 if (!T) {
208 switch (Key) {
209 case Action::InputClass:
210 case Action::BindArchClass:
211 assert(0 && "Invalid tool kind.");
212 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000213 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000214 case Action::AnalyzeJobClass:
215 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000216 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000217 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000218 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000219 case Action::AssembleJobClass: {
220 if (UseIntegratedAs)
221 T = new tools::ClangAs(*this);
222 else
223 T = new tools::darwin::Assemble(*this);
224 break;
225 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000226 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000227 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000228 case Action::LipoJobClass:
229 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000230 case Action::DsymutilJobClass:
231 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000232 }
233 }
234
235 return *T;
236}
237
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000238void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
239 ArgStringList &CmdArgs) const {
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000240 std::string Tmp;
241
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000242 // FIXME: Derive these correctly.
243 if (getArchName() == "x86_64") {
244 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
245 "/x86_64"));
246 // Intentionally duplicated for (temporary) gcc bug compatibility.
247 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
248 "/x86_64"));
249 }
Daniel Dunbar8bc9c552010-04-10 01:24:22 +0000250
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000251 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000252
253 Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir;
254 if (llvm::sys::Path(Tmp).exists())
255 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
256 Tmp = getDriver().Dir + "/../lib/gcc";
257 if (llvm::sys::Path(Tmp).exists())
258 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000259 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
260 // Intentionally duplicated for (temporary) gcc bug compatibility.
261 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000262 Tmp = getDriver().Dir + "/../lib/" + ToolChainDir;
263 if (llvm::sys::Path(Tmp).exists())
264 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
265 Tmp = getDriver().Dir + "/../lib";
266 if (llvm::sys::Path(Tmp).exists())
267 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000268 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
269 "/../../../" + ToolChainDir));
270 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
271 "/../../.."));
272}
273
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000274void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
275 ArgStringList &CmdArgs) const {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000276 // Note that this routine is only used for targetting OS X.
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000277
278 // Derived from libgcc and lib specs but refactored.
279 if (Args.hasArg(options::OPT_static)) {
280 CmdArgs.push_back("-lgcc_static");
281 } else {
282 if (Args.hasArg(options::OPT_static_libgcc)) {
283 CmdArgs.push_back("-lgcc_eh");
284 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
285 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000286 if (isTargetIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000287 CmdArgs.push_back("-lgcc_s.1");
288 } else {
289 CmdArgs.push_back("-lgcc_s.10.5");
290 }
291 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
Daniel Dunbar8a0d94d2010-01-10 00:46:10 +0000292 Args.hasFlag(options::OPT_fexceptions,
293 options::OPT_fno_exceptions) ||
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000294 Args.hasArg(options::OPT_fgnu_runtime)) {
295 // FIXME: This is probably broken on 10.3?
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000296 if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000297 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000298 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000299 CmdArgs.push_back("-lgcc_s.10.5");
300 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000301 if (isMacosxVersionLT(10, 3, 9))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000302 ; // Do nothing.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000303 else if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000304 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000305 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000306 CmdArgs.push_back("-lgcc_s.10.5");
307 }
308
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000309 if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000310 CmdArgs.push_back("-lgcc");
311 CmdArgs.push_back("-lSystem");
312 } else {
313 CmdArgs.push_back("-lSystem");
314 CmdArgs.push_back("-lgcc");
315 }
316 }
317}
318
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000319DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
Daniel Dunbarcc8e1892010-01-27 00:56:44 +0000320 const unsigned (&DarwinVersion)[3])
321 : Darwin(Host, Triple, DarwinVersion)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000322{
323 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000324 getProgramPaths().push_back(getDriver().getInstalledDir());
325 if (getDriver().getInstalledDir() != getDriver().Dir)
326 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000327}
328
329void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
330 ArgStringList &CmdArgs) const {
331 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000332
333 // Unfortunately, we still might depend on a few of the libraries that are
334 // only available in the gcc library directory (in particular
335 // libstdc++.dylib). For now, hardcode the path to the known install location.
336 llvm::sys::Path P(getDriver().Dir);
337 P.eraseComponent(); // .../usr/bin -> ../usr
338 P.appendComponent("lib");
339 P.appendComponent("gcc");
340 switch (getTriple().getArch()) {
341 default:
342 assert(0 && "Invalid Darwin arch!");
343 case llvm::Triple::x86:
344 case llvm::Triple::x86_64:
345 P.appendComponent("i686-apple-darwin10");
346 break;
347 case llvm::Triple::arm:
348 case llvm::Triple::thumb:
349 P.appendComponent("arm-apple-darwin10");
350 break;
351 case llvm::Triple::ppc:
352 case llvm::Triple::ppc64:
353 P.appendComponent("powerpc-apple-darwin10");
354 break;
355 }
356 P.appendComponent("4.2.1");
357 if (P.exists())
358 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000359}
360
361void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
362 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000363 // Darwin doesn't support real static executables, don't link any runtime
364 // libraries with -static.
365 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000366 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000367
368 // Reject -static-libgcc for now, we can deal with this when and if someone
369 // cares. This is useful in situations where someone wants to statically link
370 // something like libstdc++, and needs its runtime support routines.
371 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000372 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000373 << A->getAsString(Args);
374 return;
375 }
376
Daniel Dunbareec99102010-01-22 03:38:14 +0000377 // Otherwise link libSystem, then the dynamic runtime library, and finally any
378 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000379 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000380
381 // Select the dynamic runtime library and the target specific static library.
382 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000383 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000384 CmdArgs.push_back("-lgcc_s.1");
385
386 // We may need some static functions for armv6/thumb which are required to
387 // be in the same linkage unit as their caller.
388 if (getDarwinArchName(Args) == "armv6")
389 DarwinStaticLib = "libclang_rt.armv6.a";
390 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000391 // The dynamic runtime library was merged with libSystem for 10.6 and
392 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000393 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000394 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000395 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000396 CmdArgs.push_back("-lgcc_s.10.5");
397
398 // For OS X, we only need a static runtime library when targetting 10.4, to
399 // provide versions of the static functions which were omitted from
400 // 10.4.dylib.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000401 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000402 DarwinStaticLib = "libclang_rt.10.4.a";
403 }
404
405 /// Add the target specific static library, if needed.
406 if (DarwinStaticLib) {
407 llvm::sys::Path P(getDriver().ResourceDir);
408 P.appendComponent("lib");
409 P.appendComponent("darwin");
410 P.appendComponent(DarwinStaticLib);
411
412 // For now, allow missing resource libraries to support developers who may
413 // not have compiler-rt checked out or integrated into their build.
414 if (!P.exists())
415 getDriver().Diag(clang::diag::warn_drv_missing_resource_library)
416 << P.str();
417 else
418 CmdArgs.push_back(Args.MakeArgString(P.str()));
419 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000420}
421
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000422void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000423 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000424
Daniel Dunbar26031372010-01-27 00:56:25 +0000425 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
426 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000427 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000428 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000429 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000430 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000431 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000432 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000433 // If neither OS X nor iPhoneOS targets were specified, check for
434 // environment defines.
435 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
436 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000437
Daniel Dunbar816bc312010-01-26 01:45:19 +0000438 // Ignore empty strings.
439 if (OSXTarget && OSXTarget[0] == '\0')
440 OSXTarget = 0;
441 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
442 iPhoneOSTarget = 0;
443
Daniel Dunbar39053672010-02-02 17:31:12 +0000444 // Diagnose conflicting deployment targets, and choose default platform
445 // based on the tool chain.
446 //
447 // FIXME: Don't hardcode default here.
448 if (OSXTarget && iPhoneOSTarget) {
449 // FIXME: We should see if we can get away with warning or erroring on
450 // this. Perhaps put under -pedantic?
451 if (getTriple().getArch() == llvm::Triple::arm ||
452 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000453 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000454 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000455 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000456 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000457
Daniel Dunbar39053672010-02-02 17:31:12 +0000458 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000459 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000460 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
461 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000462 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000463 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000464 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
465 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000466 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000467 // Otherwise, assume we are targeting OS X.
468 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000469 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
470 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000471 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000472 }
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Daniel Dunbar26031372010-01-27 00:56:25 +0000474 // Set the tool chain target information.
475 unsigned Major, Minor, Micro;
476 bool HadExtra;
477 if (OSXVersion) {
478 assert(!iPhoneVersion && "Unknown target platform!");
479 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
480 Micro, HadExtra) || HadExtra ||
481 Major != 10 || Minor >= 10 || Micro >= 10)
482 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
483 << OSXVersion->getAsString(Args);
484 } else {
485 assert(iPhoneVersion && "Unknown target platform!");
486 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
487 Micro, HadExtra) || HadExtra ||
488 Major >= 10 || Minor >= 100 || Micro >= 100)
489 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
490 << iPhoneVersion->getAsString(Args);
491 }
492 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000493}
494
495DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
496 const char *BoundArch) const {
497 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
498 const OptTable &Opts = getDriver().getOpts();
499
500 // FIXME: We really want to get out of the tool chain level argument
501 // translation business, as it makes the driver functionality much
502 // more opaque. For now, we follow gcc closely solely for the
503 // purpose of easily achieving feature parity & testability. Once we
504 // have something that works, we should reevaluate each translation
505 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000506
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000507 for (ArgList::const_iterator it = Args.begin(),
508 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000509 Arg *A = *it;
510
511 if (A->getOption().matches(options::OPT_Xarch__)) {
512 // FIXME: Canonicalize name.
513 if (getArchName() != A->getValue(Args, 0))
514 continue;
515
Daniel Dunbar0e100312010-06-14 21:23:08 +0000516 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
517 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000518 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000520 // If the argument parsing failed or more than one argument was
521 // consumed, the -Xarch_ argument's parameter tried to consume
522 // extra arguments. Emit an error and ignore.
523 //
524 // We also want to disallow any options which would alter the
525 // driver behavior; that isn't going to work in our model. We
526 // use isDriverOption() as an approximation, although things
527 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000528 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000529 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000530 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000531 << A->getAsString(Args);
532 continue;
533 }
534
Daniel Dunbar478edc22009-03-29 22:29:05 +0000535 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000536 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000537
538 DAL->AddSynthesizedArg(A);
Mike Stump1eb44332009-09-09 15:08:12 +0000539 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000540
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000541 // Sob. These is strictly gcc compatible for the time being. Apple
542 // gcc translates options twice, which means that self-expanding
543 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000544 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000545 default:
546 DAL->append(A);
547 break;
548
549 case options::OPT_mkernel:
550 case options::OPT_fapple_kext:
551 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000552 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
553 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000554 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000556 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000557 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
558 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000559 break;
560
561 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000562 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
563 DAL->AddFlagArg(A,
564 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000565 break;
566
567 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000568 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
569 DAL->AddFlagArg(A,
570 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000571 break;
572
573 case options::OPT_fterminated_vtables:
574 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000575 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
576 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000577 break;
578
579 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000580 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000581 break;
582
583 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000584 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000585 break;
586
587 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000588 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000589 break;
590
591 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000592 DAL->AddFlagArg(A,
593 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000594 break;
595
596 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000597 DAL->AddFlagArg(A,
598 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000599 break;
600
601 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000602 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000603 break;
604
605 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000606 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000607 break;
608 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000609 }
610
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000611 if (getTriple().getArch() == llvm::Triple::x86 ||
612 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000613 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000614 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000615
616 // Add the arch options based on the particular spelling of -arch, to match
617 // how the driver driver works.
618 if (BoundArch) {
619 llvm::StringRef Name = BoundArch;
620 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
621 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
622
623 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
624 // which defines the list of which architectures we accept.
625 if (Name == "ppc")
626 ;
627 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000628 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000629 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000630 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000631 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000632 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000633 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000634 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000635 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000636 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000637 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000638 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000639 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000640 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000641 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000642 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000643
644 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000645 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000646
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000647 else if (Name == "i386")
648 ;
649 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000650 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000651 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000652 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000653 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000654 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000655 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000656 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000657 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000658 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000659 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000660 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000661 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000662 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000663
664 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000665 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000666
667 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000668 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000669 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000670 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000671 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000672 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000673 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000674 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000675 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000676 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000677 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000678 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000679
680 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000681 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000682 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000683
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000684 // Add an explicit version min argument for the deployment target. We do this
685 // after argument translation because -Xarch_ arguments may add a version min
686 // argument.
687 AddDeploymentTarget(*DAL);
688
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000689 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000690}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000691
Daniel Dunbarf3955282009-09-04 18:34:51 +0000692bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000693 // FIXME: Gross; we should probably have some separate target
694 // definition, possibly even reusing the one in clang.
695 return getArchName() == "x86_64";
696}
697
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000698bool Darwin::UseDwarfDebugFlags() const {
699 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
700 return S[0] != '\0';
701 return false;
702}
703
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000704bool Darwin::UseSjLjExceptions() const {
705 // Darwin uses SjLj exceptions on ARM.
706 return (getTriple().getArch() == llvm::Triple::arm ||
707 getTriple().getArch() == llvm::Triple::thumb);
708}
709
Daniel Dunbarf3955282009-09-04 18:34:51 +0000710const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000711 return "pic";
712}
713
Daniel Dunbarf3955282009-09-04 18:34:51 +0000714const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000715 if (getArchName() == "x86_64")
716 return "pic";
717 return 0;
718}
719
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000720bool Darwin::SupportsObjCGC() const {
721 // Garbage collection is supported everywhere except on iPhone OS.
722 return !isTargetIPhoneOS();
723}
724
Daniel Dunbar39176082009-03-20 00:20:03 +0000725/// Generic_GCC - A tool chain using the 'gcc' command to perform
726/// all subcommands; this relies on gcc translating the majority of
727/// command line options.
728
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000729Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000730 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000731 getProgramPaths().push_back(getDriver().getInstalledDir());
732 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
733 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000734}
735
Daniel Dunbar39176082009-03-20 00:20:03 +0000736Generic_GCC::~Generic_GCC() {
737 // Free tool implementations.
738 for (llvm::DenseMap<unsigned, Tool*>::iterator
739 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
740 delete it->second;
741}
742
Mike Stump1eb44332009-09-09 15:08:12 +0000743Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000744 const JobAction &JA) const {
745 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000746 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000747 Key = Action::AnalyzeJobClass;
748 else
749 Key = JA.getKind();
750
751 Tool *&T = Tools[Key];
752 if (!T) {
753 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000754 case Action::InputClass:
755 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000756 assert(0 && "Invalid tool kind.");
757 case Action::PreprocessJobClass:
758 T = new tools::gcc::Preprocess(*this); break;
759 case Action::PrecompileJobClass:
760 T = new tools::gcc::Precompile(*this); break;
761 case Action::AnalyzeJobClass:
762 T = new tools::Clang(*this); break;
763 case Action::CompileJobClass:
764 T = new tools::gcc::Compile(*this); break;
765 case Action::AssembleJobClass:
766 T = new tools::gcc::Assemble(*this); break;
767 case Action::LinkJobClass:
768 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000770 // This is a bit ungeneric, but the only platform using a driver
771 // driver is Darwin.
772 case Action::LipoJobClass:
773 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000774 case Action::DsymutilJobClass:
775 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000776 }
777 }
778
779 return *T;
780}
781
Daniel Dunbar39176082009-03-20 00:20:03 +0000782bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000783 // FIXME: Gross; we should probably have some separate target
784 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000785 return getArchName() == "x86_64";
786}
787
788const char *Generic_GCC::GetDefaultRelocationModel() const {
789 return "static";
790}
791
792const char *Generic_GCC::GetForcedPicModel() const {
793 return 0;
794}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000795
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000796/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
797/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
798/// Currently does not support anything else but compilation.
799
800TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
801 : ToolChain(Host, Triple) {
802 // Path mangling to find libexec
803 std::string Path(getDriver().Dir);
804
805 Path += "/../libexec";
806 getProgramPaths().push_back(Path);
807}
808
809TCEToolChain::~TCEToolChain() {
810 for (llvm::DenseMap<unsigned, Tool*>::iterator
811 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
812 delete it->second;
813}
814
815bool TCEToolChain::IsMathErrnoDefault() const {
816 return true;
817}
818
819bool TCEToolChain::IsUnwindTablesDefault() const {
820 return false;
821}
822
823const char *TCEToolChain::GetDefaultRelocationModel() const {
824 return "static";
825}
826
827const char *TCEToolChain::GetForcedPicModel() const {
828 return 0;
829}
830
831Tool &TCEToolChain::SelectTool(const Compilation &C,
832 const JobAction &JA) const {
833 Action::ActionClass Key;
834 Key = Action::AnalyzeJobClass;
835
836 Tool *&T = Tools[Key];
837 if (!T) {
838 switch (Key) {
839 case Action::PreprocessJobClass:
840 T = new tools::gcc::Preprocess(*this); break;
841 case Action::AnalyzeJobClass:
842 T = new tools::Clang(*this); break;
843 default:
844 assert(false && "Unsupported action for TCE target.");
845 }
846 }
847 return *T;
848}
849
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000850/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
851
852OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
853 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000854 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000855 getFilePaths().push_back("/usr/lib");
856}
857
858Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
859 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000860 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000861 Key = Action::AnalyzeJobClass;
862 else
863 Key = JA.getKind();
864
865 Tool *&T = Tools[Key];
866 if (!T) {
867 switch (Key) {
868 case Action::AssembleJobClass:
869 T = new tools::openbsd::Assemble(*this); break;
870 case Action::LinkJobClass:
871 T = new tools::openbsd::Link(*this); break;
872 default:
873 T = &Generic_GCC::SelectTool(C, JA);
874 }
875 }
876
877 return *T;
878}
879
Daniel Dunbar75358d22009-03-30 21:06:03 +0000880/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
881
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000882FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
883 : Generic_GCC(Host, Triple) {
Daniel Dunbar7fb2c252010-06-15 15:03:31 +0000884 getProgramPaths().push_back(getDriver().Dir + "/../libexec");
885 getProgramPaths().push_back("/usr/libexec");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000886 if (Lib32) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000887 getFilePaths().push_back(getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000888 getFilePaths().push_back("/usr/lib32");
889 } else {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000890 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000891 getFilePaths().push_back("/usr/lib");
892 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000893}
894
895Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
896 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000897 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000898 Key = Action::AnalyzeJobClass;
899 else
900 Key = JA.getKind();
901
902 Tool *&T = Tools[Key];
903 if (!T) {
904 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000905 case Action::AssembleJobClass:
906 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000907 case Action::LinkJobClass:
908 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000909 default:
910 T = &Generic_GCC::SelectTool(C, JA);
911 }
912 }
913
914 return *T;
915}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000916
Chris Lattner38e317d2010-07-07 16:01:42 +0000917/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
918
919Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
920 : Generic_GCC(Host, Triple) {
921 getFilePaths().push_back(getDriver().Dir + "/../lib");
922 getFilePaths().push_back("/usr/lib");
923 getFilePaths().push_back("/usr/gnu/lib");
924 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
925}
926
927Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
928 Action::ActionClass Key;
929 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
930 Key = Action::AnalyzeJobClass;
931 else
932 Key = JA.getKind();
933
934 Tool *&T = Tools[Key];
935 if (!T) {
936 switch (Key) {
937 case Action::AssembleJobClass:
938 T = new tools::minix::Assemble(*this); break;
939 case Action::LinkJobClass:
940 T = new tools::minix::Link(*this); break;
941 default:
942 T = &Generic_GCC::SelectTool(C, JA);
943 }
944 }
945
946 return *T;
947}
948
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000949/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
950
951AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
952 : Generic_GCC(Host, Triple) {
953
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000954 getProgramPaths().push_back(getDriver().getInstalledDir());
955 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
956 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000957
Daniel Dunbaree788e72009-12-21 18:54:17 +0000958 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000959 getFilePaths().push_back("/usr/lib");
960 getFilePaths().push_back("/usr/sfw/lib");
961 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000962 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000963
964}
965
966Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
967 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000968 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000969 Key = Action::AnalyzeJobClass;
970 else
971 Key = JA.getKind();
972
973 Tool *&T = Tools[Key];
974 if (!T) {
975 switch (Key) {
976 case Action::AssembleJobClass:
977 T = new tools::auroraux::Assemble(*this); break;
978 case Action::LinkJobClass:
979 T = new tools::auroraux::Link(*this); break;
980 default:
981 T = &Generic_GCC::SelectTool(C, JA);
982 }
983 }
984
985 return *T;
986}
987
988
Eli Friedman6b3454a2009-05-26 07:52:18 +0000989/// Linux toolchain (very bare-bones at the moment).
990
991Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
992 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000993 getFilePaths().push_back(getDriver().Dir + "/../lib/clang/1.0/");
Eli Friedman6b3454a2009-05-26 07:52:18 +0000994 getFilePaths().push_back("/lib/");
995 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +0000996
997 // Depending on the Linux distribution, any combination of lib{,32,64} is
998 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
999 // openSUSE uses lib and lib64 for the same purpose.
1000 getFilePaths().push_back("/lib32/");
1001 getFilePaths().push_back("/usr/lib32/");
1002 getFilePaths().push_back("/lib64/");
1003 getFilePaths().push_back("/usr/lib64/");
1004
Eli Friedman6b3454a2009-05-26 07:52:18 +00001005 // FIXME: Figure out some way to get gcc's libdir
1006 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
1007 // crtbegin.o/crtend.o/etc., and want static versions of various
1008 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
1009 // get away with using shared versions in /usr/lib, though.
1010 // We could fall back to the approach we used for includes (a massive
1011 // list), but that's messy at best.
1012}
1013
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001014/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1015
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001016DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
1017 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001018
1019 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001020 getProgramPaths().push_back(getDriver().getInstalledDir());
1021 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1022 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001023
Daniel Dunbaree788e72009-12-21 18:54:17 +00001024 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001025 getFilePaths().push_back("/usr/lib");
1026 getFilePaths().push_back("/usr/lib/gcc41");
1027}
1028
1029Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
1030 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001031 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001032 Key = Action::AnalyzeJobClass;
1033 else
1034 Key = JA.getKind();
1035
1036 Tool *&T = Tools[Key];
1037 if (!T) {
1038 switch (Key) {
1039 case Action::AssembleJobClass:
1040 T = new tools::dragonfly::Assemble(*this); break;
1041 case Action::LinkJobClass:
1042 T = new tools::dragonfly::Link(*this); break;
1043 default:
1044 T = &Generic_GCC::SelectTool(C, JA);
1045 }
1046 }
1047
1048 return *T;
1049}