blob: 75cb9353aa00aff933ad250d00c2f4b1fec6cb5b [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
11
Daniel Dunbarf3cad362009-03-25 04:13:45 +000012#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000014#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000018#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000019#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000020#include "clang/Driver/Options.h"
Douglas Gregor34916db2010-09-03 17:16:03 +000021#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000022
Daniel Dunbar00577ad2010-08-23 22:35:37 +000023#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000024#include "llvm/ADT/StringExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000025#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000026#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000027#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000028#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000030#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000031
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000032#include <cstdlib> // ::getenv
33
Daniel Dunbar39176082009-03-20 00:20:03 +000034using namespace clang::driver;
35using namespace clang::driver::toolchains;
36
Daniel Dunbarf3955282009-09-04 18:34:51 +000037/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000038
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000039Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbarcc8e1892010-01-27 00:56:44 +000040 : ToolChain(Host, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000041{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000042 // Compute the initial Darwin version based on the host.
43 bool HadExtra;
44 std::string OSName = Triple.getOSName();
Daniel Dunbar34f9e292011-02-25 21:20:15 +000045 if (!Driver::GetReleaseVersion(&OSName.c_str()[6],
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000046 DarwinVersion[0], DarwinVersion[1],
47 DarwinVersion[2], HadExtra))
48 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
49
Daniel Dunbar02633b52009-03-26 16:23:12 +000050 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000051 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
52 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000053}
54
Daniel Dunbar41800112010-08-02 05:43:56 +000055types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
56 types::ID Ty = types::lookupTypeForExtension(Ext);
57
58 // Darwin always preprocesses assembly files (unless -x is used explicitly).
59 if (Ty == types::TY_PP_Asm)
60 return types::TY_Asm;
61
62 return Ty;
63}
64
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000065bool Darwin::HasNativeLLVMSupport() const {
66 return true;
67}
68
Daniel Dunbareeff4062010-01-22 02:04:58 +000069// FIXME: Can we tablegen this?
70static const char *GetArmArchForMArch(llvm::StringRef Value) {
71 if (Value == "armv6k")
72 return "armv6";
73
74 if (Value == "armv5tej")
75 return "armv5";
76
77 if (Value == "xscale")
78 return "xscale";
79
80 if (Value == "armv4t")
81 return "armv4t";
82
83 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
84 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
85 Value == "armv7m")
86 return "armv7";
87
88 return 0;
89}
90
91// FIXME: Can we tablegen this?
92static const char *GetArmArchForMCpu(llvm::StringRef Value) {
93 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
94 Value == "arm946e-s" || Value == "arm966e-s" ||
95 Value == "arm968e-s" || Value == "arm10e" ||
96 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
97 Value == "arm1026ej-s")
98 return "armv5";
99
100 if (Value == "xscale")
101 return "xscale";
102
103 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000104 Value == "arm1176jz-s" || Value == "arm1176jzf-s" ||
105 Value == "cortex-m0" )
Daniel Dunbareeff4062010-01-22 02:04:58 +0000106 return "armv6";
107
108 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
109 return "armv7";
110
111 return 0;
112}
113
114llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
115 switch (getTriple().getArch()) {
116 default:
117 return getArchName();
Douglas Gregorf0594d82011-03-06 19:11:49 +0000118
119 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000120 case llvm::Triple::arm: {
121 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
122 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
123 return Arch;
124
125 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
126 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
127 return Arch;
128
129 return "arm";
130 }
131 }
132}
133
Daniel Dunbarf3955282009-09-04 18:34:51 +0000134Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000135 // Free tool implementations.
136 for (llvm::DenseMap<unsigned, Tool*>::iterator
137 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
138 delete it->second;
139}
140
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000141std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
142 llvm::Triple Triple(ComputeLLVMTriple(Args));
143
144 // If the target isn't initialized (e.g., an unknown Darwin platform, return
145 // the default triple).
146 if (!isTargetInitialized())
147 return Triple.getTriple();
148
149 unsigned Version[3];
150 getTargetVersion(Version);
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000151
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000152 llvm::SmallString<16> Str;
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000153 llvm::raw_svector_ostream(Str)
Daniel Dunbar659d23a2011-04-19 23:34:17 +0000154 << (isTargetIPhoneOS() ? "ios" : "macosx")
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000155 << Version[0] << "." << Version[1] << "." << Version[2];
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000156 Triple.setOSName(Str.str());
157
158 return Triple.getTriple();
159}
160
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000161Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
162 const ActionList &Inputs) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000163 Action::ActionClass Key;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000164
165 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
166 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
167 if (Inputs.size() == 1 &&
168 types::isCXX(Inputs[0]->getType()) &&
169 getTriple().getOS() == llvm::Triple::Darwin &&
170 getTriple().getArch() == llvm::Triple::x86 &&
171 C.getArgs().getLastArg(options::OPT_fapple_kext))
172 Key = JA.getKind();
173 else
174 Key = Action::AnalyzeJobClass;
175 } else
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000176 Key = JA.getKind();
177
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000178 // FIXME: This doesn't belong here, but ideally we will support static soon
179 // anyway.
180 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
181 C.getArgs().hasArg(options::OPT_static) ||
182 C.getArgs().hasArg(options::OPT_fapple_kext));
183 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
184 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
185 options::OPT_no_integrated_as,
186 IsIADefault);
187
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000188 Tool *&T = Tools[Key];
189 if (!T) {
190 switch (Key) {
191 case Action::InputClass:
192 case Action::BindArchClass:
193 assert(0 && "Invalid tool kind.");
194 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000195 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000196 case Action::AnalyzeJobClass:
197 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000198 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000199 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000200 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000201 case Action::AssembleJobClass: {
202 if (UseIntegratedAs)
203 T = new tools::ClangAs(*this);
204 else
205 T = new tools::darwin::Assemble(*this);
206 break;
207 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000208 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000209 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000210 case Action::LipoJobClass:
211 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000212 case Action::DsymutilJobClass:
213 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000214 }
215 }
216
217 return *T;
218}
219
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000220
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000221DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
222 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000223{
Daniel Dunbar47023092011-03-18 19:25:15 +0000224 std::string UsrPrefix = "llvm-gcc-4.2/";
225
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000226 getProgramPaths().push_back(getDriver().getInstalledDir());
227 if (getDriver().getInstalledDir() != getDriver().Dir)
228 getProgramPaths().push_back(getDriver().Dir);
229
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000230 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000231 getProgramPaths().push_back(getDriver().getInstalledDir());
232 if (getDriver().getInstalledDir() != getDriver().Dir)
233 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000234
235 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000236 // also add the GCC libexec paths. This is legacy code that can be removed
237 // once fallback is no longer useful.
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000238 std::string ToolChainDir = "i686-apple-darwin";
239 ToolChainDir += llvm::utostr(DarwinVersion[0]);
240 ToolChainDir += "/4.2.1";
241
242 std::string Path = getDriver().Dir;
Daniel Dunbar47023092011-03-18 19:25:15 +0000243 Path += "/../" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000244 Path += ToolChainDir;
245 getProgramPaths().push_back(Path);
246
Daniel Dunbar47023092011-03-18 19:25:15 +0000247 Path = "/usr/" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000248 Path += ToolChainDir;
249 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000250}
251
252void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
253 ArgStringList &CmdArgs) const {
254 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000255
256 // Unfortunately, we still might depend on a few of the libraries that are
257 // only available in the gcc library directory (in particular
258 // libstdc++.dylib). For now, hardcode the path to the known install location.
259 llvm::sys::Path P(getDriver().Dir);
260 P.eraseComponent(); // .../usr/bin -> ../usr
261 P.appendComponent("lib");
262 P.appendComponent("gcc");
263 switch (getTriple().getArch()) {
264 default:
265 assert(0 && "Invalid Darwin arch!");
266 case llvm::Triple::x86:
267 case llvm::Triple::x86_64:
268 P.appendComponent("i686-apple-darwin10");
269 break;
270 case llvm::Triple::arm:
271 case llvm::Triple::thumb:
272 P.appendComponent("arm-apple-darwin10");
273 break;
274 case llvm::Triple::ppc:
275 case llvm::Triple::ppc64:
276 P.appendComponent("powerpc-apple-darwin10");
277 break;
278 }
279 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000280
281 // Determine the arch specific GCC subdirectory.
282 const char *ArchSpecificDir = 0;
283 switch (getTriple().getArch()) {
284 default:
285 break;
286 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000287 case llvm::Triple::thumb: {
288 std::string Triple = ComputeLLVMTriple(Args);
289 llvm::StringRef TripleStr = Triple;
290 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
291 ArchSpecificDir = "v5";
292 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
293 ArchSpecificDir = "v6";
294 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
295 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000296 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000297 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000298 case llvm::Triple::ppc64:
299 ArchSpecificDir = "ppc64";
300 break;
301 case llvm::Triple::x86_64:
302 ArchSpecificDir = "x86_64";
303 break;
304 }
305
306 if (ArchSpecificDir) {
307 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000308 bool Exists;
309 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000310 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
311 P.eraseComponent();
312 }
313
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000314 bool Exists;
315 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000316 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000317}
318
319void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
320 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000321 // Darwin doesn't support real static executables, don't link any runtime
322 // libraries with -static.
323 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000324 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000325
326 // Reject -static-libgcc for now, we can deal with this when and if someone
327 // cares. This is useful in situations where someone wants to statically link
328 // something like libstdc++, and needs its runtime support routines.
329 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000330 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000331 << A->getAsString(Args);
332 return;
333 }
334
Daniel Dunbareec99102010-01-22 03:38:14 +0000335 // Otherwise link libSystem, then the dynamic runtime library, and finally any
336 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000337 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000338
339 // Select the dynamic runtime library and the target specific static library.
340 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000341 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000342 CmdArgs.push_back("-lgcc_s.1");
343
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000344 // We currently always need a static runtime library for iOS.
345 DarwinStaticLib = "libclang_rt.ios.a";
Daniel Dunbareec99102010-01-22 03:38:14 +0000346 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000347 // The dynamic runtime library was merged with libSystem for 10.6 and
348 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000349 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000350 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000351 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000352 CmdArgs.push_back("-lgcc_s.10.5");
353
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000354 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000355 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000356 // omitted from 10.4.dylib.
357 //
358 // Unfortunately, that turned out to not be true, because Darwin system
359 // headers can still use eprintf on i386, and it is not exported from
360 // libSystem. Therefore, we still must provide a runtime library just for
361 // the tiny tiny handful of projects that *might* use that symbol.
362 if (isMacosxVersionLT(10, 5)) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000363 DarwinStaticLib = "libclang_rt.10.4.a";
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000364 } else {
365 if (getTriple().getArch() == llvm::Triple::x86)
366 DarwinStaticLib = "libclang_rt.eprintf.a";
367 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000368 }
369
370 /// Add the target specific static library, if needed.
371 if (DarwinStaticLib) {
372 llvm::sys::Path P(getDriver().ResourceDir);
373 P.appendComponent("lib");
374 P.appendComponent("darwin");
375 P.appendComponent(DarwinStaticLib);
376
377 // For now, allow missing resource libraries to support developers who may
378 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000379 bool Exists;
380 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareec99102010-01-22 03:38:14 +0000381 CmdArgs.push_back(Args.MakeArgString(P.str()));
382 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000383}
384
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000385void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000386 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000387
Daniel Dunbar26031372010-01-27 00:56:25 +0000388 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
389 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000390 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000391 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000392 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000393 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000394 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000395 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000396 // If neither OS X nor iPhoneOS targets were specified, check for
397 // environment defines.
398 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
399 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000400
Daniel Dunbar816bc312010-01-26 01:45:19 +0000401 // Ignore empty strings.
402 if (OSXTarget && OSXTarget[0] == '\0')
403 OSXTarget = 0;
404 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
405 iPhoneOSTarget = 0;
406
Daniel Dunbar39053672010-02-02 17:31:12 +0000407 // Diagnose conflicting deployment targets, and choose default platform
408 // based on the tool chain.
409 //
410 // FIXME: Don't hardcode default here.
411 if (OSXTarget && iPhoneOSTarget) {
412 // FIXME: We should see if we can get away with warning or erroring on
413 // this. Perhaps put under -pedantic?
414 if (getTriple().getArch() == llvm::Triple::arm ||
415 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000416 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000417 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000418 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000419 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000420
Daniel Dunbar39053672010-02-02 17:31:12 +0000421 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000422 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000423 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
424 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000425 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000426 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000427 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
428 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000429 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000430 // Otherwise, assume we are targeting OS X.
431 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000432 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
433 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000434 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Daniel Dunbar26031372010-01-27 00:56:25 +0000437 // Set the tool chain target information.
438 unsigned Major, Minor, Micro;
439 bool HadExtra;
440 if (OSXVersion) {
441 assert(!iPhoneVersion && "Unknown target platform!");
442 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
443 Micro, HadExtra) || HadExtra ||
444 Major != 10 || Minor >= 10 || Micro >= 10)
445 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
446 << OSXVersion->getAsString(Args);
447 } else {
448 assert(iPhoneVersion && "Unknown target platform!");
449 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
450 Micro, HadExtra) || HadExtra ||
451 Major >= 10 || Minor >= 100 || Micro >= 100)
452 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
453 << iPhoneVersion->getAsString(Args);
454 }
455 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000456}
457
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000458void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000459 ArgStringList &CmdArgs) const {
460 CXXStdlibType Type = GetCXXStdlibType(Args);
461
462 switch (Type) {
463 case ToolChain::CST_Libcxx:
464 CmdArgs.push_back("-lc++");
465 break;
466
467 case ToolChain::CST_Libstdcxx: {
468 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
469 // it was previously found in the gcc lib dir. However, for all the Darwin
470 // platforms we care about it was -lstdc++.6, so we search for that
471 // explicitly if we can't see an obvious -lstdc++ candidate.
472
473 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000474 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000475 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
476 llvm::sys::Path P(A->getValue(Args));
477 P.appendComponent("usr");
478 P.appendComponent("lib");
479 P.appendComponent("libstdc++.dylib");
480
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000481 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000482 P.eraseComponent();
483 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000484 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000485 CmdArgs.push_back(Args.MakeArgString(P.str()));
486 return;
487 }
488 }
489 }
490
491 // Otherwise, look in the root.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000492 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
493 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000494 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
495 return;
496 }
497
498 // Otherwise, let the linker search.
499 CmdArgs.push_back("-lstdc++");
500 break;
501 }
502 }
503}
504
Shantonu Sen7433fed2010-09-17 18:39:08 +0000505void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
506 ArgStringList &CmdArgs) const {
507
508 // For Darwin platforms, use the compiler-rt-based support library
509 // instead of the gcc-provided one (which is also incidentally
510 // only present in the gcc lib dir, which makes it hard to find).
511
512 llvm::sys::Path P(getDriver().ResourceDir);
513 P.appendComponent("lib");
514 P.appendComponent("darwin");
515 P.appendComponent("libclang_rt.cc_kext.a");
516
517 // For now, allow missing resource libraries to support developers who may
518 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000519 bool Exists;
520 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000521 CmdArgs.push_back(Args.MakeArgString(P.str()));
522}
523
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000524DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
525 const char *BoundArch) const {
526 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
527 const OptTable &Opts = getDriver().getOpts();
528
529 // FIXME: We really want to get out of the tool chain level argument
530 // translation business, as it makes the driver functionality much
531 // more opaque. For now, we follow gcc closely solely for the
532 // purpose of easily achieving feature parity & testability. Once we
533 // have something that works, we should reevaluate each translation
534 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000535
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000536 for (ArgList::const_iterator it = Args.begin(),
537 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000538 Arg *A = *it;
539
540 if (A->getOption().matches(options::OPT_Xarch__)) {
541 // FIXME: Canonicalize name.
542 if (getArchName() != A->getValue(Args, 0))
543 continue;
544
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000545 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000546 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
547 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000548 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000550 // If the argument parsing failed or more than one argument was
551 // consumed, the -Xarch_ argument's parameter tried to consume
552 // extra arguments. Emit an error and ignore.
553 //
554 // We also want to disallow any options which would alter the
555 // driver behavior; that isn't going to work in our model. We
556 // use isDriverOption() as an approximation, although things
557 // like -O4 are going to slip through.
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000558 if (!XarchArg) {
559 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_unknown)
560 << A->getAsString(Args);
561 continue;
562 } else if (Index > Prev + 1) {
563 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_with_args)
564 << A->getAsString(Args);
565 continue;
566 } else if (XarchArg->getOption().isDriverOption()) {
567 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000568 << A->getAsString(Args);
569 continue;
570 }
571
Daniel Dunbar478edc22009-03-29 22:29:05 +0000572 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000573 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000574
575 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000576
577 // Linker input arguments require custom handling. The problem is that we
578 // have already constructed the phase actions, so we can not treat them as
579 // "input arguments".
580 if (A->getOption().isLinkerInput()) {
581 // Convert the argument into individual Zlinker_input_args.
582 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
583 DAL->AddSeparateArg(OriginalArg,
584 Opts.getOption(options::OPT_Zlinker_input),
585 A->getValue(Args, i));
586
587 }
588 continue;
589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000591
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000592 // Sob. These is strictly gcc compatible for the time being. Apple
593 // gcc translates options twice, which means that self-expanding
594 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000595 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000596 default:
597 DAL->append(A);
598 break;
599
600 case options::OPT_mkernel:
601 case options::OPT_fapple_kext:
602 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000603 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
604 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000605 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000607 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000608 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
609 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000610 break;
611
612 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000613 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
614 DAL->AddFlagArg(A,
615 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000616 break;
617
618 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000619 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
620 DAL->AddFlagArg(A,
621 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000622 break;
623
624 case options::OPT_fterminated_vtables:
625 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000626 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
627 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000628 break;
629
630 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000631 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000632 break;
633
634 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000635 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000636 break;
637
638 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000639 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000640 break;
641
642 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000643 DAL->AddFlagArg(A,
644 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000645 break;
646
647 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000648 DAL->AddFlagArg(A,
649 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000650 break;
651
652 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000653 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000654 break;
655
656 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000657 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000658 break;
659 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000660 }
661
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000662 if (getTriple().getArch() == llvm::Triple::x86 ||
663 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000664 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000665 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000666
667 // Add the arch options based on the particular spelling of -arch, to match
668 // how the driver driver works.
669 if (BoundArch) {
670 llvm::StringRef Name = BoundArch;
671 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
672 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
673
674 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
675 // which defines the list of which architectures we accept.
676 if (Name == "ppc")
677 ;
678 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000679 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000680 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000681 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000682 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000683 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000684 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000685 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000686 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000687 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000688 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000689 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000690 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000691 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000692 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000693 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000694
695 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000696 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000697
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000698 else if (Name == "i386")
699 ;
700 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000701 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000702 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000703 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000704 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000705 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000706 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000707 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000708 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000709 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000710 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000711 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000712 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000713 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000714
715 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000716 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000717
718 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000719 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000720 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000721 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000722 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000723 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000724 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000725 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000726 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000727 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000728 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000729 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000730
731 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000732 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000733 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000734
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000735 // Add an explicit version min argument for the deployment target. We do this
736 // after argument translation because -Xarch_ arguments may add a version min
737 // argument.
738 AddDeploymentTarget(*DAL);
739
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000740 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000741}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000742
Daniel Dunbarf3955282009-09-04 18:34:51 +0000743bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000744 // FIXME: Gross; we should probably have some separate target
745 // definition, possibly even reusing the one in clang.
746 return getArchName() == "x86_64";
747}
748
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000749bool Darwin::UseDwarfDebugFlags() const {
750 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
751 return S[0] != '\0';
752 return false;
753}
754
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000755bool Darwin::UseSjLjExceptions() const {
756 // Darwin uses SjLj exceptions on ARM.
757 return (getTriple().getArch() == llvm::Triple::arm ||
758 getTriple().getArch() == llvm::Triple::thumb);
759}
760
Daniel Dunbarf3955282009-09-04 18:34:51 +0000761const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000762 return "pic";
763}
764
Daniel Dunbarf3955282009-09-04 18:34:51 +0000765const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000766 if (getArchName() == "x86_64")
767 return "pic";
768 return 0;
769}
770
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000771bool Darwin::SupportsProfiling() const {
772 // Profiling instrumentation is only supported on x86.
773 return getArchName() == "i386" || getArchName() == "x86_64";
774}
775
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000776bool Darwin::SupportsObjCGC() const {
777 // Garbage collection is supported everywhere except on iPhone OS.
778 return !isTargetIPhoneOS();
779}
780
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000781std::string
782Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
783 return ComputeLLVMTriple(Args);
784}
785
Daniel Dunbar39176082009-03-20 00:20:03 +0000786/// Generic_GCC - A tool chain using the 'gcc' command to perform
787/// all subcommands; this relies on gcc translating the majority of
788/// command line options.
789
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000790Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000791 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000792 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +0000793 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000794 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000795}
796
Daniel Dunbar39176082009-03-20 00:20:03 +0000797Generic_GCC::~Generic_GCC() {
798 // Free tool implementations.
799 for (llvm::DenseMap<unsigned, Tool*>::iterator
800 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
801 delete it->second;
802}
803
Mike Stump1eb44332009-09-09 15:08:12 +0000804Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000805 const JobAction &JA,
806 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +0000807 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000808 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000809 Key = Action::AnalyzeJobClass;
810 else
811 Key = JA.getKind();
812
813 Tool *&T = Tools[Key];
814 if (!T) {
815 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000816 case Action::InputClass:
817 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000818 assert(0 && "Invalid tool kind.");
819 case Action::PreprocessJobClass:
820 T = new tools::gcc::Preprocess(*this); break;
821 case Action::PrecompileJobClass:
822 T = new tools::gcc::Precompile(*this); break;
823 case Action::AnalyzeJobClass:
824 T = new tools::Clang(*this); break;
825 case Action::CompileJobClass:
826 T = new tools::gcc::Compile(*this); break;
827 case Action::AssembleJobClass:
828 T = new tools::gcc::Assemble(*this); break;
829 case Action::LinkJobClass:
830 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000832 // This is a bit ungeneric, but the only platform using a driver
833 // driver is Darwin.
834 case Action::LipoJobClass:
835 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000836 case Action::DsymutilJobClass:
837 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000838 }
839 }
840
841 return *T;
842}
843
Daniel Dunbar39176082009-03-20 00:20:03 +0000844bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000845 // FIXME: Gross; we should probably have some separate target
846 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000847 return getArchName() == "x86_64";
848}
849
850const char *Generic_GCC::GetDefaultRelocationModel() const {
851 return "static";
852}
853
854const char *Generic_GCC::GetForcedPicModel() const {
855 return 0;
856}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000857
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000858/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
859/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
860/// Currently does not support anything else but compilation.
861
862TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
863 : ToolChain(Host, Triple) {
864 // Path mangling to find libexec
865 std::string Path(getDriver().Dir);
866
867 Path += "/../libexec";
868 getProgramPaths().push_back(Path);
869}
870
871TCEToolChain::~TCEToolChain() {
872 for (llvm::DenseMap<unsigned, Tool*>::iterator
873 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
874 delete it->second;
875}
876
877bool TCEToolChain::IsMathErrnoDefault() const {
878 return true;
879}
880
881bool TCEToolChain::IsUnwindTablesDefault() const {
882 return false;
883}
884
885const char *TCEToolChain::GetDefaultRelocationModel() const {
886 return "static";
887}
888
889const char *TCEToolChain::GetForcedPicModel() const {
890 return 0;
891}
892
893Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000894 const JobAction &JA,
895 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000896 Action::ActionClass Key;
897 Key = Action::AnalyzeJobClass;
898
899 Tool *&T = Tools[Key];
900 if (!T) {
901 switch (Key) {
902 case Action::PreprocessJobClass:
903 T = new tools::gcc::Preprocess(*this); break;
904 case Action::AnalyzeJobClass:
905 T = new tools::Clang(*this); break;
906 default:
907 assert(false && "Unsupported action for TCE target.");
908 }
909 }
910 return *T;
911}
912
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000913/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
914
915OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +0000916 : Generic_ELF(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000917 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000918 getFilePaths().push_back("/usr/lib");
919}
920
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000921Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
922 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000923 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000924 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000925 Key = Action::AnalyzeJobClass;
926 else
927 Key = JA.getKind();
928
Rafael Espindoladda5b922010-11-07 23:13:01 +0000929 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
930 options::OPT_no_integrated_as,
931 IsIntegratedAssemblerDefault());
932
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000933 Tool *&T = Tools[Key];
934 if (!T) {
935 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +0000936 case Action::AssembleJobClass: {
937 if (UseIntegratedAs)
938 T = new tools::ClangAs(*this);
939 else
940 T = new tools::openbsd::Assemble(*this);
941 break;
942 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000943 case Action::LinkJobClass:
944 T = new tools::openbsd::Link(*this); break;
945 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000946 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000947 }
948 }
949
950 return *T;
951}
952
Daniel Dunbar75358d22009-03-30 21:06:03 +0000953/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
954
Daniel Dunbar214afe92010-08-02 05:43:59 +0000955FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +0000956 : Generic_ELF(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +0000957
958 // Determine if we are compiling 32-bit code on an x86_64 platform.
959 bool Lib32 = false;
960 if (Triple.getArch() == llvm::Triple::x86 &&
961 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
962 llvm::Triple::x86_64)
963 Lib32 = true;
964
Daniel Dunbarbc534662009-04-02 18:30:04 +0000965 if (Lib32) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000966 getFilePaths().push_back("/usr/lib32");
967 } else {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000968 getFilePaths().push_back("/usr/lib");
969 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000970}
971
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000972Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
973 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000974 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000975 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000976 Key = Action::AnalyzeJobClass;
977 else
978 Key = JA.getKind();
979
Roman Divacky67dece72010-11-08 17:46:39 +0000980 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
981 options::OPT_no_integrated_as,
982 IsIntegratedAssemblerDefault());
983
Daniel Dunbar75358d22009-03-30 21:06:03 +0000984 Tool *&T = Tools[Key];
985 if (!T) {
986 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000987 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +0000988 if (UseIntegratedAs)
989 T = new tools::ClangAs(*this);
990 else
991 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +0000992 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000993 case Action::LinkJobClass:
994 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000995 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000996 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +0000997 }
998 }
999
1000 return *T;
1001}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001002
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001003/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1004
1005NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple)
1006 : Generic_ELF(Host, Triple) {
1007
1008 // Determine if we are compiling 32-bit code on an x86_64 platform.
1009 bool Lib32 = false;
1010 if (Triple.getArch() == llvm::Triple::x86 &&
1011 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1012 llvm::Triple::x86_64)
1013 Lib32 = true;
1014
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001015 if (getDriver().UseStdLib) {
1016 if (Lib32)
1017 getFilePaths().push_back("=/usr/lib/i386");
1018 else
1019 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001020 }
1021}
1022
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001023Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1024 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001025 Action::ActionClass Key;
1026 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1027 Key = Action::AnalyzeJobClass;
1028 else
1029 Key = JA.getKind();
1030
1031 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1032 options::OPT_no_integrated_as,
1033 IsIntegratedAssemblerDefault());
1034
1035 Tool *&T = Tools[Key];
1036 if (!T) {
1037 switch (Key) {
1038 case Action::AssembleJobClass:
1039 if (UseIntegratedAs)
1040 T = new tools::ClangAs(*this);
1041 else
1042 T = new tools::netbsd::Assemble(*this);
1043 break;
1044 case Action::LinkJobClass:
1045 T = new tools::netbsd::Link(*this); break;
1046 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001047 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001048 }
1049 }
1050
1051 return *T;
1052}
1053
Chris Lattner38e317d2010-07-07 16:01:42 +00001054/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1055
1056Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1057 : Generic_GCC(Host, Triple) {
1058 getFilePaths().push_back(getDriver().Dir + "/../lib");
1059 getFilePaths().push_back("/usr/lib");
1060 getFilePaths().push_back("/usr/gnu/lib");
1061 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1062}
1063
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001064Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1065 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001066 Action::ActionClass Key;
1067 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1068 Key = Action::AnalyzeJobClass;
1069 else
1070 Key = JA.getKind();
1071
1072 Tool *&T = Tools[Key];
1073 if (!T) {
1074 switch (Key) {
1075 case Action::AssembleJobClass:
1076 T = new tools::minix::Assemble(*this); break;
1077 case Action::LinkJobClass:
1078 T = new tools::minix::Link(*this); break;
1079 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001080 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001081 }
1082 }
1083
1084 return *T;
1085}
1086
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001087/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1088
1089AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1090 : Generic_GCC(Host, Triple) {
1091
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001092 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001093 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001094 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001095
Daniel Dunbaree788e72009-12-21 18:54:17 +00001096 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001097 getFilePaths().push_back("/usr/lib");
1098 getFilePaths().push_back("/usr/sfw/lib");
1099 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001100 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001101
1102}
1103
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001104Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1105 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001106 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001107 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001108 Key = Action::AnalyzeJobClass;
1109 else
1110 Key = JA.getKind();
1111
1112 Tool *&T = Tools[Key];
1113 if (!T) {
1114 switch (Key) {
1115 case Action::AssembleJobClass:
1116 T = new tools::auroraux::Assemble(*this); break;
1117 case Action::LinkJobClass:
1118 T = new tools::auroraux::Link(*this); break;
1119 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001120 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001121 }
1122 }
1123
1124 return *T;
1125}
1126
1127
Eli Friedman6b3454a2009-05-26 07:52:18 +00001128/// Linux toolchain (very bare-bones at the moment).
1129
Rafael Espindolac1da9812010-11-07 20:14:31 +00001130enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001131 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001132 DebianLenny,
1133 DebianSqueeze,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001134 Exherbo,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001135 Fedora13,
1136 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001137 Fedora15,
1138 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001139 OpenSuse11_3,
Douglas Gregor814638e2011-03-14 15:39:50 +00001140 UbuntuHardy,
1141 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001142 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001143 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001144 UbuntuLucid,
1145 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001146 UbuntuNatty,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001147 UnknownDistro
1148};
1149
1150static bool IsFedora(enum LinuxDistro Distro) {
Eric Christopher8f1cc072011-04-06 18:22:53 +00001151 return Distro == Fedora13 || Distro == Fedora14 ||
1152 Distro == Fedora15 || Distro == FedoraRawhide;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001153}
1154
1155static bool IsOpenSuse(enum LinuxDistro Distro) {
1156 return Distro == OpenSuse11_3;
1157}
1158
1159static bool IsDebian(enum LinuxDistro Distro) {
1160 return Distro == DebianLenny || Distro == DebianSqueeze;
1161}
1162
1163static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001164 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
1165 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Ted Kremenek43ac2972011-04-05 22:04:27 +00001166 Distro == UbuntuJaunty || Distro == UbuntuKarmic ||
1167 Distro == UbuntuNatty;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001168}
1169
1170static bool IsDebianBased(enum LinuxDistro Distro) {
1171 return IsDebian(Distro) || IsUbuntu(Distro);
1172}
1173
1174static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001175 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001176 bool Exists;
1177 if (Distro == Exherbo &&
1178 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001179 return false;
1180
Rafael Espindolac1da9812010-11-07 20:14:31 +00001181 return true;
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001182 }
Ted Kremenek43ac2972011-04-05 22:04:27 +00001183 if (Arch == llvm::Triple::ppc64)
1184 return true;
1185 if ((Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) && IsDebianBased(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001186 return true;
1187 return false;
1188}
1189
1190static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001191 llvm::OwningPtr<llvm::MemoryBuffer> File;
1192 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
1193 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001194 llvm::SmallVector<llvm::StringRef, 8> Lines;
1195 Data.split(Lines, "\n");
1196 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001197 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1198 return UbuntuHardy;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001199 else if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1200 return UbuntuIntrepid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001201 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001202 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001203 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1204 return UbuntuKarmic;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001205 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1206 return UbuntuLucid;
1207 else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1208 return UbuntuMaverick;
1209 else if (Lines[i] == "DISTRIB_CODENAME=natty")
1210 return UbuntuNatty;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001211 }
1212 return UnknownDistro;
1213 }
1214
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001215 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1216 llvm::StringRef Data = File.get()->getBuffer();
Eric Christopher8f1cc072011-04-06 18:22:53 +00001217 if (Data.startswith("Fedora release 15"))
1218 return Fedora15;
1219 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001220 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001221 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001222 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001223 else if (Data.startswith("Fedora release") &&
1224 Data.find("Rawhide") != llvm::StringRef::npos)
1225 return FedoraRawhide;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001226 return UnknownDistro;
1227 }
1228
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001229 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1230 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001231 if (Data[0] == '5')
1232 return DebianLenny;
1233 else if (Data.startswith("squeeze/sid"))
1234 return DebianSqueeze;
1235 return UnknownDistro;
1236 }
1237
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001238 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1239 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001240 if (Data.startswith("openSUSE 11.3"))
1241 return OpenSuse11_3;
1242 return UnknownDistro;
1243 }
1244
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001245 bool Exists;
1246 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001247 return Exherbo;
1248
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001249 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1250 return ArchLinux;
1251
Rafael Espindolac1da9812010-11-07 20:14:31 +00001252 return UnknownDistro;
1253}
1254
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001255Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001256 : Generic_ELF(Host, Triple) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001257 llvm::Triple::ArchType Arch =
1258 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Daniel Dunbara9822de2009-08-06 01:47:11 +00001259
Rafael Espindolac1da9812010-11-07 20:14:31 +00001260 std::string Suffix32 = "";
1261 if (Arch == llvm::Triple::x86_64)
1262 Suffix32 = "/32";
Daniel Dunbara9822de2009-08-06 01:47:11 +00001263
Rafael Espindolac1da9812010-11-07 20:14:31 +00001264 std::string Suffix64 = "";
Ted Kremenek43ac2972011-04-05 22:04:27 +00001265 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001266 Suffix64 = "/64";
1267
1268 std::string Lib32 = "lib";
1269
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001270 bool Exists;
1271 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001272 Lib32 = "lib32";
1273
1274 std::string Lib64 = "lib";
Michael J. Spencer2dea7c72011-01-12 23:54:48 +00001275 bool Symlink;
Chris Lattner48aef362011-01-13 01:35:58 +00001276 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1277 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001278 Lib64 = "lib64";
1279
1280 std::string GccTriple = "";
Douglas Gregorf0594d82011-03-06 19:11:49 +00001281 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001282 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1283 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001284 GccTriple = "arm-linux-gnueabi";
1285 } else if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001286 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1287 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001288 GccTriple = "x86_64-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001289 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1290 Exists) && Exists)
Rafael Espindola53dd00b2010-11-17 00:25:26 +00001291 GccTriple = "x86_64-unknown-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001292 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1293 Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001294 GccTriple = "x86_64-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001295 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1296 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001297 GccTriple = "x86_64-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001298 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1299 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001300 GccTriple = "x86_64-suse-linux";
Nick Lewyckye10f9002011-02-01 23:03:29 +00001301 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1302 Exists) && Exists)
1303 GccTriple = "x86_64-manbo-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001304 } else if (Arch == llvm::Triple::x86) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001305 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001306 GccTriple = "i686-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001307 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1308 Exists)
Nuno Lopes2a69ddd2010-11-19 17:26:57 +00001309 GccTriple = "i686-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001310 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1311 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001312 GccTriple = "i486-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001313 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1314 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001315 GccTriple = "i686-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001316 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1317 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001318 GccTriple = "i586-suse-linux";
Ted Kremenekd130c7d2011-04-18 17:50:19 +00001319 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-slackware-linux", Exists)
1320 && Exists)
1321 GccTriple = "i486-slackware-linux";
Ted Kremenek43ac2972011-04-05 22:04:27 +00001322 } else if (Arch == llvm::Triple::ppc) {
1323 if (!llvm::sys::fs::exists("/usr/lib/powerpc-linux-gnu", Exists) && Exists)
1324 GccTriple = "powerpc-linux-gnu";
1325 else if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc-unknown-linux-gnu", Exists) && Exists)
1326 GccTriple = "powerpc-unknown-linux-gnu";
1327 } else if (Arch == llvm::Triple::ppc64) {
1328 if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc64-unknown-linux-gnu", Exists) && Exists)
1329 GccTriple = "powerpc64-unknown-linux-gnu";
1330 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/powerpc64-unknown-linux-gnu", Exists) && Exists)
1331 GccTriple = "powerpc64-unknown-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001332 }
1333
Eric Christopher8f1cc072011-04-06 18:22:53 +00001334 const char* GccVersions[] = {"4.6.0",
1335 "4.5.2", "4.5.1", "4.5",
1336 "4.4.5", "4.4.4", "4.4.3", "4.4",
1337 "4.3.4", "4.3.3", "4.3.2", "4.3",
1338 "4.2.4", "4.2.3", "4.2.2", "4.2.1", "4.2"};
Rafael Espindolac1da9812010-11-07 20:14:31 +00001339 std::string Base = "";
1340 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1341 std::string Suffix = GccTriple + "/" + GccVersions[i];
1342 std::string t1 = "/usr/lib/gcc/" + Suffix;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001343 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001344 Base = t1;
1345 break;
1346 }
1347 std::string t2 = "/usr/lib64/gcc/" + Suffix;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001348 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001349 Base = t2;
1350 break;
1351 }
Ted Kremenek43ac2972011-04-05 22:04:27 +00001352 std::string t3 = "/usr/lib/" + GccTriple + "/gcc/" + Suffix;
1353 if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists) {
1354 Base = t3;
1355 break;
1356 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001357 }
1358
1359 path_list &Paths = getFilePaths();
Ted Kremenek43ac2972011-04-05 22:04:27 +00001360 bool Is32Bits = (getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::ppc);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001361
1362 std::string Suffix;
1363 std::string Lib;
1364
1365 if (Is32Bits) {
1366 Suffix = Suffix32;
1367 Lib = Lib32;
1368 } else {
1369 Suffix = Suffix64;
1370 Lib = Lib64;
1371 }
1372
1373 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001374 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001375 Linker = LinkerPath.str();
1376 else
1377 Linker = GetProgramPath("ld");
1378
1379 LinuxDistro Distro = DetectLinuxDistro(Arch);
1380
Rafael Espindola94c80222010-11-08 14:48:47 +00001381 if (IsUbuntu(Distro)) {
1382 ExtraOpts.push_back("-z");
1383 ExtraOpts.push_back("relro");
1384 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001385
Douglas Gregorf0594d82011-03-06 19:11:49 +00001386 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001387 ExtraOpts.push_back("-X");
1388
1389 if (IsFedora(Distro) || Distro == UbuntuMaverick)
1390 ExtraOpts.push_back("--hash-style=gnu");
1391
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001392 if (IsDebian(Distro) || Distro == UbuntuLucid || Distro == UbuntuJaunty ||
1393 Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001394 ExtraOpts.push_back("--hash-style=both");
1395
1396 if (IsFedora(Distro))
1397 ExtraOpts.push_back("--no-add-needed");
1398
Rafael Espindola021aaa42010-11-10 05:00:22 +00001399 if (Distro == DebianSqueeze || IsOpenSuse(Distro) ||
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001400 IsFedora(Distro) || Distro == UbuntuLucid || Distro == UbuntuMaverick ||
1401 Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001402 ExtraOpts.push_back("--build-id");
1403
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001404 if (Distro == ArchLinux)
1405 Lib = "lib";
1406
Rafael Espindolac1da9812010-11-07 20:14:31 +00001407 Paths.push_back(Base + Suffix);
1408 if (HasMultilib(Arch, Distro)) {
1409 if (IsOpenSuse(Distro) && Is32Bits)
1410 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1411 Paths.push_back(Base + "/../../../../" + Lib);
1412 Paths.push_back("/lib/../" + Lib);
1413 Paths.push_back("/usr/lib/../" + Lib);
1414 }
1415 if (!Suffix.empty())
1416 Paths.push_back(Base);
1417 if (IsOpenSuse(Distro))
1418 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1419 Paths.push_back(Base + "/../../..");
1420 if (Arch == getArch() && IsUbuntu(Distro))
1421 Paths.push_back("/usr/lib/" + GccTriple);
1422}
1423
1424bool Linux::HasNativeLLVMSupport() const {
1425 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00001426}
1427
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001428Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
1429 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001430 Action::ActionClass Key;
1431 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1432 Key = Action::AnalyzeJobClass;
1433 else
1434 Key = JA.getKind();
1435
Rafael Espindoladda5b922010-11-07 23:13:01 +00001436 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1437 options::OPT_no_integrated_as,
1438 IsIntegratedAssemblerDefault());
1439
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001440 Tool *&T = Tools[Key];
1441 if (!T) {
1442 switch (Key) {
1443 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00001444 if (UseIntegratedAs)
1445 T = new tools::ClangAs(*this);
1446 else
1447 T = new tools::linuxtools::Assemble(*this);
1448 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001449 case Action::LinkJobClass:
1450 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001451 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001452 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001453 }
1454 }
1455
1456 return *T;
1457}
1458
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001459/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1460
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001461DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001462 : Generic_ELF(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001463
1464 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001465 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001466 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001467 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001468
Daniel Dunbaree788e72009-12-21 18:54:17 +00001469 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001470 getFilePaths().push_back("/usr/lib");
1471 getFilePaths().push_back("/usr/lib/gcc41");
1472}
1473
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001474Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
1475 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001476 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001477 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001478 Key = Action::AnalyzeJobClass;
1479 else
1480 Key = JA.getKind();
1481
1482 Tool *&T = Tools[Key];
1483 if (!T) {
1484 switch (Key) {
1485 case Action::AssembleJobClass:
1486 T = new tools::dragonfly::Assemble(*this); break;
1487 case Action::LinkJobClass:
1488 T = new tools::dragonfly::Link(*this); break;
1489 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001490 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001491 }
1492 }
1493
1494 return *T;
1495}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001496
1497Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1498 : ToolChain(Host, Triple) {
1499}
1500
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001501Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA,
1502 const ActionList &Inputs) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00001503 Action::ActionClass Key;
1504 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1505 Key = Action::AnalyzeJobClass;
1506 else
1507 Key = JA.getKind();
1508
1509 Tool *&T = Tools[Key];
1510 if (!T) {
1511 switch (Key) {
1512 case Action::InputClass:
1513 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001514 case Action::LipoJobClass:
1515 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001516 assert(0 && "Invalid tool kind.");
1517 case Action::PreprocessJobClass:
1518 case Action::PrecompileJobClass:
1519 case Action::AnalyzeJobClass:
1520 case Action::CompileJobClass:
1521 T = new tools::Clang(*this); break;
1522 case Action::AssembleJobClass:
1523 T = new tools::ClangAs(*this); break;
1524 case Action::LinkJobClass:
1525 T = new tools::visualstudio::Link(*this); break;
1526 }
1527 }
1528
1529 return *T;
1530}
1531
1532bool Windows::IsIntegratedAssemblerDefault() const {
1533 return true;
1534}
1535
1536bool Windows::IsUnwindTablesDefault() const {
1537 // FIXME: Gross; we should probably have some separate target
1538 // definition, possibly even reusing the one in clang.
1539 return getArchName() == "x86_64";
1540}
1541
1542const char *Windows::GetDefaultRelocationModel() const {
1543 return "static";
1544}
1545
1546const char *Windows::GetForcedPicModel() const {
1547 if (getArchName() == "x86_64")
1548 return "pic";
1549 return 0;
1550}