blob: 0472cdca397bc2372ad42efbe864afebef5d6e42 [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));
Jim Grosbach82eee262013-11-16 00:53:35 +0000796 else if (Name == "x86_64h") {
797 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
798 DAL->AddJoinedArg(0, MArch, "x86_64h");
799 }
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000800
801 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000802 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000803 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000804 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000805 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000806 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000807 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000808 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000809 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000810 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000811 else if (Name == "armv6m")
812 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000813 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000814 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000815 else if (Name == "armv7em")
816 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000817 else if (Name == "armv7f")
818 DAL->AddJoinedArg(0, MArch, "armv7f");
819 else if (Name == "armv7k")
820 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000821 else if (Name == "armv7m")
822 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000823 else if (Name == "armv7s")
824 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000825
826 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000827 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000828 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000829
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000830 // Add an explicit version min argument for the deployment target. We do this
831 // after argument translation because -Xarch_ arguments may add a version min
832 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000833 if (BoundArch)
834 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000835
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000836 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
837 // FIXME: It would be far better to avoid inserting those -static arguments,
838 // but we can't check the deployment target in the translation code until
839 // it is set here.
840 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
841 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
842 Arg *A = *it;
843 ++it;
844 if (A->getOption().getID() != options::OPT_mkernel &&
845 A->getOption().getID() != options::OPT_fapple_kext)
846 continue;
847 assert(it != ie && "unexpected argument translation");
848 A = *it;
849 assert(A->getOption().getID() == options::OPT_static &&
850 "missing expected -static argument");
851 it = DAL->getArgs().erase(it);
852 }
853 }
854
Bob Wilson0f7445b2013-11-02 23:19:53 +0000855 // Default to use libc++ on OS X 10.9+ and iOS 7+.
856 if (((isTargetMacOS() && !isMacosxVersionLT(10, 9)) ||
857 (isTargetIPhoneOS() && !isIPhoneOSVersionLT(7, 0))) &&
858 !Args.getLastArg(options::OPT_stdlib_EQ))
859 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_stdlib_EQ), "libc++");
860
Bob Wilson102be442011-10-07 17:54:41 +0000861 // Validate the C++ standard library choice.
862 CXXStdlibType Type = GetCXXStdlibType(*DAL);
863 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000864 // Check whether the target provides libc++.
865 StringRef where;
866
867 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson5ad5a952012-11-09 01:59:30 +0000868 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
869 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000870
871 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000872 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000873 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000874 }
875 }
876
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000877 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000878}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000879
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000880bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000881 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000882}
883
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000884bool Darwin::UseDwarfDebugFlags() const {
885 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
886 return S[0] != '\0';
887 return false;
888}
889
Daniel Dunbar3241d402010-02-10 18:49:11 +0000890bool Darwin::UseSjLjExceptions() const {
891 // Darwin uses SjLj exceptions on ARM.
892 return (getTriple().getArch() == llvm::Triple::arm ||
893 getTriple().getArch() == llvm::Triple::thumb);
894}
895
Chandler Carruth76a943b2012-11-19 03:52:03 +0000896bool Darwin::isPICDefault() const {
897 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000898}
899
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000900bool Darwin::isPIEDefault() const {
901 return false;
902}
903
Chandler Carruth76a943b2012-11-19 03:52:03 +0000904bool Darwin::isPICDefaultForced() const {
905 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000906}
907
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000908bool Darwin::SupportsProfiling() const {
909 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000910 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000911}
912
Daniel Dunbar16334e12010-04-10 16:20:23 +0000913bool Darwin::SupportsObjCGC() const {
914 // Garbage collection is supported everywhere except on iPhone OS.
915 return !isTargetIPhoneOS();
916}
917
John McCall3deb1ad2012-08-21 02:47:43 +0000918void Darwin::CheckObjCARC() const {
919 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
920 return;
John McCall93207072012-08-27 01:56:21 +0000921 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000922}
923
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000924/// Generic_GCC - A tool chain using the 'gcc' command to perform
925/// all subcommands; this relies on gcc translating the majority of
926/// command line options.
927
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000928/// \brief Parse a GCCVersion object out of a string of text.
929///
930/// This is the primary means of forming GCCVersion objects.
931/*static*/
932Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000933 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000934 std::pair<StringRef, StringRef> First = VersionText.split('.');
935 std::pair<StringRef, StringRef> Second = First.second.split('.');
936
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000937 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000938 if (First.first.getAsInteger(10, GoodVersion.Major) ||
939 GoodVersion.Major < 0)
940 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000941 GoodVersion.MajorStr = First.first.str();
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000942 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
943 GoodVersion.Minor < 0)
944 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000945 GoodVersion.MinorStr = Second.first.str();
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000946
947 // First look for a number prefix and parse that if present. Otherwise just
948 // stash the entire patch string in the suffix, and leave the number
949 // unspecified. This covers versions strings such as:
950 // 4.4
951 // 4.4.0
952 // 4.4.x
953 // 4.4.2-rc4
954 // 4.4.x-patched
955 // And retains any patch number it finds.
956 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
957 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +0000958 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000959 // Try to parse the number and any suffix.
960 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
961 GoodVersion.Patch < 0)
962 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000963 GoodVersion.PatchSuffix = PatchText.substr(EndNumber);
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000964 }
965 }
966
967 return GoodVersion;
968}
969
970/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000971bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor,
972 int RHSPatch,
973 StringRef RHSPatchSuffix) const {
974 if (Major != RHSMajor)
975 return Major < RHSMajor;
976 if (Minor != RHSMinor)
977 return Minor < RHSMinor;
978 if (Patch != RHSPatch) {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000979 // Note that versions without a specified patch sort higher than those with
980 // a patch.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000981 if (RHSPatch == -1)
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000982 return true;
983 if (Patch == -1)
984 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000985
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000986 // Otherwise just sort on the patch itself.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000987 return Patch < RHSPatch;
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000988 }
Benjamin Kramer604e8482013-08-09 17:17:48 +0000989 if (PatchSuffix != RHSPatchSuffix) {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000990 // Sort empty suffixes higher.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000991 if (RHSPatchSuffix.empty())
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000992 return true;
993 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +0000994 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000995
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000996 // Provide a lexicographic sort to make this a total ordering.
Benjamin Kramer604e8482013-08-09 17:17:48 +0000997 return PatchSuffix < RHSPatchSuffix;
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000998 }
999
1000 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001001 return false;
1002}
1003
Rafael Espindola1af7c212012-02-19 01:38:32 +00001004static StringRef getGCCToolchainDir(const ArgList &Args) {
1005 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1006 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +00001007 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +00001008 return GCC_INSTALL_PREFIX;
1009}
1010
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001011/// \brief Construct a GCCInstallationDetector from the driver.
1012///
1013/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +00001014/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +00001015///
1016/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1017/// should instead pull the target out of the driver. This is currently
1018/// necessary because the driver doesn't store the final version of the target
1019/// triple.
1020Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
Chandler Carruthb427c562013-06-22 11:35:51 +00001021 const Driver &D, const llvm::Triple &TargetTriple, const ArgList &Args)
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001022 : IsValid(false) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001023 llvm::Triple BiarchVariantTriple =
1024 TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
Chandler Carruth779579b2012-02-13 02:02:09 +00001025 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001026 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001027 // The library directories which may contain GCC installations.
Chandler Carruthb427c562013-06-22 11:35:51 +00001028 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001029 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001030 SmallVector<StringRef, 10> CandidateTripleAliases;
Chandler Carruthb427c562013-06-22 11:35:51 +00001031 SmallVector<StringRef, 10> CandidateBiarchTripleAliases;
1032 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1033 CandidateTripleAliases, CandidateBiarchLibDirs,
1034 CandidateBiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001035
1036 // Compute the set of prefixes for our search.
1037 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1038 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001039
Rafael Espindola1af7c212012-02-19 01:38:32 +00001040 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1041 if (GCCToolchainDir != "") {
1042 if (GCCToolchainDir.back() == '/')
1043 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001044
Rafael Espindola1af7c212012-02-19 01:38:32 +00001045 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001046 } else {
Rafael Espindola0f4b04e2013-08-28 23:17:47 +00001047 // If we have a SysRoot, try that first.
1048 if (!D.SysRoot.empty()) {
1049 Prefixes.push_back(D.SysRoot);
1050 Prefixes.push_back(D.SysRoot + "/usr");
1051 }
1052
1053 // Then look for gcc installed alongside clang.
Rafael Espindolac29af942012-02-03 01:01:20 +00001054 Prefixes.push_back(D.InstalledDir + "/..");
Rafael Espindola0f4b04e2013-08-28 23:17:47 +00001055
1056 // And finally in /usr.
1057 if (D.SysRoot.empty())
1058 Prefixes.push_back("/usr");
Rafael Espindolac29af942012-02-03 01:01:20 +00001059 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001060
1061 // Loop over the various components which exist and select the best GCC
1062 // installation available. GCC installs are ranked by version number.
1063 Version = GCCVersion::Parse("0.0.0");
1064 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1065 if (!llvm::sys::fs::exists(Prefixes[i]))
1066 continue;
1067 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1068 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1069 if (!llvm::sys::fs::exists(LibDir))
1070 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001071 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001072 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1073 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001074 }
Chandler Carruthb427c562013-06-22 11:35:51 +00001075 for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) {
1076 const std::string LibDir = Prefixes[i] + CandidateBiarchLibDirs[j].str();
Chandler Carruth866faab2012-01-25 07:21:38 +00001077 if (!llvm::sys::fs::exists(LibDir))
1078 continue;
Chandler Carruthb427c562013-06-22 11:35:51 +00001079 for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke;
Chandler Carruth866faab2012-01-25 07:21:38 +00001080 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001081 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruthb427c562013-06-22 11:35:51 +00001082 CandidateBiarchTripleAliases[k],
1083 /*NeedsBiarchSuffix=*/ true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001084 }
1085 }
1086}
1087
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001088void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const {
Benjamin Kramera97e4d12013-08-14 18:38:51 +00001089 for (std::set<std::string>::const_iterator
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001090 I = CandidateGCCInstallPaths.begin(),
1091 E = CandidateGCCInstallPaths.end();
1092 I != E; ++I)
1093 OS << "Found candidate GCC installation: " << *I << "\n";
1094
1095 OS << "Selected GCC installation: " << GCCInstallPath << "\n";
1096}
1097
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001098/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruthb427c562013-06-22 11:35:51 +00001099 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
Chandler Carruth866faab2012-01-25 07:21:38 +00001100 SmallVectorImpl<StringRef> &LibDirs,
1101 SmallVectorImpl<StringRef> &TripleAliases,
Chandler Carruthb427c562013-06-22 11:35:51 +00001102 SmallVectorImpl<StringRef> &BiarchLibDirs,
1103 SmallVectorImpl<StringRef> &BiarchTripleAliases) {
Chandler Carruth866faab2012-01-25 07:21:38 +00001104 // Declare a bunch of static data sets that we'll select between below. These
1105 // are specifically designed to always refer to string literals to avoid any
1106 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001107 static const char *const AArch64LibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001108 static const char *const AArch64Triples[] = { "aarch64-none-linux-gnu",
1109 "aarch64-linux-gnu" };
Tim Northover9bb857a2013-01-31 12:13:10 +00001110
Chandler Carruth866faab2012-01-25 07:21:38 +00001111 static const char *const ARMLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001112 static const char *const ARMTriples[] = { "arm-linux-gnueabi",
1113 "arm-linux-androideabi" };
1114 static const char *const ARMHFTriples[] = { "arm-linux-gnueabihf",
1115 "armv7hl-redhat-linux-gnueabi" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001116
1117 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1118 static const char *const X86_64Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001119 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", "x86_64-pc-linux-gnu",
1120 "x86_64-redhat-linux6E", "x86_64-redhat-linux", "x86_64-suse-linux",
1121 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", "x86_64-slackware-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001122 };
1123 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1124 static const char *const X86Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001125 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", "i386-linux-gnu",
1126 "i386-redhat-linux6E", "i686-redhat-linux", "i586-redhat-linux",
1127 "i386-redhat-linux", "i586-suse-linux", "i486-slackware-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001128 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001129 };
1130
1131 static const char *const MIPSLibDirs[] = { "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001132 static const char *const MIPSTriples[] = { "mips-linux-gnu",
1133 "mips-mti-linux-gnu" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001134 static const char *const MIPSELLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001135 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu",
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001136 "mipsel-linux-android" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001137
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001138 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001139 static const char *const MIPS64Triples[] = { "mips64-linux-gnu",
1140 "mips-mti-linux-gnu" };
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001141 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001142 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu",
1143 "mips-mti-linux-gnu" };
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001144
Chandler Carruth866faab2012-01-25 07:21:38 +00001145 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1146 static const char *const PPCTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001147 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
1148 "powerpc-suse-linux", "powerpc-montavista-linuxspe"
Chandler Carruth866faab2012-01-25 07:21:38 +00001149 };
1150 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001151 static const char *const PPC64Triples[] = { "powerpc64-linux-gnu",
1152 "powerpc64-unknown-linux-gnu",
1153 "powerpc64-suse-linux",
1154 "ppc64-redhat-linux" };
Bill Schmidt778d3872013-07-26 01:36:11 +00001155 static const char *const PPC64LELibDirs[] = { "/lib64", "/lib" };
1156 static const char *const PPC64LETriples[] = { "powerpc64le-linux-gnu",
1157 "powerpc64le-unknown-linux-gnu",
1158 "powerpc64le-suse-linux",
1159 "ppc64le-redhat-linux" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001160
Ulrich Weigand47445072013-05-06 16:26:41 +00001161 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1162 static const char *const SystemZTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001163 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
1164 "s390x-suse-linux", "s390x-redhat-linux"
Ulrich Weigand47445072013-05-06 16:26:41 +00001165 };
1166
Chandler Carruth866faab2012-01-25 07:21:38 +00001167 switch (TargetTriple.getArch()) {
Tim Northover9bb857a2013-01-31 12:13:10 +00001168 case llvm::Triple::aarch64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001169 LibDirs.append(AArch64LibDirs,
1170 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1171 TripleAliases.append(AArch64Triples,
1172 AArch64Triples + llvm::array_lengthof(AArch64Triples));
1173 BiarchLibDirs.append(AArch64LibDirs,
1174 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1175 BiarchTripleAliases.append(
1176 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
Tim Northover9bb857a2013-01-31 12:13:10 +00001177 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001178 case llvm::Triple::arm:
1179 case llvm::Triple::thumb:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001180 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001181 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001182 TripleAliases.append(ARMHFTriples,
1183 ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001184 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001185 TripleAliases.append(ARMTriples,
1186 ARMTriples + llvm::array_lengthof(ARMTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001187 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001188 break;
1189 case llvm::Triple::x86_64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001190 LibDirs.append(X86_64LibDirs,
1191 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1192 TripleAliases.append(X86_64Triples,
1193 X86_64Triples + llvm::array_lengthof(X86_64Triples));
1194 BiarchLibDirs.append(X86LibDirs,
1195 X86LibDirs + llvm::array_lengthof(X86LibDirs));
1196 BiarchTripleAliases.append(X86Triples,
1197 X86Triples + llvm::array_lengthof(X86Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001198 break;
1199 case llvm::Triple::x86:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001200 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001201 TripleAliases.append(X86Triples,
1202 X86Triples + llvm::array_lengthof(X86Triples));
1203 BiarchLibDirs.append(X86_64LibDirs,
1204 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1205 BiarchTripleAliases.append(
1206 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001207 break;
1208 case llvm::Triple::mips:
Chandler Carruthb427c562013-06-22 11:35:51 +00001209 LibDirs.append(MIPSLibDirs,
1210 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1211 TripleAliases.append(MIPSTriples,
1212 MIPSTriples + llvm::array_lengthof(MIPSTriples));
1213 BiarchLibDirs.append(MIPS64LibDirs,
1214 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1215 BiarchTripleAliases.append(
1216 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001217 break;
1218 case llvm::Triple::mipsel:
Chandler Carruthb427c562013-06-22 11:35:51 +00001219 LibDirs.append(MIPSELLibDirs,
1220 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1221 TripleAliases.append(MIPSELTriples,
1222 MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001223 TripleAliases.append(MIPSTriples,
1224 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Chandler Carruthb427c562013-06-22 11:35:51 +00001225 BiarchLibDirs.append(
1226 MIPS64ELLibDirs,
1227 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1228 BiarchTripleAliases.append(
1229 MIPS64ELTriples,
1230 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001231 break;
1232 case llvm::Triple::mips64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001233 LibDirs.append(MIPS64LibDirs,
1234 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1235 TripleAliases.append(MIPS64Triples,
1236 MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1237 BiarchLibDirs.append(MIPSLibDirs,
1238 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1239 BiarchTripleAliases.append(MIPSTriples,
1240 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001241 break;
1242 case llvm::Triple::mips64el:
Chandler Carruthb427c562013-06-22 11:35:51 +00001243 LibDirs.append(MIPS64ELLibDirs,
1244 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001245 TripleAliases.append(
Chandler Carruthb427c562013-06-22 11:35:51 +00001246 MIPS64ELTriples,
1247 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1248 BiarchLibDirs.append(MIPSELLibDirs,
1249 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1250 BiarchTripleAliases.append(
1251 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001252 BiarchTripleAliases.append(
1253 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001254 break;
1255 case llvm::Triple::ppc:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001256 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001257 TripleAliases.append(PPCTriples,
1258 PPCTriples + llvm::array_lengthof(PPCTriples));
1259 BiarchLibDirs.append(PPC64LibDirs,
1260 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1261 BiarchTripleAliases.append(
1262 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001263 break;
1264 case llvm::Triple::ppc64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001265 LibDirs.append(PPC64LibDirs,
1266 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1267 TripleAliases.append(PPC64Triples,
1268 PPC64Triples + llvm::array_lengthof(PPC64Triples));
1269 BiarchLibDirs.append(PPCLibDirs,
1270 PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1271 BiarchTripleAliases.append(PPCTriples,
1272 PPCTriples + llvm::array_lengthof(PPCTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001273 break;
Bill Schmidt778d3872013-07-26 01:36:11 +00001274 case llvm::Triple::ppc64le:
1275 LibDirs.append(PPC64LELibDirs,
1276 PPC64LELibDirs + llvm::array_lengthof(PPC64LELibDirs));
1277 TripleAliases.append(PPC64LETriples,
1278 PPC64LETriples + llvm::array_lengthof(PPC64LETriples));
1279 break;
Ulrich Weigand47445072013-05-06 16:26:41 +00001280 case llvm::Triple::systemz:
Chandler Carruthb427c562013-06-22 11:35:51 +00001281 LibDirs.append(SystemZLibDirs,
1282 SystemZLibDirs + llvm::array_lengthof(SystemZLibDirs));
1283 TripleAliases.append(SystemZTriples,
1284 SystemZTriples + llvm::array_lengthof(SystemZTriples));
Ulrich Weigand47445072013-05-06 16:26:41 +00001285 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001286
1287 default:
1288 // By default, just rely on the standard lib directories and the original
1289 // triple.
1290 break;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001291 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001292
1293 // Always append the drivers target triple to the end, in case it doesn't
1294 // match any of our aliases.
1295 TripleAliases.push_back(TargetTriple.str());
1296
1297 // Also include the multiarch variant if it's different.
Chandler Carruthb427c562013-06-22 11:35:51 +00001298 if (TargetTriple.str() != BiarchTriple.str())
1299 BiarchTripleAliases.push_back(BiarchTriple.str());
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001300}
1301
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001302static bool isSoftFloatABI(const ArgList &Args) {
1303 Arg *A = Args.getLastArg(options::OPT_msoft_float,
1304 options::OPT_mhard_float,
1305 options::OPT_mfloat_abi_EQ);
1306 if (!A) return false;
1307
1308 return A->getOption().matches(options::OPT_msoft_float) ||
1309 (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
1310 A->getValue() == StringRef("soft"));
1311}
1312
1313static bool isMipsArch(llvm::Triple::ArchType Arch) {
1314 return Arch == llvm::Triple::mips ||
1315 Arch == llvm::Triple::mipsel ||
1316 Arch == llvm::Triple::mips64 ||
1317 Arch == llvm::Triple::mips64el;
1318}
1319
1320static bool isMips16(const ArgList &Args) {
1321 Arg *A = Args.getLastArg(options::OPT_mips16,
1322 options::OPT_mno_mips16);
1323 return A && A->getOption().matches(options::OPT_mips16);
1324}
1325
Simon Atanasyan068e0fd2013-10-09 12:12:39 +00001326static bool isMips32r2(const ArgList &Args) {
1327 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1328 options::OPT_mcpu_EQ);
1329
1330 return A && A->getValue() == StringRef("mips32r2");
1331}
1332
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001333static bool isMips64r2(const ArgList &Args) {
1334 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1335 options::OPT_mcpu_EQ);
1336
1337 return A && A->getValue() == StringRef("mips64r2");
1338}
1339
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001340static bool isMicroMips(const ArgList &Args) {
1341 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1342 options::OPT_mno_micromips);
1343 return A && A->getOption().matches(options::OPT_mmicromips);
1344}
1345
Simon Atanasyan5c5b5da2013-11-20 13:53:20 +00001346static bool isMipsFP64(const ArgList &Args) {
1347 Arg *A = Args.getLastArg(options::OPT_mfp64, options::OPT_mfp32);
1348 return A && A->getOption().matches(options::OPT_mfp64);
1349}
1350
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001351static bool isMipsNan2008(const ArgList &Args) {
1352 Arg *A = Args.getLastArg(options::OPT_mnan_EQ);
1353 return A && A->getValue() == StringRef("2008");
1354}
1355
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001356// FIXME: There is the same routine in the Tools.cpp.
1357static bool hasMipsN32ABIArg(const ArgList &Args) {
1358 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00001359 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001360}
1361
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001362static bool hasCrtBeginObj(Twine Path) {
1363 return llvm::sys::fs::exists(Path + "/crtbegin.o");
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001364}
1365
Chandler Carruthb427c562013-06-22 11:35:51 +00001366static bool findTargetBiarchSuffix(std::string &Suffix, StringRef Path,
1367 llvm::Triple::ArchType TargetArch,
1368 const ArgList &Args) {
1369 // FIXME: This routine was only intended to model bi-arch toolchains which
1370 // use -m32 and -m64 to swap between variants of a target. It shouldn't be
1371 // doing ABI-based builtin location for MIPS.
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001372 if (hasMipsN32ABIArg(Args))
1373 Suffix = "/n32";
1374 else if (TargetArch == llvm::Triple::x86_64 ||
1375 TargetArch == llvm::Triple::ppc64 ||
1376 TargetArch == llvm::Triple::systemz ||
1377 TargetArch == llvm::Triple::mips64 ||
1378 TargetArch == llvm::Triple::mips64el)
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001379 Suffix = "/64";
1380 else
1381 Suffix = "/32";
1382
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001383 return hasCrtBeginObj(Path + Suffix);
1384}
1385
Chandler Carruth8677d922013-10-29 08:53:03 +00001386void Generic_GCC::GCCInstallationDetector::findMIPSABIDirSuffix(
1387 std::string &Suffix, llvm::Triple::ArchType TargetArch, StringRef Path,
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001388 const llvm::opt::ArgList &Args) {
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'
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001413
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001414 // Check FSF Toolchain path
1415 Suffix.clear();
1416 if (TargetArch == llvm::Triple::mips ||
1417 TargetArch == llvm::Triple::mipsel) {
1418 if (isMicroMips(Args))
1419 Suffix += "/micromips";
1420 else if (isMips32r2(Args))
1421 Suffix += "";
1422 else
1423 Suffix += "/mips32";
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001424
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001425 if (isMips16(Args))
1426 Suffix += "/mips16";
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001427 } else {
1428 if (isMips64r2(Args))
1429 Suffix += hasMipsN32ABIArg(Args) ? "/mips64r2" : "/mips64r2/64";
1430 else
1431 Suffix += hasMipsN32ABIArg(Args) ? "/mips64" : "/mips64/64";
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001432 }
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001433
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001434 if (TargetArch == llvm::Triple::mipsel ||
1435 TargetArch == llvm::Triple::mips64el)
1436 Suffix += "/el";
1437
1438 if (isSoftFloatABI(Args))
1439 Suffix += "/sof";
1440 else {
1441 if (isMipsFP64(Args))
1442 Suffix += "/fp64";
1443
1444 if (isMipsNan2008(Args))
1445 Suffix += "/nan2008";
1446 }
1447
1448 if (hasCrtBeginObj(Path + Suffix))
1449 return;
1450
1451 // Check Code Sourcery Toolchain path
1452 Suffix.clear();
1453 if (isMips16(Args))
1454 Suffix += "/mips16";
1455 else if (isMicroMips(Args))
1456 Suffix += "/micromips";
1457
1458 if (isSoftFloatABI(Args))
1459 Suffix += "/soft-float";
Simon Atanasyan3e0f3882013-11-26 11:57:48 +00001460 else if (isMipsNan2008(Args))
1461 Suffix += "/nan2008";
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001462
1463 if (TargetArch == llvm::Triple::mipsel ||
1464 TargetArch == llvm::Triple::mips64el)
1465 Suffix += "/el";
1466
1467 if (hasCrtBeginObj(Path + Suffix))
1468 return;
1469
1470 Suffix.clear();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001471}
1472
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001473void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001474 llvm::Triple::ArchType TargetArch, const ArgList &Args,
Chandler Carruthb427c562013-06-22 11:35:51 +00001475 const std::string &LibDir, StringRef CandidateTriple,
1476 bool NeedsBiarchSuffix) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001477 // There are various different suffixes involving the triple we
1478 // check for. We also record what is necessary to walk from each back
1479 // up to the lib directory.
Chandler Carruth866faab2012-01-25 07:21:38 +00001480 const std::string LibSuffixes[] = {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001481 "/gcc/" + CandidateTriple.str(),
Eli Friedmanbf44f362013-07-26 00:53:40 +00001482 // Debian puts cross-compilers in gcc-cross
1483 "/gcc-cross/" + CandidateTriple.str(),
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001484 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1485
Hal Finkelf3587912012-09-18 22:25:07 +00001486 // The Freescale PPC SDK has the gcc libraries in
1487 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1488 "/" + CandidateTriple.str(),
1489
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001490 // Ubuntu has a strange mis-matched pair of triples that this happens to
1491 // match.
1492 // FIXME: It may be worthwhile to generalize this and look for a second
1493 // triple.
Chandler Carruth6e46ca22011-11-09 03:46:20 +00001494 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001495 };
Eli Friedmanbf44f362013-07-26 00:53:40 +00001496 const std::string InstallSuffixes[] = {
1497 "/../../..", // gcc/
1498 "/../../..", // gcc-cross/
1499 "/../../../..", // <triple>/gcc/
1500 "/../..", // <triple>/
1501 "/../../../.." // i386-linux-gnu/gcc/<triple>/
1502 };
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001503 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruthb427c562013-06-22 11:35:51 +00001504 const unsigned NumLibSuffixes =
1505 (llvm::array_lengthof(LibSuffixes) - (TargetArch != llvm::Triple::x86));
Chandler Carruth866faab2012-01-25 07:21:38 +00001506 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1507 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001508 llvm::error_code EC;
Chandler Carruth866faab2012-01-25 07:21:38 +00001509 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001510 !EC && LI != LE; LI = LI.increment(EC)) {
1511 StringRef VersionText = llvm::sys::path::filename(LI->path());
1512 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
Benjamin Kramera97e4d12013-08-14 18:38:51 +00001513 if (CandidateVersion.Major != -1) // Filter obviously bad entries.
1514 if (!CandidateGCCInstallPaths.insert(LI->path()).second)
1515 continue; // Saw this path before; no need to look at it again.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001516 if (CandidateVersion.isOlderThan(4, 1, 1))
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001517 continue;
1518 if (CandidateVersion <= Version)
1519 continue;
Hal Finkel221e11e2011-12-08 05:50:03 +00001520
Chandler Carruth8677d922013-10-29 08:53:03 +00001521 std::string MIPSABIDirSuffix;
Simon Atanasyan54f3f932013-11-26 11:57:09 +00001522 if (isMipsArch(TargetArch))
1523 findMIPSABIDirSuffix(MIPSABIDirSuffix, TargetArch, LI->path(), Args);
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001524
Hal Finkel221e11e2011-12-08 05:50:03 +00001525 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth64cee062012-01-24 19:21:42 +00001526 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth866faab2012-01-25 07:21:38 +00001527 // libs in a subdirectory named 64. The simple logic we follow is that
1528 // *if* there is a subdirectory of the right name with crtbegin.o in it,
Chandler Carruthb427c562013-06-22 11:35:51 +00001529 // we use that. If not, and if not a biarch triple alias, we look for
Chandler Carruth866faab2012-01-25 07:21:38 +00001530 // crtbegin.o without the subdirectory.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001531
Chandler Carruthb427c562013-06-22 11:35:51 +00001532 std::string BiarchSuffix;
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001533 if (findTargetBiarchSuffix(BiarchSuffix,
Chandler Carruth8677d922013-10-29 08:53:03 +00001534 LI->path() + MIPSABIDirSuffix,
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001535 TargetArch, Args)) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001536 GCCBiarchSuffix = BiarchSuffix;
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001537 } else if (NeedsBiarchSuffix ||
Chandler Carruth8677d922013-10-29 08:53:03 +00001538 !hasCrtBeginObj(LI->path() + MIPSABIDirSuffix)) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001539 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001540 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001541 GCCBiarchSuffix.clear();
Chandler Carruth866faab2012-01-25 07:21:38 +00001542 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001543
1544 Version = CandidateVersion;
Chandler Carruth4d9d7682012-01-24 19:28:29 +00001545 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001546 // FIXME: We hack together the directory name here instead of
1547 // using LI to ensure stable path separators across Windows and
1548 // Linux.
Chandler Carruth866faab2012-01-25 07:21:38 +00001549 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth64cee062012-01-24 19:21:42 +00001550 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth8677d922013-10-29 08:53:03 +00001551 GCCMIPSABIDirSuffix = MIPSABIDirSuffix;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001552 IsValid = true;
1553 }
1554 }
1555}
1556
Rafael Espindola1af7c212012-02-19 01:38:32 +00001557Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1558 const ArgList &Args)
Rafael Espindola84b588b2013-03-18 18:10:27 +00001559 : ToolChain(D, Triple, Args), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbar88979912010-08-01 22:29:51 +00001560 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001561 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001562 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001563}
1564
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001565Generic_GCC::~Generic_GCC() {
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001566}
1567
Rafael Espindola7cf32212013-03-20 03:05:54 +00001568Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +00001569 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001570 case Action::PreprocessJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001571 if (!Preprocess)
1572 Preprocess.reset(new tools::gcc::Preprocess(*this));
1573 return Preprocess.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001574 case Action::CompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001575 if (!Compile)
1576 Compile.reset(new tools::gcc::Compile(*this));
1577 return Compile.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +00001578 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001579 return ToolChain::getTool(AC);
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001580 }
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001581}
1582
Rafael Espindola7cf32212013-03-20 03:05:54 +00001583Tool *Generic_GCC::buildAssembler() const {
Rafael Espindolaedaa4442013-11-23 16:40:57 +00001584 return new tools::gnutools::Assemble(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00001585}
1586
1587Tool *Generic_GCC::buildLinker() const {
1588 return new tools::gcc::Link(*this);
1589}
1590
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001591void Generic_GCC::printVerboseInfo(raw_ostream &OS) const {
1592 // Print the information about how we detected the GCC installation.
1593 GCCInstallation.print(OS);
1594}
1595
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001596bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001597 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001598}
1599
Chandler Carruth76a943b2012-11-19 03:52:03 +00001600bool Generic_GCC::isPICDefault() const {
1601 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001602}
1603
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001604bool Generic_GCC::isPIEDefault() const {
1605 return false;
1606}
1607
Chandler Carruth76a943b2012-11-19 03:52:03 +00001608bool Generic_GCC::isPICDefaultForced() const {
1609 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001610}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001611
Rafael Espindolaa8b3b682013-11-25 18:50:53 +00001612bool Generic_GCC::IsIntegratedAssemblerDefault() const {
1613 return getTriple().getArch() == llvm::Triple::x86 ||
1614 getTriple().getArch() == llvm::Triple::x86_64;
1615}
1616
Tony Linthicum76329bf2011-12-12 21:14:55 +00001617/// Hexagon Toolchain
1618
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001619std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1620
1621 // Locate the rest of the toolchain ...
1622 if (strlen(GCC_INSTALL_PREFIX))
1623 return std::string(GCC_INSTALL_PREFIX);
1624
1625 std::string InstallRelDir = InstalledDir + "/../../gnu";
1626 if (llvm::sys::fs::exists(InstallRelDir))
1627 return InstallRelDir;
1628
1629 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1630 if (llvm::sys::fs::exists(PrefixRelDir))
1631 return PrefixRelDir;
1632
1633 return InstallRelDir;
1634}
1635
Matthew Curtise689b052012-12-06 15:46:07 +00001636static void GetHexagonLibraryPaths(
1637 const ArgList &Args,
1638 const std::string Ver,
1639 const std::string MarchString,
1640 const std::string &InstalledDir,
1641 ToolChain::path_list *LibPaths)
1642{
1643 bool buildingLib = Args.hasArg(options::OPT_shared);
1644
1645 //----------------------------------------------------------------------------
1646 // -L Args
1647 //----------------------------------------------------------------------------
1648 for (arg_iterator
1649 it = Args.filtered_begin(options::OPT_L),
1650 ie = Args.filtered_end();
1651 it != ie;
1652 ++it) {
1653 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1654 LibPaths->push_back((*it)->getValue(i));
1655 }
1656
1657 //----------------------------------------------------------------------------
1658 // Other standard paths
1659 //----------------------------------------------------------------------------
1660 const std::string MarchSuffix = "/" + MarchString;
1661 const std::string G0Suffix = "/G0";
1662 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1663 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1664
1665 // lib/gcc/hexagon/...
1666 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1667 if (buildingLib) {
1668 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1669 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1670 }
1671 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1672 LibPaths->push_back(LibGCCHexagonDir + Ver);
1673
1674 // lib/gcc/...
1675 LibPaths->push_back(RootDir + "lib/gcc");
1676
1677 // hexagon/lib/...
1678 std::string HexagonLibDir = RootDir + "hexagon/lib";
1679 if (buildingLib) {
1680 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1681 LibPaths->push_back(HexagonLibDir + G0Suffix);
1682 }
1683 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1684 LibPaths->push_back(HexagonLibDir);
1685}
1686
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001687Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1688 const ArgList &Args)
1689 : Linux(D, Triple, Args) {
1690 const std::string InstalledDir(getDriver().getInstalledDir());
1691 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1692
1693 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1694 // program paths
1695 const std::string BinDir(GnuDir + "/bin");
1696 if (llvm::sys::fs::exists(BinDir))
1697 getProgramPaths().push_back(BinDir);
1698
1699 // Determine version of GCC libraries and headers to use.
1700 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1701 llvm::error_code ec;
1702 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1703 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1704 !ec && di != de; di = di.increment(ec)) {
1705 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1706 if (MaxVersion < cv)
1707 MaxVersion = cv;
1708 }
1709 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001710
1711 ToolChain::path_list *LibPaths= &getFilePaths();
1712
1713 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1714 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1715 // support 'linux' we'll need to fix this up
1716 LibPaths->clear();
1717
1718 GetHexagonLibraryPaths(
1719 Args,
1720 GetGCCLibAndIncVersion(),
1721 GetTargetCPU(Args),
1722 InstalledDir,
1723 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001724}
1725
1726Hexagon_TC::~Hexagon_TC() {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001727}
1728
Rafael Espindola7cf32212013-03-20 03:05:54 +00001729Tool *Hexagon_TC::buildAssembler() const {
1730 return new tools::hexagon::Assemble(*this);
1731}
1732
1733Tool *Hexagon_TC::buildLinker() const {
1734 return new tools::hexagon::Link(*this);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001735}
1736
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001737void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1738 ArgStringList &CC1Args) const {
1739 const Driver &D = getDriver();
1740
1741 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1742 DriverArgs.hasArg(options::OPT_nostdlibinc))
1743 return;
1744
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001745 std::string Ver(GetGCCLibAndIncVersion());
1746 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1747 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1748 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1749 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1750 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001751}
1752
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001753void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1754 ArgStringList &CC1Args) const {
1755
1756 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1757 DriverArgs.hasArg(options::OPT_nostdincxx))
1758 return;
1759
1760 const Driver &D = getDriver();
1761 std::string Ver(GetGCCLibAndIncVersion());
Rafael Espindola358256c2013-06-26 02:13:00 +00001762 SmallString<128> IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001763
Rafael Espindola358256c2013-06-26 02:13:00 +00001764 llvm::sys::path::append(IncludeDir, "hexagon/include/c++/");
1765 llvm::sys::path::append(IncludeDir, Ver);
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001766 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001767}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001768
Matthew Curtise689b052012-12-06 15:46:07 +00001769ToolChain::CXXStdlibType
1770Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1771 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1772 if (!A)
1773 return ToolChain::CST_Libstdcxx;
1774
1775 StringRef Value = A->getValue();
1776 if (Value != "libstdc++") {
1777 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1778 << A->getAsString(Args);
1779 }
1780
1781 return ToolChain::CST_Libstdcxx;
1782}
1783
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001784static int getHexagonVersion(const ArgList &Args) {
1785 Arg *A = Args.getLastArg(options::OPT_march_EQ, options::OPT_mcpu_EQ);
1786 // Select the default CPU (v4) if none was given.
1787 if (!A)
1788 return 4;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001789
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001790 // FIXME: produce errors if we cannot parse the version.
1791 StringRef WhichHexagon = A->getValue();
1792 if (WhichHexagon.startswith("hexagonv")) {
1793 int Val;
1794 if (!WhichHexagon.substr(sizeof("hexagonv") - 1).getAsInteger(10, Val))
1795 return Val;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001796 }
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001797 if (WhichHexagon.startswith("v")) {
1798 int Val;
1799 if (!WhichHexagon.substr(1).getAsInteger(10, Val))
1800 return Val;
1801 }
1802
1803 // FIXME: should probably be an error.
1804 return 4;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001805}
1806
1807StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1808{
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001809 int V = getHexagonVersion(Args);
1810 // FIXME: We don't support versions < 4. We should error on them.
1811 switch (V) {
1812 default:
1813 llvm_unreachable("Unexpected version");
1814 case 5:
1815 return "v5";
1816 case 4:
1817 return "v4";
1818 case 3:
1819 return "v3";
1820 case 2:
1821 return "v2";
1822 case 1:
1823 return "v1";
Matthew Curtisf10a5952012-12-06 14:16:43 +00001824 }
Matthew Curtisf10a5952012-12-06 14:16:43 +00001825}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001826// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001827
Chris Lattner09797542010-03-04 21:07:38 +00001828/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1829/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1830/// Currently does not support anything else but compilation.
1831
Rafael Espindola84b588b2013-03-18 18:10:27 +00001832TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
1833 const ArgList &Args)
1834 : ToolChain(D, Triple, Args) {
Chris Lattner09797542010-03-04 21:07:38 +00001835 // Path mangling to find libexec
1836 std::string Path(getDriver().Dir);
1837
1838 Path += "/../libexec";
1839 getProgramPaths().push_back(Path);
1840}
1841
1842TCEToolChain::~TCEToolChain() {
Chris Lattner09797542010-03-04 21:07:38 +00001843}
1844
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001845bool TCEToolChain::IsMathErrnoDefault() const {
1846 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001847}
1848
Chandler Carruth76a943b2012-11-19 03:52:03 +00001849bool TCEToolChain::isPICDefault() const {
1850 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001851}
1852
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001853bool TCEToolChain::isPIEDefault() const {
1854 return false;
1855}
1856
Chandler Carruth76a943b2012-11-19 03:52:03 +00001857bool TCEToolChain::isPICDefaultForced() const {
1858 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001859}
1860
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001861/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1862
Rafael Espindola1af7c212012-02-19 01:38:32 +00001863OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1864 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001865 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001866 getFilePaths().push_back("/usr/lib");
1867}
1868
Rafael Espindola7cf32212013-03-20 03:05:54 +00001869Tool *OpenBSD::buildAssembler() const {
1870 return new tools::openbsd::Assemble(*this);
1871}
1872
1873Tool *OpenBSD::buildLinker() const {
1874 return new tools::openbsd::Link(*this);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001875}
1876
Eli Friedman9fa28852012-08-08 23:57:20 +00001877/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1878
1879Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1880 : Generic_ELF(D, Triple, Args) {
1881 getFilePaths().push_back(getDriver().Dir + "/../lib");
1882 getFilePaths().push_back("/usr/lib");
1883}
1884
Rafael Espindola7cf32212013-03-20 03:05:54 +00001885Tool *Bitrig::buildAssembler() const {
1886 return new tools::bitrig::Assemble(*this);
1887}
1888
1889Tool *Bitrig::buildLinker() const {
1890 return new tools::bitrig::Link(*this);
Eli Friedman9fa28852012-08-08 23:57:20 +00001891}
1892
1893void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1894 ArgStringList &CC1Args) const {
1895 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1896 DriverArgs.hasArg(options::OPT_nostdincxx))
1897 return;
1898
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001899 switch (GetCXXStdlibType(DriverArgs)) {
1900 case ToolChain::CST_Libcxx:
1901 addSystemInclude(DriverArgs, CC1Args,
1902 getDriver().SysRoot + "/usr/include/c++/");
1903 break;
1904 case ToolChain::CST_Libstdcxx:
1905 addSystemInclude(DriverArgs, CC1Args,
1906 getDriver().SysRoot + "/usr/include/c++/stdc++");
1907 addSystemInclude(DriverArgs, CC1Args,
1908 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001909
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001910 StringRef Triple = getTriple().str();
1911 if (Triple.startswith("amd64"))
1912 addSystemInclude(DriverArgs, CC1Args,
1913 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1914 Triple.substr(5));
1915 else
1916 addSystemInclude(DriverArgs, CC1Args,
1917 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1918 Triple);
1919 break;
1920 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001921}
1922
1923void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1924 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001925 switch (GetCXXStdlibType(Args)) {
1926 case ToolChain::CST_Libcxx:
1927 CmdArgs.push_back("-lc++");
1928 CmdArgs.push_back("-lcxxrt");
1929 // Include supc++ to provide Unwind until provided by libcxx.
1930 CmdArgs.push_back("-lgcc");
1931 break;
1932 case ToolChain::CST_Libstdcxx:
1933 CmdArgs.push_back("-lstdc++");
1934 break;
1935 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001936}
1937
Daniel Dunbare24297c2009-03-30 21:06:03 +00001938/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1939
Rafael Espindola1af7c212012-02-19 01:38:32 +00001940FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1941 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001942
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001943 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1944 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001945 if ((Triple.getArch() == llvm::Triple::x86 ||
1946 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001947 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001948 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1949 else
1950 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001951}
1952
David Chisnall867ccd82013-11-09 15:10:56 +00001953ToolChain::CXXStdlibType
1954FreeBSD::GetCXXStdlibType(const ArgList &Args) const {
1955 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
1956 StringRef Value = A->getValue();
1957 if (Value == "libstdc++")
1958 return ToolChain::CST_Libstdcxx;
1959 if (Value == "libc++")
1960 return ToolChain::CST_Libcxx;
1961
1962 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1963 << A->getAsString(Args);
1964 }
1965 if (getTriple().getOSMajorVersion() >= 10)
1966 return ToolChain::CST_Libcxx;
1967 return ToolChain::CST_Libstdcxx;
1968}
1969
1970void FreeBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1971 ArgStringList &CC1Args) const {
1972 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1973 DriverArgs.hasArg(options::OPT_nostdincxx))
1974 return;
1975
1976 switch (GetCXXStdlibType(DriverArgs)) {
1977 case ToolChain::CST_Libcxx:
1978 addSystemInclude(DriverArgs, CC1Args,
1979 getDriver().SysRoot + "/usr/include/c++/v1");
1980 break;
1981 case ToolChain::CST_Libstdcxx:
1982 addSystemInclude(DriverArgs, CC1Args,
1983 getDriver().SysRoot + "/usr/include/c++/4.2");
1984 addSystemInclude(DriverArgs, CC1Args,
1985 getDriver().SysRoot + "/usr/include/c++/4.2/backward");
1986 break;
1987 }
1988}
1989
Rafael Espindola7cf32212013-03-20 03:05:54 +00001990Tool *FreeBSD::buildAssembler() const {
1991 return new tools::freebsd::Assemble(*this);
1992}
1993
1994Tool *FreeBSD::buildLinker() const {
1995 return new tools::freebsd::Link(*this);
Daniel Dunbare24297c2009-03-30 21:06:03 +00001996}
Daniel Dunbarcc912342009-05-02 18:28:39 +00001997
Rafael Espindola0f207ed2012-12-13 04:17:14 +00001998bool FreeBSD::UseSjLjExceptions() const {
1999 // FreeBSD uses SjLj exceptions on ARM oabi.
2000 switch (getTriple().getEnvironment()) {
2001 case llvm::Triple::GNUEABI:
2002 case llvm::Triple::EABI:
2003 return false;
2004
2005 default:
2006 return (getTriple().getArch() == llvm::Triple::arm ||
2007 getTriple().getArch() == llvm::Triple::thumb);
2008 }
2009}
2010
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002011/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
2012
Rafael Espindola1af7c212012-02-19 01:38:32 +00002013NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2014 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002015
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00002016 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00002017 // When targeting a 32-bit platform, try the special directory used on
2018 // 64-bit hosts, and only fall back to the main library directory if that
2019 // doesn't work.
2020 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
2021 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00002022 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00002023 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00002024
2025 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002026 }
2027}
2028
Rafael Espindola7cf32212013-03-20 03:05:54 +00002029Tool *NetBSD::buildAssembler() const {
2030 return new tools::netbsd::Assemble(*this);
2031}
2032
2033Tool *NetBSD::buildLinker() const {
2034 return new tools::netbsd::Link(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002035}
2036
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00002037ToolChain::CXXStdlibType
2038NetBSD::GetCXXStdlibType(const ArgList &Args) const {
2039 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
2040 StringRef Value = A->getValue();
2041 if (Value == "libstdc++")
2042 return ToolChain::CST_Libstdcxx;
2043 if (Value == "libc++")
2044 return ToolChain::CST_Libcxx;
2045
2046 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2047 << A->getAsString(Args);
2048 }
2049
Joerg Sonnenbergera4435632013-10-14 20:13:05 +00002050 unsigned Major, Minor, Micro;
2051 getTriple().getOSVersion(Major, Minor, Micro);
2052 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 23) || Major == 0) {
2053 if (getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64)
2054 return ToolChain::CST_Libcxx;
2055 }
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00002056 return ToolChain::CST_Libstdcxx;
2057}
2058
2059void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2060 ArgStringList &CC1Args) const {
2061 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2062 DriverArgs.hasArg(options::OPT_nostdincxx))
2063 return;
2064
2065 switch (GetCXXStdlibType(DriverArgs)) {
2066 case ToolChain::CST_Libcxx:
2067 addSystemInclude(DriverArgs, CC1Args,
2068 getDriver().SysRoot + "/usr/include/c++/");
2069 break;
2070 case ToolChain::CST_Libstdcxx:
2071 addSystemInclude(DriverArgs, CC1Args,
2072 getDriver().SysRoot + "/usr/include/g++");
2073 addSystemInclude(DriverArgs, CC1Args,
2074 getDriver().SysRoot + "/usr/include/g++/backward");
2075 break;
2076 }
2077}
2078
Chris Lattner3e2ee142010-07-07 16:01:42 +00002079/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
2080
Rafael Espindola1af7c212012-02-19 01:38:32 +00002081Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2082 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00002083 getFilePaths().push_back(getDriver().Dir + "/../lib");
2084 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00002085}
2086
Rafael Espindola7cf32212013-03-20 03:05:54 +00002087Tool *Minix::buildAssembler() const {
2088 return new tools::minix::Assemble(*this);
2089}
2090
2091Tool *Minix::buildLinker() const {
2092 return new tools::minix::Link(*this);
Chris Lattner3e2ee142010-07-07 16:01:42 +00002093}
2094
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002095/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
2096
Rafael Espindola1af7c212012-02-19 01:38:32 +00002097AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
2098 const ArgList &Args)
2099 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002100
Daniel Dunbar88979912010-08-01 22:29:51 +00002101 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002102 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002103 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002104
Daniel Dunbar083edf72009-12-21 18:54:17 +00002105 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002106 getFilePaths().push_back("/usr/lib");
2107 getFilePaths().push_back("/usr/sfw/lib");
2108 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00002109 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002110
2111}
2112
Rafael Espindola7cf32212013-03-20 03:05:54 +00002113Tool *AuroraUX::buildAssembler() const {
2114 return new tools::auroraux::Assemble(*this);
2115}
2116
2117Tool *AuroraUX::buildLinker() const {
2118 return new tools::auroraux::Link(*this);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002119}
2120
David Chisnallf571cde2012-02-15 13:39:01 +00002121/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2122
Rafael Espindola1af7c212012-02-19 01:38:32 +00002123Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2124 const ArgList &Args)
2125 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00002126
2127 getProgramPaths().push_back(getDriver().getInstalledDir());
2128 if (getDriver().getInstalledDir() != getDriver().Dir)
2129 getProgramPaths().push_back(getDriver().Dir);
2130
2131 getFilePaths().push_back(getDriver().Dir + "/../lib");
2132 getFilePaths().push_back("/usr/lib");
2133}
2134
Rafael Espindola7cf32212013-03-20 03:05:54 +00002135Tool *Solaris::buildAssembler() const {
2136 return new tools::solaris::Assemble(*this);
2137}
2138
2139Tool *Solaris::buildLinker() const {
2140 return new tools::solaris::Link(*this);
David Chisnallf571cde2012-02-15 13:39:01 +00002141}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002142
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002143/// Distribution (very bare-bones at the moment).
Eli Friedman5cd659f2009-05-26 07:52:18 +00002144
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002145enum Distro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002146 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002147 DebianLenny,
2148 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00002149 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002150 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00002151 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00002152 RHEL4,
2153 RHEL5,
2154 RHEL6,
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002155 Fedora,
Rafael Espindola10a63c22013-07-03 14:14:00 +00002156 OpenSUSE,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00002157 UbuntuHardy,
2158 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00002159 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00002160 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002161 UbuntuLucid,
2162 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00002163 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00002164 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002165 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00002166 UbuntuQuantal,
2167 UbuntuRaring,
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002168 UbuntuSaucy,
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002169 UbuntuTrusty,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002170 UnknownDistro
2171};
2172
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002173static bool IsRedhat(enum Distro Distro) {
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002174 return Distro == Fedora || (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002175}
2176
Rafael Espindola10a63c22013-07-03 14:14:00 +00002177static bool IsOpenSUSE(enum Distro Distro) {
2178 return Distro == OpenSUSE;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002179}
2180
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002181static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002182 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002183}
2184
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002185static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002186 return Distro >= UbuntuHardy && Distro <= UbuntuTrusty;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002187}
2188
Rafael Espindolaa8398552013-10-28 23:14:34 +00002189static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00002190 OwningPtr<llvm::MemoryBuffer> File;
Rafael Espindolaa8398552013-10-28 23:14:34 +00002191 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002192 StringRef Data = File.get()->getBuffer();
2193 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002194 Data.split(Lines, "\n");
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002195 Distro Version = UnknownDistro;
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002196 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2197 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002198 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002199 .Case("hardy", UbuntuHardy)
2200 .Case("intrepid", UbuntuIntrepid)
2201 .Case("jaunty", UbuntuJaunty)
2202 .Case("karmic", UbuntuKarmic)
2203 .Case("lucid", UbuntuLucid)
2204 .Case("maverick", UbuntuMaverick)
2205 .Case("natty", UbuntuNatty)
2206 .Case("oneiric", UbuntuOneiric)
2207 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002208 .Case("quantal", UbuntuQuantal)
2209 .Case("raring", UbuntuRaring)
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002210 .Case("saucy", UbuntuSaucy)
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002211 .Case("trusty", UbuntuTrusty)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002212 .Default(UnknownDistro);
2213 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002214 }
2215
Rafael Espindolaa8398552013-10-28 23:14:34 +00002216 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002217 StringRef Data = File.get()->getBuffer();
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002218 if (Data.startswith("Fedora release"))
2219 return Fedora;
Chris Lattner84e38552011-05-22 05:36:06 +00002220 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002221 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002222 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002223 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002224 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002225 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002226 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002227 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002228 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002229 Data.find("release 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002230 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002231 return UnknownDistro;
2232 }
2233
Rafael Espindolaa8398552013-10-28 23:14:34 +00002234 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002235 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002236 if (Data[0] == '5')
2237 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002238 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002239 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002240 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002241 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002242 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2243 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002244 return UnknownDistro;
2245 }
2246
Rafael Espindolaa8398552013-10-28 23:14:34 +00002247 if (llvm::sys::fs::exists("/etc/SuSE-release"))
Rafael Espindola10a63c22013-07-03 14:14:00 +00002248 return OpenSUSE;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002249
Rafael Espindolaa8398552013-10-28 23:14:34 +00002250 if (llvm::sys::fs::exists("/etc/exherbo-release"))
Rafael Espindola12479842010-11-11 02:07:13 +00002251 return Exherbo;
2252
Rafael Espindolaa8398552013-10-28 23:14:34 +00002253 if (llvm::sys::fs::exists("/etc/arch-release"))
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002254 return ArchLinux;
2255
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002256 return UnknownDistro;
2257}
2258
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002259/// \brief Get our best guess at the multiarch triple for a target.
2260///
2261/// Debian-based systems are starting to use a multiarch setup where they use
2262/// a target-triple directory in the library and header search paths.
2263/// Unfortunately, this triple does not align with the vanilla target triple,
2264/// so we provide a rough mapping here.
2265static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2266 StringRef SysRoot) {
2267 // For most architectures, just use whatever we have rather than trying to be
2268 // clever.
2269 switch (TargetTriple.getArch()) {
2270 default:
2271 return TargetTriple.str();
2272
2273 // We use the existence of '/lib/<triple>' as a directory to detect some
2274 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002275 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2276 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002277 case llvm::Triple::arm:
2278 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002279 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2280 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2281 return "arm-linux-gnueabihf";
2282 } else {
2283 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2284 return "arm-linux-gnueabi";
2285 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002286 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002287 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002288 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2289 return "i386-linux-gnu";
2290 return TargetTriple.str();
2291 case llvm::Triple::x86_64:
2292 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2293 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002294 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002295 case llvm::Triple::aarch64:
2296 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2297 return "aarch64-linux-gnu";
Tim Northoverfbb56b72013-06-13 22:54:55 +00002298 return TargetTriple.str();
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002299 case llvm::Triple::mips:
2300 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2301 return "mips-linux-gnu";
2302 return TargetTriple.str();
2303 case llvm::Triple::mipsel:
2304 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2305 return "mipsel-linux-gnu";
2306 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002307 case llvm::Triple::ppc:
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00002308 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2309 return "powerpc-linux-gnuspe";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002310 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2311 return "powerpc-linux-gnu";
2312 return TargetTriple.str();
2313 case llvm::Triple::ppc64:
2314 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2315 return "powerpc64-linux-gnu";
Bill Schmidt778d3872013-07-26 01:36:11 +00002316 case llvm::Triple::ppc64le:
2317 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
2318 return "powerpc64le-linux-gnu";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002319 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002320 }
2321}
2322
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002323static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2324 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2325}
2326
Simon Atanasyand4413882012-09-14 11:27:24 +00002327static StringRef getMultilibDir(const llvm::Triple &Triple,
2328 const ArgList &Args) {
Chandler Carruthda797042013-10-29 10:27:30 +00002329 if (isMipsArch(Triple.getArch())) {
2330 // lib32 directory has a special meaning on MIPS targets.
2331 // It contains N32 ABI binaries. Use this folder if produce
2332 // code for N32 ABI only.
2333 if (hasMipsN32ABIArg(Args))
2334 return "lib32";
2335 return Triple.isArch32Bit() ? "lib" : "lib64";
2336 }
Simon Atanasyand4413882012-09-14 11:27:24 +00002337
Chandler Carruthda797042013-10-29 10:27:30 +00002338 // It happens that only x86 and PPC use the 'lib32' variant of multilib, and
2339 // using that variant while targeting other architectures causes problems
2340 // because the libraries are laid out in shared system roots that can't cope
2341 // with a 'lib32' multilib search path being considered. So we only enable
2342 // them when we know we may need it.
2343 //
2344 // FIXME: This is a bit of a hack. We should really unify this code for
2345 // reasoning about multilib spellings with the lib dir spellings in the
2346 // GCCInstallationDetector, but that is a more significant refactoring.
2347 if (Triple.getArch() == llvm::Triple::x86 ||
2348 Triple.getArch() == llvm::Triple::ppc)
Simon Atanasyand4413882012-09-14 11:27:24 +00002349 return "lib32";
2350
2351 return Triple.isArch32Bit() ? "lib" : "lib64";
2352}
2353
Rafael Espindola1af7c212012-02-19 01:38:32 +00002354Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2355 : Generic_ELF(D, Triple, Args) {
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002356 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyana0d89572013-10-05 14:37:55 +00002357 std::string SysRoot = computeSysRoot();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002358
Rafael Espindola10a63c22013-07-03 14:14:00 +00002359 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
Chandler Carruthd6c62b62013-06-20 23:37:54 +00002360 // least) put various tools in a triple-prefixed directory off of the parent
2361 // of the GCC installation. We use the GCC triple here to ensure that we end
2362 // up with tools that support the same amount of cross compiling as the
2363 // detected GCC installation. For example, if we find a GCC installation
2364 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
2365 // used to target i386.
2366 // FIXME: This seems unlikely to be Linux-specific.
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002367 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002368 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002369 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002370
2371 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002372
Rafael Espindolaa8398552013-10-28 23:14:34 +00002373 Distro Distro = DetectDistro(Arch);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002374
Rafael Espindola10a63c22013-07-03 14:14:00 +00002375 if (IsOpenSUSE(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002376 ExtraOpts.push_back("-z");
2377 ExtraOpts.push_back("relro");
2378 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002379
Douglas Gregord9bb1522011-03-06 19:11:49 +00002380 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002381 ExtraOpts.push_back("-X");
2382
Logan Chienc6fd8202012-09-02 09:30:11 +00002383 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002384 const bool IsMips = isMipsArch(Arch);
2385
2386 if (IsMips && !SysRoot.empty())
2387 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002388
Chandler Carruth0b842912011-12-09 04:45:18 +00002389 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2390 // and the MIPS ABI require .dynsym to be sorted in different ways.
2391 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2392 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002393 // Android loader does not support .gnu.hash.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002394 if (!IsMips && !IsAndroid) {
Rafael Espindola10a63c22013-07-03 14:14:00 +00002395 if (IsRedhat(Distro) || IsOpenSUSE(Distro) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002396 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002397 ExtraOpts.push_back("--hash-style=gnu");
2398
Rafael Espindola10a63c22013-07-03 14:14:00 +00002399 if (IsDebian(Distro) || IsOpenSUSE(Distro) || Distro == UbuntuLucid ||
Chandler Carruth0b842912011-12-09 04:45:18 +00002400 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2401 ExtraOpts.push_back("--hash-style=both");
2402 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002403
Chris Lattner84e38552011-05-22 05:36:06 +00002404 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002405 ExtraOpts.push_back("--no-add-needed");
2406
Eli Friedmanf7600942011-06-02 21:36:53 +00002407 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola10a63c22013-07-03 14:14:00 +00002408 Distro == DebianJessie || IsOpenSUSE(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002409 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002410 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002411 ExtraOpts.push_back("--build-id");
2412
Rafael Espindola10a63c22013-07-03 14:14:00 +00002413 if (IsOpenSUSE(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002414 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002415
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002416 // The selection of paths to try here is designed to match the patterns which
2417 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2418 // This was determined by running GCC in a fake filesystem, creating all
2419 // possible permutations of these directories, and seeing which ones it added
2420 // to the link paths.
2421 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002422
Rafael Espindolaa8398552013-10-28 23:14:34 +00002423 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002424 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002425
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002426 // Add the multilib suffixed paths where they are available.
2427 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002428 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002429 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002430
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002431 // Sourcery CodeBench MIPS toolchain holds some libraries under
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002432 // a biarch-like suffix of the GCC installation.
2433 //
2434 // FIXME: It would be cleaner to model this as a variant of bi-arch. IE,
2435 // instead of a '64' biarch suffix it would be 'el' or something.
Simon Atanasyan068e0fd2013-10-09 12:12:39 +00002436 if (IsAndroid && IsMips && isMips32r2(Args)) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002437 assert(GCCInstallation.getBiarchSuffix().empty() &&
2438 "Unexpected bi-arch suffix");
2439 addPathIfExists(GCCInstallation.getInstallPath() + "/mips-r2", Paths);
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002440 } else {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002441 addPathIfExists((GCCInstallation.getInstallPath() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002442 GCCInstallation.getMIPSABIDirSuffix() +
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002443 GCCInstallation.getBiarchSuffix()),
2444 Paths);
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002445 }
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002446
2447 // GCC cross compiling toolchains will install target libraries which ship
2448 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
2449 // any part of the GCC installation in
2450 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
2451 // debatable, but is the reality today. We need to search this tree even
2452 // when we have a sysroot somewhere else. It is the responsibility of
2453 // whomever is doing the cross build targetting a sysroot using a GCC
2454 // installation that is *not* within the system root to ensure two things:
2455 //
2456 // 1) Any DSOs that are linked in from this tree or from the install path
2457 // above must be preasant on the system root and found via an
2458 // appropriate rpath.
2459 // 2) There must not be libraries installed into
2460 // <prefix>/<triple>/<libdir> unless they should be preferred over
2461 // those within the system root.
2462 //
2463 // Note that this matches the GCC behavior. See the below comment for where
2464 // Clang diverges from GCC's behavior.
2465 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib +
Chandler Carruth8677d922013-10-29 08:53:03 +00002466 GCCInstallation.getMIPSABIDirSuffix(),
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002467 Paths);
2468
Chandler Carruth69a125b2012-04-06 16:32:06 +00002469 // If the GCC installation we found is inside of the sysroot, we want to
2470 // prefer libraries installed in the parent prefix of the GCC installation.
2471 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002472 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002473 // This usually happens when there is an external cross compiler on the
2474 // host system, and a more minimal sysroot available that is the target of
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002475 // the cross. Note that GCC does include some of these directories in some
2476 // configurations but this seems somewhere between questionable and simply
2477 // a bug.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002478 if (StringRef(LibPath).startswith(SysRoot)) {
Chandler Carruth69a125b2012-04-06 16:32:06 +00002479 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2480 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2481 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002482 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002483 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2484 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2485 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2486 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2487
Chandler Carruthb427c562013-06-22 11:35:51 +00002488 // Try walking via the GCC triple path in case of biarch or multiarch GCC
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002489 // installations with strange symlinks.
Rafael Espindolab6250162013-10-25 17:06:04 +00002490 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002491 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002492 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002493
Rafael Espindolab6250162013-10-25 17:06:04 +00002494 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002495 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002496 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Rafael Espindolaa8398552013-10-28 23:14:34 +00002497 if (!GCCInstallation.getBiarchSuffix().empty())
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002498 addPathIfExists(GCCInstallation.getInstallPath() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002499 GCCInstallation.getMIPSABIDirSuffix(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002500
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002501 // See comments above on the multilib variant for details of why this is
2502 // included even from outside the sysroot.
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002503 addPathIfExists(LibPath + "/../" + GCCTriple.str() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002504 "/lib" + GCCInstallation.getMIPSABIDirSuffix(), Paths);
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002505
2506 // See comments above on the multilib variant for details of why this is
2507 // only included from within the sysroot.
2508 if (StringRef(LibPath).startswith(SysRoot))
Chandler Carruth69a125b2012-04-06 16:32:06 +00002509 addPathIfExists(LibPath, Paths);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002510 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002511 addPathIfExists(SysRoot + "/lib", Paths);
2512 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002513}
2514
Roman Divackyf0d7f942013-11-10 09:31:43 +00002515bool FreeBSD::HasNativeLLVMSupport() const {
2516 return true;
2517}
2518
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002519bool Linux::HasNativeLLVMSupport() const {
2520 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002521}
2522
Rafael Espindola7cf32212013-03-20 03:05:54 +00002523Tool *Linux::buildLinker() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002524 return new tools::gnutools::Link(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00002525}
2526
2527Tool *Linux::buildAssembler() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002528 return new tools::gnutools::Assemble(*this);
Rafael Espindola92b00932010-08-10 00:25:48 +00002529}
2530
Chandler Carruth05fb5852012-11-21 23:40:23 +00002531void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2532 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002533 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Benjamin Kramer604e8482013-08-09 17:17:48 +00002534 bool UseInitArrayDefault =
2535 !V.isOlderThan(4, 7, 0) ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002536 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002537 getTriple().getEnvironment() == llvm::Triple::Android;
2538 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2539 options::OPT_fno_use_init_array,
2540 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002541 CC1Args.push_back("-fuse-init-array");
2542}
2543
Simon Atanasyana0d89572013-10-05 14:37:55 +00002544std::string Linux::computeSysRoot() const {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002545 if (!getDriver().SysRoot.empty())
2546 return getDriver().SysRoot;
2547
2548 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
2549 return std::string();
2550
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002551 // Standalone MIPS toolchains use different names for sysroot folder
2552 // and put it into different places. Here we try to check some known
2553 // variants.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002554
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002555 const StringRef InstallDir = GCCInstallation.getInstallPath();
2556 const StringRef TripleStr = GCCInstallation.getTriple().str();
Chandler Carruth8677d922013-10-29 08:53:03 +00002557 const StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002558
Chandler Carruth8677d922013-10-29 08:53:03 +00002559 std::string Path = (InstallDir + "/../../../../" + TripleStr + "/libc" +
2560 MIPSABIDirSuffix).str();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002561
2562 if (llvm::sys::fs::exists(Path))
2563 return Path;
2564
Chandler Carruth8677d922013-10-29 08:53:03 +00002565 Path = (InstallDir + "/../../../../sysroot" + MIPSABIDirSuffix).str();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002566
2567 if (llvm::sys::fs::exists(Path))
2568 return Path;
2569
2570 return std::string();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002571}
2572
Chandler Carrutha796f532011-11-05 20:17:13 +00002573void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2574 ArgStringList &CC1Args) const {
2575 const Driver &D = getDriver();
Simon Atanasyana0d89572013-10-05 14:37:55 +00002576 std::string SysRoot = computeSysRoot();
Chandler Carrutha796f532011-11-05 20:17:13 +00002577
2578 if (DriverArgs.hasArg(options::OPT_nostdinc))
2579 return;
2580
2581 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002582 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002583
2584 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola358256c2013-06-26 02:13:00 +00002585 SmallString<128> P(D.ResourceDir);
2586 llvm::sys::path::append(P, "include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002587 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002588 }
2589
2590 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2591 return;
2592
2593 // Check for configure-time C include directories.
2594 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2595 if (CIncludeDirs != "") {
2596 SmallVector<StringRef, 5> dirs;
2597 CIncludeDirs.split(dirs, ":");
2598 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2599 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002600 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? SysRoot : "";
Chandler Carrutha796f532011-11-05 20:17:13 +00002601 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2602 }
2603 return;
2604 }
2605
2606 // Lacking those, try to detect the correct set of system includes for the
2607 // target triple.
2608
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002609 // Sourcery CodeBench and modern FSF Mips toolchains put extern C
2610 // system includes under three additional directories.
2611 if (GCCInstallation.isValid() && isMipsArch(getTriple().getArch())) {
Chandler Carruthb427c562013-06-22 11:35:51 +00002612 addExternCSystemIncludeIfExists(
2613 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + "/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002614
Chandler Carruthb427c562013-06-22 11:35:51 +00002615 addExternCSystemIncludeIfExists(
2616 DriverArgs, CC1Args,
2617 GCCInstallation.getInstallPath() + "/../../../../" +
2618 GCCInstallation.getTriple().str() + "/libc/usr/include");
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002619
2620 addExternCSystemIncludeIfExists(
2621 DriverArgs, CC1Args,
2622 GCCInstallation.getInstallPath() + "/../../../../sysroot/usr/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002623 }
2624
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002625 // Implement generic Debian multiarch support.
2626 const StringRef X86_64MultiarchIncludeDirs[] = {
2627 "/usr/include/x86_64-linux-gnu",
2628
2629 // FIXME: These are older forms of multiarch. It's not clear that they're
2630 // in use in any released version of Debian, so we should consider
2631 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002632 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002633 };
2634 const StringRef X86MultiarchIncludeDirs[] = {
2635 "/usr/include/i386-linux-gnu",
2636
2637 // FIXME: These are older forms of multiarch. It's not clear that they're
2638 // in use in any released version of Debian, so we should consider
2639 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002640 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002641 "/usr/include/i486-linux-gnu"
2642 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002643 const StringRef AArch64MultiarchIncludeDirs[] = {
2644 "/usr/include/aarch64-linux-gnu"
2645 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002646 const StringRef ARMMultiarchIncludeDirs[] = {
2647 "/usr/include/arm-linux-gnueabi"
2648 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002649 const StringRef ARMHFMultiarchIncludeDirs[] = {
2650 "/usr/include/arm-linux-gnueabihf"
2651 };
Eli Friedman7771c832011-11-11 03:05:19 +00002652 const StringRef MIPSMultiarchIncludeDirs[] = {
2653 "/usr/include/mips-linux-gnu"
2654 };
2655 const StringRef MIPSELMultiarchIncludeDirs[] = {
2656 "/usr/include/mipsel-linux-gnu"
2657 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002658 const StringRef PPCMultiarchIncludeDirs[] = {
2659 "/usr/include/powerpc-linux-gnu"
2660 };
2661 const StringRef PPC64MultiarchIncludeDirs[] = {
2662 "/usr/include/powerpc64-linux-gnu"
2663 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002664 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002665 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002666 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002667 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002668 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002669 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2670 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002671 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002672 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2673 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2674 else
2675 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002676 } else if (getTriple().getArch() == llvm::Triple::mips) {
2677 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2678 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2679 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002680 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2681 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2682 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2683 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002684 }
2685 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2686 E = MultiarchIncludeDirs.end();
2687 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002688 if (llvm::sys::fs::exists(SysRoot + *I)) {
2689 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + *I);
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002690 break;
2691 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002692 }
2693
2694 if (getTriple().getOS() == llvm::Triple::RTEMS)
2695 return;
2696
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002697 // Add an include of '/include' directly. This isn't provided by default by
2698 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2699 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002700 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002701
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002702 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002703}
2704
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002705/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002706/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2707 const ArgList &DriverArgs,
2708 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002709 if (!llvm::sys::fs::exists(Base))
2710 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002711 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002712 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002713 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002714 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002715}
2716
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002717/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2718/// libstdc++ installation.
2719/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2720 Twine TargetArchDir,
Chandler Carruth8677d922013-10-29 08:53:03 +00002721 Twine BiarchSuffix,
2722 Twine MIPSABIDirSuffix,
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002723 const ArgList &DriverArgs,
2724 ArgStringList &CC1Args) {
Chandler Carruth8677d922013-10-29 08:53:03 +00002725 if (!addLibStdCXXIncludePaths(Base + Suffix,
2726 TargetArchDir + MIPSABIDirSuffix + BiarchSuffix,
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002727 DriverArgs, CC1Args))
2728 return false;
2729
2730 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
Chandler Carruth8677d922013-10-29 08:53:03 +00002731 + MIPSABIDirSuffix + BiarchSuffix);
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002732 return true;
2733}
2734
Chandler Carrutha796f532011-11-05 20:17:13 +00002735void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2736 ArgStringList &CC1Args) const {
2737 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2738 DriverArgs.hasArg(options::OPT_nostdincxx))
2739 return;
2740
Chandler Carruthf4701732011-11-07 09:01:17 +00002741 // Check if libc++ has been enabled and provide its include paths if so.
2742 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2743 // libc++ is always installed at a fixed path on Linux currently.
2744 addSystemInclude(DriverArgs, CC1Args,
2745 getDriver().SysRoot + "/usr/include/c++/v1");
2746 return;
2747 }
2748
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002749 // We need a detected GCC installation on Linux to provide libstdc++'s
2750 // headers. We handled the libc++ case above.
2751 if (!GCCInstallation.isValid())
2752 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002753
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002754 // By default, look for the C++ headers in an include directory adjacent to
2755 // the lib directory of the GCC installation. Note that this is expect to be
2756 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2757 StringRef LibDir = GCCInstallation.getParentLibPath();
2758 StringRef InstallDir = GCCInstallation.getInstallPath();
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002759 StringRef TripleStr = GCCInstallation.getTriple().str();
Chandler Carruth8677d922013-10-29 08:53:03 +00002760 StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix();
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002761 StringRef BiarchSuffix = GCCInstallation.getBiarchSuffix();
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002762 const GCCVersion &Version = GCCInstallation.getVersion();
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002763
Chandler Carruth8677d922013-10-29 08:53:03 +00002764 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2765 "/c++/" + Version.Text, TripleStr, BiarchSuffix,
2766 MIPSABIDirSuffix, DriverArgs, CC1Args))
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002767 return;
2768
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002769 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002770 // Gentoo is weird and places its headers inside the GCC install, so if the
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002771 // first attempt to find the headers fails, try these patterns.
2772 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
2773 Version.MinorStr,
2774 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002775 // Android standalone toolchain has C++ headers in yet another place.
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002776 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
Hal Finkelf3587912012-09-18 22:25:07 +00002777 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2778 // without a subdirectory corresponding to the gcc version.
2779 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002780 };
2781
2782 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
Chandler Carruth8677d922013-10-29 08:53:03 +00002783 if (addLibStdCXXIncludePaths(IncludePathCandidates[i],
2784 TripleStr + MIPSABIDirSuffix + BiarchSuffix,
2785 DriverArgs, CC1Args))
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002786 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002787 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002788}
2789
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002790bool Linux::isPIEDefault() const {
Peter Collingbourne32701642013-11-01 18:16:25 +00002791 return getSanitizerArgs().hasZeroBaseShadow();
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002792}
2793
Daniel Dunbarcc912342009-05-02 18:28:39 +00002794/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2795
Rafael Espindola1af7c212012-02-19 01:38:32 +00002796DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2797 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002798
2799 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002800 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002801 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002802 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002803
Daniel Dunbar083edf72009-12-21 18:54:17 +00002804 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002805 getFilePaths().push_back("/usr/lib");
John McCall65b8da02013-04-11 22:55:55 +00002806 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
2807 getFilePaths().push_back("/usr/lib/gcc47");
2808 else
2809 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002810}
2811
Rafael Espindola7cf32212013-03-20 03:05:54 +00002812Tool *DragonFly::buildAssembler() const {
2813 return new tools::dragonfly::Assemble(*this);
2814}
2815
2816Tool *DragonFly::buildLinker() const {
2817 return new tools::dragonfly::Link(*this);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002818}
Robert Lyttoncf1dd692013-10-11 10:29:40 +00002819
2820
2821/// XCore tool chain
2822XCore::XCore(const Driver &D, const llvm::Triple &Triple,
2823 const ArgList &Args) : ToolChain(D, Triple, Args) {
2824 // ProgramPaths are found via 'PATH' environment variable.
2825}
2826
2827Tool *XCore::buildAssembler() const {
2828 return new tools::XCore::Assemble(*this);
2829}
2830
2831Tool *XCore::buildLinker() const {
2832 return new tools::XCore::Link(*this);
2833}
2834
2835bool XCore::isPICDefault() const {
2836 return false;
2837}
2838
2839bool XCore::isPIEDefault() const {
2840 return false;
2841}
2842
2843bool XCore::isPICDefaultForced() const {
2844 return false;
2845}
2846
2847bool XCore::SupportsProfiling() const {
2848 return false;
2849}
2850
2851bool XCore::hasBlocksRuntime() const {
2852 return false;
2853}
2854
2855
2856void XCore::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2857 ArgStringList &CC1Args) const {
2858 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2859 DriverArgs.hasArg(options::OPT_nostdlibinc))
2860 return;
2861 if (const char *cl_include_dir = getenv("XCC_C_INCLUDE_PATH")) {
2862 SmallVector<StringRef, 4> Dirs;
2863 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
2864 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
2865 ArrayRef<StringRef> DirVec(Dirs);
2866 addSystemIncludes(DriverArgs, CC1Args, DirVec);
2867 }
2868}
2869
2870void XCore::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
2871 llvm::opt::ArgStringList &CC1Args) const {
2872 CC1Args.push_back("-nostdsysteminc");
2873}
2874
2875void XCore::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2876 ArgStringList &CC1Args) const {
2877 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2878 DriverArgs.hasArg(options::OPT_nostdlibinc))
2879 return;
2880 if (const char *cl_include_dir = getenv("XCC_CPLUS_INCLUDE_PATH")) {
2881 SmallVector<StringRef, 4> Dirs;
2882 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
2883 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
2884 ArrayRef<StringRef> DirVec(Dirs);
2885 addSystemIncludes(DriverArgs, CC1Args, DirVec);
2886 }
2887}
2888
2889void XCore::AddCXXStdlibLibArgs(const ArgList &Args,
2890 ArgStringList &CmdArgs) const {
2891 // We don't output any lib args. This is handled by xcc.
2892}