blob: 0853006b121e4264652ee8b5497eabd08477da19 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000031#include "llvm/Support/system_error.h"
NAKAMURA Takumi55426352012-12-04 14:31:59 +000032
33// FIXME: This needs to be listed last until we fix the broken include guards
34// in these files and the LLVM config.h files.
35#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
36
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000037#include <cstdlib> // ::getenv
38
Daniel Dunbar39176082009-03-20 00:20:03 +000039using namespace clang::driver;
40using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000041using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000042using namespace llvm::opt;
Daniel Dunbar39176082009-03-20 00:20:03 +000043
Daniel Dunbarf3955282009-09-04 18:34:51 +000044/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000045
Rafael Espindolaaf370e62013-03-18 18:10:27 +000046Darwin::Darwin(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
47 : ToolChain(D, Triple, Args), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000048{
Bob Wilson10853772012-01-31 21:30:03 +000049 // Compute the initial Darwin version from the triple
50 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-01-31 22:43:59 +000051 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
52 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
53 Triple.getOSName();
54 llvm::raw_string_ostream(MacosxVersionMin)
55 << Major << '.' << Minor << '.' << Micro;
56
Bob Wilson10853772012-01-31 21:30:03 +000057 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
58 // It should be removed when we stop supporting that.
59 DarwinVersion[0] = Minor + 4;
60 DarwinVersion[1] = Micro;
61 DarwinVersion[2] = 0;
Chad Rosierc793ea42012-05-09 18:46:30 +000062
63 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000064 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000065 llvm::raw_string_ostream(iOSVersionMin)
66 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000067}
68
Daniel Dunbar41800112010-08-02 05:43:56 +000069types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
70 types::ID Ty = types::lookupTypeForExtension(Ext);
71
72 // Darwin always preprocesses assembly files (unless -x is used explicitly).
73 if (Ty == types::TY_PP_Asm)
74 return types::TY_Asm;
75
76 return Ty;
77}
78
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000079bool Darwin::HasNativeLLVMSupport() const {
80 return true;
81}
82
John McCall9f084a32011-07-06 00:26:06 +000083/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall260611a2012-06-20 06:18:46 +000084ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
Bob Wilson377e5c12012-11-09 01:59:30 +000085 if (isTargetIPhoneOS())
John McCall260611a2012-06-20 06:18:46 +000086 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson377e5c12012-11-09 01:59:30 +000087 if (isNonFragile)
88 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
89 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall9f084a32011-07-06 00:26:06 +000090}
91
John McCall13db5cf2011-09-09 20:41:01 +000092/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
93bool Darwin::hasBlocksRuntime() const {
94 if (isTargetIPhoneOS())
95 return !isIPhoneOSVersionLT(3, 2);
96 else
97 return !isMacosxVersionLT(10, 6);
98}
99
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000101 return llvm::StringSwitch<const char*>(Value)
102 .Case("armv6k", "armv6")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000103 .Case("armv6m", "armv6m")
Bob Wilsona59956b2011-10-07 00:37:57 +0000104 .Case("armv5tej", "armv5")
105 .Case("xscale", "xscale")
106 .Case("armv4t", "armv4t")
107 .Case("armv7", "armv7")
108 .Cases("armv7a", "armv7-a", "armv7")
109 .Cases("armv7r", "armv7-r", "armv7")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000110 .Cases("armv7em", "armv7e-m", "armv7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000111 .Cases("armv7f", "armv7-f", "armv7f")
112 .Cases("armv7k", "armv7-k", "armv7k")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000113 .Cases("armv7m", "armv7-m", "armv7m")
Bob Wilson336bfa32012-09-29 23:52:50 +0000114 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000115 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000116}
117
Chris Lattner5f9e2722011-07-23 10:55:15 +0000118static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000119 return llvm::StringSwitch<const char *>(Value)
120 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
121 .Cases("arm10e", "arm10tdmi", "armv5")
122 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
123 .Case("xscale", "xscale")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000124 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6")
125 .Case("cortex-m0", "armv6m")
126 .Cases("cortex-a8", "cortex-r4", "cortex-a9", "cortex-a15", "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000127 .Case("cortex-a9-mp", "armv7f")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000128 .Case("cortex-m3", "armv7m")
129 .Case("cortex-m4", "armv7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000130 .Case("swift", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000131 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000132}
133
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000135 switch (getTriple().getArch()) {
136 default:
137 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000138
Douglas Gregorf0594d82011-03-06 19:11:49 +0000139 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000140 case llvm::Triple::arm: {
141 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000142 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000143 return Arch;
144
145 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000146 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000147 return Arch;
148
149 return "arm";
150 }
151 }
152}
153
Daniel Dunbarf3955282009-09-04 18:34:51 +0000154Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000155}
156
Chad Rosier61ab80a2011-09-20 20:44:06 +0000157std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
158 types::ID InputType) const {
159 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000160
161 // If the target isn't initialized (e.g., an unknown Darwin platform, return
162 // the default triple).
163 if (!isTargetInitialized())
164 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000165
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000166 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000167 Str += isTargetIPhoneOS() ? "ios" : "macosx";
168 Str += getTargetVersion().getAsString();
169 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000170
171 return Triple.getTriple();
172}
173
David Blaikie99ba9e32011-12-20 02:48:34 +0000174void Generic_ELF::anchor() {}
175
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000176Tool *Darwin::getTool(Action::ActionClass AC) const {
Rafael Espindola5b222052013-03-18 20:48:54 +0000177 switch (AC) {
Rafael Espindolaf3260562013-03-18 18:50:01 +0000178 case Action::LipoJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000179 if (!Lipo)
180 Lipo.reset(new tools::darwin::Lipo(*this));
181 return Lipo.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +0000182 case Action::DsymutilJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000183 if (!Dsymutil)
184 Dsymutil.reset(new tools::darwin::Dsymutil(*this));
185 return Dsymutil.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +0000186 case Action::VerifyJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000187 if (!VerifyDebug)
188 VerifyDebug.reset(new tools::darwin::VerifyDebug(*this));
189 return VerifyDebug.get();
Rafael Espindolac0a55d12013-03-19 00:36:57 +0000190 default:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000191 return ToolChain::getTool(AC);
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000192 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000193}
194
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000195Tool *Darwin::buildLinker() const {
196 return new tools::darwin::Link(*this);
197}
198
199Tool *Darwin::buildAssembler() const {
200 return new tools::darwin::Assemble(*this);
201}
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000202
Rafael Espindolaaf370e62013-03-18 18:10:27 +0000203DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple,
204 const ArgList &Args)
205 : Darwin(D, Triple, Args)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000206{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000207 getProgramPaths().push_back(getDriver().getInstalledDir());
208 if (getDriver().getInstalledDir() != getDriver().Dir)
209 getProgramPaths().push_back(getDriver().Dir);
210
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000211 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000212 getProgramPaths().push_back(getDriver().getInstalledDir());
213 if (getDriver().getInstalledDir() != getDriver().Dir)
214 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000215}
216
John McCallf85e1932011-06-15 23:02:42 +0000217void DarwinClang::AddLinkARCArgs(const ArgList &Args,
218 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000219
220 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000221 llvm::sys::Path P(getDriver().ClangExecutable);
222 P.eraseComponent(); // 'clang'
223 P.eraseComponent(); // 'bin'
224 P.appendComponent("lib");
225 P.appendComponent("arc");
226 P.appendComponent("libarclite_");
227 std::string s = P.str();
228 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000229 if (isTargetIOSSimulator())
230 s += "iphonesimulator";
231 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000232 s += "iphoneos";
John McCallf85e1932011-06-15 23:02:42 +0000233 else
234 s += "macosx";
235 s += ".a";
236
237 CmdArgs.push_back(Args.MakeArgString(s));
238}
239
Eric Christopher3404fe72011-06-22 17:41:40 +0000240void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000241 ArgStringList &CmdArgs,
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000242 const char *DarwinStaticLib,
243 bool AlwaysLink) const {
Eric Christopher3404fe72011-06-22 17:41:40 +0000244 llvm::sys::Path P(getDriver().ResourceDir);
245 P.appendComponent("lib");
246 P.appendComponent("darwin");
247 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000248
Eric Christopher3404fe72011-06-22 17:41:40 +0000249 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000250 // not have compiler-rt checked out or integrated into their build (unless
251 // we explicitly force linking with this library).
Eric Christopher3404fe72011-06-22 17:41:40 +0000252 bool Exists;
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000253 if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
Eric Christopher3404fe72011-06-22 17:41:40 +0000254 CmdArgs.push_back(Args.MakeArgString(P.str()));
255}
256
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000257void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
258 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000259 // Darwin only supports the compiler-rt based runtime libraries.
260 switch (GetRuntimeLibType(Args)) {
261 case ToolChain::RLT_CompilerRT:
262 break;
263 default:
264 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000265 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000266 return;
267 }
268
Daniel Dunbareec99102010-01-22 03:38:14 +0000269 // Darwin doesn't support real static executables, don't link any runtime
270 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000271 if (Args.hasArg(options::OPT_static) ||
272 Args.hasArg(options::OPT_fapple_kext) ||
273 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000274 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000275
276 // Reject -static-libgcc for now, we can deal with this when and if someone
277 // cares. This is useful in situations where someone wants to statically link
278 // something like libstdc++, and needs its runtime support routines.
279 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000281 << A->getAsString(Args);
282 return;
283 }
284
Daniel Dunbarf4714872011-11-17 00:36:57 +0000285 // If we are building profile support, link that library in.
286 if (Args.hasArg(options::OPT_fprofile_arcs) ||
287 Args.hasArg(options::OPT_fprofile_generate) ||
288 Args.hasArg(options::OPT_fcreate_profile) ||
289 Args.hasArg(options::OPT_coverage)) {
290 // Select the appropriate runtime library for the target.
291 if (isTargetIPhoneOS()) {
292 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
293 } else {
294 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
295 }
296 }
297
Peter Collingbourne52ca70d2013-04-09 04:35:11 +0000298 SanitizerArgs Sanitize(*this, Args);
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000299
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000300 // Add Ubsan runtime library, if required.
301 if (Sanitize.needsUbsanRt()) {
Alexey Samsonov2cb3d302013-01-21 08:45:02 +0000302 if (isTargetIPhoneOS()) {
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000303 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
304 << "-fsanitize=undefined";
305 } else {
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000306 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000307
308 // The Ubsan runtime library requires C++.
309 AddCXXStdlibLibArgs(Args, CmdArgs);
310 }
311 }
312
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000313 // Add ASAN runtime library, if required. Dynamic libraries and bundles
314 // should not be linked with the runtime library.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000315 if (Sanitize.needsAsanRt()) {
Alexander Potapenko3656c612013-03-21 10:49:06 +0000316 if (isTargetIPhoneOS() && !isTargetIOSSimulator()) {
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000317 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000318 << "-fsanitize=address";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000319 } else {
Alexander Potapenko3656c612013-03-21 10:49:06 +0000320 if (Args.hasArg(options::OPT_dynamiclib) ||
321 Args.hasArg(options::OPT_bundle)) {
322 // Assume the binary will provide the ASan runtime.
323 } else {
324 AddLinkRuntimeLib(Args, CmdArgs,
325 "libclang_rt.asan_osx_dynamic.dylib", true);
326 // The ASAN runtime library requires C++.
327 AddCXXStdlibLibArgs(Args, CmdArgs);
328 }
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000329 }
330 }
331
Daniel Dunbareec99102010-01-22 03:38:14 +0000332 // Otherwise link libSystem, then the dynamic runtime library, and finally any
333 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000334 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000335
336 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000337 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000338 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
339 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000340 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
341 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
342 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000343
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000344 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000345 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000346 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000347 // The dynamic runtime library was merged with libSystem for 10.6 and
348 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000349 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000350 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000351 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000352 CmdArgs.push_back("-lgcc_s.10.5");
353
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000354 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000355 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000356 // omitted from 10.4.dylib.
357 //
358 // Unfortunately, that turned out to not be true, because Darwin system
359 // headers can still use eprintf on i386, and it is not exported from
360 // libSystem. Therefore, we still must provide a runtime library just for
361 // the tiny tiny handful of projects that *might* use that symbol.
362 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000363 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000364 } else {
365 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000366 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
367 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000368 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000369 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000370}
371
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000372void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000373 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000374
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000375 // Support allowing the SDKROOT environment variable used by xcrun and other
376 // Xcode tools to define the default sysroot, by making it the default for
377 // isysroot.
Chad Rosierd7dfd982012-12-19 23:41:50 +0000378 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
379 // Warn if the path does not exist.
380 bool Exists;
381 if (llvm::sys::fs::exists(A->getValue(), Exists) || !Exists)
382 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
383 } else {
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000384 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbar32142542013-01-15 20:33:56 +0000385 // We only use this value as the default if it is an absolute path,
386 // exists, and it is not the root path.
387 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
388 StringRef(env) != "/") {
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000389 Args.append(Args.MakeSeparateArg(
390 0, Opts.getOption(options::OPT_isysroot), env));
391 }
392 }
393 }
394
Daniel Dunbar26031372010-01-27 00:56:25 +0000395 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000396 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
397 Arg *iOSSimVersion = Args.getLastArg(
398 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000399
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000400 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000401 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000402 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000403 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
404 iOSVersion = iOSSimVersion = 0;
405 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000406 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000407 << iOSVersion->getAsString(Args)
408 << iOSSimVersion->getAsString(Args);
409 iOSSimVersion = 0;
410 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000411 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000412 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000413 StringRef OSXTarget;
414 StringRef iOSTarget;
415 StringRef iOSSimTarget;
416 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
417 OSXTarget = env;
418 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
419 iOSTarget = env;
420 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
421 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000422
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000423 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000424 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000425 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000426 if (iOSTarget.empty()) {
427 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
428 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000429 StringRef isysroot = A->getValue();
Chad Rosiera4884972011-08-31 20:56:25 +0000430 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
431 if (second != "")
432 iOSTarget = second.substr(0,3);
433 }
434 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000435
Chad Rosier4f8de272011-09-28 00:46:32 +0000436 // If no OSX or iOS target has been specified and we're compiling for armv7,
437 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000438 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000439 (getDarwinArchName(Args) == "armv7" ||
440 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000441 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000442
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000443 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000444 //
445 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000446
447 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000448 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000449 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000450 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000451 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000452 "IPHONEOS_DEPLOYMENT_TARGET");
453 }
454
455 // Allow conflicts among OSX and iOS for historical reasons, but choose the
456 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000457 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000458 if (getTriple().getArch() == llvm::Triple::arm ||
459 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000460 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000461 else
Chad Rosiera4884972011-08-31 20:56:25 +0000462 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000463 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000464
Chad Rosiera4884972011-08-31 20:56:25 +0000465 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000466 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000467 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
468 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000469 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000470 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000471 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
472 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000473 } else if (!iOSSimTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000474 const Option O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000475 options::OPT_mios_simulator_version_min_EQ);
476 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
477 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000478 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000479 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000480 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000481 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
482 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000483 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000486 // Reject invalid architecture combinations.
487 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
488 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000489 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000490 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
491 }
492
Daniel Dunbar26031372010-01-27 00:56:25 +0000493 // Set the tool chain target information.
494 unsigned Major, Minor, Micro;
495 bool HadExtra;
496 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000497 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000498 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000499 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000500 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000501 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000502 << OSXVersion->getAsString(Args);
503 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000504 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
505 assert(Version && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000506 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000507 Micro, HadExtra) || HadExtra ||
508 Major >= 10 || Minor >= 100 || Micro >= 100)
509 getDriver().Diag(diag::err_drv_invalid_version_number)
510 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000511 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000512
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000513 bool IsIOSSim = bool(iOSSimVersion);
514
515 // In GCC, the simulator historically was treated as being OS X in some
516 // contexts, like determining the link logic, despite generally being called
517 // with an iOS deployment target. For compatibility, we detect the
518 // simulator as iOS + x86, and treat it differently in a few contexts.
519 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
520 getTriple().getArch() == llvm::Triple::x86_64))
521 IsIOSSim = true;
522
523 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000524}
525
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000526void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000527 ArgStringList &CmdArgs) const {
528 CXXStdlibType Type = GetCXXStdlibType(Args);
529
530 switch (Type) {
531 case ToolChain::CST_Libcxx:
532 CmdArgs.push_back("-lc++");
533 break;
534
535 case ToolChain::CST_Libstdcxx: {
536 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
537 // it was previously found in the gcc lib dir. However, for all the Darwin
538 // platforms we care about it was -lstdc++.6, so we search for that
539 // explicitly if we can't see an obvious -lstdc++ candidate.
540
541 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000542 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000543 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000544 llvm::sys::Path P(A->getValue());
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000545 P.appendComponent("usr");
546 P.appendComponent("lib");
547 P.appendComponent("libstdc++.dylib");
548
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000549 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000550 P.eraseComponent();
551 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000552 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000553 CmdArgs.push_back(Args.MakeArgString(P.str()));
554 return;
555 }
556 }
557 }
558
559 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000560 // FIXME: This should be removed someday when we don't have to care about
561 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000562 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
563 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000564 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
565 return;
566 }
567
568 // Otherwise, let the linker search.
569 CmdArgs.push_back("-lstdc++");
570 break;
571 }
572 }
573}
574
Shantonu Sen7433fed2010-09-17 18:39:08 +0000575void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
576 ArgStringList &CmdArgs) const {
577
578 // For Darwin platforms, use the compiler-rt-based support library
579 // instead of the gcc-provided one (which is also incidentally
580 // only present in the gcc lib dir, which makes it hard to find).
581
582 llvm::sys::Path P(getDriver().ResourceDir);
583 P.appendComponent("lib");
584 P.appendComponent("darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000585
586 // Use the newer cc_kext for iOS ARM after 6.0.
587 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
588 !isIPhoneOSVersionLT(6, 0)) {
589 P.appendComponent("libclang_rt.cc_kext.a");
590 } else {
591 P.appendComponent("libclang_rt.cc_kext_ios5.a");
592 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000593
Shantonu Sen7433fed2010-09-17 18:39:08 +0000594 // For now, allow missing resource libraries to support developers who may
595 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000596 bool Exists;
597 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000598 CmdArgs.push_back(Args.MakeArgString(P.str()));
599}
600
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000601DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
602 const char *BoundArch) const {
603 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
604 const OptTable &Opts = getDriver().getOpts();
605
606 // FIXME: We really want to get out of the tool chain level argument
607 // translation business, as it makes the driver functionality much
608 // more opaque. For now, we follow gcc closely solely for the
609 // purpose of easily achieving feature parity & testability. Once we
610 // have something that works, we should reevaluate each translation
611 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000612
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000613 for (ArgList::const_iterator it = Args.begin(),
614 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000615 Arg *A = *it;
616
617 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000618 // Skip this argument unless the architecture matches either the toolchain
619 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000620 llvm::Triple::ArchType XarchArch =
Richard Smith1d489cf2012-11-01 04:30:05 +0000621 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000622 if (!(XarchArch == getArch() ||
623 (BoundArch && XarchArch ==
Rafael Espindolacfed8282012-10-31 18:51:07 +0000624 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000625 continue;
626
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000627 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000628 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000629 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000630 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000632 // If the argument parsing failed or more than one argument was
633 // consumed, the -Xarch_ argument's parameter tried to consume
634 // extra arguments. Emit an error and ignore.
635 //
636 // We also want to disallow any options which would alter the
637 // driver behavior; that isn't going to work in our model. We
638 // use isDriverOption() as an approximation, although things
639 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000640 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000641 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000642 << A->getAsString(Args);
643 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000644 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000645 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000646 << A->getAsString(Args);
647 continue;
648 }
649
Daniel Dunbar478edc22009-03-29 22:29:05 +0000650 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000651 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000652
653 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000654
655 // Linker input arguments require custom handling. The problem is that we
656 // have already constructed the phase actions, so we can not treat them as
657 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000658 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000659 // Convert the argument into individual Zlinker_input_args.
660 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
661 DAL->AddSeparateArg(OriginalArg,
662 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000663 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000664
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000665 }
666 continue;
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000669
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000670 // Sob. These is strictly gcc compatible for the time being. Apple
671 // gcc translates options twice, which means that self-expanding
672 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000673 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000674 default:
675 DAL->append(A);
676 break;
677
678 case options::OPT_mkernel:
679 case options::OPT_fapple_kext:
680 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000681 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000682 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000684 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000685 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000686 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000687 break;
688
689 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000690 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
691 DAL->AddFlagArg(A,
692 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000693 break;
694
695 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000696 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
697 DAL->AddFlagArg(A,
698 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000699 break;
700
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000701 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000702 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000703 break;
704
705 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000706 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000707 break;
708
709 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000710 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000711 break;
712
713 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000714 DAL->AddFlagArg(A,
715 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000716 break;
717
718 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000719 DAL->AddFlagArg(A,
720 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000721 break;
722
723 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000724 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000725 break;
726
727 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000728 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000729 break;
730 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000731 }
732
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000733 if (getTriple().getArch() == llvm::Triple::x86 ||
734 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000735 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000736 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000737
738 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000739 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000740 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000741 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000742 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
743 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000744
745 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
746 // which defines the list of which architectures we accept.
747 if (Name == "ppc")
748 ;
749 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000750 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000751 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000752 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000753 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000754 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000755 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000756 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000757 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000758 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000759 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000760 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000761 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000762 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000763 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000764 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000765
766 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000767 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000768
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000769 else if (Name == "i386")
770 ;
771 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000772 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000773 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000774 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000775 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000776 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000777 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000778 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000779 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000780 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000781 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000782 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000783 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000784 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000785
786 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000787 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000788
789 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000790 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000791 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000792 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000793 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000794 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000795 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000796 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000797 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000798 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000799 else if (Name == "armv6m")
800 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000801 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000802 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000803 else if (Name == "armv7em")
804 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilson336bfa32012-09-29 23:52:50 +0000805 else if (Name == "armv7f")
806 DAL->AddJoinedArg(0, MArch, "armv7f");
807 else if (Name == "armv7k")
808 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000809 else if (Name == "armv7m")
810 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilson336bfa32012-09-29 23:52:50 +0000811 else if (Name == "armv7s")
812 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000813
814 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000815 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000816 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000817
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000818 // Add an explicit version min argument for the deployment target. We do this
819 // after argument translation because -Xarch_ arguments may add a version min
820 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000821 if (BoundArch)
822 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000823
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000824 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
825 // FIXME: It would be far better to avoid inserting those -static arguments,
826 // but we can't check the deployment target in the translation code until
827 // it is set here.
828 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
829 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
830 Arg *A = *it;
831 ++it;
832 if (A->getOption().getID() != options::OPT_mkernel &&
833 A->getOption().getID() != options::OPT_fapple_kext)
834 continue;
835 assert(it != ie && "unexpected argument translation");
836 A = *it;
837 assert(A->getOption().getID() == options::OPT_static &&
838 "missing expected -static argument");
839 it = DAL->getArgs().erase(it);
840 }
841 }
842
Bob Wilson163b1512011-10-07 17:54:41 +0000843 // Validate the C++ standard library choice.
844 CXXStdlibType Type = GetCXXStdlibType(*DAL);
845 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000846 // Check whether the target provides libc++.
847 StringRef where;
848
849 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson377e5c12012-11-09 01:59:30 +0000850 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
851 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000852
853 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000854 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000855 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000856 }
857 }
858
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000859 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000860}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000861
Daniel Dunbarf3955282009-09-04 18:34:51 +0000862bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000863 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000864}
865
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000866bool Darwin::UseDwarfDebugFlags() const {
867 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
868 return S[0] != '\0';
869 return false;
870}
871
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000872bool Darwin::UseSjLjExceptions() const {
873 // Darwin uses SjLj exceptions on ARM.
874 return (getTriple().getArch() == llvm::Triple::arm ||
875 getTriple().getArch() == llvm::Triple::thumb);
876}
877
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000878bool Darwin::isPICDefault() const {
879 return true;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000880}
881
Peter Collingbourne52ca70d2013-04-09 04:35:11 +0000882bool Darwin::isPIEDefault() const {
883 return false;
884}
885
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000886bool Darwin::isPICDefaultForced() const {
887 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000888}
889
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000890bool Darwin::SupportsProfiling() const {
891 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000892 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000893}
894
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000895bool Darwin::SupportsObjCGC() const {
896 // Garbage collection is supported everywhere except on iPhone OS.
897 return !isTargetIPhoneOS();
898}
899
John McCall0a7dd782012-08-21 02:47:43 +0000900void Darwin::CheckObjCARC() const {
901 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
902 return;
John McCall80fd37a2012-08-27 01:56:21 +0000903 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000904}
905
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000906std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000907Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
908 types::ID InputType) const {
909 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000910}
911
Daniel Dunbar39176082009-03-20 00:20:03 +0000912/// Generic_GCC - A tool chain using the 'gcc' command to perform
913/// all subcommands; this relies on gcc translating the majority of
914/// command line options.
915
Chandler Carruth19347ed2011-11-06 23:39:34 +0000916/// \brief Parse a GCCVersion object out of a string of text.
917///
918/// This is the primary means of forming GCCVersion objects.
919/*static*/
920Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
921 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
922 std::pair<StringRef, StringRef> First = VersionText.split('.');
923 std::pair<StringRef, StringRef> Second = First.second.split('.');
924
925 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
926 if (First.first.getAsInteger(10, GoodVersion.Major) ||
927 GoodVersion.Major < 0)
928 return BadVersion;
929 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
930 GoodVersion.Minor < 0)
931 return BadVersion;
932
933 // First look for a number prefix and parse that if present. Otherwise just
934 // stash the entire patch string in the suffix, and leave the number
935 // unspecified. This covers versions strings such as:
936 // 4.4
937 // 4.4.0
938 // 4.4.x
939 // 4.4.2-rc4
940 // 4.4.x-patched
941 // And retains any patch number it finds.
942 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
943 if (!PatchText.empty()) {
Will Dietzca1ad502013-01-10 22:20:02 +0000944 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth19347ed2011-11-06 23:39:34 +0000945 // Try to parse the number and any suffix.
946 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
947 GoodVersion.Patch < 0)
948 return BadVersion;
949 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
950 }
951 }
952
953 return GoodVersion;
954}
955
956/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
957bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
Chandler Carruthf1757652012-12-29 12:01:08 +0000958 if (Major != RHS.Major)
959 return Major < RHS.Major;
960 if (Minor != RHS.Minor)
961 return Minor < RHS.Minor;
962 if (Patch != RHS.Patch) {
963 // Note that versions without a specified patch sort higher than those with
964 // a patch.
965 if (RHS.Patch == -1)
966 return true;
967 if (Patch == -1)
968 return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000969
Chandler Carruthf1757652012-12-29 12:01:08 +0000970 // Otherwise just sort on the patch itself.
971 return Patch < RHS.Patch;
972 }
973 if (PatchSuffix != RHS.PatchSuffix) {
974 // Sort empty suffixes higher.
975 if (RHS.PatchSuffix.empty())
976 return true;
977 if (PatchSuffix.empty())
Chandler Carruth5ba0c8e2012-12-29 13:00:47 +0000978 return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000979
Chandler Carruthf1757652012-12-29 12:01:08 +0000980 // Provide a lexicographic sort to make this a total ordering.
981 return PatchSuffix < RHS.PatchSuffix;
982 }
983
984 // The versions are equal.
Chandler Carruth19347ed2011-11-06 23:39:34 +0000985 return false;
986}
987
Rafael Espindola0e659592012-02-19 01:38:32 +0000988static StringRef getGCCToolchainDir(const ArgList &Args) {
989 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
990 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +0000991 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +0000992 return GCC_INSTALL_PREFIX;
993}
994
Chandler Carruth19347ed2011-11-06 23:39:34 +0000995/// \brief Construct a GCCInstallationDetector from the driver.
996///
997/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +0000998/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +0000999///
1000/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1001/// should instead pull the target out of the driver. This is currently
1002/// necessary because the driver doesn't store the final version of the target
1003/// triple.
1004Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1005 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001006 const llvm::Triple &TargetTriple,
1007 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001008 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001009 llvm::Triple MultiarchTriple
1010 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1011 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001012 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001013 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001014 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001015 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001016 SmallVector<StringRef, 10> CandidateTripleAliases;
1017 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1018 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1019 CandidateTripleAliases,
1020 CandidateMultiarchLibDirs,
1021 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001022
1023 // Compute the set of prefixes for our search.
1024 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1025 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001026
Rafael Espindola0e659592012-02-19 01:38:32 +00001027 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1028 if (GCCToolchainDir != "") {
1029 if (GCCToolchainDir.back() == '/')
1030 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001031
Rafael Espindola0e659592012-02-19 01:38:32 +00001032 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001033 } else {
1034 Prefixes.push_back(D.SysRoot);
1035 Prefixes.push_back(D.SysRoot + "/usr");
1036 Prefixes.push_back(D.InstalledDir + "/..");
1037 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001038
1039 // Loop over the various components which exist and select the best GCC
1040 // installation available. GCC installs are ranked by version number.
1041 Version = GCCVersion::Parse("0.0.0");
1042 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1043 if (!llvm::sys::fs::exists(Prefixes[i]))
1044 continue;
1045 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1046 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1047 if (!llvm::sys::fs::exists(LibDir))
1048 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001049 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001050 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1051 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001052 }
1053 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1054 const std::string LibDir
1055 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1056 if (!llvm::sys::fs::exists(LibDir))
1057 continue;
1058 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1059 ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001060 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001061 CandidateMultiarchTripleAliases[k],
1062 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001063 }
1064 }
1065}
1066
1067/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001068 const llvm::Triple &TargetTriple,
1069 const llvm::Triple &MultiarchTriple,
1070 SmallVectorImpl<StringRef> &LibDirs,
1071 SmallVectorImpl<StringRef> &TripleAliases,
1072 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1073 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1074 // Declare a bunch of static data sets that we'll select between below. These
1075 // are specifically designed to always refer to string literals to avoid any
1076 // lifetime or initialization issues.
Tim Northoverc264e162013-01-31 12:13:10 +00001077 static const char *const AArch64LibDirs[] = { "/lib" };
1078 static const char *const AArch64Triples[] = {
1079 "aarch64-none-linux-gnu",
1080 "aarch64-linux-gnu"
1081 };
1082
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001083 static const char *const ARMLibDirs[] = { "/lib" };
1084 static const char *const ARMTriples[] = {
1085 "arm-linux-gnueabi",
1086 "arm-linux-androideabi"
1087 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001088 static const char *const ARMHFTriples[] = {
1089 "arm-linux-gnueabihf",
Rafael Espindolad44d04f2013-04-14 10:14:21 +00001090 "armv7hl-redhat-linux-gnueabi"
Jiangning Liuff104a12012-07-31 08:06:29 +00001091 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001092
1093 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1094 static const char *const X86_64Triples[] = {
1095 "x86_64-linux-gnu",
1096 "x86_64-unknown-linux-gnu",
1097 "x86_64-pc-linux-gnu",
1098 "x86_64-redhat-linux6E",
1099 "x86_64-redhat-linux",
1100 "x86_64-suse-linux",
1101 "x86_64-manbo-linux-gnu",
1102 "x86_64-linux-gnu",
1103 "x86_64-slackware-linux"
1104 };
1105 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1106 static const char *const X86Triples[] = {
1107 "i686-linux-gnu",
1108 "i686-pc-linux-gnu",
1109 "i486-linux-gnu",
1110 "i386-linux-gnu",
NAKAMURA Takumi2a907f82012-12-07 17:13:18 +00001111 "i386-redhat-linux6E",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001112 "i686-redhat-linux",
1113 "i586-redhat-linux",
1114 "i386-redhat-linux",
1115 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001116 "i486-slackware-linux",
1117 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001118 };
1119
1120 static const char *const MIPSLibDirs[] = { "/lib" };
1121 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1122 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001123 static const char *const MIPSELTriples[] = {
1124 "mipsel-linux-gnu",
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001125 "mipsel-linux-android",
1126 "mips-linux-gnu"
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001127 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001128
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001129 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1130 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1131 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1132 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1133
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001134 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1135 static const char *const PPCTriples[] = {
1136 "powerpc-linux-gnu",
1137 "powerpc-unknown-linux-gnu",
Sylvestre Ledrub7e86be2013-03-15 16:22:43 +00001138 "powerpc-linux-gnuspe",
Gabor Greif91720912012-05-15 11:21:03 +00001139 "powerpc-suse-linux",
1140 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001141 };
1142 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1143 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001144 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001145 "powerpc64-unknown-linux-gnu",
1146 "powerpc64-suse-linux",
1147 "ppc64-redhat-linux"
1148 };
1149
Ulrich Weigandb8409212013-05-06 16:26:41 +00001150 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1151 static const char *const SystemZTriples[] = {
1152 "s390x-linux-gnu",
1153 "s390x-unknown-linux-gnu",
1154 "s390x-ibm-linux-gnu",
1155 "s390x-suse-linux",
1156 "s390x-redhat-linux"
1157 };
1158
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001159 switch (TargetTriple.getArch()) {
Tim Northoverc264e162013-01-31 12:13:10 +00001160 case llvm::Triple::aarch64:
Eric Christopher4f4e2af2013-02-05 07:29:49 +00001161 LibDirs.append(AArch64LibDirs, AArch64LibDirs
Tim Northoverc264e162013-01-31 12:13:10 +00001162 + llvm::array_lengthof(AArch64LibDirs));
1163 TripleAliases.append(
1164 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1165 MultiarchLibDirs.append(
1166 AArch64LibDirs, AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1167 MultiarchTripleAliases.append(
1168 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1169 break;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001170 case llvm::Triple::arm:
1171 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001172 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001173 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1174 TripleAliases.append(
1175 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1176 } else {
1177 TripleAliases.append(
1178 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1179 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001180 break;
1181 case llvm::Triple::x86_64:
1182 LibDirs.append(
1183 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1184 TripleAliases.append(
1185 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1186 MultiarchLibDirs.append(
1187 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1188 MultiarchTripleAliases.append(
1189 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1190 break;
1191 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001192 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001193 TripleAliases.append(
1194 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1195 MultiarchLibDirs.append(
1196 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1197 MultiarchTripleAliases.append(
1198 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1199 break;
1200 case llvm::Triple::mips:
1201 LibDirs.append(
1202 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1203 TripleAliases.append(
1204 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001205 MultiarchLibDirs.append(
1206 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1207 MultiarchTripleAliases.append(
1208 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001209 break;
1210 case llvm::Triple::mipsel:
1211 LibDirs.append(
1212 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1213 TripleAliases.append(
1214 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001215 MultiarchLibDirs.append(
1216 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1217 MultiarchTripleAliases.append(
1218 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1219 break;
1220 case llvm::Triple::mips64:
1221 LibDirs.append(
1222 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1223 TripleAliases.append(
1224 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1225 MultiarchLibDirs.append(
1226 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1227 MultiarchTripleAliases.append(
1228 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1229 break;
1230 case llvm::Triple::mips64el:
1231 LibDirs.append(
1232 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1233 TripleAliases.append(
1234 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1235 MultiarchLibDirs.append(
1236 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1237 MultiarchTripleAliases.append(
1238 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001239 break;
1240 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001241 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001242 TripleAliases.append(
1243 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1244 MultiarchLibDirs.append(
1245 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1246 MultiarchTripleAliases.append(
1247 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1248 break;
1249 case llvm::Triple::ppc64:
1250 LibDirs.append(
1251 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1252 TripleAliases.append(
1253 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1254 MultiarchLibDirs.append(
1255 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1256 MultiarchTripleAliases.append(
1257 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1258 break;
Ulrich Weigandb8409212013-05-06 16:26:41 +00001259 case llvm::Triple::systemz:
1260 LibDirs.append(
1261 SystemZLibDirs, SystemZLibDirs + llvm::array_lengthof(SystemZLibDirs));
1262 TripleAliases.append(
1263 SystemZTriples, SystemZTriples + llvm::array_lengthof(SystemZTriples));
1264 break;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001265
1266 default:
1267 // By default, just rely on the standard lib directories and the original
1268 // triple.
1269 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001270 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001271
1272 // Always append the drivers target triple to the end, in case it doesn't
1273 // match any of our aliases.
1274 TripleAliases.push_back(TargetTriple.str());
1275
1276 // Also include the multiarch variant if it's different.
1277 if (TargetTriple.str() != MultiarchTriple.str())
1278 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001279}
1280
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001281static bool isSoftFloatABI(const ArgList &Args) {
1282 Arg *A = Args.getLastArg(options::OPT_msoft_float,
1283 options::OPT_mhard_float,
1284 options::OPT_mfloat_abi_EQ);
1285 if (!A) return false;
1286
1287 return A->getOption().matches(options::OPT_msoft_float) ||
1288 (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
1289 A->getValue() == StringRef("soft"));
1290}
1291
1292static bool isMipsArch(llvm::Triple::ArchType Arch) {
1293 return Arch == llvm::Triple::mips ||
1294 Arch == llvm::Triple::mipsel ||
1295 Arch == llvm::Triple::mips64 ||
1296 Arch == llvm::Triple::mips64el;
1297}
1298
1299static bool isMips16(const ArgList &Args) {
1300 Arg *A = Args.getLastArg(options::OPT_mips16,
1301 options::OPT_mno_mips16);
1302 return A && A->getOption().matches(options::OPT_mips16);
1303}
1304
1305static bool isMicroMips(const ArgList &Args) {
1306 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1307 options::OPT_mno_micromips);
1308 return A && A->getOption().matches(options::OPT_mmicromips);
1309}
1310
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001311// FIXME: There is the same routine in the Tools.cpp.
1312static bool hasMipsN32ABIArg(const ArgList &Args) {
1313 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00001314 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001315}
1316
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001317static void appendMipsTargetSuffix(std::string &Path,
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001318 llvm::Triple::ArchType TargetArch,
1319 const ArgList &Args) {
1320 if (isMips16(Args))
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001321 Path += "/mips16";
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001322 else if (isMicroMips(Args))
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001323 Path += "/micromips";
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001324
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001325 if (isSoftFloatABI(Args))
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001326 Path += "/soft-float";
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001327
1328 if (TargetArch == llvm::Triple::mipsel ||
1329 TargetArch == llvm::Triple::mips64el)
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001330 Path += "/el";
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001331}
1332
1333static StringRef getMipsTargetABISuffix(llvm::Triple::ArchType TargetArch,
1334 const ArgList &Args) {
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001335 if (TargetArch == llvm::Triple::mips64 ||
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001336 TargetArch == llvm::Triple::mips64el)
1337 return hasMipsN32ABIArg(Args) ? "/n32" : "/64";
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001338
1339 return "/32";
1340}
1341
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001342static bool findTargetMultiarchSuffix(std::string &Suffix,
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001343 StringRef Path,
1344 llvm::Triple::ArchType TargetArch,
1345 const ArgList &Args) {
1346 if (isMipsArch(TargetArch)) {
1347 StringRef ABISuffix = getMipsTargetABISuffix(TargetArch, Args);
1348
1349 // First build and check a complex path to crtbegin.o
1350 // depends on command line options (-mips16, -msoft-float, ...)
1351 // like mips-linux-gnu/4.7/mips16/soft-float/el/crtbegin.o
1352 appendMipsTargetSuffix(Suffix, TargetArch, Args);
1353
1354 if (TargetArch == llvm::Triple::mips64 ||
1355 TargetArch == llvm::Triple::mips64el)
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001356 Suffix += ABISuffix;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001357
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001358 if (llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o"))
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001359 return true;
1360
1361 // Then fall back and probe a simple case like
1362 // mips-linux-gnu/4.7/32/crtbegin.o
1363 Suffix = ABISuffix;
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001364 return llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o");
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001365 }
1366
1367 if (TargetArch == llvm::Triple::x86_64 ||
Ulrich Weigandb8409212013-05-06 16:26:41 +00001368 TargetArch == llvm::Triple::ppc64 ||
1369 TargetArch == llvm::Triple::systemz)
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001370 Suffix = "/64";
1371 else
1372 Suffix = "/32";
1373
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001374 return llvm::sys::fs::exists(Path + Suffix + "/crtbegin.o");
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001375}
1376
Chandler Carruth19347ed2011-11-06 23:39:34 +00001377void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001378 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1379 const std::string &LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001380 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001381 // There are various different suffixes involving the triple we
1382 // check for. We also record what is necessary to walk from each back
1383 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001384 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001385 "/gcc/" + CandidateTriple.str(),
1386 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1387
Hal Finkel02014b42012-09-18 22:25:07 +00001388 // The Freescale PPC SDK has the gcc libraries in
1389 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1390 "/" + CandidateTriple.str(),
1391
Chandler Carruth19347ed2011-11-06 23:39:34 +00001392 // Ubuntu has a strange mis-matched pair of triples that this happens to
1393 // match.
1394 // FIXME: It may be worthwhile to generalize this and look for a second
1395 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001396 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001397 };
1398 const std::string InstallSuffixes[] = {
1399 "/../../..",
1400 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001401 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001402 "/../../../.."
1403 };
1404 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001405 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1406 (TargetArch != llvm::Triple::x86));
1407 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1408 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001409 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001410 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001411 !EC && LI != LE; LI = LI.increment(EC)) {
1412 StringRef VersionText = llvm::sys::path::filename(LI->path());
1413 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1414 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1415 if (CandidateVersion < MinVersion)
1416 continue;
1417 if (CandidateVersion <= Version)
1418 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001419
1420 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001421 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001422 // libs in a subdirectory named 64. The simple logic we follow is that
1423 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1424 // we use that. If not, and if not a multiarch triple, we look for
1425 // crtbegin.o without the subdirectory.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001426
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001427 std::string MultiarchSuffix;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001428 if (findTargetMultiarchSuffix(MultiarchSuffix,
1429 LI->path(), TargetArch, Args)) {
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00001430 GCCMultiarchSuffix = MultiarchSuffix;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001431 } else {
1432 if (NeedsMultiarchSuffix ||
1433 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1434 continue;
1435 GCCMultiarchSuffix.clear();
1436 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001437
1438 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001439 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001440 // FIXME: We hack together the directory name here instead of
1441 // using LI to ensure stable path separators across Windows and
1442 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001443 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001444 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001445 IsValid = true;
1446 }
1447 }
1448}
1449
Rafael Espindola0e659592012-02-19 01:38:32 +00001450Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1451 const ArgList &Args)
Rafael Espindolaaf370e62013-03-18 18:10:27 +00001452 : ToolChain(D, Triple, Args), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001453 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001454 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001455 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001456}
1457
Daniel Dunbar39176082009-03-20 00:20:03 +00001458Generic_GCC::~Generic_GCC() {
Daniel Dunbar39176082009-03-20 00:20:03 +00001459}
1460
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001461Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola5b222052013-03-18 20:48:54 +00001462 switch (AC) {
Rafael Espindolaf3260562013-03-18 18:50:01 +00001463 case Action::PreprocessJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001464 if (!Preprocess)
1465 Preprocess.reset(new tools::gcc::Preprocess(*this));
1466 return Preprocess.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +00001467 case Action::PrecompileJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001468 if (!Precompile)
1469 Precompile.reset(new tools::gcc::Precompile(*this));
1470 return Precompile.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +00001471 case Action::CompileJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001472 if (!Compile)
1473 Compile.reset(new tools::gcc::Compile(*this));
1474 return Compile.get();
Rafael Espindolac0a55d12013-03-19 00:36:57 +00001475 default:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001476 return ToolChain::getTool(AC);
Daniel Dunbar39176082009-03-20 00:20:03 +00001477 }
Daniel Dunbar39176082009-03-20 00:20:03 +00001478}
1479
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001480Tool *Generic_GCC::buildAssembler() const {
1481 return new tools::gcc::Assemble(*this);
1482}
1483
1484Tool *Generic_GCC::buildLinker() const {
1485 return new tools::gcc::Link(*this);
1486}
1487
Daniel Dunbar39176082009-03-20 00:20:03 +00001488bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00001489 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00001490}
1491
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001492bool Generic_GCC::isPICDefault() const {
1493 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00001494}
1495
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00001496bool Generic_GCC::isPIEDefault() const {
1497 return false;
1498}
1499
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001500bool Generic_GCC::isPICDefaultForced() const {
1501 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00001502}
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001503
Tony Linthicum96319392011-12-12 21:14:55 +00001504/// Hexagon Toolchain
1505
Matthew Curtisb3489a02012-12-06 12:43:18 +00001506std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1507
1508 // Locate the rest of the toolchain ...
1509 if (strlen(GCC_INSTALL_PREFIX))
1510 return std::string(GCC_INSTALL_PREFIX);
1511
1512 std::string InstallRelDir = InstalledDir + "/../../gnu";
1513 if (llvm::sys::fs::exists(InstallRelDir))
1514 return InstallRelDir;
1515
1516 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1517 if (llvm::sys::fs::exists(PrefixRelDir))
1518 return PrefixRelDir;
1519
1520 return InstallRelDir;
1521}
1522
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001523static void GetHexagonLibraryPaths(
1524 const ArgList &Args,
1525 const std::string Ver,
1526 const std::string MarchString,
1527 const std::string &InstalledDir,
1528 ToolChain::path_list *LibPaths)
1529{
1530 bool buildingLib = Args.hasArg(options::OPT_shared);
1531
1532 //----------------------------------------------------------------------------
1533 // -L Args
1534 //----------------------------------------------------------------------------
1535 for (arg_iterator
1536 it = Args.filtered_begin(options::OPT_L),
1537 ie = Args.filtered_end();
1538 it != ie;
1539 ++it) {
1540 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1541 LibPaths->push_back((*it)->getValue(i));
1542 }
1543
1544 //----------------------------------------------------------------------------
1545 // Other standard paths
1546 //----------------------------------------------------------------------------
1547 const std::string MarchSuffix = "/" + MarchString;
1548 const std::string G0Suffix = "/G0";
1549 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1550 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1551
1552 // lib/gcc/hexagon/...
1553 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1554 if (buildingLib) {
1555 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1556 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1557 }
1558 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1559 LibPaths->push_back(LibGCCHexagonDir + Ver);
1560
1561 // lib/gcc/...
1562 LibPaths->push_back(RootDir + "lib/gcc");
1563
1564 // hexagon/lib/...
1565 std::string HexagonLibDir = RootDir + "hexagon/lib";
1566 if (buildingLib) {
1567 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1568 LibPaths->push_back(HexagonLibDir + G0Suffix);
1569 }
1570 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1571 LibPaths->push_back(HexagonLibDir);
1572}
1573
Matthew Curtisb3489a02012-12-06 12:43:18 +00001574Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1575 const ArgList &Args)
1576 : Linux(D, Triple, Args) {
1577 const std::string InstalledDir(getDriver().getInstalledDir());
1578 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1579
1580 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1581 // program paths
1582 const std::string BinDir(GnuDir + "/bin");
1583 if (llvm::sys::fs::exists(BinDir))
1584 getProgramPaths().push_back(BinDir);
1585
1586 // Determine version of GCC libraries and headers to use.
1587 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1588 llvm::error_code ec;
1589 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1590 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1591 !ec && di != de; di = di.increment(ec)) {
1592 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1593 if (MaxVersion < cv)
1594 MaxVersion = cv;
1595 }
1596 GCCLibAndIncVersion = MaxVersion;
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001597
1598 ToolChain::path_list *LibPaths= &getFilePaths();
1599
1600 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1601 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1602 // support 'linux' we'll need to fix this up
1603 LibPaths->clear();
1604
1605 GetHexagonLibraryPaths(
1606 Args,
1607 GetGCCLibAndIncVersion(),
1608 GetTargetCPU(Args),
1609 InstalledDir,
1610 LibPaths);
Tony Linthicum96319392011-12-12 21:14:55 +00001611}
1612
1613Hexagon_TC::~Hexagon_TC() {
Tony Linthicum96319392011-12-12 21:14:55 +00001614}
1615
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001616Tool *Hexagon_TC::buildAssembler() const {
1617 return new tools::hexagon::Assemble(*this);
1618}
1619
1620Tool *Hexagon_TC::buildLinker() const {
1621 return new tools::hexagon::Link(*this);
Tony Linthicum96319392011-12-12 21:14:55 +00001622}
1623
Matthew Curtisb3489a02012-12-06 12:43:18 +00001624void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1625 ArgStringList &CC1Args) const {
1626 const Driver &D = getDriver();
1627
1628 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1629 DriverArgs.hasArg(options::OPT_nostdlibinc))
1630 return;
1631
1632 llvm::sys::Path InstallDir(D.InstalledDir);
1633 std::string Ver(GetGCCLibAndIncVersion());
1634 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1635 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1636 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1637 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1638 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum96319392011-12-12 21:14:55 +00001639}
1640
Matthew Curtisb3489a02012-12-06 12:43:18 +00001641void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1642 ArgStringList &CC1Args) const {
1643
1644 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1645 DriverArgs.hasArg(options::OPT_nostdincxx))
1646 return;
1647
1648 const Driver &D = getDriver();
1649 std::string Ver(GetGCCLibAndIncVersion());
1650 llvm::sys::Path IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
1651
1652 IncludeDir.appendComponent("hexagon/include/c++/");
1653 IncludeDir.appendComponent(Ver);
1654 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001655}
Matthew Curtis67814152012-12-06 14:16:43 +00001656
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001657ToolChain::CXXStdlibType
1658Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1659 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1660 if (!A)
1661 return ToolChain::CST_Libstdcxx;
1662
1663 StringRef Value = A->getValue();
1664 if (Value != "libstdc++") {
1665 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1666 << A->getAsString(Args);
1667 }
1668
1669 return ToolChain::CST_Libstdcxx;
1670}
1671
1672static Arg *GetLastHexagonArchArg(const ArgList &Args)
Matthew Curtis67814152012-12-06 14:16:43 +00001673{
1674 Arg *A = NULL;
1675
1676 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1677 it != ie; ++it) {
1678 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
1679 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1680 A = *it;
1681 A->claim();
1682 } else if ((*it)->getOption().matches(options::OPT_m_Joined)) {
1683 StringRef Value = (*it)->getValue(0);
1684 if (Value.startswith("v")) {
1685 A = *it;
1686 A->claim();
1687 }
1688 }
1689 }
1690 return A;
1691}
1692
1693StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1694{
1695 // Select the default CPU (v4) if none was given or detection failed.
1696 Arg *A = GetLastHexagonArchArg (Args);
1697 if (A) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001698 StringRef WhichHexagon = A->getValue();
Matthew Curtis67814152012-12-06 14:16:43 +00001699 if (WhichHexagon.startswith("hexagon"))
1700 return WhichHexagon.substr(sizeof("hexagon") - 1);
1701 if (WhichHexagon != "")
1702 return WhichHexagon;
1703 }
1704
1705 return "v4";
1706}
Matthew Curtisb3489a02012-12-06 12:43:18 +00001707// End Hexagon
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001708
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001709/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1710/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1711/// Currently does not support anything else but compilation.
1712
Rafael Espindolaaf370e62013-03-18 18:10:27 +00001713TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
1714 const ArgList &Args)
1715 : ToolChain(D, Triple, Args) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001716 // Path mangling to find libexec
1717 std::string Path(getDriver().Dir);
1718
1719 Path += "/../libexec";
1720 getProgramPaths().push_back(Path);
1721}
1722
1723TCEToolChain::~TCEToolChain() {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001724}
1725
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001726bool TCEToolChain::IsMathErrnoDefault() const {
1727 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001728}
1729
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001730bool TCEToolChain::isPICDefault() const {
1731 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001732}
1733
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00001734bool TCEToolChain::isPIEDefault() const {
1735 return false;
1736}
1737
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001738bool TCEToolChain::isPICDefaultForced() const {
1739 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001740}
1741
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001742/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1743
Rafael Espindola0e659592012-02-19 01:38:32 +00001744OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1745 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001746 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001747 getFilePaths().push_back("/usr/lib");
1748}
1749
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001750Tool *OpenBSD::buildAssembler() const {
1751 return new tools::openbsd::Assemble(*this);
1752}
1753
1754Tool *OpenBSD::buildLinker() const {
1755 return new tools::openbsd::Link(*this);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001756}
1757
Eli Friedman42f74f22012-08-08 23:57:20 +00001758/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1759
1760Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1761 : Generic_ELF(D, Triple, Args) {
1762 getFilePaths().push_back(getDriver().Dir + "/../lib");
1763 getFilePaths().push_back("/usr/lib");
1764}
1765
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001766Tool *Bitrig::buildAssembler() const {
1767 return new tools::bitrig::Assemble(*this);
1768}
1769
1770Tool *Bitrig::buildLinker() const {
1771 return new tools::bitrig::Link(*this);
Eli Friedman42f74f22012-08-08 23:57:20 +00001772}
1773
1774void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1775 ArgStringList &CC1Args) const {
1776 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1777 DriverArgs.hasArg(options::OPT_nostdincxx))
1778 return;
1779
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001780 switch (GetCXXStdlibType(DriverArgs)) {
1781 case ToolChain::CST_Libcxx:
1782 addSystemInclude(DriverArgs, CC1Args,
1783 getDriver().SysRoot + "/usr/include/c++/");
1784 break;
1785 case ToolChain::CST_Libstdcxx:
1786 addSystemInclude(DriverArgs, CC1Args,
1787 getDriver().SysRoot + "/usr/include/c++/stdc++");
1788 addSystemInclude(DriverArgs, CC1Args,
1789 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001790
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001791 StringRef Triple = getTriple().str();
1792 if (Triple.startswith("amd64"))
1793 addSystemInclude(DriverArgs, CC1Args,
1794 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1795 Triple.substr(5));
1796 else
1797 addSystemInclude(DriverArgs, CC1Args,
1798 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1799 Triple);
1800 break;
1801 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001802}
1803
1804void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1805 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001806 switch (GetCXXStdlibType(Args)) {
1807 case ToolChain::CST_Libcxx:
1808 CmdArgs.push_back("-lc++");
1809 CmdArgs.push_back("-lcxxrt");
1810 // Include supc++ to provide Unwind until provided by libcxx.
1811 CmdArgs.push_back("-lgcc");
1812 break;
1813 case ToolChain::CST_Libstdcxx:
1814 CmdArgs.push_back("-lstdc++");
1815 break;
1816 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001817}
1818
Daniel Dunbar75358d22009-03-30 21:06:03 +00001819/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1820
Rafael Espindola0e659592012-02-19 01:38:32 +00001821FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1822 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001823
Chandler Carruth24248e32012-01-26 01:35:15 +00001824 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1825 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001826 if ((Triple.getArch() == llvm::Triple::x86 ||
1827 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001828 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001829 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1830 else
1831 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001832}
1833
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001834Tool *FreeBSD::buildAssembler() const {
1835 return new tools::freebsd::Assemble(*this);
1836}
1837
1838Tool *FreeBSD::buildLinker() const {
1839 return new tools::freebsd::Link(*this);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001840}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001841
Rafael Espindola27fa2362012-12-13 04:17:14 +00001842bool FreeBSD::UseSjLjExceptions() const {
1843 // FreeBSD uses SjLj exceptions on ARM oabi.
1844 switch (getTriple().getEnvironment()) {
1845 case llvm::Triple::GNUEABI:
1846 case llvm::Triple::EABI:
1847 return false;
1848
1849 default:
1850 return (getTriple().getArch() == llvm::Triple::arm ||
1851 getTriple().getArch() == llvm::Triple::thumb);
1852 }
1853}
1854
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001855/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1856
Rafael Espindola0e659592012-02-19 01:38:32 +00001857NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1858 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001859
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001860 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001861 // When targeting a 32-bit platform, try the special directory used on
1862 // 64-bit hosts, and only fall back to the main library directory if that
1863 // doesn't work.
1864 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1865 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001866 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001867 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001868
1869 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001870 }
1871}
1872
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001873Tool *NetBSD::buildAssembler() const {
1874 return new tools::netbsd::Assemble(*this);
1875}
1876
1877Tool *NetBSD::buildLinker() const {
1878 return new tools::netbsd::Link(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001879}
1880
Joerg Sonnenbergera7efaf92013-04-30 01:21:43 +00001881ToolChain::CXXStdlibType
1882NetBSD::GetCXXStdlibType(const ArgList &Args) const {
1883 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
1884 StringRef Value = A->getValue();
1885 if (Value == "libstdc++")
1886 return ToolChain::CST_Libstdcxx;
1887 if (Value == "libc++")
1888 return ToolChain::CST_Libcxx;
1889
1890 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1891 << A->getAsString(Args);
1892 }
1893
1894 return ToolChain::CST_Libstdcxx;
1895}
1896
1897void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1898 ArgStringList &CC1Args) const {
1899 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1900 DriverArgs.hasArg(options::OPT_nostdincxx))
1901 return;
1902
1903 switch (GetCXXStdlibType(DriverArgs)) {
1904 case ToolChain::CST_Libcxx:
1905 addSystemInclude(DriverArgs, CC1Args,
1906 getDriver().SysRoot + "/usr/include/c++/");
1907 break;
1908 case ToolChain::CST_Libstdcxx:
1909 addSystemInclude(DriverArgs, CC1Args,
1910 getDriver().SysRoot + "/usr/include/g++");
1911 addSystemInclude(DriverArgs, CC1Args,
1912 getDriver().SysRoot + "/usr/include/g++/backward");
1913 break;
1914 }
1915}
1916
Chris Lattner38e317d2010-07-07 16:01:42 +00001917/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1918
Rafael Espindola0e659592012-02-19 01:38:32 +00001919Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1920 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001921 getFilePaths().push_back(getDriver().Dir + "/../lib");
1922 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001923}
1924
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001925Tool *Minix::buildAssembler() const {
1926 return new tools::minix::Assemble(*this);
1927}
1928
1929Tool *Minix::buildLinker() const {
1930 return new tools::minix::Link(*this);
Chris Lattner38e317d2010-07-07 16:01:42 +00001931}
1932
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001933/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1934
Rafael Espindola0e659592012-02-19 01:38:32 +00001935AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1936 const ArgList &Args)
1937 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001938
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001939 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001940 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001941 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001942
Daniel Dunbaree788e72009-12-21 18:54:17 +00001943 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001944 getFilePaths().push_back("/usr/lib");
1945 getFilePaths().push_back("/usr/sfw/lib");
1946 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001947 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001948
1949}
1950
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001951Tool *AuroraUX::buildAssembler() const {
1952 return new tools::auroraux::Assemble(*this);
1953}
1954
1955Tool *AuroraUX::buildLinker() const {
1956 return new tools::auroraux::Link(*this);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001957}
1958
David Chisnall31c46902012-02-15 13:39:01 +00001959/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1960
Rafael Espindola0e659592012-02-19 01:38:32 +00001961Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1962 const ArgList &Args)
1963 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001964
1965 getProgramPaths().push_back(getDriver().getInstalledDir());
1966 if (getDriver().getInstalledDir() != getDriver().Dir)
1967 getProgramPaths().push_back(getDriver().Dir);
1968
1969 getFilePaths().push_back(getDriver().Dir + "/../lib");
1970 getFilePaths().push_back("/usr/lib");
1971}
1972
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00001973Tool *Solaris::buildAssembler() const {
1974 return new tools::solaris::Assemble(*this);
1975}
1976
1977Tool *Solaris::buildLinker() const {
1978 return new tools::solaris::Link(*this);
David Chisnall31c46902012-02-15 13:39:01 +00001979}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001980
Thomas Schwinged52b4a92013-03-28 19:02:48 +00001981/// Distribution (very bare-bones at the moment).
Eli Friedman6b3454a2009-05-26 07:52:18 +00001982
Thomas Schwinged52b4a92013-03-28 19:02:48 +00001983enum Distro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001984 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001985 DebianLenny,
1986 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001987 DebianWheezy,
Sylvestre Ledrub1718522013-01-06 08:09:29 +00001988 DebianJessie,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001989 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001990 RHEL4,
1991 RHEL5,
1992 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001993 Fedora13,
1994 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001995 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001996 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001997 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001998 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001999 OpenSuse11_4,
2000 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002001 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00002002 UbuntuHardy,
2003 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00002004 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00002005 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002006 UbuntuLucid,
2007 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00002008 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00002009 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002010 UbuntuPrecise,
Rafael Espindolade39d172012-12-13 20:26:05 +00002011 UbuntuQuantal,
2012 UbuntuRaring,
Sylvestre Ledru1b034642013-06-13 11:52:27 +00002013 UbuntuSaucy,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002014 UnknownDistro
2015};
2016
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002017static bool IsRedhat(enum Distro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002018 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
2019 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002020}
2021
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002022static bool IsOpenSuse(enum Distro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002023 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002024}
2025
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002026static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002027 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002028}
2029
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002030static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledru1b034642013-06-13 11:52:27 +00002031 return Distro >= UbuntuHardy && Distro <= UbuntuSaucy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002032}
2033
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002034static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002035 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002036 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002037 StringRef Data = File.get()->getBuffer();
2038 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002039 Data.split(Lines, "\n");
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002040 Distro Version = UnknownDistro;
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002041 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2042 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002043 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002044 .Case("hardy", UbuntuHardy)
2045 .Case("intrepid", UbuntuIntrepid)
2046 .Case("jaunty", UbuntuJaunty)
2047 .Case("karmic", UbuntuKarmic)
2048 .Case("lucid", UbuntuLucid)
2049 .Case("maverick", UbuntuMaverick)
2050 .Case("natty", UbuntuNatty)
2051 .Case("oneiric", UbuntuOneiric)
2052 .Case("precise", UbuntuPrecise)
Rafael Espindolade39d172012-12-13 20:26:05 +00002053 .Case("quantal", UbuntuQuantal)
2054 .Case("raring", UbuntuRaring)
Sylvestre Ledru1b034642013-06-13 11:52:27 +00002055 .Case("saucy", UbuntuSaucy)
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002056 .Default(UnknownDistro);
2057 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002058 }
2059
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002060 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002061 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002062 if (Data.startswith("Fedora release 16"))
2063 return Fedora16;
2064 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00002065 return Fedora15;
2066 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002067 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00002068 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002069 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00002070 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002071 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00002072 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00002073 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002074 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002075 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002076 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopher4f4e2af2013-02-05 07:29:49 +00002077 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002078 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002079 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002080 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopher4f4e2af2013-02-05 07:29:49 +00002081 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002082 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002083 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002084 return UnknownDistro;
2085 }
2086
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002087 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002088 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002089 if (Data[0] == '5')
2090 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002091 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00002092 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002093 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00002094 return DebianWheezy;
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002095 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2096 return DebianJessie;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002097 return UnknownDistro;
2098 }
2099
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002100 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002101 return llvm::StringSwitch<Distro>(File.get()->getBuffer())
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002102 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2103 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2104 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002105 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002106 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002107
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00002108 bool Exists;
2109 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00002110 return Exherbo;
2111
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002112 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2113 return ArchLinux;
2114
Rafael Espindolac1da9812010-11-07 20:14:31 +00002115 return UnknownDistro;
2116}
2117
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002118/// \brief Get our best guess at the multiarch triple for a target.
2119///
2120/// Debian-based systems are starting to use a multiarch setup where they use
2121/// a target-triple directory in the library and header search paths.
2122/// Unfortunately, this triple does not align with the vanilla target triple,
2123/// so we provide a rough mapping here.
2124static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2125 StringRef SysRoot) {
2126 // For most architectures, just use whatever we have rather than trying to be
2127 // clever.
2128 switch (TargetTriple.getArch()) {
2129 default:
2130 return TargetTriple.str();
2131
2132 // We use the existence of '/lib/<triple>' as a directory to detect some
2133 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00002134 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2135 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00002136 case llvm::Triple::arm:
2137 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00002138 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2139 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2140 return "arm-linux-gnueabihf";
2141 } else {
2142 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2143 return "arm-linux-gnueabi";
2144 }
Chad Rosier0337efd2012-07-11 19:08:21 +00002145 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002146 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002147 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2148 return "i386-linux-gnu";
2149 return TargetTriple.str();
2150 case llvm::Triple::x86_64:
2151 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2152 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002153 return TargetTriple.str();
Tim Northoverc264e162013-01-31 12:13:10 +00002154 case llvm::Triple::aarch64:
2155 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2156 return "aarch64-linux-gnu";
Tim Northover162579a2013-06-13 22:54:55 +00002157 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00002158 case llvm::Triple::mips:
2159 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2160 return "mips-linux-gnu";
2161 return TargetTriple.str();
2162 case llvm::Triple::mipsel:
2163 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2164 return "mipsel-linux-gnu";
2165 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002166 case llvm::Triple::ppc:
Sylvestre Ledrub7e86be2013-03-15 16:22:43 +00002167 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2168 return "powerpc-linux-gnuspe";
Chandler Carruth155c54c2012-02-26 09:03:21 +00002169 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2170 return "powerpc-linux-gnu";
2171 return TargetTriple.str();
2172 case llvm::Triple::ppc64:
2173 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2174 return "powerpc64-linux-gnu";
2175 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002176 }
2177}
2178
Chandler Carruth00646ba2012-01-25 11:24:24 +00002179static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2180 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2181}
2182
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002183static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2184 const ArgList &Args) {
2185 if (Arch != llvm::Triple::mips &&
2186 Arch != llvm::Triple::mipsel)
2187 return false;
2188
2189 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2190 options::OPT_mcpu_EQ,
2191 options::OPT_mips_CPUs_Group);
2192
2193 if (!A)
2194 return false;
2195
2196 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2197 return A->getOption().matches(options::OPT_mips32r2);
2198
Richard Smith1d489cf2012-11-01 04:30:05 +00002199 return A->getValue() == StringRef("mips32r2");
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002200}
2201
Simon Atanasyan7a918882012-09-14 11:27:24 +00002202static StringRef getMultilibDir(const llvm::Triple &Triple,
2203 const ArgList &Args) {
2204 if (!isMipsArch(Triple.getArch()))
2205 return Triple.isArch32Bit() ? "lib32" : "lib64";
2206
2207 // lib32 directory has a special meaning on MIPS targets.
2208 // It contains N32 ABI binaries. Use this folder if produce
2209 // code for N32 ABI only.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00002210 if (hasMipsN32ABIArg(Args))
Simon Atanasyan7a918882012-09-14 11:27:24 +00002211 return "lib32";
2212
2213 return Triple.isArch32Bit() ? "lib" : "lib64";
2214}
2215
Rafael Espindola0e659592012-02-19 01:38:32 +00002216Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2217 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002218 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002219 std::string SysRoot = computeSysRoot(Args);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002220
Rafael Espindolaab784082011-09-01 16:25:49 +00002221 // OpenSuse stores the linker with the compiler, add that to the search
2222 // path.
2223 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002224 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002225 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002226
2227 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002228
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002229 Distro Distro = DetectDistro(Arch);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002230
Chris Lattner64a89172011-05-22 16:45:07 +00002231 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002232 ExtraOpts.push_back("-z");
2233 ExtraOpts.push_back("relro");
2234 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002235
Douglas Gregorf0594d82011-03-06 19:11:49 +00002236 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002237 ExtraOpts.push_back("-X");
2238
Logan Chien94a71422012-09-02 09:30:11 +00002239 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002240 const bool IsMips = isMipsArch(Arch);
2241
2242 if (IsMips && !SysRoot.empty())
2243 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002244
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002245 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2246 // and the MIPS ABI require .dynsym to be sorted in different ways.
2247 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2248 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002249 // Android loader does not support .gnu.hash.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002250 if (!IsMips && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002251 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2252 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002253 ExtraOpts.push_back("--hash-style=gnu");
2254
2255 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2256 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2257 ExtraOpts.push_back("--hash-style=both");
2258 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002259
Chris Lattnerd753b562011-05-22 05:36:06 +00002260 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002261 ExtraOpts.push_back("--no-add-needed");
2262
Eli Friedman0b200f62011-06-02 21:36:53 +00002263 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002264 Distro == DebianJessie || IsOpenSuse(Distro) ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002265 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002266 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002267 ExtraOpts.push_back("--build-id");
2268
Chris Lattner64a89172011-05-22 16:45:07 +00002269 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002270 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002271
Chandler Carruthd2deee12011-10-03 05:28:29 +00002272 // The selection of paths to try here is designed to match the patterns which
2273 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2274 // This was determined by running GCC in a fake filesystem, creating all
2275 // possible permutations of these directories, and seeing which ones it added
2276 // to the link paths.
2277 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002278
Simon Atanasyan7a918882012-09-14 11:27:24 +00002279 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002280 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002281
Chandler Carruthd1f73062011-11-06 23:09:05 +00002282 // Add the multilib suffixed paths where they are available.
2283 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002284 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002285 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002286
2287 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2288 addPathIfExists(GCCInstallation.getInstallPath() +
2289 GCCInstallation.getMultiarchSuffix() +
2290 "/mips-r2",
2291 Paths);
2292 else
2293 addPathIfExists((GCCInstallation.getInstallPath() +
2294 GCCInstallation.getMultiarchSuffix()),
2295 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002296
2297 // If the GCC installation we found is inside of the sysroot, we want to
2298 // prefer libraries installed in the parent prefix of the GCC installation.
2299 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002300 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002301 // This usually happens when there is an external cross compiler on the
2302 // host system, and a more minimal sysroot available that is the target of
2303 // the cross.
2304 if (StringRef(LibPath).startswith(SysRoot)) {
2305 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2306 Paths);
2307 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2308 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2309 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002310 // On Android, libraries in the parent prefix of the GCC installation are
2311 // preferred to the ones under sysroot.
2312 if (IsAndroid) {
2313 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2314 }
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002315 // Sourcery CodeBench MIPS toolchain holds some libraries under
2316 // the parent prefix of the GCC installation.
2317 if (IsMips) {
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00002318 std::string Suffix;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002319 appendMipsTargetSuffix(Suffix, Arch, Args);
2320 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" +
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00002321 Multilib + Suffix,
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002322 Paths);
2323 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002324 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002325 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2326 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2327 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2328 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2329
2330 // Try walking via the GCC triple path in case of multiarch GCC
2331 // installations with strange symlinks.
2332 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002333 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002334 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002335
Chandler Carruth7a09d012011-10-16 10:54:30 +00002336 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002337 if (GCCInstallation.isValid()) {
2338 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002339 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002340 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002341 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002342
2343 if (StringRef(LibPath).startswith(SysRoot)) {
2344 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2345 addPathIfExists(LibPath, Paths);
2346 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002347 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002348 addPathIfExists(SysRoot + "/lib", Paths);
2349 addPathIfExists(SysRoot + "/usr/lib", Paths);
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002350
2351 IsPIEDefault = SanitizerArgs(*this, Args).hasZeroBaseShadow();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002352}
2353
2354bool Linux::HasNativeLLVMSupport() const {
2355 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002356}
2357
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002358Tool *Linux::buildLinker() const {
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00002359 return new tools::gnutools::Link(*this);
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002360}
2361
2362Tool *Linux::buildAssembler() const {
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00002363 return new tools::gnutools::Assemble(*this);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002364}
2365
Chandler Carrutha6b25812012-11-21 23:40:23 +00002366void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2367 ArgStringList &CC1Args) const {
Rafael Espindola8af669f2012-06-19 01:26:10 +00002368 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carrutha6b25812012-11-21 23:40:23 +00002369 bool UseInitArrayDefault
2370 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
Tim Northoverc264e162013-01-31 12:13:10 +00002371 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carrutha6b25812012-11-21 23:40:23 +00002372 getTriple().getEnvironment() == llvm::Triple::Android;
2373 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2374 options::OPT_fno_use_init_array,
2375 UseInitArrayDefault))
Rafael Espindola8af669f2012-06-19 01:26:10 +00002376 CC1Args.push_back("-fuse-init-array");
2377}
2378
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002379std::string Linux::computeSysRoot(const ArgList &Args) const {
2380 if (!getDriver().SysRoot.empty())
2381 return getDriver().SysRoot;
2382
2383 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
2384 return std::string();
2385
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00002386 std::string Path =
2387 (GCCInstallation.getInstallPath() +
2388 "/../../../../" + GCCInstallation.getTriple().str() + "/libc").str();
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002389 appendMipsTargetSuffix(Path, getTriple().getArch(), Args);
2390
Rafael Espindolaec2b1b92013-04-30 12:24:40 +00002391 return llvm::sys::fs::exists(Path) ? Path : "";
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002392}
2393
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002394void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2395 ArgStringList &CC1Args) const {
2396 const Driver &D = getDriver();
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002397 std::string SysRoot = computeSysRoot(DriverArgs);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002398
2399 if (DriverArgs.hasArg(options::OPT_nostdinc))
2400 return;
2401
2402 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002403 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002404
2405 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002406 llvm::sys::Path P(D.ResourceDir);
2407 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002408 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002409 }
2410
2411 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2412 return;
2413
2414 // Check for configure-time C include directories.
2415 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2416 if (CIncludeDirs != "") {
2417 SmallVector<StringRef, 5> dirs;
2418 CIncludeDirs.split(dirs, ":");
2419 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2420 I != E; ++I) {
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002421 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? SysRoot : "";
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002422 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2423 }
2424 return;
2425 }
2426
2427 // Lacking those, try to detect the correct set of system includes for the
2428 // target triple.
2429
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002430 // Sourcery CodeBench and modern FSF Mips toolchains put extern C
2431 // system includes under three additional directories.
2432 if (GCCInstallation.isValid() && isMipsArch(getTriple().getArch())) {
2433 addExternCSystemIncludeIfExists(DriverArgs, CC1Args,
2434 GCCInstallation.getInstallPath() +
2435 "/include");
2436
2437 addExternCSystemIncludeIfExists(DriverArgs, CC1Args,
2438 GCCInstallation.getInstallPath() +
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002439 "/../../../../" +
2440 GCCInstallation.getTriple().str() +
2441 "/libc/usr/include");
2442 }
2443
Chandler Carrutha4630892011-11-06 08:21:07 +00002444 // Implement generic Debian multiarch support.
2445 const StringRef X86_64MultiarchIncludeDirs[] = {
2446 "/usr/include/x86_64-linux-gnu",
2447
2448 // FIXME: These are older forms of multiarch. It's not clear that they're
2449 // in use in any released version of Debian, so we should consider
2450 // removing them.
2451 "/usr/include/i686-linux-gnu/64",
2452 "/usr/include/i486-linux-gnu/64"
2453 };
2454 const StringRef X86MultiarchIncludeDirs[] = {
2455 "/usr/include/i386-linux-gnu",
2456
2457 // FIXME: These are older forms of multiarch. It's not clear that they're
2458 // in use in any released version of Debian, so we should consider
2459 // removing them.
2460 "/usr/include/x86_64-linux-gnu/32",
2461 "/usr/include/i686-linux-gnu",
2462 "/usr/include/i486-linux-gnu"
2463 };
Tim Northoverc264e162013-01-31 12:13:10 +00002464 const StringRef AArch64MultiarchIncludeDirs[] = {
2465 "/usr/include/aarch64-linux-gnu"
2466 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002467 const StringRef ARMMultiarchIncludeDirs[] = {
2468 "/usr/include/arm-linux-gnueabi"
2469 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002470 const StringRef ARMHFMultiarchIncludeDirs[] = {
2471 "/usr/include/arm-linux-gnueabihf"
2472 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002473 const StringRef MIPSMultiarchIncludeDirs[] = {
2474 "/usr/include/mips-linux-gnu"
2475 };
2476 const StringRef MIPSELMultiarchIncludeDirs[] = {
2477 "/usr/include/mipsel-linux-gnu"
2478 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002479 const StringRef PPCMultiarchIncludeDirs[] = {
2480 "/usr/include/powerpc-linux-gnu"
2481 };
2482 const StringRef PPC64MultiarchIncludeDirs[] = {
2483 "/usr/include/powerpc64-linux-gnu"
2484 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002485 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002486 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002487 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002488 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002489 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northoverc264e162013-01-31 12:13:10 +00002490 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2491 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002492 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002493 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2494 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2495 else
2496 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002497 } else if (getTriple().getArch() == llvm::Triple::mips) {
2498 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2499 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2500 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002501 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2502 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2503 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2504 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002505 }
2506 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2507 E = MultiarchIncludeDirs.end();
2508 I != E; ++I) {
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002509 if (llvm::sys::fs::exists(SysRoot + *I)) {
2510 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + *I);
Chandler Carrutha4630892011-11-06 08:21:07 +00002511 break;
2512 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002513 }
2514
2515 if (getTriple().getOS() == llvm::Triple::RTEMS)
2516 return;
2517
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002518 // Add an include of '/include' directly. This isn't provided by default by
2519 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2520 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002521 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002522
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002523 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002524}
2525
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002526/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002527/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2528 const ArgList &DriverArgs,
2529 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002530 if (!llvm::sys::fs::exists(Base))
2531 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002532 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002533 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002534 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002535 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002536}
2537
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002538/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2539/// libstdc++ installation.
2540/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2541 Twine TargetArchDir,
2542 Twine MultiLibSuffix,
2543 const ArgList &DriverArgs,
2544 ArgStringList &CC1Args) {
2545 if (!addLibStdCXXIncludePaths(Base+Suffix, TargetArchDir + MultiLibSuffix,
2546 DriverArgs, CC1Args))
2547 return false;
2548
2549 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
2550 + MultiLibSuffix);
2551 return true;
2552}
2553
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002554void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2555 ArgStringList &CC1Args) const {
2556 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2557 DriverArgs.hasArg(options::OPT_nostdincxx))
2558 return;
2559
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002560 // Check if libc++ has been enabled and provide its include paths if so.
2561 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2562 // libc++ is always installed at a fixed path on Linux currently.
2563 addSystemInclude(DriverArgs, CC1Args,
2564 getDriver().SysRoot + "/usr/include/c++/v1");
2565 return;
2566 }
2567
Chandler Carruthfc52f752012-01-25 08:04:13 +00002568 // We need a detected GCC installation on Linux to provide libstdc++'s
2569 // headers. We handled the libc++ case above.
2570 if (!GCCInstallation.isValid())
2571 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002572
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002573 // By default, look for the C++ headers in an include directory adjacent to
2574 // the lib directory of the GCC installation. Note that this is expect to be
2575 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2576 StringRef LibDir = GCCInstallation.getParentLibPath();
2577 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002578 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002579 StringRef TripleStr = GCCInstallation.getTriple().str();
2580
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002581 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2582 "/c++/" + Version.str(),
2583 TripleStr,
2584 GCCInstallation.getMultiarchSuffix(),
2585 DriverArgs, CC1Args))
2586 return;
2587
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002588 const std::string IncludePathCandidates[] = {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002589 // Gentoo is weird and places its headers inside the GCC install, so if the
2590 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002591 InstallDir.str() + "/include/g++-v4",
2592 // Android standalone toolchain has C++ headers in yet another place.
2593 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002594 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2595 // without a subdirectory corresponding to the gcc version.
2596 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002597 };
2598
2599 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2600 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2601 GCCInstallation.getMultiarchSuffix()),
2602 DriverArgs, CC1Args))
2603 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002604 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002605}
2606
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002607bool Linux::isPIEDefault() const {
2608 return IsPIEDefault;
2609}
2610
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002611/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2612
Rafael Espindola0e659592012-02-19 01:38:32 +00002613DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2614 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002615
2616 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002617 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002618 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002619 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002620
Daniel Dunbaree788e72009-12-21 18:54:17 +00002621 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002622 getFilePaths().push_back("/usr/lib");
John McCall8cfb7202013-04-11 22:55:55 +00002623 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
2624 getFilePaths().push_back("/usr/lib/gcc47");
2625 else
2626 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002627}
2628
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002629Tool *DragonFly::buildAssembler() const {
2630 return new tools::dragonfly::Assemble(*this);
2631}
2632
2633Tool *DragonFly::buildLinker() const {
2634 return new tools::dragonfly::Link(*this);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002635}