blob: f7404e97f79a1b570ad8d10f7b8908341f6c47c4 [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) ||
Justin Bogner6a9d2cf2014-01-06 22:27:36 +0000325 Args.hasArg(options::OPT_fprofile_instr_generate) ||
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000326 Args.hasArg(options::OPT_fcreate_profile) ||
327 Args.hasArg(options::OPT_coverage)) {
328 // Select the appropriate runtime library for the target.
Tim Northover9c7e0352013-12-12 11:55:52 +0000329 if (isTargetIOSBased()) {
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000330 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
331 } else {
332 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
333 }
334 }
335
Peter Collingbourne32701642013-11-01 18:16:25 +0000336 const SanitizerArgs &Sanitize = getSanitizerArgs();
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000337
Alexey Samsonovcc429802012-11-16 12:53:14 +0000338 // Add Ubsan runtime library, if required.
339 if (Sanitize.needsUbsanRt()) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000340 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
Tim Northover9c7e0352013-12-12 11:55:52 +0000341 if (isTargetIOSBased()) {
Alexey Samsonovcc429802012-11-16 12:53:14 +0000342 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
343 << "-fsanitize=undefined";
344 } else {
Tim Northover9c7e0352013-12-12 11:55:52 +0000345 assert(isTargetMacOS() && "unexpected non OS X target");
Alexey Samsonov8368b372012-11-21 14:17:42 +0000346 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonovcc429802012-11-16 12:53:14 +0000347
348 // The Ubsan runtime library requires C++.
349 AddCXXStdlibLibArgs(Args, CmdArgs);
350 }
351 }
352
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000353 // Add ASAN runtime library, if required. Dynamic libraries and bundles
354 // should not be linked with the runtime library.
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000355 if (Sanitize.needsAsanRt()) {
Peter Collingbourne32701642013-11-01 18:16:25 +0000356 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
Tim Northover9c7e0352013-12-12 11:55:52 +0000357 if (isTargetIPhoneOS()) {
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000358 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000359 << "-fsanitize=address";
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000360 } else {
Alexander Potapenkoce876332013-09-20 08:09:51 +0000361 if (!Args.hasArg(options::OPT_dynamiclib) &&
362 !Args.hasArg(options::OPT_bundle)) {
Alexander Potapenko5700f402013-03-21 10:49:06 +0000363 // The ASAN runtime library requires C++.
364 AddCXXStdlibLibArgs(Args, CmdArgs);
365 }
Alexander Potapenko868cca92013-11-15 16:07:44 +0000366 if (isTargetMacOS()) {
367 AddLinkRuntimeLib(Args, CmdArgs,
368 "libclang_rt.asan_osx_dynamic.dylib",
369 true);
370 } else {
371 if (isTargetIOSSimulator()) {
372 AddLinkRuntimeLib(Args, CmdArgs,
373 "libclang_rt.asan_iossim_dynamic.dylib",
374 true);
375 }
376 }
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000377 }
378 }
379
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000380 // Otherwise link libSystem, then the dynamic runtime library, and finally any
381 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000382 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000383
384 // Select the dynamic runtime library and the target specific static library.
Tim Northover9c7e0352013-12-12 11:55:52 +0000385 if (isTargetIOSBased()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000386 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
387 // it never went into the SDK.
Bob Wilson102be442011-10-07 17:54:41 +0000388 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
389 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
390 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000391
Daniel Dunbard1076382011-04-18 23:48:36 +0000392 // We currently always need a static runtime library for iOS.
Eric Christopherc235d0c62011-06-22 17:41:40 +0000393 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000394 } else {
Tim Northover9c7e0352013-12-12 11:55:52 +0000395 assert(isTargetMacOS() && "unexpected non MacOS platform");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000396 // The dynamic runtime library was merged with libSystem for 10.6 and
397 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000398 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000399 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000400 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000401 CmdArgs.push_back("-lgcc_s.10.5");
402
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000403 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000404 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000405 // omitted from 10.4.dylib.
406 //
407 // Unfortunately, that turned out to not be true, because Darwin system
408 // headers can still use eprintf on i386, and it is not exported from
409 // libSystem. Therefore, we still must provide a runtime library just for
410 // the tiny tiny handful of projects that *might* use that symbol.
411 if (isMacosxVersionLT(10, 5)) {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000412 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000413 } else {
414 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopherc235d0c62011-06-22 17:41:40 +0000415 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
416 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000417 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000418 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000419}
420
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000421void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000422 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000423
Daniel Dunbar455a0492012-08-17 18:43:50 +0000424 // Support allowing the SDKROOT environment variable used by xcrun and other
425 // Xcode tools to define the default sysroot, by making it the default for
426 // isysroot.
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000427 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
428 // Warn if the path does not exist.
Rafael Espindola355670d2013-06-25 15:14:22 +0000429 if (!llvm::sys::fs::exists(A->getValue()))
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000430 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
431 } else {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000432 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbarb2543042013-01-15 20:33:56 +0000433 // We only use this value as the default if it is an absolute path,
434 // exists, and it is not the root path.
435 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
436 StringRef(env) != "/") {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000437 Args.append(Args.MakeSeparateArg(
438 0, Opts.getOption(options::OPT_isysroot), env));
439 }
440 }
441 }
442
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000443 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000444 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
445 Arg *iOSSimVersion = Args.getLastArg(
446 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman027e9c32012-01-11 02:41:15 +0000447
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000448 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000449 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000450 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000451 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
452 iOSVersion = iOSSimVersion = 0;
453 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000454 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000455 << iOSVersion->getAsString(Args)
456 << iOSSimVersion->getAsString(Args);
457 iOSSimVersion = 0;
458 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosier64707fe2011-08-31 20:56:25 +0000459 // If no deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000460 // environment defines.
Chad Rosier64707fe2011-08-31 20:56:25 +0000461 StringRef OSXTarget;
462 StringRef iOSTarget;
463 StringRef iOSSimTarget;
464 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
465 OSXTarget = env;
466 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
467 iOSTarget = env;
468 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
469 iOSSimTarget = env;
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000470
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000471 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosier64707fe2011-08-31 20:56:25 +0000472 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif5d3231c2012-04-18 10:59:08 +0000473 // based on -isysroot.
Chad Rosier64707fe2011-08-31 20:56:25 +0000474 if (iOSTarget.empty()) {
475 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
476 StringRef first, second;
Richard Smithbd55daf2012-11-01 04:30:05 +0000477 StringRef isysroot = A->getValue();
Chad Rosier64707fe2011-08-31 20:56:25 +0000478 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
479 if (second != "")
480 iOSTarget = second.substr(0,3);
481 }
482 }
Daniel Dunbard54669d2010-01-26 01:45:19 +0000483
Chad Rosierfe6fd362011-09-28 00:46:32 +0000484 // If no OSX or iOS target has been specified and we're compiling for armv7,
485 // go ahead as assume we're targeting iOS.
Tim Northover9c7e0352013-12-12 11:55:52 +0000486 StringRef DarwinArchName = getDarwinArchName(Args);
Chad Rosier7b1fee12012-05-09 18:55:57 +0000487 if (OSXTarget.empty() && iOSTarget.empty() &&
Tim Northover9c7e0352013-12-12 11:55:52 +0000488 (DarwinArchName == "armv7" || DarwinArchName == "armv7s"))
Chad Rosierf761fe92012-05-09 18:09:58 +0000489 iOSTarget = iOSVersionMin;
Chad Rosierfe6fd362011-09-28 00:46:32 +0000490
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000491 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000492 //
493 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000494
495 // Do not allow conflicts with the iOS simulator target.
Chad Rosier64707fe2011-08-31 20:56:25 +0000496 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000497 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000498 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosier64707fe2011-08-31 20:56:25 +0000499 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000500 "IPHONEOS_DEPLOYMENT_TARGET");
501 }
502
503 // Allow conflicts among OSX and iOS for historical reasons, but choose the
504 // default platform.
Chad Rosier64707fe2011-08-31 20:56:25 +0000505 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000506 if (getTriple().getArch() == llvm::Triple::arm ||
507 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosier64707fe2011-08-31 20:56:25 +0000508 OSXTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000509 else
Chad Rosier64707fe2011-08-31 20:56:25 +0000510 iOSTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000511 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000512
Chad Rosier64707fe2011-08-31 20:56:25 +0000513 if (!OSXTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000514 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000515 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
516 Args.append(OSXVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000517 } else if (!iOSTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000518 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000519 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
520 Args.append(iOSVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000521 } else if (!iOSSimTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000522 const Option O = Opts.getOption(
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000523 options::OPT_mios_simulator_version_min_EQ);
524 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
525 Args.append(iOSSimVersion);
Tim Northover9c7e0352013-12-12 11:55:52 +0000526 } else if (DarwinArchName != "armv6m" && DarwinArchName != "armv7m" &&
527 DarwinArchName != "armv7em") {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000528 // Otherwise, assume we are targeting OS X.
Michael J. Spencerfc790902012-10-19 22:36:40 +0000529 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000530 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
531 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000532 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000533 }
Mike Stump11289f42009-09-09 15:08:12 +0000534
Tim Northover9c7e0352013-12-12 11:55:52 +0000535 DarwinPlatformKind Platform;
536 if (OSXVersion)
537 Platform = MacOS;
538 else if (iOSVersion)
539 Platform = IPhoneOS;
540 else if (iOSSimVersion)
541 Platform = IPhoneOSSimulator;
542 else
543 Platform = Embedded;
544
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000545 // Reject invalid architecture combinations.
546 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
547 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000548 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000549 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
550 }
551
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000552 // Set the tool chain target information.
553 unsigned Major, Minor, Micro;
554 bool HadExtra;
Tim Northover9c7e0352013-12-12 11:55:52 +0000555 if (Platform == MacOS) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000556 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000557 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000558 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000559 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000560 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000561 << OSXVersion->getAsString(Args);
Tim Northover9c7e0352013-12-12 11:55:52 +0000562 } else if (Platform == IPhoneOS || Platform == IPhoneOSSimulator) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000563 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
564 assert(Version && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000565 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman027e9c32012-01-11 02:41:15 +0000566 Micro, HadExtra) || HadExtra ||
567 Major >= 10 || Minor >= 100 || Micro >= 100)
568 getDriver().Diag(diag::err_drv_invalid_version_number)
569 << Version->getAsString(Args);
Tim Northover9c7e0352013-12-12 11:55:52 +0000570 } else {
571 assert(Platform == Embedded && "unexpected platform");
572 Major = Minor = Micro = 0;
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000573 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000574
Daniel Dunbarb1189432011-04-30 04:18:16 +0000575 // In GCC, the simulator historically was treated as being OS X in some
576 // contexts, like determining the link logic, despite generally being called
577 // with an iOS deployment target. For compatibility, we detect the
578 // simulator as iOS + x86, and treat it differently in a few contexts.
579 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
580 getTriple().getArch() == llvm::Triple::x86_64))
Tim Northover9c7e0352013-12-12 11:55:52 +0000581 Platform = IPhoneOSSimulator;
Daniel Dunbarb1189432011-04-30 04:18:16 +0000582
Tim Northover9c7e0352013-12-12 11:55:52 +0000583 setTarget(Platform, Major, Minor, Micro);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000584}
585
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000586void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000587 ArgStringList &CmdArgs) const {
588 CXXStdlibType Type = GetCXXStdlibType(Args);
589
590 switch (Type) {
591 case ToolChain::CST_Libcxx:
592 CmdArgs.push_back("-lc++");
593 break;
594
595 case ToolChain::CST_Libstdcxx: {
596 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
597 // it was previously found in the gcc lib dir. However, for all the Darwin
598 // platforms we care about it was -lstdc++.6, so we search for that
599 // explicitly if we can't see an obvious -lstdc++ candidate.
600
601 // Check in the sysroot first.
602 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000603 SmallString<128> P(A->getValue());
Benjamin Kramer17381a02013-06-28 16:25:46 +0000604 llvm::sys::path::append(P, "usr", "lib", "libstdc++.dylib");
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000605
Rafael Espindola355670d2013-06-25 15:14:22 +0000606 if (!llvm::sys::fs::exists(P.str())) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000607 llvm::sys::path::remove_filename(P);
608 llvm::sys::path::append(P, "libstdc++.6.dylib");
Rafael Espindola355670d2013-06-25 15:14:22 +0000609 if (llvm::sys::fs::exists(P.str())) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000610 CmdArgs.push_back(Args.MakeArgString(P.str()));
611 return;
612 }
613 }
614 }
615
616 // Otherwise, look in the root.
Bob Wilson1a9ad0f2011-11-11 07:47:04 +0000617 // FIXME: This should be removed someday when we don't have to care about
618 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Rafael Espindola355670d2013-06-25 15:14:22 +0000619 if (!llvm::sys::fs::exists("/usr/lib/libstdc++.dylib") &&
620 llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib")) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000621 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
622 return;
623 }
624
625 // Otherwise, let the linker search.
626 CmdArgs.push_back("-lstdc++");
627 break;
628 }
629 }
630}
631
Shantonu Senafeb03b2010-09-17 18:39:08 +0000632void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
633 ArgStringList &CmdArgs) const {
634
635 // For Darwin platforms, use the compiler-rt-based support library
636 // instead of the gcc-provided one (which is also incidentally
637 // only present in the gcc lib dir, which makes it hard to find).
638
Rafael Espindola358256c2013-06-26 02:13:00 +0000639 SmallString<128> P(getDriver().ResourceDir);
Benjamin Kramer17381a02013-06-28 16:25:46 +0000640 llvm::sys::path::append(P, "lib", "darwin");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000641
642 // Use the newer cc_kext for iOS ARM after 6.0.
643 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
644 !isIPhoneOSVersionLT(6, 0)) {
Rafael Espindola358256c2013-06-26 02:13:00 +0000645 llvm::sys::path::append(P, "libclang_rt.cc_kext.a");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000646 } else {
Rafael Espindola358256c2013-06-26 02:13:00 +0000647 llvm::sys::path::append(P, "libclang_rt.cc_kext_ios5.a");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000648 }
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000649
Shantonu Senafeb03b2010-09-17 18:39:08 +0000650 // For now, allow missing resource libraries to support developers who may
651 // not have compiler-rt checked out or integrated into their build.
Rafael Espindola355670d2013-06-25 15:14:22 +0000652 if (llvm::sys::fs::exists(P.str()))
Shantonu Senafeb03b2010-09-17 18:39:08 +0000653 CmdArgs.push_back(Args.MakeArgString(P.str()));
654}
655
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000656DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
657 const char *BoundArch) const {
658 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
659 const OptTable &Opts = getDriver().getOpts();
660
661 // FIXME: We really want to get out of the tool chain level argument
662 // translation business, as it makes the driver functionality much
663 // more opaque. For now, we follow gcc closely solely for the
664 // purpose of easily achieving feature parity & testability. Once we
665 // have something that works, we should reevaluate each translation
666 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000667
Daniel Dunbar775d4062010-06-11 22:00:26 +0000668 for (ArgList::const_iterator it = Args.begin(),
669 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000670 Arg *A = *it;
671
672 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar471c4f82011-06-21 00:20:17 +0000673 // Skip this argument unless the architecture matches either the toolchain
674 // triple arch, or the arch being bound.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000675 llvm::Triple::ArchType XarchArch =
Richard Smithbd55daf2012-11-01 04:30:05 +0000676 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000677 if (!(XarchArch == getArch() ||
678 (BoundArch && XarchArch ==
Rafael Espindoladcbf6982012-10-31 18:51:07 +0000679 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000680 continue;
681
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000682 Arg *OriginalArg = A;
Richard Smithbd55daf2012-11-01 04:30:05 +0000683 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000684 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000685 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000686
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000687 // If the argument parsing failed or more than one argument was
688 // consumed, the -Xarch_ argument's parameter tried to consume
689 // extra arguments. Emit an error and ignore.
690 //
691 // We also want to disallow any options which would alter the
692 // driver behavior; that isn't going to work in our model. We
693 // use isDriverOption() as an approximation, although things
694 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000695 if (!XarchArg || Index > Prev + 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000696 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar6914a982011-04-21 17:32:21 +0000697 << A->getAsString(Args);
698 continue;
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000699 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000700 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000701 << A->getAsString(Args);
702 continue;
703 }
704
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000705 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000706 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000707
708 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000709
710 // Linker input arguments require custom handling. The problem is that we
711 // have already constructed the phase actions, so we can not treat them as
712 // "input arguments".
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000713 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000714 // Convert the argument into individual Zlinker_input_args.
715 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
716 DAL->AddSeparateArg(OriginalArg,
717 Opts.getOption(options::OPT_Zlinker_input),
Richard Smithbd55daf2012-11-01 04:30:05 +0000718 A->getValue(i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000719
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000720 }
721 continue;
722 }
Mike Stump11289f42009-09-09 15:08:12 +0000723 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000724
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000725 // Sob. These is strictly gcc compatible for the time being. Apple
726 // gcc translates options twice, which means that self-expanding
727 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000728 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000729 default:
730 DAL->append(A);
731 break;
732
733 case options::OPT_mkernel:
734 case options::OPT_fapple_kext:
735 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000736 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000737 break;
Mike Stump11289f42009-09-09 15:08:12 +0000738
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000739 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000740 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smithbd55daf2012-11-01 04:30:05 +0000741 A->getValue());
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000742 break;
743
744 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000745 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
746 DAL->AddFlagArg(A,
747 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000748 break;
749
750 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000751 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
752 DAL->AddFlagArg(A,
753 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000754 break;
755
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000756 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000757 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000758 break;
759
760 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000761 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000762 break;
763
764 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000765 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000766 break;
767
768 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000769 DAL->AddFlagArg(A,
770 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000771 break;
772
773 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000774 DAL->AddFlagArg(A,
775 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000776 break;
777
778 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000779 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000780 break;
781
782 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000783 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000784 break;
785 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000786 }
787
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000788 if (getTriple().getArch() == llvm::Triple::x86 ||
789 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000790 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000791 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000792
793 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosier7c5d9082012-04-27 14:58:16 +0000794 // how the driver driver works.
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000795 if (BoundArch) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000796 StringRef Name = BoundArch;
Michael J. Spencerfc790902012-10-19 22:36:40 +0000797 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
798 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000799
800 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
801 // which defines the list of which architectures we accept.
802 if (Name == "ppc")
803 ;
804 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000805 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000806 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000807 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000808 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000809 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000810 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000811 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000812 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000813 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000814 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000815 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000816 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000817 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000818 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000819 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000820
Bill Schmidt778d3872013-07-26 01:36:11 +0000821 else if (Name == "ppc64" || Name == "ppc64le")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000822 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000823
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000824 else if (Name == "i386")
825 ;
826 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000827 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000828 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000829 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000830 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000831 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000832 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000833 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000834 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000835 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000836 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000837 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000838 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000839 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000840
841 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000842 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Jim Grosbach82eee262013-11-16 00:53:35 +0000843 else if (Name == "x86_64h") {
844 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
845 DAL->AddJoinedArg(0, MArch, "x86_64h");
846 }
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000847
848 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000849 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000850 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000851 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000852 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000853 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000854 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000855 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000856 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000857 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000858 else if (Name == "armv6m")
859 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000860 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000861 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000862 else if (Name == "armv7em")
863 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000864 else if (Name == "armv7f")
865 DAL->AddJoinedArg(0, MArch, "armv7f");
866 else if (Name == "armv7k")
867 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000868 else if (Name == "armv7m")
869 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000870 else if (Name == "armv7s")
871 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000872
873 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000874 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000875 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000876
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000877 // Add an explicit version min argument for the deployment target. We do this
878 // after argument translation because -Xarch_ arguments may add a version min
879 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000880 if (BoundArch)
881 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000882
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000883 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
884 // FIXME: It would be far better to avoid inserting those -static arguments,
885 // but we can't check the deployment target in the translation code until
886 // it is set here.
Tim Northover9c7e0352013-12-12 11:55:52 +0000887 if (isTargetIOSBased() && !isIPhoneOSVersionLT(6, 0)) {
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000888 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
889 Arg *A = *it;
890 ++it;
891 if (A->getOption().getID() != options::OPT_mkernel &&
892 A->getOption().getID() != options::OPT_fapple_kext)
893 continue;
894 assert(it != ie && "unexpected argument translation");
895 A = *it;
896 assert(A->getOption().getID() == options::OPT_static &&
897 "missing expected -static argument");
898 it = DAL->getArgs().erase(it);
899 }
900 }
901
Bob Wilson0f7445b2013-11-02 23:19:53 +0000902 // Default to use libc++ on OS X 10.9+ and iOS 7+.
903 if (((isTargetMacOS() && !isMacosxVersionLT(10, 9)) ||
Tim Northover9c7e0352013-12-12 11:55:52 +0000904 (isTargetIOSBased() && !isIPhoneOSVersionLT(7, 0))) &&
Bob Wilson0f7445b2013-11-02 23:19:53 +0000905 !Args.getLastArg(options::OPT_stdlib_EQ))
906 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_stdlib_EQ), "libc++");
907
Bob Wilson102be442011-10-07 17:54:41 +0000908 // Validate the C++ standard library choice.
909 CXXStdlibType Type = GetCXXStdlibType(*DAL);
910 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000911 // Check whether the target provides libc++.
912 StringRef where;
913
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000914 // Complain about targeting iOS < 5.0 in any way.
Tim Northover9c7e0352013-12-12 11:55:52 +0000915 if (isTargetIOSBased() && isIPhoneOSVersionLT(5, 0))
Bob Wilson5ad5a952012-11-09 01:59:30 +0000916 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000917
918 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000919 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000920 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000921 }
922 }
923
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000924 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000925}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000926
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000927bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000928 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000929}
930
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000931bool Darwin::UseDwarfDebugFlags() const {
932 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
933 return S[0] != '\0';
934 return false;
935}
936
Daniel Dunbar3241d402010-02-10 18:49:11 +0000937bool Darwin::UseSjLjExceptions() const {
938 // Darwin uses SjLj exceptions on ARM.
939 return (getTriple().getArch() == llvm::Triple::arm ||
940 getTriple().getArch() == llvm::Triple::thumb);
941}
942
Chandler Carruth76a943b2012-11-19 03:52:03 +0000943bool Darwin::isPICDefault() const {
944 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000945}
946
Peter Collingbourne54d770c2013-04-09 04:35:11 +0000947bool Darwin::isPIEDefault() const {
948 return false;
949}
950
Chandler Carruth76a943b2012-11-19 03:52:03 +0000951bool Darwin::isPICDefaultForced() const {
952 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000953}
954
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000955bool Darwin::SupportsProfiling() const {
956 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000957 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000958}
959
Daniel Dunbar16334e12010-04-10 16:20:23 +0000960bool Darwin::SupportsObjCGC() const {
Tim Northover9c7e0352013-12-12 11:55:52 +0000961 return isTargetMacOS();
Daniel Dunbar16334e12010-04-10 16:20:23 +0000962}
963
John McCall3deb1ad2012-08-21 02:47:43 +0000964void Darwin::CheckObjCARC() const {
Tim Northover9c7e0352013-12-12 11:55:52 +0000965 if (isTargetIOSBased()|| (isTargetMacOS() && !isMacosxVersionLT(10, 6)))
John McCall3deb1ad2012-08-21 02:47:43 +0000966 return;
John McCall93207072012-08-27 01:56:21 +0000967 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000968}
969
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000970/// Generic_GCC - A tool chain using the 'gcc' command to perform
971/// all subcommands; this relies on gcc translating the majority of
972/// command line options.
973
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000974/// \brief Parse a GCCVersion object out of a string of text.
975///
976/// This is the primary means of forming GCCVersion objects.
977/*static*/
978Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000979 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000980 std::pair<StringRef, StringRef> First = VersionText.split('.');
981 std::pair<StringRef, StringRef> Second = First.second.split('.');
982
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000983 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000984 if (First.first.getAsInteger(10, GoodVersion.Major) ||
985 GoodVersion.Major < 0)
986 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000987 GoodVersion.MajorStr = First.first.str();
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000988 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
989 GoodVersion.Minor < 0)
990 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +0000991 GoodVersion.MinorStr = Second.first.str();
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000992
993 // First look for a number prefix and parse that if present. Otherwise just
994 // stash the entire patch string in the suffix, and leave the number
995 // unspecified. This covers versions strings such as:
996 // 4.4
997 // 4.4.0
998 // 4.4.x
999 // 4.4.2-rc4
1000 // 4.4.x-patched
1001 // And retains any patch number it finds.
1002 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1003 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +00001004 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001005 // Try to parse the number and any suffix.
1006 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1007 GoodVersion.Patch < 0)
1008 return BadVersion;
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00001009 GoodVersion.PatchSuffix = PatchText.substr(EndNumber);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001010 }
1011 }
1012
1013 return GoodVersion;
1014}
1015
1016/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001017bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor,
1018 int RHSPatch,
1019 StringRef RHSPatchSuffix) const {
1020 if (Major != RHSMajor)
1021 return Major < RHSMajor;
1022 if (Minor != RHSMinor)
1023 return Minor < RHSMinor;
1024 if (Patch != RHSPatch) {
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001025 // Note that versions without a specified patch sort higher than those with
1026 // a patch.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001027 if (RHSPatch == -1)
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001028 return true;
1029 if (Patch == -1)
1030 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001031
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001032 // Otherwise just sort on the patch itself.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001033 return Patch < RHSPatch;
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001034 }
Benjamin Kramer604e8482013-08-09 17:17:48 +00001035 if (PatchSuffix != RHSPatchSuffix) {
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001036 // Sort empty suffixes higher.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001037 if (RHSPatchSuffix.empty())
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001038 return true;
1039 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +00001040 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001041
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001042 // Provide a lexicographic sort to make this a total ordering.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001043 return PatchSuffix < RHSPatchSuffix;
Chandler Carruth5193dfc2012-12-29 12:01:08 +00001044 }
1045
1046 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001047 return false;
1048}
1049
Rafael Espindola1af7c212012-02-19 01:38:32 +00001050static StringRef getGCCToolchainDir(const ArgList &Args) {
1051 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1052 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +00001053 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +00001054 return GCC_INSTALL_PREFIX;
1055}
1056
Roman Divacky326d9982013-12-06 18:32:18 +00001057/// \brief Initialize a GCCInstallationDetector from the driver.
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001058///
1059/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +00001060/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +00001061///
1062/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1063/// should instead pull the target out of the driver. This is currently
1064/// necessary because the driver doesn't store the final version of the target
1065/// triple.
Roman Divacky326d9982013-12-06 18:32:18 +00001066void
1067Generic_GCC::GCCInstallationDetector::init(
1068 const Driver &D, const llvm::Triple &TargetTriple, const ArgList &Args) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001069 llvm::Triple BiarchVariantTriple =
1070 TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
Chandler Carruth779579b2012-02-13 02:02:09 +00001071 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001072 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001073 // The library directories which may contain GCC installations.
Chandler Carruthb427c562013-06-22 11:35:51 +00001074 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001075 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001076 SmallVector<StringRef, 10> CandidateTripleAliases;
Chandler Carruthb427c562013-06-22 11:35:51 +00001077 SmallVector<StringRef, 10> CandidateBiarchTripleAliases;
1078 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1079 CandidateTripleAliases, CandidateBiarchLibDirs,
1080 CandidateBiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001081
1082 // Compute the set of prefixes for our search.
1083 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1084 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001085
Rafael Espindola1af7c212012-02-19 01:38:32 +00001086 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1087 if (GCCToolchainDir != "") {
1088 if (GCCToolchainDir.back() == '/')
1089 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001090
Rafael Espindola1af7c212012-02-19 01:38:32 +00001091 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001092 } else {
Rafael Espindola0f4b04e2013-08-28 23:17:47 +00001093 // If we have a SysRoot, try that first.
1094 if (!D.SysRoot.empty()) {
1095 Prefixes.push_back(D.SysRoot);
1096 Prefixes.push_back(D.SysRoot + "/usr");
1097 }
1098
1099 // Then look for gcc installed alongside clang.
Rafael Espindolac29af942012-02-03 01:01:20 +00001100 Prefixes.push_back(D.InstalledDir + "/..");
Rafael Espindola0f4b04e2013-08-28 23:17:47 +00001101
1102 // And finally in /usr.
1103 if (D.SysRoot.empty())
1104 Prefixes.push_back("/usr");
Rafael Espindolac29af942012-02-03 01:01:20 +00001105 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001106
1107 // Loop over the various components which exist and select the best GCC
1108 // installation available. GCC installs are ranked by version number.
1109 Version = GCCVersion::Parse("0.0.0");
1110 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1111 if (!llvm::sys::fs::exists(Prefixes[i]))
1112 continue;
1113 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1114 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1115 if (!llvm::sys::fs::exists(LibDir))
1116 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001117 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001118 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1119 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001120 }
Chandler Carruthb427c562013-06-22 11:35:51 +00001121 for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) {
1122 const std::string LibDir = Prefixes[i] + CandidateBiarchLibDirs[j].str();
Chandler Carruth866faab2012-01-25 07:21:38 +00001123 if (!llvm::sys::fs::exists(LibDir))
1124 continue;
Chandler Carruthb427c562013-06-22 11:35:51 +00001125 for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke;
Chandler Carruth866faab2012-01-25 07:21:38 +00001126 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001127 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruthb427c562013-06-22 11:35:51 +00001128 CandidateBiarchTripleAliases[k],
1129 /*NeedsBiarchSuffix=*/ true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001130 }
1131 }
1132}
1133
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001134void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const {
Benjamin Kramera97e4d12013-08-14 18:38:51 +00001135 for (std::set<std::string>::const_iterator
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001136 I = CandidateGCCInstallPaths.begin(),
1137 E = CandidateGCCInstallPaths.end();
1138 I != E; ++I)
1139 OS << "Found candidate GCC installation: " << *I << "\n";
1140
1141 OS << "Selected GCC installation: " << GCCInstallPath << "\n";
1142}
1143
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001144/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruthb427c562013-06-22 11:35:51 +00001145 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
Chandler Carruth866faab2012-01-25 07:21:38 +00001146 SmallVectorImpl<StringRef> &LibDirs,
1147 SmallVectorImpl<StringRef> &TripleAliases,
Chandler Carruthb427c562013-06-22 11:35:51 +00001148 SmallVectorImpl<StringRef> &BiarchLibDirs,
1149 SmallVectorImpl<StringRef> &BiarchTripleAliases) {
Chandler Carruth866faab2012-01-25 07:21:38 +00001150 // Declare a bunch of static data sets that we'll select between below. These
1151 // are specifically designed to always refer to string literals to avoid any
1152 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001153 static const char *const AArch64LibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001154 static const char *const AArch64Triples[] = { "aarch64-none-linux-gnu",
1155 "aarch64-linux-gnu" };
Tim Northover9bb857a2013-01-31 12:13:10 +00001156
Chandler Carruth866faab2012-01-25 07:21:38 +00001157 static const char *const ARMLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001158 static const char *const ARMTriples[] = { "arm-linux-gnueabi",
1159 "arm-linux-androideabi" };
1160 static const char *const ARMHFTriples[] = { "arm-linux-gnueabihf",
1161 "armv7hl-redhat-linux-gnueabi" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001162
1163 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1164 static const char *const X86_64Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001165 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", "x86_64-pc-linux-gnu",
1166 "x86_64-redhat-linux6E", "x86_64-redhat-linux", "x86_64-suse-linux",
1167 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", "x86_64-slackware-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001168 };
1169 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1170 static const char *const X86Triples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001171 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", "i386-linux-gnu",
1172 "i386-redhat-linux6E", "i686-redhat-linux", "i586-redhat-linux",
1173 "i386-redhat-linux", "i586-suse-linux", "i486-slackware-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001174 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001175 };
1176
1177 static const char *const MIPSLibDirs[] = { "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001178 static const char *const MIPSTriples[] = { "mips-linux-gnu",
1179 "mips-mti-linux-gnu" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001180 static const char *const MIPSELLibDirs[] = { "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001181 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu",
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001182 "mipsel-linux-android" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001183
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001184 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001185 static const char *const MIPS64Triples[] = { "mips64-linux-gnu",
1186 "mips-mti-linux-gnu" };
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001187 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001188 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu",
1189 "mips-mti-linux-gnu" };
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001190
Chandler Carruth866faab2012-01-25 07:21:38 +00001191 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1192 static const char *const PPCTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001193 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
1194 "powerpc-suse-linux", "powerpc-montavista-linuxspe"
Chandler Carruth866faab2012-01-25 07:21:38 +00001195 };
1196 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
Chandler Carruthb427c562013-06-22 11:35:51 +00001197 static const char *const PPC64Triples[] = { "powerpc64-linux-gnu",
1198 "powerpc64-unknown-linux-gnu",
1199 "powerpc64-suse-linux",
1200 "ppc64-redhat-linux" };
Bill Schmidt778d3872013-07-26 01:36:11 +00001201 static const char *const PPC64LELibDirs[] = { "/lib64", "/lib" };
1202 static const char *const PPC64LETriples[] = { "powerpc64le-linux-gnu",
1203 "powerpc64le-unknown-linux-gnu",
1204 "powerpc64le-suse-linux",
1205 "ppc64le-redhat-linux" };
Chandler Carruth866faab2012-01-25 07:21:38 +00001206
Ulrich Weigand47445072013-05-06 16:26:41 +00001207 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1208 static const char *const SystemZTriples[] = {
Chandler Carruthb427c562013-06-22 11:35:51 +00001209 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
1210 "s390x-suse-linux", "s390x-redhat-linux"
Ulrich Weigand47445072013-05-06 16:26:41 +00001211 };
1212
Chandler Carruth866faab2012-01-25 07:21:38 +00001213 switch (TargetTriple.getArch()) {
Tim Northover9bb857a2013-01-31 12:13:10 +00001214 case llvm::Triple::aarch64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001215 LibDirs.append(AArch64LibDirs,
1216 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1217 TripleAliases.append(AArch64Triples,
1218 AArch64Triples + llvm::array_lengthof(AArch64Triples));
1219 BiarchLibDirs.append(AArch64LibDirs,
1220 AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1221 BiarchTripleAliases.append(
1222 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
Tim Northover9bb857a2013-01-31 12:13:10 +00001223 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001224 case llvm::Triple::arm:
1225 case llvm::Triple::thumb:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001226 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001227 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001228 TripleAliases.append(ARMHFTriples,
1229 ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001230 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001231 TripleAliases.append(ARMTriples,
1232 ARMTriples + llvm::array_lengthof(ARMTriples));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001233 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001234 break;
1235 case llvm::Triple::x86_64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001236 LibDirs.append(X86_64LibDirs,
1237 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1238 TripleAliases.append(X86_64Triples,
1239 X86_64Triples + llvm::array_lengthof(X86_64Triples));
1240 BiarchLibDirs.append(X86LibDirs,
1241 X86LibDirs + llvm::array_lengthof(X86LibDirs));
1242 BiarchTripleAliases.append(X86Triples,
1243 X86Triples + llvm::array_lengthof(X86Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001244 break;
1245 case llvm::Triple::x86:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001246 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001247 TripleAliases.append(X86Triples,
1248 X86Triples + llvm::array_lengthof(X86Triples));
1249 BiarchLibDirs.append(X86_64LibDirs,
1250 X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1251 BiarchTripleAliases.append(
1252 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001253 break;
1254 case llvm::Triple::mips:
Chandler Carruthb427c562013-06-22 11:35:51 +00001255 LibDirs.append(MIPSLibDirs,
1256 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1257 TripleAliases.append(MIPSTriples,
1258 MIPSTriples + llvm::array_lengthof(MIPSTriples));
1259 BiarchLibDirs.append(MIPS64LibDirs,
1260 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1261 BiarchTripleAliases.append(
1262 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001263 break;
1264 case llvm::Triple::mipsel:
Chandler Carruthb427c562013-06-22 11:35:51 +00001265 LibDirs.append(MIPSELLibDirs,
1266 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1267 TripleAliases.append(MIPSELTriples,
1268 MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001269 TripleAliases.append(MIPSTriples,
1270 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Chandler Carruthb427c562013-06-22 11:35:51 +00001271 BiarchLibDirs.append(
1272 MIPS64ELLibDirs,
1273 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1274 BiarchTripleAliases.append(
1275 MIPS64ELTriples,
1276 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001277 break;
1278 case llvm::Triple::mips64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001279 LibDirs.append(MIPS64LibDirs,
1280 MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1281 TripleAliases.append(MIPS64Triples,
1282 MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1283 BiarchLibDirs.append(MIPSLibDirs,
1284 MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1285 BiarchTripleAliases.append(MIPSTriples,
1286 MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001287 break;
1288 case llvm::Triple::mips64el:
Chandler Carruthb427c562013-06-22 11:35:51 +00001289 LibDirs.append(MIPS64ELLibDirs,
1290 MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001291 TripleAliases.append(
Chandler Carruthb427c562013-06-22 11:35:51 +00001292 MIPS64ELTriples,
1293 MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1294 BiarchLibDirs.append(MIPSELLibDirs,
1295 MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1296 BiarchTripleAliases.append(
1297 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001298 BiarchTripleAliases.append(
1299 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001300 break;
1301 case llvm::Triple::ppc:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001302 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruthb427c562013-06-22 11:35:51 +00001303 TripleAliases.append(PPCTriples,
1304 PPCTriples + llvm::array_lengthof(PPCTriples));
1305 BiarchLibDirs.append(PPC64LibDirs,
1306 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1307 BiarchTripleAliases.append(
1308 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001309 break;
1310 case llvm::Triple::ppc64:
Chandler Carruthb427c562013-06-22 11:35:51 +00001311 LibDirs.append(PPC64LibDirs,
1312 PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1313 TripleAliases.append(PPC64Triples,
1314 PPC64Triples + llvm::array_lengthof(PPC64Triples));
1315 BiarchLibDirs.append(PPCLibDirs,
1316 PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1317 BiarchTripleAliases.append(PPCTriples,
1318 PPCTriples + llvm::array_lengthof(PPCTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001319 break;
Bill Schmidt778d3872013-07-26 01:36:11 +00001320 case llvm::Triple::ppc64le:
1321 LibDirs.append(PPC64LELibDirs,
1322 PPC64LELibDirs + llvm::array_lengthof(PPC64LELibDirs));
1323 TripleAliases.append(PPC64LETriples,
1324 PPC64LETriples + llvm::array_lengthof(PPC64LETriples));
1325 break;
Ulrich Weigand47445072013-05-06 16:26:41 +00001326 case llvm::Triple::systemz:
Chandler Carruthb427c562013-06-22 11:35:51 +00001327 LibDirs.append(SystemZLibDirs,
1328 SystemZLibDirs + llvm::array_lengthof(SystemZLibDirs));
1329 TripleAliases.append(SystemZTriples,
1330 SystemZTriples + llvm::array_lengthof(SystemZTriples));
Ulrich Weigand47445072013-05-06 16:26:41 +00001331 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001332
1333 default:
1334 // By default, just rely on the standard lib directories and the original
1335 // triple.
1336 break;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001337 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001338
1339 // Always append the drivers target triple to the end, in case it doesn't
1340 // match any of our aliases.
1341 TripleAliases.push_back(TargetTriple.str());
1342
1343 // Also include the multiarch variant if it's different.
Chandler Carruthb427c562013-06-22 11:35:51 +00001344 if (TargetTriple.str() != BiarchTriple.str())
1345 BiarchTripleAliases.push_back(BiarchTriple.str());
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001346}
1347
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001348static bool isMipsArch(llvm::Triple::ArchType Arch) {
1349 return Arch == llvm::Triple::mips ||
1350 Arch == llvm::Triple::mipsel ||
1351 Arch == llvm::Triple::mips64 ||
1352 Arch == llvm::Triple::mips64el;
1353}
1354
1355static bool isMips16(const ArgList &Args) {
1356 Arg *A = Args.getLastArg(options::OPT_mips16,
1357 options::OPT_mno_mips16);
1358 return A && A->getOption().matches(options::OPT_mips16);
1359}
1360
Simon Atanasyan068e0fd2013-10-09 12:12:39 +00001361static bool isMips32r2(const ArgList &Args) {
1362 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1363 options::OPT_mcpu_EQ);
1364
1365 return A && A->getValue() == StringRef("mips32r2");
1366}
1367
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001368static bool isMips64r2(const ArgList &Args) {
1369 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1370 options::OPT_mcpu_EQ);
1371
1372 return A && A->getValue() == StringRef("mips64r2");
1373}
1374
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001375static bool isMicroMips(const ArgList &Args) {
1376 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1377 options::OPT_mno_micromips);
1378 return A && A->getOption().matches(options::OPT_mmicromips);
1379}
1380
Simon Atanasyan5c5b5da2013-11-20 13:53:20 +00001381static bool isMipsFP64(const ArgList &Args) {
1382 Arg *A = Args.getLastArg(options::OPT_mfp64, options::OPT_mfp32);
1383 return A && A->getOption().matches(options::OPT_mfp64);
1384}
1385
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001386static bool isMipsNan2008(const ArgList &Args) {
1387 Arg *A = Args.getLastArg(options::OPT_mnan_EQ);
1388 return A && A->getValue() == StringRef("2008");
1389}
1390
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001391// FIXME: There is the same routine in the Tools.cpp.
1392static bool hasMipsN32ABIArg(const ArgList &Args) {
1393 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00001394 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001395}
1396
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001397static bool hasCrtBeginObj(Twine Path) {
1398 return llvm::sys::fs::exists(Path + "/crtbegin.o");
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001399}
1400
Chandler Carruthb427c562013-06-22 11:35:51 +00001401static bool findTargetBiarchSuffix(std::string &Suffix, StringRef Path,
1402 llvm::Triple::ArchType TargetArch,
1403 const ArgList &Args) {
1404 // FIXME: This routine was only intended to model bi-arch toolchains which
1405 // use -m32 and -m64 to swap between variants of a target. It shouldn't be
1406 // doing ABI-based builtin location for MIPS.
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001407 if (hasMipsN32ABIArg(Args))
1408 Suffix = "/n32";
1409 else if (TargetArch == llvm::Triple::x86_64 ||
1410 TargetArch == llvm::Triple::ppc64 ||
1411 TargetArch == llvm::Triple::systemz ||
1412 TargetArch == llvm::Triple::mips64 ||
1413 TargetArch == llvm::Triple::mips64el)
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001414 Suffix = "/64";
1415 else
1416 Suffix = "/32";
1417
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001418 return hasCrtBeginObj(Path + Suffix);
1419}
1420
Chandler Carruth8677d922013-10-29 08:53:03 +00001421void Generic_GCC::GCCInstallationDetector::findMIPSABIDirSuffix(
1422 std::string &Suffix, llvm::Triple::ArchType TargetArch, StringRef Path,
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001423 const llvm::opt::ArgList &Args) {
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001424 // Some MIPS toolchains put libraries and object files compiled
1425 // using different options in to the sub-directoris which names
1426 // reflects the flags used for compilation. For example sysroot
1427 // directory might looks like the following examples:
1428 //
1429 // /usr
1430 // /lib <= crt*.o files compiled with '-mips32'
1431 // /mips16
1432 // /usr
1433 // /lib <= crt*.o files compiled with '-mips16'
1434 // /el
1435 // /usr
1436 // /lib <= crt*.o files compiled with '-mips16 -EL'
1437 //
1438 // or
1439 //
1440 // /usr
1441 // /lib <= crt*.o files compiled with '-mips32r2'
1442 // /mips16
1443 // /usr
1444 // /lib <= crt*.o files compiled with '-mips32r2 -mips16'
1445 // /mips32
1446 // /usr
1447 // /lib <= crt*.o files compiled with '-mips32'
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001448
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001449 // Check FSF Toolchain path
1450 Suffix.clear();
1451 if (TargetArch == llvm::Triple::mips ||
1452 TargetArch == llvm::Triple::mipsel) {
1453 if (isMicroMips(Args))
1454 Suffix += "/micromips";
1455 else if (isMips32r2(Args))
1456 Suffix += "";
1457 else
1458 Suffix += "/mips32";
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001459
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001460 if (isMips16(Args))
1461 Suffix += "/mips16";
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001462 } else {
1463 if (isMips64r2(Args))
1464 Suffix += hasMipsN32ABIArg(Args) ? "/mips64r2" : "/mips64r2/64";
1465 else
1466 Suffix += hasMipsN32ABIArg(Args) ? "/mips64" : "/mips64/64";
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00001467 }
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001468
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001469 if (TargetArch == llvm::Triple::mipsel ||
1470 TargetArch == llvm::Triple::mips64el)
1471 Suffix += "/el";
1472
1473 if (isSoftFloatABI(Args))
1474 Suffix += "/sof";
1475 else {
1476 if (isMipsFP64(Args))
1477 Suffix += "/fp64";
1478
1479 if (isMipsNan2008(Args))
1480 Suffix += "/nan2008";
1481 }
1482
1483 if (hasCrtBeginObj(Path + Suffix))
1484 return;
1485
1486 // Check Code Sourcery Toolchain path
1487 Suffix.clear();
1488 if (isMips16(Args))
1489 Suffix += "/mips16";
1490 else if (isMicroMips(Args))
1491 Suffix += "/micromips";
1492
1493 if (isSoftFloatABI(Args))
1494 Suffix += "/soft-float";
Simon Atanasyan3e0f3882013-11-26 11:57:48 +00001495 else if (isMipsNan2008(Args))
1496 Suffix += "/nan2008";
Simon Atanasyan13e965a2013-11-26 11:57:14 +00001497
1498 if (TargetArch == llvm::Triple::mipsel ||
1499 TargetArch == llvm::Triple::mips64el)
1500 Suffix += "/el";
1501
1502 if (hasCrtBeginObj(Path + Suffix))
1503 return;
1504
1505 Suffix.clear();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001506}
1507
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001508void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001509 llvm::Triple::ArchType TargetArch, const ArgList &Args,
Chandler Carruthb427c562013-06-22 11:35:51 +00001510 const std::string &LibDir, StringRef CandidateTriple,
1511 bool NeedsBiarchSuffix) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001512 // There are various different suffixes involving the triple we
1513 // check for. We also record what is necessary to walk from each back
1514 // up to the lib directory.
Chandler Carruth866faab2012-01-25 07:21:38 +00001515 const std::string LibSuffixes[] = {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001516 "/gcc/" + CandidateTriple.str(),
Eli Friedmanbf44f362013-07-26 00:53:40 +00001517 // Debian puts cross-compilers in gcc-cross
1518 "/gcc-cross/" + CandidateTriple.str(),
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001519 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1520
Hal Finkelf3587912012-09-18 22:25:07 +00001521 // The Freescale PPC SDK has the gcc libraries in
1522 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1523 "/" + CandidateTriple.str(),
1524
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001525 // Ubuntu has a strange mis-matched pair of triples that this happens to
1526 // match.
1527 // FIXME: It may be worthwhile to generalize this and look for a second
1528 // triple.
Chandler Carruth6e46ca22011-11-09 03:46:20 +00001529 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001530 };
Eli Friedmanbf44f362013-07-26 00:53:40 +00001531 const std::string InstallSuffixes[] = {
1532 "/../../..", // gcc/
1533 "/../../..", // gcc-cross/
1534 "/../../../..", // <triple>/gcc/
1535 "/../..", // <triple>/
1536 "/../../../.." // i386-linux-gnu/gcc/<triple>/
1537 };
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001538 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruthb427c562013-06-22 11:35:51 +00001539 const unsigned NumLibSuffixes =
1540 (llvm::array_lengthof(LibSuffixes) - (TargetArch != llvm::Triple::x86));
Chandler Carruth866faab2012-01-25 07:21:38 +00001541 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1542 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001543 llvm::error_code EC;
Chandler Carruth866faab2012-01-25 07:21:38 +00001544 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001545 !EC && LI != LE; LI = LI.increment(EC)) {
1546 StringRef VersionText = llvm::sys::path::filename(LI->path());
1547 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
Benjamin Kramera97e4d12013-08-14 18:38:51 +00001548 if (CandidateVersion.Major != -1) // Filter obviously bad entries.
1549 if (!CandidateGCCInstallPaths.insert(LI->path()).second)
1550 continue; // Saw this path before; no need to look at it again.
Benjamin Kramer604e8482013-08-09 17:17:48 +00001551 if (CandidateVersion.isOlderThan(4, 1, 1))
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001552 continue;
1553 if (CandidateVersion <= Version)
1554 continue;
Hal Finkel221e11e2011-12-08 05:50:03 +00001555
Chandler Carruth8677d922013-10-29 08:53:03 +00001556 std::string MIPSABIDirSuffix;
Simon Atanasyan54f3f932013-11-26 11:57:09 +00001557 if (isMipsArch(TargetArch))
1558 findMIPSABIDirSuffix(MIPSABIDirSuffix, TargetArch, LI->path(), Args);
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001559
Hal Finkel221e11e2011-12-08 05:50:03 +00001560 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth64cee062012-01-24 19:21:42 +00001561 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth866faab2012-01-25 07:21:38 +00001562 // libs in a subdirectory named 64. The simple logic we follow is that
1563 // *if* there is a subdirectory of the right name with crtbegin.o in it,
Chandler Carruthb427c562013-06-22 11:35:51 +00001564 // we use that. If not, and if not a biarch triple alias, we look for
Chandler Carruth866faab2012-01-25 07:21:38 +00001565 // crtbegin.o without the subdirectory.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00001566
Chandler Carruthb427c562013-06-22 11:35:51 +00001567 std::string BiarchSuffix;
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001568 if (findTargetBiarchSuffix(BiarchSuffix,
Chandler Carruth8677d922013-10-29 08:53:03 +00001569 LI->path() + MIPSABIDirSuffix,
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001570 TargetArch, Args)) {
Chandler Carruthb427c562013-06-22 11:35:51 +00001571 GCCBiarchSuffix = BiarchSuffix;
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001572 } else if (NeedsBiarchSuffix ||
Chandler Carruth8677d922013-10-29 08:53:03 +00001573 !hasCrtBeginObj(LI->path() + MIPSABIDirSuffix)) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00001574 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001575 } else {
Chandler Carruthb427c562013-06-22 11:35:51 +00001576 GCCBiarchSuffix.clear();
Chandler Carruth866faab2012-01-25 07:21:38 +00001577 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001578
1579 Version = CandidateVersion;
Chandler Carruth4d9d7682012-01-24 19:28:29 +00001580 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001581 // FIXME: We hack together the directory name here instead of
1582 // using LI to ensure stable path separators across Windows and
1583 // Linux.
Chandler Carruth866faab2012-01-25 07:21:38 +00001584 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth64cee062012-01-24 19:21:42 +00001585 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth8677d922013-10-29 08:53:03 +00001586 GCCMIPSABIDirSuffix = MIPSABIDirSuffix;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001587 IsValid = true;
1588 }
1589 }
1590}
1591
Rafael Espindola1af7c212012-02-19 01:38:32 +00001592Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1593 const ArgList &Args)
Roman Divacky326d9982013-12-06 18:32:18 +00001594 : ToolChain(D, Triple, Args), GCCInstallation() {
Daniel Dunbar88979912010-08-01 22:29:51 +00001595 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001596 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001597 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001598}
1599
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001600Generic_GCC::~Generic_GCC() {
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001601}
1602
Rafael Espindola7cf32212013-03-20 03:05:54 +00001603Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola260e28d2013-03-18 20:48:54 +00001604 switch (AC) {
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001605 case Action::PreprocessJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001606 if (!Preprocess)
1607 Preprocess.reset(new tools::gcc::Preprocess(*this));
1608 return Preprocess.get();
Rafael Espindolac8e3a012013-03-18 18:50:01 +00001609 case Action::CompileJobClass:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001610 if (!Compile)
1611 Compile.reset(new tools::gcc::Compile(*this));
1612 return Compile.get();
Rafael Espindolad15a8912013-03-19 00:36:57 +00001613 default:
Rafael Espindola7cf32212013-03-20 03:05:54 +00001614 return ToolChain::getTool(AC);
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001615 }
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001616}
1617
Rafael Espindola7cf32212013-03-20 03:05:54 +00001618Tool *Generic_GCC::buildAssembler() const {
Rafael Espindolaedaa4442013-11-23 16:40:57 +00001619 return new tools::gnutools::Assemble(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00001620}
1621
1622Tool *Generic_GCC::buildLinker() const {
1623 return new tools::gcc::Link(*this);
1624}
1625
Chandler Carruth0ae39aa2013-07-30 17:57:09 +00001626void Generic_GCC::printVerboseInfo(raw_ostream &OS) const {
1627 // Print the information about how we detected the GCC installation.
1628 GCCInstallation.print(OS);
1629}
1630
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001631bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001632 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001633}
1634
Chandler Carruth76a943b2012-11-19 03:52:03 +00001635bool Generic_GCC::isPICDefault() const {
1636 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001637}
1638
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001639bool Generic_GCC::isPIEDefault() const {
1640 return false;
1641}
1642
Chandler Carruth76a943b2012-11-19 03:52:03 +00001643bool Generic_GCC::isPICDefaultForced() const {
1644 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001645}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001646
Rafael Espindolaa8b3b682013-11-25 18:50:53 +00001647bool Generic_GCC::IsIntegratedAssemblerDefault() const {
1648 return getTriple().getArch() == llvm::Triple::x86 ||
Renato Golin1588cda2013-12-11 09:35:10 +00001649 getTriple().getArch() == llvm::Triple::x86_64 ||
1650 getTriple().getArch() == llvm::Triple::aarch64 ||
1651 getTriple().getArch() == llvm::Triple::arm ||
1652 getTriple().getArch() == llvm::Triple::thumb;
Rafael Espindolaa8b3b682013-11-25 18:50:53 +00001653}
1654
Tony Linthicum76329bf2011-12-12 21:14:55 +00001655/// Hexagon Toolchain
1656
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001657std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1658
1659 // Locate the rest of the toolchain ...
1660 if (strlen(GCC_INSTALL_PREFIX))
1661 return std::string(GCC_INSTALL_PREFIX);
1662
1663 std::string InstallRelDir = InstalledDir + "/../../gnu";
1664 if (llvm::sys::fs::exists(InstallRelDir))
1665 return InstallRelDir;
1666
1667 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1668 if (llvm::sys::fs::exists(PrefixRelDir))
1669 return PrefixRelDir;
1670
1671 return InstallRelDir;
1672}
1673
Matthew Curtise689b052012-12-06 15:46:07 +00001674static void GetHexagonLibraryPaths(
1675 const ArgList &Args,
1676 const std::string Ver,
1677 const std::string MarchString,
1678 const std::string &InstalledDir,
1679 ToolChain::path_list *LibPaths)
1680{
1681 bool buildingLib = Args.hasArg(options::OPT_shared);
1682
1683 //----------------------------------------------------------------------------
1684 // -L Args
1685 //----------------------------------------------------------------------------
1686 for (arg_iterator
1687 it = Args.filtered_begin(options::OPT_L),
1688 ie = Args.filtered_end();
1689 it != ie;
1690 ++it) {
1691 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1692 LibPaths->push_back((*it)->getValue(i));
1693 }
1694
1695 //----------------------------------------------------------------------------
1696 // Other standard paths
1697 //----------------------------------------------------------------------------
1698 const std::string MarchSuffix = "/" + MarchString;
1699 const std::string G0Suffix = "/G0";
1700 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1701 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1702
1703 // lib/gcc/hexagon/...
1704 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1705 if (buildingLib) {
1706 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1707 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1708 }
1709 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1710 LibPaths->push_back(LibGCCHexagonDir + Ver);
1711
1712 // lib/gcc/...
1713 LibPaths->push_back(RootDir + "lib/gcc");
1714
1715 // hexagon/lib/...
1716 std::string HexagonLibDir = RootDir + "hexagon/lib";
1717 if (buildingLib) {
1718 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1719 LibPaths->push_back(HexagonLibDir + G0Suffix);
1720 }
1721 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1722 LibPaths->push_back(HexagonLibDir);
1723}
1724
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001725Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1726 const ArgList &Args)
1727 : Linux(D, Triple, Args) {
1728 const std::string InstalledDir(getDriver().getInstalledDir());
1729 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1730
1731 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1732 // program paths
1733 const std::string BinDir(GnuDir + "/bin");
1734 if (llvm::sys::fs::exists(BinDir))
1735 getProgramPaths().push_back(BinDir);
1736
1737 // Determine version of GCC libraries and headers to use.
1738 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1739 llvm::error_code ec;
1740 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1741 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1742 !ec && di != de; di = di.increment(ec)) {
1743 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1744 if (MaxVersion < cv)
1745 MaxVersion = cv;
1746 }
1747 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001748
1749 ToolChain::path_list *LibPaths= &getFilePaths();
1750
1751 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1752 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1753 // support 'linux' we'll need to fix this up
1754 LibPaths->clear();
1755
1756 GetHexagonLibraryPaths(
1757 Args,
1758 GetGCCLibAndIncVersion(),
1759 GetTargetCPU(Args),
1760 InstalledDir,
1761 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001762}
1763
1764Hexagon_TC::~Hexagon_TC() {
Tony Linthicum76329bf2011-12-12 21:14:55 +00001765}
1766
Rafael Espindola7cf32212013-03-20 03:05:54 +00001767Tool *Hexagon_TC::buildAssembler() const {
1768 return new tools::hexagon::Assemble(*this);
1769}
1770
1771Tool *Hexagon_TC::buildLinker() const {
1772 return new tools::hexagon::Link(*this);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001773}
1774
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001775void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1776 ArgStringList &CC1Args) const {
1777 const Driver &D = getDriver();
1778
1779 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1780 DriverArgs.hasArg(options::OPT_nostdlibinc))
1781 return;
1782
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001783 std::string Ver(GetGCCLibAndIncVersion());
1784 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1785 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1786 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1787 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1788 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001789}
1790
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001791void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1792 ArgStringList &CC1Args) const {
1793
1794 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1795 DriverArgs.hasArg(options::OPT_nostdincxx))
1796 return;
1797
1798 const Driver &D = getDriver();
1799 std::string Ver(GetGCCLibAndIncVersion());
Rafael Espindola358256c2013-06-26 02:13:00 +00001800 SmallString<128> IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001801
Rafael Espindola358256c2013-06-26 02:13:00 +00001802 llvm::sys::path::append(IncludeDir, "hexagon/include/c++/");
1803 llvm::sys::path::append(IncludeDir, Ver);
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001804 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001805}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001806
Matthew Curtise689b052012-12-06 15:46:07 +00001807ToolChain::CXXStdlibType
1808Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1809 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1810 if (!A)
1811 return ToolChain::CST_Libstdcxx;
1812
1813 StringRef Value = A->getValue();
1814 if (Value != "libstdc++") {
1815 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1816 << A->getAsString(Args);
1817 }
1818
1819 return ToolChain::CST_Libstdcxx;
1820}
1821
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001822static int getHexagonVersion(const ArgList &Args) {
1823 Arg *A = Args.getLastArg(options::OPT_march_EQ, options::OPT_mcpu_EQ);
1824 // Select the default CPU (v4) if none was given.
1825 if (!A)
1826 return 4;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001827
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001828 // FIXME: produce errors if we cannot parse the version.
1829 StringRef WhichHexagon = A->getValue();
1830 if (WhichHexagon.startswith("hexagonv")) {
1831 int Val;
1832 if (!WhichHexagon.substr(sizeof("hexagonv") - 1).getAsInteger(10, Val))
1833 return Val;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001834 }
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001835 if (WhichHexagon.startswith("v")) {
1836 int Val;
1837 if (!WhichHexagon.substr(1).getAsInteger(10, Val))
1838 return Val;
1839 }
1840
1841 // FIXME: should probably be an error.
1842 return 4;
Matthew Curtisf10a5952012-12-06 14:16:43 +00001843}
1844
1845StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1846{
Rafael Espindolabfd88f22013-09-24 13:28:24 +00001847 int V = getHexagonVersion(Args);
1848 // FIXME: We don't support versions < 4. We should error on them.
1849 switch (V) {
1850 default:
1851 llvm_unreachable("Unexpected version");
1852 case 5:
1853 return "v5";
1854 case 4:
1855 return "v4";
1856 case 3:
1857 return "v3";
1858 case 2:
1859 return "v2";
1860 case 1:
1861 return "v1";
Matthew Curtisf10a5952012-12-06 14:16:43 +00001862 }
Matthew Curtisf10a5952012-12-06 14:16:43 +00001863}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001864// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001865
Chris Lattner09797542010-03-04 21:07:38 +00001866/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1867/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1868/// Currently does not support anything else but compilation.
1869
Rafael Espindola84b588b2013-03-18 18:10:27 +00001870TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
1871 const ArgList &Args)
1872 : ToolChain(D, Triple, Args) {
Chris Lattner09797542010-03-04 21:07:38 +00001873 // Path mangling to find libexec
1874 std::string Path(getDriver().Dir);
1875
1876 Path += "/../libexec";
1877 getProgramPaths().push_back(Path);
1878}
1879
1880TCEToolChain::~TCEToolChain() {
Chris Lattner09797542010-03-04 21:07:38 +00001881}
1882
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001883bool TCEToolChain::IsMathErrnoDefault() const {
1884 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001885}
1886
Chandler Carruth76a943b2012-11-19 03:52:03 +00001887bool TCEToolChain::isPICDefault() const {
1888 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001889}
1890
Peter Collingbourne54d770c2013-04-09 04:35:11 +00001891bool TCEToolChain::isPIEDefault() const {
1892 return false;
1893}
1894
Chandler Carruth76a943b2012-11-19 03:52:03 +00001895bool TCEToolChain::isPICDefaultForced() const {
1896 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001897}
1898
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001899/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1900
Rafael Espindola1af7c212012-02-19 01:38:32 +00001901OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1902 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001903 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001904 getFilePaths().push_back("/usr/lib");
1905}
1906
Rafael Espindola7cf32212013-03-20 03:05:54 +00001907Tool *OpenBSD::buildAssembler() const {
1908 return new tools::openbsd::Assemble(*this);
1909}
1910
1911Tool *OpenBSD::buildLinker() const {
1912 return new tools::openbsd::Link(*this);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001913}
1914
Eli Friedman9fa28852012-08-08 23:57:20 +00001915/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1916
1917Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1918 : Generic_ELF(D, Triple, Args) {
1919 getFilePaths().push_back(getDriver().Dir + "/../lib");
1920 getFilePaths().push_back("/usr/lib");
1921}
1922
Rafael Espindola7cf32212013-03-20 03:05:54 +00001923Tool *Bitrig::buildAssembler() const {
1924 return new tools::bitrig::Assemble(*this);
1925}
1926
1927Tool *Bitrig::buildLinker() const {
1928 return new tools::bitrig::Link(*this);
Eli Friedman9fa28852012-08-08 23:57:20 +00001929}
1930
1931void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1932 ArgStringList &CC1Args) const {
1933 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1934 DriverArgs.hasArg(options::OPT_nostdincxx))
1935 return;
1936
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001937 switch (GetCXXStdlibType(DriverArgs)) {
1938 case ToolChain::CST_Libcxx:
1939 addSystemInclude(DriverArgs, CC1Args,
1940 getDriver().SysRoot + "/usr/include/c++/");
1941 break;
1942 case ToolChain::CST_Libstdcxx:
1943 addSystemInclude(DriverArgs, CC1Args,
1944 getDriver().SysRoot + "/usr/include/c++/stdc++");
1945 addSystemInclude(DriverArgs, CC1Args,
1946 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001947
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001948 StringRef Triple = getTriple().str();
1949 if (Triple.startswith("amd64"))
1950 addSystemInclude(DriverArgs, CC1Args,
1951 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1952 Triple.substr(5));
1953 else
1954 addSystemInclude(DriverArgs, CC1Args,
1955 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1956 Triple);
1957 break;
1958 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001959}
1960
1961void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1962 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001963 switch (GetCXXStdlibType(Args)) {
1964 case ToolChain::CST_Libcxx:
1965 CmdArgs.push_back("-lc++");
1966 CmdArgs.push_back("-lcxxrt");
1967 // Include supc++ to provide Unwind until provided by libcxx.
1968 CmdArgs.push_back("-lgcc");
1969 break;
1970 case ToolChain::CST_Libstdcxx:
1971 CmdArgs.push_back("-lstdc++");
1972 break;
1973 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001974}
1975
Daniel Dunbare24297c2009-03-30 21:06:03 +00001976/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1977
Rafael Espindola1af7c212012-02-19 01:38:32 +00001978FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1979 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001980
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001981 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1982 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001983 if ((Triple.getArch() == llvm::Triple::x86 ||
1984 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001985 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001986 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1987 else
1988 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001989}
1990
David Chisnall867ccd82013-11-09 15:10:56 +00001991ToolChain::CXXStdlibType
1992FreeBSD::GetCXXStdlibType(const ArgList &Args) const {
1993 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
1994 StringRef Value = A->getValue();
1995 if (Value == "libstdc++")
1996 return ToolChain::CST_Libstdcxx;
1997 if (Value == "libc++")
1998 return ToolChain::CST_Libcxx;
1999
2000 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2001 << A->getAsString(Args);
2002 }
2003 if (getTriple().getOSMajorVersion() >= 10)
2004 return ToolChain::CST_Libcxx;
2005 return ToolChain::CST_Libstdcxx;
2006}
2007
2008void FreeBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2009 ArgStringList &CC1Args) const {
2010 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2011 DriverArgs.hasArg(options::OPT_nostdincxx))
2012 return;
2013
2014 switch (GetCXXStdlibType(DriverArgs)) {
2015 case ToolChain::CST_Libcxx:
2016 addSystemInclude(DriverArgs, CC1Args,
2017 getDriver().SysRoot + "/usr/include/c++/v1");
2018 break;
2019 case ToolChain::CST_Libstdcxx:
2020 addSystemInclude(DriverArgs, CC1Args,
2021 getDriver().SysRoot + "/usr/include/c++/4.2");
2022 addSystemInclude(DriverArgs, CC1Args,
2023 getDriver().SysRoot + "/usr/include/c++/4.2/backward");
2024 break;
2025 }
2026}
2027
Rafael Espindola7cf32212013-03-20 03:05:54 +00002028Tool *FreeBSD::buildAssembler() const {
2029 return new tools::freebsd::Assemble(*this);
2030}
2031
2032Tool *FreeBSD::buildLinker() const {
2033 return new tools::freebsd::Link(*this);
Daniel Dunbare24297c2009-03-30 21:06:03 +00002034}
Daniel Dunbarcc912342009-05-02 18:28:39 +00002035
Rafael Espindola0f207ed2012-12-13 04:17:14 +00002036bool FreeBSD::UseSjLjExceptions() const {
2037 // FreeBSD uses SjLj exceptions on ARM oabi.
2038 switch (getTriple().getEnvironment()) {
2039 case llvm::Triple::GNUEABI:
2040 case llvm::Triple::EABI:
2041 return false;
2042
2043 default:
2044 return (getTriple().getArch() == llvm::Triple::arm ||
2045 getTriple().getArch() == llvm::Triple::thumb);
2046 }
2047}
2048
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002049/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
2050
Rafael Espindola1af7c212012-02-19 01:38:32 +00002051NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2052 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002053
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00002054 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00002055 // When targeting a 32-bit platform, try the special directory used on
2056 // 64-bit hosts, and only fall back to the main library directory if that
2057 // doesn't work.
2058 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
2059 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00002060 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00002061 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00002062
2063 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002064 }
2065}
2066
Rafael Espindola7cf32212013-03-20 03:05:54 +00002067Tool *NetBSD::buildAssembler() const {
2068 return new tools::netbsd::Assemble(*this);
2069}
2070
2071Tool *NetBSD::buildLinker() const {
2072 return new tools::netbsd::Link(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002073}
2074
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00002075ToolChain::CXXStdlibType
2076NetBSD::GetCXXStdlibType(const ArgList &Args) const {
2077 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
2078 StringRef Value = A->getValue();
2079 if (Value == "libstdc++")
2080 return ToolChain::CST_Libstdcxx;
2081 if (Value == "libc++")
2082 return ToolChain::CST_Libcxx;
2083
2084 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2085 << A->getAsString(Args);
2086 }
2087
Joerg Sonnenbergera4435632013-10-14 20:13:05 +00002088 unsigned Major, Minor, Micro;
2089 getTriple().getOSVersion(Major, Minor, Micro);
2090 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 23) || Major == 0) {
2091 if (getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64)
2092 return ToolChain::CST_Libcxx;
2093 }
Joerg Sonnenberger428ef1e2013-04-30 01:21:43 +00002094 return ToolChain::CST_Libstdcxx;
2095}
2096
2097void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2098 ArgStringList &CC1Args) const {
2099 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2100 DriverArgs.hasArg(options::OPT_nostdincxx))
2101 return;
2102
2103 switch (GetCXXStdlibType(DriverArgs)) {
2104 case ToolChain::CST_Libcxx:
2105 addSystemInclude(DriverArgs, CC1Args,
2106 getDriver().SysRoot + "/usr/include/c++/");
2107 break;
2108 case ToolChain::CST_Libstdcxx:
2109 addSystemInclude(DriverArgs, CC1Args,
2110 getDriver().SysRoot + "/usr/include/g++");
2111 addSystemInclude(DriverArgs, CC1Args,
2112 getDriver().SysRoot + "/usr/include/g++/backward");
2113 break;
2114 }
2115}
2116
Chris Lattner3e2ee142010-07-07 16:01:42 +00002117/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
2118
Rafael Espindola1af7c212012-02-19 01:38:32 +00002119Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2120 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00002121 getFilePaths().push_back(getDriver().Dir + "/../lib");
2122 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00002123}
2124
Rafael Espindola7cf32212013-03-20 03:05:54 +00002125Tool *Minix::buildAssembler() const {
2126 return new tools::minix::Assemble(*this);
2127}
2128
2129Tool *Minix::buildLinker() const {
2130 return new tools::minix::Link(*this);
Chris Lattner3e2ee142010-07-07 16:01:42 +00002131}
2132
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002133/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
2134
Rafael Espindola1af7c212012-02-19 01:38:32 +00002135AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
2136 const ArgList &Args)
2137 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002138
Daniel Dunbar88979912010-08-01 22:29:51 +00002139 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002140 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002141 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002142
Daniel Dunbar083edf72009-12-21 18:54:17 +00002143 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002144 getFilePaths().push_back("/usr/lib");
2145 getFilePaths().push_back("/usr/sfw/lib");
2146 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00002147 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002148
2149}
2150
Rafael Espindola7cf32212013-03-20 03:05:54 +00002151Tool *AuroraUX::buildAssembler() const {
2152 return new tools::auroraux::Assemble(*this);
2153}
2154
2155Tool *AuroraUX::buildLinker() const {
2156 return new tools::auroraux::Link(*this);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002157}
2158
David Chisnallf571cde2012-02-15 13:39:01 +00002159/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2160
Rafael Espindola1af7c212012-02-19 01:38:32 +00002161Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2162 const ArgList &Args)
2163 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00002164
2165 getProgramPaths().push_back(getDriver().getInstalledDir());
2166 if (getDriver().getInstalledDir() != getDriver().Dir)
2167 getProgramPaths().push_back(getDriver().Dir);
2168
2169 getFilePaths().push_back(getDriver().Dir + "/../lib");
2170 getFilePaths().push_back("/usr/lib");
2171}
2172
Rafael Espindola7cf32212013-03-20 03:05:54 +00002173Tool *Solaris::buildAssembler() const {
2174 return new tools::solaris::Assemble(*this);
2175}
2176
2177Tool *Solaris::buildLinker() const {
2178 return new tools::solaris::Link(*this);
David Chisnallf571cde2012-02-15 13:39:01 +00002179}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002180
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002181/// Distribution (very bare-bones at the moment).
Eli Friedman5cd659f2009-05-26 07:52:18 +00002182
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002183enum Distro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002184 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002185 DebianLenny,
2186 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00002187 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002188 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00002189 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00002190 RHEL4,
2191 RHEL5,
2192 RHEL6,
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002193 Fedora,
Rafael Espindola10a63c22013-07-03 14:14:00 +00002194 OpenSUSE,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00002195 UbuntuHardy,
2196 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00002197 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00002198 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002199 UbuntuLucid,
2200 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00002201 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00002202 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002203 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00002204 UbuntuQuantal,
2205 UbuntuRaring,
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002206 UbuntuSaucy,
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002207 UbuntuTrusty,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002208 UnknownDistro
2209};
2210
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002211static bool IsRedhat(enum Distro Distro) {
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002212 return Distro == Fedora || (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002213}
2214
Rafael Espindola10a63c22013-07-03 14:14:00 +00002215static bool IsOpenSUSE(enum Distro Distro) {
2216 return Distro == OpenSUSE;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002217}
2218
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002219static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002220 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002221}
2222
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002223static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002224 return Distro >= UbuntuHardy && Distro <= UbuntuTrusty;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002225}
2226
Rafael Espindolaa8398552013-10-28 23:14:34 +00002227static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00002228 OwningPtr<llvm::MemoryBuffer> File;
Rafael Espindolaa8398552013-10-28 23:14:34 +00002229 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002230 StringRef Data = File.get()->getBuffer();
2231 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002232 Data.split(Lines, "\n");
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002233 Distro Version = UnknownDistro;
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002234 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2235 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinge6d94da02013-03-28 19:02:48 +00002236 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002237 .Case("hardy", UbuntuHardy)
2238 .Case("intrepid", UbuntuIntrepid)
2239 .Case("jaunty", UbuntuJaunty)
2240 .Case("karmic", UbuntuKarmic)
2241 .Case("lucid", UbuntuLucid)
2242 .Case("maverick", UbuntuMaverick)
2243 .Case("natty", UbuntuNatty)
2244 .Case("oneiric", UbuntuOneiric)
2245 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002246 .Case("quantal", UbuntuQuantal)
2247 .Case("raring", UbuntuRaring)
Sylvestre Ledru93c47b72013-06-13 11:52:27 +00002248 .Case("saucy", UbuntuSaucy)
Sylvestre Ledruaaf01a72013-11-07 09:31:30 +00002249 .Case("trusty", UbuntuTrusty)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002250 .Default(UnknownDistro);
2251 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002252 }
2253
Rafael Espindolaa8398552013-10-28 23:14:34 +00002254 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002255 StringRef Data = File.get()->getBuffer();
Rafael Espindola0d2cbc02013-10-25 18:09:41 +00002256 if (Data.startswith("Fedora release"))
2257 return Fedora;
Chris Lattner84e38552011-05-22 05:36:06 +00002258 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002259 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002260 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002261 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002262 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002263 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002264 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002265 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002266 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002267 Data.find("release 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002268 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002269 return UnknownDistro;
2270 }
2271
Rafael Espindolaa8398552013-10-28 23:14:34 +00002272 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002273 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002274 if (Data[0] == '5')
2275 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002276 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002277 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002278 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002279 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002280 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2281 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002282 return UnknownDistro;
2283 }
2284
Rafael Espindolaa8398552013-10-28 23:14:34 +00002285 if (llvm::sys::fs::exists("/etc/SuSE-release"))
Rafael Espindola10a63c22013-07-03 14:14:00 +00002286 return OpenSUSE;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002287
Rafael Espindolaa8398552013-10-28 23:14:34 +00002288 if (llvm::sys::fs::exists("/etc/exherbo-release"))
Rafael Espindola12479842010-11-11 02:07:13 +00002289 return Exherbo;
2290
Rafael Espindolaa8398552013-10-28 23:14:34 +00002291 if (llvm::sys::fs::exists("/etc/arch-release"))
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002292 return ArchLinux;
2293
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002294 return UnknownDistro;
2295}
2296
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002297/// \brief Get our best guess at the multiarch triple for a target.
2298///
2299/// Debian-based systems are starting to use a multiarch setup where they use
2300/// a target-triple directory in the library and header search paths.
2301/// Unfortunately, this triple does not align with the vanilla target triple,
2302/// so we provide a rough mapping here.
2303static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2304 StringRef SysRoot) {
2305 // For most architectures, just use whatever we have rather than trying to be
2306 // clever.
2307 switch (TargetTriple.getArch()) {
2308 default:
2309 return TargetTriple.str();
2310
2311 // We use the existence of '/lib/<triple>' as a directory to detect some
2312 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002313 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2314 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002315 case llvm::Triple::arm:
2316 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002317 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2318 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2319 return "arm-linux-gnueabihf";
2320 } else {
2321 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2322 return "arm-linux-gnueabi";
2323 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002324 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002325 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002326 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2327 return "i386-linux-gnu";
2328 return TargetTriple.str();
2329 case llvm::Triple::x86_64:
2330 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2331 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002332 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002333 case llvm::Triple::aarch64:
2334 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2335 return "aarch64-linux-gnu";
Tim Northoverfbb56b72013-06-13 22:54:55 +00002336 return TargetTriple.str();
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002337 case llvm::Triple::mips:
2338 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2339 return "mips-linux-gnu";
2340 return TargetTriple.str();
2341 case llvm::Triple::mipsel:
2342 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2343 return "mipsel-linux-gnu";
2344 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002345 case llvm::Triple::ppc:
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00002346 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2347 return "powerpc-linux-gnuspe";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002348 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2349 return "powerpc-linux-gnu";
2350 return TargetTriple.str();
2351 case llvm::Triple::ppc64:
2352 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2353 return "powerpc64-linux-gnu";
Bill Schmidt778d3872013-07-26 01:36:11 +00002354 case llvm::Triple::ppc64le:
2355 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
2356 return "powerpc64le-linux-gnu";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002357 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002358 }
2359}
2360
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002361static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2362 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2363}
2364
Simon Atanasyand4413882012-09-14 11:27:24 +00002365static StringRef getMultilibDir(const llvm::Triple &Triple,
2366 const ArgList &Args) {
Chandler Carruthda797042013-10-29 10:27:30 +00002367 if (isMipsArch(Triple.getArch())) {
2368 // lib32 directory has a special meaning on MIPS targets.
2369 // It contains N32 ABI binaries. Use this folder if produce
2370 // code for N32 ABI only.
2371 if (hasMipsN32ABIArg(Args))
2372 return "lib32";
2373 return Triple.isArch32Bit() ? "lib" : "lib64";
2374 }
Simon Atanasyand4413882012-09-14 11:27:24 +00002375
Chandler Carruthda797042013-10-29 10:27:30 +00002376 // It happens that only x86 and PPC use the 'lib32' variant of multilib, and
2377 // using that variant while targeting other architectures causes problems
2378 // because the libraries are laid out in shared system roots that can't cope
2379 // with a 'lib32' multilib search path being considered. So we only enable
2380 // them when we know we may need it.
2381 //
2382 // FIXME: This is a bit of a hack. We should really unify this code for
2383 // reasoning about multilib spellings with the lib dir spellings in the
2384 // GCCInstallationDetector, but that is a more significant refactoring.
2385 if (Triple.getArch() == llvm::Triple::x86 ||
2386 Triple.getArch() == llvm::Triple::ppc)
Simon Atanasyand4413882012-09-14 11:27:24 +00002387 return "lib32";
2388
2389 return Triple.isArch32Bit() ? "lib" : "lib64";
2390}
2391
Rafael Espindola1af7c212012-02-19 01:38:32 +00002392Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2393 : Generic_ELF(D, Triple, Args) {
Roman Divacky326d9982013-12-06 18:32:18 +00002394 GCCInstallation.init(D, Triple, Args);
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002395 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyana0d89572013-10-05 14:37:55 +00002396 std::string SysRoot = computeSysRoot();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002397
Rafael Espindola10a63c22013-07-03 14:14:00 +00002398 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
Chandler Carruthd6c62b62013-06-20 23:37:54 +00002399 // least) put various tools in a triple-prefixed directory off of the parent
2400 // of the GCC installation. We use the GCC triple here to ensure that we end
2401 // up with tools that support the same amount of cross compiling as the
2402 // detected GCC installation. For example, if we find a GCC installation
2403 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
2404 // used to target i386.
2405 // FIXME: This seems unlikely to be Linux-specific.
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002406 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002407 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002408 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002409
2410 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002411
Rafael Espindolaa8398552013-10-28 23:14:34 +00002412 Distro Distro = DetectDistro(Arch);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002413
Rafael Espindola10a63c22013-07-03 14:14:00 +00002414 if (IsOpenSUSE(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002415 ExtraOpts.push_back("-z");
2416 ExtraOpts.push_back("relro");
2417 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002418
Douglas Gregord9bb1522011-03-06 19:11:49 +00002419 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002420 ExtraOpts.push_back("-X");
2421
Logan Chienc6fd8202012-09-02 09:30:11 +00002422 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002423 const bool IsMips = isMipsArch(Arch);
2424
2425 if (IsMips && !SysRoot.empty())
2426 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002427
Chandler Carruth0b842912011-12-09 04:45:18 +00002428 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2429 // and the MIPS ABI require .dynsym to be sorted in different ways.
2430 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2431 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002432 // Android loader does not support .gnu.hash.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002433 if (!IsMips && !IsAndroid) {
Rafael Espindola10a63c22013-07-03 14:14:00 +00002434 if (IsRedhat(Distro) || IsOpenSUSE(Distro) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002435 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002436 ExtraOpts.push_back("--hash-style=gnu");
2437
Rafael Espindola10a63c22013-07-03 14:14:00 +00002438 if (IsDebian(Distro) || IsOpenSUSE(Distro) || Distro == UbuntuLucid ||
Chandler Carruth0b842912011-12-09 04:45:18 +00002439 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2440 ExtraOpts.push_back("--hash-style=both");
2441 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002442
Chris Lattner84e38552011-05-22 05:36:06 +00002443 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002444 ExtraOpts.push_back("--no-add-needed");
2445
Eli Friedmanf7600942011-06-02 21:36:53 +00002446 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola10a63c22013-07-03 14:14:00 +00002447 Distro == DebianJessie || IsOpenSUSE(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002448 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002449 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002450 ExtraOpts.push_back("--build-id");
2451
Rafael Espindola10a63c22013-07-03 14:14:00 +00002452 if (IsOpenSUSE(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002453 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002454
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002455 // The selection of paths to try here is designed to match the patterns which
2456 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2457 // This was determined by running GCC in a fake filesystem, creating all
2458 // possible permutations of these directories, and seeing which ones it added
2459 // to the link paths.
2460 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002461
Rafael Espindolaa8398552013-10-28 23:14:34 +00002462 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002463 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002464
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002465 // Add the multilib suffixed paths where they are available.
2466 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002467 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002468 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002469
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002470 // Sourcery CodeBench MIPS toolchain holds some libraries under
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002471 // a biarch-like suffix of the GCC installation.
2472 //
2473 // FIXME: It would be cleaner to model this as a variant of bi-arch. IE,
2474 // instead of a '64' biarch suffix it would be 'el' or something.
Simon Atanasyan068e0fd2013-10-09 12:12:39 +00002475 if (IsAndroid && IsMips && isMips32r2(Args)) {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002476 assert(GCCInstallation.getBiarchSuffix().empty() &&
2477 "Unexpected bi-arch suffix");
2478 addPathIfExists(GCCInstallation.getInstallPath() + "/mips-r2", Paths);
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002479 } else {
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002480 addPathIfExists((GCCInstallation.getInstallPath() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002481 GCCInstallation.getMIPSABIDirSuffix() +
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002482 GCCInstallation.getBiarchSuffix()),
2483 Paths);
Chandler Carruth9b6ce932013-10-29 02:27:56 +00002484 }
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002485
2486 // GCC cross compiling toolchains will install target libraries which ship
2487 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
2488 // any part of the GCC installation in
2489 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
2490 // debatable, but is the reality today. We need to search this tree even
2491 // when we have a sysroot somewhere else. It is the responsibility of
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002492 // whomever is doing the cross build targeting a sysroot using a GCC
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002493 // installation that is *not* within the system root to ensure two things:
2494 //
2495 // 1) Any DSOs that are linked in from this tree or from the install path
2496 // above must be preasant on the system root and found via an
2497 // appropriate rpath.
2498 // 2) There must not be libraries installed into
2499 // <prefix>/<triple>/<libdir> unless they should be preferred over
2500 // those within the system root.
2501 //
2502 // Note that this matches the GCC behavior. See the below comment for where
2503 // Clang diverges from GCC's behavior.
2504 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib +
Chandler Carruth8677d922013-10-29 08:53:03 +00002505 GCCInstallation.getMIPSABIDirSuffix(),
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002506 Paths);
2507
Chandler Carruth69a125b2012-04-06 16:32:06 +00002508 // If the GCC installation we found is inside of the sysroot, we want to
2509 // prefer libraries installed in the parent prefix of the GCC installation.
2510 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002511 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002512 // This usually happens when there is an external cross compiler on the
2513 // host system, and a more minimal sysroot available that is the target of
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002514 // the cross. Note that GCC does include some of these directories in some
2515 // configurations but this seems somewhere between questionable and simply
2516 // a bug.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002517 if (StringRef(LibPath).startswith(SysRoot)) {
Chandler Carruth69a125b2012-04-06 16:32:06 +00002518 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2519 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2520 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002521 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002522 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2523 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2524 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2525 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2526
Chandler Carruthb427c562013-06-22 11:35:51 +00002527 // Try walking via the GCC triple path in case of biarch or multiarch GCC
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002528 // installations with strange symlinks.
Rafael Espindolab6250162013-10-25 17:06:04 +00002529 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002530 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002531 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002532
Rafael Espindolab6250162013-10-25 17:06:04 +00002533 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002534 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002535 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Rafael Espindolaa8398552013-10-28 23:14:34 +00002536 if (!GCCInstallation.getBiarchSuffix().empty())
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002537 addPathIfExists(GCCInstallation.getInstallPath() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002538 GCCInstallation.getMIPSABIDirSuffix(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002539
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002540 // See comments above on the multilib variant for details of why this is
2541 // included even from outside the sysroot.
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002542 addPathIfExists(LibPath + "/../" + GCCTriple.str() +
Chandler Carruth8677d922013-10-29 08:53:03 +00002543 "/lib" + GCCInstallation.getMIPSABIDirSuffix(), Paths);
Chandler Carruth7f8042c2013-07-31 00:37:07 +00002544
2545 // See comments above on the multilib variant for details of why this is
2546 // only included from within the sysroot.
2547 if (StringRef(LibPath).startswith(SysRoot))
Chandler Carruth69a125b2012-04-06 16:32:06 +00002548 addPathIfExists(LibPath, Paths);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002549 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002550 addPathIfExists(SysRoot + "/lib", Paths);
2551 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002552}
2553
Roman Divackyf0d7f942013-11-10 09:31:43 +00002554bool FreeBSD::HasNativeLLVMSupport() const {
2555 return true;
2556}
2557
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002558bool Linux::HasNativeLLVMSupport() const {
2559 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002560}
2561
Rafael Espindola7cf32212013-03-20 03:05:54 +00002562Tool *Linux::buildLinker() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002563 return new tools::gnutools::Link(*this);
Rafael Espindola7cf32212013-03-20 03:05:54 +00002564}
2565
2566Tool *Linux::buildAssembler() const {
Thomas Schwinge4e555262013-03-28 19:04:25 +00002567 return new tools::gnutools::Assemble(*this);
Rafael Espindola92b00932010-08-10 00:25:48 +00002568}
2569
Chandler Carruth05fb5852012-11-21 23:40:23 +00002570void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2571 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002572 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Benjamin Kramer604e8482013-08-09 17:17:48 +00002573 bool UseInitArrayDefault =
2574 !V.isOlderThan(4, 7, 0) ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002575 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002576 getTriple().getEnvironment() == llvm::Triple::Android;
2577 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2578 options::OPT_fno_use_init_array,
2579 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002580 CC1Args.push_back("-fuse-init-array");
2581}
2582
Simon Atanasyana0d89572013-10-05 14:37:55 +00002583std::string Linux::computeSysRoot() const {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002584 if (!getDriver().SysRoot.empty())
2585 return getDriver().SysRoot;
2586
2587 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
2588 return std::string();
2589
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002590 // Standalone MIPS toolchains use different names for sysroot folder
2591 // and put it into different places. Here we try to check some known
2592 // variants.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002593
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002594 const StringRef InstallDir = GCCInstallation.getInstallPath();
2595 const StringRef TripleStr = GCCInstallation.getTriple().str();
Chandler Carruth8677d922013-10-29 08:53:03 +00002596 const StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002597
Chandler Carruth8677d922013-10-29 08:53:03 +00002598 std::string Path = (InstallDir + "/../../../../" + TripleStr + "/libc" +
2599 MIPSABIDirSuffix).str();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002600
2601 if (llvm::sys::fs::exists(Path))
2602 return Path;
2603
Chandler Carruth8677d922013-10-29 08:53:03 +00002604 Path = (InstallDir + "/../../../../sysroot" + MIPSABIDirSuffix).str();
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002605
2606 if (llvm::sys::fs::exists(Path))
2607 return Path;
2608
2609 return std::string();
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002610}
2611
Chandler Carrutha796f532011-11-05 20:17:13 +00002612void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2613 ArgStringList &CC1Args) const {
2614 const Driver &D = getDriver();
Simon Atanasyana0d89572013-10-05 14:37:55 +00002615 std::string SysRoot = computeSysRoot();
Chandler Carrutha796f532011-11-05 20:17:13 +00002616
2617 if (DriverArgs.hasArg(options::OPT_nostdinc))
2618 return;
2619
2620 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002621 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002622
2623 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola358256c2013-06-26 02:13:00 +00002624 SmallString<128> P(D.ResourceDir);
2625 llvm::sys::path::append(P, "include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002626 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002627 }
2628
2629 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2630 return;
2631
2632 // Check for configure-time C include directories.
2633 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2634 if (CIncludeDirs != "") {
2635 SmallVector<StringRef, 5> dirs;
2636 CIncludeDirs.split(dirs, ":");
2637 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2638 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002639 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? SysRoot : "";
Chandler Carrutha796f532011-11-05 20:17:13 +00002640 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2641 }
2642 return;
2643 }
2644
2645 // Lacking those, try to detect the correct set of system includes for the
2646 // target triple.
2647
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002648 // Sourcery CodeBench and modern FSF Mips toolchains put extern C
2649 // system includes under three additional directories.
2650 if (GCCInstallation.isValid() && isMipsArch(getTriple().getArch())) {
Chandler Carruthb427c562013-06-22 11:35:51 +00002651 addExternCSystemIncludeIfExists(
2652 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + "/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002653
Chandler Carruthb427c562013-06-22 11:35:51 +00002654 addExternCSystemIncludeIfExists(
2655 DriverArgs, CC1Args,
2656 GCCInstallation.getInstallPath() + "/../../../../" +
2657 GCCInstallation.getTriple().str() + "/libc/usr/include");
Simon Atanasyana61b7ec2013-10-10 07:57:44 +00002658
2659 addExternCSystemIncludeIfExists(
2660 DriverArgs, CC1Args,
2661 GCCInstallation.getInstallPath() + "/../../../../sysroot/usr/include");
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002662 }
2663
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002664 // Implement generic Debian multiarch support.
2665 const StringRef X86_64MultiarchIncludeDirs[] = {
2666 "/usr/include/x86_64-linux-gnu",
2667
2668 // FIXME: These are older forms of multiarch. It's not clear that they're
2669 // in use in any released version of Debian, so we should consider
2670 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002671 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002672 };
2673 const StringRef X86MultiarchIncludeDirs[] = {
2674 "/usr/include/i386-linux-gnu",
2675
2676 // FIXME: These are older forms of multiarch. It's not clear that they're
2677 // in use in any released version of Debian, so we should consider
2678 // removing them.
Chandler Carruthb427c562013-06-22 11:35:51 +00002679 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002680 "/usr/include/i486-linux-gnu"
2681 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002682 const StringRef AArch64MultiarchIncludeDirs[] = {
2683 "/usr/include/aarch64-linux-gnu"
2684 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002685 const StringRef ARMMultiarchIncludeDirs[] = {
2686 "/usr/include/arm-linux-gnueabi"
2687 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002688 const StringRef ARMHFMultiarchIncludeDirs[] = {
2689 "/usr/include/arm-linux-gnueabihf"
2690 };
Eli Friedman7771c832011-11-11 03:05:19 +00002691 const StringRef MIPSMultiarchIncludeDirs[] = {
2692 "/usr/include/mips-linux-gnu"
2693 };
2694 const StringRef MIPSELMultiarchIncludeDirs[] = {
2695 "/usr/include/mipsel-linux-gnu"
2696 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002697 const StringRef PPCMultiarchIncludeDirs[] = {
2698 "/usr/include/powerpc-linux-gnu"
2699 };
2700 const StringRef PPC64MultiarchIncludeDirs[] = {
2701 "/usr/include/powerpc64-linux-gnu"
2702 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002703 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002704 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002705 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002706 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002707 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002708 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2709 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002710 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002711 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2712 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2713 else
2714 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002715 } else if (getTriple().getArch() == llvm::Triple::mips) {
2716 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2717 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2718 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002719 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2720 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2721 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2722 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002723 }
2724 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2725 E = MultiarchIncludeDirs.end();
2726 I != E; ++I) {
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002727 if (llvm::sys::fs::exists(SysRoot + *I)) {
2728 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + *I);
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002729 break;
2730 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002731 }
2732
2733 if (getTriple().getOS() == llvm::Triple::RTEMS)
2734 return;
2735
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002736 // Add an include of '/include' directly. This isn't provided by default by
2737 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2738 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002739 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002740
Simon Atanasyan08450bd2013-04-20 08:15:03 +00002741 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carrutha796f532011-11-05 20:17:13 +00002742}
2743
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002744/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002745/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2746 const ArgList &DriverArgs,
2747 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002748 if (!llvm::sys::fs::exists(Base))
2749 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002750 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002751 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002752 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002753 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002754}
2755
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002756/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2757/// libstdc++ installation.
2758/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2759 Twine TargetArchDir,
Chandler Carruth8677d922013-10-29 08:53:03 +00002760 Twine BiarchSuffix,
2761 Twine MIPSABIDirSuffix,
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002762 const ArgList &DriverArgs,
2763 ArgStringList &CC1Args) {
Chandler Carruth8677d922013-10-29 08:53:03 +00002764 if (!addLibStdCXXIncludePaths(Base + Suffix,
2765 TargetArchDir + MIPSABIDirSuffix + BiarchSuffix,
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002766 DriverArgs, CC1Args))
2767 return false;
2768
2769 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
Chandler Carruth8677d922013-10-29 08:53:03 +00002770 + MIPSABIDirSuffix + BiarchSuffix);
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002771 return true;
2772}
2773
Chandler Carrutha796f532011-11-05 20:17:13 +00002774void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2775 ArgStringList &CC1Args) const {
2776 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2777 DriverArgs.hasArg(options::OPT_nostdincxx))
2778 return;
2779
Chandler Carruthf4701732011-11-07 09:01:17 +00002780 // Check if libc++ has been enabled and provide its include paths if so.
2781 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2782 // libc++ is always installed at a fixed path on Linux currently.
2783 addSystemInclude(DriverArgs, CC1Args,
2784 getDriver().SysRoot + "/usr/include/c++/v1");
2785 return;
2786 }
2787
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002788 // We need a detected GCC installation on Linux to provide libstdc++'s
2789 // headers. We handled the libc++ case above.
2790 if (!GCCInstallation.isValid())
2791 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002792
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002793 // By default, look for the C++ headers in an include directory adjacent to
2794 // the lib directory of the GCC installation. Note that this is expect to be
2795 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2796 StringRef LibDir = GCCInstallation.getParentLibPath();
2797 StringRef InstallDir = GCCInstallation.getInstallPath();
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002798 StringRef TripleStr = GCCInstallation.getTriple().str();
Chandler Carruth8677d922013-10-29 08:53:03 +00002799 StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix();
Simon Atanasyanee1accf2013-09-28 13:45:11 +00002800 StringRef BiarchSuffix = GCCInstallation.getBiarchSuffix();
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002801 const GCCVersion &Version = GCCInstallation.getVersion();
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002802
Chandler Carruth8677d922013-10-29 08:53:03 +00002803 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2804 "/c++/" + Version.Text, TripleStr, BiarchSuffix,
2805 MIPSABIDirSuffix, DriverArgs, CC1Args))
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002806 return;
2807
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002808 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002809 // Gentoo is weird and places its headers inside the GCC install, so if the
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002810 // first attempt to find the headers fails, try these patterns.
2811 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
2812 Version.MinorStr,
2813 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002814 // Android standalone toolchain has C++ headers in yet another place.
Chandler Carruth1f2b2f82013-08-26 08:59:53 +00002815 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
Hal Finkelf3587912012-09-18 22:25:07 +00002816 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2817 // without a subdirectory corresponding to the gcc version.
2818 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002819 };
2820
2821 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
Chandler Carruth8677d922013-10-29 08:53:03 +00002822 if (addLibStdCXXIncludePaths(IncludePathCandidates[i],
2823 TripleStr + MIPSABIDirSuffix + BiarchSuffix,
2824 DriverArgs, CC1Args))
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002825 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002826 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002827}
2828
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002829bool Linux::isPIEDefault() const {
Peter Collingbourne32701642013-11-01 18:16:25 +00002830 return getSanitizerArgs().hasZeroBaseShadow();
Peter Collingbourne54d770c2013-04-09 04:35:11 +00002831}
2832
Daniel Dunbarcc912342009-05-02 18:28:39 +00002833/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2834
Rafael Espindola1af7c212012-02-19 01:38:32 +00002835DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2836 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002837
2838 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002839 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002840 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002841 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002842
Daniel Dunbar083edf72009-12-21 18:54:17 +00002843 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002844 getFilePaths().push_back("/usr/lib");
John McCall65b8da02013-04-11 22:55:55 +00002845 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
2846 getFilePaths().push_back("/usr/lib/gcc47");
2847 else
2848 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002849}
2850
Rafael Espindola7cf32212013-03-20 03:05:54 +00002851Tool *DragonFly::buildAssembler() const {
2852 return new tools::dragonfly::Assemble(*this);
2853}
2854
2855Tool *DragonFly::buildLinker() const {
2856 return new tools::dragonfly::Link(*this);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002857}
Robert Lyttoncf1dd692013-10-11 10:29:40 +00002858
2859
2860/// XCore tool chain
2861XCore::XCore(const Driver &D, const llvm::Triple &Triple,
2862 const ArgList &Args) : ToolChain(D, Triple, Args) {
2863 // ProgramPaths are found via 'PATH' environment variable.
2864}
2865
2866Tool *XCore::buildAssembler() const {
2867 return new tools::XCore::Assemble(*this);
2868}
2869
2870Tool *XCore::buildLinker() const {
2871 return new tools::XCore::Link(*this);
2872}
2873
2874bool XCore::isPICDefault() const {
2875 return false;
2876}
2877
2878bool XCore::isPIEDefault() const {
2879 return false;
2880}
2881
2882bool XCore::isPICDefaultForced() const {
2883 return false;
2884}
2885
2886bool XCore::SupportsProfiling() const {
2887 return false;
2888}
2889
2890bool XCore::hasBlocksRuntime() const {
2891 return false;
2892}
2893
2894
2895void XCore::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2896 ArgStringList &CC1Args) const {
2897 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2898 DriverArgs.hasArg(options::OPT_nostdlibinc))
2899 return;
2900 if (const char *cl_include_dir = getenv("XCC_C_INCLUDE_PATH")) {
2901 SmallVector<StringRef, 4> Dirs;
2902 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
2903 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
2904 ArrayRef<StringRef> DirVec(Dirs);
2905 addSystemIncludes(DriverArgs, CC1Args, DirVec);
2906 }
2907}
2908
2909void XCore::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
2910 llvm::opt::ArgStringList &CC1Args) const {
2911 CC1Args.push_back("-nostdsysteminc");
2912}
2913
2914void XCore::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2915 ArgStringList &CC1Args) const {
2916 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2917 DriverArgs.hasArg(options::OPT_nostdlibinc))
2918 return;
2919 if (const char *cl_include_dir = getenv("XCC_CPLUS_INCLUDE_PATH")) {
2920 SmallVector<StringRef, 4> Dirs;
2921 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
2922 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
2923 ArrayRef<StringRef> DirVec(Dirs);
2924 addSystemIncludes(DriverArgs, CC1Args, DirVec);
2925 }
2926}
2927
2928void XCore::AddCXXStdlibLibArgs(const ArgList &Args,
2929 ArgStringList &CmdArgs) const {
2930 // We don't output any lib args. This is handled by xcc.
2931}