blob: 136d2134eab0f1c07c8f300df3855a41b29d4b95 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar59e5e882009-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"
Chandler Carruth3a022472012-12-04 09:13:33 +000011#include "SanitizerArgs.h"
12#include "clang/Basic/ObjCRuntime.h"
13#include "clang/Basic/Version.h"
Daniel Dunbar6232d342010-05-20 21:48:38 +000014#include "clang/Driver/Compilation.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000015#include "clang/Driver/Driver.h"
Daniel Dunbaraabb0b12009-03-25 06:12:34 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000017#include "clang/Driver/Options.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "llvm/ADT/STLExtras.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000019#include "llvm/ADT/SmallString.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000020#include "llvm/ADT/StringExtras.h"
Bob Wilson997a97f2011-10-07 00:37:57 +000021#include "llvm/ADT/StringSwitch.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000022#include "llvm/Option/Arg.h"
23#include "llvm/Option/ArgList.h"
24#include "llvm/Option/OptTable.h"
25#include "llvm/Option/Option.h"
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +000026#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000027#include "llvm/Support/FileSystem.h"
Rafael Espindolac8f008f2010-11-07 20:14:31 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Rafael Espindola1600a532013-06-17 17:23:47 +000030#include "llvm/Support/PathV1.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000031#include "llvm/Support/raw_ostream.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000032#include "llvm/Support/system_error.h"
NAKAMURA Takumi067fcb52012-12-04 14:31:59 +000033
34// FIXME: This needs to be listed last until we fix the broken include guards
35// in these files and the LLVM config.h files.
36#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
37
Daniel Dunbarb5023e92009-04-10 21:00:07 +000038#include <cstdlib> // ::getenv
39
Daniel Dunbar59e5e882009-03-20 00:20:03 +000040using namespace clang::driver;
41using namespace clang::driver::toolchains;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000042using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000043using namespace llvm::opt;
Daniel Dunbar59e5e882009-03-20 00:20:03 +000044
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +000045/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +000046
Rafael Espindola84b588b2013-03-18 18:10:27 +000047Darwin::Darwin(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
48 : ToolChain(D, Triple, Args), TargetInitialized(false)
Daniel Dunbar6276f992009-09-18 08:15:13 +000049{
Bob Wilson9d3f7af2012-01-31 21:30:03 +000050 // Compute the initial Darwin version from the triple
51 unsigned Major, Minor, Micro;
Bob Wilsona2234012012-01-31 22:43:59 +000052 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
53 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
54 Triple.getOSName();
55 llvm::raw_string_ostream(MacosxVersionMin)
56 << Major << '.' << Minor << '.' << Micro;
57
Bob Wilson9d3f7af2012-01-31 21:30:03 +000058 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
59 // It should be removed when we stop supporting that.
60 DarwinVersion[0] = Minor + 4;
61 DarwinVersion[1] = Micro;
62 DarwinVersion[2] = 0;
Chad Rosier266c6202012-05-09 18:46:30 +000063
64 // Compute the initial iOS version from the triple
Chad Rosierbf8628e2012-05-09 18:51:13 +000065 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosier266c6202012-05-09 18:46:30 +000066 llvm::raw_string_ostream(iOSVersionMin)
67 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar6276f992009-09-18 08:15:13 +000068}
69
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +000070types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
71 types::ID Ty = types::lookupTypeForExtension(Ext);
72
73 // Darwin always preprocesses assembly files (unless -x is used explicitly).
74 if (Ty == types::TY_PP_Asm)
75 return types::TY_Asm;
76
77 return Ty;
78}
79
Daniel Dunbar62123a12010-09-17 00:24:52 +000080bool Darwin::HasNativeLLVMSupport() const {
81 return true;
82}
83
John McCall24fc0de2011-07-06 00:26:06 +000084/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall5fb5df92012-06-20 06:18:46 +000085ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
Bob Wilson5ad5a952012-11-09 01:59:30 +000086 if (isTargetIPhoneOS())
John McCall5fb5df92012-06-20 06:18:46 +000087 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson5ad5a952012-11-09 01:59:30 +000088 if (isNonFragile)
89 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
90 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall24fc0de2011-07-06 00:26:06 +000091}
92
John McCall7959fee2011-09-09 20:41:01 +000093/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
94bool Darwin::hasBlocksRuntime() const {
95 if (isTargetIPhoneOS())
96 return !isIPhoneOSVersionLT(3, 2);
97 else
98 return !isMacosxVersionLT(10, 6);
99}
100
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000101static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilson997a97f2011-10-07 00:37:57 +0000102 return llvm::StringSwitch<const char*>(Value)
103 .Case("armv6k", "armv6")
Bob Wilson743bf672013-03-04 22:37:49 +0000104 .Case("armv6m", "armv6m")
Bob Wilson997a97f2011-10-07 00:37:57 +0000105 .Case("armv5tej", "armv5")
106 .Case("xscale", "xscale")
107 .Case("armv4t", "armv4t")
108 .Case("armv7", "armv7")
109 .Cases("armv7a", "armv7-a", "armv7")
110 .Cases("armv7r", "armv7-r", "armv7")
Bob Wilson743bf672013-03-04 22:37:49 +0000111 .Cases("armv7em", "armv7e-m", "armv7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000112 .Cases("armv7f", "armv7-f", "armv7f")
113 .Cases("armv7k", "armv7-k", "armv7k")
Bob Wilson743bf672013-03-04 22:37:49 +0000114 .Cases("armv7m", "armv7-m", "armv7m")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000115 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilson997a97f2011-10-07 00:37:57 +0000116 .Default(0);
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000117}
118
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000119static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilson997a97f2011-10-07 00:37:57 +0000120 return llvm::StringSwitch<const char *>(Value)
121 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
122 .Cases("arm10e", "arm10tdmi", "armv5")
123 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
124 .Case("xscale", "xscale")
Bob Wilson743bf672013-03-04 22:37:49 +0000125 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6")
126 .Case("cortex-m0", "armv6m")
127 .Cases("cortex-a8", "cortex-r4", "cortex-a9", "cortex-a15", "armv7")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000128 .Case("cortex-a9-mp", "armv7f")
Bob Wilson743bf672013-03-04 22:37:49 +0000129 .Case("cortex-m3", "armv7m")
130 .Case("cortex-m4", "armv7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000131 .Case("swift", "armv7s")
Bob Wilson997a97f2011-10-07 00:37:57 +0000132 .Default(0);
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000133}
134
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000135StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000136 switch (getTriple().getArch()) {
137 default:
138 return getArchName();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000139
Douglas Gregord9bb1522011-03-06 19:11:49 +0000140 case llvm::Triple::thumb:
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000141 case llvm::Triple::arm: {
142 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +0000143 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000144 return Arch;
145
146 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +0000147 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000148 return Arch;
149
150 return "arm";
151 }
152 }
153}
154
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000155Darwin::~Darwin() {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000156}
157
Chad Rosierd3a0f952011-09-20 20:44:06 +0000158std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
159 types::ID InputType) const {
160 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000161
162 // If the target isn't initialized (e.g., an unknown Darwin platform, return
163 // the default triple).
164 if (!isTargetInitialized())
165 return Triple.getTriple();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000166
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000167 SmallString<16> Str;
Benjamin Kramerddefa6d2012-03-10 20:55:36 +0000168 Str += isTargetIPhoneOS() ? "ios" : "macosx";
169 Str += getTargetVersion().getAsString();
170 Triple.setOSName(Str);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000171
172 return Triple.getTriple();
173}
174
David Blaikie68e081d2011-12-20 02:48:34 +0000175void Generic_ELF::anchor() {}
176
Rafael Espindola7cf32212013-03-20 03:05:54 +0000177Tool *Darwin::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +0000178 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000179 case Action::LipoJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000180 if (!Lipo)
181 Lipo.reset(new tools::darwin::Lipo(*this));
182 return Lipo.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000183 case Action::DsymutilJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000184 if (!Dsymutil)
185 Dsymutil.reset(new tools::darwin::Dsymutil(*this));
186 return Dsymutil.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000187 case Action::VerifyJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000188 if (!VerifyDebug)
189 VerifyDebug.reset(new tools::darwin::VerifyDebug(*this));
190 return VerifyDebug.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000191 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000192 return ToolChain::getTool(AC);
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000193 }
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000194}
195
Rafael Espindola7cf32212013-03-20 03:05:54 +0000196Tool *Darwin::buildLinker() const {
197 return new tools::darwin::Link(*this);
198}
199
200Tool *Darwin::buildAssembler() const {
201 return new tools::darwin::Assemble(*this);
202}
Daniel Dunbar26d482a2009-09-18 08:15:03 +0000203
Rafael Espindola84b588b2013-03-18 18:10:27 +0000204DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple,
205 const ArgList &Args)
206 : Darwin(D, Triple, Args)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000207{
Daniel Dunbar00aff042010-09-17 08:22:12 +0000208 getProgramPaths().push_back(getDriver().getInstalledDir());
209 if (getDriver().getInstalledDir() != getDriver().Dir)
210 getProgramPaths().push_back(getDriver().Dir);
211
Daniel Dunbar6276f992009-09-18 08:15:13 +0000212 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbar88979912010-08-01 22:29:51 +0000213 getProgramPaths().push_back(getDriver().getInstalledDir());
214 if (getDriver().getInstalledDir() != getDriver().Dir)
215 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar6276f992009-09-18 08:15:13 +0000216}
217
John McCall31168b02011-06-15 23:02:42 +0000218void DarwinClang::AddLinkARCArgs(const ArgList &Args,
219 ArgStringList &CmdArgs) const {
Eric Christopher551ef452011-08-23 17:56:55 +0000220
221 CmdArgs.push_back("-force_load");
John McCall31168b02011-06-15 23:02:42 +0000222 llvm::sys::Path P(getDriver().ClangExecutable);
223 P.eraseComponent(); // 'clang'
224 P.eraseComponent(); // 'bin'
225 P.appendComponent("lib");
226 P.appendComponent("arc");
227 P.appendComponent("libarclite_");
228 std::string s = P.str();
229 // Mash in the platform.
Argyrios Kyrtzidis058b4512011-10-18 17:40:15 +0000230 if (isTargetIOSSimulator())
231 s += "iphonesimulator";
232 else if (isTargetIPhoneOS())
John McCall31168b02011-06-15 23:02:42 +0000233 s += "iphoneos";
John McCall31168b02011-06-15 23:02:42 +0000234 else
235 s += "macosx";
236 s += ".a";
237
238 CmdArgs.push_back(Args.MakeArgString(s));
239}
240
Eric Christopherc235d0c62011-06-22 17:41:40 +0000241void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopher551ef452011-08-23 17:56:55 +0000242 ArgStringList &CmdArgs,
Alexey Samsonov8368b372012-11-21 14:17:42 +0000243 const char *DarwinStaticLib,
244 bool AlwaysLink) const {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000245 llvm::sys::Path P(getDriver().ResourceDir);
246 P.appendComponent("lib");
247 P.appendComponent("darwin");
248 P.appendComponent(DarwinStaticLib);
Eric Christopher551ef452011-08-23 17:56:55 +0000249
Eric Christopherc235d0c62011-06-22 17:41:40 +0000250 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov8368b372012-11-21 14:17:42 +0000251 // not have compiler-rt checked out or integrated into their build (unless
252 // we explicitly force linking with this library).
Eric Christopherc235d0c62011-06-22 17:41:40 +0000253 bool Exists;
Alexey Samsonov8368b372012-11-21 14:17:42 +0000254 if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
Eric Christopherc235d0c62011-06-22 17:41:40 +0000255 CmdArgs.push_back(Args.MakeArgString(P.str()));
256}
257
Daniel Dunbar6276f992009-09-18 08:15:13 +0000258void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
259 ArgStringList &CmdArgs) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000260 // Darwin only supports the compiler-rt based runtime libraries.
261 switch (GetRuntimeLibType(Args)) {
262 case ToolChain::RLT_CompilerRT:
263 break;
264 default:
265 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smithbd55daf2012-11-01 04:30:05 +0000266 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000267 return;
268 }
269
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000270 // Darwin doesn't support real static executables, don't link any runtime
271 // libraries with -static.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000272 if (Args.hasArg(options::OPT_static) ||
273 Args.hasArg(options::OPT_fapple_kext) ||
274 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar6276f992009-09-18 08:15:13 +0000275 return;
Daniel Dunbar6276f992009-09-18 08:15:13 +0000276
277 // Reject -static-libgcc for now, we can deal with this when and if someone
278 // cares. This is useful in situations where someone wants to statically link
279 // something like libstdc++, and needs its runtime support routines.
280 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000281 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000282 << A->getAsString(Args);
283 return;
284 }
285
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000286 // If we are building profile support, link that library in.
287 if (Args.hasArg(options::OPT_fprofile_arcs) ||
288 Args.hasArg(options::OPT_fprofile_generate) ||
289 Args.hasArg(options::OPT_fcreate_profile) ||
290 Args.hasArg(options::OPT_coverage)) {
291 // Select the appropriate runtime library for the target.
292 if (isTargetIPhoneOS()) {
293 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
294 } else {
295 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
296 }
297 }
298
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000299 SanitizerArgs Sanitize(*this, Args);
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000300
Alexey Samsonovcc429802012-11-16 12:53:14 +0000301 // Add Ubsan runtime library, if required.
302 if (Sanitize.needsUbsanRt()) {
Alexey Samsonov969be242013-01-21 08:45:02 +0000303 if (isTargetIPhoneOS()) {
Alexey Samsonovcc429802012-11-16 12:53:14 +0000304 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
305 << "-fsanitize=undefined";
306 } else {
Alexey Samsonov8368b372012-11-21 14:17:42 +0000307 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonovcc429802012-11-16 12:53:14 +0000308
309 // The Ubsan runtime library requires C++.
310 AddCXXStdlibLibArgs(Args, CmdArgs);
311 }
312 }
313
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000314 // Add ASAN runtime library, if required. Dynamic libraries and bundles
315 // should not be linked with the runtime library.
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000316 if (Sanitize.needsAsanRt()) {
Alexander Potapenko5700f402013-03-21 10:49:06 +0000317 if (isTargetIPhoneOS() && !isTargetIOSSimulator()) {
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000318 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000319 << "-fsanitize=address";
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000320 } else {
Alexander Potapenko5700f402013-03-21 10:49:06 +0000321 if (Args.hasArg(options::OPT_dynamiclib) ||
322 Args.hasArg(options::OPT_bundle)) {
323 // Assume the binary will provide the ASan runtime.
324 } else {
325 AddLinkRuntimeLib(Args, CmdArgs,
326 "libclang_rt.asan_osx_dynamic.dylib", true);
327 // The ASAN runtime library requires C++.
328 AddCXXStdlibLibArgs(Args, CmdArgs);
329 }
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000330 }
331 }
332
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000333 // Otherwise link libSystem, then the dynamic runtime library, and finally any
334 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000335 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000336
337 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar15c89422010-01-27 00:56:37 +0000338 if (isTargetIPhoneOS()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000339 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
340 // it never went into the SDK.
Bob Wilson102be442011-10-07 17:54:41 +0000341 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
342 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
343 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000344
Daniel Dunbard1076382011-04-18 23:48:36 +0000345 // We currently always need a static runtime library for iOS.
Eric Christopherc235d0c62011-06-22 17:41:40 +0000346 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000347 } else {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000348 // The dynamic runtime library was merged with libSystem for 10.6 and
349 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000350 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000351 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000352 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000353 CmdArgs.push_back("-lgcc_s.10.5");
354
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000355 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000356 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000357 // omitted from 10.4.dylib.
358 //
359 // Unfortunately, that turned out to not be true, because Darwin system
360 // headers can still use eprintf on i386, and it is not exported from
361 // libSystem. Therefore, we still must provide a runtime library just for
362 // the tiny tiny handful of projects that *might* use that symbol.
363 if (isMacosxVersionLT(10, 5)) {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000364 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000365 } else {
366 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopherc235d0c62011-06-22 17:41:40 +0000367 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
368 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000369 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000370 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000371}
372
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000373void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000374 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000375
Daniel Dunbar455a0492012-08-17 18:43:50 +0000376 // Support allowing the SDKROOT environment variable used by xcrun and other
377 // Xcode tools to define the default sysroot, by making it the default for
378 // isysroot.
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000379 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
380 // Warn if the path does not exist.
381 bool Exists;
382 if (llvm::sys::fs::exists(A->getValue(), Exists) || !Exists)
383 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
384 } else {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000385 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbarb2543042013-01-15 20:33:56 +0000386 // We only use this value as the default if it is an absolute path,
387 // exists, and it is not the root path.
388 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
389 StringRef(env) != "/") {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000390 Args.append(Args.MakeSeparateArg(
391 0, Opts.getOption(options::OPT_isysroot), env));
392 }
393 }
394 }
395
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000396 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000397 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
398 Arg *iOSSimVersion = Args.getLastArg(
399 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman027e9c32012-01-11 02:41:15 +0000400
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000401 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000402 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000403 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000404 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
405 iOSVersion = iOSSimVersion = 0;
406 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000407 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000408 << iOSVersion->getAsString(Args)
409 << iOSSimVersion->getAsString(Args);
410 iOSSimVersion = 0;
411 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosier64707fe2011-08-31 20:56:25 +0000412 // If no deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000413 // environment defines.
Chad Rosier64707fe2011-08-31 20:56:25 +0000414 StringRef OSXTarget;
415 StringRef iOSTarget;
416 StringRef iOSSimTarget;
417 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
418 OSXTarget = env;
419 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
420 iOSTarget = env;
421 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
422 iOSSimTarget = env;
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000423
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000424 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosier64707fe2011-08-31 20:56:25 +0000425 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif5d3231c2012-04-18 10:59:08 +0000426 // based on -isysroot.
Chad Rosier64707fe2011-08-31 20:56:25 +0000427 if (iOSTarget.empty()) {
428 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
429 StringRef first, second;
Richard Smithbd55daf2012-11-01 04:30:05 +0000430 StringRef isysroot = A->getValue();
Chad Rosier64707fe2011-08-31 20:56:25 +0000431 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
432 if (second != "")
433 iOSTarget = second.substr(0,3);
434 }
435 }
Daniel Dunbard54669d2010-01-26 01:45:19 +0000436
Chad Rosierfe6fd362011-09-28 00:46:32 +0000437 // If no OSX or iOS target has been specified and we're compiling for armv7,
438 // go ahead as assume we're targeting iOS.
Chad Rosier7b1fee12012-05-09 18:55:57 +0000439 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilsond7cf1042012-09-29 23:52:50 +0000440 (getDarwinArchName(Args) == "armv7" ||
441 getDarwinArchName(Args) == "armv7s"))
Chad Rosierf761fe92012-05-09 18:09:58 +0000442 iOSTarget = iOSVersionMin;
Chad Rosierfe6fd362011-09-28 00:46:32 +0000443
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000444 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000445 //
446 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000447
448 // Do not allow conflicts with the iOS simulator target.
Chad Rosier64707fe2011-08-31 20:56:25 +0000449 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000450 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000451 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosier64707fe2011-08-31 20:56:25 +0000452 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000453 "IPHONEOS_DEPLOYMENT_TARGET");
454 }
455
456 // Allow conflicts among OSX and iOS for historical reasons, but choose the
457 // default platform.
Chad Rosier64707fe2011-08-31 20:56:25 +0000458 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000459 if (getTriple().getArch() == llvm::Triple::arm ||
460 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosier64707fe2011-08-31 20:56:25 +0000461 OSXTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000462 else
Chad Rosier64707fe2011-08-31 20:56:25 +0000463 iOSTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000464 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000465
Chad Rosier64707fe2011-08-31 20:56:25 +0000466 if (!OSXTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000467 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000468 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
469 Args.append(OSXVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000470 } else if (!iOSTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000471 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000472 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
473 Args.append(iOSVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000474 } else if (!iOSSimTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000475 const Option O = Opts.getOption(
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000476 options::OPT_mios_simulator_version_min_EQ);
477 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
478 Args.append(iOSSimVersion);
Daniel Dunbard54669d2010-01-26 01:45:19 +0000479 } else {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000480 // Otherwise, assume we are targeting OS X.
Michael J. Spencerfc790902012-10-19 22:36:40 +0000481 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000482 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
483 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000484 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000487 // Reject invalid architecture combinations.
488 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
489 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000490 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000491 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
492 }
493
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000494 // Set the tool chain target information.
495 unsigned Major, Minor, Micro;
496 bool HadExtra;
497 if (OSXVersion) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000498 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000499 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000500 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000501 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000502 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000503 << OSXVersion->getAsString(Args);
504 } else {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000505 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
506 assert(Version && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000507 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman027e9c32012-01-11 02:41:15 +0000508 Micro, HadExtra) || HadExtra ||
509 Major >= 10 || Minor >= 100 || Micro >= 100)
510 getDriver().Diag(diag::err_drv_invalid_version_number)
511 << Version->getAsString(Args);
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000512 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000513
Daniel Dunbarb1189432011-04-30 04:18:16 +0000514 bool IsIOSSim = bool(iOSSimVersion);
515
516 // In GCC, the simulator historically was treated as being OS X in some
517 // contexts, like determining the link logic, despite generally being called
518 // with an iOS deployment target. For compatibility, we detect the
519 // simulator as iOS + x86, and treat it differently in a few contexts.
520 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
521 getTriple().getArch() == llvm::Triple::x86_64))
522 IsIOSSim = true;
523
524 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000525}
526
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000527void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000528 ArgStringList &CmdArgs) const {
529 CXXStdlibType Type = GetCXXStdlibType(Args);
530
531 switch (Type) {
532 case ToolChain::CST_Libcxx:
533 CmdArgs.push_back("-lc++");
534 break;
535
536 case ToolChain::CST_Libstdcxx: {
537 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
538 // it was previously found in the gcc lib dir. However, for all the Darwin
539 // platforms we care about it was -lstdc++.6, so we search for that
540 // explicitly if we can't see an obvious -lstdc++ candidate.
541
542 // Check in the sysroot first.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000543 bool Exists;
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000544 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000545 llvm::sys::Path P(A->getValue());
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000546 P.appendComponent("usr");
547 P.appendComponent("lib");
548 P.appendComponent("libstdc++.dylib");
549
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000550 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000551 P.eraseComponent();
552 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000553 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000554 CmdArgs.push_back(Args.MakeArgString(P.str()));
555 return;
556 }
557 }
558 }
559
560 // Otherwise, look in the root.
Bob Wilson1a9ad0f2011-11-11 07:47:04 +0000561 // FIXME: This should be removed someday when we don't have to care about
562 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000563 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
564 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000565 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
566 return;
567 }
568
569 // Otherwise, let the linker search.
570 CmdArgs.push_back("-lstdc++");
571 break;
572 }
573 }
574}
575
Shantonu Senafeb03b2010-09-17 18:39:08 +0000576void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
577 ArgStringList &CmdArgs) const {
578
579 // For Darwin platforms, use the compiler-rt-based support library
580 // instead of the gcc-provided one (which is also incidentally
581 // only present in the gcc lib dir, which makes it hard to find).
582
583 llvm::sys::Path P(getDriver().ResourceDir);
584 P.appendComponent("lib");
585 P.appendComponent("darwin");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000586
587 // Use the newer cc_kext for iOS ARM after 6.0.
588 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
589 !isIPhoneOSVersionLT(6, 0)) {
590 P.appendComponent("libclang_rt.cc_kext.a");
591 } else {
592 P.appendComponent("libclang_rt.cc_kext_ios5.a");
593 }
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000594
Shantonu Senafeb03b2010-09-17 18:39:08 +0000595 // For now, allow missing resource libraries to support developers who may
596 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000597 bool Exists;
598 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Senafeb03b2010-09-17 18:39:08 +0000599 CmdArgs.push_back(Args.MakeArgString(P.str()));
600}
601
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000602DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
603 const char *BoundArch) const {
604 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
605 const OptTable &Opts = getDriver().getOpts();
606
607 // FIXME: We really want to get out of the tool chain level argument
608 // translation business, as it makes the driver functionality much
609 // more opaque. For now, we follow gcc closely solely for the
610 // purpose of easily achieving feature parity & testability. Once we
611 // have something that works, we should reevaluate each translation
612 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000613
Daniel Dunbar775d4062010-06-11 22:00:26 +0000614 for (ArgList::const_iterator it = Args.begin(),
615 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000616 Arg *A = *it;
617
618 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar471c4f82011-06-21 00:20:17 +0000619 // Skip this argument unless the architecture matches either the toolchain
620 // triple arch, or the arch being bound.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000621 llvm::Triple::ArchType XarchArch =
Richard Smithbd55daf2012-11-01 04:30:05 +0000622 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000623 if (!(XarchArch == getArch() ||
624 (BoundArch && XarchArch ==
Rafael Espindoladcbf6982012-10-31 18:51:07 +0000625 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000626 continue;
627
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000628 Arg *OriginalArg = A;
Richard Smithbd55daf2012-11-01 04:30:05 +0000629 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000630 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000631 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000632
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000633 // If the argument parsing failed or more than one argument was
634 // consumed, the -Xarch_ argument's parameter tried to consume
635 // extra arguments. Emit an error and ignore.
636 //
637 // We also want to disallow any options which would alter the
638 // driver behavior; that isn't going to work in our model. We
639 // use isDriverOption() as an approximation, although things
640 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000641 if (!XarchArg || Index > Prev + 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000642 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar6914a982011-04-21 17:32:21 +0000643 << A->getAsString(Args);
644 continue;
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000645 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000646 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000647 << A->getAsString(Args);
648 continue;
649 }
650
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000651 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000652 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000653
654 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000655
656 // Linker input arguments require custom handling. The problem is that we
657 // have already constructed the phase actions, so we can not treat them as
658 // "input arguments".
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000659 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000660 // Convert the argument into individual Zlinker_input_args.
661 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
662 DAL->AddSeparateArg(OriginalArg,
663 Opts.getOption(options::OPT_Zlinker_input),
Richard Smithbd55daf2012-11-01 04:30:05 +0000664 A->getValue(i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000665
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000666 }
667 continue;
668 }
Mike Stump11289f42009-09-09 15:08:12 +0000669 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000670
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000671 // Sob. These is strictly gcc compatible for the time being. Apple
672 // gcc translates options twice, which means that self-expanding
673 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000674 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000675 default:
676 DAL->append(A);
677 break;
678
679 case options::OPT_mkernel:
680 case options::OPT_fapple_kext:
681 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000682 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000683 break;
Mike Stump11289f42009-09-09 15:08:12 +0000684
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000685 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000686 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smithbd55daf2012-11-01 04:30:05 +0000687 A->getValue());
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000688 break;
689
690 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000691 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
692 DAL->AddFlagArg(A,
693 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000694 break;
695
696 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000697 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
698 DAL->AddFlagArg(A,
699 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000700 break;
701
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000702 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000703 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000704 break;
705
706 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000707 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000708 break;
709
710 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000711 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000712 break;
713
714 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000715 DAL->AddFlagArg(A,
716 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000717 break;
718
719 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000720 DAL->AddFlagArg(A,
721 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000722 break;
723
724 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000725 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000726 break;
727
728 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000729 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000730 break;
731 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000732 }
733
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000734 if (getTriple().getArch() == llvm::Triple::x86 ||
735 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000736 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000737 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000738
739 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosier7c5d9082012-04-27 14:58:16 +0000740 // how the driver driver works.
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000741 if (BoundArch) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000742 StringRef Name = BoundArch;
Michael J. Spencerfc790902012-10-19 22:36:40 +0000743 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
744 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000745
746 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
747 // which defines the list of which architectures we accept.
748 if (Name == "ppc")
749 ;
750 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000751 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000752 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000753 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000754 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000755 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000756 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000757 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000758 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000759 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000760 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000761 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000762 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000763 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000764 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000765 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000766
767 else if (Name == "ppc64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000768 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000769
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000770 else if (Name == "i386")
771 ;
772 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000773 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000774 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000775 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000776 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000777 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000778 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000779 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000780 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000781 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000782 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000783 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000784 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000785 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000786
787 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000788 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000789
790 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000791 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000792 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000793 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000794 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000795 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000796 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000797 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000798 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000799 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000800 else if (Name == "armv6m")
801 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000802 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000803 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000804 else if (Name == "armv7em")
805 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000806 else if (Name == "armv7f")
807 DAL->AddJoinedArg(0, MArch, "armv7f");
808 else if (Name == "armv7k")
809 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000810 else if (Name == "armv7m")
811 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000812 else if (Name == "armv7s")
813 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000814
815 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000816 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000817 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000818
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000819 // Add an explicit version min argument for the deployment target. We do this
820 // after argument translation because -Xarch_ arguments may add a version min
821 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000822 if (BoundArch)
823 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000824
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000825 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
826 // FIXME: It would be far better to avoid inserting those -static arguments,
827 // but we can't check the deployment target in the translation code until
828 // it is set here.
829 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
830 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
831 Arg *A = *it;
832 ++it;
833 if (A->getOption().getID() != options::OPT_mkernel &&
834 A->getOption().getID() != options::OPT_fapple_kext)
835 continue;
836 assert(it != ie && "unexpected argument translation");
837 A = *it;
838 assert(A->getOption().getID() == options::OPT_static &&
839 "missing expected -static argument");
840 it = DAL->getArgs().erase(it);
841 }
842 }
843
Bob Wilson102be442011-10-07 17:54:41 +0000844 // Validate the C++ standard library choice.
845 CXXStdlibType Type = GetCXXStdlibType(*DAL);
846 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000847 // Check whether the target provides libc++.
848 StringRef where;
849
850 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson5ad5a952012-11-09 01:59:30 +0000851 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
852 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000853
854 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000855 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000856 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000857 }
858 }
859
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000860 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000861}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000862
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000863bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000864 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000865}
866
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000867bool Darwin::UseDwarfDebugFlags() const {
868 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
869 return S[0] != '\0';
870 return false;
871}
872
Daniel Dunbar3241d402010-02-10 18:49:11 +0000873bool Darwin::UseSjLjExceptions() const {
874 // Darwin uses SjLj exceptions on ARM.
875 return (getTriple().getArch() == llvm::Triple::arm ||
876 getTriple().getArch() == llvm::Triple::thumb);
877}
878
Chandler Carruth76a943b2012-11-19 03:52:03 +0000879bool Darwin::isPICDefault() const {
880 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000881}
882
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000883bool Darwin::isPIEDefault() const {
884 return false;
885}
886
Chandler Carruth76a943b2012-11-19 03:52:03 +0000887bool Darwin::isPICDefaultForced() const {
888 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000889}
890
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000891bool Darwin::SupportsProfiling() const {
892 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000893 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000894}
895
Daniel Dunbar16334e12010-04-10 16:20:23 +0000896bool Darwin::SupportsObjCGC() const {
897 // Garbage collection is supported everywhere except on iPhone OS.
898 return !isTargetIPhoneOS();
899}
900
John McCall3deb1ad2012-08-21 02:47:43 +0000901void Darwin::CheckObjCARC() const {
902 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
903 return;
John McCall93207072012-08-27 01:56:21 +0000904 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000905}
906
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000907std::string
Chad Rosierd3a0f952011-09-20 20:44:06 +0000908Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
909 types::ID InputType) const {
910 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000911}
912
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000913/// Generic_GCC - A tool chain using the 'gcc' command to perform
914/// all subcommands; this relies on gcc translating the majority of
915/// command line options.
916
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000917/// \brief Parse a GCCVersion object out of a string of text.
918///
919/// This is the primary means of forming GCCVersion objects.
920/*static*/
921Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
922 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
923 std::pair<StringRef, StringRef> First = VersionText.split('.');
924 std::pair<StringRef, StringRef> Second = First.second.split('.');
925
926 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
927 if (First.first.getAsInteger(10, GoodVersion.Major) ||
928 GoodVersion.Major < 0)
929 return BadVersion;
930 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
931 GoodVersion.Minor < 0)
932 return BadVersion;
933
934 // First look for a number prefix and parse that if present. Otherwise just
935 // stash the entire patch string in the suffix, and leave the number
936 // unspecified. This covers versions strings such as:
937 // 4.4
938 // 4.4.0
939 // 4.4.x
940 // 4.4.2-rc4
941 // 4.4.x-patched
942 // And retains any patch number it finds.
943 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
944 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +0000945 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000946 // Try to parse the number and any suffix.
947 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
948 GoodVersion.Patch < 0)
949 return BadVersion;
950 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
951 }
952 }
953
954 return GoodVersion;
955}
956
957/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
958bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000959 if (Major != RHS.Major)
960 return Major < RHS.Major;
961 if (Minor != RHS.Minor)
962 return Minor < RHS.Minor;
963 if (Patch != RHS.Patch) {
964 // Note that versions without a specified patch sort higher than those with
965 // a patch.
966 if (RHS.Patch == -1)
967 return true;
968 if (Patch == -1)
969 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000970
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000971 // Otherwise just sort on the patch itself.
972 return Patch < RHS.Patch;
973 }
974 if (PatchSuffix != RHS.PatchSuffix) {
975 // Sort empty suffixes higher.
976 if (RHS.PatchSuffix.empty())
977 return true;
978 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +0000979 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000980
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000981 // Provide a lexicographic sort to make this a total ordering.
982 return PatchSuffix < RHS.PatchSuffix;
983 }
984
985 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000986 return false;
987}
988
Rafael Espindola1af7c212012-02-19 01:38:32 +0000989static StringRef getGCCToolchainDir(const ArgList &Args) {
990 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
991 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +0000992 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +0000993 return GCC_INSTALL_PREFIX;
994}
995
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000996/// \brief Construct a GCCInstallationDetector from the driver.
997///
998/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +0000999/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +00001000///
1001/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1002/// should instead pull the target out of the driver. This is currently
1003/// necessary because the driver doesn't store the final version of the target
1004/// triple.
1005Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
Chandler Carruthb427c562013-06-22 11:35:51 +00001006 const Driver &D, const llvm::Triple &TargetTriple, const ArgList &Args)
Chandler Carruth866faab2012-01-25 07:21:38 +00001007 : IsValid(false) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001008 llvm::Triple BiarchVariantTriple =
1009 TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
Chandler Carruth779579b2012-02-13 02:02:09 +00001010 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001011 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001012 // The library directories which may contain GCC installations.
Chandler Carruthb427c562013-06-22 11:35:51 +00001013 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001014 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001015 SmallVector<StringRef, 10> CandidateTripleAliases;
Chandler Carruthb427c562013-06-22 11:35:51 +00001016 SmallVector<StringRef, 10> CandidateBiarchTripleAliases;
1017 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1018 CandidateTripleAliases, CandidateBiarchLibDirs,
1019 CandidateBiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001020
1021 // Compute the set of prefixes for our search.
1022 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1023 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001024
Rafael Espindola1af7c212012-02-19 01:38:32 +00001025 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1026 if (GCCToolchainDir != "") {
1027 if (GCCToolchainDir.back() == '/')
1028 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001029
Rafael Espindola1af7c212012-02-19 01:38:32 +00001030 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001031 } else {
1032 Prefixes.push_back(D.SysRoot);
1033 Prefixes.push_back(D.SysRoot + "/usr");
1034 Prefixes.push_back(D.InstalledDir + "/..");
1035 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001036
1037 // Loop over the various components which exist and select the best GCC
1038 // installation available. GCC installs are ranked by version number.
1039 Version = GCCVersion::Parse("0.0.0");
1040 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1041 if (!llvm::sys::fs::exists(Prefixes[i]))
1042 continue;
1043 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1044 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1045 if (!llvm::sys::fs::exists(LibDir))
1046 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001047 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001048 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1049 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001050 }
Chandler Carruthb427c562013-06-22 11:35:51 +00001051 for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) {
1052 const std::string LibDir = Prefixes[i] + CandidateBiarchLibDirs[j].str();
Chandler Carruth866faab2012-01-25 07:21:38 +00001053 if (!llvm::sys::fs::exists(LibDir))
1054 continue;
Chandler Carruthb427c562013-06-22 11:35:51 +00001055 for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke;
Chandler Carruth866faab2012-01-25 07:21:38 +00001056 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001057 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruthb427c562013-06-22 11:35:51 +00001058 CandidateBiarchTripleAliases[k],
1059 /*NeedsBiarchSuffix=*/ true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001060 }
1061 }
1062}
1063
1064/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruthb427c562013-06-22 11:35:51 +00001065 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
Chandler Carruth866faab2012-01-25 07:21:38 +00001066 SmallVectorImpl<StringRef> &LibDirs,
1067 SmallVectorImpl<StringRef> &TripleAliases,
Chandler Carruthb427c562013-06-22 11:35:51 +00001068 SmallVectorImpl<StringRef> &BiarchLibDirs,
1069 SmallVectorImpl<StringRef> &BiarchTripleAliases) {
Chandler Carruth866faab2012-01-25 07:21:38 +00001070 // Declare a bunch of static data sets that we'll select between below. These
1071 // are specifically designed to always refer to string literals to avoid any
1072 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001073 static const char *const AArch64LibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001074 static const char *const AArch64Triples[] = { "aarch64-none-linux-gnu",
1075 "aarch64-linux-gnu" };
Tim Northover9bb857a2013-01-31 12:13:10 +00001076
Chandler Carruth866faab2012-01-25 07:21:38 +00001077 static const char *const ARMLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001078 static const char *const ARMTriples[] = { "arm-linux-gnueabi",
1079 "arm-linux-androideabi" };
1080 static const char *const ARMHFTriples[] = { "arm-linux-gnueabihf",
1081 "armv7hl-redhat-linux-gnueabi" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001082
1083 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1084 static const char *const X86_64Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001085 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", "x86_64-pc-linux-gnu",
1086 "x86_64-redhat-linux6E", "x86_64-redhat-linux", "x86_64-suse-linux",
1087 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", "x86_64-slackware-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001088 };
1089 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1090 static const char *const X86Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001091 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", "i386-linux-gnu",
1092 "i386-redhat-linux6E", "i686-redhat-linux", "i586-redhat-linux",
1093 "i386-redhat-linux", "i586-suse-linux", "i486-slackware-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001094 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001095 };
1096
1097 static const char *const MIPSLibDirs[] = { "/lib" };
1098 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1099 static const char *const MIPSELLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001100 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu",
1101 "mipsel-linux-android",
1102 "mips-linux-gnu" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001103
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001104 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1105 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1106 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1107 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1108
Chandler Carruth866faab2012-01-25 07:21:38 +00001109 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1110 static const char *const PPCTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001111 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
1112 "powerpc-suse-linux", "powerpc-montavista-linuxspe"
Chandler Carruth866faab2012-01-25 07:21:38 +00001113 };
1114 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001115 static const char *const PPC64Triples[] = { "powerpc64-linux-gnu",
1116 "powerpc64-unknown-linux-gnu",
1117 "powerpc64-suse-linux",
1118 "ppc64-redhat-linux" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001119
Ulrich Weigand47445072013-05-06 16:26:41 +00001120 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1121 static const char *const SystemZTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001122 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
1123 "s390x-suse-linux", "s390x-redhat-linux"
Ulrich Weigand47445072013-05-06 16:26:41 +00001124 };
1125
Chandler Carruth866faab2012-01-25 07:21:38 +00001126 switch (TargetTriple.getArch()) {
Tim Northover9bb857a2013-01-31 12:13:10 +00001127 case llvm::Triple::aarch64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001128 LibDirs.append(AArch64LibDirs,
1129 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1130 TripleAliases.append(AArch64Triples,
1131 AArch64Triples + llvm::array_lengthof(AArch64Triples));
1132 BiarchLibDirs.append(AArch64LibDirs,
1133 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1134 BiarchTripleAliases.append(
1135 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
Tim Northover9bb857a2013-01-31 12:13:10 +00001136 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001137 case llvm::Triple::arm:
1138 case llvm::Triple::thumb:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001139 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001140 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001141 TripleAliases.append(ARMHFTriples,
1142 ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001143 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001144 TripleAliases.append(ARMTriples,
1145 ARMTriples + llvm::array_lengthof(ARMTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001146 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001147 break;
1148 case llvm::Triple::x86_64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001149 LibDirs.append(X86_64LibDirs,
1150 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1151 TripleAliases.append(X86_64Triples,
1152 X86_64Triples + llvm::array_lengthof(X86_64Triples));
1153 BiarchLibDirs.append(X86LibDirs,
1154 X86LibDirs + llvm::array_lengthof(X86LibDirs));
1155 BiarchTripleAliases.append(X86Triples,
1156 X86Triples + llvm::array_lengthof(X86Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001157 break;
1158 case llvm::Triple::x86:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001159 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001160 TripleAliases.append(X86Triples,
1161 X86Triples + llvm::array_lengthof(X86Triples));
1162 BiarchLibDirs.append(X86_64LibDirs,
1163 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1164 BiarchTripleAliases.append(
1165 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001166 break;
1167 case llvm::Triple::mips:
Chandler Carruthb427c562013-06-22 11:35:51 +00001168 LibDirs.append(MIPSLibDirs,
1169 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1170 TripleAliases.append(MIPSTriples,
1171 MIPSTriples + llvm::array_lengthof(MIPSTriples));
1172 BiarchLibDirs.append(MIPS64LibDirs,
1173 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1174 BiarchTripleAliases.append(
1175 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001176 break;
1177 case llvm::Triple::mipsel:
Chandler Carruthb427c562013-06-22 11:35:51 +00001178 LibDirs.append(MIPSELLibDirs,
1179 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1180 TripleAliases.append(MIPSELTriples,
1181 MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
1182 BiarchLibDirs.append(
1183 MIPS64ELLibDirs,
1184 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1185 BiarchTripleAliases.append(
1186 MIPS64ELTriples,
1187 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001188 break;
1189 case llvm::Triple::mips64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001190 LibDirs.append(MIPS64LibDirs,
1191 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1192 TripleAliases.append(MIPS64Triples,
1193 MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1194 BiarchLibDirs.append(MIPSLibDirs,
1195 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1196 BiarchTripleAliases.append(MIPSTriples,
1197 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001198 break;
1199 case llvm::Triple::mips64el:
Chandler Carruthb427c562013-06-22 11:35:51 +00001200 LibDirs.append(MIPS64ELLibDirs,
1201 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001202 TripleAliases.append(
Chandler Carruthb427c562013-06-22 11:35:51 +00001203 MIPS64ELTriples,
1204 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1205 BiarchLibDirs.append(MIPSELLibDirs,
1206 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1207 BiarchTripleAliases.append(
1208 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001209 break;
1210 case llvm::Triple::ppc:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001211 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001212 TripleAliases.append(PPCTriples,
1213 PPCTriples + llvm::array_lengthof(PPCTriples));
1214 BiarchLibDirs.append(PPC64LibDirs,
1215 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1216 BiarchTripleAliases.append(
1217 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001218 break;
1219 case llvm::Triple::ppc64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001220 LibDirs.append(PPC64LibDirs,
1221 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1222 TripleAliases.append(PPC64Triples,
1223 PPC64Triples + llvm::array_lengthof(PPC64Triples));
1224 BiarchLibDirs.append(PPCLibDirs,
1225 PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1226 BiarchTripleAliases.append(PPCTriples,
1227 PPCTriples + llvm::array_lengthof(PPCTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001228 break;
Ulrich Weigand47445072013-05-06 16:26:41 +00001229 case llvm::Triple::systemz:
Chandler Carruthb427c562013-06-22 11:35:51 +00001230 LibDirs.append(SystemZLibDirs,
1231 SystemZLibDirs + llvm::array_lengthof(SystemZLibDirs));
1232 TripleAliases.append(SystemZTriples,
1233 SystemZTriples + llvm::array_lengthof(SystemZTriples));
Ulrich Weigand47445072013-05-06 16:26:41 +00001234 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001235
1236 default:
1237 // By default, just rely on the standard lib directories and the original
1238 // triple.
1239 break;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001240 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001241
1242 // Always append the drivers target triple to the end, in case it doesn't
1243 // match any of our aliases.
1244 TripleAliases.push_back(TargetTriple.str());
1245
1246 // Also include the multiarch variant if it's different.
Chandler Carruthb427c562013-06-22 11:35:51 +00001247 if (TargetTriple.str() != BiarchTriple.str())
1248 BiarchTripleAliases.push_back(BiarchTriple.str());
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001249}
1250
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001251static bool isSoftFloatABI(const ArgList &Args) {
1252 Arg *A = Args.getLastArg(options::OPT_msoft_float,
1253 options::OPT_mhard_float,
1254 options::OPT_mfloat_abi_EQ);
1255 if (!A) return false;
1256
1257 return A->getOption().matches(options::OPT_msoft_float) ||
1258 (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
1259 A->getValue() == StringRef("soft"));
1260}
1261
1262static bool isMipsArch(llvm::Triple::ArchType Arch) {
1263 return Arch == llvm::Triple::mips ||
1264 Arch == llvm::Triple::mipsel ||
1265 Arch == llvm::Triple::mips64 ||
1266 Arch == llvm::Triple::mips64el;
1267}
1268
1269static bool isMips16(const ArgList &Args) {
1270 Arg *A = Args.getLastArg(options::OPT_mips16,
1271 options::OPT_mno_mips16);
1272 return A && A->getOption().matches(options::OPT_mips16);
1273}
1274
1275static bool isMicroMips(const ArgList &Args) {
1276 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1277 options::OPT_mno_micromips);
1278 return A && A->getOption().matches(options::OPT_mmicromips);
1279}
1280
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001281// FIXME: There is the same routine in the Tools.cpp.
1282static bool hasMipsN32ABIArg(const ArgList &Args) {
1283 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00001284 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001285}
1286
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001287static void appendMipsTargetSuffix(std::string &Path,
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001288 llvm::Triple::ArchType TargetArch,
1289 const ArgList &Args) {
1290 if (isMips16(Args))
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001291 Path += "/mips16";
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001292 else if (isMicroMips(Args))
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001293 Path += "/micromips";
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001294
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001295 if (isSoftFloatABI(Args))
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001296 Path += "/soft-float";
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001297
1298 if (TargetArch == llvm::Triple::mipsel ||
1299 TargetArch == llvm::Triple::mips64el)
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001300 Path += "/el";
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001301}
1302
1303static StringRef getMipsTargetABISuffix(llvm::Triple::ArchType TargetArch,
1304 const ArgList &Args) {
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001305 if (TargetArch == llvm::Triple::mips64 ||
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001306 TargetArch == llvm::Triple::mips64el)
1307 return hasMipsN32ABIArg(Args) ? "/n32" : "/64";
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001308
1309 return "/32";
1310}
1311
Chandler Carruthb427c562013-06-22 11:35:51 +00001312static bool findTargetBiarchSuffix(std::string &Suffix, StringRef Path,
1313 llvm::Triple::ArchType TargetArch,
1314 const ArgList &Args) {
1315 // FIXME: This routine was only intended to model bi-arch toolchains which
1316 // use -m32 and -m64 to swap between variants of a target. It shouldn't be
1317 // doing ABI-based builtin location for MIPS.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001318 if (isMipsArch(TargetArch)) {
1319 StringRef ABISuffix = getMipsTargetABISuffix(TargetArch, Args);
1320
1321 // First build and check a complex path to crtbegin.o
1322 // depends on command line options (-mips16, -msoft-float, ...)
1323 // like mips-linux-gnu/4.7/mips16/soft-float/el/crtbegin.o
1324 appendMipsTargetSuffix(Suffix, TargetArch, Args);
1325
1326 if (TargetArch == llvm::Triple::mips64 ||
1327 TargetArch == llvm::Triple::mips64el)
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001328 Suffix += ABISuffix;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001329
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001330 if (llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o"))
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001331 return true;
1332
1333 // Then fall back and probe a simple case like
1334 // mips-linux-gnu/4.7/32/crtbegin.o
1335 Suffix = ABISuffix;
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001336 return llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001337 }
1338
1339 if (TargetArch == llvm::Triple::x86_64 ||
Ulrich Weigand47445072013-05-06 16:26:41 +00001340 TargetArch == llvm::Triple::ppc64 ||
1341 TargetArch == llvm::Triple::systemz)
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001342 Suffix = "/64";
1343 else
1344 Suffix = "/32";
1345
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001346 return llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001347}
1348
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001349void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001350 llvm::Triple::ArchType TargetArch, const ArgList &Args,
Chandler Carruthb427c562013-06-22 11:35:51 +00001351 const std::string &LibDir, StringRef CandidateTriple,
1352 bool NeedsBiarchSuffix) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001353 // There are various different suffixes involving the triple we
1354 // check for. We also record what is necessary to walk from each back
1355 // up to the lib directory.
Chandler Carruth866faab2012-01-25 07:21:38 +00001356 const std::string LibSuffixes[] = {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001357 "/gcc/" + CandidateTriple.str(),
1358 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1359
Hal Finkelf3587912012-09-18 22:25:07 +00001360 // The Freescale PPC SDK has the gcc libraries in
1361 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1362 "/" + CandidateTriple.str(),
1363
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001364 // Ubuntu has a strange mis-matched pair of triples that this happens to
1365 // match.
1366 // FIXME: It may be worthwhile to generalize this and look for a second
1367 // triple.
Chandler Carruth6e46ca22011-11-09 03:46:20 +00001368 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001369 };
Chandler Carruthb427c562013-06-22 11:35:51 +00001370 const std::string InstallSuffixes[] = { "/../../..", "/../../../..", "/../..",
1371 "/../../../.." };
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001372 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruthb427c562013-06-22 11:35:51 +00001373 const unsigned NumLibSuffixes =
1374 (llvm::array_lengthof(LibSuffixes) - (TargetArch != llvm::Triple::x86));
Chandler Carruth866faab2012-01-25 07:21:38 +00001375 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1376 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001377 llvm::error_code EC;
Chandler Carruth866faab2012-01-25 07:21:38 +00001378 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001379 !EC && LI != LE; LI = LI.increment(EC)) {
1380 StringRef VersionText = llvm::sys::path::filename(LI->path());
1381 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1382 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1383 if (CandidateVersion < MinVersion)
1384 continue;
1385 if (CandidateVersion <= Version)
1386 continue;
Hal Finkel221e11e2011-12-08 05:50:03 +00001387
1388 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth64cee062012-01-24 19:21:42 +00001389 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth866faab2012-01-25 07:21:38 +00001390 // libs in a subdirectory named 64. The simple logic we follow is that
1391 // *if* there is a subdirectory of the right name with crtbegin.o in it,
Chandler Carruthb427c562013-06-22 11:35:51 +00001392 // we use that. If not, and if not a biarch triple alias, we look for
Chandler Carruth866faab2012-01-25 07:21:38 +00001393 // crtbegin.o without the subdirectory.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001394
Chandler Carruthb427c562013-06-22 11:35:51 +00001395 std::string BiarchSuffix;
1396 if (findTargetBiarchSuffix(BiarchSuffix, LI->path(), TargetArch, Args)) {
1397 GCCBiarchSuffix = BiarchSuffix;
Chandler Carruth866faab2012-01-25 07:21:38 +00001398 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001399 if (NeedsBiarchSuffix ||
Chandler Carruth866faab2012-01-25 07:21:38 +00001400 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1401 continue;
Chandler Carruthb427c562013-06-22 11:35:51 +00001402 GCCBiarchSuffix.clear();
Chandler Carruth866faab2012-01-25 07:21:38 +00001403 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001404
1405 Version = CandidateVersion;
Chandler Carruth4d9d7682012-01-24 19:28:29 +00001406 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001407 // FIXME: We hack together the directory name here instead of
1408 // using LI to ensure stable path separators across Windows and
1409 // Linux.
Chandler Carruth866faab2012-01-25 07:21:38 +00001410 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth64cee062012-01-24 19:21:42 +00001411 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001412 IsValid = true;
1413 }
1414 }
1415}
1416
Rafael Espindola1af7c212012-02-19 01:38:32 +00001417Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1418 const ArgList &Args)
Rafael Espindola84b588b2013-03-18 18:10:27 +00001419 : ToolChain(D, Triple, Args), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbar88979912010-08-01 22:29:51 +00001420 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001421 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001422 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001423}
1424
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001425Generic_GCC::~Generic_GCC() {
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001426}
1427
Rafael Espindola7cf32212013-03-20 03:05:54 +00001428Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +00001429 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001430 case Action::PreprocessJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001431 if (!Preprocess)
1432 Preprocess.reset(new tools::gcc::Preprocess(*this));
1433 return Preprocess.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001434 case Action::PrecompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001435 if (!Precompile)
1436 Precompile.reset(new tools::gcc::Precompile(*this));
1437 return Precompile.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001438 case Action::CompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001439 if (!Compile)
1440 Compile.reset(new tools::gcc::Compile(*this));
1441 return Compile.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +00001442 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001443 return ToolChain::getTool(AC);
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001444 }
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001445}
1446
Rafael Espindola7cf32212013-03-20 03:05:54 +00001447Tool *Generic_GCC::buildAssembler() const {
1448 return new tools::gcc::Assemble(*this);
1449}
1450
1451Tool *Generic_GCC::buildLinker() const {
1452 return new tools::gcc::Link(*this);
1453}
1454
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001455bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001456 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001457}
1458
Chandler Carruth76a943b2012-11-19 03:52:03 +00001459bool Generic_GCC::isPICDefault() const {
1460 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001461}
1462
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001463bool Generic_GCC::isPIEDefault() const {
1464 return false;
1465}
1466
Chandler Carruth76a943b2012-11-19 03:52:03 +00001467bool Generic_GCC::isPICDefaultForced() const {
1468 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001469}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001470
Tony Linthicum76329bf2011-12-12 21:14:55 +00001471/// Hexagon Toolchain
1472
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001473std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1474
1475 // Locate the rest of the toolchain ...
1476 if (strlen(GCC_INSTALL_PREFIX))
1477 return std::string(GCC_INSTALL_PREFIX);
1478
1479 std::string InstallRelDir = InstalledDir + "/../../gnu";
1480 if (llvm::sys::fs::exists(InstallRelDir))
1481 return InstallRelDir;
1482
1483 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1484 if (llvm::sys::fs::exists(PrefixRelDir))
1485 return PrefixRelDir;
1486
1487 return InstallRelDir;
1488}
1489
Matthew Curtise689b052012-12-06 15:46:07 +00001490static void GetHexagonLibraryPaths(
1491 const ArgList &Args,
1492 const std::string Ver,
1493 const std::string MarchString,
1494 const std::string &InstalledDir,
1495 ToolChain::path_list *LibPaths)
1496{
1497 bool buildingLib = Args.hasArg(options::OPT_shared);
1498
1499 //----------------------------------------------------------------------------
1500 // -L Args
1501 //----------------------------------------------------------------------------
1502 for (arg_iterator
1503 it = Args.filtered_begin(options::OPT_L),
1504 ie = Args.filtered_end();
1505 it != ie;
1506 ++it) {
1507 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1508 LibPaths->push_back((*it)->getValue(i));
1509 }
1510
1511 //----------------------------------------------------------------------------
1512 // Other standard paths
1513 //----------------------------------------------------------------------------
1514 const std::string MarchSuffix = "/" + MarchString;
1515 const std::string G0Suffix = "/G0";
1516 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1517 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1518
1519 // lib/gcc/hexagon/...
1520 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1521 if (buildingLib) {
1522 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1523 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1524 }
1525 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1526 LibPaths->push_back(LibGCCHexagonDir + Ver);
1527
1528 // lib/gcc/...
1529 LibPaths->push_back(RootDir + "lib/gcc");
1530
1531 // hexagon/lib/...
1532 std::string HexagonLibDir = RootDir + "hexagon/lib";
1533 if (buildingLib) {
1534 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1535 LibPaths->push_back(HexagonLibDir + G0Suffix);
1536 }
1537 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1538 LibPaths->push_back(HexagonLibDir);
1539}
1540
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001541Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1542 const ArgList &Args)
1543 : Linux(D, Triple, Args) {
1544 const std::string InstalledDir(getDriver().getInstalledDir());
1545 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1546
1547 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1548 // program paths
1549 const std::string BinDir(GnuDir + "/bin");
1550 if (llvm::sys::fs::exists(BinDir))
1551 getProgramPaths().push_back(BinDir);
1552
1553 // Determine version of GCC libraries and headers to use.
1554 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1555 llvm::error_code ec;
1556 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1557 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1558 !ec && di != de; di = di.increment(ec)) {
1559 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1560 if (MaxVersion < cv)
1561 MaxVersion = cv;
1562 }
1563 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001564
1565 ToolChain::path_list *LibPaths= &getFilePaths();
1566
1567 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1568 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1569 // support 'linux' we'll need to fix this up
1570 LibPaths->clear();
1571
1572 GetHexagonLibraryPaths(
1573 Args,
1574 GetGCCLibAndIncVersion(),
1575 GetTargetCPU(Args),
1576 InstalledDir,
1577 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001578}
1579
1580Hexagon_TC::~Hexagon_TC() {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001581}
1582
Rafael Espindola7cf32212013-03-20 03:05:54 +00001583Tool *Hexagon_TC::buildAssembler() const {
1584 return new tools::hexagon::Assemble(*this);
1585}
1586
1587Tool *Hexagon_TC::buildLinker() const {
1588 return new tools::hexagon::Link(*this);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001589}
1590
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001591void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1592 ArgStringList &CC1Args) const {
1593 const Driver &D = getDriver();
1594
1595 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1596 DriverArgs.hasArg(options::OPT_nostdlibinc))
1597 return;
1598
1599 llvm::sys::Path InstallDir(D.InstalledDir);
1600 std::string Ver(GetGCCLibAndIncVersion());
1601 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1602 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1603 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1604 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1605 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001606}
1607
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001608void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1609 ArgStringList &CC1Args) const {
1610
1611 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1612 DriverArgs.hasArg(options::OPT_nostdincxx))
1613 return;
1614
1615 const Driver &D = getDriver();
1616 std::string Ver(GetGCCLibAndIncVersion());
1617 llvm::sys::Path IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
1618
1619 IncludeDir.appendComponent("hexagon/include/c++/");
1620 IncludeDir.appendComponent(Ver);
1621 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001622}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001623
Matthew Curtise689b052012-12-06 15:46:07 +00001624ToolChain::CXXStdlibType
1625Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1626 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1627 if (!A)
1628 return ToolChain::CST_Libstdcxx;
1629
1630 StringRef Value = A->getValue();
1631 if (Value != "libstdc++") {
1632 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1633 << A->getAsString(Args);
1634 }
1635
1636 return ToolChain::CST_Libstdcxx;
1637}
1638
1639static Arg *GetLastHexagonArchArg(const ArgList &Args)
Matthew Curtisf10a5952012-12-06 14:16:43 +00001640{
1641 Arg *A = NULL;
1642
1643 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1644 it != ie; ++it) {
1645 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
1646 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1647 A = *it;
1648 A->claim();
1649 } else if ((*it)->getOption().matches(options::OPT_m_Joined)) {
1650 StringRef Value = (*it)->getValue(0);
1651 if (Value.startswith("v")) {
1652 A = *it;
1653 A->claim();
1654 }
1655 }
1656 }
1657 return A;
1658}
1659
1660StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1661{
1662 // Select the default CPU (v4) if none was given or detection failed.
1663 Arg *A = GetLastHexagonArchArg (Args);
1664 if (A) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001665 StringRef WhichHexagon = A->getValue();
Matthew Curtisf10a5952012-12-06 14:16:43 +00001666 if (WhichHexagon.startswith("hexagon"))
1667 return WhichHexagon.substr(sizeof("hexagon") - 1);
1668 if (WhichHexagon != "")
1669 return WhichHexagon;
1670 }
1671
1672 return "v4";
1673}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001674// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001675
Chris Lattner09797542010-03-04 21:07:38 +00001676/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1677/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1678/// Currently does not support anything else but compilation.
1679
Rafael Espindola84b588b2013-03-18 18:10:27 +00001680TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
1681 const ArgList &Args)
1682 : ToolChain(D, Triple, Args) {
Chris Lattner09797542010-03-04 21:07:38 +00001683 // Path mangling to find libexec
1684 std::string Path(getDriver().Dir);
1685
1686 Path += "/../libexec";
1687 getProgramPaths().push_back(Path);
1688}
1689
1690TCEToolChain::~TCEToolChain() {
Chris Lattner09797542010-03-04 21:07:38 +00001691}
1692
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001693bool TCEToolChain::IsMathErrnoDefault() const {
1694 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001695}
1696
Chandler Carruth76a943b2012-11-19 03:52:03 +00001697bool TCEToolChain::isPICDefault() const {
1698 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001699}
1700
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001701bool TCEToolChain::isPIEDefault() const {
1702 return false;
1703}
1704
Chandler Carruth76a943b2012-11-19 03:52:03 +00001705bool TCEToolChain::isPICDefaultForced() const {
1706 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001707}
1708
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001709/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1710
Rafael Espindola1af7c212012-02-19 01:38:32 +00001711OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1712 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001713 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001714 getFilePaths().push_back("/usr/lib");
1715}
1716
Rafael Espindola7cf32212013-03-20 03:05:54 +00001717Tool *OpenBSD::buildAssembler() const {
1718 return new tools::openbsd::Assemble(*this);
1719}
1720
1721Tool *OpenBSD::buildLinker() const {
1722 return new tools::openbsd::Link(*this);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001723}
1724
Eli Friedman9fa28852012-08-08 23:57:20 +00001725/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1726
1727Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1728 : Generic_ELF(D, Triple, Args) {
1729 getFilePaths().push_back(getDriver().Dir + "/../lib");
1730 getFilePaths().push_back("/usr/lib");
1731}
1732
Rafael Espindola7cf32212013-03-20 03:05:54 +00001733Tool *Bitrig::buildAssembler() const {
1734 return new tools::bitrig::Assemble(*this);
1735}
1736
1737Tool *Bitrig::buildLinker() const {
1738 return new tools::bitrig::Link(*this);
Eli Friedman9fa28852012-08-08 23:57:20 +00001739}
1740
1741void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1742 ArgStringList &CC1Args) const {
1743 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1744 DriverArgs.hasArg(options::OPT_nostdincxx))
1745 return;
1746
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001747 switch (GetCXXStdlibType(DriverArgs)) {
1748 case ToolChain::CST_Libcxx:
1749 addSystemInclude(DriverArgs, CC1Args,
1750 getDriver().SysRoot + "/usr/include/c++/");
1751 break;
1752 case ToolChain::CST_Libstdcxx:
1753 addSystemInclude(DriverArgs, CC1Args,
1754 getDriver().SysRoot + "/usr/include/c++/stdc++");
1755 addSystemInclude(DriverArgs, CC1Args,
1756 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001757
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001758 StringRef Triple = getTriple().str();
1759 if (Triple.startswith("amd64"))
1760 addSystemInclude(DriverArgs, CC1Args,
1761 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1762 Triple.substr(5));
1763 else
1764 addSystemInclude(DriverArgs, CC1Args,
1765 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1766 Triple);
1767 break;
1768 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001769}
1770
1771void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1772 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001773 switch (GetCXXStdlibType(Args)) {
1774 case ToolChain::CST_Libcxx:
1775 CmdArgs.push_back("-lc++");
1776 CmdArgs.push_back("-lcxxrt");
1777 // Include supc++ to provide Unwind until provided by libcxx.
1778 CmdArgs.push_back("-lgcc");
1779 break;
1780 case ToolChain::CST_Libstdcxx:
1781 CmdArgs.push_back("-lstdc++");
1782 break;
1783 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001784}
1785
Daniel Dunbare24297c2009-03-30 21:06:03 +00001786/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1787
Rafael Espindola1af7c212012-02-19 01:38:32 +00001788FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1789 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001790
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001791 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1792 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001793 if ((Triple.getArch() == llvm::Triple::x86 ||
1794 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001795 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001796 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1797 else
1798 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001799}
1800
Rafael Espindola7cf32212013-03-20 03:05:54 +00001801Tool *FreeBSD::buildAssembler() const {
1802 return new tools::freebsd::Assemble(*this);
1803}
1804
1805Tool *FreeBSD::buildLinker() const {
1806 return new tools::freebsd::Link(*this);
Daniel Dunbare24297c2009-03-30 21:06:03 +00001807}
Daniel Dunbarcc912342009-05-02 18:28:39 +00001808
Rafael Espindola0f207ed2012-12-13 04:17:14 +00001809bool FreeBSD::UseSjLjExceptions() const {
1810 // FreeBSD uses SjLj exceptions on ARM oabi.
1811 switch (getTriple().getEnvironment()) {
1812 case llvm::Triple::GNUEABI:
1813 case llvm::Triple::EABI:
1814 return false;
1815
1816 default:
1817 return (getTriple().getArch() == llvm::Triple::arm ||
1818 getTriple().getArch() == llvm::Triple::thumb);
1819 }
1820}
1821
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001822/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1823
Rafael Espindola1af7c212012-02-19 01:38:32 +00001824NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1825 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001826
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001827 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001828 // When targeting a 32-bit platform, try the special directory used on
1829 // 64-bit hosts, and only fall back to the main library directory if that
1830 // doesn't work.
1831 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1832 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00001833 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001834 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001835
1836 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001837 }
1838}
1839
Rafael Espindola7cf32212013-03-20 03:05:54 +00001840Tool *NetBSD::buildAssembler() const {
1841 return new tools::netbsd::Assemble(*this);
1842}
1843
1844Tool *NetBSD::buildLinker() const {
1845 return new tools::netbsd::Link(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001846}
1847
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00001848ToolChain::CXXStdlibType
1849NetBSD::GetCXXStdlibType(const ArgList &Args) const {
1850 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
1851 StringRef Value = A->getValue();
1852 if (Value == "libstdc++")
1853 return ToolChain::CST_Libstdcxx;
1854 if (Value == "libc++")
1855 return ToolChain::CST_Libcxx;
1856
1857 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1858 << A->getAsString(Args);
1859 }
1860
1861 return ToolChain::CST_Libstdcxx;
1862}
1863
1864void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1865 ArgStringList &CC1Args) const {
1866 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1867 DriverArgs.hasArg(options::OPT_nostdincxx))
1868 return;
1869
1870 switch (GetCXXStdlibType(DriverArgs)) {
1871 case ToolChain::CST_Libcxx:
1872 addSystemInclude(DriverArgs, CC1Args,
1873 getDriver().SysRoot + "/usr/include/c++/");
1874 break;
1875 case ToolChain::CST_Libstdcxx:
1876 addSystemInclude(DriverArgs, CC1Args,
1877 getDriver().SysRoot + "/usr/include/g++");
1878 addSystemInclude(DriverArgs, CC1Args,
1879 getDriver().SysRoot + "/usr/include/g++/backward");
1880 break;
1881 }
1882}
1883
Chris Lattner3e2ee142010-07-07 16:01:42 +00001884/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1885
Rafael Espindola1af7c212012-02-19 01:38:32 +00001886Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1887 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00001888 getFilePaths().push_back(getDriver().Dir + "/../lib");
1889 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00001890}
1891
Rafael Espindola7cf32212013-03-20 03:05:54 +00001892Tool *Minix::buildAssembler() const {
1893 return new tools::minix::Assemble(*this);
1894}
1895
1896Tool *Minix::buildLinker() const {
1897 return new tools::minix::Link(*this);
Chris Lattner3e2ee142010-07-07 16:01:42 +00001898}
1899
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001900/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1901
Rafael Espindola1af7c212012-02-19 01:38:32 +00001902AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1903 const ArgList &Args)
1904 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001905
Daniel Dunbar88979912010-08-01 22:29:51 +00001906 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001907 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001908 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001909
Daniel Dunbar083edf72009-12-21 18:54:17 +00001910 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001911 getFilePaths().push_back("/usr/lib");
1912 getFilePaths().push_back("/usr/sfw/lib");
1913 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00001914 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001915
1916}
1917
Rafael Espindola7cf32212013-03-20 03:05:54 +00001918Tool *AuroraUX::buildAssembler() const {
1919 return new tools::auroraux::Assemble(*this);
1920}
1921
1922Tool *AuroraUX::buildLinker() const {
1923 return new tools::auroraux::Link(*this);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001924}
1925
David Chisnallf571cde2012-02-15 13:39:01 +00001926/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1927
Rafael Espindola1af7c212012-02-19 01:38:32 +00001928Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1929 const ArgList &Args)
1930 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00001931
1932 getProgramPaths().push_back(getDriver().getInstalledDir());
1933 if (getDriver().getInstalledDir() != getDriver().Dir)
1934 getProgramPaths().push_back(getDriver().Dir);
1935
1936 getFilePaths().push_back(getDriver().Dir + "/../lib");
1937 getFilePaths().push_back("/usr/lib");
1938}
1939
Rafael Espindola7cf32212013-03-20 03:05:54 +00001940Tool *Solaris::buildAssembler() const {
1941 return new tools::solaris::Assemble(*this);
1942}
1943
1944Tool *Solaris::buildLinker() const {
1945 return new tools::solaris::Link(*this);
David Chisnallf571cde2012-02-15 13:39:01 +00001946}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001947
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001948/// Distribution (very bare-bones at the moment).
Eli Friedman5cd659f2009-05-26 07:52:18 +00001949
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001950enum Distro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00001951 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001952 DebianLenny,
1953 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00001954 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00001955 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00001956 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00001957 RHEL4,
1958 RHEL5,
1959 RHEL6,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001960 Fedora13,
1961 Fedora14,
Eric Christopher534b6a02011-04-06 18:22:53 +00001962 Fedora15,
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00001963 Fedora16,
Eric Christopher534b6a02011-04-06 18:22:53 +00001964 FedoraRawhide,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001965 OpenSuse11_3,
David Chisnallb8f65e22011-05-19 13:26:33 +00001966 OpenSuse11_4,
1967 OpenSuse12_1,
Douglas Gregorf7b88412012-04-30 23:42:57 +00001968 OpenSuse12_2,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00001969 UbuntuHardy,
1970 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00001971 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00001972 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001973 UbuntuLucid,
1974 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001975 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00001976 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00001977 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00001978 UbuntuQuantal,
1979 UbuntuRaring,
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00001980 UbuntuSaucy,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001981 UnknownDistro
1982};
1983
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001984static bool IsRedhat(enum Distro Distro) {
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00001985 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1986 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001987}
1988
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001989static bool IsOpenSuse(enum Distro Distro) {
Douglas Gregorf7b88412012-04-30 23:42:57 +00001990 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001991}
1992
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001993static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00001994 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001995}
1996
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001997static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00001998 return Distro >= UbuntuHardy && Distro <= UbuntuSaucy;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001999}
2000
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002001static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00002002 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002003 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002004 StringRef Data = File.get()->getBuffer();
2005 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002006 Data.split(Lines, "\n");
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002007 Distro Version = UnknownDistro;
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002008 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2009 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002010 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002011 .Case("hardy", UbuntuHardy)
2012 .Case("intrepid", UbuntuIntrepid)
2013 .Case("jaunty", UbuntuJaunty)
2014 .Case("karmic", UbuntuKarmic)
2015 .Case("lucid", UbuntuLucid)
2016 .Case("maverick", UbuntuMaverick)
2017 .Case("natty", UbuntuNatty)
2018 .Case("oneiric", UbuntuOneiric)
2019 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002020 .Case("quantal", UbuntuQuantal)
2021 .Case("raring", UbuntuRaring)
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002022 .Case("saucy", UbuntuSaucy)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002023 .Default(UnknownDistro);
2024 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002025 }
2026
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002027 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002028 StringRef Data = File.get()->getBuffer();
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002029 if (Data.startswith("Fedora release 16"))
2030 return Fedora16;
2031 else if (Data.startswith("Fedora release 15"))
Eric Christopher534b6a02011-04-06 18:22:53 +00002032 return Fedora15;
2033 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002034 return Fedora14;
Eric Christopher534b6a02011-04-06 18:22:53 +00002035 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002036 return Fedora13;
Eric Christopher534b6a02011-04-06 18:22:53 +00002037 else if (Data.startswith("Fedora release") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002038 Data.find("Rawhide") != StringRef::npos)
Eric Christopher534b6a02011-04-06 18:22:53 +00002039 return FedoraRawhide;
Chris Lattner84e38552011-05-22 05:36:06 +00002040 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002041 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002042 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002043 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002044 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002045 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002046 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002047 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002048 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002049 Data.find("release 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002050 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002051 return UnknownDistro;
2052 }
2053
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002054 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002055 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002056 if (Data[0] == '5')
2057 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002058 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002059 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002060 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002061 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002062 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2063 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002064 return UnknownDistro;
2065 }
2066
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002067 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002068 return llvm::StringSwitch<Distro>(File.get()->getBuffer())
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002069 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2070 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2071 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregorf7b88412012-04-30 23:42:57 +00002072 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002073 .Default(UnknownDistro);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002074
Michael J. Spencerf6efe582011-01-10 02:34:13 +00002075 bool Exists;
2076 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola12479842010-11-11 02:07:13 +00002077 return Exherbo;
2078
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002079 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2080 return ArchLinux;
2081
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002082 return UnknownDistro;
2083}
2084
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002085/// \brief Get our best guess at the multiarch triple for a target.
2086///
2087/// Debian-based systems are starting to use a multiarch setup where they use
2088/// a target-triple directory in the library and header search paths.
2089/// Unfortunately, this triple does not align with the vanilla target triple,
2090/// so we provide a rough mapping here.
2091static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2092 StringRef SysRoot) {
2093 // For most architectures, just use whatever we have rather than trying to be
2094 // clever.
2095 switch (TargetTriple.getArch()) {
2096 default:
2097 return TargetTriple.str();
2098
2099 // We use the existence of '/lib/<triple>' as a directory to detect some
2100 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002101 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2102 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002103 case llvm::Triple::arm:
2104 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002105 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2106 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2107 return "arm-linux-gnueabihf";
2108 } else {
2109 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2110 return "arm-linux-gnueabi";
2111 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002112 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002113 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002114 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2115 return "i386-linux-gnu";
2116 return TargetTriple.str();
2117 case llvm::Triple::x86_64:
2118 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2119 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002120 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002121 case llvm::Triple::aarch64:
2122 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2123 return "aarch64-linux-gnu";
Tim Northoverfbb56b72013-06-13 22:54:55 +00002124 return TargetTriple.str();
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002125 case llvm::Triple::mips:
2126 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2127 return "mips-linux-gnu";
2128 return TargetTriple.str();
2129 case llvm::Triple::mipsel:
2130 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2131 return "mipsel-linux-gnu";
2132 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002133 case llvm::Triple::ppc:
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00002134 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2135 return "powerpc-linux-gnuspe";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002136 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2137 return "powerpc-linux-gnu";
2138 return TargetTriple.str();
2139 case llvm::Triple::ppc64:
2140 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2141 return "powerpc64-linux-gnu";
2142 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002143 }
2144}
2145
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002146static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2147 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2148}
2149
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002150static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2151 const ArgList &Args) {
2152 if (Arch != llvm::Triple::mips &&
2153 Arch != llvm::Triple::mipsel)
2154 return false;
2155
2156 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2157 options::OPT_mcpu_EQ,
2158 options::OPT_mips_CPUs_Group);
2159
2160 if (!A)
2161 return false;
2162
2163 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2164 return A->getOption().matches(options::OPT_mips32r2);
2165
Richard Smithbd55daf2012-11-01 04:30:05 +00002166 return A->getValue() == StringRef("mips32r2");
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002167}
2168
Simon Atanasyand4413882012-09-14 11:27:24 +00002169static StringRef getMultilibDir(const llvm::Triple &Triple,
2170 const ArgList &Args) {
2171 if (!isMipsArch(Triple.getArch()))
2172 return Triple.isArch32Bit() ? "lib32" : "lib64";
2173
2174 // lib32 directory has a special meaning on MIPS targets.
2175 // It contains N32 ABI binaries. Use this folder if produce
2176 // code for N32 ABI only.
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00002177 if (hasMipsN32ABIArg(Args))
Simon Atanasyand4413882012-09-14 11:27:24 +00002178 return "lib32";
2179
2180 return Triple.isArch32Bit() ? "lib" : "lib64";
2181}
2182
Rafael Espindola1af7c212012-02-19 01:38:32 +00002183Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2184 : Generic_ELF(D, Triple, Args) {
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002185 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002186 std::string SysRoot = computeSysRoot(Args);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002187
Chandler Carruthd6c62b62013-06-20 23:37:54 +00002188 // Cross-compiling binutils and GCC installations (vanilla and OpenSuse at
2189 // least) put various tools in a triple-prefixed directory off of the parent
2190 // of the GCC installation. We use the GCC triple here to ensure that we end
2191 // up with tools that support the same amount of cross compiling as the
2192 // detected GCC installation. For example, if we find a GCC installation
2193 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
2194 // used to target i386.
2195 // FIXME: This seems unlikely to be Linux-specific.
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002196 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002197 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002198 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002199
2200 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002201
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002202 Distro Distro = DetectDistro(Arch);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002203
Chris Lattnerd075c822011-05-22 16:45:07 +00002204 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002205 ExtraOpts.push_back("-z");
2206 ExtraOpts.push_back("relro");
2207 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002208
Douglas Gregord9bb1522011-03-06 19:11:49 +00002209 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002210 ExtraOpts.push_back("-X");
2211
Logan Chienc6fd8202012-09-02 09:30:11 +00002212 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002213 const bool IsMips = isMipsArch(Arch);
2214
2215 if (IsMips && !SysRoot.empty())
2216 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002217
Chandler Carruth0b842912011-12-09 04:45:18 +00002218 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2219 // and the MIPS ABI require .dynsym to be sorted in different ways.
2220 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2221 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002222 // Android loader does not support .gnu.hash.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002223 if (!IsMips && !IsAndroid) {
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002224 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2225 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002226 ExtraOpts.push_back("--hash-style=gnu");
2227
2228 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2229 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2230 ExtraOpts.push_back("--hash-style=both");
2231 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002232
Chris Lattner84e38552011-05-22 05:36:06 +00002233 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002234 ExtraOpts.push_back("--no-add-needed");
2235
Eli Friedmanf7600942011-06-02 21:36:53 +00002236 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002237 Distro == DebianJessie || IsOpenSuse(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002238 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002239 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002240 ExtraOpts.push_back("--build-id");
2241
Chris Lattnerd075c822011-05-22 16:45:07 +00002242 if (IsOpenSuse(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002243 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002244
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002245 // The selection of paths to try here is designed to match the patterns which
2246 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2247 // This was determined by running GCC in a fake filesystem, creating all
2248 // possible permutations of these directories, and seeing which ones it added
2249 // to the link paths.
2250 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002251
Simon Atanasyand4413882012-09-14 11:27:24 +00002252 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002253 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002254
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002255 // Add the multilib suffixed paths where they are available.
2256 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002257 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002258 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002259
2260 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2261 addPathIfExists(GCCInstallation.getInstallPath() +
Chandler Carruthb427c562013-06-22 11:35:51 +00002262 GCCInstallation.getBiarchSuffix() + "/mips-r2",
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002263 Paths);
2264 else
2265 addPathIfExists((GCCInstallation.getInstallPath() +
Chandler Carruthb427c562013-06-22 11:35:51 +00002266 GCCInstallation.getBiarchSuffix()),
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002267 Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002268
2269 // If the GCC installation we found is inside of the sysroot, we want to
2270 // prefer libraries installed in the parent prefix of the GCC installation.
2271 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002272 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002273 // This usually happens when there is an external cross compiler on the
2274 // host system, and a more minimal sysroot available that is the target of
2275 // the cross.
2276 if (StringRef(LibPath).startswith(SysRoot)) {
2277 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2278 Paths);
2279 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2280 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2281 }
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002282 // On Android, libraries in the parent prefix of the GCC installation are
2283 // preferred to the ones under sysroot.
2284 if (IsAndroid) {
2285 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2286 }
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002287 // Sourcery CodeBench MIPS toolchain holds some libraries under
2288 // the parent prefix of the GCC installation.
2289 if (IsMips) {
Rafael Espindola34cfafd2013-04-30 12:24:40 +00002290 std::string Suffix;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002291 appendMipsTargetSuffix(Suffix, Arch, Args);
2292 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" +
Rafael Espindola34cfafd2013-04-30 12:24:40 +00002293 Multilib + Suffix,
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002294 Paths);
2295 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002296 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002297 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2298 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2299 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2300 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2301
Chandler Carruthb427c562013-06-22 11:35:51 +00002302 // Try walking via the GCC triple path in case of biarch or multiarch GCC
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002303 // installations with strange symlinks.
2304 if (GCCInstallation.isValid())
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002305 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002306 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002307
Chandler Carrutha7b44142011-10-16 10:54:30 +00002308 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002309 if (GCCInstallation.isValid()) {
2310 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002311 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruthb427c562013-06-22 11:35:51 +00002312 if (!GCCInstallation.getBiarchSuffix().empty())
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002313 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002314
2315 if (StringRef(LibPath).startswith(SysRoot)) {
2316 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2317 addPathIfExists(LibPath, Paths);
2318 }
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002319 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002320 addPathIfExists(SysRoot + "/lib", Paths);
2321 addPathIfExists(SysRoot + "/usr/lib", Paths);
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002322
2323 IsPIEDefault = SanitizerArgs(*this, Args).hasZeroBaseShadow();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002324}
2325
2326bool Linux::HasNativeLLVMSupport() const {
2327 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002328}
2329
Rafael Espindola7cf32212013-03-20 03:05:54 +00002330Tool *Linux::buildLinker() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002331 return new tools::gnutools::Link(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00002332}
2333
2334Tool *Linux::buildAssembler() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002335 return new tools::gnutools::Assemble(*this);
Rafael Espindola92b00932010-08-10 00:25:48 +00002336}
2337
Chandler Carruth05fb5852012-11-21 23:40:23 +00002338void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2339 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002340 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carruth05fb5852012-11-21 23:40:23 +00002341 bool UseInitArrayDefault
2342 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002343 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002344 getTriple().getEnvironment() == llvm::Triple::Android;
2345 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2346 options::OPT_fno_use_init_array,
2347 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002348 CC1Args.push_back("-fuse-init-array");
2349}
2350
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002351std::string Linux::computeSysRoot(const ArgList &Args) const {
2352 if (!getDriver().SysRoot.empty())
2353 return getDriver().SysRoot;
2354
2355 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
2356 return std::string();
2357
Rafael Espindola34cfafd2013-04-30 12:24:40 +00002358 std::string Path =
2359 (GCCInstallation.getInstallPath() +
2360 "/../../../../" + GCCInstallation.getTriple().str() + "/libc").str();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002361 appendMipsTargetSuffix(Path, getTriple().getArch(), Args);
2362
Rafael Espindola34cfafd2013-04-30 12:24:40 +00002363 return llvm::sys::fs::exists(Path) ? Path : "";
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002364}
2365
Chandler Carrutha796f532011-11-05 20:17:13 +00002366void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2367 ArgStringList &CC1Args) const {
2368 const Driver &D = getDriver();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002369 std::string SysRoot = computeSysRoot(DriverArgs);
Chandler Carrutha796f532011-11-05 20:17:13 +00002370
2371 if (DriverArgs.hasArg(options::OPT_nostdinc))
2372 return;
2373
2374 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002375 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002376
2377 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carrutha796f532011-11-05 20:17:13 +00002378 llvm::sys::Path P(D.ResourceDir);
2379 P.appendComponent("include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002380 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002381 }
2382
2383 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2384 return;
2385
2386 // Check for configure-time C include directories.
2387 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2388 if (CIncludeDirs != "") {
2389 SmallVector<StringRef, 5> dirs;
2390 CIncludeDirs.split(dirs, ":");
2391 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2392 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002393 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? SysRoot : "";
Chandler Carrutha796f532011-11-05 20:17:13 +00002394 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2395 }
2396 return;
2397 }
2398
2399 // Lacking those, try to detect the correct set of system includes for the
2400 // target triple.
2401
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002402 // Sourcery CodeBench and modern FSF Mips toolchains put extern C
2403 // system includes under three additional directories.
2404 if (GCCInstallation.isValid() && isMipsArch(getTriple().getArch())) {
Chandler Carruthb427c562013-06-22 11:35:51 +00002405 addExternCSystemIncludeIfExists(
2406 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + "/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002407
Chandler Carruthb427c562013-06-22 11:35:51 +00002408 addExternCSystemIncludeIfExists(
2409 DriverArgs, CC1Args,
2410 GCCInstallation.getInstallPath() + "/../../../../" +
2411 GCCInstallation.getTriple().str() + "/libc/usr/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002412 }
2413
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002414 // Implement generic Debian multiarch support.
2415 const StringRef X86_64MultiarchIncludeDirs[] = {
2416 "/usr/include/x86_64-linux-gnu",
2417
2418 // FIXME: These are older forms of multiarch. It's not clear that they're
2419 // in use in any released version of Debian, so we should consider
2420 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002421 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002422 };
2423 const StringRef X86MultiarchIncludeDirs[] = {
2424 "/usr/include/i386-linux-gnu",
2425
2426 // FIXME: These are older forms of multiarch. It's not clear that they're
2427 // in use in any released version of Debian, so we should consider
2428 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002429 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002430 "/usr/include/i486-linux-gnu"
2431 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002432 const StringRef AArch64MultiarchIncludeDirs[] = {
2433 "/usr/include/aarch64-linux-gnu"
2434 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002435 const StringRef ARMMultiarchIncludeDirs[] = {
2436 "/usr/include/arm-linux-gnueabi"
2437 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002438 const StringRef ARMHFMultiarchIncludeDirs[] = {
2439 "/usr/include/arm-linux-gnueabihf"
2440 };
Eli Friedman7771c832011-11-11 03:05:19 +00002441 const StringRef MIPSMultiarchIncludeDirs[] = {
2442 "/usr/include/mips-linux-gnu"
2443 };
2444 const StringRef MIPSELMultiarchIncludeDirs[] = {
2445 "/usr/include/mipsel-linux-gnu"
2446 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002447 const StringRef PPCMultiarchIncludeDirs[] = {
2448 "/usr/include/powerpc-linux-gnu"
2449 };
2450 const StringRef PPC64MultiarchIncludeDirs[] = {
2451 "/usr/include/powerpc64-linux-gnu"
2452 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002453 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002454 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002455 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002456 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002457 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002458 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2459 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002460 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002461 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2462 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2463 else
2464 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002465 } else if (getTriple().getArch() == llvm::Triple::mips) {
2466 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2467 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2468 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002469 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2470 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2471 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2472 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002473 }
2474 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2475 E = MultiarchIncludeDirs.end();
2476 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002477 if (llvm::sys::fs::exists(SysRoot + *I)) {
2478 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + *I);
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002479 break;
2480 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002481 }
2482
2483 if (getTriple().getOS() == llvm::Triple::RTEMS)
2484 return;
2485
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002486 // Add an include of '/include' directly. This isn't provided by default by
2487 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2488 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002489 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002490
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002491 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002492}
2493
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002494/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002495/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2496 const ArgList &DriverArgs,
2497 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002498 if (!llvm::sys::fs::exists(Base))
2499 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002500 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002501 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002502 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002503 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002504}
2505
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002506/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2507/// libstdc++ installation.
2508/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2509 Twine TargetArchDir,
2510 Twine MultiLibSuffix,
2511 const ArgList &DriverArgs,
2512 ArgStringList &CC1Args) {
2513 if (!addLibStdCXXIncludePaths(Base+Suffix, TargetArchDir + MultiLibSuffix,
2514 DriverArgs, CC1Args))
2515 return false;
2516
2517 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
2518 + MultiLibSuffix);
2519 return true;
2520}
2521
Chandler Carrutha796f532011-11-05 20:17:13 +00002522void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2523 ArgStringList &CC1Args) const {
2524 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2525 DriverArgs.hasArg(options::OPT_nostdincxx))
2526 return;
2527
Chandler Carruthf4701732011-11-07 09:01:17 +00002528 // Check if libc++ has been enabled and provide its include paths if so.
2529 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2530 // libc++ is always installed at a fixed path on Linux currently.
2531 addSystemInclude(DriverArgs, CC1Args,
2532 getDriver().SysRoot + "/usr/include/c++/v1");
2533 return;
2534 }
2535
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002536 // We need a detected GCC installation on Linux to provide libstdc++'s
2537 // headers. We handled the libc++ case above.
2538 if (!GCCInstallation.isValid())
2539 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002540
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002541 // By default, look for the C++ headers in an include directory adjacent to
2542 // the lib directory of the GCC installation. Note that this is expect to be
2543 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2544 StringRef LibDir = GCCInstallation.getParentLibPath();
2545 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola66aa0452012-06-19 01:26:10 +00002546 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002547 StringRef TripleStr = GCCInstallation.getTriple().str();
2548
Chandler Carruthb427c562013-06-22 11:35:51 +00002549 if (addLibStdCXXIncludePaths(
2550 LibDir.str() + "/../include", "/c++/" + Version.str(), TripleStr,
2551 GCCInstallation.getBiarchSuffix(), DriverArgs, CC1Args))
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002552 return;
2553
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002554 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002555 // Gentoo is weird and places its headers inside the GCC install, so if the
2556 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002557 InstallDir.str() + "/include/g++-v4",
2558 // Android standalone toolchain has C++ headers in yet another place.
2559 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkelf3587912012-09-18 22:25:07 +00002560 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2561 // without a subdirectory corresponding to the gcc version.
2562 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002563 };
2564
2565 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
Chandler Carruthb427c562013-06-22 11:35:51 +00002566 if (addLibStdCXXIncludePaths(
2567 IncludePathCandidates[i],
2568 (TripleStr + GCCInstallation.getBiarchSuffix()), DriverArgs,
2569 CC1Args))
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002570 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002571 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002572}
2573
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002574bool Linux::isPIEDefault() const {
2575 return IsPIEDefault;
2576}
2577
Daniel Dunbarcc912342009-05-02 18:28:39 +00002578/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2579
Rafael Espindola1af7c212012-02-19 01:38:32 +00002580DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2581 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002582
2583 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002584 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002585 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002586 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002587
Daniel Dunbar083edf72009-12-21 18:54:17 +00002588 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002589 getFilePaths().push_back("/usr/lib");
John McCall65b8da02013-04-11 22:55:55 +00002590 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
2591 getFilePaths().push_back("/usr/lib/gcc47");
2592 else
2593 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002594}
2595
Rafael Espindola7cf32212013-03-20 03:05:54 +00002596Tool *DragonFly::buildAssembler() const {
2597 return new tools::dragonfly::Assemble(*this);
2598}
2599
2600Tool *DragonFly::buildLinker() const {
2601 return new tools::dragonfly::Link(*this);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002602}