blob: 5c0ab55a048f4771ee4d5fdc88efc8ca1e989e40 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000011#include "SanitizerArgs.h"
12#include "clang/Basic/ObjCRuntime.h"
13#include "clang/Basic/Version.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000014#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000017#include "clang/Driver/Options.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "llvm/ADT/STLExtras.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000019#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000020#include "llvm/ADT/StringExtras.h"
Bob Wilsona59956b2011-10-07 00:37:57 +000021#include "llvm/ADT/StringSwitch.h"
Reid Klecknerb1e25a12013-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 Dunbar84ec96c2009-09-09 22:33:15 +000026#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000027#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Rafael Espindolaa372f402013-06-17 17:23:47 +000030#include "llvm/Support/PathV1.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000031#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000032#include "llvm/Support/system_error.h"
NAKAMURA Takumi55426352012-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 Dunbarf36a06a2009-04-10 21:00:07 +000038#include <cstdlib> // ::getenv
39
Daniel Dunbar39176082009-03-20 00:20:03 +000040using namespace clang::driver;
41using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000042using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000043using namespace llvm::opt;
Daniel Dunbar39176082009-03-20 00:20:03 +000044
Daniel Dunbarf3955282009-09-04 18:34:51 +000045/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000046
Rafael Espindolaaf370e62013-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 Dunbar1d4612b2009-09-18 08:15:13 +000049{
Bob Wilson10853772012-01-31 21:30:03 +000050 // Compute the initial Darwin version from the triple
51 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-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 Wilson10853772012-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 Rosierc793ea42012-05-09 18:46:30 +000063
64 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000065 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000066 llvm::raw_string_ostream(iOSVersionMin)
67 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000068}
69
Daniel Dunbar41800112010-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 Dunbarb993f5d2010-09-17 00:24:52 +000080bool Darwin::HasNativeLLVMSupport() const {
81 return true;
82}
83
John McCall9f084a32011-07-06 00:26:06 +000084/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall260611a2012-06-20 06:18:46 +000085ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
Bob Wilson377e5c12012-11-09 01:59:30 +000086 if (isTargetIPhoneOS())
John McCall260611a2012-06-20 06:18:46 +000087 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson377e5c12012-11-09 01:59:30 +000088 if (isNonFragile)
89 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
90 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall9f084a32011-07-06 00:26:06 +000091}
92
John McCall13db5cf2011-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 Lattner5f9e2722011-07-23 10:55:15 +0000101static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000102 return llvm::StringSwitch<const char*>(Value)
103 .Case("armv6k", "armv6")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000104 .Case("armv6m", "armv6m")
Bob Wilsona59956b2011-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 Wilson2503ebd2013-03-04 22:37:49 +0000111 .Cases("armv7em", "armv7e-m", "armv7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000112 .Cases("armv7f", "armv7-f", "armv7f")
113 .Cases("armv7k", "armv7-k", "armv7k")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000114 .Cases("armv7m", "armv7-m", "armv7m")
Bob Wilson336bfa32012-09-29 23:52:50 +0000115 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000116 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000117}
118
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-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 Wilson2503ebd2013-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 Wilson336bfa32012-09-29 23:52:50 +0000128 .Case("cortex-a9-mp", "armv7f")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000129 .Case("cortex-m3", "armv7m")
130 .Case("cortex-m4", "armv7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000131 .Case("swift", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000132 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000133}
134
Chris Lattner5f9e2722011-07-23 10:55:15 +0000135StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000136 switch (getTriple().getArch()) {
137 default:
138 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000139
Douglas Gregorf0594d82011-03-06 19:11:49 +0000140 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000141 case llvm::Triple::arm: {
142 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000143 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000144 return Arch;
145
146 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000147 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000148 return Arch;
149
150 return "arm";
151 }
152 }
153}
154
Daniel Dunbarf3955282009-09-04 18:34:51 +0000155Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000156}
157
Chad Rosier61ab80a2011-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 Dunbar00577ad2010-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 Takumi304ed3f2011-06-03 03:49:51 +0000166
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000167 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000168 Str += isTargetIPhoneOS() ? "ios" : "macosx";
169 Str += getTargetVersion().getAsString();
170 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000171
172 return Triple.getTriple();
173}
174
David Blaikie99ba9e32011-12-20 02:48:34 +0000175void Generic_ELF::anchor() {}
176
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000177Tool *Darwin::getTool(Action::ActionClass AC) const {
Rafael Espindola5b222052013-03-18 20:48:54 +0000178 switch (AC) {
Rafael Espindolaf3260562013-03-18 18:50:01 +0000179 case Action::LipoJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000180 if (!Lipo)
181 Lipo.reset(new tools::darwin::Lipo(*this));
182 return Lipo.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +0000183 case Action::DsymutilJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000184 if (!Dsymutil)
185 Dsymutil.reset(new tools::darwin::Dsymutil(*this));
186 return Dsymutil.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +0000187 case Action::VerifyJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000188 if (!VerifyDebug)
189 VerifyDebug.reset(new tools::darwin::VerifyDebug(*this));
190 return VerifyDebug.get();
Rafael Espindolac0a55d12013-03-19 00:36:57 +0000191 default:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000192 return ToolChain::getTool(AC);
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000193 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000194}
195
Rafael Espindolaf48b93c2013-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 Dunbar6cd41542009-09-18 08:15:03 +0000203
Rafael Espindolaaf370e62013-03-18 18:10:27 +0000204DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple,
205 const ArgList &Args)
206 : Darwin(D, Triple, Args)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000207{
Daniel Dunbar0e50ee42010-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 Dunbar1d4612b2009-09-18 08:15:13 +0000212 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000213 getProgramPaths().push_back(getDriver().getInstalledDir());
214 if (getDriver().getInstalledDir() != getDriver().Dir)
215 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000216}
217
John McCallf85e1932011-06-15 23:02:42 +0000218void DarwinClang::AddLinkARCArgs(const ArgList &Args,
219 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000220
221 CmdArgs.push_back("-force_load");
John McCallf85e1932011-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 Kyrtzidisc19981c2011-10-18 17:40:15 +0000230 if (isTargetIOSSimulator())
231 s += "iphonesimulator";
232 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000233 s += "iphoneos";
John McCallf85e1932011-06-15 23:02:42 +0000234 else
235 s += "macosx";
236 s += ".a";
237
238 CmdArgs.push_back(Args.MakeArgString(s));
239}
240
Eric Christopher3404fe72011-06-22 17:41:40 +0000241void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000242 ArgStringList &CmdArgs,
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000243 const char *DarwinStaticLib,
244 bool AlwaysLink) const {
Eric Christopher3404fe72011-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 Christopherf8571862011-08-23 17:56:55 +0000249
Eric Christopher3404fe72011-06-22 17:41:40 +0000250 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000251 // not have compiler-rt checked out or integrated into their build (unless
252 // we explicitly force linking with this library).
Eric Christopher3404fe72011-06-22 17:41:40 +0000253 bool Exists;
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000254 if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
Eric Christopher3404fe72011-06-22 17:41:40 +0000255 CmdArgs.push_back(Args.MakeArgString(P.str()));
256}
257
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000258void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
259 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000260 // Darwin only supports the compiler-rt based runtime libraries.
261 switch (GetRuntimeLibType(Args)) {
262 case ToolChain::RLT_CompilerRT:
263 break;
264 default:
265 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000266 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000267 return;
268 }
269
Daniel Dunbareec99102010-01-22 03:38:14 +0000270 // Darwin doesn't support real static executables, don't link any runtime
271 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000272 if (Args.hasArg(options::OPT_static) ||
273 Args.hasArg(options::OPT_fapple_kext) ||
274 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000275 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000276
277 // Reject -static-libgcc for now, we can deal with this when and if someone
278 // cares. This is useful in situations where someone wants to statically link
279 // something like libstdc++, and needs its runtime support routines.
280 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000281 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000282 << A->getAsString(Args);
283 return;
284 }
285
Daniel Dunbarf4714872011-11-17 00:36:57 +0000286 // If we are building profile support, link that library in.
287 if (Args.hasArg(options::OPT_fprofile_arcs) ||
288 Args.hasArg(options::OPT_fprofile_generate) ||
289 Args.hasArg(options::OPT_fcreate_profile) ||
290 Args.hasArg(options::OPT_coverage)) {
291 // Select the appropriate runtime library for the target.
292 if (isTargetIPhoneOS()) {
293 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
294 } else {
295 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
296 }
297 }
298
Peter Collingbourne52ca70d2013-04-09 04:35:11 +0000299 SanitizerArgs Sanitize(*this, Args);
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000300
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000301 // Add Ubsan runtime library, if required.
302 if (Sanitize.needsUbsanRt()) {
Alexey Samsonov2cb3d302013-01-21 08:45:02 +0000303 if (isTargetIPhoneOS()) {
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000304 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
305 << "-fsanitize=undefined";
306 } else {
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000307 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000308
309 // The Ubsan runtime library requires C++.
310 AddCXXStdlibLibArgs(Args, CmdArgs);
311 }
312 }
313
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000314 // Add ASAN runtime library, if required. Dynamic libraries and bundles
315 // should not be linked with the runtime library.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000316 if (Sanitize.needsAsanRt()) {
Alexander Potapenko3656c612013-03-21 10:49:06 +0000317 if (isTargetIPhoneOS() && !isTargetIOSSimulator()) {
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000318 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000319 << "-fsanitize=address";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000320 } else {
Alexander Potapenko3656c612013-03-21 10:49:06 +0000321 if (Args.hasArg(options::OPT_dynamiclib) ||
322 Args.hasArg(options::OPT_bundle)) {
323 // Assume the binary will provide the ASan runtime.
324 } else {
325 AddLinkRuntimeLib(Args, CmdArgs,
326 "libclang_rt.asan_osx_dynamic.dylib", true);
327 // The ASAN runtime library requires C++.
328 AddCXXStdlibLibArgs(Args, CmdArgs);
329 }
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000330 }
331 }
332
Daniel Dunbareec99102010-01-22 03:38:14 +0000333 // Otherwise link libSystem, then the dynamic runtime library, and finally any
334 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000335 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000336
337 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000338 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000339 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
340 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000341 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
342 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
343 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000344
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000345 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000346 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000347 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000348 // The dynamic runtime library was merged with libSystem for 10.6 and
349 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000350 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000351 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000352 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000353 CmdArgs.push_back("-lgcc_s.10.5");
354
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000355 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000356 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000357 // omitted from 10.4.dylib.
358 //
359 // Unfortunately, that turned out to not be true, because Darwin system
360 // headers can still use eprintf on i386, and it is not exported from
361 // libSystem. Therefore, we still must provide a runtime library just for
362 // the tiny tiny handful of projects that *might* use that symbol.
363 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000364 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000365 } else {
366 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000367 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
368 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000369 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000370 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000371}
372
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000373void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000374 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000375
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000376 // Support allowing the SDKROOT environment variable used by xcrun and other
377 // Xcode tools to define the default sysroot, by making it the default for
378 // isysroot.
Chad Rosierd7dfd982012-12-19 23:41:50 +0000379 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
380 // Warn if the path does not exist.
381 bool Exists;
382 if (llvm::sys::fs::exists(A->getValue(), Exists) || !Exists)
383 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
384 } else {
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000385 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbar32142542013-01-15 20:33:56 +0000386 // We only use this value as the default if it is an absolute path,
387 // exists, and it is not the root path.
388 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
389 StringRef(env) != "/") {
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000390 Args.append(Args.MakeSeparateArg(
391 0, Opts.getOption(options::OPT_isysroot), env));
392 }
393 }
394 }
395
Daniel Dunbar26031372010-01-27 00:56:25 +0000396 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000397 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
398 Arg *iOSSimVersion = Args.getLastArg(
399 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000400
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000401 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000402 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000403 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000404 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
405 iOSVersion = iOSSimVersion = 0;
406 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000407 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000408 << iOSVersion->getAsString(Args)
409 << iOSSimVersion->getAsString(Args);
410 iOSSimVersion = 0;
411 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000412 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000413 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000414 StringRef OSXTarget;
415 StringRef iOSTarget;
416 StringRef iOSSimTarget;
417 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
418 OSXTarget = env;
419 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
420 iOSTarget = env;
421 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
422 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000423
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000424 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000425 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000426 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000427 if (iOSTarget.empty()) {
428 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
429 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000430 StringRef isysroot = A->getValue();
Chad Rosiera4884972011-08-31 20:56:25 +0000431 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
432 if (second != "")
433 iOSTarget = second.substr(0,3);
434 }
435 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000436
Chad Rosier4f8de272011-09-28 00:46:32 +0000437 // If no OSX or iOS target has been specified and we're compiling for armv7,
438 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000439 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000440 (getDarwinArchName(Args) == "armv7" ||
441 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000442 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000443
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000444 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000445 //
446 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000447
448 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000449 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000450 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000451 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000452 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000453 "IPHONEOS_DEPLOYMENT_TARGET");
454 }
455
456 // Allow conflicts among OSX and iOS for historical reasons, but choose the
457 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000458 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000459 if (getTriple().getArch() == llvm::Triple::arm ||
460 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000461 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000462 else
Chad Rosiera4884972011-08-31 20:56:25 +0000463 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000464 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000465
Chad Rosiera4884972011-08-31 20:56:25 +0000466 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000467 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000468 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
469 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000470 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000471 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000472 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
473 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000474 } else if (!iOSSimTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000475 const Option O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000476 options::OPT_mios_simulator_version_min_EQ);
477 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
478 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000479 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000480 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000481 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000482 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
483 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000484 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000487 // Reject invalid architecture combinations.
488 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
489 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000490 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000491 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
492 }
493
Daniel Dunbar26031372010-01-27 00:56:25 +0000494 // Set the tool chain target information.
495 unsigned Major, Minor, Micro;
496 bool HadExtra;
497 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000498 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000499 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000500 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000501 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000502 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000503 << OSXVersion->getAsString(Args);
504 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000505 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
506 assert(Version && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000507 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000508 Micro, HadExtra) || HadExtra ||
509 Major >= 10 || Minor >= 100 || Micro >= 100)
510 getDriver().Diag(diag::err_drv_invalid_version_number)
511 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000512 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000513
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000514 bool IsIOSSim = bool(iOSSimVersion);
515
516 // In GCC, the simulator historically was treated as being OS X in some
517 // contexts, like determining the link logic, despite generally being called
518 // with an iOS deployment target. For compatibility, we detect the
519 // simulator as iOS + x86, and treat it differently in a few contexts.
520 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
521 getTriple().getArch() == llvm::Triple::x86_64))
522 IsIOSSim = true;
523
524 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000525}
526
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000527void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000528 ArgStringList &CmdArgs) const {
529 CXXStdlibType Type = GetCXXStdlibType(Args);
530
531 switch (Type) {
532 case ToolChain::CST_Libcxx:
533 CmdArgs.push_back("-lc++");
534 break;
535
536 case ToolChain::CST_Libstdcxx: {
537 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
538 // it was previously found in the gcc lib dir. However, for all the Darwin
539 // platforms we care about it was -lstdc++.6, so we search for that
540 // explicitly if we can't see an obvious -lstdc++ candidate.
541
542 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000543 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000544 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000545 llvm::sys::Path P(A->getValue());
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000546 P.appendComponent("usr");
547 P.appendComponent("lib");
548 P.appendComponent("libstdc++.dylib");
549
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000550 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000551 P.eraseComponent();
552 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000553 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000554 CmdArgs.push_back(Args.MakeArgString(P.str()));
555 return;
556 }
557 }
558 }
559
560 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000561 // FIXME: This should be removed someday when we don't have to care about
562 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000563 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
564 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000565 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
566 return;
567 }
568
569 // Otherwise, let the linker search.
570 CmdArgs.push_back("-lstdc++");
571 break;
572 }
573 }
574}
575
Shantonu Sen7433fed2010-09-17 18:39:08 +0000576void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
577 ArgStringList &CmdArgs) const {
578
579 // For Darwin platforms, use the compiler-rt-based support library
580 // instead of the gcc-provided one (which is also incidentally
581 // only present in the gcc lib dir, which makes it hard to find).
582
583 llvm::sys::Path P(getDriver().ResourceDir);
584 P.appendComponent("lib");
585 P.appendComponent("darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000586
587 // Use the newer cc_kext for iOS ARM after 6.0.
588 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
589 !isIPhoneOSVersionLT(6, 0)) {
590 P.appendComponent("libclang_rt.cc_kext.a");
591 } else {
592 P.appendComponent("libclang_rt.cc_kext_ios5.a");
593 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000594
Shantonu Sen7433fed2010-09-17 18:39:08 +0000595 // For now, allow missing resource libraries to support developers who may
596 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000597 bool Exists;
598 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000599 CmdArgs.push_back(Args.MakeArgString(P.str()));
600}
601
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000602DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
603 const char *BoundArch) const {
604 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
605 const OptTable &Opts = getDriver().getOpts();
606
607 // FIXME: We really want to get out of the tool chain level argument
608 // translation business, as it makes the driver functionality much
609 // more opaque. For now, we follow gcc closely solely for the
610 // purpose of easily achieving feature parity & testability. Once we
611 // have something that works, we should reevaluate each translation
612 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000613
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000614 for (ArgList::const_iterator it = Args.begin(),
615 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000616 Arg *A = *it;
617
618 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000619 // Skip this argument unless the architecture matches either the toolchain
620 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000621 llvm::Triple::ArchType XarchArch =
Richard Smith1d489cf2012-11-01 04:30:05 +0000622 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000623 if (!(XarchArch == getArch() ||
624 (BoundArch && XarchArch ==
Rafael Espindolacfed8282012-10-31 18:51:07 +0000625 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000626 continue;
627
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000628 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000629 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000630 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000631 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000633 // If the argument parsing failed or more than one argument was
634 // consumed, the -Xarch_ argument's parameter tried to consume
635 // extra arguments. Emit an error and ignore.
636 //
637 // We also want to disallow any options which would alter the
638 // driver behavior; that isn't going to work in our model. We
639 // use isDriverOption() as an approximation, although things
640 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000641 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000642 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000643 << A->getAsString(Args);
644 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000645 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000646 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000647 << A->getAsString(Args);
648 continue;
649 }
650
Daniel Dunbar478edc22009-03-29 22:29:05 +0000651 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000652 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000653
654 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000655
656 // Linker input arguments require custom handling. The problem is that we
657 // have already constructed the phase actions, so we can not treat them as
658 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000659 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000660 // Convert the argument into individual Zlinker_input_args.
661 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
662 DAL->AddSeparateArg(OriginalArg,
663 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000664 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000665
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000666 }
667 continue;
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000670
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000671 // Sob. These is strictly gcc compatible for the time being. Apple
672 // gcc translates options twice, which means that self-expanding
673 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000674 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000675 default:
676 DAL->append(A);
677 break;
678
679 case options::OPT_mkernel:
680 case options::OPT_fapple_kext:
681 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000682 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000683 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000685 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000686 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000687 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000688 break;
689
690 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000691 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
692 DAL->AddFlagArg(A,
693 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000694 break;
695
696 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000697 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
698 DAL->AddFlagArg(A,
699 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000700 break;
701
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000702 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000703 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000704 break;
705
706 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000707 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000708 break;
709
710 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000711 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000712 break;
713
714 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000715 DAL->AddFlagArg(A,
716 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000717 break;
718
719 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000720 DAL->AddFlagArg(A,
721 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000722 break;
723
724 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000725 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000726 break;
727
728 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000729 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000730 break;
731 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000732 }
733
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000734 if (getTriple().getArch() == llvm::Triple::x86 ||
735 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000736 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000737 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000738
739 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000740 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000741 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000742 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000743 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
744 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000745
746 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
747 // which defines the list of which architectures we accept.
748 if (Name == "ppc")
749 ;
750 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000751 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000752 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000753 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000754 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000755 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000756 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000757 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000758 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000759 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000760 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000761 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000762 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000763 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000764 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000765 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000766
767 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000768 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000769
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000770 else if (Name == "i386")
771 ;
772 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000773 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000774 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000775 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000776 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000777 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000778 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000779 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000780 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000781 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000782 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000783 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000784 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000785 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000786
787 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000788 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000789
790 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000791 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000792 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000793 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000794 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000795 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000796 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000797 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000798 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000799 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000800 else if (Name == "armv6m")
801 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000802 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000803 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000804 else if (Name == "armv7em")
805 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilson336bfa32012-09-29 23:52:50 +0000806 else if (Name == "armv7f")
807 DAL->AddJoinedArg(0, MArch, "armv7f");
808 else if (Name == "armv7k")
809 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000810 else if (Name == "armv7m")
811 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilson336bfa32012-09-29 23:52:50 +0000812 else if (Name == "armv7s")
813 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000814
815 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000816 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000817 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000818
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000819 // Add an explicit version min argument for the deployment target. We do this
820 // after argument translation because -Xarch_ arguments may add a version min
821 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000822 if (BoundArch)
823 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000824
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000825 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
826 // FIXME: It would be far better to avoid inserting those -static arguments,
827 // but we can't check the deployment target in the translation code until
828 // it is set here.
829 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
830 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
831 Arg *A = *it;
832 ++it;
833 if (A->getOption().getID() != options::OPT_mkernel &&
834 A->getOption().getID() != options::OPT_fapple_kext)
835 continue;
836 assert(it != ie && "unexpected argument translation");
837 A = *it;
838 assert(A->getOption().getID() == options::OPT_static &&
839 "missing expected -static argument");
840 it = DAL->getArgs().erase(it);
841 }
842 }
843
Bob Wilson163b1512011-10-07 17:54:41 +0000844 // Validate the C++ standard library choice.
845 CXXStdlibType Type = GetCXXStdlibType(*DAL);
846 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000847 // Check whether the target provides libc++.
848 StringRef where;
849
850 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson377e5c12012-11-09 01:59:30 +0000851 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
852 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000853
854 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000855 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000856 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000857 }
858 }
859
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000860 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000861}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000862
Daniel Dunbarf3955282009-09-04 18:34:51 +0000863bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000864 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000865}
866
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000867bool Darwin::UseDwarfDebugFlags() const {
868 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
869 return S[0] != '\0';
870 return false;
871}
872
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000873bool Darwin::UseSjLjExceptions() const {
874 // Darwin uses SjLj exceptions on ARM.
875 return (getTriple().getArch() == llvm::Triple::arm ||
876 getTriple().getArch() == llvm::Triple::thumb);
877}
878
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000879bool Darwin::isPICDefault() const {
880 return true;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000881}
882
Peter Collingbourne52ca70d2013-04-09 04:35:11 +0000883bool Darwin::isPIEDefault() const {
884 return false;
885}
886
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000887bool Darwin::isPICDefaultForced() const {
888 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000889}
890
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000891bool Darwin::SupportsProfiling() const {
892 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000893 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000894}
895
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000896bool Darwin::SupportsObjCGC() const {
897 // Garbage collection is supported everywhere except on iPhone OS.
898 return !isTargetIPhoneOS();
899}
900
John McCall0a7dd782012-08-21 02:47:43 +0000901void Darwin::CheckObjCARC() const {
902 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
903 return;
John McCall80fd37a2012-08-27 01:56:21 +0000904 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000905}
906
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000907std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000908Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
909 types::ID InputType) const {
910 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000911}
912
Daniel Dunbar39176082009-03-20 00:20:03 +0000913/// Generic_GCC - A tool chain using the 'gcc' command to perform
914/// all subcommands; this relies on gcc translating the majority of
915/// command line options.
916
Chandler Carruth19347ed2011-11-06 23:39:34 +0000917/// \brief Parse a GCCVersion object out of a string of text.
918///
919/// This is the primary means of forming GCCVersion objects.
920/*static*/
921Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
922 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
923 std::pair<StringRef, StringRef> First = VersionText.split('.');
924 std::pair<StringRef, StringRef> Second = First.second.split('.');
925
926 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
927 if (First.first.getAsInteger(10, GoodVersion.Major) ||
928 GoodVersion.Major < 0)
929 return BadVersion;
930 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
931 GoodVersion.Minor < 0)
932 return BadVersion;
933
934 // First look for a number prefix and parse that if present. Otherwise just
935 // stash the entire patch string in the suffix, and leave the number
936 // unspecified. This covers versions strings such as:
937 // 4.4
938 // 4.4.0
939 // 4.4.x
940 // 4.4.2-rc4
941 // 4.4.x-patched
942 // And retains any patch number it finds.
943 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
944 if (!PatchText.empty()) {
Will Dietzca1ad502013-01-10 22:20:02 +0000945 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth19347ed2011-11-06 23:39:34 +0000946 // Try to parse the number and any suffix.
947 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
948 GoodVersion.Patch < 0)
949 return BadVersion;
950 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
951 }
952 }
953
954 return GoodVersion;
955}
956
957/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
958bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
Chandler Carruthf1757652012-12-29 12:01:08 +0000959 if (Major != RHS.Major)
960 return Major < RHS.Major;
961 if (Minor != RHS.Minor)
962 return Minor < RHS.Minor;
963 if (Patch != RHS.Patch) {
964 // Note that versions without a specified patch sort higher than those with
965 // a patch.
966 if (RHS.Patch == -1)
967 return true;
968 if (Patch == -1)
969 return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000970
Chandler Carruthf1757652012-12-29 12:01:08 +0000971 // Otherwise just sort on the patch itself.
972 return Patch < RHS.Patch;
973 }
974 if (PatchSuffix != RHS.PatchSuffix) {
975 // Sort empty suffixes higher.
976 if (RHS.PatchSuffix.empty())
977 return true;
978 if (PatchSuffix.empty())
Chandler Carruth5ba0c8e2012-12-29 13:00:47 +0000979 return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000980
Chandler Carruthf1757652012-12-29 12:01:08 +0000981 // Provide a lexicographic sort to make this a total ordering.
982 return PatchSuffix < RHS.PatchSuffix;
983 }
984
985 // The versions are equal.
Chandler Carruth19347ed2011-11-06 23:39:34 +0000986 return false;
987}
988
Rafael Espindola0e659592012-02-19 01:38:32 +0000989static StringRef getGCCToolchainDir(const ArgList &Args) {
990 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
991 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +0000992 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +0000993 return GCC_INSTALL_PREFIX;
994}
995
Chandler Carruth19347ed2011-11-06 23:39:34 +0000996/// \brief Construct a GCCInstallationDetector from the driver.
997///
998/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +0000999/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001000///
1001/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1002/// should instead pull the target out of the driver. This is currently
1003/// necessary because the driver doesn't store the final version of the target
1004/// triple.
1005Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1006 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001007 const llvm::Triple &TargetTriple,
1008 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001009 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001010 llvm::Triple MultiarchTriple
1011 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1012 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001013 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001014 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001015 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001016 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001017 SmallVector<StringRef, 10> CandidateTripleAliases;
1018 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1019 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1020 CandidateTripleAliases,
1021 CandidateMultiarchLibDirs,
1022 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001023
1024 // Compute the set of prefixes for our search.
1025 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1026 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001027
Rafael Espindola0e659592012-02-19 01:38:32 +00001028 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1029 if (GCCToolchainDir != "") {
1030 if (GCCToolchainDir.back() == '/')
1031 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001032
Rafael Espindola0e659592012-02-19 01:38:32 +00001033 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001034 } else {
1035 Prefixes.push_back(D.SysRoot);
1036 Prefixes.push_back(D.SysRoot + "/usr");
1037 Prefixes.push_back(D.InstalledDir + "/..");
1038 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001039
1040 // Loop over the various components which exist and select the best GCC
1041 // installation available. GCC installs are ranked by version number.
1042 Version = GCCVersion::Parse("0.0.0");
1043 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1044 if (!llvm::sys::fs::exists(Prefixes[i]))
1045 continue;
1046 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1047 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1048 if (!llvm::sys::fs::exists(LibDir))
1049 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001050 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001051 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1052 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001053 }
1054 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1055 const std::string LibDir
1056 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1057 if (!llvm::sys::fs::exists(LibDir))
1058 continue;
1059 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1060 ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001061 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001062 CandidateMultiarchTripleAliases[k],
1063 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001064 }
1065 }
1066}
1067
1068/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001069 const llvm::Triple &TargetTriple,
1070 const llvm::Triple &MultiarchTriple,
1071 SmallVectorImpl<StringRef> &LibDirs,
1072 SmallVectorImpl<StringRef> &TripleAliases,
1073 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1074 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1075 // Declare a bunch of static data sets that we'll select between below. These
1076 // are specifically designed to always refer to string literals to avoid any
1077 // lifetime or initialization issues.
Tim Northoverc264e162013-01-31 12:13:10 +00001078 static const char *const AArch64LibDirs[] = { "/lib" };
1079 static const char *const AArch64Triples[] = {
1080 "aarch64-none-linux-gnu",
1081 "aarch64-linux-gnu"
1082 };
1083
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001084 static const char *const ARMLibDirs[] = { "/lib" };
1085 static const char *const ARMTriples[] = {
1086 "arm-linux-gnueabi",
1087 "arm-linux-androideabi"
1088 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001089 static const char *const ARMHFTriples[] = {
1090 "arm-linux-gnueabihf",
Rafael Espindolad44d04f2013-04-14 10:14:21 +00001091 "armv7hl-redhat-linux-gnueabi"
Jiangning Liuff104a12012-07-31 08:06:29 +00001092 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001093
1094 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1095 static const char *const X86_64Triples[] = {
1096 "x86_64-linux-gnu",
1097 "x86_64-unknown-linux-gnu",
1098 "x86_64-pc-linux-gnu",
1099 "x86_64-redhat-linux6E",
1100 "x86_64-redhat-linux",
1101 "x86_64-suse-linux",
1102 "x86_64-manbo-linux-gnu",
1103 "x86_64-linux-gnu",
1104 "x86_64-slackware-linux"
1105 };
1106 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1107 static const char *const X86Triples[] = {
1108 "i686-linux-gnu",
1109 "i686-pc-linux-gnu",
1110 "i486-linux-gnu",
1111 "i386-linux-gnu",
NAKAMURA Takumi2a907f82012-12-07 17:13:18 +00001112 "i386-redhat-linux6E",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001113 "i686-redhat-linux",
1114 "i586-redhat-linux",
1115 "i386-redhat-linux",
1116 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001117 "i486-slackware-linux",
1118 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001119 };
1120
1121 static const char *const MIPSLibDirs[] = { "/lib" };
1122 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1123 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001124 static const char *const MIPSELTriples[] = {
1125 "mipsel-linux-gnu",
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001126 "mipsel-linux-android",
1127 "mips-linux-gnu"
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001128 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001129
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001130 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1131 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1132 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1133 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1134
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001135 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1136 static const char *const PPCTriples[] = {
1137 "powerpc-linux-gnu",
1138 "powerpc-unknown-linux-gnu",
Sylvestre Ledrub7e86be2013-03-15 16:22:43 +00001139 "powerpc-linux-gnuspe",
Gabor Greif91720912012-05-15 11:21:03 +00001140 "powerpc-suse-linux",
1141 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001142 };
1143 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1144 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001145 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001146 "powerpc64-unknown-linux-gnu",
1147 "powerpc64-suse-linux",
1148 "ppc64-redhat-linux"
1149 };
1150
Ulrich Weigandb8409212013-05-06 16:26:41 +00001151 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1152 static const char *const SystemZTriples[] = {
1153 "s390x-linux-gnu",
1154 "s390x-unknown-linux-gnu",
1155 "s390x-ibm-linux-gnu",
1156 "s390x-suse-linux",
1157 "s390x-redhat-linux"
1158 };
1159
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001160 switch (TargetTriple.getArch()) {
Tim Northoverc264e162013-01-31 12:13:10 +00001161 case llvm::Triple::aarch64:
Eric Christopher4f4e2af2013-02-05 07:29:49 +00001162 LibDirs.append(AArch64LibDirs, AArch64LibDirs
Tim Northoverc264e162013-01-31 12:13:10 +00001163 + llvm::array_lengthof(AArch64LibDirs));
1164 TripleAliases.append(
1165 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1166 MultiarchLibDirs.append(
1167 AArch64LibDirs, AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1168 MultiarchTripleAliases.append(
1169 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1170 break;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001171 case llvm::Triple::arm:
1172 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001173 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001174 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1175 TripleAliases.append(
1176 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1177 } else {
1178 TripleAliases.append(
1179 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1180 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001181 break;
1182 case llvm::Triple::x86_64:
1183 LibDirs.append(
1184 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1185 TripleAliases.append(
1186 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1187 MultiarchLibDirs.append(
1188 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1189 MultiarchTripleAliases.append(
1190 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1191 break;
1192 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001193 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001194 TripleAliases.append(
1195 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1196 MultiarchLibDirs.append(
1197 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1198 MultiarchTripleAliases.append(
1199 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1200 break;
1201 case llvm::Triple::mips:
1202 LibDirs.append(
1203 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1204 TripleAliases.append(
1205 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001206 MultiarchLibDirs.append(
1207 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1208 MultiarchTripleAliases.append(
1209 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001210 break;
1211 case llvm::Triple::mipsel:
1212 LibDirs.append(
1213 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1214 TripleAliases.append(
1215 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001216 MultiarchLibDirs.append(
1217 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1218 MultiarchTripleAliases.append(
1219 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1220 break;
1221 case llvm::Triple::mips64:
1222 LibDirs.append(
1223 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1224 TripleAliases.append(
1225 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1226 MultiarchLibDirs.append(
1227 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1228 MultiarchTripleAliases.append(
1229 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1230 break;
1231 case llvm::Triple::mips64el:
1232 LibDirs.append(
1233 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1234 TripleAliases.append(
1235 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1236 MultiarchLibDirs.append(
1237 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1238 MultiarchTripleAliases.append(
1239 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001240 break;
1241 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001242 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001243 TripleAliases.append(
1244 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1245 MultiarchLibDirs.append(
1246 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1247 MultiarchTripleAliases.append(
1248 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1249 break;
1250 case llvm::Triple::ppc64:
1251 LibDirs.append(
1252 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1253 TripleAliases.append(
1254 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1255 MultiarchLibDirs.append(
1256 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1257 MultiarchTripleAliases.append(
1258 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1259 break;
Ulrich Weigandb8409212013-05-06 16:26:41 +00001260 case llvm::Triple::systemz:
1261 LibDirs.append(
1262 SystemZLibDirs, SystemZLibDirs + llvm::array_lengthof(SystemZLibDirs));
1263 TripleAliases.append(
1264 SystemZTriples, SystemZTriples + llvm::array_lengthof(SystemZTriples));
1265 break;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001266
1267 default:
1268 // By default, just rely on the standard lib directories and the original
1269 // triple.
1270 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001271 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001272
1273 // Always append the drivers target triple to the end, in case it doesn't
1274 // match any of our aliases.
1275 TripleAliases.push_back(TargetTriple.str());
1276
1277 // Also include the multiarch variant if it's different.
1278 if (TargetTriple.str() != MultiarchTriple.str())
1279 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001280}
1281
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001282static bool isSoftFloatABI(const ArgList &Args) {
1283 Arg *A = Args.getLastArg(options::OPT_msoft_float,
1284 options::OPT_mhard_float,
1285 options::OPT_mfloat_abi_EQ);
1286 if (!A) return false;
1287
1288 return A->getOption().matches(options::OPT_msoft_float) ||
1289 (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
1290 A->getValue() == StringRef("soft"));
1291}
1292
1293static bool isMipsArch(llvm::Triple::ArchType Arch) {
1294 return Arch == llvm::Triple::mips ||
1295 Arch == llvm::Triple::mipsel ||
1296 Arch == llvm::Triple::mips64 ||
1297 Arch == llvm::Triple::mips64el;
1298}
1299
1300static bool isMips16(const ArgList &Args) {
1301 Arg *A = Args.getLastArg(options::OPT_mips16,
1302 options::OPT_mno_mips16);
1303 return A && A->getOption().matches(options::OPT_mips16);
1304}
1305
1306static bool isMicroMips(const ArgList &Args) {
1307 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1308 options::OPT_mno_micromips);
1309 return A && A->getOption().matches(options::OPT_mmicromips);
1310}
1311
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001312// FIXME: There is the same routine in the Tools.cpp.
1313static bool hasMipsN32ABIArg(const ArgList &Args) {
1314 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00001315 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001316}
1317
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001318static void appendMipsTargetSuffix(std::string &Path,
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001319 llvm::Triple::ArchType TargetArch,
1320 const ArgList &Args) {
1321 if (isMips16(Args))
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001322 Path += "/mips16";
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001323 else if (isMicroMips(Args))
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001324 Path += "/micromips";
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001325
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001326 if (isSoftFloatABI(Args))
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001327 Path += "/soft-float";
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001328
1329 if (TargetArch == llvm::Triple::mipsel ||
1330 TargetArch == llvm::Triple::mips64el)
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001331 Path += "/el";
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001332}
1333
1334static StringRef getMipsTargetABISuffix(llvm::Triple::ArchType TargetArch,
1335 const ArgList &Args) {
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001336 if (TargetArch == llvm::Triple::mips64 ||
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001337 TargetArch == llvm::Triple::mips64el)
1338 return hasMipsN32ABIArg(Args) ? "/n32" : "/64";
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001339
1340 return "/32";
1341}
1342
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001343static bool findTargetMultiarchSuffix(std::string &Suffix,
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001344 StringRef Path,
1345 llvm::Triple::ArchType TargetArch,
1346 const ArgList &Args) {
1347 if (isMipsArch(TargetArch)) {
1348 StringRef ABISuffix = getMipsTargetABISuffix(TargetArch, Args);
1349
1350 // First build and check a complex path to crtbegin.o
1351 // depends on command line options (-mips16, -msoft-float, ...)
1352 // like mips-linux-gnu/4.7/mips16/soft-float/el/crtbegin.o
1353 appendMipsTargetSuffix(Suffix, TargetArch, Args);
1354
1355 if (TargetArch == llvm::Triple::mips64 ||
1356 TargetArch == llvm::Triple::mips64el)
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001357 Suffix += ABISuffix;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001358
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001359 if (llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o"))
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001360 return true;
1361
1362 // Then fall back and probe a simple case like
1363 // mips-linux-gnu/4.7/32/crtbegin.o
1364 Suffix = ABISuffix;
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001365 return llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o");
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001366 }
1367
1368 if (TargetArch == llvm::Triple::x86_64 ||
Ulrich Weigandb8409212013-05-06 16:26:41 +00001369 TargetArch == llvm::Triple::ppc64 ||
1370 TargetArch == llvm::Triple::systemz)
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001371 Suffix = "/64";
1372 else
1373 Suffix = "/32";
1374
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001375 return llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o");
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001376}
1377
Chandler Carruth19347ed2011-11-06 23:39:34 +00001378void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001379 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1380 const std::string &LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001381 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001382 // There are various different suffixes involving the triple we
1383 // check for. We also record what is necessary to walk from each back
1384 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001385 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001386 "/gcc/" + CandidateTriple.str(),
1387 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1388
Hal Finkel02014b42012-09-18 22:25:07 +00001389 // The Freescale PPC SDK has the gcc libraries in
1390 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1391 "/" + CandidateTriple.str(),
1392
Chandler Carruth19347ed2011-11-06 23:39:34 +00001393 // Ubuntu has a strange mis-matched pair of triples that this happens to
1394 // match.
1395 // FIXME: It may be worthwhile to generalize this and look for a second
1396 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001397 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001398 };
1399 const std::string InstallSuffixes[] = {
1400 "/../../..",
1401 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001402 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001403 "/../../../.."
1404 };
1405 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001406 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1407 (TargetArch != llvm::Triple::x86));
1408 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1409 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001410 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001411 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001412 !EC && LI != LE; LI = LI.increment(EC)) {
1413 StringRef VersionText = llvm::sys::path::filename(LI->path());
1414 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1415 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1416 if (CandidateVersion < MinVersion)
1417 continue;
1418 if (CandidateVersion <= Version)
1419 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001420
1421 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001422 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001423 // libs in a subdirectory named 64. The simple logic we follow is that
1424 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1425 // we use that. If not, and if not a multiarch triple, we look for
1426 // crtbegin.o without the subdirectory.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001427
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001428 std::string MultiarchSuffix;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001429 if (findTargetMultiarchSuffix(MultiarchSuffix,
1430 LI->path(), TargetArch, Args)) {
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001431 GCCMultiarchSuffix = MultiarchSuffix;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001432 } else {
1433 if (NeedsMultiarchSuffix ||
1434 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1435 continue;
1436 GCCMultiarchSuffix.clear();
1437 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001438
1439 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001440 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001441 // FIXME: We hack together the directory name here instead of
1442 // using LI to ensure stable path separators across Windows and
1443 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001444 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001445 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001446 IsValid = true;
1447 }
1448 }
1449}
1450
Rafael Espindola0e659592012-02-19 01:38:32 +00001451Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1452 const ArgList &Args)
Rafael Espindolaaf370e62013-03-18 18:10:27 +00001453 : ToolChain(D, Triple, Args), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001454 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001455 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001456 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001457}
1458
Daniel Dunbar39176082009-03-20 00:20:03 +00001459Generic_GCC::~Generic_GCC() {
Daniel Dunbar39176082009-03-20 00:20:03 +00001460}
1461
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001462Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola5b222052013-03-18 20:48:54 +00001463 switch (AC) {
Rafael Espindolaf3260562013-03-18 18:50:01 +00001464 case Action::PreprocessJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001465 if (!Preprocess)
1466 Preprocess.reset(new tools::gcc::Preprocess(*this));
1467 return Preprocess.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +00001468 case Action::PrecompileJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001469 if (!Precompile)
1470 Precompile.reset(new tools::gcc::Precompile(*this));
1471 return Precompile.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +00001472 case Action::CompileJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001473 if (!Compile)
1474 Compile.reset(new tools::gcc::Compile(*this));
1475 return Compile.get();
Rafael Espindolac0a55d12013-03-19 00:36:57 +00001476 default:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001477 return ToolChain::getTool(AC);
Daniel Dunbar39176082009-03-20 00:20:03 +00001478 }
Daniel Dunbar39176082009-03-20 00:20:03 +00001479}
1480
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001481Tool *Generic_GCC::buildAssembler() const {
1482 return new tools::gcc::Assemble(*this);
1483}
1484
1485Tool *Generic_GCC::buildLinker() const {
1486 return new tools::gcc::Link(*this);
1487}
1488
Daniel Dunbar39176082009-03-20 00:20:03 +00001489bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00001490 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00001491}
1492
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001493bool Generic_GCC::isPICDefault() const {
1494 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00001495}
1496
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00001497bool Generic_GCC::isPIEDefault() const {
1498 return false;
1499}
1500
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001501bool Generic_GCC::isPICDefaultForced() const {
1502 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00001503}
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001504
Tony Linthicum96319392011-12-12 21:14:55 +00001505/// Hexagon Toolchain
1506
Matthew Curtisb3489a02012-12-06 12:43:18 +00001507std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1508
1509 // Locate the rest of the toolchain ...
1510 if (strlen(GCC_INSTALL_PREFIX))
1511 return std::string(GCC_INSTALL_PREFIX);
1512
1513 std::string InstallRelDir = InstalledDir + "/../../gnu";
1514 if (llvm::sys::fs::exists(InstallRelDir))
1515 return InstallRelDir;
1516
1517 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1518 if (llvm::sys::fs::exists(PrefixRelDir))
1519 return PrefixRelDir;
1520
1521 return InstallRelDir;
1522}
1523
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001524static void GetHexagonLibraryPaths(
1525 const ArgList &Args,
1526 const std::string Ver,
1527 const std::string MarchString,
1528 const std::string &InstalledDir,
1529 ToolChain::path_list *LibPaths)
1530{
1531 bool buildingLib = Args.hasArg(options::OPT_shared);
1532
1533 //----------------------------------------------------------------------------
1534 // -L Args
1535 //----------------------------------------------------------------------------
1536 for (arg_iterator
1537 it = Args.filtered_begin(options::OPT_L),
1538 ie = Args.filtered_end();
1539 it != ie;
1540 ++it) {
1541 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1542 LibPaths->push_back((*it)->getValue(i));
1543 }
1544
1545 //----------------------------------------------------------------------------
1546 // Other standard paths
1547 //----------------------------------------------------------------------------
1548 const std::string MarchSuffix = "/" + MarchString;
1549 const std::string G0Suffix = "/G0";
1550 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1551 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1552
1553 // lib/gcc/hexagon/...
1554 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1555 if (buildingLib) {
1556 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1557 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1558 }
1559 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1560 LibPaths->push_back(LibGCCHexagonDir + Ver);
1561
1562 // lib/gcc/...
1563 LibPaths->push_back(RootDir + "lib/gcc");
1564
1565 // hexagon/lib/...
1566 std::string HexagonLibDir = RootDir + "hexagon/lib";
1567 if (buildingLib) {
1568 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1569 LibPaths->push_back(HexagonLibDir + G0Suffix);
1570 }
1571 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1572 LibPaths->push_back(HexagonLibDir);
1573}
1574
Matthew Curtisb3489a02012-12-06 12:43:18 +00001575Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1576 const ArgList &Args)
1577 : Linux(D, Triple, Args) {
1578 const std::string InstalledDir(getDriver().getInstalledDir());
1579 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1580
1581 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1582 // program paths
1583 const std::string BinDir(GnuDir + "/bin");
1584 if (llvm::sys::fs::exists(BinDir))
1585 getProgramPaths().push_back(BinDir);
1586
1587 // Determine version of GCC libraries and headers to use.
1588 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1589 llvm::error_code ec;
1590 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1591 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1592 !ec && di != de; di = di.increment(ec)) {
1593 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1594 if (MaxVersion < cv)
1595 MaxVersion = cv;
1596 }
1597 GCCLibAndIncVersion = MaxVersion;
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001598
1599 ToolChain::path_list *LibPaths= &getFilePaths();
1600
1601 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1602 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1603 // support 'linux' we'll need to fix this up
1604 LibPaths->clear();
1605
1606 GetHexagonLibraryPaths(
1607 Args,
1608 GetGCCLibAndIncVersion(),
1609 GetTargetCPU(Args),
1610 InstalledDir,
1611 LibPaths);
Tony Linthicum96319392011-12-12 21:14:55 +00001612}
1613
1614Hexagon_TC::~Hexagon_TC() {
Tony Linthicum96319392011-12-12 21:14:55 +00001615}
1616
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001617Tool *Hexagon_TC::buildAssembler() const {
1618 return new tools::hexagon::Assemble(*this);
1619}
1620
1621Tool *Hexagon_TC::buildLinker() const {
1622 return new tools::hexagon::Link(*this);
Tony Linthicum96319392011-12-12 21:14:55 +00001623}
1624
Matthew Curtisb3489a02012-12-06 12:43:18 +00001625void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1626 ArgStringList &CC1Args) const {
1627 const Driver &D = getDriver();
1628
1629 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1630 DriverArgs.hasArg(options::OPT_nostdlibinc))
1631 return;
1632
1633 llvm::sys::Path InstallDir(D.InstalledDir);
1634 std::string Ver(GetGCCLibAndIncVersion());
1635 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1636 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1637 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1638 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1639 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum96319392011-12-12 21:14:55 +00001640}
1641
Matthew Curtisb3489a02012-12-06 12:43:18 +00001642void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1643 ArgStringList &CC1Args) const {
1644
1645 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1646 DriverArgs.hasArg(options::OPT_nostdincxx))
1647 return;
1648
1649 const Driver &D = getDriver();
1650 std::string Ver(GetGCCLibAndIncVersion());
1651 llvm::sys::Path IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
1652
1653 IncludeDir.appendComponent("hexagon/include/c++/");
1654 IncludeDir.appendComponent(Ver);
1655 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001656}
Matthew Curtis67814152012-12-06 14:16:43 +00001657
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001658ToolChain::CXXStdlibType
1659Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1660 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1661 if (!A)
1662 return ToolChain::CST_Libstdcxx;
1663
1664 StringRef Value = A->getValue();
1665 if (Value != "libstdc++") {
1666 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1667 << A->getAsString(Args);
1668 }
1669
1670 return ToolChain::CST_Libstdcxx;
1671}
1672
1673static Arg *GetLastHexagonArchArg(const ArgList &Args)
Matthew Curtis67814152012-12-06 14:16:43 +00001674{
1675 Arg *A = NULL;
1676
1677 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1678 it != ie; ++it) {
1679 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
1680 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1681 A = *it;
1682 A->claim();
1683 } else if ((*it)->getOption().matches(options::OPT_m_Joined)) {
1684 StringRef Value = (*it)->getValue(0);
1685 if (Value.startswith("v")) {
1686 A = *it;
1687 A->claim();
1688 }
1689 }
1690 }
1691 return A;
1692}
1693
1694StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1695{
1696 // Select the default CPU (v4) if none was given or detection failed.
1697 Arg *A = GetLastHexagonArchArg (Args);
1698 if (A) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001699 StringRef WhichHexagon = A->getValue();
Matthew Curtis67814152012-12-06 14:16:43 +00001700 if (WhichHexagon.startswith("hexagon"))
1701 return WhichHexagon.substr(sizeof("hexagon") - 1);
1702 if (WhichHexagon != "")
1703 return WhichHexagon;
1704 }
1705
1706 return "v4";
1707}
Matthew Curtisb3489a02012-12-06 12:43:18 +00001708// End Hexagon
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001709
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001710/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1711/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1712/// Currently does not support anything else but compilation.
1713
Rafael Espindolaaf370e62013-03-18 18:10:27 +00001714TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
1715 const ArgList &Args)
1716 : ToolChain(D, Triple, Args) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001717 // Path mangling to find libexec
1718 std::string Path(getDriver().Dir);
1719
1720 Path += "/../libexec";
1721 getProgramPaths().push_back(Path);
1722}
1723
1724TCEToolChain::~TCEToolChain() {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001725}
1726
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001727bool TCEToolChain::IsMathErrnoDefault() const {
1728 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001729}
1730
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001731bool TCEToolChain::isPICDefault() const {
1732 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001733}
1734
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00001735bool TCEToolChain::isPIEDefault() const {
1736 return false;
1737}
1738
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001739bool TCEToolChain::isPICDefaultForced() const {
1740 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001741}
1742
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001743/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1744
Rafael Espindola0e659592012-02-19 01:38:32 +00001745OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1746 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001747 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001748 getFilePaths().push_back("/usr/lib");
1749}
1750
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001751Tool *OpenBSD::buildAssembler() const {
1752 return new tools::openbsd::Assemble(*this);
1753}
1754
1755Tool *OpenBSD::buildLinker() const {
1756 return new tools::openbsd::Link(*this);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001757}
1758
Eli Friedman42f74f22012-08-08 23:57:20 +00001759/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1760
1761Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1762 : Generic_ELF(D, Triple, Args) {
1763 getFilePaths().push_back(getDriver().Dir + "/../lib");
1764 getFilePaths().push_back("/usr/lib");
1765}
1766
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001767Tool *Bitrig::buildAssembler() const {
1768 return new tools::bitrig::Assemble(*this);
1769}
1770
1771Tool *Bitrig::buildLinker() const {
1772 return new tools::bitrig::Link(*this);
Eli Friedman42f74f22012-08-08 23:57:20 +00001773}
1774
1775void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1776 ArgStringList &CC1Args) const {
1777 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1778 DriverArgs.hasArg(options::OPT_nostdincxx))
1779 return;
1780
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001781 switch (GetCXXStdlibType(DriverArgs)) {
1782 case ToolChain::CST_Libcxx:
1783 addSystemInclude(DriverArgs, CC1Args,
1784 getDriver().SysRoot + "/usr/include/c++/");
1785 break;
1786 case ToolChain::CST_Libstdcxx:
1787 addSystemInclude(DriverArgs, CC1Args,
1788 getDriver().SysRoot + "/usr/include/c++/stdc++");
1789 addSystemInclude(DriverArgs, CC1Args,
1790 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001791
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001792 StringRef Triple = getTriple().str();
1793 if (Triple.startswith("amd64"))
1794 addSystemInclude(DriverArgs, CC1Args,
1795 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1796 Triple.substr(5));
1797 else
1798 addSystemInclude(DriverArgs, CC1Args,
1799 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1800 Triple);
1801 break;
1802 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001803}
1804
1805void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1806 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001807 switch (GetCXXStdlibType(Args)) {
1808 case ToolChain::CST_Libcxx:
1809 CmdArgs.push_back("-lc++");
1810 CmdArgs.push_back("-lcxxrt");
1811 // Include supc++ to provide Unwind until provided by libcxx.
1812 CmdArgs.push_back("-lgcc");
1813 break;
1814 case ToolChain::CST_Libstdcxx:
1815 CmdArgs.push_back("-lstdc++");
1816 break;
1817 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001818}
1819
Daniel Dunbar75358d22009-03-30 21:06:03 +00001820/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1821
Rafael Espindola0e659592012-02-19 01:38:32 +00001822FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1823 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001824
Chandler Carruth24248e32012-01-26 01:35:15 +00001825 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1826 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001827 if ((Triple.getArch() == llvm::Triple::x86 ||
1828 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001829 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001830 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1831 else
1832 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001833}
1834
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001835Tool *FreeBSD::buildAssembler() const {
1836 return new tools::freebsd::Assemble(*this);
1837}
1838
1839Tool *FreeBSD::buildLinker() const {
1840 return new tools::freebsd::Link(*this);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001841}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001842
Rafael Espindola27fa2362012-12-13 04:17:14 +00001843bool FreeBSD::UseSjLjExceptions() const {
1844 // FreeBSD uses SjLj exceptions on ARM oabi.
1845 switch (getTriple().getEnvironment()) {
1846 case llvm::Triple::GNUEABI:
1847 case llvm::Triple::EABI:
1848 return false;
1849
1850 default:
1851 return (getTriple().getArch() == llvm::Triple::arm ||
1852 getTriple().getArch() == llvm::Triple::thumb);
1853 }
1854}
1855
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001856/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1857
Rafael Espindola0e659592012-02-19 01:38:32 +00001858NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1859 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001860
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001861 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001862 // When targeting a 32-bit platform, try the special directory used on
1863 // 64-bit hosts, and only fall back to the main library directory if that
1864 // doesn't work.
1865 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1866 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001867 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001868 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001869
1870 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001871 }
1872}
1873
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001874Tool *NetBSD::buildAssembler() const {
1875 return new tools::netbsd::Assemble(*this);
1876}
1877
1878Tool *NetBSD::buildLinker() const {
1879 return new tools::netbsd::Link(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001880}
1881
Joerg Sonnenbergera7efaf92013-04-30 01:21:43 +00001882ToolChain::CXXStdlibType
1883NetBSD::GetCXXStdlibType(const ArgList &Args) const {
1884 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
1885 StringRef Value = A->getValue();
1886 if (Value == "libstdc++")
1887 return ToolChain::CST_Libstdcxx;
1888 if (Value == "libc++")
1889 return ToolChain::CST_Libcxx;
1890
1891 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1892 << A->getAsString(Args);
1893 }
1894
1895 return ToolChain::CST_Libstdcxx;
1896}
1897
1898void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1899 ArgStringList &CC1Args) const {
1900 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1901 DriverArgs.hasArg(options::OPT_nostdincxx))
1902 return;
1903
1904 switch (GetCXXStdlibType(DriverArgs)) {
1905 case ToolChain::CST_Libcxx:
1906 addSystemInclude(DriverArgs, CC1Args,
1907 getDriver().SysRoot + "/usr/include/c++/");
1908 break;
1909 case ToolChain::CST_Libstdcxx:
1910 addSystemInclude(DriverArgs, CC1Args,
1911 getDriver().SysRoot + "/usr/include/g++");
1912 addSystemInclude(DriverArgs, CC1Args,
1913 getDriver().SysRoot + "/usr/include/g++/backward");
1914 break;
1915 }
1916}
1917
Chris Lattner38e317d2010-07-07 16:01:42 +00001918/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1919
Rafael Espindola0e659592012-02-19 01:38:32 +00001920Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1921 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001922 getFilePaths().push_back(getDriver().Dir + "/../lib");
1923 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001924}
1925
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001926Tool *Minix::buildAssembler() const {
1927 return new tools::minix::Assemble(*this);
1928}
1929
1930Tool *Minix::buildLinker() const {
1931 return new tools::minix::Link(*this);
Chris Lattner38e317d2010-07-07 16:01:42 +00001932}
1933
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001934/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1935
Rafael Espindola0e659592012-02-19 01:38:32 +00001936AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1937 const ArgList &Args)
1938 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001939
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001940 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001941 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001942 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001943
Daniel Dunbaree788e72009-12-21 18:54:17 +00001944 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001945 getFilePaths().push_back("/usr/lib");
1946 getFilePaths().push_back("/usr/sfw/lib");
1947 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001948 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001949
1950}
1951
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001952Tool *AuroraUX::buildAssembler() const {
1953 return new tools::auroraux::Assemble(*this);
1954}
1955
1956Tool *AuroraUX::buildLinker() const {
1957 return new tools::auroraux::Link(*this);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001958}
1959
David Chisnall31c46902012-02-15 13:39:01 +00001960/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1961
Rafael Espindola0e659592012-02-19 01:38:32 +00001962Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1963 const ArgList &Args)
1964 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001965
1966 getProgramPaths().push_back(getDriver().getInstalledDir());
1967 if (getDriver().getInstalledDir() != getDriver().Dir)
1968 getProgramPaths().push_back(getDriver().Dir);
1969
1970 getFilePaths().push_back(getDriver().Dir + "/../lib");
1971 getFilePaths().push_back("/usr/lib");
1972}
1973
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001974Tool *Solaris::buildAssembler() const {
1975 return new tools::solaris::Assemble(*this);
1976}
1977
1978Tool *Solaris::buildLinker() const {
1979 return new tools::solaris::Link(*this);
David Chisnall31c46902012-02-15 13:39:01 +00001980}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001981
Thomas Schwinged52b4a92013-03-28 19:02:48 +00001982/// Distribution (very bare-bones at the moment).
Eli Friedman6b3454a2009-05-26 07:52:18 +00001983
Thomas Schwinged52b4a92013-03-28 19:02:48 +00001984enum Distro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001985 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001986 DebianLenny,
1987 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001988 DebianWheezy,
Sylvestre Ledrub1718522013-01-06 08:09:29 +00001989 DebianJessie,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001990 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001991 RHEL4,
1992 RHEL5,
1993 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001994 Fedora13,
1995 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001996 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001997 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001998 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001999 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00002000 OpenSuse11_4,
2001 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002002 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00002003 UbuntuHardy,
2004 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00002005 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00002006 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002007 UbuntuLucid,
2008 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00002009 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00002010 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002011 UbuntuPrecise,
Rafael Espindolade39d172012-12-13 20:26:05 +00002012 UbuntuQuantal,
2013 UbuntuRaring,
Sylvestre Ledru1b034642013-06-13 11:52:27 +00002014 UbuntuSaucy,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002015 UnknownDistro
2016};
2017
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002018static bool IsRedhat(enum Distro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002019 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
2020 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002021}
2022
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002023static bool IsOpenSuse(enum Distro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002024 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002025}
2026
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002027static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002028 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002029}
2030
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002031static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledru1b034642013-06-13 11:52:27 +00002032 return Distro >= UbuntuHardy && Distro <= UbuntuSaucy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002033}
2034
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002035static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002036 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002037 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002038 StringRef Data = File.get()->getBuffer();
2039 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002040 Data.split(Lines, "\n");
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002041 Distro Version = UnknownDistro;
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002042 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2043 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002044 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002045 .Case("hardy", UbuntuHardy)
2046 .Case("intrepid", UbuntuIntrepid)
2047 .Case("jaunty", UbuntuJaunty)
2048 .Case("karmic", UbuntuKarmic)
2049 .Case("lucid", UbuntuLucid)
2050 .Case("maverick", UbuntuMaverick)
2051 .Case("natty", UbuntuNatty)
2052 .Case("oneiric", UbuntuOneiric)
2053 .Case("precise", UbuntuPrecise)
Rafael Espindolade39d172012-12-13 20:26:05 +00002054 .Case("quantal", UbuntuQuantal)
2055 .Case("raring", UbuntuRaring)
Sylvestre Ledru1b034642013-06-13 11:52:27 +00002056 .Case("saucy", UbuntuSaucy)
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002057 .Default(UnknownDistro);
2058 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002059 }
2060
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002061 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002062 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002063 if (Data.startswith("Fedora release 16"))
2064 return Fedora16;
2065 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00002066 return Fedora15;
2067 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002068 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00002069 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002070 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00002071 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002072 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00002073 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00002074 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002075 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002076 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002077 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopher4f4e2af2013-02-05 07:29:49 +00002078 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002079 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002080 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002081 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopher4f4e2af2013-02-05 07:29:49 +00002082 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002083 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002084 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002085 return UnknownDistro;
2086 }
2087
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002088 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002089 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002090 if (Data[0] == '5')
2091 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002092 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00002093 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002094 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00002095 return DebianWheezy;
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002096 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2097 return DebianJessie;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002098 return UnknownDistro;
2099 }
2100
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002101 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002102 return llvm::StringSwitch<Distro>(File.get()->getBuffer())
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002103 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2104 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2105 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002106 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002107 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002108
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00002109 bool Exists;
2110 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00002111 return Exherbo;
2112
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002113 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2114 return ArchLinux;
2115
Rafael Espindolac1da9812010-11-07 20:14:31 +00002116 return UnknownDistro;
2117}
2118
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002119/// \brief Get our best guess at the multiarch triple for a target.
2120///
2121/// Debian-based systems are starting to use a multiarch setup where they use
2122/// a target-triple directory in the library and header search paths.
2123/// Unfortunately, this triple does not align with the vanilla target triple,
2124/// so we provide a rough mapping here.
2125static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2126 StringRef SysRoot) {
2127 // For most architectures, just use whatever we have rather than trying to be
2128 // clever.
2129 switch (TargetTriple.getArch()) {
2130 default:
2131 return TargetTriple.str();
2132
2133 // We use the existence of '/lib/<triple>' as a directory to detect some
2134 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00002135 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2136 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00002137 case llvm::Triple::arm:
2138 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00002139 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2140 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2141 return "arm-linux-gnueabihf";
2142 } else {
2143 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2144 return "arm-linux-gnueabi";
2145 }
Chad Rosier0337efd2012-07-11 19:08:21 +00002146 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002147 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002148 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2149 return "i386-linux-gnu";
2150 return TargetTriple.str();
2151 case llvm::Triple::x86_64:
2152 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2153 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002154 return TargetTriple.str();
Tim Northoverc264e162013-01-31 12:13:10 +00002155 case llvm::Triple::aarch64:
2156 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2157 return "aarch64-linux-gnu";
Tim Northover162579a2013-06-13 22:54:55 +00002158 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00002159 case llvm::Triple::mips:
2160 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2161 return "mips-linux-gnu";
2162 return TargetTriple.str();
2163 case llvm::Triple::mipsel:
2164 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2165 return "mipsel-linux-gnu";
2166 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002167 case llvm::Triple::ppc:
Sylvestre Ledrub7e86be2013-03-15 16:22:43 +00002168 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2169 return "powerpc-linux-gnuspe";
Chandler Carruth155c54c2012-02-26 09:03:21 +00002170 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2171 return "powerpc-linux-gnu";
2172 return TargetTriple.str();
2173 case llvm::Triple::ppc64:
2174 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2175 return "powerpc64-linux-gnu";
2176 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002177 }
2178}
2179
Chandler Carruth00646ba2012-01-25 11:24:24 +00002180static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2181 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2182}
2183
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002184static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2185 const ArgList &Args) {
2186 if (Arch != llvm::Triple::mips &&
2187 Arch != llvm::Triple::mipsel)
2188 return false;
2189
2190 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2191 options::OPT_mcpu_EQ,
2192 options::OPT_mips_CPUs_Group);
2193
2194 if (!A)
2195 return false;
2196
2197 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2198 return A->getOption().matches(options::OPT_mips32r2);
2199
Richard Smith1d489cf2012-11-01 04:30:05 +00002200 return A->getValue() == StringRef("mips32r2");
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002201}
2202
Simon Atanasyan7a918882012-09-14 11:27:24 +00002203static StringRef getMultilibDir(const llvm::Triple &Triple,
2204 const ArgList &Args) {
2205 if (!isMipsArch(Triple.getArch()))
2206 return Triple.isArch32Bit() ? "lib32" : "lib64";
2207
2208 // lib32 directory has a special meaning on MIPS targets.
2209 // It contains N32 ABI binaries. Use this folder if produce
2210 // code for N32 ABI only.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00002211 if (hasMipsN32ABIArg(Args))
Simon Atanasyan7a918882012-09-14 11:27:24 +00002212 return "lib32";
2213
2214 return Triple.isArch32Bit() ? "lib" : "lib64";
2215}
2216
Rafael Espindola0e659592012-02-19 01:38:32 +00002217Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2218 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002219 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002220 std::string SysRoot = computeSysRoot(Args);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002221
Rafael Espindolaab784082011-09-01 16:25:49 +00002222 // OpenSuse stores the linker with the compiler, add that to the search
2223 // path.
2224 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002225 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002226 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002227
2228 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002229
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002230 Distro Distro = DetectDistro(Arch);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002231
Chris Lattner64a89172011-05-22 16:45:07 +00002232 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002233 ExtraOpts.push_back("-z");
2234 ExtraOpts.push_back("relro");
2235 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002236
Douglas Gregorf0594d82011-03-06 19:11:49 +00002237 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002238 ExtraOpts.push_back("-X");
2239
Logan Chien94a71422012-09-02 09:30:11 +00002240 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002241 const bool IsMips = isMipsArch(Arch);
2242
2243 if (IsMips && !SysRoot.empty())
2244 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002245
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002246 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2247 // and the MIPS ABI require .dynsym to be sorted in different ways.
2248 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2249 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002250 // Android loader does not support .gnu.hash.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002251 if (!IsMips && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002252 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2253 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002254 ExtraOpts.push_back("--hash-style=gnu");
2255
2256 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2257 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2258 ExtraOpts.push_back("--hash-style=both");
2259 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002260
Chris Lattnerd753b562011-05-22 05:36:06 +00002261 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002262 ExtraOpts.push_back("--no-add-needed");
2263
Eli Friedman0b200f62011-06-02 21:36:53 +00002264 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002265 Distro == DebianJessie || IsOpenSuse(Distro) ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002266 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002267 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002268 ExtraOpts.push_back("--build-id");
2269
Chris Lattner64a89172011-05-22 16:45:07 +00002270 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002271 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002272
Chandler Carruthd2deee12011-10-03 05:28:29 +00002273 // The selection of paths to try here is designed to match the patterns which
2274 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2275 // This was determined by running GCC in a fake filesystem, creating all
2276 // possible permutations of these directories, and seeing which ones it added
2277 // to the link paths.
2278 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002279
Simon Atanasyan7a918882012-09-14 11:27:24 +00002280 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002281 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002282
Chandler Carruthd1f73062011-11-06 23:09:05 +00002283 // Add the multilib suffixed paths where they are available.
2284 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002285 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002286 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002287
2288 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2289 addPathIfExists(GCCInstallation.getInstallPath() +
2290 GCCInstallation.getMultiarchSuffix() +
2291 "/mips-r2",
2292 Paths);
2293 else
2294 addPathIfExists((GCCInstallation.getInstallPath() +
2295 GCCInstallation.getMultiarchSuffix()),
2296 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002297
2298 // If the GCC installation we found is inside of the sysroot, we want to
2299 // prefer libraries installed in the parent prefix of the GCC installation.
2300 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002301 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002302 // This usually happens when there is an external cross compiler on the
2303 // host system, and a more minimal sysroot available that is the target of
2304 // the cross.
2305 if (StringRef(LibPath).startswith(SysRoot)) {
2306 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2307 Paths);
2308 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2309 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2310 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002311 // On Android, libraries in the parent prefix of the GCC installation are
2312 // preferred to the ones under sysroot.
2313 if (IsAndroid) {
2314 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2315 }
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002316 // Sourcery CodeBench MIPS toolchain holds some libraries under
2317 // the parent prefix of the GCC installation.
2318 if (IsMips) {
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00002319 std::string Suffix;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002320 appendMipsTargetSuffix(Suffix, Arch, Args);
2321 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" +
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00002322 Multilib + Suffix,
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002323 Paths);
2324 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002325 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002326 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2327 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2328 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2329 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2330
2331 // Try walking via the GCC triple path in case of multiarch GCC
2332 // installations with strange symlinks.
2333 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002334 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002335 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002336
Chandler Carruth7a09d012011-10-16 10:54:30 +00002337 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002338 if (GCCInstallation.isValid()) {
2339 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002340 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002341 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002342 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002343
2344 if (StringRef(LibPath).startswith(SysRoot)) {
2345 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2346 addPathIfExists(LibPath, Paths);
2347 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002348 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002349 addPathIfExists(SysRoot + "/lib", Paths);
2350 addPathIfExists(SysRoot + "/usr/lib", Paths);
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002351
2352 IsPIEDefault = SanitizerArgs(*this, Args).hasZeroBaseShadow();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002353}
2354
2355bool Linux::HasNativeLLVMSupport() const {
2356 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002357}
2358
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002359Tool *Linux::buildLinker() const {
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00002360 return new tools::gnutools::Link(*this);
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002361}
2362
2363Tool *Linux::buildAssembler() const {
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00002364 return new tools::gnutools::Assemble(*this);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002365}
2366
Chandler Carrutha6b25812012-11-21 23:40:23 +00002367void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2368 ArgStringList &CC1Args) const {
Rafael Espindola8af669f2012-06-19 01:26:10 +00002369 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carrutha6b25812012-11-21 23:40:23 +00002370 bool UseInitArrayDefault
2371 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
Tim Northoverc264e162013-01-31 12:13:10 +00002372 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carrutha6b25812012-11-21 23:40:23 +00002373 getTriple().getEnvironment() == llvm::Triple::Android;
2374 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2375 options::OPT_fno_use_init_array,
2376 UseInitArrayDefault))
Rafael Espindola8af669f2012-06-19 01:26:10 +00002377 CC1Args.push_back("-fuse-init-array");
2378}
2379
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002380std::string Linux::computeSysRoot(const ArgList &Args) const {
2381 if (!getDriver().SysRoot.empty())
2382 return getDriver().SysRoot;
2383
2384 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
2385 return std::string();
2386
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00002387 std::string Path =
2388 (GCCInstallation.getInstallPath() +
2389 "/../../../../" + GCCInstallation.getTriple().str() + "/libc").str();
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002390 appendMipsTargetSuffix(Path, getTriple().getArch(), Args);
2391
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00002392 return llvm::sys::fs::exists(Path) ? Path : "";
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002393}
2394
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002395void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2396 ArgStringList &CC1Args) const {
2397 const Driver &D = getDriver();
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002398 std::string SysRoot = computeSysRoot(DriverArgs);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002399
2400 if (DriverArgs.hasArg(options::OPT_nostdinc))
2401 return;
2402
2403 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002404 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002405
2406 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002407 llvm::sys::Path P(D.ResourceDir);
2408 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002409 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002410 }
2411
2412 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2413 return;
2414
2415 // Check for configure-time C include directories.
2416 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2417 if (CIncludeDirs != "") {
2418 SmallVector<StringRef, 5> dirs;
2419 CIncludeDirs.split(dirs, ":");
2420 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2421 I != E; ++I) {
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002422 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? SysRoot : "";
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002423 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2424 }
2425 return;
2426 }
2427
2428 // Lacking those, try to detect the correct set of system includes for the
2429 // target triple.
2430
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002431 // Sourcery CodeBench and modern FSF Mips toolchains put extern C
2432 // system includes under three additional directories.
2433 if (GCCInstallation.isValid() && isMipsArch(getTriple().getArch())) {
2434 addExternCSystemIncludeIfExists(DriverArgs, CC1Args,
2435 GCCInstallation.getInstallPath() +
2436 "/include");
2437
2438 addExternCSystemIncludeIfExists(DriverArgs, CC1Args,
2439 GCCInstallation.getInstallPath() +
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002440 "/../../../../" +
2441 GCCInstallation.getTriple().str() +
2442 "/libc/usr/include");
2443 }
2444
Chandler Carrutha4630892011-11-06 08:21:07 +00002445 // Implement generic Debian multiarch support.
2446 const StringRef X86_64MultiarchIncludeDirs[] = {
2447 "/usr/include/x86_64-linux-gnu",
2448
2449 // FIXME: These are older forms of multiarch. It's not clear that they're
2450 // in use in any released version of Debian, so we should consider
2451 // removing them.
2452 "/usr/include/i686-linux-gnu/64",
2453 "/usr/include/i486-linux-gnu/64"
2454 };
2455 const StringRef X86MultiarchIncludeDirs[] = {
2456 "/usr/include/i386-linux-gnu",
2457
2458 // FIXME: These are older forms of multiarch. It's not clear that they're
2459 // in use in any released version of Debian, so we should consider
2460 // removing them.
2461 "/usr/include/x86_64-linux-gnu/32",
2462 "/usr/include/i686-linux-gnu",
2463 "/usr/include/i486-linux-gnu"
2464 };
Tim Northoverc264e162013-01-31 12:13:10 +00002465 const StringRef AArch64MultiarchIncludeDirs[] = {
2466 "/usr/include/aarch64-linux-gnu"
2467 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002468 const StringRef ARMMultiarchIncludeDirs[] = {
2469 "/usr/include/arm-linux-gnueabi"
2470 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002471 const StringRef ARMHFMultiarchIncludeDirs[] = {
2472 "/usr/include/arm-linux-gnueabihf"
2473 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002474 const StringRef MIPSMultiarchIncludeDirs[] = {
2475 "/usr/include/mips-linux-gnu"
2476 };
2477 const StringRef MIPSELMultiarchIncludeDirs[] = {
2478 "/usr/include/mipsel-linux-gnu"
2479 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002480 const StringRef PPCMultiarchIncludeDirs[] = {
2481 "/usr/include/powerpc-linux-gnu"
2482 };
2483 const StringRef PPC64MultiarchIncludeDirs[] = {
2484 "/usr/include/powerpc64-linux-gnu"
2485 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002486 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002487 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002488 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002489 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002490 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northoverc264e162013-01-31 12:13:10 +00002491 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2492 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002493 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002494 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2495 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2496 else
2497 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002498 } else if (getTriple().getArch() == llvm::Triple::mips) {
2499 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2500 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2501 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002502 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2503 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2504 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2505 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002506 }
2507 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2508 E = MultiarchIncludeDirs.end();
2509 I != E; ++I) {
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002510 if (llvm::sys::fs::exists(SysRoot + *I)) {
2511 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + *I);
Chandler Carrutha4630892011-11-06 08:21:07 +00002512 break;
2513 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002514 }
2515
2516 if (getTriple().getOS() == llvm::Triple::RTEMS)
2517 return;
2518
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002519 // Add an include of '/include' directly. This isn't provided by default by
2520 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2521 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002522 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002523
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002524 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002525}
2526
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002527/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002528/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2529 const ArgList &DriverArgs,
2530 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002531 if (!llvm::sys::fs::exists(Base))
2532 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002533 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002534 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002535 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002536 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002537}
2538
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002539/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2540/// libstdc++ installation.
2541/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2542 Twine TargetArchDir,
2543 Twine MultiLibSuffix,
2544 const ArgList &DriverArgs,
2545 ArgStringList &CC1Args) {
2546 if (!addLibStdCXXIncludePaths(Base+Suffix, TargetArchDir + MultiLibSuffix,
2547 DriverArgs, CC1Args))
2548 return false;
2549
2550 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
2551 + MultiLibSuffix);
2552 return true;
2553}
2554
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002555void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2556 ArgStringList &CC1Args) const {
2557 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2558 DriverArgs.hasArg(options::OPT_nostdincxx))
2559 return;
2560
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002561 // Check if libc++ has been enabled and provide its include paths if so.
2562 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2563 // libc++ is always installed at a fixed path on Linux currently.
2564 addSystemInclude(DriverArgs, CC1Args,
2565 getDriver().SysRoot + "/usr/include/c++/v1");
2566 return;
2567 }
2568
Chandler Carruthfc52f752012-01-25 08:04:13 +00002569 // We need a detected GCC installation on Linux to provide libstdc++'s
2570 // headers. We handled the libc++ case above.
2571 if (!GCCInstallation.isValid())
2572 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002573
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002574 // By default, look for the C++ headers in an include directory adjacent to
2575 // the lib directory of the GCC installation. Note that this is expect to be
2576 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2577 StringRef LibDir = GCCInstallation.getParentLibPath();
2578 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002579 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002580 StringRef TripleStr = GCCInstallation.getTriple().str();
2581
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002582 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2583 "/c++/" + Version.str(),
2584 TripleStr,
2585 GCCInstallation.getMultiarchSuffix(),
2586 DriverArgs, CC1Args))
2587 return;
2588
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002589 const std::string IncludePathCandidates[] = {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002590 // Gentoo is weird and places its headers inside the GCC install, so if the
2591 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002592 InstallDir.str() + "/include/g++-v4",
2593 // Android standalone toolchain has C++ headers in yet another place.
2594 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002595 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2596 // without a subdirectory corresponding to the gcc version.
2597 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002598 };
2599
2600 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2601 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2602 GCCInstallation.getMultiarchSuffix()),
2603 DriverArgs, CC1Args))
2604 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002605 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002606}
2607
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002608bool Linux::isPIEDefault() const {
2609 return IsPIEDefault;
2610}
2611
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002612/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2613
Rafael Espindola0e659592012-02-19 01:38:32 +00002614DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2615 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002616
2617 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002618 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002619 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002620 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002621
Daniel Dunbaree788e72009-12-21 18:54:17 +00002622 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002623 getFilePaths().push_back("/usr/lib");
John McCall8cfb7202013-04-11 22:55:55 +00002624 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
2625 getFilePaths().push_back("/usr/lib/gcc47");
2626 else
2627 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002628}
2629
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002630Tool *DragonFly::buildAssembler() const {
2631 return new tools::dragonfly::Assemble(*this);
2632}
2633
2634Tool *DragonFly::buildLinker() const {
2635 return new tools::dragonfly::Link(*this);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002636}