blob: 7dc73422113c961c791f119105058a8b2ba58f8c [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar59e5e882009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000011#include "clang/Basic/ObjCRuntime.h"
12#include "clang/Basic/Version.h"
Daniel Dunbar6232d342010-05-20 21:48:38 +000013#include "clang/Driver/Compilation.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000014#include "clang/Driver/Driver.h"
Daniel Dunbaraabb0b12009-03-25 06:12:34 +000015#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000016#include "clang/Driver/Options.h"
Alexey Samsonov609213f92013-08-19 09:14:21 +000017#include "clang/Driver/SanitizerArgs.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "llvm/ADT/STLExtras.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000019#include "llvm/ADT/SmallString.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000020#include "llvm/ADT/StringExtras.h"
Bob Wilson997a97f2011-10-07 00:37:57 +000021#include "llvm/ADT/StringSwitch.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000022#include "llvm/Option/Arg.h"
23#include "llvm/Option/ArgList.h"
24#include "llvm/Option/OptTable.h"
25#include "llvm/Option/Option.h"
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +000026#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000027#include "llvm/Support/FileSystem.h"
Rafael Espindolac8f008f2010-11-07 20:14:31 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000031#include "llvm/Support/system_error.h"
Robert Lyttoncf1dd692013-10-11 10:29:40 +000032#include "llvm/Support/Program.h"
NAKAMURA Takumi067fcb52012-12-04 14:31:59 +000033
34// FIXME: This needs to be listed last until we fix the broken include guards
35// in these files and the LLVM config.h files.
36#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
37
Daniel Dunbarb5023e92009-04-10 21:00:07 +000038#include <cstdlib> // ::getenv
39
Daniel Dunbar59e5e882009-03-20 00:20:03 +000040using namespace clang::driver;
41using namespace clang::driver::toolchains;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000042using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000043using namespace llvm::opt;
Daniel Dunbar59e5e882009-03-20 00:20:03 +000044
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +000045/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +000046
Rafael Espindola84b588b2013-03-18 18:10:27 +000047Darwin::Darwin(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
48 : ToolChain(D, Triple, Args), TargetInitialized(false)
Daniel Dunbar6276f992009-09-18 08:15:13 +000049{
Bob Wilson9d3f7af2012-01-31 21:30:03 +000050 // Compute the initial Darwin version from the triple
51 unsigned Major, Minor, Micro;
Bob Wilsona2234012012-01-31 22:43:59 +000052 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
53 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
54 Triple.getOSName();
55 llvm::raw_string_ostream(MacosxVersionMin)
56 << Major << '.' << Minor << '.' << Micro;
57
Bob Wilson9d3f7af2012-01-31 21:30:03 +000058 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
59 // It should be removed when we stop supporting that.
60 DarwinVersion[0] = Minor + 4;
61 DarwinVersion[1] = Micro;
62 DarwinVersion[2] = 0;
Chad Rosier266c6202012-05-09 18:46:30 +000063
64 // Compute the initial iOS version from the triple
Chad Rosierbf8628e2012-05-09 18:51:13 +000065 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosier266c6202012-05-09 18:46:30 +000066 llvm::raw_string_ostream(iOSVersionMin)
67 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar6276f992009-09-18 08:15:13 +000068}
69
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +000070types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
71 types::ID Ty = types::lookupTypeForExtension(Ext);
72
73 // Darwin always preprocesses assembly files (unless -x is used explicitly).
74 if (Ty == types::TY_PP_Asm)
75 return types::TY_Asm;
76
77 return Ty;
78}
79
Daniel Dunbar62123a12010-09-17 00:24:52 +000080bool Darwin::HasNativeLLVMSupport() const {
81 return true;
82}
83
John McCall24fc0de2011-07-06 00:26:06 +000084/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall5fb5df92012-06-20 06:18:46 +000085ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
Bob Wilson5ad5a952012-11-09 01:59:30 +000086 if (isTargetIPhoneOS())
John McCall5fb5df92012-06-20 06:18:46 +000087 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson5ad5a952012-11-09 01:59:30 +000088 if (isNonFragile)
89 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
90 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall24fc0de2011-07-06 00:26:06 +000091}
92
John McCall7959fee2011-09-09 20:41:01 +000093/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
94bool Darwin::hasBlocksRuntime() const {
95 if (isTargetIPhoneOS())
96 return !isIPhoneOSVersionLT(3, 2);
97 else
98 return !isMacosxVersionLT(10, 6);
99}
100
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000101static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilson997a97f2011-10-07 00:37:57 +0000102 return llvm::StringSwitch<const char*>(Value)
103 .Case("armv6k", "armv6")
Bob Wilson743bf672013-03-04 22:37:49 +0000104 .Case("armv6m", "armv6m")
Bob Wilson997a97f2011-10-07 00:37:57 +0000105 .Case("armv5tej", "armv5")
106 .Case("xscale", "xscale")
107 .Case("armv4t", "armv4t")
108 .Case("armv7", "armv7")
109 .Cases("armv7a", "armv7-a", "armv7")
110 .Cases("armv7r", "armv7-r", "armv7")
Bob Wilson743bf672013-03-04 22:37:49 +0000111 .Cases("armv7em", "armv7e-m", "armv7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000112 .Cases("armv7f", "armv7-f", "armv7f")
113 .Cases("armv7k", "armv7-k", "armv7k")
Bob Wilson743bf672013-03-04 22:37:49 +0000114 .Cases("armv7m", "armv7-m", "armv7m")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000115 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilson997a97f2011-10-07 00:37:57 +0000116 .Default(0);
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000117}
118
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000119static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilson997a97f2011-10-07 00:37:57 +0000120 return llvm::StringSwitch<const char *>(Value)
121 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
122 .Cases("arm10e", "arm10tdmi", "armv5")
123 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
124 .Case("xscale", "xscale")
Bob Wilson743bf672013-03-04 22:37:49 +0000125 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6")
126 .Case("cortex-m0", "armv6m")
Renato Golin1a04f222013-09-13 17:02:54 +0000127 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "armv7")
128 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "armv7")
Renato Golin60312302013-09-13 17:02:59 +0000129 .Cases("cortex-r4", "cortex-r5", "armv7r")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000130 .Case("cortex-a9-mp", "armv7f")
Bob Wilson743bf672013-03-04 22:37:49 +0000131 .Case("cortex-m3", "armv7m")
132 .Case("cortex-m4", "armv7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000133 .Case("swift", "armv7s")
Bob Wilson997a97f2011-10-07 00:37:57 +0000134 .Default(0);
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000135}
136
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000137StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000138 switch (getTriple().getArch()) {
139 default:
140 return getArchName();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000141
Douglas Gregord9bb1522011-03-06 19:11:49 +0000142 case llvm::Triple::thumb:
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000143 case llvm::Triple::arm: {
144 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +0000145 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000146 return Arch;
147
148 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +0000149 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000150 return Arch;
151
152 return "arm";
153 }
154 }
155}
156
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000157Darwin::~Darwin() {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000158}
159
Chad Rosierd3a0f952011-09-20 20:44:06 +0000160std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
161 types::ID InputType) const {
162 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000163
164 // If the target isn't initialized (e.g., an unknown Darwin platform, return
165 // the default triple).
166 if (!isTargetInitialized())
167 return Triple.getTriple();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000168
Tim Northover901dee42013-10-24 10:48:50 +0000169 if (Triple.getArchName() == "thumbv6m" ||
170 Triple.getArchName() == "thumbv7m" ||
171 Triple.getArchName() == "thumbv7em") {
172 // OS is ios or macosx unless it's the v6m or v7m.
173 Triple.setOS(llvm::Triple::Darwin);
174 Triple.setEnvironment(llvm::Triple::EABI);
175 } else {
176 SmallString<16> Str;
177 Str += isTargetIPhoneOS() ? "ios" : "macosx";
178 Str += getTargetVersion().getAsString();
179 Triple.setOSName(Str);
180 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000181
182 return Triple.getTriple();
183}
184
David Blaikie68e081d2011-12-20 02:48:34 +0000185void Generic_ELF::anchor() {}
186
Rafael Espindola7cf32212013-03-20 03:05:54 +0000187Tool *Darwin::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +0000188 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000189 case Action::LipoJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000190 if (!Lipo)
191 Lipo.reset(new tools::darwin::Lipo(*this));
192 return Lipo.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000193 case Action::DsymutilJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000194 if (!Dsymutil)
195 Dsymutil.reset(new tools::darwin::Dsymutil(*this));
196 return Dsymutil.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000197 case Action::VerifyJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000198 if (!VerifyDebug)
199 VerifyDebug.reset(new tools::darwin::VerifyDebug(*this));
200 return VerifyDebug.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000201 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000202 return ToolChain::getTool(AC);
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000203 }
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000204}
205
Rafael Espindola7cf32212013-03-20 03:05:54 +0000206Tool *Darwin::buildLinker() const {
207 return new tools::darwin::Link(*this);
208}
209
210Tool *Darwin::buildAssembler() const {
211 return new tools::darwin::Assemble(*this);
212}
Daniel Dunbar26d482a2009-09-18 08:15:03 +0000213
Rafael Espindola84b588b2013-03-18 18:10:27 +0000214DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple,
215 const ArgList &Args)
216 : Darwin(D, Triple, Args)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000217{
Daniel Dunbar00aff042010-09-17 08:22:12 +0000218 getProgramPaths().push_back(getDriver().getInstalledDir());
219 if (getDriver().getInstalledDir() != getDriver().Dir)
220 getProgramPaths().push_back(getDriver().Dir);
221
Daniel Dunbar6276f992009-09-18 08:15:13 +0000222 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbar88979912010-08-01 22:29:51 +0000223 getProgramPaths().push_back(getDriver().getInstalledDir());
224 if (getDriver().getInstalledDir() != getDriver().Dir)
225 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar6276f992009-09-18 08:15:13 +0000226}
227
John McCall31168b02011-06-15 23:02:42 +0000228void DarwinClang::AddLinkARCArgs(const ArgList &Args,
229 ArgStringList &CmdArgs) const {
Eric Christopher551ef452011-08-23 17:56:55 +0000230
231 CmdArgs.push_back("-force_load");
Rafael Espindola358256c2013-06-26 02:13:00 +0000232 SmallString<128> P(getDriver().ClangExecutable);
233 llvm::sys::path::remove_filename(P); // 'clang'
234 llvm::sys::path::remove_filename(P); // 'bin'
Benjamin Kramer17381a02013-06-28 16:25:46 +0000235 llvm::sys::path::append(P, "lib", "arc", "libarclite_");
John McCall31168b02011-06-15 23:02:42 +0000236 // Mash in the platform.
Argyrios Kyrtzidis058b4512011-10-18 17:40:15 +0000237 if (isTargetIOSSimulator())
Rafael Espindola358256c2013-06-26 02:13:00 +0000238 P += "iphonesimulator";
Argyrios Kyrtzidis058b4512011-10-18 17:40:15 +0000239 else if (isTargetIPhoneOS())
Rafael Espindola358256c2013-06-26 02:13:00 +0000240 P += "iphoneos";
John McCall31168b02011-06-15 23:02:42 +0000241 else
Rafael Espindola358256c2013-06-26 02:13:00 +0000242 P += "macosx";
243 P += ".a";
John McCall31168b02011-06-15 23:02:42 +0000244
Rafael Espindola358256c2013-06-26 02:13:00 +0000245 CmdArgs.push_back(Args.MakeArgString(P));
John McCall31168b02011-06-15 23:02:42 +0000246}
247
Eric Christopherc235d0c62011-06-22 17:41:40 +0000248void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopher551ef452011-08-23 17:56:55 +0000249 ArgStringList &CmdArgs,
Alexey Samsonov8368b372012-11-21 14:17:42 +0000250 const char *DarwinStaticLib,
251 bool AlwaysLink) const {
Rafael Espindola358256c2013-06-26 02:13:00 +0000252 SmallString<128> P(getDriver().ResourceDir);
Benjamin Kramer17381a02013-06-28 16:25:46 +0000253 llvm::sys::path::append(P, "lib", "darwin", DarwinStaticLib);
Eric Christopher551ef452011-08-23 17:56:55 +0000254
Eric Christopherc235d0c62011-06-22 17:41:40 +0000255 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov8368b372012-11-21 14:17:42 +0000256 // not have compiler-rt checked out or integrated into their build (unless
257 // we explicitly force linking with this library).
Rafael Espindola355670d2013-06-25 15:14:22 +0000258 if (AlwaysLink || llvm::sys::fs::exists(P.str()))
Eric Christopherc235d0c62011-06-22 17:41:40 +0000259 CmdArgs.push_back(Args.MakeArgString(P.str()));
260}
261
Daniel Dunbar6276f992009-09-18 08:15:13 +0000262void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
263 ArgStringList &CmdArgs) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000264 // Darwin only supports the compiler-rt based runtime libraries.
265 switch (GetRuntimeLibType(Args)) {
266 case ToolChain::RLT_CompilerRT:
267 break;
268 default:
269 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smithbd55daf2012-11-01 04:30:05 +0000270 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000271 return;
272 }
273
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000274 // Darwin doesn't support real static executables, don't link any runtime
275 // libraries with -static.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000276 if (Args.hasArg(options::OPT_static) ||
277 Args.hasArg(options::OPT_fapple_kext) ||
278 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar6276f992009-09-18 08:15:13 +0000279 return;
Daniel Dunbar6276f992009-09-18 08:15:13 +0000280
281 // Reject -static-libgcc for now, we can deal with this when and if someone
282 // cares. This is useful in situations where someone wants to statically link
283 // something like libstdc++, and needs its runtime support routines.
284 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000285 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000286 << A->getAsString(Args);
287 return;
288 }
289
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000290 // If we are building profile support, link that library in.
291 if (Args.hasArg(options::OPT_fprofile_arcs) ||
292 Args.hasArg(options::OPT_fprofile_generate) ||
293 Args.hasArg(options::OPT_fcreate_profile) ||
294 Args.hasArg(options::OPT_coverage)) {
295 // Select the appropriate runtime library for the target.
296 if (isTargetIPhoneOS()) {
297 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
298 } else {
299 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
300 }
301 }
302
Peter Collingbourne32701642013-11-01 18:16:25 +0000303 const SanitizerArgs &Sanitize = getSanitizerArgs();
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000304
Alexey Samsonovcc429802012-11-16 12:53:14 +0000305 // Add Ubsan runtime library, if required.
306 if (Sanitize.needsUbsanRt()) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000307 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
Alexey Samsonov969be242013-01-21 08:45:02 +0000308 if (isTargetIPhoneOS()) {
Alexey Samsonovcc429802012-11-16 12:53:14 +0000309 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
310 << "-fsanitize=undefined";
311 } else {
Alexey Samsonov8368b372012-11-21 14:17:42 +0000312 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonovcc429802012-11-16 12:53:14 +0000313
314 // The Ubsan runtime library requires C++.
315 AddCXXStdlibLibArgs(Args, CmdArgs);
316 }
317 }
318
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000319 // Add ASAN runtime library, if required. Dynamic libraries and bundles
320 // should not be linked with the runtime library.
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000321 if (Sanitize.needsAsanRt()) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000322 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
Alexander Potapenko5700f402013-03-21 10:49:06 +0000323 if (isTargetIPhoneOS() && !isTargetIOSSimulator()) {
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000324 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000325 << "-fsanitize=address";
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000326 } else {
Alexander Potapenkoce876332013-09-20 08:09:51 +0000327 if (!Args.hasArg(options::OPT_dynamiclib) &&
328 !Args.hasArg(options::OPT_bundle)) {
Alexander Potapenko5700f402013-03-21 10:49:06 +0000329 // The ASAN runtime library requires C++.
330 AddCXXStdlibLibArgs(Args, CmdArgs);
331 }
Alexander Potapenko868cca92013-11-15 16:07:44 +0000332 if (isTargetMacOS()) {
333 AddLinkRuntimeLib(Args, CmdArgs,
334 "libclang_rt.asan_osx_dynamic.dylib",
335 true);
336 } else {
337 if (isTargetIOSSimulator()) {
338 AddLinkRuntimeLib(Args, CmdArgs,
339 "libclang_rt.asan_iossim_dynamic.dylib",
340 true);
341 }
342 }
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000343 }
344 }
345
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000346 // Otherwise link libSystem, then the dynamic runtime library, and finally any
347 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000348 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000349
350 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar15c89422010-01-27 00:56:37 +0000351 if (isTargetIPhoneOS()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000352 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
353 // it never went into the SDK.
Bob Wilson102be442011-10-07 17:54:41 +0000354 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
355 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
356 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000357
Daniel Dunbard1076382011-04-18 23:48:36 +0000358 // We currently always need a static runtime library for iOS.
Eric Christopherc235d0c62011-06-22 17:41:40 +0000359 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000360 } else {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000361 // The dynamic runtime library was merged with libSystem for 10.6 and
362 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000363 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000364 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000365 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000366 CmdArgs.push_back("-lgcc_s.10.5");
367
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000368 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000369 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000370 // omitted from 10.4.dylib.
371 //
372 // Unfortunately, that turned out to not be true, because Darwin system
373 // headers can still use eprintf on i386, and it is not exported from
374 // libSystem. Therefore, we still must provide a runtime library just for
375 // the tiny tiny handful of projects that *might* use that symbol.
376 if (isMacosxVersionLT(10, 5)) {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000377 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000378 } else {
379 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopherc235d0c62011-06-22 17:41:40 +0000380 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
381 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000382 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000383 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000384}
385
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000386void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000387 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000388
Daniel Dunbar455a0492012-08-17 18:43:50 +0000389 // Support allowing the SDKROOT environment variable used by xcrun and other
390 // Xcode tools to define the default sysroot, by making it the default for
391 // isysroot.
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000392 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
393 // Warn if the path does not exist.
Rafael Espindola355670d2013-06-25 15:14:22 +0000394 if (!llvm::sys::fs::exists(A->getValue()))
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000395 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
396 } else {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000397 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbarb2543042013-01-15 20:33:56 +0000398 // We only use this value as the default if it is an absolute path,
399 // exists, and it is not the root path.
400 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
401 StringRef(env) != "/") {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000402 Args.append(Args.MakeSeparateArg(
403 0, Opts.getOption(options::OPT_isysroot), env));
404 }
405 }
406 }
407
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000408 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000409 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
410 Arg *iOSSimVersion = Args.getLastArg(
411 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman027e9c32012-01-11 02:41:15 +0000412
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000413 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000414 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000415 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000416 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
417 iOSVersion = iOSSimVersion = 0;
418 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000419 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000420 << iOSVersion->getAsString(Args)
421 << iOSSimVersion->getAsString(Args);
422 iOSSimVersion = 0;
423 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosier64707fe2011-08-31 20:56:25 +0000424 // If no deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000425 // environment defines.
Chad Rosier64707fe2011-08-31 20:56:25 +0000426 StringRef OSXTarget;
427 StringRef iOSTarget;
428 StringRef iOSSimTarget;
429 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
430 OSXTarget = env;
431 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
432 iOSTarget = env;
433 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
434 iOSSimTarget = env;
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000435
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000436 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosier64707fe2011-08-31 20:56:25 +0000437 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif5d3231c2012-04-18 10:59:08 +0000438 // based on -isysroot.
Chad Rosier64707fe2011-08-31 20:56:25 +0000439 if (iOSTarget.empty()) {
440 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
441 StringRef first, second;
Richard Smithbd55daf2012-11-01 04:30:05 +0000442 StringRef isysroot = A->getValue();
Chad Rosier64707fe2011-08-31 20:56:25 +0000443 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
444 if (second != "")
445 iOSTarget = second.substr(0,3);
446 }
447 }
Daniel Dunbard54669d2010-01-26 01:45:19 +0000448
Chad Rosierfe6fd362011-09-28 00:46:32 +0000449 // If no OSX or iOS target has been specified and we're compiling for armv7,
450 // go ahead as assume we're targeting iOS.
Chad Rosier7b1fee12012-05-09 18:55:57 +0000451 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilsond7cf1042012-09-29 23:52:50 +0000452 (getDarwinArchName(Args) == "armv7" ||
453 getDarwinArchName(Args) == "armv7s"))
Chad Rosierf761fe92012-05-09 18:09:58 +0000454 iOSTarget = iOSVersionMin;
Chad Rosierfe6fd362011-09-28 00:46:32 +0000455
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000456 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000457 //
458 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000459
460 // Do not allow conflicts with the iOS simulator target.
Chad Rosier64707fe2011-08-31 20:56:25 +0000461 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000462 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000463 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosier64707fe2011-08-31 20:56:25 +0000464 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000465 "IPHONEOS_DEPLOYMENT_TARGET");
466 }
467
468 // Allow conflicts among OSX and iOS for historical reasons, but choose the
469 // default platform.
Chad Rosier64707fe2011-08-31 20:56:25 +0000470 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000471 if (getTriple().getArch() == llvm::Triple::arm ||
472 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosier64707fe2011-08-31 20:56:25 +0000473 OSXTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000474 else
Chad Rosier64707fe2011-08-31 20:56:25 +0000475 iOSTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000476 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000477
Chad Rosier64707fe2011-08-31 20:56:25 +0000478 if (!OSXTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000479 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000480 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
481 Args.append(OSXVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000482 } else if (!iOSTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000483 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000484 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
485 Args.append(iOSVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000486 } else if (!iOSSimTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000487 const Option O = Opts.getOption(
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000488 options::OPT_mios_simulator_version_min_EQ);
489 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
490 Args.append(iOSSimVersion);
Daniel Dunbard54669d2010-01-26 01:45:19 +0000491 } else {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000492 // Otherwise, assume we are targeting OS X.
Michael J. Spencerfc790902012-10-19 22:36:40 +0000493 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000494 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
495 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000496 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000497 }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000499 // Reject invalid architecture combinations.
500 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
501 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000502 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000503 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
504 }
505
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000506 // Set the tool chain target information.
507 unsigned Major, Minor, Micro;
508 bool HadExtra;
509 if (OSXVersion) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000510 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000511 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000512 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000513 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000514 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000515 << OSXVersion->getAsString(Args);
516 } else {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000517 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
518 assert(Version && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000519 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman027e9c32012-01-11 02:41:15 +0000520 Micro, HadExtra) || HadExtra ||
521 Major >= 10 || Minor >= 100 || Micro >= 100)
522 getDriver().Diag(diag::err_drv_invalid_version_number)
523 << Version->getAsString(Args);
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000524 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000525
Daniel Dunbarb1189432011-04-30 04:18:16 +0000526 bool IsIOSSim = bool(iOSSimVersion);
527
528 // In GCC, the simulator historically was treated as being OS X in some
529 // contexts, like determining the link logic, despite generally being called
530 // with an iOS deployment target. For compatibility, we detect the
531 // simulator as iOS + x86, and treat it differently in a few contexts.
532 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
533 getTriple().getArch() == llvm::Triple::x86_64))
534 IsIOSSim = true;
535
536 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000537}
538
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000539void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000540 ArgStringList &CmdArgs) const {
541 CXXStdlibType Type = GetCXXStdlibType(Args);
542
543 switch (Type) {
544 case ToolChain::CST_Libcxx:
545 CmdArgs.push_back("-lc++");
546 break;
547
548 case ToolChain::CST_Libstdcxx: {
549 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
550 // it was previously found in the gcc lib dir. However, for all the Darwin
551 // platforms we care about it was -lstdc++.6, so we search for that
552 // explicitly if we can't see an obvious -lstdc++ candidate.
553
554 // Check in the sysroot first.
555 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000556 SmallString<128> P(A->getValue());
Benjamin Kramer17381a02013-06-28 16:25:46 +0000557 llvm::sys::path::append(P, "usr", "lib", "libstdc++.dylib");
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000558
Rafael Espindola355670d2013-06-25 15:14:22 +0000559 if (!llvm::sys::fs::exists(P.str())) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000560 llvm::sys::path::remove_filename(P);
561 llvm::sys::path::append(P, "libstdc++.6.dylib");
Rafael Espindola355670d2013-06-25 15:14:22 +0000562 if (llvm::sys::fs::exists(P.str())) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000563 CmdArgs.push_back(Args.MakeArgString(P.str()));
564 return;
565 }
566 }
567 }
568
569 // Otherwise, look in the root.
Bob Wilson1a9ad0f2011-11-11 07:47:04 +0000570 // FIXME: This should be removed someday when we don't have to care about
571 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Rafael Espindola355670d2013-06-25 15:14:22 +0000572 if (!llvm::sys::fs::exists("/usr/lib/libstdc++.dylib") &&
573 llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib")) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000574 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
575 return;
576 }
577
578 // Otherwise, let the linker search.
579 CmdArgs.push_back("-lstdc++");
580 break;
581 }
582 }
583}
584
Shantonu Senafeb03b2010-09-17 18:39:08 +0000585void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
586 ArgStringList &CmdArgs) const {
587
588 // For Darwin platforms, use the compiler-rt-based support library
589 // instead of the gcc-provided one (which is also incidentally
590 // only present in the gcc lib dir, which makes it hard to find).
591
Rafael Espindola358256c2013-06-26 02:13:00 +0000592 SmallString<128> P(getDriver().ResourceDir);
Benjamin Kramer17381a02013-06-28 16:25:46 +0000593 llvm::sys::path::append(P, "lib", "darwin");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000594
595 // Use the newer cc_kext for iOS ARM after 6.0.
596 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
597 !isIPhoneOSVersionLT(6, 0)) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000598 llvm::sys::path::append(P, "libclang_rt.cc_kext.a");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000599 } else {
Rafael Espindola358256c2013-06-26 02:13:00 +0000600 llvm::sys::path::append(P, "libclang_rt.cc_kext_ios5.a");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000601 }
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000602
Shantonu Senafeb03b2010-09-17 18:39:08 +0000603 // For now, allow missing resource libraries to support developers who may
604 // not have compiler-rt checked out or integrated into their build.
Rafael Espindola355670d2013-06-25 15:14:22 +0000605 if (llvm::sys::fs::exists(P.str()))
Shantonu Senafeb03b2010-09-17 18:39:08 +0000606 CmdArgs.push_back(Args.MakeArgString(P.str()));
607}
608
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000609DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
610 const char *BoundArch) const {
611 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
612 const OptTable &Opts = getDriver().getOpts();
613
614 // FIXME: We really want to get out of the tool chain level argument
615 // translation business, as it makes the driver functionality much
616 // more opaque. For now, we follow gcc closely solely for the
617 // purpose of easily achieving feature parity & testability. Once we
618 // have something that works, we should reevaluate each translation
619 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000620
Daniel Dunbar775d4062010-06-11 22:00:26 +0000621 for (ArgList::const_iterator it = Args.begin(),
622 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000623 Arg *A = *it;
624
625 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar471c4f82011-06-21 00:20:17 +0000626 // Skip this argument unless the architecture matches either the toolchain
627 // triple arch, or the arch being bound.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000628 llvm::Triple::ArchType XarchArch =
Richard Smithbd55daf2012-11-01 04:30:05 +0000629 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000630 if (!(XarchArch == getArch() ||
631 (BoundArch && XarchArch ==
Rafael Espindoladcbf6982012-10-31 18:51:07 +0000632 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000633 continue;
634
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000635 Arg *OriginalArg = A;
Richard Smithbd55daf2012-11-01 04:30:05 +0000636 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000637 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000638 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000639
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000640 // If the argument parsing failed or more than one argument was
641 // consumed, the -Xarch_ argument's parameter tried to consume
642 // extra arguments. Emit an error and ignore.
643 //
644 // We also want to disallow any options which would alter the
645 // driver behavior; that isn't going to work in our model. We
646 // use isDriverOption() as an approximation, although things
647 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000648 if (!XarchArg || Index > Prev + 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000649 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar6914a982011-04-21 17:32:21 +0000650 << A->getAsString(Args);
651 continue;
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000652 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000653 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000654 << A->getAsString(Args);
655 continue;
656 }
657
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000658 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000659 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000660
661 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000662
663 // Linker input arguments require custom handling. The problem is that we
664 // have already constructed the phase actions, so we can not treat them as
665 // "input arguments".
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000666 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000667 // Convert the argument into individual Zlinker_input_args.
668 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
669 DAL->AddSeparateArg(OriginalArg,
670 Opts.getOption(options::OPT_Zlinker_input),
Richard Smithbd55daf2012-11-01 04:30:05 +0000671 A->getValue(i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000672
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000673 }
674 continue;
675 }
Mike Stump11289f42009-09-09 15:08:12 +0000676 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000677
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000678 // Sob. These is strictly gcc compatible for the time being. Apple
679 // gcc translates options twice, which means that self-expanding
680 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000681 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000682 default:
683 DAL->append(A);
684 break;
685
686 case options::OPT_mkernel:
687 case options::OPT_fapple_kext:
688 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000689 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000690 break;
Mike Stump11289f42009-09-09 15:08:12 +0000691
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000692 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000693 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smithbd55daf2012-11-01 04:30:05 +0000694 A->getValue());
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000695 break;
696
697 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000698 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
699 DAL->AddFlagArg(A,
700 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000701 break;
702
703 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000704 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
705 DAL->AddFlagArg(A,
706 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000707 break;
708
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000709 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000710 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000711 break;
712
713 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000714 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000715 break;
716
717 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000718 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000719 break;
720
721 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000722 DAL->AddFlagArg(A,
723 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000724 break;
725
726 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000727 DAL->AddFlagArg(A,
728 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000729 break;
730
731 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000732 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000733 break;
734
735 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000736 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000737 break;
738 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000739 }
740
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000741 if (getTriple().getArch() == llvm::Triple::x86 ||
742 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000743 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000744 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000745
746 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosier7c5d9082012-04-27 14:58:16 +0000747 // how the driver driver works.
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000748 if (BoundArch) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000749 StringRef Name = BoundArch;
Michael J. Spencerfc790902012-10-19 22:36:40 +0000750 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
751 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000752
753 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
754 // which defines the list of which architectures we accept.
755 if (Name == "ppc")
756 ;
757 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000758 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000759 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000760 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000761 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000762 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000763 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000764 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000765 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000766 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000767 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000768 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000769 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000770 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000771 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000772 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000773
Bill Schmidt778d3872013-07-26 01:36:11 +0000774 else if (Name == "ppc64" || Name == "ppc64le")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000775 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000776
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000777 else if (Name == "i386")
778 ;
779 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000780 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000781 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000782 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000783 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000784 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000785 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000786 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000787 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000788 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000789 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000790 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000791 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000792 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000793
794 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000795 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000796
797 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000798 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000799 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000800 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000801 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000802 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000803 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000804 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000805 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000806 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000807 else if (Name == "armv6m")
808 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000809 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000810 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000811 else if (Name == "armv7em")
812 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000813 else if (Name == "armv7f")
814 DAL->AddJoinedArg(0, MArch, "armv7f");
815 else if (Name == "armv7k")
816 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000817 else if (Name == "armv7m")
818 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000819 else if (Name == "armv7s")
820 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000821
822 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000823 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000824 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000825
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000826 // Add an explicit version min argument for the deployment target. We do this
827 // after argument translation because -Xarch_ arguments may add a version min
828 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000829 if (BoundArch)
830 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000831
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000832 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
833 // FIXME: It would be far better to avoid inserting those -static arguments,
834 // but we can't check the deployment target in the translation code until
835 // it is set here.
836 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
837 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
838 Arg *A = *it;
839 ++it;
840 if (A->getOption().getID() != options::OPT_mkernel &&
841 A->getOption().getID() != options::OPT_fapple_kext)
842 continue;
843 assert(it != ie && "unexpected argument translation");
844 A = *it;
845 assert(A->getOption().getID() == options::OPT_static &&
846 "missing expected -static argument");
847 it = DAL->getArgs().erase(it);
848 }
849 }
850
Bob Wilson0f7445b2013-11-02 23:19:53 +0000851 // Default to use libc++ on OS X 10.9+ and iOS 7+.
852 if (((isTargetMacOS() && !isMacosxVersionLT(10, 9)) ||
853 (isTargetIPhoneOS() && !isIPhoneOSVersionLT(7, 0))) &&
854 !Args.getLastArg(options::OPT_stdlib_EQ))
855 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_stdlib_EQ), "libc++");
856
Bob Wilson102be442011-10-07 17:54:41 +0000857 // Validate the C++ standard library choice.
858 CXXStdlibType Type = GetCXXStdlibType(*DAL);
859 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000860 // Check whether the target provides libc++.
861 StringRef where;
862
863 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson5ad5a952012-11-09 01:59:30 +0000864 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
865 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000866
867 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000868 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000869 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000870 }
871 }
872
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000873 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000874}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000875
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000876bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000877 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000878}
879
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000880bool Darwin::UseDwarfDebugFlags() const {
881 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
882 return S[0] != '\0';
883 return false;
884}
885
Daniel Dunbar3241d402010-02-10 18:49:11 +0000886bool Darwin::UseSjLjExceptions() const {
887 // Darwin uses SjLj exceptions on ARM.
888 return (getTriple().getArch() == llvm::Triple::arm ||
889 getTriple().getArch() == llvm::Triple::thumb);
890}
891
Chandler Carruth76a943b2012-11-19 03:52:03 +0000892bool Darwin::isPICDefault() const {
893 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000894}
895
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000896bool Darwin::isPIEDefault() const {
897 return false;
898}
899
Chandler Carruth76a943b2012-11-19 03:52:03 +0000900bool Darwin::isPICDefaultForced() const {
901 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000902}
903
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000904bool Darwin::SupportsProfiling() const {
905 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000906 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000907}
908
Daniel Dunbar16334e12010-04-10 16:20:23 +0000909bool Darwin::SupportsObjCGC() const {
910 // Garbage collection is supported everywhere except on iPhone OS.
911 return !isTargetIPhoneOS();
912}
913
John McCall3deb1ad2012-08-21 02:47:43 +0000914void Darwin::CheckObjCARC() const {
915 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
916 return;
John McCall93207072012-08-27 01:56:21 +0000917 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000918}
919
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000920std::string
Chad Rosierd3a0f952011-09-20 20:44:06 +0000921Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
922 types::ID InputType) const {
923 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000924}
925
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000926/// Generic_GCC - A tool chain using the 'gcc' command to perform
927/// all subcommands; this relies on gcc translating the majority of
928/// command line options.
929
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000930/// \brief Parse a GCCVersion object out of a string of text.
931///
932/// This is the primary means of forming GCCVersion objects.
933/*static*/
934Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000935 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000936 std::pair<StringRef, StringRef> First = VersionText.split('.');
937 std::pair<StringRef, StringRef> Second = First.second.split('.');
938
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000939 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000940 if (First.first.getAsInteger(10, GoodVersion.Major) ||
941 GoodVersion.Major < 0)
942 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000943 GoodVersion.MajorStr = First.first.str();
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000944 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
945 GoodVersion.Minor < 0)
946 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000947 GoodVersion.MinorStr = Second.first.str();
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000948
949 // First look for a number prefix and parse that if present. Otherwise just
950 // stash the entire patch string in the suffix, and leave the number
951 // unspecified. This covers versions strings such as:
952 // 4.4
953 // 4.4.0
954 // 4.4.x
955 // 4.4.2-rc4
956 // 4.4.x-patched
957 // And retains any patch number it finds.
958 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
959 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +0000960 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000961 // Try to parse the number and any suffix.
962 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
963 GoodVersion.Patch < 0)
964 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000965 GoodVersion.PatchSuffix = PatchText.substr(EndNumber);
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000966 }
967 }
968
969 return GoodVersion;
970}
971
972/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000973bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor,
974 int RHSPatch,
975 StringRef RHSPatchSuffix) const {
976 if (Major != RHSMajor)
977 return Major < RHSMajor;
978 if (Minor != RHSMinor)
979 return Minor < RHSMinor;
980 if (Patch != RHSPatch) {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000981 // Note that versions without a specified patch sort higher than those with
982 // a patch.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000983 if (RHSPatch == -1)
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000984 return true;
985 if (Patch == -1)
986 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000987
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000988 // Otherwise just sort on the patch itself.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000989 return Patch < RHSPatch;
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000990 }
Benjamin Kramer604e8482013-08-09 17:17:48 +0000991 if (PatchSuffix != RHSPatchSuffix) {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000992 // Sort empty suffixes higher.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000993 if (RHSPatchSuffix.empty())
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000994 return true;
995 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +0000996 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000997
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000998 // Provide a lexicographic sort to make this a total ordering.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000999 return PatchSuffix < RHSPatchSuffix;
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001000 }
1001
1002 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001003 return false;
1004}
1005
Rafael Espindola1af7c212012-02-19 01:38:32 +00001006static StringRef getGCCToolchainDir(const ArgList &Args) {
1007 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1008 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +00001009 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +00001010 return GCC_INSTALL_PREFIX;
1011}
1012
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001013/// \brief Construct a GCCInstallationDetector from the driver.
1014///
1015/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +00001016/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +00001017///
1018/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1019/// should instead pull the target out of the driver. This is currently
1020/// necessary because the driver doesn't store the final version of the target
1021/// triple.
1022Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
Chandler Carruthb427c562013-06-22 11:35:51 +00001023 const Driver &D, const llvm::Triple &TargetTriple, const ArgList &Args)
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001024 : IsValid(false), D(D) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001025 llvm::Triple BiarchVariantTriple =
1026 TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
Chandler Carruth779579b2012-02-13 02:02:09 +00001027 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001028 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001029 // The library directories which may contain GCC installations.
Chandler Carruthb427c562013-06-22 11:35:51 +00001030 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001031 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001032 SmallVector<StringRef, 10> CandidateTripleAliases;
Chandler Carruthb427c562013-06-22 11:35:51 +00001033 SmallVector<StringRef, 10> CandidateBiarchTripleAliases;
1034 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1035 CandidateTripleAliases, CandidateBiarchLibDirs,
1036 CandidateBiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001037
1038 // Compute the set of prefixes for our search.
1039 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1040 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001041
Rafael Espindola1af7c212012-02-19 01:38:32 +00001042 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1043 if (GCCToolchainDir != "") {
1044 if (GCCToolchainDir.back() == '/')
1045 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001046
Rafael Espindola1af7c212012-02-19 01:38:32 +00001047 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001048 } else {
Rafael Espindola0f4b04e2013-08-28 23:17:47 +00001049 // If we have a SysRoot, try that first.
1050 if (!D.SysRoot.empty()) {
1051 Prefixes.push_back(D.SysRoot);
1052 Prefixes.push_back(D.SysRoot + "/usr");
1053 }
1054
1055 // Then look for gcc installed alongside clang.
Rafael Espindolac29af942012-02-03 01:01:20 +00001056 Prefixes.push_back(D.InstalledDir + "/..");
Rafael Espindola0f4b04e2013-08-28 23:17:47 +00001057
1058 // And finally in /usr.
1059 if (D.SysRoot.empty())
1060 Prefixes.push_back("/usr");
Rafael Espindolac29af942012-02-03 01:01:20 +00001061 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001062
1063 // Loop over the various components which exist and select the best GCC
1064 // installation available. GCC installs are ranked by version number.
1065 Version = GCCVersion::Parse("0.0.0");
1066 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1067 if (!llvm::sys::fs::exists(Prefixes[i]))
1068 continue;
1069 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1070 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1071 if (!llvm::sys::fs::exists(LibDir))
1072 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001073 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001074 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1075 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001076 }
Chandler Carruthb427c562013-06-22 11:35:51 +00001077 for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) {
1078 const std::string LibDir = Prefixes[i] + CandidateBiarchLibDirs[j].str();
Chandler Carruth866faab2012-01-25 07:21:38 +00001079 if (!llvm::sys::fs::exists(LibDir))
1080 continue;
Chandler Carruthb427c562013-06-22 11:35:51 +00001081 for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke;
Chandler Carruth866faab2012-01-25 07:21:38 +00001082 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001083 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruthb427c562013-06-22 11:35:51 +00001084 CandidateBiarchTripleAliases[k],
1085 /*NeedsBiarchSuffix=*/ true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001086 }
1087 }
1088}
1089
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001090void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const {
Benjamin Kramera97e4d12013-08-14 18:38:51 +00001091 for (std::set<std::string>::const_iterator
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001092 I = CandidateGCCInstallPaths.begin(),
1093 E = CandidateGCCInstallPaths.end();
1094 I != E; ++I)
1095 OS << "Found candidate GCC installation: " << *I << "\n";
1096
1097 OS << "Selected GCC installation: " << GCCInstallPath << "\n";
1098}
1099
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001100/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruthb427c562013-06-22 11:35:51 +00001101 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
Chandler Carruth866faab2012-01-25 07:21:38 +00001102 SmallVectorImpl<StringRef> &LibDirs,
1103 SmallVectorImpl<StringRef> &TripleAliases,
Chandler Carruthb427c562013-06-22 11:35:51 +00001104 SmallVectorImpl<StringRef> &BiarchLibDirs,
1105 SmallVectorImpl<StringRef> &BiarchTripleAliases) {
Chandler Carruth866faab2012-01-25 07:21:38 +00001106 // Declare a bunch of static data sets that we'll select between below. These
1107 // are specifically designed to always refer to string literals to avoid any
1108 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001109 static const char *const AArch64LibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001110 static const char *const AArch64Triples[] = { "aarch64-none-linux-gnu",
1111 "aarch64-linux-gnu" };
Tim Northover9bb857a2013-01-31 12:13:10 +00001112
Chandler Carruth866faab2012-01-25 07:21:38 +00001113 static const char *const ARMLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001114 static const char *const ARMTriples[] = { "arm-linux-gnueabi",
1115 "arm-linux-androideabi" };
1116 static const char *const ARMHFTriples[] = { "arm-linux-gnueabihf",
1117 "armv7hl-redhat-linux-gnueabi" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001118
1119 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1120 static const char *const X86_64Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001121 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", "x86_64-pc-linux-gnu",
1122 "x86_64-redhat-linux6E", "x86_64-redhat-linux", "x86_64-suse-linux",
1123 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", "x86_64-slackware-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001124 };
1125 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1126 static const char *const X86Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001127 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", "i386-linux-gnu",
1128 "i386-redhat-linux6E", "i686-redhat-linux", "i586-redhat-linux",
1129 "i386-redhat-linux", "i586-suse-linux", "i486-slackware-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001130 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001131 };
1132
1133 static const char *const MIPSLibDirs[] = { "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001134 static const char *const MIPSTriples[] = { "mips-linux-gnu",
1135 "mips-mti-linux-gnu" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001136 static const char *const MIPSELLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001137 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu",
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001138 "mipsel-linux-android" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001139
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001140 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001141 static const char *const MIPS64Triples[] = { "mips64-linux-gnu",
1142 "mips-mti-linux-gnu" };
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001143 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001144 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu",
1145 "mips-mti-linux-gnu" };
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001146
Chandler Carruth866faab2012-01-25 07:21:38 +00001147 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1148 static const char *const PPCTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001149 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
1150 "powerpc-suse-linux", "powerpc-montavista-linuxspe"
Chandler Carruth866faab2012-01-25 07:21:38 +00001151 };
1152 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001153 static const char *const PPC64Triples[] = { "powerpc64-linux-gnu",
1154 "powerpc64-unknown-linux-gnu",
1155 "powerpc64-suse-linux",
1156 "ppc64-redhat-linux" };
Bill Schmidt778d3872013-07-26 01:36:11 +00001157 static const char *const PPC64LELibDirs[] = { "/lib64", "/lib" };
1158 static const char *const PPC64LETriples[] = { "powerpc64le-linux-gnu",
1159 "powerpc64le-unknown-linux-gnu",
1160 "powerpc64le-suse-linux",
1161 "ppc64le-redhat-linux" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001162
Ulrich Weigand47445072013-05-06 16:26:41 +00001163 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1164 static const char *const SystemZTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001165 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
1166 "s390x-suse-linux", "s390x-redhat-linux"
Ulrich Weigand47445072013-05-06 16:26:41 +00001167 };
1168
Chandler Carruth866faab2012-01-25 07:21:38 +00001169 switch (TargetTriple.getArch()) {
Tim Northover9bb857a2013-01-31 12:13:10 +00001170 case llvm::Triple::aarch64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001171 LibDirs.append(AArch64LibDirs,
1172 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1173 TripleAliases.append(AArch64Triples,
1174 AArch64Triples + llvm::array_lengthof(AArch64Triples));
1175 BiarchLibDirs.append(AArch64LibDirs,
1176 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1177 BiarchTripleAliases.append(
1178 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
Tim Northover9bb857a2013-01-31 12:13:10 +00001179 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001180 case llvm::Triple::arm:
1181 case llvm::Triple::thumb:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001182 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001183 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001184 TripleAliases.append(ARMHFTriples,
1185 ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001186 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001187 TripleAliases.append(ARMTriples,
1188 ARMTriples + llvm::array_lengthof(ARMTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001189 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001190 break;
1191 case llvm::Triple::x86_64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001192 LibDirs.append(X86_64LibDirs,
1193 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1194 TripleAliases.append(X86_64Triples,
1195 X86_64Triples + llvm::array_lengthof(X86_64Triples));
1196 BiarchLibDirs.append(X86LibDirs,
1197 X86LibDirs + llvm::array_lengthof(X86LibDirs));
1198 BiarchTripleAliases.append(X86Triples,
1199 X86Triples + llvm::array_lengthof(X86Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001200 break;
1201 case llvm::Triple::x86:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001202 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001203 TripleAliases.append(X86Triples,
1204 X86Triples + llvm::array_lengthof(X86Triples));
1205 BiarchLibDirs.append(X86_64LibDirs,
1206 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1207 BiarchTripleAliases.append(
1208 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001209 break;
1210 case llvm::Triple::mips:
Chandler Carruthb427c562013-06-22 11:35:51 +00001211 LibDirs.append(MIPSLibDirs,
1212 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1213 TripleAliases.append(MIPSTriples,
1214 MIPSTriples + llvm::array_lengthof(MIPSTriples));
1215 BiarchLibDirs.append(MIPS64LibDirs,
1216 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1217 BiarchTripleAliases.append(
1218 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001219 break;
1220 case llvm::Triple::mipsel:
Chandler Carruthb427c562013-06-22 11:35:51 +00001221 LibDirs.append(MIPSELLibDirs,
1222 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1223 TripleAliases.append(MIPSELTriples,
1224 MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001225 TripleAliases.append(MIPSTriples,
1226 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Chandler Carruthb427c562013-06-22 11:35:51 +00001227 BiarchLibDirs.append(
1228 MIPS64ELLibDirs,
1229 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1230 BiarchTripleAliases.append(
1231 MIPS64ELTriples,
1232 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001233 break;
1234 case llvm::Triple::mips64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001235 LibDirs.append(MIPS64LibDirs,
1236 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1237 TripleAliases.append(MIPS64Triples,
1238 MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1239 BiarchLibDirs.append(MIPSLibDirs,
1240 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1241 BiarchTripleAliases.append(MIPSTriples,
1242 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001243 break;
1244 case llvm::Triple::mips64el:
Chandler Carruthb427c562013-06-22 11:35:51 +00001245 LibDirs.append(MIPS64ELLibDirs,
1246 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001247 TripleAliases.append(
Chandler Carruthb427c562013-06-22 11:35:51 +00001248 MIPS64ELTriples,
1249 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1250 BiarchLibDirs.append(MIPSELLibDirs,
1251 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1252 BiarchTripleAliases.append(
1253 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001254 BiarchTripleAliases.append(
1255 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001256 break;
1257 case llvm::Triple::ppc:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001258 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001259 TripleAliases.append(PPCTriples,
1260 PPCTriples + llvm::array_lengthof(PPCTriples));
1261 BiarchLibDirs.append(PPC64LibDirs,
1262 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1263 BiarchTripleAliases.append(
1264 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001265 break;
1266 case llvm::Triple::ppc64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001267 LibDirs.append(PPC64LibDirs,
1268 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1269 TripleAliases.append(PPC64Triples,
1270 PPC64Triples + llvm::array_lengthof(PPC64Triples));
1271 BiarchLibDirs.append(PPCLibDirs,
1272 PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1273 BiarchTripleAliases.append(PPCTriples,
1274 PPCTriples + llvm::array_lengthof(PPCTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001275 break;
Bill Schmidt778d3872013-07-26 01:36:11 +00001276 case llvm::Triple::ppc64le:
1277 LibDirs.append(PPC64LELibDirs,
1278 PPC64LELibDirs + llvm::array_lengthof(PPC64LELibDirs));
1279 TripleAliases.append(PPC64LETriples,
1280 PPC64LETriples + llvm::array_lengthof(PPC64LETriples));
1281 break;
Ulrich Weigand47445072013-05-06 16:26:41 +00001282 case llvm::Triple::systemz:
Chandler Carruthb427c562013-06-22 11:35:51 +00001283 LibDirs.append(SystemZLibDirs,
1284 SystemZLibDirs + llvm::array_lengthof(SystemZLibDirs));
1285 TripleAliases.append(SystemZTriples,
1286 SystemZTriples + llvm::array_lengthof(SystemZTriples));
Ulrich Weigand47445072013-05-06 16:26:41 +00001287 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001288
1289 default:
1290 // By default, just rely on the standard lib directories and the original
1291 // triple.
1292 break;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001293 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001294
1295 // Always append the drivers target triple to the end, in case it doesn't
1296 // match any of our aliases.
1297 TripleAliases.push_back(TargetTriple.str());
1298
1299 // Also include the multiarch variant if it's different.
Chandler Carruthb427c562013-06-22 11:35:51 +00001300 if (TargetTriple.str() != BiarchTriple.str())
1301 BiarchTripleAliases.push_back(BiarchTriple.str());
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001302}
1303
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001304static bool isSoftFloatABI(const ArgList &Args) {
1305 Arg *A = Args.getLastArg(options::OPT_msoft_float,
1306 options::OPT_mhard_float,
1307 options::OPT_mfloat_abi_EQ);
1308 if (!A) return false;
1309
1310 return A->getOption().matches(options::OPT_msoft_float) ||
1311 (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
1312 A->getValue() == StringRef("soft"));
1313}
1314
1315static bool isMipsArch(llvm::Triple::ArchType Arch) {
1316 return Arch == llvm::Triple::mips ||
1317 Arch == llvm::Triple::mipsel ||
1318 Arch == llvm::Triple::mips64 ||
1319 Arch == llvm::Triple::mips64el;
1320}
1321
1322static bool isMips16(const ArgList &Args) {
1323 Arg *A = Args.getLastArg(options::OPT_mips16,
1324 options::OPT_mno_mips16);
1325 return A && A->getOption().matches(options::OPT_mips16);
1326}
1327
Simon Atanasyan068e0fd2013-10-09 12:12:39 +00001328static bool isMips32r2(const ArgList &Args) {
1329 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1330 options::OPT_mcpu_EQ);
1331
1332 return A && A->getValue() == StringRef("mips32r2");
1333}
1334
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001335static bool isMips64r2(const ArgList &Args) {
1336 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1337 options::OPT_mcpu_EQ);
1338
1339 return A && A->getValue() == StringRef("mips64r2");
1340}
1341
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001342static bool isMicroMips(const ArgList &Args) {
1343 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1344 options::OPT_mno_micromips);
1345 return A && A->getOption().matches(options::OPT_mmicromips);
1346}
1347
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001348static bool isMipsNan2008(const ArgList &Args) {
1349 Arg *A = Args.getLastArg(options::OPT_mnan_EQ);
1350 return A && A->getValue() == StringRef("2008");
1351}
1352
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001353// FIXME: There is the same routine in the Tools.cpp.
1354static bool hasMipsN32ABIArg(const ArgList &Args) {
1355 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00001356 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001357}
1358
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001359static bool hasCrtBeginObj(Twine Path) {
1360 return llvm::sys::fs::exists(Path + "/crtbegin.o");
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001361}
1362
Chandler Carruthb427c562013-06-22 11:35:51 +00001363static bool findTargetBiarchSuffix(std::string &Suffix, StringRef Path,
1364 llvm::Triple::ArchType TargetArch,
1365 const ArgList &Args) {
1366 // FIXME: This routine was only intended to model bi-arch toolchains which
1367 // use -m32 and -m64 to swap between variants of a target. It shouldn't be
1368 // doing ABI-based builtin location for MIPS.
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001369 if (hasMipsN32ABIArg(Args))
1370 Suffix = "/n32";
1371 else if (TargetArch == llvm::Triple::x86_64 ||
1372 TargetArch == llvm::Triple::ppc64 ||
1373 TargetArch == llvm::Triple::systemz ||
1374 TargetArch == llvm::Triple::mips64 ||
1375 TargetArch == llvm::Triple::mips64el)
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001376 Suffix = "/64";
1377 else
1378 Suffix = "/32";
1379
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001380 return hasCrtBeginObj(Path + Suffix);
1381}
1382
Chandler Carruth8677d922013-10-29 08:53:03 +00001383void Generic_GCC::GCCInstallationDetector::findMIPSABIDirSuffix(
1384 std::string &Suffix, llvm::Triple::ArchType TargetArch, StringRef Path,
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001385 const llvm::opt::ArgList &Args) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001386 if (!isMipsArch(TargetArch))
1387 return;
1388
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001389 // Some MIPS toolchains put libraries and object files compiled
1390 // using different options in to the sub-directoris which names
1391 // reflects the flags used for compilation. For example sysroot
1392 // directory might looks like the following examples:
1393 //
1394 // /usr
1395 // /lib <= crt*.o files compiled with '-mips32'
1396 // /mips16
1397 // /usr
1398 // /lib <= crt*.o files compiled with '-mips16'
1399 // /el
1400 // /usr
1401 // /lib <= crt*.o files compiled with '-mips16 -EL'
1402 //
1403 // or
1404 //
1405 // /usr
1406 // /lib <= crt*.o files compiled with '-mips32r2'
1407 // /mips16
1408 // /usr
1409 // /lib <= crt*.o files compiled with '-mips32r2 -mips16'
1410 // /mips32
1411 // /usr
1412 // /lib <= crt*.o files compiled with '-mips32'
1413 //
1414 // Unfortunately different toolchains use different and partially
1415 // overlapped naming schemes. So we have to make a trick for detection
1416 // of using toolchain. We lookup a path which unique for each toolchains.
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001417
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001418 bool IsMentorToolChain = hasCrtBeginObj(Path + "/mips16/soft-float");
1419 bool IsFSFToolChain = hasCrtBeginObj(Path + "/mips32/mips16/sof");
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001420
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001421 if (IsMentorToolChain && IsFSFToolChain)
1422 D.Diag(diag::err_drv_unknown_toolchain);
1423
1424 if (IsMentorToolChain) {
1425 if (isMips16(Args))
1426 Suffix += "/mips16";
1427 else if (isMicroMips(Args))
1428 Suffix += "/micromips";
1429
1430 if (isSoftFloatABI(Args))
1431 Suffix += "/soft-float";
1432
1433 if (TargetArch == llvm::Triple::mipsel ||
1434 TargetArch == llvm::Triple::mips64el)
1435 Suffix += "/el";
1436 } else if (IsFSFToolChain) {
1437 if (TargetArch == llvm::Triple::mips ||
1438 TargetArch == llvm::Triple::mipsel) {
1439 if (isMicroMips(Args))
1440 Suffix += "/micromips";
1441 else if (isMips32r2(Args))
1442 Suffix += "";
1443 else
1444 Suffix += "/mips32";
1445
1446 if (isMips16(Args))
1447 Suffix += "/mips16";
1448 } else {
1449 if (isMips64r2(Args))
1450 Suffix += hasMipsN32ABIArg(Args) ? "/mips64r2" : "/mips64r2/64";
1451 else
1452 Suffix += hasMipsN32ABIArg(Args) ? "/mips64" : "/mips64/64";
1453 }
1454
1455 if (TargetArch == llvm::Triple::mipsel ||
1456 TargetArch == llvm::Triple::mips64el)
1457 Suffix += "/el";
1458
1459 if (isSoftFloatABI(Args))
1460 Suffix += "/sof";
1461 else if (isMipsNan2008(Args))
1462 Suffix += "/nan2008";
1463 }
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001464
1465 if (!hasCrtBeginObj(Path + Suffix))
1466 Suffix.clear();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001467}
1468
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001469void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001470 llvm::Triple::ArchType TargetArch, const ArgList &Args,
Chandler Carruthb427c562013-06-22 11:35:51 +00001471 const std::string &LibDir, StringRef CandidateTriple,
1472 bool NeedsBiarchSuffix) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001473 // There are various different suffixes involving the triple we
1474 // check for. We also record what is necessary to walk from each back
1475 // up to the lib directory.
Chandler Carruth866faab2012-01-25 07:21:38 +00001476 const std::string LibSuffixes[] = {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001477 "/gcc/" + CandidateTriple.str(),
Eli Friedmanbf44f362013-07-26 00:53:40 +00001478 // Debian puts cross-compilers in gcc-cross
1479 "/gcc-cross/" + CandidateTriple.str(),
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001480 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1481
Hal Finkelf3587912012-09-18 22:25:07 +00001482 // The Freescale PPC SDK has the gcc libraries in
1483 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1484 "/" + CandidateTriple.str(),
1485
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001486 // Ubuntu has a strange mis-matched pair of triples that this happens to
1487 // match.
1488 // FIXME: It may be worthwhile to generalize this and look for a second
1489 // triple.
Chandler Carruth6e46ca22011-11-09 03:46:20 +00001490 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001491 };
Eli Friedmanbf44f362013-07-26 00:53:40 +00001492 const std::string InstallSuffixes[] = {
1493 "/../../..", // gcc/
1494 "/../../..", // gcc-cross/
1495 "/../../../..", // <triple>/gcc/
1496 "/../..", // <triple>/
1497 "/../../../.." // i386-linux-gnu/gcc/<triple>/
1498 };
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001499 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruthb427c562013-06-22 11:35:51 +00001500 const unsigned NumLibSuffixes =
1501 (llvm::array_lengthof(LibSuffixes) - (TargetArch != llvm::Triple::x86));
Chandler Carruth866faab2012-01-25 07:21:38 +00001502 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1503 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001504 llvm::error_code EC;
Chandler Carruth866faab2012-01-25 07:21:38 +00001505 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001506 !EC && LI != LE; LI = LI.increment(EC)) {
1507 StringRef VersionText = llvm::sys::path::filename(LI->path());
1508 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
Benjamin Kramera97e4d12013-08-14 18:38:51 +00001509 if (CandidateVersion.Major != -1) // Filter obviously bad entries.
1510 if (!CandidateGCCInstallPaths.insert(LI->path()).second)
1511 continue; // Saw this path before; no need to look at it again.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001512 if (CandidateVersion.isOlderThan(4, 1, 1))
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001513 continue;
1514 if (CandidateVersion <= Version)
1515 continue;
Hal Finkel221e11e2011-12-08 05:50:03 +00001516
Chandler Carruth8677d922013-10-29 08:53:03 +00001517 std::string MIPSABIDirSuffix;
1518 findMIPSABIDirSuffix(MIPSABIDirSuffix, TargetArch, LI->path(), Args);
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001519
Hal Finkel221e11e2011-12-08 05:50:03 +00001520 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth64cee062012-01-24 19:21:42 +00001521 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth866faab2012-01-25 07:21:38 +00001522 // libs in a subdirectory named 64. The simple logic we follow is that
1523 // *if* there is a subdirectory of the right name with crtbegin.o in it,
Chandler Carruthb427c562013-06-22 11:35:51 +00001524 // we use that. If not, and if not a biarch triple alias, we look for
Chandler Carruth866faab2012-01-25 07:21:38 +00001525 // crtbegin.o without the subdirectory.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001526
Chandler Carruthb427c562013-06-22 11:35:51 +00001527 std::string BiarchSuffix;
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001528 if (findTargetBiarchSuffix(BiarchSuffix,
Chandler Carruth8677d922013-10-29 08:53:03 +00001529 LI->path() + MIPSABIDirSuffix,
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001530 TargetArch, Args)) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001531 GCCBiarchSuffix = BiarchSuffix;
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001532 } else if (NeedsBiarchSuffix ||
Chandler Carruth8677d922013-10-29 08:53:03 +00001533 !hasCrtBeginObj(LI->path() + MIPSABIDirSuffix)) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001534 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001535 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001536 GCCBiarchSuffix.clear();
Chandler Carruth866faab2012-01-25 07:21:38 +00001537 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001538
1539 Version = CandidateVersion;
Chandler Carruth4d9d7682012-01-24 19:28:29 +00001540 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001541 // FIXME: We hack together the directory name here instead of
1542 // using LI to ensure stable path separators across Windows and
1543 // Linux.
Chandler Carruth866faab2012-01-25 07:21:38 +00001544 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth64cee062012-01-24 19:21:42 +00001545 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth8677d922013-10-29 08:53:03 +00001546 GCCMIPSABIDirSuffix = MIPSABIDirSuffix;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001547 IsValid = true;
1548 }
1549 }
1550}
1551
Rafael Espindola1af7c212012-02-19 01:38:32 +00001552Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1553 const ArgList &Args)
Rafael Espindola84b588b2013-03-18 18:10:27 +00001554 : ToolChain(D, Triple, Args), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbar88979912010-08-01 22:29:51 +00001555 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001556 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001557 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001558}
1559
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001560Generic_GCC::~Generic_GCC() {
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001561}
1562
Rafael Espindola7cf32212013-03-20 03:05:54 +00001563Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +00001564 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001565 case Action::PreprocessJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001566 if (!Preprocess)
1567 Preprocess.reset(new tools::gcc::Preprocess(*this));
1568 return Preprocess.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001569 case Action::PrecompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001570 if (!Precompile)
1571 Precompile.reset(new tools::gcc::Precompile(*this));
1572 return Precompile.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001573 case Action::CompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001574 if (!Compile)
1575 Compile.reset(new tools::gcc::Compile(*this));
1576 return Compile.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +00001577 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001578 return ToolChain::getTool(AC);
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001579 }
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001580}
1581
Rafael Espindola7cf32212013-03-20 03:05:54 +00001582Tool *Generic_GCC::buildAssembler() const {
1583 return new tools::gcc::Assemble(*this);
1584}
1585
1586Tool *Generic_GCC::buildLinker() const {
1587 return new tools::gcc::Link(*this);
1588}
1589
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001590void Generic_GCC::printVerboseInfo(raw_ostream &OS) const {
1591 // Print the information about how we detected the GCC installation.
1592 GCCInstallation.print(OS);
1593}
1594
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001595bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001596 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001597}
1598
Chandler Carruth76a943b2012-11-19 03:52:03 +00001599bool Generic_GCC::isPICDefault() const {
1600 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001601}
1602
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001603bool Generic_GCC::isPIEDefault() const {
1604 return false;
1605}
1606
Chandler Carruth76a943b2012-11-19 03:52:03 +00001607bool Generic_GCC::isPICDefaultForced() const {
1608 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001609}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001610
Tony Linthicum76329bf2011-12-12 21:14:55 +00001611/// Hexagon Toolchain
1612
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001613std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1614
1615 // Locate the rest of the toolchain ...
1616 if (strlen(GCC_INSTALL_PREFIX))
1617 return std::string(GCC_INSTALL_PREFIX);
1618
1619 std::string InstallRelDir = InstalledDir + "/../../gnu";
1620 if (llvm::sys::fs::exists(InstallRelDir))
1621 return InstallRelDir;
1622
1623 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1624 if (llvm::sys::fs::exists(PrefixRelDir))
1625 return PrefixRelDir;
1626
1627 return InstallRelDir;
1628}
1629
Matthew Curtise689b052012-12-06 15:46:07 +00001630static void GetHexagonLibraryPaths(
1631 const ArgList &Args,
1632 const std::string Ver,
1633 const std::string MarchString,
1634 const std::string &InstalledDir,
1635 ToolChain::path_list *LibPaths)
1636{
1637 bool buildingLib = Args.hasArg(options::OPT_shared);
1638
1639 //----------------------------------------------------------------------------
1640 // -L Args
1641 //----------------------------------------------------------------------------
1642 for (arg_iterator
1643 it = Args.filtered_begin(options::OPT_L),
1644 ie = Args.filtered_end();
1645 it != ie;
1646 ++it) {
1647 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1648 LibPaths->push_back((*it)->getValue(i));
1649 }
1650
1651 //----------------------------------------------------------------------------
1652 // Other standard paths
1653 //----------------------------------------------------------------------------
1654 const std::string MarchSuffix = "/" + MarchString;
1655 const std::string G0Suffix = "/G0";
1656 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1657 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1658
1659 // lib/gcc/hexagon/...
1660 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1661 if (buildingLib) {
1662 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1663 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1664 }
1665 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1666 LibPaths->push_back(LibGCCHexagonDir + Ver);
1667
1668 // lib/gcc/...
1669 LibPaths->push_back(RootDir + "lib/gcc");
1670
1671 // hexagon/lib/...
1672 std::string HexagonLibDir = RootDir + "hexagon/lib";
1673 if (buildingLib) {
1674 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1675 LibPaths->push_back(HexagonLibDir + G0Suffix);
1676 }
1677 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1678 LibPaths->push_back(HexagonLibDir);
1679}
1680
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001681Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1682 const ArgList &Args)
1683 : Linux(D, Triple, Args) {
1684 const std::string InstalledDir(getDriver().getInstalledDir());
1685 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1686
1687 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1688 // program paths
1689 const std::string BinDir(GnuDir + "/bin");
1690 if (llvm::sys::fs::exists(BinDir))
1691 getProgramPaths().push_back(BinDir);
1692
1693 // Determine version of GCC libraries and headers to use.
1694 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1695 llvm::error_code ec;
1696 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1697 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1698 !ec && di != de; di = di.increment(ec)) {
1699 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1700 if (MaxVersion < cv)
1701 MaxVersion = cv;
1702 }
1703 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001704
1705 ToolChain::path_list *LibPaths= &getFilePaths();
1706
1707 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1708 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1709 // support 'linux' we'll need to fix this up
1710 LibPaths->clear();
1711
1712 GetHexagonLibraryPaths(
1713 Args,
1714 GetGCCLibAndIncVersion(),
1715 GetTargetCPU(Args),
1716 InstalledDir,
1717 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001718}
1719
1720Hexagon_TC::~Hexagon_TC() {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001721}
1722
Rafael Espindola7cf32212013-03-20 03:05:54 +00001723Tool *Hexagon_TC::buildAssembler() const {
1724 return new tools::hexagon::Assemble(*this);
1725}
1726
1727Tool *Hexagon_TC::buildLinker() const {
1728 return new tools::hexagon::Link(*this);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001729}
1730
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001731void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1732 ArgStringList &CC1Args) const {
1733 const Driver &D = getDriver();
1734
1735 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1736 DriverArgs.hasArg(options::OPT_nostdlibinc))
1737 return;
1738
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001739 std::string Ver(GetGCCLibAndIncVersion());
1740 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1741 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1742 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1743 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1744 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001745}
1746
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001747void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1748 ArgStringList &CC1Args) const {
1749
1750 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1751 DriverArgs.hasArg(options::OPT_nostdincxx))
1752 return;
1753
1754 const Driver &D = getDriver();
1755 std::string Ver(GetGCCLibAndIncVersion());
Rafael Espindola358256c2013-06-26 02:13:00 +00001756 SmallString<128> IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001757
Rafael Espindola358256c2013-06-26 02:13:00 +00001758 llvm::sys::path::append(IncludeDir, "hexagon/include/c++/");
1759 llvm::sys::path::append(IncludeDir, Ver);
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001760 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001761}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001762
Matthew Curtise689b052012-12-06 15:46:07 +00001763ToolChain::CXXStdlibType
1764Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1765 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1766 if (!A)
1767 return ToolChain::CST_Libstdcxx;
1768
1769 StringRef Value = A->getValue();
1770 if (Value != "libstdc++") {
1771 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1772 << A->getAsString(Args);
1773 }
1774
1775 return ToolChain::CST_Libstdcxx;
1776}
1777
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001778static int getHexagonVersion(const ArgList &Args) {
1779 Arg *A = Args.getLastArg(options::OPT_march_EQ, options::OPT_mcpu_EQ);
1780 // Select the default CPU (v4) if none was given.
1781 if (!A)
1782 return 4;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001783
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001784 // FIXME: produce errors if we cannot parse the version.
1785 StringRef WhichHexagon = A->getValue();
1786 if (WhichHexagon.startswith("hexagonv")) {
1787 int Val;
1788 if (!WhichHexagon.substr(sizeof("hexagonv") - 1).getAsInteger(10, Val))
1789 return Val;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001790 }
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001791 if (WhichHexagon.startswith("v")) {
1792 int Val;
1793 if (!WhichHexagon.substr(1).getAsInteger(10, Val))
1794 return Val;
1795 }
1796
1797 // FIXME: should probably be an error.
1798 return 4;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001799}
1800
1801StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1802{
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001803 int V = getHexagonVersion(Args);
1804 // FIXME: We don't support versions < 4. We should error on them.
1805 switch (V) {
1806 default:
1807 llvm_unreachable("Unexpected version");
1808 case 5:
1809 return "v5";
1810 case 4:
1811 return "v4";
1812 case 3:
1813 return "v3";
1814 case 2:
1815 return "v2";
1816 case 1:
1817 return "v1";
Matthew Curtisf10a5952012-12-06 14:16:43 +00001818 }
Matthew Curtisf10a5952012-12-06 14:16:43 +00001819}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001820// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001821
Chris Lattner09797542010-03-04 21:07:38 +00001822/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1823/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1824/// Currently does not support anything else but compilation.
1825
Rafael Espindola84b588b2013-03-18 18:10:27 +00001826TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
1827 const ArgList &Args)
1828 : ToolChain(D, Triple, Args) {
Chris Lattner09797542010-03-04 21:07:38 +00001829 // Path mangling to find libexec
1830 std::string Path(getDriver().Dir);
1831
1832 Path += "/../libexec";
1833 getProgramPaths().push_back(Path);
1834}
1835
1836TCEToolChain::~TCEToolChain() {
Chris Lattner09797542010-03-04 21:07:38 +00001837}
1838
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001839bool TCEToolChain::IsMathErrnoDefault() const {
1840 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001841}
1842
Chandler Carruth76a943b2012-11-19 03:52:03 +00001843bool TCEToolChain::isPICDefault() const {
1844 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001845}
1846
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001847bool TCEToolChain::isPIEDefault() const {
1848 return false;
1849}
1850
Chandler Carruth76a943b2012-11-19 03:52:03 +00001851bool TCEToolChain::isPICDefaultForced() const {
1852 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001853}
1854
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001855/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1856
Rafael Espindola1af7c212012-02-19 01:38:32 +00001857OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1858 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001859 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001860 getFilePaths().push_back("/usr/lib");
1861}
1862
Rafael Espindola7cf32212013-03-20 03:05:54 +00001863Tool *OpenBSD::buildAssembler() const {
1864 return new tools::openbsd::Assemble(*this);
1865}
1866
1867Tool *OpenBSD::buildLinker() const {
1868 return new tools::openbsd::Link(*this);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001869}
1870
Eli Friedman9fa28852012-08-08 23:57:20 +00001871/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1872
1873Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1874 : Generic_ELF(D, Triple, Args) {
1875 getFilePaths().push_back(getDriver().Dir + "/../lib");
1876 getFilePaths().push_back("/usr/lib");
1877}
1878
Rafael Espindola7cf32212013-03-20 03:05:54 +00001879Tool *Bitrig::buildAssembler() const {
1880 return new tools::bitrig::Assemble(*this);
1881}
1882
1883Tool *Bitrig::buildLinker() const {
1884 return new tools::bitrig::Link(*this);
Eli Friedman9fa28852012-08-08 23:57:20 +00001885}
1886
1887void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1888 ArgStringList &CC1Args) const {
1889 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1890 DriverArgs.hasArg(options::OPT_nostdincxx))
1891 return;
1892
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001893 switch (GetCXXStdlibType(DriverArgs)) {
1894 case ToolChain::CST_Libcxx:
1895 addSystemInclude(DriverArgs, CC1Args,
1896 getDriver().SysRoot + "/usr/include/c++/");
1897 break;
1898 case ToolChain::CST_Libstdcxx:
1899 addSystemInclude(DriverArgs, CC1Args,
1900 getDriver().SysRoot + "/usr/include/c++/stdc++");
1901 addSystemInclude(DriverArgs, CC1Args,
1902 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001903
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001904 StringRef Triple = getTriple().str();
1905 if (Triple.startswith("amd64"))
1906 addSystemInclude(DriverArgs, CC1Args,
1907 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1908 Triple.substr(5));
1909 else
1910 addSystemInclude(DriverArgs, CC1Args,
1911 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1912 Triple);
1913 break;
1914 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001915}
1916
1917void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1918 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001919 switch (GetCXXStdlibType(Args)) {
1920 case ToolChain::CST_Libcxx:
1921 CmdArgs.push_back("-lc++");
1922 CmdArgs.push_back("-lcxxrt");
1923 // Include supc++ to provide Unwind until provided by libcxx.
1924 CmdArgs.push_back("-lgcc");
1925 break;
1926 case ToolChain::CST_Libstdcxx:
1927 CmdArgs.push_back("-lstdc++");
1928 break;
1929 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001930}
1931
Daniel Dunbare24297c2009-03-30 21:06:03 +00001932/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1933
Rafael Espindola1af7c212012-02-19 01:38:32 +00001934FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1935 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001936
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001937 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1938 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001939 if ((Triple.getArch() == llvm::Triple::x86 ||
1940 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001941 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001942 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1943 else
1944 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001945}
1946
David Chisnall867ccd82013-11-09 15:10:56 +00001947ToolChain::CXXStdlibType
1948FreeBSD::GetCXXStdlibType(const ArgList &Args) const {
1949 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
1950 StringRef Value = A->getValue();
1951 if (Value == "libstdc++")
1952 return ToolChain::CST_Libstdcxx;
1953 if (Value == "libc++")
1954 return ToolChain::CST_Libcxx;
1955
1956 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1957 << A->getAsString(Args);
1958 }
1959 if (getTriple().getOSMajorVersion() >= 10)
1960 return ToolChain::CST_Libcxx;
1961 return ToolChain::CST_Libstdcxx;
1962}
1963
1964void FreeBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1965 ArgStringList &CC1Args) const {
1966 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1967 DriverArgs.hasArg(options::OPT_nostdincxx))
1968 return;
1969
1970 switch (GetCXXStdlibType(DriverArgs)) {
1971 case ToolChain::CST_Libcxx:
1972 addSystemInclude(DriverArgs, CC1Args,
1973 getDriver().SysRoot + "/usr/include/c++/v1");
1974 break;
1975 case ToolChain::CST_Libstdcxx:
1976 addSystemInclude(DriverArgs, CC1Args,
1977 getDriver().SysRoot + "/usr/include/c++/4.2");
1978 addSystemInclude(DriverArgs, CC1Args,
1979 getDriver().SysRoot + "/usr/include/c++/4.2/backward");
1980 break;
1981 }
1982}
1983
Rafael Espindola7cf32212013-03-20 03:05:54 +00001984Tool *FreeBSD::buildAssembler() const {
1985 return new tools::freebsd::Assemble(*this);
1986}
1987
1988Tool *FreeBSD::buildLinker() const {
1989 return new tools::freebsd::Link(*this);
Daniel Dunbare24297c2009-03-30 21:06:03 +00001990}
Daniel Dunbarcc912342009-05-02 18:28:39 +00001991
Rafael Espindola0f207ed2012-12-13 04:17:14 +00001992bool FreeBSD::UseSjLjExceptions() const {
1993 // FreeBSD uses SjLj exceptions on ARM oabi.
1994 switch (getTriple().getEnvironment()) {
1995 case llvm::Triple::GNUEABI:
1996 case llvm::Triple::EABI:
1997 return false;
1998
1999 default:
2000 return (getTriple().getArch() == llvm::Triple::arm ||
2001 getTriple().getArch() == llvm::Triple::thumb);
2002 }
2003}
2004
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002005/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
2006
Rafael Espindola1af7c212012-02-19 01:38:32 +00002007NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2008 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002009
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00002010 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00002011 // When targeting a 32-bit platform, try the special directory used on
2012 // 64-bit hosts, and only fall back to the main library directory if that
2013 // doesn't work.
2014 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
2015 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00002016 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00002017 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00002018
2019 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002020 }
2021}
2022
Rafael Espindola7cf32212013-03-20 03:05:54 +00002023Tool *NetBSD::buildAssembler() const {
2024 return new tools::netbsd::Assemble(*this);
2025}
2026
2027Tool *NetBSD::buildLinker() const {
2028 return new tools::netbsd::Link(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002029}
2030
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00002031ToolChain::CXXStdlibType
2032NetBSD::GetCXXStdlibType(const ArgList &Args) const {
2033 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
2034 StringRef Value = A->getValue();
2035 if (Value == "libstdc++")
2036 return ToolChain::CST_Libstdcxx;
2037 if (Value == "libc++")
2038 return ToolChain::CST_Libcxx;
2039
2040 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2041 << A->getAsString(Args);
2042 }
2043
Joerg Sonnenbergera4435632013-10-14 20:13:05 +00002044 unsigned Major, Minor, Micro;
2045 getTriple().getOSVersion(Major, Minor, Micro);
2046 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 23) || Major == 0) {
2047 if (getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64)
2048 return ToolChain::CST_Libcxx;
2049 }
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00002050 return ToolChain::CST_Libstdcxx;
2051}
2052
2053void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2054 ArgStringList &CC1Args) const {
2055 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2056 DriverArgs.hasArg(options::OPT_nostdincxx))
2057 return;
2058
2059 switch (GetCXXStdlibType(DriverArgs)) {
2060 case ToolChain::CST_Libcxx:
2061 addSystemInclude(DriverArgs, CC1Args,
2062 getDriver().SysRoot + "/usr/include/c++/");
2063 break;
2064 case ToolChain::CST_Libstdcxx:
2065 addSystemInclude(DriverArgs, CC1Args,
2066 getDriver().SysRoot + "/usr/include/g++");
2067 addSystemInclude(DriverArgs, CC1Args,
2068 getDriver().SysRoot + "/usr/include/g++/backward");
2069 break;
2070 }
2071}
2072
Chris Lattner3e2ee142010-07-07 16:01:42 +00002073/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
2074
Rafael Espindola1af7c212012-02-19 01:38:32 +00002075Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2076 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00002077 getFilePaths().push_back(getDriver().Dir + "/../lib");
2078 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00002079}
2080
Rafael Espindola7cf32212013-03-20 03:05:54 +00002081Tool *Minix::buildAssembler() const {
2082 return new tools::minix::Assemble(*this);
2083}
2084
2085Tool *Minix::buildLinker() const {
2086 return new tools::minix::Link(*this);
Chris Lattner3e2ee142010-07-07 16:01:42 +00002087}
2088
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002089/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
2090
Rafael Espindola1af7c212012-02-19 01:38:32 +00002091AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
2092 const ArgList &Args)
2093 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002094
Daniel Dunbar88979912010-08-01 22:29:51 +00002095 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002096 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002097 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002098
Daniel Dunbar083edf72009-12-21 18:54:17 +00002099 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002100 getFilePaths().push_back("/usr/lib");
2101 getFilePaths().push_back("/usr/sfw/lib");
2102 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00002103 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002104
2105}
2106
Rafael Espindola7cf32212013-03-20 03:05:54 +00002107Tool *AuroraUX::buildAssembler() const {
2108 return new tools::auroraux::Assemble(*this);
2109}
2110
2111Tool *AuroraUX::buildLinker() const {
2112 return new tools::auroraux::Link(*this);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002113}
2114
David Chisnallf571cde2012-02-15 13:39:01 +00002115/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2116
Rafael Espindola1af7c212012-02-19 01:38:32 +00002117Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2118 const ArgList &Args)
2119 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00002120
2121 getProgramPaths().push_back(getDriver().getInstalledDir());
2122 if (getDriver().getInstalledDir() != getDriver().Dir)
2123 getProgramPaths().push_back(getDriver().Dir);
2124
2125 getFilePaths().push_back(getDriver().Dir + "/../lib");
2126 getFilePaths().push_back("/usr/lib");
2127}
2128
Rafael Espindola7cf32212013-03-20 03:05:54 +00002129Tool *Solaris::buildAssembler() const {
2130 return new tools::solaris::Assemble(*this);
2131}
2132
2133Tool *Solaris::buildLinker() const {
2134 return new tools::solaris::Link(*this);
David Chisnallf571cde2012-02-15 13:39:01 +00002135}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002136
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002137/// Distribution (very bare-bones at the moment).
Eli Friedman5cd659f2009-05-26 07:52:18 +00002138
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002139enum Distro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002140 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002141 DebianLenny,
2142 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00002143 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002144 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00002145 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00002146 RHEL4,
2147 RHEL5,
2148 RHEL6,
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002149 Fedora,
Rafael Espindola10a63c22013-07-03 14:14:00 +00002150 OpenSUSE,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00002151 UbuntuHardy,
2152 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00002153 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00002154 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002155 UbuntuLucid,
2156 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00002157 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00002158 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002159 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00002160 UbuntuQuantal,
2161 UbuntuRaring,
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002162 UbuntuSaucy,
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002163 UbuntuTrusty,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002164 UnknownDistro
2165};
2166
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002167static bool IsRedhat(enum Distro Distro) {
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002168 return Distro == Fedora || (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002169}
2170
Rafael Espindola10a63c22013-07-03 14:14:00 +00002171static bool IsOpenSUSE(enum Distro Distro) {
2172 return Distro == OpenSUSE;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002173}
2174
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002175static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002176 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002177}
2178
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002179static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002180 return Distro >= UbuntuHardy && Distro <= UbuntuTrusty;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002181}
2182
Rafael Espindolaa8398552013-10-28 23:14:34 +00002183static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00002184 OwningPtr<llvm::MemoryBuffer> File;
Rafael Espindolaa8398552013-10-28 23:14:34 +00002185 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002186 StringRef Data = File.get()->getBuffer();
2187 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002188 Data.split(Lines, "\n");
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002189 Distro Version = UnknownDistro;
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002190 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2191 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002192 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002193 .Case("hardy", UbuntuHardy)
2194 .Case("intrepid", UbuntuIntrepid)
2195 .Case("jaunty", UbuntuJaunty)
2196 .Case("karmic", UbuntuKarmic)
2197 .Case("lucid", UbuntuLucid)
2198 .Case("maverick", UbuntuMaverick)
2199 .Case("natty", UbuntuNatty)
2200 .Case("oneiric", UbuntuOneiric)
2201 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002202 .Case("quantal", UbuntuQuantal)
2203 .Case("raring", UbuntuRaring)
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002204 .Case("saucy", UbuntuSaucy)
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002205 .Case("trusty", UbuntuTrusty)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002206 .Default(UnknownDistro);
2207 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002208 }
2209
Rafael Espindolaa8398552013-10-28 23:14:34 +00002210 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002211 StringRef Data = File.get()->getBuffer();
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002212 if (Data.startswith("Fedora release"))
2213 return Fedora;
Chris Lattner84e38552011-05-22 05:36:06 +00002214 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002215 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002216 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002217 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002218 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002219 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002220 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002221 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002222 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002223 Data.find("release 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002224 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002225 return UnknownDistro;
2226 }
2227
Rafael Espindolaa8398552013-10-28 23:14:34 +00002228 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002229 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002230 if (Data[0] == '5')
2231 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002232 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002233 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002234 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002235 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002236 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2237 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002238 return UnknownDistro;
2239 }
2240
Rafael Espindolaa8398552013-10-28 23:14:34 +00002241 if (llvm::sys::fs::exists("/etc/SuSE-release"))
Rafael Espindola10a63c22013-07-03 14:14:00 +00002242 return OpenSUSE;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002243
Rafael Espindolaa8398552013-10-28 23:14:34 +00002244 if (llvm::sys::fs::exists("/etc/exherbo-release"))
Rafael Espindola12479842010-11-11 02:07:13 +00002245 return Exherbo;
2246
Rafael Espindolaa8398552013-10-28 23:14:34 +00002247 if (llvm::sys::fs::exists("/etc/arch-release"))
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002248 return ArchLinux;
2249
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002250 return UnknownDistro;
2251}
2252
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002253/// \brief Get our best guess at the multiarch triple for a target.
2254///
2255/// Debian-based systems are starting to use a multiarch setup where they use
2256/// a target-triple directory in the library and header search paths.
2257/// Unfortunately, this triple does not align with the vanilla target triple,
2258/// so we provide a rough mapping here.
2259static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2260 StringRef SysRoot) {
2261 // For most architectures, just use whatever we have rather than trying to be
2262 // clever.
2263 switch (TargetTriple.getArch()) {
2264 default:
2265 return TargetTriple.str();
2266
2267 // We use the existence of '/lib/<triple>' as a directory to detect some
2268 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002269 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2270 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002271 case llvm::Triple::arm:
2272 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002273 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2274 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2275 return "arm-linux-gnueabihf";
2276 } else {
2277 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2278 return "arm-linux-gnueabi";
2279 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002280 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002281 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002282 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2283 return "i386-linux-gnu";
2284 return TargetTriple.str();
2285 case llvm::Triple::x86_64:
2286 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2287 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002288 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002289 case llvm::Triple::aarch64:
2290 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2291 return "aarch64-linux-gnu";
Tim Northoverfbb56b72013-06-13 22:54:55 +00002292 return TargetTriple.str();
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002293 case llvm::Triple::mips:
2294 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2295 return "mips-linux-gnu";
2296 return TargetTriple.str();
2297 case llvm::Triple::mipsel:
2298 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2299 return "mipsel-linux-gnu";
2300 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002301 case llvm::Triple::ppc:
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00002302 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2303 return "powerpc-linux-gnuspe";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002304 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2305 return "powerpc-linux-gnu";
2306 return TargetTriple.str();
2307 case llvm::Triple::ppc64:
2308 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2309 return "powerpc64-linux-gnu";
Bill Schmidt778d3872013-07-26 01:36:11 +00002310 case llvm::Triple::ppc64le:
2311 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
2312 return "powerpc64le-linux-gnu";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002313 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002314 }
2315}
2316
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002317static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2318 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2319}
2320
Simon Atanasyand4413882012-09-14 11:27:24 +00002321static StringRef getMultilibDir(const llvm::Triple &Triple,
2322 const ArgList &Args) {
Chandler Carruthda797042013-10-29 10:27:30 +00002323 if (isMipsArch(Triple.getArch())) {
2324 // lib32 directory has a special meaning on MIPS targets.
2325 // It contains N32 ABI binaries. Use this folder if produce
2326 // code for N32 ABI only.
2327 if (hasMipsN32ABIArg(Args))
2328 return "lib32";
2329 return Triple.isArch32Bit() ? "lib" : "lib64";
2330 }
Simon Atanasyand4413882012-09-14 11:27:24 +00002331
Chandler Carruthda797042013-10-29 10:27:30 +00002332 // It happens that only x86 and PPC use the 'lib32' variant of multilib, and
2333 // using that variant while targeting other architectures causes problems
2334 // because the libraries are laid out in shared system roots that can't cope
2335 // with a 'lib32' multilib search path being considered. So we only enable
2336 // them when we know we may need it.
2337 //
2338 // FIXME: This is a bit of a hack. We should really unify this code for
2339 // reasoning about multilib spellings with the lib dir spellings in the
2340 // GCCInstallationDetector, but that is a more significant refactoring.
2341 if (Triple.getArch() == llvm::Triple::x86 ||
2342 Triple.getArch() == llvm::Triple::ppc)
Simon Atanasyand4413882012-09-14 11:27:24 +00002343 return "lib32";
2344
2345 return Triple.isArch32Bit() ? "lib" : "lib64";
2346}
2347
Rafael Espindola1af7c212012-02-19 01:38:32 +00002348Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2349 : Generic_ELF(D, Triple, Args) {
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002350 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyana0d89572013-10-05 14:37:55 +00002351 std::string SysRoot = computeSysRoot();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002352
Rafael Espindola10a63c22013-07-03 14:14:00 +00002353 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
Chandler Carruthd6c62b62013-06-20 23:37:54 +00002354 // least) put various tools in a triple-prefixed directory off of the parent
2355 // of the GCC installation. We use the GCC triple here to ensure that we end
2356 // up with tools that support the same amount of cross compiling as the
2357 // detected GCC installation. For example, if we find a GCC installation
2358 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
2359 // used to target i386.
2360 // FIXME: This seems unlikely to be Linux-specific.
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002361 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002362 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002363 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002364
2365 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002366
Rafael Espindolaa8398552013-10-28 23:14:34 +00002367 Distro Distro = DetectDistro(Arch);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002368
Rafael Espindola10a63c22013-07-03 14:14:00 +00002369 if (IsOpenSUSE(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002370 ExtraOpts.push_back("-z");
2371 ExtraOpts.push_back("relro");
2372 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002373
Douglas Gregord9bb1522011-03-06 19:11:49 +00002374 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002375 ExtraOpts.push_back("-X");
2376
Logan Chienc6fd8202012-09-02 09:30:11 +00002377 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002378 const bool IsMips = isMipsArch(Arch);
2379
2380 if (IsMips && !SysRoot.empty())
2381 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002382
Chandler Carruth0b842912011-12-09 04:45:18 +00002383 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2384 // and the MIPS ABI require .dynsym to be sorted in different ways.
2385 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2386 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002387 // Android loader does not support .gnu.hash.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002388 if (!IsMips && !IsAndroid) {
Rafael Espindola10a63c22013-07-03 14:14:00 +00002389 if (IsRedhat(Distro) || IsOpenSUSE(Distro) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002390 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002391 ExtraOpts.push_back("--hash-style=gnu");
2392
Rafael Espindola10a63c22013-07-03 14:14:00 +00002393 if (IsDebian(Distro) || IsOpenSUSE(Distro) || Distro == UbuntuLucid ||
Chandler Carruth0b842912011-12-09 04:45:18 +00002394 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2395 ExtraOpts.push_back("--hash-style=both");
2396 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002397
Chris Lattner84e38552011-05-22 05:36:06 +00002398 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002399 ExtraOpts.push_back("--no-add-needed");
2400
Eli Friedmanf7600942011-06-02 21:36:53 +00002401 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola10a63c22013-07-03 14:14:00 +00002402 Distro == DebianJessie || IsOpenSUSE(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002403 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002404 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002405 ExtraOpts.push_back("--build-id");
2406
Rafael Espindola10a63c22013-07-03 14:14:00 +00002407 if (IsOpenSUSE(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002408 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002409
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002410 // The selection of paths to try here is designed to match the patterns which
2411 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2412 // This was determined by running GCC in a fake filesystem, creating all
2413 // possible permutations of these directories, and seeing which ones it added
2414 // to the link paths.
2415 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002416
Rafael Espindolaa8398552013-10-28 23:14:34 +00002417 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002418 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002419
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002420 // Add the multilib suffixed paths where they are available.
2421 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002422 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002423 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002424
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002425 // Sourcery CodeBench MIPS toolchain holds some libraries under
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002426 // a biarch-like suffix of the GCC installation.
2427 //
2428 // FIXME: It would be cleaner to model this as a variant of bi-arch. IE,
2429 // instead of a '64' biarch suffix it would be 'el' or something.
Simon Atanasyan068e0fd2013-10-09 12:12:39 +00002430 if (IsAndroid && IsMips && isMips32r2(Args)) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002431 assert(GCCInstallation.getBiarchSuffix().empty() &&
2432 "Unexpected bi-arch suffix");
2433 addPathIfExists(GCCInstallation.getInstallPath() + "/mips-r2", Paths);
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002434 } else {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002435 addPathIfExists((GCCInstallation.getInstallPath() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002436 GCCInstallation.getMIPSABIDirSuffix() +
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002437 GCCInstallation.getBiarchSuffix()),
2438 Paths);
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002439 }
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002440
2441 // GCC cross compiling toolchains will install target libraries which ship
2442 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
2443 // any part of the GCC installation in
2444 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
2445 // debatable, but is the reality today. We need to search this tree even
2446 // when we have a sysroot somewhere else. It is the responsibility of
2447 // whomever is doing the cross build targetting a sysroot using a GCC
2448 // installation that is *not* within the system root to ensure two things:
2449 //
2450 // 1) Any DSOs that are linked in from this tree or from the install path
2451 // above must be preasant on the system root and found via an
2452 // appropriate rpath.
2453 // 2) There must not be libraries installed into
2454 // <prefix>/<triple>/<libdir> unless they should be preferred over
2455 // those within the system root.
2456 //
2457 // Note that this matches the GCC behavior. See the below comment for where
2458 // Clang diverges from GCC's behavior.
2459 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib +
Chandler Carruth8677d922013-10-29 08:53:03 +00002460 GCCInstallation.getMIPSABIDirSuffix(),
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002461 Paths);
2462
Chandler Carruth69a125b2012-04-06 16:32:06 +00002463 // If the GCC installation we found is inside of the sysroot, we want to
2464 // prefer libraries installed in the parent prefix of the GCC installation.
2465 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002466 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002467 // This usually happens when there is an external cross compiler on the
2468 // host system, and a more minimal sysroot available that is the target of
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002469 // the cross. Note that GCC does include some of these directories in some
2470 // configurations but this seems somewhere between questionable and simply
2471 // a bug.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002472 if (StringRef(LibPath).startswith(SysRoot)) {
Chandler Carruth69a125b2012-04-06 16:32:06 +00002473 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2474 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2475 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002476 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002477 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2478 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2479 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2480 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2481
Chandler Carruthb427c562013-06-22 11:35:51 +00002482 // Try walking via the GCC triple path in case of biarch or multiarch GCC
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002483 // installations with strange symlinks.
Rafael Espindolab6250162013-10-25 17:06:04 +00002484 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002485 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002486 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002487
Rafael Espindolab6250162013-10-25 17:06:04 +00002488 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002489 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002490 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Rafael Espindolaa8398552013-10-28 23:14:34 +00002491 if (!GCCInstallation.getBiarchSuffix().empty())
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002492 addPathIfExists(GCCInstallation.getInstallPath() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002493 GCCInstallation.getMIPSABIDirSuffix(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002494
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002495 // See comments above on the multilib variant for details of why this is
2496 // included even from outside the sysroot.
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002497 addPathIfExists(LibPath + "/../" + GCCTriple.str() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002498 "/lib" + GCCInstallation.getMIPSABIDirSuffix(), Paths);
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002499
2500 // See comments above on the multilib variant for details of why this is
2501 // only included from within the sysroot.
2502 if (StringRef(LibPath).startswith(SysRoot))
Chandler Carruth69a125b2012-04-06 16:32:06 +00002503 addPathIfExists(LibPath, Paths);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002504 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002505 addPathIfExists(SysRoot + "/lib", Paths);
2506 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002507}
2508
Roman Divackyf0d7f942013-11-10 09:31:43 +00002509bool FreeBSD::HasNativeLLVMSupport() const {
2510 return true;
2511}
2512
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002513bool Linux::HasNativeLLVMSupport() const {
2514 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002515}
2516
Rafael Espindola7cf32212013-03-20 03:05:54 +00002517Tool *Linux::buildLinker() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002518 return new tools::gnutools::Link(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00002519}
2520
2521Tool *Linux::buildAssembler() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002522 return new tools::gnutools::Assemble(*this);
Rafael Espindola92b00932010-08-10 00:25:48 +00002523}
2524
Chandler Carruth05fb5852012-11-21 23:40:23 +00002525void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2526 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002527 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Benjamin Kramer604e8482013-08-09 17:17:48 +00002528 bool UseInitArrayDefault =
2529 !V.isOlderThan(4, 7, 0) ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002530 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002531 getTriple().getEnvironment() == llvm::Triple::Android;
2532 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2533 options::OPT_fno_use_init_array,
2534 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002535 CC1Args.push_back("-fuse-init-array");
2536}
2537
Simon Atanasyana0d89572013-10-05 14:37:55 +00002538std::string Linux::computeSysRoot() const {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002539 if (!getDriver().SysRoot.empty())
2540 return getDriver().SysRoot;
2541
2542 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
2543 return std::string();
2544
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002545 // Standalone MIPS toolchains use different names for sysroot folder
2546 // and put it into different places. Here we try to check some known
2547 // variants.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002548
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002549 const StringRef InstallDir = GCCInstallation.getInstallPath();
2550 const StringRef TripleStr = GCCInstallation.getTriple().str();
Chandler Carruth8677d922013-10-29 08:53:03 +00002551 const StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002552
Chandler Carruth8677d922013-10-29 08:53:03 +00002553 std::string Path = (InstallDir + "/../../../../" + TripleStr + "/libc" +
2554 MIPSABIDirSuffix).str();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002555
2556 if (llvm::sys::fs::exists(Path))
2557 return Path;
2558
Chandler Carruth8677d922013-10-29 08:53:03 +00002559 Path = (InstallDir + "/../../../../sysroot" + MIPSABIDirSuffix).str();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002560
2561 if (llvm::sys::fs::exists(Path))
2562 return Path;
2563
2564 return std::string();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002565}
2566
Chandler Carrutha796f532011-11-05 20:17:13 +00002567void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2568 ArgStringList &CC1Args) const {
2569 const Driver &D = getDriver();
Simon Atanasyana0d89572013-10-05 14:37:55 +00002570 std::string SysRoot = computeSysRoot();
Chandler Carrutha796f532011-11-05 20:17:13 +00002571
2572 if (DriverArgs.hasArg(options::OPT_nostdinc))
2573 return;
2574
2575 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002576 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002577
2578 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola358256c2013-06-26 02:13:00 +00002579 SmallString<128> P(D.ResourceDir);
2580 llvm::sys::path::append(P, "include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002581 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002582 }
2583
2584 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2585 return;
2586
2587 // Check for configure-time C include directories.
2588 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2589 if (CIncludeDirs != "") {
2590 SmallVector<StringRef, 5> dirs;
2591 CIncludeDirs.split(dirs, ":");
2592 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2593 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002594 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? SysRoot : "";
Chandler Carrutha796f532011-11-05 20:17:13 +00002595 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2596 }
2597 return;
2598 }
2599
2600 // Lacking those, try to detect the correct set of system includes for the
2601 // target triple.
2602
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002603 // Sourcery CodeBench and modern FSF Mips toolchains put extern C
2604 // system includes under three additional directories.
2605 if (GCCInstallation.isValid() && isMipsArch(getTriple().getArch())) {
Chandler Carruthb427c562013-06-22 11:35:51 +00002606 addExternCSystemIncludeIfExists(
2607 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + "/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002608
Chandler Carruthb427c562013-06-22 11:35:51 +00002609 addExternCSystemIncludeIfExists(
2610 DriverArgs, CC1Args,
2611 GCCInstallation.getInstallPath() + "/../../../../" +
2612 GCCInstallation.getTriple().str() + "/libc/usr/include");
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002613
2614 addExternCSystemIncludeIfExists(
2615 DriverArgs, CC1Args,
2616 GCCInstallation.getInstallPath() + "/../../../../sysroot/usr/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002617 }
2618
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002619 // Implement generic Debian multiarch support.
2620 const StringRef X86_64MultiarchIncludeDirs[] = {
2621 "/usr/include/x86_64-linux-gnu",
2622
2623 // FIXME: These are older forms of multiarch. It's not clear that they're
2624 // in use in any released version of Debian, so we should consider
2625 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002626 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002627 };
2628 const StringRef X86MultiarchIncludeDirs[] = {
2629 "/usr/include/i386-linux-gnu",
2630
2631 // FIXME: These are older forms of multiarch. It's not clear that they're
2632 // in use in any released version of Debian, so we should consider
2633 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002634 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002635 "/usr/include/i486-linux-gnu"
2636 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002637 const StringRef AArch64MultiarchIncludeDirs[] = {
2638 "/usr/include/aarch64-linux-gnu"
2639 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002640 const StringRef ARMMultiarchIncludeDirs[] = {
2641 "/usr/include/arm-linux-gnueabi"
2642 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002643 const StringRef ARMHFMultiarchIncludeDirs[] = {
2644 "/usr/include/arm-linux-gnueabihf"
2645 };
Eli Friedman7771c832011-11-11 03:05:19 +00002646 const StringRef MIPSMultiarchIncludeDirs[] = {
2647 "/usr/include/mips-linux-gnu"
2648 };
2649 const StringRef MIPSELMultiarchIncludeDirs[] = {
2650 "/usr/include/mipsel-linux-gnu"
2651 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002652 const StringRef PPCMultiarchIncludeDirs[] = {
2653 "/usr/include/powerpc-linux-gnu"
2654 };
2655 const StringRef PPC64MultiarchIncludeDirs[] = {
2656 "/usr/include/powerpc64-linux-gnu"
2657 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002658 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002659 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002660 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002661 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002662 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002663 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2664 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002665 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002666 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2667 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2668 else
2669 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002670 } else if (getTriple().getArch() == llvm::Triple::mips) {
2671 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2672 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2673 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002674 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2675 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2676 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2677 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002678 }
2679 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2680 E = MultiarchIncludeDirs.end();
2681 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002682 if (llvm::sys::fs::exists(SysRoot + *I)) {
2683 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + *I);
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002684 break;
2685 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002686 }
2687
2688 if (getTriple().getOS() == llvm::Triple::RTEMS)
2689 return;
2690
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002691 // Add an include of '/include' directly. This isn't provided by default by
2692 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2693 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002694 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002695
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002696 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002697}
2698
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002699/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002700/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2701 const ArgList &DriverArgs,
2702 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002703 if (!llvm::sys::fs::exists(Base))
2704 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002705 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002706 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002707 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002708 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002709}
2710
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002711/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2712/// libstdc++ installation.
2713/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2714 Twine TargetArchDir,
Chandler Carruth8677d922013-10-29 08:53:03 +00002715 Twine BiarchSuffix,
2716 Twine MIPSABIDirSuffix,
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002717 const ArgList &DriverArgs,
2718 ArgStringList &CC1Args) {
Chandler Carruth8677d922013-10-29 08:53:03 +00002719 if (!addLibStdCXXIncludePaths(Base + Suffix,
2720 TargetArchDir + MIPSABIDirSuffix + BiarchSuffix,
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002721 DriverArgs, CC1Args))
2722 return false;
2723
2724 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
Chandler Carruth8677d922013-10-29 08:53:03 +00002725 + MIPSABIDirSuffix + BiarchSuffix);
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002726 return true;
2727}
2728
Chandler Carrutha796f532011-11-05 20:17:13 +00002729void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2730 ArgStringList &CC1Args) const {
2731 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2732 DriverArgs.hasArg(options::OPT_nostdincxx))
2733 return;
2734
Chandler Carruthf4701732011-11-07 09:01:17 +00002735 // Check if libc++ has been enabled and provide its include paths if so.
2736 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2737 // libc++ is always installed at a fixed path on Linux currently.
2738 addSystemInclude(DriverArgs, CC1Args,
2739 getDriver().SysRoot + "/usr/include/c++/v1");
2740 return;
2741 }
2742
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002743 // We need a detected GCC installation on Linux to provide libstdc++'s
2744 // headers. We handled the libc++ case above.
2745 if (!GCCInstallation.isValid())
2746 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002747
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002748 // By default, look for the C++ headers in an include directory adjacent to
2749 // the lib directory of the GCC installation. Note that this is expect to be
2750 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2751 StringRef LibDir = GCCInstallation.getParentLibPath();
2752 StringRef InstallDir = GCCInstallation.getInstallPath();
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002753 StringRef TripleStr = GCCInstallation.getTriple().str();
Chandler Carruth8677d922013-10-29 08:53:03 +00002754 StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix();
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002755 StringRef BiarchSuffix = GCCInstallation.getBiarchSuffix();
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002756 const GCCVersion &Version = GCCInstallation.getVersion();
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002757
Chandler Carruth8677d922013-10-29 08:53:03 +00002758 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2759 "/c++/" + Version.Text, TripleStr, BiarchSuffix,
2760 MIPSABIDirSuffix, DriverArgs, CC1Args))
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002761 return;
2762
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002763 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002764 // Gentoo is weird and places its headers inside the GCC install, so if the
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002765 // first attempt to find the headers fails, try these patterns.
2766 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
2767 Version.MinorStr,
2768 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002769 // Android standalone toolchain has C++ headers in yet another place.
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002770 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
Hal Finkelf3587912012-09-18 22:25:07 +00002771 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2772 // without a subdirectory corresponding to the gcc version.
2773 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002774 };
2775
2776 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
Chandler Carruth8677d922013-10-29 08:53:03 +00002777 if (addLibStdCXXIncludePaths(IncludePathCandidates[i],
2778 TripleStr + MIPSABIDirSuffix + BiarchSuffix,
2779 DriverArgs, CC1Args))
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002780 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002781 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002782}
2783
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002784bool Linux::isPIEDefault() const {
Peter Collingbourne32701642013-11-01 18:16:25 +00002785 return getSanitizerArgs().hasZeroBaseShadow();
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002786}
2787
Daniel Dunbarcc912342009-05-02 18:28:39 +00002788/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2789
Rafael Espindola1af7c212012-02-19 01:38:32 +00002790DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2791 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002792
2793 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002794 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002795 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002796 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002797
Daniel Dunbar083edf72009-12-21 18:54:17 +00002798 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002799 getFilePaths().push_back("/usr/lib");
John McCall65b8da02013-04-11 22:55:55 +00002800 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
2801 getFilePaths().push_back("/usr/lib/gcc47");
2802 else
2803 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002804}
2805
Rafael Espindola7cf32212013-03-20 03:05:54 +00002806Tool *DragonFly::buildAssembler() const {
2807 return new tools::dragonfly::Assemble(*this);
2808}
2809
2810Tool *DragonFly::buildLinker() const {
2811 return new tools::dragonfly::Link(*this);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002812}
Robert Lyttoncf1dd692013-10-11 10:29:40 +00002813
2814
2815/// XCore tool chain
2816XCore::XCore(const Driver &D, const llvm::Triple &Triple,
2817 const ArgList &Args) : ToolChain(D, Triple, Args) {
2818 // ProgramPaths are found via 'PATH' environment variable.
2819}
2820
2821Tool *XCore::buildAssembler() const {
2822 return new tools::XCore::Assemble(*this);
2823}
2824
2825Tool *XCore::buildLinker() const {
2826 return new tools::XCore::Link(*this);
2827}
2828
2829bool XCore::isPICDefault() const {
2830 return false;
2831}
2832
2833bool XCore::isPIEDefault() const {
2834 return false;
2835}
2836
2837bool XCore::isPICDefaultForced() const {
2838 return false;
2839}
2840
2841bool XCore::SupportsProfiling() const {
2842 return false;
2843}
2844
2845bool XCore::hasBlocksRuntime() const {
2846 return false;
2847}
2848
2849
2850void XCore::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2851 ArgStringList &CC1Args) const {
2852 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2853 DriverArgs.hasArg(options::OPT_nostdlibinc))
2854 return;
2855 if (const char *cl_include_dir = getenv("XCC_C_INCLUDE_PATH")) {
2856 SmallVector<StringRef, 4> Dirs;
2857 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
2858 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
2859 ArrayRef<StringRef> DirVec(Dirs);
2860 addSystemIncludes(DriverArgs, CC1Args, DirVec);
2861 }
2862}
2863
2864void XCore::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
2865 llvm::opt::ArgStringList &CC1Args) const {
2866 CC1Args.push_back("-nostdsysteminc");
2867}
2868
2869void XCore::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2870 ArgStringList &CC1Args) const {
2871 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2872 DriverArgs.hasArg(options::OPT_nostdlibinc))
2873 return;
2874 if (const char *cl_include_dir = getenv("XCC_CPLUS_INCLUDE_PATH")) {
2875 SmallVector<StringRef, 4> Dirs;
2876 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
2877 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
2878 ArrayRef<StringRef> DirVec(Dirs);
2879 addSystemIncludes(DriverArgs, CC1Args, DirVec);
2880 }
2881}
2882
2883void XCore::AddCXXStdlibLibArgs(const ArgList &Args,
2884 ArgStringList &CmdArgs) const {
2885 // We don't output any lib args. This is handled by xcc.
2886}