blob: d2d481c9f252ce714ba9fa3ce3464d90db1bc9f9 [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 {
Tim Northover9c7e0352013-12-12 11:55:52 +000086 if (isTargetIOSBased())
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 {
Tim Northover9c7e0352013-12-12 11:55:52 +000095 if (isTargetIOSBased())
John McCall7959fee2011-09-09 20:41:01 +000096 return !isIPhoneOSVersionLT(3, 2);
Tim Northover9c7e0352013-12-12 11:55:52 +000097 else if (isTargetMacOS())
John McCall7959fee2011-09-09 20:41:01 +000098 return !isMacosxVersionLT(10, 6);
Tim Northover9c7e0352013-12-12 11:55:52 +000099 else {
100 assert(isTargetEmbedded() && "unexpected target platform");
101 return false;
102 }
John McCall7959fee2011-09-09 20:41:01 +0000103}
104
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000105static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilson997a97f2011-10-07 00:37:57 +0000106 return llvm::StringSwitch<const char*>(Value)
107 .Case("armv6k", "armv6")
Bob Wilson743bf672013-03-04 22:37:49 +0000108 .Case("armv6m", "armv6m")
Bob Wilson997a97f2011-10-07 00:37:57 +0000109 .Case("armv5tej", "armv5")
110 .Case("xscale", "xscale")
111 .Case("armv4t", "armv4t")
112 .Case("armv7", "armv7")
113 .Cases("armv7a", "armv7-a", "armv7")
114 .Cases("armv7r", "armv7-r", "armv7")
Bob Wilson743bf672013-03-04 22:37:49 +0000115 .Cases("armv7em", "armv7e-m", "armv7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000116 .Cases("armv7f", "armv7-f", "armv7f")
117 .Cases("armv7k", "armv7-k", "armv7k")
Bob Wilson743bf672013-03-04 22:37:49 +0000118 .Cases("armv7m", "armv7-m", "armv7m")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000119 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilson997a97f2011-10-07 00:37:57 +0000120 .Default(0);
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000121}
122
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000123static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilson997a97f2011-10-07 00:37:57 +0000124 return llvm::StringSwitch<const char *>(Value)
125 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
126 .Cases("arm10e", "arm10tdmi", "armv5")
127 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
128 .Case("xscale", "xscale")
Bob Wilson743bf672013-03-04 22:37:49 +0000129 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6")
130 .Case("cortex-m0", "armv6m")
Renato Golin1a04f222013-09-13 17:02:54 +0000131 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "armv7")
Ana Pazosdd6068d2013-12-06 22:43:17 +0000132 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "krait", "armv7")
Renato Golin60312302013-09-13 17:02:59 +0000133 .Cases("cortex-r4", "cortex-r5", "armv7r")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000134 .Case("cortex-a9-mp", "armv7f")
Bob Wilson743bf672013-03-04 22:37:49 +0000135 .Case("cortex-m3", "armv7m")
136 .Case("cortex-m4", "armv7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000137 .Case("swift", "armv7s")
Bob Wilson997a97f2011-10-07 00:37:57 +0000138 .Default(0);
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000139}
140
Tim Northover9c7e0352013-12-12 11:55:52 +0000141static bool isSoftFloatABI(const ArgList &Args) {
142 Arg *A = Args.getLastArg(options::OPT_msoft_float,
143 options::OPT_mhard_float,
144 options::OPT_mfloat_abi_EQ);
145 if (!A) return false;
146
147 return A->getOption().matches(options::OPT_msoft_float) ||
148 (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
149 A->getValue() == StringRef("soft"));
150}
151
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000152StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000153 switch (getTriple().getArch()) {
154 default:
155 return getArchName();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000156
Douglas Gregord9bb1522011-03-06 19:11:49 +0000157 case llvm::Triple::thumb:
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000158 case llvm::Triple::arm: {
159 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +0000160 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000161 return Arch;
162
163 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +0000164 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000165 return Arch;
166
167 return "arm";
168 }
169 }
170}
171
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000172Darwin::~Darwin() {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000173}
174
Chad Rosierd3a0f952011-09-20 20:44:06 +0000175std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
176 types::ID InputType) const {
177 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000178
179 // If the target isn't initialized (e.g., an unknown Darwin platform, return
180 // the default triple).
181 if (!isTargetInitialized())
182 return Triple.getTriple();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000183
Tim Northover901dee42013-10-24 10:48:50 +0000184 if (Triple.getArchName() == "thumbv6m" ||
185 Triple.getArchName() == "thumbv7m" ||
186 Triple.getArchName() == "thumbv7em") {
187 // OS is ios or macosx unless it's the v6m or v7m.
188 Triple.setOS(llvm::Triple::Darwin);
189 Triple.setEnvironment(llvm::Triple::EABI);
190 } else {
191 SmallString<16> Str;
Tim Northover9c7e0352013-12-12 11:55:52 +0000192 Str += isTargetIOSBased() ? "ios" : "macosx";
Tim Northover901dee42013-10-24 10:48:50 +0000193 Str += getTargetVersion().getAsString();
194 Triple.setOSName(Str);
195 }
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000196
197 return Triple.getTriple();
198}
199
David Blaikie68e081d2011-12-20 02:48:34 +0000200void Generic_ELF::anchor() {}
201
Rafael Espindola7cf32212013-03-20 03:05:54 +0000202Tool *Darwin::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +0000203 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000204 case Action::LipoJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000205 if (!Lipo)
206 Lipo.reset(new tools::darwin::Lipo(*this));
207 return Lipo.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000208 case Action::DsymutilJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000209 if (!Dsymutil)
210 Dsymutil.reset(new tools::darwin::Dsymutil(*this));
211 return Dsymutil.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +0000212 case Action::VerifyJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000213 if (!VerifyDebug)
214 VerifyDebug.reset(new tools::darwin::VerifyDebug(*this));
215 return VerifyDebug.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +0000216 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +0000217 return ToolChain::getTool(AC);
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000218 }
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000219}
220
Rafael Espindola7cf32212013-03-20 03:05:54 +0000221Tool *Darwin::buildLinker() const {
222 return new tools::darwin::Link(*this);
223}
224
225Tool *Darwin::buildAssembler() const {
226 return new tools::darwin::Assemble(*this);
227}
Daniel Dunbar26d482a2009-09-18 08:15:03 +0000228
Rafael Espindola84b588b2013-03-18 18:10:27 +0000229DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple,
230 const ArgList &Args)
231 : Darwin(D, Triple, Args)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000232{
Daniel Dunbar00aff042010-09-17 08:22:12 +0000233 getProgramPaths().push_back(getDriver().getInstalledDir());
234 if (getDriver().getInstalledDir() != getDriver().Dir)
235 getProgramPaths().push_back(getDriver().Dir);
236
Daniel Dunbar6276f992009-09-18 08:15:13 +0000237 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbar88979912010-08-01 22:29:51 +0000238 getProgramPaths().push_back(getDriver().getInstalledDir());
239 if (getDriver().getInstalledDir() != getDriver().Dir)
240 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar6276f992009-09-18 08:15:13 +0000241}
242
John McCall31168b02011-06-15 23:02:42 +0000243void DarwinClang::AddLinkARCArgs(const ArgList &Args,
244 ArgStringList &CmdArgs) const {
Eric Christopher551ef452011-08-23 17:56:55 +0000245
246 CmdArgs.push_back("-force_load");
Rafael Espindola358256c2013-06-26 02:13:00 +0000247 SmallString<128> P(getDriver().ClangExecutable);
248 llvm::sys::path::remove_filename(P); // 'clang'
249 llvm::sys::path::remove_filename(P); // 'bin'
Benjamin Kramer17381a02013-06-28 16:25:46 +0000250 llvm::sys::path::append(P, "lib", "arc", "libarclite_");
John McCall31168b02011-06-15 23:02:42 +0000251 // Mash in the platform.
Argyrios Kyrtzidis058b4512011-10-18 17:40:15 +0000252 if (isTargetIOSSimulator())
Rafael Espindola358256c2013-06-26 02:13:00 +0000253 P += "iphonesimulator";
Argyrios Kyrtzidis058b4512011-10-18 17:40:15 +0000254 else if (isTargetIPhoneOS())
Rafael Espindola358256c2013-06-26 02:13:00 +0000255 P += "iphoneos";
John McCall31168b02011-06-15 23:02:42 +0000256 else
Rafael Espindola358256c2013-06-26 02:13:00 +0000257 P += "macosx";
258 P += ".a";
John McCall31168b02011-06-15 23:02:42 +0000259
Rafael Espindola358256c2013-06-26 02:13:00 +0000260 CmdArgs.push_back(Args.MakeArgString(P));
John McCall31168b02011-06-15 23:02:42 +0000261}
262
Eric Christopherc235d0c62011-06-22 17:41:40 +0000263void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopher551ef452011-08-23 17:56:55 +0000264 ArgStringList &CmdArgs,
Tim Northover9c7e0352013-12-12 11:55:52 +0000265 StringRef DarwinStaticLib,
266 bool AlwaysLink,
267 bool IsEmbedded) const {
Rafael Espindola358256c2013-06-26 02:13:00 +0000268 SmallString<128> P(getDriver().ResourceDir);
Tim Northover9c7e0352013-12-12 11:55:52 +0000269 llvm::sys::path::append(P, "lib", IsEmbedded ? "darwin_embedded" : "darwin",
270 DarwinStaticLib);
Eric Christopher551ef452011-08-23 17:56:55 +0000271
Eric Christopherc235d0c62011-06-22 17:41:40 +0000272 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov8368b372012-11-21 14:17:42 +0000273 // not have compiler-rt checked out or integrated into their build (unless
274 // we explicitly force linking with this library).
Rafael Espindola355670d2013-06-25 15:14:22 +0000275 if (AlwaysLink || llvm::sys::fs::exists(P.str()))
Eric Christopherc235d0c62011-06-22 17:41:40 +0000276 CmdArgs.push_back(Args.MakeArgString(P.str()));
277}
278
Daniel Dunbar6276f992009-09-18 08:15:13 +0000279void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
280 ArgStringList &CmdArgs) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000281 // Darwin only supports the compiler-rt based runtime libraries.
282 switch (GetRuntimeLibType(Args)) {
283 case ToolChain::RLT_CompilerRT:
284 break;
285 default:
286 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smithbd55daf2012-11-01 04:30:05 +0000287 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000288 return;
289 }
290
Tim Northover9c7e0352013-12-12 11:55:52 +0000291 if (isTargetEmbedded()) {
292 // Embedded targets are simple at the moment, not supporting sanitizers and
293 // with different libraries for each member of the product { static, PIC } x
294 // { hard-float, soft-float }
295 llvm::SmallString<32> CompilerRT = StringRef("libclang_rt.");
296 CompilerRT +=
297 tools::arm::getARMFloatABI(getDriver(), Args, getTriple()) == "hard"
298 ? "hard"
299 : "soft";
300 CompilerRT += Args.hasArg(options::OPT_fPIC) ? "_pic.a" : "_static.a";
301
302 AddLinkRuntimeLib(Args, CmdArgs, CompilerRT, false, true);
303 return;
304 }
305
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000306 // Darwin doesn't support real static executables, don't link any runtime
307 // libraries with -static.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000308 if (Args.hasArg(options::OPT_static) ||
309 Args.hasArg(options::OPT_fapple_kext) ||
310 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar6276f992009-09-18 08:15:13 +0000311 return;
Daniel Dunbar6276f992009-09-18 08:15:13 +0000312
313 // Reject -static-libgcc for now, we can deal with this when and if someone
314 // cares. This is useful in situations where someone wants to statically link
315 // something like libstdc++, and needs its runtime support routines.
316 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000317 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000318 << A->getAsString(Args);
319 return;
320 }
321
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000322 // If we are building profile support, link that library in.
323 if (Args.hasArg(options::OPT_fprofile_arcs) ||
324 Args.hasArg(options::OPT_fprofile_generate) ||
325 Args.hasArg(options::OPT_fcreate_profile) ||
326 Args.hasArg(options::OPT_coverage)) {
327 // Select the appropriate runtime library for the target.
Tim Northover9c7e0352013-12-12 11:55:52 +0000328 if (isTargetIOSBased()) {
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000329 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
330 } else {
331 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
332 }
333 }
334
Peter Collingbourne32701642013-11-01 18:16:25 +0000335 const SanitizerArgs &Sanitize = getSanitizerArgs();
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000336
Alexey Samsonovcc429802012-11-16 12:53:14 +0000337 // Add Ubsan runtime library, if required.
338 if (Sanitize.needsUbsanRt()) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000339 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
Tim Northover9c7e0352013-12-12 11:55:52 +0000340 if (isTargetIOSBased()) {
Alexey Samsonovcc429802012-11-16 12:53:14 +0000341 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
342 << "-fsanitize=undefined";
343 } else {
Tim Northover9c7e0352013-12-12 11:55:52 +0000344 assert(isTargetMacOS() && "unexpected non OS X target");
Alexey Samsonov8368b372012-11-21 14:17:42 +0000345 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonovcc429802012-11-16 12:53:14 +0000346
347 // The Ubsan runtime library requires C++.
348 AddCXXStdlibLibArgs(Args, CmdArgs);
349 }
350 }
351
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000352 // Add ASAN runtime library, if required. Dynamic libraries and bundles
353 // should not be linked with the runtime library.
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000354 if (Sanitize.needsAsanRt()) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000355 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
Tim Northover9c7e0352013-12-12 11:55:52 +0000356 if (isTargetIPhoneOS()) {
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000357 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000358 << "-fsanitize=address";
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000359 } else {
Alexander Potapenkoce876332013-09-20 08:09:51 +0000360 if (!Args.hasArg(options::OPT_dynamiclib) &&
361 !Args.hasArg(options::OPT_bundle)) {
Alexander Potapenko5700f402013-03-21 10:49:06 +0000362 // The ASAN runtime library requires C++.
363 AddCXXStdlibLibArgs(Args, CmdArgs);
364 }
Alexander Potapenko868cca92013-11-15 16:07:44 +0000365 if (isTargetMacOS()) {
366 AddLinkRuntimeLib(Args, CmdArgs,
367 "libclang_rt.asan_osx_dynamic.dylib",
368 true);
369 } else {
370 if (isTargetIOSSimulator()) {
371 AddLinkRuntimeLib(Args, CmdArgs,
372 "libclang_rt.asan_iossim_dynamic.dylib",
373 true);
374 }
375 }
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000376 }
377 }
378
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000379 // Otherwise link libSystem, then the dynamic runtime library, and finally any
380 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000381 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000382
383 // Select the dynamic runtime library and the target specific static library.
Tim Northover9c7e0352013-12-12 11:55:52 +0000384 if (isTargetIOSBased()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000385 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
386 // it never went into the SDK.
Bob Wilson102be442011-10-07 17:54:41 +0000387 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
388 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
389 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000390
Daniel Dunbard1076382011-04-18 23:48:36 +0000391 // We currently always need a static runtime library for iOS.
Eric Christopherc235d0c62011-06-22 17:41:40 +0000392 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000393 } else {
Tim Northover9c7e0352013-12-12 11:55:52 +0000394 assert(isTargetMacOS() && "unexpected non MacOS platform");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000395 // The dynamic runtime library was merged with libSystem for 10.6 and
396 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000397 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000398 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000399 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000400 CmdArgs.push_back("-lgcc_s.10.5");
401
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000402 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000403 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000404 // omitted from 10.4.dylib.
405 //
406 // Unfortunately, that turned out to not be true, because Darwin system
407 // headers can still use eprintf on i386, and it is not exported from
408 // libSystem. Therefore, we still must provide a runtime library just for
409 // the tiny tiny handful of projects that *might* use that symbol.
410 if (isMacosxVersionLT(10, 5)) {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000411 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000412 } else {
413 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopherc235d0c62011-06-22 17:41:40 +0000414 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
415 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000416 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000417 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000418}
419
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000420void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000421 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000422
Daniel Dunbar455a0492012-08-17 18:43:50 +0000423 // Support allowing the SDKROOT environment variable used by xcrun and other
424 // Xcode tools to define the default sysroot, by making it the default for
425 // isysroot.
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000426 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
427 // Warn if the path does not exist.
Rafael Espindola355670d2013-06-25 15:14:22 +0000428 if (!llvm::sys::fs::exists(A->getValue()))
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000429 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
430 } else {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000431 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbarb2543042013-01-15 20:33:56 +0000432 // We only use this value as the default if it is an absolute path,
433 // exists, and it is not the root path.
434 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
435 StringRef(env) != "/") {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000436 Args.append(Args.MakeSeparateArg(
437 0, Opts.getOption(options::OPT_isysroot), env));
438 }
439 }
440 }
441
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000442 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000443 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
444 Arg *iOSSimVersion = Args.getLastArg(
445 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman027e9c32012-01-11 02:41:15 +0000446
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000447 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000448 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000449 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000450 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
451 iOSVersion = iOSSimVersion = 0;
452 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000453 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000454 << iOSVersion->getAsString(Args)
455 << iOSSimVersion->getAsString(Args);
456 iOSSimVersion = 0;
457 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosier64707fe2011-08-31 20:56:25 +0000458 // If no deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000459 // environment defines.
Chad Rosier64707fe2011-08-31 20:56:25 +0000460 StringRef OSXTarget;
461 StringRef iOSTarget;
462 StringRef iOSSimTarget;
463 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
464 OSXTarget = env;
465 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
466 iOSTarget = env;
467 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
468 iOSSimTarget = env;
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000469
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000470 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosier64707fe2011-08-31 20:56:25 +0000471 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif5d3231c2012-04-18 10:59:08 +0000472 // based on -isysroot.
Chad Rosier64707fe2011-08-31 20:56:25 +0000473 if (iOSTarget.empty()) {
474 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
475 StringRef first, second;
Richard Smithbd55daf2012-11-01 04:30:05 +0000476 StringRef isysroot = A->getValue();
Chad Rosier64707fe2011-08-31 20:56:25 +0000477 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
478 if (second != "")
479 iOSTarget = second.substr(0,3);
480 }
481 }
Daniel Dunbard54669d2010-01-26 01:45:19 +0000482
Chad Rosierfe6fd362011-09-28 00:46:32 +0000483 // If no OSX or iOS target has been specified and we're compiling for armv7,
484 // go ahead as assume we're targeting iOS.
Tim Northover9c7e0352013-12-12 11:55:52 +0000485 StringRef DarwinArchName = getDarwinArchName(Args);
Chad Rosier7b1fee12012-05-09 18:55:57 +0000486 if (OSXTarget.empty() && iOSTarget.empty() &&
Tim Northover9c7e0352013-12-12 11:55:52 +0000487 (DarwinArchName == "armv7" || DarwinArchName == "armv7s"))
Chad Rosierf761fe92012-05-09 18:09:58 +0000488 iOSTarget = iOSVersionMin;
Chad Rosierfe6fd362011-09-28 00:46:32 +0000489
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000490 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000491 //
492 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000493
494 // Do not allow conflicts with the iOS simulator target.
Chad Rosier64707fe2011-08-31 20:56:25 +0000495 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000496 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000497 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosier64707fe2011-08-31 20:56:25 +0000498 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000499 "IPHONEOS_DEPLOYMENT_TARGET");
500 }
501
502 // Allow conflicts among OSX and iOS for historical reasons, but choose the
503 // default platform.
Chad Rosier64707fe2011-08-31 20:56:25 +0000504 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000505 if (getTriple().getArch() == llvm::Triple::arm ||
506 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosier64707fe2011-08-31 20:56:25 +0000507 OSXTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000508 else
Chad Rosier64707fe2011-08-31 20:56:25 +0000509 iOSTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000510 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000511
Chad Rosier64707fe2011-08-31 20:56:25 +0000512 if (!OSXTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000513 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000514 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
515 Args.append(OSXVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000516 } else if (!iOSTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000517 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000518 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
519 Args.append(iOSVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000520 } else if (!iOSSimTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000521 const Option O = Opts.getOption(
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000522 options::OPT_mios_simulator_version_min_EQ);
523 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
524 Args.append(iOSSimVersion);
Tim Northover9c7e0352013-12-12 11:55:52 +0000525 } else if (DarwinArchName != "armv6m" && DarwinArchName != "armv7m" &&
526 DarwinArchName != "armv7em") {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000527 // Otherwise, assume we are targeting OS X.
Michael J. Spencerfc790902012-10-19 22:36:40 +0000528 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000529 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
530 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000531 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Tim Northover9c7e0352013-12-12 11:55:52 +0000534 DarwinPlatformKind Platform;
535 if (OSXVersion)
536 Platform = MacOS;
537 else if (iOSVersion)
538 Platform = IPhoneOS;
539 else if (iOSSimVersion)
540 Platform = IPhoneOSSimulator;
541 else
542 Platform = Embedded;
543
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000544 // Reject invalid architecture combinations.
545 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
546 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000547 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000548 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
549 }
550
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000551 // Set the tool chain target information.
552 unsigned Major, Minor, Micro;
553 bool HadExtra;
Tim Northover9c7e0352013-12-12 11:55:52 +0000554 if (Platform == MacOS) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000555 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000556 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000557 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000558 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000560 << OSXVersion->getAsString(Args);
Tim Northover9c7e0352013-12-12 11:55:52 +0000561 } else if (Platform == IPhoneOS || Platform == IPhoneOSSimulator) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000562 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
563 assert(Version && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000564 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman027e9c32012-01-11 02:41:15 +0000565 Micro, HadExtra) || HadExtra ||
566 Major >= 10 || Minor >= 100 || Micro >= 100)
567 getDriver().Diag(diag::err_drv_invalid_version_number)
568 << Version->getAsString(Args);
Tim Northover9c7e0352013-12-12 11:55:52 +0000569 } else {
570 assert(Platform == Embedded && "unexpected platform");
571 Major = Minor = Micro = 0;
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000572 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000573
Daniel Dunbarb1189432011-04-30 04:18:16 +0000574 // In GCC, the simulator historically was treated as being OS X in some
575 // contexts, like determining the link logic, despite generally being called
576 // with an iOS deployment target. For compatibility, we detect the
577 // simulator as iOS + x86, and treat it differently in a few contexts.
578 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
579 getTriple().getArch() == llvm::Triple::x86_64))
Tim Northover9c7e0352013-12-12 11:55:52 +0000580 Platform = IPhoneOSSimulator;
Daniel Dunbarb1189432011-04-30 04:18:16 +0000581
Tim Northover9c7e0352013-12-12 11:55:52 +0000582 setTarget(Platform, Major, Minor, Micro);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000583}
584
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000585void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000586 ArgStringList &CmdArgs) const {
587 CXXStdlibType Type = GetCXXStdlibType(Args);
588
589 switch (Type) {
590 case ToolChain::CST_Libcxx:
591 CmdArgs.push_back("-lc++");
592 break;
593
594 case ToolChain::CST_Libstdcxx: {
595 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
596 // it was previously found in the gcc lib dir. However, for all the Darwin
597 // platforms we care about it was -lstdc++.6, so we search for that
598 // explicitly if we can't see an obvious -lstdc++ candidate.
599
600 // Check in the sysroot first.
601 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000602 SmallString<128> P(A->getValue());
Benjamin Kramer17381a02013-06-28 16:25:46 +0000603 llvm::sys::path::append(P, "usr", "lib", "libstdc++.dylib");
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000604
Rafael Espindola355670d2013-06-25 15:14:22 +0000605 if (!llvm::sys::fs::exists(P.str())) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000606 llvm::sys::path::remove_filename(P);
607 llvm::sys::path::append(P, "libstdc++.6.dylib");
Rafael Espindola355670d2013-06-25 15:14:22 +0000608 if (llvm::sys::fs::exists(P.str())) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000609 CmdArgs.push_back(Args.MakeArgString(P.str()));
610 return;
611 }
612 }
613 }
614
615 // Otherwise, look in the root.
Bob Wilson1a9ad0f2011-11-11 07:47:04 +0000616 // FIXME: This should be removed someday when we don't have to care about
617 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Rafael Espindola355670d2013-06-25 15:14:22 +0000618 if (!llvm::sys::fs::exists("/usr/lib/libstdc++.dylib") &&
619 llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib")) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000620 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
621 return;
622 }
623
624 // Otherwise, let the linker search.
625 CmdArgs.push_back("-lstdc++");
626 break;
627 }
628 }
629}
630
Shantonu Senafeb03b2010-09-17 18:39:08 +0000631void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
632 ArgStringList &CmdArgs) const {
633
634 // For Darwin platforms, use the compiler-rt-based support library
635 // instead of the gcc-provided one (which is also incidentally
636 // only present in the gcc lib dir, which makes it hard to find).
637
Rafael Espindola358256c2013-06-26 02:13:00 +0000638 SmallString<128> P(getDriver().ResourceDir);
Benjamin Kramer17381a02013-06-28 16:25:46 +0000639 llvm::sys::path::append(P, "lib", "darwin");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000640
641 // Use the newer cc_kext for iOS ARM after 6.0.
642 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
643 !isIPhoneOSVersionLT(6, 0)) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000644 llvm::sys::path::append(P, "libclang_rt.cc_kext.a");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000645 } else {
Rafael Espindola358256c2013-06-26 02:13:00 +0000646 llvm::sys::path::append(P, "libclang_rt.cc_kext_ios5.a");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000647 }
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000648
Shantonu Senafeb03b2010-09-17 18:39:08 +0000649 // For now, allow missing resource libraries to support developers who may
650 // not have compiler-rt checked out or integrated into their build.
Rafael Espindola355670d2013-06-25 15:14:22 +0000651 if (llvm::sys::fs::exists(P.str()))
Shantonu Senafeb03b2010-09-17 18:39:08 +0000652 CmdArgs.push_back(Args.MakeArgString(P.str()));
653}
654
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000655DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
656 const char *BoundArch) const {
657 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
658 const OptTable &Opts = getDriver().getOpts();
659
660 // FIXME: We really want to get out of the tool chain level argument
661 // translation business, as it makes the driver functionality much
662 // more opaque. For now, we follow gcc closely solely for the
663 // purpose of easily achieving feature parity & testability. Once we
664 // have something that works, we should reevaluate each translation
665 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000666
Daniel Dunbar775d4062010-06-11 22:00:26 +0000667 for (ArgList::const_iterator it = Args.begin(),
668 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000669 Arg *A = *it;
670
671 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar471c4f82011-06-21 00:20:17 +0000672 // Skip this argument unless the architecture matches either the toolchain
673 // triple arch, or the arch being bound.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000674 llvm::Triple::ArchType XarchArch =
Richard Smithbd55daf2012-11-01 04:30:05 +0000675 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000676 if (!(XarchArch == getArch() ||
677 (BoundArch && XarchArch ==
Rafael Espindoladcbf6982012-10-31 18:51:07 +0000678 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000679 continue;
680
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000681 Arg *OriginalArg = A;
Richard Smithbd55daf2012-11-01 04:30:05 +0000682 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000683 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000684 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000685
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000686 // If the argument parsing failed or more than one argument was
687 // consumed, the -Xarch_ argument's parameter tried to consume
688 // extra arguments. Emit an error and ignore.
689 //
690 // We also want to disallow any options which would alter the
691 // driver behavior; that isn't going to work in our model. We
692 // use isDriverOption() as an approximation, although things
693 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000694 if (!XarchArg || Index > Prev + 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000695 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar6914a982011-04-21 17:32:21 +0000696 << A->getAsString(Args);
697 continue;
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000698 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000699 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000700 << A->getAsString(Args);
701 continue;
702 }
703
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000704 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000705 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000706
707 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000708
709 // Linker input arguments require custom handling. The problem is that we
710 // have already constructed the phase actions, so we can not treat them as
711 // "input arguments".
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000712 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000713 // Convert the argument into individual Zlinker_input_args.
714 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
715 DAL->AddSeparateArg(OriginalArg,
716 Opts.getOption(options::OPT_Zlinker_input),
Richard Smithbd55daf2012-11-01 04:30:05 +0000717 A->getValue(i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000718
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000719 }
720 continue;
721 }
Mike Stump11289f42009-09-09 15:08:12 +0000722 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000723
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000724 // Sob. These is strictly gcc compatible for the time being. Apple
725 // gcc translates options twice, which means that self-expanding
726 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000727 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000728 default:
729 DAL->append(A);
730 break;
731
732 case options::OPT_mkernel:
733 case options::OPT_fapple_kext:
734 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000735 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000736 break;
Mike Stump11289f42009-09-09 15:08:12 +0000737
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000738 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000739 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smithbd55daf2012-11-01 04:30:05 +0000740 A->getValue());
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000741 break;
742
743 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000744 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
745 DAL->AddFlagArg(A,
746 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000747 break;
748
749 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000750 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
751 DAL->AddFlagArg(A,
752 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000753 break;
754
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000755 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000756 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000757 break;
758
759 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000760 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000761 break;
762
763 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000764 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000765 break;
766
767 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000768 DAL->AddFlagArg(A,
769 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000770 break;
771
772 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000773 DAL->AddFlagArg(A,
774 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000775 break;
776
777 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000778 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000779 break;
780
781 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000782 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000783 break;
784 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000785 }
786
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000787 if (getTriple().getArch() == llvm::Triple::x86 ||
788 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000789 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000790 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000791
792 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosier7c5d9082012-04-27 14:58:16 +0000793 // how the driver driver works.
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000794 if (BoundArch) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000795 StringRef Name = BoundArch;
Michael J. Spencerfc790902012-10-19 22:36:40 +0000796 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
797 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000798
799 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
800 // which defines the list of which architectures we accept.
801 if (Name == "ppc")
802 ;
803 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000804 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000805 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000806 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000807 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000808 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000809 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000810 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000811 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000812 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000813 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000814 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000815 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000816 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000817 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000818 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000819
Bill Schmidt778d3872013-07-26 01:36:11 +0000820 else if (Name == "ppc64" || Name == "ppc64le")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000821 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000822
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000823 else if (Name == "i386")
824 ;
825 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000826 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000827 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000828 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000829 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000830 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000831 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000832 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000833 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000834 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000835 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000836 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000837 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000838 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000839
840 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000841 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Jim Grosbach82eee262013-11-16 00:53:35 +0000842 else if (Name == "x86_64h") {
843 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
844 DAL->AddJoinedArg(0, MArch, "x86_64h");
845 }
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000846
847 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000848 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000849 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000850 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000851 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000852 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000853 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000854 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000855 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000856 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000857 else if (Name == "armv6m")
858 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000859 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000860 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000861 else if (Name == "armv7em")
862 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000863 else if (Name == "armv7f")
864 DAL->AddJoinedArg(0, MArch, "armv7f");
865 else if (Name == "armv7k")
866 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000867 else if (Name == "armv7m")
868 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000869 else if (Name == "armv7s")
870 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000871
872 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000873 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000874 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000875
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000876 // Add an explicit version min argument for the deployment target. We do this
877 // after argument translation because -Xarch_ arguments may add a version min
878 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000879 if (BoundArch)
880 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000881
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000882 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
883 // FIXME: It would be far better to avoid inserting those -static arguments,
884 // but we can't check the deployment target in the translation code until
885 // it is set here.
Tim Northover9c7e0352013-12-12 11:55:52 +0000886 if (isTargetIOSBased() && !isIPhoneOSVersionLT(6, 0)) {
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000887 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
888 Arg *A = *it;
889 ++it;
890 if (A->getOption().getID() != options::OPT_mkernel &&
891 A->getOption().getID() != options::OPT_fapple_kext)
892 continue;
893 assert(it != ie && "unexpected argument translation");
894 A = *it;
895 assert(A->getOption().getID() == options::OPT_static &&
896 "missing expected -static argument");
897 it = DAL->getArgs().erase(it);
898 }
899 }
900
Bob Wilson0f7445b2013-11-02 23:19:53 +0000901 // Default to use libc++ on OS X 10.9+ and iOS 7+.
902 if (((isTargetMacOS() && !isMacosxVersionLT(10, 9)) ||
Tim Northover9c7e0352013-12-12 11:55:52 +0000903 (isTargetIOSBased() && !isIPhoneOSVersionLT(7, 0))) &&
Bob Wilson0f7445b2013-11-02 23:19:53 +0000904 !Args.getLastArg(options::OPT_stdlib_EQ))
905 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_stdlib_EQ), "libc++");
906
Bob Wilson102be442011-10-07 17:54:41 +0000907 // Validate the C++ standard library choice.
908 CXXStdlibType Type = GetCXXStdlibType(*DAL);
909 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000910 // Check whether the target provides libc++.
911 StringRef where;
912
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000913 // Complain about targeting iOS < 5.0 in any way.
Tim Northover9c7e0352013-12-12 11:55:52 +0000914 if (isTargetIOSBased() && isIPhoneOSVersionLT(5, 0))
Bob Wilson5ad5a952012-11-09 01:59:30 +0000915 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000916
917 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000918 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000919 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000920 }
921 }
922
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000923 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000924}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000925
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000926bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000927 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000928}
929
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000930bool Darwin::UseDwarfDebugFlags() const {
931 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
932 return S[0] != '\0';
933 return false;
934}
935
Daniel Dunbar3241d402010-02-10 18:49:11 +0000936bool Darwin::UseSjLjExceptions() const {
937 // Darwin uses SjLj exceptions on ARM.
938 return (getTriple().getArch() == llvm::Triple::arm ||
939 getTriple().getArch() == llvm::Triple::thumb);
940}
941
Chandler Carruth76a943b2012-11-19 03:52:03 +0000942bool Darwin::isPICDefault() const {
943 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000944}
945
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000946bool Darwin::isPIEDefault() const {
947 return false;
948}
949
Chandler Carruth76a943b2012-11-19 03:52:03 +0000950bool Darwin::isPICDefaultForced() const {
951 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000952}
953
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000954bool Darwin::SupportsProfiling() const {
955 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000956 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000957}
958
Daniel Dunbar16334e12010-04-10 16:20:23 +0000959bool Darwin::SupportsObjCGC() const {
Tim Northover9c7e0352013-12-12 11:55:52 +0000960 return isTargetMacOS();
Daniel Dunbar16334e12010-04-10 16:20:23 +0000961}
962
John McCall3deb1ad2012-08-21 02:47:43 +0000963void Darwin::CheckObjCARC() const {
Tim Northover9c7e0352013-12-12 11:55:52 +0000964 if (isTargetIOSBased()|| (isTargetMacOS() && !isMacosxVersionLT(10, 6)))
John McCall3deb1ad2012-08-21 02:47:43 +0000965 return;
John McCall93207072012-08-27 01:56:21 +0000966 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000967}
968
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000969/// Generic_GCC - A tool chain using the 'gcc' command to perform
970/// all subcommands; this relies on gcc translating the majority of
971/// command line options.
972
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000973/// \brief Parse a GCCVersion object out of a string of text.
974///
975/// This is the primary means of forming GCCVersion objects.
976/*static*/
977Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000978 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000979 std::pair<StringRef, StringRef> First = VersionText.split('.');
980 std::pair<StringRef, StringRef> Second = First.second.split('.');
981
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000982 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000983 if (First.first.getAsInteger(10, GoodVersion.Major) ||
984 GoodVersion.Major < 0)
985 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000986 GoodVersion.MajorStr = First.first.str();
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000987 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
988 GoodVersion.Minor < 0)
989 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000990 GoodVersion.MinorStr = Second.first.str();
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000991
992 // First look for a number prefix and parse that if present. Otherwise just
993 // stash the entire patch string in the suffix, and leave the number
994 // unspecified. This covers versions strings such as:
995 // 4.4
996 // 4.4.0
997 // 4.4.x
998 // 4.4.2-rc4
999 // 4.4.x-patched
1000 // And retains any patch number it finds.
1001 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1002 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +00001003 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001004 // Try to parse the number and any suffix.
1005 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1006 GoodVersion.Patch < 0)
1007 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00001008 GoodVersion.PatchSuffix = PatchText.substr(EndNumber);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001009 }
1010 }
1011
1012 return GoodVersion;
1013}
1014
1015/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001016bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor,
1017 int RHSPatch,
1018 StringRef RHSPatchSuffix) const {
1019 if (Major != RHSMajor)
1020 return Major < RHSMajor;
1021 if (Minor != RHSMinor)
1022 return Minor < RHSMinor;
1023 if (Patch != RHSPatch) {
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001024 // Note that versions without a specified patch sort higher than those with
1025 // a patch.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001026 if (RHSPatch == -1)
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001027 return true;
1028 if (Patch == -1)
1029 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001030
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001031 // Otherwise just sort on the patch itself.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001032 return Patch < RHSPatch;
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001033 }
Benjamin Kramer604e8482013-08-09 17:17:48 +00001034 if (PatchSuffix != RHSPatchSuffix) {
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001035 // Sort empty suffixes higher.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001036 if (RHSPatchSuffix.empty())
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001037 return true;
1038 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +00001039 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001040
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001041 // Provide a lexicographic sort to make this a total ordering.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001042 return PatchSuffix < RHSPatchSuffix;
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001043 }
1044
1045 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001046 return false;
1047}
1048
Rafael Espindola1af7c212012-02-19 01:38:32 +00001049static StringRef getGCCToolchainDir(const ArgList &Args) {
1050 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1051 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +00001052 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +00001053 return GCC_INSTALL_PREFIX;
1054}
1055
Roman Divacky326d9982013-12-06 18:32:18 +00001056/// \brief Initialize a GCCInstallationDetector from the driver.
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001057///
1058/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +00001059/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +00001060///
1061/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1062/// should instead pull the target out of the driver. This is currently
1063/// necessary because the driver doesn't store the final version of the target
1064/// triple.
Roman Divacky326d9982013-12-06 18:32:18 +00001065void
1066Generic_GCC::GCCInstallationDetector::init(
1067 const Driver &D, const llvm::Triple &TargetTriple, const ArgList &Args) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001068 llvm::Triple BiarchVariantTriple =
1069 TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
Chandler Carruth779579b2012-02-13 02:02:09 +00001070 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001071 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001072 // The library directories which may contain GCC installations.
Chandler Carruthb427c562013-06-22 11:35:51 +00001073 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001074 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001075 SmallVector<StringRef, 10> CandidateTripleAliases;
Chandler Carruthb427c562013-06-22 11:35:51 +00001076 SmallVector<StringRef, 10> CandidateBiarchTripleAliases;
1077 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1078 CandidateTripleAliases, CandidateBiarchLibDirs,
1079 CandidateBiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001080
1081 // Compute the set of prefixes for our search.
1082 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1083 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001084
Rafael Espindola1af7c212012-02-19 01:38:32 +00001085 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1086 if (GCCToolchainDir != "") {
1087 if (GCCToolchainDir.back() == '/')
1088 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001089
Rafael Espindola1af7c212012-02-19 01:38:32 +00001090 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001091 } else {
Rafael Espindola0f4b04e2013-08-28 23:17:47 +00001092 // If we have a SysRoot, try that first.
1093 if (!D.SysRoot.empty()) {
1094 Prefixes.push_back(D.SysRoot);
1095 Prefixes.push_back(D.SysRoot + "/usr");
1096 }
1097
1098 // Then look for gcc installed alongside clang.
Rafael Espindolac29af942012-02-03 01:01:20 +00001099 Prefixes.push_back(D.InstalledDir + "/..");
Rafael Espindola0f4b04e2013-08-28 23:17:47 +00001100
1101 // And finally in /usr.
1102 if (D.SysRoot.empty())
1103 Prefixes.push_back("/usr");
Rafael Espindolac29af942012-02-03 01:01:20 +00001104 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001105
1106 // Loop over the various components which exist and select the best GCC
1107 // installation available. GCC installs are ranked by version number.
1108 Version = GCCVersion::Parse("0.0.0");
1109 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1110 if (!llvm::sys::fs::exists(Prefixes[i]))
1111 continue;
1112 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1113 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1114 if (!llvm::sys::fs::exists(LibDir))
1115 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001116 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001117 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1118 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001119 }
Chandler Carruthb427c562013-06-22 11:35:51 +00001120 for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) {
1121 const std::string LibDir = Prefixes[i] + CandidateBiarchLibDirs[j].str();
Chandler Carruth866faab2012-01-25 07:21:38 +00001122 if (!llvm::sys::fs::exists(LibDir))
1123 continue;
Chandler Carruthb427c562013-06-22 11:35:51 +00001124 for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke;
Chandler Carruth866faab2012-01-25 07:21:38 +00001125 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001126 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruthb427c562013-06-22 11:35:51 +00001127 CandidateBiarchTripleAliases[k],
1128 /*NeedsBiarchSuffix=*/ true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001129 }
1130 }
1131}
1132
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001133void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const {
Benjamin Kramera97e4d12013-08-14 18:38:51 +00001134 for (std::set<std::string>::const_iterator
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001135 I = CandidateGCCInstallPaths.begin(),
1136 E = CandidateGCCInstallPaths.end();
1137 I != E; ++I)
1138 OS << "Found candidate GCC installation: " << *I << "\n";
1139
1140 OS << "Selected GCC installation: " << GCCInstallPath << "\n";
1141}
1142
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001143/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruthb427c562013-06-22 11:35:51 +00001144 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
Chandler Carruth866faab2012-01-25 07:21:38 +00001145 SmallVectorImpl<StringRef> &LibDirs,
1146 SmallVectorImpl<StringRef> &TripleAliases,
Chandler Carruthb427c562013-06-22 11:35:51 +00001147 SmallVectorImpl<StringRef> &BiarchLibDirs,
1148 SmallVectorImpl<StringRef> &BiarchTripleAliases) {
Chandler Carruth866faab2012-01-25 07:21:38 +00001149 // Declare a bunch of static data sets that we'll select between below. These
1150 // are specifically designed to always refer to string literals to avoid any
1151 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001152 static const char *const AArch64LibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001153 static const char *const AArch64Triples[] = { "aarch64-none-linux-gnu",
1154 "aarch64-linux-gnu" };
Tim Northover9bb857a2013-01-31 12:13:10 +00001155
Chandler Carruth866faab2012-01-25 07:21:38 +00001156 static const char *const ARMLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001157 static const char *const ARMTriples[] = { "arm-linux-gnueabi",
1158 "arm-linux-androideabi" };
1159 static const char *const ARMHFTriples[] = { "arm-linux-gnueabihf",
1160 "armv7hl-redhat-linux-gnueabi" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001161
1162 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1163 static const char *const X86_64Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001164 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", "x86_64-pc-linux-gnu",
1165 "x86_64-redhat-linux6E", "x86_64-redhat-linux", "x86_64-suse-linux",
1166 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", "x86_64-slackware-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001167 };
1168 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1169 static const char *const X86Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001170 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", "i386-linux-gnu",
1171 "i386-redhat-linux6E", "i686-redhat-linux", "i586-redhat-linux",
1172 "i386-redhat-linux", "i586-suse-linux", "i486-slackware-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001173 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001174 };
1175
1176 static const char *const MIPSLibDirs[] = { "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001177 static const char *const MIPSTriples[] = { "mips-linux-gnu",
1178 "mips-mti-linux-gnu" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001179 static const char *const MIPSELLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001180 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu",
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001181 "mipsel-linux-android" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001182
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001183 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001184 static const char *const MIPS64Triples[] = { "mips64-linux-gnu",
1185 "mips-mti-linux-gnu" };
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001186 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001187 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu",
1188 "mips-mti-linux-gnu" };
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001189
Chandler Carruth866faab2012-01-25 07:21:38 +00001190 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1191 static const char *const PPCTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001192 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
1193 "powerpc-suse-linux", "powerpc-montavista-linuxspe"
Chandler Carruth866faab2012-01-25 07:21:38 +00001194 };
1195 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001196 static const char *const PPC64Triples[] = { "powerpc64-linux-gnu",
1197 "powerpc64-unknown-linux-gnu",
1198 "powerpc64-suse-linux",
1199 "ppc64-redhat-linux" };
Bill Schmidt778d3872013-07-26 01:36:11 +00001200 static const char *const PPC64LELibDirs[] = { "/lib64", "/lib" };
1201 static const char *const PPC64LETriples[] = { "powerpc64le-linux-gnu",
1202 "powerpc64le-unknown-linux-gnu",
1203 "powerpc64le-suse-linux",
1204 "ppc64le-redhat-linux" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001205
Ulrich Weigand47445072013-05-06 16:26:41 +00001206 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1207 static const char *const SystemZTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001208 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
1209 "s390x-suse-linux", "s390x-redhat-linux"
Ulrich Weigand47445072013-05-06 16:26:41 +00001210 };
1211
Chandler Carruth866faab2012-01-25 07:21:38 +00001212 switch (TargetTriple.getArch()) {
Tim Northover9bb857a2013-01-31 12:13:10 +00001213 case llvm::Triple::aarch64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001214 LibDirs.append(AArch64LibDirs,
1215 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1216 TripleAliases.append(AArch64Triples,
1217 AArch64Triples + llvm::array_lengthof(AArch64Triples));
1218 BiarchLibDirs.append(AArch64LibDirs,
1219 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1220 BiarchTripleAliases.append(
1221 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
Tim Northover9bb857a2013-01-31 12:13:10 +00001222 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001223 case llvm::Triple::arm:
1224 case llvm::Triple::thumb:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001225 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001226 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001227 TripleAliases.append(ARMHFTriples,
1228 ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001229 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001230 TripleAliases.append(ARMTriples,
1231 ARMTriples + llvm::array_lengthof(ARMTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001232 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001233 break;
1234 case llvm::Triple::x86_64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001235 LibDirs.append(X86_64LibDirs,
1236 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1237 TripleAliases.append(X86_64Triples,
1238 X86_64Triples + llvm::array_lengthof(X86_64Triples));
1239 BiarchLibDirs.append(X86LibDirs,
1240 X86LibDirs + llvm::array_lengthof(X86LibDirs));
1241 BiarchTripleAliases.append(X86Triples,
1242 X86Triples + llvm::array_lengthof(X86Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001243 break;
1244 case llvm::Triple::x86:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001245 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001246 TripleAliases.append(X86Triples,
1247 X86Triples + llvm::array_lengthof(X86Triples));
1248 BiarchLibDirs.append(X86_64LibDirs,
1249 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1250 BiarchTripleAliases.append(
1251 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001252 break;
1253 case llvm::Triple::mips:
Chandler Carruthb427c562013-06-22 11:35:51 +00001254 LibDirs.append(MIPSLibDirs,
1255 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1256 TripleAliases.append(MIPSTriples,
1257 MIPSTriples + llvm::array_lengthof(MIPSTriples));
1258 BiarchLibDirs.append(MIPS64LibDirs,
1259 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1260 BiarchTripleAliases.append(
1261 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001262 break;
1263 case llvm::Triple::mipsel:
Chandler Carruthb427c562013-06-22 11:35:51 +00001264 LibDirs.append(MIPSELLibDirs,
1265 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1266 TripleAliases.append(MIPSELTriples,
1267 MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001268 TripleAliases.append(MIPSTriples,
1269 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Chandler Carruthb427c562013-06-22 11:35:51 +00001270 BiarchLibDirs.append(
1271 MIPS64ELLibDirs,
1272 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1273 BiarchTripleAliases.append(
1274 MIPS64ELTriples,
1275 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001276 break;
1277 case llvm::Triple::mips64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001278 LibDirs.append(MIPS64LibDirs,
1279 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1280 TripleAliases.append(MIPS64Triples,
1281 MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1282 BiarchLibDirs.append(MIPSLibDirs,
1283 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1284 BiarchTripleAliases.append(MIPSTriples,
1285 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001286 break;
1287 case llvm::Triple::mips64el:
Chandler Carruthb427c562013-06-22 11:35:51 +00001288 LibDirs.append(MIPS64ELLibDirs,
1289 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001290 TripleAliases.append(
Chandler Carruthb427c562013-06-22 11:35:51 +00001291 MIPS64ELTriples,
1292 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1293 BiarchLibDirs.append(MIPSELLibDirs,
1294 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1295 BiarchTripleAliases.append(
1296 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001297 BiarchTripleAliases.append(
1298 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001299 break;
1300 case llvm::Triple::ppc:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001301 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001302 TripleAliases.append(PPCTriples,
1303 PPCTriples + llvm::array_lengthof(PPCTriples));
1304 BiarchLibDirs.append(PPC64LibDirs,
1305 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1306 BiarchTripleAliases.append(
1307 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001308 break;
1309 case llvm::Triple::ppc64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001310 LibDirs.append(PPC64LibDirs,
1311 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1312 TripleAliases.append(PPC64Triples,
1313 PPC64Triples + llvm::array_lengthof(PPC64Triples));
1314 BiarchLibDirs.append(PPCLibDirs,
1315 PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1316 BiarchTripleAliases.append(PPCTriples,
1317 PPCTriples + llvm::array_lengthof(PPCTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001318 break;
Bill Schmidt778d3872013-07-26 01:36:11 +00001319 case llvm::Triple::ppc64le:
1320 LibDirs.append(PPC64LELibDirs,
1321 PPC64LELibDirs + llvm::array_lengthof(PPC64LELibDirs));
1322 TripleAliases.append(PPC64LETriples,
1323 PPC64LETriples + llvm::array_lengthof(PPC64LETriples));
1324 break;
Ulrich Weigand47445072013-05-06 16:26:41 +00001325 case llvm::Triple::systemz:
Chandler Carruthb427c562013-06-22 11:35:51 +00001326 LibDirs.append(SystemZLibDirs,
1327 SystemZLibDirs + llvm::array_lengthof(SystemZLibDirs));
1328 TripleAliases.append(SystemZTriples,
1329 SystemZTriples + llvm::array_lengthof(SystemZTriples));
Ulrich Weigand47445072013-05-06 16:26:41 +00001330 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001331
1332 default:
1333 // By default, just rely on the standard lib directories and the original
1334 // triple.
1335 break;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001336 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001337
1338 // Always append the drivers target triple to the end, in case it doesn't
1339 // match any of our aliases.
1340 TripleAliases.push_back(TargetTriple.str());
1341
1342 // Also include the multiarch variant if it's different.
Chandler Carruthb427c562013-06-22 11:35:51 +00001343 if (TargetTriple.str() != BiarchTriple.str())
1344 BiarchTripleAliases.push_back(BiarchTriple.str());
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001345}
1346
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001347static bool isMipsArch(llvm::Triple::ArchType Arch) {
1348 return Arch == llvm::Triple::mips ||
1349 Arch == llvm::Triple::mipsel ||
1350 Arch == llvm::Triple::mips64 ||
1351 Arch == llvm::Triple::mips64el;
1352}
1353
1354static bool isMips16(const ArgList &Args) {
1355 Arg *A = Args.getLastArg(options::OPT_mips16,
1356 options::OPT_mno_mips16);
1357 return A && A->getOption().matches(options::OPT_mips16);
1358}
1359
Simon Atanasyan068e0fd2013-10-09 12:12:39 +00001360static bool isMips32r2(const ArgList &Args) {
1361 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1362 options::OPT_mcpu_EQ);
1363
1364 return A && A->getValue() == StringRef("mips32r2");
1365}
1366
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001367static bool isMips64r2(const ArgList &Args) {
1368 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1369 options::OPT_mcpu_EQ);
1370
1371 return A && A->getValue() == StringRef("mips64r2");
1372}
1373
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001374static bool isMicroMips(const ArgList &Args) {
1375 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1376 options::OPT_mno_micromips);
1377 return A && A->getOption().matches(options::OPT_mmicromips);
1378}
1379
Simon Atanasyan5c5b5da2013-11-20 13:53:20 +00001380static bool isMipsFP64(const ArgList &Args) {
1381 Arg *A = Args.getLastArg(options::OPT_mfp64, options::OPT_mfp32);
1382 return A && A->getOption().matches(options::OPT_mfp64);
1383}
1384
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001385static bool isMipsNan2008(const ArgList &Args) {
1386 Arg *A = Args.getLastArg(options::OPT_mnan_EQ);
1387 return A && A->getValue() == StringRef("2008");
1388}
1389
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001390// FIXME: There is the same routine in the Tools.cpp.
1391static bool hasMipsN32ABIArg(const ArgList &Args) {
1392 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00001393 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001394}
1395
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001396static bool hasCrtBeginObj(Twine Path) {
1397 return llvm::sys::fs::exists(Path + "/crtbegin.o");
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001398}
1399
Chandler Carruthb427c562013-06-22 11:35:51 +00001400static bool findTargetBiarchSuffix(std::string &Suffix, StringRef Path,
1401 llvm::Triple::ArchType TargetArch,
1402 const ArgList &Args) {
1403 // FIXME: This routine was only intended to model bi-arch toolchains which
1404 // use -m32 and -m64 to swap between variants of a target. It shouldn't be
1405 // doing ABI-based builtin location for MIPS.
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001406 if (hasMipsN32ABIArg(Args))
1407 Suffix = "/n32";
1408 else if (TargetArch == llvm::Triple::x86_64 ||
1409 TargetArch == llvm::Triple::ppc64 ||
1410 TargetArch == llvm::Triple::systemz ||
1411 TargetArch == llvm::Triple::mips64 ||
1412 TargetArch == llvm::Triple::mips64el)
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001413 Suffix = "/64";
1414 else
1415 Suffix = "/32";
1416
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001417 return hasCrtBeginObj(Path + Suffix);
1418}
1419
Chandler Carruth8677d922013-10-29 08:53:03 +00001420void Generic_GCC::GCCInstallationDetector::findMIPSABIDirSuffix(
1421 std::string &Suffix, llvm::Triple::ArchType TargetArch, StringRef Path,
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001422 const llvm::opt::ArgList &Args) {
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001423 // Some MIPS toolchains put libraries and object files compiled
1424 // using different options in to the sub-directoris which names
1425 // reflects the flags used for compilation. For example sysroot
1426 // directory might looks like the following examples:
1427 //
1428 // /usr
1429 // /lib <= crt*.o files compiled with '-mips32'
1430 // /mips16
1431 // /usr
1432 // /lib <= crt*.o files compiled with '-mips16'
1433 // /el
1434 // /usr
1435 // /lib <= crt*.o files compiled with '-mips16 -EL'
1436 //
1437 // or
1438 //
1439 // /usr
1440 // /lib <= crt*.o files compiled with '-mips32r2'
1441 // /mips16
1442 // /usr
1443 // /lib <= crt*.o files compiled with '-mips32r2 -mips16'
1444 // /mips32
1445 // /usr
1446 // /lib <= crt*.o files compiled with '-mips32'
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001447
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001448 // Check FSF Toolchain path
1449 Suffix.clear();
1450 if (TargetArch == llvm::Triple::mips ||
1451 TargetArch == llvm::Triple::mipsel) {
1452 if (isMicroMips(Args))
1453 Suffix += "/micromips";
1454 else if (isMips32r2(Args))
1455 Suffix += "";
1456 else
1457 Suffix += "/mips32";
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001458
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001459 if (isMips16(Args))
1460 Suffix += "/mips16";
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001461 } else {
1462 if (isMips64r2(Args))
1463 Suffix += hasMipsN32ABIArg(Args) ? "/mips64r2" : "/mips64r2/64";
1464 else
1465 Suffix += hasMipsN32ABIArg(Args) ? "/mips64" : "/mips64/64";
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001466 }
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001467
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001468 if (TargetArch == llvm::Triple::mipsel ||
1469 TargetArch == llvm::Triple::mips64el)
1470 Suffix += "/el";
1471
1472 if (isSoftFloatABI(Args))
1473 Suffix += "/sof";
1474 else {
1475 if (isMipsFP64(Args))
1476 Suffix += "/fp64";
1477
1478 if (isMipsNan2008(Args))
1479 Suffix += "/nan2008";
1480 }
1481
1482 if (hasCrtBeginObj(Path + Suffix))
1483 return;
1484
1485 // Check Code Sourcery Toolchain path
1486 Suffix.clear();
1487 if (isMips16(Args))
1488 Suffix += "/mips16";
1489 else if (isMicroMips(Args))
1490 Suffix += "/micromips";
1491
1492 if (isSoftFloatABI(Args))
1493 Suffix += "/soft-float";
Simon Atanasyan3e0f3882013-11-26 11:57:48 +00001494 else if (isMipsNan2008(Args))
1495 Suffix += "/nan2008";
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001496
1497 if (TargetArch == llvm::Triple::mipsel ||
1498 TargetArch == llvm::Triple::mips64el)
1499 Suffix += "/el";
1500
1501 if (hasCrtBeginObj(Path + Suffix))
1502 return;
1503
1504 Suffix.clear();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001505}
1506
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001507void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001508 llvm::Triple::ArchType TargetArch, const ArgList &Args,
Chandler Carruthb427c562013-06-22 11:35:51 +00001509 const std::string &LibDir, StringRef CandidateTriple,
1510 bool NeedsBiarchSuffix) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001511 // There are various different suffixes involving the triple we
1512 // check for. We also record what is necessary to walk from each back
1513 // up to the lib directory.
Chandler Carruth866faab2012-01-25 07:21:38 +00001514 const std::string LibSuffixes[] = {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001515 "/gcc/" + CandidateTriple.str(),
Eli Friedmanbf44f362013-07-26 00:53:40 +00001516 // Debian puts cross-compilers in gcc-cross
1517 "/gcc-cross/" + CandidateTriple.str(),
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001518 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1519
Hal Finkelf3587912012-09-18 22:25:07 +00001520 // The Freescale PPC SDK has the gcc libraries in
1521 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1522 "/" + CandidateTriple.str(),
1523
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001524 // Ubuntu has a strange mis-matched pair of triples that this happens to
1525 // match.
1526 // FIXME: It may be worthwhile to generalize this and look for a second
1527 // triple.
Chandler Carruth6e46ca22011-11-09 03:46:20 +00001528 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001529 };
Eli Friedmanbf44f362013-07-26 00:53:40 +00001530 const std::string InstallSuffixes[] = {
1531 "/../../..", // gcc/
1532 "/../../..", // gcc-cross/
1533 "/../../../..", // <triple>/gcc/
1534 "/../..", // <triple>/
1535 "/../../../.." // i386-linux-gnu/gcc/<triple>/
1536 };
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001537 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruthb427c562013-06-22 11:35:51 +00001538 const unsigned NumLibSuffixes =
1539 (llvm::array_lengthof(LibSuffixes) - (TargetArch != llvm::Triple::x86));
Chandler Carruth866faab2012-01-25 07:21:38 +00001540 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1541 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001542 llvm::error_code EC;
Chandler Carruth866faab2012-01-25 07:21:38 +00001543 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001544 !EC && LI != LE; LI = LI.increment(EC)) {
1545 StringRef VersionText = llvm::sys::path::filename(LI->path());
1546 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
Benjamin Kramera97e4d12013-08-14 18:38:51 +00001547 if (CandidateVersion.Major != -1) // Filter obviously bad entries.
1548 if (!CandidateGCCInstallPaths.insert(LI->path()).second)
1549 continue; // Saw this path before; no need to look at it again.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001550 if (CandidateVersion.isOlderThan(4, 1, 1))
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001551 continue;
1552 if (CandidateVersion <= Version)
1553 continue;
Hal Finkel221e11e2011-12-08 05:50:03 +00001554
Chandler Carruth8677d922013-10-29 08:53:03 +00001555 std::string MIPSABIDirSuffix;
Simon Atanasyan54f3f932013-11-26 11:57:09 +00001556 if (isMipsArch(TargetArch))
1557 findMIPSABIDirSuffix(MIPSABIDirSuffix, TargetArch, LI->path(), Args);
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001558
Hal Finkel221e11e2011-12-08 05:50:03 +00001559 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth64cee062012-01-24 19:21:42 +00001560 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth866faab2012-01-25 07:21:38 +00001561 // libs in a subdirectory named 64. The simple logic we follow is that
1562 // *if* there is a subdirectory of the right name with crtbegin.o in it,
Chandler Carruthb427c562013-06-22 11:35:51 +00001563 // we use that. If not, and if not a biarch triple alias, we look for
Chandler Carruth866faab2012-01-25 07:21:38 +00001564 // crtbegin.o without the subdirectory.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001565
Chandler Carruthb427c562013-06-22 11:35:51 +00001566 std::string BiarchSuffix;
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001567 if (findTargetBiarchSuffix(BiarchSuffix,
Chandler Carruth8677d922013-10-29 08:53:03 +00001568 LI->path() + MIPSABIDirSuffix,
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001569 TargetArch, Args)) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001570 GCCBiarchSuffix = BiarchSuffix;
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001571 } else if (NeedsBiarchSuffix ||
Chandler Carruth8677d922013-10-29 08:53:03 +00001572 !hasCrtBeginObj(LI->path() + MIPSABIDirSuffix)) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001573 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001574 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001575 GCCBiarchSuffix.clear();
Chandler Carruth866faab2012-01-25 07:21:38 +00001576 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001577
1578 Version = CandidateVersion;
Chandler Carruth4d9d7682012-01-24 19:28:29 +00001579 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001580 // FIXME: We hack together the directory name here instead of
1581 // using LI to ensure stable path separators across Windows and
1582 // Linux.
Chandler Carruth866faab2012-01-25 07:21:38 +00001583 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth64cee062012-01-24 19:21:42 +00001584 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth8677d922013-10-29 08:53:03 +00001585 GCCMIPSABIDirSuffix = MIPSABIDirSuffix;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001586 IsValid = true;
1587 }
1588 }
1589}
1590
Rafael Espindola1af7c212012-02-19 01:38:32 +00001591Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1592 const ArgList &Args)
Roman Divacky326d9982013-12-06 18:32:18 +00001593 : ToolChain(D, Triple, Args), GCCInstallation() {
Daniel Dunbar88979912010-08-01 22:29:51 +00001594 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001595 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001596 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001597}
1598
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001599Generic_GCC::~Generic_GCC() {
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001600}
1601
Rafael Espindola7cf32212013-03-20 03:05:54 +00001602Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +00001603 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001604 case Action::PreprocessJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001605 if (!Preprocess)
1606 Preprocess.reset(new tools::gcc::Preprocess(*this));
1607 return Preprocess.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001608 case Action::CompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001609 if (!Compile)
1610 Compile.reset(new tools::gcc::Compile(*this));
1611 return Compile.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +00001612 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001613 return ToolChain::getTool(AC);
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001614 }
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001615}
1616
Rafael Espindola7cf32212013-03-20 03:05:54 +00001617Tool *Generic_GCC::buildAssembler() const {
Rafael Espindolaedaa4442013-11-23 16:40:57 +00001618 return new tools::gnutools::Assemble(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00001619}
1620
1621Tool *Generic_GCC::buildLinker() const {
1622 return new tools::gcc::Link(*this);
1623}
1624
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001625void Generic_GCC::printVerboseInfo(raw_ostream &OS) const {
1626 // Print the information about how we detected the GCC installation.
1627 GCCInstallation.print(OS);
1628}
1629
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001630bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001631 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001632}
1633
Chandler Carruth76a943b2012-11-19 03:52:03 +00001634bool Generic_GCC::isPICDefault() const {
1635 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001636}
1637
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001638bool Generic_GCC::isPIEDefault() const {
1639 return false;
1640}
1641
Chandler Carruth76a943b2012-11-19 03:52:03 +00001642bool Generic_GCC::isPICDefaultForced() const {
1643 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001644}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001645
Rafael Espindolaa8b3b682013-11-25 18:50:53 +00001646bool Generic_GCC::IsIntegratedAssemblerDefault() const {
1647 return getTriple().getArch() == llvm::Triple::x86 ||
Renato Golin1588cda2013-12-11 09:35:10 +00001648 getTriple().getArch() == llvm::Triple::x86_64 ||
1649 getTriple().getArch() == llvm::Triple::aarch64 ||
1650 getTriple().getArch() == llvm::Triple::arm ||
1651 getTriple().getArch() == llvm::Triple::thumb;
Rafael Espindolaa8b3b682013-11-25 18:50:53 +00001652}
1653
Tony Linthicum76329bf2011-12-12 21:14:55 +00001654/// Hexagon Toolchain
1655
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001656std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1657
1658 // Locate the rest of the toolchain ...
1659 if (strlen(GCC_INSTALL_PREFIX))
1660 return std::string(GCC_INSTALL_PREFIX);
1661
1662 std::string InstallRelDir = InstalledDir + "/../../gnu";
1663 if (llvm::sys::fs::exists(InstallRelDir))
1664 return InstallRelDir;
1665
1666 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1667 if (llvm::sys::fs::exists(PrefixRelDir))
1668 return PrefixRelDir;
1669
1670 return InstallRelDir;
1671}
1672
Matthew Curtise689b052012-12-06 15:46:07 +00001673static void GetHexagonLibraryPaths(
1674 const ArgList &Args,
1675 const std::string Ver,
1676 const std::string MarchString,
1677 const std::string &InstalledDir,
1678 ToolChain::path_list *LibPaths)
1679{
1680 bool buildingLib = Args.hasArg(options::OPT_shared);
1681
1682 //----------------------------------------------------------------------------
1683 // -L Args
1684 //----------------------------------------------------------------------------
1685 for (arg_iterator
1686 it = Args.filtered_begin(options::OPT_L),
1687 ie = Args.filtered_end();
1688 it != ie;
1689 ++it) {
1690 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1691 LibPaths->push_back((*it)->getValue(i));
1692 }
1693
1694 //----------------------------------------------------------------------------
1695 // Other standard paths
1696 //----------------------------------------------------------------------------
1697 const std::string MarchSuffix = "/" + MarchString;
1698 const std::string G0Suffix = "/G0";
1699 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1700 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1701
1702 // lib/gcc/hexagon/...
1703 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1704 if (buildingLib) {
1705 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1706 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1707 }
1708 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1709 LibPaths->push_back(LibGCCHexagonDir + Ver);
1710
1711 // lib/gcc/...
1712 LibPaths->push_back(RootDir + "lib/gcc");
1713
1714 // hexagon/lib/...
1715 std::string HexagonLibDir = RootDir + "hexagon/lib";
1716 if (buildingLib) {
1717 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1718 LibPaths->push_back(HexagonLibDir + G0Suffix);
1719 }
1720 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1721 LibPaths->push_back(HexagonLibDir);
1722}
1723
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001724Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1725 const ArgList &Args)
1726 : Linux(D, Triple, Args) {
1727 const std::string InstalledDir(getDriver().getInstalledDir());
1728 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1729
1730 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1731 // program paths
1732 const std::string BinDir(GnuDir + "/bin");
1733 if (llvm::sys::fs::exists(BinDir))
1734 getProgramPaths().push_back(BinDir);
1735
1736 // Determine version of GCC libraries and headers to use.
1737 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1738 llvm::error_code ec;
1739 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1740 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1741 !ec && di != de; di = di.increment(ec)) {
1742 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1743 if (MaxVersion < cv)
1744 MaxVersion = cv;
1745 }
1746 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001747
1748 ToolChain::path_list *LibPaths= &getFilePaths();
1749
1750 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1751 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1752 // support 'linux' we'll need to fix this up
1753 LibPaths->clear();
1754
1755 GetHexagonLibraryPaths(
1756 Args,
1757 GetGCCLibAndIncVersion(),
1758 GetTargetCPU(Args),
1759 InstalledDir,
1760 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001761}
1762
1763Hexagon_TC::~Hexagon_TC() {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001764}
1765
Rafael Espindola7cf32212013-03-20 03:05:54 +00001766Tool *Hexagon_TC::buildAssembler() const {
1767 return new tools::hexagon::Assemble(*this);
1768}
1769
1770Tool *Hexagon_TC::buildLinker() const {
1771 return new tools::hexagon::Link(*this);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001772}
1773
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001774void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1775 ArgStringList &CC1Args) const {
1776 const Driver &D = getDriver();
1777
1778 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1779 DriverArgs.hasArg(options::OPT_nostdlibinc))
1780 return;
1781
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001782 std::string Ver(GetGCCLibAndIncVersion());
1783 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1784 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1785 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1786 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1787 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001788}
1789
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001790void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1791 ArgStringList &CC1Args) const {
1792
1793 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1794 DriverArgs.hasArg(options::OPT_nostdincxx))
1795 return;
1796
1797 const Driver &D = getDriver();
1798 std::string Ver(GetGCCLibAndIncVersion());
Rafael Espindola358256c2013-06-26 02:13:00 +00001799 SmallString<128> IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001800
Rafael Espindola358256c2013-06-26 02:13:00 +00001801 llvm::sys::path::append(IncludeDir, "hexagon/include/c++/");
1802 llvm::sys::path::append(IncludeDir, Ver);
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001803 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001804}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001805
Matthew Curtise689b052012-12-06 15:46:07 +00001806ToolChain::CXXStdlibType
1807Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1808 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1809 if (!A)
1810 return ToolChain::CST_Libstdcxx;
1811
1812 StringRef Value = A->getValue();
1813 if (Value != "libstdc++") {
1814 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1815 << A->getAsString(Args);
1816 }
1817
1818 return ToolChain::CST_Libstdcxx;
1819}
1820
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001821static int getHexagonVersion(const ArgList &Args) {
1822 Arg *A = Args.getLastArg(options::OPT_march_EQ, options::OPT_mcpu_EQ);
1823 // Select the default CPU (v4) if none was given.
1824 if (!A)
1825 return 4;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001826
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001827 // FIXME: produce errors if we cannot parse the version.
1828 StringRef WhichHexagon = A->getValue();
1829 if (WhichHexagon.startswith("hexagonv")) {
1830 int Val;
1831 if (!WhichHexagon.substr(sizeof("hexagonv") - 1).getAsInteger(10, Val))
1832 return Val;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001833 }
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001834 if (WhichHexagon.startswith("v")) {
1835 int Val;
1836 if (!WhichHexagon.substr(1).getAsInteger(10, Val))
1837 return Val;
1838 }
1839
1840 // FIXME: should probably be an error.
1841 return 4;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001842}
1843
1844StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1845{
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001846 int V = getHexagonVersion(Args);
1847 // FIXME: We don't support versions < 4. We should error on them.
1848 switch (V) {
1849 default:
1850 llvm_unreachable("Unexpected version");
1851 case 5:
1852 return "v5";
1853 case 4:
1854 return "v4";
1855 case 3:
1856 return "v3";
1857 case 2:
1858 return "v2";
1859 case 1:
1860 return "v1";
Matthew Curtisf10a5952012-12-06 14:16:43 +00001861 }
Matthew Curtisf10a5952012-12-06 14:16:43 +00001862}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001863// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001864
Chris Lattner09797542010-03-04 21:07:38 +00001865/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1866/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1867/// Currently does not support anything else but compilation.
1868
Rafael Espindola84b588b2013-03-18 18:10:27 +00001869TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
1870 const ArgList &Args)
1871 : ToolChain(D, Triple, Args) {
Chris Lattner09797542010-03-04 21:07:38 +00001872 // Path mangling to find libexec
1873 std::string Path(getDriver().Dir);
1874
1875 Path += "/../libexec";
1876 getProgramPaths().push_back(Path);
1877}
1878
1879TCEToolChain::~TCEToolChain() {
Chris Lattner09797542010-03-04 21:07:38 +00001880}
1881
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001882bool TCEToolChain::IsMathErrnoDefault() const {
1883 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001884}
1885
Chandler Carruth76a943b2012-11-19 03:52:03 +00001886bool TCEToolChain::isPICDefault() const {
1887 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001888}
1889
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001890bool TCEToolChain::isPIEDefault() const {
1891 return false;
1892}
1893
Chandler Carruth76a943b2012-11-19 03:52:03 +00001894bool TCEToolChain::isPICDefaultForced() const {
1895 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001896}
1897
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001898/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1899
Rafael Espindola1af7c212012-02-19 01:38:32 +00001900OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1901 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001902 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001903 getFilePaths().push_back("/usr/lib");
1904}
1905
Rafael Espindola7cf32212013-03-20 03:05:54 +00001906Tool *OpenBSD::buildAssembler() const {
1907 return new tools::openbsd::Assemble(*this);
1908}
1909
1910Tool *OpenBSD::buildLinker() const {
1911 return new tools::openbsd::Link(*this);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001912}
1913
Eli Friedman9fa28852012-08-08 23:57:20 +00001914/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1915
1916Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1917 : Generic_ELF(D, Triple, Args) {
1918 getFilePaths().push_back(getDriver().Dir + "/../lib");
1919 getFilePaths().push_back("/usr/lib");
1920}
1921
Rafael Espindola7cf32212013-03-20 03:05:54 +00001922Tool *Bitrig::buildAssembler() const {
1923 return new tools::bitrig::Assemble(*this);
1924}
1925
1926Tool *Bitrig::buildLinker() const {
1927 return new tools::bitrig::Link(*this);
Eli Friedman9fa28852012-08-08 23:57:20 +00001928}
1929
1930void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1931 ArgStringList &CC1Args) const {
1932 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1933 DriverArgs.hasArg(options::OPT_nostdincxx))
1934 return;
1935
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001936 switch (GetCXXStdlibType(DriverArgs)) {
1937 case ToolChain::CST_Libcxx:
1938 addSystemInclude(DriverArgs, CC1Args,
1939 getDriver().SysRoot + "/usr/include/c++/");
1940 break;
1941 case ToolChain::CST_Libstdcxx:
1942 addSystemInclude(DriverArgs, CC1Args,
1943 getDriver().SysRoot + "/usr/include/c++/stdc++");
1944 addSystemInclude(DriverArgs, CC1Args,
1945 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001946
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001947 StringRef Triple = getTriple().str();
1948 if (Triple.startswith("amd64"))
1949 addSystemInclude(DriverArgs, CC1Args,
1950 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1951 Triple.substr(5));
1952 else
1953 addSystemInclude(DriverArgs, CC1Args,
1954 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1955 Triple);
1956 break;
1957 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001958}
1959
1960void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1961 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001962 switch (GetCXXStdlibType(Args)) {
1963 case ToolChain::CST_Libcxx:
1964 CmdArgs.push_back("-lc++");
1965 CmdArgs.push_back("-lcxxrt");
1966 // Include supc++ to provide Unwind until provided by libcxx.
1967 CmdArgs.push_back("-lgcc");
1968 break;
1969 case ToolChain::CST_Libstdcxx:
1970 CmdArgs.push_back("-lstdc++");
1971 break;
1972 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001973}
1974
Daniel Dunbare24297c2009-03-30 21:06:03 +00001975/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1976
Rafael Espindola1af7c212012-02-19 01:38:32 +00001977FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1978 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001979
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001980 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1981 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001982 if ((Triple.getArch() == llvm::Triple::x86 ||
1983 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001984 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001985 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1986 else
1987 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001988}
1989
David Chisnall867ccd82013-11-09 15:10:56 +00001990ToolChain::CXXStdlibType
1991FreeBSD::GetCXXStdlibType(const ArgList &Args) const {
1992 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
1993 StringRef Value = A->getValue();
1994 if (Value == "libstdc++")
1995 return ToolChain::CST_Libstdcxx;
1996 if (Value == "libc++")
1997 return ToolChain::CST_Libcxx;
1998
1999 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2000 << A->getAsString(Args);
2001 }
2002 if (getTriple().getOSMajorVersion() >= 10)
2003 return ToolChain::CST_Libcxx;
2004 return ToolChain::CST_Libstdcxx;
2005}
2006
2007void FreeBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2008 ArgStringList &CC1Args) const {
2009 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2010 DriverArgs.hasArg(options::OPT_nostdincxx))
2011 return;
2012
2013 switch (GetCXXStdlibType(DriverArgs)) {
2014 case ToolChain::CST_Libcxx:
2015 addSystemInclude(DriverArgs, CC1Args,
2016 getDriver().SysRoot + "/usr/include/c++/v1");
2017 break;
2018 case ToolChain::CST_Libstdcxx:
2019 addSystemInclude(DriverArgs, CC1Args,
2020 getDriver().SysRoot + "/usr/include/c++/4.2");
2021 addSystemInclude(DriverArgs, CC1Args,
2022 getDriver().SysRoot + "/usr/include/c++/4.2/backward");
2023 break;
2024 }
2025}
2026
Rafael Espindola7cf32212013-03-20 03:05:54 +00002027Tool *FreeBSD::buildAssembler() const {
2028 return new tools::freebsd::Assemble(*this);
2029}
2030
2031Tool *FreeBSD::buildLinker() const {
2032 return new tools::freebsd::Link(*this);
Daniel Dunbare24297c2009-03-30 21:06:03 +00002033}
Daniel Dunbarcc912342009-05-02 18:28:39 +00002034
Rafael Espindola0f207ed2012-12-13 04:17:14 +00002035bool FreeBSD::UseSjLjExceptions() const {
2036 // FreeBSD uses SjLj exceptions on ARM oabi.
2037 switch (getTriple().getEnvironment()) {
2038 case llvm::Triple::GNUEABI:
2039 case llvm::Triple::EABI:
2040 return false;
2041
2042 default:
2043 return (getTriple().getArch() == llvm::Triple::arm ||
2044 getTriple().getArch() == llvm::Triple::thumb);
2045 }
2046}
2047
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002048/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
2049
Rafael Espindola1af7c212012-02-19 01:38:32 +00002050NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2051 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002052
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00002053 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00002054 // When targeting a 32-bit platform, try the special directory used on
2055 // 64-bit hosts, and only fall back to the main library directory if that
2056 // doesn't work.
2057 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
2058 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00002059 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00002060 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00002061
2062 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002063 }
2064}
2065
Rafael Espindola7cf32212013-03-20 03:05:54 +00002066Tool *NetBSD::buildAssembler() const {
2067 return new tools::netbsd::Assemble(*this);
2068}
2069
2070Tool *NetBSD::buildLinker() const {
2071 return new tools::netbsd::Link(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002072}
2073
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00002074ToolChain::CXXStdlibType
2075NetBSD::GetCXXStdlibType(const ArgList &Args) const {
2076 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
2077 StringRef Value = A->getValue();
2078 if (Value == "libstdc++")
2079 return ToolChain::CST_Libstdcxx;
2080 if (Value == "libc++")
2081 return ToolChain::CST_Libcxx;
2082
2083 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2084 << A->getAsString(Args);
2085 }
2086
Joerg Sonnenbergera4435632013-10-14 20:13:05 +00002087 unsigned Major, Minor, Micro;
2088 getTriple().getOSVersion(Major, Minor, Micro);
2089 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 23) || Major == 0) {
2090 if (getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64)
2091 return ToolChain::CST_Libcxx;
2092 }
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00002093 return ToolChain::CST_Libstdcxx;
2094}
2095
2096void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2097 ArgStringList &CC1Args) const {
2098 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2099 DriverArgs.hasArg(options::OPT_nostdincxx))
2100 return;
2101
2102 switch (GetCXXStdlibType(DriverArgs)) {
2103 case ToolChain::CST_Libcxx:
2104 addSystemInclude(DriverArgs, CC1Args,
2105 getDriver().SysRoot + "/usr/include/c++/");
2106 break;
2107 case ToolChain::CST_Libstdcxx:
2108 addSystemInclude(DriverArgs, CC1Args,
2109 getDriver().SysRoot + "/usr/include/g++");
2110 addSystemInclude(DriverArgs, CC1Args,
2111 getDriver().SysRoot + "/usr/include/g++/backward");
2112 break;
2113 }
2114}
2115
Chris Lattner3e2ee142010-07-07 16:01:42 +00002116/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
2117
Rafael Espindola1af7c212012-02-19 01:38:32 +00002118Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2119 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00002120 getFilePaths().push_back(getDriver().Dir + "/../lib");
2121 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00002122}
2123
Rafael Espindola7cf32212013-03-20 03:05:54 +00002124Tool *Minix::buildAssembler() const {
2125 return new tools::minix::Assemble(*this);
2126}
2127
2128Tool *Minix::buildLinker() const {
2129 return new tools::minix::Link(*this);
Chris Lattner3e2ee142010-07-07 16:01:42 +00002130}
2131
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002132/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
2133
Rafael Espindola1af7c212012-02-19 01:38:32 +00002134AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
2135 const ArgList &Args)
2136 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002137
Daniel Dunbar88979912010-08-01 22:29:51 +00002138 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002139 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002140 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002141
Daniel Dunbar083edf72009-12-21 18:54:17 +00002142 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002143 getFilePaths().push_back("/usr/lib");
2144 getFilePaths().push_back("/usr/sfw/lib");
2145 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00002146 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002147
2148}
2149
Rafael Espindola7cf32212013-03-20 03:05:54 +00002150Tool *AuroraUX::buildAssembler() const {
2151 return new tools::auroraux::Assemble(*this);
2152}
2153
2154Tool *AuroraUX::buildLinker() const {
2155 return new tools::auroraux::Link(*this);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002156}
2157
David Chisnallf571cde2012-02-15 13:39:01 +00002158/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2159
Rafael Espindola1af7c212012-02-19 01:38:32 +00002160Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2161 const ArgList &Args)
2162 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00002163
2164 getProgramPaths().push_back(getDriver().getInstalledDir());
2165 if (getDriver().getInstalledDir() != getDriver().Dir)
2166 getProgramPaths().push_back(getDriver().Dir);
2167
2168 getFilePaths().push_back(getDriver().Dir + "/../lib");
2169 getFilePaths().push_back("/usr/lib");
2170}
2171
Rafael Espindola7cf32212013-03-20 03:05:54 +00002172Tool *Solaris::buildAssembler() const {
2173 return new tools::solaris::Assemble(*this);
2174}
2175
2176Tool *Solaris::buildLinker() const {
2177 return new tools::solaris::Link(*this);
David Chisnallf571cde2012-02-15 13:39:01 +00002178}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002179
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002180/// Distribution (very bare-bones at the moment).
Eli Friedman5cd659f2009-05-26 07:52:18 +00002181
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002182enum Distro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002183 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002184 DebianLenny,
2185 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00002186 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002187 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00002188 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00002189 RHEL4,
2190 RHEL5,
2191 RHEL6,
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002192 Fedora,
Rafael Espindola10a63c22013-07-03 14:14:00 +00002193 OpenSUSE,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00002194 UbuntuHardy,
2195 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00002196 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00002197 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002198 UbuntuLucid,
2199 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00002200 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00002201 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002202 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00002203 UbuntuQuantal,
2204 UbuntuRaring,
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002205 UbuntuSaucy,
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002206 UbuntuTrusty,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002207 UnknownDistro
2208};
2209
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002210static bool IsRedhat(enum Distro Distro) {
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002211 return Distro == Fedora || (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002212}
2213
Rafael Espindola10a63c22013-07-03 14:14:00 +00002214static bool IsOpenSUSE(enum Distro Distro) {
2215 return Distro == OpenSUSE;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002216}
2217
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002218static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002219 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002220}
2221
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002222static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002223 return Distro >= UbuntuHardy && Distro <= UbuntuTrusty;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002224}
2225
Rafael Espindolaa8398552013-10-28 23:14:34 +00002226static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00002227 OwningPtr<llvm::MemoryBuffer> File;
Rafael Espindolaa8398552013-10-28 23:14:34 +00002228 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002229 StringRef Data = File.get()->getBuffer();
2230 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002231 Data.split(Lines, "\n");
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002232 Distro Version = UnknownDistro;
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002233 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2234 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002235 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002236 .Case("hardy", UbuntuHardy)
2237 .Case("intrepid", UbuntuIntrepid)
2238 .Case("jaunty", UbuntuJaunty)
2239 .Case("karmic", UbuntuKarmic)
2240 .Case("lucid", UbuntuLucid)
2241 .Case("maverick", UbuntuMaverick)
2242 .Case("natty", UbuntuNatty)
2243 .Case("oneiric", UbuntuOneiric)
2244 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002245 .Case("quantal", UbuntuQuantal)
2246 .Case("raring", UbuntuRaring)
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002247 .Case("saucy", UbuntuSaucy)
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002248 .Case("trusty", UbuntuTrusty)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002249 .Default(UnknownDistro);
2250 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002251 }
2252
Rafael Espindolaa8398552013-10-28 23:14:34 +00002253 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002254 StringRef Data = File.get()->getBuffer();
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002255 if (Data.startswith("Fedora release"))
2256 return Fedora;
Chris Lattner84e38552011-05-22 05:36:06 +00002257 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002258 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002259 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002260 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002261 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002262 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002263 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002264 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002265 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002266 Data.find("release 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002267 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002268 return UnknownDistro;
2269 }
2270
Rafael Espindolaa8398552013-10-28 23:14:34 +00002271 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002272 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002273 if (Data[0] == '5')
2274 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002275 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002276 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002277 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002278 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002279 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2280 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002281 return UnknownDistro;
2282 }
2283
Rafael Espindolaa8398552013-10-28 23:14:34 +00002284 if (llvm::sys::fs::exists("/etc/SuSE-release"))
Rafael Espindola10a63c22013-07-03 14:14:00 +00002285 return OpenSUSE;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002286
Rafael Espindolaa8398552013-10-28 23:14:34 +00002287 if (llvm::sys::fs::exists("/etc/exherbo-release"))
Rafael Espindola12479842010-11-11 02:07:13 +00002288 return Exherbo;
2289
Rafael Espindolaa8398552013-10-28 23:14:34 +00002290 if (llvm::sys::fs::exists("/etc/arch-release"))
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002291 return ArchLinux;
2292
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002293 return UnknownDistro;
2294}
2295
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002296/// \brief Get our best guess at the multiarch triple for a target.
2297///
2298/// Debian-based systems are starting to use a multiarch setup where they use
2299/// a target-triple directory in the library and header search paths.
2300/// Unfortunately, this triple does not align with the vanilla target triple,
2301/// so we provide a rough mapping here.
2302static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2303 StringRef SysRoot) {
2304 // For most architectures, just use whatever we have rather than trying to be
2305 // clever.
2306 switch (TargetTriple.getArch()) {
2307 default:
2308 return TargetTriple.str();
2309
2310 // We use the existence of '/lib/<triple>' as a directory to detect some
2311 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002312 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2313 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002314 case llvm::Triple::arm:
2315 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002316 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2317 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2318 return "arm-linux-gnueabihf";
2319 } else {
2320 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2321 return "arm-linux-gnueabi";
2322 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002323 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002324 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002325 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2326 return "i386-linux-gnu";
2327 return TargetTriple.str();
2328 case llvm::Triple::x86_64:
2329 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2330 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002331 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002332 case llvm::Triple::aarch64:
2333 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2334 return "aarch64-linux-gnu";
Tim Northoverfbb56b72013-06-13 22:54:55 +00002335 return TargetTriple.str();
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002336 case llvm::Triple::mips:
2337 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2338 return "mips-linux-gnu";
2339 return TargetTriple.str();
2340 case llvm::Triple::mipsel:
2341 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2342 return "mipsel-linux-gnu";
2343 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002344 case llvm::Triple::ppc:
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00002345 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2346 return "powerpc-linux-gnuspe";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002347 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2348 return "powerpc-linux-gnu";
2349 return TargetTriple.str();
2350 case llvm::Triple::ppc64:
2351 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2352 return "powerpc64-linux-gnu";
Bill Schmidt778d3872013-07-26 01:36:11 +00002353 case llvm::Triple::ppc64le:
2354 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
2355 return "powerpc64le-linux-gnu";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002356 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002357 }
2358}
2359
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002360static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2361 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2362}
2363
Simon Atanasyand4413882012-09-14 11:27:24 +00002364static StringRef getMultilibDir(const llvm::Triple &Triple,
2365 const ArgList &Args) {
Chandler Carruthda797042013-10-29 10:27:30 +00002366 if (isMipsArch(Triple.getArch())) {
2367 // lib32 directory has a special meaning on MIPS targets.
2368 // It contains N32 ABI binaries. Use this folder if produce
2369 // code for N32 ABI only.
2370 if (hasMipsN32ABIArg(Args))
2371 return "lib32";
2372 return Triple.isArch32Bit() ? "lib" : "lib64";
2373 }
Simon Atanasyand4413882012-09-14 11:27:24 +00002374
Chandler Carruthda797042013-10-29 10:27:30 +00002375 // It happens that only x86 and PPC use the 'lib32' variant of multilib, and
2376 // using that variant while targeting other architectures causes problems
2377 // because the libraries are laid out in shared system roots that can't cope
2378 // with a 'lib32' multilib search path being considered. So we only enable
2379 // them when we know we may need it.
2380 //
2381 // FIXME: This is a bit of a hack. We should really unify this code for
2382 // reasoning about multilib spellings with the lib dir spellings in the
2383 // GCCInstallationDetector, but that is a more significant refactoring.
2384 if (Triple.getArch() == llvm::Triple::x86 ||
2385 Triple.getArch() == llvm::Triple::ppc)
Simon Atanasyand4413882012-09-14 11:27:24 +00002386 return "lib32";
2387
2388 return Triple.isArch32Bit() ? "lib" : "lib64";
2389}
2390
Rafael Espindola1af7c212012-02-19 01:38:32 +00002391Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2392 : Generic_ELF(D, Triple, Args) {
Roman Divacky326d9982013-12-06 18:32:18 +00002393 GCCInstallation.init(D, Triple, Args);
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002394 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyana0d89572013-10-05 14:37:55 +00002395 std::string SysRoot = computeSysRoot();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002396
Rafael Espindola10a63c22013-07-03 14:14:00 +00002397 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
Chandler Carruthd6c62b62013-06-20 23:37:54 +00002398 // least) put various tools in a triple-prefixed directory off of the parent
2399 // of the GCC installation. We use the GCC triple here to ensure that we end
2400 // up with tools that support the same amount of cross compiling as the
2401 // detected GCC installation. For example, if we find a GCC installation
2402 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
2403 // used to target i386.
2404 // FIXME: This seems unlikely to be Linux-specific.
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002405 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002406 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002407 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002408
2409 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002410
Rafael Espindolaa8398552013-10-28 23:14:34 +00002411 Distro Distro = DetectDistro(Arch);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002412
Rafael Espindola10a63c22013-07-03 14:14:00 +00002413 if (IsOpenSUSE(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002414 ExtraOpts.push_back("-z");
2415 ExtraOpts.push_back("relro");
2416 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002417
Douglas Gregord9bb1522011-03-06 19:11:49 +00002418 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002419 ExtraOpts.push_back("-X");
2420
Logan Chienc6fd8202012-09-02 09:30:11 +00002421 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002422 const bool IsMips = isMipsArch(Arch);
2423
2424 if (IsMips && !SysRoot.empty())
2425 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002426
Chandler Carruth0b842912011-12-09 04:45:18 +00002427 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2428 // and the MIPS ABI require .dynsym to be sorted in different ways.
2429 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2430 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002431 // Android loader does not support .gnu.hash.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002432 if (!IsMips && !IsAndroid) {
Rafael Espindola10a63c22013-07-03 14:14:00 +00002433 if (IsRedhat(Distro) || IsOpenSUSE(Distro) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002434 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002435 ExtraOpts.push_back("--hash-style=gnu");
2436
Rafael Espindola10a63c22013-07-03 14:14:00 +00002437 if (IsDebian(Distro) || IsOpenSUSE(Distro) || Distro == UbuntuLucid ||
Chandler Carruth0b842912011-12-09 04:45:18 +00002438 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2439 ExtraOpts.push_back("--hash-style=both");
2440 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002441
Chris Lattner84e38552011-05-22 05:36:06 +00002442 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002443 ExtraOpts.push_back("--no-add-needed");
2444
Eli Friedmanf7600942011-06-02 21:36:53 +00002445 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola10a63c22013-07-03 14:14:00 +00002446 Distro == DebianJessie || IsOpenSUSE(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002447 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002448 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002449 ExtraOpts.push_back("--build-id");
2450
Rafael Espindola10a63c22013-07-03 14:14:00 +00002451 if (IsOpenSUSE(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002452 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002453
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002454 // The selection of paths to try here is designed to match the patterns which
2455 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2456 // This was determined by running GCC in a fake filesystem, creating all
2457 // possible permutations of these directories, and seeing which ones it added
2458 // to the link paths.
2459 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002460
Rafael Espindolaa8398552013-10-28 23:14:34 +00002461 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002462 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002463
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002464 // Add the multilib suffixed paths where they are available.
2465 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002466 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002467 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002468
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002469 // Sourcery CodeBench MIPS toolchain holds some libraries under
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002470 // a biarch-like suffix of the GCC installation.
2471 //
2472 // FIXME: It would be cleaner to model this as a variant of bi-arch. IE,
2473 // instead of a '64' biarch suffix it would be 'el' or something.
Simon Atanasyan068e0fd2013-10-09 12:12:39 +00002474 if (IsAndroid && IsMips && isMips32r2(Args)) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002475 assert(GCCInstallation.getBiarchSuffix().empty() &&
2476 "Unexpected bi-arch suffix");
2477 addPathIfExists(GCCInstallation.getInstallPath() + "/mips-r2", Paths);
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002478 } else {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002479 addPathIfExists((GCCInstallation.getInstallPath() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002480 GCCInstallation.getMIPSABIDirSuffix() +
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002481 GCCInstallation.getBiarchSuffix()),
2482 Paths);
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002483 }
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002484
2485 // GCC cross compiling toolchains will install target libraries which ship
2486 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
2487 // any part of the GCC installation in
2488 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
2489 // debatable, but is the reality today. We need to search this tree even
2490 // when we have a sysroot somewhere else. It is the responsibility of
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002491 // whomever is doing the cross build targeting a sysroot using a GCC
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002492 // installation that is *not* within the system root to ensure two things:
2493 //
2494 // 1) Any DSOs that are linked in from this tree or from the install path
2495 // above must be preasant on the system root and found via an
2496 // appropriate rpath.
2497 // 2) There must not be libraries installed into
2498 // <prefix>/<triple>/<libdir> unless they should be preferred over
2499 // those within the system root.
2500 //
2501 // Note that this matches the GCC behavior. See the below comment for where
2502 // Clang diverges from GCC's behavior.
2503 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib +
Chandler Carruth8677d922013-10-29 08:53:03 +00002504 GCCInstallation.getMIPSABIDirSuffix(),
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002505 Paths);
2506
Chandler Carruth69a125b2012-04-06 16:32:06 +00002507 // If the GCC installation we found is inside of the sysroot, we want to
2508 // prefer libraries installed in the parent prefix of the GCC installation.
2509 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002510 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002511 // This usually happens when there is an external cross compiler on the
2512 // host system, and a more minimal sysroot available that is the target of
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002513 // the cross. Note that GCC does include some of these directories in some
2514 // configurations but this seems somewhere between questionable and simply
2515 // a bug.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002516 if (StringRef(LibPath).startswith(SysRoot)) {
Chandler Carruth69a125b2012-04-06 16:32:06 +00002517 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2518 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2519 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002520 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002521 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2522 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2523 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2524 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2525
Chandler Carruthb427c562013-06-22 11:35:51 +00002526 // Try walking via the GCC triple path in case of biarch or multiarch GCC
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002527 // installations with strange symlinks.
Rafael Espindolab6250162013-10-25 17:06:04 +00002528 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002529 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002530 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002531
Rafael Espindolab6250162013-10-25 17:06:04 +00002532 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002533 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002534 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Rafael Espindolaa8398552013-10-28 23:14:34 +00002535 if (!GCCInstallation.getBiarchSuffix().empty())
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002536 addPathIfExists(GCCInstallation.getInstallPath() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002537 GCCInstallation.getMIPSABIDirSuffix(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002538
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002539 // See comments above on the multilib variant for details of why this is
2540 // included even from outside the sysroot.
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002541 addPathIfExists(LibPath + "/../" + GCCTriple.str() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002542 "/lib" + GCCInstallation.getMIPSABIDirSuffix(), Paths);
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002543
2544 // See comments above on the multilib variant for details of why this is
2545 // only included from within the sysroot.
2546 if (StringRef(LibPath).startswith(SysRoot))
Chandler Carruth69a125b2012-04-06 16:32:06 +00002547 addPathIfExists(LibPath, Paths);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002548 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002549 addPathIfExists(SysRoot + "/lib", Paths);
2550 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002551}
2552
Roman Divackyf0d7f942013-11-10 09:31:43 +00002553bool FreeBSD::HasNativeLLVMSupport() const {
2554 return true;
2555}
2556
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002557bool Linux::HasNativeLLVMSupport() const {
2558 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002559}
2560
Rafael Espindola7cf32212013-03-20 03:05:54 +00002561Tool *Linux::buildLinker() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002562 return new tools::gnutools::Link(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00002563}
2564
2565Tool *Linux::buildAssembler() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002566 return new tools::gnutools::Assemble(*this);
Rafael Espindola92b00932010-08-10 00:25:48 +00002567}
2568
Chandler Carruth05fb5852012-11-21 23:40:23 +00002569void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2570 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002571 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Benjamin Kramer604e8482013-08-09 17:17:48 +00002572 bool UseInitArrayDefault =
2573 !V.isOlderThan(4, 7, 0) ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002574 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002575 getTriple().getEnvironment() == llvm::Triple::Android;
2576 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2577 options::OPT_fno_use_init_array,
2578 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002579 CC1Args.push_back("-fuse-init-array");
2580}
2581
Simon Atanasyana0d89572013-10-05 14:37:55 +00002582std::string Linux::computeSysRoot() const {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002583 if (!getDriver().SysRoot.empty())
2584 return getDriver().SysRoot;
2585
2586 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
2587 return std::string();
2588
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002589 // Standalone MIPS toolchains use different names for sysroot folder
2590 // and put it into different places. Here we try to check some known
2591 // variants.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002592
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002593 const StringRef InstallDir = GCCInstallation.getInstallPath();
2594 const StringRef TripleStr = GCCInstallation.getTriple().str();
Chandler Carruth8677d922013-10-29 08:53:03 +00002595 const StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002596
Chandler Carruth8677d922013-10-29 08:53:03 +00002597 std::string Path = (InstallDir + "/../../../../" + TripleStr + "/libc" +
2598 MIPSABIDirSuffix).str();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002599
2600 if (llvm::sys::fs::exists(Path))
2601 return Path;
2602
Chandler Carruth8677d922013-10-29 08:53:03 +00002603 Path = (InstallDir + "/../../../../sysroot" + MIPSABIDirSuffix).str();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002604
2605 if (llvm::sys::fs::exists(Path))
2606 return Path;
2607
2608 return std::string();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002609}
2610
Chandler Carrutha796f532011-11-05 20:17:13 +00002611void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2612 ArgStringList &CC1Args) const {
2613 const Driver &D = getDriver();
Simon Atanasyana0d89572013-10-05 14:37:55 +00002614 std::string SysRoot = computeSysRoot();
Chandler Carrutha796f532011-11-05 20:17:13 +00002615
2616 if (DriverArgs.hasArg(options::OPT_nostdinc))
2617 return;
2618
2619 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002620 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002621
2622 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola358256c2013-06-26 02:13:00 +00002623 SmallString<128> P(D.ResourceDir);
2624 llvm::sys::path::append(P, "include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002625 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002626 }
2627
2628 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2629 return;
2630
2631 // Check for configure-time C include directories.
2632 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2633 if (CIncludeDirs != "") {
2634 SmallVector<StringRef, 5> dirs;
2635 CIncludeDirs.split(dirs, ":");
2636 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2637 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002638 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? SysRoot : "";
Chandler Carrutha796f532011-11-05 20:17:13 +00002639 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2640 }
2641 return;
2642 }
2643
2644 // Lacking those, try to detect the correct set of system includes for the
2645 // target triple.
2646
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002647 // Sourcery CodeBench and modern FSF Mips toolchains put extern C
2648 // system includes under three additional directories.
2649 if (GCCInstallation.isValid() && isMipsArch(getTriple().getArch())) {
Chandler Carruthb427c562013-06-22 11:35:51 +00002650 addExternCSystemIncludeIfExists(
2651 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + "/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002652
Chandler Carruthb427c562013-06-22 11:35:51 +00002653 addExternCSystemIncludeIfExists(
2654 DriverArgs, CC1Args,
2655 GCCInstallation.getInstallPath() + "/../../../../" +
2656 GCCInstallation.getTriple().str() + "/libc/usr/include");
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002657
2658 addExternCSystemIncludeIfExists(
2659 DriverArgs, CC1Args,
2660 GCCInstallation.getInstallPath() + "/../../../../sysroot/usr/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002661 }
2662
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002663 // Implement generic Debian multiarch support.
2664 const StringRef X86_64MultiarchIncludeDirs[] = {
2665 "/usr/include/x86_64-linux-gnu",
2666
2667 // FIXME: These are older forms of multiarch. It's not clear that they're
2668 // in use in any released version of Debian, so we should consider
2669 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002670 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002671 };
2672 const StringRef X86MultiarchIncludeDirs[] = {
2673 "/usr/include/i386-linux-gnu",
2674
2675 // FIXME: These are older forms of multiarch. It's not clear that they're
2676 // in use in any released version of Debian, so we should consider
2677 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002678 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002679 "/usr/include/i486-linux-gnu"
2680 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002681 const StringRef AArch64MultiarchIncludeDirs[] = {
2682 "/usr/include/aarch64-linux-gnu"
2683 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002684 const StringRef ARMMultiarchIncludeDirs[] = {
2685 "/usr/include/arm-linux-gnueabi"
2686 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002687 const StringRef ARMHFMultiarchIncludeDirs[] = {
2688 "/usr/include/arm-linux-gnueabihf"
2689 };
Eli Friedman7771c832011-11-11 03:05:19 +00002690 const StringRef MIPSMultiarchIncludeDirs[] = {
2691 "/usr/include/mips-linux-gnu"
2692 };
2693 const StringRef MIPSELMultiarchIncludeDirs[] = {
2694 "/usr/include/mipsel-linux-gnu"
2695 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002696 const StringRef PPCMultiarchIncludeDirs[] = {
2697 "/usr/include/powerpc-linux-gnu"
2698 };
2699 const StringRef PPC64MultiarchIncludeDirs[] = {
2700 "/usr/include/powerpc64-linux-gnu"
2701 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002702 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002703 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002704 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002705 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002706 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002707 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2708 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002709 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002710 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2711 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2712 else
2713 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002714 } else if (getTriple().getArch() == llvm::Triple::mips) {
2715 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2716 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2717 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002718 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2719 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2720 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2721 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002722 }
2723 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2724 E = MultiarchIncludeDirs.end();
2725 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002726 if (llvm::sys::fs::exists(SysRoot + *I)) {
2727 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + *I);
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002728 break;
2729 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002730 }
2731
2732 if (getTriple().getOS() == llvm::Triple::RTEMS)
2733 return;
2734
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002735 // Add an include of '/include' directly. This isn't provided by default by
2736 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2737 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002738 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002739
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002740 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002741}
2742
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002743/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002744/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2745 const ArgList &DriverArgs,
2746 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002747 if (!llvm::sys::fs::exists(Base))
2748 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002749 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002750 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002751 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002752 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002753}
2754
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002755/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2756/// libstdc++ installation.
2757/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2758 Twine TargetArchDir,
Chandler Carruth8677d922013-10-29 08:53:03 +00002759 Twine BiarchSuffix,
2760 Twine MIPSABIDirSuffix,
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002761 const ArgList &DriverArgs,
2762 ArgStringList &CC1Args) {
Chandler Carruth8677d922013-10-29 08:53:03 +00002763 if (!addLibStdCXXIncludePaths(Base + Suffix,
2764 TargetArchDir + MIPSABIDirSuffix + BiarchSuffix,
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002765 DriverArgs, CC1Args))
2766 return false;
2767
2768 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
Chandler Carruth8677d922013-10-29 08:53:03 +00002769 + MIPSABIDirSuffix + BiarchSuffix);
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002770 return true;
2771}
2772
Chandler Carrutha796f532011-11-05 20:17:13 +00002773void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2774 ArgStringList &CC1Args) const {
2775 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2776 DriverArgs.hasArg(options::OPT_nostdincxx))
2777 return;
2778
Chandler Carruthf4701732011-11-07 09:01:17 +00002779 // Check if libc++ has been enabled and provide its include paths if so.
2780 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2781 // libc++ is always installed at a fixed path on Linux currently.
2782 addSystemInclude(DriverArgs, CC1Args,
2783 getDriver().SysRoot + "/usr/include/c++/v1");
2784 return;
2785 }
2786
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002787 // We need a detected GCC installation on Linux to provide libstdc++'s
2788 // headers. We handled the libc++ case above.
2789 if (!GCCInstallation.isValid())
2790 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002791
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002792 // By default, look for the C++ headers in an include directory adjacent to
2793 // the lib directory of the GCC installation. Note that this is expect to be
2794 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2795 StringRef LibDir = GCCInstallation.getParentLibPath();
2796 StringRef InstallDir = GCCInstallation.getInstallPath();
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002797 StringRef TripleStr = GCCInstallation.getTriple().str();
Chandler Carruth8677d922013-10-29 08:53:03 +00002798 StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix();
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002799 StringRef BiarchSuffix = GCCInstallation.getBiarchSuffix();
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002800 const GCCVersion &Version = GCCInstallation.getVersion();
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002801
Chandler Carruth8677d922013-10-29 08:53:03 +00002802 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2803 "/c++/" + Version.Text, TripleStr, BiarchSuffix,
2804 MIPSABIDirSuffix, DriverArgs, CC1Args))
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002805 return;
2806
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002807 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002808 // Gentoo is weird and places its headers inside the GCC install, so if the
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002809 // first attempt to find the headers fails, try these patterns.
2810 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
2811 Version.MinorStr,
2812 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002813 // Android standalone toolchain has C++ headers in yet another place.
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002814 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
Hal Finkelf3587912012-09-18 22:25:07 +00002815 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2816 // without a subdirectory corresponding to the gcc version.
2817 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002818 };
2819
2820 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
Chandler Carruth8677d922013-10-29 08:53:03 +00002821 if (addLibStdCXXIncludePaths(IncludePathCandidates[i],
2822 TripleStr + MIPSABIDirSuffix + BiarchSuffix,
2823 DriverArgs, CC1Args))
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002824 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002825 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002826}
2827
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002828bool Linux::isPIEDefault() const {
Peter Collingbourne32701642013-11-01 18:16:25 +00002829 return getSanitizerArgs().hasZeroBaseShadow();
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002830}
2831
Daniel Dunbarcc912342009-05-02 18:28:39 +00002832/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2833
Rafael Espindola1af7c212012-02-19 01:38:32 +00002834DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2835 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002836
2837 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002838 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002839 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002840 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002841
Daniel Dunbar083edf72009-12-21 18:54:17 +00002842 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002843 getFilePaths().push_back("/usr/lib");
John McCall65b8da02013-04-11 22:55:55 +00002844 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
2845 getFilePaths().push_back("/usr/lib/gcc47");
2846 else
2847 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002848}
2849
Rafael Espindola7cf32212013-03-20 03:05:54 +00002850Tool *DragonFly::buildAssembler() const {
2851 return new tools::dragonfly::Assemble(*this);
2852}
2853
2854Tool *DragonFly::buildLinker() const {
2855 return new tools::dragonfly::Link(*this);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002856}
Robert Lyttoncf1dd692013-10-11 10:29:40 +00002857
2858
2859/// XCore tool chain
2860XCore::XCore(const Driver &D, const llvm::Triple &Triple,
2861 const ArgList &Args) : ToolChain(D, Triple, Args) {
2862 // ProgramPaths are found via 'PATH' environment variable.
2863}
2864
2865Tool *XCore::buildAssembler() const {
2866 return new tools::XCore::Assemble(*this);
2867}
2868
2869Tool *XCore::buildLinker() const {
2870 return new tools::XCore::Link(*this);
2871}
2872
2873bool XCore::isPICDefault() const {
2874 return false;
2875}
2876
2877bool XCore::isPIEDefault() const {
2878 return false;
2879}
2880
2881bool XCore::isPICDefaultForced() const {
2882 return false;
2883}
2884
2885bool XCore::SupportsProfiling() const {
2886 return false;
2887}
2888
2889bool XCore::hasBlocksRuntime() const {
2890 return false;
2891}
2892
2893
2894void XCore::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2895 ArgStringList &CC1Args) const {
2896 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2897 DriverArgs.hasArg(options::OPT_nostdlibinc))
2898 return;
2899 if (const char *cl_include_dir = getenv("XCC_C_INCLUDE_PATH")) {
2900 SmallVector<StringRef, 4> Dirs;
2901 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
2902 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
2903 ArrayRef<StringRef> DirVec(Dirs);
2904 addSystemIncludes(DriverArgs, CC1Args, DirVec);
2905 }
2906}
2907
2908void XCore::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
2909 llvm::opt::ArgStringList &CC1Args) const {
2910 CC1Args.push_back("-nostdsysteminc");
2911}
2912
2913void XCore::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2914 ArgStringList &CC1Args) const {
2915 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2916 DriverArgs.hasArg(options::OPT_nostdlibinc))
2917 return;
2918 if (const char *cl_include_dir = getenv("XCC_CPLUS_INCLUDE_PATH")) {
2919 SmallVector<StringRef, 4> Dirs;
2920 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
2921 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
2922 ArrayRef<StringRef> DirVec(Dirs);
2923 addSystemIncludes(DriverArgs, CC1Args, DirVec);
2924 }
2925}
2926
2927void XCore::AddCXXStdlibLibArgs(const ArgList &Args,
2928 ArgStringList &CmdArgs) const {
2929 // We don't output any lib args. This is handled by xcc.
2930}