blob: 46fde1e0540cd173eb8eb7e9cc73336b40c00e0f [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;
Chris Lattner5f9e2722011-07-23 10:55:15 +000044using namespace clang;
Daniel Dunbar39176082009-03-20 00:20:03 +000045
Daniel Dunbarf3955282009-09-04 18:34:51 +000046/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000047
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000048Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
John McCallf85e1932011-06-15 23:02:42 +000049 : ToolChain(Host, Triple), TargetInitialized(false),
50 ARCRuntimeForSimulator(ARCSimulator_None)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000051{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000052 // Compute the initial Darwin version based on the host.
53 bool HadExtra;
54 std::string OSName = Triple.getOSName();
Daniel Dunbar34f9e292011-02-25 21:20:15 +000055 if (!Driver::GetReleaseVersion(&OSName.c_str()[6],
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000056 DarwinVersion[0], DarwinVersion[1],
57 DarwinVersion[2], HadExtra))
Chris Lattner5f9e2722011-07-23 10:55:15 +000058 getDriver().Diag(diag::err_drv_invalid_darwin_version) << OSName;
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000059
Daniel Dunbar02633b52009-03-26 16:23:12 +000060 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000061 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
62 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000063}
64
Daniel Dunbar41800112010-08-02 05:43:56 +000065types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
66 types::ID Ty = types::lookupTypeForExtension(Ext);
67
68 // Darwin always preprocesses assembly files (unless -x is used explicitly).
69 if (Ty == types::TY_PP_Asm)
70 return types::TY_Asm;
71
72 return Ty;
73}
74
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000075bool Darwin::HasNativeLLVMSupport() const {
76 return true;
77}
78
John McCall9f084a32011-07-06 00:26:06 +000079bool Darwin::hasARCRuntime() const {
John McCallf85e1932011-06-15 23:02:42 +000080 // FIXME: Remove this once there is a proper way to detect an ARC runtime
81 // for the simulator.
82 switch (ARCRuntimeForSimulator) {
83 case ARCSimulator_None:
84 break;
85 case ARCSimulator_HasARCRuntime:
86 return true;
87 case ARCSimulator_NoARCRuntime:
88 return false;
89 }
90
91 if (isTargetIPhoneOS())
92 return !isIPhoneOSVersionLT(5);
93 else
94 return !isMacosxVersionLT(10, 7);
95}
96
John McCall9f084a32011-07-06 00:26:06 +000097/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
98void Darwin::configureObjCRuntime(ObjCRuntime &runtime) const {
99 if (runtime.getKind() != ObjCRuntime::NeXT)
100 return ToolChain::configureObjCRuntime(runtime);
101
102 runtime.HasARC = runtime.HasWeak = hasARCRuntime();
John McCall256a76e2011-07-06 01:22:26 +0000103
104 // So far, objc_terminate is only available in iOS 5.
105 // FIXME: do the simulator logic properly.
106 if (!ARCRuntimeForSimulator && isTargetIPhoneOS())
107 runtime.HasTerminate = !isIPhoneOSVersionLT(5);
108 else
109 runtime.HasTerminate = false;
John McCall9f084a32011-07-06 00:26:06 +0000110}
111
John McCall13db5cf2011-09-09 20:41:01 +0000112/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
113bool Darwin::hasBlocksRuntime() const {
114 if (isTargetIPhoneOS())
115 return !isIPhoneOSVersionLT(3, 2);
116 else
117 return !isMacosxVersionLT(10, 6);
118}
119
Daniel Dunbareeff4062010-01-22 02:04:58 +0000120// FIXME: Can we tablegen this?
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121static const char *GetArmArchForMArch(StringRef Value) {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000122 if (Value == "armv6k")
123 return "armv6";
124
125 if (Value == "armv5tej")
126 return "armv5";
127
128 if (Value == "xscale")
129 return "xscale";
130
131 if (Value == "armv4t")
132 return "armv4t";
133
134 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
135 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
136 Value == "armv7m")
137 return "armv7";
138
139 return 0;
140}
141
142// FIXME: Can we tablegen this?
Chris Lattner5f9e2722011-07-23 10:55:15 +0000143static const char *GetArmArchForMCpu(StringRef Value) {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000144 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
145 Value == "arm946e-s" || Value == "arm966e-s" ||
146 Value == "arm968e-s" || Value == "arm10e" ||
147 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
148 Value == "arm1026ej-s")
149 return "armv5";
150
151 if (Value == "xscale")
152 return "xscale";
153
154 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000155 Value == "arm1176jz-s" || Value == "arm1176jzf-s" ||
156 Value == "cortex-m0" )
Daniel Dunbareeff4062010-01-22 02:04:58 +0000157 return "armv6";
158
Bob Wilson169a6e02011-10-06 20:27:38 +0000159 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3" ||
160 Value == "cortex-a9")
Daniel Dunbareeff4062010-01-22 02:04:58 +0000161 return "armv7";
162
163 return 0;
164}
165
Chris Lattner5f9e2722011-07-23 10:55:15 +0000166StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000167 switch (getTriple().getArch()) {
168 default:
169 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000170
Douglas Gregorf0594d82011-03-06 19:11:49 +0000171 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000172 case llvm::Triple::arm: {
173 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
174 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
175 return Arch;
176
177 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
178 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
179 return Arch;
180
181 return "arm";
182 }
183 }
184}
185
Daniel Dunbarf3955282009-09-04 18:34:51 +0000186Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000187 // Free tool implementations.
188 for (llvm::DenseMap<unsigned, Tool*>::iterator
189 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
190 delete it->second;
191}
192
Chad Rosier61ab80a2011-09-20 20:44:06 +0000193std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
194 types::ID InputType) const {
195 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000196
197 // If the target isn't initialized (e.g., an unknown Darwin platform, return
198 // the default triple).
199 if (!isTargetInitialized())
200 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000201
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000202 unsigned Version[3];
203 getTargetVersion(Version);
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000204
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000205 llvm::SmallString<16> Str;
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000206 llvm::raw_svector_ostream(Str)
Daniel Dunbar659d23a2011-04-19 23:34:17 +0000207 << (isTargetIPhoneOS() ? "ios" : "macosx")
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000208 << Version[0] << "." << Version[1] << "." << Version[2];
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000209 Triple.setOSName(Str.str());
210
211 return Triple.getTriple();
212}
213
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000214Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
215 const ActionList &Inputs) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000216 Action::ActionClass Key;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000217
218 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
219 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
220 if (Inputs.size() == 1 &&
221 types::isCXX(Inputs[0]->getType()) &&
222 getTriple().getOS() == llvm::Triple::Darwin &&
223 getTriple().getArch() == llvm::Triple::x86 &&
Bob Wilsona544aee2011-08-13 23:48:55 +0000224 (C.getArgs().getLastArg(options::OPT_fapple_kext) ||
225 C.getArgs().getLastArg(options::OPT_mkernel)))
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000226 Key = JA.getKind();
227 else
228 Key = Action::AnalyzeJobClass;
229 } else
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000230 Key = JA.getKind();
231
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000232 // FIXME: This doesn't belong here, but ideally we will support static soon
233 // anyway.
234 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
235 C.getArgs().hasArg(options::OPT_static) ||
236 C.getArgs().hasArg(options::OPT_fapple_kext));
237 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
238 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
239 options::OPT_no_integrated_as,
240 IsIADefault);
241
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000242 Tool *&T = Tools[Key];
243 if (!T) {
244 switch (Key) {
245 case Action::InputClass:
246 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000247 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000248 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000249 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000250 case Action::AnalyzeJobClass:
251 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000252 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000253 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000254 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000255 case Action::AssembleJobClass: {
256 if (UseIntegratedAs)
257 T = new tools::ClangAs(*this);
258 else
259 T = new tools::darwin::Assemble(*this);
260 break;
261 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000262 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000263 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000264 case Action::LipoJobClass:
265 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000266 case Action::DsymutilJobClass:
267 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000268 case Action::VerifyJobClass:
269 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000270 }
271 }
272
273 return *T;
274}
275
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000276
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000277DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
278 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000279{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000280 getProgramPaths().push_back(getDriver().getInstalledDir());
281 if (getDriver().getInstalledDir() != getDriver().Dir)
282 getProgramPaths().push_back(getDriver().Dir);
283
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000284 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000285 getProgramPaths().push_back(getDriver().getInstalledDir());
286 if (getDriver().getInstalledDir() != getDriver().Dir)
287 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000288
289 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000290 // also add the GCC libexec paths. This is legacy code that can be removed
291 // once fallback is no longer useful.
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000292 AddGCCLibexecPath(DarwinVersion[0]);
293 AddGCCLibexecPath(DarwinVersion[0] - 2);
294 AddGCCLibexecPath(DarwinVersion[0] - 1);
295 AddGCCLibexecPath(DarwinVersion[0] + 1);
296 AddGCCLibexecPath(DarwinVersion[0] + 2);
297}
298
299void DarwinClang::AddGCCLibexecPath(unsigned darwinVersion) {
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000300 std::string ToolChainDir = "i686-apple-darwin";
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000301 ToolChainDir += llvm::utostr(darwinVersion);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000302 ToolChainDir += "/4.2.1";
303
304 std::string Path = getDriver().Dir;
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000305 Path += "/../llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000306 Path += ToolChainDir;
307 getProgramPaths().push_back(Path);
308
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000309 Path = "/usr/llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000310 Path += ToolChainDir;
311 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000312}
313
314void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
315 ArgStringList &CmdArgs) const {
316 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000317
318 // Unfortunately, we still might depend on a few of the libraries that are
319 // only available in the gcc library directory (in particular
320 // libstdc++.dylib). For now, hardcode the path to the known install location.
321 llvm::sys::Path P(getDriver().Dir);
322 P.eraseComponent(); // .../usr/bin -> ../usr
323 P.appendComponent("lib");
324 P.appendComponent("gcc");
325 switch (getTriple().getArch()) {
326 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000327 llvm_unreachable("Invalid Darwin arch!");
Daniel Dunbar424b6612010-06-30 23:56:13 +0000328 case llvm::Triple::x86:
329 case llvm::Triple::x86_64:
330 P.appendComponent("i686-apple-darwin10");
331 break;
332 case llvm::Triple::arm:
333 case llvm::Triple::thumb:
334 P.appendComponent("arm-apple-darwin10");
335 break;
336 case llvm::Triple::ppc:
337 case llvm::Triple::ppc64:
338 P.appendComponent("powerpc-apple-darwin10");
339 break;
340 }
341 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000342
343 // Determine the arch specific GCC subdirectory.
344 const char *ArchSpecificDir = 0;
345 switch (getTriple().getArch()) {
346 default:
347 break;
348 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000349 case llvm::Triple::thumb: {
350 std::string Triple = ComputeLLVMTriple(Args);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000351 StringRef TripleStr = Triple;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000352 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
353 ArchSpecificDir = "v5";
354 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
355 ArchSpecificDir = "v6";
356 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
357 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000358 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000359 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000360 case llvm::Triple::ppc64:
361 ArchSpecificDir = "ppc64";
362 break;
363 case llvm::Triple::x86_64:
364 ArchSpecificDir = "x86_64";
365 break;
366 }
367
368 if (ArchSpecificDir) {
369 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000370 bool Exists;
371 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000372 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
373 P.eraseComponent();
374 }
375
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000376 bool Exists;
377 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000378 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000379}
380
John McCallf85e1932011-06-15 23:02:42 +0000381void DarwinClang::AddLinkARCArgs(const ArgList &Args,
382 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000383
384 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000385 llvm::sys::Path P(getDriver().ClangExecutable);
386 P.eraseComponent(); // 'clang'
387 P.eraseComponent(); // 'bin'
388 P.appendComponent("lib");
389 P.appendComponent("arc");
390 P.appendComponent("libarclite_");
391 std::string s = P.str();
392 // Mash in the platform.
393 if (isTargetIPhoneOS())
394 s += "iphoneos";
395 // FIXME: isTargetIphoneOSSimulator() is not returning true.
396 else if (ARCRuntimeForSimulator != ARCSimulator_None)
397 s += "iphonesimulator";
398 else
399 s += "macosx";
400 s += ".a";
401
402 CmdArgs.push_back(Args.MakeArgString(s));
403}
404
Eric Christopher3404fe72011-06-22 17:41:40 +0000405void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000406 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000407 const char *DarwinStaticLib) const {
408 llvm::sys::Path P(getDriver().ResourceDir);
409 P.appendComponent("lib");
410 P.appendComponent("darwin");
411 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000412
Eric Christopher3404fe72011-06-22 17:41:40 +0000413 // For now, allow missing resource libraries to support developers who may
414 // not have compiler-rt checked out or integrated into their build.
415 bool Exists;
416 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
417 CmdArgs.push_back(Args.MakeArgString(P.str()));
418}
419
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000420void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
421 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000422 // Darwin doesn't support real static executables, don't link any runtime
423 // libraries with -static.
424 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000425 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000426
427 // Reject -static-libgcc for now, we can deal with this when and if someone
428 // cares. This is useful in situations where someone wants to statically link
429 // something like libstdc++, and needs its runtime support routines.
430 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000431 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000432 << A->getAsString(Args);
433 return;
434 }
435
Daniel Dunbareec99102010-01-22 03:38:14 +0000436 // Otherwise link libSystem, then the dynamic runtime library, and finally any
437 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000438 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000439
440 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000441 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000442 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
443 // it never went into the SDK.
444 if (!isTargetIOSSimulator())
445 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000446
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000447 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000448 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000449 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000450 // The dynamic runtime library was merged with libSystem for 10.6 and
451 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000452 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000453 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000454 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000455 CmdArgs.push_back("-lgcc_s.10.5");
456
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000457 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000458 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000459 // omitted from 10.4.dylib.
460 //
461 // Unfortunately, that turned out to not be true, because Darwin system
462 // headers can still use eprintf on i386, and it is not exported from
463 // libSystem. Therefore, we still must provide a runtime library just for
464 // the tiny tiny handful of projects that *might* use that symbol.
465 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000466 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000467 } else {
468 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000469 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
470 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000471 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000472 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000473}
474
Chris Lattner5f9e2722011-07-23 10:55:15 +0000475static inline StringRef SimulatorVersionDefineName() {
John McCallf85e1932011-06-15 23:02:42 +0000476 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
477}
478
479/// \brief Parse the simulator version define:
480/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
481// and return the grouped values as integers, e.g:
482// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
483// will return Major=4, Minor=2, Micro=1.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000484static bool GetVersionFromSimulatorDefine(StringRef define,
John McCallf85e1932011-06-15 23:02:42 +0000485 unsigned &Major, unsigned &Minor,
486 unsigned &Micro) {
487 assert(define.startswith(SimulatorVersionDefineName()));
Chris Lattner5f9e2722011-07-23 10:55:15 +0000488 StringRef name, version;
John McCallf85e1932011-06-15 23:02:42 +0000489 llvm::tie(name, version) = define.split('=');
490 if (version.empty())
491 return false;
492 std::string verstr = version.str();
493 char *end;
494 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
495 if (*end != '\0')
496 return false;
497 Major = num / 10000;
498 num = num % 10000;
499 Minor = num / 100;
500 Micro = num % 100;
501 return true;
502}
503
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000504void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000505 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000506
Daniel Dunbar26031372010-01-27 00:56:25 +0000507 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000508 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
509 Arg *iOSSimVersion = Args.getLastArg(
510 options::OPT_mios_simulator_version_min_EQ);
John McCallf85e1932011-06-15 23:02:42 +0000511
512 // FIXME: HACK! When compiling for the simulator we don't get a
513 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
514 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
515 // define passed in command-line.
516 if (!iOSVersion) {
517 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
518 ie = Args.filtered_end(); it != ie; ++it) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000519 StringRef define = (*it)->getValue(Args);
John McCallf85e1932011-06-15 23:02:42 +0000520 if (define.startswith(SimulatorVersionDefineName())) {
NAKAMURA Takumi57c43a22011-08-14 00:37:22 +0000521 unsigned Major = 0, Minor = 0, Micro = 0;
John McCallf85e1932011-06-15 23:02:42 +0000522 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
523 Major < 10 && Minor < 100 && Micro < 100) {
524 ARCRuntimeForSimulator = Major < 5 ? ARCSimulator_NoARCRuntime
525 : ARCSimulator_HasARCRuntime;
526 }
527 break;
528 }
529 }
530 }
531
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000532 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000533 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000534 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000535 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
536 iOSVersion = iOSSimVersion = 0;
537 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000538 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000539 << iOSVersion->getAsString(Args)
540 << iOSSimVersion->getAsString(Args);
541 iOSSimVersion = 0;
542 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000543 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000544 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000545 StringRef OSXTarget;
546 StringRef iOSTarget;
547 StringRef iOSSimTarget;
548 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
549 OSXTarget = env;
550 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
551 iOSTarget = env;
552 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
553 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000554
Chad Rosiera4884972011-08-31 20:56:25 +0000555 // If no '-miphoneos-version-min' specified on the command line and
556 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
557 // based on isysroot.
558 if (iOSTarget.empty()) {
559 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
560 StringRef first, second;
561 StringRef isysroot = A->getValue(Args);
562 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
563 if (second != "")
564 iOSTarget = second.substr(0,3);
565 }
566 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000567
Chad Rosier4f8de272011-09-28 00:46:32 +0000568 // If no OSX or iOS target has been specified and we're compiling for armv7,
569 // go ahead as assume we're targeting iOS.
570 if (OSXTarget.empty() && iOSTarget.empty())
571 if (getDarwinArchName(Args) == "armv7")
572 iOSTarget = "0.0";
573
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000574 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000575 //
576 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000577
578 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000579 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000580 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000581 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000582 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000583 "IPHONEOS_DEPLOYMENT_TARGET");
584 }
585
586 // Allow conflicts among OSX and iOS for historical reasons, but choose the
587 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000588 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000589 if (getTriple().getArch() == llvm::Triple::arm ||
590 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000591 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000592 else
Chad Rosiera4884972011-08-31 20:56:25 +0000593 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000594 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000595
Chad Rosiera4884972011-08-31 20:56:25 +0000596 if (!OSXTarget.empty()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000597 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000598 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
599 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000600 } else if (!iOSTarget.empty()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000601 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000602 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
603 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000604 } else if (!iOSSimTarget.empty()) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000605 const Option *O = Opts.getOption(
606 options::OPT_mios_simulator_version_min_EQ);
607 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
608 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000609 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000610 // Otherwise, assume we are targeting OS X.
611 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000612 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
613 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000614 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000617 // Reject invalid architecture combinations.
618 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
619 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000620 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000621 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
622 }
623
Daniel Dunbar26031372010-01-27 00:56:25 +0000624 // Set the tool chain target information.
625 unsigned Major, Minor, Micro;
626 bool HadExtra;
627 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000628 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar26031372010-01-27 00:56:25 +0000629 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
630 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000631 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000632 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000633 << OSXVersion->getAsString(Args);
634 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000635 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
636 assert(Version && "Unknown target platform!");
637 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000638 Micro, HadExtra) || HadExtra ||
639 Major >= 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000640 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000641 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000642 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000643
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000644 bool IsIOSSim = bool(iOSSimVersion);
645
646 // In GCC, the simulator historically was treated as being OS X in some
647 // contexts, like determining the link logic, despite generally being called
648 // with an iOS deployment target. For compatibility, we detect the
649 // simulator as iOS + x86, and treat it differently in a few contexts.
650 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
651 getTriple().getArch() == llvm::Triple::x86_64))
652 IsIOSSim = true;
653
654 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000655}
656
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000657void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000658 ArgStringList &CmdArgs) const {
659 CXXStdlibType Type = GetCXXStdlibType(Args);
660
661 switch (Type) {
662 case ToolChain::CST_Libcxx:
663 CmdArgs.push_back("-lc++");
664 break;
665
666 case ToolChain::CST_Libstdcxx: {
667 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
668 // it was previously found in the gcc lib dir. However, for all the Darwin
669 // platforms we care about it was -lstdc++.6, so we search for that
670 // explicitly if we can't see an obvious -lstdc++ candidate.
671
672 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000673 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000674 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
675 llvm::sys::Path P(A->getValue(Args));
676 P.appendComponent("usr");
677 P.appendComponent("lib");
678 P.appendComponent("libstdc++.dylib");
679
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000680 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000681 P.eraseComponent();
682 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000683 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000684 CmdArgs.push_back(Args.MakeArgString(P.str()));
685 return;
686 }
687 }
688 }
689
690 // Otherwise, look in the root.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000691 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
692 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000693 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
694 return;
695 }
696
697 // Otherwise, let the linker search.
698 CmdArgs.push_back("-lstdc++");
699 break;
700 }
701 }
702}
703
Shantonu Sen7433fed2010-09-17 18:39:08 +0000704void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
705 ArgStringList &CmdArgs) const {
706
707 // For Darwin platforms, use the compiler-rt-based support library
708 // instead of the gcc-provided one (which is also incidentally
709 // only present in the gcc lib dir, which makes it hard to find).
710
711 llvm::sys::Path P(getDriver().ResourceDir);
712 P.appendComponent("lib");
713 P.appendComponent("darwin");
714 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000715
Shantonu Sen7433fed2010-09-17 18:39:08 +0000716 // For now, allow missing resource libraries to support developers who may
717 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000718 bool Exists;
719 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000720 CmdArgs.push_back(Args.MakeArgString(P.str()));
721}
722
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000723DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
724 const char *BoundArch) const {
725 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
726 const OptTable &Opts = getDriver().getOpts();
727
728 // FIXME: We really want to get out of the tool chain level argument
729 // translation business, as it makes the driver functionality much
730 // more opaque. For now, we follow gcc closely solely for the
731 // purpose of easily achieving feature parity & testability. Once we
732 // have something that works, we should reevaluate each translation
733 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000734
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000735 for (ArgList::const_iterator it = Args.begin(),
736 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000737 Arg *A = *it;
738
739 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000740 // Skip this argument unless the architecture matches either the toolchain
741 // triple arch, or the arch being bound.
742 //
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000743 // FIXME: Canonicalize name.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000744 StringRef XarchArch = A->getValue(Args, 0);
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000745 if (!(XarchArch == getArchName() ||
746 (BoundArch && XarchArch == BoundArch)))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000747 continue;
748
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000749 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000750 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
751 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000752 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000754 // If the argument parsing failed or more than one argument was
755 // consumed, the -Xarch_ argument's parameter tried to consume
756 // extra arguments. Emit an error and ignore.
757 //
758 // We also want to disallow any options which would alter the
759 // driver behavior; that isn't going to work in our model. We
760 // use isDriverOption() as an approximation, although things
761 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000762 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000763 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000764 << A->getAsString(Args);
765 continue;
766 } else if (XarchArg->getOption().isDriverOption()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000767 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000768 << A->getAsString(Args);
769 continue;
770 }
771
Daniel Dunbar478edc22009-03-29 22:29:05 +0000772 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000773 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000774
775 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000776
777 // Linker input arguments require custom handling. The problem is that we
778 // have already constructed the phase actions, so we can not treat them as
779 // "input arguments".
780 if (A->getOption().isLinkerInput()) {
781 // Convert the argument into individual Zlinker_input_args.
782 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
783 DAL->AddSeparateArg(OriginalArg,
784 Opts.getOption(options::OPT_Zlinker_input),
785 A->getValue(Args, i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000786
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000787 }
788 continue;
789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000791
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000792 // Sob. These is strictly gcc compatible for the time being. Apple
793 // gcc translates options twice, which means that self-expanding
794 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000795 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000796 default:
797 DAL->append(A);
798 break;
799
800 case options::OPT_mkernel:
801 case options::OPT_fapple_kext:
802 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000803 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000804 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000806 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000807 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
808 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000809 break;
810
811 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000812 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
813 DAL->AddFlagArg(A,
814 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000815 break;
816
817 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000818 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
819 DAL->AddFlagArg(A,
820 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000821 break;
822
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000823 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000824 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000825 break;
826
827 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000828 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000829 break;
830
831 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000832 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000833 break;
834
835 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000836 DAL->AddFlagArg(A,
837 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000838 break;
839
840 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000841 DAL->AddFlagArg(A,
842 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000843 break;
844
845 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000846 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000847 break;
848
849 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000850 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000851 break;
852 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000853 }
854
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000855 if (getTriple().getArch() == llvm::Triple::x86 ||
856 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000857 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000858 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000859
860 // Add the arch options based on the particular spelling of -arch, to match
861 // how the driver driver works.
862 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000863 StringRef Name = BoundArch;
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000864 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
865 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
866
867 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
868 // which defines the list of which architectures we accept.
869 if (Name == "ppc")
870 ;
871 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000872 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000873 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000874 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000875 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000876 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000877 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000878 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000879 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000880 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000881 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000882 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000883 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000884 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000885 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000886 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000887
888 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000889 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000890
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000891 else if (Name == "i386")
892 ;
893 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000894 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000895 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000896 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000897 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000898 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000899 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000900 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000901 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000902 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000903 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000904 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000905 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000906 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000907
908 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000909 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000910
911 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000912 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000913 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000914 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000915 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000916 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000917 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000918 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000919 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000920 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000921 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000922 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000923
924 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000925 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000926 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000927
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000928 // Add an explicit version min argument for the deployment target. We do this
929 // after argument translation because -Xarch_ arguments may add a version min
930 // argument.
931 AddDeploymentTarget(*DAL);
932
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000933 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000934}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000935
Daniel Dunbarf3955282009-09-04 18:34:51 +0000936bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000937 // FIXME: Gross; we should probably have some separate target
938 // definition, possibly even reusing the one in clang.
939 return getArchName() == "x86_64";
940}
941
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000942bool Darwin::UseDwarfDebugFlags() const {
943 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
944 return S[0] != '\0';
945 return false;
946}
947
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000948bool Darwin::UseSjLjExceptions() const {
949 // Darwin uses SjLj exceptions on ARM.
950 return (getTriple().getArch() == llvm::Triple::arm ||
951 getTriple().getArch() == llvm::Triple::thumb);
952}
953
Daniel Dunbarf3955282009-09-04 18:34:51 +0000954const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000955 return "pic";
956}
957
Daniel Dunbarf3955282009-09-04 18:34:51 +0000958const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000959 if (getArchName() == "x86_64")
960 return "pic";
961 return 0;
962}
963
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000964bool Darwin::SupportsProfiling() const {
965 // Profiling instrumentation is only supported on x86.
966 return getArchName() == "i386" || getArchName() == "x86_64";
967}
968
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000969bool Darwin::SupportsObjCGC() const {
970 // Garbage collection is supported everywhere except on iPhone OS.
971 return !isTargetIPhoneOS();
972}
973
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000974std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000975Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
976 types::ID InputType) const {
977 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000978}
979
Daniel Dunbar39176082009-03-20 00:20:03 +0000980/// Generic_GCC - A tool chain using the 'gcc' command to perform
981/// all subcommands; this relies on gcc translating the majority of
982/// command line options.
983
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000984Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000985 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000986 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +0000987 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000988 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000989}
990
Daniel Dunbar39176082009-03-20 00:20:03 +0000991Generic_GCC::~Generic_GCC() {
992 // Free tool implementations.
993 for (llvm::DenseMap<unsigned, Tool*>::iterator
994 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
995 delete it->second;
996}
997
Mike Stump1eb44332009-09-09 15:08:12 +0000998Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000999 const JobAction &JA,
1000 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +00001001 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001002 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +00001003 Key = Action::AnalyzeJobClass;
1004 else
1005 Key = JA.getKind();
1006
1007 Tool *&T = Tools[Key];
1008 if (!T) {
1009 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001010 case Action::InputClass:
1011 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001012 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001013 case Action::PreprocessJobClass:
1014 T = new tools::gcc::Preprocess(*this); break;
1015 case Action::PrecompileJobClass:
1016 T = new tools::gcc::Precompile(*this); break;
1017 case Action::AnalyzeJobClass:
1018 T = new tools::Clang(*this); break;
1019 case Action::CompileJobClass:
1020 T = new tools::gcc::Compile(*this); break;
1021 case Action::AssembleJobClass:
1022 T = new tools::gcc::Assemble(*this); break;
1023 case Action::LinkJobClass:
1024 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001026 // This is a bit ungeneric, but the only platform using a driver
1027 // driver is Darwin.
1028 case Action::LipoJobClass:
1029 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001030 case Action::DsymutilJobClass:
1031 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001032 case Action::VerifyJobClass:
1033 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001034 }
1035 }
1036
1037 return *T;
1038}
1039
Daniel Dunbar39176082009-03-20 00:20:03 +00001040bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001041 // FIXME: Gross; we should probably have some separate target
1042 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +00001043 return getArchName() == "x86_64";
1044}
1045
1046const char *Generic_GCC::GetDefaultRelocationModel() const {
1047 return "static";
1048}
1049
1050const char *Generic_GCC::GetForcedPicModel() const {
1051 return 0;
1052}
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001053
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001054/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1055/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1056/// Currently does not support anything else but compilation.
1057
1058TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
1059 : ToolChain(Host, Triple) {
1060 // Path mangling to find libexec
1061 std::string Path(getDriver().Dir);
1062
1063 Path += "/../libexec";
1064 getProgramPaths().push_back(Path);
1065}
1066
1067TCEToolChain::~TCEToolChain() {
1068 for (llvm::DenseMap<unsigned, Tool*>::iterator
1069 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1070 delete it->second;
1071}
1072
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001073bool TCEToolChain::IsMathErrnoDefault() const {
1074 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001075}
1076
1077bool TCEToolChain::IsUnwindTablesDefault() const {
1078 return false;
1079}
1080
1081const char *TCEToolChain::GetDefaultRelocationModel() const {
1082 return "static";
1083}
1084
1085const char *TCEToolChain::GetForcedPicModel() const {
1086 return 0;
1087}
1088
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001089Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001090 const JobAction &JA,
1091 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001092 Action::ActionClass Key;
1093 Key = Action::AnalyzeJobClass;
1094
1095 Tool *&T = Tools[Key];
1096 if (!T) {
1097 switch (Key) {
1098 case Action::PreprocessJobClass:
1099 T = new tools::gcc::Preprocess(*this); break;
1100 case Action::AnalyzeJobClass:
1101 T = new tools::Clang(*this); break;
1102 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001103 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001104 }
1105 }
1106 return *T;
1107}
1108
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001109/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1110
1111OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001112 : Generic_ELF(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001113 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001114 getFilePaths().push_back("/usr/lib");
1115}
1116
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001117Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1118 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001119 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001120 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001121 Key = Action::AnalyzeJobClass;
1122 else
1123 Key = JA.getKind();
1124
Rafael Espindoladda5b922010-11-07 23:13:01 +00001125 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1126 options::OPT_no_integrated_as,
1127 IsIntegratedAssemblerDefault());
1128
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001129 Tool *&T = Tools[Key];
1130 if (!T) {
1131 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001132 case Action::AssembleJobClass: {
1133 if (UseIntegratedAs)
1134 T = new tools::ClangAs(*this);
1135 else
1136 T = new tools::openbsd::Assemble(*this);
1137 break;
1138 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001139 case Action::LinkJobClass:
1140 T = new tools::openbsd::Link(*this); break;
1141 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001142 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001143 }
1144 }
1145
1146 return *T;
1147}
1148
Daniel Dunbar75358d22009-03-30 21:06:03 +00001149/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1150
Daniel Dunbar214afe92010-08-02 05:43:59 +00001151FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001152 : Generic_ELF(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001153
1154 // Determine if we are compiling 32-bit code on an x86_64 platform.
1155 bool Lib32 = false;
1156 if (Triple.getArch() == llvm::Triple::x86 &&
1157 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1158 llvm::Triple::x86_64)
1159 Lib32 = true;
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001160
Roman Divacky3393cef2011-06-04 07:37:31 +00001161 if (Triple.getArch() == llvm::Triple::ppc &&
1162 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1163 llvm::Triple::ppc64)
1164 Lib32 = true;
1165
Daniel Dunbarbc534662009-04-02 18:30:04 +00001166 if (Lib32) {
Daniel Dunbarbc534662009-04-02 18:30:04 +00001167 getFilePaths().push_back("/usr/lib32");
1168 } else {
Daniel Dunbarbc534662009-04-02 18:30:04 +00001169 getFilePaths().push_back("/usr/lib");
1170 }
Daniel Dunbar75358d22009-03-30 21:06:03 +00001171}
1172
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001173Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1174 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001175 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001176 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001177 Key = Action::AnalyzeJobClass;
1178 else
1179 Key = JA.getKind();
1180
Roman Divacky67dece72010-11-08 17:46:39 +00001181 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1182 options::OPT_no_integrated_as,
1183 IsIntegratedAssemblerDefault());
1184
Daniel Dunbar75358d22009-03-30 21:06:03 +00001185 Tool *&T = Tools[Key];
1186 if (!T) {
1187 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001188 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001189 if (UseIntegratedAs)
1190 T = new tools::ClangAs(*this);
1191 else
1192 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001193 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001194 case Action::LinkJobClass:
1195 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001196 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001197 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001198 }
1199 }
1200
1201 return *T;
1202}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001203
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001204/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1205
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001206NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
1207 const llvm::Triple& ToolTriple)
1208 : Generic_ELF(Host, Triple), ToolTriple(ToolTriple) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001209
1210 // Determine if we are compiling 32-bit code on an x86_64 platform.
1211 bool Lib32 = false;
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001212 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
1213 Triple.getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001214 Lib32 = true;
1215
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001216 if (getDriver().UseStdLib) {
1217 if (Lib32)
1218 getFilePaths().push_back("=/usr/lib/i386");
1219 else
1220 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001221 }
1222}
1223
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001224Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1225 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001226 Action::ActionClass Key;
1227 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1228 Key = Action::AnalyzeJobClass;
1229 else
1230 Key = JA.getKind();
1231
1232 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1233 options::OPT_no_integrated_as,
1234 IsIntegratedAssemblerDefault());
1235
1236 Tool *&T = Tools[Key];
1237 if (!T) {
1238 switch (Key) {
1239 case Action::AssembleJobClass:
1240 if (UseIntegratedAs)
1241 T = new tools::ClangAs(*this);
1242 else
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001243 T = new tools::netbsd::Assemble(*this, ToolTriple);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001244 break;
1245 case Action::LinkJobClass:
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001246 T = new tools::netbsd::Link(*this, ToolTriple);
1247 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001248 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001249 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001250 }
1251 }
1252
1253 return *T;
1254}
1255
Chris Lattner38e317d2010-07-07 16:01:42 +00001256/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1257
1258Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1259 : Generic_GCC(Host, Triple) {
1260 getFilePaths().push_back(getDriver().Dir + "/../lib");
1261 getFilePaths().push_back("/usr/lib");
1262 getFilePaths().push_back("/usr/gnu/lib");
1263 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1264}
1265
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001266Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1267 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001268 Action::ActionClass Key;
1269 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1270 Key = Action::AnalyzeJobClass;
1271 else
1272 Key = JA.getKind();
1273
1274 Tool *&T = Tools[Key];
1275 if (!T) {
1276 switch (Key) {
1277 case Action::AssembleJobClass:
1278 T = new tools::minix::Assemble(*this); break;
1279 case Action::LinkJobClass:
1280 T = new tools::minix::Link(*this); break;
1281 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001282 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001283 }
1284 }
1285
1286 return *T;
1287}
1288
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001289/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1290
1291AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1292 : Generic_GCC(Host, Triple) {
1293
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001294 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001295 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001296 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001297
Daniel Dunbaree788e72009-12-21 18:54:17 +00001298 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001299 getFilePaths().push_back("/usr/lib");
1300 getFilePaths().push_back("/usr/sfw/lib");
1301 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001302 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001303
1304}
1305
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001306Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1307 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001308 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001309 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001310 Key = Action::AnalyzeJobClass;
1311 else
1312 Key = JA.getKind();
1313
1314 Tool *&T = Tools[Key];
1315 if (!T) {
1316 switch (Key) {
1317 case Action::AssembleJobClass:
1318 T = new tools::auroraux::Assemble(*this); break;
1319 case Action::LinkJobClass:
1320 T = new tools::auroraux::Link(*this); break;
1321 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001322 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001323 }
1324 }
1325
1326 return *T;
1327}
1328
1329
Eli Friedman6b3454a2009-05-26 07:52:18 +00001330/// Linux toolchain (very bare-bones at the moment).
1331
Rafael Espindolac1da9812010-11-07 20:14:31 +00001332enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001333 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001334 DebianLenny,
1335 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001336 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001337 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001338 RHEL4,
1339 RHEL5,
1340 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001341 Fedora13,
1342 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001343 Fedora15,
1344 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001345 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001346 OpenSuse11_4,
1347 OpenSuse12_1,
Douglas Gregor814638e2011-03-14 15:39:50 +00001348 UbuntuHardy,
1349 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001350 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001351 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001352 UbuntuLucid,
1353 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001354 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001355 UbuntuOneiric,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001356 UnknownDistro
1357};
1358
Chris Lattnerd753b562011-05-22 05:36:06 +00001359static bool IsRedhat(enum LinuxDistro Distro) {
Eric Christopher8f1cc072011-04-06 18:22:53 +00001360 return Distro == Fedora13 || Distro == Fedora14 ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001361 Distro == Fedora15 || Distro == FedoraRawhide ||
1362 Distro == RHEL4 || Distro == RHEL5 || Distro == RHEL6;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001363}
1364
1365static bool IsOpenSuse(enum LinuxDistro Distro) {
David Chisnallde5c0482011-05-19 13:26:33 +00001366 return Distro == OpenSuse11_3 || Distro == OpenSuse11_4 ||
1367 Distro == OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001368}
1369
1370static bool IsDebian(enum LinuxDistro Distro) {
Eli Friedman0b200f62011-06-02 21:36:53 +00001371 return Distro == DebianLenny || Distro == DebianSqueeze ||
1372 Distro == DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001373}
1374
1375static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001376 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001377 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Ted Kremenek43ac2972011-04-05 22:04:27 +00001378 Distro == UbuntuJaunty || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001379 Distro == UbuntuNatty || Distro == UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001380}
1381
Chandler Carruth38ec5462011-10-03 09:00:50 +00001382// FIXME: This should be deleted. We should assume a multilib environment, and
1383// fallback gracefully if any parts of it are absent.
Rafael Espindolac1da9812010-11-07 20:14:31 +00001384static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001385 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001386 bool Exists;
1387 if (Distro == Exherbo &&
1388 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001389 return false;
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001390 }
Chandler Carruth38ec5462011-10-03 09:00:50 +00001391
1392 return true;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001393}
1394
1395static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001396 llvm::OwningPtr<llvm::MemoryBuffer> File;
1397 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001398 StringRef Data = File.get()->getBuffer();
1399 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001400 Data.split(Lines, "\n");
1401 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001402 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1403 return UbuntuHardy;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001404 else if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1405 return UbuntuIntrepid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001406 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001407 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001408 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1409 return UbuntuKarmic;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001410 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1411 return UbuntuLucid;
1412 else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1413 return UbuntuMaverick;
1414 else if (Lines[i] == "DISTRIB_CODENAME=natty")
1415 return UbuntuNatty;
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001416 else if (Lines[i] == "DISTRIB_CODENAME=oneiric")
1417 return UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001418 }
1419 return UnknownDistro;
1420 }
1421
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001422 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001423 StringRef Data = File.get()->getBuffer();
Eric Christopher8f1cc072011-04-06 18:22:53 +00001424 if (Data.startswith("Fedora release 15"))
1425 return Fedora15;
1426 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001427 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001428 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001429 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001430 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001431 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001432 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001433 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001434 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001435 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001436 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1437 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001438 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001439 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001440 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1441 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001442 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001443 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001444 return UnknownDistro;
1445 }
1446
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001447 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001448 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001449 if (Data[0] == '5')
1450 return DebianLenny;
1451 else if (Data.startswith("squeeze/sid"))
1452 return DebianSqueeze;
Eli Friedman0b200f62011-06-02 21:36:53 +00001453 else if (Data.startswith("wheezy/sid"))
1454 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001455 return UnknownDistro;
1456 }
1457
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001458 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001459 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001460 if (Data.startswith("openSUSE 11.3"))
1461 return OpenSuse11_3;
David Chisnallde5c0482011-05-19 13:26:33 +00001462 else if (Data.startswith("openSUSE 11.4"))
1463 return OpenSuse11_4;
1464 else if (Data.startswith("openSUSE 12.1"))
1465 return OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001466 return UnknownDistro;
1467 }
1468
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001469 bool Exists;
1470 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001471 return Exherbo;
1472
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001473 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1474 return ArchLinux;
1475
Rafael Espindolac1da9812010-11-07 20:14:31 +00001476 return UnknownDistro;
1477}
1478
Chandler Carruth048e6492011-10-03 18:16:54 +00001479/// \brief Trivial helper function to simplify code checking path existence.
Chandler Carruthadc4afb2011-10-04 02:28:41 +00001480static bool PathExists(StringRef Path) {
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001481 bool Exists;
Chandler Carruth048e6492011-10-03 18:16:54 +00001482 if (!llvm::sys::fs::exists(Path, Exists))
1483 return Exists;
1484 return false;
1485}
1486
1487namespace {
1488/// \brief This is a class to find a viable GCC installation for Clang to use.
1489///
1490/// This class tries to find a GCC installation on the system, and report
1491/// information about it. It starts from the host information provided to the
1492/// Driver, and has logic for fuzzing that where appropriate.
1493class GCCInstallationDetector {
Chandler Carruth6d9694c2011-10-04 21:22:33 +00001494 /// \brief Struct to store and manipulate GCC versions.
1495 ///
1496 /// We rely on assumptions about the form and structure of GCC version
1497 /// numbers: they consist of at most three '.'-separated components, and each
1498 /// component is a non-negative integer.
1499 struct GCCVersion {
1500 unsigned Major, Minor, Patch;
1501
1502 static GCCVersion Parse(StringRef VersionText) {
1503 const GCCVersion BadVersion = {};
1504 std::pair<StringRef, StringRef> First = VersionText.split('.');
1505 std::pair<StringRef, StringRef> Second = First.second.split('.');
1506
1507 GCCVersion GoodVersion = {};
1508 if (First.first.getAsInteger(10, GoodVersion.Major))
1509 return BadVersion;
1510 if (Second.first.getAsInteger(10, GoodVersion.Minor))
1511 return BadVersion;
Chandler Carruthdbc21442011-10-05 03:09:51 +00001512 // We accept a number, or a string for the patch version, in case there
1513 // is a strang suffix, or other mangling: '4.1.x', '4.1.2-rc3'. When it
1514 // isn't a number, we just use '0' as the number but accept it.
1515 if (Second.first.getAsInteger(10, GoodVersion.Patch))
1516 GoodVersion.Patch = 0;
Chandler Carruth6d9694c2011-10-04 21:22:33 +00001517 return GoodVersion;
1518 }
1519
1520 bool operator<(const GCCVersion &RHS) const {
1521 if (Major < RHS.Major) return true;
1522 if (Major > RHS.Major) return false;
1523 if (Minor < RHS.Minor) return true;
1524 if (Minor > RHS.Minor) return false;
1525 return Patch < RHS.Patch;
1526 }
1527 bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
1528 bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
1529 bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
1530 };
1531
Chandler Carruth048e6492011-10-03 18:16:54 +00001532 bool IsValid;
1533 std::string GccTriple;
1534
1535 // FIXME: These might be better as path objects.
1536 std::string GccInstallPath;
1537 std::string GccParentLibPath;
1538
1539 llvm::SmallString<128> CxxIncludeRoot;
1540
1541public:
1542 /// \brief Construct a GCCInstallationDetector from the driver.
1543 ///
1544 /// This performs all of the autodetection and sets up the various paths.
1545 /// Once constructed, a GCCInstallation is esentially immutable.
1546 GCCInstallationDetector(const Driver &D)
1547 : IsValid(false),
1548 GccTriple(D.DefaultHostTriple),
1549 CxxIncludeRoot(CXX_INCLUDE_ROOT) {
1550 // FIXME: Using CXX_INCLUDE_ROOT is here is a bit of a hack, but
1551 // avoids adding yet another option to configure/cmake.
1552 // It would probably be cleaner to break it in two variables
1553 // CXX_GCC_ROOT with just /foo/bar
1554 // CXX_GCC_VER with 4.5.2
1555 // Then we would have
1556 // CXX_INCLUDE_ROOT = CXX_GCC_ROOT/include/c++/CXX_GCC_VER
1557 // and this function would return
1558 // CXX_GCC_ROOT/lib/gcc/CXX_INCLUDE_ARCH/CXX_GCC_VER
1559 if (CxxIncludeRoot != "") {
1560 // This is of the form /foo/bar/include/c++/4.5.2/
1561 if (CxxIncludeRoot.back() == '/')
1562 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the /
1563 StringRef Version = llvm::sys::path::filename(CxxIncludeRoot);
1564 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the version
1565 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the c++
1566 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the include
1567 GccInstallPath = CxxIncludeRoot.str();
1568 GccInstallPath.append("/lib/gcc/");
1569 GccInstallPath.append(CXX_INCLUDE_ARCH);
1570 GccInstallPath.append("/");
1571 GccInstallPath.append(Version);
1572 GccParentLibPath = GccInstallPath + "/../../../..";
1573 IsValid = true;
1574 return;
1575 }
1576
1577 llvm::Triple::ArchType HostArch = llvm::Triple(GccTriple).getArch();
Chandler Carruth810e0812011-10-04 08:32:14 +00001578 // The library directories which may contain GCC installations.
Chandler Carrutha24b9802011-10-04 09:47:17 +00001579 SmallVector<StringRef, 4> CandidateLibDirs;
Chandler Carruth810e0812011-10-04 08:32:14 +00001580 // The compatible GCC triples for this particular architecture.
Chandler Carruthadc4afb2011-10-04 02:28:41 +00001581 SmallVector<StringRef, 10> CandidateTriples;
Chandler Carruth4c9403c2011-10-04 22:58:04 +00001582 CollectLibDirsAndTriples(HostArch, CandidateLibDirs, CandidateTriples);
Chandler Carruthadc4afb2011-10-04 02:28:41 +00001583
Chandler Carruth810e0812011-10-04 08:32:14 +00001584 // Always include the default host triple as the final fallback if no
1585 // specific triple is detected.
1586 CandidateTriples.push_back(D.DefaultHostTriple);
Chandler Carruth048e6492011-10-03 18:16:54 +00001587
Chandler Carruth6d9694c2011-10-04 21:22:33 +00001588 // Compute the set of prefixes for our search.
Chandler Carruth810e0812011-10-04 08:32:14 +00001589 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1590 D.PrefixDirs.end());
Chandler Carruth6ab8e622011-10-04 21:22:42 +00001591 Prefixes.push_back(D.SysRoot);
Chandler Carruth810e0812011-10-04 08:32:14 +00001592 Prefixes.push_back(D.SysRoot + "/usr");
Chandler Carruth12036002011-10-05 06:38:03 +00001593 Prefixes.push_back(D.InstalledDir + "/..");
Chandler Carruth810e0812011-10-04 08:32:14 +00001594
Chandler Carruth6d9694c2011-10-04 21:22:33 +00001595 // Loop over the various components which exist and select the best GCC
1596 // installation available. GCC installs are ranked by version number.
Chandler Carruth6d9694c2011-10-04 21:22:33 +00001597 GCCVersion BestVersion = {};
1598 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1599 if (!PathExists(Prefixes[i]))
1600 continue;
1601 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1602 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1603 if (!PathExists(LibDir))
1604 continue;
Chandler Carruthe0890882011-10-04 23:17:12 +00001605 for (unsigned k = 0, ke = CandidateTriples.size(); k < ke; ++k)
1606 ScanLibDirForGCCTriple(LibDir, CandidateTriples[k], BestVersion);
David Chisnall5adcec12011-09-27 22:03:18 +00001607 }
Eli Friedman733a83b2011-09-16 21:04:38 +00001608 }
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001609 }
Chandler Carruth048e6492011-10-03 18:16:54 +00001610
1611 /// \brief Check whether we detected a valid GCC install.
1612 bool isValid() const { return IsValid; }
1613
1614 /// \brief Get the GCC triple for the detected install.
1615 const std::string &getTriple() const { return GccTriple; }
1616
1617 /// \brief Get the detected GCC installation path.
1618 const std::string &getInstallPath() const { return GccInstallPath; }
1619
1620 /// \brief Get the detected GCC parent lib path.
1621 const std::string &getParentLibPath() const { return GccParentLibPath; }
Chandler Carruth4c9403c2011-10-04 22:58:04 +00001622
1623private:
1624 static void CollectLibDirsAndTriples(llvm::Triple::ArchType HostArch,
1625 SmallVectorImpl<StringRef> &LibDirs,
1626 SmallVectorImpl<StringRef> &Triples) {
1627 if (HostArch == llvm::Triple::arm || HostArch == llvm::Triple::thumb) {
1628 static const char *const ARMLibDirs[] = { "/lib/gcc" };
1629 static const char *const ARMTriples[] = { "arm-linux-gnueabi" };
1630 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
1631 Triples.append(ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1632 } else if (HostArch == llvm::Triple::x86_64) {
1633 static const char *const X86_64LibDirs[] = {
1634 "/lib64/gcc", "/lib/gcc", "/lib64", "/lib"
1635 };
1636 static const char *const X86_64Triples[] = {
1637 "x86_64-linux-gnu",
1638 "x86_64-unknown-linux-gnu",
1639 "x86_64-pc-linux-gnu",
1640 "x86_64-redhat-linux6E",
1641 "x86_64-redhat-linux",
1642 "x86_64-suse-linux",
1643 "x86_64-manbo-linux-gnu",
1644 "x86_64-linux-gnu",
1645 "x86_64-slackware-linux"
1646 };
1647 LibDirs.append(X86_64LibDirs,
1648 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1649 Triples.append(X86_64Triples,
1650 X86_64Triples + llvm::array_lengthof(X86_64Triples));
1651 } else if (HostArch == llvm::Triple::x86) {
1652 static const char *const X86LibDirs[] = {
1653 "/lib32/gcc", "/lib/gcc", "/lib32", "/lib"
1654 };
1655 static const char *const X86Triples[] = {
1656 "i686-linux-gnu",
1657 "i386-linux-gnu",
1658 "i686-pc-linux-gnu",
1659 "i486-linux-gnu",
1660 "i686-redhat-linux",
1661 "i586-suse-linux",
1662 "i486-slackware-linux"
1663 };
1664 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1665 Triples.append(X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1666 } else if (HostArch == llvm::Triple::ppc) {
1667 static const char *const PPCLibDirs[] = {
1668 "/lib32/gcc", "/lib/gcc", "/lib32", "/lib"
1669 };
1670 static const char *const PPCTriples[] = {
1671 "powerpc-linux-gnu",
1672 "powerpc-unknown-linux-gnu"
1673 };
1674 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1675 Triples.append(PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1676 } else if (HostArch == llvm::Triple::ppc64) {
1677 static const char *const PPC64LibDirs[] = {
1678 "/lib64/gcc", "/lib/gcc", "/lib64", "/lib"
1679 };
1680 static const char *const PPC64Triples[] = {
1681 "powerpc64-unknown-linux-gnu"
1682 };
1683 LibDirs.append(PPC64LibDirs,
1684 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1685 Triples.append(PPC64Triples,
1686 PPC64Triples + llvm::array_lengthof(PPC64Triples));
1687 }
1688 }
Chandler Carruthe0890882011-10-04 23:17:12 +00001689
1690 void ScanLibDirForGCCTriple(const std::string &LibDir,
1691 StringRef CandidateTriple,
1692 GCCVersion &BestVersion) {
1693 const std::string TripleDir = LibDir + "/" + CandidateTriple.str();
1694 if (!PathExists(TripleDir))
1695 return;
1696
1697 // There are various different suffixes on the triple directory we
1698 // check for. We also record what is necessary to walk from each back
1699 // up to the lib directory.
1700 const std::string Suffixes[] = { "", "/gcc/" + CandidateTriple.str(),
1701 "/gcc/i686-linux-gnu" };
1702 const std::string InstallSuffixes[] = { "/../../..", "/../../../..",
1703 "/../../../.." };
1704 const unsigned NumSuffixes = (llvm::array_lengthof(Suffixes) -
1705 (CandidateTriple != "i386-linux-gnu"));
1706 for (unsigned i = 0; i < NumSuffixes; ++i) {
1707 StringRef Suffix = Suffixes[i];
1708 llvm::error_code EC;
1709 for (llvm::sys::fs::directory_iterator LI(TripleDir + Suffix, EC), LE;
1710 !EC && LI != LE; LI = LI.increment(EC)) {
1711 StringRef VersionText = llvm::sys::path::filename(LI->path());
1712 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1713 static const GCCVersion MinVersion = { 4, 1, 1 };
1714 if (CandidateVersion < MinVersion)
1715 continue;
1716 if (CandidateVersion <= BestVersion)
1717 continue;
1718 if (!PathExists(LI->path() + "/crtbegin.o"))
1719 continue;
1720
1721 BestVersion = CandidateVersion;
1722 GccTriple = CandidateTriple.str();
1723 // FIXME: We hack together the directory name here instead of
1724 // using LI to ensure stable path separators across Windows and
1725 // Linux.
1726 GccInstallPath = TripleDir + Suffixes[i] + "/" + VersionText.str();
1727 GccParentLibPath = GccInstallPath + InstallSuffixes[i];
1728 IsValid = true;
1729 }
1730 }
1731 }
Chandler Carruth048e6492011-10-03 18:16:54 +00001732};
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001733}
1734
Chandler Carruthd2deee12011-10-03 05:28:29 +00001735static void addPathIfExists(const std::string &Path,
1736 ToolChain::path_list &Paths) {
Chandler Carruth048e6492011-10-03 18:16:54 +00001737 if (PathExists(Path)) Paths.push_back(Path);
Chandler Carruthd2deee12011-10-03 05:28:29 +00001738}
1739
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001740Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001741 : Generic_ELF(Host, Triple) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001742 llvm::Triple::ArchType Arch =
1743 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00001744 const std::string &SysRoot = getDriver().SysRoot;
Chandler Carruth048e6492011-10-03 18:16:54 +00001745 GCCInstallationDetector GCCInstallation(getDriver());
Rafael Espindolac1da9812010-11-07 20:14:31 +00001746
Rafael Espindolaab784082011-09-01 16:25:49 +00001747 // OpenSuse stores the linker with the compiler, add that to the search
1748 // path.
1749 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth048e6492011-10-03 18:16:54 +00001750 PPaths.push_back(GCCInstallation.getParentLibPath() + "/../" +
1751 GCCInstallation.getTriple() + "/bin");
Rafael Espindolaab784082011-09-01 16:25:49 +00001752
1753 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00001754
1755 LinuxDistro Distro = DetectLinuxDistro(Arch);
1756
Chris Lattner64a89172011-05-22 16:45:07 +00001757 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00001758 ExtraOpts.push_back("-z");
1759 ExtraOpts.push_back("relro");
1760 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001761
Douglas Gregorf0594d82011-03-06 19:11:49 +00001762 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001763 ExtraOpts.push_back("-X");
1764
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001765 if (IsRedhat(Distro) || IsOpenSuse(Distro) || Distro == UbuntuMaverick ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001766 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001767 ExtraOpts.push_back("--hash-style=gnu");
1768
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001769 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
Chris Lattner64a89172011-05-22 16:45:07 +00001770 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001771 ExtraOpts.push_back("--hash-style=both");
1772
Chris Lattnerd753b562011-05-22 05:36:06 +00001773 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001774 ExtraOpts.push_back("--no-add-needed");
1775
Eli Friedman0b200f62011-06-02 21:36:53 +00001776 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001777 IsOpenSuse(Distro) ||
1778 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
1779 Distro == UbuntuLucid ||
Eli Friedman0b200f62011-06-02 21:36:53 +00001780 Distro == UbuntuMaverick || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001781 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001782 ExtraOpts.push_back("--build-id");
1783
Chris Lattner64a89172011-05-22 16:45:07 +00001784 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00001785 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00001786
Chandler Carruthd2deee12011-10-03 05:28:29 +00001787 // The selection of paths to try here is designed to match the patterns which
1788 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
1789 // This was determined by running GCC in a fake filesystem, creating all
1790 // possible permutations of these directories, and seeing which ones it added
1791 // to the link paths.
1792 path_list &Paths = getFilePaths();
1793 const bool Is32Bits = (getArch() == llvm::Triple::x86 ||
1794 getArch() == llvm::Triple::ppc);
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001795
Chandler Carruthd2deee12011-10-03 05:28:29 +00001796 const std::string Suffix32 = Arch == llvm::Triple::x86_64 ? "/32" : "";
Chandler Carruth38ec5462011-10-03 09:00:50 +00001797 const std::string Suffix64 = Arch == llvm::Triple::x86_64 ? "" : "/64";
Chandler Carruthd2deee12011-10-03 05:28:29 +00001798 const std::string Suffix = Is32Bits ? Suffix32 : Suffix64;
1799 const std::string Multilib = Is32Bits ? "lib32" : "lib64";
1800
1801 // FIXME: Because we add paths only when they exist on the system, I think we
1802 // should remove the concept of 'HasMultilib'. It's more likely to break the
1803 // behavior than to preserve any useful invariant on the system.
Rafael Espindolac1da9812010-11-07 20:14:31 +00001804 if (HasMultilib(Arch, Distro)) {
Chandler Carruthd2deee12011-10-03 05:28:29 +00001805 // Add the multilib suffixed paths.
Chandler Carruth048e6492011-10-03 18:16:54 +00001806 if (GCCInstallation.isValid()) {
1807 const std::string &LibPath = GCCInstallation.getParentLibPath();
1808 const std::string &GccTriple = GCCInstallation.getTriple();
1809 // FIXME: This OpenSuse-specific path shouldn't be needed any more, but
1810 // I don't want to remove it without finding someone to test.
1811 if (IsOpenSuse(Distro) && Is32Bits)
1812 Paths.push_back(LibPath + "/../" + GccTriple + "/lib/../lib");
1813
1814 addPathIfExists(GCCInstallation.getInstallPath() + Suffix, Paths);
1815 addPathIfExists(LibPath + "/../" + GccTriple + "/lib/../" + Multilib,
1816 Paths);
1817 addPathIfExists(LibPath + "/../" + Multilib, Paths);
Chandler Carruthd2deee12011-10-03 05:28:29 +00001818 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00001819 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
1820 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001821 }
Rafael Espindolac7409a02011-06-03 15:39:42 +00001822
Chandler Carruthd2deee12011-10-03 05:28:29 +00001823 // Add the non-multiplib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00001824 if (GCCInstallation.isValid()) {
1825 const std::string &LibPath = GCCInstallation.getParentLibPath();
1826 const std::string &GccTriple = GCCInstallation.getTriple();
Chandler Carruth663abc92011-10-03 08:02:58 +00001827 if (!Suffix.empty() || !HasMultilib(Arch, Distro))
Chandler Carruth048e6492011-10-03 18:16:54 +00001828 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
1829 addPathIfExists(LibPath + "/../" + GccTriple + "/lib", Paths);
1830 addPathIfExists(LibPath, Paths);
Chandler Carruthd2deee12011-10-03 05:28:29 +00001831 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00001832 addPathIfExists(SysRoot + "/lib", Paths);
1833 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00001834
Chandler Carruth048e6492011-10-03 18:16:54 +00001835 if (GCCInstallation.isValid() && Arch == getArch() && IsUbuntu(Distro))
1836 Paths.push_back(SysRoot + "/usr/lib/" + GCCInstallation.getTriple());
Rafael Espindolac1da9812010-11-07 20:14:31 +00001837}
1838
1839bool Linux::HasNativeLLVMSupport() const {
1840 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00001841}
1842
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001843Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
1844 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001845 Action::ActionClass Key;
1846 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1847 Key = Action::AnalyzeJobClass;
1848 else
1849 Key = JA.getKind();
1850
Rafael Espindoladda5b922010-11-07 23:13:01 +00001851 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1852 options::OPT_no_integrated_as,
1853 IsIntegratedAssemblerDefault());
1854
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001855 Tool *&T = Tools[Key];
1856 if (!T) {
1857 switch (Key) {
1858 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00001859 if (UseIntegratedAs)
1860 T = new tools::ClangAs(*this);
1861 else
1862 T = new tools::linuxtools::Assemble(*this);
1863 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001864 case Action::LinkJobClass:
1865 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001866 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001867 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001868 }
1869 }
1870
1871 return *T;
1872}
1873
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001874/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1875
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001876DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001877 : Generic_ELF(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001878
1879 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001880 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001881 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001882 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001883
Daniel Dunbaree788e72009-12-21 18:54:17 +00001884 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001885 getFilePaths().push_back("/usr/lib");
1886 getFilePaths().push_back("/usr/lib/gcc41");
1887}
1888
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001889Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
1890 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001891 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001892 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001893 Key = Action::AnalyzeJobClass;
1894 else
1895 Key = JA.getKind();
1896
1897 Tool *&T = Tools[Key];
1898 if (!T) {
1899 switch (Key) {
1900 case Action::AssembleJobClass:
1901 T = new tools::dragonfly::Assemble(*this); break;
1902 case Action::LinkJobClass:
1903 T = new tools::dragonfly::Link(*this); break;
1904 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001905 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001906 }
1907 }
1908
1909 return *T;
1910}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001911
1912Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1913 : ToolChain(Host, Triple) {
1914}
1915
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001916Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA,
1917 const ActionList &Inputs) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00001918 Action::ActionClass Key;
1919 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1920 Key = Action::AnalyzeJobClass;
1921 else
1922 Key = JA.getKind();
1923
Chad Rosierc57114a2011-07-20 19:14:30 +00001924 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1925 options::OPT_no_integrated_as,
1926 IsIntegratedAssemblerDefault());
1927
Michael J. Spencerff58e362010-08-21 21:55:07 +00001928 Tool *&T = Tools[Key];
1929 if (!T) {
1930 switch (Key) {
1931 case Action::InputClass:
1932 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001933 case Action::LipoJobClass:
1934 case Action::DsymutilJobClass:
Eric Christopherf8571862011-08-23 17:56:55 +00001935 case Action::VerifyJobClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001936 llvm_unreachable("Invalid tool kind.");
Michael J. Spencerff58e362010-08-21 21:55:07 +00001937 case Action::PreprocessJobClass:
1938 case Action::PrecompileJobClass:
1939 case Action::AnalyzeJobClass:
1940 case Action::CompileJobClass:
1941 T = new tools::Clang(*this); break;
1942 case Action::AssembleJobClass:
Chad Rosierc57114a2011-07-20 19:14:30 +00001943 if (!UseIntegratedAs && getTriple().getEnvironment() == llvm::Triple::MachO)
1944 T = new tools::darwin::Assemble(*this);
1945 else
1946 T = new tools::ClangAs(*this);
1947 break;
Michael J. Spencerff58e362010-08-21 21:55:07 +00001948 case Action::LinkJobClass:
1949 T = new tools::visualstudio::Link(*this); break;
1950 }
1951 }
1952
1953 return *T;
1954}
1955
1956bool Windows::IsIntegratedAssemblerDefault() const {
1957 return true;
1958}
1959
1960bool Windows::IsUnwindTablesDefault() const {
1961 // FIXME: Gross; we should probably have some separate target
1962 // definition, possibly even reusing the one in clang.
1963 return getArchName() == "x86_64";
1964}
1965
1966const char *Windows::GetDefaultRelocationModel() const {
1967 return "static";
1968}
1969
1970const char *Windows::GetForcedPicModel() const {
1971 if (getArchName() == "x86_64")
1972 return "pic";
1973 return 0;
1974}