blob: 9606df6b5a208b5096c24fd1810af1a970b1201a [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000011#include "clang/Basic/ObjCRuntime.h"
12#include "clang/Basic/Version.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070013#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
Daniel Dunbar0f602de2010-05-20 21:48:38 +000014#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000017#include "clang/Driver/Options.h"
Alexey Samsonov1b8f12d2013-08-19 09:14:21 +000018#include "clang/Driver/SanitizerArgs.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "llvm/ADT/STLExtras.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000020#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000021#include "llvm/ADT/StringExtras.h"
Bob Wilsona59956b2011-10-07 00:37:57 +000022#include "llvm/ADT/StringSwitch.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000023#include "llvm/Option/Arg.h"
24#include "llvm/Option/ArgList.h"
25#include "llvm/Option/OptTable.h"
26#include "llvm/Option/Option.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000027#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000028#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000029#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000030#include "llvm/Support/Path.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070031#include "llvm/Support/Program.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000033#include <cstdlib> // ::getenv
Stephen Hinesc568f1e2014-07-21 00:47:37 -070034#include <system_error>
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000035
Daniel Dunbar39176082009-03-20 00:20:03 +000036using namespace clang::driver;
37using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000038using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000039using namespace llvm::opt;
Daniel Dunbar39176082009-03-20 00:20:03 +000040
Stephen Hines651f13c2014-04-23 16:59:28 -070041MachO::MachO(const Driver &D, const llvm::Triple &Triple,
42 const ArgList &Args)
43 : ToolChain(D, Triple, Args) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070044 getProgramPaths().push_back(getDriver().getInstalledDir());
45 if (getDriver().getInstalledDir() != getDriver().Dir)
46 getProgramPaths().push_back(getDriver().Dir);
47
48 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
49 getProgramPaths().push_back(getDriver().getInstalledDir());
50 if (getDriver().getInstalledDir() != getDriver().Dir)
51 getProgramPaths().push_back(getDriver().Dir);
Stephen Hines651f13c2014-04-23 16:59:28 -070052}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000053
Stephen Hines651f13c2014-04-23 16:59:28 -070054/// Darwin - Darwin tool chain for i386 and x86_64.
55Darwin::Darwin(const Driver & D, const llvm::Triple & Triple,
56 const ArgList & Args)
57 : MachO(D, Triple, Args), TargetInitialized(false) {
Bob Wilson10853772012-01-31 21:30:03 +000058 // Compute the initial Darwin version from the triple
59 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-01-31 22:43:59 +000060 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
61 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
62 Triple.getOSName();
63 llvm::raw_string_ostream(MacosxVersionMin)
64 << Major << '.' << Minor << '.' << Micro;
65
Bob Wilson10853772012-01-31 21:30:03 +000066 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
67 // It should be removed when we stop supporting that.
68 DarwinVersion[0] = Minor + 4;
69 DarwinVersion[1] = Micro;
70 DarwinVersion[2] = 0;
Chad Rosierc793ea42012-05-09 18:46:30 +000071
72 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000073 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000074 llvm::raw_string_ostream(iOSVersionMin)
75 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000076}
77
Stephen Hines651f13c2014-04-23 16:59:28 -070078types::ID MachO::LookupTypeForExtension(const char *Ext) const {
Daniel Dunbar41800112010-08-02 05:43:56 +000079 types::ID Ty = types::lookupTypeForExtension(Ext);
80
81 // Darwin always preprocesses assembly files (unless -x is used explicitly).
82 if (Ty == types::TY_PP_Asm)
83 return types::TY_Asm;
84
85 return Ty;
86}
87
Stephen Hines651f13c2014-04-23 16:59:28 -070088bool MachO::HasNativeLLVMSupport() const {
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000089 return true;
90}
91
John McCall9f084a32011-07-06 00:26:06 +000092/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall260611a2012-06-20 06:18:46 +000093ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
Stephen Hines651f13c2014-04-23 16:59:28 -070094 if (isTargetIOSBased())
John McCall260611a2012-06-20 06:18:46 +000095 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson377e5c12012-11-09 01:59:30 +000096 if (isNonFragile)
97 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
98 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall9f084a32011-07-06 00:26:06 +000099}
100
John McCall13db5cf2011-09-09 20:41:01 +0000101/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
102bool Darwin::hasBlocksRuntime() const {
Stephen Hines651f13c2014-04-23 16:59:28 -0700103 if (isTargetIOSBased())
John McCall13db5cf2011-09-09 20:41:01 +0000104 return !isIPhoneOSVersionLT(3, 2);
Stephen Hines651f13c2014-04-23 16:59:28 -0700105 else {
106 assert(isTargetMacOS() && "unexpected darwin target");
John McCall13db5cf2011-09-09 20:41:01 +0000107 return !isMacosxVersionLT(10, 6);
Stephen Hines651f13c2014-04-23 16:59:28 -0700108 }
John McCall13db5cf2011-09-09 20:41:01 +0000109}
110
Chris Lattner5f9e2722011-07-23 10:55:15 +0000111static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000112 return llvm::StringSwitch<const char*>(Value)
113 .Case("armv6k", "armv6")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000114 .Case("armv6m", "armv6m")
Bob Wilsona59956b2011-10-07 00:37:57 +0000115 .Case("armv5tej", "armv5")
116 .Case("xscale", "xscale")
117 .Case("armv4t", "armv4t")
118 .Case("armv7", "armv7")
119 .Cases("armv7a", "armv7-a", "armv7")
120 .Cases("armv7r", "armv7-r", "armv7")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000121 .Cases("armv7em", "armv7e-m", "armv7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000122 .Cases("armv7k", "armv7-k", "armv7k")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000123 .Cases("armv7m", "armv7-m", "armv7m")
Bob Wilson336bfa32012-09-29 23:52:50 +0000124 .Cases("armv7s", "armv7-s", "armv7s")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700125 .Default(nullptr);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000126}
127
Chris Lattner5f9e2722011-07-23 10:55:15 +0000128static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000129 return llvm::StringSwitch<const char *>(Value)
130 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
131 .Cases("arm10e", "arm10tdmi", "armv5")
132 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
133 .Case("xscale", "xscale")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000134 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6")
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700135 .Cases("sc000", "cortex-m0", "cortex-m0plus", "cortex-m1", "armv6m")
Stephen Hines176edba2014-12-01 14:53:08 -0800136 .Cases("cortex-a5", "cortex-a7", "cortex-a8", "armv7")
137 .Cases("cortex-a9", "cortex-a12", "cortex-a15", "cortex-a17", "krait", "armv7")
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700138 .Cases("cortex-r4", "cortex-r5", "cortex-r7", "armv7r")
139 .Cases("sc300", "cortex-m3", "armv7m")
Stephen Hines176edba2014-12-01 14:53:08 -0800140 .Cases("cortex-m4", "cortex-m7", "armv7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000141 .Case("swift", "armv7s")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700142 .Default(nullptr);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000143}
144
Stephen Hines651f13c2014-04-23 16:59:28 -0700145static bool isSoftFloatABI(const ArgList &Args) {
146 Arg *A = Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
147 options::OPT_mfloat_abi_EQ);
148 if (!A)
149 return false;
Stephen Hines176edba2014-12-01 14:53:08 -0800150
Stephen Hines651f13c2014-04-23 16:59:28 -0700151 return A->getOption().matches(options::OPT_msoft_float) ||
152 (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
153 A->getValue() == StringRef("soft"));
154}
155
156StringRef MachO::getMachOArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000157 switch (getTriple().getArch()) {
158 default:
Stephen Hines176edba2014-12-01 14:53:08 -0800159 return getDefaultUniversalArchName();
160
161 case llvm::Triple::aarch64:
162 return "arm64";
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000163
Douglas Gregorf0594d82011-03-06 19:11:49 +0000164 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000165 case llvm::Triple::arm: {
166 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000167 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000168 return Arch;
169
170 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000171 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000172 return Arch;
173
174 return "arm";
175 }
176 }
177}
178
Daniel Dunbarf3955282009-09-04 18:34:51 +0000179Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000180}
181
Stephen Hines651f13c2014-04-23 16:59:28 -0700182MachO::~MachO() {
183}
184
185
186std::string MachO::ComputeEffectiveClangTriple(const ArgList &Args,
187 types::ID InputType) const {
188 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
189
190 return Triple.getTriple();
191}
192
Chad Rosier61ab80a2011-09-20 20:44:06 +0000193std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
194 types::ID InputType) const {
195 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000196
197 // If the target isn't initialized (e.g., an unknown Darwin platform, return
198 // the default triple).
199 if (!isTargetInitialized())
200 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000201
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 SmallString<16> Str;
203 Str += isTargetIOSBased() ? "ios" : "macosx";
204 Str += getTargetVersion().getAsString();
205 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000206
207 return Triple.getTriple();
208}
209
David Blaikie99ba9e32011-12-20 02:48:34 +0000210void Generic_ELF::anchor() {}
211
Stephen Hines651f13c2014-04-23 16:59:28 -0700212Tool *MachO::getTool(Action::ActionClass AC) const {
Rafael Espindola5b222052013-03-18 20:48:54 +0000213 switch (AC) {
Rafael Espindolaf3260562013-03-18 18:50:01 +0000214 case Action::LipoJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000215 if (!Lipo)
216 Lipo.reset(new tools::darwin::Lipo(*this));
217 return Lipo.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +0000218 case Action::DsymutilJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000219 if (!Dsymutil)
220 Dsymutil.reset(new tools::darwin::Dsymutil(*this));
221 return Dsymutil.get();
Stephen Hines651f13c2014-04-23 16:59:28 -0700222 case Action::VerifyDebugInfoJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000223 if (!VerifyDebug)
224 VerifyDebug.reset(new tools::darwin::VerifyDebug(*this));
225 return VerifyDebug.get();
Rafael Espindolac0a55d12013-03-19 00:36:57 +0000226 default:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000227 return ToolChain::getTool(AC);
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000228 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000229}
230
Stephen Hines651f13c2014-04-23 16:59:28 -0700231Tool *MachO::buildLinker() const {
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000232 return new tools::darwin::Link(*this);
233}
234
Stephen Hines651f13c2014-04-23 16:59:28 -0700235Tool *MachO::buildAssembler() const {
Rafael Espindolaf48b93c2013-03-20 03:05:54 +0000236 return new tools::darwin::Assemble(*this);
237}
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000238
Rafael Espindolaaf370e62013-03-18 18:10:27 +0000239DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple,
240 const ArgList &Args)
Stephen Hines651f13c2014-04-23 16:59:28 -0700241 : Darwin(D, Triple, Args) {
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000242}
243
Stephen Hines651f13c2014-04-23 16:59:28 -0700244void DarwinClang::addClangWarningOptions(ArgStringList &CC1Args) const {
245 // For iOS, 64-bit, promote certain warnings to errors.
246 if (!isTargetMacOS() && getTriple().isArch64Bit()) {
247 // Always enable -Wdeprecated-objc-isa-usage and promote it
248 // to an error.
249 CC1Args.push_back("-Wdeprecated-objc-isa-usage");
250 CC1Args.push_back("-Werror=deprecated-objc-isa-usage");
251
252 // Also error about implicit function declarations, as that
253 // can impact calling conventions.
254 CC1Args.push_back("-Werror=implicit-function-declaration");
255 }
256}
257
258/// \brief Determine whether Objective-C automated reference counting is
259/// enabled.
260static bool isObjCAutoRefCount(const ArgList &Args) {
261 return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false);
262}
263
John McCallf85e1932011-06-15 23:02:42 +0000264void DarwinClang::AddLinkARCArgs(const ArgList &Args,
265 ArgStringList &CmdArgs) const {
Stephen Hines651f13c2014-04-23 16:59:28 -0700266 // Avoid linking compatibility stubs on i386 mac.
267 if (isTargetMacOS() && getArch() == llvm::Triple::x86)
268 return;
269
270 ObjCRuntime runtime = getDefaultObjCRuntime(/*nonfragile*/ true);
271
272 if ((runtime.hasNativeARC() || !isObjCAutoRefCount(Args)) &&
273 runtime.hasSubscripting())
274 return;
Eric Christopherf8571862011-08-23 17:56:55 +0000275
276 CmdArgs.push_back("-force_load");
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000277 SmallString<128> P(getDriver().ClangExecutable);
278 llvm::sys::path::remove_filename(P); // 'clang'
279 llvm::sys::path::remove_filename(P); // 'bin'
Benjamin Kramerceb6dc82013-06-28 16:25:46 +0000280 llvm::sys::path::append(P, "lib", "arc", "libarclite_");
John McCallf85e1932011-06-15 23:02:42 +0000281 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000282 if (isTargetIOSSimulator())
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000283 P += "iphonesimulator";
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000284 else if (isTargetIPhoneOS())
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000285 P += "iphoneos";
John McCallf85e1932011-06-15 23:02:42 +0000286 else
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000287 P += "macosx";
288 P += ".a";
John McCallf85e1932011-06-15 23:02:42 +0000289
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000290 CmdArgs.push_back(Args.MakeArgString(P));
John McCallf85e1932011-06-15 23:02:42 +0000291}
292
Stephen Hines651f13c2014-04-23 16:59:28 -0700293void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
Stephen Hines176edba2014-12-01 14:53:08 -0800294 StringRef DarwinLibName, bool AlwaysLink,
295 bool IsEmbedded, bool AddRPath) const {
296 SmallString<128> Dir(getDriver().ResourceDir);
297 llvm::sys::path::append(Dir, "lib", IsEmbedded ? "macho_embedded" : "darwin");
298
299 SmallString<128> P(Dir);
300 llvm::sys::path::append(P, DarwinLibName);
Eric Christopherf8571862011-08-23 17:56:55 +0000301
Eric Christopher3404fe72011-06-22 17:41:40 +0000302 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000303 // not have compiler-rt checked out or integrated into their build (unless
304 // we explicitly force linking with this library).
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700305 if (AlwaysLink || llvm::sys::fs::exists(P))
306 CmdArgs.push_back(Args.MakeArgString(P));
Stephen Hines176edba2014-12-01 14:53:08 -0800307
308 // Adding the rpaths might negatively interact when other rpaths are involved,
309 // so we should make sure we add the rpaths last, after all user-specified
310 // rpaths. This is currently true from this place, but we need to be
311 // careful if this function is ever called before user's rpaths are emitted.
312 if (AddRPath) {
313 assert(DarwinLibName.endswith(".dylib") && "must be a dynamic library");
314
315 // Add @executable_path to rpath to support having the dylib copied with
316 // the executable.
317 CmdArgs.push_back("-rpath");
318 CmdArgs.push_back("@executable_path");
319
320 // Add the path to the resource dir to rpath to support using the dylib
321 // from the default location without copying.
322 CmdArgs.push_back("-rpath");
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700323 CmdArgs.push_back(Args.MakeArgString(Dir));
Stephen Hines176edba2014-12-01 14:53:08 -0800324 }
Eric Christopher3404fe72011-06-22 17:41:40 +0000325}
326
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700327void DarwinClang::AddLinkSanitizerLibArgs(const ArgList &Args,
328 ArgStringList &CmdArgs,
329 StringRef Sanitizer) const {
330 if (!Args.hasArg(options::OPT_dynamiclib) &&
331 !Args.hasArg(options::OPT_bundle)) {
332 // Sanitizer runtime libraries requires C++.
333 AddCXXStdlibLibArgs(Args, CmdArgs);
334 }
335 assert(isTargetMacOS() || isTargetIOSSimulator());
336 StringRef OS = isTargetMacOS() ? "osx" : "iossim";
337 AddLinkRuntimeLib(Args, CmdArgs, (Twine("libclang_rt.") + Sanitizer + "_" +
338 OS + "_dynamic.dylib").str(),
339 /*AlwaysLink*/ true, /*IsEmbedded*/ false,
340 /*AddRPath*/ true);
341}
342
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000343void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
344 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000345 // Darwin only supports the compiler-rt based runtime libraries.
346 switch (GetRuntimeLibType(Args)) {
347 case ToolChain::RLT_CompilerRT:
348 break;
349 default:
350 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000351 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000352 return;
353 }
354
Daniel Dunbareec99102010-01-22 03:38:14 +0000355 // Darwin doesn't support real static executables, don't link any runtime
356 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000357 if (Args.hasArg(options::OPT_static) ||
358 Args.hasArg(options::OPT_fapple_kext) ||
359 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000360 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000361
362 // Reject -static-libgcc for now, we can deal with this when and if someone
363 // cares. This is useful in situations where someone wants to statically link
364 // something like libstdc++, and needs its runtime support routines.
365 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000366 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000367 << A->getAsString(Args);
368 return;
369 }
370
Daniel Dunbarf4714872011-11-17 00:36:57 +0000371 // If we are building profile support, link that library in.
Stephen Hines176edba2014-12-01 14:53:08 -0800372 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
373 false) ||
Daniel Dunbarf4714872011-11-17 00:36:57 +0000374 Args.hasArg(options::OPT_fprofile_generate) ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700375 Args.hasArg(options::OPT_fprofile_instr_generate) ||
Daniel Dunbarf4714872011-11-17 00:36:57 +0000376 Args.hasArg(options::OPT_fcreate_profile) ||
377 Args.hasArg(options::OPT_coverage)) {
378 // Select the appropriate runtime library for the target.
Stephen Hines651f13c2014-04-23 16:59:28 -0700379 if (isTargetIOSBased())
Daniel Dunbarf4714872011-11-17 00:36:57 +0000380 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
Stephen Hines651f13c2014-04-23 16:59:28 -0700381 else
Daniel Dunbarf4714872011-11-17 00:36:57 +0000382 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
Daniel Dunbarf4714872011-11-17 00:36:57 +0000383 }
384
Peter Collingbournec6911a22013-11-01 18:16:25 +0000385 const SanitizerArgs &Sanitize = getSanitizerArgs();
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000386
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700387 if (Sanitize.needsAsanRt()) {
388 if (!isTargetMacOS() && !isTargetIOSSimulator()) {
389 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000390 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700391 << "-fsanitize=address";
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000392 } else {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700393 AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000394 }
395 }
396
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700397 if (Sanitize.needsUbsanRt()) {
398 if (!isTargetMacOS() && !isTargetIOSSimulator()) {
399 // FIXME: Move this check to SanitizerArgs::filterUnsupportedKinds.
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000400 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700401 << "-fsanitize=undefined";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000402 } else {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700403 AddLinkSanitizerLibArgs(Args, CmdArgs, "ubsan");
404 // Add explicit dependcy on -lc++abi, as -lc++ doesn't re-export
405 // all RTTI-related symbols that UBSan uses.
406 CmdArgs.push_back("-lc++abi");
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000407 }
408 }
409
Daniel Dunbareec99102010-01-22 03:38:14 +0000410 // Otherwise link libSystem, then the dynamic runtime library, and finally any
411 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000412 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000413
414 // Select the dynamic runtime library and the target specific static library.
Stephen Hines651f13c2014-04-23 16:59:28 -0700415 if (isTargetIOSBased()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000416 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
417 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000418 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
Stephen Hines651f13c2014-04-23 16:59:28 -0700419 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator() &&
Stephen Hines176edba2014-12-01 14:53:08 -0800420 getTriple().getArch() != llvm::Triple::aarch64)
Bob Wilson163b1512011-10-07 17:54:41 +0000421 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000422
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000423 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000424 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000425 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -0700426 assert(isTargetMacOS() && "unexpected non MacOS platform");
Daniel Dunbareec99102010-01-22 03:38:14 +0000427 // The dynamic runtime library was merged with libSystem for 10.6 and
428 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000429 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000430 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000431 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000432 CmdArgs.push_back("-lgcc_s.10.5");
433
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000434 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000435 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000436 // omitted from 10.4.dylib.
437 //
438 // Unfortunately, that turned out to not be true, because Darwin system
439 // headers can still use eprintf on i386, and it is not exported from
440 // libSystem. Therefore, we still must provide a runtime library just for
441 // the tiny tiny handful of projects that *might* use that symbol.
442 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000443 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000444 } else {
445 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000446 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
447 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000448 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000449 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000450}
451
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000452void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000453 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000454
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000455 // Support allowing the SDKROOT environment variable used by xcrun and other
456 // Xcode tools to define the default sysroot, by making it the default for
457 // isysroot.
Chad Rosierd7dfd982012-12-19 23:41:50 +0000458 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
459 // Warn if the path does not exist.
Rafael Espindola40e6b302013-06-25 15:14:22 +0000460 if (!llvm::sys::fs::exists(A->getValue()))
Chad Rosierd7dfd982012-12-19 23:41:50 +0000461 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
462 } else {
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000463 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbar32142542013-01-15 20:33:56 +0000464 // We only use this value as the default if it is an absolute path,
465 // exists, and it is not the root path.
466 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
467 StringRef(env) != "/") {
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000468 Args.append(Args.MakeSeparateArg(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700469 nullptr, Opts.getOption(options::OPT_isysroot), env));
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000470 }
471 }
472 }
473
Daniel Dunbar26031372010-01-27 00:56:25 +0000474 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000475 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000476
Stephen Hines176edba2014-12-01 14:53:08 -0800477 if (OSXVersion && iOSVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000478 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000479 << OSXVersion->getAsString(Args)
Stephen Hines176edba2014-12-01 14:53:08 -0800480 << iOSVersion->getAsString(Args);
481 iOSVersion = nullptr;
482 } else if (!OSXVersion && !iOSVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000483 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000484 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000485 StringRef OSXTarget;
486 StringRef iOSTarget;
Chad Rosiera4884972011-08-31 20:56:25 +0000487 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
488 OSXTarget = env;
489 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
490 iOSTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000491
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000492 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000493 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000494 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000495 if (iOSTarget.empty()) {
496 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
497 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000498 StringRef isysroot = A->getValue();
Stephen Hines651f13c2014-04-23 16:59:28 -0700499 std::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
Chad Rosiera4884972011-08-31 20:56:25 +0000500 if (second != "")
501 iOSTarget = second.substr(0,3);
502 }
503 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000504
Chad Rosier4f8de272011-09-28 00:46:32 +0000505 // If no OSX or iOS target has been specified and we're compiling for armv7,
506 // go ahead as assume we're targeting iOS.
Stephen Hines651f13c2014-04-23 16:59:28 -0700507 StringRef MachOArchName = getMachOArchName(Args);
Chad Rosier49033202012-05-09 18:55:57 +0000508 if (OSXTarget.empty() && iOSTarget.empty() &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700509 (MachOArchName == "armv7" || MachOArchName == "armv7s" ||
510 MachOArchName == "arm64"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000511 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000512
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000513 // Allow conflicts among OSX and iOS for historical reasons, but choose the
514 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000515 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000516 if (getTriple().getArch() == llvm::Triple::arm ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700517 getTriple().getArch() == llvm::Triple::aarch64 ||
Daniel Dunbar39053672010-02-02 17:31:12 +0000518 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000519 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000520 else
Chad Rosiera4884972011-08-31 20:56:25 +0000521 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000522 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000523
Chad Rosiera4884972011-08-31 20:56:25 +0000524 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000525 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700526 OSXVersion = Args.MakeJoinedArg(nullptr, O, OSXTarget);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000527 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000528 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000529 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700530 iOSVersion = Args.MakeJoinedArg(nullptr, O, iOSTarget);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000531 Args.append(iOSVersion);
Stephen Hines651f13c2014-04-23 16:59:28 -0700532 } else if (MachOArchName != "armv6m" && MachOArchName != "armv7m" &&
533 MachOArchName != "armv7em") {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000534 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000535 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700536 OSXVersion = Args.MakeJoinedArg(nullptr, O, MacosxVersionMin);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000537 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000538 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Stephen Hines651f13c2014-04-23 16:59:28 -0700541 DarwinPlatformKind Platform;
542 if (OSXVersion)
543 Platform = MacOS;
544 else if (iOSVersion)
545 Platform = IPhoneOS;
Stephen Hines651f13c2014-04-23 16:59:28 -0700546 else
547 llvm_unreachable("Unable to infer Darwin variant");
548
Daniel Dunbar26031372010-01-27 00:56:25 +0000549 // Set the tool chain target information.
550 unsigned Major, Minor, Micro;
551 bool HadExtra;
Stephen Hines651f13c2014-04-23 16:59:28 -0700552 if (Platform == MacOS) {
Stephen Hines176edba2014-12-01 14:53:08 -0800553 assert(!iOSVersion && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000554 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000555 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000556 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000557 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000558 << OSXVersion->getAsString(Args);
Stephen Hines176edba2014-12-01 14:53:08 -0800559 } else if (Platform == IPhoneOS) {
560 assert(iOSVersion && "Unknown target platform!");
561 if (!Driver::GetReleaseVersion(iOSVersion->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000562 Micro, HadExtra) || HadExtra ||
563 Major >= 10 || Minor >= 100 || Micro >= 100)
564 getDriver().Diag(diag::err_drv_invalid_version_number)
Stephen Hines176edba2014-12-01 14:53:08 -0800565 << iOSVersion->getAsString(Args);
Stephen Hines651f13c2014-04-23 16:59:28 -0700566 } else
567 llvm_unreachable("unknown kind of Darwin platform");
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000568
Stephen Hines176edba2014-12-01 14:53:08 -0800569 // Recognize iOS targets with an x86 architecture as the iOS simulator.
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000570 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
571 getTriple().getArch() == llvm::Triple::x86_64))
Stephen Hines651f13c2014-04-23 16:59:28 -0700572 Platform = IPhoneOSSimulator;
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000573
Stephen Hines651f13c2014-04-23 16:59:28 -0700574 setTarget(Platform, Major, Minor, Micro);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000575}
576
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000577void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000578 ArgStringList &CmdArgs) const {
579 CXXStdlibType Type = GetCXXStdlibType(Args);
580
581 switch (Type) {
582 case ToolChain::CST_Libcxx:
583 CmdArgs.push_back("-lc++");
584 break;
585
586 case ToolChain::CST_Libstdcxx: {
587 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
588 // it was previously found in the gcc lib dir. However, for all the Darwin
589 // platforms we care about it was -lstdc++.6, so we search for that
590 // explicitly if we can't see an obvious -lstdc++ candidate.
591
592 // Check in the sysroot first.
593 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000594 SmallString<128> P(A->getValue());
Benjamin Kramerceb6dc82013-06-28 16:25:46 +0000595 llvm::sys::path::append(P, "usr", "lib", "libstdc++.dylib");
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000596
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700597 if (!llvm::sys::fs::exists(P)) {
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000598 llvm::sys::path::remove_filename(P);
599 llvm::sys::path::append(P, "libstdc++.6.dylib");
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700600 if (llvm::sys::fs::exists(P)) {
601 CmdArgs.push_back(Args.MakeArgString(P));
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000602 return;
603 }
604 }
605 }
606
607 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000608 // FIXME: This should be removed someday when we don't have to care about
609 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Rafael Espindola40e6b302013-06-25 15:14:22 +0000610 if (!llvm::sys::fs::exists("/usr/lib/libstdc++.dylib") &&
611 llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib")) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000612 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
613 return;
614 }
615
616 // Otherwise, let the linker search.
617 CmdArgs.push_back("-lstdc++");
618 break;
619 }
620 }
621}
622
Shantonu Sen7433fed2010-09-17 18:39:08 +0000623void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
624 ArgStringList &CmdArgs) const {
625
626 // For Darwin platforms, use the compiler-rt-based support library
627 // instead of the gcc-provided one (which is also incidentally
628 // only present in the gcc lib dir, which makes it hard to find).
629
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000630 SmallString<128> P(getDriver().ResourceDir);
Benjamin Kramerceb6dc82013-06-28 16:25:46 +0000631 llvm::sys::path::append(P, "lib", "darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000632
633 // Use the newer cc_kext for iOS ARM after 6.0.
634 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700635 getTriple().getArch() == llvm::Triple::aarch64 ||
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000636 !isIPhoneOSVersionLT(6, 0)) {
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000637 llvm::sys::path::append(P, "libclang_rt.cc_kext.a");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000638 } else {
Rafael Espindola7b6301f2013-06-26 02:13:00 +0000639 llvm::sys::path::append(P, "libclang_rt.cc_kext_ios5.a");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000640 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000641
Shantonu Sen7433fed2010-09-17 18:39:08 +0000642 // For now, allow missing resource libraries to support developers who may
643 // not have compiler-rt checked out or integrated into their build.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700644 if (llvm::sys::fs::exists(P))
645 CmdArgs.push_back(Args.MakeArgString(P));
Shantonu Sen7433fed2010-09-17 18:39:08 +0000646}
647
Stephen Hines651f13c2014-04-23 16:59:28 -0700648DerivedArgList *MachO::TranslateArgs(const DerivedArgList &Args,
649 const char *BoundArch) const {
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000650 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
651 const OptTable &Opts = getDriver().getOpts();
652
653 // FIXME: We really want to get out of the tool chain level argument
654 // translation business, as it makes the driver functionality much
655 // more opaque. For now, we follow gcc closely solely for the
656 // purpose of easily achieving feature parity & testability. Once we
657 // have something that works, we should reevaluate each translation
658 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000659
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700660 for (Arg *A : Args) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000661 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000662 // Skip this argument unless the architecture matches either the toolchain
663 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000664 llvm::Triple::ArchType XarchArch =
Stephen Hines651f13c2014-04-23 16:59:28 -0700665 tools::darwin::getArchTypeForMachOArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000666 if (!(XarchArch == getArch() ||
667 (BoundArch && XarchArch ==
Stephen Hines651f13c2014-04-23 16:59:28 -0700668 tools::darwin::getArchTypeForMachOArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000669 continue;
670
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000671 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000672 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000673 unsigned Prev = Index;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700674 std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000676 // If the argument parsing failed or more than one argument was
677 // consumed, the -Xarch_ argument's parameter tried to consume
678 // extra arguments. Emit an error and ignore.
679 //
680 // We also want to disallow any options which would alter the
681 // driver behavior; that isn't going to work in our model. We
682 // use isDriverOption() as an approximation, although things
683 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000684 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000685 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000686 << A->getAsString(Args);
687 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000688 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000689 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000690 << A->getAsString(Args);
691 continue;
692 }
693
Daniel Dunbar478edc22009-03-29 22:29:05 +0000694 XarchArg->setBaseArg(A);
Daniel Dunbar0e100312010-06-14 21:23:08 +0000695
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700696 A = XarchArg.release();
Daniel Dunbar0e100312010-06-14 21:23:08 +0000697 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000698
699 // Linker input arguments require custom handling. The problem is that we
700 // have already constructed the phase actions, so we can not treat them as
701 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000702 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000703 // Convert the argument into individual Zlinker_input_args.
704 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
705 DAL->AddSeparateArg(OriginalArg,
706 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000707 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000708
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000709 }
710 continue;
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000713
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000714 // Sob. These is strictly gcc compatible for the time being. Apple
715 // gcc translates options twice, which means that self-expanding
716 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000717 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000718 default:
719 DAL->append(A);
720 break;
721
722 case options::OPT_mkernel:
723 case options::OPT_fapple_kext:
724 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000725 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000726 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000728 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000729 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000730 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000731 break;
732
733 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000734 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
735 DAL->AddFlagArg(A,
736 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000737 break;
738
739 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000740 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
741 DAL->AddFlagArg(A,
742 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000743 break;
744
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000745 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000746 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000747 break;
748
749 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000750 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000751 break;
752
753 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000754 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000755 break;
756
757 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000758 DAL->AddFlagArg(A,
759 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000760 break;
761
762 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000763 DAL->AddFlagArg(A,
764 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000765 break;
766
767 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000768 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000769 break;
770
771 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000772 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000773 break;
774 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000775 }
776
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000777 if (getTriple().getArch() == llvm::Triple::x86 ||
778 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000779 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700780 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mtune_EQ),
781 "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000782
783 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000784 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000785 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000786 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000787 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
788 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000789
790 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
791 // which defines the list of which architectures we accept.
792 if (Name == "ppc")
793 ;
794 else if (Name == "ppc601")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700795 DAL->AddJoinedArg(nullptr, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000796 else if (Name == "ppc603")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700797 DAL->AddJoinedArg(nullptr, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000798 else if (Name == "ppc604")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700799 DAL->AddJoinedArg(nullptr, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000800 else if (Name == "ppc604e")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700801 DAL->AddJoinedArg(nullptr, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000802 else if (Name == "ppc750")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700803 DAL->AddJoinedArg(nullptr, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000804 else if (Name == "ppc7400")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700805 DAL->AddJoinedArg(nullptr, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000806 else if (Name == "ppc7450")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700807 DAL->AddJoinedArg(nullptr, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000808 else if (Name == "ppc970")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700809 DAL->AddJoinedArg(nullptr, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000810
Bill Schmidtea7fb0c2013-07-26 01:36:11 +0000811 else if (Name == "ppc64" || Name == "ppc64le")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700812 DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000813
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000814 else if (Name == "i386")
815 ;
816 else if (Name == "i486")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700817 DAL->AddJoinedArg(nullptr, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000818 else if (Name == "i586")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700819 DAL->AddJoinedArg(nullptr, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000820 else if (Name == "i686")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700821 DAL->AddJoinedArg(nullptr, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000822 else if (Name == "pentium")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700823 DAL->AddJoinedArg(nullptr, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000824 else if (Name == "pentium2")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700825 DAL->AddJoinedArg(nullptr, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000826 else if (Name == "pentpro")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700827 DAL->AddJoinedArg(nullptr, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000828 else if (Name == "pentIIm3")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700829 DAL->AddJoinedArg(nullptr, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000830
831 else if (Name == "x86_64")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700832 DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64));
Jim Grosbach32ca73e2013-11-16 00:53:35 +0000833 else if (Name == "x86_64h") {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700834 DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64));
835 DAL->AddJoinedArg(nullptr, MArch, "x86_64h");
Jim Grosbach32ca73e2013-11-16 00:53:35 +0000836 }
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000837
838 else if (Name == "arm")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700839 DAL->AddJoinedArg(nullptr, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000840 else if (Name == "armv4t")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700841 DAL->AddJoinedArg(nullptr, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000842 else if (Name == "armv5")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700843 DAL->AddJoinedArg(nullptr, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000844 else if (Name == "xscale")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700845 DAL->AddJoinedArg(nullptr, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000846 else if (Name == "armv6")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700847 DAL->AddJoinedArg(nullptr, MArch, "armv6k");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000848 else if (Name == "armv6m")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700849 DAL->AddJoinedArg(nullptr, MArch, "armv6m");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000850 else if (Name == "armv7")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700851 DAL->AddJoinedArg(nullptr, MArch, "armv7a");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000852 else if (Name == "armv7em")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700853 DAL->AddJoinedArg(nullptr, MArch, "armv7em");
Bob Wilson336bfa32012-09-29 23:52:50 +0000854 else if (Name == "armv7k")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700855 DAL->AddJoinedArg(nullptr, MArch, "armv7k");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000856 else if (Name == "armv7m")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700857 DAL->AddJoinedArg(nullptr, MArch, "armv7m");
Bob Wilson336bfa32012-09-29 23:52:50 +0000858 else if (Name == "armv7s")
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700859 DAL->AddJoinedArg(nullptr, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000860 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000861
Stephen Hines651f13c2014-04-23 16:59:28 -0700862 return DAL;
863}
864
865void MachO::AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args,
866 llvm::opt::ArgStringList &CmdArgs) const {
867 // Embedded targets are simple at the moment, not supporting sanitizers and
868 // with different libraries for each member of the product { static, PIC } x
869 // { hard-float, soft-float }
870 llvm::SmallString<32> CompilerRT = StringRef("libclang_rt.");
871 CompilerRT +=
872 tools::arm::getARMFloatABI(getDriver(), Args, getTriple()) == "hard"
873 ? "hard"
874 : "soft";
875 CompilerRT += Args.hasArg(options::OPT_fPIC) ? "_pic.a" : "_static.a";
876
877 AddLinkRuntimeLib(Args, CmdArgs, CompilerRT, false, true);
878}
879
880
881DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
882 const char *BoundArch) const {
883 // First get the generic Apple args, before moving onto Darwin-specific ones.
884 DerivedArgList *DAL = MachO::TranslateArgs(Args, BoundArch);
885 const OptTable &Opts = getDriver().getOpts();
886
887 // If no architecture is bound, none of the translations here are relevant.
888 if (!BoundArch)
889 return DAL;
890
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000891 // Add an explicit version min argument for the deployment target. We do this
892 // after argument translation because -Xarch_ arguments may add a version min
893 // argument.
Stephen Hines651f13c2014-04-23 16:59:28 -0700894 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000895
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000896 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
897 // FIXME: It would be far better to avoid inserting those -static arguments,
898 // but we can't check the deployment target in the translation code until
899 // it is set here.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700900 if (isTargetIOSBased() && !isIPhoneOSVersionLT(6, 0)) {
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000901 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
902 Arg *A = *it;
903 ++it;
904 if (A->getOption().getID() != options::OPT_mkernel &&
905 A->getOption().getID() != options::OPT_fapple_kext)
906 continue;
907 assert(it != ie && "unexpected argument translation");
908 A = *it;
909 assert(A->getOption().getID() == options::OPT_static &&
910 "missing expected -static argument");
911 it = DAL->getArgs().erase(it);
912 }
913 }
914
Bob Wilson66dbb3f2013-11-02 23:19:53 +0000915 // Default to use libc++ on OS X 10.9+ and iOS 7+.
916 if (((isTargetMacOS() && !isMacosxVersionLT(10, 9)) ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700917 (isTargetIOSBased() && !isIPhoneOSVersionLT(7, 0))) &&
Bob Wilson66dbb3f2013-11-02 23:19:53 +0000918 !Args.getLastArg(options::OPT_stdlib_EQ))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700919 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_stdlib_EQ),
920 "libc++");
Bob Wilson66dbb3f2013-11-02 23:19:53 +0000921
Bob Wilson163b1512011-10-07 17:54:41 +0000922 // Validate the C++ standard library choice.
923 CXXStdlibType Type = GetCXXStdlibType(*DAL);
924 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000925 // Check whether the target provides libc++.
926 StringRef where;
927
Stephen Hines651f13c2014-04-23 16:59:28 -0700928 // Complain about targeting iOS < 5.0 in any way.
929 if (isTargetIOSBased() && isIPhoneOSVersionLT(5, 0))
Bob Wilson377e5c12012-11-09 01:59:30 +0000930 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000931
932 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000933 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000934 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000935 }
936 }
937
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000938 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000939}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000940
Stephen Hines651f13c2014-04-23 16:59:28 -0700941bool MachO::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000942 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000943}
944
Stephen Hines651f13c2014-04-23 16:59:28 -0700945bool MachO::UseDwarfDebugFlags() const {
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000946 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
947 return S[0] != '\0';
948 return false;
949}
950
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000951bool Darwin::UseSjLjExceptions() const {
952 // Darwin uses SjLj exceptions on ARM.
953 return (getTriple().getArch() == llvm::Triple::arm ||
954 getTriple().getArch() == llvm::Triple::thumb);
955}
956
Stephen Hines651f13c2014-04-23 16:59:28 -0700957bool MachO::isPICDefault() const {
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000958 return true;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000959}
960
Stephen Hines651f13c2014-04-23 16:59:28 -0700961bool MachO::isPIEDefault() const {
Peter Collingbourne52ca70d2013-04-09 04:35:11 +0000962 return false;
963}
964
Stephen Hines651f13c2014-04-23 16:59:28 -0700965bool MachO::isPICDefaultForced() const {
966 return (getArch() == llvm::Triple::x86_64 ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700967 getArch() == llvm::Triple::aarch64);
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000968}
969
Stephen Hines651f13c2014-04-23 16:59:28 -0700970bool MachO::SupportsProfiling() const {
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000971 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000972 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000973}
974
Stephen Hines651f13c2014-04-23 16:59:28 -0700975void Darwin::addMinVersionArgs(const llvm::opt::ArgList &Args,
976 llvm::opt::ArgStringList &CmdArgs) const {
977 VersionTuple TargetVersion = getTargetVersion();
978
Stephen Hines176edba2014-12-01 14:53:08 -0800979 if (isTargetIOSSimulator())
Stephen Hines651f13c2014-04-23 16:59:28 -0700980 CmdArgs.push_back("-ios_simulator_version_min");
981 else if (isTargetIOSBased())
982 CmdArgs.push_back("-iphoneos_version_min");
983 else {
984 assert(isTargetMacOS() && "unexpected target");
985 CmdArgs.push_back("-macosx_version_min");
986 }
987
988 CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
989}
990
991void Darwin::addStartObjectFileArgs(const llvm::opt::ArgList &Args,
992 llvm::opt::ArgStringList &CmdArgs) const {
993 // Derived from startfile spec.
994 if (Args.hasArg(options::OPT_dynamiclib)) {
995 // Derived from darwin_dylib1 spec.
996 if (isTargetIOSSimulator()) {
997 ; // iOS simulator does not need dylib1.o.
998 } else if (isTargetIPhoneOS()) {
999 if (isIPhoneOSVersionLT(3, 1))
1000 CmdArgs.push_back("-ldylib1.o");
1001 } else {
1002 if (isMacosxVersionLT(10, 5))
1003 CmdArgs.push_back("-ldylib1.o");
1004 else if (isMacosxVersionLT(10, 6))
1005 CmdArgs.push_back("-ldylib1.10.5.o");
1006 }
1007 } else {
1008 if (Args.hasArg(options::OPT_bundle)) {
1009 if (!Args.hasArg(options::OPT_static)) {
1010 // Derived from darwin_bundle1 spec.
1011 if (isTargetIOSSimulator()) {
1012 ; // iOS simulator does not need bundle1.o.
1013 } else if (isTargetIPhoneOS()) {
1014 if (isIPhoneOSVersionLT(3, 1))
1015 CmdArgs.push_back("-lbundle1.o");
1016 } else {
1017 if (isMacosxVersionLT(10, 6))
1018 CmdArgs.push_back("-lbundle1.o");
1019 }
1020 }
1021 } else {
1022 if (Args.hasArg(options::OPT_pg) && SupportsProfiling()) {
1023 if (Args.hasArg(options::OPT_static) ||
1024 Args.hasArg(options::OPT_object) ||
1025 Args.hasArg(options::OPT_preload)) {
1026 CmdArgs.push_back("-lgcrt0.o");
1027 } else {
1028 CmdArgs.push_back("-lgcrt1.o");
1029
1030 // darwin_crt2 spec is empty.
1031 }
1032 // By default on OS X 10.8 and later, we don't link with a crt1.o
1033 // file and the linker knows to use _main as the entry point. But,
1034 // when compiling with -pg, we need to link with the gcrt1.o file,
1035 // so pass the -no_new_main option to tell the linker to use the
1036 // "start" symbol as the entry point.
1037 if (isTargetMacOS() && !isMacosxVersionLT(10, 8))
1038 CmdArgs.push_back("-no_new_main");
1039 } else {
1040 if (Args.hasArg(options::OPT_static) ||
1041 Args.hasArg(options::OPT_object) ||
1042 Args.hasArg(options::OPT_preload)) {
1043 CmdArgs.push_back("-lcrt0.o");
1044 } else {
1045 // Derived from darwin_crt1 spec.
1046 if (isTargetIOSSimulator()) {
1047 ; // iOS simulator does not need crt1.o.
1048 } else if (isTargetIPhoneOS()) {
Stephen Hines176edba2014-12-01 14:53:08 -08001049 if (getArch() == llvm::Triple::aarch64)
Stephen Hines651f13c2014-04-23 16:59:28 -07001050 ; // iOS does not need any crt1 files for arm64
1051 else if (isIPhoneOSVersionLT(3, 1))
1052 CmdArgs.push_back("-lcrt1.o");
1053 else if (isIPhoneOSVersionLT(6, 0))
1054 CmdArgs.push_back("-lcrt1.3.1.o");
1055 } else {
1056 if (isMacosxVersionLT(10, 5))
1057 CmdArgs.push_back("-lcrt1.o");
1058 else if (isMacosxVersionLT(10, 6))
1059 CmdArgs.push_back("-lcrt1.10.5.o");
1060 else if (isMacosxVersionLT(10, 8))
1061 CmdArgs.push_back("-lcrt1.10.6.o");
1062
1063 // darwin_crt2 spec is empty.
1064 }
1065 }
1066 }
1067 }
1068 }
1069
1070 if (!isTargetIPhoneOS() && Args.hasArg(options::OPT_shared_libgcc) &&
1071 isMacosxVersionLT(10, 5)) {
1072 const char *Str = Args.MakeArgString(GetFilePath("crt3.o"));
1073 CmdArgs.push_back(Str);
1074 }
1075}
1076
Daniel Dunbar43a9b322010-04-10 16:20:23 +00001077bool Darwin::SupportsObjCGC() const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001078 return isTargetMacOS();
Daniel Dunbar43a9b322010-04-10 16:20:23 +00001079}
1080
John McCall0a7dd782012-08-21 02:47:43 +00001081void Darwin::CheckObjCARC() const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001082 if (isTargetIOSBased()|| (isTargetMacOS() && !isMacosxVersionLT(10, 6)))
John McCall0a7dd782012-08-21 02:47:43 +00001083 return;
John McCall80fd37a2012-08-27 01:56:21 +00001084 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +00001085}
1086
Daniel Dunbar39176082009-03-20 00:20:03 +00001087/// Generic_GCC - A tool chain using the 'gcc' command to perform
1088/// all subcommands; this relies on gcc translating the majority of
1089/// command line options.
1090
Chandler Carruth19347ed2011-11-06 23:39:34 +00001091/// \brief Parse a GCCVersion object out of a string of text.
1092///
1093/// This is the primary means of forming GCCVersion objects.
1094/*static*/
1095Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
Chandler Carruth0affc672013-08-26 08:59:53 +00001096 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth19347ed2011-11-06 23:39:34 +00001097 std::pair<StringRef, StringRef> First = VersionText.split('.');
1098 std::pair<StringRef, StringRef> Second = First.second.split('.');
1099
Chandler Carruth0affc672013-08-26 08:59:53 +00001100 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "", "", "" };
Chandler Carruth19347ed2011-11-06 23:39:34 +00001101 if (First.first.getAsInteger(10, GoodVersion.Major) ||
1102 GoodVersion.Major < 0)
1103 return BadVersion;
Chandler Carruth0affc672013-08-26 08:59:53 +00001104 GoodVersion.MajorStr = First.first.str();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001105 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
1106 GoodVersion.Minor < 0)
1107 return BadVersion;
Chandler Carruth0affc672013-08-26 08:59:53 +00001108 GoodVersion.MinorStr = Second.first.str();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001109
1110 // First look for a number prefix and parse that if present. Otherwise just
1111 // stash the entire patch string in the suffix, and leave the number
1112 // unspecified. This covers versions strings such as:
1113 // 4.4
1114 // 4.4.0
1115 // 4.4.x
1116 // 4.4.2-rc4
1117 // 4.4.x-patched
1118 // And retains any patch number it finds.
1119 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1120 if (!PatchText.empty()) {
Will Dietzca1ad502013-01-10 22:20:02 +00001121 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001122 // Try to parse the number and any suffix.
1123 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1124 GoodVersion.Patch < 0)
1125 return BadVersion;
Chandler Carruth0affc672013-08-26 08:59:53 +00001126 GoodVersion.PatchSuffix = PatchText.substr(EndNumber);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001127 }
1128 }
1129
1130 return GoodVersion;
1131}
1132
1133/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
Benjamin Kramered5f28f2013-08-09 17:17:48 +00001134bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor,
1135 int RHSPatch,
1136 StringRef RHSPatchSuffix) const {
1137 if (Major != RHSMajor)
1138 return Major < RHSMajor;
1139 if (Minor != RHSMinor)
1140 return Minor < RHSMinor;
1141 if (Patch != RHSPatch) {
Chandler Carruthf1757652012-12-29 12:01:08 +00001142 // Note that versions without a specified patch sort higher than those with
1143 // a patch.
Benjamin Kramered5f28f2013-08-09 17:17:48 +00001144 if (RHSPatch == -1)
Chandler Carruthf1757652012-12-29 12:01:08 +00001145 return true;
1146 if (Patch == -1)
1147 return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001148
Chandler Carruthf1757652012-12-29 12:01:08 +00001149 // Otherwise just sort on the patch itself.
Benjamin Kramered5f28f2013-08-09 17:17:48 +00001150 return Patch < RHSPatch;
Chandler Carruthf1757652012-12-29 12:01:08 +00001151 }
Benjamin Kramered5f28f2013-08-09 17:17:48 +00001152 if (PatchSuffix != RHSPatchSuffix) {
Chandler Carruthf1757652012-12-29 12:01:08 +00001153 // Sort empty suffixes higher.
Benjamin Kramered5f28f2013-08-09 17:17:48 +00001154 if (RHSPatchSuffix.empty())
Chandler Carruthf1757652012-12-29 12:01:08 +00001155 return true;
1156 if (PatchSuffix.empty())
Chandler Carruth5ba0c8e2012-12-29 13:00:47 +00001157 return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001158
Chandler Carruthf1757652012-12-29 12:01:08 +00001159 // Provide a lexicographic sort to make this a total ordering.
Benjamin Kramered5f28f2013-08-09 17:17:48 +00001160 return PatchSuffix < RHSPatchSuffix;
Chandler Carruthf1757652012-12-29 12:01:08 +00001161 }
1162
1163 // The versions are equal.
Chandler Carruth19347ed2011-11-06 23:39:34 +00001164 return false;
1165}
1166
Stephen Hines651f13c2014-04-23 16:59:28 -07001167static llvm::StringRef getGCCToolchainDir(const ArgList &Args) {
Rafael Espindola0e659592012-02-19 01:38:32 +00001168 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1169 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +00001170 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +00001171 return GCC_INSTALL_PREFIX;
1172}
1173
Stephen Hines651f13c2014-04-23 16:59:28 -07001174/// \brief Initialize a GCCInstallationDetector from the driver.
Chandler Carruth19347ed2011-11-06 23:39:34 +00001175///
1176/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +00001177/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001178///
1179/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1180/// should instead pull the target out of the driver. This is currently
1181/// necessary because the driver doesn't store the final version of the target
1182/// triple.
Stephen Hines651f13c2014-04-23 16:59:28 -07001183void
1184Generic_GCC::GCCInstallationDetector::init(
1185 const Driver &D, const llvm::Triple &TargetTriple, const ArgList &Args) {
Chandler Carruthd79486a2013-06-22 11:35:51 +00001186 llvm::Triple BiarchVariantTriple =
1187 TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
Chandler Carruth9b338a72012-02-13 02:02:09 +00001188 : TargetTriple.get32BitArchVariant();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001189 // The library directories which may contain GCC installations.
Chandler Carruthd79486a2013-06-22 11:35:51 +00001190 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001191 // The compatible GCC triples for this particular architecture.
Stephen Hines176edba2014-12-01 14:53:08 -08001192 SmallVector<StringRef, 16> CandidateTripleAliases;
1193 SmallVector<StringRef, 16> CandidateBiarchTripleAliases;
Chandler Carruthd79486a2013-06-22 11:35:51 +00001194 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs,
1195 CandidateTripleAliases, CandidateBiarchLibDirs,
1196 CandidateBiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001197
1198 // Compute the set of prefixes for our search.
1199 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1200 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001201
Rafael Espindola0e659592012-02-19 01:38:32 +00001202 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1203 if (GCCToolchainDir != "") {
1204 if (GCCToolchainDir.back() == '/')
1205 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001206
Rafael Espindola0e659592012-02-19 01:38:32 +00001207 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001208 } else {
Rafael Espindola69ca5e22013-08-28 23:17:47 +00001209 // If we have a SysRoot, try that first.
1210 if (!D.SysRoot.empty()) {
1211 Prefixes.push_back(D.SysRoot);
1212 Prefixes.push_back(D.SysRoot + "/usr");
1213 }
1214
1215 // Then look for gcc installed alongside clang.
Rafael Espindola353300c2012-02-03 01:01:20 +00001216 Prefixes.push_back(D.InstalledDir + "/..");
Rafael Espindola69ca5e22013-08-28 23:17:47 +00001217
1218 // And finally in /usr.
1219 if (D.SysRoot.empty())
1220 Prefixes.push_back("/usr");
Rafael Espindola353300c2012-02-03 01:01:20 +00001221 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001222
1223 // Loop over the various components which exist and select the best GCC
1224 // installation available. GCC installs are ranked by version number.
1225 Version = GCCVersion::Parse("0.0.0");
1226 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1227 if (!llvm::sys::fs::exists(Prefixes[i]))
1228 continue;
1229 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1230 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1231 if (!llvm::sys::fs::exists(LibDir))
1232 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001233 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Stephen Hines651f13c2014-04-23 16:59:28 -07001234 ScanLibDirForGCCTriple(TargetTriple, Args, LibDir,
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001235 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001236 }
Chandler Carruthd79486a2013-06-22 11:35:51 +00001237 for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) {
1238 const std::string LibDir = Prefixes[i] + CandidateBiarchLibDirs[j].str();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001239 if (!llvm::sys::fs::exists(LibDir))
1240 continue;
Chandler Carruthd79486a2013-06-22 11:35:51 +00001241 for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001242 ++k)
Stephen Hines651f13c2014-04-23 16:59:28 -07001243 ScanLibDirForGCCTriple(TargetTriple, Args, LibDir,
Chandler Carruthd79486a2013-06-22 11:35:51 +00001244 CandidateBiarchTripleAliases[k],
1245 /*NeedsBiarchSuffix=*/ true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001246 }
1247 }
1248}
1249
Chandler Carruth6365ab92013-07-30 17:57:09 +00001250void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001251 for (const auto &InstallPath : CandidateGCCInstallPaths)
1252 OS << "Found candidate GCC installation: " << InstallPath << "\n";
Chandler Carruth6365ab92013-07-30 17:57:09 +00001253
Stephen Hines651f13c2014-04-23 16:59:28 -07001254 if (!GCCInstallPath.empty())
1255 OS << "Selected GCC installation: " << GCCInstallPath << "\n";
1256
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001257 for (const auto &Multilib : Multilibs)
1258 OS << "Candidate multilib: " << Multilib << "\n";
Stephen Hines651f13c2014-04-23 16:59:28 -07001259
1260 if (Multilibs.size() != 0 || !SelectedMultilib.isDefault())
1261 OS << "Selected multilib: " << SelectedMultilib << "\n";
1262}
1263
1264bool Generic_GCC::GCCInstallationDetector::getBiarchSibling(Multilib &M) const {
1265 if (BiarchSibling.hasValue()) {
1266 M = BiarchSibling.getValue();
1267 return true;
1268 }
1269 return false;
Chandler Carruth6365ab92013-07-30 17:57:09 +00001270}
1271
Chandler Carruth19347ed2011-11-06 23:39:34 +00001272/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruthd79486a2013-06-22 11:35:51 +00001273 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001274 SmallVectorImpl<StringRef> &LibDirs,
1275 SmallVectorImpl<StringRef> &TripleAliases,
Chandler Carruthd79486a2013-06-22 11:35:51 +00001276 SmallVectorImpl<StringRef> &BiarchLibDirs,
1277 SmallVectorImpl<StringRef> &BiarchTripleAliases) {
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001278 // Declare a bunch of static data sets that we'll select between below. These
1279 // are specifically designed to always refer to string literals to avoid any
1280 // lifetime or initialization issues.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001281 static const char *const AArch64LibDirs[] = { "/lib64", "/lib" };
Chandler Carruthd79486a2013-06-22 11:35:51 +00001282 static const char *const AArch64Triples[] = { "aarch64-none-linux-gnu",
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001283 "aarch64-linux-gnu",
1284 "aarch64-linux-android",
1285 "aarch64-redhat-linux" };
Stephen Hines651f13c2014-04-23 16:59:28 -07001286 static const char *const AArch64beLibDirs[] = { "/lib" };
1287 static const char *const AArch64beTriples[] = { "aarch64_be-none-linux-gnu",
1288 "aarch64_be-linux-gnu" };
Tim Northoverc264e162013-01-31 12:13:10 +00001289
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001290 static const char *const ARMLibDirs[] = { "/lib" };
Chandler Carruthd79486a2013-06-22 11:35:51 +00001291 static const char *const ARMTriples[] = { "arm-linux-gnueabi",
1292 "arm-linux-androideabi" };
1293 static const char *const ARMHFTriples[] = { "arm-linux-gnueabihf",
1294 "armv7hl-redhat-linux-gnueabi" };
Stephen Hines651f13c2014-04-23 16:59:28 -07001295 static const char *const ARMebLibDirs[] = { "/lib" };
1296 static const char *const ARMebTriples[] = { "armeb-linux-gnueabi",
1297 "armeb-linux-androideabi" };
1298 static const char *const ARMebHFTriples[] = { "armeb-linux-gnueabihf",
1299 "armebv7hl-redhat-linux-gnueabi" };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001300
1301 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1302 static const char *const X86_64Triples[] = {
Chandler Carruthd79486a2013-06-22 11:35:51 +00001303 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", "x86_64-pc-linux-gnu",
1304 "x86_64-redhat-linux6E", "x86_64-redhat-linux", "x86_64-suse-linux",
Stephen Hines651f13c2014-04-23 16:59:28 -07001305 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", "x86_64-slackware-linux",
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001306 "x86_64-linux-android", "x86_64-unknown-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001307 };
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001308 static const char *const X32LibDirs[] = { "/libx32" };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001309 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1310 static const char *const X86Triples[] = {
Chandler Carruthd79486a2013-06-22 11:35:51 +00001311 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", "i386-linux-gnu",
1312 "i386-redhat-linux6E", "i686-redhat-linux", "i586-redhat-linux",
1313 "i386-redhat-linux", "i586-suse-linux", "i486-slackware-linux",
Stephen Hines176edba2014-12-01 14:53:08 -08001314 "i686-montavista-linux", "i686-linux-android", "i586-linux-gnu"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001315 };
1316
1317 static const char *const MIPSLibDirs[] = { "/lib" };
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001318 static const char *const MIPSTriples[] = { "mips-linux-gnu",
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001319 "mips-mti-linux-gnu",
1320 "mips-img-linux-gnu" };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001321 static const char *const MIPSELLibDirs[] = { "/lib" };
Chandler Carruthd79486a2013-06-22 11:35:51 +00001322 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu",
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001323 "mipsel-linux-android",
1324 "mips-img-linux-gnu" };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001325
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001326 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001327 static const char *const MIPS64Triples[] = { "mips64-linux-gnu",
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001328 "mips-mti-linux-gnu",
1329 "mips-img-linux-gnu",
1330 "mips64-linux-gnuabi64" };
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001331 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001332 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu",
Stephen Hines651f13c2014-04-23 16:59:28 -07001333 "mips-mti-linux-gnu",
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001334 "mips-img-linux-gnu",
1335 "mips64el-linux-android",
1336 "mips64el-linux-gnuabi64" };
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001337
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001338 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1339 static const char *const PPCTriples[] = {
Chandler Carruthd79486a2013-06-22 11:35:51 +00001340 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe",
1341 "powerpc-suse-linux", "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001342 };
1343 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
Chandler Carruthd79486a2013-06-22 11:35:51 +00001344 static const char *const PPC64Triples[] = { "powerpc64-linux-gnu",
1345 "powerpc64-unknown-linux-gnu",
1346 "powerpc64-suse-linux",
1347 "ppc64-redhat-linux" };
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00001348 static const char *const PPC64LELibDirs[] = { "/lib64", "/lib" };
1349 static const char *const PPC64LETriples[] = { "powerpc64le-linux-gnu",
1350 "powerpc64le-unknown-linux-gnu",
1351 "powerpc64le-suse-linux",
1352 "ppc64le-redhat-linux" };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001353
Stephen Hines651f13c2014-04-23 16:59:28 -07001354 static const char *const SPARCv8LibDirs[] = { "/lib32", "/lib" };
1355 static const char *const SPARCv8Triples[] = { "sparc-linux-gnu",
1356 "sparcv8-linux-gnu" };
1357 static const char *const SPARCv9LibDirs[] = { "/lib64", "/lib" };
1358 static const char *const SPARCv9Triples[] = { "sparc64-linux-gnu",
1359 "sparcv9-linux-gnu" };
1360
Ulrich Weigandb8409212013-05-06 16:26:41 +00001361 static const char *const SystemZLibDirs[] = { "/lib64", "/lib" };
1362 static const char *const SystemZTriples[] = {
Chandler Carruthd79486a2013-06-22 11:35:51 +00001363 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu",
1364 "s390x-suse-linux", "s390x-redhat-linux"
Ulrich Weigandb8409212013-05-06 16:26:41 +00001365 };
1366
Stephen Hines176edba2014-12-01 14:53:08 -08001367 using std::begin;
1368 using std::end;
1369
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001370 switch (TargetTriple.getArch()) {
Tim Northoverc264e162013-01-31 12:13:10 +00001371 case llvm::Triple::aarch64:
Stephen Hines176edba2014-12-01 14:53:08 -08001372 LibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs));
1373 TripleAliases.append(begin(AArch64Triples), end(AArch64Triples));
1374 BiarchLibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs));
1375 BiarchTripleAliases.append(begin(AArch64Triples), end(AArch64Triples));
Tim Northoverc264e162013-01-31 12:13:10 +00001376 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07001377 case llvm::Triple::aarch64_be:
Stephen Hines176edba2014-12-01 14:53:08 -08001378 LibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs));
1379 TripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples));
1380 BiarchLibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs));
1381 BiarchTripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples));
Stephen Hines651f13c2014-04-23 16:59:28 -07001382 break;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001383 case llvm::Triple::arm:
1384 case llvm::Triple::thumb:
Stephen Hines176edba2014-12-01 14:53:08 -08001385 LibDirs.append(begin(ARMLibDirs), end(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001386 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
Stephen Hines176edba2014-12-01 14:53:08 -08001387 TripleAliases.append(begin(ARMHFTriples), end(ARMHFTriples));
Jiangning Liuff104a12012-07-31 08:06:29 +00001388 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08001389 TripleAliases.append(begin(ARMTriples), end(ARMTriples));
Jiangning Liuff104a12012-07-31 08:06:29 +00001390 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001391 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07001392 case llvm::Triple::armeb:
1393 case llvm::Triple::thumbeb:
Stephen Hines176edba2014-12-01 14:53:08 -08001394 LibDirs.append(begin(ARMebLibDirs), end(ARMebLibDirs));
Stephen Hines651f13c2014-04-23 16:59:28 -07001395 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
Stephen Hines176edba2014-12-01 14:53:08 -08001396 TripleAliases.append(begin(ARMebHFTriples), end(ARMebHFTriples));
Stephen Hines651f13c2014-04-23 16:59:28 -07001397 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08001398 TripleAliases.append(begin(ARMebTriples), end(ARMebTriples));
Stephen Hines651f13c2014-04-23 16:59:28 -07001399 }
1400 break;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001401 case llvm::Triple::x86_64:
Stephen Hines176edba2014-12-01 14:53:08 -08001402 LibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
1403 TripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
1404 // x32 is always available when x86_64 is available, so adding it as
1405 // secondary arch with x86_64 triples
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001406 if (TargetTriple.getEnvironment() == llvm::Triple::GNUX32) {
Stephen Hines176edba2014-12-01 14:53:08 -08001407 BiarchLibDirs.append(begin(X32LibDirs), end(X32LibDirs));
1408 BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001409 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08001410 BiarchLibDirs.append(begin(X86LibDirs), end(X86LibDirs));
1411 BiarchTripleAliases.append(begin(X86Triples), end(X86Triples));
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001412 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001413 break;
1414 case llvm::Triple::x86:
Stephen Hines176edba2014-12-01 14:53:08 -08001415 LibDirs.append(begin(X86LibDirs), end(X86LibDirs));
1416 TripleAliases.append(begin(X86Triples), end(X86Triples));
1417 BiarchLibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs));
1418 BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001419 break;
1420 case llvm::Triple::mips:
Stephen Hines176edba2014-12-01 14:53:08 -08001421 LibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs));
1422 TripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
1423 BiarchLibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs));
1424 BiarchTripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001425 break;
1426 case llvm::Triple::mipsel:
Stephen Hines176edba2014-12-01 14:53:08 -08001427 LibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs));
1428 TripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples));
1429 TripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
1430 BiarchLibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs));
1431 BiarchTripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001432 break;
1433 case llvm::Triple::mips64:
Stephen Hines176edba2014-12-01 14:53:08 -08001434 LibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs));
1435 TripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples));
1436 BiarchLibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs));
1437 BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001438 break;
1439 case llvm::Triple::mips64el:
Stephen Hines176edba2014-12-01 14:53:08 -08001440 LibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs));
1441 TripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples));
1442 BiarchLibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs));
1443 BiarchTripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples));
1444 BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001445 break;
1446 case llvm::Triple::ppc:
Stephen Hines176edba2014-12-01 14:53:08 -08001447 LibDirs.append(begin(PPCLibDirs), end(PPCLibDirs));
1448 TripleAliases.append(begin(PPCTriples), end(PPCTriples));
1449 BiarchLibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs));
1450 BiarchTripleAliases.append(begin(PPC64Triples), end(PPC64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001451 break;
1452 case llvm::Triple::ppc64:
Stephen Hines176edba2014-12-01 14:53:08 -08001453 LibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs));
1454 TripleAliases.append(begin(PPC64Triples), end(PPC64Triples));
1455 BiarchLibDirs.append(begin(PPCLibDirs), end(PPCLibDirs));
1456 BiarchTripleAliases.append(begin(PPCTriples), end(PPCTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001457 break;
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00001458 case llvm::Triple::ppc64le:
Stephen Hines176edba2014-12-01 14:53:08 -08001459 LibDirs.append(begin(PPC64LELibDirs), end(PPC64LELibDirs));
1460 TripleAliases.append(begin(PPC64LETriples), end(PPC64LETriples));
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00001461 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07001462 case llvm::Triple::sparc:
Stephen Hines176edba2014-12-01 14:53:08 -08001463 LibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs));
1464 TripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples));
1465 BiarchLibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs));
1466 BiarchTripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples));
Stephen Hines651f13c2014-04-23 16:59:28 -07001467 break;
1468 case llvm::Triple::sparcv9:
Stephen Hines176edba2014-12-01 14:53:08 -08001469 LibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs));
1470 TripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples));
1471 BiarchLibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs));
1472 BiarchTripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples));
Stephen Hines651f13c2014-04-23 16:59:28 -07001473 break;
Ulrich Weigandb8409212013-05-06 16:26:41 +00001474 case llvm::Triple::systemz:
Stephen Hines176edba2014-12-01 14:53:08 -08001475 LibDirs.append(begin(SystemZLibDirs), end(SystemZLibDirs));
1476 TripleAliases.append(begin(SystemZTriples), end(SystemZTriples));
Ulrich Weigandb8409212013-05-06 16:26:41 +00001477 break;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001478
1479 default:
1480 // By default, just rely on the standard lib directories and the original
1481 // triple.
1482 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001483 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001484
1485 // Always append the drivers target triple to the end, in case it doesn't
1486 // match any of our aliases.
1487 TripleAliases.push_back(TargetTriple.str());
1488
1489 // Also include the multiarch variant if it's different.
Chandler Carruthd79486a2013-06-22 11:35:51 +00001490 if (TargetTriple.str() != BiarchTriple.str())
1491 BiarchTripleAliases.push_back(BiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001492}
1493
Stephen Hines651f13c2014-04-23 16:59:28 -07001494namespace {
1495// Filter to remove Multilibs that don't exist as a suffix to Path
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001496class FilterNonExistent {
1497 StringRef Base;
1498
Stephen Hines651f13c2014-04-23 16:59:28 -07001499public:
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001500 FilterNonExistent(StringRef Base) : Base(Base) {}
1501 bool operator()(const Multilib &M) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001502 return !llvm::sys::fs::exists(Base + M.gccSuffix() + "/crtbegin.o");
1503 }
1504};
1505} // end anonymous namespace
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001506
Stephen Hines651f13c2014-04-23 16:59:28 -07001507static void addMultilibFlag(bool Enabled, const char *const Flag,
1508 std::vector<std::string> &Flags) {
1509 if (Enabled)
1510 Flags.push_back(std::string("+") + Flag);
1511 else
1512 Flags.push_back(std::string("-") + Flag);
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001513}
1514
1515static bool isMipsArch(llvm::Triple::ArchType Arch) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001516 return Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel ||
1517 Arch == llvm::Triple::mips64 || Arch == llvm::Triple::mips64el;
1518}
1519
1520static bool isMips32(llvm::Triple::ArchType Arch) {
1521 return Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel;
1522}
1523
1524static bool isMips64(llvm::Triple::ArchType Arch) {
1525 return Arch == llvm::Triple::mips64 || Arch == llvm::Triple::mips64el;
1526}
1527
1528static bool isMipsEL(llvm::Triple::ArchType Arch) {
1529 return Arch == llvm::Triple::mipsel || Arch == llvm::Triple::mips64el;
1530}
1531
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001532static bool isMips16(const ArgList &Args) {
1533 Arg *A = Args.getLastArg(options::OPT_mips16,
1534 options::OPT_mno_mips16);
1535 return A && A->getOption().matches(options::OPT_mips16);
1536}
1537
1538static bool isMicroMips(const ArgList &Args) {
1539 Arg *A = Args.getLastArg(options::OPT_mmicromips,
1540 options::OPT_mno_micromips);
1541 return A && A->getOption().matches(options::OPT_mmicromips);
1542}
1543
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001544struct DetectedMultilibs {
1545 /// The set of multilibs that the detected installation supports.
1546 MultilibSet Multilibs;
1547
1548 /// The primary multilib appropriate for the given flags.
1549 Multilib SelectedMultilib;
1550
1551 /// On Biarch systems, this corresponds to the default multilib when
1552 /// targeting the non-default multilib. Otherwise, it is empty.
1553 llvm::Optional<Multilib> BiarchSibling;
1554};
1555
Stephen Hines176edba2014-12-01 14:53:08 -08001556static Multilib makeMultilib(StringRef commonSuffix) {
1557 return Multilib(commonSuffix, commonSuffix, commonSuffix);
1558}
1559
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001560static bool findMIPSMultilibs(const llvm::Triple &TargetTriple, StringRef Path,
1561 const llvm::opt::ArgList &Args,
1562 DetectedMultilibs &Result) {
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001563 // Some MIPS toolchains put libraries and object files compiled
1564 // using different options in to the sub-directoris which names
1565 // reflects the flags used for compilation. For example sysroot
1566 // directory might looks like the following examples:
1567 //
1568 // /usr
1569 // /lib <= crt*.o files compiled with '-mips32'
1570 // /mips16
1571 // /usr
1572 // /lib <= crt*.o files compiled with '-mips16'
1573 // /el
1574 // /usr
1575 // /lib <= crt*.o files compiled with '-mips16 -EL'
1576 //
1577 // or
1578 //
1579 // /usr
1580 // /lib <= crt*.o files compiled with '-mips32r2'
1581 // /mips16
1582 // /usr
1583 // /lib <= crt*.o files compiled with '-mips32r2 -mips16'
1584 // /mips32
1585 // /usr
1586 // /lib <= crt*.o files compiled with '-mips32'
Simon Atanasyan5c805e92013-09-28 13:45:11 +00001587
Stephen Hines651f13c2014-04-23 16:59:28 -07001588 FilterNonExistent NonExistent(Path);
Simon Atanasyan5c805e92013-09-28 13:45:11 +00001589
Stephen Hines651f13c2014-04-23 16:59:28 -07001590 // Check for FSF toolchain multilibs
1591 MultilibSet FSFMipsMultilibs;
1592 {
Stephen Hines176edba2014-12-01 14:53:08 -08001593 auto MArchMips32 = makeMultilib("/mips32")
1594 .flag("+m32").flag("-m64").flag("-mmicromips").flag("+march=mips32");
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001595
Stephen Hines176edba2014-12-01 14:53:08 -08001596 auto MArchMicroMips = makeMultilib("/micromips")
Stephen Hines651f13c2014-04-23 16:59:28 -07001597 .flag("+m32").flag("-m64").flag("+mmicromips");
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001598
Stephen Hines176edba2014-12-01 14:53:08 -08001599 auto MArchMips64r2 = makeMultilib("/mips64r2")
Stephen Hines651f13c2014-04-23 16:59:28 -07001600 .flag("-m32").flag("+m64").flag("+march=mips64r2");
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001601
Stephen Hines176edba2014-12-01 14:53:08 -08001602 auto MArchMips64 = makeMultilib("/mips64")
Stephen Hines651f13c2014-04-23 16:59:28 -07001603 .flag("-m32").flag("+m64").flag("-march=mips64r2");
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001604
Stephen Hines176edba2014-12-01 14:53:08 -08001605 auto MArchDefault = makeMultilib("")
1606 .flag("+m32").flag("-m64").flag("-mmicromips").flag("+march=mips32r2");
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001607
Stephen Hines176edba2014-12-01 14:53:08 -08001608 auto Mips16 = makeMultilib("/mips16")
Stephen Hines651f13c2014-04-23 16:59:28 -07001609 .flag("+mips16");
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001610
Stephen Hines176edba2014-12-01 14:53:08 -08001611 auto UCLibc = makeMultilib("/uclibc")
1612 .flag("+muclibc");
Daniel Sanders6c5c3ad2013-12-02 10:00:07 +00001613
Stephen Hines176edba2014-12-01 14:53:08 -08001614 auto MAbi64 = makeMultilib("/64")
1615 .flag("+mabi=n64").flag("-mabi=n32").flag("-m32");
1616
1617 auto BigEndian = makeMultilib("")
Stephen Hines651f13c2014-04-23 16:59:28 -07001618 .flag("+EB").flag("-EL");
1619
Stephen Hines176edba2014-12-01 14:53:08 -08001620 auto LittleEndian = makeMultilib("/el")
Stephen Hines651f13c2014-04-23 16:59:28 -07001621 .flag("+EL").flag("-EB");
1622
Stephen Hines176edba2014-12-01 14:53:08 -08001623 auto SoftFloat = makeMultilib("/sof")
Stephen Hines651f13c2014-04-23 16:59:28 -07001624 .flag("+msoft-float");
1625
Stephen Hines176edba2014-12-01 14:53:08 -08001626 auto Nan2008 = makeMultilib("/nan2008")
Stephen Hines651f13c2014-04-23 16:59:28 -07001627 .flag("+mnan=2008");
1628
1629 FSFMipsMultilibs = MultilibSet()
1630 .Either(MArchMips32, MArchMicroMips,
1631 MArchMips64r2, MArchMips64, MArchDefault)
Stephen Hines176edba2014-12-01 14:53:08 -08001632 .Maybe(UCLibc)
Stephen Hines651f13c2014-04-23 16:59:28 -07001633 .Maybe(Mips16)
1634 .FilterOut("/mips64/mips16")
1635 .FilterOut("/mips64r2/mips16")
1636 .FilterOut("/micromips/mips16")
1637 .Maybe(MAbi64)
1638 .FilterOut("/micromips/64")
1639 .FilterOut("/mips32/64")
1640 .FilterOut("^/64")
1641 .FilterOut("/mips16/64")
1642 .Either(BigEndian, LittleEndian)
1643 .Maybe(SoftFloat)
Stephen Hines651f13c2014-04-23 16:59:28 -07001644 .Maybe(Nan2008)
1645 .FilterOut(".*sof/nan2008")
Stephen Hines176edba2014-12-01 14:53:08 -08001646 .FilterOut(NonExistent)
1647 .setIncludeDirsCallback([](
1648 StringRef InstallDir, StringRef TripleStr, const Multilib &M) {
1649 std::vector<std::string> Dirs;
1650 Dirs.push_back((InstallDir + "/include").str());
1651 std::string SysRootInc = InstallDir.str() + "/../../../../sysroot";
1652 if (StringRef(M.includeSuffix()).startswith("/uclibc"))
1653 Dirs.push_back(SysRootInc + "/uclibc/usr/include");
1654 else
1655 Dirs.push_back(SysRootInc + "/usr/include");
1656 return Dirs;
1657 });
Stephen Hines651f13c2014-04-23 16:59:28 -07001658 }
1659
1660 // Check for Code Sourcery toolchain multilibs
1661 MultilibSet CSMipsMultilibs;
1662 {
Stephen Hines176edba2014-12-01 14:53:08 -08001663 auto MArchMips16 = makeMultilib("/mips16")
Stephen Hines651f13c2014-04-23 16:59:28 -07001664 .flag("+m32").flag("+mips16");
1665
Stephen Hines176edba2014-12-01 14:53:08 -08001666 auto MArchMicroMips = makeMultilib("/micromips")
Stephen Hines651f13c2014-04-23 16:59:28 -07001667 .flag("+m32").flag("+mmicromips");
1668
Stephen Hines176edba2014-12-01 14:53:08 -08001669 auto MArchDefault = makeMultilib("")
Stephen Hines651f13c2014-04-23 16:59:28 -07001670 .flag("-mips16").flag("-mmicromips");
1671
Stephen Hines176edba2014-12-01 14:53:08 -08001672 auto UCLibc = makeMultilib("/uclibc")
1673 .flag("+muclibc");
1674
1675 auto SoftFloat = makeMultilib("/soft-float")
Stephen Hines651f13c2014-04-23 16:59:28 -07001676 .flag("+msoft-float");
1677
Stephen Hines176edba2014-12-01 14:53:08 -08001678 auto Nan2008 = makeMultilib("/nan2008")
Stephen Hines651f13c2014-04-23 16:59:28 -07001679 .flag("+mnan=2008");
1680
Stephen Hines176edba2014-12-01 14:53:08 -08001681 auto DefaultFloat = makeMultilib("")
Stephen Hines651f13c2014-04-23 16:59:28 -07001682 .flag("-msoft-float").flag("-mnan=2008");
1683
Stephen Hines176edba2014-12-01 14:53:08 -08001684 auto BigEndian = makeMultilib("")
Stephen Hines651f13c2014-04-23 16:59:28 -07001685 .flag("+EB").flag("-EL");
1686
Stephen Hines176edba2014-12-01 14:53:08 -08001687 auto LittleEndian = makeMultilib("/el")
Stephen Hines651f13c2014-04-23 16:59:28 -07001688 .flag("+EL").flag("-EB");
1689
1690 // Note that this one's osSuffix is ""
Stephen Hines176edba2014-12-01 14:53:08 -08001691 auto MAbi64 = makeMultilib("")
Stephen Hines651f13c2014-04-23 16:59:28 -07001692 .gccSuffix("/64")
1693 .includeSuffix("/64")
Stephen Hines176edba2014-12-01 14:53:08 -08001694 .flag("+mabi=n64").flag("-mabi=n32").flag("-m32");
Stephen Hines651f13c2014-04-23 16:59:28 -07001695
1696 CSMipsMultilibs = MultilibSet()
1697 .Either(MArchMips16, MArchMicroMips, MArchDefault)
Stephen Hines176edba2014-12-01 14:53:08 -08001698 .Maybe(UCLibc)
Stephen Hines651f13c2014-04-23 16:59:28 -07001699 .Either(SoftFloat, Nan2008, DefaultFloat)
1700 .FilterOut("/micromips/nan2008")
1701 .FilterOut("/mips16/nan2008")
1702 .Either(BigEndian, LittleEndian)
1703 .Maybe(MAbi64)
1704 .FilterOut("/mips16.*/64")
1705 .FilterOut("/micromips.*/64")
Stephen Hines176edba2014-12-01 14:53:08 -08001706 .FilterOut(NonExistent)
1707 .setIncludeDirsCallback([](
1708 StringRef InstallDir, StringRef TripleStr, const Multilib &M) {
1709 std::vector<std::string> Dirs;
1710 Dirs.push_back((InstallDir + "/include").str());
1711 std::string SysRootInc =
1712 InstallDir.str() + "/../../../../" + TripleStr.str();
1713 if (StringRef(M.includeSuffix()).startswith("/uclibc"))
1714 Dirs.push_back(SysRootInc + "/libc/uclibc/usr/include");
1715 else
1716 Dirs.push_back(SysRootInc + "/libc/usr/include");
1717 return Dirs;
1718 });
Stephen Hines651f13c2014-04-23 16:59:28 -07001719 }
1720
1721 MultilibSet AndroidMipsMultilibs = MultilibSet()
1722 .Maybe(Multilib("/mips-r2").flag("+march=mips32r2"))
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001723 .Maybe(Multilib("/mips-r6").flag("+march=mips32r6"))
Stephen Hines651f13c2014-04-23 16:59:28 -07001724 .FilterOut(NonExistent);
1725
1726 MultilibSet DebianMipsMultilibs;
1727 {
1728 Multilib MAbiN32 = Multilib()
1729 .gccSuffix("/n32")
1730 .includeSuffix("/n32")
1731 .flag("+mabi=n32");
1732
1733 Multilib M64 = Multilib()
1734 .gccSuffix("/64")
1735 .includeSuffix("/64")
1736 .flag("+m64").flag("-m32").flag("-mabi=n32");
1737
1738 Multilib M32 = Multilib()
1739 .flag("-m64").flag("+m32").flag("-mabi=n32");
1740
1741 DebianMipsMultilibs = MultilibSet()
1742 .Either(M32, M64, MAbiN32)
1743 .FilterOut(NonExistent);
1744 }
1745
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001746 MultilibSet ImgMultilibs;
1747 {
Stephen Hines176edba2014-12-01 14:53:08 -08001748 auto Mips64r6 = makeMultilib("/mips64r6")
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001749 .flag("+m64").flag("-m32");
1750
Stephen Hines176edba2014-12-01 14:53:08 -08001751 auto LittleEndian = makeMultilib("/el")
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001752 .flag("+EL").flag("-EB");
1753
Stephen Hines176edba2014-12-01 14:53:08 -08001754 auto MAbi64 = makeMultilib("/64")
1755 .flag("+mabi=n64").flag("-mabi=n32").flag("-m32");
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001756
1757 ImgMultilibs = MultilibSet()
1758 .Maybe(Mips64r6)
1759 .Maybe(MAbi64)
1760 .Maybe(LittleEndian)
Stephen Hines176edba2014-12-01 14:53:08 -08001761 .FilterOut(NonExistent)
1762 .setIncludeDirsCallback([](
1763 StringRef InstallDir, StringRef TripleStr, const Multilib &M) {
1764 std::vector<std::string> Dirs;
1765 Dirs.push_back((InstallDir + "/include").str());
1766 Dirs.push_back((InstallDir + "/../../../../sysroot/usr/include").str());
1767 return Dirs;
1768 });
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001769 }
1770
Stephen Hines176edba2014-12-01 14:53:08 -08001771 StringRef CPUName;
1772 StringRef ABIName;
1773 tools::mips::getMipsCPUAndABI(Args, TargetTriple, CPUName, ABIName);
1774
Stephen Hines651f13c2014-04-23 16:59:28 -07001775 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
1776
1777 Multilib::flags_list Flags;
1778 addMultilibFlag(isMips32(TargetArch), "m32", Flags);
1779 addMultilibFlag(isMips64(TargetArch), "m64", Flags);
1780 addMultilibFlag(isMips16(Args), "mips16", Flags);
Stephen Hines176edba2014-12-01 14:53:08 -08001781 addMultilibFlag(CPUName == "mips32", "march=mips32", Flags);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001782 addMultilibFlag(CPUName == "mips32r2" || CPUName == "mips32r3" ||
1783 CPUName == "mips32r5",
1784 "march=mips32r2", Flags);
1785 addMultilibFlag(CPUName == "mips32r6", "march=mips32r6", Flags);
Stephen Hines176edba2014-12-01 14:53:08 -08001786 addMultilibFlag(CPUName == "mips64", "march=mips64", Flags);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001787 addMultilibFlag(CPUName == "mips64r2" || CPUName == "mips64r3" ||
1788 CPUName == "mips64r5" || CPUName == "octeon",
Stephen Hines176edba2014-12-01 14:53:08 -08001789 "march=mips64r2", Flags);
Stephen Hines651f13c2014-04-23 16:59:28 -07001790 addMultilibFlag(isMicroMips(Args), "mmicromips", Flags);
Stephen Hines176edba2014-12-01 14:53:08 -08001791 addMultilibFlag(tools::mips::isUCLibc(Args), "muclibc", Flags);
1792 addMultilibFlag(tools::mips::isNaN2008(Args, TargetTriple), "mnan=2008",
1793 Flags);
1794 addMultilibFlag(ABIName == "n32", "mabi=n32", Flags);
1795 addMultilibFlag(ABIName == "n64", "mabi=n64", Flags);
Stephen Hines651f13c2014-04-23 16:59:28 -07001796 addMultilibFlag(isSoftFloatABI(Args), "msoft-float", Flags);
Stephen Hines651f13c2014-04-23 16:59:28 -07001797 addMultilibFlag(!isSoftFloatABI(Args), "mhard-float", Flags);
Stephen Hines651f13c2014-04-23 16:59:28 -07001798 addMultilibFlag(isMipsEL(TargetArch), "EL", Flags);
Stephen Hines176edba2014-12-01 14:53:08 -08001799 addMultilibFlag(!isMipsEL(TargetArch), "EB", Flags);
Stephen Hines651f13c2014-04-23 16:59:28 -07001800
1801 if (TargetTriple.getEnvironment() == llvm::Triple::Android) {
1802 // Select Android toolchain. It's the only choice in that case.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001803 if (AndroidMipsMultilibs.select(Flags, Result.SelectedMultilib)) {
1804 Result.Multilibs = AndroidMipsMultilibs;
1805 return true;
1806 }
1807 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001808 }
1809
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001810 if (TargetTriple.getVendor() == llvm::Triple::ImaginationTechnologies &&
1811 TargetTriple.getOS() == llvm::Triple::Linux &&
1812 TargetTriple.getEnvironment() == llvm::Triple::GNU) {
1813 // Select mips-img-linux-gnu toolchain.
1814 if (ImgMultilibs.select(Flags, Result.SelectedMultilib)) {
1815 Result.Multilibs = ImgMultilibs;
1816 return true;
1817 }
1818 return false;
1819 }
1820
Stephen Hines651f13c2014-04-23 16:59:28 -07001821 // Sort candidates. Toolchain that best meets the directories goes first.
1822 // Then select the first toolchains matches command line flags.
1823 MultilibSet *candidates[] = { &DebianMipsMultilibs, &FSFMipsMultilibs,
1824 &CSMipsMultilibs };
1825 std::sort(
1826 std::begin(candidates), std::end(candidates),
1827 [](MultilibSet *a, MultilibSet *b) { return a->size() > b->size(); });
1828 for (const auto &candidate : candidates) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001829 if (candidate->select(Flags, Result.SelectedMultilib)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001830 if (candidate == &DebianMipsMultilibs)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001831 Result.BiarchSibling = Multilib();
1832 Result.Multilibs = *candidate;
Stephen Hines651f13c2014-04-23 16:59:28 -07001833 return true;
Daniel Sanders6c5c3ad2013-12-02 10:00:07 +00001834 }
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00001835 }
Simon Atanasyan5c805e92013-09-28 13:45:11 +00001836
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001837 {
1838 // Fallback to the regular toolchain-tree structure.
1839 Multilib Default;
1840 Result.Multilibs.push_back(Default);
1841 Result.Multilibs.FilterOut(NonExistent);
1842
1843 if (Result.Multilibs.select(Flags, Result.SelectedMultilib)) {
1844 Result.BiarchSibling = Multilib();
1845 return true;
1846 }
1847 }
1848
Stephen Hines651f13c2014-04-23 16:59:28 -07001849 return false;
1850}
1851
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001852static bool findBiarchMultilibs(const llvm::Triple &TargetTriple,
1853 StringRef Path, const ArgList &Args,
1854 bool NeedsBiarchSuffix,
1855 DetectedMultilibs &Result) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001856
1857 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
1858 // in what would normally be GCCInstallPath and put the 64-bit
1859 // libs in a subdirectory named 64. The simple logic we follow is that
1860 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1861 // we use that. If not, and if not a biarch triple alias, we look for
1862 // crtbegin.o without the subdirectory.
1863
1864 Multilib Default;
1865 Multilib Alt64 = Multilib()
1866 .gccSuffix("/64")
1867 .includeSuffix("/64")
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001868 .flag("-m32").flag("+m64").flag("-mx32");
Stephen Hines651f13c2014-04-23 16:59:28 -07001869 Multilib Alt32 = Multilib()
1870 .gccSuffix("/32")
1871 .includeSuffix("/32")
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001872 .flag("+m32").flag("-m64").flag("-mx32");
1873 Multilib Altx32 = Multilib()
1874 .gccSuffix("/x32")
1875 .includeSuffix("/x32")
1876 .flag("-m32").flag("-m64").flag("+mx32");
Stephen Hines651f13c2014-04-23 16:59:28 -07001877
1878 FilterNonExistent NonExistent(Path);
1879
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001880 // Determine default multilib from: 32, 64, x32
1881 // Also handle cases such as 64 on 32, 32 on 64, etc.
1882 enum { UNKNOWN, WANT32, WANT64, WANTX32 } Want = UNKNOWN;
Stephen Hines176edba2014-12-01 14:53:08 -08001883 const bool IsX32 = TargetTriple.getEnvironment() == llvm::Triple::GNUX32;
Stephen Hines651f13c2014-04-23 16:59:28 -07001884 if (TargetTriple.isArch32Bit() && !NonExistent(Alt32))
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001885 Want = WANT64;
1886 else if (TargetTriple.isArch64Bit() && IsX32 && !NonExistent(Altx32))
1887 Want = WANT64;
1888 else if (TargetTriple.isArch64Bit() && !IsX32 && !NonExistent(Alt64))
1889 Want = WANT32;
Stephen Hines651f13c2014-04-23 16:59:28 -07001890 else {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001891 if (TargetTriple.isArch32Bit())
1892 Want = NeedsBiarchSuffix ? WANT64 : WANT32;
1893 else if (IsX32)
1894 Want = NeedsBiarchSuffix ? WANT64 : WANTX32;
Stephen Hines651f13c2014-04-23 16:59:28 -07001895 else
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001896 Want = NeedsBiarchSuffix ? WANT32 : WANT64;
Stephen Hines651f13c2014-04-23 16:59:28 -07001897 }
1898
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001899 if (Want == WANT32)
1900 Default.flag("+m32").flag("-m64").flag("-mx32");
1901 else if (Want == WANT64)
1902 Default.flag("-m32").flag("+m64").flag("-mx32");
1903 else if (Want == WANTX32)
1904 Default.flag("-m32").flag("-m64").flag("+mx32");
Stephen Hines651f13c2014-04-23 16:59:28 -07001905 else
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001906 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001907
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001908 Result.Multilibs.push_back(Default);
1909 Result.Multilibs.push_back(Alt64);
1910 Result.Multilibs.push_back(Alt32);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001911 Result.Multilibs.push_back(Altx32);
Stephen Hines651f13c2014-04-23 16:59:28 -07001912
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001913 Result.Multilibs.FilterOut(NonExistent);
Stephen Hines651f13c2014-04-23 16:59:28 -07001914
1915 Multilib::flags_list Flags;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001916 addMultilibFlag(TargetTriple.isArch64Bit() && !IsX32, "m64", Flags);
Stephen Hines651f13c2014-04-23 16:59:28 -07001917 addMultilibFlag(TargetTriple.isArch32Bit(), "m32", Flags);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001918 addMultilibFlag(TargetTriple.isArch64Bit() && IsX32, "mx32", Flags);
Stephen Hines651f13c2014-04-23 16:59:28 -07001919
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001920 if (!Result.Multilibs.select(Flags, Result.SelectedMultilib))
Stephen Hines651f13c2014-04-23 16:59:28 -07001921 return false;
1922
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001923 if (Result.SelectedMultilib == Alt64 ||
1924 Result.SelectedMultilib == Alt32 ||
1925 Result.SelectedMultilib == Altx32)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001926 Result.BiarchSibling = Default;
Stephen Hines651f13c2014-04-23 16:59:28 -07001927
1928 return true;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00001929}
1930
Chandler Carruth19347ed2011-11-06 23:39:34 +00001931void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Stephen Hines651f13c2014-04-23 16:59:28 -07001932 const llvm::Triple &TargetTriple, const ArgList &Args,
Chandler Carruthd79486a2013-06-22 11:35:51 +00001933 const std::string &LibDir, StringRef CandidateTriple,
1934 bool NeedsBiarchSuffix) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001935 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001936 // There are various different suffixes involving the triple we
1937 // check for. We also record what is necessary to walk from each back
1938 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001939 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001940 "/gcc/" + CandidateTriple.str(),
Eli Friedman003e1dc2013-07-26 00:53:40 +00001941 // Debian puts cross-compilers in gcc-cross
1942 "/gcc-cross/" + CandidateTriple.str(),
Chandler Carruth19347ed2011-11-06 23:39:34 +00001943 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1944
Hal Finkel02014b42012-09-18 22:25:07 +00001945 // The Freescale PPC SDK has the gcc libraries in
1946 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1947 "/" + CandidateTriple.str(),
1948
Chandler Carruth19347ed2011-11-06 23:39:34 +00001949 // Ubuntu has a strange mis-matched pair of triples that this happens to
1950 // match.
1951 // FIXME: It may be worthwhile to generalize this and look for a second
1952 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001953 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001954 };
Eli Friedman003e1dc2013-07-26 00:53:40 +00001955 const std::string InstallSuffixes[] = {
1956 "/../../..", // gcc/
1957 "/../../..", // gcc-cross/
1958 "/../../../..", // <triple>/gcc/
1959 "/../..", // <triple>/
1960 "/../../../.." // i386-linux-gnu/gcc/<triple>/
1961 };
Chandler Carruth19347ed2011-11-06 23:39:34 +00001962 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruthd79486a2013-06-22 11:35:51 +00001963 const unsigned NumLibSuffixes =
1964 (llvm::array_lengthof(LibSuffixes) - (TargetArch != llvm::Triple::x86));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001965 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1966 StringRef LibSuffix = LibSuffixes[i];
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001967 std::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001968 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001969 !EC && LI != LE; LI = LI.increment(EC)) {
1970 StringRef VersionText = llvm::sys::path::filename(LI->path());
1971 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
Benjamin Kramerf15b26c2013-08-14 18:38:51 +00001972 if (CandidateVersion.Major != -1) // Filter obviously bad entries.
1973 if (!CandidateGCCInstallPaths.insert(LI->path()).second)
1974 continue; // Saw this path before; no need to look at it again.
Benjamin Kramered5f28f2013-08-09 17:17:48 +00001975 if (CandidateVersion.isOlderThan(4, 1, 1))
Chandler Carruth19347ed2011-11-06 23:39:34 +00001976 continue;
1977 if (CandidateVersion <= Version)
1978 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001979
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001980 DetectedMultilibs Detected;
Simon Atanasyan5c805e92013-09-28 13:45:11 +00001981
Stephen Hines651f13c2014-04-23 16:59:28 -07001982 // Debian mips multilibs behave more like the rest of the biarch ones,
1983 // so handle them there
1984 if (isMipsArch(TargetArch)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001985 if (!findMIPSMultilibs(TargetTriple, LI->path(), Args, Detected))
Stephen Hines651f13c2014-04-23 16:59:28 -07001986 continue;
1987 } else if (!findBiarchMultilibs(TargetTriple, LI->path(), Args,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001988 NeedsBiarchSuffix, Detected)) {
Simon Atanasyan5c805e92013-09-28 13:45:11 +00001989 continue;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001990 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001991
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001992 Multilibs = Detected.Multilibs;
1993 SelectedMultilib = Detected.SelectedMultilib;
1994 BiarchSibling = Detected.BiarchSibling;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001995 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001996 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001997 // FIXME: We hack together the directory name here instead of
1998 // using LI to ensure stable path separators across Windows and
1999 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002000 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00002001 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00002002 IsValid = true;
2003 }
2004 }
2005}
2006
Rafael Espindola0e659592012-02-19 01:38:32 +00002007Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
2008 const ArgList &Args)
Stephen Hines651f13c2014-04-23 16:59:28 -07002009 : ToolChain(D, Triple, Args), GCCInstallation() {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002010 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002011 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002012 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00002013}
2014
Daniel Dunbar39176082009-03-20 00:20:03 +00002015Generic_GCC::~Generic_GCC() {
Daniel Dunbar39176082009-03-20 00:20:03 +00002016}
2017
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002018Tool *Generic_GCC::getTool(Action::ActionClass AC) const {
Rafael Espindola5b222052013-03-18 20:48:54 +00002019 switch (AC) {
Rafael Espindolaf3260562013-03-18 18:50:01 +00002020 case Action::PreprocessJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002021 if (!Preprocess)
2022 Preprocess.reset(new tools::gcc::Preprocess(*this));
2023 return Preprocess.get();
Rafael Espindolaf3260562013-03-18 18:50:01 +00002024 case Action::CompileJobClass:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002025 if (!Compile)
2026 Compile.reset(new tools::gcc::Compile(*this));
2027 return Compile.get();
Rafael Espindolac0a55d12013-03-19 00:36:57 +00002028 default:
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002029 return ToolChain::getTool(AC);
Daniel Dunbar39176082009-03-20 00:20:03 +00002030 }
Daniel Dunbar39176082009-03-20 00:20:03 +00002031}
2032
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002033Tool *Generic_GCC::buildAssembler() const {
Stephen Hines651f13c2014-04-23 16:59:28 -07002034 return new tools::gnutools::Assemble(*this);
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002035}
2036
2037Tool *Generic_GCC::buildLinker() const {
2038 return new tools::gcc::Link(*this);
2039}
2040
Chandler Carruth6365ab92013-07-30 17:57:09 +00002041void Generic_GCC::printVerboseInfo(raw_ostream &OS) const {
2042 // Print the information about how we detected the GCC installation.
2043 GCCInstallation.print(OS);
2044}
2045
Daniel Dunbar39176082009-03-20 00:20:03 +00002046bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00002047 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00002048}
2049
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002050bool Generic_GCC::isPICDefault() const {
2051 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00002052}
2053
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002054bool Generic_GCC::isPIEDefault() const {
2055 return false;
2056}
2057
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002058bool Generic_GCC::isPICDefaultForced() const {
2059 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00002060}
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002061
Stephen Hines651f13c2014-04-23 16:59:28 -07002062bool Generic_GCC::IsIntegratedAssemblerDefault() const {
2063 return getTriple().getArch() == llvm::Triple::x86 ||
2064 getTriple().getArch() == llvm::Triple::x86_64 ||
2065 getTriple().getArch() == llvm::Triple::aarch64 ||
2066 getTriple().getArch() == llvm::Triple::aarch64_be ||
Stephen Hines651f13c2014-04-23 16:59:28 -07002067 getTriple().getArch() == llvm::Triple::arm ||
2068 getTriple().getArch() == llvm::Triple::armeb ||
2069 getTriple().getArch() == llvm::Triple::thumb ||
Stephen Hines176edba2014-12-01 14:53:08 -08002070 getTriple().getArch() == llvm::Triple::thumbeb ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002071 getTriple().getArch() == llvm::Triple::ppc ||
Stephen Hines176edba2014-12-01 14:53:08 -08002072 getTriple().getArch() == llvm::Triple::ppc64 ||
2073 getTriple().getArch() == llvm::Triple::ppc64le ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002074 getTriple().getArch() == llvm::Triple::sparc ||
2075 getTriple().getArch() == llvm::Triple::sparcv9 ||
Stephen Hines176edba2014-12-01 14:53:08 -08002076 getTriple().getArch() == llvm::Triple::systemz;
Stephen Hines651f13c2014-04-23 16:59:28 -07002077}
2078
2079void Generic_ELF::addClangTargetOptions(const ArgList &DriverArgs,
2080 ArgStringList &CC1Args) const {
2081 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
2082 bool UseInitArrayDefault =
2083 getTriple().getArch() == llvm::Triple::aarch64 ||
2084 getTriple().getArch() == llvm::Triple::aarch64_be ||
Stephen Hines651f13c2014-04-23 16:59:28 -07002085 (getTriple().getOS() == llvm::Triple::Linux &&
2086 (!V.isOlderThan(4, 7, 0) ||
2087 getTriple().getEnvironment() == llvm::Triple::Android));
2088
2089 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2090 options::OPT_fno_use_init_array,
2091 UseInitArrayDefault))
2092 CC1Args.push_back("-fuse-init-array");
2093}
2094
Tony Linthicum96319392011-12-12 21:14:55 +00002095/// Hexagon Toolchain
2096
Stephen Hines176edba2014-12-01 14:53:08 -08002097std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir,
2098 const ArgList &Args) {
Matthew Curtisb3489a02012-12-06 12:43:18 +00002099
2100 // Locate the rest of the toolchain ...
Stephen Hines176edba2014-12-01 14:53:08 -08002101 std::string GccToolchain = getGCCToolchainDir(Args);
2102
2103 if (!GccToolchain.empty())
2104 return GccToolchain;
Matthew Curtisb3489a02012-12-06 12:43:18 +00002105
2106 std::string InstallRelDir = InstalledDir + "/../../gnu";
2107 if (llvm::sys::fs::exists(InstallRelDir))
2108 return InstallRelDir;
2109
2110 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
2111 if (llvm::sys::fs::exists(PrefixRelDir))
2112 return PrefixRelDir;
2113
2114 return InstallRelDir;
2115}
2116
Matthew Curtis5fdf3502012-12-06 15:46:07 +00002117static void GetHexagonLibraryPaths(
2118 const ArgList &Args,
Stephen Hines176edba2014-12-01 14:53:08 -08002119 const std::string &Ver,
2120 const std::string &MarchString,
Matthew Curtis5fdf3502012-12-06 15:46:07 +00002121 const std::string &InstalledDir,
2122 ToolChain::path_list *LibPaths)
2123{
2124 bool buildingLib = Args.hasArg(options::OPT_shared);
2125
2126 //----------------------------------------------------------------------------
2127 // -L Args
2128 //----------------------------------------------------------------------------
2129 for (arg_iterator
2130 it = Args.filtered_begin(options::OPT_L),
2131 ie = Args.filtered_end();
2132 it != ie;
2133 ++it) {
2134 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
2135 LibPaths->push_back((*it)->getValue(i));
2136 }
2137
2138 //----------------------------------------------------------------------------
2139 // Other standard paths
2140 //----------------------------------------------------------------------------
2141 const std::string MarchSuffix = "/" + MarchString;
2142 const std::string G0Suffix = "/G0";
2143 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
Stephen Hines176edba2014-12-01 14:53:08 -08002144 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir, Args) + "/";
Matthew Curtis5fdf3502012-12-06 15:46:07 +00002145
2146 // lib/gcc/hexagon/...
2147 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
2148 if (buildingLib) {
2149 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
2150 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
2151 }
2152 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
2153 LibPaths->push_back(LibGCCHexagonDir + Ver);
2154
2155 // lib/gcc/...
2156 LibPaths->push_back(RootDir + "lib/gcc");
2157
2158 // hexagon/lib/...
2159 std::string HexagonLibDir = RootDir + "hexagon/lib";
2160 if (buildingLib) {
2161 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
2162 LibPaths->push_back(HexagonLibDir + G0Suffix);
2163 }
2164 LibPaths->push_back(HexagonLibDir + MarchSuffix);
2165 LibPaths->push_back(HexagonLibDir);
2166}
2167
Matthew Curtisb3489a02012-12-06 12:43:18 +00002168Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
2169 const ArgList &Args)
2170 : Linux(D, Triple, Args) {
2171 const std::string InstalledDir(getDriver().getInstalledDir());
Stephen Hines176edba2014-12-01 14:53:08 -08002172 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir, Args);
Matthew Curtisb3489a02012-12-06 12:43:18 +00002173
2174 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
2175 // program paths
2176 const std::string BinDir(GnuDir + "/bin");
2177 if (llvm::sys::fs::exists(BinDir))
2178 getProgramPaths().push_back(BinDir);
2179
2180 // Determine version of GCC libraries and headers to use.
2181 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002182 std::error_code ec;
Matthew Curtisb3489a02012-12-06 12:43:18 +00002183 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
2184 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
2185 !ec && di != de; di = di.increment(ec)) {
2186 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
2187 if (MaxVersion < cv)
2188 MaxVersion = cv;
2189 }
2190 GCCLibAndIncVersion = MaxVersion;
Matthew Curtis5fdf3502012-12-06 15:46:07 +00002191
2192 ToolChain::path_list *LibPaths= &getFilePaths();
2193
2194 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
2195 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
2196 // support 'linux' we'll need to fix this up
2197 LibPaths->clear();
2198
2199 GetHexagonLibraryPaths(
2200 Args,
2201 GetGCCLibAndIncVersion(),
2202 GetTargetCPU(Args),
2203 InstalledDir,
2204 LibPaths);
Tony Linthicum96319392011-12-12 21:14:55 +00002205}
2206
2207Hexagon_TC::~Hexagon_TC() {
Tony Linthicum96319392011-12-12 21:14:55 +00002208}
2209
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002210Tool *Hexagon_TC::buildAssembler() const {
2211 return new tools::hexagon::Assemble(*this);
2212}
2213
2214Tool *Hexagon_TC::buildLinker() const {
2215 return new tools::hexagon::Link(*this);
Tony Linthicum96319392011-12-12 21:14:55 +00002216}
2217
Matthew Curtisb3489a02012-12-06 12:43:18 +00002218void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2219 ArgStringList &CC1Args) const {
2220 const Driver &D = getDriver();
2221
2222 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
2223 DriverArgs.hasArg(options::OPT_nostdlibinc))
2224 return;
2225
Matthew Curtisb3489a02012-12-06 12:43:18 +00002226 std::string Ver(GetGCCLibAndIncVersion());
Stephen Hines176edba2014-12-01 14:53:08 -08002227 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir, DriverArgs);
Matthew Curtisb3489a02012-12-06 12:43:18 +00002228 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
2229 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
2230 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
2231 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum96319392011-12-12 21:14:55 +00002232}
2233
Matthew Curtisb3489a02012-12-06 12:43:18 +00002234void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2235 ArgStringList &CC1Args) const {
2236
2237 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2238 DriverArgs.hasArg(options::OPT_nostdincxx))
2239 return;
2240
2241 const Driver &D = getDriver();
2242 std::string Ver(GetGCCLibAndIncVersion());
Stephen Hines176edba2014-12-01 14:53:08 -08002243 SmallString<128> IncludeDir(
2244 Hexagon_TC::GetGnuDir(D.InstalledDir, DriverArgs));
Matthew Curtisb3489a02012-12-06 12:43:18 +00002245
Rafael Espindola7b6301f2013-06-26 02:13:00 +00002246 llvm::sys::path::append(IncludeDir, "hexagon/include/c++/");
2247 llvm::sys::path::append(IncludeDir, Ver);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002248 addSystemInclude(DriverArgs, CC1Args, IncludeDir);
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002249}
Matthew Curtis67814152012-12-06 14:16:43 +00002250
Matthew Curtis5fdf3502012-12-06 15:46:07 +00002251ToolChain::CXXStdlibType
2252Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
2253 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
2254 if (!A)
2255 return ToolChain::CST_Libstdcxx;
2256
2257 StringRef Value = A->getValue();
2258 if (Value != "libstdc++") {
2259 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2260 << A->getAsString(Args);
2261 }
2262
2263 return ToolChain::CST_Libstdcxx;
2264}
2265
Rafael Espindolaf26d8bc2013-09-24 13:28:24 +00002266static int getHexagonVersion(const ArgList &Args) {
2267 Arg *A = Args.getLastArg(options::OPT_march_EQ, options::OPT_mcpu_EQ);
2268 // Select the default CPU (v4) if none was given.
2269 if (!A)
2270 return 4;
Matthew Curtis67814152012-12-06 14:16:43 +00002271
Rafael Espindolaf26d8bc2013-09-24 13:28:24 +00002272 // FIXME: produce errors if we cannot parse the version.
2273 StringRef WhichHexagon = A->getValue();
2274 if (WhichHexagon.startswith("hexagonv")) {
2275 int Val;
2276 if (!WhichHexagon.substr(sizeof("hexagonv") - 1).getAsInteger(10, Val))
2277 return Val;
Matthew Curtis67814152012-12-06 14:16:43 +00002278 }
Rafael Espindolaf26d8bc2013-09-24 13:28:24 +00002279 if (WhichHexagon.startswith("v")) {
2280 int Val;
2281 if (!WhichHexagon.substr(1).getAsInteger(10, Val))
2282 return Val;
2283 }
2284
2285 // FIXME: should probably be an error.
2286 return 4;
Matthew Curtis67814152012-12-06 14:16:43 +00002287}
2288
2289StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
2290{
Rafael Espindolaf26d8bc2013-09-24 13:28:24 +00002291 int V = getHexagonVersion(Args);
2292 // FIXME: We don't support versions < 4. We should error on them.
2293 switch (V) {
2294 default:
2295 llvm_unreachable("Unexpected version");
2296 case 5:
2297 return "v5";
2298 case 4:
2299 return "v4";
2300 case 3:
2301 return "v3";
2302 case 2:
2303 return "v2";
2304 case 1:
2305 return "v1";
Matthew Curtis67814152012-12-06 14:16:43 +00002306 }
Matthew Curtis67814152012-12-06 14:16:43 +00002307}
Matthew Curtisb3489a02012-12-06 12:43:18 +00002308// End Hexagon
Daniel Dunbarf3cad362009-03-25 04:13:45 +00002309
Chris Lattner3a47c4e2010-03-04 21:07:38 +00002310/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
2311/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
2312/// Currently does not support anything else but compilation.
2313
Rafael Espindolaaf370e62013-03-18 18:10:27 +00002314TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple,
2315 const ArgList &Args)
2316 : ToolChain(D, Triple, Args) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00002317 // Path mangling to find libexec
2318 std::string Path(getDriver().Dir);
2319
2320 Path += "/../libexec";
2321 getProgramPaths().push_back(Path);
2322}
2323
2324TCEToolChain::~TCEToolChain() {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00002325}
2326
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00002327bool TCEToolChain::IsMathErrnoDefault() const {
2328 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00002329}
2330
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002331bool TCEToolChain::isPICDefault() const {
2332 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00002333}
2334
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00002335bool TCEToolChain::isPIEDefault() const {
2336 return false;
2337}
2338
Chandler Carruth7ce816a2012-11-19 03:52:03 +00002339bool TCEToolChain::isPICDefaultForced() const {
2340 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00002341}
2342
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002343// CloudABI - CloudABI tool chain which can call ld(1) directly.
2344
2345CloudABI::CloudABI(const Driver &D, const llvm::Triple &Triple,
2346 const ArgList &Args)
2347 : Generic_ELF(D, Triple, Args) {
2348 SmallString<128> P(getDriver().Dir);
2349 llvm::sys::path::append(P, "..", getTriple().str(), "lib");
2350 getFilePaths().push_back(P.str());
2351}
2352
2353void CloudABI::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2354 ArgStringList &CC1Args) const {
2355 if (DriverArgs.hasArg(options::OPT_nostdlibinc) &&
2356 DriverArgs.hasArg(options::OPT_nostdincxx))
2357 return;
2358
2359 SmallString<128> P(getDriver().Dir);
2360 llvm::sys::path::append(P, "..", getTriple().str(), "include/c++/v1");
2361 addSystemInclude(DriverArgs, CC1Args, P.str());
2362}
2363
2364void CloudABI::AddCXXStdlibLibArgs(const ArgList &Args,
2365 ArgStringList &CmdArgs) const {
2366 CmdArgs.push_back("-lc++");
2367 CmdArgs.push_back("-lc++abi");
2368 CmdArgs.push_back("-lunwind");
2369}
2370
2371Tool *CloudABI::buildLinker() const { return new tools::cloudabi::Link(*this); }
2372
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002373/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
2374
Rafael Espindola0e659592012-02-19 01:38:32 +00002375OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2376 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002377 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002378 getFilePaths().push_back("/usr/lib");
2379}
2380
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002381Tool *OpenBSD::buildAssembler() const {
2382 return new tools::openbsd::Assemble(*this);
2383}
2384
2385Tool *OpenBSD::buildLinker() const {
2386 return new tools::openbsd::Link(*this);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002387}
2388
Eli Friedman42f74f22012-08-08 23:57:20 +00002389/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
2390
2391Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2392 : Generic_ELF(D, Triple, Args) {
2393 getFilePaths().push_back(getDriver().Dir + "/../lib");
2394 getFilePaths().push_back("/usr/lib");
2395}
2396
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002397Tool *Bitrig::buildAssembler() const {
2398 return new tools::bitrig::Assemble(*this);
2399}
2400
2401Tool *Bitrig::buildLinker() const {
2402 return new tools::bitrig::Link(*this);
Eli Friedman42f74f22012-08-08 23:57:20 +00002403}
2404
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002405ToolChain::CXXStdlibType
2406Bitrig::GetCXXStdlibType(const ArgList &Args) const {
2407 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
2408 StringRef Value = A->getValue();
2409 if (Value == "libstdc++")
2410 return ToolChain::CST_Libstdcxx;
2411 if (Value == "libc++")
2412 return ToolChain::CST_Libcxx;
2413
2414 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2415 << A->getAsString(Args);
2416 }
2417 return ToolChain::CST_Libcxx;
2418}
2419
Eli Friedman42f74f22012-08-08 23:57:20 +00002420void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2421 ArgStringList &CC1Args) const {
2422 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2423 DriverArgs.hasArg(options::OPT_nostdincxx))
2424 return;
2425
Chandler Carruth8e6881d2012-10-08 21:31:38 +00002426 switch (GetCXXStdlibType(DriverArgs)) {
2427 case ToolChain::CST_Libcxx:
2428 addSystemInclude(DriverArgs, CC1Args,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002429 getDriver().SysRoot + "/usr/include/c++/v1");
Chandler Carruth8e6881d2012-10-08 21:31:38 +00002430 break;
2431 case ToolChain::CST_Libstdcxx:
2432 addSystemInclude(DriverArgs, CC1Args,
2433 getDriver().SysRoot + "/usr/include/c++/stdc++");
2434 addSystemInclude(DriverArgs, CC1Args,
2435 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00002436
Chandler Carruth8e6881d2012-10-08 21:31:38 +00002437 StringRef Triple = getTriple().str();
2438 if (Triple.startswith("amd64"))
2439 addSystemInclude(DriverArgs, CC1Args,
2440 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
2441 Triple.substr(5));
2442 else
2443 addSystemInclude(DriverArgs, CC1Args,
2444 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
2445 Triple);
2446 break;
2447 }
Eli Friedman42f74f22012-08-08 23:57:20 +00002448}
2449
2450void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
2451 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00002452 switch (GetCXXStdlibType(Args)) {
2453 case ToolChain::CST_Libcxx:
2454 CmdArgs.push_back("-lc++");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002455 CmdArgs.push_back("-lc++abi");
2456 CmdArgs.push_back("-lpthread");
Chandler Carruth8e6881d2012-10-08 21:31:38 +00002457 break;
2458 case ToolChain::CST_Libstdcxx:
2459 CmdArgs.push_back("-lstdc++");
2460 break;
2461 }
Eli Friedman42f74f22012-08-08 23:57:20 +00002462}
2463
Daniel Dunbar75358d22009-03-30 21:06:03 +00002464/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
2465
Rafael Espindola0e659592012-02-19 01:38:32 +00002466FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2467 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00002468
Chandler Carruth24248e32012-01-26 01:35:15 +00002469 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
2470 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00002471 if ((Triple.getArch() == llvm::Triple::x86 ||
2472 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00002473 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00002474 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
2475 else
2476 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00002477}
2478
David Chisnall1cc766f2013-11-09 15:10:56 +00002479ToolChain::CXXStdlibType
2480FreeBSD::GetCXXStdlibType(const ArgList &Args) const {
2481 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
2482 StringRef Value = A->getValue();
2483 if (Value == "libstdc++")
2484 return ToolChain::CST_Libstdcxx;
2485 if (Value == "libc++")
2486 return ToolChain::CST_Libcxx;
2487
2488 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2489 << A->getAsString(Args);
2490 }
2491 if (getTriple().getOSMajorVersion() >= 10)
2492 return ToolChain::CST_Libcxx;
2493 return ToolChain::CST_Libstdcxx;
2494}
2495
2496void FreeBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2497 ArgStringList &CC1Args) const {
2498 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2499 DriverArgs.hasArg(options::OPT_nostdincxx))
2500 return;
2501
2502 switch (GetCXXStdlibType(DriverArgs)) {
2503 case ToolChain::CST_Libcxx:
2504 addSystemInclude(DriverArgs, CC1Args,
2505 getDriver().SysRoot + "/usr/include/c++/v1");
2506 break;
2507 case ToolChain::CST_Libstdcxx:
2508 addSystemInclude(DriverArgs, CC1Args,
2509 getDriver().SysRoot + "/usr/include/c++/4.2");
2510 addSystemInclude(DriverArgs, CC1Args,
2511 getDriver().SysRoot + "/usr/include/c++/4.2/backward");
2512 break;
2513 }
2514}
2515
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002516Tool *FreeBSD::buildAssembler() const {
2517 return new tools::freebsd::Assemble(*this);
2518}
2519
2520Tool *FreeBSD::buildLinker() const {
2521 return new tools::freebsd::Link(*this);
Daniel Dunbar75358d22009-03-30 21:06:03 +00002522}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002523
Rafael Espindola27fa2362012-12-13 04:17:14 +00002524bool FreeBSD::UseSjLjExceptions() const {
2525 // FreeBSD uses SjLj exceptions on ARM oabi.
2526 switch (getTriple().getEnvironment()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002527 case llvm::Triple::GNUEABIHF:
Rafael Espindola27fa2362012-12-13 04:17:14 +00002528 case llvm::Triple::GNUEABI:
2529 case llvm::Triple::EABI:
2530 return false;
2531
2532 default:
2533 return (getTriple().getArch() == llvm::Triple::arm ||
2534 getTriple().getArch() == llvm::Triple::thumb);
2535 }
2536}
2537
Stephen Hines651f13c2014-04-23 16:59:28 -07002538bool FreeBSD::HasNativeLLVMSupport() const {
2539 return true;
2540}
2541
2542bool FreeBSD::isPIEDefault() const {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002543 return getSanitizerArgs().requiresPIE();
Stephen Hines651f13c2014-04-23 16:59:28 -07002544}
2545
Benjamin Kramer8e50a962011-02-02 18:59:27 +00002546/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
2547
Rafael Espindola0e659592012-02-19 01:38:32 +00002548NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2549 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00002550
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00002551 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00002552 // When targeting a 32-bit platform, try the special directory used on
2553 // 64-bit hosts, and only fall back to the main library directory if that
2554 // doesn't work.
2555 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
2556 // what all logic is needed to emulate the '=' prefix here.
Stephen Hines651f13c2014-04-23 16:59:28 -07002557 switch (Triple.getArch()) {
2558 case llvm::Triple::x86:
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00002559 getFilePaths().push_back("=/usr/lib/i386");
Stephen Hines651f13c2014-04-23 16:59:28 -07002560 break;
2561 case llvm::Triple::arm:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002562 case llvm::Triple::armeb:
Stephen Hines651f13c2014-04-23 16:59:28 -07002563 case llvm::Triple::thumb:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002564 case llvm::Triple::thumbeb:
Stephen Hines651f13c2014-04-23 16:59:28 -07002565 switch (Triple.getEnvironment()) {
2566 case llvm::Triple::EABI:
Stephen Hines651f13c2014-04-23 16:59:28 -07002567 case llvm::Triple::GNUEABI:
Stephen Hines651f13c2014-04-23 16:59:28 -07002568 getFilePaths().push_back("=/usr/lib/eabi");
2569 break;
Stephen Hines176edba2014-12-01 14:53:08 -08002570 case llvm::Triple::EABIHF:
2571 case llvm::Triple::GNUEABIHF:
2572 getFilePaths().push_back("=/usr/lib/eabihf");
2573 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07002574 default:
2575 getFilePaths().push_back("=/usr/lib/oabi");
2576 break;
2577 }
2578 break;
2579 case llvm::Triple::mips64:
2580 case llvm::Triple::mips64el:
2581 if (tools::mips::hasMipsAbiArg(Args, "o32"))
2582 getFilePaths().push_back("=/usr/lib/o32");
2583 else if (tools::mips::hasMipsAbiArg(Args, "64"))
2584 getFilePaths().push_back("=/usr/lib/64");
2585 break;
Stephen Hines176edba2014-12-01 14:53:08 -08002586 case llvm::Triple::ppc:
2587 getFilePaths().push_back("=/usr/lib/powerpc");
2588 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002589 case llvm::Triple::sparc:
2590 getFilePaths().push_back("=/usr/lib/sparc");
2591 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07002592 default:
2593 break;
2594 }
Chandler Carruth32f88be2012-01-25 11:18:20 +00002595
2596 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00002597 }
2598}
2599
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002600Tool *NetBSD::buildAssembler() const {
2601 return new tools::netbsd::Assemble(*this);
2602}
2603
2604Tool *NetBSD::buildLinker() const {
2605 return new tools::netbsd::Link(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00002606}
2607
Joerg Sonnenbergera7efaf92013-04-30 01:21:43 +00002608ToolChain::CXXStdlibType
2609NetBSD::GetCXXStdlibType(const ArgList &Args) const {
2610 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
2611 StringRef Value = A->getValue();
2612 if (Value == "libstdc++")
2613 return ToolChain::CST_Libstdcxx;
2614 if (Value == "libc++")
2615 return ToolChain::CST_Libcxx;
2616
2617 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
2618 << A->getAsString(Args);
2619 }
2620
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00002621 unsigned Major, Minor, Micro;
2622 getTriple().getOSVersion(Major, Minor, Micro);
Stephen Hines176edba2014-12-01 14:53:08 -08002623 if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 49) || Major == 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002624 switch (getArch()) {
Stephen Hines176edba2014-12-01 14:53:08 -08002625 case llvm::Triple::aarch64:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002626 case llvm::Triple::arm:
2627 case llvm::Triple::armeb:
2628 case llvm::Triple::thumb:
2629 case llvm::Triple::thumbeb:
Stephen Hines176edba2014-12-01 14:53:08 -08002630 case llvm::Triple::ppc:
2631 case llvm::Triple::ppc64:
2632 case llvm::Triple::ppc64le:
Stephen Hines651f13c2014-04-23 16:59:28 -07002633 case llvm::Triple::x86:
2634 case llvm::Triple::x86_64:
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00002635 return ToolChain::CST_Libcxx;
Stephen Hines651f13c2014-04-23 16:59:28 -07002636 default:
2637 break;
2638 }
Joerg Sonnenbergere69cca42013-10-14 20:13:05 +00002639 }
Joerg Sonnenbergera7efaf92013-04-30 01:21:43 +00002640 return ToolChain::CST_Libstdcxx;
2641}
2642
2643void NetBSD::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2644 ArgStringList &CC1Args) const {
2645 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2646 DriverArgs.hasArg(options::OPT_nostdincxx))
2647 return;
2648
2649 switch (GetCXXStdlibType(DriverArgs)) {
2650 case ToolChain::CST_Libcxx:
2651 addSystemInclude(DriverArgs, CC1Args,
2652 getDriver().SysRoot + "/usr/include/c++/");
2653 break;
2654 case ToolChain::CST_Libstdcxx:
2655 addSystemInclude(DriverArgs, CC1Args,
2656 getDriver().SysRoot + "/usr/include/g++");
2657 addSystemInclude(DriverArgs, CC1Args,
2658 getDriver().SysRoot + "/usr/include/g++/backward");
2659 break;
2660 }
2661}
2662
Chris Lattner38e317d2010-07-07 16:01:42 +00002663/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
2664
Rafael Espindola0e659592012-02-19 01:38:32 +00002665Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2666 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00002667 getFilePaths().push_back(getDriver().Dir + "/../lib");
2668 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00002669}
2670
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002671Tool *Minix::buildAssembler() const {
2672 return new tools::minix::Assemble(*this);
2673}
2674
2675Tool *Minix::buildLinker() const {
2676 return new tools::minix::Link(*this);
Chris Lattner38e317d2010-07-07 16:01:42 +00002677}
2678
David Chisnall31c46902012-02-15 13:39:01 +00002679/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2680
Rafael Espindola0e659592012-02-19 01:38:32 +00002681Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2682 const ArgList &Args)
2683 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00002684
2685 getProgramPaths().push_back(getDriver().getInstalledDir());
2686 if (getDriver().getInstalledDir() != getDriver().Dir)
2687 getProgramPaths().push_back(getDriver().Dir);
2688
2689 getFilePaths().push_back(getDriver().Dir + "/../lib");
2690 getFilePaths().push_back("/usr/lib");
2691}
2692
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00002693Tool *Solaris::buildAssembler() const {
2694 return new tools::solaris::Assemble(*this);
2695}
2696
2697Tool *Solaris::buildLinker() const {
2698 return new tools::solaris::Link(*this);
David Chisnall31c46902012-02-15 13:39:01 +00002699}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002700
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002701/// Distribution (very bare-bones at the moment).
Eli Friedman6b3454a2009-05-26 07:52:18 +00002702
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002703enum Distro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002704 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002705 DebianLenny,
2706 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00002707 DebianWheezy,
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002708 DebianJessie,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00002709 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00002710 RHEL4,
2711 RHEL5,
2712 RHEL6,
Rafael Espindola1a747822013-10-25 18:09:41 +00002713 Fedora,
Rafael Espindola76663342013-07-03 14:14:00 +00002714 OpenSUSE,
Douglas Gregor814638e2011-03-14 15:39:50 +00002715 UbuntuHardy,
2716 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00002717 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00002718 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002719 UbuntuLucid,
2720 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00002721 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00002722 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002723 UbuntuPrecise,
Rafael Espindolade39d172012-12-13 20:26:05 +00002724 UbuntuQuantal,
2725 UbuntuRaring,
Sylvestre Ledru1b034642013-06-13 11:52:27 +00002726 UbuntuSaucy,
Sylvestre Ledru24d91e62013-11-07 09:31:30 +00002727 UbuntuTrusty,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002728 UnknownDistro
2729};
2730
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002731static bool IsRedhat(enum Distro Distro) {
Rafael Espindola1a747822013-10-25 18:09:41 +00002732 return Distro == Fedora || (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002733}
2734
Rafael Espindola76663342013-07-03 14:14:00 +00002735static bool IsOpenSUSE(enum Distro Distro) {
2736 return Distro == OpenSUSE;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002737}
2738
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002739static bool IsDebian(enum Distro Distro) {
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002740 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002741}
2742
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002743static bool IsUbuntu(enum Distro Distro) {
Sylvestre Ledru24d91e62013-11-07 09:31:30 +00002744 return Distro >= UbuntuHardy && Distro <= UbuntuTrusty;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002745}
2746
Rafael Espindola9a478242013-10-28 23:14:34 +00002747static Distro DetectDistro(llvm::Triple::ArchType Arch) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002748 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
2749 llvm::MemoryBuffer::getFile("/etc/lsb-release");
2750 if (File) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002751 StringRef Data = File.get()->getBuffer();
Stephen Hines176edba2014-12-01 14:53:08 -08002752 SmallVector<StringRef, 16> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002753 Data.split(Lines, "\n");
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002754 Distro Version = UnknownDistro;
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002755 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2756 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
Thomas Schwinged52b4a92013-03-28 19:02:48 +00002757 Version = llvm::StringSwitch<Distro>(Lines[i].substr(17))
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002758 .Case("hardy", UbuntuHardy)
2759 .Case("intrepid", UbuntuIntrepid)
2760 .Case("jaunty", UbuntuJaunty)
2761 .Case("karmic", UbuntuKarmic)
2762 .Case("lucid", UbuntuLucid)
2763 .Case("maverick", UbuntuMaverick)
2764 .Case("natty", UbuntuNatty)
2765 .Case("oneiric", UbuntuOneiric)
2766 .Case("precise", UbuntuPrecise)
Rafael Espindolade39d172012-12-13 20:26:05 +00002767 .Case("quantal", UbuntuQuantal)
2768 .Case("raring", UbuntuRaring)
Sylvestre Ledru1b034642013-06-13 11:52:27 +00002769 .Case("saucy", UbuntuSaucy)
Sylvestre Ledru24d91e62013-11-07 09:31:30 +00002770 .Case("trusty", UbuntuTrusty)
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002771 .Default(UnknownDistro);
2772 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002773 }
2774
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002775 File = llvm::MemoryBuffer::getFile("/etc/redhat-release");
2776 if (File) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002777 StringRef Data = File.get()->getBuffer();
Rafael Espindola1a747822013-10-25 18:09:41 +00002778 if (Data.startswith("Fedora release"))
2779 return Fedora;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002780 if (Data.startswith("Red Hat Enterprise Linux") ||
2781 Data.startswith("CentOS")) {
2782 if (Data.find("release 6") != StringRef::npos)
2783 return RHEL6;
2784 else if (Data.find("release 5") != StringRef::npos)
2785 return RHEL5;
2786 else if (Data.find("release 4") != StringRef::npos)
2787 return RHEL4;
2788 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002789 return UnknownDistro;
2790 }
2791
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002792 File = llvm::MemoryBuffer::getFile("/etc/debian_version");
2793 if (File) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002794 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002795 if (Data[0] == '5')
2796 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002797 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00002798 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002799 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00002800 return DebianWheezy;
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002801 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2802 return DebianJessie;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002803 return UnknownDistro;
2804 }
2805
Rafael Espindola9a478242013-10-28 23:14:34 +00002806 if (llvm::sys::fs::exists("/etc/SuSE-release"))
Rafael Espindola76663342013-07-03 14:14:00 +00002807 return OpenSUSE;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002808
Rafael Espindola9a478242013-10-28 23:14:34 +00002809 if (llvm::sys::fs::exists("/etc/exherbo-release"))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00002810 return Exherbo;
2811
Rafael Espindola9a478242013-10-28 23:14:34 +00002812 if (llvm::sys::fs::exists("/etc/arch-release"))
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002813 return ArchLinux;
2814
Rafael Espindolac1da9812010-11-07 20:14:31 +00002815 return UnknownDistro;
2816}
2817
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002818/// \brief Get our best guess at the multiarch triple for a target.
2819///
2820/// Debian-based systems are starting to use a multiarch setup where they use
2821/// a target-triple directory in the library and header search paths.
2822/// Unfortunately, this triple does not align with the vanilla target triple,
2823/// so we provide a rough mapping here.
Stephen Hines651f13c2014-04-23 16:59:28 -07002824static std::string getMultiarchTriple(const llvm::Triple &TargetTriple,
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002825 StringRef SysRoot) {
2826 // For most architectures, just use whatever we have rather than trying to be
2827 // clever.
2828 switch (TargetTriple.getArch()) {
2829 default:
2830 return TargetTriple.str();
2831
2832 // We use the existence of '/lib/<triple>' as a directory to detect some
2833 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00002834 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2835 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00002836 case llvm::Triple::arm:
2837 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00002838 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2839 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2840 return "arm-linux-gnueabihf";
2841 } else {
2842 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2843 return "arm-linux-gnueabi";
2844 }
Chad Rosier0337efd2012-07-11 19:08:21 +00002845 return TargetTriple.str();
Stephen Hines651f13c2014-04-23 16:59:28 -07002846 case llvm::Triple::armeb:
2847 case llvm::Triple::thumbeb:
2848 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2849 if (llvm::sys::fs::exists(SysRoot + "/lib/armeb-linux-gnueabihf"))
2850 return "armeb-linux-gnueabihf";
2851 } else {
2852 if (llvm::sys::fs::exists(SysRoot + "/lib/armeb-linux-gnueabi"))
2853 return "armeb-linux-gnueabi";
2854 }
2855 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002856 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002857 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2858 return "i386-linux-gnu";
2859 return TargetTriple.str();
2860 case llvm::Triple::x86_64:
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002861 // We don't want this for x32, otherwise it will match x86_64 libs
2862 if (TargetTriple.getEnvironment() != llvm::Triple::GNUX32 &&
2863 llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002864 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002865 return TargetTriple.str();
Tim Northoverc264e162013-01-31 12:13:10 +00002866 case llvm::Triple::aarch64:
2867 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2868 return "aarch64-linux-gnu";
Tim Northover162579a2013-06-13 22:54:55 +00002869 return TargetTriple.str();
Stephen Hines651f13c2014-04-23 16:59:28 -07002870 case llvm::Triple::aarch64_be:
2871 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64_be-linux-gnu"))
2872 return "aarch64_be-linux-gnu";
2873 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00002874 case llvm::Triple::mips:
2875 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2876 return "mips-linux-gnu";
2877 return TargetTriple.str();
2878 case llvm::Triple::mipsel:
2879 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2880 return "mipsel-linux-gnu";
2881 return TargetTriple.str();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002882 case llvm::Triple::mips64:
2883 if (llvm::sys::fs::exists(SysRoot + "/lib/mips64-linux-gnu"))
2884 return "mips64-linux-gnu";
2885 if (llvm::sys::fs::exists(SysRoot + "/lib/mips64-linux-gnuabi64"))
2886 return "mips64-linux-gnuabi64";
2887 return TargetTriple.str();
2888 case llvm::Triple::mips64el:
2889 if (llvm::sys::fs::exists(SysRoot + "/lib/mips64el-linux-gnu"))
2890 return "mips64el-linux-gnu";
2891 if (llvm::sys::fs::exists(SysRoot + "/lib/mips64el-linux-gnuabi64"))
2892 return "mips64el-linux-gnuabi64";
2893 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002894 case llvm::Triple::ppc:
Sylvestre Ledrub7e86be2013-03-15 16:22:43 +00002895 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2896 return "powerpc-linux-gnuspe";
Chandler Carruth155c54c2012-02-26 09:03:21 +00002897 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2898 return "powerpc-linux-gnu";
2899 return TargetTriple.str();
2900 case llvm::Triple::ppc64:
2901 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2902 return "powerpc64-linux-gnu";
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00002903 case llvm::Triple::ppc64le:
2904 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64le-linux-gnu"))
2905 return "powerpc64le-linux-gnu";
Chandler Carruth155c54c2012-02-26 09:03:21 +00002906 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002907 }
2908}
2909
Chandler Carruth00646ba2012-01-25 11:24:24 +00002910static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2911 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2912}
2913
Stephen Hines651f13c2014-04-23 16:59:28 -07002914static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
Chandler Carruth2a9b7582013-10-29 10:27:30 +00002915 if (isMipsArch(Triple.getArch())) {
2916 // lib32 directory has a special meaning on MIPS targets.
2917 // It contains N32 ABI binaries. Use this folder if produce
2918 // code for N32 ABI only.
Stephen Hines651f13c2014-04-23 16:59:28 -07002919 if (tools::mips::hasMipsAbiArg(Args, "n32"))
Chandler Carruth2a9b7582013-10-29 10:27:30 +00002920 return "lib32";
2921 return Triple.isArch32Bit() ? "lib" : "lib64";
2922 }
Simon Atanasyan7a918882012-09-14 11:27:24 +00002923
Stephen Hines651f13c2014-04-23 16:59:28 -07002924 // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
Chandler Carruth2a9b7582013-10-29 10:27:30 +00002925 // using that variant while targeting other architectures causes problems
2926 // because the libraries are laid out in shared system roots that can't cope
Stephen Hines651f13c2014-04-23 16:59:28 -07002927 // with a 'lib32' library search path being considered. So we only enable
Chandler Carruth2a9b7582013-10-29 10:27:30 +00002928 // them when we know we may need it.
2929 //
2930 // FIXME: This is a bit of a hack. We should really unify this code for
Stephen Hines651f13c2014-04-23 16:59:28 -07002931 // reasoning about oslibdir spellings with the lib dir spellings in the
Chandler Carruth2a9b7582013-10-29 10:27:30 +00002932 // GCCInstallationDetector, but that is a more significant refactoring.
2933 if (Triple.getArch() == llvm::Triple::x86 ||
2934 Triple.getArch() == llvm::Triple::ppc)
Simon Atanasyan7a918882012-09-14 11:27:24 +00002935 return "lib32";
2936
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002937 if (Triple.getArch() == llvm::Triple::x86_64 &&
2938 Triple.getEnvironment() == llvm::Triple::GNUX32)
2939 return "libx32";
2940
Simon Atanasyan7a918882012-09-14 11:27:24 +00002941 return Triple.isArch32Bit() ? "lib" : "lib64";
2942}
2943
Rafael Espindola0e659592012-02-19 01:38:32 +00002944Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2945 : Generic_ELF(D, Triple, Args) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002946 GCCInstallation.init(D, Triple, Args);
2947 Multilibs = GCCInstallation.getMultilibs();
Chandler Carruth89088792012-01-24 20:08:17 +00002948 llvm::Triple::ArchType Arch = Triple.getArch();
Simon Atanasyanab5df752013-10-05 14:37:55 +00002949 std::string SysRoot = computeSysRoot();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002950
Rafael Espindola76663342013-07-03 14:14:00 +00002951 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at
Chandler Carruthef377442013-06-20 23:37:54 +00002952 // least) put various tools in a triple-prefixed directory off of the parent
2953 // of the GCC installation. We use the GCC triple here to ensure that we end
2954 // up with tools that support the same amount of cross compiling as the
2955 // detected GCC installation. For example, if we find a GCC installation
2956 // targeting x86_64, but it is a bi-arch GCC installation, it can also be
2957 // used to target i386.
2958 // FIXME: This seems unlikely to be Linux-specific.
Rafael Espindolaab784082011-09-01 16:25:49 +00002959 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002960 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002961 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002962
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002963 Linker = GetLinkerPath();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002964
Rafael Espindola9a478242013-10-28 23:14:34 +00002965 Distro Distro = DetectDistro(Arch);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002966
Rafael Espindola76663342013-07-03 14:14:00 +00002967 if (IsOpenSUSE(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002968 ExtraOpts.push_back("-z");
2969 ExtraOpts.push_back("relro");
2970 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002971
Douglas Gregorf0594d82011-03-06 19:11:49 +00002972 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002973 ExtraOpts.push_back("-X");
2974
Logan Chien94a71422012-09-02 09:30:11 +00002975 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002976 const bool IsMips = isMipsArch(Arch);
2977
2978 if (IsMips && !SysRoot.empty())
2979 ExtraOpts.push_back("--sysroot=" + SysRoot);
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002980
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002981 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2982 // and the MIPS ABI require .dynsym to be sorted in different ways.
2983 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2984 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002985 // Android loader does not support .gnu.hash.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00002986 if (!IsMips && !IsAndroid) {
Rafael Espindola76663342013-07-03 14:14:00 +00002987 if (IsRedhat(Distro) || IsOpenSUSE(Distro) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002988 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002989 ExtraOpts.push_back("--hash-style=gnu");
2990
Rafael Espindola76663342013-07-03 14:14:00 +00002991 if (IsDebian(Distro) || IsOpenSUSE(Distro) || Distro == UbuntuLucid ||
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002992 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2993 ExtraOpts.push_back("--hash-style=both");
2994 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002995
Chris Lattnerd753b562011-05-22 05:36:06 +00002996 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002997 ExtraOpts.push_back("--no-add-needed");
2998
Eli Friedman0b200f62011-06-02 21:36:53 +00002999 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola76663342013-07-03 14:14:00 +00003000 Distro == DebianJessie || IsOpenSUSE(Distro) ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00003001 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00003002 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00003003 ExtraOpts.push_back("--build-id");
3004
Rafael Espindola76663342013-07-03 14:14:00 +00003005 if (IsOpenSUSE(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00003006 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00003007
Chandler Carruthd2deee12011-10-03 05:28:29 +00003008 // The selection of paths to try here is designed to match the patterns which
3009 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
3010 // This was determined by running GCC in a fake filesystem, creating all
3011 // possible permutations of these directories, and seeing which ones it added
3012 // to the link paths.
3013 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00003014
Stephen Hines651f13c2014-04-23 16:59:28 -07003015 const std::string OSLibDir = getOSLibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00003016 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00003017
Chandler Carruthd1f73062011-11-06 23:09:05 +00003018 // Add the multilib suffixed paths where they are available.
3019 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00003020 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00003021 const std::string &LibPath = GCCInstallation.getParentLibPath();
Stephen Hines651f13c2014-04-23 16:59:28 -07003022 const Multilib &Multilib = GCCInstallation.getMultilib();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00003023
Chandler Carruth28803ba2013-07-31 00:37:07 +00003024 // Sourcery CodeBench MIPS toolchain holds some libraries under
Chandler Carruth76671ed2013-10-29 02:27:56 +00003025 // a biarch-like suffix of the GCC installation.
Stephen Hines651f13c2014-04-23 16:59:28 -07003026 addPathIfExists((GCCInstallation.getInstallPath() +
3027 Multilib.gccSuffix()),
3028 Paths);
Chandler Carruth28803ba2013-07-31 00:37:07 +00003029
3030 // GCC cross compiling toolchains will install target libraries which ship
3031 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as
3032 // any part of the GCC installation in
3033 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat
3034 // debatable, but is the reality today. We need to search this tree even
3035 // when we have a sysroot somewhere else. It is the responsibility of
Stephen Hines651f13c2014-04-23 16:59:28 -07003036 // whomever is doing the cross build targeting a sysroot using a GCC
Chandler Carruth28803ba2013-07-31 00:37:07 +00003037 // installation that is *not* within the system root to ensure two things:
3038 //
3039 // 1) Any DSOs that are linked in from this tree or from the install path
Stephen Hines176edba2014-12-01 14:53:08 -08003040 // above must be present on the system root and found via an
Chandler Carruth28803ba2013-07-31 00:37:07 +00003041 // appropriate rpath.
3042 // 2) There must not be libraries installed into
3043 // <prefix>/<triple>/<libdir> unless they should be preferred over
3044 // those within the system root.
3045 //
3046 // Note that this matches the GCC behavior. See the below comment for where
3047 // Clang diverges from GCC's behavior.
Stephen Hines651f13c2014-04-23 16:59:28 -07003048 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + OSLibDir +
3049 Multilib.osSuffix(),
Chandler Carruth28803ba2013-07-31 00:37:07 +00003050 Paths);
3051
Chandler Carruth9f314372012-04-06 16:32:06 +00003052 // If the GCC installation we found is inside of the sysroot, we want to
3053 // prefer libraries installed in the parent prefix of the GCC installation.
3054 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00003055 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00003056 // This usually happens when there is an external cross compiler on the
3057 // host system, and a more minimal sysroot available that is the target of
Chandler Carruth28803ba2013-07-31 00:37:07 +00003058 // the cross. Note that GCC does include some of these directories in some
3059 // configurations but this seems somewhere between questionable and simply
3060 // a bug.
Chandler Carruth9f314372012-04-06 16:32:06 +00003061 if (StringRef(LibPath).startswith(SysRoot)) {
Chandler Carruth9f314372012-04-06 16:32:06 +00003062 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
Stephen Hines651f13c2014-04-23 16:59:28 -07003063 addPathIfExists(LibPath + "/../" + OSLibDir, Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00003064 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00003065 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003066
3067 // Similar to the logic for GCC above, if we currently running Clang inside
3068 // of the requested system root, add its parent library paths to
3069 // those searched.
3070 // FIXME: It's not clear whether we should use the driver's installed
3071 // directory ('Dir' below) or the ResourceDir.
3072 if (StringRef(D.Dir).startswith(SysRoot)) {
3073 addPathIfExists(D.Dir + "/../lib/" + MultiarchTriple, Paths);
3074 addPathIfExists(D.Dir + "/../" + OSLibDir, Paths);
3075 }
3076
Chandler Carruthd1f73062011-11-06 23:09:05 +00003077 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
Stephen Hines651f13c2014-04-23 16:59:28 -07003078 addPathIfExists(SysRoot + "/lib/../" + OSLibDir, Paths);
Chandler Carruthd1f73062011-11-06 23:09:05 +00003079 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
Stephen Hines651f13c2014-04-23 16:59:28 -07003080 addPathIfExists(SysRoot + "/usr/lib/../" + OSLibDir, Paths);
Chandler Carruthd1f73062011-11-06 23:09:05 +00003081
Chandler Carruthd79486a2013-06-22 11:35:51 +00003082 // Try walking via the GCC triple path in case of biarch or multiarch GCC
Chandler Carruthd1f73062011-11-06 23:09:05 +00003083 // installations with strange symlinks.
Rafael Espindola79cabd02013-10-25 17:06:04 +00003084 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00003085 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Stephen Hines651f13c2014-04-23 16:59:28 -07003086 "/../../" + OSLibDir, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00003087
Stephen Hines651f13c2014-04-23 16:59:28 -07003088 // Add the 'other' biarch variant path
3089 Multilib BiarchSibling;
3090 if (GCCInstallation.getBiarchSibling(BiarchSibling)) {
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00003091 addPathIfExists(GCCInstallation.getInstallPath() +
Stephen Hines651f13c2014-04-23 16:59:28 -07003092 BiarchSibling.gccSuffix(), Paths);
3093 }
Chandler Carruth9f314372012-04-06 16:32:06 +00003094
Chandler Carruth28803ba2013-07-31 00:37:07 +00003095 // See comments above on the multilib variant for details of why this is
3096 // included even from outside the sysroot.
Stephen Hines651f13c2014-04-23 16:59:28 -07003097 const std::string &LibPath = GCCInstallation.getParentLibPath();
3098 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
3099 const Multilib &Multilib = GCCInstallation.getMultilib();
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00003100 addPathIfExists(LibPath + "/../" + GCCTriple.str() +
Stephen Hines651f13c2014-04-23 16:59:28 -07003101 "/lib" + Multilib.osSuffix(), Paths);
Chandler Carruth28803ba2013-07-31 00:37:07 +00003102
3103 // See comments above on the multilib variant for details of why this is
3104 // only included from within the sysroot.
3105 if (StringRef(LibPath).startswith(SysRoot))
Chandler Carruth9f314372012-04-06 16:32:06 +00003106 addPathIfExists(LibPath, Paths);
Chandler Carruthd2deee12011-10-03 05:28:29 +00003107 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003108
3109 // Similar to the logic for GCC above, if we are currently running Clang
3110 // inside of the requested system root, add its parent library path to those
3111 // searched.
3112 // FIXME: It's not clear whether we should use the driver's installed
3113 // directory ('Dir' below) or the ResourceDir.
3114 if (StringRef(D.Dir).startswith(SysRoot))
3115 addPathIfExists(D.Dir + "/../lib", Paths);
3116
Chandler Carruthfde8d142011-10-03 06:41:08 +00003117 addPathIfExists(SysRoot + "/lib", Paths);
3118 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00003119}
3120
3121bool Linux::HasNativeLLVMSupport() const {
3122 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00003123}
3124
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00003125Tool *Linux::buildLinker() const {
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00003126 return new tools::gnutools::Link(*this);
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00003127}
3128
3129Tool *Linux::buildAssembler() const {
Thomas Schwinge577bb0a2013-03-28 19:04:25 +00003130 return new tools::gnutools::Assemble(*this);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00003131}
3132
Simon Atanasyanab5df752013-10-05 14:37:55 +00003133std::string Linux::computeSysRoot() const {
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00003134 if (!getDriver().SysRoot.empty())
3135 return getDriver().SysRoot;
3136
3137 if (!GCCInstallation.isValid() || !isMipsArch(getTriple().getArch()))
3138 return std::string();
3139
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00003140 // Standalone MIPS toolchains use different names for sysroot folder
3141 // and put it into different places. Here we try to check some known
3142 // variants.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00003143
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00003144 const StringRef InstallDir = GCCInstallation.getInstallPath();
3145 const StringRef TripleStr = GCCInstallation.getTriple().str();
Stephen Hines651f13c2014-04-23 16:59:28 -07003146 const Multilib &Multilib = GCCInstallation.getMultilib();
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00003147
Chandler Carruthf4ad5ad2013-10-29 08:53:03 +00003148 std::string Path = (InstallDir + "/../../../../" + TripleStr + "/libc" +
Stephen Hines651f13c2014-04-23 16:59:28 -07003149 Multilib.osSuffix()).str();
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00003150
3151 if (llvm::sys::fs::exists(Path))
3152 return Path;
3153
Stephen Hines651f13c2014-04-23 16:59:28 -07003154 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
Simon Atanasyan4e30cdf2013-10-10 07:57:44 +00003155
3156 if (llvm::sys::fs::exists(Path))
3157 return Path;
3158
3159 return std::string();
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00003160}
3161
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003162void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3163 ArgStringList &CC1Args) const {
3164 const Driver &D = getDriver();
Simon Atanasyanab5df752013-10-05 14:37:55 +00003165 std::string SysRoot = computeSysRoot();
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003166
3167 if (DriverArgs.hasArg(options::OPT_nostdinc))
3168 return;
3169
3170 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00003171 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003172
3173 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola7b6301f2013-06-26 02:13:00 +00003174 SmallString<128> P(D.ResourceDir);
3175 llvm::sys::path::append(P, "include");
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003176 addSystemInclude(DriverArgs, CC1Args, P);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003177 }
3178
3179 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
3180 return;
3181
3182 // Check for configure-time C include directories.
3183 StringRef CIncludeDirs(C_INCLUDE_DIRS);
3184 if (CIncludeDirs != "") {
3185 SmallVector<StringRef, 5> dirs;
3186 CIncludeDirs.split(dirs, ":");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003187 for (StringRef dir : dirs) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003188 StringRef Prefix =
3189 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : "";
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003190 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003191 }
3192 return;
3193 }
3194
3195 // Lacking those, try to detect the correct set of system includes for the
3196 // target triple.
3197
Stephen Hines176edba2014-12-01 14:53:08 -08003198 // Add include directories specific to the selected multilib set and multilib.
3199 if (GCCInstallation.isValid()) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003200 const auto &Callback = Multilibs.includeDirsCallback();
Stephen Hines176edba2014-12-01 14:53:08 -08003201 if (Callback) {
3202 const auto IncludePaths = Callback(GCCInstallation.getInstallPath(),
3203 GCCInstallation.getTriple().str(),
3204 GCCInstallation.getMultilib());
3205 for (const auto &Path : IncludePaths)
3206 addExternCSystemIncludeIfExists(DriverArgs, CC1Args, Path);
3207 }
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00003208 }
3209
Chandler Carrutha4630892011-11-06 08:21:07 +00003210 // Implement generic Debian multiarch support.
3211 const StringRef X86_64MultiarchIncludeDirs[] = {
3212 "/usr/include/x86_64-linux-gnu",
3213
3214 // FIXME: These are older forms of multiarch. It's not clear that they're
3215 // in use in any released version of Debian, so we should consider
3216 // removing them.
Chandler Carruthd79486a2013-06-22 11:35:51 +00003217 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"
Chandler Carrutha4630892011-11-06 08:21:07 +00003218 };
3219 const StringRef X86MultiarchIncludeDirs[] = {
3220 "/usr/include/i386-linux-gnu",
3221
3222 // FIXME: These are older forms of multiarch. It's not clear that they're
3223 // in use in any released version of Debian, so we should consider
3224 // removing them.
Chandler Carruthd79486a2013-06-22 11:35:51 +00003225 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu",
Chandler Carrutha4630892011-11-06 08:21:07 +00003226 "/usr/include/i486-linux-gnu"
3227 };
Tim Northoverc264e162013-01-31 12:13:10 +00003228 const StringRef AArch64MultiarchIncludeDirs[] = {
3229 "/usr/include/aarch64-linux-gnu"
3230 };
Chandler Carrutha4630892011-11-06 08:21:07 +00003231 const StringRef ARMMultiarchIncludeDirs[] = {
3232 "/usr/include/arm-linux-gnueabi"
3233 };
Jiangning Liuff104a12012-07-31 08:06:29 +00003234 const StringRef ARMHFMultiarchIncludeDirs[] = {
3235 "/usr/include/arm-linux-gnueabihf"
3236 };
Eli Friedmand7df7852011-11-11 03:05:19 +00003237 const StringRef MIPSMultiarchIncludeDirs[] = {
3238 "/usr/include/mips-linux-gnu"
3239 };
3240 const StringRef MIPSELMultiarchIncludeDirs[] = {
3241 "/usr/include/mipsel-linux-gnu"
3242 };
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003243 const StringRef MIPS64MultiarchIncludeDirs[] = {
3244 "/usr/include/mips64-linux-gnu",
3245 "/usr/include/mips64-linux-gnuabi64"
3246 };
3247 const StringRef MIPS64ELMultiarchIncludeDirs[] = {
3248 "/usr/include/mips64el-linux-gnu",
3249 "/usr/include/mips64el-linux-gnuabi64"
3250 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00003251 const StringRef PPCMultiarchIncludeDirs[] = {
3252 "/usr/include/powerpc-linux-gnu"
3253 };
3254 const StringRef PPC64MultiarchIncludeDirs[] = {
3255 "/usr/include/powerpc64-linux-gnu"
3256 };
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003257 const StringRef PPC64LEMultiarchIncludeDirs[] = {
3258 "/usr/include/powerpc64le-linux-gnu"
3259 };
Chandler Carrutha4630892011-11-06 08:21:07 +00003260 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003261 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00003262 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003263 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00003264 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Stephen Hines651f13c2014-04-23 16:59:28 -07003265 } else if (getTriple().getArch() == llvm::Triple::aarch64 ||
Stephen Hines176edba2014-12-01 14:53:08 -08003266 getTriple().getArch() == llvm::Triple::aarch64_be) {
Tim Northoverc264e162013-01-31 12:13:10 +00003267 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003268 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00003269 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
3270 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
3271 else
3272 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00003273 } else if (getTriple().getArch() == llvm::Triple::mips) {
3274 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
3275 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
3276 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003277 } else if (getTriple().getArch() == llvm::Triple::mips64) {
3278 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs;
3279 } else if (getTriple().getArch() == llvm::Triple::mips64el) {
3280 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00003281 } else if (getTriple().getArch() == llvm::Triple::ppc) {
3282 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
3283 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
3284 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003285 } else if (getTriple().getArch() == llvm::Triple::ppc64le) {
3286 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00003287 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003288 for (StringRef Dir : MultiarchIncludeDirs) {
3289 if (llvm::sys::fs::exists(SysRoot + Dir)) {
3290 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir);
Chandler Carrutha4630892011-11-06 08:21:07 +00003291 break;
3292 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003293 }
3294
3295 if (getTriple().getOS() == llvm::Triple::RTEMS)
3296 return;
3297
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00003298 // Add an include of '/include' directly. This isn't provided by default by
3299 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
3300 // add even when Clang is acting as-if it were a system compiler.
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00003301 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00003302
Simon Atanasyan8e8e95c2013-04-20 08:15:03 +00003303 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003304}
3305
Stephen Hines176edba2014-12-01 14:53:08 -08003306/// \brief Helper to add the variant paths of a libstdc++ installation.
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00003307/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
Stephen Hines176edba2014-12-01 14:53:08 -08003308 StringRef GCCTriple,
3309 StringRef GCCMultiarchTriple,
3310 StringRef TargetMultiarchTriple,
Stephen Hines651f13c2014-04-23 16:59:28 -07003311 Twine IncludeSuffix,
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00003312 const ArgList &DriverArgs,
3313 ArgStringList &CC1Args) {
Stephen Hines176edba2014-12-01 14:53:08 -08003314 if (!llvm::sys::fs::exists(Base + Suffix))
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00003315 return false;
3316
Stephen Hines176edba2014-12-01 14:53:08 -08003317 addSystemInclude(DriverArgs, CC1Args, Base + Suffix);
3318
3319 // The vanilla GCC layout of libstdc++ headers uses a triple subdirectory. If
3320 // that path exists or we have neither a GCC nor target multiarch triple, use
3321 // this vanilla search path.
3322 if ((GCCMultiarchTriple.empty() && TargetMultiarchTriple.empty()) ||
3323 llvm::sys::fs::exists(Base + Suffix + "/" + GCCTriple + IncludeSuffix)) {
3324 addSystemInclude(DriverArgs, CC1Args,
3325 Base + Suffix + "/" + GCCTriple + IncludeSuffix);
3326 } else {
3327 // Otherwise try to use multiarch naming schemes which have normalized the
3328 // triples and put the triple before the suffix.
3329 //
3330 // GCC surprisingly uses *both* the GCC triple with a multilib suffix and
3331 // the target triple, so we support that here.
3332 addSystemInclude(DriverArgs, CC1Args,
3333 Base + "/" + GCCMultiarchTriple + Suffix + IncludeSuffix);
3334 addSystemInclude(DriverArgs, CC1Args,
3335 Base + "/" + TargetMultiarchTriple + Suffix);
3336 }
3337
3338 addSystemInclude(DriverArgs, CC1Args, Base + Suffix + "/backward");
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00003339 return true;
3340}
3341
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003342void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
3343 ArgStringList &CC1Args) const {
3344 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
3345 DriverArgs.hasArg(options::OPT_nostdincxx))
3346 return;
3347
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00003348 // Check if libc++ has been enabled and provide its include paths if so.
3349 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003350 const std::string LibCXXIncludePathCandidates[] = {
3351 // The primary location is within the Clang installation.
3352 // FIXME: We shouldn't hard code 'v1' here to make Clang future proof to
3353 // newer ABI versions.
3354 getDriver().Dir + "/../include/c++/v1",
3355
3356 // We also check the system as for a long time this is the only place Clang looked.
3357 // FIXME: We should really remove this. It doesn't make any sense.
3358 getDriver().SysRoot + "/usr/include/c++/v1"
3359 };
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003360 for (const auto &IncludePath : LibCXXIncludePathCandidates) {
3361 if (!llvm::sys::fs::exists(IncludePath))
Stephen Hines651f13c2014-04-23 16:59:28 -07003362 continue;
3363 // Add the first candidate that exists.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003364 addSystemInclude(DriverArgs, CC1Args, IncludePath);
Stephen Hines651f13c2014-04-23 16:59:28 -07003365 break;
3366 }
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00003367 return;
3368 }
3369
Chandler Carruthfc52f752012-01-25 08:04:13 +00003370 // We need a detected GCC installation on Linux to provide libstdc++'s
3371 // headers. We handled the libc++ case above.
3372 if (!GCCInstallation.isValid())
3373 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003374
Chandler Carruthabaa1d72011-11-06 10:31:01 +00003375 // By default, look for the C++ headers in an include directory adjacent to
3376 // the lib directory of the GCC installation. Note that this is expect to be
3377 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
3378 StringRef LibDir = GCCInstallation.getParentLibPath();
3379 StringRef InstallDir = GCCInstallation.getInstallPath();
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00003380 StringRef TripleStr = GCCInstallation.getTriple().str();
Stephen Hines651f13c2014-04-23 16:59:28 -07003381 const Multilib &Multilib = GCCInstallation.getMultilib();
Stephen Hines176edba2014-12-01 14:53:08 -08003382 const std::string GCCMultiarchTriple =
3383 getMultiarchTriple(GCCInstallation.getTriple(), getDriver().SysRoot);
3384 const std::string TargetMultiarchTriple =
3385 getMultiarchTriple(getTriple(), getDriver().SysRoot);
Chandler Carruth0affc672013-08-26 08:59:53 +00003386 const GCCVersion &Version = GCCInstallation.getVersion();
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00003387
Stephen Hines176edba2014-12-01 14:53:08 -08003388 // The primary search for libstdc++ supports multiarch variants.
Chandler Carruthf4ad5ad2013-10-29 08:53:03 +00003389 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
Stephen Hines176edba2014-12-01 14:53:08 -08003390 "/c++/" + Version.Text, TripleStr, GCCMultiarchTriple,
3391 TargetMultiarchTriple,
Stephen Hines651f13c2014-04-23 16:59:28 -07003392 Multilib.includeSuffix(), DriverArgs, CC1Args))
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00003393 return;
3394
Stephen Hines176edba2014-12-01 14:53:08 -08003395 // Otherwise, fall back on a bunch of options which don't use multiarch
3396 // layouts for simplicity.
Stephen Hines651f13c2014-04-23 16:59:28 -07003397 const std::string LibStdCXXIncludePathCandidates[] = {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00003398 // Gentoo is weird and places its headers inside the GCC install, so if the
Chandler Carruth0affc672013-08-26 08:59:53 +00003399 // first attempt to find the headers fails, try these patterns.
3400 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." +
3401 Version.MinorStr,
3402 InstallDir.str() + "/include/g++-v" + Version.MajorStr,
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00003403 // Android standalone toolchain has C++ headers in yet another place.
Chandler Carruth0affc672013-08-26 08:59:53 +00003404 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
Hal Finkel02014b42012-09-18 22:25:07 +00003405 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
3406 // without a subdirectory corresponding to the gcc version.
3407 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00003408 };
3409
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003410 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
Stephen Hines176edba2014-12-01 14:53:08 -08003411 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr,
3412 /*GCCMultiarchTriple*/ "",
3413 /*TargetMultiarchTriple*/ "",
3414 Multilib.includeSuffix(), DriverArgs, CC1Args))
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00003415 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00003416 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00003417}
3418
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00003419bool Linux::isPIEDefault() const {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003420 return getSanitizerArgs().requiresPIE();
Peter Collingbourne52ca70d2013-04-09 04:35:11 +00003421}
3422
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003423/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
3424
Rafael Espindola0e659592012-02-19 01:38:32 +00003425DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
3426 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003427
3428 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00003429 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00003430 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00003431 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003432
Daniel Dunbaree788e72009-12-21 18:54:17 +00003433 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003434 getFilePaths().push_back("/usr/lib");
John McCall8cfb7202013-04-11 22:55:55 +00003435 if (llvm::sys::fs::exists("/usr/lib/gcc47"))
3436 getFilePaths().push_back("/usr/lib/gcc47");
3437 else
3438 getFilePaths().push_back("/usr/lib/gcc44");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003439}
3440
Rafael Espindolaf48b93c2013-03-20 03:05:54 +00003441Tool *DragonFly::buildAssembler() const {
3442 return new tools::dragonfly::Assemble(*this);
3443}
3444
3445Tool *DragonFly::buildLinker() const {
3446 return new tools::dragonfly::Link(*this);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003447}
Robert Lytton4e490e22013-10-11 10:29:40 +00003448
3449
3450/// XCore tool chain
3451XCore::XCore(const Driver &D, const llvm::Triple &Triple,
3452 const ArgList &Args) : ToolChain(D, Triple, Args) {
3453 // ProgramPaths are found via 'PATH' environment variable.
3454}
3455
3456Tool *XCore::buildAssembler() const {
3457 return new tools::XCore::Assemble(*this);
3458}
3459
3460Tool *XCore::buildLinker() const {
3461 return new tools::XCore::Link(*this);
3462}
3463
3464bool XCore::isPICDefault() const {
3465 return false;
3466}
3467
3468bool XCore::isPIEDefault() const {
3469 return false;
3470}
3471
3472bool XCore::isPICDefaultForced() const {
3473 return false;
3474}
3475
3476bool XCore::SupportsProfiling() const {
3477 return false;
3478}
3479
3480bool XCore::hasBlocksRuntime() const {
3481 return false;
3482}
3483
Robert Lytton4e490e22013-10-11 10:29:40 +00003484void XCore::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3485 ArgStringList &CC1Args) const {
3486 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
3487 DriverArgs.hasArg(options::OPT_nostdlibinc))
3488 return;
3489 if (const char *cl_include_dir = getenv("XCC_C_INCLUDE_PATH")) {
3490 SmallVector<StringRef, 4> Dirs;
3491 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
3492 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
3493 ArrayRef<StringRef> DirVec(Dirs);
3494 addSystemIncludes(DriverArgs, CC1Args, DirVec);
3495 }
3496}
3497
3498void XCore::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
3499 llvm::opt::ArgStringList &CC1Args) const {
3500 CC1Args.push_back("-nostdsysteminc");
3501}
3502
3503void XCore::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
3504 ArgStringList &CC1Args) const {
3505 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
Stephen Hines176edba2014-12-01 14:53:08 -08003506 DriverArgs.hasArg(options::OPT_nostdlibinc) ||
3507 DriverArgs.hasArg(options::OPT_nostdincxx))
Robert Lytton4e490e22013-10-11 10:29:40 +00003508 return;
3509 if (const char *cl_include_dir = getenv("XCC_CPLUS_INCLUDE_PATH")) {
3510 SmallVector<StringRef, 4> Dirs;
3511 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,'\0'};
3512 StringRef(cl_include_dir).split(Dirs, StringRef(EnvPathSeparatorStr));
3513 ArrayRef<StringRef> DirVec(Dirs);
3514 addSystemIncludes(DriverArgs, CC1Args, DirVec);
3515 }
3516}
3517
3518void XCore::AddCXXStdlibLibArgs(const ArgList &Args,
3519 ArgStringList &CmdArgs) const {
3520 // We don't output any lib args. This is handled by xcc.
3521}