blob: 35e7d80c295512cb885b74ef34c705ee5b3f8b95 [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"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000022#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000023#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000024#include "clang/Driver/Options.h"
Douglas Gregor34916db2010-09-03 17:16:03 +000025#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000026
Daniel Dunbar00577ad2010-08-23 22:35:37 +000027#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000028#include "llvm/ADT/StringExtras.h"
John McCallf85e1932011-06-15 23:02:42 +000029#include "llvm/ADT/STLExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000030#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000031#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000032#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000033#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000034#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000035#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000036
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000037#include <cstdlib> // ::getenv
38
Daniel Dunbar39176082009-03-20 00:20:03 +000039using namespace clang::driver;
40using namespace clang::driver::toolchains;
41
Daniel Dunbarf3955282009-09-04 18:34:51 +000042/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000043
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000044Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
John McCallf85e1932011-06-15 23:02:42 +000045 : ToolChain(Host, Triple), TargetInitialized(false),
46 ARCRuntimeForSimulator(ARCSimulator_None)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000047{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000048 // Compute the initial Darwin version based on the host.
49 bool HadExtra;
50 std::string OSName = Triple.getOSName();
Daniel Dunbar34f9e292011-02-25 21:20:15 +000051 if (!Driver::GetReleaseVersion(&OSName.c_str()[6],
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000052 DarwinVersion[0], DarwinVersion[1],
53 DarwinVersion[2], HadExtra))
54 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
55
Daniel Dunbar02633b52009-03-26 16:23:12 +000056 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000057 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
58 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000059}
60
Daniel Dunbar41800112010-08-02 05:43:56 +000061types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
62 types::ID Ty = types::lookupTypeForExtension(Ext);
63
64 // Darwin always preprocesses assembly files (unless -x is used explicitly).
65 if (Ty == types::TY_PP_Asm)
66 return types::TY_Asm;
67
68 return Ty;
69}
70
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000071bool Darwin::HasNativeLLVMSupport() const {
72 return true;
73}
74
John McCallf85e1932011-06-15 23:02:42 +000075/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
76bool Darwin::HasARCRuntime() const {
77 // FIXME: Remove this once there is a proper way to detect an ARC runtime
78 // for the simulator.
79 switch (ARCRuntimeForSimulator) {
80 case ARCSimulator_None:
81 break;
82 case ARCSimulator_HasARCRuntime:
83 return true;
84 case ARCSimulator_NoARCRuntime:
85 return false;
86 }
87
88 if (isTargetIPhoneOS())
89 return !isIPhoneOSVersionLT(5);
90 else
91 return !isMacosxVersionLT(10, 7);
92}
93
Daniel Dunbareeff4062010-01-22 02:04:58 +000094// FIXME: Can we tablegen this?
95static const char *GetArmArchForMArch(llvm::StringRef Value) {
96 if (Value == "armv6k")
97 return "armv6";
98
99 if (Value == "armv5tej")
100 return "armv5";
101
102 if (Value == "xscale")
103 return "xscale";
104
105 if (Value == "armv4t")
106 return "armv4t";
107
108 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
109 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
110 Value == "armv7m")
111 return "armv7";
112
113 return 0;
114}
115
116// FIXME: Can we tablegen this?
117static const char *GetArmArchForMCpu(llvm::StringRef Value) {
118 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
119 Value == "arm946e-s" || Value == "arm966e-s" ||
120 Value == "arm968e-s" || Value == "arm10e" ||
121 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
122 Value == "arm1026ej-s")
123 return "armv5";
124
125 if (Value == "xscale")
126 return "xscale";
127
128 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000129 Value == "arm1176jz-s" || Value == "arm1176jzf-s" ||
130 Value == "cortex-m0" )
Daniel Dunbareeff4062010-01-22 02:04:58 +0000131 return "armv6";
132
133 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
134 return "armv7";
135
136 return 0;
137}
138
139llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
140 switch (getTriple().getArch()) {
141 default:
142 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000143
Douglas Gregorf0594d82011-03-06 19:11:49 +0000144 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000145 case llvm::Triple::arm: {
146 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
147 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
148 return Arch;
149
150 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
151 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
152 return Arch;
153
154 return "arm";
155 }
156 }
157}
158
Daniel Dunbarf3955282009-09-04 18:34:51 +0000159Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000160 // Free tool implementations.
161 for (llvm::DenseMap<unsigned, Tool*>::iterator
162 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
163 delete it->second;
164}
165
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000166std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
167 llvm::Triple Triple(ComputeLLVMTriple(Args));
168
169 // If the target isn't initialized (e.g., an unknown Darwin platform, return
170 // the default triple).
171 if (!isTargetInitialized())
172 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000173
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000174 unsigned Version[3];
175 getTargetVersion(Version);
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000176
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000177 llvm::SmallString<16> Str;
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000178 llvm::raw_svector_ostream(Str)
Daniel Dunbar659d23a2011-04-19 23:34:17 +0000179 << (isTargetIPhoneOS() ? "ios" : "macosx")
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000180 << Version[0] << "." << Version[1] << "." << Version[2];
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000181 Triple.setOSName(Str.str());
182
183 return Triple.getTriple();
184}
185
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000186Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
187 const ActionList &Inputs) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000188 Action::ActionClass Key;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000189
190 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
191 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
192 if (Inputs.size() == 1 &&
193 types::isCXX(Inputs[0]->getType()) &&
194 getTriple().getOS() == llvm::Triple::Darwin &&
195 getTriple().getArch() == llvm::Triple::x86 &&
196 C.getArgs().getLastArg(options::OPT_fapple_kext))
197 Key = JA.getKind();
198 else
199 Key = Action::AnalyzeJobClass;
200 } else
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000201 Key = JA.getKind();
202
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000203 // FIXME: This doesn't belong here, but ideally we will support static soon
204 // anyway.
205 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
206 C.getArgs().hasArg(options::OPT_static) ||
207 C.getArgs().hasArg(options::OPT_fapple_kext));
208 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
209 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
210 options::OPT_no_integrated_as,
211 IsIADefault);
212
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000213 Tool *&T = Tools[Key];
214 if (!T) {
215 switch (Key) {
216 case Action::InputClass:
217 case Action::BindArchClass:
218 assert(0 && "Invalid tool kind.");
219 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000220 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000221 case Action::AnalyzeJobClass:
222 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000223 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000224 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000225 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000226 case Action::AssembleJobClass: {
227 if (UseIntegratedAs)
228 T = new tools::ClangAs(*this);
229 else
230 T = new tools::darwin::Assemble(*this);
231 break;
232 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000233 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000234 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000235 case Action::LipoJobClass:
236 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000237 case Action::DsymutilJobClass:
238 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000239 }
240 }
241
242 return *T;
243}
244
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000245
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000246DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
247 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000248{
Daniel Dunbar47023092011-03-18 19:25:15 +0000249 std::string UsrPrefix = "llvm-gcc-4.2/";
250
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000251 getProgramPaths().push_back(getDriver().getInstalledDir());
252 if (getDriver().getInstalledDir() != getDriver().Dir)
253 getProgramPaths().push_back(getDriver().Dir);
254
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000255 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000256 getProgramPaths().push_back(getDriver().getInstalledDir());
257 if (getDriver().getInstalledDir() != getDriver().Dir)
258 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000259
260 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000261 // also add the GCC libexec paths. This is legacy code that can be removed
262 // once fallback is no longer useful.
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000263 std::string ToolChainDir = "i686-apple-darwin";
264 ToolChainDir += llvm::utostr(DarwinVersion[0]);
265 ToolChainDir += "/4.2.1";
266
267 std::string Path = getDriver().Dir;
Daniel Dunbar47023092011-03-18 19:25:15 +0000268 Path += "/../" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000269 Path += ToolChainDir;
270 getProgramPaths().push_back(Path);
271
Daniel Dunbar47023092011-03-18 19:25:15 +0000272 Path = "/usr/" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000273 Path += ToolChainDir;
274 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000275}
276
277void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
278 ArgStringList &CmdArgs) const {
279 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000280
281 // Unfortunately, we still might depend on a few of the libraries that are
282 // only available in the gcc library directory (in particular
283 // libstdc++.dylib). For now, hardcode the path to the known install location.
284 llvm::sys::Path P(getDriver().Dir);
285 P.eraseComponent(); // .../usr/bin -> ../usr
286 P.appendComponent("lib");
287 P.appendComponent("gcc");
288 switch (getTriple().getArch()) {
289 default:
290 assert(0 && "Invalid Darwin arch!");
291 case llvm::Triple::x86:
292 case llvm::Triple::x86_64:
293 P.appendComponent("i686-apple-darwin10");
294 break;
295 case llvm::Triple::arm:
296 case llvm::Triple::thumb:
297 P.appendComponent("arm-apple-darwin10");
298 break;
299 case llvm::Triple::ppc:
300 case llvm::Triple::ppc64:
301 P.appendComponent("powerpc-apple-darwin10");
302 break;
303 }
304 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000305
306 // Determine the arch specific GCC subdirectory.
307 const char *ArchSpecificDir = 0;
308 switch (getTriple().getArch()) {
309 default:
310 break;
311 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000312 case llvm::Triple::thumb: {
313 std::string Triple = ComputeLLVMTriple(Args);
314 llvm::StringRef TripleStr = Triple;
315 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
316 ArchSpecificDir = "v5";
317 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
318 ArchSpecificDir = "v6";
319 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
320 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000321 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000322 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000323 case llvm::Triple::ppc64:
324 ArchSpecificDir = "ppc64";
325 break;
326 case llvm::Triple::x86_64:
327 ArchSpecificDir = "x86_64";
328 break;
329 }
330
331 if (ArchSpecificDir) {
332 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000333 bool Exists;
334 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000335 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
336 P.eraseComponent();
337 }
338
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000339 bool Exists;
340 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000341 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000342}
343
John McCallf85e1932011-06-15 23:02:42 +0000344void DarwinClang::AddLinkARCArgs(const ArgList &Args,
345 ArgStringList &CmdArgs) const {
346
347 CmdArgs.push_back("-force_load");
348 llvm::sys::Path P(getDriver().ClangExecutable);
349 P.eraseComponent(); // 'clang'
350 P.eraseComponent(); // 'bin'
351 P.appendComponent("lib");
352 P.appendComponent("arc");
353 P.appendComponent("libarclite_");
354 std::string s = P.str();
355 // Mash in the platform.
356 if (isTargetIPhoneOS())
357 s += "iphoneos";
358 // FIXME: isTargetIphoneOSSimulator() is not returning true.
359 else if (ARCRuntimeForSimulator != ARCSimulator_None)
360 s += "iphonesimulator";
361 else
362 s += "macosx";
363 s += ".a";
364
365 CmdArgs.push_back(Args.MakeArgString(s));
366}
367
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000368void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
369 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000370 // Darwin doesn't support real static executables, don't link any runtime
371 // libraries with -static.
372 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000373 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000374
375 // Reject -static-libgcc for now, we can deal with this when and if someone
376 // cares. This is useful in situations where someone wants to statically link
377 // something like libstdc++, and needs its runtime support routines.
378 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000379 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000380 << A->getAsString(Args);
381 return;
382 }
383
Daniel Dunbareec99102010-01-22 03:38:14 +0000384 // Otherwise link libSystem, then the dynamic runtime library, and finally any
385 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000386 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000387
388 // Select the dynamic runtime library and the target specific static library.
389 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000390 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000391 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
392 // it never went into the SDK.
393 if (!isTargetIOSSimulator())
394 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000395
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000396 // We currently always need a static runtime library for iOS.
397 DarwinStaticLib = "libclang_rt.ios.a";
Daniel Dunbareec99102010-01-22 03:38:14 +0000398 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000399 // The dynamic runtime library was merged with libSystem for 10.6 and
400 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000401 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000402 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000403 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000404 CmdArgs.push_back("-lgcc_s.10.5");
405
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000406 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000407 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000408 // omitted from 10.4.dylib.
409 //
410 // Unfortunately, that turned out to not be true, because Darwin system
411 // headers can still use eprintf on i386, and it is not exported from
412 // libSystem. Therefore, we still must provide a runtime library just for
413 // the tiny tiny handful of projects that *might* use that symbol.
414 if (isMacosxVersionLT(10, 5)) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000415 DarwinStaticLib = "libclang_rt.10.4.a";
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000416 } else {
417 if (getTriple().getArch() == llvm::Triple::x86)
418 DarwinStaticLib = "libclang_rt.eprintf.a";
419 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000420 }
421
422 /// Add the target specific static library, if needed.
423 if (DarwinStaticLib) {
424 llvm::sys::Path P(getDriver().ResourceDir);
425 P.appendComponent("lib");
426 P.appendComponent("darwin");
427 P.appendComponent(DarwinStaticLib);
428
429 // For now, allow missing resource libraries to support developers who may
430 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000431 bool Exists;
432 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareec99102010-01-22 03:38:14 +0000433 CmdArgs.push_back(Args.MakeArgString(P.str()));
434 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000435}
436
John McCallf85e1932011-06-15 23:02:42 +0000437static inline llvm::StringRef SimulatorVersionDefineName() {
438 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
439}
440
441/// \brief Parse the simulator version define:
442/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
443// and return the grouped values as integers, e.g:
444// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
445// will return Major=4, Minor=2, Micro=1.
446static bool GetVersionFromSimulatorDefine(llvm::StringRef define,
447 unsigned &Major, unsigned &Minor,
448 unsigned &Micro) {
449 assert(define.startswith(SimulatorVersionDefineName()));
450 llvm::StringRef name, version;
451 llvm::tie(name, version) = define.split('=');
452 if (version.empty())
453 return false;
454 std::string verstr = version.str();
455 char *end;
456 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
457 if (*end != '\0')
458 return false;
459 Major = num / 10000;
460 num = num % 10000;
461 Minor = num / 100;
462 Micro = num % 100;
463 return true;
464}
465
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000466void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000467 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000468
Daniel Dunbar26031372010-01-27 00:56:25 +0000469 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000470 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
471 Arg *iOSSimVersion = Args.getLastArg(
472 options::OPT_mios_simulator_version_min_EQ);
John McCallf85e1932011-06-15 23:02:42 +0000473
474 // FIXME: HACK! When compiling for the simulator we don't get a
475 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
476 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
477 // define passed in command-line.
478 if (!iOSVersion) {
479 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
480 ie = Args.filtered_end(); it != ie; ++it) {
481 llvm::StringRef define = (*it)->getValue(Args);
482 if (define.startswith(SimulatorVersionDefineName())) {
483 unsigned Major, Minor, Micro;
484 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
485 Major < 10 && Minor < 100 && Micro < 100) {
486 ARCRuntimeForSimulator = Major < 5 ? ARCSimulator_NoARCRuntime
487 : ARCSimulator_HasARCRuntime;
488 }
489 break;
490 }
491 }
492 }
493
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000494 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000495 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000496 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000497 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
498 iOSVersion = iOSSimVersion = 0;
499 } else if (iOSVersion && iOSSimVersion) {
500 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
501 << iOSVersion->getAsString(Args)
502 << iOSSimVersion->getAsString(Args);
503 iOSSimVersion = 0;
504 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
505 // If not deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000506 // environment defines.
507 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000508 const char *iOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
509 const char *iOSSimTarget = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000510
Daniel Dunbar816bc312010-01-26 01:45:19 +0000511 // Ignore empty strings.
512 if (OSXTarget && OSXTarget[0] == '\0')
513 OSXTarget = 0;
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000514 if (iOSTarget && iOSTarget[0] == '\0')
515 iOSTarget = 0;
516 if (iOSSimTarget && iOSSimTarget[0] == '\0')
517 iOSSimTarget = 0;
Daniel Dunbar816bc312010-01-26 01:45:19 +0000518
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000519 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000520 //
521 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000522
523 // Do not allow conflicts with the iOS simulator target.
524 if (iOSSimTarget && (OSXTarget || iOSTarget)) {
525 getDriver().Diag(clang::diag::err_drv_conflicting_deployment_targets)
526 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
527 << (OSXTarget ? "MACOSX_DEPLOYMENT_TARGET" :
528 "IPHONEOS_DEPLOYMENT_TARGET");
529 }
530
531 // Allow conflicts among OSX and iOS for historical reasons, but choose the
532 // default platform.
533 if (OSXTarget && iOSTarget) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000534 if (getTriple().getArch() == llvm::Triple::arm ||
535 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000536 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000537 else
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000538 iOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000539 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000540
Daniel Dunbar39053672010-02-02 17:31:12 +0000541 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000542 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000543 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
544 Args.append(OSXVersion);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000545 } else if (iOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000546 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000547 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
548 Args.append(iOSVersion);
549 } else if (iOSSimTarget) {
550 const Option *O = Opts.getOption(
551 options::OPT_mios_simulator_version_min_EQ);
552 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
553 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000554 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000555 // Otherwise, assume we are targeting OS X.
556 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000557 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
558 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000559 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000560 }
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000562 // Reject invalid architecture combinations.
563 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
564 getTriple().getArch() != llvm::Triple::x86_64)) {
565 getDriver().Diag(clang::diag::err_drv_invalid_arch_for_deployment_target)
566 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
567 }
568
Daniel Dunbar26031372010-01-27 00:56:25 +0000569 // Set the tool chain target information.
570 unsigned Major, Minor, Micro;
571 bool HadExtra;
572 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000573 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar26031372010-01-27 00:56:25 +0000574 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
575 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000576 Major != 10 || Minor >= 100 || Micro >= 100)
Daniel Dunbar26031372010-01-27 00:56:25 +0000577 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
578 << OSXVersion->getAsString(Args);
579 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000580 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
581 assert(Version && "Unknown target platform!");
582 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000583 Micro, HadExtra) || HadExtra ||
584 Major >= 10 || Minor >= 100 || Micro >= 100)
585 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000586 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000587 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000588
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000589 bool IsIOSSim = bool(iOSSimVersion);
590
591 // In GCC, the simulator historically was treated as being OS X in some
592 // contexts, like determining the link logic, despite generally being called
593 // with an iOS deployment target. For compatibility, we detect the
594 // simulator as iOS + x86, and treat it differently in a few contexts.
595 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
596 getTriple().getArch() == llvm::Triple::x86_64))
597 IsIOSSim = true;
598
599 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000600}
601
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000602void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000603 ArgStringList &CmdArgs) const {
604 CXXStdlibType Type = GetCXXStdlibType(Args);
605
606 switch (Type) {
607 case ToolChain::CST_Libcxx:
608 CmdArgs.push_back("-lc++");
609 break;
610
611 case ToolChain::CST_Libstdcxx: {
612 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
613 // it was previously found in the gcc lib dir. However, for all the Darwin
614 // platforms we care about it was -lstdc++.6, so we search for that
615 // explicitly if we can't see an obvious -lstdc++ candidate.
616
617 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000618 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000619 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
620 llvm::sys::Path P(A->getValue(Args));
621 P.appendComponent("usr");
622 P.appendComponent("lib");
623 P.appendComponent("libstdc++.dylib");
624
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000625 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000626 P.eraseComponent();
627 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000628 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000629 CmdArgs.push_back(Args.MakeArgString(P.str()));
630 return;
631 }
632 }
633 }
634
635 // Otherwise, look in the root.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000636 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
637 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000638 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
639 return;
640 }
641
642 // Otherwise, let the linker search.
643 CmdArgs.push_back("-lstdc++");
644 break;
645 }
646 }
647}
648
Shantonu Sen7433fed2010-09-17 18:39:08 +0000649void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
650 ArgStringList &CmdArgs) const {
651
652 // For Darwin platforms, use the compiler-rt-based support library
653 // instead of the gcc-provided one (which is also incidentally
654 // only present in the gcc lib dir, which makes it hard to find).
655
656 llvm::sys::Path P(getDriver().ResourceDir);
657 P.appendComponent("lib");
658 P.appendComponent("darwin");
659 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000660
Shantonu Sen7433fed2010-09-17 18:39:08 +0000661 // For now, allow missing resource libraries to support developers who may
662 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000663 bool Exists;
664 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000665 CmdArgs.push_back(Args.MakeArgString(P.str()));
666}
667
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000668DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
669 const char *BoundArch) const {
670 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
671 const OptTable &Opts = getDriver().getOpts();
672
673 // FIXME: We really want to get out of the tool chain level argument
674 // translation business, as it makes the driver functionality much
675 // more opaque. For now, we follow gcc closely solely for the
676 // purpose of easily achieving feature parity & testability. Once we
677 // have something that works, we should reevaluate each translation
678 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000679
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000680 for (ArgList::const_iterator it = Args.begin(),
681 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000682 Arg *A = *it;
683
684 if (A->getOption().matches(options::OPT_Xarch__)) {
685 // FIXME: Canonicalize name.
686 if (getArchName() != A->getValue(Args, 0))
687 continue;
688
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000689 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000690 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
691 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000692 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000694 // If the argument parsing failed or more than one argument was
695 // consumed, the -Xarch_ argument's parameter tried to consume
696 // extra arguments. Emit an error and ignore.
697 //
698 // We also want to disallow any options which would alter the
699 // driver behavior; that isn't going to work in our model. We
700 // use isDriverOption() as an approximation, although things
701 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000702 if (!XarchArg || Index > Prev + 1) {
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000703 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_with_args)
704 << A->getAsString(Args);
705 continue;
706 } else if (XarchArg->getOption().isDriverOption()) {
707 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000708 << A->getAsString(Args);
709 continue;
710 }
711
Daniel Dunbar478edc22009-03-29 22:29:05 +0000712 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000713 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000714
715 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000716
717 // Linker input arguments require custom handling. The problem is that we
718 // have already constructed the phase actions, so we can not treat them as
719 // "input arguments".
720 if (A->getOption().isLinkerInput()) {
721 // Convert the argument into individual Zlinker_input_args.
722 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
723 DAL->AddSeparateArg(OriginalArg,
724 Opts.getOption(options::OPT_Zlinker_input),
725 A->getValue(Args, i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000726
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000727 }
728 continue;
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000731
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000732 // Sob. These is strictly gcc compatible for the time being. Apple
733 // gcc translates options twice, which means that self-expanding
734 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000735 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000736 default:
737 DAL->append(A);
738 break;
739
740 case options::OPT_mkernel:
741 case options::OPT_fapple_kext:
742 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000743 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
744 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000745 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000747 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000748 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
749 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000750 break;
751
752 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000753 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
754 DAL->AddFlagArg(A,
755 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000756 break;
757
758 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000759 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
760 DAL->AddFlagArg(A,
761 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000762 break;
763
764 case options::OPT_fterminated_vtables:
765 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000766 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
767 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000768 break;
769
770 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000771 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000772 break;
773
774 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000775 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000776 break;
777
778 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000779 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000780 break;
781
782 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000783 DAL->AddFlagArg(A,
784 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000785 break;
786
787 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000788 DAL->AddFlagArg(A,
789 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000790 break;
791
792 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000793 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000794 break;
795
796 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000797 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000798 break;
799 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000800 }
801
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000802 if (getTriple().getArch() == llvm::Triple::x86 ||
803 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000804 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000805 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000806
807 // Add the arch options based on the particular spelling of -arch, to match
808 // how the driver driver works.
809 if (BoundArch) {
810 llvm::StringRef Name = BoundArch;
811 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
812 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
813
814 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
815 // which defines the list of which architectures we accept.
816 if (Name == "ppc")
817 ;
818 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000819 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000820 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000821 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000822 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000823 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000824 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000825 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000826 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000827 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000828 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000829 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000830 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000831 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000832 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000833 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000834
835 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000836 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000837
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000838 else if (Name == "i386")
839 ;
840 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000841 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000842 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000843 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000844 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000845 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000846 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000847 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000848 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000849 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000850 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000851 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000852 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000853 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000854
855 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000856 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000857
858 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000859 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000860 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000861 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000862 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000863 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000864 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000865 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000866 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000867 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000868 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000869 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000870
871 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000872 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000873 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000874
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000875 // Add an explicit version min argument for the deployment target. We do this
876 // after argument translation because -Xarch_ arguments may add a version min
877 // argument.
878 AddDeploymentTarget(*DAL);
879
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000880 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000881}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000882
Daniel Dunbarf3955282009-09-04 18:34:51 +0000883bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000884 // FIXME: Gross; we should probably have some separate target
885 // definition, possibly even reusing the one in clang.
886 return getArchName() == "x86_64";
887}
888
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000889bool Darwin::UseDwarfDebugFlags() const {
890 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
891 return S[0] != '\0';
892 return false;
893}
894
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000895bool Darwin::UseSjLjExceptions() const {
896 // Darwin uses SjLj exceptions on ARM.
897 return (getTriple().getArch() == llvm::Triple::arm ||
898 getTriple().getArch() == llvm::Triple::thumb);
899}
900
Daniel Dunbarf3955282009-09-04 18:34:51 +0000901const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000902 return "pic";
903}
904
Daniel Dunbarf3955282009-09-04 18:34:51 +0000905const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000906 if (getArchName() == "x86_64")
907 return "pic";
908 return 0;
909}
910
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000911bool Darwin::SupportsProfiling() const {
912 // Profiling instrumentation is only supported on x86.
913 return getArchName() == "i386" || getArchName() == "x86_64";
914}
915
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000916bool Darwin::SupportsObjCGC() const {
917 // Garbage collection is supported everywhere except on iPhone OS.
918 return !isTargetIPhoneOS();
919}
920
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000921std::string
922Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
923 return ComputeLLVMTriple(Args);
924}
925
Daniel Dunbar39176082009-03-20 00:20:03 +0000926/// Generic_GCC - A tool chain using the 'gcc' command to perform
927/// all subcommands; this relies on gcc translating the majority of
928/// command line options.
929
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000930Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000931 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000932 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +0000933 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000934 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000935}
936
Daniel Dunbar39176082009-03-20 00:20:03 +0000937Generic_GCC::~Generic_GCC() {
938 // Free tool implementations.
939 for (llvm::DenseMap<unsigned, Tool*>::iterator
940 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
941 delete it->second;
942}
943
Mike Stump1eb44332009-09-09 15:08:12 +0000944Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000945 const JobAction &JA,
946 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +0000947 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000948 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000949 Key = Action::AnalyzeJobClass;
950 else
951 Key = JA.getKind();
952
953 Tool *&T = Tools[Key];
954 if (!T) {
955 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000956 case Action::InputClass:
957 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000958 assert(0 && "Invalid tool kind.");
959 case Action::PreprocessJobClass:
960 T = new tools::gcc::Preprocess(*this); break;
961 case Action::PrecompileJobClass:
962 T = new tools::gcc::Precompile(*this); break;
963 case Action::AnalyzeJobClass:
964 T = new tools::Clang(*this); break;
965 case Action::CompileJobClass:
966 T = new tools::gcc::Compile(*this); break;
967 case Action::AssembleJobClass:
968 T = new tools::gcc::Assemble(*this); break;
969 case Action::LinkJobClass:
970 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000972 // This is a bit ungeneric, but the only platform using a driver
973 // driver is Darwin.
974 case Action::LipoJobClass:
975 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000976 case Action::DsymutilJobClass:
977 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000978 }
979 }
980
981 return *T;
982}
983
Daniel Dunbar39176082009-03-20 00:20:03 +0000984bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000985 // FIXME: Gross; we should probably have some separate target
986 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000987 return getArchName() == "x86_64";
988}
989
990const char *Generic_GCC::GetDefaultRelocationModel() const {
991 return "static";
992}
993
994const char *Generic_GCC::GetForcedPicModel() const {
995 return 0;
996}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000997
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000998/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
999/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1000/// Currently does not support anything else but compilation.
1001
1002TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
1003 : ToolChain(Host, Triple) {
1004 // Path mangling to find libexec
1005 std::string Path(getDriver().Dir);
1006
1007 Path += "/../libexec";
1008 getProgramPaths().push_back(Path);
1009}
1010
1011TCEToolChain::~TCEToolChain() {
1012 for (llvm::DenseMap<unsigned, Tool*>::iterator
1013 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1014 delete it->second;
1015}
1016
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001017bool TCEToolChain::IsMathErrnoDefault() const {
1018 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001019}
1020
1021bool TCEToolChain::IsUnwindTablesDefault() const {
1022 return false;
1023}
1024
1025const char *TCEToolChain::GetDefaultRelocationModel() const {
1026 return "static";
1027}
1028
1029const char *TCEToolChain::GetForcedPicModel() const {
1030 return 0;
1031}
1032
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001033Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001034 const JobAction &JA,
1035 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001036 Action::ActionClass Key;
1037 Key = Action::AnalyzeJobClass;
1038
1039 Tool *&T = Tools[Key];
1040 if (!T) {
1041 switch (Key) {
1042 case Action::PreprocessJobClass:
1043 T = new tools::gcc::Preprocess(*this); break;
1044 case Action::AnalyzeJobClass:
1045 T = new tools::Clang(*this); break;
1046 default:
1047 assert(false && "Unsupported action for TCE target.");
1048 }
1049 }
1050 return *T;
1051}
1052
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001053/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1054
1055OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001056 : Generic_ELF(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001057 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001058 getFilePaths().push_back("/usr/lib");
1059}
1060
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001061Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1062 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001063 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001064 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001065 Key = Action::AnalyzeJobClass;
1066 else
1067 Key = JA.getKind();
1068
Rafael Espindoladda5b922010-11-07 23:13:01 +00001069 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1070 options::OPT_no_integrated_as,
1071 IsIntegratedAssemblerDefault());
1072
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001073 Tool *&T = Tools[Key];
1074 if (!T) {
1075 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001076 case Action::AssembleJobClass: {
1077 if (UseIntegratedAs)
1078 T = new tools::ClangAs(*this);
1079 else
1080 T = new tools::openbsd::Assemble(*this);
1081 break;
1082 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001083 case Action::LinkJobClass:
1084 T = new tools::openbsd::Link(*this); break;
1085 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001086 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001087 }
1088 }
1089
1090 return *T;
1091}
1092
Daniel Dunbar75358d22009-03-30 21:06:03 +00001093/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1094
Daniel Dunbar214afe92010-08-02 05:43:59 +00001095FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001096 : Generic_ELF(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001097
1098 // Determine if we are compiling 32-bit code on an x86_64 platform.
1099 bool Lib32 = false;
1100 if (Triple.getArch() == llvm::Triple::x86 &&
1101 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1102 llvm::Triple::x86_64)
1103 Lib32 = true;
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001104
Roman Divacky3393cef2011-06-04 07:37:31 +00001105 if (Triple.getArch() == llvm::Triple::ppc &&
1106 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1107 llvm::Triple::ppc64)
1108 Lib32 = true;
1109
Daniel Dunbarbc534662009-04-02 18:30:04 +00001110 if (Lib32) {
Daniel Dunbarbc534662009-04-02 18:30:04 +00001111 getFilePaths().push_back("/usr/lib32");
1112 } else {
Daniel Dunbarbc534662009-04-02 18:30:04 +00001113 getFilePaths().push_back("/usr/lib");
1114 }
Daniel Dunbar75358d22009-03-30 21:06:03 +00001115}
1116
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001117Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1118 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001119 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001120 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001121 Key = Action::AnalyzeJobClass;
1122 else
1123 Key = JA.getKind();
1124
Roman Divacky67dece72010-11-08 17:46:39 +00001125 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1126 options::OPT_no_integrated_as,
1127 IsIntegratedAssemblerDefault());
1128
Daniel Dunbar75358d22009-03-30 21:06:03 +00001129 Tool *&T = Tools[Key];
1130 if (!T) {
1131 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001132 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001133 if (UseIntegratedAs)
1134 T = new tools::ClangAs(*this);
1135 else
1136 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001137 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001138 case Action::LinkJobClass:
1139 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001140 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001141 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001142 }
1143 }
1144
1145 return *T;
1146}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001147
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001148/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1149
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001150NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
1151 const llvm::Triple& ToolTriple)
1152 : Generic_ELF(Host, Triple), ToolTriple(ToolTriple) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001153
1154 // Determine if we are compiling 32-bit code on an x86_64 platform.
1155 bool Lib32 = false;
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001156 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
1157 Triple.getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001158 Lib32 = true;
1159
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001160 if (getDriver().UseStdLib) {
1161 if (Lib32)
1162 getFilePaths().push_back("=/usr/lib/i386");
1163 else
1164 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001165 }
1166}
1167
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001168Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1169 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001170 Action::ActionClass Key;
1171 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1172 Key = Action::AnalyzeJobClass;
1173 else
1174 Key = JA.getKind();
1175
1176 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1177 options::OPT_no_integrated_as,
1178 IsIntegratedAssemblerDefault());
1179
1180 Tool *&T = Tools[Key];
1181 if (!T) {
1182 switch (Key) {
1183 case Action::AssembleJobClass:
1184 if (UseIntegratedAs)
1185 T = new tools::ClangAs(*this);
1186 else
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001187 T = new tools::netbsd::Assemble(*this, ToolTriple);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001188 break;
1189 case Action::LinkJobClass:
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001190 T = new tools::netbsd::Link(*this, ToolTriple);
1191 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001192 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001193 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001194 }
1195 }
1196
1197 return *T;
1198}
1199
Chris Lattner38e317d2010-07-07 16:01:42 +00001200/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1201
1202Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1203 : Generic_GCC(Host, Triple) {
1204 getFilePaths().push_back(getDriver().Dir + "/../lib");
1205 getFilePaths().push_back("/usr/lib");
1206 getFilePaths().push_back("/usr/gnu/lib");
1207 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1208}
1209
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001210Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1211 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001212 Action::ActionClass Key;
1213 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1214 Key = Action::AnalyzeJobClass;
1215 else
1216 Key = JA.getKind();
1217
1218 Tool *&T = Tools[Key];
1219 if (!T) {
1220 switch (Key) {
1221 case Action::AssembleJobClass:
1222 T = new tools::minix::Assemble(*this); break;
1223 case Action::LinkJobClass:
1224 T = new tools::minix::Link(*this); break;
1225 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001226 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001227 }
1228 }
1229
1230 return *T;
1231}
1232
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001233/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1234
1235AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1236 : Generic_GCC(Host, Triple) {
1237
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001238 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001239 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001240 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001241
Daniel Dunbaree788e72009-12-21 18:54:17 +00001242 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001243 getFilePaths().push_back("/usr/lib");
1244 getFilePaths().push_back("/usr/sfw/lib");
1245 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001246 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001247
1248}
1249
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001250Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1251 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001252 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001253 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001254 Key = Action::AnalyzeJobClass;
1255 else
1256 Key = JA.getKind();
1257
1258 Tool *&T = Tools[Key];
1259 if (!T) {
1260 switch (Key) {
1261 case Action::AssembleJobClass:
1262 T = new tools::auroraux::Assemble(*this); break;
1263 case Action::LinkJobClass:
1264 T = new tools::auroraux::Link(*this); break;
1265 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001266 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001267 }
1268 }
1269
1270 return *T;
1271}
1272
1273
Eli Friedman6b3454a2009-05-26 07:52:18 +00001274/// Linux toolchain (very bare-bones at the moment).
1275
Rafael Espindolac1da9812010-11-07 20:14:31 +00001276enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001277 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001278 DebianLenny,
1279 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001280 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001281 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001282 RHEL4,
1283 RHEL5,
1284 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001285 Fedora13,
1286 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001287 Fedora15,
1288 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001289 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001290 OpenSuse11_4,
1291 OpenSuse12_1,
Douglas Gregor814638e2011-03-14 15:39:50 +00001292 UbuntuHardy,
1293 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001294 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001295 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001296 UbuntuLucid,
1297 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001298 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001299 UbuntuOneiric,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001300 UnknownDistro
1301};
1302
Chris Lattnerd753b562011-05-22 05:36:06 +00001303static bool IsRedhat(enum LinuxDistro Distro) {
Eric Christopher8f1cc072011-04-06 18:22:53 +00001304 return Distro == Fedora13 || Distro == Fedora14 ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001305 Distro == Fedora15 || Distro == FedoraRawhide ||
1306 Distro == RHEL4 || Distro == RHEL5 || Distro == RHEL6;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001307}
1308
1309static bool IsOpenSuse(enum LinuxDistro Distro) {
David Chisnallde5c0482011-05-19 13:26:33 +00001310 return Distro == OpenSuse11_3 || Distro == OpenSuse11_4 ||
1311 Distro == OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001312}
1313
1314static bool IsDebian(enum LinuxDistro Distro) {
Eli Friedman0b200f62011-06-02 21:36:53 +00001315 return Distro == DebianLenny || Distro == DebianSqueeze ||
1316 Distro == DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001317}
1318
1319static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001320 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001321 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Ted Kremenek43ac2972011-04-05 22:04:27 +00001322 Distro == UbuntuJaunty || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001323 Distro == UbuntuNatty || Distro == UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001324}
1325
1326static bool IsDebianBased(enum LinuxDistro Distro) {
1327 return IsDebian(Distro) || IsUbuntu(Distro);
1328}
1329
1330static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001331 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001332 bool Exists;
1333 if (Distro == Exherbo &&
1334 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001335 return false;
1336
Rafael Espindolac1da9812010-11-07 20:14:31 +00001337 return true;
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001338 }
Ted Kremenek43ac2972011-04-05 22:04:27 +00001339 if (Arch == llvm::Triple::ppc64)
1340 return true;
Eric Christopher9af535a2011-06-03 13:28:31 +00001341 if ((Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) &&
1342 IsDebianBased(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001343 return true;
1344 return false;
1345}
1346
1347static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001348 llvm::OwningPtr<llvm::MemoryBuffer> File;
1349 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
1350 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001351 llvm::SmallVector<llvm::StringRef, 8> Lines;
1352 Data.split(Lines, "\n");
1353 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001354 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1355 return UbuntuHardy;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001356 else if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1357 return UbuntuIntrepid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001358 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001359 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001360 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1361 return UbuntuKarmic;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001362 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1363 return UbuntuLucid;
1364 else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1365 return UbuntuMaverick;
1366 else if (Lines[i] == "DISTRIB_CODENAME=natty")
1367 return UbuntuNatty;
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001368 else if (Lines[i] == "DISTRIB_CODENAME=oneiric")
1369 return UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001370 }
1371 return UnknownDistro;
1372 }
1373
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001374 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1375 llvm::StringRef Data = File.get()->getBuffer();
Eric Christopher8f1cc072011-04-06 18:22:53 +00001376 if (Data.startswith("Fedora release 15"))
1377 return Fedora15;
1378 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001379 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001380 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001381 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001382 else if (Data.startswith("Fedora release") &&
1383 Data.find("Rawhide") != llvm::StringRef::npos)
1384 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001385 else if (Data.startswith("Red Hat Enterprise Linux") &&
1386 Data.find("release 6") != llvm::StringRef::npos)
1387 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001388 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1389 Data.startswith("CentOS")) &&
Chris Lattnerd753b562011-05-22 05:36:06 +00001390 Data.find("release 5") != llvm::StringRef::npos)
1391 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001392 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1393 Data.startswith("CentOS")) &&
Chris Lattnerd753b562011-05-22 05:36:06 +00001394 Data.find("release 4") != llvm::StringRef::npos)
1395 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001396 return UnknownDistro;
1397 }
1398
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001399 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1400 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001401 if (Data[0] == '5')
1402 return DebianLenny;
1403 else if (Data.startswith("squeeze/sid"))
1404 return DebianSqueeze;
Eli Friedman0b200f62011-06-02 21:36:53 +00001405 else if (Data.startswith("wheezy/sid"))
1406 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001407 return UnknownDistro;
1408 }
1409
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001410 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1411 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001412 if (Data.startswith("openSUSE 11.3"))
1413 return OpenSuse11_3;
David Chisnallde5c0482011-05-19 13:26:33 +00001414 else if (Data.startswith("openSUSE 11.4"))
1415 return OpenSuse11_4;
1416 else if (Data.startswith("openSUSE 12.1"))
1417 return OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001418 return UnknownDistro;
1419 }
1420
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001421 bool Exists;
1422 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001423 return Exherbo;
1424
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001425 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1426 return ArchLinux;
1427
Rafael Espindolac1da9812010-11-07 20:14:31 +00001428 return UnknownDistro;
1429}
1430
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001431static std::string findGCCBaseLibDir(const std::string &GccTriple) {
Chandler Carruth169dd312011-06-08 10:14:38 +00001432 // FIXME: Using CXX_INCLUDE_ROOT is here is a bit of a hack, but
1433 // avoids adding yet another option to configure/cmake.
1434 // It would probably be cleaner to break it in two variables
1435 // CXX_GCC_ROOT with just /foo/bar
1436 // CXX_GCC_VER with 4.5.2
1437 // Then we would have
1438 // CXX_INCLUDE_ROOT = CXX_GCC_ROOT/include/c++/CXX_GCC_VER
1439 // and this function would return
1440 // CXX_GCC_ROOT/lib/gcc/CXX_INCLUDE_ARCH/CXX_GCC_VER
1441 llvm::SmallString<128> CxxIncludeRoot(CXX_INCLUDE_ROOT);
1442 if (CxxIncludeRoot != "") {
1443 // This is of the form /foo/bar/include/c++/4.5.2/
1444 if (CxxIncludeRoot.back() == '/')
1445 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the /
1446 llvm::StringRef Version = llvm::sys::path::filename(CxxIncludeRoot);
1447 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the version
1448 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the c++
1449 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the include
1450 std::string ret(CxxIncludeRoot.c_str());
1451 ret.append("/lib/gcc/");
1452 ret.append(CXX_INCLUDE_ARCH);
1453 ret.append("/");
1454 ret.append(Version);
1455 return ret;
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001456 }
1457 static const char* GccVersions[] = {"4.6.0", "4.6",
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001458 "4.5.2", "4.5.1", "4.5",
1459 "4.4.5", "4.4.4", "4.4.3", "4.4",
1460 "4.3.4", "4.3.3", "4.3.2", "4.3",
1461 "4.2.4", "4.2.3", "4.2.2", "4.2.1",
1462 "4.2", "4.1.1"};
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001463 bool Exists;
1464 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1465 std::string Suffix = GccTriple + "/" + GccVersions[i];
1466 std::string t1 = "/usr/lib/gcc/" + Suffix;
1467 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists)
1468 return t1;
1469 std::string t2 = "/usr/lib64/gcc/" + Suffix;
1470 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists)
1471 return t2;
1472 std::string t3 = "/usr/lib/" + GccTriple + "/gcc/" + Suffix;
1473 if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists)
1474 return t3;
1475 }
1476 return "";
1477}
1478
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001479Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001480 : Generic_ELF(Host, Triple) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001481 llvm::Triple::ArchType Arch =
1482 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Daniel Dunbara9822de2009-08-06 01:47:11 +00001483
Rafael Espindolac1da9812010-11-07 20:14:31 +00001484 std::string Suffix32 = "";
1485 if (Arch == llvm::Triple::x86_64)
1486 Suffix32 = "/32";
Daniel Dunbara9822de2009-08-06 01:47:11 +00001487
Rafael Espindolac1da9812010-11-07 20:14:31 +00001488 std::string Suffix64 = "";
Ted Kremenek43ac2972011-04-05 22:04:27 +00001489 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001490 Suffix64 = "/64";
1491
1492 std::string Lib32 = "lib";
1493
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001494 bool Exists;
1495 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001496 Lib32 = "lib32";
1497
1498 std::string Lib64 = "lib";
Michael J. Spencer2dea7c72011-01-12 23:54:48 +00001499 bool Symlink;
Chris Lattner48aef362011-01-13 01:35:58 +00001500 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1501 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001502 Lib64 = "lib64";
1503
1504 std::string GccTriple = "";
Douglas Gregorf0594d82011-03-06 19:11:49 +00001505 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001506 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1507 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001508 GccTriple = "arm-linux-gnueabi";
1509 } else if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001510 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1511 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001512 GccTriple = "x86_64-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001513 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1514 Exists) && Exists)
Rafael Espindola53dd00b2010-11-17 00:25:26 +00001515 GccTriple = "x86_64-unknown-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001516 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1517 Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001518 GccTriple = "x86_64-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001519 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1520 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001521 GccTriple = "x86_64-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001522 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1523 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001524 GccTriple = "x86_64-suse-linux";
Nick Lewyckye10f9002011-02-01 23:03:29 +00001525 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1526 Exists) && Exists)
1527 GccTriple = "x86_64-manbo-linux-gnu";
Nico Weber80585d82011-04-25 03:17:35 +00001528 else if (!llvm::sys::fs::exists("/usr/lib/x86_64-linux-gnu/gcc",
1529 Exists) && Exists)
1530 GccTriple = "x86_64-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001531 } else if (Arch == llvm::Triple::x86) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001532 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001533 GccTriple = "i686-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001534 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1535 Exists)
Nuno Lopes2a69ddd2010-11-19 17:26:57 +00001536 GccTriple = "i686-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001537 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1538 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001539 GccTriple = "i486-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001540 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1541 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001542 GccTriple = "i686-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001543 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1544 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001545 GccTriple = "i586-suse-linux";
Ted Kremenekd130c7d2011-04-18 17:50:19 +00001546 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-slackware-linux", Exists)
1547 && Exists)
1548 GccTriple = "i486-slackware-linux";
Ted Kremenek43ac2972011-04-05 22:04:27 +00001549 } else if (Arch == llvm::Triple::ppc) {
1550 if (!llvm::sys::fs::exists("/usr/lib/powerpc-linux-gnu", Exists) && Exists)
1551 GccTriple = "powerpc-linux-gnu";
Eric Christopher9af535a2011-06-03 13:28:31 +00001552 else if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc-unknown-linux-gnu",
1553 Exists) && Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001554 GccTriple = "powerpc-unknown-linux-gnu";
1555 } else if (Arch == llvm::Triple::ppc64) {
Eric Christopher9af535a2011-06-03 13:28:31 +00001556 if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc64-unknown-linux-gnu",
1557 Exists) && Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001558 GccTriple = "powerpc64-unknown-linux-gnu";
Eric Christopher9af535a2011-06-03 13:28:31 +00001559 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/"
1560 "powerpc64-unknown-linux-gnu", Exists) &&
1561 Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001562 GccTriple = "powerpc64-unknown-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001563 }
1564
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001565 std::string Base = findGCCBaseLibDir(GccTriple);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001566 path_list &Paths = getFilePaths();
Eric Christopher9af535a2011-06-03 13:28:31 +00001567 bool Is32Bits = (getArch() == llvm::Triple::x86 ||
1568 getArch() == llvm::Triple::ppc);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001569
1570 std::string Suffix;
1571 std::string Lib;
1572
1573 if (Is32Bits) {
1574 Suffix = Suffix32;
1575 Lib = Lib32;
1576 } else {
1577 Suffix = Suffix64;
1578 Lib = Lib64;
1579 }
1580
1581 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001582 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001583 Linker = LinkerPath.str();
1584 else
1585 Linker = GetProgramPath("ld");
1586
1587 LinuxDistro Distro = DetectLinuxDistro(Arch);
1588
Chris Lattner64a89172011-05-22 16:45:07 +00001589 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00001590 ExtraOpts.push_back("-z");
1591 ExtraOpts.push_back("relro");
1592 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001593
Douglas Gregorf0594d82011-03-06 19:11:49 +00001594 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001595 ExtraOpts.push_back("-X");
1596
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001597 if (IsRedhat(Distro) || IsOpenSuse(Distro) || Distro == UbuntuMaverick ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001598 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001599 ExtraOpts.push_back("--hash-style=gnu");
1600
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001601 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
Chris Lattner64a89172011-05-22 16:45:07 +00001602 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001603 ExtraOpts.push_back("--hash-style=both");
1604
Chris Lattnerd753b562011-05-22 05:36:06 +00001605 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001606 ExtraOpts.push_back("--no-add-needed");
1607
Eli Friedman0b200f62011-06-02 21:36:53 +00001608 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001609 IsOpenSuse(Distro) ||
1610 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
1611 Distro == UbuntuLucid ||
Eli Friedman0b200f62011-06-02 21:36:53 +00001612 Distro == UbuntuMaverick || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001613 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001614 ExtraOpts.push_back("--build-id");
1615
Chris Lattner64a89172011-05-22 16:45:07 +00001616 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00001617 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00001618
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001619 if (Distro == ArchLinux)
1620 Lib = "lib";
1621
Rafael Espindolac1da9812010-11-07 20:14:31 +00001622 Paths.push_back(Base + Suffix);
1623 if (HasMultilib(Arch, Distro)) {
1624 if (IsOpenSuse(Distro) && Is32Bits)
1625 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1626 Paths.push_back(Base + "/../../../../" + Lib);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001627 }
Rafael Espindolac7409a02011-06-03 15:39:42 +00001628
1629 // FIXME: This is in here to find crt1.o. It is provided by libc, and
1630 // libc (like gcc), can be installed in any directory. Once we are
1631 // fetching this from a config file, we should have a libc prefix.
1632 Paths.push_back("/lib/../" + Lib);
1633 Paths.push_back("/usr/lib/../" + Lib);
1634
Rafael Espindolac1da9812010-11-07 20:14:31 +00001635 if (!Suffix.empty())
1636 Paths.push_back(Base);
1637 if (IsOpenSuse(Distro))
1638 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1639 Paths.push_back(Base + "/../../..");
1640 if (Arch == getArch() && IsUbuntu(Distro))
1641 Paths.push_back("/usr/lib/" + GccTriple);
1642}
1643
1644bool Linux::HasNativeLLVMSupport() const {
1645 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00001646}
1647
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001648Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
1649 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001650 Action::ActionClass Key;
1651 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1652 Key = Action::AnalyzeJobClass;
1653 else
1654 Key = JA.getKind();
1655
Rafael Espindoladda5b922010-11-07 23:13:01 +00001656 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1657 options::OPT_no_integrated_as,
1658 IsIntegratedAssemblerDefault());
1659
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001660 Tool *&T = Tools[Key];
1661 if (!T) {
1662 switch (Key) {
1663 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00001664 if (UseIntegratedAs)
1665 T = new tools::ClangAs(*this);
1666 else
1667 T = new tools::linuxtools::Assemble(*this);
1668 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001669 case Action::LinkJobClass:
1670 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001671 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001672 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001673 }
1674 }
1675
1676 return *T;
1677}
1678
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001679/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1680
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001681DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001682 : Generic_ELF(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001683
1684 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001685 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001686 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001687 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001688
Daniel Dunbaree788e72009-12-21 18:54:17 +00001689 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001690 getFilePaths().push_back("/usr/lib");
1691 getFilePaths().push_back("/usr/lib/gcc41");
1692}
1693
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001694Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
1695 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001696 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001697 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001698 Key = Action::AnalyzeJobClass;
1699 else
1700 Key = JA.getKind();
1701
1702 Tool *&T = Tools[Key];
1703 if (!T) {
1704 switch (Key) {
1705 case Action::AssembleJobClass:
1706 T = new tools::dragonfly::Assemble(*this); break;
1707 case Action::LinkJobClass:
1708 T = new tools::dragonfly::Link(*this); break;
1709 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001710 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001711 }
1712 }
1713
1714 return *T;
1715}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001716
1717Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1718 : ToolChain(Host, Triple) {
1719}
1720
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001721Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA,
1722 const ActionList &Inputs) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00001723 Action::ActionClass Key;
1724 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1725 Key = Action::AnalyzeJobClass;
1726 else
1727 Key = JA.getKind();
1728
1729 Tool *&T = Tools[Key];
1730 if (!T) {
1731 switch (Key) {
1732 case Action::InputClass:
1733 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001734 case Action::LipoJobClass:
1735 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001736 assert(0 && "Invalid tool kind.");
1737 case Action::PreprocessJobClass:
1738 case Action::PrecompileJobClass:
1739 case Action::AnalyzeJobClass:
1740 case Action::CompileJobClass:
1741 T = new tools::Clang(*this); break;
1742 case Action::AssembleJobClass:
1743 T = new tools::ClangAs(*this); break;
1744 case Action::LinkJobClass:
1745 T = new tools::visualstudio::Link(*this); break;
1746 }
1747 }
1748
1749 return *T;
1750}
1751
1752bool Windows::IsIntegratedAssemblerDefault() const {
1753 return true;
1754}
1755
1756bool Windows::IsUnwindTablesDefault() const {
1757 // FIXME: Gross; we should probably have some separate target
1758 // definition, possibly even reusing the one in clang.
1759 return getArchName() == "x86_64";
1760}
1761
1762const char *Windows::GetDefaultRelocationModel() const {
1763 return "static";
1764}
1765
1766const char *Windows::GetForcedPicModel() const {
1767 if (getArchName() == "x86_64")
1768 return "pic";
1769 return 0;
1770}