blob: 5be810da4f296fe3636b2d34a8a3317785904f31 [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
Daniel Dunbar00577ad2010-08-23 22:35:37 +000022#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000023#include "llvm/ADT/StringExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000024#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000026#include "llvm/System/Path.h"
27
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000028#include <cstdlib> // ::getenv
29
Daniel Dunbar39176082009-03-20 00:20:03 +000030using namespace clang::driver;
31using namespace clang::driver::toolchains;
32
Daniel Dunbarf3955282009-09-04 18:34:51 +000033/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000034
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000035Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcc8e1892010-01-27 00:56:44 +000036 : ToolChain(Host, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000037{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000038 // Compute the initial Darwin version based on the host.
39 bool HadExtra;
40 std::string OSName = Triple.getOSName();
41 if (!Driver::GetReleaseVersion(&OSName[6],
42 DarwinVersion[0], DarwinVersion[1],
43 DarwinVersion[2], HadExtra))
44 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
45
Daniel Dunbar02633b52009-03-26 16:23:12 +000046 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000047 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
48 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000049}
50
Daniel Dunbar41800112010-08-02 05:43:56 +000051types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
52 types::ID Ty = types::lookupTypeForExtension(Ext);
53
54 // Darwin always preprocesses assembly files (unless -x is used explicitly).
55 if (Ty == types::TY_PP_Asm)
56 return types::TY_Asm;
57
58 return Ty;
59}
60
Daniel Dunbareeff4062010-01-22 02:04:58 +000061// FIXME: Can we tablegen this?
62static const char *GetArmArchForMArch(llvm::StringRef Value) {
63 if (Value == "armv6k")
64 return "armv6";
65
66 if (Value == "armv5tej")
67 return "armv5";
68
69 if (Value == "xscale")
70 return "xscale";
71
72 if (Value == "armv4t")
73 return "armv4t";
74
75 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
76 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
77 Value == "armv7m")
78 return "armv7";
79
80 return 0;
81}
82
83// FIXME: Can we tablegen this?
84static const char *GetArmArchForMCpu(llvm::StringRef Value) {
85 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
86 Value == "arm946e-s" || Value == "arm966e-s" ||
87 Value == "arm968e-s" || Value == "arm10e" ||
88 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
89 Value == "arm1026ej-s")
90 return "armv5";
91
92 if (Value == "xscale")
93 return "xscale";
94
95 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
96 Value == "arm1176jz-s" || Value == "arm1176jzf-s")
97 return "armv6";
98
99 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
100 return "armv7";
101
102 return 0;
103}
104
105llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
106 switch (getTriple().getArch()) {
107 default:
108 return getArchName();
109
110 case llvm::Triple::arm: {
111 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
112 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
113 return Arch;
114
115 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
116 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
117 return Arch;
118
119 return "arm";
120 }
121 }
122}
123
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000124DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple)
125 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000126{
Daniel Dunbarc2bda622010-08-02 05:44:01 +0000127 // We can only work with 4.2.1 currently.
128 GCCVersion[0] = 4;
129 GCCVersion[1] = 2;
130 GCCVersion[2] = 1;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000131
132 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000133 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +0000134 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000135 ToolChainDir += "/";
136 ToolChainDir += llvm::utostr(GCCVersion[0]);
137 ToolChainDir += '.';
138 ToolChainDir += llvm::utostr(GCCVersion[1]);
139 ToolChainDir += '.';
140 ToolChainDir += llvm::utostr(GCCVersion[2]);
141
Daniel Dunbar74782b02009-10-20 19:25:43 +0000142 // Try the next major version if that tool chain dir is invalid.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000143 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
144 if (!llvm::sys::Path(Tmp).exists()) {
Daniel Dunbar74782b02009-10-20 19:25:43 +0000145 std::string Next = "i686-apple-darwin";
146 Next += llvm::utostr(DarwinVersion[0] + 1);
147 Next += "/";
148 Next += llvm::utostr(GCCVersion[0]);
149 Next += '.';
150 Next += llvm::utostr(GCCVersion[1]);
151 Next += '.';
152 Next += llvm::utostr(GCCVersion[2]);
153
154 // Use that if it exists, otherwise hope the user isn't linking.
155 //
156 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000157 Tmp = "/usr/lib/gcc/" + Next;
158 if (llvm::sys::Path(Tmp).exists())
Daniel Dunbar74782b02009-10-20 19:25:43 +0000159 ToolChainDir = Next;
160 }
161
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000162 std::string Path;
163 if (getArchName() == "x86_64") {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000164 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000165 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000166 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000167 Path += "/x86_64";
168 getFilePaths().push_back(Path);
169
170 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000171 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000172 Path += "/x86_64";
173 getFilePaths().push_back(Path);
174 }
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Daniel Dunbaree788e72009-12-21 18:54:17 +0000176 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000177 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000178 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000179 getFilePaths().push_back(Path);
180
181 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000182 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000183 getFilePaths().push_back(Path);
184
Daniel Dunbaree788e72009-12-21 18:54:17 +0000185 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000186 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000187 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000188 getProgramPaths().push_back(Path);
189
190 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000191 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000192 getProgramPaths().push_back(Path);
193
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000194 getProgramPaths().push_back(getDriver().getInstalledDir());
195 if (getDriver().getInstalledDir() != getDriver().Dir)
196 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000197}
198
Daniel Dunbarf3955282009-09-04 18:34:51 +0000199Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000200 // Free tool implementations.
201 for (llvm::DenseMap<unsigned, Tool*>::iterator
202 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
203 delete it->second;
204}
205
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000206std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
207 llvm::Triple Triple(ComputeLLVMTriple(Args));
208
209 // If the target isn't initialized (e.g., an unknown Darwin platform, return
210 // the default triple).
211 if (!isTargetInitialized())
212 return Triple.getTriple();
213
214 unsigned Version[3];
215 getTargetVersion(Version);
216
217 // Mangle the target version into the OS triple component. For historical
218 // reasons that make little sense, the version passed here is the "darwin"
219 // version, which drops the 10 and offsets by 4. See inverse code when
220 // setting the OS version preprocessor define.
221 if (!isTargetIPhoneOS()) {
222 Version[0] = Version[1] + 4;
223 Version[1] = Version[2];
224 Version[2] = 0;
225 } else {
226 // Use the environment to communicate that we are targetting iPhoneOS.
227 Triple.setEnvironmentName("iphoneos");
228 }
229
230 llvm::SmallString<16> Str;
231 llvm::raw_svector_ostream(Str) << "darwin" << Version[0]
232 << "." << Version[1] << "." << Version[2];
233 Triple.setOSName(Str.str());
234
235 return Triple.getTriple();
236}
237
Daniel Dunbarf3955282009-09-04 18:34:51 +0000238Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000239 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000240 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000241 Key = Action::AnalyzeJobClass;
242 else
243 Key = JA.getKind();
244
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000245 // FIXME: This doesn't belong here, but ideally we will support static soon
246 // anyway.
247 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
248 C.getArgs().hasArg(options::OPT_static) ||
249 C.getArgs().hasArg(options::OPT_fapple_kext));
250 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
251 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
252 options::OPT_no_integrated_as,
253 IsIADefault);
254
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000255 Tool *&T = Tools[Key];
256 if (!T) {
257 switch (Key) {
258 case Action::InputClass:
259 case Action::BindArchClass:
260 assert(0 && "Invalid tool kind.");
261 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000262 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000263 case Action::AnalyzeJobClass:
264 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000265 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000266 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000267 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000268 case Action::AssembleJobClass: {
269 if (UseIntegratedAs)
270 T = new tools::ClangAs(*this);
271 else
272 T = new tools::darwin::Assemble(*this);
273 break;
274 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000275 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000276 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000277 case Action::LipoJobClass:
278 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000279 case Action::DsymutilJobClass:
280 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000281 }
282 }
283
284 return *T;
285}
286
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000287void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
288 ArgStringList &CmdArgs) const {
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000289 std::string Tmp;
290
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000291 // FIXME: Derive these correctly.
292 if (getArchName() == "x86_64") {
293 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
294 "/x86_64"));
295 // Intentionally duplicated for (temporary) gcc bug compatibility.
296 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
297 "/x86_64"));
298 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000299
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000300 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000301
302 Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir;
303 if (llvm::sys::Path(Tmp).exists())
304 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
305 Tmp = getDriver().Dir + "/../lib/gcc";
306 if (llvm::sys::Path(Tmp).exists())
307 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000308 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
309 // Intentionally duplicated for (temporary) gcc bug compatibility.
310 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000311 Tmp = getDriver().Dir + "/../lib/" + ToolChainDir;
312 if (llvm::sys::Path(Tmp).exists())
313 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
314 Tmp = getDriver().Dir + "/../lib";
315 if (llvm::sys::Path(Tmp).exists())
316 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000317 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
318 "/../../../" + ToolChainDir));
319 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
320 "/../../.."));
321}
322
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000323void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
324 ArgStringList &CmdArgs) const {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000325 // Note that this routine is only used for targetting OS X.
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000326
327 // Derived from libgcc and lib specs but refactored.
328 if (Args.hasArg(options::OPT_static)) {
329 CmdArgs.push_back("-lgcc_static");
330 } else {
331 if (Args.hasArg(options::OPT_static_libgcc)) {
332 CmdArgs.push_back("-lgcc_eh");
333 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
334 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000335 if (isTargetIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000336 CmdArgs.push_back("-lgcc_s.1");
337 } else {
338 CmdArgs.push_back("-lgcc_s.10.5");
339 }
340 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
Daniel Dunbar8a0d94d2010-01-10 00:46:10 +0000341 Args.hasFlag(options::OPT_fexceptions,
342 options::OPT_fno_exceptions) ||
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000343 Args.hasArg(options::OPT_fgnu_runtime)) {
344 // FIXME: This is probably broken on 10.3?
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000345 if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000346 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000347 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000348 CmdArgs.push_back("-lgcc_s.10.5");
349 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000350 if (isMacosxVersionLT(10, 3, 9))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000351 ; // Do nothing.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000352 else if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000353 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000354 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000355 CmdArgs.push_back("-lgcc_s.10.5");
356 }
357
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000358 if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000359 CmdArgs.push_back("-lgcc");
360 CmdArgs.push_back("-lSystem");
361 } else {
362 CmdArgs.push_back("-lSystem");
363 CmdArgs.push_back("-lgcc");
364 }
365 }
366}
367
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000368DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
369 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000370{
371 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000372 getProgramPaths().push_back(getDriver().getInstalledDir());
373 if (getDriver().getInstalledDir() != getDriver().Dir)
374 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000375}
376
377void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
378 ArgStringList &CmdArgs) const {
379 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000380
381 // Unfortunately, we still might depend on a few of the libraries that are
382 // only available in the gcc library directory (in particular
383 // libstdc++.dylib). For now, hardcode the path to the known install location.
384 llvm::sys::Path P(getDriver().Dir);
385 P.eraseComponent(); // .../usr/bin -> ../usr
386 P.appendComponent("lib");
387 P.appendComponent("gcc");
388 switch (getTriple().getArch()) {
389 default:
390 assert(0 && "Invalid Darwin arch!");
391 case llvm::Triple::x86:
392 case llvm::Triple::x86_64:
393 P.appendComponent("i686-apple-darwin10");
394 break;
395 case llvm::Triple::arm:
396 case llvm::Triple::thumb:
397 P.appendComponent("arm-apple-darwin10");
398 break;
399 case llvm::Triple::ppc:
400 case llvm::Triple::ppc64:
401 P.appendComponent("powerpc-apple-darwin10");
402 break;
403 }
404 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000405
406 // Determine the arch specific GCC subdirectory.
407 const char *ArchSpecificDir = 0;
408 switch (getTriple().getArch()) {
409 default:
410 break;
411 case llvm::Triple::arm:
412 case llvm::Triple::thumb:
413 // FIXME: Get the right subdirectory for ARM.
414 break;
415 case llvm::Triple::ppc64:
416 ArchSpecificDir = "ppc64";
417 break;
418 case llvm::Triple::x86_64:
419 ArchSpecificDir = "x86_64";
420 break;
421 }
422
423 if (ArchSpecificDir) {
424 P.appendComponent(ArchSpecificDir);
425 llvm::errs() << P.str() << "\n";
426 if (P.exists())
427 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
428 P.eraseComponent();
429 }
430
Daniel Dunbar424b6612010-06-30 23:56:13 +0000431 if (P.exists())
432 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000433}
434
435void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
436 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000437 // Darwin doesn't support real static executables, don't link any runtime
438 // libraries with -static.
439 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000440 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000441
442 // Reject -static-libgcc for now, we can deal with this when and if someone
443 // cares. This is useful in situations where someone wants to statically link
444 // something like libstdc++, and needs its runtime support routines.
445 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000446 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000447 << A->getAsString(Args);
448 return;
449 }
450
Daniel Dunbareec99102010-01-22 03:38:14 +0000451 // Otherwise link libSystem, then the dynamic runtime library, and finally any
452 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000453 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000454
455 // Select the dynamic runtime library and the target specific static library.
456 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000457 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000458 CmdArgs.push_back("-lgcc_s.1");
459
460 // We may need some static functions for armv6/thumb which are required to
461 // be in the same linkage unit as their caller.
462 if (getDarwinArchName(Args) == "armv6")
463 DarwinStaticLib = "libclang_rt.armv6.a";
464 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000465 // The dynamic runtime library was merged with libSystem for 10.6 and
466 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000467 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000468 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000469 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000470 CmdArgs.push_back("-lgcc_s.10.5");
471
472 // For OS X, we only need a static runtime library when targetting 10.4, to
473 // provide versions of the static functions which were omitted from
474 // 10.4.dylib.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000475 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000476 DarwinStaticLib = "libclang_rt.10.4.a";
477 }
478
479 /// Add the target specific static library, if needed.
480 if (DarwinStaticLib) {
481 llvm::sys::Path P(getDriver().ResourceDir);
482 P.appendComponent("lib");
483 P.appendComponent("darwin");
484 P.appendComponent(DarwinStaticLib);
485
486 // For now, allow missing resource libraries to support developers who may
487 // not have compiler-rt checked out or integrated into their build.
488 if (!P.exists())
489 getDriver().Diag(clang::diag::warn_drv_missing_resource_library)
490 << P.str();
491 else
492 CmdArgs.push_back(Args.MakeArgString(P.str()));
493 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000494}
495
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000496void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000497 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000498
Daniel Dunbar26031372010-01-27 00:56:25 +0000499 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
500 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000501 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000502 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000503 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000504 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000505 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000506 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000507 // If neither OS X nor iPhoneOS targets were specified, check for
508 // environment defines.
509 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
510 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000511
Daniel Dunbar816bc312010-01-26 01:45:19 +0000512 // Ignore empty strings.
513 if (OSXTarget && OSXTarget[0] == '\0')
514 OSXTarget = 0;
515 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
516 iPhoneOSTarget = 0;
517
Daniel Dunbar39053672010-02-02 17:31:12 +0000518 // Diagnose conflicting deployment targets, and choose default platform
519 // based on the tool chain.
520 //
521 // FIXME: Don't hardcode default here.
522 if (OSXTarget && iPhoneOSTarget) {
523 // FIXME: We should see if we can get away with warning or erroring on
524 // this. Perhaps put under -pedantic?
525 if (getTriple().getArch() == llvm::Triple::arm ||
526 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000527 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000528 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000529 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000530 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000531
Daniel Dunbar39053672010-02-02 17:31:12 +0000532 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000533 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000534 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
535 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000536 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000537 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000538 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
539 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000540 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000541 // Otherwise, assume we are targeting OS X.
542 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000543 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
544 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000545 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Daniel Dunbar26031372010-01-27 00:56:25 +0000548 // Set the tool chain target information.
549 unsigned Major, Minor, Micro;
550 bool HadExtra;
551 if (OSXVersion) {
552 assert(!iPhoneVersion && "Unknown target platform!");
553 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
554 Micro, HadExtra) || HadExtra ||
555 Major != 10 || Minor >= 10 || Micro >= 10)
556 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
557 << OSXVersion->getAsString(Args);
558 } else {
559 assert(iPhoneVersion && "Unknown target platform!");
560 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
561 Micro, HadExtra) || HadExtra ||
562 Major >= 10 || Minor >= 100 || Micro >= 100)
563 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
564 << iPhoneVersion->getAsString(Args);
565 }
566 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000567}
568
569DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
570 const char *BoundArch) const {
571 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
572 const OptTable &Opts = getDriver().getOpts();
573
574 // FIXME: We really want to get out of the tool chain level argument
575 // translation business, as it makes the driver functionality much
576 // more opaque. For now, we follow gcc closely solely for the
577 // purpose of easily achieving feature parity & testability. Once we
578 // have something that works, we should reevaluate each translation
579 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000580
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000581 for (ArgList::const_iterator it = Args.begin(),
582 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000583 Arg *A = *it;
584
585 if (A->getOption().matches(options::OPT_Xarch__)) {
586 // FIXME: Canonicalize name.
587 if (getArchName() != A->getValue(Args, 0))
588 continue;
589
Daniel Dunbar0e100312010-06-14 21:23:08 +0000590 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
591 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000592 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000594 // If the argument parsing failed or more than one argument was
595 // consumed, the -Xarch_ argument's parameter tried to consume
596 // extra arguments. Emit an error and ignore.
597 //
598 // We also want to disallow any options which would alter the
599 // driver behavior; that isn't going to work in our model. We
600 // use isDriverOption() as an approximation, although things
601 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000602 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000603 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000604 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000605 << A->getAsString(Args);
606 continue;
607 }
608
Daniel Dunbar478edc22009-03-29 22:29:05 +0000609 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000610 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000611
612 DAL->AddSynthesizedArg(A);
Mike Stump1eb44332009-09-09 15:08:12 +0000613 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000614
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000615 // Sob. These is strictly gcc compatible for the time being. Apple
616 // gcc translates options twice, which means that self-expanding
617 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000618 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000619 default:
620 DAL->append(A);
621 break;
622
623 case options::OPT_mkernel:
624 case options::OPT_fapple_kext:
625 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000626 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
627 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000628 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000630 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000631 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
632 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000633 break;
634
635 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000636 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
637 DAL->AddFlagArg(A,
638 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000639 break;
640
641 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000642 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
643 DAL->AddFlagArg(A,
644 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000645 break;
646
647 case options::OPT_fterminated_vtables:
648 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000649 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
650 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000651 break;
652
653 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000654 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000655 break;
656
657 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000658 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000659 break;
660
661 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000662 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000663 break;
664
665 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000666 DAL->AddFlagArg(A,
667 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000668 break;
669
670 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000671 DAL->AddFlagArg(A,
672 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000673 break;
674
675 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000676 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000677 break;
678
679 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000680 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000681 break;
682 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000683 }
684
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000685 if (getTriple().getArch() == llvm::Triple::x86 ||
686 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000687 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000688 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000689
690 // Add the arch options based on the particular spelling of -arch, to match
691 // how the driver driver works.
692 if (BoundArch) {
693 llvm::StringRef Name = BoundArch;
694 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
695 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
696
697 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
698 // which defines the list of which architectures we accept.
699 if (Name == "ppc")
700 ;
701 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000702 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000703 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000704 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000705 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000706 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000707 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000708 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000709 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000710 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000711 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000712 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000713 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000714 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000715 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000716 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000717
718 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000719 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000720
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000721 else if (Name == "i386")
722 ;
723 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000724 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000725 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000726 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000727 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000728 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000729 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000730 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000731 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000732 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000733 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000734 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000735 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000736 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000737
738 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000739 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000740
741 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000742 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000743 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000744 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000745 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000746 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000747 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000748 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000749 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000750 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000751 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000752 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000753
754 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000755 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000756 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000757
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000758 // Add an explicit version min argument for the deployment target. We do this
759 // after argument translation because -Xarch_ arguments may add a version min
760 // argument.
761 AddDeploymentTarget(*DAL);
762
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000763 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000764}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000765
Daniel Dunbarf3955282009-09-04 18:34:51 +0000766bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000767 // FIXME: Gross; we should probably have some separate target
768 // definition, possibly even reusing the one in clang.
769 return getArchName() == "x86_64";
770}
771
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000772bool Darwin::UseDwarfDebugFlags() const {
773 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
774 return S[0] != '\0';
775 return false;
776}
777
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000778bool Darwin::UseSjLjExceptions() const {
779 // Darwin uses SjLj exceptions on ARM.
780 return (getTriple().getArch() == llvm::Triple::arm ||
781 getTriple().getArch() == llvm::Triple::thumb);
782}
783
Daniel Dunbarf3955282009-09-04 18:34:51 +0000784const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000785 return "pic";
786}
787
Daniel Dunbarf3955282009-09-04 18:34:51 +0000788const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000789 if (getArchName() == "x86_64")
790 return "pic";
791 return 0;
792}
793
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000794bool Darwin::SupportsObjCGC() const {
795 // Garbage collection is supported everywhere except on iPhone OS.
796 return !isTargetIPhoneOS();
797}
798
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000799std::string
800Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
801 return ComputeLLVMTriple(Args);
802}
803
Daniel Dunbar39176082009-03-20 00:20:03 +0000804/// Generic_GCC - A tool chain using the 'gcc' command to perform
805/// all subcommands; this relies on gcc translating the majority of
806/// command line options.
807
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000808Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000809 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000810 getProgramPaths().push_back(getDriver().getInstalledDir());
811 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
812 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000813}
814
Daniel Dunbar39176082009-03-20 00:20:03 +0000815Generic_GCC::~Generic_GCC() {
816 // Free tool implementations.
817 for (llvm::DenseMap<unsigned, Tool*>::iterator
818 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
819 delete it->second;
820}
821
Mike Stump1eb44332009-09-09 15:08:12 +0000822Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000823 const JobAction &JA) const {
824 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000825 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000826 Key = Action::AnalyzeJobClass;
827 else
828 Key = JA.getKind();
829
830 Tool *&T = Tools[Key];
831 if (!T) {
832 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000833 case Action::InputClass:
834 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000835 assert(0 && "Invalid tool kind.");
836 case Action::PreprocessJobClass:
837 T = new tools::gcc::Preprocess(*this); break;
838 case Action::PrecompileJobClass:
839 T = new tools::gcc::Precompile(*this); break;
840 case Action::AnalyzeJobClass:
841 T = new tools::Clang(*this); break;
842 case Action::CompileJobClass:
843 T = new tools::gcc::Compile(*this); break;
844 case Action::AssembleJobClass:
845 T = new tools::gcc::Assemble(*this); break;
846 case Action::LinkJobClass:
847 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000849 // This is a bit ungeneric, but the only platform using a driver
850 // driver is Darwin.
851 case Action::LipoJobClass:
852 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000853 case Action::DsymutilJobClass:
854 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000855 }
856 }
857
858 return *T;
859}
860
Daniel Dunbar39176082009-03-20 00:20:03 +0000861bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000862 // FIXME: Gross; we should probably have some separate target
863 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000864 return getArchName() == "x86_64";
865}
866
867const char *Generic_GCC::GetDefaultRelocationModel() const {
868 return "static";
869}
870
871const char *Generic_GCC::GetForcedPicModel() const {
872 return 0;
873}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000874
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000875/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
876/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
877/// Currently does not support anything else but compilation.
878
879TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
880 : ToolChain(Host, Triple) {
881 // Path mangling to find libexec
882 std::string Path(getDriver().Dir);
883
884 Path += "/../libexec";
885 getProgramPaths().push_back(Path);
886}
887
888TCEToolChain::~TCEToolChain() {
889 for (llvm::DenseMap<unsigned, Tool*>::iterator
890 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
891 delete it->second;
892}
893
894bool TCEToolChain::IsMathErrnoDefault() const {
895 return true;
896}
897
898bool TCEToolChain::IsUnwindTablesDefault() const {
899 return false;
900}
901
902const char *TCEToolChain::GetDefaultRelocationModel() const {
903 return "static";
904}
905
906const char *TCEToolChain::GetForcedPicModel() const {
907 return 0;
908}
909
910Tool &TCEToolChain::SelectTool(const Compilation &C,
911 const JobAction &JA) const {
912 Action::ActionClass Key;
913 Key = Action::AnalyzeJobClass;
914
915 Tool *&T = Tools[Key];
916 if (!T) {
917 switch (Key) {
918 case Action::PreprocessJobClass:
919 T = new tools::gcc::Preprocess(*this); break;
920 case Action::AnalyzeJobClass:
921 T = new tools::Clang(*this); break;
922 default:
923 assert(false && "Unsupported action for TCE target.");
924 }
925 }
926 return *T;
927}
928
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000929/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
930
931OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
932 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000933 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000934 getFilePaths().push_back("/usr/lib");
935}
936
937Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
938 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000939 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000940 Key = Action::AnalyzeJobClass;
941 else
942 Key = JA.getKind();
943
944 Tool *&T = Tools[Key];
945 if (!T) {
946 switch (Key) {
947 case Action::AssembleJobClass:
948 T = new tools::openbsd::Assemble(*this); break;
949 case Action::LinkJobClass:
950 T = new tools::openbsd::Link(*this); break;
951 default:
952 T = &Generic_GCC::SelectTool(C, JA);
953 }
954 }
955
956 return *T;
957}
958
Daniel Dunbar75358d22009-03-30 21:06:03 +0000959/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
960
Daniel Dunbar214afe92010-08-02 05:43:59 +0000961FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000962 : Generic_GCC(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +0000963
964 // Determine if we are compiling 32-bit code on an x86_64 platform.
965 bool Lib32 = false;
966 if (Triple.getArch() == llvm::Triple::x86 &&
967 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
968 llvm::Triple::x86_64)
969 Lib32 = true;
970
Daniel Dunbar7fb2c252010-06-15 15:03:31 +0000971 getProgramPaths().push_back(getDriver().Dir + "/../libexec");
972 getProgramPaths().push_back("/usr/libexec");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000973 if (Lib32) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000974 getFilePaths().push_back(getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000975 getFilePaths().push_back("/usr/lib32");
976 } else {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000977 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000978 getFilePaths().push_back("/usr/lib");
979 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000980}
981
982Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
983 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000984 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000985 Key = Action::AnalyzeJobClass;
986 else
987 Key = JA.getKind();
988
989 Tool *&T = Tools[Key];
990 if (!T) {
991 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000992 case Action::AssembleJobClass:
993 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000994 case Action::LinkJobClass:
995 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000996 default:
997 T = &Generic_GCC::SelectTool(C, JA);
998 }
999 }
1000
1001 return *T;
1002}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001003
Chris Lattner38e317d2010-07-07 16:01:42 +00001004/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1005
1006Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1007 : Generic_GCC(Host, Triple) {
1008 getFilePaths().push_back(getDriver().Dir + "/../lib");
1009 getFilePaths().push_back("/usr/lib");
1010 getFilePaths().push_back("/usr/gnu/lib");
1011 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1012}
1013
1014Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
1015 Action::ActionClass Key;
1016 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1017 Key = Action::AnalyzeJobClass;
1018 else
1019 Key = JA.getKind();
1020
1021 Tool *&T = Tools[Key];
1022 if (!T) {
1023 switch (Key) {
1024 case Action::AssembleJobClass:
1025 T = new tools::minix::Assemble(*this); break;
1026 case Action::LinkJobClass:
1027 T = new tools::minix::Link(*this); break;
1028 default:
1029 T = &Generic_GCC::SelectTool(C, JA);
1030 }
1031 }
1032
1033 return *T;
1034}
1035
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001036/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1037
1038AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1039 : Generic_GCC(Host, Triple) {
1040
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001041 getProgramPaths().push_back(getDriver().getInstalledDir());
1042 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1043 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001044
Daniel Dunbaree788e72009-12-21 18:54:17 +00001045 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001046 getFilePaths().push_back("/usr/lib");
1047 getFilePaths().push_back("/usr/sfw/lib");
1048 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001049 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001050
1051}
1052
1053Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
1054 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001055 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001056 Key = Action::AnalyzeJobClass;
1057 else
1058 Key = JA.getKind();
1059
1060 Tool *&T = Tools[Key];
1061 if (!T) {
1062 switch (Key) {
1063 case Action::AssembleJobClass:
1064 T = new tools::auroraux::Assemble(*this); break;
1065 case Action::LinkJobClass:
1066 T = new tools::auroraux::Link(*this); break;
1067 default:
1068 T = &Generic_GCC::SelectTool(C, JA);
1069 }
1070 }
1071
1072 return *T;
1073}
1074
1075
Eli Friedman6b3454a2009-05-26 07:52:18 +00001076/// Linux toolchain (very bare-bones at the moment).
1077
1078Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
1079 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001080 getFilePaths().push_back(getDriver().Dir + "/../lib/clang/1.0/");
Eli Friedman6b3454a2009-05-26 07:52:18 +00001081 getFilePaths().push_back("/lib/");
1082 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +00001083
1084 // Depending on the Linux distribution, any combination of lib{,32,64} is
1085 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
1086 // openSUSE uses lib and lib64 for the same purpose.
1087 getFilePaths().push_back("/lib32/");
1088 getFilePaths().push_back("/usr/lib32/");
1089 getFilePaths().push_back("/lib64/");
1090 getFilePaths().push_back("/usr/lib64/");
1091
Eli Friedman6b3454a2009-05-26 07:52:18 +00001092 // FIXME: Figure out some way to get gcc's libdir
1093 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
1094 // crtbegin.o/crtend.o/etc., and want static versions of various
1095 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
1096 // get away with using shared versions in /usr/lib, though.
1097 // We could fall back to the approach we used for includes (a massive
1098 // list), but that's messy at best.
1099}
1100
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001101Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA) const {
1102 Action::ActionClass Key;
1103 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1104 Key = Action::AnalyzeJobClass;
1105 else
1106 Key = JA.getKind();
1107
1108 Tool *&T = Tools[Key];
1109 if (!T) {
1110 switch (Key) {
1111 case Action::AssembleJobClass:
1112 T = new tools::linuxtools::Assemble(*this); break;
1113 default:
1114 T = &Generic_GCC::SelectTool(C, JA);
1115 }
1116 }
1117
1118 return *T;
1119}
1120
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001121/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1122
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001123DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
1124 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001125
1126 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001127 getProgramPaths().push_back(getDriver().getInstalledDir());
1128 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1129 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001130
Daniel Dunbaree788e72009-12-21 18:54:17 +00001131 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001132 getFilePaths().push_back("/usr/lib");
1133 getFilePaths().push_back("/usr/lib/gcc41");
1134}
1135
1136Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
1137 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001138 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001139 Key = Action::AnalyzeJobClass;
1140 else
1141 Key = JA.getKind();
1142
1143 Tool *&T = Tools[Key];
1144 if (!T) {
1145 switch (Key) {
1146 case Action::AssembleJobClass:
1147 T = new tools::dragonfly::Assemble(*this); break;
1148 case Action::LinkJobClass:
1149 T = new tools::dragonfly::Link(*this); break;
1150 default:
1151 T = &Generic_GCC::SelectTool(C, JA);
1152 }
1153 }
1154
1155 return *T;
1156}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001157
1158Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1159 : ToolChain(Host, Triple) {
1160}
1161
1162Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA) const {
1163 Action::ActionClass Key;
1164 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1165 Key = Action::AnalyzeJobClass;
1166 else
1167 Key = JA.getKind();
1168
1169 Tool *&T = Tools[Key];
1170 if (!T) {
1171 switch (Key) {
1172 case Action::InputClass:
1173 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001174 case Action::LipoJobClass:
1175 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001176 assert(0 && "Invalid tool kind.");
1177 case Action::PreprocessJobClass:
1178 case Action::PrecompileJobClass:
1179 case Action::AnalyzeJobClass:
1180 case Action::CompileJobClass:
1181 T = new tools::Clang(*this); break;
1182 case Action::AssembleJobClass:
1183 T = new tools::ClangAs(*this); break;
1184 case Action::LinkJobClass:
1185 T = new tools::visualstudio::Link(*this); break;
1186 }
1187 }
1188
1189 return *T;
1190}
1191
1192bool Windows::IsIntegratedAssemblerDefault() const {
1193 return true;
1194}
1195
1196bool Windows::IsUnwindTablesDefault() const {
1197 // FIXME: Gross; we should probably have some separate target
1198 // definition, possibly even reusing the one in clang.
1199 return getArchName() == "x86_64";
1200}
1201
1202const char *Windows::GetDefaultRelocationModel() const {
1203 return "static";
1204}
1205
1206const char *Windows::GetForcedPicModel() const {
1207 if (getArchName() == "x86_64")
1208 return "pic";
1209 return 0;
1210}