blob: 1619ef8f84a96b155b27753fa610e0a7c7111959 [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
Rafael Espindola14ea13c2011-06-02 22:18:46 +000012#ifdef HAVE_CLANG_CONFIG_H
13# include "clang/Config/config.h"
14#endif
15
Daniel Dunbarf3cad362009-03-25 04:13:45 +000016#include "clang/Driver/Arg.h"
17#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000018#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000019#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000020#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000021#include "clang/Driver/HostInfo.h"
John McCall9f084a32011-07-06 00:26:06 +000022#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000023#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000024#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000025#include "clang/Driver/Options.h"
Douglas Gregor34916db2010-09-03 17:16:03 +000026#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000027
Daniel Dunbar00577ad2010-08-23 22:35:37 +000028#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000029#include "llvm/ADT/StringExtras.h"
John McCallf85e1932011-06-15 23:02:42 +000030#include "llvm/ADT/STLExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000031#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000032#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000033#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000034#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000035#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000036#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000037
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000038#include <cstdlib> // ::getenv
39
Dylan Noblesmith89bb6142011-06-23 13:50:47 +000040#include "llvm/Config/config.h" // for CXX_INCLUDE_ROOT
41
Daniel Dunbar39176082009-03-20 00:20:03 +000042using namespace clang::driver;
43using namespace clang::driver::toolchains;
44
Daniel Dunbarf3955282009-09-04 18:34:51 +000045/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000046
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000047Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
John McCallf85e1932011-06-15 23:02:42 +000048 : ToolChain(Host, Triple), TargetInitialized(false),
49 ARCRuntimeForSimulator(ARCSimulator_None)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000050{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000051 // Compute the initial Darwin version based on the host.
52 bool HadExtra;
53 std::string OSName = Triple.getOSName();
Daniel Dunbar34f9e292011-02-25 21:20:15 +000054 if (!Driver::GetReleaseVersion(&OSName.c_str()[6],
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000055 DarwinVersion[0], DarwinVersion[1],
56 DarwinVersion[2], HadExtra))
57 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
58
Daniel Dunbar02633b52009-03-26 16:23:12 +000059 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000060 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
61 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000062}
63
Daniel Dunbar41800112010-08-02 05:43:56 +000064types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
65 types::ID Ty = types::lookupTypeForExtension(Ext);
66
67 // Darwin always preprocesses assembly files (unless -x is used explicitly).
68 if (Ty == types::TY_PP_Asm)
69 return types::TY_Asm;
70
71 return Ty;
72}
73
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000074bool Darwin::HasNativeLLVMSupport() const {
75 return true;
76}
77
John McCall9f084a32011-07-06 00:26:06 +000078bool Darwin::hasARCRuntime() const {
John McCallf85e1932011-06-15 23:02:42 +000079 // FIXME: Remove this once there is a proper way to detect an ARC runtime
80 // for the simulator.
81 switch (ARCRuntimeForSimulator) {
82 case ARCSimulator_None:
83 break;
84 case ARCSimulator_HasARCRuntime:
85 return true;
86 case ARCSimulator_NoARCRuntime:
87 return false;
88 }
89
90 if (isTargetIPhoneOS())
91 return !isIPhoneOSVersionLT(5);
92 else
93 return !isMacosxVersionLT(10, 7);
94}
95
John McCall9f084a32011-07-06 00:26:06 +000096/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
97void Darwin::configureObjCRuntime(ObjCRuntime &runtime) const {
98 if (runtime.getKind() != ObjCRuntime::NeXT)
99 return ToolChain::configureObjCRuntime(runtime);
100
101 runtime.HasARC = runtime.HasWeak = hasARCRuntime();
John McCall256a76e2011-07-06 01:22:26 +0000102
103 // So far, objc_terminate is only available in iOS 5.
104 // FIXME: do the simulator logic properly.
105 if (!ARCRuntimeForSimulator && isTargetIPhoneOS())
106 runtime.HasTerminate = !isIPhoneOSVersionLT(5);
107 else
108 runtime.HasTerminate = false;
John McCall9f084a32011-07-06 00:26:06 +0000109}
110
Daniel Dunbareeff4062010-01-22 02:04:58 +0000111// FIXME: Can we tablegen this?
112static const char *GetArmArchForMArch(llvm::StringRef Value) {
113 if (Value == "armv6k")
114 return "armv6";
115
116 if (Value == "armv5tej")
117 return "armv5";
118
119 if (Value == "xscale")
120 return "xscale";
121
122 if (Value == "armv4t")
123 return "armv4t";
124
125 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
126 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
127 Value == "armv7m")
128 return "armv7";
129
130 return 0;
131}
132
133// FIXME: Can we tablegen this?
134static const char *GetArmArchForMCpu(llvm::StringRef Value) {
135 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
136 Value == "arm946e-s" || Value == "arm966e-s" ||
137 Value == "arm968e-s" || Value == "arm10e" ||
138 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
139 Value == "arm1026ej-s")
140 return "armv5";
141
142 if (Value == "xscale")
143 return "xscale";
144
145 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000146 Value == "arm1176jz-s" || Value == "arm1176jzf-s" ||
147 Value == "cortex-m0" )
Daniel Dunbareeff4062010-01-22 02:04:58 +0000148 return "armv6";
149
150 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
151 return "armv7";
152
153 return 0;
154}
155
156llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
157 switch (getTriple().getArch()) {
158 default:
159 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000160
Douglas Gregorf0594d82011-03-06 19:11:49 +0000161 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000162 case llvm::Triple::arm: {
163 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
164 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
165 return Arch;
166
167 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
168 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
169 return Arch;
170
171 return "arm";
172 }
173 }
174}
175
Daniel Dunbarf3955282009-09-04 18:34:51 +0000176Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000177 // Free tool implementations.
178 for (llvm::DenseMap<unsigned, Tool*>::iterator
179 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
180 delete it->second;
181}
182
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000183std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
184 llvm::Triple Triple(ComputeLLVMTriple(Args));
185
186 // If the target isn't initialized (e.g., an unknown Darwin platform, return
187 // the default triple).
188 if (!isTargetInitialized())
189 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000190
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000191 unsigned Version[3];
192 getTargetVersion(Version);
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000193
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000194 llvm::SmallString<16> Str;
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000195 llvm::raw_svector_ostream(Str)
Daniel Dunbar659d23a2011-04-19 23:34:17 +0000196 << (isTargetIPhoneOS() ? "ios" : "macosx")
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000197 << Version[0] << "." << Version[1] << "." << Version[2];
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000198 Triple.setOSName(Str.str());
199
200 return Triple.getTriple();
201}
202
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000203Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
204 const ActionList &Inputs) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000205 Action::ActionClass Key;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000206
207 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
208 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
209 if (Inputs.size() == 1 &&
210 types::isCXX(Inputs[0]->getType()) &&
211 getTriple().getOS() == llvm::Triple::Darwin &&
212 getTriple().getArch() == llvm::Triple::x86 &&
213 C.getArgs().getLastArg(options::OPT_fapple_kext))
214 Key = JA.getKind();
215 else
216 Key = Action::AnalyzeJobClass;
217 } else
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000218 Key = JA.getKind();
219
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000220 // FIXME: This doesn't belong here, but ideally we will support static soon
221 // anyway.
222 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
223 C.getArgs().hasArg(options::OPT_static) ||
224 C.getArgs().hasArg(options::OPT_fapple_kext));
225 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
226 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
227 options::OPT_no_integrated_as,
228 IsIADefault);
229
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000230 Tool *&T = Tools[Key];
231 if (!T) {
232 switch (Key) {
233 case Action::InputClass:
234 case Action::BindArchClass:
235 assert(0 && "Invalid tool kind.");
236 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000237 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000238 case Action::AnalyzeJobClass:
239 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000240 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000241 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000242 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000243 case Action::AssembleJobClass: {
244 if (UseIntegratedAs)
245 T = new tools::ClangAs(*this);
246 else
247 T = new tools::darwin::Assemble(*this);
248 break;
249 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000250 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000251 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000252 case Action::LipoJobClass:
253 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000254 case Action::DsymutilJobClass:
255 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000256 }
257 }
258
259 return *T;
260}
261
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000262
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000263DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
264 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000265{
Daniel Dunbar47023092011-03-18 19:25:15 +0000266 std::string UsrPrefix = "llvm-gcc-4.2/";
267
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000268 getProgramPaths().push_back(getDriver().getInstalledDir());
269 if (getDriver().getInstalledDir() != getDriver().Dir)
270 getProgramPaths().push_back(getDriver().Dir);
271
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000272 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000273 getProgramPaths().push_back(getDriver().getInstalledDir());
274 if (getDriver().getInstalledDir() != getDriver().Dir)
275 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000276
277 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000278 // also add the GCC libexec paths. This is legacy code that can be removed
279 // once fallback is no longer useful.
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000280 std::string ToolChainDir = "i686-apple-darwin";
281 ToolChainDir += llvm::utostr(DarwinVersion[0]);
282 ToolChainDir += "/4.2.1";
283
284 std::string Path = getDriver().Dir;
Daniel Dunbar47023092011-03-18 19:25:15 +0000285 Path += "/../" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000286 Path += ToolChainDir;
287 getProgramPaths().push_back(Path);
288
Daniel Dunbar47023092011-03-18 19:25:15 +0000289 Path = "/usr/" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000290 Path += ToolChainDir;
291 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000292}
293
294void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
295 ArgStringList &CmdArgs) const {
296 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000297
298 // Unfortunately, we still might depend on a few of the libraries that are
299 // only available in the gcc library directory (in particular
300 // libstdc++.dylib). For now, hardcode the path to the known install location.
301 llvm::sys::Path P(getDriver().Dir);
302 P.eraseComponent(); // .../usr/bin -> ../usr
303 P.appendComponent("lib");
304 P.appendComponent("gcc");
305 switch (getTriple().getArch()) {
306 default:
307 assert(0 && "Invalid Darwin arch!");
308 case llvm::Triple::x86:
309 case llvm::Triple::x86_64:
310 P.appendComponent("i686-apple-darwin10");
311 break;
312 case llvm::Triple::arm:
313 case llvm::Triple::thumb:
314 P.appendComponent("arm-apple-darwin10");
315 break;
316 case llvm::Triple::ppc:
317 case llvm::Triple::ppc64:
318 P.appendComponent("powerpc-apple-darwin10");
319 break;
320 }
321 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000322
323 // Determine the arch specific GCC subdirectory.
324 const char *ArchSpecificDir = 0;
325 switch (getTriple().getArch()) {
326 default:
327 break;
328 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000329 case llvm::Triple::thumb: {
330 std::string Triple = ComputeLLVMTriple(Args);
331 llvm::StringRef TripleStr = Triple;
332 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
333 ArchSpecificDir = "v5";
334 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
335 ArchSpecificDir = "v6";
336 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
337 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000338 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000339 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000340 case llvm::Triple::ppc64:
341 ArchSpecificDir = "ppc64";
342 break;
343 case llvm::Triple::x86_64:
344 ArchSpecificDir = "x86_64";
345 break;
346 }
347
348 if (ArchSpecificDir) {
349 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000350 bool Exists;
351 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000352 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
353 P.eraseComponent();
354 }
355
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000356 bool Exists;
357 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000358 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000359}
360
John McCallf85e1932011-06-15 23:02:42 +0000361void DarwinClang::AddLinkARCArgs(const ArgList &Args,
362 ArgStringList &CmdArgs) const {
363
364 CmdArgs.push_back("-force_load");
365 llvm::sys::Path P(getDriver().ClangExecutable);
366 P.eraseComponent(); // 'clang'
367 P.eraseComponent(); // 'bin'
368 P.appendComponent("lib");
369 P.appendComponent("arc");
370 P.appendComponent("libarclite_");
371 std::string s = P.str();
372 // Mash in the platform.
373 if (isTargetIPhoneOS())
374 s += "iphoneos";
375 // FIXME: isTargetIphoneOSSimulator() is not returning true.
376 else if (ARCRuntimeForSimulator != ARCSimulator_None)
377 s += "iphonesimulator";
378 else
379 s += "macosx";
380 s += ".a";
381
382 CmdArgs.push_back(Args.MakeArgString(s));
383}
384
Eric Christopher3404fe72011-06-22 17:41:40 +0000385void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
386 ArgStringList &CmdArgs,
387 const char *DarwinStaticLib) const {
388 llvm::sys::Path P(getDriver().ResourceDir);
389 P.appendComponent("lib");
390 P.appendComponent("darwin");
391 P.appendComponent(DarwinStaticLib);
392
393 // For now, allow missing resource libraries to support developers who may
394 // not have compiler-rt checked out or integrated into their build.
395 bool Exists;
396 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
397 CmdArgs.push_back(Args.MakeArgString(P.str()));
398}
399
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000400void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
401 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000402 // Darwin doesn't support real static executables, don't link any runtime
403 // libraries with -static.
404 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000405 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000406
407 // Reject -static-libgcc for now, we can deal with this when and if someone
408 // cares. This is useful in situations where someone wants to statically link
409 // something like libstdc++, and needs its runtime support routines.
410 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000411 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000412 << A->getAsString(Args);
413 return;
414 }
415
Daniel Dunbareec99102010-01-22 03:38:14 +0000416 // Otherwise link libSystem, then the dynamic runtime library, and finally any
417 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000418 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000419
420 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000421 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000422 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
423 // it never went into the SDK.
424 if (!isTargetIOSSimulator())
425 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000426
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000427 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000428 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000429 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000430 // The dynamic runtime library was merged with libSystem for 10.6 and
431 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000432 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000433 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000434 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000435 CmdArgs.push_back("-lgcc_s.10.5");
436
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000437 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000438 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000439 // omitted from 10.4.dylib.
440 //
441 // Unfortunately, that turned out to not be true, because Darwin system
442 // headers can still use eprintf on i386, and it is not exported from
443 // libSystem. Therefore, we still must provide a runtime library just for
444 // the tiny tiny handful of projects that *might* use that symbol.
445 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000446 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000447 } else {
448 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000449 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
450 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000451 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000452 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000453}
454
John McCallf85e1932011-06-15 23:02:42 +0000455static inline llvm::StringRef SimulatorVersionDefineName() {
456 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
457}
458
459/// \brief Parse the simulator version define:
460/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
461// and return the grouped values as integers, e.g:
462// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
463// will return Major=4, Minor=2, Micro=1.
464static bool GetVersionFromSimulatorDefine(llvm::StringRef define,
465 unsigned &Major, unsigned &Minor,
466 unsigned &Micro) {
467 assert(define.startswith(SimulatorVersionDefineName()));
468 llvm::StringRef name, version;
469 llvm::tie(name, version) = define.split('=');
470 if (version.empty())
471 return false;
472 std::string verstr = version.str();
473 char *end;
474 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
475 if (*end != '\0')
476 return false;
477 Major = num / 10000;
478 num = num % 10000;
479 Minor = num / 100;
480 Micro = num % 100;
481 return true;
482}
483
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000484void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000485 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000486
Daniel Dunbar26031372010-01-27 00:56:25 +0000487 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000488 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
489 Arg *iOSSimVersion = Args.getLastArg(
490 options::OPT_mios_simulator_version_min_EQ);
John McCallf85e1932011-06-15 23:02:42 +0000491
492 // FIXME: HACK! When compiling for the simulator we don't get a
493 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
494 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
495 // define passed in command-line.
496 if (!iOSVersion) {
497 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
498 ie = Args.filtered_end(); it != ie; ++it) {
499 llvm::StringRef define = (*it)->getValue(Args);
500 if (define.startswith(SimulatorVersionDefineName())) {
501 unsigned Major, Minor, Micro;
502 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
503 Major < 10 && Minor < 100 && Micro < 100) {
504 ARCRuntimeForSimulator = Major < 5 ? ARCSimulator_NoARCRuntime
505 : ARCSimulator_HasARCRuntime;
506 }
507 break;
508 }
509 }
510 }
511
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000512 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000513 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000514 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000515 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
516 iOSVersion = iOSSimVersion = 0;
517 } else if (iOSVersion && iOSSimVersion) {
518 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
519 << iOSVersion->getAsString(Args)
520 << iOSSimVersion->getAsString(Args);
521 iOSSimVersion = 0;
522 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
523 // If not deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000524 // environment defines.
525 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000526 const char *iOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
527 const char *iOSSimTarget = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000528
Daniel Dunbar816bc312010-01-26 01:45:19 +0000529 // Ignore empty strings.
530 if (OSXTarget && OSXTarget[0] == '\0')
531 OSXTarget = 0;
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000532 if (iOSTarget && iOSTarget[0] == '\0')
533 iOSTarget = 0;
534 if (iOSSimTarget && iOSSimTarget[0] == '\0')
535 iOSSimTarget = 0;
Daniel Dunbar816bc312010-01-26 01:45:19 +0000536
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000537 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000538 //
539 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000540
541 // Do not allow conflicts with the iOS simulator target.
542 if (iOSSimTarget && (OSXTarget || iOSTarget)) {
543 getDriver().Diag(clang::diag::err_drv_conflicting_deployment_targets)
544 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
545 << (OSXTarget ? "MACOSX_DEPLOYMENT_TARGET" :
546 "IPHONEOS_DEPLOYMENT_TARGET");
547 }
548
549 // Allow conflicts among OSX and iOS for historical reasons, but choose the
550 // default platform.
551 if (OSXTarget && iOSTarget) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000552 if (getTriple().getArch() == llvm::Triple::arm ||
553 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000554 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000555 else
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000556 iOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000557 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000558
Daniel Dunbar39053672010-02-02 17:31:12 +0000559 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000560 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000561 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
562 Args.append(OSXVersion);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000563 } else if (iOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000564 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000565 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
566 Args.append(iOSVersion);
567 } else if (iOSSimTarget) {
568 const Option *O = Opts.getOption(
569 options::OPT_mios_simulator_version_min_EQ);
570 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
571 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000572 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000573 // Otherwise, assume we are targeting OS X.
574 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000575 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
576 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000577 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000580 // Reject invalid architecture combinations.
581 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
582 getTriple().getArch() != llvm::Triple::x86_64)) {
583 getDriver().Diag(clang::diag::err_drv_invalid_arch_for_deployment_target)
584 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
585 }
586
Daniel Dunbar26031372010-01-27 00:56:25 +0000587 // Set the tool chain target information.
588 unsigned Major, Minor, Micro;
589 bool HadExtra;
590 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000591 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar26031372010-01-27 00:56:25 +0000592 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
593 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000594 Major != 10 || Minor >= 100 || Micro >= 100)
Daniel Dunbar26031372010-01-27 00:56:25 +0000595 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
596 << OSXVersion->getAsString(Args);
597 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000598 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
599 assert(Version && "Unknown target platform!");
600 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000601 Micro, HadExtra) || HadExtra ||
602 Major >= 10 || Minor >= 100 || Micro >= 100)
603 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000604 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000605 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000606
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000607 bool IsIOSSim = bool(iOSSimVersion);
608
609 // In GCC, the simulator historically was treated as being OS X in some
610 // contexts, like determining the link logic, despite generally being called
611 // with an iOS deployment target. For compatibility, we detect the
612 // simulator as iOS + x86, and treat it differently in a few contexts.
613 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
614 getTriple().getArch() == llvm::Triple::x86_64))
615 IsIOSSim = true;
616
617 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000618}
619
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000620void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000621 ArgStringList &CmdArgs) const {
622 CXXStdlibType Type = GetCXXStdlibType(Args);
623
624 switch (Type) {
625 case ToolChain::CST_Libcxx:
626 CmdArgs.push_back("-lc++");
627 break;
628
629 case ToolChain::CST_Libstdcxx: {
630 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
631 // it was previously found in the gcc lib dir. However, for all the Darwin
632 // platforms we care about it was -lstdc++.6, so we search for that
633 // explicitly if we can't see an obvious -lstdc++ candidate.
634
635 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000636 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000637 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
638 llvm::sys::Path P(A->getValue(Args));
639 P.appendComponent("usr");
640 P.appendComponent("lib");
641 P.appendComponent("libstdc++.dylib");
642
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000643 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000644 P.eraseComponent();
645 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000646 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000647 CmdArgs.push_back(Args.MakeArgString(P.str()));
648 return;
649 }
650 }
651 }
652
653 // Otherwise, look in the root.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000654 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
655 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000656 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
657 return;
658 }
659
660 // Otherwise, let the linker search.
661 CmdArgs.push_back("-lstdc++");
662 break;
663 }
664 }
665}
666
Shantonu Sen7433fed2010-09-17 18:39:08 +0000667void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
668 ArgStringList &CmdArgs) const {
669
670 // For Darwin platforms, use the compiler-rt-based support library
671 // instead of the gcc-provided one (which is also incidentally
672 // only present in the gcc lib dir, which makes it hard to find).
673
674 llvm::sys::Path P(getDriver().ResourceDir);
675 P.appendComponent("lib");
676 P.appendComponent("darwin");
677 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000678
Shantonu Sen7433fed2010-09-17 18:39:08 +0000679 // For now, allow missing resource libraries to support developers who may
680 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000681 bool Exists;
682 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000683 CmdArgs.push_back(Args.MakeArgString(P.str()));
684}
685
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000686DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
687 const char *BoundArch) const {
688 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
689 const OptTable &Opts = getDriver().getOpts();
690
691 // FIXME: We really want to get out of the tool chain level argument
692 // translation business, as it makes the driver functionality much
693 // more opaque. For now, we follow gcc closely solely for the
694 // purpose of easily achieving feature parity & testability. Once we
695 // have something that works, we should reevaluate each translation
696 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000697
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000698 for (ArgList::const_iterator it = Args.begin(),
699 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000700 Arg *A = *it;
701
702 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000703 // Skip this argument unless the architecture matches either the toolchain
704 // triple arch, or the arch being bound.
705 //
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000706 // FIXME: Canonicalize name.
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000707 llvm::StringRef XarchArch = A->getValue(Args, 0);
708 if (!(XarchArch == getArchName() ||
709 (BoundArch && XarchArch == BoundArch)))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000710 continue;
711
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000712 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000713 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
714 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000715 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000717 // If the argument parsing failed or more than one argument was
718 // consumed, the -Xarch_ argument's parameter tried to consume
719 // extra arguments. Emit an error and ignore.
720 //
721 // We also want to disallow any options which would alter the
722 // driver behavior; that isn't going to work in our model. We
723 // use isDriverOption() as an approximation, although things
724 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000725 if (!XarchArg || Index > Prev + 1) {
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000726 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_with_args)
727 << A->getAsString(Args);
728 continue;
729 } else if (XarchArg->getOption().isDriverOption()) {
730 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000731 << A->getAsString(Args);
732 continue;
733 }
734
Daniel Dunbar478edc22009-03-29 22:29:05 +0000735 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000736 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000737
738 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000739
740 // Linker input arguments require custom handling. The problem is that we
741 // have already constructed the phase actions, so we can not treat them as
742 // "input arguments".
743 if (A->getOption().isLinkerInput()) {
744 // Convert the argument into individual Zlinker_input_args.
745 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
746 DAL->AddSeparateArg(OriginalArg,
747 Opts.getOption(options::OPT_Zlinker_input),
748 A->getValue(Args, i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000749
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000750 }
751 continue;
752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000754
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000755 // Sob. These is strictly gcc compatible for the time being. Apple
756 // gcc translates options twice, which means that self-expanding
757 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000758 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000759 default:
760 DAL->append(A);
761 break;
762
763 case options::OPT_mkernel:
764 case options::OPT_fapple_kext:
765 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000766 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
767 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000768 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000770 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000771 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
772 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000773 break;
774
775 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000776 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
777 DAL->AddFlagArg(A,
778 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000779 break;
780
781 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000782 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
783 DAL->AddFlagArg(A,
784 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000785 break;
786
787 case options::OPT_fterminated_vtables:
788 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000789 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
790 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000791 break;
792
793 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000794 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000795 break;
796
797 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000798 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000799 break;
800
801 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000802 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000803 break;
804
805 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000806 DAL->AddFlagArg(A,
807 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000808 break;
809
810 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000811 DAL->AddFlagArg(A,
812 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000813 break;
814
815 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000816 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000817 break;
818
819 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000820 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000821 break;
822 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000823 }
824
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000825 if (getTriple().getArch() == llvm::Triple::x86 ||
826 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000827 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000828 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000829
830 // Add the arch options based on the particular spelling of -arch, to match
831 // how the driver driver works.
832 if (BoundArch) {
833 llvm::StringRef Name = BoundArch;
834 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
835 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
836
837 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
838 // which defines the list of which architectures we accept.
839 if (Name == "ppc")
840 ;
841 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000842 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000843 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000844 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000845 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000846 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000847 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000848 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000849 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000850 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000851 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000852 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000853 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000854 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000855 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000856 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000857
858 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000859 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000860
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000861 else if (Name == "i386")
862 ;
863 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000864 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000865 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000866 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000867 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000868 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000869 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000870 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000871 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000872 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000873 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000874 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000875 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000876 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000877
878 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000879 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000880
881 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000882 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000883 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000884 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000885 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000886 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000887 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000888 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000889 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000890 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000891 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000892 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000893
894 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000895 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000896 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000897
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000898 // Add an explicit version min argument for the deployment target. We do this
899 // after argument translation because -Xarch_ arguments may add a version min
900 // argument.
901 AddDeploymentTarget(*DAL);
902
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000903 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000904}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000905
Daniel Dunbarf3955282009-09-04 18:34:51 +0000906bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000907 // FIXME: Gross; we should probably have some separate target
908 // definition, possibly even reusing the one in clang.
909 return getArchName() == "x86_64";
910}
911
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000912bool Darwin::UseDwarfDebugFlags() const {
913 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
914 return S[0] != '\0';
915 return false;
916}
917
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000918bool Darwin::UseSjLjExceptions() const {
919 // Darwin uses SjLj exceptions on ARM.
920 return (getTriple().getArch() == llvm::Triple::arm ||
921 getTriple().getArch() == llvm::Triple::thumb);
922}
923
Daniel Dunbarf3955282009-09-04 18:34:51 +0000924const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000925 return "pic";
926}
927
Daniel Dunbarf3955282009-09-04 18:34:51 +0000928const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000929 if (getArchName() == "x86_64")
930 return "pic";
931 return 0;
932}
933
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000934bool Darwin::SupportsProfiling() const {
935 // Profiling instrumentation is only supported on x86.
936 return getArchName() == "i386" || getArchName() == "x86_64";
937}
938
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000939bool Darwin::SupportsObjCGC() const {
940 // Garbage collection is supported everywhere except on iPhone OS.
941 return !isTargetIPhoneOS();
942}
943
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000944std::string
945Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
946 return ComputeLLVMTriple(Args);
947}
948
Daniel Dunbar39176082009-03-20 00:20:03 +0000949/// Generic_GCC - A tool chain using the 'gcc' command to perform
950/// all subcommands; this relies on gcc translating the majority of
951/// command line options.
952
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000953Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000954 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000955 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +0000956 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000957 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000958}
959
Daniel Dunbar39176082009-03-20 00:20:03 +0000960Generic_GCC::~Generic_GCC() {
961 // Free tool implementations.
962 for (llvm::DenseMap<unsigned, Tool*>::iterator
963 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
964 delete it->second;
965}
966
Mike Stump1eb44332009-09-09 15:08:12 +0000967Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000968 const JobAction &JA,
969 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +0000970 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000971 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000972 Key = Action::AnalyzeJobClass;
973 else
974 Key = JA.getKind();
975
976 Tool *&T = Tools[Key];
977 if (!T) {
978 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000979 case Action::InputClass:
980 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000981 assert(0 && "Invalid tool kind.");
982 case Action::PreprocessJobClass:
983 T = new tools::gcc::Preprocess(*this); break;
984 case Action::PrecompileJobClass:
985 T = new tools::gcc::Precompile(*this); break;
986 case Action::AnalyzeJobClass:
987 T = new tools::Clang(*this); break;
988 case Action::CompileJobClass:
989 T = new tools::gcc::Compile(*this); break;
990 case Action::AssembleJobClass:
991 T = new tools::gcc::Assemble(*this); break;
992 case Action::LinkJobClass:
993 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000995 // This is a bit ungeneric, but the only platform using a driver
996 // driver is Darwin.
997 case Action::LipoJobClass:
998 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000999 case Action::DsymutilJobClass:
1000 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001001 }
1002 }
1003
1004 return *T;
1005}
1006
Daniel Dunbar39176082009-03-20 00:20:03 +00001007bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001008 // FIXME: Gross; we should probably have some separate target
1009 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +00001010 return getArchName() == "x86_64";
1011}
1012
1013const char *Generic_GCC::GetDefaultRelocationModel() const {
1014 return "static";
1015}
1016
1017const char *Generic_GCC::GetForcedPicModel() const {
1018 return 0;
1019}
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001020
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001021/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1022/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1023/// Currently does not support anything else but compilation.
1024
1025TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
1026 : ToolChain(Host, Triple) {
1027 // Path mangling to find libexec
1028 std::string Path(getDriver().Dir);
1029
1030 Path += "/../libexec";
1031 getProgramPaths().push_back(Path);
1032}
1033
1034TCEToolChain::~TCEToolChain() {
1035 for (llvm::DenseMap<unsigned, Tool*>::iterator
1036 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1037 delete it->second;
1038}
1039
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001040bool TCEToolChain::IsMathErrnoDefault() const {
1041 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001042}
1043
1044bool TCEToolChain::IsUnwindTablesDefault() const {
1045 return false;
1046}
1047
1048const char *TCEToolChain::GetDefaultRelocationModel() const {
1049 return "static";
1050}
1051
1052const char *TCEToolChain::GetForcedPicModel() const {
1053 return 0;
1054}
1055
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001056Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001057 const JobAction &JA,
1058 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001059 Action::ActionClass Key;
1060 Key = Action::AnalyzeJobClass;
1061
1062 Tool *&T = Tools[Key];
1063 if (!T) {
1064 switch (Key) {
1065 case Action::PreprocessJobClass:
1066 T = new tools::gcc::Preprocess(*this); break;
1067 case Action::AnalyzeJobClass:
1068 T = new tools::Clang(*this); break;
1069 default:
1070 assert(false && "Unsupported action for TCE target.");
1071 }
1072 }
1073 return *T;
1074}
1075
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001076/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1077
1078OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001079 : Generic_ELF(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001080 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001081 getFilePaths().push_back("/usr/lib");
1082}
1083
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001084Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1085 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001086 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001087 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001088 Key = Action::AnalyzeJobClass;
1089 else
1090 Key = JA.getKind();
1091
Rafael Espindoladda5b922010-11-07 23:13:01 +00001092 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1093 options::OPT_no_integrated_as,
1094 IsIntegratedAssemblerDefault());
1095
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001096 Tool *&T = Tools[Key];
1097 if (!T) {
1098 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001099 case Action::AssembleJobClass: {
1100 if (UseIntegratedAs)
1101 T = new tools::ClangAs(*this);
1102 else
1103 T = new tools::openbsd::Assemble(*this);
1104 break;
1105 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001106 case Action::LinkJobClass:
1107 T = new tools::openbsd::Link(*this); break;
1108 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001109 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001110 }
1111 }
1112
1113 return *T;
1114}
1115
Daniel Dunbar75358d22009-03-30 21:06:03 +00001116/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1117
Daniel Dunbar214afe92010-08-02 05:43:59 +00001118FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001119 : Generic_ELF(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001120
1121 // Determine if we are compiling 32-bit code on an x86_64 platform.
1122 bool Lib32 = false;
1123 if (Triple.getArch() == llvm::Triple::x86 &&
1124 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1125 llvm::Triple::x86_64)
1126 Lib32 = true;
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001127
Roman Divacky3393cef2011-06-04 07:37:31 +00001128 if (Triple.getArch() == llvm::Triple::ppc &&
1129 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1130 llvm::Triple::ppc64)
1131 Lib32 = true;
1132
Daniel Dunbarbc534662009-04-02 18:30:04 +00001133 if (Lib32) {
Daniel Dunbarbc534662009-04-02 18:30:04 +00001134 getFilePaths().push_back("/usr/lib32");
1135 } else {
Daniel Dunbarbc534662009-04-02 18:30:04 +00001136 getFilePaths().push_back("/usr/lib");
1137 }
Daniel Dunbar75358d22009-03-30 21:06:03 +00001138}
1139
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001140Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1141 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001142 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001143 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001144 Key = Action::AnalyzeJobClass;
1145 else
1146 Key = JA.getKind();
1147
Roman Divacky67dece72010-11-08 17:46:39 +00001148 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1149 options::OPT_no_integrated_as,
1150 IsIntegratedAssemblerDefault());
1151
Daniel Dunbar75358d22009-03-30 21:06:03 +00001152 Tool *&T = Tools[Key];
1153 if (!T) {
1154 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001155 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001156 if (UseIntegratedAs)
1157 T = new tools::ClangAs(*this);
1158 else
1159 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001160 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001161 case Action::LinkJobClass:
1162 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001163 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001164 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001165 }
1166 }
1167
1168 return *T;
1169}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001170
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001171/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1172
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001173NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
1174 const llvm::Triple& ToolTriple)
1175 : Generic_ELF(Host, Triple), ToolTriple(ToolTriple) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001176
1177 // Determine if we are compiling 32-bit code on an x86_64 platform.
1178 bool Lib32 = false;
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001179 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
1180 Triple.getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001181 Lib32 = true;
1182
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001183 if (getDriver().UseStdLib) {
1184 if (Lib32)
1185 getFilePaths().push_back("=/usr/lib/i386");
1186 else
1187 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001188 }
1189}
1190
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001191Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1192 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001193 Action::ActionClass Key;
1194 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1195 Key = Action::AnalyzeJobClass;
1196 else
1197 Key = JA.getKind();
1198
1199 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1200 options::OPT_no_integrated_as,
1201 IsIntegratedAssemblerDefault());
1202
1203 Tool *&T = Tools[Key];
1204 if (!T) {
1205 switch (Key) {
1206 case Action::AssembleJobClass:
1207 if (UseIntegratedAs)
1208 T = new tools::ClangAs(*this);
1209 else
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001210 T = new tools::netbsd::Assemble(*this, ToolTriple);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001211 break;
1212 case Action::LinkJobClass:
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001213 T = new tools::netbsd::Link(*this, ToolTriple);
1214 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001215 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001216 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001217 }
1218 }
1219
1220 return *T;
1221}
1222
Chris Lattner38e317d2010-07-07 16:01:42 +00001223/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1224
1225Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1226 : Generic_GCC(Host, Triple) {
1227 getFilePaths().push_back(getDriver().Dir + "/../lib");
1228 getFilePaths().push_back("/usr/lib");
1229 getFilePaths().push_back("/usr/gnu/lib");
1230 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1231}
1232
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001233Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1234 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001235 Action::ActionClass Key;
1236 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1237 Key = Action::AnalyzeJobClass;
1238 else
1239 Key = JA.getKind();
1240
1241 Tool *&T = Tools[Key];
1242 if (!T) {
1243 switch (Key) {
1244 case Action::AssembleJobClass:
1245 T = new tools::minix::Assemble(*this); break;
1246 case Action::LinkJobClass:
1247 T = new tools::minix::Link(*this); break;
1248 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001249 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001250 }
1251 }
1252
1253 return *T;
1254}
1255
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001256/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1257
1258AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1259 : Generic_GCC(Host, Triple) {
1260
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001261 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001262 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001263 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001264
Daniel Dunbaree788e72009-12-21 18:54:17 +00001265 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001266 getFilePaths().push_back("/usr/lib");
1267 getFilePaths().push_back("/usr/sfw/lib");
1268 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001269 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001270
1271}
1272
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001273Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1274 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001275 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001276 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001277 Key = Action::AnalyzeJobClass;
1278 else
1279 Key = JA.getKind();
1280
1281 Tool *&T = Tools[Key];
1282 if (!T) {
1283 switch (Key) {
1284 case Action::AssembleJobClass:
1285 T = new tools::auroraux::Assemble(*this); break;
1286 case Action::LinkJobClass:
1287 T = new tools::auroraux::Link(*this); break;
1288 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001289 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001290 }
1291 }
1292
1293 return *T;
1294}
1295
1296
Eli Friedman6b3454a2009-05-26 07:52:18 +00001297/// Linux toolchain (very bare-bones at the moment).
1298
Rafael Espindolac1da9812010-11-07 20:14:31 +00001299enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001300 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001301 DebianLenny,
1302 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001303 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001304 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001305 RHEL4,
1306 RHEL5,
1307 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001308 Fedora13,
1309 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001310 Fedora15,
1311 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001312 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001313 OpenSuse11_4,
1314 OpenSuse12_1,
Douglas Gregor814638e2011-03-14 15:39:50 +00001315 UbuntuHardy,
1316 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001317 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001318 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001319 UbuntuLucid,
1320 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001321 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001322 UbuntuOneiric,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001323 UnknownDistro
1324};
1325
Chris Lattnerd753b562011-05-22 05:36:06 +00001326static bool IsRedhat(enum LinuxDistro Distro) {
Eric Christopher8f1cc072011-04-06 18:22:53 +00001327 return Distro == Fedora13 || Distro == Fedora14 ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001328 Distro == Fedora15 || Distro == FedoraRawhide ||
1329 Distro == RHEL4 || Distro == RHEL5 || Distro == RHEL6;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001330}
1331
1332static bool IsOpenSuse(enum LinuxDistro Distro) {
David Chisnallde5c0482011-05-19 13:26:33 +00001333 return Distro == OpenSuse11_3 || Distro == OpenSuse11_4 ||
1334 Distro == OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001335}
1336
1337static bool IsDebian(enum LinuxDistro Distro) {
Eli Friedman0b200f62011-06-02 21:36:53 +00001338 return Distro == DebianLenny || Distro == DebianSqueeze ||
1339 Distro == DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001340}
1341
1342static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001343 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001344 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Ted Kremenek43ac2972011-04-05 22:04:27 +00001345 Distro == UbuntuJaunty || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001346 Distro == UbuntuNatty || Distro == UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001347}
1348
1349static bool IsDebianBased(enum LinuxDistro Distro) {
1350 return IsDebian(Distro) || IsUbuntu(Distro);
1351}
1352
1353static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001354 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001355 bool Exists;
1356 if (Distro == Exherbo &&
1357 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001358 return false;
1359
Rafael Espindolac1da9812010-11-07 20:14:31 +00001360 return true;
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001361 }
Ted Kremenek43ac2972011-04-05 22:04:27 +00001362 if (Arch == llvm::Triple::ppc64)
1363 return true;
Eric Christopher9af535a2011-06-03 13:28:31 +00001364 if ((Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) &&
1365 IsDebianBased(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001366 return true;
1367 return false;
1368}
1369
1370static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001371 llvm::OwningPtr<llvm::MemoryBuffer> File;
1372 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
1373 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001374 llvm::SmallVector<llvm::StringRef, 8> Lines;
1375 Data.split(Lines, "\n");
1376 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001377 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1378 return UbuntuHardy;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001379 else if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1380 return UbuntuIntrepid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001381 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001382 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001383 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1384 return UbuntuKarmic;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001385 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1386 return UbuntuLucid;
1387 else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1388 return UbuntuMaverick;
1389 else if (Lines[i] == "DISTRIB_CODENAME=natty")
1390 return UbuntuNatty;
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001391 else if (Lines[i] == "DISTRIB_CODENAME=oneiric")
1392 return UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001393 }
1394 return UnknownDistro;
1395 }
1396
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001397 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1398 llvm::StringRef Data = File.get()->getBuffer();
Eric Christopher8f1cc072011-04-06 18:22:53 +00001399 if (Data.startswith("Fedora release 15"))
1400 return Fedora15;
1401 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001402 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001403 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001404 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001405 else if (Data.startswith("Fedora release") &&
1406 Data.find("Rawhide") != llvm::StringRef::npos)
1407 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001408 else if (Data.startswith("Red Hat Enterprise Linux") &&
1409 Data.find("release 6") != llvm::StringRef::npos)
1410 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001411 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1412 Data.startswith("CentOS")) &&
Chris Lattnerd753b562011-05-22 05:36:06 +00001413 Data.find("release 5") != llvm::StringRef::npos)
1414 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001415 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1416 Data.startswith("CentOS")) &&
Chris Lattnerd753b562011-05-22 05:36:06 +00001417 Data.find("release 4") != llvm::StringRef::npos)
1418 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001419 return UnknownDistro;
1420 }
1421
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001422 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1423 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001424 if (Data[0] == '5')
1425 return DebianLenny;
1426 else if (Data.startswith("squeeze/sid"))
1427 return DebianSqueeze;
Eli Friedman0b200f62011-06-02 21:36:53 +00001428 else if (Data.startswith("wheezy/sid"))
1429 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001430 return UnknownDistro;
1431 }
1432
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001433 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1434 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001435 if (Data.startswith("openSUSE 11.3"))
1436 return OpenSuse11_3;
David Chisnallde5c0482011-05-19 13:26:33 +00001437 else if (Data.startswith("openSUSE 11.4"))
1438 return OpenSuse11_4;
1439 else if (Data.startswith("openSUSE 12.1"))
1440 return OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001441 return UnknownDistro;
1442 }
1443
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001444 bool Exists;
1445 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001446 return Exherbo;
1447
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001448 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1449 return ArchLinux;
1450
Rafael Espindolac1da9812010-11-07 20:14:31 +00001451 return UnknownDistro;
1452}
1453
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001454static std::string findGCCBaseLibDir(const std::string &GccTriple) {
Chandler Carruth169dd312011-06-08 10:14:38 +00001455 // FIXME: Using CXX_INCLUDE_ROOT is here is a bit of a hack, but
1456 // avoids adding yet another option to configure/cmake.
1457 // It would probably be cleaner to break it in two variables
1458 // CXX_GCC_ROOT with just /foo/bar
1459 // CXX_GCC_VER with 4.5.2
1460 // Then we would have
1461 // CXX_INCLUDE_ROOT = CXX_GCC_ROOT/include/c++/CXX_GCC_VER
1462 // and this function would return
1463 // CXX_GCC_ROOT/lib/gcc/CXX_INCLUDE_ARCH/CXX_GCC_VER
1464 llvm::SmallString<128> CxxIncludeRoot(CXX_INCLUDE_ROOT);
1465 if (CxxIncludeRoot != "") {
1466 // This is of the form /foo/bar/include/c++/4.5.2/
1467 if (CxxIncludeRoot.back() == '/')
1468 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the /
1469 llvm::StringRef Version = llvm::sys::path::filename(CxxIncludeRoot);
1470 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the version
1471 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the c++
1472 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the include
1473 std::string ret(CxxIncludeRoot.c_str());
1474 ret.append("/lib/gcc/");
1475 ret.append(CXX_INCLUDE_ARCH);
1476 ret.append("/");
1477 ret.append(Version);
1478 return ret;
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001479 }
Chandler Carruth37187cc2011-07-02 00:51:03 +00001480 static const char* GccVersions[] = {"4.6.1", "4.6.0", "4.6",
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001481 "4.5.2", "4.5.1", "4.5",
1482 "4.4.5", "4.4.4", "4.4.3", "4.4",
1483 "4.3.4", "4.3.3", "4.3.2", "4.3",
1484 "4.2.4", "4.2.3", "4.2.2", "4.2.1",
1485 "4.2", "4.1.1"};
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001486 bool Exists;
1487 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1488 std::string Suffix = GccTriple + "/" + GccVersions[i];
1489 std::string t1 = "/usr/lib/gcc/" + Suffix;
1490 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists)
1491 return t1;
1492 std::string t2 = "/usr/lib64/gcc/" + Suffix;
1493 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists)
1494 return t2;
1495 std::string t3 = "/usr/lib/" + GccTriple + "/gcc/" + Suffix;
1496 if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists)
1497 return t3;
1498 }
1499 return "";
1500}
1501
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001502Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001503 : Generic_ELF(Host, Triple) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001504 llvm::Triple::ArchType Arch =
1505 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Daniel Dunbara9822de2009-08-06 01:47:11 +00001506
Rafael Espindolac1da9812010-11-07 20:14:31 +00001507 std::string Suffix32 = "";
1508 if (Arch == llvm::Triple::x86_64)
1509 Suffix32 = "/32";
Daniel Dunbara9822de2009-08-06 01:47:11 +00001510
Rafael Espindolac1da9812010-11-07 20:14:31 +00001511 std::string Suffix64 = "";
Ted Kremenek43ac2972011-04-05 22:04:27 +00001512 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001513 Suffix64 = "/64";
1514
1515 std::string Lib32 = "lib";
1516
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001517 bool Exists;
1518 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001519 Lib32 = "lib32";
1520
1521 std::string Lib64 = "lib";
Michael J. Spencer2dea7c72011-01-12 23:54:48 +00001522 bool Symlink;
Chris Lattner48aef362011-01-13 01:35:58 +00001523 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1524 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001525 Lib64 = "lib64";
1526
1527 std::string GccTriple = "";
Douglas Gregorf0594d82011-03-06 19:11:49 +00001528 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001529 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1530 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001531 GccTriple = "arm-linux-gnueabi";
1532 } else if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001533 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1534 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001535 GccTriple = "x86_64-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001536 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1537 Exists) && Exists)
Rafael Espindola53dd00b2010-11-17 00:25:26 +00001538 GccTriple = "x86_64-unknown-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001539 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1540 Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001541 GccTriple = "x86_64-pc-linux-gnu";
NAKAMURA Takumic3703982011-06-16 12:43:57 +00001542 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux6E",
1543 Exists) && Exists)
1544 GccTriple = "x86_64-redhat-linux6E";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001545 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1546 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001547 GccTriple = "x86_64-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001548 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1549 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001550 GccTriple = "x86_64-suse-linux";
Nick Lewyckye10f9002011-02-01 23:03:29 +00001551 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1552 Exists) && Exists)
1553 GccTriple = "x86_64-manbo-linux-gnu";
Nico Weber80585d82011-04-25 03:17:35 +00001554 else if (!llvm::sys::fs::exists("/usr/lib/x86_64-linux-gnu/gcc",
1555 Exists) && Exists)
1556 GccTriple = "x86_64-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001557 } else if (Arch == llvm::Triple::x86) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001558 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001559 GccTriple = "i686-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001560 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1561 Exists)
Nuno Lopes2a69ddd2010-11-19 17:26:57 +00001562 GccTriple = "i686-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001563 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1564 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001565 GccTriple = "i486-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001566 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1567 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001568 GccTriple = "i686-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001569 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1570 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001571 GccTriple = "i586-suse-linux";
Ted Kremenekd130c7d2011-04-18 17:50:19 +00001572 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-slackware-linux", Exists)
1573 && Exists)
1574 GccTriple = "i486-slackware-linux";
Ted Kremenek43ac2972011-04-05 22:04:27 +00001575 } else if (Arch == llvm::Triple::ppc) {
1576 if (!llvm::sys::fs::exists("/usr/lib/powerpc-linux-gnu", Exists) && Exists)
1577 GccTriple = "powerpc-linux-gnu";
Eric Christopher9af535a2011-06-03 13:28:31 +00001578 else if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc-unknown-linux-gnu",
1579 Exists) && Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001580 GccTriple = "powerpc-unknown-linux-gnu";
1581 } else if (Arch == llvm::Triple::ppc64) {
Eric Christopher9af535a2011-06-03 13:28:31 +00001582 if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc64-unknown-linux-gnu",
1583 Exists) && Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001584 GccTriple = "powerpc64-unknown-linux-gnu";
Eric Christopher9af535a2011-06-03 13:28:31 +00001585 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/"
1586 "powerpc64-unknown-linux-gnu", Exists) &&
1587 Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001588 GccTriple = "powerpc64-unknown-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001589 }
1590
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001591 std::string Base = findGCCBaseLibDir(GccTriple);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001592 path_list &Paths = getFilePaths();
Eric Christopher9af535a2011-06-03 13:28:31 +00001593 bool Is32Bits = (getArch() == llvm::Triple::x86 ||
1594 getArch() == llvm::Triple::ppc);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001595
1596 std::string Suffix;
1597 std::string Lib;
1598
1599 if (Is32Bits) {
1600 Suffix = Suffix32;
1601 Lib = Lib32;
1602 } else {
1603 Suffix = Suffix64;
1604 Lib = Lib64;
1605 }
1606
1607 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001608 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001609 Linker = LinkerPath.str();
1610 else
1611 Linker = GetProgramPath("ld");
1612
1613 LinuxDistro Distro = DetectLinuxDistro(Arch);
1614
Chris Lattner64a89172011-05-22 16:45:07 +00001615 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00001616 ExtraOpts.push_back("-z");
1617 ExtraOpts.push_back("relro");
1618 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001619
Douglas Gregorf0594d82011-03-06 19:11:49 +00001620 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001621 ExtraOpts.push_back("-X");
1622
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001623 if (IsRedhat(Distro) || IsOpenSuse(Distro) || Distro == UbuntuMaverick ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001624 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001625 ExtraOpts.push_back("--hash-style=gnu");
1626
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001627 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
Chris Lattner64a89172011-05-22 16:45:07 +00001628 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001629 ExtraOpts.push_back("--hash-style=both");
1630
Chris Lattnerd753b562011-05-22 05:36:06 +00001631 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001632 ExtraOpts.push_back("--no-add-needed");
1633
Eli Friedman0b200f62011-06-02 21:36:53 +00001634 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001635 IsOpenSuse(Distro) ||
1636 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
1637 Distro == UbuntuLucid ||
Eli Friedman0b200f62011-06-02 21:36:53 +00001638 Distro == UbuntuMaverick || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001639 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001640 ExtraOpts.push_back("--build-id");
1641
Chris Lattner64a89172011-05-22 16:45:07 +00001642 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00001643 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00001644
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001645 if (Distro == ArchLinux)
1646 Lib = "lib";
1647
Rafael Espindolac1da9812010-11-07 20:14:31 +00001648 Paths.push_back(Base + Suffix);
1649 if (HasMultilib(Arch, Distro)) {
1650 if (IsOpenSuse(Distro) && Is32Bits)
1651 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1652 Paths.push_back(Base + "/../../../../" + Lib);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001653 }
Rafael Espindolac7409a02011-06-03 15:39:42 +00001654
1655 // FIXME: This is in here to find crt1.o. It is provided by libc, and
1656 // libc (like gcc), can be installed in any directory. Once we are
1657 // fetching this from a config file, we should have a libc prefix.
1658 Paths.push_back("/lib/../" + Lib);
1659 Paths.push_back("/usr/lib/../" + Lib);
1660
Rafael Espindolac1da9812010-11-07 20:14:31 +00001661 if (!Suffix.empty())
1662 Paths.push_back(Base);
1663 if (IsOpenSuse(Distro))
1664 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1665 Paths.push_back(Base + "/../../..");
1666 if (Arch == getArch() && IsUbuntu(Distro))
1667 Paths.push_back("/usr/lib/" + GccTriple);
1668}
1669
1670bool Linux::HasNativeLLVMSupport() const {
1671 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00001672}
1673
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001674Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
1675 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001676 Action::ActionClass Key;
1677 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1678 Key = Action::AnalyzeJobClass;
1679 else
1680 Key = JA.getKind();
1681
Rafael Espindoladda5b922010-11-07 23:13:01 +00001682 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1683 options::OPT_no_integrated_as,
1684 IsIntegratedAssemblerDefault());
1685
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001686 Tool *&T = Tools[Key];
1687 if (!T) {
1688 switch (Key) {
1689 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00001690 if (UseIntegratedAs)
1691 T = new tools::ClangAs(*this);
1692 else
1693 T = new tools::linuxtools::Assemble(*this);
1694 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001695 case Action::LinkJobClass:
1696 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001697 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001698 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001699 }
1700 }
1701
1702 return *T;
1703}
1704
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001705/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1706
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001707DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001708 : Generic_ELF(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001709
1710 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001711 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001712 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001713 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001714
Daniel Dunbaree788e72009-12-21 18:54:17 +00001715 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001716 getFilePaths().push_back("/usr/lib");
1717 getFilePaths().push_back("/usr/lib/gcc41");
1718}
1719
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001720Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
1721 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001722 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001723 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001724 Key = Action::AnalyzeJobClass;
1725 else
1726 Key = JA.getKind();
1727
1728 Tool *&T = Tools[Key];
1729 if (!T) {
1730 switch (Key) {
1731 case Action::AssembleJobClass:
1732 T = new tools::dragonfly::Assemble(*this); break;
1733 case Action::LinkJobClass:
1734 T = new tools::dragonfly::Link(*this); break;
1735 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001736 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001737 }
1738 }
1739
1740 return *T;
1741}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001742
1743Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1744 : ToolChain(Host, Triple) {
1745}
1746
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001747Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA,
1748 const ActionList &Inputs) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00001749 Action::ActionClass Key;
1750 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1751 Key = Action::AnalyzeJobClass;
1752 else
1753 Key = JA.getKind();
1754
1755 Tool *&T = Tools[Key];
1756 if (!T) {
1757 switch (Key) {
1758 case Action::InputClass:
1759 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001760 case Action::LipoJobClass:
1761 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001762 assert(0 && "Invalid tool kind.");
1763 case Action::PreprocessJobClass:
1764 case Action::PrecompileJobClass:
1765 case Action::AnalyzeJobClass:
1766 case Action::CompileJobClass:
1767 T = new tools::Clang(*this); break;
1768 case Action::AssembleJobClass:
1769 T = new tools::ClangAs(*this); break;
1770 case Action::LinkJobClass:
1771 T = new tools::visualstudio::Link(*this); break;
1772 }
1773 }
1774
1775 return *T;
1776}
1777
1778bool Windows::IsIntegratedAssemblerDefault() const {
1779 return true;
1780}
1781
1782bool Windows::IsUnwindTablesDefault() const {
1783 // FIXME: Gross; we should probably have some separate target
1784 // definition, possibly even reusing the one in clang.
1785 return getArchName() == "x86_64";
1786}
1787
1788const char *Windows::GetDefaultRelocationModel() const {
1789 return "static";
1790}
1791
1792const char *Windows::GetForcedPicModel() const {
1793 if (getArchName() == "x86_64")
1794 return "pic";
1795 return 0;
1796}