blob: e9d6e34864367f101f229c834e6c9f2ae317bf4c [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 Dunbar25b58eb2010-08-02 05:44:07 +000034Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcc8e1892010-01-27 00:56:44 +000035 : ToolChain(Host, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000036{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000037 // Compute the initial Darwin version based on the host.
38 bool HadExtra;
39 std::string OSName = Triple.getOSName();
40 if (!Driver::GetReleaseVersion(&OSName[6],
41 DarwinVersion[0], DarwinVersion[1],
42 DarwinVersion[2], HadExtra))
43 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
44
Daniel Dunbar02633b52009-03-26 16:23:12 +000045 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000046 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
47 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000048}
49
Daniel Dunbar41800112010-08-02 05:43:56 +000050types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
51 types::ID Ty = types::lookupTypeForExtension(Ext);
52
53 // Darwin always preprocesses assembly files (unless -x is used explicitly).
54 if (Ty == types::TY_PP_Asm)
55 return types::TY_Asm;
56
57 return Ty;
58}
59
Daniel Dunbareeff4062010-01-22 02:04:58 +000060// FIXME: Can we tablegen this?
61static const char *GetArmArchForMArch(llvm::StringRef Value) {
62 if (Value == "armv6k")
63 return "armv6";
64
65 if (Value == "armv5tej")
66 return "armv5";
67
68 if (Value == "xscale")
69 return "xscale";
70
71 if (Value == "armv4t")
72 return "armv4t";
73
74 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
75 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
76 Value == "armv7m")
77 return "armv7";
78
79 return 0;
80}
81
82// FIXME: Can we tablegen this?
83static const char *GetArmArchForMCpu(llvm::StringRef Value) {
84 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
85 Value == "arm946e-s" || Value == "arm966e-s" ||
86 Value == "arm968e-s" || Value == "arm10e" ||
87 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
88 Value == "arm1026ej-s")
89 return "armv5";
90
91 if (Value == "xscale")
92 return "xscale";
93
94 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
95 Value == "arm1176jz-s" || Value == "arm1176jzf-s")
96 return "armv6";
97
98 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
99 return "armv7";
100
101 return 0;
102}
103
104llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
105 switch (getTriple().getArch()) {
106 default:
107 return getArchName();
108
109 case llvm::Triple::arm: {
110 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
111 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
112 return Arch;
113
114 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
115 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
116 return Arch;
117
118 return "arm";
119 }
120 }
121}
122
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000123DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple)
124 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000125{
Daniel Dunbarc2bda622010-08-02 05:44:01 +0000126 // We can only work with 4.2.1 currently.
127 GCCVersion[0] = 4;
128 GCCVersion[1] = 2;
129 GCCVersion[2] = 1;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000130
131 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000132 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +0000133 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000134 ToolChainDir += "/";
135 ToolChainDir += llvm::utostr(GCCVersion[0]);
136 ToolChainDir += '.';
137 ToolChainDir += llvm::utostr(GCCVersion[1]);
138 ToolChainDir += '.';
139 ToolChainDir += llvm::utostr(GCCVersion[2]);
140
Daniel Dunbar74782b02009-10-20 19:25:43 +0000141 // Try the next major version if that tool chain dir is invalid.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000142 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
143 if (!llvm::sys::Path(Tmp).exists()) {
Daniel Dunbar74782b02009-10-20 19:25:43 +0000144 std::string Next = "i686-apple-darwin";
145 Next += llvm::utostr(DarwinVersion[0] + 1);
146 Next += "/";
147 Next += llvm::utostr(GCCVersion[0]);
148 Next += '.';
149 Next += llvm::utostr(GCCVersion[1]);
150 Next += '.';
151 Next += llvm::utostr(GCCVersion[2]);
152
153 // Use that if it exists, otherwise hope the user isn't linking.
154 //
155 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000156 Tmp = "/usr/lib/gcc/" + Next;
157 if (llvm::sys::Path(Tmp).exists())
Daniel Dunbar74782b02009-10-20 19:25:43 +0000158 ToolChainDir = Next;
159 }
160
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000161 std::string Path;
162 if (getArchName() == "x86_64") {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000163 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000164 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000165 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000166 Path += "/x86_64";
167 getFilePaths().push_back(Path);
168
169 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000170 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000171 Path += "/x86_64";
172 getFilePaths().push_back(Path);
173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Daniel Dunbaree788e72009-12-21 18:54:17 +0000175 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000176 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000177 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000178 getFilePaths().push_back(Path);
179
180 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000181 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000182 getFilePaths().push_back(Path);
183
Daniel Dunbaree788e72009-12-21 18:54:17 +0000184 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000185 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000186 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000187 getProgramPaths().push_back(Path);
188
189 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000190 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000191 getProgramPaths().push_back(Path);
192
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000193 getProgramPaths().push_back(getDriver().getInstalledDir());
194 if (getDriver().getInstalledDir() != getDriver().Dir)
195 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000196}
197
Daniel Dunbarf3955282009-09-04 18:34:51 +0000198Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000199 // Free tool implementations.
200 for (llvm::DenseMap<unsigned, Tool*>::iterator
201 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
202 delete it->second;
203}
204
Daniel Dunbarf3955282009-09-04 18:34:51 +0000205Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000206 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000207 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000208 Key = Action::AnalyzeJobClass;
209 else
210 Key = JA.getKind();
211
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000212 // FIXME: This doesn't belong here, but ideally we will support static soon
213 // anyway.
214 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
215 C.getArgs().hasArg(options::OPT_static) ||
216 C.getArgs().hasArg(options::OPT_fapple_kext));
217 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
218 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
219 options::OPT_no_integrated_as,
220 IsIADefault);
221
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000222 Tool *&T = Tools[Key];
223 if (!T) {
224 switch (Key) {
225 case Action::InputClass:
226 case Action::BindArchClass:
227 assert(0 && "Invalid tool kind.");
228 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000229 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000230 case Action::AnalyzeJobClass:
231 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000232 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000233 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000234 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000235 case Action::AssembleJobClass: {
236 if (UseIntegratedAs)
237 T = new tools::ClangAs(*this);
238 else
239 T = new tools::darwin::Assemble(*this);
240 break;
241 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000242 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000243 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000244 case Action::LipoJobClass:
245 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000246 case Action::DsymutilJobClass:
247 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000248 }
249 }
250
251 return *T;
252}
253
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000254void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
255 ArgStringList &CmdArgs) const {
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000256 std::string Tmp;
257
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000258 // FIXME: Derive these correctly.
259 if (getArchName() == "x86_64") {
260 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
261 "/x86_64"));
262 // Intentionally duplicated for (temporary) gcc bug compatibility.
263 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
264 "/x86_64"));
265 }
Daniel Dunbar8bc9c552010-04-10 01:24:22 +0000266
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000267 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000268
269 Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir;
270 if (llvm::sys::Path(Tmp).exists())
271 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
272 Tmp = getDriver().Dir + "/../lib/gcc";
273 if (llvm::sys::Path(Tmp).exists())
274 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000275 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
276 // Intentionally duplicated for (temporary) gcc bug compatibility.
277 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000278 Tmp = getDriver().Dir + "/../lib/" + ToolChainDir;
279 if (llvm::sys::Path(Tmp).exists())
280 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
281 Tmp = getDriver().Dir + "/../lib";
282 if (llvm::sys::Path(Tmp).exists())
283 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000284 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
285 "/../../../" + ToolChainDir));
286 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
287 "/../../.."));
288}
289
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000290void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
291 ArgStringList &CmdArgs) const {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000292 // Note that this routine is only used for targetting OS X.
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000293
294 // Derived from libgcc and lib specs but refactored.
295 if (Args.hasArg(options::OPT_static)) {
296 CmdArgs.push_back("-lgcc_static");
297 } else {
298 if (Args.hasArg(options::OPT_static_libgcc)) {
299 CmdArgs.push_back("-lgcc_eh");
300 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
301 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000302 if (isTargetIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000303 CmdArgs.push_back("-lgcc_s.1");
304 } else {
305 CmdArgs.push_back("-lgcc_s.10.5");
306 }
307 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
Daniel Dunbar8a0d94d2010-01-10 00:46:10 +0000308 Args.hasFlag(options::OPT_fexceptions,
309 options::OPT_fno_exceptions) ||
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000310 Args.hasArg(options::OPT_fgnu_runtime)) {
311 // FIXME: This is probably broken on 10.3?
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000312 if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000313 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000314 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000315 CmdArgs.push_back("-lgcc_s.10.5");
316 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000317 if (isMacosxVersionLT(10, 3, 9))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000318 ; // Do nothing.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000319 else if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000320 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000321 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000322 CmdArgs.push_back("-lgcc_s.10.5");
323 }
324
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000325 if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000326 CmdArgs.push_back("-lgcc");
327 CmdArgs.push_back("-lSystem");
328 } else {
329 CmdArgs.push_back("-lSystem");
330 CmdArgs.push_back("-lgcc");
331 }
332 }
333}
334
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000335DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
336 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000337{
338 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000339 getProgramPaths().push_back(getDriver().getInstalledDir());
340 if (getDriver().getInstalledDir() != getDriver().Dir)
341 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000342}
343
344void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
345 ArgStringList &CmdArgs) const {
346 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000347
348 // Unfortunately, we still might depend on a few of the libraries that are
349 // only available in the gcc library directory (in particular
350 // libstdc++.dylib). For now, hardcode the path to the known install location.
351 llvm::sys::Path P(getDriver().Dir);
352 P.eraseComponent(); // .../usr/bin -> ../usr
353 P.appendComponent("lib");
354 P.appendComponent("gcc");
355 switch (getTriple().getArch()) {
356 default:
357 assert(0 && "Invalid Darwin arch!");
358 case llvm::Triple::x86:
359 case llvm::Triple::x86_64:
360 P.appendComponent("i686-apple-darwin10");
361 break;
362 case llvm::Triple::arm:
363 case llvm::Triple::thumb:
364 P.appendComponent("arm-apple-darwin10");
365 break;
366 case llvm::Triple::ppc:
367 case llvm::Triple::ppc64:
368 P.appendComponent("powerpc-apple-darwin10");
369 break;
370 }
371 P.appendComponent("4.2.1");
372 if (P.exists())
373 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000374}
375
376void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
377 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000378 // Darwin doesn't support real static executables, don't link any runtime
379 // libraries with -static.
380 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000381 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000382
383 // Reject -static-libgcc for now, we can deal with this when and if someone
384 // cares. This is useful in situations where someone wants to statically link
385 // something like libstdc++, and needs its runtime support routines.
386 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000387 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000388 << A->getAsString(Args);
389 return;
390 }
391
Daniel Dunbareec99102010-01-22 03:38:14 +0000392 // Otherwise link libSystem, then the dynamic runtime library, and finally any
393 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000394 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000395
396 // Select the dynamic runtime library and the target specific static library.
397 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000398 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000399 CmdArgs.push_back("-lgcc_s.1");
400
401 // We may need some static functions for armv6/thumb which are required to
402 // be in the same linkage unit as their caller.
403 if (getDarwinArchName(Args) == "armv6")
404 DarwinStaticLib = "libclang_rt.armv6.a";
405 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000406 // The dynamic runtime library was merged with libSystem for 10.6 and
407 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000408 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000409 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000410 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000411 CmdArgs.push_back("-lgcc_s.10.5");
412
413 // For OS X, we only need a static runtime library when targetting 10.4, to
414 // provide versions of the static functions which were omitted from
415 // 10.4.dylib.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000416 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000417 DarwinStaticLib = "libclang_rt.10.4.a";
418 }
419
420 /// Add the target specific static library, if needed.
421 if (DarwinStaticLib) {
422 llvm::sys::Path P(getDriver().ResourceDir);
423 P.appendComponent("lib");
424 P.appendComponent("darwin");
425 P.appendComponent(DarwinStaticLib);
426
427 // For now, allow missing resource libraries to support developers who may
428 // not have compiler-rt checked out or integrated into their build.
429 if (!P.exists())
430 getDriver().Diag(clang::diag::warn_drv_missing_resource_library)
431 << P.str();
432 else
433 CmdArgs.push_back(Args.MakeArgString(P.str()));
434 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000435}
436
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000437void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000438 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000439
Daniel Dunbar26031372010-01-27 00:56:25 +0000440 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
441 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000442 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000443 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000444 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000445 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000446 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000447 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000448 // If neither OS X nor iPhoneOS targets were specified, check for
449 // environment defines.
450 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
451 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000452
Daniel Dunbar816bc312010-01-26 01:45:19 +0000453 // Ignore empty strings.
454 if (OSXTarget && OSXTarget[0] == '\0')
455 OSXTarget = 0;
456 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
457 iPhoneOSTarget = 0;
458
Daniel Dunbar39053672010-02-02 17:31:12 +0000459 // Diagnose conflicting deployment targets, and choose default platform
460 // based on the tool chain.
461 //
462 // FIXME: Don't hardcode default here.
463 if (OSXTarget && iPhoneOSTarget) {
464 // FIXME: We should see if we can get away with warning or erroring on
465 // this. Perhaps put under -pedantic?
466 if (getTriple().getArch() == llvm::Triple::arm ||
467 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000468 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000469 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000470 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000471 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000472
Daniel Dunbar39053672010-02-02 17:31:12 +0000473 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000474 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000475 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
476 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000477 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000478 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000479 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
480 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000481 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000482 // Otherwise, assume we are targeting OS X.
483 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000484 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
485 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000486 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Daniel Dunbar26031372010-01-27 00:56:25 +0000489 // Set the tool chain target information.
490 unsigned Major, Minor, Micro;
491 bool HadExtra;
492 if (OSXVersion) {
493 assert(!iPhoneVersion && "Unknown target platform!");
494 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
495 Micro, HadExtra) || HadExtra ||
496 Major != 10 || Minor >= 10 || Micro >= 10)
497 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
498 << OSXVersion->getAsString(Args);
499 } else {
500 assert(iPhoneVersion && "Unknown target platform!");
501 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
502 Micro, HadExtra) || HadExtra ||
503 Major >= 10 || Minor >= 100 || Micro >= 100)
504 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
505 << iPhoneVersion->getAsString(Args);
506 }
507 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000508}
509
510DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
511 const char *BoundArch) const {
512 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
513 const OptTable &Opts = getDriver().getOpts();
514
515 // FIXME: We really want to get out of the tool chain level argument
516 // translation business, as it makes the driver functionality much
517 // more opaque. For now, we follow gcc closely solely for the
518 // purpose of easily achieving feature parity & testability. Once we
519 // have something that works, we should reevaluate each translation
520 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000521
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000522 for (ArgList::const_iterator it = Args.begin(),
523 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000524 Arg *A = *it;
525
526 if (A->getOption().matches(options::OPT_Xarch__)) {
527 // FIXME: Canonicalize name.
528 if (getArchName() != A->getValue(Args, 0))
529 continue;
530
Daniel Dunbar0e100312010-06-14 21:23:08 +0000531 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
532 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000533 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000535 // If the argument parsing failed or more than one argument was
536 // consumed, the -Xarch_ argument's parameter tried to consume
537 // extra arguments. Emit an error and ignore.
538 //
539 // We also want to disallow any options which would alter the
540 // driver behavior; that isn't going to work in our model. We
541 // use isDriverOption() as an approximation, although things
542 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000543 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000544 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000545 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000546 << A->getAsString(Args);
547 continue;
548 }
549
Daniel Dunbar478edc22009-03-29 22:29:05 +0000550 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000551 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000552
553 DAL->AddSynthesizedArg(A);
Mike Stump1eb44332009-09-09 15:08:12 +0000554 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000555
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000556 // Sob. These is strictly gcc compatible for the time being. Apple
557 // gcc translates options twice, which means that self-expanding
558 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000559 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000560 default:
561 DAL->append(A);
562 break;
563
564 case options::OPT_mkernel:
565 case options::OPT_fapple_kext:
566 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000567 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
568 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000569 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000571 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000572 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
573 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000574 break;
575
576 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000577 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
578 DAL->AddFlagArg(A,
579 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000580 break;
581
582 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000583 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
584 DAL->AddFlagArg(A,
585 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000586 break;
587
588 case options::OPT_fterminated_vtables:
589 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000590 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
591 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000592 break;
593
594 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000595 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000596 break;
597
598 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000599 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000600 break;
601
602 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000603 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000604 break;
605
606 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000607 DAL->AddFlagArg(A,
608 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000609 break;
610
611 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000612 DAL->AddFlagArg(A,
613 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000614 break;
615
616 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000617 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000618 break;
619
620 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000621 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000622 break;
623 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000624 }
625
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000626 if (getTriple().getArch() == llvm::Triple::x86 ||
627 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000628 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000629 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000630
631 // Add the arch options based on the particular spelling of -arch, to match
632 // how the driver driver works.
633 if (BoundArch) {
634 llvm::StringRef Name = BoundArch;
635 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
636 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
637
638 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
639 // which defines the list of which architectures we accept.
640 if (Name == "ppc")
641 ;
642 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000643 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000644 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000645 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000646 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000647 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000648 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000649 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000650 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000651 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000652 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000653 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000654 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000655 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000656 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000657 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000658
659 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000660 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000661
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000662 else if (Name == "i386")
663 ;
664 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000665 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000666 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000667 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000668 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000669 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000670 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000671 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000672 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000673 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000674 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000675 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000676 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000677 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000678
679 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000680 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000681
682 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000683 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000684 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000685 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000686 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000687 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000688 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000689 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000690 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000691 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000692 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000693 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000694
695 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000696 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000697 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000698
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000699 // Add an explicit version min argument for the deployment target. We do this
700 // after argument translation because -Xarch_ arguments may add a version min
701 // argument.
702 AddDeploymentTarget(*DAL);
703
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000704 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000705}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000706
Daniel Dunbarf3955282009-09-04 18:34:51 +0000707bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000708 // FIXME: Gross; we should probably have some separate target
709 // definition, possibly even reusing the one in clang.
710 return getArchName() == "x86_64";
711}
712
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000713bool Darwin::UseDwarfDebugFlags() const {
714 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
715 return S[0] != '\0';
716 return false;
717}
718
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000719bool Darwin::UseSjLjExceptions() const {
720 // Darwin uses SjLj exceptions on ARM.
721 return (getTriple().getArch() == llvm::Triple::arm ||
722 getTriple().getArch() == llvm::Triple::thumb);
723}
724
Daniel Dunbarf3955282009-09-04 18:34:51 +0000725const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000726 return "pic";
727}
728
Daniel Dunbarf3955282009-09-04 18:34:51 +0000729const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000730 if (getArchName() == "x86_64")
731 return "pic";
732 return 0;
733}
734
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000735bool Darwin::SupportsObjCGC() const {
736 // Garbage collection is supported everywhere except on iPhone OS.
737 return !isTargetIPhoneOS();
738}
739
Daniel Dunbar39176082009-03-20 00:20:03 +0000740/// Generic_GCC - A tool chain using the 'gcc' command to perform
741/// all subcommands; this relies on gcc translating the majority of
742/// command line options.
743
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000744Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000745 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000746 getProgramPaths().push_back(getDriver().getInstalledDir());
747 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
748 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000749}
750
Daniel Dunbar39176082009-03-20 00:20:03 +0000751Generic_GCC::~Generic_GCC() {
752 // Free tool implementations.
753 for (llvm::DenseMap<unsigned, Tool*>::iterator
754 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
755 delete it->second;
756}
757
Mike Stump1eb44332009-09-09 15:08:12 +0000758Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000759 const JobAction &JA) const {
760 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000761 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000762 Key = Action::AnalyzeJobClass;
763 else
764 Key = JA.getKind();
765
766 Tool *&T = Tools[Key];
767 if (!T) {
768 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000769 case Action::InputClass:
770 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000771 assert(0 && "Invalid tool kind.");
772 case Action::PreprocessJobClass:
773 T = new tools::gcc::Preprocess(*this); break;
774 case Action::PrecompileJobClass:
775 T = new tools::gcc::Precompile(*this); break;
776 case Action::AnalyzeJobClass:
777 T = new tools::Clang(*this); break;
778 case Action::CompileJobClass:
779 T = new tools::gcc::Compile(*this); break;
780 case Action::AssembleJobClass:
781 T = new tools::gcc::Assemble(*this); break;
782 case Action::LinkJobClass:
783 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000785 // This is a bit ungeneric, but the only platform using a driver
786 // driver is Darwin.
787 case Action::LipoJobClass:
788 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000789 case Action::DsymutilJobClass:
790 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000791 }
792 }
793
794 return *T;
795}
796
Daniel Dunbar39176082009-03-20 00:20:03 +0000797bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000798 // FIXME: Gross; we should probably have some separate target
799 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000800 return getArchName() == "x86_64";
801}
802
803const char *Generic_GCC::GetDefaultRelocationModel() const {
804 return "static";
805}
806
807const char *Generic_GCC::GetForcedPicModel() const {
808 return 0;
809}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000810
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000811/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
812/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
813/// Currently does not support anything else but compilation.
814
815TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
816 : ToolChain(Host, Triple) {
817 // Path mangling to find libexec
818 std::string Path(getDriver().Dir);
819
820 Path += "/../libexec";
821 getProgramPaths().push_back(Path);
822}
823
824TCEToolChain::~TCEToolChain() {
825 for (llvm::DenseMap<unsigned, Tool*>::iterator
826 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
827 delete it->second;
828}
829
830bool TCEToolChain::IsMathErrnoDefault() const {
831 return true;
832}
833
834bool TCEToolChain::IsUnwindTablesDefault() const {
835 return false;
836}
837
838const char *TCEToolChain::GetDefaultRelocationModel() const {
839 return "static";
840}
841
842const char *TCEToolChain::GetForcedPicModel() const {
843 return 0;
844}
845
846Tool &TCEToolChain::SelectTool(const Compilation &C,
847 const JobAction &JA) const {
848 Action::ActionClass Key;
849 Key = Action::AnalyzeJobClass;
850
851 Tool *&T = Tools[Key];
852 if (!T) {
853 switch (Key) {
854 case Action::PreprocessJobClass:
855 T = new tools::gcc::Preprocess(*this); break;
856 case Action::AnalyzeJobClass:
857 T = new tools::Clang(*this); break;
858 default:
859 assert(false && "Unsupported action for TCE target.");
860 }
861 }
862 return *T;
863}
864
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000865/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
866
867OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
868 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000869 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000870 getFilePaths().push_back("/usr/lib");
871}
872
873Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
874 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000875 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000876 Key = Action::AnalyzeJobClass;
877 else
878 Key = JA.getKind();
879
880 Tool *&T = Tools[Key];
881 if (!T) {
882 switch (Key) {
883 case Action::AssembleJobClass:
884 T = new tools::openbsd::Assemble(*this); break;
885 case Action::LinkJobClass:
886 T = new tools::openbsd::Link(*this); break;
887 default:
888 T = &Generic_GCC::SelectTool(C, JA);
889 }
890 }
891
892 return *T;
893}
894
Daniel Dunbar75358d22009-03-30 21:06:03 +0000895/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
896
Daniel Dunbar214afe92010-08-02 05:43:59 +0000897FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000898 : Generic_GCC(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +0000899
900 // Determine if we are compiling 32-bit code on an x86_64 platform.
901 bool Lib32 = false;
902 if (Triple.getArch() == llvm::Triple::x86 &&
903 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
904 llvm::Triple::x86_64)
905 Lib32 = true;
906
Daniel Dunbar7fb2c252010-06-15 15:03:31 +0000907 getProgramPaths().push_back(getDriver().Dir + "/../libexec");
908 getProgramPaths().push_back("/usr/libexec");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000909 if (Lib32) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000910 getFilePaths().push_back(getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000911 getFilePaths().push_back("/usr/lib32");
912 } else {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000913 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000914 getFilePaths().push_back("/usr/lib");
915 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000916}
917
918Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
919 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000920 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000921 Key = Action::AnalyzeJobClass;
922 else
923 Key = JA.getKind();
924
925 Tool *&T = Tools[Key];
926 if (!T) {
927 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000928 case Action::AssembleJobClass:
929 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000930 case Action::LinkJobClass:
931 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000932 default:
933 T = &Generic_GCC::SelectTool(C, JA);
934 }
935 }
936
937 return *T;
938}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000939
Chris Lattner38e317d2010-07-07 16:01:42 +0000940/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
941
942Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
943 : Generic_GCC(Host, Triple) {
944 getFilePaths().push_back(getDriver().Dir + "/../lib");
945 getFilePaths().push_back("/usr/lib");
946 getFilePaths().push_back("/usr/gnu/lib");
947 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
948}
949
950Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
951 Action::ActionClass Key;
952 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
953 Key = Action::AnalyzeJobClass;
954 else
955 Key = JA.getKind();
956
957 Tool *&T = Tools[Key];
958 if (!T) {
959 switch (Key) {
960 case Action::AssembleJobClass:
961 T = new tools::minix::Assemble(*this); break;
962 case Action::LinkJobClass:
963 T = new tools::minix::Link(*this); break;
964 default:
965 T = &Generic_GCC::SelectTool(C, JA);
966 }
967 }
968
969 return *T;
970}
971
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000972/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
973
974AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
975 : Generic_GCC(Host, Triple) {
976
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000977 getProgramPaths().push_back(getDriver().getInstalledDir());
978 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
979 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000980
Daniel Dunbaree788e72009-12-21 18:54:17 +0000981 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000982 getFilePaths().push_back("/usr/lib");
983 getFilePaths().push_back("/usr/sfw/lib");
984 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000985 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000986
987}
988
989Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
990 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000991 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000992 Key = Action::AnalyzeJobClass;
993 else
994 Key = JA.getKind();
995
996 Tool *&T = Tools[Key];
997 if (!T) {
998 switch (Key) {
999 case Action::AssembleJobClass:
1000 T = new tools::auroraux::Assemble(*this); break;
1001 case Action::LinkJobClass:
1002 T = new tools::auroraux::Link(*this); break;
1003 default:
1004 T = &Generic_GCC::SelectTool(C, JA);
1005 }
1006 }
1007
1008 return *T;
1009}
1010
1011
Eli Friedman6b3454a2009-05-26 07:52:18 +00001012/// Linux toolchain (very bare-bones at the moment).
1013
1014Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
1015 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001016 getFilePaths().push_back(getDriver().Dir + "/../lib/clang/1.0/");
Eli Friedman6b3454a2009-05-26 07:52:18 +00001017 getFilePaths().push_back("/lib/");
1018 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +00001019
1020 // Depending on the Linux distribution, any combination of lib{,32,64} is
1021 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
1022 // openSUSE uses lib and lib64 for the same purpose.
1023 getFilePaths().push_back("/lib32/");
1024 getFilePaths().push_back("/usr/lib32/");
1025 getFilePaths().push_back("/lib64/");
1026 getFilePaths().push_back("/usr/lib64/");
1027
Eli Friedman6b3454a2009-05-26 07:52:18 +00001028 // FIXME: Figure out some way to get gcc's libdir
1029 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
1030 // crtbegin.o/crtend.o/etc., and want static versions of various
1031 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
1032 // get away with using shared versions in /usr/lib, though.
1033 // We could fall back to the approach we used for includes (a massive
1034 // list), but that's messy at best.
1035}
1036
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001037Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA) const {
1038 Action::ActionClass Key;
1039 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1040 Key = Action::AnalyzeJobClass;
1041 else
1042 Key = JA.getKind();
1043
1044 Tool *&T = Tools[Key];
1045 if (!T) {
1046 switch (Key) {
1047 case Action::AssembleJobClass:
1048 T = new tools::linuxtools::Assemble(*this); break;
1049 default:
1050 T = &Generic_GCC::SelectTool(C, JA);
1051 }
1052 }
1053
1054 return *T;
1055}
1056
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001057/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1058
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001059DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
1060 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001061
1062 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001063 getProgramPaths().push_back(getDriver().getInstalledDir());
1064 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1065 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001066
Daniel Dunbaree788e72009-12-21 18:54:17 +00001067 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001068 getFilePaths().push_back("/usr/lib");
1069 getFilePaths().push_back("/usr/lib/gcc41");
1070}
1071
1072Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
1073 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001074 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001075 Key = Action::AnalyzeJobClass;
1076 else
1077 Key = JA.getKind();
1078
1079 Tool *&T = Tools[Key];
1080 if (!T) {
1081 switch (Key) {
1082 case Action::AssembleJobClass:
1083 T = new tools::dragonfly::Assemble(*this); break;
1084 case Action::LinkJobClass:
1085 T = new tools::dragonfly::Link(*this); break;
1086 default:
1087 T = &Generic_GCC::SelectTool(C, JA);
1088 }
1089 }
1090
1091 return *T;
1092}