blob: 537372cb0b914ea7b952bb4f57518aeb39d361e8 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
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"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000026#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000027#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000028#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000030#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000031
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000032#include <cstdlib> // ::getenv
33
Daniel Dunbar39176082009-03-20 00:20:03 +000034using namespace clang::driver;
35using namespace clang::driver::toolchains;
36
Daniel Dunbarf3955282009-09-04 18:34:51 +000037/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000038
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000039Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcc8e1892010-01-27 00:56:44 +000040 : ToolChain(Host, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000041{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000042 // Compute the initial Darwin version based on the host.
43 bool HadExtra;
44 std::string OSName = Triple.getOSName();
45 if (!Driver::GetReleaseVersion(&OSName[6],
46 DarwinVersion[0], DarwinVersion[1],
47 DarwinVersion[2], HadExtra))
48 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
49
Daniel Dunbar02633b52009-03-26 16:23:12 +000050 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000051 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
52 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000053}
54
Daniel Dunbar41800112010-08-02 05:43:56 +000055types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
56 types::ID Ty = types::lookupTypeForExtension(Ext);
57
58 // Darwin always preprocesses assembly files (unless -x is used explicitly).
59 if (Ty == types::TY_PP_Asm)
60 return types::TY_Asm;
61
62 return Ty;
63}
64
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000065bool Darwin::HasNativeLLVMSupport() const {
66 return true;
67}
68
Daniel Dunbareeff4062010-01-22 02:04:58 +000069// FIXME: Can we tablegen this?
70static const char *GetArmArchForMArch(llvm::StringRef Value) {
71 if (Value == "armv6k")
72 return "armv6";
73
74 if (Value == "armv5tej")
75 return "armv5";
76
77 if (Value == "xscale")
78 return "xscale";
79
80 if (Value == "armv4t")
81 return "armv4t";
82
83 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
84 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
85 Value == "armv7m")
86 return "armv7";
87
88 return 0;
89}
90
91// FIXME: Can we tablegen this?
92static const char *GetArmArchForMCpu(llvm::StringRef Value) {
93 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
94 Value == "arm946e-s" || Value == "arm966e-s" ||
95 Value == "arm968e-s" || Value == "arm10e" ||
96 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
97 Value == "arm1026ej-s")
98 return "armv5";
99
100 if (Value == "xscale")
101 return "xscale";
102
103 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
104 Value == "arm1176jz-s" || Value == "arm1176jzf-s")
105 return "armv6";
106
107 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
108 return "armv7";
109
110 return 0;
111}
112
113llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
114 switch (getTriple().getArch()) {
115 default:
116 return getArchName();
117
118 case llvm::Triple::arm: {
119 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
120 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
121 return Arch;
122
123 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
124 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
125 return Arch;
126
127 return "arm";
128 }
129 }
130}
131
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000132DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple)
133 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000134{
Daniel Dunbarc2bda622010-08-02 05:44:01 +0000135 // We can only work with 4.2.1 currently.
136 GCCVersion[0] = 4;
137 GCCVersion[1] = 2;
138 GCCVersion[2] = 1;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000139
140 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000141 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +0000142 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000143 ToolChainDir += "/";
144 ToolChainDir += llvm::utostr(GCCVersion[0]);
145 ToolChainDir += '.';
146 ToolChainDir += llvm::utostr(GCCVersion[1]);
147 ToolChainDir += '.';
148 ToolChainDir += llvm::utostr(GCCVersion[2]);
149
Daniel Dunbar74782b02009-10-20 19:25:43 +0000150 // Try the next major version if that tool chain dir is invalid.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000151 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000152 bool Exists;
153 if (llvm::sys::fs::exists(Tmp, Exists) || Exists) {
Daniel Dunbar74782b02009-10-20 19:25:43 +0000154 std::string Next = "i686-apple-darwin";
155 Next += llvm::utostr(DarwinVersion[0] + 1);
156 Next += "/";
157 Next += llvm::utostr(GCCVersion[0]);
158 Next += '.';
159 Next += llvm::utostr(GCCVersion[1]);
160 Next += '.';
161 Next += llvm::utostr(GCCVersion[2]);
162
163 // Use that if it exists, otherwise hope the user isn't linking.
164 //
165 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +0000166 Tmp = "/usr/lib/gcc/" + Next;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000167 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists)
Daniel Dunbar74782b02009-10-20 19:25:43 +0000168 ToolChainDir = Next;
169 }
170
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000171 std::string Path;
172 if (getArchName() == "x86_64") {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000173 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000174 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000175 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000176 Path += "/x86_64";
177 getFilePaths().push_back(Path);
178
179 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000180 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000181 Path += "/x86_64";
182 getFilePaths().push_back(Path);
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Daniel Dunbaree788e72009-12-21 18:54:17 +0000185 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000186 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000187 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000188 getFilePaths().push_back(Path);
189
190 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000191 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000192 getFilePaths().push_back(Path);
193
Daniel Dunbaree788e72009-12-21 18:54:17 +0000194 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000195 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000196 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000197 getProgramPaths().push_back(Path);
198
199 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000200 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000201 getProgramPaths().push_back(Path);
202
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000203 getProgramPaths().push_back(getDriver().getInstalledDir());
204 if (getDriver().getInstalledDir() != getDriver().Dir)
205 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000206}
207
Daniel Dunbarf3955282009-09-04 18:34:51 +0000208Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000209 // Free tool implementations.
210 for (llvm::DenseMap<unsigned, Tool*>::iterator
211 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
212 delete it->second;
213}
214
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000215std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
216 llvm::Triple Triple(ComputeLLVMTriple(Args));
217
218 // If the target isn't initialized (e.g., an unknown Darwin platform, return
219 // the default triple).
220 if (!isTargetInitialized())
221 return Triple.getTriple();
222
223 unsigned Version[3];
224 getTargetVersion(Version);
225
226 // Mangle the target version into the OS triple component. For historical
227 // reasons that make little sense, the version passed here is the "darwin"
228 // version, which drops the 10 and offsets by 4. See inverse code when
229 // setting the OS version preprocessor define.
230 if (!isTargetIPhoneOS()) {
231 Version[0] = Version[1] + 4;
232 Version[1] = Version[2];
233 Version[2] = 0;
234 } else {
235 // Use the environment to communicate that we are targetting iPhoneOS.
236 Triple.setEnvironmentName("iphoneos");
237 }
238
239 llvm::SmallString<16> Str;
240 llvm::raw_svector_ostream(Str) << "darwin" << Version[0]
241 << "." << Version[1] << "." << Version[2];
242 Triple.setOSName(Str.str());
243
244 return Triple.getTriple();
245}
246
Daniel Dunbarf3955282009-09-04 18:34:51 +0000247Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000248 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000249 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000250 Key = Action::AnalyzeJobClass;
251 else
252 Key = JA.getKind();
253
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000254 // FIXME: This doesn't belong here, but ideally we will support static soon
255 // anyway.
256 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
257 C.getArgs().hasArg(options::OPT_static) ||
258 C.getArgs().hasArg(options::OPT_fapple_kext));
259 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
260 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
261 options::OPT_no_integrated_as,
262 IsIADefault);
263
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000264 Tool *&T = Tools[Key];
265 if (!T) {
266 switch (Key) {
267 case Action::InputClass:
268 case Action::BindArchClass:
269 assert(0 && "Invalid tool kind.");
270 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000271 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000272 case Action::AnalyzeJobClass:
273 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000274 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000275 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000276 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000277 case Action::AssembleJobClass: {
278 if (UseIntegratedAs)
279 T = new tools::ClangAs(*this);
280 else
281 T = new tools::darwin::Assemble(*this);
282 break;
283 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000284 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000285 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000286 case Action::LipoJobClass:
287 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000288 case Action::DsymutilJobClass:
289 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000290 }
291 }
292
293 return *T;
294}
295
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000296void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
297 ArgStringList &CmdArgs) const {
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000298 std::string Tmp;
299
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000300 // FIXME: Derive these correctly.
301 if (getArchName() == "x86_64") {
302 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
303 "/x86_64"));
304 // Intentionally duplicated for (temporary) gcc bug compatibility.
305 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
306 "/x86_64"));
307 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000308
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000309 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000310
311 Tmp = getDriver().Dir + "/../lib/gcc/" + ToolChainDir;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000312 bool Exists;
313 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists)
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000314 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
315 Tmp = getDriver().Dir + "/../lib/gcc";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000316 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists)
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000317 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 // Intentionally duplicated for (temporary) gcc bug compatibility.
320 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000321 Tmp = getDriver().Dir + "/../lib/" + ToolChainDir;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000322 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists)
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000323 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
324 Tmp = getDriver().Dir + "/../lib";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000325 if (!llvm::sys::fs::exists(Tmp, Exists) && Exists)
Daniel Dunbare3c153a2010-04-10 18:18:57 +0000326 CmdArgs.push_back(Args.MakeArgString("-L" + Tmp));
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000327 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
328 "/../../../" + ToolChainDir));
329 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
330 "/../../.."));
331}
332
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000333void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
334 ArgStringList &CmdArgs) const {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000335 // Note that this routine is only used for targetting OS X.
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000336
337 // Derived from libgcc and lib specs but refactored.
338 if (Args.hasArg(options::OPT_static)) {
339 CmdArgs.push_back("-lgcc_static");
340 } else {
341 if (Args.hasArg(options::OPT_static_libgcc)) {
342 CmdArgs.push_back("-lgcc_eh");
343 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
344 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000345 if (isTargetIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000346 CmdArgs.push_back("-lgcc_s.1");
347 } else {
348 CmdArgs.push_back("-lgcc_s.10.5");
349 }
350 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
Daniel Dunbar8a0d94d2010-01-10 00:46:10 +0000351 Args.hasFlag(options::OPT_fexceptions,
352 options::OPT_fno_exceptions) ||
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000353 Args.hasArg(options::OPT_fgnu_runtime)) {
354 // FIXME: This is probably broken on 10.3?
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000355 if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000356 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000357 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000358 CmdArgs.push_back("-lgcc_s.10.5");
359 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000360 if (isMacosxVersionLT(10, 3, 9))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000361 ; // Do nothing.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000362 else if (isMacosxVersionLT(10, 5))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000363 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000364 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000365 CmdArgs.push_back("-lgcc_s.10.5");
366 }
367
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000368 if (isTargetIPhoneOS() || isMacosxVersionLT(10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000369 CmdArgs.push_back("-lgcc");
370 CmdArgs.push_back("-lSystem");
371 } else {
372 CmdArgs.push_back("-lSystem");
373 CmdArgs.push_back("-lgcc");
374 }
375 }
376}
377
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000378DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
379 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000380{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000381 getProgramPaths().push_back(getDriver().getInstalledDir());
382 if (getDriver().getInstalledDir() != getDriver().Dir)
383 getProgramPaths().push_back(getDriver().Dir);
384
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000385 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000386 getProgramPaths().push_back(getDriver().getInstalledDir());
387 if (getDriver().getInstalledDir() != getDriver().Dir)
388 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000389
390 // For fallback, we need to know how to find the GCC cc1 executables, so we
391 // also add the GCC libexec paths. This is legiy code that can be removed once
392 // fallback is no longer useful.
393 std::string ToolChainDir = "i686-apple-darwin";
394 ToolChainDir += llvm::utostr(DarwinVersion[0]);
395 ToolChainDir += "/4.2.1";
396
397 std::string Path = getDriver().Dir;
398 Path += "/../libexec/gcc/";
399 Path += ToolChainDir;
400 getProgramPaths().push_back(Path);
401
402 Path = "/usr/libexec/gcc/";
403 Path += ToolChainDir;
404 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000405}
406
407void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
408 ArgStringList &CmdArgs) const {
409 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000410
411 // Unfortunately, we still might depend on a few of the libraries that are
412 // only available in the gcc library directory (in particular
413 // libstdc++.dylib). For now, hardcode the path to the known install location.
414 llvm::sys::Path P(getDriver().Dir);
415 P.eraseComponent(); // .../usr/bin -> ../usr
416 P.appendComponent("lib");
417 P.appendComponent("gcc");
418 switch (getTriple().getArch()) {
419 default:
420 assert(0 && "Invalid Darwin arch!");
421 case llvm::Triple::x86:
422 case llvm::Triple::x86_64:
423 P.appendComponent("i686-apple-darwin10");
424 break;
425 case llvm::Triple::arm:
426 case llvm::Triple::thumb:
427 P.appendComponent("arm-apple-darwin10");
428 break;
429 case llvm::Triple::ppc:
430 case llvm::Triple::ppc64:
431 P.appendComponent("powerpc-apple-darwin10");
432 break;
433 }
434 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000435
436 // Determine the arch specific GCC subdirectory.
437 const char *ArchSpecificDir = 0;
438 switch (getTriple().getArch()) {
439 default:
440 break;
441 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000442 case llvm::Triple::thumb: {
443 std::string Triple = ComputeLLVMTriple(Args);
444 llvm::StringRef TripleStr = Triple;
445 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
446 ArchSpecificDir = "v5";
447 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
448 ArchSpecificDir = "v6";
449 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
450 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000451 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000452 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000453 case llvm::Triple::ppc64:
454 ArchSpecificDir = "ppc64";
455 break;
456 case llvm::Triple::x86_64:
457 ArchSpecificDir = "x86_64";
458 break;
459 }
460
461 if (ArchSpecificDir) {
462 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000463 bool Exists;
464 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000465 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
466 P.eraseComponent();
467 }
468
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000469 bool Exists;
470 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000471 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000472}
473
474void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
475 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000476 // Darwin doesn't support real static executables, don't link any runtime
477 // libraries with -static.
478 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000479 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000480
481 // Reject -static-libgcc for now, we can deal with this when and if someone
482 // cares. This is useful in situations where someone wants to statically link
483 // something like libstdc++, and needs its runtime support routines.
484 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000485 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000486 << A->getAsString(Args);
487 return;
488 }
489
Daniel Dunbareec99102010-01-22 03:38:14 +0000490 // Otherwise link libSystem, then the dynamic runtime library, and finally any
491 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000492 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000493
494 // Select the dynamic runtime library and the target specific static library.
495 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000496 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000497 CmdArgs.push_back("-lgcc_s.1");
498
499 // We may need some static functions for armv6/thumb which are required to
500 // be in the same linkage unit as their caller.
501 if (getDarwinArchName(Args) == "armv6")
502 DarwinStaticLib = "libclang_rt.armv6.a";
503 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000504 // The dynamic runtime library was merged with libSystem for 10.6 and
505 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000506 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000507 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000508 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000509 CmdArgs.push_back("-lgcc_s.10.5");
510
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000511 // For OS X, we thought we would only need a static runtime library when
512 // targetting 10.4, to provide versions of the static functions which were
513 // omitted from 10.4.dylib.
514 //
515 // Unfortunately, that turned out to not be true, because Darwin system
516 // headers can still use eprintf on i386, and it is not exported from
517 // libSystem. Therefore, we still must provide a runtime library just for
518 // the tiny tiny handful of projects that *might* use that symbol.
519 if (isMacosxVersionLT(10, 5)) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000520 DarwinStaticLib = "libclang_rt.10.4.a";
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000521 } else {
522 if (getTriple().getArch() == llvm::Triple::x86)
523 DarwinStaticLib = "libclang_rt.eprintf.a";
524 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000525 }
526
527 /// Add the target specific static library, if needed.
528 if (DarwinStaticLib) {
529 llvm::sys::Path P(getDriver().ResourceDir);
530 P.appendComponent("lib");
531 P.appendComponent("darwin");
532 P.appendComponent(DarwinStaticLib);
533
534 // For now, allow missing resource libraries to support developers who may
535 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000536 bool Exists;
537 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareec99102010-01-22 03:38:14 +0000538 CmdArgs.push_back(Args.MakeArgString(P.str()));
539 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000540}
541
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000542void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000543 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000544
Daniel Dunbar26031372010-01-27 00:56:25 +0000545 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
546 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000547 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000548 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000549 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000550 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000551 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000552 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000553 // If neither OS X nor iPhoneOS targets were specified, check for
554 // environment defines.
555 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
556 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000557
Daniel Dunbar816bc312010-01-26 01:45:19 +0000558 // Ignore empty strings.
559 if (OSXTarget && OSXTarget[0] == '\0')
560 OSXTarget = 0;
561 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
562 iPhoneOSTarget = 0;
563
Daniel Dunbar39053672010-02-02 17:31:12 +0000564 // Diagnose conflicting deployment targets, and choose default platform
565 // based on the tool chain.
566 //
567 // FIXME: Don't hardcode default here.
568 if (OSXTarget && iPhoneOSTarget) {
569 // FIXME: We should see if we can get away with warning or erroring on
570 // this. Perhaps put under -pedantic?
571 if (getTriple().getArch() == llvm::Triple::arm ||
572 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000573 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000574 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000575 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000576 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000577
Daniel Dunbar39053672010-02-02 17:31:12 +0000578 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000579 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000580 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
581 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000582 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000583 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000584 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
585 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000586 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000587 // Otherwise, assume we are targeting OS X.
588 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000589 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
590 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000591 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Daniel Dunbar26031372010-01-27 00:56:25 +0000594 // Set the tool chain target information.
595 unsigned Major, Minor, Micro;
596 bool HadExtra;
597 if (OSXVersion) {
598 assert(!iPhoneVersion && "Unknown target platform!");
599 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
600 Micro, HadExtra) || HadExtra ||
601 Major != 10 || Minor >= 10 || Micro >= 10)
602 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
603 << OSXVersion->getAsString(Args);
604 } else {
605 assert(iPhoneVersion && "Unknown target platform!");
606 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
607 Micro, HadExtra) || HadExtra ||
608 Major >= 10 || Minor >= 100 || Micro >= 100)
609 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
610 << iPhoneVersion->getAsString(Args);
611 }
612 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000613}
614
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000615void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000616 ArgStringList &CmdArgs) const {
617 CXXStdlibType Type = GetCXXStdlibType(Args);
618
619 switch (Type) {
620 case ToolChain::CST_Libcxx:
621 CmdArgs.push_back("-lc++");
622 break;
623
624 case ToolChain::CST_Libstdcxx: {
625 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
626 // it was previously found in the gcc lib dir. However, for all the Darwin
627 // platforms we care about it was -lstdc++.6, so we search for that
628 // explicitly if we can't see an obvious -lstdc++ candidate.
629
630 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000631 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000632 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
633 llvm::sys::Path P(A->getValue(Args));
634 P.appendComponent("usr");
635 P.appendComponent("lib");
636 P.appendComponent("libstdc++.dylib");
637
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000638 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000639 P.eraseComponent();
640 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000641 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000642 CmdArgs.push_back(Args.MakeArgString(P.str()));
643 return;
644 }
645 }
646 }
647
648 // Otherwise, look in the root.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000649 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
650 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000651 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
652 return;
653 }
654
655 // Otherwise, let the linker search.
656 CmdArgs.push_back("-lstdc++");
657 break;
658 }
659 }
660}
661
Shantonu Sen7433fed2010-09-17 18:39:08 +0000662void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
663 ArgStringList &CmdArgs) const {
664
665 // For Darwin platforms, use the compiler-rt-based support library
666 // instead of the gcc-provided one (which is also incidentally
667 // only present in the gcc lib dir, which makes it hard to find).
668
669 llvm::sys::Path P(getDriver().ResourceDir);
670 P.appendComponent("lib");
671 P.appendComponent("darwin");
672 P.appendComponent("libclang_rt.cc_kext.a");
673
674 // For now, allow missing resource libraries to support developers who may
675 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000676 bool Exists;
677 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000678 CmdArgs.push_back(Args.MakeArgString(P.str()));
679}
680
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000681DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
682 const char *BoundArch) const {
683 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
684 const OptTable &Opts = getDriver().getOpts();
685
686 // FIXME: We really want to get out of the tool chain level argument
687 // translation business, as it makes the driver functionality much
688 // more opaque. For now, we follow gcc closely solely for the
689 // purpose of easily achieving feature parity & testability. Once we
690 // have something that works, we should reevaluate each translation
691 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000692
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000693 for (ArgList::const_iterator it = Args.begin(),
694 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000695 Arg *A = *it;
696
697 if (A->getOption().matches(options::OPT_Xarch__)) {
698 // FIXME: Canonicalize name.
699 if (getArchName() != A->getValue(Args, 0))
700 continue;
701
Daniel Dunbar0e100312010-06-14 21:23:08 +0000702 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
703 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000704 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000706 // If the argument parsing failed or more than one argument was
707 // consumed, the -Xarch_ argument's parameter tried to consume
708 // extra arguments. Emit an error and ignore.
709 //
710 // We also want to disallow any options which would alter the
711 // driver behavior; that isn't going to work in our model. We
712 // use isDriverOption() as an approximation, although things
713 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000714 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000715 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000716 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000717 << A->getAsString(Args);
718 continue;
719 }
720
Daniel Dunbar478edc22009-03-29 22:29:05 +0000721 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000722 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000723
724 DAL->AddSynthesizedArg(A);
Mike Stump1eb44332009-09-09 15:08:12 +0000725 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000726
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000727 // Sob. These is strictly gcc compatible for the time being. Apple
728 // gcc translates options twice, which means that self-expanding
729 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000730 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000731 default:
732 DAL->append(A);
733 break;
734
735 case options::OPT_mkernel:
736 case options::OPT_fapple_kext:
737 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000738 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
739 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000740 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000742 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000743 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
744 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000745 break;
746
747 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000748 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
749 DAL->AddFlagArg(A,
750 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000751 break;
752
753 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000754 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
755 DAL->AddFlagArg(A,
756 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000757 break;
758
759 case options::OPT_fterminated_vtables:
760 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000761 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
762 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000763 break;
764
765 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000766 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000767 break;
768
769 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000770 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000771 break;
772
773 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000774 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000775 break;
776
777 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000778 DAL->AddFlagArg(A,
779 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000780 break;
781
782 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000783 DAL->AddFlagArg(A,
784 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000785 break;
786
787 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000788 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000789 break;
790
791 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000792 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000793 break;
794 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000795 }
796
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000797 if (getTriple().getArch() == llvm::Triple::x86 ||
798 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000799 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000800 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000801
802 // Add the arch options based on the particular spelling of -arch, to match
803 // how the driver driver works.
804 if (BoundArch) {
805 llvm::StringRef Name = BoundArch;
806 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
807 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
808
809 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
810 // which defines the list of which architectures we accept.
811 if (Name == "ppc")
812 ;
813 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000814 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000815 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000816 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000817 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000818 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000819 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000820 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000821 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000822 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000823 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000824 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000825 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000826 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000827 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000828 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000829
830 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000831 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000832
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000833 else if (Name == "i386")
834 ;
835 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000836 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000837 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000838 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000839 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000840 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000841 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000842 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000843 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000844 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000845 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000846 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000847 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000848 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000849
850 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000851 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000852
853 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000854 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000855 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000856 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000857 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000858 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000859 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000860 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000861 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000862 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000863 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000864 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000865
866 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000867 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000868 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000869
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000870 // Add an explicit version min argument for the deployment target. We do this
871 // after argument translation because -Xarch_ arguments may add a version min
872 // argument.
873 AddDeploymentTarget(*DAL);
874
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000875 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000876}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000877
Daniel Dunbarf3955282009-09-04 18:34:51 +0000878bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000879 // FIXME: Gross; we should probably have some separate target
880 // definition, possibly even reusing the one in clang.
881 return getArchName() == "x86_64";
882}
883
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000884bool Darwin::UseDwarfDebugFlags() const {
885 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
886 return S[0] != '\0';
887 return false;
888}
889
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000890bool Darwin::UseSjLjExceptions() const {
891 // Darwin uses SjLj exceptions on ARM.
892 return (getTriple().getArch() == llvm::Triple::arm ||
893 getTriple().getArch() == llvm::Triple::thumb);
894}
895
Daniel Dunbarf3955282009-09-04 18:34:51 +0000896const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000897 return "pic";
898}
899
Daniel Dunbarf3955282009-09-04 18:34:51 +0000900const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000901 if (getArchName() == "x86_64")
902 return "pic";
903 return 0;
904}
905
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000906bool Darwin::SupportsObjCGC() const {
907 // Garbage collection is supported everywhere except on iPhone OS.
908 return !isTargetIPhoneOS();
909}
910
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000911std::string
912Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
913 return ComputeLLVMTriple(Args);
914}
915
Daniel Dunbar39176082009-03-20 00:20:03 +0000916/// Generic_GCC - A tool chain using the 'gcc' command to perform
917/// all subcommands; this relies on gcc translating the majority of
918/// command line options.
919
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000920Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000921 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000922 getProgramPaths().push_back(getDriver().getInstalledDir());
923 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
924 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000925}
926
Daniel Dunbar39176082009-03-20 00:20:03 +0000927Generic_GCC::~Generic_GCC() {
928 // Free tool implementations.
929 for (llvm::DenseMap<unsigned, Tool*>::iterator
930 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
931 delete it->second;
932}
933
Mike Stump1eb44332009-09-09 15:08:12 +0000934Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000935 const JobAction &JA) const {
936 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000937 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000938 Key = Action::AnalyzeJobClass;
939 else
940 Key = JA.getKind();
941
942 Tool *&T = Tools[Key];
943 if (!T) {
944 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000945 case Action::InputClass:
946 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000947 assert(0 && "Invalid tool kind.");
948 case Action::PreprocessJobClass:
949 T = new tools::gcc::Preprocess(*this); break;
950 case Action::PrecompileJobClass:
951 T = new tools::gcc::Precompile(*this); break;
952 case Action::AnalyzeJobClass:
953 T = new tools::Clang(*this); break;
954 case Action::CompileJobClass:
955 T = new tools::gcc::Compile(*this); break;
956 case Action::AssembleJobClass:
957 T = new tools::gcc::Assemble(*this); break;
958 case Action::LinkJobClass:
959 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000961 // This is a bit ungeneric, but the only platform using a driver
962 // driver is Darwin.
963 case Action::LipoJobClass:
964 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000965 case Action::DsymutilJobClass:
966 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000967 }
968 }
969
970 return *T;
971}
972
Daniel Dunbar39176082009-03-20 00:20:03 +0000973bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000974 // FIXME: Gross; we should probably have some separate target
975 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000976 return getArchName() == "x86_64";
977}
978
979const char *Generic_GCC::GetDefaultRelocationModel() const {
980 return "static";
981}
982
983const char *Generic_GCC::GetForcedPicModel() const {
984 return 0;
985}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000986
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000987/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
988/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
989/// Currently does not support anything else but compilation.
990
991TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
992 : ToolChain(Host, Triple) {
993 // Path mangling to find libexec
994 std::string Path(getDriver().Dir);
995
996 Path += "/../libexec";
997 getProgramPaths().push_back(Path);
998}
999
1000TCEToolChain::~TCEToolChain() {
1001 for (llvm::DenseMap<unsigned, Tool*>::iterator
1002 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1003 delete it->second;
1004}
1005
1006bool TCEToolChain::IsMathErrnoDefault() const {
1007 return true;
1008}
1009
1010bool TCEToolChain::IsUnwindTablesDefault() const {
1011 return false;
1012}
1013
1014const char *TCEToolChain::GetDefaultRelocationModel() const {
1015 return "static";
1016}
1017
1018const char *TCEToolChain::GetForcedPicModel() const {
1019 return 0;
1020}
1021
1022Tool &TCEToolChain::SelectTool(const Compilation &C,
1023 const JobAction &JA) const {
1024 Action::ActionClass Key;
1025 Key = Action::AnalyzeJobClass;
1026
1027 Tool *&T = Tools[Key];
1028 if (!T) {
1029 switch (Key) {
1030 case Action::PreprocessJobClass:
1031 T = new tools::gcc::Preprocess(*this); break;
1032 case Action::AnalyzeJobClass:
1033 T = new tools::Clang(*this); break;
1034 default:
1035 assert(false && "Unsupported action for TCE target.");
1036 }
1037 }
1038 return *T;
1039}
1040
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001041/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1042
1043OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001044 : Generic_ELF(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001045 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001046 getFilePaths().push_back("/usr/lib");
1047}
1048
1049Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
1050 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001051 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001052 Key = Action::AnalyzeJobClass;
1053 else
1054 Key = JA.getKind();
1055
Rafael Espindoladda5b922010-11-07 23:13:01 +00001056 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1057 options::OPT_no_integrated_as,
1058 IsIntegratedAssemblerDefault());
1059
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001060 Tool *&T = Tools[Key];
1061 if (!T) {
1062 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001063 case Action::AssembleJobClass: {
1064 if (UseIntegratedAs)
1065 T = new tools::ClangAs(*this);
1066 else
1067 T = new tools::openbsd::Assemble(*this);
1068 break;
1069 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001070 case Action::LinkJobClass:
1071 T = new tools::openbsd::Link(*this); break;
1072 default:
1073 T = &Generic_GCC::SelectTool(C, JA);
1074 }
1075 }
1076
1077 return *T;
1078}
1079
Daniel Dunbar75358d22009-03-30 21:06:03 +00001080/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1081
Daniel Dunbar214afe92010-08-02 05:43:59 +00001082FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001083 : Generic_ELF(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001084
1085 // Determine if we are compiling 32-bit code on an x86_64 platform.
1086 bool Lib32 = false;
1087 if (Triple.getArch() == llvm::Triple::x86 &&
1088 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1089 llvm::Triple::x86_64)
1090 Lib32 = true;
1091
Daniel Dunbar7fb2c252010-06-15 15:03:31 +00001092 getProgramPaths().push_back(getDriver().Dir + "/../libexec");
1093 getProgramPaths().push_back("/usr/libexec");
Daniel Dunbarbc534662009-04-02 18:30:04 +00001094 if (Lib32) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001095 getFilePaths().push_back(getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +00001096 getFilePaths().push_back("/usr/lib32");
1097 } else {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001098 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +00001099 getFilePaths().push_back("/usr/lib");
1100 }
Daniel Dunbar75358d22009-03-30 21:06:03 +00001101}
1102
1103Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
1104 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001105 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001106 Key = Action::AnalyzeJobClass;
1107 else
1108 Key = JA.getKind();
1109
Roman Divacky67dece72010-11-08 17:46:39 +00001110 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1111 options::OPT_no_integrated_as,
1112 IsIntegratedAssemblerDefault());
1113
Daniel Dunbar75358d22009-03-30 21:06:03 +00001114 Tool *&T = Tools[Key];
1115 if (!T) {
1116 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001117 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001118 if (UseIntegratedAs)
1119 T = new tools::ClangAs(*this);
1120 else
1121 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001122 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001123 case Action::LinkJobClass:
1124 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001125 default:
1126 T = &Generic_GCC::SelectTool(C, JA);
1127 }
1128 }
1129
1130 return *T;
1131}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001132
Chris Lattner38e317d2010-07-07 16:01:42 +00001133/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1134
1135Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1136 : Generic_GCC(Host, Triple) {
1137 getFilePaths().push_back(getDriver().Dir + "/../lib");
1138 getFilePaths().push_back("/usr/lib");
1139 getFilePaths().push_back("/usr/gnu/lib");
1140 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1141}
1142
1143Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
1144 Action::ActionClass Key;
1145 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1146 Key = Action::AnalyzeJobClass;
1147 else
1148 Key = JA.getKind();
1149
1150 Tool *&T = Tools[Key];
1151 if (!T) {
1152 switch (Key) {
1153 case Action::AssembleJobClass:
1154 T = new tools::minix::Assemble(*this); break;
1155 case Action::LinkJobClass:
1156 T = new tools::minix::Link(*this); break;
1157 default:
1158 T = &Generic_GCC::SelectTool(C, JA);
1159 }
1160 }
1161
1162 return *T;
1163}
1164
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001165/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1166
1167AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1168 : Generic_GCC(Host, Triple) {
1169
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001170 getProgramPaths().push_back(getDriver().getInstalledDir());
1171 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1172 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001173
Daniel Dunbaree788e72009-12-21 18:54:17 +00001174 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001175 getFilePaths().push_back("/usr/lib");
1176 getFilePaths().push_back("/usr/sfw/lib");
1177 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001178 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001179
1180}
1181
1182Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
1183 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001184 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001185 Key = Action::AnalyzeJobClass;
1186 else
1187 Key = JA.getKind();
1188
1189 Tool *&T = Tools[Key];
1190 if (!T) {
1191 switch (Key) {
1192 case Action::AssembleJobClass:
1193 T = new tools::auroraux::Assemble(*this); break;
1194 case Action::LinkJobClass:
1195 T = new tools::auroraux::Link(*this); break;
1196 default:
1197 T = &Generic_GCC::SelectTool(C, JA);
1198 }
1199 }
1200
1201 return *T;
1202}
1203
1204
Eli Friedman6b3454a2009-05-26 07:52:18 +00001205/// Linux toolchain (very bare-bones at the moment).
1206
Rafael Espindolac1da9812010-11-07 20:14:31 +00001207enum LinuxDistro {
1208 DebianLenny,
1209 DebianSqueeze,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001210 Exherbo,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001211 Fedora13,
1212 Fedora14,
1213 OpenSuse11_3,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001214 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001215 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001216 UbuntuLucid,
1217 UbuntuMaverick,
1218 UnknownDistro
1219};
1220
1221static bool IsFedora(enum LinuxDistro Distro) {
1222 return Distro == Fedora13 || Distro == Fedora14;
1223}
1224
1225static bool IsOpenSuse(enum LinuxDistro Distro) {
1226 return Distro == OpenSuse11_3;
1227}
1228
1229static bool IsDebian(enum LinuxDistro Distro) {
1230 return Distro == DebianLenny || Distro == DebianSqueeze;
1231}
1232
1233static bool IsUbuntu(enum LinuxDistro Distro) {
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001234 return Distro == UbuntuLucid || Distro == UbuntuMaverick ||
1235 Distro == UbuntuJaunty || Distro == UbuntuKarmic;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001236}
1237
1238static bool IsDebianBased(enum LinuxDistro Distro) {
1239 return IsDebian(Distro) || IsUbuntu(Distro);
1240}
1241
1242static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001243 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001244 bool Exists;
1245 if (Distro == Exherbo &&
1246 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001247 return false;
1248
Rafael Espindolac1da9812010-11-07 20:14:31 +00001249 return true;
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001250 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001251 if (Arch == llvm::Triple::x86 && IsDebianBased(Distro))
1252 return true;
1253 return false;
1254}
1255
1256static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001257 llvm::OwningPtr<llvm::MemoryBuffer> File;
1258 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
1259 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001260 llvm::SmallVector<llvm::StringRef, 8> Lines;
1261 Data.split(Lines, "\n");
1262 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
1263 if (Lines[i] == "DISTRIB_CODENAME=maverick")
1264 return UbuntuMaverick;
1265 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1266 return UbuntuLucid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001267 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001268 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001269 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1270 return UbuntuKarmic;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001271 }
1272 return UnknownDistro;
1273 }
1274
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001275 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1276 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001277 if (Data.startswith("Fedora release 14 (Laughlin)"))
1278 return Fedora14;
1279 else if (Data.startswith("Fedora release 13 (Goddard)"))
1280 return Fedora13;
1281 return UnknownDistro;
1282 }
1283
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001284 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1285 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001286 if (Data[0] == '5')
1287 return DebianLenny;
1288 else if (Data.startswith("squeeze/sid"))
1289 return DebianSqueeze;
1290 return UnknownDistro;
1291 }
1292
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001293 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1294 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001295 if (Data.startswith("openSUSE 11.3"))
1296 return OpenSuse11_3;
1297 return UnknownDistro;
1298 }
1299
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001300 bool Exists;
1301 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001302 return Exherbo;
1303
Rafael Espindolac1da9812010-11-07 20:14:31 +00001304 return UnknownDistro;
1305}
1306
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001307Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001308 : Generic_ELF(Host, Triple) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001309 llvm::Triple::ArchType Arch =
1310 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Daniel Dunbara9822de2009-08-06 01:47:11 +00001311
Rafael Espindolac1da9812010-11-07 20:14:31 +00001312 std::string Suffix32 = "";
1313 if (Arch == llvm::Triple::x86_64)
1314 Suffix32 = "/32";
Daniel Dunbara9822de2009-08-06 01:47:11 +00001315
Rafael Espindolac1da9812010-11-07 20:14:31 +00001316 std::string Suffix64 = "";
1317 if (Arch == llvm::Triple::x86)
1318 Suffix64 = "/64";
1319
1320 std::string Lib32 = "lib";
1321
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001322 bool Exists;
1323 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001324 Lib32 = "lib32";
1325
1326 std::string Lib64 = "lib";
Michael J. Spencer2dea7c72011-01-12 23:54:48 +00001327 bool Symlink;
Chris Lattner48aef362011-01-13 01:35:58 +00001328 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1329 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001330 Lib64 = "lib64";
1331
1332 std::string GccTriple = "";
1333 if (Arch == llvm::Triple::arm) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001334 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1335 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001336 GccTriple = "arm-linux-gnueabi";
1337 } else if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001338 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1339 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001340 GccTriple = "x86_64-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001341 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1342 Exists) && Exists)
Rafael Espindola53dd00b2010-11-17 00:25:26 +00001343 GccTriple = "x86_64-unknown-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001344 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1345 Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001346 GccTriple = "x86_64-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001347 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1348 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001349 GccTriple = "x86_64-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001350 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1351 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001352 GccTriple = "x86_64-suse-linux";
1353 } else if (Arch == llvm::Triple::x86) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001354 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001355 GccTriple = "i686-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001356 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1357 Exists)
Nuno Lopes2a69ddd2010-11-19 17:26:57 +00001358 GccTriple = "i686-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001359 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1360 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001361 GccTriple = "i486-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001362 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1363 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001364 GccTriple = "i686-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001365 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1366 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001367 GccTriple = "i586-suse-linux";
1368 }
1369
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001370 const char* GccVersions[] = {"4.5.1", "4.5", "4.4.5", "4.4.4", "4.4.3", "4.4",
Rafael Espindolae4539ef2010-11-19 21:02:06 +00001371 "4.3.4", "4.3.3", "4.3.2"};
Rafael Espindolac1da9812010-11-07 20:14:31 +00001372 std::string Base = "";
1373 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1374 std::string Suffix = GccTriple + "/" + GccVersions[i];
1375 std::string t1 = "/usr/lib/gcc/" + Suffix;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001376 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001377 Base = t1;
1378 break;
1379 }
1380 std::string t2 = "/usr/lib64/gcc/" + Suffix;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001381 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001382 Base = t2;
1383 break;
1384 }
1385 }
1386
1387 path_list &Paths = getFilePaths();
1388 bool Is32Bits = getArch() == llvm::Triple::x86;
1389
1390 std::string Suffix;
1391 std::string Lib;
1392
1393 if (Is32Bits) {
1394 Suffix = Suffix32;
1395 Lib = Lib32;
1396 } else {
1397 Suffix = Suffix64;
1398 Lib = Lib64;
1399 }
1400
1401 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001402 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001403 Linker = LinkerPath.str();
1404 else
1405 Linker = GetProgramPath("ld");
1406
1407 LinuxDistro Distro = DetectLinuxDistro(Arch);
1408
Rafael Espindola94c80222010-11-08 14:48:47 +00001409 if (IsUbuntu(Distro)) {
1410 ExtraOpts.push_back("-z");
1411 ExtraOpts.push_back("relro");
1412 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001413
1414 if (Arch == llvm::Triple::arm)
1415 ExtraOpts.push_back("-X");
1416
1417 if (IsFedora(Distro) || Distro == UbuntuMaverick)
1418 ExtraOpts.push_back("--hash-style=gnu");
1419
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001420 if (IsDebian(Distro) || Distro == UbuntuLucid || Distro == UbuntuJaunty ||
1421 Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001422 ExtraOpts.push_back("--hash-style=both");
1423
1424 if (IsFedora(Distro))
1425 ExtraOpts.push_back("--no-add-needed");
1426
Rafael Espindola021aaa42010-11-10 05:00:22 +00001427 if (Distro == DebianSqueeze || IsOpenSuse(Distro) ||
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001428 IsFedora(Distro) || Distro == UbuntuLucid || Distro == UbuntuMaverick ||
1429 Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001430 ExtraOpts.push_back("--build-id");
1431
1432 Paths.push_back(Base + Suffix);
1433 if (HasMultilib(Arch, Distro)) {
1434 if (IsOpenSuse(Distro) && Is32Bits)
1435 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1436 Paths.push_back(Base + "/../../../../" + Lib);
1437 Paths.push_back("/lib/../" + Lib);
1438 Paths.push_back("/usr/lib/../" + Lib);
1439 }
1440 if (!Suffix.empty())
1441 Paths.push_back(Base);
1442 if (IsOpenSuse(Distro))
1443 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1444 Paths.push_back(Base + "/../../..");
1445 if (Arch == getArch() && IsUbuntu(Distro))
1446 Paths.push_back("/usr/lib/" + GccTriple);
1447}
1448
1449bool Linux::HasNativeLLVMSupport() const {
1450 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00001451}
1452
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001453Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA) const {
1454 Action::ActionClass Key;
1455 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1456 Key = Action::AnalyzeJobClass;
1457 else
1458 Key = JA.getKind();
1459
Rafael Espindoladda5b922010-11-07 23:13:01 +00001460 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1461 options::OPT_no_integrated_as,
1462 IsIntegratedAssemblerDefault());
1463
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001464 Tool *&T = Tools[Key];
1465 if (!T) {
1466 switch (Key) {
1467 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00001468 if (UseIntegratedAs)
1469 T = new tools::ClangAs(*this);
1470 else
1471 T = new tools::linuxtools::Assemble(*this);
1472 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001473 case Action::LinkJobClass:
1474 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001475 default:
1476 T = &Generic_GCC::SelectTool(C, JA);
1477 }
1478 }
1479
1480 return *T;
1481}
1482
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001483/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1484
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001485DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001486 : Generic_ELF(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001487
1488 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001489 getProgramPaths().push_back(getDriver().getInstalledDir());
1490 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1491 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001492
Daniel Dunbaree788e72009-12-21 18:54:17 +00001493 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001494 getFilePaths().push_back("/usr/lib");
1495 getFilePaths().push_back("/usr/lib/gcc41");
1496}
1497
1498Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
1499 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001500 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001501 Key = Action::AnalyzeJobClass;
1502 else
1503 Key = JA.getKind();
1504
1505 Tool *&T = Tools[Key];
1506 if (!T) {
1507 switch (Key) {
1508 case Action::AssembleJobClass:
1509 T = new tools::dragonfly::Assemble(*this); break;
1510 case Action::LinkJobClass:
1511 T = new tools::dragonfly::Link(*this); break;
1512 default:
1513 T = &Generic_GCC::SelectTool(C, JA);
1514 }
1515 }
1516
1517 return *T;
1518}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001519
1520Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1521 : ToolChain(Host, Triple) {
1522}
1523
1524Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA) const {
1525 Action::ActionClass Key;
1526 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1527 Key = Action::AnalyzeJobClass;
1528 else
1529 Key = JA.getKind();
1530
1531 Tool *&T = Tools[Key];
1532 if (!T) {
1533 switch (Key) {
1534 case Action::InputClass:
1535 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001536 case Action::LipoJobClass:
1537 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001538 assert(0 && "Invalid tool kind.");
1539 case Action::PreprocessJobClass:
1540 case Action::PrecompileJobClass:
1541 case Action::AnalyzeJobClass:
1542 case Action::CompileJobClass:
1543 T = new tools::Clang(*this); break;
1544 case Action::AssembleJobClass:
1545 T = new tools::ClangAs(*this); break;
1546 case Action::LinkJobClass:
1547 T = new tools::visualstudio::Link(*this); break;
1548 }
1549 }
1550
1551 return *T;
1552}
1553
1554bool Windows::IsIntegratedAssemblerDefault() const {
1555 return true;
1556}
1557
1558bool Windows::IsUnwindTablesDefault() const {
1559 // FIXME: Gross; we should probably have some separate target
1560 // definition, possibly even reusing the one in clang.
1561 return getArchName() == "x86_64";
1562}
1563
1564const char *Windows::GetDefaultRelocationModel() const {
1565 return "static";
1566}
1567
1568const char *Windows::GetForcedPicModel() const {
1569 if (getArchName() == "x86_64")
1570 return "pic";
1571 return 0;
1572}