blob: a78370f0e0d77e4a7576e178152f6942be70f142 [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).
Rafael Espindola355670d2013-06-25 15:14:22 +0000253 if (AlwaysLink || llvm::sys::fs::exists(P.str()))
Eric Christopherc235d0c62011-06-22 17:41:40 +0000254 CmdArgs.push_back(Args.MakeArgString(P.str()));
255}
256
Daniel Dunbar6276f992009-09-18 08:15:13 +0000257void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
258 ArgStringList &CmdArgs) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000259 // Darwin only supports the compiler-rt based runtime libraries.
260 switch (GetRuntimeLibType(Args)) {
261 case ToolChain::RLT_CompilerRT:
262 break;
263 default:
264 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smithbd55daf2012-11-01 04:30:05 +0000265 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000266 return;
267 }
268
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000269 // Darwin doesn't support real static executables, don't link any runtime
270 // libraries with -static.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000271 if (Args.hasArg(options::OPT_static) ||
272 Args.hasArg(options::OPT_fapple_kext) ||
273 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar6276f992009-09-18 08:15:13 +0000274 return;
Daniel Dunbar6276f992009-09-18 08:15:13 +0000275
276 // Reject -static-libgcc for now, we can deal with this when and if someone
277 // cares. This is useful in situations where someone wants to statically link
278 // something like libstdc++, and needs its runtime support routines.
279 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000280 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000281 << A->getAsString(Args);
282 return;
283 }
284
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000285 // If we are building profile support, link that library in.
286 if (Args.hasArg(options::OPT_fprofile_arcs) ||
287 Args.hasArg(options::OPT_fprofile_generate) ||
288 Args.hasArg(options::OPT_fcreate_profile) ||
289 Args.hasArg(options::OPT_coverage)) {
290 // Select the appropriate runtime library for the target.
291 if (isTargetIPhoneOS()) {
292 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
293 } else {
294 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
295 }
296 }
297
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000298 SanitizerArgs Sanitize(*this, Args);
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000299
Alexey Samsonovcc429802012-11-16 12:53:14 +0000300 // Add Ubsan runtime library, if required.
301 if (Sanitize.needsUbsanRt()) {
Alexey Samsonov969be242013-01-21 08:45:02 +0000302 if (isTargetIPhoneOS()) {
Alexey Samsonovcc429802012-11-16 12:53:14 +0000303 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
304 << "-fsanitize=undefined";
305 } else {
Alexey Samsonov8368b372012-11-21 14:17:42 +0000306 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonovcc429802012-11-16 12:53:14 +0000307
308 // The Ubsan runtime library requires C++.
309 AddCXXStdlibLibArgs(Args, CmdArgs);
310 }
311 }
312
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000313 // Add ASAN runtime library, if required. Dynamic libraries and bundles
314 // should not be linked with the runtime library.
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000315 if (Sanitize.needsAsanRt()) {
Alexander Potapenko5700f402013-03-21 10:49:06 +0000316 if (isTargetIPhoneOS() && !isTargetIOSSimulator()) {
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000317 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000318 << "-fsanitize=address";
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000319 } else {
Alexander Potapenko5700f402013-03-21 10:49:06 +0000320 if (Args.hasArg(options::OPT_dynamiclib) ||
321 Args.hasArg(options::OPT_bundle)) {
322 // Assume the binary will provide the ASan runtime.
323 } else {
324 AddLinkRuntimeLib(Args, CmdArgs,
325 "libclang_rt.asan_osx_dynamic.dylib", true);
326 // The ASAN runtime library requires C++.
327 AddCXXStdlibLibArgs(Args, CmdArgs);
328 }
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000329 }
330 }
331
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000332 // Otherwise link libSystem, then the dynamic runtime library, and finally any
333 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000334 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000335
336 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar15c89422010-01-27 00:56:37 +0000337 if (isTargetIPhoneOS()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000338 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
339 // it never went into the SDK.
Bob Wilson102be442011-10-07 17:54:41 +0000340 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
341 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
342 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000343
Daniel Dunbard1076382011-04-18 23:48:36 +0000344 // We currently always need a static runtime library for iOS.
Eric Christopherc235d0c62011-06-22 17:41:40 +0000345 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000346 } else {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000347 // The dynamic runtime library was merged with libSystem for 10.6 and
348 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000349 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000350 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000351 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000352 CmdArgs.push_back("-lgcc_s.10.5");
353
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000354 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000355 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000356 // omitted from 10.4.dylib.
357 //
358 // Unfortunately, that turned out to not be true, because Darwin system
359 // headers can still use eprintf on i386, and it is not exported from
360 // libSystem. Therefore, we still must provide a runtime library just for
361 // the tiny tiny handful of projects that *might* use that symbol.
362 if (isMacosxVersionLT(10, 5)) {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000363 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000364 } else {
365 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopherc235d0c62011-06-22 17:41:40 +0000366 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
367 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000368 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000369 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000370}
371
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000372void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000373 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000374
Daniel Dunbar455a0492012-08-17 18:43:50 +0000375 // Support allowing the SDKROOT environment variable used by xcrun and other
376 // Xcode tools to define the default sysroot, by making it the default for
377 // isysroot.
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000378 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
379 // Warn if the path does not exist.
Rafael Espindola355670d2013-06-25 15:14:22 +0000380 if (!llvm::sys::fs::exists(A->getValue()))
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000381 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
382 } else {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000383 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbarb2543042013-01-15 20:33:56 +0000384 // We only use this value as the default if it is an absolute path,
385 // exists, and it is not the root path.
386 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
387 StringRef(env) != "/") {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000388 Args.append(Args.MakeSeparateArg(
389 0, Opts.getOption(options::OPT_isysroot), env));
390 }
391 }
392 }
393
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000394 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000395 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
396 Arg *iOSSimVersion = Args.getLastArg(
397 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman027e9c32012-01-11 02:41:15 +0000398
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000399 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000400 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000401 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000402 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
403 iOSVersion = iOSSimVersion = 0;
404 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000405 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000406 << iOSVersion->getAsString(Args)
407 << iOSSimVersion->getAsString(Args);
408 iOSSimVersion = 0;
409 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosier64707fe2011-08-31 20:56:25 +0000410 // If no deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000411 // environment defines.
Chad Rosier64707fe2011-08-31 20:56:25 +0000412 StringRef OSXTarget;
413 StringRef iOSTarget;
414 StringRef iOSSimTarget;
415 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
416 OSXTarget = env;
417 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
418 iOSTarget = env;
419 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
420 iOSSimTarget = env;
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000421
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000422 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosier64707fe2011-08-31 20:56:25 +0000423 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif5d3231c2012-04-18 10:59:08 +0000424 // based on -isysroot.
Chad Rosier64707fe2011-08-31 20:56:25 +0000425 if (iOSTarget.empty()) {
426 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
427 StringRef first, second;
Richard Smithbd55daf2012-11-01 04:30:05 +0000428 StringRef isysroot = A->getValue();
Chad Rosier64707fe2011-08-31 20:56:25 +0000429 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
430 if (second != "")
431 iOSTarget = second.substr(0,3);
432 }
433 }
Daniel Dunbard54669d2010-01-26 01:45:19 +0000434
Chad Rosierfe6fd362011-09-28 00:46:32 +0000435 // If no OSX or iOS target has been specified and we're compiling for armv7,
436 // go ahead as assume we're targeting iOS.
Chad Rosier7b1fee12012-05-09 18:55:57 +0000437 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilsond7cf1042012-09-29 23:52:50 +0000438 (getDarwinArchName(Args) == "armv7" ||
439 getDarwinArchName(Args) == "armv7s"))
Chad Rosierf761fe92012-05-09 18:09:58 +0000440 iOSTarget = iOSVersionMin;
Chad Rosierfe6fd362011-09-28 00:46:32 +0000441
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000442 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000443 //
444 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000445
446 // Do not allow conflicts with the iOS simulator target.
Chad Rosier64707fe2011-08-31 20:56:25 +0000447 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000448 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000449 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosier64707fe2011-08-31 20:56:25 +0000450 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000451 "IPHONEOS_DEPLOYMENT_TARGET");
452 }
453
454 // Allow conflicts among OSX and iOS for historical reasons, but choose the
455 // default platform.
Chad Rosier64707fe2011-08-31 20:56:25 +0000456 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000457 if (getTriple().getArch() == llvm::Triple::arm ||
458 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosier64707fe2011-08-31 20:56:25 +0000459 OSXTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000460 else
Chad Rosier64707fe2011-08-31 20:56:25 +0000461 iOSTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000462 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000463
Chad Rosier64707fe2011-08-31 20:56:25 +0000464 if (!OSXTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000465 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000466 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
467 Args.append(OSXVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000468 } else if (!iOSTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000469 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000470 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
471 Args.append(iOSVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000472 } else if (!iOSSimTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000473 const Option O = Opts.getOption(
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000474 options::OPT_mios_simulator_version_min_EQ);
475 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
476 Args.append(iOSSimVersion);
Daniel Dunbard54669d2010-01-26 01:45:19 +0000477 } else {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000478 // Otherwise, assume we are targeting OS X.
Michael J. Spencerfc790902012-10-19 22:36:40 +0000479 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000480 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
481 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000482 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000485 // Reject invalid architecture combinations.
486 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
487 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000488 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000489 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
490 }
491
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000492 // Set the tool chain target information.
493 unsigned Major, Minor, Micro;
494 bool HadExtra;
495 if (OSXVersion) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000496 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000497 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000498 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000499 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000500 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000501 << OSXVersion->getAsString(Args);
502 } else {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000503 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
504 assert(Version && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000505 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman027e9c32012-01-11 02:41:15 +0000506 Micro, HadExtra) || HadExtra ||
507 Major >= 10 || Minor >= 100 || Micro >= 100)
508 getDriver().Diag(diag::err_drv_invalid_version_number)
509 << Version->getAsString(Args);
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000510 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000511
Daniel Dunbarb1189432011-04-30 04:18:16 +0000512 bool IsIOSSim = bool(iOSSimVersion);
513
514 // In GCC, the simulator historically was treated as being OS X in some
515 // contexts, like determining the link logic, despite generally being called
516 // with an iOS deployment target. For compatibility, we detect the
517 // simulator as iOS + x86, and treat it differently in a few contexts.
518 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
519 getTriple().getArch() == llvm::Triple::x86_64))
520 IsIOSSim = true;
521
522 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000523}
524
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000525void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000526 ArgStringList &CmdArgs) const {
527 CXXStdlibType Type = GetCXXStdlibType(Args);
528
529 switch (Type) {
530 case ToolChain::CST_Libcxx:
531 CmdArgs.push_back("-lc++");
532 break;
533
534 case ToolChain::CST_Libstdcxx: {
535 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
536 // it was previously found in the gcc lib dir. However, for all the Darwin
537 // platforms we care about it was -lstdc++.6, so we search for that
538 // explicitly if we can't see an obvious -lstdc++ candidate.
539
540 // Check in the sysroot first.
541 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000542 llvm::sys::Path P(A->getValue());
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000543 P.appendComponent("usr");
544 P.appendComponent("lib");
545 P.appendComponent("libstdc++.dylib");
546
Rafael Espindola355670d2013-06-25 15:14:22 +0000547 if (!llvm::sys::fs::exists(P.str())) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000548 P.eraseComponent();
549 P.appendComponent("libstdc++.6.dylib");
Rafael Espindola355670d2013-06-25 15:14:22 +0000550 if (llvm::sys::fs::exists(P.str())) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000551 CmdArgs.push_back(Args.MakeArgString(P.str()));
552 return;
553 }
554 }
555 }
556
557 // Otherwise, look in the root.
Bob Wilson1a9ad0f2011-11-11 07:47:04 +0000558 // FIXME: This should be removed someday when we don't have to care about
559 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Rafael Espindola355670d2013-06-25 15:14:22 +0000560 if (!llvm::sys::fs::exists("/usr/lib/libstdc++.dylib") &&
561 llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib")) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000562 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
563 return;
564 }
565
566 // Otherwise, let the linker search.
567 CmdArgs.push_back("-lstdc++");
568 break;
569 }
570 }
571}
572
Shantonu Senafeb03b2010-09-17 18:39:08 +0000573void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
574 ArgStringList &CmdArgs) const {
575
576 // For Darwin platforms, use the compiler-rt-based support library
577 // instead of the gcc-provided one (which is also incidentally
578 // only present in the gcc lib dir, which makes it hard to find).
579
580 llvm::sys::Path P(getDriver().ResourceDir);
581 P.appendComponent("lib");
582 P.appendComponent("darwin");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000583
584 // Use the newer cc_kext for iOS ARM after 6.0.
585 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
586 !isIPhoneOSVersionLT(6, 0)) {
587 P.appendComponent("libclang_rt.cc_kext.a");
588 } else {
589 P.appendComponent("libclang_rt.cc_kext_ios5.a");
590 }
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000591
Shantonu Senafeb03b2010-09-17 18:39:08 +0000592 // For now, allow missing resource libraries to support developers who may
593 // not have compiler-rt checked out or integrated into their build.
Rafael Espindola355670d2013-06-25 15:14:22 +0000594 if (llvm::sys::fs::exists(P.str()))
Shantonu Senafeb03b2010-09-17 18:39:08 +0000595 CmdArgs.push_back(Args.MakeArgString(P.str()));
596}
597
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000598DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
599 const char *BoundArch) const {
600 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
601 const OptTable &Opts = getDriver().getOpts();
602
603 // FIXME: We really want to get out of the tool chain level argument
604 // translation business, as it makes the driver functionality much
605 // more opaque. For now, we follow gcc closely solely for the
606 // purpose of easily achieving feature parity & testability. Once we
607 // have something that works, we should reevaluate each translation
608 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000609
Daniel Dunbar775d4062010-06-11 22:00:26 +0000610 for (ArgList::const_iterator it = Args.begin(),
611 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000612 Arg *A = *it;
613
614 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar471c4f82011-06-21 00:20:17 +0000615 // Skip this argument unless the architecture matches either the toolchain
616 // triple arch, or the arch being bound.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000617 llvm::Triple::ArchType XarchArch =
Richard Smithbd55daf2012-11-01 04:30:05 +0000618 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000619 if (!(XarchArch == getArch() ||
620 (BoundArch && XarchArch ==
Rafael Espindoladcbf6982012-10-31 18:51:07 +0000621 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000622 continue;
623
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000624 Arg *OriginalArg = A;
Richard Smithbd55daf2012-11-01 04:30:05 +0000625 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000626 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000627 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000628
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000629 // If the argument parsing failed or more than one argument was
630 // consumed, the -Xarch_ argument's parameter tried to consume
631 // extra arguments. Emit an error and ignore.
632 //
633 // We also want to disallow any options which would alter the
634 // driver behavior; that isn't going to work in our model. We
635 // use isDriverOption() as an approximation, although things
636 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000637 if (!XarchArg || Index > Prev + 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000638 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar6914a982011-04-21 17:32:21 +0000639 << A->getAsString(Args);
640 continue;
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000641 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000642 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000643 << A->getAsString(Args);
644 continue;
645 }
646
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000647 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000648 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000649
650 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000651
652 // Linker input arguments require custom handling. The problem is that we
653 // have already constructed the phase actions, so we can not treat them as
654 // "input arguments".
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000655 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000656 // Convert the argument into individual Zlinker_input_args.
657 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
658 DAL->AddSeparateArg(OriginalArg,
659 Opts.getOption(options::OPT_Zlinker_input),
Richard Smithbd55daf2012-11-01 04:30:05 +0000660 A->getValue(i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000661
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000662 }
663 continue;
664 }
Mike Stump11289f42009-09-09 15:08:12 +0000665 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000666
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000667 // Sob. These is strictly gcc compatible for the time being. Apple
668 // gcc translates options twice, which means that self-expanding
669 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000670 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000671 default:
672 DAL->append(A);
673 break;
674
675 case options::OPT_mkernel:
676 case options::OPT_fapple_kext:
677 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000678 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000679 break;
Mike Stump11289f42009-09-09 15:08:12 +0000680
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000681 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000682 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smithbd55daf2012-11-01 04:30:05 +0000683 A->getValue());
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000684 break;
685
686 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000687 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
688 DAL->AddFlagArg(A,
689 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000690 break;
691
692 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000693 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
694 DAL->AddFlagArg(A,
695 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000696 break;
697
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000698 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000699 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000700 break;
701
702 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000703 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000704 break;
705
706 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000707 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000708 break;
709
710 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000711 DAL->AddFlagArg(A,
712 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000713 break;
714
715 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000716 DAL->AddFlagArg(A,
717 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000718 break;
719
720 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000721 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000722 break;
723
724 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000725 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000726 break;
727 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000728 }
729
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000730 if (getTriple().getArch() == llvm::Triple::x86 ||
731 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000732 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000733 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000734
735 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosier7c5d9082012-04-27 14:58:16 +0000736 // how the driver driver works.
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000737 if (BoundArch) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000738 StringRef Name = BoundArch;
Michael J. Spencerfc790902012-10-19 22:36:40 +0000739 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
740 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000741
742 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
743 // which defines the list of which architectures we accept.
744 if (Name == "ppc")
745 ;
746 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000747 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000748 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000749 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000750 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000751 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000752 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000753 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000754 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000755 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000756 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000757 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000758 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000759 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000760 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000761 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000762
763 else if (Name == "ppc64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000764 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000765
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000766 else if (Name == "i386")
767 ;
768 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000769 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000770 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000771 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000772 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000773 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000774 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000775 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000776 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000777 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000778 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000779 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000780 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000781 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000782
783 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000784 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000785
786 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000787 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000788 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000789 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000790 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000791 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000792 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000793 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000794 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000795 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000796 else if (Name == "armv6m")
797 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000798 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000799 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000800 else if (Name == "armv7em")
801 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000802 else if (Name == "armv7f")
803 DAL->AddJoinedArg(0, MArch, "armv7f");
804 else if (Name == "armv7k")
805 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000806 else if (Name == "armv7m")
807 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000808 else if (Name == "armv7s")
809 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000810
811 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000812 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000813 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000814
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000815 // Add an explicit version min argument for the deployment target. We do this
816 // after argument translation because -Xarch_ arguments may add a version min
817 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000818 if (BoundArch)
819 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000820
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000821 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
822 // FIXME: It would be far better to avoid inserting those -static arguments,
823 // but we can't check the deployment target in the translation code until
824 // it is set here.
825 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
826 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
827 Arg *A = *it;
828 ++it;
829 if (A->getOption().getID() != options::OPT_mkernel &&
830 A->getOption().getID() != options::OPT_fapple_kext)
831 continue;
832 assert(it != ie && "unexpected argument translation");
833 A = *it;
834 assert(A->getOption().getID() == options::OPT_static &&
835 "missing expected -static argument");
836 it = DAL->getArgs().erase(it);
837 }
838 }
839
Bob Wilson102be442011-10-07 17:54:41 +0000840 // Validate the C++ standard library choice.
841 CXXStdlibType Type = GetCXXStdlibType(*DAL);
842 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000843 // Check whether the target provides libc++.
844 StringRef where;
845
846 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson5ad5a952012-11-09 01:59:30 +0000847 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
848 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000849
850 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000851 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000852 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000853 }
854 }
855
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000856 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000857}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000858
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000859bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000860 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000861}
862
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000863bool Darwin::UseDwarfDebugFlags() const {
864 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
865 return S[0] != '\0';
866 return false;
867}
868
Daniel Dunbar3241d402010-02-10 18:49:11 +0000869bool Darwin::UseSjLjExceptions() const {
870 // Darwin uses SjLj exceptions on ARM.
871 return (getTriple().getArch() == llvm::Triple::arm ||
872 getTriple().getArch() == llvm::Triple::thumb);
873}
874
Chandler Carruth76a943b2012-11-19 03:52:03 +0000875bool Darwin::isPICDefault() const {
876 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000877}
878
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000879bool Darwin::isPIEDefault() const {
880 return false;
881}
882
Chandler Carruth76a943b2012-11-19 03:52:03 +0000883bool Darwin::isPICDefaultForced() const {
884 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000885}
886
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000887bool Darwin::SupportsProfiling() const {
888 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000889 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000890}
891
Daniel Dunbar16334e12010-04-10 16:20:23 +0000892bool Darwin::SupportsObjCGC() const {
893 // Garbage collection is supported everywhere except on iPhone OS.
894 return !isTargetIPhoneOS();
895}
896
John McCall3deb1ad2012-08-21 02:47:43 +0000897void Darwin::CheckObjCARC() const {
898 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
899 return;
John McCall93207072012-08-27 01:56:21 +0000900 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000901}
902
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000903std::string
Chad Rosierd3a0f952011-09-20 20:44:06 +0000904Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
905 types::ID InputType) const {
906 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000907}
908
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000909/// Generic_GCC - A tool chain using the 'gcc' command to perform
910/// all subcommands; this relies on gcc translating the majority of
911/// command line options.
912
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000913/// \brief Parse a GCCVersion object out of a string of text.
914///
915/// This is the primary means of forming GCCVersion objects.
916/*static*/
917Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
918 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
919 std::pair<StringRef, StringRef> First = VersionText.split('.');
920 std::pair<StringRef, StringRef> Second = First.second.split('.');
921
922 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
923 if (First.first.getAsInteger(10, GoodVersion.Major) ||
924 GoodVersion.Major < 0)
925 return BadVersion;
926 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
927 GoodVersion.Minor < 0)
928 return BadVersion;
929
930 // First look for a number prefix and parse that if present. Otherwise just
931 // stash the entire patch string in the suffix, and leave the number
932 // unspecified. This covers versions strings such as:
933 // 4.4
934 // 4.4.0
935 // 4.4.x
936 // 4.4.2-rc4
937 // 4.4.x-patched
938 // And retains any patch number it finds.
939 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
940 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +0000941 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000942 // Try to parse the number and any suffix.
943 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
944 GoodVersion.Patch < 0)
945 return BadVersion;
946 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
947 }
948 }
949
950 return GoodVersion;
951}
952
953/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
954bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000955 if (Major != RHS.Major)
956 return Major < RHS.Major;
957 if (Minor != RHS.Minor)
958 return Minor < RHS.Minor;
959 if (Patch != RHS.Patch) {
960 // Note that versions without a specified patch sort higher than those with
961 // a patch.
962 if (RHS.Patch == -1)
963 return true;
964 if (Patch == -1)
965 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000966
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000967 // Otherwise just sort on the patch itself.
968 return Patch < RHS.Patch;
969 }
970 if (PatchSuffix != RHS.PatchSuffix) {
971 // Sort empty suffixes higher.
972 if (RHS.PatchSuffix.empty())
973 return true;
974 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +0000975 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000976
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000977 // Provide a lexicographic sort to make this a total ordering.
978 return PatchSuffix < RHS.PatchSuffix;
979 }
980
981 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000982 return false;
983}
984
Rafael Espindola1af7c212012-02-19 01:38:32 +0000985static StringRef getGCCToolchainDir(const ArgList &Args) {
986 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
987 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +0000988 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +0000989 return GCC_INSTALL_PREFIX;
990}
991
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000992/// \brief Construct a GCCInstallationDetector from the driver.
993///
994/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +0000995/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +0000996///
997/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
998/// should instead pull the target out of the driver. This is currently
999/// necessary because the driver doesn't store the final version of the target
1000/// triple.
1001Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
Chandler Carruthb427c562013-06-22 11:35:51 +00001002 const Driver &D, const llvm::Triple &TargetTriple, const ArgList &Args)
Chandler Carruth866faab2012-01-25 07:21:38 +00001003 : IsValid(false) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001004 llvm::Triple BiarchVariantTriple =
1005 TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
Chandler Carruth779579b2012-02-13 02:02:09 +00001006 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001007 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001008 // The library directories which may contain GCC installations.
Chandler Carruthb427c562013-06-22 11:35:51 +00001009 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001010 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001011 SmallVector<StringRef, 10> CandidateTripleAliases;
Chandler Carruthb427c562013-06-22 11:35:51 +00001012 SmallVector<StringRef, 10> CandidateBiarchTripleAliases;
1013 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1014 CandidateTripleAliases, CandidateBiarchLibDirs,
1015 CandidateBiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001016
1017 // Compute the set of prefixes for our search.
1018 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1019 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001020
Rafael Espindola1af7c212012-02-19 01:38:32 +00001021 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1022 if (GCCToolchainDir != "") {
1023 if (GCCToolchainDir.back() == '/')
1024 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001025
Rafael Espindola1af7c212012-02-19 01:38:32 +00001026 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001027 } else {
1028 Prefixes.push_back(D.SysRoot);
1029 Prefixes.push_back(D.SysRoot + "/usr");
1030 Prefixes.push_back(D.InstalledDir + "/..");
1031 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001032
1033 // Loop over the various components which exist and select the best GCC
1034 // installation available. GCC installs are ranked by version number.
1035 Version = GCCVersion::Parse("0.0.0");
1036 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1037 if (!llvm::sys::fs::exists(Prefixes[i]))
1038 continue;
1039 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1040 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1041 if (!llvm::sys::fs::exists(LibDir))
1042 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001043 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001044 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1045 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001046 }
Chandler Carruthb427c562013-06-22 11:35:51 +00001047 for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) {
1048 const std::string LibDir = Prefixes[i] + CandidateBiarchLibDirs[j].str();
Chandler Carruth866faab2012-01-25 07:21:38 +00001049 if (!llvm::sys::fs::exists(LibDir))
1050 continue;
Chandler Carruthb427c562013-06-22 11:35:51 +00001051 for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke;
Chandler Carruth866faab2012-01-25 07:21:38 +00001052 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001053 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruthb427c562013-06-22 11:35:51 +00001054 CandidateBiarchTripleAliases[k],
1055 /*NeedsBiarchSuffix=*/ true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001056 }
1057 }
1058}
1059
1060/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruthb427c562013-06-22 11:35:51 +00001061 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
Chandler Carruth866faab2012-01-25 07:21:38 +00001062 SmallVectorImpl<StringRef> &LibDirs,
1063 SmallVectorImpl<StringRef> &TripleAliases,
Chandler Carruthb427c562013-06-22 11:35:51 +00001064 SmallVectorImpl<StringRef> &BiarchLibDirs,
1065 SmallVectorImpl<StringRef> &BiarchTripleAliases) {
Chandler Carruth866faab2012-01-25 07:21:38 +00001066 // Declare a bunch of static data sets that we'll select between below. These
1067 // are specifically designed to always refer to string literals to avoid any
1068 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001069 static const char *const AArch64LibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001070 static const char *const AArch64Triples[] = { "aarch64-none-linux-gnu",
1071 "aarch64-linux-gnu" };
Tim Northover9bb857a2013-01-31 12:13:10 +00001072
Chandler Carruth866faab2012-01-25 07:21:38 +00001073 static const char *const ARMLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001074 static const char *const ARMTriples[] = { "arm-linux-gnueabi",
1075 "arm-linux-androideabi" };
1076 static const char *const ARMHFTriples[] = { "arm-linux-gnueabihf",
1077 "armv7hl-redhat-linux-gnueabi" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001078
1079 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1080 static const char *const X86_64Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001081 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", "x86_64-pc-linux-gnu",
1082 "x86_64-redhat-linux6E", "x86_64-redhat-linux", "x86_64-suse-linux",
1083 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", "x86_64-slackware-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001084 };
1085 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1086 static const char *const X86Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001087 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", "i386-linux-gnu",
1088 "i386-redhat-linux6E", "i686-redhat-linux", "i586-redhat-linux",
1089 "i386-redhat-linux", "i586-suse-linux", "i486-slackware-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001090 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001091 };
1092
1093 static const char *const MIPSLibDirs[] = { "/lib" };
1094 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1095 static const char *const MIPSELLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001096 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu",
1097 "mipsel-linux-android",
1098 "mips-linux-gnu" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001099
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001100 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1101 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1102 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1103 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1104
Chandler Carruth866faab2012-01-25 07:21:38 +00001105 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1106 static const char *const PPCTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001107 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
1108 "powerpc-suse-linux", "powerpc-montavista-linuxspe"
Chandler Carruth866faab2012-01-25 07:21:38 +00001109 };
1110 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001111 static const char *const PPC64Triples[] = { "powerpc64-linux-gnu",
1112 "powerpc64-unknown-linux-gnu",
1113 "powerpc64-suse-linux",
1114 "ppc64-redhat-linux" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001115
Ulrich Weigand47445072013-05-06 16:26:41 +00001116 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1117 static const char *const SystemZTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001118 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
1119 "s390x-suse-linux", "s390x-redhat-linux"
Ulrich Weigand47445072013-05-06 16:26:41 +00001120 };
1121
Chandler Carruth866faab2012-01-25 07:21:38 +00001122 switch (TargetTriple.getArch()) {
Tim Northover9bb857a2013-01-31 12:13:10 +00001123 case llvm::Triple::aarch64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001124 LibDirs.append(AArch64LibDirs,
1125 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1126 TripleAliases.append(AArch64Triples,
1127 AArch64Triples + llvm::array_lengthof(AArch64Triples));
1128 BiarchLibDirs.append(AArch64LibDirs,
1129 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1130 BiarchTripleAliases.append(
1131 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
Tim Northover9bb857a2013-01-31 12:13:10 +00001132 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001133 case llvm::Triple::arm:
1134 case llvm::Triple::thumb:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001135 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001136 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001137 TripleAliases.append(ARMHFTriples,
1138 ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001139 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001140 TripleAliases.append(ARMTriples,
1141 ARMTriples + llvm::array_lengthof(ARMTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001142 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001143 break;
1144 case llvm::Triple::x86_64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001145 LibDirs.append(X86_64LibDirs,
1146 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1147 TripleAliases.append(X86_64Triples,
1148 X86_64Triples + llvm::array_lengthof(X86_64Triples));
1149 BiarchLibDirs.append(X86LibDirs,
1150 X86LibDirs + llvm::array_lengthof(X86LibDirs));
1151 BiarchTripleAliases.append(X86Triples,
1152 X86Triples + llvm::array_lengthof(X86Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001153 break;
1154 case llvm::Triple::x86:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001155 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001156 TripleAliases.append(X86Triples,
1157 X86Triples + llvm::array_lengthof(X86Triples));
1158 BiarchLibDirs.append(X86_64LibDirs,
1159 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1160 BiarchTripleAliases.append(
1161 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001162 break;
1163 case llvm::Triple::mips:
Chandler Carruthb427c562013-06-22 11:35:51 +00001164 LibDirs.append(MIPSLibDirs,
1165 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1166 TripleAliases.append(MIPSTriples,
1167 MIPSTriples + llvm::array_lengthof(MIPSTriples));
1168 BiarchLibDirs.append(MIPS64LibDirs,
1169 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1170 BiarchTripleAliases.append(
1171 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001172 break;
1173 case llvm::Triple::mipsel:
Chandler Carruthb427c562013-06-22 11:35:51 +00001174 LibDirs.append(MIPSELLibDirs,
1175 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1176 TripleAliases.append(MIPSELTriples,
1177 MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
1178 BiarchLibDirs.append(
1179 MIPS64ELLibDirs,
1180 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1181 BiarchTripleAliases.append(
1182 MIPS64ELTriples,
1183 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001184 break;
1185 case llvm::Triple::mips64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001186 LibDirs.append(MIPS64LibDirs,
1187 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1188 TripleAliases.append(MIPS64Triples,
1189 MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1190 BiarchLibDirs.append(MIPSLibDirs,
1191 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1192 BiarchTripleAliases.append(MIPSTriples,
1193 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001194 break;
1195 case llvm::Triple::mips64el:
Chandler Carruthb427c562013-06-22 11:35:51 +00001196 LibDirs.append(MIPS64ELLibDirs,
1197 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001198 TripleAliases.append(
Chandler Carruthb427c562013-06-22 11:35:51 +00001199 MIPS64ELTriples,
1200 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1201 BiarchLibDirs.append(MIPSELLibDirs,
1202 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1203 BiarchTripleAliases.append(
1204 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001205 break;
1206 case llvm::Triple::ppc:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001207 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001208 TripleAliases.append(PPCTriples,
1209 PPCTriples + llvm::array_lengthof(PPCTriples));
1210 BiarchLibDirs.append(PPC64LibDirs,
1211 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1212 BiarchTripleAliases.append(
1213 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001214 break;
1215 case llvm::Triple::ppc64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001216 LibDirs.append(PPC64LibDirs,
1217 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1218 TripleAliases.append(PPC64Triples,
1219 PPC64Triples + llvm::array_lengthof(PPC64Triples));
1220 BiarchLibDirs.append(PPCLibDirs,
1221 PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1222 BiarchTripleAliases.append(PPCTriples,
1223 PPCTriples + llvm::array_lengthof(PPCTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001224 break;
Ulrich Weigand47445072013-05-06 16:26:41 +00001225 case llvm::Triple::systemz:
Chandler Carruthb427c562013-06-22 11:35:51 +00001226 LibDirs.append(SystemZLibDirs,
1227 SystemZLibDirs + llvm::array_lengthof(SystemZLibDirs));
1228 TripleAliases.append(SystemZTriples,
1229 SystemZTriples + llvm::array_lengthof(SystemZTriples));
Ulrich Weigand47445072013-05-06 16:26:41 +00001230 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001231
1232 default:
1233 // By default, just rely on the standard lib directories and the original
1234 // triple.
1235 break;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001236 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001237
1238 // Always append the drivers target triple to the end, in case it doesn't
1239 // match any of our aliases.
1240 TripleAliases.push_back(TargetTriple.str());
1241
1242 // Also include the multiarch variant if it's different.
Chandler Carruthb427c562013-06-22 11:35:51 +00001243 if (TargetTriple.str() != BiarchTriple.str())
1244 BiarchTripleAliases.push_back(BiarchTriple.str());
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001245}
1246
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001247static bool isSoftFloatABI(const ArgList &Args) {
1248 Arg *A = Args.getLastArg(options::OPT_msoft_float,
1249 options::OPT_mhard_float,
1250 options::OPT_mfloat_abi_EQ);
1251 if (!A) return false;
1252
1253 return A->getOption().matches(options::OPT_msoft_float) ||
1254 (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
1255 A->getValue() == StringRef("soft"));
1256}
1257
1258static bool isMipsArch(llvm::Triple::ArchType Arch) {
1259 return Arch == llvm::Triple::mips ||
1260 Arch == llvm::Triple::mipsel ||
1261 Arch == llvm::Triple::mips64 ||
1262 Arch == llvm::Triple::mips64el;
1263}
1264
1265static bool isMips16(const ArgList &Args) {
1266 Arg *A = Args.getLastArg(options::OPT_mips16,
1267 options::OPT_mno_mips16);
1268 return A && A->getOption().matches(options::OPT_mips16);
1269}
1270
1271static bool isMicroMips(const ArgList &Args) {
1272 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1273 options::OPT_mno_micromips);
1274 return A && A->getOption().matches(options::OPT_mmicromips);
1275}
1276
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001277// FIXME: There is the same routine in the Tools.cpp.
1278static bool hasMipsN32ABIArg(const ArgList &Args) {
1279 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00001280 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001281}
1282
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001283static void appendMipsTargetSuffix(std::string &Path,
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001284 llvm::Triple::ArchType TargetArch,
1285 const ArgList &Args) {
1286 if (isMips16(Args))
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001287 Path += "/mips16";
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001288 else if (isMicroMips(Args))
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001289 Path += "/micromips";
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001290
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001291 if (isSoftFloatABI(Args))
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001292 Path += "/soft-float";
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001293
1294 if (TargetArch == llvm::Triple::mipsel ||
1295 TargetArch == llvm::Triple::mips64el)
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001296 Path += "/el";
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001297}
1298
1299static StringRef getMipsTargetABISuffix(llvm::Triple::ArchType TargetArch,
1300 const ArgList &Args) {
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001301 if (TargetArch == llvm::Triple::mips64 ||
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001302 TargetArch == llvm::Triple::mips64el)
1303 return hasMipsN32ABIArg(Args) ? "/n32" : "/64";
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001304
1305 return "/32";
1306}
1307
Chandler Carruthb427c562013-06-22 11:35:51 +00001308static bool findTargetBiarchSuffix(std::string &Suffix, StringRef Path,
1309 llvm::Triple::ArchType TargetArch,
1310 const ArgList &Args) {
1311 // FIXME: This routine was only intended to model bi-arch toolchains which
1312 // use -m32 and -m64 to swap between variants of a target. It shouldn't be
1313 // doing ABI-based builtin location for MIPS.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001314 if (isMipsArch(TargetArch)) {
1315 StringRef ABISuffix = getMipsTargetABISuffix(TargetArch, Args);
1316
1317 // First build and check a complex path to crtbegin.o
1318 // depends on command line options (-mips16, -msoft-float, ...)
1319 // like mips-linux-gnu/4.7/mips16/soft-float/el/crtbegin.o
1320 appendMipsTargetSuffix(Suffix, TargetArch, Args);
1321
1322 if (TargetArch == llvm::Triple::mips64 ||
1323 TargetArch == llvm::Triple::mips64el)
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001324 Suffix += ABISuffix;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001325
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001326 if (llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o"))
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001327 return true;
1328
1329 // Then fall back and probe a simple case like
1330 // mips-linux-gnu/4.7/32/crtbegin.o
1331 Suffix = ABISuffix;
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001332 return llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001333 }
1334
1335 if (TargetArch == llvm::Triple::x86_64 ||
Ulrich Weigand47445072013-05-06 16:26:41 +00001336 TargetArch == llvm::Triple::ppc64 ||
1337 TargetArch == llvm::Triple::systemz)
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001338 Suffix = "/64";
1339 else
1340 Suffix = "/32";
1341
Rafael Espindola34cfafd2013-04-30 12:24:40 +00001342 return llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001343}
1344
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001345void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001346 llvm::Triple::ArchType TargetArch, const ArgList &Args,
Chandler Carruthb427c562013-06-22 11:35:51 +00001347 const std::string &LibDir, StringRef CandidateTriple,
1348 bool NeedsBiarchSuffix) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001349 // There are various different suffixes involving the triple we
1350 // check for. We also record what is necessary to walk from each back
1351 // up to the lib directory.
Chandler Carruth866faab2012-01-25 07:21:38 +00001352 const std::string LibSuffixes[] = {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001353 "/gcc/" + CandidateTriple.str(),
1354 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1355
Hal Finkelf3587912012-09-18 22:25:07 +00001356 // The Freescale PPC SDK has the gcc libraries in
1357 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1358 "/" + CandidateTriple.str(),
1359
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001360 // Ubuntu has a strange mis-matched pair of triples that this happens to
1361 // match.
1362 // FIXME: It may be worthwhile to generalize this and look for a second
1363 // triple.
Chandler Carruth6e46ca22011-11-09 03:46:20 +00001364 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001365 };
Chandler Carruthb427c562013-06-22 11:35:51 +00001366 const std::string InstallSuffixes[] = { "/../../..", "/../../../..", "/../..",
1367 "/../../../.." };
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001368 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruthb427c562013-06-22 11:35:51 +00001369 const unsigned NumLibSuffixes =
1370 (llvm::array_lengthof(LibSuffixes) - (TargetArch != llvm::Triple::x86));
Chandler Carruth866faab2012-01-25 07:21:38 +00001371 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1372 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001373 llvm::error_code EC;
Chandler Carruth866faab2012-01-25 07:21:38 +00001374 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001375 !EC && LI != LE; LI = LI.increment(EC)) {
1376 StringRef VersionText = llvm::sys::path::filename(LI->path());
1377 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1378 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1379 if (CandidateVersion < MinVersion)
1380 continue;
1381 if (CandidateVersion <= Version)
1382 continue;
Hal Finkel221e11e2011-12-08 05:50:03 +00001383
1384 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth64cee062012-01-24 19:21:42 +00001385 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth866faab2012-01-25 07:21:38 +00001386 // libs in a subdirectory named 64. The simple logic we follow is that
1387 // *if* there is a subdirectory of the right name with crtbegin.o in it,
Chandler Carruthb427c562013-06-22 11:35:51 +00001388 // we use that. If not, and if not a biarch triple alias, we look for
Chandler Carruth866faab2012-01-25 07:21:38 +00001389 // crtbegin.o without the subdirectory.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001390
Chandler Carruthb427c562013-06-22 11:35:51 +00001391 std::string BiarchSuffix;
1392 if (findTargetBiarchSuffix(BiarchSuffix, LI->path(), TargetArch, Args)) {
1393 GCCBiarchSuffix = BiarchSuffix;
Chandler Carruth866faab2012-01-25 07:21:38 +00001394 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001395 if (NeedsBiarchSuffix ||
Chandler Carruth866faab2012-01-25 07:21:38 +00001396 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1397 continue;
Chandler Carruthb427c562013-06-22 11:35:51 +00001398 GCCBiarchSuffix.clear();
Chandler Carruth866faab2012-01-25 07:21:38 +00001399 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001400
1401 Version = CandidateVersion;
Chandler Carruth4d9d7682012-01-24 19:28:29 +00001402 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001403 // FIXME: We hack together the directory name here instead of
1404 // using LI to ensure stable path separators across Windows and
1405 // Linux.
Chandler Carruth866faab2012-01-25 07:21:38 +00001406 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth64cee062012-01-24 19:21:42 +00001407 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001408 IsValid = true;
1409 }
1410 }
1411}
1412
Rafael Espindola1af7c212012-02-19 01:38:32 +00001413Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1414 const ArgList &Args)
Rafael Espindola84b588b2013-03-18 18:10:27 +00001415 : ToolChain(D, Triple, Args), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbar88979912010-08-01 22:29:51 +00001416 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001417 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001418 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001419}
1420
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001421Generic_GCC::~Generic_GCC() {
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001422}
1423
Rafael Espindola7cf32212013-03-20 03:05:54 +00001424Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +00001425 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001426 case Action::PreprocessJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001427 if (!Preprocess)
1428 Preprocess.reset(new tools::gcc::Preprocess(*this));
1429 return Preprocess.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001430 case Action::PrecompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001431 if (!Precompile)
1432 Precompile.reset(new tools::gcc::Precompile(*this));
1433 return Precompile.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001434 case Action::CompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001435 if (!Compile)
1436 Compile.reset(new tools::gcc::Compile(*this));
1437 return Compile.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +00001438 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001439 return ToolChain::getTool(AC);
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001440 }
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001441}
1442
Rafael Espindola7cf32212013-03-20 03:05:54 +00001443Tool *Generic_GCC::buildAssembler() const {
1444 return new tools::gcc::Assemble(*this);
1445}
1446
1447Tool *Generic_GCC::buildLinker() const {
1448 return new tools::gcc::Link(*this);
1449}
1450
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001451bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001452 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001453}
1454
Chandler Carruth76a943b2012-11-19 03:52:03 +00001455bool Generic_GCC::isPICDefault() const {
1456 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001457}
1458
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001459bool Generic_GCC::isPIEDefault() const {
1460 return false;
1461}
1462
Chandler Carruth76a943b2012-11-19 03:52:03 +00001463bool Generic_GCC::isPICDefaultForced() const {
1464 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001465}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001466
Tony Linthicum76329bf2011-12-12 21:14:55 +00001467/// Hexagon Toolchain
1468
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001469std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1470
1471 // Locate the rest of the toolchain ...
1472 if (strlen(GCC_INSTALL_PREFIX))
1473 return std::string(GCC_INSTALL_PREFIX);
1474
1475 std::string InstallRelDir = InstalledDir + "/../../gnu";
1476 if (llvm::sys::fs::exists(InstallRelDir))
1477 return InstallRelDir;
1478
1479 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1480 if (llvm::sys::fs::exists(PrefixRelDir))
1481 return PrefixRelDir;
1482
1483 return InstallRelDir;
1484}
1485
Matthew Curtise689b052012-12-06 15:46:07 +00001486static void GetHexagonLibraryPaths(
1487 const ArgList &Args,
1488 const std::string Ver,
1489 const std::string MarchString,
1490 const std::string &InstalledDir,
1491 ToolChain::path_list *LibPaths)
1492{
1493 bool buildingLib = Args.hasArg(options::OPT_shared);
1494
1495 //----------------------------------------------------------------------------
1496 // -L Args
1497 //----------------------------------------------------------------------------
1498 for (arg_iterator
1499 it = Args.filtered_begin(options::OPT_L),
1500 ie = Args.filtered_end();
1501 it != ie;
1502 ++it) {
1503 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1504 LibPaths->push_back((*it)->getValue(i));
1505 }
1506
1507 //----------------------------------------------------------------------------
1508 // Other standard paths
1509 //----------------------------------------------------------------------------
1510 const std::string MarchSuffix = "/" + MarchString;
1511 const std::string G0Suffix = "/G0";
1512 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1513 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1514
1515 // lib/gcc/hexagon/...
1516 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1517 if (buildingLib) {
1518 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1519 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1520 }
1521 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1522 LibPaths->push_back(LibGCCHexagonDir + Ver);
1523
1524 // lib/gcc/...
1525 LibPaths->push_back(RootDir + "lib/gcc");
1526
1527 // hexagon/lib/...
1528 std::string HexagonLibDir = RootDir + "hexagon/lib";
1529 if (buildingLib) {
1530 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1531 LibPaths->push_back(HexagonLibDir + G0Suffix);
1532 }
1533 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1534 LibPaths->push_back(HexagonLibDir);
1535}
1536
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001537Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1538 const ArgList &Args)
1539 : Linux(D, Triple, Args) {
1540 const std::string InstalledDir(getDriver().getInstalledDir());
1541 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1542
1543 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1544 // program paths
1545 const std::string BinDir(GnuDir + "/bin");
1546 if (llvm::sys::fs::exists(BinDir))
1547 getProgramPaths().push_back(BinDir);
1548
1549 // Determine version of GCC libraries and headers to use.
1550 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1551 llvm::error_code ec;
1552 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1553 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1554 !ec && di != de; di = di.increment(ec)) {
1555 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1556 if (MaxVersion < cv)
1557 MaxVersion = cv;
1558 }
1559 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001560
1561 ToolChain::path_list *LibPaths= &getFilePaths();
1562
1563 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1564 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1565 // support 'linux' we'll need to fix this up
1566 LibPaths->clear();
1567
1568 GetHexagonLibraryPaths(
1569 Args,
1570 GetGCCLibAndIncVersion(),
1571 GetTargetCPU(Args),
1572 InstalledDir,
1573 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001574}
1575
1576Hexagon_TC::~Hexagon_TC() {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001577}
1578
Rafael Espindola7cf32212013-03-20 03:05:54 +00001579Tool *Hexagon_TC::buildAssembler() const {
1580 return new tools::hexagon::Assemble(*this);
1581}
1582
1583Tool *Hexagon_TC::buildLinker() const {
1584 return new tools::hexagon::Link(*this);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001585}
1586
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001587void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1588 ArgStringList &CC1Args) const {
1589 const Driver &D = getDriver();
1590
1591 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1592 DriverArgs.hasArg(options::OPT_nostdlibinc))
1593 return;
1594
1595 llvm::sys::Path InstallDir(D.InstalledDir);
1596 std::string Ver(GetGCCLibAndIncVersion());
1597 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1598 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1599 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1600 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1601 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001602}
1603
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001604void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1605 ArgStringList &CC1Args) const {
1606
1607 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1608 DriverArgs.hasArg(options::OPT_nostdincxx))
1609 return;
1610
1611 const Driver &D = getDriver();
1612 std::string Ver(GetGCCLibAndIncVersion());
1613 llvm::sys::Path IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
1614
1615 IncludeDir.appendComponent("hexagon/include/c++/");
1616 IncludeDir.appendComponent(Ver);
1617 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001618}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001619
Matthew Curtise689b052012-12-06 15:46:07 +00001620ToolChain::CXXStdlibType
1621Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1622 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1623 if (!A)
1624 return ToolChain::CST_Libstdcxx;
1625
1626 StringRef Value = A->getValue();
1627 if (Value != "libstdc++") {
1628 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1629 << A->getAsString(Args);
1630 }
1631
1632 return ToolChain::CST_Libstdcxx;
1633}
1634
1635static Arg *GetLastHexagonArchArg(const ArgList &Args)
Matthew Curtisf10a5952012-12-06 14:16:43 +00001636{
1637 Arg *A = NULL;
1638
1639 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1640 it != ie; ++it) {
1641 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
1642 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1643 A = *it;
1644 A->claim();
1645 } else if ((*it)->getOption().matches(options::OPT_m_Joined)) {
1646 StringRef Value = (*it)->getValue(0);
1647 if (Value.startswith("v")) {
1648 A = *it;
1649 A->claim();
1650 }
1651 }
1652 }
1653 return A;
1654}
1655
1656StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1657{
1658 // Select the default CPU (v4) if none was given or detection failed.
1659 Arg *A = GetLastHexagonArchArg (Args);
1660 if (A) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001661 StringRef WhichHexagon = A->getValue();
Matthew Curtisf10a5952012-12-06 14:16:43 +00001662 if (WhichHexagon.startswith("hexagon"))
1663 return WhichHexagon.substr(sizeof("hexagon") - 1);
1664 if (WhichHexagon != "")
1665 return WhichHexagon;
1666 }
1667
1668 return "v4";
1669}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001670// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001671
Chris Lattner09797542010-03-04 21:07:38 +00001672/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1673/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1674/// Currently does not support anything else but compilation.
1675
Rafael Espindola84b588b2013-03-18 18:10:27 +00001676TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
1677 const ArgList &Args)
1678 : ToolChain(D, Triple, Args) {
Chris Lattner09797542010-03-04 21:07:38 +00001679 // Path mangling to find libexec
1680 std::string Path(getDriver().Dir);
1681
1682 Path += "/../libexec";
1683 getProgramPaths().push_back(Path);
1684}
1685
1686TCEToolChain::~TCEToolChain() {
Chris Lattner09797542010-03-04 21:07:38 +00001687}
1688
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001689bool TCEToolChain::IsMathErrnoDefault() const {
1690 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001691}
1692
Chandler Carruth76a943b2012-11-19 03:52:03 +00001693bool TCEToolChain::isPICDefault() const {
1694 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001695}
1696
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001697bool TCEToolChain::isPIEDefault() const {
1698 return false;
1699}
1700
Chandler Carruth76a943b2012-11-19 03:52:03 +00001701bool TCEToolChain::isPICDefaultForced() const {
1702 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001703}
1704
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001705/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1706
Rafael Espindola1af7c212012-02-19 01:38:32 +00001707OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1708 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001709 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001710 getFilePaths().push_back("/usr/lib");
1711}
1712
Rafael Espindola7cf32212013-03-20 03:05:54 +00001713Tool *OpenBSD::buildAssembler() const {
1714 return new tools::openbsd::Assemble(*this);
1715}
1716
1717Tool *OpenBSD::buildLinker() const {
1718 return new tools::openbsd::Link(*this);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001719}
1720
Eli Friedman9fa28852012-08-08 23:57:20 +00001721/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1722
1723Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1724 : Generic_ELF(D, Triple, Args) {
1725 getFilePaths().push_back(getDriver().Dir + "/../lib");
1726 getFilePaths().push_back("/usr/lib");
1727}
1728
Rafael Espindola7cf32212013-03-20 03:05:54 +00001729Tool *Bitrig::buildAssembler() const {
1730 return new tools::bitrig::Assemble(*this);
1731}
1732
1733Tool *Bitrig::buildLinker() const {
1734 return new tools::bitrig::Link(*this);
Eli Friedman9fa28852012-08-08 23:57:20 +00001735}
1736
1737void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1738 ArgStringList &CC1Args) const {
1739 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1740 DriverArgs.hasArg(options::OPT_nostdincxx))
1741 return;
1742
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001743 switch (GetCXXStdlibType(DriverArgs)) {
1744 case ToolChain::CST_Libcxx:
1745 addSystemInclude(DriverArgs, CC1Args,
1746 getDriver().SysRoot + "/usr/include/c++/");
1747 break;
1748 case ToolChain::CST_Libstdcxx:
1749 addSystemInclude(DriverArgs, CC1Args,
1750 getDriver().SysRoot + "/usr/include/c++/stdc++");
1751 addSystemInclude(DriverArgs, CC1Args,
1752 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001753
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001754 StringRef Triple = getTriple().str();
1755 if (Triple.startswith("amd64"))
1756 addSystemInclude(DriverArgs, CC1Args,
1757 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1758 Triple.substr(5));
1759 else
1760 addSystemInclude(DriverArgs, CC1Args,
1761 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1762 Triple);
1763 break;
1764 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001765}
1766
1767void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1768 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001769 switch (GetCXXStdlibType(Args)) {
1770 case ToolChain::CST_Libcxx:
1771 CmdArgs.push_back("-lc++");
1772 CmdArgs.push_back("-lcxxrt");
1773 // Include supc++ to provide Unwind until provided by libcxx.
1774 CmdArgs.push_back("-lgcc");
1775 break;
1776 case ToolChain::CST_Libstdcxx:
1777 CmdArgs.push_back("-lstdc++");
1778 break;
1779 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001780}
1781
Daniel Dunbare24297c2009-03-30 21:06:03 +00001782/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1783
Rafael Espindola1af7c212012-02-19 01:38:32 +00001784FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1785 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001786
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001787 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1788 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001789 if ((Triple.getArch() == llvm::Triple::x86 ||
1790 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001791 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001792 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1793 else
1794 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001795}
1796
Rafael Espindola7cf32212013-03-20 03:05:54 +00001797Tool *FreeBSD::buildAssembler() const {
1798 return new tools::freebsd::Assemble(*this);
1799}
1800
1801Tool *FreeBSD::buildLinker() const {
1802 return new tools::freebsd::Link(*this);
Daniel Dunbare24297c2009-03-30 21:06:03 +00001803}
Daniel Dunbarcc912342009-05-02 18:28:39 +00001804
Rafael Espindola0f207ed2012-12-13 04:17:14 +00001805bool FreeBSD::UseSjLjExceptions() const {
1806 // FreeBSD uses SjLj exceptions on ARM oabi.
1807 switch (getTriple().getEnvironment()) {
1808 case llvm::Triple::GNUEABI:
1809 case llvm::Triple::EABI:
1810 return false;
1811
1812 default:
1813 return (getTriple().getArch() == llvm::Triple::arm ||
1814 getTriple().getArch() == llvm::Triple::thumb);
1815 }
1816}
1817
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001818/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1819
Rafael Espindola1af7c212012-02-19 01:38:32 +00001820NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1821 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001822
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001823 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001824 // When targeting a 32-bit platform, try the special directory used on
1825 // 64-bit hosts, and only fall back to the main library directory if that
1826 // doesn't work.
1827 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1828 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00001829 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001830 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001831
1832 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001833 }
1834}
1835
Rafael Espindola7cf32212013-03-20 03:05:54 +00001836Tool *NetBSD::buildAssembler() const {
1837 return new tools::netbsd::Assemble(*this);
1838}
1839
1840Tool *NetBSD::buildLinker() const {
1841 return new tools::netbsd::Link(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001842}
1843
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00001844ToolChain::CXXStdlibType
1845NetBSD::GetCXXStdlibType(const ArgList &Args) const {
1846 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
1847 StringRef Value = A->getValue();
1848 if (Value == "libstdc++")
1849 return ToolChain::CST_Libstdcxx;
1850 if (Value == "libc++")
1851 return ToolChain::CST_Libcxx;
1852
1853 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1854 << A->getAsString(Args);
1855 }
1856
1857 return ToolChain::CST_Libstdcxx;
1858}
1859
1860void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1861 ArgStringList &CC1Args) const {
1862 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1863 DriverArgs.hasArg(options::OPT_nostdincxx))
1864 return;
1865
1866 switch (GetCXXStdlibType(DriverArgs)) {
1867 case ToolChain::CST_Libcxx:
1868 addSystemInclude(DriverArgs, CC1Args,
1869 getDriver().SysRoot + "/usr/include/c++/");
1870 break;
1871 case ToolChain::CST_Libstdcxx:
1872 addSystemInclude(DriverArgs, CC1Args,
1873 getDriver().SysRoot + "/usr/include/g++");
1874 addSystemInclude(DriverArgs, CC1Args,
1875 getDriver().SysRoot + "/usr/include/g++/backward");
1876 break;
1877 }
1878}
1879
Chris Lattner3e2ee142010-07-07 16:01:42 +00001880/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1881
Rafael Espindola1af7c212012-02-19 01:38:32 +00001882Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1883 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00001884 getFilePaths().push_back(getDriver().Dir + "/../lib");
1885 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00001886}
1887
Rafael Espindola7cf32212013-03-20 03:05:54 +00001888Tool *Minix::buildAssembler() const {
1889 return new tools::minix::Assemble(*this);
1890}
1891
1892Tool *Minix::buildLinker() const {
1893 return new tools::minix::Link(*this);
Chris Lattner3e2ee142010-07-07 16:01:42 +00001894}
1895
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001896/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1897
Rafael Espindola1af7c212012-02-19 01:38:32 +00001898AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1899 const ArgList &Args)
1900 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001901
Daniel Dunbar88979912010-08-01 22:29:51 +00001902 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001903 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001904 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001905
Daniel Dunbar083edf72009-12-21 18:54:17 +00001906 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001907 getFilePaths().push_back("/usr/lib");
1908 getFilePaths().push_back("/usr/sfw/lib");
1909 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00001910 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001911
1912}
1913
Rafael Espindola7cf32212013-03-20 03:05:54 +00001914Tool *AuroraUX::buildAssembler() const {
1915 return new tools::auroraux::Assemble(*this);
1916}
1917
1918Tool *AuroraUX::buildLinker() const {
1919 return new tools::auroraux::Link(*this);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001920}
1921
David Chisnallf571cde2012-02-15 13:39:01 +00001922/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1923
Rafael Espindola1af7c212012-02-19 01:38:32 +00001924Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1925 const ArgList &Args)
1926 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00001927
1928 getProgramPaths().push_back(getDriver().getInstalledDir());
1929 if (getDriver().getInstalledDir() != getDriver().Dir)
1930 getProgramPaths().push_back(getDriver().Dir);
1931
1932 getFilePaths().push_back(getDriver().Dir + "/../lib");
1933 getFilePaths().push_back("/usr/lib");
1934}
1935
Rafael Espindola7cf32212013-03-20 03:05:54 +00001936Tool *Solaris::buildAssembler() const {
1937 return new tools::solaris::Assemble(*this);
1938}
1939
1940Tool *Solaris::buildLinker() const {
1941 return new tools::solaris::Link(*this);
David Chisnallf571cde2012-02-15 13:39:01 +00001942}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001943
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001944/// Distribution (very bare-bones at the moment).
Eli Friedman5cd659f2009-05-26 07:52:18 +00001945
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001946enum Distro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00001947 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001948 DebianLenny,
1949 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00001950 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00001951 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00001952 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00001953 RHEL4,
1954 RHEL5,
1955 RHEL6,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001956 Fedora13,
1957 Fedora14,
Eric Christopher534b6a02011-04-06 18:22:53 +00001958 Fedora15,
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00001959 Fedora16,
Eric Christopher534b6a02011-04-06 18:22:53 +00001960 FedoraRawhide,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001961 OpenSuse11_3,
David Chisnallb8f65e22011-05-19 13:26:33 +00001962 OpenSuse11_4,
1963 OpenSuse12_1,
Douglas Gregorf7b88412012-04-30 23:42:57 +00001964 OpenSuse12_2,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00001965 UbuntuHardy,
1966 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00001967 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00001968 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001969 UbuntuLucid,
1970 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001971 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00001972 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00001973 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00001974 UbuntuQuantal,
1975 UbuntuRaring,
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00001976 UbuntuSaucy,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001977 UnknownDistro
1978};
1979
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001980static bool IsRedhat(enum Distro Distro) {
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00001981 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1982 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001983}
1984
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001985static bool IsOpenSuse(enum Distro Distro) {
Douglas Gregorf7b88412012-04-30 23:42:57 +00001986 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001987}
1988
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001989static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00001990 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001991}
1992
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001993static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00001994 return Distro >= UbuntuHardy && Distro <= UbuntuSaucy;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001995}
1996
Thomas Schwinge6d94da02013-03-28 19:02:48 +00001997static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00001998 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00001999 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002000 StringRef Data = File.get()->getBuffer();
2001 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002002 Data.split(Lines, "\n");
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002003 Distro Version = UnknownDistro;
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002004 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2005 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002006 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002007 .Case("hardy", UbuntuHardy)
2008 .Case("intrepid", UbuntuIntrepid)
2009 .Case("jaunty", UbuntuJaunty)
2010 .Case("karmic", UbuntuKarmic)
2011 .Case("lucid", UbuntuLucid)
2012 .Case("maverick", UbuntuMaverick)
2013 .Case("natty", UbuntuNatty)
2014 .Case("oneiric", UbuntuOneiric)
2015 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002016 .Case("quantal", UbuntuQuantal)
2017 .Case("raring", UbuntuRaring)
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002018 .Case("saucy", UbuntuSaucy)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002019 .Default(UnknownDistro);
2020 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002021 }
2022
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002023 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002024 StringRef Data = File.get()->getBuffer();
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002025 if (Data.startswith("Fedora release 16"))
2026 return Fedora16;
2027 else if (Data.startswith("Fedora release 15"))
Eric Christopher534b6a02011-04-06 18:22:53 +00002028 return Fedora15;
2029 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002030 return Fedora14;
Eric Christopher534b6a02011-04-06 18:22:53 +00002031 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002032 return Fedora13;
Eric Christopher534b6a02011-04-06 18:22:53 +00002033 else if (Data.startswith("Fedora release") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002034 Data.find("Rawhide") != StringRef::npos)
Eric Christopher534b6a02011-04-06 18:22:53 +00002035 return FedoraRawhide;
Chris Lattner84e38552011-05-22 05:36:06 +00002036 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002037 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002038 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002039 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002040 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002041 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002042 return RHEL5;
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 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002046 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002047 return UnknownDistro;
2048 }
2049
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002050 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002051 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002052 if (Data[0] == '5')
2053 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002054 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002055 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002056 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002057 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002058 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2059 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002060 return UnknownDistro;
2061 }
2062
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002063 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002064 return llvm::StringSwitch<Distro>(File.get()->getBuffer())
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002065 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2066 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2067 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregorf7b88412012-04-30 23:42:57 +00002068 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002069 .Default(UnknownDistro);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002070
Rafael Espindola355670d2013-06-25 15:14:22 +00002071 if (llvm::sys::fs::exists("/etc/exherbo-release"))
Rafael Espindola12479842010-11-11 02:07:13 +00002072 return Exherbo;
2073
Rafael Espindola355670d2013-06-25 15:14:22 +00002074 if (llvm::sys::fs::exists("/etc/arch-release"))
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002075 return ArchLinux;
2076
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002077 return UnknownDistro;
2078}
2079
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002080/// \brief Get our best guess at the multiarch triple for a target.
2081///
2082/// Debian-based systems are starting to use a multiarch setup where they use
2083/// a target-triple directory in the library and header search paths.
2084/// Unfortunately, this triple does not align with the vanilla target triple,
2085/// so we provide a rough mapping here.
2086static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2087 StringRef SysRoot) {
2088 // For most architectures, just use whatever we have rather than trying to be
2089 // clever.
2090 switch (TargetTriple.getArch()) {
2091 default:
2092 return TargetTriple.str();
2093
2094 // We use the existence of '/lib/<triple>' as a directory to detect some
2095 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002096 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2097 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002098 case llvm::Triple::arm:
2099 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002100 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2101 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2102 return "arm-linux-gnueabihf";
2103 } else {
2104 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2105 return "arm-linux-gnueabi";
2106 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002107 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002108 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002109 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2110 return "i386-linux-gnu";
2111 return TargetTriple.str();
2112 case llvm::Triple::x86_64:
2113 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2114 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002115 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002116 case llvm::Triple::aarch64:
2117 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2118 return "aarch64-linux-gnu";
Tim Northoverfbb56b72013-06-13 22:54:55 +00002119 return TargetTriple.str();
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002120 case llvm::Triple::mips:
2121 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2122 return "mips-linux-gnu";
2123 return TargetTriple.str();
2124 case llvm::Triple::mipsel:
2125 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2126 return "mipsel-linux-gnu";
2127 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002128 case llvm::Triple::ppc:
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00002129 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2130 return "powerpc-linux-gnuspe";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002131 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2132 return "powerpc-linux-gnu";
2133 return TargetTriple.str();
2134 case llvm::Triple::ppc64:
2135 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2136 return "powerpc64-linux-gnu";
2137 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002138 }
2139}
2140
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002141static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2142 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2143}
2144
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002145static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2146 const ArgList &Args) {
2147 if (Arch != llvm::Triple::mips &&
2148 Arch != llvm::Triple::mipsel)
2149 return false;
2150
2151 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2152 options::OPT_mcpu_EQ,
2153 options::OPT_mips_CPUs_Group);
2154
2155 if (!A)
2156 return false;
2157
2158 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2159 return A->getOption().matches(options::OPT_mips32r2);
2160
Richard Smithbd55daf2012-11-01 04:30:05 +00002161 return A->getValue() == StringRef("mips32r2");
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002162}
2163
Simon Atanasyand4413882012-09-14 11:27:24 +00002164static StringRef getMultilibDir(const llvm::Triple &Triple,
2165 const ArgList &Args) {
2166 if (!isMipsArch(Triple.getArch()))
2167 return Triple.isArch32Bit() ? "lib32" : "lib64";
2168
2169 // lib32 directory has a special meaning on MIPS targets.
2170 // It contains N32 ABI binaries. Use this folder if produce
2171 // code for N32 ABI only.
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00002172 if (hasMipsN32ABIArg(Args))
Simon Atanasyand4413882012-09-14 11:27:24 +00002173 return "lib32";
2174
2175 return Triple.isArch32Bit() ? "lib" : "lib64";
2176}
2177
Rafael Espindola1af7c212012-02-19 01:38:32 +00002178Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2179 : Generic_ELF(D, Triple, Args) {
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002180 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002181 std::string SysRoot = computeSysRoot(Args);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002182
Chandler Carruthd6c62b62013-06-20 23:37:54 +00002183 // Cross-compiling binutils and GCC installations (vanilla and OpenSuse at
2184 // least) put various tools in a triple-prefixed directory off of the parent
2185 // of the GCC installation. We use the GCC triple here to ensure that we end
2186 // up with tools that support the same amount of cross compiling as the
2187 // detected GCC installation. For example, if we find a GCC installation
2188 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
2189 // used to target i386.
2190 // FIXME: This seems unlikely to be Linux-specific.
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002191 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002192 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002193 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002194
2195 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002196
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002197 Distro Distro = DetectDistro(Arch);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002198
Chris Lattnerd075c822011-05-22 16:45:07 +00002199 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002200 ExtraOpts.push_back("-z");
2201 ExtraOpts.push_back("relro");
2202 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002203
Douglas Gregord9bb1522011-03-06 19:11:49 +00002204 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002205 ExtraOpts.push_back("-X");
2206
Logan Chienc6fd8202012-09-02 09:30:11 +00002207 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002208 const bool IsMips = isMipsArch(Arch);
2209
2210 if (IsMips && !SysRoot.empty())
2211 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002212
Chandler Carruth0b842912011-12-09 04:45:18 +00002213 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2214 // and the MIPS ABI require .dynsym to be sorted in different ways.
2215 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2216 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002217 // Android loader does not support .gnu.hash.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002218 if (!IsMips && !IsAndroid) {
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002219 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2220 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002221 ExtraOpts.push_back("--hash-style=gnu");
2222
2223 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2224 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2225 ExtraOpts.push_back("--hash-style=both");
2226 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002227
Chris Lattner84e38552011-05-22 05:36:06 +00002228 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002229 ExtraOpts.push_back("--no-add-needed");
2230
Eli Friedmanf7600942011-06-02 21:36:53 +00002231 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002232 Distro == DebianJessie || IsOpenSuse(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002233 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002234 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002235 ExtraOpts.push_back("--build-id");
2236
Chris Lattnerd075c822011-05-22 16:45:07 +00002237 if (IsOpenSuse(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002238 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002239
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002240 // The selection of paths to try here is designed to match the patterns which
2241 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2242 // This was determined by running GCC in a fake filesystem, creating all
2243 // possible permutations of these directories, and seeing which ones it added
2244 // to the link paths.
2245 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002246
Simon Atanasyand4413882012-09-14 11:27:24 +00002247 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002248 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002249
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002250 // Add the multilib suffixed paths where they are available.
2251 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002252 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002253 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002254
2255 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2256 addPathIfExists(GCCInstallation.getInstallPath() +
Chandler Carruthb427c562013-06-22 11:35:51 +00002257 GCCInstallation.getBiarchSuffix() + "/mips-r2",
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002258 Paths);
2259 else
2260 addPathIfExists((GCCInstallation.getInstallPath() +
Chandler Carruthb427c562013-06-22 11:35:51 +00002261 GCCInstallation.getBiarchSuffix()),
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002262 Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002263
2264 // If the GCC installation we found is inside of the sysroot, we want to
2265 // prefer libraries installed in the parent prefix of the GCC installation.
2266 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002267 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002268 // This usually happens when there is an external cross compiler on the
2269 // host system, and a more minimal sysroot available that is the target of
2270 // the cross.
2271 if (StringRef(LibPath).startswith(SysRoot)) {
2272 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2273 Paths);
2274 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2275 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2276 }
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002277 // On Android, libraries in the parent prefix of the GCC installation are
2278 // preferred to the ones under sysroot.
2279 if (IsAndroid) {
2280 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2281 }
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002282 // Sourcery CodeBench MIPS toolchain holds some libraries under
2283 // the parent prefix of the GCC installation.
2284 if (IsMips) {
Rafael Espindola34cfafd2013-04-30 12:24:40 +00002285 std::string Suffix;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002286 appendMipsTargetSuffix(Suffix, Arch, Args);
2287 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" +
Rafael Espindola34cfafd2013-04-30 12:24:40 +00002288 Multilib + Suffix,
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002289 Paths);
2290 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002291 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002292 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2293 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2294 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2295 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2296
Chandler Carruthb427c562013-06-22 11:35:51 +00002297 // Try walking via the GCC triple path in case of biarch or multiarch GCC
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002298 // installations with strange symlinks.
2299 if (GCCInstallation.isValid())
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002300 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002301 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002302
Chandler Carrutha7b44142011-10-16 10:54:30 +00002303 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002304 if (GCCInstallation.isValid()) {
2305 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002306 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruthb427c562013-06-22 11:35:51 +00002307 if (!GCCInstallation.getBiarchSuffix().empty())
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002308 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002309
2310 if (StringRef(LibPath).startswith(SysRoot)) {
2311 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2312 addPathIfExists(LibPath, Paths);
2313 }
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002314 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002315 addPathIfExists(SysRoot + "/lib", Paths);
2316 addPathIfExists(SysRoot + "/usr/lib", Paths);
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002317
2318 IsPIEDefault = SanitizerArgs(*this, Args).hasZeroBaseShadow();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002319}
2320
2321bool Linux::HasNativeLLVMSupport() const {
2322 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002323}
2324
Rafael Espindola7cf32212013-03-20 03:05:54 +00002325Tool *Linux::buildLinker() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002326 return new tools::gnutools::Link(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00002327}
2328
2329Tool *Linux::buildAssembler() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002330 return new tools::gnutools::Assemble(*this);
Rafael Espindola92b00932010-08-10 00:25:48 +00002331}
2332
Chandler Carruth05fb5852012-11-21 23:40:23 +00002333void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2334 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002335 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carruth05fb5852012-11-21 23:40:23 +00002336 bool UseInitArrayDefault
2337 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002338 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002339 getTriple().getEnvironment() == llvm::Triple::Android;
2340 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2341 options::OPT_fno_use_init_array,
2342 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002343 CC1Args.push_back("-fuse-init-array");
2344}
2345
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002346std::string Linux::computeSysRoot(const ArgList &Args) const {
2347 if (!getDriver().SysRoot.empty())
2348 return getDriver().SysRoot;
2349
2350 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
2351 return std::string();
2352
Rafael Espindola34cfafd2013-04-30 12:24:40 +00002353 std::string Path =
2354 (GCCInstallation.getInstallPath() +
2355 "/../../../../" + GCCInstallation.getTriple().str() + "/libc").str();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002356 appendMipsTargetSuffix(Path, getTriple().getArch(), Args);
2357
Rafael Espindola34cfafd2013-04-30 12:24:40 +00002358 return llvm::sys::fs::exists(Path) ? Path : "";
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002359}
2360
Chandler Carrutha796f532011-11-05 20:17:13 +00002361void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2362 ArgStringList &CC1Args) const {
2363 const Driver &D = getDriver();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002364 std::string SysRoot = computeSysRoot(DriverArgs);
Chandler Carrutha796f532011-11-05 20:17:13 +00002365
2366 if (DriverArgs.hasArg(options::OPT_nostdinc))
2367 return;
2368
2369 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002370 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002371
2372 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carrutha796f532011-11-05 20:17:13 +00002373 llvm::sys::Path P(D.ResourceDir);
2374 P.appendComponent("include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002375 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002376 }
2377
2378 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2379 return;
2380
2381 // Check for configure-time C include directories.
2382 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2383 if (CIncludeDirs != "") {
2384 SmallVector<StringRef, 5> dirs;
2385 CIncludeDirs.split(dirs, ":");
2386 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2387 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002388 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? SysRoot : "";
Chandler Carrutha796f532011-11-05 20:17:13 +00002389 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2390 }
2391 return;
2392 }
2393
2394 // Lacking those, try to detect the correct set of system includes for the
2395 // target triple.
2396
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002397 // Sourcery CodeBench and modern FSF Mips toolchains put extern C
2398 // system includes under three additional directories.
2399 if (GCCInstallation.isValid() && isMipsArch(getTriple().getArch())) {
Chandler Carruthb427c562013-06-22 11:35:51 +00002400 addExternCSystemIncludeIfExists(
2401 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + "/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002402
Chandler Carruthb427c562013-06-22 11:35:51 +00002403 addExternCSystemIncludeIfExists(
2404 DriverArgs, CC1Args,
2405 GCCInstallation.getInstallPath() + "/../../../../" +
2406 GCCInstallation.getTriple().str() + "/libc/usr/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002407 }
2408
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002409 // Implement generic Debian multiarch support.
2410 const StringRef X86_64MultiarchIncludeDirs[] = {
2411 "/usr/include/x86_64-linux-gnu",
2412
2413 // FIXME: These are older forms of multiarch. It's not clear that they're
2414 // in use in any released version of Debian, so we should consider
2415 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002416 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002417 };
2418 const StringRef X86MultiarchIncludeDirs[] = {
2419 "/usr/include/i386-linux-gnu",
2420
2421 // FIXME: These are older forms of multiarch. It's not clear that they're
2422 // in use in any released version of Debian, so we should consider
2423 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002424 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002425 "/usr/include/i486-linux-gnu"
2426 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002427 const StringRef AArch64MultiarchIncludeDirs[] = {
2428 "/usr/include/aarch64-linux-gnu"
2429 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002430 const StringRef ARMMultiarchIncludeDirs[] = {
2431 "/usr/include/arm-linux-gnueabi"
2432 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002433 const StringRef ARMHFMultiarchIncludeDirs[] = {
2434 "/usr/include/arm-linux-gnueabihf"
2435 };
Eli Friedman7771c832011-11-11 03:05:19 +00002436 const StringRef MIPSMultiarchIncludeDirs[] = {
2437 "/usr/include/mips-linux-gnu"
2438 };
2439 const StringRef MIPSELMultiarchIncludeDirs[] = {
2440 "/usr/include/mipsel-linux-gnu"
2441 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002442 const StringRef PPCMultiarchIncludeDirs[] = {
2443 "/usr/include/powerpc-linux-gnu"
2444 };
2445 const StringRef PPC64MultiarchIncludeDirs[] = {
2446 "/usr/include/powerpc64-linux-gnu"
2447 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002448 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002449 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002450 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002451 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002452 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002453 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2454 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002455 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002456 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2457 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2458 else
2459 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002460 } else if (getTriple().getArch() == llvm::Triple::mips) {
2461 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2462 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2463 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002464 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2465 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2466 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2467 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002468 }
2469 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2470 E = MultiarchIncludeDirs.end();
2471 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002472 if (llvm::sys::fs::exists(SysRoot + *I)) {
2473 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + *I);
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002474 break;
2475 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002476 }
2477
2478 if (getTriple().getOS() == llvm::Triple::RTEMS)
2479 return;
2480
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002481 // Add an include of '/include' directly. This isn't provided by default by
2482 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2483 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002484 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002485
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002486 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002487}
2488
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002489/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002490/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2491 const ArgList &DriverArgs,
2492 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002493 if (!llvm::sys::fs::exists(Base))
2494 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002495 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002496 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002497 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002498 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002499}
2500
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002501/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2502/// libstdc++ installation.
2503/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2504 Twine TargetArchDir,
2505 Twine MultiLibSuffix,
2506 const ArgList &DriverArgs,
2507 ArgStringList &CC1Args) {
2508 if (!addLibStdCXXIncludePaths(Base+Suffix, TargetArchDir + MultiLibSuffix,
2509 DriverArgs, CC1Args))
2510 return false;
2511
2512 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
2513 + MultiLibSuffix);
2514 return true;
2515}
2516
Chandler Carrutha796f532011-11-05 20:17:13 +00002517void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2518 ArgStringList &CC1Args) const {
2519 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2520 DriverArgs.hasArg(options::OPT_nostdincxx))
2521 return;
2522
Chandler Carruthf4701732011-11-07 09:01:17 +00002523 // Check if libc++ has been enabled and provide its include paths if so.
2524 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2525 // libc++ is always installed at a fixed path on Linux currently.
2526 addSystemInclude(DriverArgs, CC1Args,
2527 getDriver().SysRoot + "/usr/include/c++/v1");
2528 return;
2529 }
2530
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002531 // We need a detected GCC installation on Linux to provide libstdc++'s
2532 // headers. We handled the libc++ case above.
2533 if (!GCCInstallation.isValid())
2534 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002535
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002536 // By default, look for the C++ headers in an include directory adjacent to
2537 // the lib directory of the GCC installation. Note that this is expect to be
2538 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2539 StringRef LibDir = GCCInstallation.getParentLibPath();
2540 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola66aa0452012-06-19 01:26:10 +00002541 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002542 StringRef TripleStr = GCCInstallation.getTriple().str();
2543
Chandler Carruthb427c562013-06-22 11:35:51 +00002544 if (addLibStdCXXIncludePaths(
2545 LibDir.str() + "/../include", "/c++/" + Version.str(), TripleStr,
2546 GCCInstallation.getBiarchSuffix(), DriverArgs, CC1Args))
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002547 return;
2548
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002549 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002550 // Gentoo is weird and places its headers inside the GCC install, so if the
2551 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002552 InstallDir.str() + "/include/g++-v4",
2553 // Android standalone toolchain has C++ headers in yet another place.
2554 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkelf3587912012-09-18 22:25:07 +00002555 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2556 // without a subdirectory corresponding to the gcc version.
2557 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002558 };
2559
2560 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
Chandler Carruthb427c562013-06-22 11:35:51 +00002561 if (addLibStdCXXIncludePaths(
2562 IncludePathCandidates[i],
2563 (TripleStr + GCCInstallation.getBiarchSuffix()), DriverArgs,
2564 CC1Args))
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002565 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002566 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002567}
2568
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002569bool Linux::isPIEDefault() const {
2570 return IsPIEDefault;
2571}
2572
Daniel Dunbarcc912342009-05-02 18:28:39 +00002573/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2574
Rafael Espindola1af7c212012-02-19 01:38:32 +00002575DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2576 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002577
2578 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002579 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002580 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002581 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002582
Daniel Dunbar083edf72009-12-21 18:54:17 +00002583 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002584 getFilePaths().push_back("/usr/lib");
John McCall65b8da02013-04-11 22:55:55 +00002585 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
2586 getFilePaths().push_back("/usr/lib/gcc47");
2587 else
2588 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002589}
2590
Rafael Espindola7cf32212013-03-20 03:05:54 +00002591Tool *DragonFly::buildAssembler() const {
2592 return new tools::dragonfly::Assemble(*this);
2593}
2594
2595Tool *DragonFly::buildLinker() const {
2596 return new tools::dragonfly::Link(*this);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002597}