blob: 471c47dd2b50a84e9f0c971e47a5b00248262b40 [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"
Douglas Gregor34916db2010-09-03 17:16:03 +000021#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000022
Daniel Dunbar00577ad2010-08-23 22:35:37 +000023#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000024#include "llvm/ADT/StringExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000025#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000027#include "llvm/System/Path.h"
28
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000029#include <cstdlib> // ::getenv
30
Daniel Dunbar39176082009-03-20 00:20:03 +000031using namespace clang::driver;
32using namespace clang::driver::toolchains;
33
Daniel Dunbarf3955282009-09-04 18:34:51 +000034/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000035
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000036Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcc8e1892010-01-27 00:56:44 +000037 : ToolChain(Host, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000038{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000039 // Compute the initial Darwin version based on the host.
40 bool HadExtra;
41 std::string OSName = Triple.getOSName();
42 if (!Driver::GetReleaseVersion(&OSName[6],
43 DarwinVersion[0], DarwinVersion[1],
44 DarwinVersion[2], HadExtra))
45 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
46
Daniel Dunbar02633b52009-03-26 16:23:12 +000047 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000048 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
49 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000050}
51
Daniel Dunbar41800112010-08-02 05:43:56 +000052types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
53 types::ID Ty = types::lookupTypeForExtension(Ext);
54
55 // Darwin always preprocesses assembly files (unless -x is used explicitly).
56 if (Ty == types::TY_PP_Asm)
57 return types::TY_Asm;
58
59 return Ty;
60}
61
Daniel Dunbareeff4062010-01-22 02:04:58 +000062// FIXME: Can we tablegen this?
63static const char *GetArmArchForMArch(llvm::StringRef Value) {
64 if (Value == "armv6k")
65 return "armv6";
66
67 if (Value == "armv5tej")
68 return "armv5";
69
70 if (Value == "xscale")
71 return "xscale";
72
73 if (Value == "armv4t")
74 return "armv4t";
75
76 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
77 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
78 Value == "armv7m")
79 return "armv7";
80
81 return 0;
82}
83
84// FIXME: Can we tablegen this?
85static const char *GetArmArchForMCpu(llvm::StringRef Value) {
86 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
87 Value == "arm946e-s" || Value == "arm966e-s" ||
88 Value == "arm968e-s" || Value == "arm10e" ||
89 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
90 Value == "arm1026ej-s")
91 return "armv5";
92
93 if (Value == "xscale")
94 return "xscale";
95
96 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
97 Value == "arm1176jz-s" || Value == "arm1176jzf-s")
98 return "armv6";
99
100 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
101 return "armv7";
102
103 return 0;
104}
105
106llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
107 switch (getTriple().getArch()) {
108 default:
109 return getArchName();
110
111 case llvm::Triple::arm: {
112 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
113 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
114 return Arch;
115
116 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
117 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
118 return Arch;
119
120 return "arm";
121 }
122 }
123}
124
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000125DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple)
126 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000127{
Daniel Dunbarc2bda622010-08-02 05:44:01 +0000128 // We can only work with 4.2.1 currently.
129 GCCVersion[0] = 4;
130 GCCVersion[1] = 2;
131 GCCVersion[2] = 1;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000132
133 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000134 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +0000135 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000136 ToolChainDir += "/";
137 ToolChainDir += llvm::utostr(GCCVersion[0]);
138 ToolChainDir += '.';
139 ToolChainDir += llvm::utostr(GCCVersion[1]);
140 ToolChainDir += '.';
141 ToolChainDir += llvm::utostr(GCCVersion[2]);
142
Daniel Dunbar74782b02009-10-20 19:25:43 +0000143 // Try the next major version if that tool chain dir is invalid.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000144 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
145 if (!llvm::sys::Path(Tmp).exists()) {
Daniel Dunbar74782b02009-10-20 19:25:43 +0000146 std::string Next = "i686-apple-darwin";
147 Next += llvm::utostr(DarwinVersion[0] + 1);
148 Next += "/";
149 Next += llvm::utostr(GCCVersion[0]);
150 Next += '.';
151 Next += llvm::utostr(GCCVersion[1]);
152 Next += '.';
153 Next += llvm::utostr(GCCVersion[2]);
154
155 // Use that if it exists, otherwise hope the user isn't linking.
156 //
157 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000158 Tmp = "/usr/lib/gcc/" + Next;
159 if (llvm::sys::Path(Tmp).exists())
Daniel Dunbar74782b02009-10-20 19:25:43 +0000160 ToolChainDir = Next;
161 }
162
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000163 std::string Path;
164 if (getArchName() == "x86_64") {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000165 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000166 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000167 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000168 Path += "/x86_64";
169 getFilePaths().push_back(Path);
170
171 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000172 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000173 Path += "/x86_64";
174 getFilePaths().push_back(Path);
175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Daniel Dunbaree788e72009-12-21 18:54:17 +0000177 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000178 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000179 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000180 getFilePaths().push_back(Path);
181
182 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000183 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000184 getFilePaths().push_back(Path);
185
Daniel Dunbaree788e72009-12-21 18:54:17 +0000186 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000187 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000188 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000189 getProgramPaths().push_back(Path);
190
191 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000192 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000193 getProgramPaths().push_back(Path);
194
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000195 getProgramPaths().push_back(getDriver().getInstalledDir());
196 if (getDriver().getInstalledDir() != getDriver().Dir)
197 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000198}
199
Daniel Dunbarf3955282009-09-04 18:34:51 +0000200Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000201 // Free tool implementations.
202 for (llvm::DenseMap<unsigned, Tool*>::iterator
203 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
204 delete it->second;
205}
206
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000207std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
208 llvm::Triple Triple(ComputeLLVMTriple(Args));
209
210 // If the target isn't initialized (e.g., an unknown Darwin platform, return
211 // the default triple).
212 if (!isTargetInitialized())
213 return Triple.getTriple();
214
215 unsigned Version[3];
216 getTargetVersion(Version);
217
218 // Mangle the target version into the OS triple component. For historical
219 // reasons that make little sense, the version passed here is the "darwin"
220 // version, which drops the 10 and offsets by 4. See inverse code when
221 // setting the OS version preprocessor define.
222 if (!isTargetIPhoneOS()) {
223 Version[0] = Version[1] + 4;
224 Version[1] = Version[2];
225 Version[2] = 0;
226 } else {
227 // Use the environment to communicate that we are targetting iPhoneOS.
228 Triple.setEnvironmentName("iphoneos");
229 }
230
231 llvm::SmallString<16> Str;
232 llvm::raw_svector_ostream(Str) << "darwin" << Version[0]
233 << "." << Version[1] << "." << Version[2];
234 Triple.setOSName(Str.str());
235
236 return Triple.getTriple();
237}
238
Daniel Dunbarf3955282009-09-04 18:34:51 +0000239Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000240 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000241 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000242 Key = Action::AnalyzeJobClass;
243 else
244 Key = JA.getKind();
245
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000246 // FIXME: This doesn't belong here, but ideally we will support static soon
247 // anyway.
248 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
249 C.getArgs().hasArg(options::OPT_static) ||
250 C.getArgs().hasArg(options::OPT_fapple_kext));
251 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
252 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
253 options::OPT_no_integrated_as,
254 IsIADefault);
255
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000256 Tool *&T = Tools[Key];
257 if (!T) {
258 switch (Key) {
259 case Action::InputClass:
260 case Action::BindArchClass:
261 assert(0 && "Invalid tool kind.");
262 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000263 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000264 case Action::AnalyzeJobClass:
265 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000266 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000267 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000268 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000269 case Action::AssembleJobClass: {
270 if (UseIntegratedAs)
271 T = new tools::ClangAs(*this);
272 else
273 T = new tools::darwin::Assemble(*this);
274 break;
275 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000276 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000277 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000278 case Action::LipoJobClass:
279 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000280 case Action::DsymutilJobClass:
281 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000282 }
283 }
284
285 return *T;
286}
287
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000288void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
289 ArgStringList &CmdArgs) const {
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000290 std::string Tmp;
291
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000292 // FIXME: Derive these correctly.
293 if (getArchName() == "x86_64") {
294 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
295 "/x86_64"));
296 // Intentionally duplicated for (temporary) gcc bug compatibility.
297 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
298 "/x86_64"));
299 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000300
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000301 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000302
303 Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir;
304 if (llvm::sys::Path(Tmp).exists())
305 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
306 Tmp = getDriver().Dir + "/../lib/gcc";
307 if (llvm::sys::Path(Tmp).exists())
308 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000309 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
310 // Intentionally duplicated for (temporary) gcc bug compatibility.
311 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000312 Tmp = getDriver().Dir + "/../lib/" + ToolChainDir;
313 if (llvm::sys::Path(Tmp).exists())
314 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
315 Tmp = getDriver().Dir + "/../lib";
316 if (llvm::sys::Path(Tmp).exists())
317 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000318 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
319 "/../../../" + ToolChainDir));
320 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
321 "/../../.."));
322}
323
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000324void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
325 ArgStringList &CmdArgs) const {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000326 // Note that this routine is only used for targetting OS X.
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000327
328 // Derived from libgcc and lib specs but refactored.
329 if (Args.hasArg(options::OPT_static)) {
330 CmdArgs.push_back("-lgcc_static");
331 } else {
332 if (Args.hasArg(options::OPT_static_libgcc)) {
333 CmdArgs.push_back("-lgcc_eh");
334 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
335 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000336 if (isTargetIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000337 CmdArgs.push_back("-lgcc_s.1");
338 } else {
339 CmdArgs.push_back("-lgcc_s.10.5");
340 }
341 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
Daniel Dunbar8a0d94d2010-01-10 00:46:10 +0000342 Args.hasFlag(options::OPT_fexceptions,
343 options::OPT_fno_exceptions) ||
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000344 Args.hasArg(options::OPT_fgnu_runtime)) {
345 // FIXME: This is probably broken on 10.3?
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000346 if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000347 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000348 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000349 CmdArgs.push_back("-lgcc_s.10.5");
350 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000351 if (isMacosxVersionLT(10, 3, 9))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000352 ; // Do nothing.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000353 else if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000354 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000355 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000356 CmdArgs.push_back("-lgcc_s.10.5");
357 }
358
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000359 if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000360 CmdArgs.push_back("-lgcc");
361 CmdArgs.push_back("-lSystem");
362 } else {
363 CmdArgs.push_back("-lSystem");
364 CmdArgs.push_back("-lgcc");
365 }
366 }
367}
368
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000369DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
370 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000371{
372 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000373 getProgramPaths().push_back(getDriver().getInstalledDir());
374 if (getDriver().getInstalledDir() != getDriver().Dir)
375 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000376}
377
378void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
379 ArgStringList &CmdArgs) const {
380 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000381
382 // Unfortunately, we still might depend on a few of the libraries that are
383 // only available in the gcc library directory (in particular
384 // libstdc++.dylib). For now, hardcode the path to the known install location.
385 llvm::sys::Path P(getDriver().Dir);
386 P.eraseComponent(); // .../usr/bin -> ../usr
387 P.appendComponent("lib");
388 P.appendComponent("gcc");
389 switch (getTriple().getArch()) {
390 default:
391 assert(0 && "Invalid Darwin arch!");
392 case llvm::Triple::x86:
393 case llvm::Triple::x86_64:
394 P.appendComponent("i686-apple-darwin10");
395 break;
396 case llvm::Triple::arm:
397 case llvm::Triple::thumb:
398 P.appendComponent("arm-apple-darwin10");
399 break;
400 case llvm::Triple::ppc:
401 case llvm::Triple::ppc64:
402 P.appendComponent("powerpc-apple-darwin10");
403 break;
404 }
405 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000406
407 // Determine the arch specific GCC subdirectory.
408 const char *ArchSpecificDir = 0;
409 switch (getTriple().getArch()) {
410 default:
411 break;
412 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000413 case llvm::Triple::thumb: {
414 std::string Triple = ComputeLLVMTriple(Args);
415 llvm::StringRef TripleStr = Triple;
416 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
417 ArchSpecificDir = "v5";
418 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
419 ArchSpecificDir = "v6";
420 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
421 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000422 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000423 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000424 case llvm::Triple::ppc64:
425 ArchSpecificDir = "ppc64";
426 break;
427 case llvm::Triple::x86_64:
428 ArchSpecificDir = "x86_64";
429 break;
430 }
431
432 if (ArchSpecificDir) {
433 P.appendComponent(ArchSpecificDir);
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000434 if (P.exists())
435 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
436 P.eraseComponent();
437 }
438
Daniel Dunbar424b6612010-06-30 23:56:13 +0000439 if (P.exists())
440 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000441}
442
443void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
444 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000445 // Darwin doesn't support real static executables, don't link any runtime
446 // libraries with -static.
447 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000448 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000449
450 // Reject -static-libgcc for now, we can deal with this when and if someone
451 // cares. This is useful in situations where someone wants to statically link
452 // something like libstdc++, and needs its runtime support routines.
453 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000454 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000455 << A->getAsString(Args);
456 return;
457 }
458
Daniel Dunbareec99102010-01-22 03:38:14 +0000459 // Otherwise link libSystem, then the dynamic runtime library, and finally any
460 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000461 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000462
463 // Select the dynamic runtime library and the target specific static library.
464 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000465 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000466 CmdArgs.push_back("-lgcc_s.1");
467
468 // We may need some static functions for armv6/thumb which are required to
469 // be in the same linkage unit as their caller.
470 if (getDarwinArchName(Args) == "armv6")
471 DarwinStaticLib = "libclang_rt.armv6.a";
472 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000473 // The dynamic runtime library was merged with libSystem for 10.6 and
474 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000475 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000476 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000477 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000478 CmdArgs.push_back("-lgcc_s.10.5");
479
480 // For OS X, we only need a static runtime library when targetting 10.4, to
481 // provide versions of the static functions which were omitted from
482 // 10.4.dylib.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000483 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000484 DarwinStaticLib = "libclang_rt.10.4.a";
485 }
486
487 /// Add the target specific static library, if needed.
488 if (DarwinStaticLib) {
489 llvm::sys::Path P(getDriver().ResourceDir);
490 P.appendComponent("lib");
491 P.appendComponent("darwin");
492 P.appendComponent(DarwinStaticLib);
493
494 // For now, allow missing resource libraries to support developers who may
495 // not have compiler-rt checked out or integrated into their build.
496 if (!P.exists())
497 getDriver().Diag(clang::diag::warn_drv_missing_resource_library)
498 << P.str();
499 else
500 CmdArgs.push_back(Args.MakeArgString(P.str()));
501 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000502}
503
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000504void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000505 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000506
Daniel Dunbar26031372010-01-27 00:56:25 +0000507 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
508 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000509 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000510 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000511 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000512 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000513 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000514 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000515 // If neither OS X nor iPhoneOS targets were specified, check for
516 // environment defines.
517 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
518 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000519
Daniel Dunbar816bc312010-01-26 01:45:19 +0000520 // Ignore empty strings.
521 if (OSXTarget && OSXTarget[0] == '\0')
522 OSXTarget = 0;
523 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
524 iPhoneOSTarget = 0;
525
Daniel Dunbar39053672010-02-02 17:31:12 +0000526 // Diagnose conflicting deployment targets, and choose default platform
527 // based on the tool chain.
528 //
529 // FIXME: Don't hardcode default here.
530 if (OSXTarget && iPhoneOSTarget) {
531 // FIXME: We should see if we can get away with warning or erroring on
532 // this. Perhaps put under -pedantic?
533 if (getTriple().getArch() == llvm::Triple::arm ||
534 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000535 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000536 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000537 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000538 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000539
Daniel Dunbar39053672010-02-02 17:31:12 +0000540 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000541 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000542 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
543 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000544 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000545 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000546 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
547 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000548 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000549 // Otherwise, assume we are targeting OS X.
550 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000551 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
552 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000553 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Daniel Dunbar26031372010-01-27 00:56:25 +0000556 // Set the tool chain target information.
557 unsigned Major, Minor, Micro;
558 bool HadExtra;
559 if (OSXVersion) {
560 assert(!iPhoneVersion && "Unknown target platform!");
561 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
562 Micro, HadExtra) || HadExtra ||
563 Major != 10 || Minor >= 10 || Micro >= 10)
564 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
565 << OSXVersion->getAsString(Args);
566 } else {
567 assert(iPhoneVersion && "Unknown target platform!");
568 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
569 Micro, HadExtra) || HadExtra ||
570 Major >= 10 || Minor >= 100 || Micro >= 100)
571 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
572 << iPhoneVersion->getAsString(Args);
573 }
574 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000575}
576
577DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
578 const char *BoundArch) const {
579 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
580 const OptTable &Opts = getDriver().getOpts();
581
582 // FIXME: We really want to get out of the tool chain level argument
583 // translation business, as it makes the driver functionality much
584 // more opaque. For now, we follow gcc closely solely for the
585 // purpose of easily achieving feature parity & testability. Once we
586 // have something that works, we should reevaluate each translation
587 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000588
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000589 for (ArgList::const_iterator it = Args.begin(),
590 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000591 Arg *A = *it;
592
593 if (A->getOption().matches(options::OPT_Xarch__)) {
594 // FIXME: Canonicalize name.
595 if (getArchName() != A->getValue(Args, 0))
596 continue;
597
Daniel Dunbar0e100312010-06-14 21:23:08 +0000598 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
599 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000600 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000602 // If the argument parsing failed or more than one argument was
603 // consumed, the -Xarch_ argument's parameter tried to consume
604 // extra arguments. Emit an error and ignore.
605 //
606 // We also want to disallow any options which would alter the
607 // driver behavior; that isn't going to work in our model. We
608 // use isDriverOption() as an approximation, although things
609 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000610 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000611 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000612 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000613 << A->getAsString(Args);
614 continue;
615 }
616
Daniel Dunbar478edc22009-03-29 22:29:05 +0000617 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000618 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000619
620 DAL->AddSynthesizedArg(A);
Mike Stump1eb44332009-09-09 15:08:12 +0000621 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000622
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000623 // Sob. These is strictly gcc compatible for the time being. Apple
624 // gcc translates options twice, which means that self-expanding
625 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000626 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000627 default:
628 DAL->append(A);
629 break;
630
631 case options::OPT_mkernel:
632 case options::OPT_fapple_kext:
633 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000634 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
635 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000636 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000638 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000639 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
640 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000641 break;
642
643 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000644 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
645 DAL->AddFlagArg(A,
646 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000647 break;
648
649 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000650 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
651 DAL->AddFlagArg(A,
652 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000653 break;
654
655 case options::OPT_fterminated_vtables:
656 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000657 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
658 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000659 break;
660
661 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000662 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000663 break;
664
665 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000666 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000667 break;
668
669 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000670 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000671 break;
672
673 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000674 DAL->AddFlagArg(A,
675 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000676 break;
677
678 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000679 DAL->AddFlagArg(A,
680 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000681 break;
682
683 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000684 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000685 break;
686
687 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000688 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000689 break;
690 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000691 }
692
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000693 if (getTriple().getArch() == llvm::Triple::x86 ||
694 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000695 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000696 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000697
698 // Add the arch options based on the particular spelling of -arch, to match
699 // how the driver driver works.
700 if (BoundArch) {
701 llvm::StringRef Name = BoundArch;
702 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
703 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
704
705 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
706 // which defines the list of which architectures we accept.
707 if (Name == "ppc")
708 ;
709 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000710 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000711 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000712 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000713 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000714 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000715 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000716 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000717 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000718 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000719 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000720 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000721 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000722 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000723 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000724 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000725
726 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000727 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000728
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000729 else if (Name == "i386")
730 ;
731 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000732 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000733 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000734 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000735 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000736 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000737 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000738 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000739 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000740 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000741 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000742 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000743 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000744 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000745
746 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000747 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000748
749 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000750 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000751 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000752 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000753 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000754 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000755 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000756 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000757 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000758 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000759 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000760 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000761
762 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000763 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000764 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000765
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000766 // Add an explicit version min argument for the deployment target. We do this
767 // after argument translation because -Xarch_ arguments may add a version min
768 // argument.
769 AddDeploymentTarget(*DAL);
770
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000771 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000772}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000773
Daniel Dunbarf3955282009-09-04 18:34:51 +0000774bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000775 // FIXME: Gross; we should probably have some separate target
776 // definition, possibly even reusing the one in clang.
777 return getArchName() == "x86_64";
778}
779
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000780bool Darwin::UseDwarfDebugFlags() const {
781 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
782 return S[0] != '\0';
783 return false;
784}
785
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000786bool Darwin::UseSjLjExceptions() const {
787 // Darwin uses SjLj exceptions on ARM.
788 return (getTriple().getArch() == llvm::Triple::arm ||
789 getTriple().getArch() == llvm::Triple::thumb);
790}
791
Daniel Dunbarf3955282009-09-04 18:34:51 +0000792const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000793 return "pic";
794}
795
Daniel Dunbarf3955282009-09-04 18:34:51 +0000796const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000797 if (getArchName() == "x86_64")
798 return "pic";
799 return 0;
800}
801
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000802bool Darwin::SupportsObjCGC() const {
803 // Garbage collection is supported everywhere except on iPhone OS.
804 return !isTargetIPhoneOS();
805}
806
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000807std::string
808Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
809 return ComputeLLVMTriple(Args);
810}
811
Daniel Dunbar39176082009-03-20 00:20:03 +0000812/// Generic_GCC - A tool chain using the 'gcc' command to perform
813/// all subcommands; this relies on gcc translating the majority of
814/// command line options.
815
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000816Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000817 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000818 getProgramPaths().push_back(getDriver().getInstalledDir());
819 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
820 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000821}
822
Daniel Dunbar39176082009-03-20 00:20:03 +0000823Generic_GCC::~Generic_GCC() {
824 // Free tool implementations.
825 for (llvm::DenseMap<unsigned, Tool*>::iterator
826 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
827 delete it->second;
828}
829
Mike Stump1eb44332009-09-09 15:08:12 +0000830Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000831 const JobAction &JA) const {
832 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000833 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000834 Key = Action::AnalyzeJobClass;
835 else
836 Key = JA.getKind();
837
838 Tool *&T = Tools[Key];
839 if (!T) {
840 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000841 case Action::InputClass:
842 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000843 assert(0 && "Invalid tool kind.");
844 case Action::PreprocessJobClass:
845 T = new tools::gcc::Preprocess(*this); break;
846 case Action::PrecompileJobClass:
847 T = new tools::gcc::Precompile(*this); break;
848 case Action::AnalyzeJobClass:
849 T = new tools::Clang(*this); break;
850 case Action::CompileJobClass:
851 T = new tools::gcc::Compile(*this); break;
852 case Action::AssembleJobClass:
853 T = new tools::gcc::Assemble(*this); break;
854 case Action::LinkJobClass:
855 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000857 // This is a bit ungeneric, but the only platform using a driver
858 // driver is Darwin.
859 case Action::LipoJobClass:
860 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000861 case Action::DsymutilJobClass:
862 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000863 }
864 }
865
866 return *T;
867}
868
Daniel Dunbar39176082009-03-20 00:20:03 +0000869bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000870 // FIXME: Gross; we should probably have some separate target
871 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000872 return getArchName() == "x86_64";
873}
874
875const char *Generic_GCC::GetDefaultRelocationModel() const {
876 return "static";
877}
878
879const char *Generic_GCC::GetForcedPicModel() const {
880 return 0;
881}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000882
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000883/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
884/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
885/// Currently does not support anything else but compilation.
886
887TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
888 : ToolChain(Host, Triple) {
889 // Path mangling to find libexec
890 std::string Path(getDriver().Dir);
891
892 Path += "/../libexec";
893 getProgramPaths().push_back(Path);
894}
895
896TCEToolChain::~TCEToolChain() {
897 for (llvm::DenseMap<unsigned, Tool*>::iterator
898 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
899 delete it->second;
900}
901
902bool TCEToolChain::IsMathErrnoDefault() const {
903 return true;
904}
905
906bool TCEToolChain::IsUnwindTablesDefault() const {
907 return false;
908}
909
910const char *TCEToolChain::GetDefaultRelocationModel() const {
911 return "static";
912}
913
914const char *TCEToolChain::GetForcedPicModel() const {
915 return 0;
916}
917
918Tool &TCEToolChain::SelectTool(const Compilation &C,
919 const JobAction &JA) const {
920 Action::ActionClass Key;
921 Key = Action::AnalyzeJobClass;
922
923 Tool *&T = Tools[Key];
924 if (!T) {
925 switch (Key) {
926 case Action::PreprocessJobClass:
927 T = new tools::gcc::Preprocess(*this); break;
928 case Action::AnalyzeJobClass:
929 T = new tools::Clang(*this); break;
930 default:
931 assert(false && "Unsupported action for TCE target.");
932 }
933 }
934 return *T;
935}
936
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000937/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
938
939OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
940 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000941 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000942 getFilePaths().push_back("/usr/lib");
943}
944
945Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
946 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000947 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000948 Key = Action::AnalyzeJobClass;
949 else
950 Key = JA.getKind();
951
952 Tool *&T = Tools[Key];
953 if (!T) {
954 switch (Key) {
955 case Action::AssembleJobClass:
956 T = new tools::openbsd::Assemble(*this); break;
957 case Action::LinkJobClass:
958 T = new tools::openbsd::Link(*this); break;
959 default:
960 T = &Generic_GCC::SelectTool(C, JA);
961 }
962 }
963
964 return *T;
965}
966
Daniel Dunbar75358d22009-03-30 21:06:03 +0000967/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
968
Daniel Dunbar214afe92010-08-02 05:43:59 +0000969FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000970 : Generic_GCC(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +0000971
972 // Determine if we are compiling 32-bit code on an x86_64 platform.
973 bool Lib32 = false;
974 if (Triple.getArch() == llvm::Triple::x86 &&
975 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
976 llvm::Triple::x86_64)
977 Lib32 = true;
978
Daniel Dunbar7fb2c252010-06-15 15:03:31 +0000979 getProgramPaths().push_back(getDriver().Dir + "/../libexec");
980 getProgramPaths().push_back("/usr/libexec");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000981 if (Lib32) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000982 getFilePaths().push_back(getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000983 getFilePaths().push_back("/usr/lib32");
984 } else {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000985 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000986 getFilePaths().push_back("/usr/lib");
987 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000988}
989
990Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
991 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000992 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000993 Key = Action::AnalyzeJobClass;
994 else
995 Key = JA.getKind();
996
997 Tool *&T = Tools[Key];
998 if (!T) {
999 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001000 case Action::AssembleJobClass:
1001 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001002 case Action::LinkJobClass:
1003 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001004 default:
1005 T = &Generic_GCC::SelectTool(C, JA);
1006 }
1007 }
1008
1009 return *T;
1010}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001011
Chris Lattner38e317d2010-07-07 16:01:42 +00001012/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1013
1014Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1015 : Generic_GCC(Host, Triple) {
1016 getFilePaths().push_back(getDriver().Dir + "/../lib");
1017 getFilePaths().push_back("/usr/lib");
1018 getFilePaths().push_back("/usr/gnu/lib");
1019 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1020}
1021
1022Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
1023 Action::ActionClass Key;
1024 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1025 Key = Action::AnalyzeJobClass;
1026 else
1027 Key = JA.getKind();
1028
1029 Tool *&T = Tools[Key];
1030 if (!T) {
1031 switch (Key) {
1032 case Action::AssembleJobClass:
1033 T = new tools::minix::Assemble(*this); break;
1034 case Action::LinkJobClass:
1035 T = new tools::minix::Link(*this); break;
1036 default:
1037 T = &Generic_GCC::SelectTool(C, JA);
1038 }
1039 }
1040
1041 return *T;
1042}
1043
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001044/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1045
1046AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1047 : Generic_GCC(Host, Triple) {
1048
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001049 getProgramPaths().push_back(getDriver().getInstalledDir());
1050 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1051 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001052
Daniel Dunbaree788e72009-12-21 18:54:17 +00001053 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001054 getFilePaths().push_back("/usr/lib");
1055 getFilePaths().push_back("/usr/sfw/lib");
1056 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001057 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001058
1059}
1060
1061Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
1062 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001063 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001064 Key = Action::AnalyzeJobClass;
1065 else
1066 Key = JA.getKind();
1067
1068 Tool *&T = Tools[Key];
1069 if (!T) {
1070 switch (Key) {
1071 case Action::AssembleJobClass:
1072 T = new tools::auroraux::Assemble(*this); break;
1073 case Action::LinkJobClass:
1074 T = new tools::auroraux::Link(*this); break;
1075 default:
1076 T = &Generic_GCC::SelectTool(C, JA);
1077 }
1078 }
1079
1080 return *T;
1081}
1082
1083
Eli Friedman6b3454a2009-05-26 07:52:18 +00001084/// Linux toolchain (very bare-bones at the moment).
1085
1086Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
1087 : Generic_GCC(Host, Triple) {
Chris Lattner3f2cc6f2010-09-03 16:47:03 +00001088 getFilePaths().push_back(getDriver().Dir +
1089 "/../lib/clang/" CLANG_VERSION_STRING "/");
Eli Friedman6b3454a2009-05-26 07:52:18 +00001090 getFilePaths().push_back("/lib/");
1091 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +00001092
1093 // Depending on the Linux distribution, any combination of lib{,32,64} is
1094 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
1095 // openSUSE uses lib and lib64 for the same purpose.
1096 getFilePaths().push_back("/lib32/");
1097 getFilePaths().push_back("/usr/lib32/");
1098 getFilePaths().push_back("/lib64/");
1099 getFilePaths().push_back("/usr/lib64/");
1100
Eli Friedman6b3454a2009-05-26 07:52:18 +00001101 // FIXME: Figure out some way to get gcc's libdir
1102 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
1103 // crtbegin.o/crtend.o/etc., and want static versions of various
1104 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
1105 // get away with using shared versions in /usr/lib, though.
1106 // We could fall back to the approach we used for includes (a massive
1107 // list), but that's messy at best.
1108}
1109
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001110Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA) const {
1111 Action::ActionClass Key;
1112 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1113 Key = Action::AnalyzeJobClass;
1114 else
1115 Key = JA.getKind();
1116
1117 Tool *&T = Tools[Key];
1118 if (!T) {
1119 switch (Key) {
1120 case Action::AssembleJobClass:
1121 T = new tools::linuxtools::Assemble(*this); break;
1122 default:
1123 T = &Generic_GCC::SelectTool(C, JA);
1124 }
1125 }
1126
1127 return *T;
1128}
1129
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001130/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1131
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001132DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
1133 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001134
1135 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001136 getProgramPaths().push_back(getDriver().getInstalledDir());
1137 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1138 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001139
Daniel Dunbaree788e72009-12-21 18:54:17 +00001140 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001141 getFilePaths().push_back("/usr/lib");
1142 getFilePaths().push_back("/usr/lib/gcc41");
1143}
1144
1145Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
1146 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001147 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001148 Key = Action::AnalyzeJobClass;
1149 else
1150 Key = JA.getKind();
1151
1152 Tool *&T = Tools[Key];
1153 if (!T) {
1154 switch (Key) {
1155 case Action::AssembleJobClass:
1156 T = new tools::dragonfly::Assemble(*this); break;
1157 case Action::LinkJobClass:
1158 T = new tools::dragonfly::Link(*this); break;
1159 default:
1160 T = &Generic_GCC::SelectTool(C, JA);
1161 }
1162 }
1163
1164 return *T;
1165}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001166
1167Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1168 : ToolChain(Host, Triple) {
1169}
1170
1171Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA) const {
1172 Action::ActionClass Key;
1173 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1174 Key = Action::AnalyzeJobClass;
1175 else
1176 Key = JA.getKind();
1177
1178 Tool *&T = Tools[Key];
1179 if (!T) {
1180 switch (Key) {
1181 case Action::InputClass:
1182 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001183 case Action::LipoJobClass:
1184 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001185 assert(0 && "Invalid tool kind.");
1186 case Action::PreprocessJobClass:
1187 case Action::PrecompileJobClass:
1188 case Action::AnalyzeJobClass:
1189 case Action::CompileJobClass:
1190 T = new tools::Clang(*this); break;
1191 case Action::AssembleJobClass:
1192 T = new tools::ClangAs(*this); break;
1193 case Action::LinkJobClass:
1194 T = new tools::visualstudio::Link(*this); break;
1195 }
1196 }
1197
1198 return *T;
1199}
1200
1201bool Windows::IsIntegratedAssemblerDefault() const {
1202 return true;
1203}
1204
1205bool Windows::IsUnwindTablesDefault() const {
1206 // FIXME: Gross; we should probably have some separate target
1207 // definition, possibly even reusing the one in clang.
1208 return getArchName() == "x86_64";
1209}
1210
1211const char *Windows::GetDefaultRelocationModel() const {
1212 return "static";
1213}
1214
1215const char *Windows::GetForcedPicModel() const {
1216 if (getArchName() == "x86_64")
1217 return "pic";
1218 return 0;
1219}