blob: 1684fa9424e14736df9e8f400e5705f420e9a56a [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 Dunbar41800112010-08-02 05:43:56 +000043types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
44 types::ID Ty = types::lookupTypeForExtension(Ext);
45
46 // Darwin always preprocesses assembly files (unless -x is used explicitly).
47 if (Ty == types::TY_PP_Asm)
48 return types::TY_Asm;
49
50 return Ty;
51}
52
Daniel Dunbareeff4062010-01-22 02:04:58 +000053// FIXME: Can we tablegen this?
54static const char *GetArmArchForMArch(llvm::StringRef Value) {
55 if (Value == "armv6k")
56 return "armv6";
57
58 if (Value == "armv5tej")
59 return "armv5";
60
61 if (Value == "xscale")
62 return "xscale";
63
64 if (Value == "armv4t")
65 return "armv4t";
66
67 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
68 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
69 Value == "armv7m")
70 return "armv7";
71
72 return 0;
73}
74
75// FIXME: Can we tablegen this?
76static const char *GetArmArchForMCpu(llvm::StringRef Value) {
77 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
78 Value == "arm946e-s" || Value == "arm966e-s" ||
79 Value == "arm968e-s" || Value == "arm10e" ||
80 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
81 Value == "arm1026ej-s")
82 return "armv5";
83
84 if (Value == "xscale")
85 return "xscale";
86
87 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
88 Value == "arm1176jz-s" || Value == "arm1176jzf-s")
89 return "armv6";
90
91 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
92 return "armv7";
93
94 return 0;
95}
96
97llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
98 switch (getTriple().getArch()) {
99 default:
100 return getArchName();
101
102 case llvm::Triple::arm: {
103 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
104 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
105 return Arch;
106
107 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
108 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
109 return Arch;
110
111 return "arm";
112 }
113 }
114}
115
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000116DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
Daniel Dunbarc2bda622010-08-02 05:44:01 +0000117 const unsigned (&DarwinVersion)[3])
Daniel Dunbarcc8e1892010-01-27 00:56:44 +0000118 : Darwin(Host, Triple, DarwinVersion)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000119{
Daniel Dunbarc2bda622010-08-02 05:44:01 +0000120 // We can only work with 4.2.1 currently.
121 GCCVersion[0] = 4;
122 GCCVersion[1] = 2;
123 GCCVersion[2] = 1;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000124
125 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000126 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +0000127 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000128 ToolChainDir += "/";
129 ToolChainDir += llvm::utostr(GCCVersion[0]);
130 ToolChainDir += '.';
131 ToolChainDir += llvm::utostr(GCCVersion[1]);
132 ToolChainDir += '.';
133 ToolChainDir += llvm::utostr(GCCVersion[2]);
134
Daniel Dunbar74782b02009-10-20 19:25:43 +0000135 // Try the next major version if that tool chain dir is invalid.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000136 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
137 if (!llvm::sys::Path(Tmp).exists()) {
Daniel Dunbar74782b02009-10-20 19:25:43 +0000138 std::string Next = "i686-apple-darwin";
139 Next += llvm::utostr(DarwinVersion[0] + 1);
140 Next += "/";
141 Next += llvm::utostr(GCCVersion[0]);
142 Next += '.';
143 Next += llvm::utostr(GCCVersion[1]);
144 Next += '.';
145 Next += llvm::utostr(GCCVersion[2]);
146
147 // Use that if it exists, otherwise hope the user isn't linking.
148 //
149 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000150 Tmp = "/usr/lib/gcc/" + Next;
151 if (llvm::sys::Path(Tmp).exists())
Daniel Dunbar74782b02009-10-20 19:25:43 +0000152 ToolChainDir = Next;
153 }
154
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000155 std::string Path;
156 if (getArchName() == "x86_64") {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000157 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000158 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000159 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000160 Path += "/x86_64";
161 getFilePaths().push_back(Path);
162
163 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000164 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000165 Path += "/x86_64";
166 getFilePaths().push_back(Path);
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Daniel Dunbaree788e72009-12-21 18:54:17 +0000169 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000170 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000171 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000172 getFilePaths().push_back(Path);
173
174 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000175 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000176 getFilePaths().push_back(Path);
177
Daniel Dunbaree788e72009-12-21 18:54:17 +0000178 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000179 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000180 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000181 getProgramPaths().push_back(Path);
182
183 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000184 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000185 getProgramPaths().push_back(Path);
186
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000187 getProgramPaths().push_back(getDriver().getInstalledDir());
188 if (getDriver().getInstalledDir() != getDriver().Dir)
189 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000190}
191
Daniel Dunbarf3955282009-09-04 18:34:51 +0000192Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000193 // Free tool implementations.
194 for (llvm::DenseMap<unsigned, Tool*>::iterator
195 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
196 delete it->second;
197}
198
Daniel Dunbarf3955282009-09-04 18:34:51 +0000199Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000200 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000201 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000202 Key = Action::AnalyzeJobClass;
203 else
204 Key = JA.getKind();
205
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000206 // FIXME: This doesn't belong here, but ideally we will support static soon
207 // anyway.
208 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
209 C.getArgs().hasArg(options::OPT_static) ||
210 C.getArgs().hasArg(options::OPT_fapple_kext));
211 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
212 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
213 options::OPT_no_integrated_as,
214 IsIADefault);
215
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000216 Tool *&T = Tools[Key];
217 if (!T) {
218 switch (Key) {
219 case Action::InputClass:
220 case Action::BindArchClass:
221 assert(0 && "Invalid tool kind.");
222 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000223 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000224 case Action::AnalyzeJobClass:
225 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000226 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000227 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000228 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000229 case Action::AssembleJobClass: {
230 if (UseIntegratedAs)
231 T = new tools::ClangAs(*this);
232 else
233 T = new tools::darwin::Assemble(*this);
234 break;
235 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000236 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000237 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000238 case Action::LipoJobClass:
239 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000240 case Action::DsymutilJobClass:
241 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000242 }
243 }
244
245 return *T;
246}
247
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000248void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
249 ArgStringList &CmdArgs) const {
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000250 std::string Tmp;
251
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000252 // FIXME: Derive these correctly.
253 if (getArchName() == "x86_64") {
254 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
255 "/x86_64"));
256 // Intentionally duplicated for (temporary) gcc bug compatibility.
257 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
258 "/x86_64"));
259 }
Daniel Dunbar8bc9c552010-04-10 01:24:22 +0000260
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000261 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000262
263 Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir;
264 if (llvm::sys::Path(Tmp).exists())
265 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
266 Tmp = getDriver().Dir + "/../lib/gcc";
267 if (llvm::sys::Path(Tmp).exists())
268 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000269 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
270 // Intentionally duplicated for (temporary) gcc bug compatibility.
271 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000272 Tmp = getDriver().Dir + "/../lib/" + ToolChainDir;
273 if (llvm::sys::Path(Tmp).exists())
274 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
275 Tmp = getDriver().Dir + "/../lib";
276 if (llvm::sys::Path(Tmp).exists())
277 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000278 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
279 "/../../../" + ToolChainDir));
280 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
281 "/../../.."));
282}
283
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000284void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
285 ArgStringList &CmdArgs) const {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000286 // Note that this routine is only used for targetting OS X.
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000287
288 // Derived from libgcc and lib specs but refactored.
289 if (Args.hasArg(options::OPT_static)) {
290 CmdArgs.push_back("-lgcc_static");
291 } else {
292 if (Args.hasArg(options::OPT_static_libgcc)) {
293 CmdArgs.push_back("-lgcc_eh");
294 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
295 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000296 if (isTargetIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000297 CmdArgs.push_back("-lgcc_s.1");
298 } else {
299 CmdArgs.push_back("-lgcc_s.10.5");
300 }
301 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
Daniel Dunbar8a0d94d2010-01-10 00:46:10 +0000302 Args.hasFlag(options::OPT_fexceptions,
303 options::OPT_fno_exceptions) ||
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000304 Args.hasArg(options::OPT_fgnu_runtime)) {
305 // FIXME: This is probably broken on 10.3?
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000306 if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000307 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000308 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000309 CmdArgs.push_back("-lgcc_s.10.5");
310 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000311 if (isMacosxVersionLT(10, 3, 9))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000312 ; // Do nothing.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000313 else if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000314 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000315 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000316 CmdArgs.push_back("-lgcc_s.10.5");
317 }
318
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000319 if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000320 CmdArgs.push_back("-lgcc");
321 CmdArgs.push_back("-lSystem");
322 } else {
323 CmdArgs.push_back("-lSystem");
324 CmdArgs.push_back("-lgcc");
325 }
326 }
327}
328
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000329DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
Daniel Dunbarcc8e1892010-01-27 00:56:44 +0000330 const unsigned (&DarwinVersion)[3])
331 : Darwin(Host, Triple, DarwinVersion)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000332{
333 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000334 getProgramPaths().push_back(getDriver().getInstalledDir());
335 if (getDriver().getInstalledDir() != getDriver().Dir)
336 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000337}
338
339void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
340 ArgStringList &CmdArgs) const {
341 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000342
343 // Unfortunately, we still might depend on a few of the libraries that are
344 // only available in the gcc library directory (in particular
345 // libstdc++.dylib). For now, hardcode the path to the known install location.
346 llvm::sys::Path P(getDriver().Dir);
347 P.eraseComponent(); // .../usr/bin -> ../usr
348 P.appendComponent("lib");
349 P.appendComponent("gcc");
350 switch (getTriple().getArch()) {
351 default:
352 assert(0 && "Invalid Darwin arch!");
353 case llvm::Triple::x86:
354 case llvm::Triple::x86_64:
355 P.appendComponent("i686-apple-darwin10");
356 break;
357 case llvm::Triple::arm:
358 case llvm::Triple::thumb:
359 P.appendComponent("arm-apple-darwin10");
360 break;
361 case llvm::Triple::ppc:
362 case llvm::Triple::ppc64:
363 P.appendComponent("powerpc-apple-darwin10");
364 break;
365 }
366 P.appendComponent("4.2.1");
367 if (P.exists())
368 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000369}
370
371void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
372 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000373 // Darwin doesn't support real static executables, don't link any runtime
374 // libraries with -static.
375 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000376 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000377
378 // Reject -static-libgcc for now, we can deal with this when and if someone
379 // cares. This is useful in situations where someone wants to statically link
380 // something like libstdc++, and needs its runtime support routines.
381 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000382 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000383 << A->getAsString(Args);
384 return;
385 }
386
Daniel Dunbareec99102010-01-22 03:38:14 +0000387 // Otherwise link libSystem, then the dynamic runtime library, and finally any
388 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000389 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000390
391 // Select the dynamic runtime library and the target specific static library.
392 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000393 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000394 CmdArgs.push_back("-lgcc_s.1");
395
396 // We may need some static functions for armv6/thumb which are required to
397 // be in the same linkage unit as their caller.
398 if (getDarwinArchName(Args) == "armv6")
399 DarwinStaticLib = "libclang_rt.armv6.a";
400 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000401 // The dynamic runtime library was merged with libSystem for 10.6 and
402 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000403 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000404 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000405 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000406 CmdArgs.push_back("-lgcc_s.10.5");
407
408 // For OS X, we only need a static runtime library when targetting 10.4, to
409 // provide versions of the static functions which were omitted from
410 // 10.4.dylib.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000411 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000412 DarwinStaticLib = "libclang_rt.10.4.a";
413 }
414
415 /// Add the target specific static library, if needed.
416 if (DarwinStaticLib) {
417 llvm::sys::Path P(getDriver().ResourceDir);
418 P.appendComponent("lib");
419 P.appendComponent("darwin");
420 P.appendComponent(DarwinStaticLib);
421
422 // For now, allow missing resource libraries to support developers who may
423 // not have compiler-rt checked out or integrated into their build.
424 if (!P.exists())
425 getDriver().Diag(clang::diag::warn_drv_missing_resource_library)
426 << P.str();
427 else
428 CmdArgs.push_back(Args.MakeArgString(P.str()));
429 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000430}
431
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000432void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000433 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000434
Daniel Dunbar26031372010-01-27 00:56:25 +0000435 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
436 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000437 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000438 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000439 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000440 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000441 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000442 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000443 // If neither OS X nor iPhoneOS targets were specified, check for
444 // environment defines.
445 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
446 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000447
Daniel Dunbar816bc312010-01-26 01:45:19 +0000448 // Ignore empty strings.
449 if (OSXTarget && OSXTarget[0] == '\0')
450 OSXTarget = 0;
451 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
452 iPhoneOSTarget = 0;
453
Daniel Dunbar39053672010-02-02 17:31:12 +0000454 // Diagnose conflicting deployment targets, and choose default platform
455 // based on the tool chain.
456 //
457 // FIXME: Don't hardcode default here.
458 if (OSXTarget && iPhoneOSTarget) {
459 // FIXME: We should see if we can get away with warning or erroring on
460 // this. Perhaps put under -pedantic?
461 if (getTriple().getArch() == llvm::Triple::arm ||
462 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000463 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000464 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000465 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000466 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000467
Daniel Dunbar39053672010-02-02 17:31:12 +0000468 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000469 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000470 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
471 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000472 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000473 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000474 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
475 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000476 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000477 // Otherwise, assume we are targeting OS X.
478 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000479 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
480 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000481 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Daniel Dunbar26031372010-01-27 00:56:25 +0000484 // Set the tool chain target information.
485 unsigned Major, Minor, Micro;
486 bool HadExtra;
487 if (OSXVersion) {
488 assert(!iPhoneVersion && "Unknown target platform!");
489 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
490 Micro, HadExtra) || HadExtra ||
491 Major != 10 || Minor >= 10 || Micro >= 10)
492 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
493 << OSXVersion->getAsString(Args);
494 } else {
495 assert(iPhoneVersion && "Unknown target platform!");
496 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
497 Micro, HadExtra) || HadExtra ||
498 Major >= 10 || Minor >= 100 || Micro >= 100)
499 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
500 << iPhoneVersion->getAsString(Args);
501 }
502 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000503}
504
505DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
506 const char *BoundArch) const {
507 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
508 const OptTable &Opts = getDriver().getOpts();
509
510 // FIXME: We really want to get out of the tool chain level argument
511 // translation business, as it makes the driver functionality much
512 // more opaque. For now, we follow gcc closely solely for the
513 // purpose of easily achieving feature parity & testability. Once we
514 // have something that works, we should reevaluate each translation
515 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000516
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000517 for (ArgList::const_iterator it = Args.begin(),
518 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000519 Arg *A = *it;
520
521 if (A->getOption().matches(options::OPT_Xarch__)) {
522 // FIXME: Canonicalize name.
523 if (getArchName() != A->getValue(Args, 0))
524 continue;
525
Daniel Dunbar0e100312010-06-14 21:23:08 +0000526 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
527 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000528 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000530 // If the argument parsing failed or more than one argument was
531 // consumed, the -Xarch_ argument's parameter tried to consume
532 // extra arguments. Emit an error and ignore.
533 //
534 // We also want to disallow any options which would alter the
535 // driver behavior; that isn't going to work in our model. We
536 // use isDriverOption() as an approximation, although things
537 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000538 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000539 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000540 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000541 << A->getAsString(Args);
542 continue;
543 }
544
Daniel Dunbar478edc22009-03-29 22:29:05 +0000545 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000546 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000547
548 DAL->AddSynthesizedArg(A);
Mike Stump1eb44332009-09-09 15:08:12 +0000549 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000550
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000551 // Sob. These is strictly gcc compatible for the time being. Apple
552 // gcc translates options twice, which means that self-expanding
553 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000554 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000555 default:
556 DAL->append(A);
557 break;
558
559 case options::OPT_mkernel:
560 case options::OPT_fapple_kext:
561 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000562 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
563 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000564 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000566 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000567 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
568 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000569 break;
570
571 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000572 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
573 DAL->AddFlagArg(A,
574 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000575 break;
576
577 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000578 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
579 DAL->AddFlagArg(A,
580 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000581 break;
582
583 case options::OPT_fterminated_vtables:
584 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000585 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
586 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000587 break;
588
589 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000590 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000591 break;
592
593 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000594 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000595 break;
596
597 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000598 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000599 break;
600
601 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000602 DAL->AddFlagArg(A,
603 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000604 break;
605
606 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000607 DAL->AddFlagArg(A,
608 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000609 break;
610
611 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000612 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000613 break;
614
615 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000616 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000617 break;
618 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000619 }
620
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000621 if (getTriple().getArch() == llvm::Triple::x86 ||
622 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000623 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000624 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000625
626 // Add the arch options based on the particular spelling of -arch, to match
627 // how the driver driver works.
628 if (BoundArch) {
629 llvm::StringRef Name = BoundArch;
630 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
631 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
632
633 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
634 // which defines the list of which architectures we accept.
635 if (Name == "ppc")
636 ;
637 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000638 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000639 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000640 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000641 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000642 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000643 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000644 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000645 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000646 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000647 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000648 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000649 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000650 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000651 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000652 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000653
654 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000655 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000656
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000657 else if (Name == "i386")
658 ;
659 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000660 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000661 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000662 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000663 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000664 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000665 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000666 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000667 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000668 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000669 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000670 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000671 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000672 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000673
674 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000675 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000676
677 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000678 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000679 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000680 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000681 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000682 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000683 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000684 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000685 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000686 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000687 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000688 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000689
690 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000691 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000692 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000693
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000694 // Add an explicit version min argument for the deployment target. We do this
695 // after argument translation because -Xarch_ arguments may add a version min
696 // argument.
697 AddDeploymentTarget(*DAL);
698
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000699 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000700}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000701
Daniel Dunbarf3955282009-09-04 18:34:51 +0000702bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000703 // FIXME: Gross; we should probably have some separate target
704 // definition, possibly even reusing the one in clang.
705 return getArchName() == "x86_64";
706}
707
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000708bool Darwin::UseDwarfDebugFlags() const {
709 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
710 return S[0] != '\0';
711 return false;
712}
713
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000714bool Darwin::UseSjLjExceptions() const {
715 // Darwin uses SjLj exceptions on ARM.
716 return (getTriple().getArch() == llvm::Triple::arm ||
717 getTriple().getArch() == llvm::Triple::thumb);
718}
719
Daniel Dunbarf3955282009-09-04 18:34:51 +0000720const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000721 return "pic";
722}
723
Daniel Dunbarf3955282009-09-04 18:34:51 +0000724const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000725 if (getArchName() == "x86_64")
726 return "pic";
727 return 0;
728}
729
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000730bool Darwin::SupportsObjCGC() const {
731 // Garbage collection is supported everywhere except on iPhone OS.
732 return !isTargetIPhoneOS();
733}
734
Daniel Dunbar39176082009-03-20 00:20:03 +0000735/// Generic_GCC - A tool chain using the 'gcc' command to perform
736/// all subcommands; this relies on gcc translating the majority of
737/// command line options.
738
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000739Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000740 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000741 getProgramPaths().push_back(getDriver().getInstalledDir());
742 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
743 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000744}
745
Daniel Dunbar39176082009-03-20 00:20:03 +0000746Generic_GCC::~Generic_GCC() {
747 // Free tool implementations.
748 for (llvm::DenseMap<unsigned, Tool*>::iterator
749 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
750 delete it->second;
751}
752
Mike Stump1eb44332009-09-09 15:08:12 +0000753Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000754 const JobAction &JA) const {
755 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000756 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000757 Key = Action::AnalyzeJobClass;
758 else
759 Key = JA.getKind();
760
761 Tool *&T = Tools[Key];
762 if (!T) {
763 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000764 case Action::InputClass:
765 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000766 assert(0 && "Invalid tool kind.");
767 case Action::PreprocessJobClass:
768 T = new tools::gcc::Preprocess(*this); break;
769 case Action::PrecompileJobClass:
770 T = new tools::gcc::Precompile(*this); break;
771 case Action::AnalyzeJobClass:
772 T = new tools::Clang(*this); break;
773 case Action::CompileJobClass:
774 T = new tools::gcc::Compile(*this); break;
775 case Action::AssembleJobClass:
776 T = new tools::gcc::Assemble(*this); break;
777 case Action::LinkJobClass:
778 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000780 // This is a bit ungeneric, but the only platform using a driver
781 // driver is Darwin.
782 case Action::LipoJobClass:
783 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000784 case Action::DsymutilJobClass:
785 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000786 }
787 }
788
789 return *T;
790}
791
Daniel Dunbar39176082009-03-20 00:20:03 +0000792bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000793 // FIXME: Gross; we should probably have some separate target
794 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000795 return getArchName() == "x86_64";
796}
797
798const char *Generic_GCC::GetDefaultRelocationModel() const {
799 return "static";
800}
801
802const char *Generic_GCC::GetForcedPicModel() const {
803 return 0;
804}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000805
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000806/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
807/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
808/// Currently does not support anything else but compilation.
809
810TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
811 : ToolChain(Host, Triple) {
812 // Path mangling to find libexec
813 std::string Path(getDriver().Dir);
814
815 Path += "/../libexec";
816 getProgramPaths().push_back(Path);
817}
818
819TCEToolChain::~TCEToolChain() {
820 for (llvm::DenseMap<unsigned, Tool*>::iterator
821 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
822 delete it->second;
823}
824
825bool TCEToolChain::IsMathErrnoDefault() const {
826 return true;
827}
828
829bool TCEToolChain::IsUnwindTablesDefault() const {
830 return false;
831}
832
833const char *TCEToolChain::GetDefaultRelocationModel() const {
834 return "static";
835}
836
837const char *TCEToolChain::GetForcedPicModel() const {
838 return 0;
839}
840
841Tool &TCEToolChain::SelectTool(const Compilation &C,
842 const JobAction &JA) const {
843 Action::ActionClass Key;
844 Key = Action::AnalyzeJobClass;
845
846 Tool *&T = Tools[Key];
847 if (!T) {
848 switch (Key) {
849 case Action::PreprocessJobClass:
850 T = new tools::gcc::Preprocess(*this); break;
851 case Action::AnalyzeJobClass:
852 T = new tools::Clang(*this); break;
853 default:
854 assert(false && "Unsupported action for TCE target.");
855 }
856 }
857 return *T;
858}
859
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000860/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
861
862OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
863 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000864 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000865 getFilePaths().push_back("/usr/lib");
866}
867
868Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
869 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000870 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000871 Key = Action::AnalyzeJobClass;
872 else
873 Key = JA.getKind();
874
875 Tool *&T = Tools[Key];
876 if (!T) {
877 switch (Key) {
878 case Action::AssembleJobClass:
879 T = new tools::openbsd::Assemble(*this); break;
880 case Action::LinkJobClass:
881 T = new tools::openbsd::Link(*this); break;
882 default:
883 T = &Generic_GCC::SelectTool(C, JA);
884 }
885 }
886
887 return *T;
888}
889
Daniel Dunbar75358d22009-03-30 21:06:03 +0000890/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
891
Daniel Dunbar214afe92010-08-02 05:43:59 +0000892FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000893 : Generic_GCC(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +0000894
895 // Determine if we are compiling 32-bit code on an x86_64 platform.
896 bool Lib32 = false;
897 if (Triple.getArch() == llvm::Triple::x86 &&
898 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
899 llvm::Triple::x86_64)
900 Lib32 = true;
901
Daniel Dunbar7fb2c252010-06-15 15:03:31 +0000902 getProgramPaths().push_back(getDriver().Dir + "/../libexec");
903 getProgramPaths().push_back("/usr/libexec");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000904 if (Lib32) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000905 getFilePaths().push_back(getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000906 getFilePaths().push_back("/usr/lib32");
907 } else {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000908 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000909 getFilePaths().push_back("/usr/lib");
910 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000911}
912
913Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
914 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000915 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000916 Key = Action::AnalyzeJobClass;
917 else
918 Key = JA.getKind();
919
920 Tool *&T = Tools[Key];
921 if (!T) {
922 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000923 case Action::AssembleJobClass:
924 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000925 case Action::LinkJobClass:
926 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000927 default:
928 T = &Generic_GCC::SelectTool(C, JA);
929 }
930 }
931
932 return *T;
933}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000934
Chris Lattner38e317d2010-07-07 16:01:42 +0000935/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
936
937Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
938 : Generic_GCC(Host, Triple) {
939 getFilePaths().push_back(getDriver().Dir + "/../lib");
940 getFilePaths().push_back("/usr/lib");
941 getFilePaths().push_back("/usr/gnu/lib");
942 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
943}
944
945Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
946 Action::ActionClass Key;
947 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
948 Key = Action::AnalyzeJobClass;
949 else
950 Key = JA.getKind();
951
952 Tool *&T = Tools[Key];
953 if (!T) {
954 switch (Key) {
955 case Action::AssembleJobClass:
956 T = new tools::minix::Assemble(*this); break;
957 case Action::LinkJobClass:
958 T = new tools::minix::Link(*this); break;
959 default:
960 T = &Generic_GCC::SelectTool(C, JA);
961 }
962 }
963
964 return *T;
965}
966
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000967/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
968
969AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
970 : Generic_GCC(Host, Triple) {
971
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000972 getProgramPaths().push_back(getDriver().getInstalledDir());
973 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
974 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000975
Daniel Dunbaree788e72009-12-21 18:54:17 +0000976 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000977 getFilePaths().push_back("/usr/lib");
978 getFilePaths().push_back("/usr/sfw/lib");
979 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000980 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000981
982}
983
984Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
985 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000986 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000987 Key = Action::AnalyzeJobClass;
988 else
989 Key = JA.getKind();
990
991 Tool *&T = Tools[Key];
992 if (!T) {
993 switch (Key) {
994 case Action::AssembleJobClass:
995 T = new tools::auroraux::Assemble(*this); break;
996 case Action::LinkJobClass:
997 T = new tools::auroraux::Link(*this); break;
998 default:
999 T = &Generic_GCC::SelectTool(C, JA);
1000 }
1001 }
1002
1003 return *T;
1004}
1005
1006
Eli Friedman6b3454a2009-05-26 07:52:18 +00001007/// Linux toolchain (very bare-bones at the moment).
1008
1009Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
1010 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001011 getFilePaths().push_back(getDriver().Dir + "/../lib/clang/1.0/");
Eli Friedman6b3454a2009-05-26 07:52:18 +00001012 getFilePaths().push_back("/lib/");
1013 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +00001014
1015 // Depending on the Linux distribution, any combination of lib{,32,64} is
1016 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
1017 // openSUSE uses lib and lib64 for the same purpose.
1018 getFilePaths().push_back("/lib32/");
1019 getFilePaths().push_back("/usr/lib32/");
1020 getFilePaths().push_back("/lib64/");
1021 getFilePaths().push_back("/usr/lib64/");
1022
Eli Friedman6b3454a2009-05-26 07:52:18 +00001023 // FIXME: Figure out some way to get gcc's libdir
1024 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
1025 // crtbegin.o/crtend.o/etc., and want static versions of various
1026 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
1027 // get away with using shared versions in /usr/lib, though.
1028 // We could fall back to the approach we used for includes (a massive
1029 // list), but that's messy at best.
1030}
1031
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001032/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1033
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001034DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
1035 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001036
1037 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001038 getProgramPaths().push_back(getDriver().getInstalledDir());
1039 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1040 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001041
Daniel Dunbaree788e72009-12-21 18:54:17 +00001042 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001043 getFilePaths().push_back("/usr/lib");
1044 getFilePaths().push_back("/usr/lib/gcc41");
1045}
1046
1047Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
1048 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001049 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001050 Key = Action::AnalyzeJobClass;
1051 else
1052 Key = JA.getKind();
1053
1054 Tool *&T = Tools[Key];
1055 if (!T) {
1056 switch (Key) {
1057 case Action::AssembleJobClass:
1058 T = new tools::dragonfly::Assemble(*this); break;
1059 case Action::LinkJobClass:
1060 T = new tools::dragonfly::Link(*this); break;
1061 default:
1062 T = &Generic_GCC::SelectTool(C, JA);
1063 }
1064 }
1065
1066 return *T;
1067}