blob: 6127d45fa3a4713dd382e1b2c94bda0eba73a8d3 [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);
151
152 // Mangle the target version into the OS triple component. For historical
153 // reasons that make little sense, the version passed here is the "darwin"
154 // version, which drops the 10 and offsets by 4. See inverse code when
155 // setting the OS version preprocessor define.
156 if (!isTargetIPhoneOS()) {
157 Version[0] = Version[1] + 4;
158 Version[1] = Version[2];
159 Version[2] = 0;
160 } else {
161 // Use the environment to communicate that we are targetting iPhoneOS.
162 Triple.setEnvironmentName("iphoneos");
163 }
164
165 llvm::SmallString<16> Str;
166 llvm::raw_svector_ostream(Str) << "darwin" << Version[0]
167 << "." << Version[1] << "." << Version[2];
168 Triple.setOSName(Str.str());
169
170 return Triple.getTriple();
171}
172
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000173Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
174 const ActionList &Inputs) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000175 Action::ActionClass Key;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000176
177 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
178 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
179 if (Inputs.size() == 1 &&
180 types::isCXX(Inputs[0]->getType()) &&
181 getTriple().getOS() == llvm::Triple::Darwin &&
182 getTriple().getArch() == llvm::Triple::x86 &&
183 C.getArgs().getLastArg(options::OPT_fapple_kext))
184 Key = JA.getKind();
185 else
186 Key = Action::AnalyzeJobClass;
187 } else
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000188 Key = JA.getKind();
189
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000190 // FIXME: This doesn't belong here, but ideally we will support static soon
191 // anyway.
192 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
193 C.getArgs().hasArg(options::OPT_static) ||
194 C.getArgs().hasArg(options::OPT_fapple_kext));
195 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
196 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
197 options::OPT_no_integrated_as,
198 IsIADefault);
199
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000200 Tool *&T = Tools[Key];
201 if (!T) {
202 switch (Key) {
203 case Action::InputClass:
204 case Action::BindArchClass:
205 assert(0 && "Invalid tool kind.");
206 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000207 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000208 case Action::AnalyzeJobClass:
209 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000210 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000211 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000212 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000213 case Action::AssembleJobClass: {
214 if (UseIntegratedAs)
215 T = new tools::ClangAs(*this);
216 else
217 T = new tools::darwin::Assemble(*this);
218 break;
219 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000220 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000221 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000222 case Action::LipoJobClass:
223 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000224 case Action::DsymutilJobClass:
225 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000226 }
227 }
228
229 return *T;
230}
231
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000232
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000233DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
234 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000235{
Daniel Dunbar47023092011-03-18 19:25:15 +0000236 std::string UsrPrefix = "llvm-gcc-4.2/";
237
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000238 getProgramPaths().push_back(getDriver().getInstalledDir());
239 if (getDriver().getInstalledDir() != getDriver().Dir)
240 getProgramPaths().push_back(getDriver().Dir);
241
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000242 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000243 getProgramPaths().push_back(getDriver().getInstalledDir());
244 if (getDriver().getInstalledDir() != getDriver().Dir)
245 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000246
247 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000248 // also add the GCC libexec paths. This is legacy code that can be removed
249 // once fallback is no longer useful.
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000250 std::string ToolChainDir = "i686-apple-darwin";
251 ToolChainDir += llvm::utostr(DarwinVersion[0]);
252 ToolChainDir += "/4.2.1";
253
254 std::string Path = getDriver().Dir;
Daniel Dunbar47023092011-03-18 19:25:15 +0000255 Path += "/../" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000256 Path += ToolChainDir;
257 getProgramPaths().push_back(Path);
258
Daniel Dunbar47023092011-03-18 19:25:15 +0000259 Path = "/usr/" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000260 Path += ToolChainDir;
261 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000262}
263
264void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
265 ArgStringList &CmdArgs) const {
266 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000267
268 // Unfortunately, we still might depend on a few of the libraries that are
269 // only available in the gcc library directory (in particular
270 // libstdc++.dylib). For now, hardcode the path to the known install location.
271 llvm::sys::Path P(getDriver().Dir);
272 P.eraseComponent(); // .../usr/bin -> ../usr
273 P.appendComponent("lib");
274 P.appendComponent("gcc");
275 switch (getTriple().getArch()) {
276 default:
277 assert(0 && "Invalid Darwin arch!");
278 case llvm::Triple::x86:
279 case llvm::Triple::x86_64:
280 P.appendComponent("i686-apple-darwin10");
281 break;
282 case llvm::Triple::arm:
283 case llvm::Triple::thumb:
284 P.appendComponent("arm-apple-darwin10");
285 break;
286 case llvm::Triple::ppc:
287 case llvm::Triple::ppc64:
288 P.appendComponent("powerpc-apple-darwin10");
289 break;
290 }
291 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000292
293 // Determine the arch specific GCC subdirectory.
294 const char *ArchSpecificDir = 0;
295 switch (getTriple().getArch()) {
296 default:
297 break;
298 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000299 case llvm::Triple::thumb: {
300 std::string Triple = ComputeLLVMTriple(Args);
301 llvm::StringRef TripleStr = Triple;
302 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
303 ArchSpecificDir = "v5";
304 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
305 ArchSpecificDir = "v6";
306 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
307 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000308 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000309 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000310 case llvm::Triple::ppc64:
311 ArchSpecificDir = "ppc64";
312 break;
313 case llvm::Triple::x86_64:
314 ArchSpecificDir = "x86_64";
315 break;
316 }
317
318 if (ArchSpecificDir) {
319 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000320 bool Exists;
321 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000322 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
323 P.eraseComponent();
324 }
325
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000326 bool Exists;
327 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000328 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000329}
330
331void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
332 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000333 // Darwin doesn't support real static executables, don't link any runtime
334 // libraries with -static.
335 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000336 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000337
338 // Reject -static-libgcc for now, we can deal with this when and if someone
339 // cares. This is useful in situations where someone wants to statically link
340 // something like libstdc++, and needs its runtime support routines.
341 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000342 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000343 << A->getAsString(Args);
344 return;
345 }
346
Daniel Dunbareec99102010-01-22 03:38:14 +0000347 // Otherwise link libSystem, then the dynamic runtime library, and finally any
348 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000349 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000350
351 // Select the dynamic runtime library and the target specific static library.
352 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000353 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000354 CmdArgs.push_back("-lgcc_s.1");
355
356 // We may need some static functions for armv6/thumb which are required to
357 // be in the same linkage unit as their caller.
358 if (getDarwinArchName(Args) == "armv6")
359 DarwinStaticLib = "libclang_rt.armv6.a";
360 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000361 // The dynamic runtime library was merged with libSystem for 10.6 and
362 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000363 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000364 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000365 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000366 CmdArgs.push_back("-lgcc_s.10.5");
367
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000368 // For OS X, we thought we would only need a static runtime library when
369 // targetting 10.4, to provide versions of the static functions which were
370 // omitted from 10.4.dylib.
371 //
372 // Unfortunately, that turned out to not be true, because Darwin system
373 // headers can still use eprintf on i386, and it is not exported from
374 // libSystem. Therefore, we still must provide a runtime library just for
375 // the tiny tiny handful of projects that *might* use that symbol.
376 if (isMacosxVersionLT(10, 5)) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000377 DarwinStaticLib = "libclang_rt.10.4.a";
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000378 } else {
379 if (getTriple().getArch() == llvm::Triple::x86)
380 DarwinStaticLib = "libclang_rt.eprintf.a";
381 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000382 }
383
384 /// Add the target specific static library, if needed.
385 if (DarwinStaticLib) {
386 llvm::sys::Path P(getDriver().ResourceDir);
387 P.appendComponent("lib");
388 P.appendComponent("darwin");
389 P.appendComponent(DarwinStaticLib);
390
391 // For now, allow missing resource libraries to support developers who may
392 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000393 bool Exists;
394 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareec99102010-01-22 03:38:14 +0000395 CmdArgs.push_back(Args.MakeArgString(P.str()));
396 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000397}
398
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000399void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000400 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000401
Daniel Dunbar26031372010-01-27 00:56:25 +0000402 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
403 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000404 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000405 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000406 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000407 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000408 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000409 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000410 // If neither OS X nor iPhoneOS targets were specified, check for
411 // environment defines.
412 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
413 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000414
Daniel Dunbar816bc312010-01-26 01:45:19 +0000415 // Ignore empty strings.
416 if (OSXTarget && OSXTarget[0] == '\0')
417 OSXTarget = 0;
418 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
419 iPhoneOSTarget = 0;
420
Daniel Dunbar39053672010-02-02 17:31:12 +0000421 // Diagnose conflicting deployment targets, and choose default platform
422 // based on the tool chain.
423 //
424 // FIXME: Don't hardcode default here.
425 if (OSXTarget && iPhoneOSTarget) {
426 // FIXME: We should see if we can get away with warning or erroring on
427 // this. Perhaps put under -pedantic?
428 if (getTriple().getArch() == llvm::Triple::arm ||
429 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000430 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000431 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000432 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000433 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000434
Daniel Dunbar39053672010-02-02 17:31:12 +0000435 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000436 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000437 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
438 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000439 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000440 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000441 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
442 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000443 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000444 // Otherwise, assume we are targeting OS X.
445 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000446 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
447 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000448 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Daniel Dunbar26031372010-01-27 00:56:25 +0000451 // Set the tool chain target information.
452 unsigned Major, Minor, Micro;
453 bool HadExtra;
454 if (OSXVersion) {
455 assert(!iPhoneVersion && "Unknown target platform!");
456 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
457 Micro, HadExtra) || HadExtra ||
458 Major != 10 || Minor >= 10 || Micro >= 10)
459 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
460 << OSXVersion->getAsString(Args);
461 } else {
462 assert(iPhoneVersion && "Unknown target platform!");
463 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
464 Micro, HadExtra) || HadExtra ||
465 Major >= 10 || Minor >= 100 || Micro >= 100)
466 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
467 << iPhoneVersion->getAsString(Args);
468 }
469 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000470}
471
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000472void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000473 ArgStringList &CmdArgs) const {
474 CXXStdlibType Type = GetCXXStdlibType(Args);
475
476 switch (Type) {
477 case ToolChain::CST_Libcxx:
478 CmdArgs.push_back("-lc++");
479 break;
480
481 case ToolChain::CST_Libstdcxx: {
482 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
483 // it was previously found in the gcc lib dir. However, for all the Darwin
484 // platforms we care about it was -lstdc++.6, so we search for that
485 // explicitly if we can't see an obvious -lstdc++ candidate.
486
487 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000488 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000489 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
490 llvm::sys::Path P(A->getValue(Args));
491 P.appendComponent("usr");
492 P.appendComponent("lib");
493 P.appendComponent("libstdc++.dylib");
494
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000495 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000496 P.eraseComponent();
497 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000498 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000499 CmdArgs.push_back(Args.MakeArgString(P.str()));
500 return;
501 }
502 }
503 }
504
505 // Otherwise, look in the root.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000506 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
507 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000508 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
509 return;
510 }
511
512 // Otherwise, let the linker search.
513 CmdArgs.push_back("-lstdc++");
514 break;
515 }
516 }
517}
518
Shantonu Sen7433fed2010-09-17 18:39:08 +0000519void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
520 ArgStringList &CmdArgs) const {
521
522 // For Darwin platforms, use the compiler-rt-based support library
523 // instead of the gcc-provided one (which is also incidentally
524 // only present in the gcc lib dir, which makes it hard to find).
525
526 llvm::sys::Path P(getDriver().ResourceDir);
527 P.appendComponent("lib");
528 P.appendComponent("darwin");
529 P.appendComponent("libclang_rt.cc_kext.a");
530
531 // For now, allow missing resource libraries to support developers who may
532 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000533 bool Exists;
534 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000535 CmdArgs.push_back(Args.MakeArgString(P.str()));
536}
537
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000538DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
539 const char *BoundArch) const {
540 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
541 const OptTable &Opts = getDriver().getOpts();
542
543 // FIXME: We really want to get out of the tool chain level argument
544 // translation business, as it makes the driver functionality much
545 // more opaque. For now, we follow gcc closely solely for the
546 // purpose of easily achieving feature parity & testability. Once we
547 // have something that works, we should reevaluate each translation
548 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000549
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000550 for (ArgList::const_iterator it = Args.begin(),
551 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000552 Arg *A = *it;
553
554 if (A->getOption().matches(options::OPT_Xarch__)) {
555 // FIXME: Canonicalize name.
556 if (getArchName() != A->getValue(Args, 0))
557 continue;
558
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000559 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000560 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
561 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000562 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000564 // If the argument parsing failed or more than one argument was
565 // consumed, the -Xarch_ argument's parameter tried to consume
566 // extra arguments. Emit an error and ignore.
567 //
568 // We also want to disallow any options which would alter the
569 // driver behavior; that isn't going to work in our model. We
570 // use isDriverOption() as an approximation, although things
571 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000572 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000573 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000574 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000575 << A->getAsString(Args);
576 continue;
577 }
578
Daniel Dunbar478edc22009-03-29 22:29:05 +0000579 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000580 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000581
582 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000583
584 // Linker input arguments require custom handling. The problem is that we
585 // have already constructed the phase actions, so we can not treat them as
586 // "input arguments".
587 if (A->getOption().isLinkerInput()) {
588 // Convert the argument into individual Zlinker_input_args.
589 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
590 DAL->AddSeparateArg(OriginalArg,
591 Opts.getOption(options::OPT_Zlinker_input),
592 A->getValue(Args, i));
593
594 }
595 continue;
596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000598
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000599 // Sob. These is strictly gcc compatible for the time being. Apple
600 // gcc translates options twice, which means that self-expanding
601 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000602 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000603 default:
604 DAL->append(A);
605 break;
606
607 case options::OPT_mkernel:
608 case options::OPT_fapple_kext:
609 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000610 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
611 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000612 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000614 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000615 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
616 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000617 break;
618
619 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000620 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
621 DAL->AddFlagArg(A,
622 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000623 break;
624
625 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000626 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
627 DAL->AddFlagArg(A,
628 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000629 break;
630
631 case options::OPT_fterminated_vtables:
632 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000633 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
634 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000635 break;
636
637 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000638 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000639 break;
640
641 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000642 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000643 break;
644
645 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000646 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000647 break;
648
649 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000650 DAL->AddFlagArg(A,
651 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000652 break;
653
654 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000655 DAL->AddFlagArg(A,
656 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000657 break;
658
659 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000660 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000661 break;
662
663 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000664 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000665 break;
666 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000667 }
668
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000669 if (getTriple().getArch() == llvm::Triple::x86 ||
670 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000671 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000672 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000673
674 // Add the arch options based on the particular spelling of -arch, to match
675 // how the driver driver works.
676 if (BoundArch) {
677 llvm::StringRef Name = BoundArch;
678 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
679 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
680
681 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
682 // which defines the list of which architectures we accept.
683 if (Name == "ppc")
684 ;
685 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000686 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000687 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000688 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000689 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000690 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000691 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000692 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000693 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000694 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000695 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000696 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000697 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000698 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000699 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000700 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000701
702 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000703 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000704
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000705 else if (Name == "i386")
706 ;
707 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000708 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000709 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000710 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000711 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000712 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000713 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000714 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000715 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000716 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000717 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000718 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000719 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000720 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000721
722 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000723 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000724
725 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000726 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000727 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000728 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000729 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000730 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000731 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000732 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000733 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000734 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000735 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000736 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000737
738 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000739 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000740 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000741
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000742 // Add an explicit version min argument for the deployment target. We do this
743 // after argument translation because -Xarch_ arguments may add a version min
744 // argument.
745 AddDeploymentTarget(*DAL);
746
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000747 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000748}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000749
Daniel Dunbarf3955282009-09-04 18:34:51 +0000750bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000751 // FIXME: Gross; we should probably have some separate target
752 // definition, possibly even reusing the one in clang.
753 return getArchName() == "x86_64";
754}
755
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000756bool Darwin::UseDwarfDebugFlags() const {
757 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
758 return S[0] != '\0';
759 return false;
760}
761
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000762bool Darwin::UseSjLjExceptions() const {
763 // Darwin uses SjLj exceptions on ARM.
764 return (getTriple().getArch() == llvm::Triple::arm ||
765 getTriple().getArch() == llvm::Triple::thumb);
766}
767
Daniel Dunbarf3955282009-09-04 18:34:51 +0000768const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000769 return "pic";
770}
771
Daniel Dunbarf3955282009-09-04 18:34:51 +0000772const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000773 if (getArchName() == "x86_64")
774 return "pic";
775 return 0;
776}
777
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000778bool Darwin::SupportsProfiling() const {
779 // Profiling instrumentation is only supported on x86.
780 return getArchName() == "i386" || getArchName() == "x86_64";
781}
782
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000783bool Darwin::SupportsObjCGC() const {
784 // Garbage collection is supported everywhere except on iPhone OS.
785 return !isTargetIPhoneOS();
786}
787
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000788std::string
789Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
790 return ComputeLLVMTriple(Args);
791}
792
Daniel Dunbar39176082009-03-20 00:20:03 +0000793/// Generic_GCC - A tool chain using the 'gcc' command to perform
794/// all subcommands; this relies on gcc translating the majority of
795/// command line options.
796
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000797Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000798 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000799 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +0000800 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000801 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000802}
803
Daniel Dunbar39176082009-03-20 00:20:03 +0000804Generic_GCC::~Generic_GCC() {
805 // Free tool implementations.
806 for (llvm::DenseMap<unsigned, Tool*>::iterator
807 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
808 delete it->second;
809}
810
Mike Stump1eb44332009-09-09 15:08:12 +0000811Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000812 const JobAction &JA,
813 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +0000814 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000815 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000816 Key = Action::AnalyzeJobClass;
817 else
818 Key = JA.getKind();
819
820 Tool *&T = Tools[Key];
821 if (!T) {
822 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000823 case Action::InputClass:
824 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000825 assert(0 && "Invalid tool kind.");
826 case Action::PreprocessJobClass:
827 T = new tools::gcc::Preprocess(*this); break;
828 case Action::PrecompileJobClass:
829 T = new tools::gcc::Precompile(*this); break;
830 case Action::AnalyzeJobClass:
831 T = new tools::Clang(*this); break;
832 case Action::CompileJobClass:
833 T = new tools::gcc::Compile(*this); break;
834 case Action::AssembleJobClass:
835 T = new tools::gcc::Assemble(*this); break;
836 case Action::LinkJobClass:
837 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000839 // This is a bit ungeneric, but the only platform using a driver
840 // driver is Darwin.
841 case Action::LipoJobClass:
842 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000843 case Action::DsymutilJobClass:
844 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000845 }
846 }
847
848 return *T;
849}
850
Daniel Dunbar39176082009-03-20 00:20:03 +0000851bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000852 // FIXME: Gross; we should probably have some separate target
853 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000854 return getArchName() == "x86_64";
855}
856
857const char *Generic_GCC::GetDefaultRelocationModel() const {
858 return "static";
859}
860
861const char *Generic_GCC::GetForcedPicModel() const {
862 return 0;
863}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000864
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000865/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
866/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
867/// Currently does not support anything else but compilation.
868
869TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
870 : ToolChain(Host, Triple) {
871 // Path mangling to find libexec
872 std::string Path(getDriver().Dir);
873
874 Path += "/../libexec";
875 getProgramPaths().push_back(Path);
876}
877
878TCEToolChain::~TCEToolChain() {
879 for (llvm::DenseMap<unsigned, Tool*>::iterator
880 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
881 delete it->second;
882}
883
884bool TCEToolChain::IsMathErrnoDefault() const {
885 return true;
886}
887
888bool TCEToolChain::IsUnwindTablesDefault() const {
889 return false;
890}
891
892const char *TCEToolChain::GetDefaultRelocationModel() const {
893 return "static";
894}
895
896const char *TCEToolChain::GetForcedPicModel() const {
897 return 0;
898}
899
900Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000901 const JobAction &JA,
902 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000903 Action::ActionClass Key;
904 Key = Action::AnalyzeJobClass;
905
906 Tool *&T = Tools[Key];
907 if (!T) {
908 switch (Key) {
909 case Action::PreprocessJobClass:
910 T = new tools::gcc::Preprocess(*this); break;
911 case Action::AnalyzeJobClass:
912 T = new tools::Clang(*this); break;
913 default:
914 assert(false && "Unsupported action for TCE target.");
915 }
916 }
917 return *T;
918}
919
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000920/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
921
922OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +0000923 : Generic_ELF(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000924 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000925 getFilePaths().push_back("/usr/lib");
926}
927
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000928Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
929 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000930 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000931 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000932 Key = Action::AnalyzeJobClass;
933 else
934 Key = JA.getKind();
935
Rafael Espindoladda5b922010-11-07 23:13:01 +0000936 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
937 options::OPT_no_integrated_as,
938 IsIntegratedAssemblerDefault());
939
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000940 Tool *&T = Tools[Key];
941 if (!T) {
942 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +0000943 case Action::AssembleJobClass: {
944 if (UseIntegratedAs)
945 T = new tools::ClangAs(*this);
946 else
947 T = new tools::openbsd::Assemble(*this);
948 break;
949 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000950 case Action::LinkJobClass:
951 T = new tools::openbsd::Link(*this); break;
952 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000953 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000954 }
955 }
956
957 return *T;
958}
959
Daniel Dunbar75358d22009-03-30 21:06:03 +0000960/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
961
Daniel Dunbar214afe92010-08-02 05:43:59 +0000962FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +0000963 : Generic_ELF(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +0000964
965 // Determine if we are compiling 32-bit code on an x86_64 platform.
966 bool Lib32 = false;
967 if (Triple.getArch() == llvm::Triple::x86 &&
968 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
969 llvm::Triple::x86_64)
970 Lib32 = true;
971
Daniel Dunbarbc534662009-04-02 18:30:04 +0000972 if (Lib32) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000973 getFilePaths().push_back("/usr/lib32");
974 } else {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000975 getFilePaths().push_back("/usr/lib");
976 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000977}
978
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000979Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
980 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000981 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000982 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000983 Key = Action::AnalyzeJobClass;
984 else
985 Key = JA.getKind();
986
Roman Divacky67dece72010-11-08 17:46:39 +0000987 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
988 options::OPT_no_integrated_as,
989 IsIntegratedAssemblerDefault());
990
Daniel Dunbar75358d22009-03-30 21:06:03 +0000991 Tool *&T = Tools[Key];
992 if (!T) {
993 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000994 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +0000995 if (UseIntegratedAs)
996 T = new tools::ClangAs(*this);
997 else
998 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +0000999 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001000 case Action::LinkJobClass:
1001 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001002 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001003 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001004 }
1005 }
1006
1007 return *T;
1008}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001009
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001010/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1011
1012NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple)
1013 : Generic_ELF(Host, Triple) {
1014
1015 // Determine if we are compiling 32-bit code on an x86_64 platform.
1016 bool Lib32 = false;
1017 if (Triple.getArch() == llvm::Triple::x86 &&
1018 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1019 llvm::Triple::x86_64)
1020 Lib32 = true;
1021
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001022 if (getDriver().UseStdLib) {
1023 if (Lib32)
1024 getFilePaths().push_back("=/usr/lib/i386");
1025 else
1026 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001027 }
1028}
1029
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001030Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1031 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001032 Action::ActionClass Key;
1033 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1034 Key = Action::AnalyzeJobClass;
1035 else
1036 Key = JA.getKind();
1037
1038 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1039 options::OPT_no_integrated_as,
1040 IsIntegratedAssemblerDefault());
1041
1042 Tool *&T = Tools[Key];
1043 if (!T) {
1044 switch (Key) {
1045 case Action::AssembleJobClass:
1046 if (UseIntegratedAs)
1047 T = new tools::ClangAs(*this);
1048 else
1049 T = new tools::netbsd::Assemble(*this);
1050 break;
1051 case Action::LinkJobClass:
1052 T = new tools::netbsd::Link(*this); break;
1053 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001054 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001055 }
1056 }
1057
1058 return *T;
1059}
1060
Chris Lattner38e317d2010-07-07 16:01:42 +00001061/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1062
1063Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1064 : Generic_GCC(Host, Triple) {
1065 getFilePaths().push_back(getDriver().Dir + "/../lib");
1066 getFilePaths().push_back("/usr/lib");
1067 getFilePaths().push_back("/usr/gnu/lib");
1068 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1069}
1070
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001071Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1072 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001073 Action::ActionClass Key;
1074 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1075 Key = Action::AnalyzeJobClass;
1076 else
1077 Key = JA.getKind();
1078
1079 Tool *&T = Tools[Key];
1080 if (!T) {
1081 switch (Key) {
1082 case Action::AssembleJobClass:
1083 T = new tools::minix::Assemble(*this); break;
1084 case Action::LinkJobClass:
1085 T = new tools::minix::Link(*this); break;
1086 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001087 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001088 }
1089 }
1090
1091 return *T;
1092}
1093
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001094/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1095
1096AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1097 : Generic_GCC(Host, Triple) {
1098
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001099 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001100 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001101 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001102
Daniel Dunbaree788e72009-12-21 18:54:17 +00001103 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001104 getFilePaths().push_back("/usr/lib");
1105 getFilePaths().push_back("/usr/sfw/lib");
1106 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001107 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001108
1109}
1110
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001111Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1112 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001113 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001114 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001115 Key = Action::AnalyzeJobClass;
1116 else
1117 Key = JA.getKind();
1118
1119 Tool *&T = Tools[Key];
1120 if (!T) {
1121 switch (Key) {
1122 case Action::AssembleJobClass:
1123 T = new tools::auroraux::Assemble(*this); break;
1124 case Action::LinkJobClass:
1125 T = new tools::auroraux::Link(*this); break;
1126 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001127 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001128 }
1129 }
1130
1131 return *T;
1132}
1133
1134
Eli Friedman6b3454a2009-05-26 07:52:18 +00001135/// Linux toolchain (very bare-bones at the moment).
1136
Rafael Espindolac1da9812010-11-07 20:14:31 +00001137enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001138 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001139 DebianLenny,
1140 DebianSqueeze,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001141 Exherbo,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001142 Fedora13,
1143 Fedora14,
1144 OpenSuse11_3,
Douglas Gregor814638e2011-03-14 15:39:50 +00001145 UbuntuHardy,
1146 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001147 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001148 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001149 UbuntuLucid,
1150 UbuntuMaverick,
1151 UnknownDistro
1152};
1153
1154static bool IsFedora(enum LinuxDistro Distro) {
1155 return Distro == Fedora13 || Distro == Fedora14;
1156}
1157
1158static bool IsOpenSuse(enum LinuxDistro Distro) {
1159 return Distro == OpenSuse11_3;
1160}
1161
1162static bool IsDebian(enum LinuxDistro Distro) {
1163 return Distro == DebianLenny || Distro == DebianSqueeze;
1164}
1165
1166static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001167 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
1168 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001169 Distro == UbuntuJaunty || Distro == UbuntuKarmic;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001170}
1171
1172static bool IsDebianBased(enum LinuxDistro Distro) {
1173 return IsDebian(Distro) || IsUbuntu(Distro);
1174}
1175
1176static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001177 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001178 bool Exists;
1179 if (Distro == Exherbo &&
1180 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001181 return false;
1182
Rafael Espindolac1da9812010-11-07 20:14:31 +00001183 return true;
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001184 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001185 if (Arch == llvm::Triple::x86 && IsDebianBased(Distro))
1186 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;
1199 if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1200 return UbuntuIntrepid;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001201 if (Lines[i] == "DISTRIB_CODENAME=maverick")
1202 return UbuntuMaverick;
1203 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1204 return UbuntuLucid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001205 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001206 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001207 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1208 return UbuntuKarmic;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001209 }
1210 return UnknownDistro;
1211 }
1212
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001213 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1214 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001215 if (Data.startswith("Fedora release 14 (Laughlin)"))
1216 return Fedora14;
1217 else if (Data.startswith("Fedora release 13 (Goddard)"))
1218 return Fedora13;
1219 return UnknownDistro;
1220 }
1221
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001222 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1223 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001224 if (Data[0] == '5')
1225 return DebianLenny;
1226 else if (Data.startswith("squeeze/sid"))
1227 return DebianSqueeze;
1228 return UnknownDistro;
1229 }
1230
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001231 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1232 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001233 if (Data.startswith("openSUSE 11.3"))
1234 return OpenSuse11_3;
1235 return UnknownDistro;
1236 }
1237
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001238 bool Exists;
1239 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001240 return Exherbo;
1241
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001242 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1243 return ArchLinux;
1244
Rafael Espindolac1da9812010-11-07 20:14:31 +00001245 return UnknownDistro;
1246}
1247
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001248Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001249 : Generic_ELF(Host, Triple) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001250 llvm::Triple::ArchType Arch =
1251 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Daniel Dunbara9822de2009-08-06 01:47:11 +00001252
Rafael Espindolac1da9812010-11-07 20:14:31 +00001253 std::string Suffix32 = "";
1254 if (Arch == llvm::Triple::x86_64)
1255 Suffix32 = "/32";
Daniel Dunbara9822de2009-08-06 01:47:11 +00001256
Rafael Espindolac1da9812010-11-07 20:14:31 +00001257 std::string Suffix64 = "";
1258 if (Arch == llvm::Triple::x86)
1259 Suffix64 = "/64";
1260
1261 std::string Lib32 = "lib";
1262
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001263 bool Exists;
1264 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001265 Lib32 = "lib32";
1266
1267 std::string Lib64 = "lib";
Michael J. Spencer2dea7c72011-01-12 23:54:48 +00001268 bool Symlink;
Chris Lattner48aef362011-01-13 01:35:58 +00001269 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1270 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001271 Lib64 = "lib64";
1272
1273 std::string GccTriple = "";
Douglas Gregorf0594d82011-03-06 19:11:49 +00001274 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001275 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1276 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001277 GccTriple = "arm-linux-gnueabi";
1278 } else if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001279 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1280 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001281 GccTriple = "x86_64-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001282 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1283 Exists) && Exists)
Rafael Espindola53dd00b2010-11-17 00:25:26 +00001284 GccTriple = "x86_64-unknown-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001285 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1286 Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001287 GccTriple = "x86_64-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001288 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1289 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001290 GccTriple = "x86_64-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001291 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1292 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001293 GccTriple = "x86_64-suse-linux";
Nick Lewyckye10f9002011-02-01 23:03:29 +00001294 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1295 Exists) && Exists)
1296 GccTriple = "x86_64-manbo-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001297 } else if (Arch == llvm::Triple::x86) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001298 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001299 GccTriple = "i686-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001300 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1301 Exists)
Nuno Lopes2a69ddd2010-11-19 17:26:57 +00001302 GccTriple = "i686-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001303 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1304 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001305 GccTriple = "i486-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001306 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1307 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001308 GccTriple = "i686-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001309 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1310 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001311 GccTriple = "i586-suse-linux";
1312 }
1313
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001314 const char* GccVersions[] = {"4.5.2", "4.5.1", "4.5", "4.4.5", "4.4.4",
1315 "4.4.3", "4.4", "4.3.4", "4.3.3", "4.3.2",
Douglas Gregor814638e2011-03-14 15:39:50 +00001316 "4.3", "4.2.4", "4.2.3", "4.2.2", "4.2.1",
1317 "4.2"};
Rafael Espindolac1da9812010-11-07 20:14:31 +00001318 std::string Base = "";
1319 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1320 std::string Suffix = GccTriple + "/" + GccVersions[i];
1321 std::string t1 = "/usr/lib/gcc/" + Suffix;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001322 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001323 Base = t1;
1324 break;
1325 }
1326 std::string t2 = "/usr/lib64/gcc/" + Suffix;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001327 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001328 Base = t2;
1329 break;
1330 }
1331 }
1332
1333 path_list &Paths = getFilePaths();
1334 bool Is32Bits = getArch() == llvm::Triple::x86;
1335
1336 std::string Suffix;
1337 std::string Lib;
1338
1339 if (Is32Bits) {
1340 Suffix = Suffix32;
1341 Lib = Lib32;
1342 } else {
1343 Suffix = Suffix64;
1344 Lib = Lib64;
1345 }
1346
1347 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001348 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001349 Linker = LinkerPath.str();
1350 else
1351 Linker = GetProgramPath("ld");
1352
1353 LinuxDistro Distro = DetectLinuxDistro(Arch);
1354
Rafael Espindola94c80222010-11-08 14:48:47 +00001355 if (IsUbuntu(Distro)) {
1356 ExtraOpts.push_back("-z");
1357 ExtraOpts.push_back("relro");
1358 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001359
Douglas Gregorf0594d82011-03-06 19:11:49 +00001360 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001361 ExtraOpts.push_back("-X");
1362
1363 if (IsFedora(Distro) || Distro == UbuntuMaverick)
1364 ExtraOpts.push_back("--hash-style=gnu");
1365
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001366 if (IsDebian(Distro) || Distro == UbuntuLucid || Distro == UbuntuJaunty ||
1367 Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001368 ExtraOpts.push_back("--hash-style=both");
1369
1370 if (IsFedora(Distro))
1371 ExtraOpts.push_back("--no-add-needed");
1372
Rafael Espindola021aaa42010-11-10 05:00:22 +00001373 if (Distro == DebianSqueeze || IsOpenSuse(Distro) ||
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001374 IsFedora(Distro) || Distro == UbuntuLucid || Distro == UbuntuMaverick ||
1375 Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001376 ExtraOpts.push_back("--build-id");
1377
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001378 if (Distro == ArchLinux)
1379 Lib = "lib";
1380
Rafael Espindolac1da9812010-11-07 20:14:31 +00001381 Paths.push_back(Base + Suffix);
1382 if (HasMultilib(Arch, Distro)) {
1383 if (IsOpenSuse(Distro) && Is32Bits)
1384 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1385 Paths.push_back(Base + "/../../../../" + Lib);
1386 Paths.push_back("/lib/../" + Lib);
1387 Paths.push_back("/usr/lib/../" + Lib);
1388 }
1389 if (!Suffix.empty())
1390 Paths.push_back(Base);
1391 if (IsOpenSuse(Distro))
1392 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1393 Paths.push_back(Base + "/../../..");
1394 if (Arch == getArch() && IsUbuntu(Distro))
1395 Paths.push_back("/usr/lib/" + GccTriple);
1396}
1397
1398bool Linux::HasNativeLLVMSupport() const {
1399 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00001400}
1401
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001402Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
1403 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001404 Action::ActionClass Key;
1405 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1406 Key = Action::AnalyzeJobClass;
1407 else
1408 Key = JA.getKind();
1409
Rafael Espindoladda5b922010-11-07 23:13:01 +00001410 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1411 options::OPT_no_integrated_as,
1412 IsIntegratedAssemblerDefault());
1413
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001414 Tool *&T = Tools[Key];
1415 if (!T) {
1416 switch (Key) {
1417 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00001418 if (UseIntegratedAs)
1419 T = new tools::ClangAs(*this);
1420 else
1421 T = new tools::linuxtools::Assemble(*this);
1422 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001423 case Action::LinkJobClass:
1424 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001425 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001426 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001427 }
1428 }
1429
1430 return *T;
1431}
1432
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001433/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1434
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001435DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001436 : Generic_ELF(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001437
1438 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001439 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001440 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001441 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001442
Daniel Dunbaree788e72009-12-21 18:54:17 +00001443 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001444 getFilePaths().push_back("/usr/lib");
1445 getFilePaths().push_back("/usr/lib/gcc41");
1446}
1447
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001448Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
1449 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001450 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001451 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001452 Key = Action::AnalyzeJobClass;
1453 else
1454 Key = JA.getKind();
1455
1456 Tool *&T = Tools[Key];
1457 if (!T) {
1458 switch (Key) {
1459 case Action::AssembleJobClass:
1460 T = new tools::dragonfly::Assemble(*this); break;
1461 case Action::LinkJobClass:
1462 T = new tools::dragonfly::Link(*this); break;
1463 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001464 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001465 }
1466 }
1467
1468 return *T;
1469}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001470
1471Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1472 : ToolChain(Host, Triple) {
1473}
1474
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001475Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA,
1476 const ActionList &Inputs) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00001477 Action::ActionClass Key;
1478 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1479 Key = Action::AnalyzeJobClass;
1480 else
1481 Key = JA.getKind();
1482
1483 Tool *&T = Tools[Key];
1484 if (!T) {
1485 switch (Key) {
1486 case Action::InputClass:
1487 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001488 case Action::LipoJobClass:
1489 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001490 assert(0 && "Invalid tool kind.");
1491 case Action::PreprocessJobClass:
1492 case Action::PrecompileJobClass:
1493 case Action::AnalyzeJobClass:
1494 case Action::CompileJobClass:
1495 T = new tools::Clang(*this); break;
1496 case Action::AssembleJobClass:
1497 T = new tools::ClangAs(*this); break;
1498 case Action::LinkJobClass:
1499 T = new tools::visualstudio::Link(*this); break;
1500 }
1501 }
1502
1503 return *T;
1504}
1505
1506bool Windows::IsIntegratedAssemblerDefault() const {
1507 return true;
1508}
1509
1510bool Windows::IsUnwindTablesDefault() const {
1511 // FIXME: Gross; we should probably have some separate target
1512 // definition, possibly even reusing the one in clang.
1513 return getArchName() == "x86_64";
1514}
1515
1516const char *Windows::GetDefaultRelocationModel() const {
1517 return "static";
1518}
1519
1520const char *Windows::GetForcedPicModel() const {
1521 if (getArchName() == "x86_64")
1522 return "pic";
1523 return 0;
1524}