blob: 41d7a941a155d27561f66de7701c51d3c8dcd26d [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" ||
104 Value == "arm1176jz-s" || Value == "arm1176jzf-s")
105 return "armv6";
106
107 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
108 return "armv7";
109
110 return 0;
111}
112
113llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
114 switch (getTriple().getArch()) {
115 default:
116 return getArchName();
Douglas Gregorf0594d82011-03-06 19:11:49 +0000117
118 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000119 case llvm::Triple::arm: {
120 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
121 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
122 return Arch;
123
124 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
125 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
126 return Arch;
127
128 return "arm";
129 }
130 }
131}
132
Daniel Dunbarf3955282009-09-04 18:34:51 +0000133Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000134 // Free tool implementations.
135 for (llvm::DenseMap<unsigned, Tool*>::iterator
136 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
137 delete it->second;
138}
139
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000140std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
141 llvm::Triple Triple(ComputeLLVMTriple(Args));
142
143 // If the target isn't initialized (e.g., an unknown Darwin platform, return
144 // the default triple).
145 if (!isTargetInitialized())
146 return Triple.getTriple();
147
148 unsigned Version[3];
149 getTargetVersion(Version);
150
151 // Mangle the target version into the OS triple component. For historical
152 // reasons that make little sense, the version passed here is the "darwin"
153 // version, which drops the 10 and offsets by 4. See inverse code when
154 // setting the OS version preprocessor define.
155 if (!isTargetIPhoneOS()) {
156 Version[0] = Version[1] + 4;
157 Version[1] = Version[2];
158 Version[2] = 0;
159 } else {
160 // Use the environment to communicate that we are targetting iPhoneOS.
161 Triple.setEnvironmentName("iphoneos");
162 }
163
164 llvm::SmallString<16> Str;
165 llvm::raw_svector_ostream(Str) << "darwin" << Version[0]
166 << "." << Version[1] << "." << Version[2];
167 Triple.setOSName(Str.str());
168
169 return Triple.getTriple();
170}
171
Daniel Dunbarf3955282009-09-04 18:34:51 +0000172Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000173 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000174 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000175 Key = Action::AnalyzeJobClass;
176 else
177 Key = JA.getKind();
178
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000179 // FIXME: This doesn't belong here, but ideally we will support static soon
180 // anyway.
181 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
182 C.getArgs().hasArg(options::OPT_static) ||
183 C.getArgs().hasArg(options::OPT_fapple_kext));
184 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
185 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
186 options::OPT_no_integrated_as,
187 IsIADefault);
188
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000189 Tool *&T = Tools[Key];
190 if (!T) {
191 switch (Key) {
192 case Action::InputClass:
193 case Action::BindArchClass:
194 assert(0 && "Invalid tool kind.");
195 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000196 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000197 case Action::AnalyzeJobClass:
198 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000199 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000200 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000201 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000202 case Action::AssembleJobClass: {
203 if (UseIntegratedAs)
204 T = new tools::ClangAs(*this);
205 else
206 T = new tools::darwin::Assemble(*this);
207 break;
208 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000209 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000210 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000211 case Action::LipoJobClass:
212 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000213 case Action::DsymutilJobClass:
214 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000215 }
216 }
217
218 return *T;
219}
220
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000221
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000222DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
223 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000224{
Daniel Dunbar47023092011-03-18 19:25:15 +0000225 std::string UsrPrefix = "llvm-gcc-4.2/";
226
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000227 getProgramPaths().push_back(getDriver().getInstalledDir());
228 if (getDriver().getInstalledDir() != getDriver().Dir)
229 getProgramPaths().push_back(getDriver().Dir);
230
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000231 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000232 getProgramPaths().push_back(getDriver().getInstalledDir());
233 if (getDriver().getInstalledDir() != getDriver().Dir)
234 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000235
236 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000237 // also add the GCC libexec paths. This is legacy code that can be removed
238 // once fallback is no longer useful.
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000239 std::string ToolChainDir = "i686-apple-darwin";
240 ToolChainDir += llvm::utostr(DarwinVersion[0]);
241 ToolChainDir += "/4.2.1";
242
243 std::string Path = getDriver().Dir;
Daniel Dunbar47023092011-03-18 19:25:15 +0000244 Path += "/../" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000245 Path += ToolChainDir;
246 getProgramPaths().push_back(Path);
247
Daniel Dunbar47023092011-03-18 19:25:15 +0000248 Path = "/usr/" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000249 Path += ToolChainDir;
250 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000251}
252
253void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
254 ArgStringList &CmdArgs) const {
255 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000256
257 // Unfortunately, we still might depend on a few of the libraries that are
258 // only available in the gcc library directory (in particular
259 // libstdc++.dylib). For now, hardcode the path to the known install location.
260 llvm::sys::Path P(getDriver().Dir);
261 P.eraseComponent(); // .../usr/bin -> ../usr
262 P.appendComponent("lib");
263 P.appendComponent("gcc");
264 switch (getTriple().getArch()) {
265 default:
266 assert(0 && "Invalid Darwin arch!");
267 case llvm::Triple::x86:
268 case llvm::Triple::x86_64:
269 P.appendComponent("i686-apple-darwin10");
270 break;
271 case llvm::Triple::arm:
272 case llvm::Triple::thumb:
273 P.appendComponent("arm-apple-darwin10");
274 break;
275 case llvm::Triple::ppc:
276 case llvm::Triple::ppc64:
277 P.appendComponent("powerpc-apple-darwin10");
278 break;
279 }
280 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000281
282 // Determine the arch specific GCC subdirectory.
283 const char *ArchSpecificDir = 0;
284 switch (getTriple().getArch()) {
285 default:
286 break;
287 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000288 case llvm::Triple::thumb: {
289 std::string Triple = ComputeLLVMTriple(Args);
290 llvm::StringRef TripleStr = Triple;
291 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
292 ArchSpecificDir = "v5";
293 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
294 ArchSpecificDir = "v6";
295 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
296 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000297 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000298 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000299 case llvm::Triple::ppc64:
300 ArchSpecificDir = "ppc64";
301 break;
302 case llvm::Triple::x86_64:
303 ArchSpecificDir = "x86_64";
304 break;
305 }
306
307 if (ArchSpecificDir) {
308 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000309 bool Exists;
310 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000311 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
312 P.eraseComponent();
313 }
314
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000315 bool Exists;
316 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000317 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000318}
319
320void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
321 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000322 // Darwin doesn't support real static executables, don't link any runtime
323 // libraries with -static.
324 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000325 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000326
327 // Reject -static-libgcc for now, we can deal with this when and if someone
328 // cares. This is useful in situations where someone wants to statically link
329 // something like libstdc++, and needs its runtime support routines.
330 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000331 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000332 << A->getAsString(Args);
333 return;
334 }
335
Daniel Dunbareec99102010-01-22 03:38:14 +0000336 // Otherwise link libSystem, then the dynamic runtime library, and finally any
337 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000338 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000339
340 // Select the dynamic runtime library and the target specific static library.
341 const char *DarwinStaticLib = 0;
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000342 if (isTargetIPhoneOS()) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000343 CmdArgs.push_back("-lgcc_s.1");
344
345 // We may need some static functions for armv6/thumb which are required to
346 // be in the same linkage unit as their caller.
347 if (getDarwinArchName(Args) == "armv6")
348 DarwinStaticLib = "libclang_rt.armv6.a";
349 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000350 // The dynamic runtime library was merged with libSystem for 10.6 and
351 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000352 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000353 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000354 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000355 CmdArgs.push_back("-lgcc_s.10.5");
356
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000357 // For OS X, we thought we would only need a static runtime library when
358 // targetting 10.4, to provide versions of the static functions which were
359 // omitted from 10.4.dylib.
360 //
361 // Unfortunately, that turned out to not be true, because Darwin system
362 // headers can still use eprintf on i386, and it is not exported from
363 // libSystem. Therefore, we still must provide a runtime library just for
364 // the tiny tiny handful of projects that *might* use that symbol.
365 if (isMacosxVersionLT(10, 5)) {
Daniel Dunbareec99102010-01-22 03:38:14 +0000366 DarwinStaticLib = "libclang_rt.10.4.a";
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000367 } else {
368 if (getTriple().getArch() == llvm::Triple::x86)
369 DarwinStaticLib = "libclang_rt.eprintf.a";
370 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000371 }
372
373 /// Add the target specific static library, if needed.
374 if (DarwinStaticLib) {
375 llvm::sys::Path P(getDriver().ResourceDir);
376 P.appendComponent("lib");
377 P.appendComponent("darwin");
378 P.appendComponent(DarwinStaticLib);
379
380 // For now, allow missing resource libraries to support developers who may
381 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000382 bool Exists;
383 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareec99102010-01-22 03:38:14 +0000384 CmdArgs.push_back(Args.MakeArgString(P.str()));
385 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000386}
387
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000388void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000389 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000390
Daniel Dunbar26031372010-01-27 00:56:25 +0000391 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
392 Arg *iPhoneVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000393 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000394 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000395 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000396 << iPhoneVersion->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000397 iPhoneVersion = 0;
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000398 } else if (!OSXVersion && !iPhoneVersion) {
Daniel Dunbar816bc312010-01-26 01:45:19 +0000399 // If neither OS X nor iPhoneOS targets were specified, check for
400 // environment defines.
401 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
402 const char *iPhoneOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000403
Daniel Dunbar816bc312010-01-26 01:45:19 +0000404 // Ignore empty strings.
405 if (OSXTarget && OSXTarget[0] == '\0')
406 OSXTarget = 0;
407 if (iPhoneOSTarget && iPhoneOSTarget[0] == '\0')
408 iPhoneOSTarget = 0;
409
Daniel Dunbar39053672010-02-02 17:31:12 +0000410 // Diagnose conflicting deployment targets, and choose default platform
411 // based on the tool chain.
412 //
413 // FIXME: Don't hardcode default here.
414 if (OSXTarget && iPhoneOSTarget) {
415 // FIXME: We should see if we can get away with warning or erroring on
416 // this. Perhaps put under -pedantic?
417 if (getTriple().getArch() == llvm::Triple::arm ||
418 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000419 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000420 else
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000421 iPhoneOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000422 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000423
Daniel Dunbar39053672010-02-02 17:31:12 +0000424 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000425 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000426 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
427 Args.append(OSXVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000428 } else if (iPhoneOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000429 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000430 iPhoneVersion = Args.MakeJoinedArg(0, O, iPhoneOSTarget);
431 Args.append(iPhoneVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000432 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000433 // Otherwise, assume we are targeting OS X.
434 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000435 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
436 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000437 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Daniel Dunbar26031372010-01-27 00:56:25 +0000440 // Set the tool chain target information.
441 unsigned Major, Minor, Micro;
442 bool HadExtra;
443 if (OSXVersion) {
444 assert(!iPhoneVersion && "Unknown target platform!");
445 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
446 Micro, HadExtra) || HadExtra ||
447 Major != 10 || Minor >= 10 || Micro >= 10)
448 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
449 << OSXVersion->getAsString(Args);
450 } else {
451 assert(iPhoneVersion && "Unknown target platform!");
452 if (!Driver::GetReleaseVersion(iPhoneVersion->getValue(Args), Major, Minor,
453 Micro, HadExtra) || HadExtra ||
454 Major >= 10 || Minor >= 100 || Micro >= 100)
455 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
456 << iPhoneVersion->getAsString(Args);
457 }
458 setTarget(iPhoneVersion, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000459}
460
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000461void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000462 ArgStringList &CmdArgs) const {
463 CXXStdlibType Type = GetCXXStdlibType(Args);
464
465 switch (Type) {
466 case ToolChain::CST_Libcxx:
467 CmdArgs.push_back("-lc++");
468 break;
469
470 case ToolChain::CST_Libstdcxx: {
471 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
472 // it was previously found in the gcc lib dir. However, for all the Darwin
473 // platforms we care about it was -lstdc++.6, so we search for that
474 // explicitly if we can't see an obvious -lstdc++ candidate.
475
476 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000477 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000478 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
479 llvm::sys::Path P(A->getValue(Args));
480 P.appendComponent("usr");
481 P.appendComponent("lib");
482 P.appendComponent("libstdc++.dylib");
483
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 P.eraseComponent();
486 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000487 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000488 CmdArgs.push_back(Args.MakeArgString(P.str()));
489 return;
490 }
491 }
492 }
493
494 // Otherwise, look in the root.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000495 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
496 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000497 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
498 return;
499 }
500
501 // Otherwise, let the linker search.
502 CmdArgs.push_back("-lstdc++");
503 break;
504 }
505 }
506}
507
Shantonu Sen7433fed2010-09-17 18:39:08 +0000508void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
509 ArgStringList &CmdArgs) const {
510
511 // For Darwin platforms, use the compiler-rt-based support library
512 // instead of the gcc-provided one (which is also incidentally
513 // only present in the gcc lib dir, which makes it hard to find).
514
515 llvm::sys::Path P(getDriver().ResourceDir);
516 P.appendComponent("lib");
517 P.appendComponent("darwin");
518 P.appendComponent("libclang_rt.cc_kext.a");
519
520 // For now, allow missing resource libraries to support developers who may
521 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000522 bool Exists;
523 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000524 CmdArgs.push_back(Args.MakeArgString(P.str()));
525}
526
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000527DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
528 const char *BoundArch) const {
529 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
530 const OptTable &Opts = getDriver().getOpts();
531
532 // FIXME: We really want to get out of the tool chain level argument
533 // translation business, as it makes the driver functionality much
534 // more opaque. For now, we follow gcc closely solely for the
535 // purpose of easily achieving feature parity & testability. Once we
536 // have something that works, we should reevaluate each translation
537 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000538
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000539 for (ArgList::const_iterator it = Args.begin(),
540 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000541 Arg *A = *it;
542
543 if (A->getOption().matches(options::OPT_Xarch__)) {
544 // FIXME: Canonicalize name.
545 if (getArchName() != A->getValue(Args, 0))
546 continue;
547
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000548 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000549 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
550 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000551 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000553 // If the argument parsing failed or more than one argument was
554 // consumed, the -Xarch_ argument's parameter tried to consume
555 // extra arguments. Emit an error and ignore.
556 //
557 // We also want to disallow any options which would alter the
558 // driver behavior; that isn't going to work in our model. We
559 // use isDriverOption() as an approximation, although things
560 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000561 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000562 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000563 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000564 << A->getAsString(Args);
565 continue;
566 }
567
Daniel Dunbar478edc22009-03-29 22:29:05 +0000568 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000569 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000570
571 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000572
573 // Linker input arguments require custom handling. The problem is that we
574 // have already constructed the phase actions, so we can not treat them as
575 // "input arguments".
576 if (A->getOption().isLinkerInput()) {
577 // Convert the argument into individual Zlinker_input_args.
578 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
579 DAL->AddSeparateArg(OriginalArg,
580 Opts.getOption(options::OPT_Zlinker_input),
581 A->getValue(Args, i));
582
583 }
584 continue;
585 }
Mike Stump1eb44332009-09-09 15:08:12 +0000586 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000587
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000588 // Sob. These is strictly gcc compatible for the time being. Apple
589 // gcc translates options twice, which means that self-expanding
590 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000591 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000592 default:
593 DAL->append(A);
594 break;
595
596 case options::OPT_mkernel:
597 case options::OPT_fapple_kext:
598 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000599 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
600 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000601 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000603 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000604 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
605 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000606 break;
607
608 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000609 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
610 DAL->AddFlagArg(A,
611 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000612 break;
613
614 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000615 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
616 DAL->AddFlagArg(A,
617 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000618 break;
619
620 case options::OPT_fterminated_vtables:
621 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000622 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
623 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000624 break;
625
626 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000627 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000628 break;
629
630 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000631 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000632 break;
633
634 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000635 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000636 break;
637
638 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000639 DAL->AddFlagArg(A,
640 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000641 break;
642
643 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000644 DAL->AddFlagArg(A,
645 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000646 break;
647
648 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000649 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000650 break;
651
652 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000653 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000654 break;
655 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000656 }
657
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000658 if (getTriple().getArch() == llvm::Triple::x86 ||
659 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000660 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000661 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000662
663 // Add the arch options based on the particular spelling of -arch, to match
664 // how the driver driver works.
665 if (BoundArch) {
666 llvm::StringRef Name = BoundArch;
667 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
668 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
669
670 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
671 // which defines the list of which architectures we accept.
672 if (Name == "ppc")
673 ;
674 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000675 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000676 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000677 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000678 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000679 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000680 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000681 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000682 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000683 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000684 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000685 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000686 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000687 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000688 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000689 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000690
691 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000692 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000693
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000694 else if (Name == "i386")
695 ;
696 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000697 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000698 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000699 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000700 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000701 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000702 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000703 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000704 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000705 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000706 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000707 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000708 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000709 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000710
711 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000712 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000713
714 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000715 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000716 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000717 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000718 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000719 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000720 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000721 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000722 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000723 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000724 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000725 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000726
727 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000728 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000729 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000730
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000731 // Add an explicit version min argument for the deployment target. We do this
732 // after argument translation because -Xarch_ arguments may add a version min
733 // argument.
734 AddDeploymentTarget(*DAL);
735
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000736 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000737}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000738
Daniel Dunbarf3955282009-09-04 18:34:51 +0000739bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000740 // FIXME: Gross; we should probably have some separate target
741 // definition, possibly even reusing the one in clang.
742 return getArchName() == "x86_64";
743}
744
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000745bool Darwin::UseDwarfDebugFlags() const {
746 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
747 return S[0] != '\0';
748 return false;
749}
750
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000751bool Darwin::UseSjLjExceptions() const {
752 // Darwin uses SjLj exceptions on ARM.
753 return (getTriple().getArch() == llvm::Triple::arm ||
754 getTriple().getArch() == llvm::Triple::thumb);
755}
756
Daniel Dunbarf3955282009-09-04 18:34:51 +0000757const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000758 return "pic";
759}
760
Daniel Dunbarf3955282009-09-04 18:34:51 +0000761const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000762 if (getArchName() == "x86_64")
763 return "pic";
764 return 0;
765}
766
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000767bool Darwin::SupportsProfiling() const {
768 // Profiling instrumentation is only supported on x86.
769 return getArchName() == "i386" || getArchName() == "x86_64";
770}
771
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000772bool Darwin::SupportsObjCGC() const {
773 // Garbage collection is supported everywhere except on iPhone OS.
774 return !isTargetIPhoneOS();
775}
776
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000777std::string
778Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
779 return ComputeLLVMTriple(Args);
780}
781
Daniel Dunbar39176082009-03-20 00:20:03 +0000782/// Generic_GCC - A tool chain using the 'gcc' command to perform
783/// all subcommands; this relies on gcc translating the majority of
784/// command line options.
785
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000786Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000787 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000788 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +0000789 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000790 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000791}
792
Daniel Dunbar39176082009-03-20 00:20:03 +0000793Generic_GCC::~Generic_GCC() {
794 // Free tool implementations.
795 for (llvm::DenseMap<unsigned, Tool*>::iterator
796 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
797 delete it->second;
798}
799
Mike Stump1eb44332009-09-09 15:08:12 +0000800Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000801 const JobAction &JA) const {
802 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000803 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000804 Key = Action::AnalyzeJobClass;
805 else
806 Key = JA.getKind();
807
808 Tool *&T = Tools[Key];
809 if (!T) {
810 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000811 case Action::InputClass:
812 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000813 assert(0 && "Invalid tool kind.");
814 case Action::PreprocessJobClass:
815 T = new tools::gcc::Preprocess(*this); break;
816 case Action::PrecompileJobClass:
817 T = new tools::gcc::Precompile(*this); break;
818 case Action::AnalyzeJobClass:
819 T = new tools::Clang(*this); break;
820 case Action::CompileJobClass:
821 T = new tools::gcc::Compile(*this); break;
822 case Action::AssembleJobClass:
823 T = new tools::gcc::Assemble(*this); break;
824 case Action::LinkJobClass:
825 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000827 // This is a bit ungeneric, but the only platform using a driver
828 // driver is Darwin.
829 case Action::LipoJobClass:
830 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000831 case Action::DsymutilJobClass:
832 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000833 }
834 }
835
836 return *T;
837}
838
Daniel Dunbar39176082009-03-20 00:20:03 +0000839bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000840 // FIXME: Gross; we should probably have some separate target
841 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000842 return getArchName() == "x86_64";
843}
844
845const char *Generic_GCC::GetDefaultRelocationModel() const {
846 return "static";
847}
848
849const char *Generic_GCC::GetForcedPicModel() const {
850 return 0;
851}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000852
Chris Lattner3a47c4e2010-03-04 21:07:38 +0000853/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
854/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
855/// Currently does not support anything else but compilation.
856
857TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
858 : ToolChain(Host, Triple) {
859 // Path mangling to find libexec
860 std::string Path(getDriver().Dir);
861
862 Path += "/../libexec";
863 getProgramPaths().push_back(Path);
864}
865
866TCEToolChain::~TCEToolChain() {
867 for (llvm::DenseMap<unsigned, Tool*>::iterator
868 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
869 delete it->second;
870}
871
872bool TCEToolChain::IsMathErrnoDefault() const {
873 return true;
874}
875
876bool TCEToolChain::IsUnwindTablesDefault() const {
877 return false;
878}
879
880const char *TCEToolChain::GetDefaultRelocationModel() const {
881 return "static";
882}
883
884const char *TCEToolChain::GetForcedPicModel() const {
885 return 0;
886}
887
888Tool &TCEToolChain::SelectTool(const Compilation &C,
889 const JobAction &JA) const {
890 Action::ActionClass Key;
891 Key = Action::AnalyzeJobClass;
892
893 Tool *&T = Tools[Key];
894 if (!T) {
895 switch (Key) {
896 case Action::PreprocessJobClass:
897 T = new tools::gcc::Preprocess(*this); break;
898 case Action::AnalyzeJobClass:
899 T = new tools::Clang(*this); break;
900 default:
901 assert(false && "Unsupported action for TCE target.");
902 }
903 }
904 return *T;
905}
906
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000907/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
908
909OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +0000910 : Generic_ELF(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000911 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000912 getFilePaths().push_back("/usr/lib");
913}
914
915Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
916 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000917 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000918 Key = Action::AnalyzeJobClass;
919 else
920 Key = JA.getKind();
921
Rafael Espindoladda5b922010-11-07 23:13:01 +0000922 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
923 options::OPT_no_integrated_as,
924 IsIntegratedAssemblerDefault());
925
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000926 Tool *&T = Tools[Key];
927 if (!T) {
928 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +0000929 case Action::AssembleJobClass: {
930 if (UseIntegratedAs)
931 T = new tools::ClangAs(*this);
932 else
933 T = new tools::openbsd::Assemble(*this);
934 break;
935 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000936 case Action::LinkJobClass:
937 T = new tools::openbsd::Link(*this); break;
938 default:
939 T = &Generic_GCC::SelectTool(C, JA);
940 }
941 }
942
943 return *T;
944}
945
Daniel Dunbar75358d22009-03-30 21:06:03 +0000946/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
947
Daniel Dunbar214afe92010-08-02 05:43:59 +0000948FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +0000949 : Generic_ELF(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +0000950
951 // Determine if we are compiling 32-bit code on an x86_64 platform.
952 bool Lib32 = false;
953 if (Triple.getArch() == llvm::Triple::x86 &&
954 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
955 llvm::Triple::x86_64)
956 Lib32 = true;
957
Daniel Dunbarbc534662009-04-02 18:30:04 +0000958 if (Lib32) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000959 getFilePaths().push_back("/usr/lib32");
960 } else {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000961 getFilePaths().push_back("/usr/lib");
962 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000963}
964
965Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
966 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000967 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000968 Key = Action::AnalyzeJobClass;
969 else
970 Key = JA.getKind();
971
Roman Divacky67dece72010-11-08 17:46:39 +0000972 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
973 options::OPT_no_integrated_as,
974 IsIntegratedAssemblerDefault());
975
Daniel Dunbar75358d22009-03-30 21:06:03 +0000976 Tool *&T = Tools[Key];
977 if (!T) {
978 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000979 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +0000980 if (UseIntegratedAs)
981 T = new tools::ClangAs(*this);
982 else
983 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +0000984 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000985 case Action::LinkJobClass:
986 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000987 default:
988 T = &Generic_GCC::SelectTool(C, JA);
989 }
990 }
991
992 return *T;
993}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000994
Benjamin Kramer8e50a962011-02-02 18:59:27 +0000995/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
996
997NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple)
998 : Generic_ELF(Host, Triple) {
999
1000 // Determine if we are compiling 32-bit code on an x86_64 platform.
1001 bool Lib32 = false;
1002 if (Triple.getArch() == llvm::Triple::x86 &&
1003 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1004 llvm::Triple::x86_64)
1005 Lib32 = true;
1006
1007 getProgramPaths().push_back(getDriver().Dir + "/../libexec");
1008 getProgramPaths().push_back("/usr/libexec");
1009 if (Lib32) {
1010 getFilePaths().push_back("/usr/lib/i386");
1011 } else {
1012 getFilePaths().push_back("/usr/lib");
1013 }
1014}
1015
1016Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
1017 Action::ActionClass Key;
1018 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1019 Key = Action::AnalyzeJobClass;
1020 else
1021 Key = JA.getKind();
1022
1023 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1024 options::OPT_no_integrated_as,
1025 IsIntegratedAssemblerDefault());
1026
1027 Tool *&T = Tools[Key];
1028 if (!T) {
1029 switch (Key) {
1030 case Action::AssembleJobClass:
1031 if (UseIntegratedAs)
1032 T = new tools::ClangAs(*this);
1033 else
1034 T = new tools::netbsd::Assemble(*this);
1035 break;
1036 case Action::LinkJobClass:
1037 T = new tools::netbsd::Link(*this); break;
1038 default:
1039 T = &Generic_GCC::SelectTool(C, JA);
1040 }
1041 }
1042
1043 return *T;
1044}
1045
Chris Lattner38e317d2010-07-07 16:01:42 +00001046/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1047
1048Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1049 : Generic_GCC(Host, Triple) {
1050 getFilePaths().push_back(getDriver().Dir + "/../lib");
1051 getFilePaths().push_back("/usr/lib");
1052 getFilePaths().push_back("/usr/gnu/lib");
1053 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1054}
1055
1056Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
1057 Action::ActionClass Key;
1058 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1059 Key = Action::AnalyzeJobClass;
1060 else
1061 Key = JA.getKind();
1062
1063 Tool *&T = Tools[Key];
1064 if (!T) {
1065 switch (Key) {
1066 case Action::AssembleJobClass:
1067 T = new tools::minix::Assemble(*this); break;
1068 case Action::LinkJobClass:
1069 T = new tools::minix::Link(*this); break;
1070 default:
1071 T = &Generic_GCC::SelectTool(C, JA);
1072 }
1073 }
1074
1075 return *T;
1076}
1077
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001078/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1079
1080AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1081 : Generic_GCC(Host, Triple) {
1082
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001083 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001084 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001085 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001086
Daniel Dunbaree788e72009-12-21 18:54:17 +00001087 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001088 getFilePaths().push_back("/usr/lib");
1089 getFilePaths().push_back("/usr/sfw/lib");
1090 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001091 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001092
1093}
1094
1095Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
1096 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001097 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001098 Key = Action::AnalyzeJobClass;
1099 else
1100 Key = JA.getKind();
1101
1102 Tool *&T = Tools[Key];
1103 if (!T) {
1104 switch (Key) {
1105 case Action::AssembleJobClass:
1106 T = new tools::auroraux::Assemble(*this); break;
1107 case Action::LinkJobClass:
1108 T = new tools::auroraux::Link(*this); break;
1109 default:
1110 T = &Generic_GCC::SelectTool(C, JA);
1111 }
1112 }
1113
1114 return *T;
1115}
1116
1117
Eli Friedman6b3454a2009-05-26 07:52:18 +00001118/// Linux toolchain (very bare-bones at the moment).
1119
Rafael Espindolac1da9812010-11-07 20:14:31 +00001120enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001121 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001122 DebianLenny,
1123 DebianSqueeze,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001124 Exherbo,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001125 Fedora13,
1126 Fedora14,
1127 OpenSuse11_3,
Douglas Gregor814638e2011-03-14 15:39:50 +00001128 UbuntuHardy,
1129 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001130 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001131 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001132 UbuntuLucid,
1133 UbuntuMaverick,
1134 UnknownDistro
1135};
1136
1137static bool IsFedora(enum LinuxDistro Distro) {
1138 return Distro == Fedora13 || Distro == Fedora14;
1139}
1140
1141static bool IsOpenSuse(enum LinuxDistro Distro) {
1142 return Distro == OpenSuse11_3;
1143}
1144
1145static bool IsDebian(enum LinuxDistro Distro) {
1146 return Distro == DebianLenny || Distro == DebianSqueeze;
1147}
1148
1149static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001150 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
1151 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001152 Distro == UbuntuJaunty || Distro == UbuntuKarmic;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001153}
1154
1155static bool IsDebianBased(enum LinuxDistro Distro) {
1156 return IsDebian(Distro) || IsUbuntu(Distro);
1157}
1158
1159static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001160 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001161 bool Exists;
1162 if (Distro == Exherbo &&
1163 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001164 return false;
1165
Rafael Espindolac1da9812010-11-07 20:14:31 +00001166 return true;
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001167 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001168 if (Arch == llvm::Triple::x86 && IsDebianBased(Distro))
1169 return true;
1170 return false;
1171}
1172
1173static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001174 llvm::OwningPtr<llvm::MemoryBuffer> File;
1175 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
1176 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001177 llvm::SmallVector<llvm::StringRef, 8> Lines;
1178 Data.split(Lines, "\n");
1179 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001180 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1181 return UbuntuHardy;
1182 if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1183 return UbuntuIntrepid;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001184 if (Lines[i] == "DISTRIB_CODENAME=maverick")
1185 return UbuntuMaverick;
1186 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1187 return UbuntuLucid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001188 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001189 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001190 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1191 return UbuntuKarmic;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001192 }
1193 return UnknownDistro;
1194 }
1195
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001196 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1197 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001198 if (Data.startswith("Fedora release 14 (Laughlin)"))
1199 return Fedora14;
1200 else if (Data.startswith("Fedora release 13 (Goddard)"))
1201 return Fedora13;
1202 return UnknownDistro;
1203 }
1204
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001205 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1206 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001207 if (Data[0] == '5')
1208 return DebianLenny;
1209 else if (Data.startswith("squeeze/sid"))
1210 return DebianSqueeze;
1211 return UnknownDistro;
1212 }
1213
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001214 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1215 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001216 if (Data.startswith("openSUSE 11.3"))
1217 return OpenSuse11_3;
1218 return UnknownDistro;
1219 }
1220
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001221 bool Exists;
1222 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001223 return Exherbo;
1224
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001225 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1226 return ArchLinux;
1227
Rafael Espindolac1da9812010-11-07 20:14:31 +00001228 return UnknownDistro;
1229}
1230
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001231Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001232 : Generic_ELF(Host, Triple) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001233 llvm::Triple::ArchType Arch =
1234 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Daniel Dunbara9822de2009-08-06 01:47:11 +00001235
Rafael Espindolac1da9812010-11-07 20:14:31 +00001236 std::string Suffix32 = "";
1237 if (Arch == llvm::Triple::x86_64)
1238 Suffix32 = "/32";
Daniel Dunbara9822de2009-08-06 01:47:11 +00001239
Rafael Espindolac1da9812010-11-07 20:14:31 +00001240 std::string Suffix64 = "";
1241 if (Arch == llvm::Triple::x86)
1242 Suffix64 = "/64";
1243
1244 std::string Lib32 = "lib";
1245
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001246 bool Exists;
1247 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001248 Lib32 = "lib32";
1249
1250 std::string Lib64 = "lib";
Michael J. Spencer2dea7c72011-01-12 23:54:48 +00001251 bool Symlink;
Chris Lattner48aef362011-01-13 01:35:58 +00001252 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1253 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001254 Lib64 = "lib64";
1255
1256 std::string GccTriple = "";
Douglas Gregorf0594d82011-03-06 19:11:49 +00001257 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001258 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1259 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001260 GccTriple = "arm-linux-gnueabi";
1261 } else if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001262 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1263 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001264 GccTriple = "x86_64-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001265 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1266 Exists) && Exists)
Rafael Espindola53dd00b2010-11-17 00:25:26 +00001267 GccTriple = "x86_64-unknown-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001268 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1269 Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001270 GccTriple = "x86_64-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001271 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1272 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001273 GccTriple = "x86_64-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001274 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1275 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001276 GccTriple = "x86_64-suse-linux";
Nick Lewyckye10f9002011-02-01 23:03:29 +00001277 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1278 Exists) && Exists)
1279 GccTriple = "x86_64-manbo-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001280 } else if (Arch == llvm::Triple::x86) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001281 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001282 GccTriple = "i686-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001283 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1284 Exists)
Nuno Lopes2a69ddd2010-11-19 17:26:57 +00001285 GccTriple = "i686-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001286 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1287 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001288 GccTriple = "i486-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001289 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1290 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001291 GccTriple = "i686-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001292 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1293 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001294 GccTriple = "i586-suse-linux";
1295 }
1296
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001297 const char* GccVersions[] = {"4.5.2", "4.5.1", "4.5", "4.4.5", "4.4.4",
1298 "4.4.3", "4.4", "4.3.4", "4.3.3", "4.3.2",
Douglas Gregor814638e2011-03-14 15:39:50 +00001299 "4.3", "4.2.4", "4.2.3", "4.2.2", "4.2.1",
1300 "4.2"};
Rafael Espindolac1da9812010-11-07 20:14:31 +00001301 std::string Base = "";
1302 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1303 std::string Suffix = GccTriple + "/" + GccVersions[i];
1304 std::string t1 = "/usr/lib/gcc/" + Suffix;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001305 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001306 Base = t1;
1307 break;
1308 }
1309 std::string t2 = "/usr/lib64/gcc/" + Suffix;
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001310 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001311 Base = t2;
1312 break;
1313 }
1314 }
1315
1316 path_list &Paths = getFilePaths();
1317 bool Is32Bits = getArch() == llvm::Triple::x86;
1318
1319 std::string Suffix;
1320 std::string Lib;
1321
1322 if (Is32Bits) {
1323 Suffix = Suffix32;
1324 Lib = Lib32;
1325 } else {
1326 Suffix = Suffix64;
1327 Lib = Lib64;
1328 }
1329
1330 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001331 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001332 Linker = LinkerPath.str();
1333 else
1334 Linker = GetProgramPath("ld");
1335
1336 LinuxDistro Distro = DetectLinuxDistro(Arch);
1337
Rafael Espindola94c80222010-11-08 14:48:47 +00001338 if (IsUbuntu(Distro)) {
1339 ExtraOpts.push_back("-z");
1340 ExtraOpts.push_back("relro");
1341 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001342
Douglas Gregorf0594d82011-03-06 19:11:49 +00001343 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001344 ExtraOpts.push_back("-X");
1345
1346 if (IsFedora(Distro) || Distro == UbuntuMaverick)
1347 ExtraOpts.push_back("--hash-style=gnu");
1348
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001349 if (IsDebian(Distro) || Distro == UbuntuLucid || Distro == UbuntuJaunty ||
1350 Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001351 ExtraOpts.push_back("--hash-style=both");
1352
1353 if (IsFedora(Distro))
1354 ExtraOpts.push_back("--no-add-needed");
1355
Rafael Espindola021aaa42010-11-10 05:00:22 +00001356 if (Distro == DebianSqueeze || IsOpenSuse(Distro) ||
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001357 IsFedora(Distro) || Distro == UbuntuLucid || Distro == UbuntuMaverick ||
1358 Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001359 ExtraOpts.push_back("--build-id");
1360
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001361 if (Distro == ArchLinux)
1362 Lib = "lib";
1363
Rafael Espindolac1da9812010-11-07 20:14:31 +00001364 Paths.push_back(Base + Suffix);
1365 if (HasMultilib(Arch, Distro)) {
1366 if (IsOpenSuse(Distro) && Is32Bits)
1367 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1368 Paths.push_back(Base + "/../../../../" + Lib);
1369 Paths.push_back("/lib/../" + Lib);
1370 Paths.push_back("/usr/lib/../" + Lib);
1371 }
1372 if (!Suffix.empty())
1373 Paths.push_back(Base);
1374 if (IsOpenSuse(Distro))
1375 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1376 Paths.push_back(Base + "/../../..");
1377 if (Arch == getArch() && IsUbuntu(Distro))
1378 Paths.push_back("/usr/lib/" + GccTriple);
1379}
1380
1381bool Linux::HasNativeLLVMSupport() const {
1382 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00001383}
1384
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001385Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA) const {
1386 Action::ActionClass Key;
1387 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1388 Key = Action::AnalyzeJobClass;
1389 else
1390 Key = JA.getKind();
1391
Rafael Espindoladda5b922010-11-07 23:13:01 +00001392 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1393 options::OPT_no_integrated_as,
1394 IsIntegratedAssemblerDefault());
1395
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001396 Tool *&T = Tools[Key];
1397 if (!T) {
1398 switch (Key) {
1399 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00001400 if (UseIntegratedAs)
1401 T = new tools::ClangAs(*this);
1402 else
1403 T = new tools::linuxtools::Assemble(*this);
1404 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001405 case Action::LinkJobClass:
1406 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001407 default:
1408 T = &Generic_GCC::SelectTool(C, JA);
1409 }
1410 }
1411
1412 return *T;
1413}
1414
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001415/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1416
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001417DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001418 : Generic_ELF(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001419
1420 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001421 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001422 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001423 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001424
Daniel Dunbaree788e72009-12-21 18:54:17 +00001425 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001426 getFilePaths().push_back("/usr/lib");
1427 getFilePaths().push_back("/usr/lib/gcc41");
1428}
1429
1430Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
1431 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001432 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001433 Key = Action::AnalyzeJobClass;
1434 else
1435 Key = JA.getKind();
1436
1437 Tool *&T = Tools[Key];
1438 if (!T) {
1439 switch (Key) {
1440 case Action::AssembleJobClass:
1441 T = new tools::dragonfly::Assemble(*this); break;
1442 case Action::LinkJobClass:
1443 T = new tools::dragonfly::Link(*this); break;
1444 default:
1445 T = &Generic_GCC::SelectTool(C, JA);
1446 }
1447 }
1448
1449 return *T;
1450}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001451
1452Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1453 : ToolChain(Host, Triple) {
1454}
1455
1456Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA) const {
1457 Action::ActionClass Key;
1458 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1459 Key = Action::AnalyzeJobClass;
1460 else
1461 Key = JA.getKind();
1462
1463 Tool *&T = Tools[Key];
1464 if (!T) {
1465 switch (Key) {
1466 case Action::InputClass:
1467 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001468 case Action::LipoJobClass:
1469 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001470 assert(0 && "Invalid tool kind.");
1471 case Action::PreprocessJobClass:
1472 case Action::PrecompileJobClass:
1473 case Action::AnalyzeJobClass:
1474 case Action::CompileJobClass:
1475 T = new tools::Clang(*this); break;
1476 case Action::AssembleJobClass:
1477 T = new tools::ClangAs(*this); break;
1478 case Action::LinkJobClass:
1479 T = new tools::visualstudio::Link(*this); break;
1480 }
1481 }
1482
1483 return *T;
1484}
1485
1486bool Windows::IsIntegratedAssemblerDefault() const {
1487 return true;
1488}
1489
1490bool Windows::IsUnwindTablesDefault() const {
1491 // FIXME: Gross; we should probably have some separate target
1492 // definition, possibly even reusing the one in clang.
1493 return getArchName() == "x86_64";
1494}
1495
1496const char *Windows::GetDefaultRelocationModel() const {
1497 return "static";
1498}
1499
1500const char *Windows::GetForcedPicModel() const {
1501 if (getArchName() == "x86_64")
1502 return "pic";
1503 return 0;
1504}