blob: 5b8f5c98065ebe64e0dda4c63ff846114dddd8a3 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar59e5e882009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000011#include "SanitizerArgs.h"
12#include "clang/Basic/ObjCRuntime.h"
13#include "clang/Basic/Version.h"
Daniel Dunbardac54a82009-03-25 04:13:45 +000014#include "clang/Driver/Arg.h"
15#include "clang/Driver/ArgList.h"
Daniel Dunbar6232d342010-05-20 21:48:38 +000016#include "clang/Driver/Compilation.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000017#include "clang/Driver/Driver.h"
Daniel Dunbaraabb0b12009-03-25 06:12:34 +000018#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbaraa767372009-11-19 00:15:11 +000019#include "clang/Driver/OptTable.h"
Daniel Dunbaraabb0b12009-03-25 06:12:34 +000020#include "clang/Driver/Option.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000021#include "clang/Driver/Options.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/ADT/STLExtras.h"
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000023#include "llvm/ADT/SmallString.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000024#include "llvm/ADT/StringExtras.h"
Bob Wilson997a97f2011-10-07 00:37:57 +000025#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +000026#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000027#include "llvm/Support/FileSystem.h"
Rafael Espindolac8f008f2010-11-07 20:14:31 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000031#include "llvm/Support/system_error.h"
NAKAMURA Takumi067fcb52012-12-04 14:31:59 +000032
33// FIXME: This needs to be listed last until we fix the broken include guards
34// in these files and the LLVM config.h files.
35#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
36
Daniel Dunbarb5023e92009-04-10 21:00:07 +000037#include <cstdlib> // ::getenv
38
Daniel Dunbar59e5e882009-03-20 00:20:03 +000039using namespace clang::driver;
40using namespace clang::driver::toolchains;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000041using namespace clang;
Daniel Dunbar59e5e882009-03-20 00:20:03 +000042
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +000043/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +000044
Chandler Carruthd7fa2e02012-01-31 02:21:20 +000045Darwin::Darwin(const Driver &D, const llvm::Triple& Triple)
John McCall5fb5df92012-06-20 06:18:46 +000046 : ToolChain(D, Triple), TargetInitialized(false)
Daniel Dunbar6276f992009-09-18 08:15:13 +000047{
Bob Wilson9d3f7af2012-01-31 21:30:03 +000048 // Compute the initial Darwin version from the triple
49 unsigned Major, Minor, Micro;
Bob Wilsona2234012012-01-31 22:43:59 +000050 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
51 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
52 Triple.getOSName();
53 llvm::raw_string_ostream(MacosxVersionMin)
54 << Major << '.' << Minor << '.' << Micro;
55
Bob Wilson9d3f7af2012-01-31 21:30:03 +000056 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
57 // It should be removed when we stop supporting that.
58 DarwinVersion[0] = Minor + 4;
59 DarwinVersion[1] = Micro;
60 DarwinVersion[2] = 0;
Chad Rosier266c6202012-05-09 18:46:30 +000061
62 // Compute the initial iOS version from the triple
Chad Rosierbf8628e2012-05-09 18:51:13 +000063 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosier266c6202012-05-09 18:46:30 +000064 llvm::raw_string_ostream(iOSVersionMin)
65 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar6276f992009-09-18 08:15:13 +000066}
67
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +000068types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
69 types::ID Ty = types::lookupTypeForExtension(Ext);
70
71 // Darwin always preprocesses assembly files (unless -x is used explicitly).
72 if (Ty == types::TY_PP_Asm)
73 return types::TY_Asm;
74
75 return Ty;
76}
77
Daniel Dunbar62123a12010-09-17 00:24:52 +000078bool Darwin::HasNativeLLVMSupport() const {
79 return true;
80}
81
John McCall24fc0de2011-07-06 00:26:06 +000082/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall5fb5df92012-06-20 06:18:46 +000083ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
Bob Wilson5ad5a952012-11-09 01:59:30 +000084 if (isTargetIPhoneOS())
John McCall5fb5df92012-06-20 06:18:46 +000085 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson5ad5a952012-11-09 01:59:30 +000086 if (isNonFragile)
87 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
88 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall24fc0de2011-07-06 00:26:06 +000089}
90
John McCall7959fee2011-09-09 20:41:01 +000091/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
92bool Darwin::hasBlocksRuntime() const {
93 if (isTargetIPhoneOS())
94 return !isIPhoneOSVersionLT(3, 2);
95 else
96 return !isMacosxVersionLT(10, 6);
97}
98
Chris Lattner0e62c1c2011-07-23 10:55:15 +000099static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilson997a97f2011-10-07 00:37:57 +0000100 return llvm::StringSwitch<const char*>(Value)
101 .Case("armv6k", "armv6")
Bob Wilson743bf672013-03-04 22:37:49 +0000102 .Case("armv6m", "armv6m")
Bob Wilson997a97f2011-10-07 00:37:57 +0000103 .Case("armv5tej", "armv5")
104 .Case("xscale", "xscale")
105 .Case("armv4t", "armv4t")
106 .Case("armv7", "armv7")
107 .Cases("armv7a", "armv7-a", "armv7")
108 .Cases("armv7r", "armv7-r", "armv7")
Bob Wilson743bf672013-03-04 22:37:49 +0000109 .Cases("armv7em", "armv7e-m", "armv7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000110 .Cases("armv7f", "armv7-f", "armv7f")
111 .Cases("armv7k", "armv7-k", "armv7k")
Bob Wilson743bf672013-03-04 22:37:49 +0000112 .Cases("armv7m", "armv7-m", "armv7m")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000113 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilson997a97f2011-10-07 00:37:57 +0000114 .Default(0);
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000115}
116
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000117static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilson997a97f2011-10-07 00:37:57 +0000118 return llvm::StringSwitch<const char *>(Value)
119 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
120 .Cases("arm10e", "arm10tdmi", "armv5")
121 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
122 .Case("xscale", "xscale")
Bob Wilson743bf672013-03-04 22:37:49 +0000123 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6")
124 .Case("cortex-m0", "armv6m")
125 .Cases("cortex-a8", "cortex-r4", "cortex-a9", "cortex-a15", "armv7")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000126 .Case("cortex-a9-mp", "armv7f")
Bob Wilson743bf672013-03-04 22:37:49 +0000127 .Case("cortex-m3", "armv7m")
128 .Case("cortex-m4", "armv7em")
Bob Wilsond7cf1042012-09-29 23:52:50 +0000129 .Case("swift", "armv7s")
Bob Wilson997a97f2011-10-07 00:37:57 +0000130 .Default(0);
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000131}
132
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000133StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000134 switch (getTriple().getArch()) {
135 default:
136 return getArchName();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000137
Douglas Gregord9bb1522011-03-06 19:11:49 +0000138 case llvm::Triple::thumb:
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000139 case llvm::Triple::arm: {
140 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +0000141 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000142 return Arch;
143
144 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smithbd55daf2012-11-01 04:30:05 +0000145 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000146 return Arch;
147
148 return "arm";
149 }
150 }
151}
152
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000153Darwin::~Darwin() {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000154 // Free tool implementations.
155 for (llvm::DenseMap<unsigned, Tool*>::iterator
156 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
157 delete it->second;
158}
159
Chad Rosierd3a0f952011-09-20 20:44:06 +0000160std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
161 types::ID InputType) const {
162 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000163
164 // If the target isn't initialized (e.g., an unknown Darwin platform, return
165 // the default triple).
166 if (!isTargetInitialized())
167 return Triple.getTriple();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000168
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000169 SmallString<16> Str;
Benjamin Kramerddefa6d2012-03-10 20:55:36 +0000170 Str += isTargetIPhoneOS() ? "ios" : "macosx";
171 Str += getTargetVersion().getAsString();
172 Triple.setOSName(Str);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000173
174 return Triple.getTriple();
175}
176
David Blaikie68e081d2011-12-20 02:48:34 +0000177void Generic_ELF::anchor() {}
178
Rafael Espindola88b55ea2013-03-18 17:25:58 +0000179Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Argyrios Kyrtzidis3169e802012-05-21 20:11:54 +0000180 Action::ActionClass Key = JA.getKind();
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +0000181
Rafael Espindola2f69d402013-03-18 15:33:26 +0000182 if (getDriver().ShouldUseClangCompiler(JA)) {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +0000183 // FIXME: This seems like a hacky way to choose clang frontend.
184 Key = Action::AnalyzeJobClass;
185 }
186
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000187 Tool *&T = Tools[Key];
188 if (!T) {
189 switch (Key) {
190 case Action::InputClass:
191 case Action::BindArchClass:
David Blaikie83d382b2011-09-23 05:06:16 +0000192 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000193 case Action::PreprocessJobClass:
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000194 case Action::AnalyzeJobClass:
Ted Kremenekf7639e12012-03-06 20:06:33 +0000195 case Action::MigrateJobClass:
Daniel Dunbarf64f5302009-03-29 22:27:40 +0000196 case Action::PrecompileJobClass:
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000197 case Action::CompileJobClass:
Bob Wilsondecc03e2012-11-23 06:14:39 +0000198 T = new tools::Clang(*this); break;
Daniel Dunbar6232d342010-05-20 21:48:38 +0000199 case Action::AssembleJobClass: {
Rafael Espindola248e2192013-03-18 17:52:57 +0000200 if (useIntegratedAs(C.getArgs()))
Daniel Dunbar6232d342010-05-20 21:48:38 +0000201 T = new tools::ClangAs(*this);
202 else
203 T = new tools::darwin::Assemble(*this);
204 break;
205 }
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000206 case Action::LinkJobClass:
Daniel Dunbar5095b292009-09-04 17:39:02 +0000207 T = new tools::darwin::Link(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000208 case Action::LipoJobClass:
209 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar88299622010-06-04 18:28:36 +0000210 case Action::DsymutilJobClass:
211 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopher551ef452011-08-23 17:56:55 +0000212 case Action::VerifyJobClass:
213 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000214 }
215 }
216
217 return *T;
218}
219
Daniel Dunbar26d482a2009-09-18 08:15:03 +0000220
Chandler Carruthd7fa2e02012-01-31 02:21:20 +0000221DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
222 : Darwin(D, Triple)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000223{
Daniel Dunbar00aff042010-09-17 08:22:12 +0000224 getProgramPaths().push_back(getDriver().getInstalledDir());
225 if (getDriver().getInstalledDir() != getDriver().Dir)
226 getProgramPaths().push_back(getDriver().Dir);
227
Daniel Dunbar6276f992009-09-18 08:15:13 +0000228 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbar88979912010-08-01 22:29:51 +0000229 getProgramPaths().push_back(getDriver().getInstalledDir());
230 if (getDriver().getInstalledDir() != getDriver().Dir)
231 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar6276f992009-09-18 08:15:13 +0000232}
233
John McCall31168b02011-06-15 23:02:42 +0000234void DarwinClang::AddLinkARCArgs(const ArgList &Args,
235 ArgStringList &CmdArgs) const {
Eric Christopher551ef452011-08-23 17:56:55 +0000236
237 CmdArgs.push_back("-force_load");
John McCall31168b02011-06-15 23:02:42 +0000238 llvm::sys::Path P(getDriver().ClangExecutable);
239 P.eraseComponent(); // 'clang'
240 P.eraseComponent(); // 'bin'
241 P.appendComponent("lib");
242 P.appendComponent("arc");
243 P.appendComponent("libarclite_");
244 std::string s = P.str();
245 // Mash in the platform.
Argyrios Kyrtzidis058b4512011-10-18 17:40:15 +0000246 if (isTargetIOSSimulator())
247 s += "iphonesimulator";
248 else if (isTargetIPhoneOS())
John McCall31168b02011-06-15 23:02:42 +0000249 s += "iphoneos";
John McCall31168b02011-06-15 23:02:42 +0000250 else
251 s += "macosx";
252 s += ".a";
253
254 CmdArgs.push_back(Args.MakeArgString(s));
255}
256
Eric Christopherc235d0c62011-06-22 17:41:40 +0000257void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopher551ef452011-08-23 17:56:55 +0000258 ArgStringList &CmdArgs,
Alexey Samsonov8368b372012-11-21 14:17:42 +0000259 const char *DarwinStaticLib,
260 bool AlwaysLink) const {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000261 llvm::sys::Path P(getDriver().ResourceDir);
262 P.appendComponent("lib");
263 P.appendComponent("darwin");
264 P.appendComponent(DarwinStaticLib);
Eric Christopher551ef452011-08-23 17:56:55 +0000265
Eric Christopherc235d0c62011-06-22 17:41:40 +0000266 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov8368b372012-11-21 14:17:42 +0000267 // not have compiler-rt checked out or integrated into their build (unless
268 // we explicitly force linking with this library).
Eric Christopherc235d0c62011-06-22 17:41:40 +0000269 bool Exists;
Alexey Samsonov8368b372012-11-21 14:17:42 +0000270 if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
Eric Christopherc235d0c62011-06-22 17:41:40 +0000271 CmdArgs.push_back(Args.MakeArgString(P.str()));
272}
273
Daniel Dunbar6276f992009-09-18 08:15:13 +0000274void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
275 ArgStringList &CmdArgs) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000276 // Darwin only supports the compiler-rt based runtime libraries.
277 switch (GetRuntimeLibType(Args)) {
278 case ToolChain::RLT_CompilerRT:
279 break;
280 default:
281 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smithbd55daf2012-11-01 04:30:05 +0000282 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000283 return;
284 }
285
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000286 // Darwin doesn't support real static executables, don't link any runtime
287 // libraries with -static.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000288 if (Args.hasArg(options::OPT_static) ||
289 Args.hasArg(options::OPT_fapple_kext) ||
290 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar6276f992009-09-18 08:15:13 +0000291 return;
Daniel Dunbar6276f992009-09-18 08:15:13 +0000292
293 // Reject -static-libgcc for now, we can deal with this when and if someone
294 // cares. This is useful in situations where someone wants to statically link
295 // something like libstdc++, and needs its runtime support routines.
296 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000297 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000298 << A->getAsString(Args);
299 return;
300 }
301
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000302 // If we are building profile support, link that library in.
303 if (Args.hasArg(options::OPT_fprofile_arcs) ||
304 Args.hasArg(options::OPT_fprofile_generate) ||
305 Args.hasArg(options::OPT_fcreate_profile) ||
306 Args.hasArg(options::OPT_coverage)) {
307 // Select the appropriate runtime library for the target.
308 if (isTargetIPhoneOS()) {
309 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
310 } else {
311 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
312 }
313 }
314
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000315 SanitizerArgs Sanitize(getDriver(), Args);
316
Alexey Samsonovcc429802012-11-16 12:53:14 +0000317 // Add Ubsan runtime library, if required.
318 if (Sanitize.needsUbsanRt()) {
Alexey Samsonov969be242013-01-21 08:45:02 +0000319 if (isTargetIPhoneOS()) {
Alexey Samsonovcc429802012-11-16 12:53:14 +0000320 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
321 << "-fsanitize=undefined";
322 } else {
Alexey Samsonov8368b372012-11-21 14:17:42 +0000323 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonovcc429802012-11-16 12:53:14 +0000324
325 // The Ubsan runtime library requires C++.
326 AddCXXStdlibLibArgs(Args, CmdArgs);
327 }
328 }
329
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000330 // Add ASAN runtime library, if required. Dynamic libraries and bundles
331 // should not be linked with the runtime library.
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000332 if (Sanitize.needsAsanRt()) {
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000333 if (Args.hasArg(options::OPT_dynamiclib) ||
Alexey Samsonovcc429802012-11-16 12:53:14 +0000334 Args.hasArg(options::OPT_bundle)) {
335 // Assume the binary will provide the ASan runtime.
336 } else if (isTargetIPhoneOS()) {
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000337 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000338 << "-fsanitize=address";
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000339 } else {
Alexander Potapenkof6b56972013-01-22 09:16:03 +0000340 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx_dynamic.dylib", true);
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000341
Alexander Potapenkof6b56972013-01-22 09:16:03 +0000342 // The ASAN runtime library requires C++.
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000343 AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000344 }
345 }
346
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000347 // Otherwise link libSystem, then the dynamic runtime library, and finally any
348 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000349 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000350
351 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar15c89422010-01-27 00:56:37 +0000352 if (isTargetIPhoneOS()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000353 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
354 // it never went into the SDK.
Bob Wilson102be442011-10-07 17:54:41 +0000355 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
356 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
357 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000358
Daniel Dunbard1076382011-04-18 23:48:36 +0000359 // We currently always need a static runtime library for iOS.
Eric Christopherc235d0c62011-06-22 17:41:40 +0000360 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000361 } else {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000362 // The dynamic runtime library was merged with libSystem for 10.6 and
363 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000364 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000365 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000366 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000367 CmdArgs.push_back("-lgcc_s.10.5");
368
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000369 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000370 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000371 // omitted from 10.4.dylib.
372 //
373 // Unfortunately, that turned out to not be true, because Darwin system
374 // headers can still use eprintf on i386, and it is not exported from
375 // libSystem. Therefore, we still must provide a runtime library just for
376 // the tiny tiny handful of projects that *might* use that symbol.
377 if (isMacosxVersionLT(10, 5)) {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000378 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000379 } else {
380 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopherc235d0c62011-06-22 17:41:40 +0000381 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
382 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000383 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000384 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000385}
386
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000387void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000388 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000389
Daniel Dunbar455a0492012-08-17 18:43:50 +0000390 // Support allowing the SDKROOT environment variable used by xcrun and other
391 // Xcode tools to define the default sysroot, by making it the default for
392 // isysroot.
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000393 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
394 // Warn if the path does not exist.
395 bool Exists;
396 if (llvm::sys::fs::exists(A->getValue(), Exists) || !Exists)
397 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
398 } else {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000399 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbarb2543042013-01-15 20:33:56 +0000400 // We only use this value as the default if it is an absolute path,
401 // exists, and it is not the root path.
402 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
403 StringRef(env) != "/") {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000404 Args.append(Args.MakeSeparateArg(
405 0, Opts.getOption(options::OPT_isysroot), env));
406 }
407 }
408 }
409
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000410 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000411 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
412 Arg *iOSSimVersion = Args.getLastArg(
413 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman027e9c32012-01-11 02:41:15 +0000414
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000415 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000416 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000417 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000418 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
419 iOSVersion = iOSSimVersion = 0;
420 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000421 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000422 << iOSVersion->getAsString(Args)
423 << iOSSimVersion->getAsString(Args);
424 iOSSimVersion = 0;
425 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosier64707fe2011-08-31 20:56:25 +0000426 // If no deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000427 // environment defines.
Chad Rosier64707fe2011-08-31 20:56:25 +0000428 StringRef OSXTarget;
429 StringRef iOSTarget;
430 StringRef iOSSimTarget;
431 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
432 OSXTarget = env;
433 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
434 iOSTarget = env;
435 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
436 iOSSimTarget = env;
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000437
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000438 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosier64707fe2011-08-31 20:56:25 +0000439 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif5d3231c2012-04-18 10:59:08 +0000440 // based on -isysroot.
Chad Rosier64707fe2011-08-31 20:56:25 +0000441 if (iOSTarget.empty()) {
442 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
443 StringRef first, second;
Richard Smithbd55daf2012-11-01 04:30:05 +0000444 StringRef isysroot = A->getValue();
Chad Rosier64707fe2011-08-31 20:56:25 +0000445 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
446 if (second != "")
447 iOSTarget = second.substr(0,3);
448 }
449 }
Daniel Dunbard54669d2010-01-26 01:45:19 +0000450
Chad Rosierfe6fd362011-09-28 00:46:32 +0000451 // If no OSX or iOS target has been specified and we're compiling for armv7,
452 // go ahead as assume we're targeting iOS.
Chad Rosier7b1fee12012-05-09 18:55:57 +0000453 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilsond7cf1042012-09-29 23:52:50 +0000454 (getDarwinArchName(Args) == "armv7" ||
455 getDarwinArchName(Args) == "armv7s"))
Chad Rosierf761fe92012-05-09 18:09:58 +0000456 iOSTarget = iOSVersionMin;
Chad Rosierfe6fd362011-09-28 00:46:32 +0000457
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000458 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000459 //
460 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000461
462 // Do not allow conflicts with the iOS simulator target.
Chad Rosier64707fe2011-08-31 20:56:25 +0000463 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000464 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000465 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosier64707fe2011-08-31 20:56:25 +0000466 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000467 "IPHONEOS_DEPLOYMENT_TARGET");
468 }
469
470 // Allow conflicts among OSX and iOS for historical reasons, but choose the
471 // default platform.
Chad Rosier64707fe2011-08-31 20:56:25 +0000472 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000473 if (getTriple().getArch() == llvm::Triple::arm ||
474 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosier64707fe2011-08-31 20:56:25 +0000475 OSXTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000476 else
Chad Rosier64707fe2011-08-31 20:56:25 +0000477 iOSTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000478 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000479
Chad Rosier64707fe2011-08-31 20:56:25 +0000480 if (!OSXTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000481 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000482 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
483 Args.append(OSXVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000484 } else if (!iOSTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000485 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000486 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
487 Args.append(iOSVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000488 } else if (!iOSSimTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000489 const Option O = Opts.getOption(
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000490 options::OPT_mios_simulator_version_min_EQ);
491 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
492 Args.append(iOSSimVersion);
Daniel Dunbard54669d2010-01-26 01:45:19 +0000493 } else {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000494 // Otherwise, assume we are targeting OS X.
Michael J. Spencerfc790902012-10-19 22:36:40 +0000495 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000496 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
497 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000498 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000499 }
Mike Stump11289f42009-09-09 15:08:12 +0000500
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000501 // Reject invalid architecture combinations.
502 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
503 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000504 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000505 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
506 }
507
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000508 // Set the tool chain target information.
509 unsigned Major, Minor, Micro;
510 bool HadExtra;
511 if (OSXVersion) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000512 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000513 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000514 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000515 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000516 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000517 << OSXVersion->getAsString(Args);
518 } else {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000519 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
520 assert(Version && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000521 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman027e9c32012-01-11 02:41:15 +0000522 Micro, HadExtra) || HadExtra ||
523 Major >= 10 || Minor >= 100 || Micro >= 100)
524 getDriver().Diag(diag::err_drv_invalid_version_number)
525 << Version->getAsString(Args);
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000526 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000527
Daniel Dunbarb1189432011-04-30 04:18:16 +0000528 bool IsIOSSim = bool(iOSSimVersion);
529
530 // In GCC, the simulator historically was treated as being OS X in some
531 // contexts, like determining the link logic, despite generally being called
532 // with an iOS deployment target. For compatibility, we detect the
533 // simulator as iOS + x86, and treat it differently in a few contexts.
534 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
535 getTriple().getArch() == llvm::Triple::x86_64))
536 IsIOSSim = true;
537
538 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000539}
540
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000541void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000542 ArgStringList &CmdArgs) const {
543 CXXStdlibType Type = GetCXXStdlibType(Args);
544
545 switch (Type) {
546 case ToolChain::CST_Libcxx:
547 CmdArgs.push_back("-lc++");
548 break;
549
550 case ToolChain::CST_Libstdcxx: {
551 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
552 // it was previously found in the gcc lib dir. However, for all the Darwin
553 // platforms we care about it was -lstdc++.6, so we search for that
554 // explicitly if we can't see an obvious -lstdc++ candidate.
555
556 // Check in the sysroot first.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000557 bool Exists;
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000558 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000559 llvm::sys::Path P(A->getValue());
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000560 P.appendComponent("usr");
561 P.appendComponent("lib");
562 P.appendComponent("libstdc++.dylib");
563
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000564 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000565 P.eraseComponent();
566 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000567 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000568 CmdArgs.push_back(Args.MakeArgString(P.str()));
569 return;
570 }
571 }
572 }
573
574 // Otherwise, look in the root.
Bob Wilson1a9ad0f2011-11-11 07:47:04 +0000575 // FIXME: This should be removed someday when we don't have to care about
576 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000577 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
578 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000579 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
580 return;
581 }
582
583 // Otherwise, let the linker search.
584 CmdArgs.push_back("-lstdc++");
585 break;
586 }
587 }
588}
589
Shantonu Senafeb03b2010-09-17 18:39:08 +0000590void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
591 ArgStringList &CmdArgs) const {
592
593 // For Darwin platforms, use the compiler-rt-based support library
594 // instead of the gcc-provided one (which is also incidentally
595 // only present in the gcc lib dir, which makes it hard to find).
596
597 llvm::sys::Path P(getDriver().ResourceDir);
598 P.appendComponent("lib");
599 P.appendComponent("darwin");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000600
601 // Use the newer cc_kext for iOS ARM after 6.0.
602 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
603 !isIPhoneOSVersionLT(6, 0)) {
604 P.appendComponent("libclang_rt.cc_kext.a");
605 } else {
606 P.appendComponent("libclang_rt.cc_kext_ios5.a");
607 }
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000608
Shantonu Senafeb03b2010-09-17 18:39:08 +0000609 // For now, allow missing resource libraries to support developers who may
610 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000611 bool Exists;
612 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Senafeb03b2010-09-17 18:39:08 +0000613 CmdArgs.push_back(Args.MakeArgString(P.str()));
614}
615
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000616DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
617 const char *BoundArch) const {
618 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
619 const OptTable &Opts = getDriver().getOpts();
620
621 // FIXME: We really want to get out of the tool chain level argument
622 // translation business, as it makes the driver functionality much
623 // more opaque. For now, we follow gcc closely solely for the
624 // purpose of easily achieving feature parity & testability. Once we
625 // have something that works, we should reevaluate each translation
626 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000627
Daniel Dunbar775d4062010-06-11 22:00:26 +0000628 for (ArgList::const_iterator it = Args.begin(),
629 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000630 Arg *A = *it;
631
632 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar471c4f82011-06-21 00:20:17 +0000633 // Skip this argument unless the architecture matches either the toolchain
634 // triple arch, or the arch being bound.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000635 llvm::Triple::ArchType XarchArch =
Richard Smithbd55daf2012-11-01 04:30:05 +0000636 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000637 if (!(XarchArch == getArch() ||
638 (BoundArch && XarchArch ==
Rafael Espindoladcbf6982012-10-31 18:51:07 +0000639 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000640 continue;
641
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000642 Arg *OriginalArg = A;
Richard Smithbd55daf2012-11-01 04:30:05 +0000643 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000644 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000645 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000646
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000647 // If the argument parsing failed or more than one argument was
648 // consumed, the -Xarch_ argument's parameter tried to consume
649 // extra arguments. Emit an error and ignore.
650 //
651 // We also want to disallow any options which would alter the
652 // driver behavior; that isn't going to work in our model. We
653 // use isDriverOption() as an approximation, although things
654 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000655 if (!XarchArg || Index > Prev + 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000656 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar6914a982011-04-21 17:32:21 +0000657 << A->getAsString(Args);
658 continue;
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000659 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000660 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000661 << A->getAsString(Args);
662 continue;
663 }
664
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000665 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000666 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000667
668 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000669
670 // Linker input arguments require custom handling. The problem is that we
671 // have already constructed the phase actions, so we can not treat them as
672 // "input arguments".
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000673 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000674 // Convert the argument into individual Zlinker_input_args.
675 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
676 DAL->AddSeparateArg(OriginalArg,
677 Opts.getOption(options::OPT_Zlinker_input),
Richard Smithbd55daf2012-11-01 04:30:05 +0000678 A->getValue(i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000679
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000680 }
681 continue;
682 }
Mike Stump11289f42009-09-09 15:08:12 +0000683 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000684
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000685 // Sob. These is strictly gcc compatible for the time being. Apple
686 // gcc translates options twice, which means that self-expanding
687 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000688 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000689 default:
690 DAL->append(A);
691 break;
692
693 case options::OPT_mkernel:
694 case options::OPT_fapple_kext:
695 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000696 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000697 break;
Mike Stump11289f42009-09-09 15:08:12 +0000698
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000699 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000700 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smithbd55daf2012-11-01 04:30:05 +0000701 A->getValue());
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000702 break;
703
704 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000705 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
706 DAL->AddFlagArg(A,
707 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000708 break;
709
710 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000711 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
712 DAL->AddFlagArg(A,
713 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000714 break;
715
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000716 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000717 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000718 break;
719
720 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000721 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000722 break;
723
724 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000725 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000726 break;
727
728 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000729 DAL->AddFlagArg(A,
730 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000731 break;
732
733 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000734 DAL->AddFlagArg(A,
735 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000736 break;
737
738 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000739 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000740 break;
741
742 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000743 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000744 break;
745 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000746 }
747
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000748 if (getTriple().getArch() == llvm::Triple::x86 ||
749 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000750 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000751 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000752
753 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosier7c5d9082012-04-27 14:58:16 +0000754 // how the driver driver works.
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000755 if (BoundArch) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000756 StringRef Name = BoundArch;
Michael J. Spencerfc790902012-10-19 22:36:40 +0000757 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
758 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000759
760 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
761 // which defines the list of which architectures we accept.
762 if (Name == "ppc")
763 ;
764 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000765 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000766 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000767 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000768 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000769 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000770 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000771 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000772 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000773 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000774 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000775 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000776 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000777 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000778 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000779 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000780
781 else if (Name == "ppc64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000782 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000783
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000784 else if (Name == "i386")
785 ;
786 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000787 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000788 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000789 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000790 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000791 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000792 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000793 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000794 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000795 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000796 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000797 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000798 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000799 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000800
801 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000802 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000803
804 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000805 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000806 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000807 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000808 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000809 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000810 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000811 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000812 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000813 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000814 else if (Name == "armv6m")
815 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000816 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000817 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000818 else if (Name == "armv7em")
819 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000820 else if (Name == "armv7f")
821 DAL->AddJoinedArg(0, MArch, "armv7f");
822 else if (Name == "armv7k")
823 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000824 else if (Name == "armv7m")
825 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000826 else if (Name == "armv7s")
827 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000828
829 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000830 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000831 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000832
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000833 // Add an explicit version min argument for the deployment target. We do this
834 // after argument translation because -Xarch_ arguments may add a version min
835 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000836 if (BoundArch)
837 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000838
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000839 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
840 // FIXME: It would be far better to avoid inserting those -static arguments,
841 // but we can't check the deployment target in the translation code until
842 // it is set here.
843 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
844 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
845 Arg *A = *it;
846 ++it;
847 if (A->getOption().getID() != options::OPT_mkernel &&
848 A->getOption().getID() != options::OPT_fapple_kext)
849 continue;
850 assert(it != ie && "unexpected argument translation");
851 A = *it;
852 assert(A->getOption().getID() == options::OPT_static &&
853 "missing expected -static argument");
854 it = DAL->getArgs().erase(it);
855 }
856 }
857
Bob Wilson102be442011-10-07 17:54:41 +0000858 // Validate the C++ standard library choice.
859 CXXStdlibType Type = GetCXXStdlibType(*DAL);
860 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000861 // Check whether the target provides libc++.
862 StringRef where;
863
864 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson5ad5a952012-11-09 01:59:30 +0000865 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
866 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000867
868 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000869 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000870 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000871 }
872 }
873
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000874 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000875}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000876
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000877bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000878 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000879}
880
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000881bool Darwin::UseDwarfDebugFlags() const {
882 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
883 return S[0] != '\0';
884 return false;
885}
886
Daniel Dunbar3241d402010-02-10 18:49:11 +0000887bool Darwin::UseSjLjExceptions() const {
888 // Darwin uses SjLj exceptions on ARM.
889 return (getTriple().getArch() == llvm::Triple::arm ||
890 getTriple().getArch() == llvm::Triple::thumb);
891}
892
Chandler Carruth76a943b2012-11-19 03:52:03 +0000893bool Darwin::isPICDefault() const {
894 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000895}
896
Chandler Carruth76a943b2012-11-19 03:52:03 +0000897bool Darwin::isPICDefaultForced() const {
898 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000899}
900
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000901bool Darwin::SupportsProfiling() const {
902 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000903 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000904}
905
Daniel Dunbar16334e12010-04-10 16:20:23 +0000906bool Darwin::SupportsObjCGC() const {
907 // Garbage collection is supported everywhere except on iPhone OS.
908 return !isTargetIPhoneOS();
909}
910
John McCall3deb1ad2012-08-21 02:47:43 +0000911void Darwin::CheckObjCARC() const {
912 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
913 return;
John McCall93207072012-08-27 01:56:21 +0000914 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000915}
916
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000917std::string
Chad Rosierd3a0f952011-09-20 20:44:06 +0000918Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
919 types::ID InputType) const {
920 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000921}
922
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000923/// Generic_GCC - A tool chain using the 'gcc' command to perform
924/// all subcommands; this relies on gcc translating the majority of
925/// command line options.
926
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000927/// \brief Parse a GCCVersion object out of a string of text.
928///
929/// This is the primary means of forming GCCVersion objects.
930/*static*/
931Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
932 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
933 std::pair<StringRef, StringRef> First = VersionText.split('.');
934 std::pair<StringRef, StringRef> Second = First.second.split('.');
935
936 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
937 if (First.first.getAsInteger(10, GoodVersion.Major) ||
938 GoodVersion.Major < 0)
939 return BadVersion;
940 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
941 GoodVersion.Minor < 0)
942 return BadVersion;
943
944 // First look for a number prefix and parse that if present. Otherwise just
945 // stash the entire patch string in the suffix, and leave the number
946 // unspecified. This covers versions strings such as:
947 // 4.4
948 // 4.4.0
949 // 4.4.x
950 // 4.4.2-rc4
951 // 4.4.x-patched
952 // And retains any patch number it finds.
953 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
954 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +0000955 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000956 // Try to parse the number and any suffix.
957 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
958 GoodVersion.Patch < 0)
959 return BadVersion;
960 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
961 }
962 }
963
964 return GoodVersion;
965}
966
967/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
968bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000969 if (Major != RHS.Major)
970 return Major < RHS.Major;
971 if (Minor != RHS.Minor)
972 return Minor < RHS.Minor;
973 if (Patch != RHS.Patch) {
974 // Note that versions without a specified patch sort higher than those with
975 // a patch.
976 if (RHS.Patch == -1)
977 return true;
978 if (Patch == -1)
979 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000980
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000981 // Otherwise just sort on the patch itself.
982 return Patch < RHS.Patch;
983 }
984 if (PatchSuffix != RHS.PatchSuffix) {
985 // Sort empty suffixes higher.
986 if (RHS.PatchSuffix.empty())
987 return true;
988 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +0000989 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000990
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000991 // Provide a lexicographic sort to make this a total ordering.
992 return PatchSuffix < RHS.PatchSuffix;
993 }
994
995 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000996 return false;
997}
998
Rafael Espindola1af7c212012-02-19 01:38:32 +0000999static StringRef getGCCToolchainDir(const ArgList &Args) {
1000 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1001 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +00001002 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +00001003 return GCC_INSTALL_PREFIX;
1004}
1005
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001006/// \brief Construct a GCCInstallationDetector from the driver.
1007///
1008/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +00001009/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +00001010///
1011/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1012/// should instead pull the target out of the driver. This is currently
1013/// necessary because the driver doesn't store the final version of the target
1014/// triple.
1015Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1016 const Driver &D,
Rafael Espindola1af7c212012-02-19 01:38:32 +00001017 const llvm::Triple &TargetTriple,
1018 const ArgList &Args)
Chandler Carruth866faab2012-01-25 07:21:38 +00001019 : IsValid(false) {
Chandler Carruth779579b2012-02-13 02:02:09 +00001020 llvm::Triple MultiarchTriple
1021 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1022 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001023 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001024 // The library directories which may contain GCC installations.
Chandler Carruth866faab2012-01-25 07:21:38 +00001025 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001026 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001027 SmallVector<StringRef, 10> CandidateTripleAliases;
1028 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1029 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1030 CandidateTripleAliases,
1031 CandidateMultiarchLibDirs,
1032 CandidateMultiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001033
1034 // Compute the set of prefixes for our search.
1035 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1036 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001037
Rafael Espindola1af7c212012-02-19 01:38:32 +00001038 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1039 if (GCCToolchainDir != "") {
1040 if (GCCToolchainDir.back() == '/')
1041 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001042
Rafael Espindola1af7c212012-02-19 01:38:32 +00001043 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001044 } else {
1045 Prefixes.push_back(D.SysRoot);
1046 Prefixes.push_back(D.SysRoot + "/usr");
1047 Prefixes.push_back(D.InstalledDir + "/..");
1048 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001049
1050 // Loop over the various components which exist and select the best GCC
1051 // installation available. GCC installs are ranked by version number.
1052 Version = GCCVersion::Parse("0.0.0");
1053 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1054 if (!llvm::sys::fs::exists(Prefixes[i]))
1055 continue;
1056 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1057 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1058 if (!llvm::sys::fs::exists(LibDir))
1059 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001060 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001061 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1062 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001063 }
1064 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1065 const std::string LibDir
1066 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1067 if (!llvm::sys::fs::exists(LibDir))
1068 continue;
1069 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1070 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001071 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth866faab2012-01-25 07:21:38 +00001072 CandidateMultiarchTripleAliases[k],
1073 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001074 }
1075 }
1076}
1077
1078/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth866faab2012-01-25 07:21:38 +00001079 const llvm::Triple &TargetTriple,
1080 const llvm::Triple &MultiarchTriple,
1081 SmallVectorImpl<StringRef> &LibDirs,
1082 SmallVectorImpl<StringRef> &TripleAliases,
1083 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1084 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1085 // Declare a bunch of static data sets that we'll select between below. These
1086 // are specifically designed to always refer to string literals to avoid any
1087 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001088 static const char *const AArch64LibDirs[] = { "/lib" };
1089 static const char *const AArch64Triples[] = {
1090 "aarch64-none-linux-gnu",
1091 "aarch64-linux-gnu"
1092 };
1093
Chandler Carruth866faab2012-01-25 07:21:38 +00001094 static const char *const ARMLibDirs[] = { "/lib" };
1095 static const char *const ARMTriples[] = {
1096 "arm-linux-gnueabi",
1097 "arm-linux-androideabi"
1098 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001099 static const char *const ARMHFTriples[] = {
1100 "arm-linux-gnueabihf",
1101 };
Chandler Carruth866faab2012-01-25 07:21:38 +00001102
1103 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1104 static const char *const X86_64Triples[] = {
1105 "x86_64-linux-gnu",
1106 "x86_64-unknown-linux-gnu",
1107 "x86_64-pc-linux-gnu",
1108 "x86_64-redhat-linux6E",
1109 "x86_64-redhat-linux",
1110 "x86_64-suse-linux",
1111 "x86_64-manbo-linux-gnu",
1112 "x86_64-linux-gnu",
1113 "x86_64-slackware-linux"
1114 };
1115 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1116 static const char *const X86Triples[] = {
1117 "i686-linux-gnu",
1118 "i686-pc-linux-gnu",
1119 "i486-linux-gnu",
1120 "i386-linux-gnu",
NAKAMURA Takumi55c12892012-12-07 17:13:18 +00001121 "i386-redhat-linux6E",
Chandler Carruth866faab2012-01-25 07:21:38 +00001122 "i686-redhat-linux",
1123 "i586-redhat-linux",
1124 "i386-redhat-linux",
1125 "i586-suse-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001126 "i486-slackware-linux",
1127 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001128 };
1129
1130 static const char *const MIPSLibDirs[] = { "/lib" };
1131 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1132 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyan53fefd12012-10-03 17:46:38 +00001133 static const char *const MIPSELTriples[] = {
1134 "mipsel-linux-gnu",
1135 "mipsel-linux-android"
1136 };
Chandler Carruth866faab2012-01-25 07:21:38 +00001137
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001138 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1139 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1140 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1141 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1142
Chandler Carruth866faab2012-01-25 07:21:38 +00001143 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1144 static const char *const PPCTriples[] = {
1145 "powerpc-linux-gnu",
1146 "powerpc-unknown-linux-gnu",
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00001147 "powerpc-linux-gnuspe",
Gabor Greif93047632012-05-15 11:21:03 +00001148 "powerpc-suse-linux",
1149 "powerpc-montavista-linuxspe"
Chandler Carruth866faab2012-01-25 07:21:38 +00001150 };
1151 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1152 static const char *const PPC64Triples[] = {
Chandler Carruthaf3c2092012-02-26 09:03:21 +00001153 "powerpc64-linux-gnu",
Chandler Carruth866faab2012-01-25 07:21:38 +00001154 "powerpc64-unknown-linux-gnu",
1155 "powerpc64-suse-linux",
1156 "ppc64-redhat-linux"
1157 };
1158
1159 switch (TargetTriple.getArch()) {
Tim Northover9bb857a2013-01-31 12:13:10 +00001160 case llvm::Triple::aarch64:
Eric Christopherc4b0be92013-02-05 07:29:49 +00001161 LibDirs.append(AArch64LibDirs, AArch64LibDirs
Tim Northover9bb857a2013-01-31 12:13:10 +00001162 + llvm::array_lengthof(AArch64LibDirs));
1163 TripleAliases.append(
1164 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1165 MultiarchLibDirs.append(
1166 AArch64LibDirs, AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1167 MultiarchTripleAliases.append(
1168 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1169 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001170 case llvm::Triple::arm:
1171 case llvm::Triple::thumb:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001172 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001173 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1174 TripleAliases.append(
1175 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1176 } else {
1177 TripleAliases.append(
1178 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1179 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001180 break;
1181 case llvm::Triple::x86_64:
1182 LibDirs.append(
1183 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1184 TripleAliases.append(
1185 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1186 MultiarchLibDirs.append(
1187 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1188 MultiarchTripleAliases.append(
1189 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1190 break;
1191 case llvm::Triple::x86:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001192 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth866faab2012-01-25 07:21:38 +00001193 TripleAliases.append(
1194 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1195 MultiarchLibDirs.append(
1196 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1197 MultiarchTripleAliases.append(
1198 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1199 break;
1200 case llvm::Triple::mips:
1201 LibDirs.append(
1202 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1203 TripleAliases.append(
1204 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001205 MultiarchLibDirs.append(
1206 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1207 MultiarchTripleAliases.append(
1208 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001209 break;
1210 case llvm::Triple::mipsel:
1211 LibDirs.append(
1212 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1213 TripleAliases.append(
1214 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001215 MultiarchLibDirs.append(
1216 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1217 MultiarchTripleAliases.append(
1218 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1219 break;
1220 case llvm::Triple::mips64:
1221 LibDirs.append(
1222 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1223 TripleAliases.append(
1224 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1225 MultiarchLibDirs.append(
1226 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1227 MultiarchTripleAliases.append(
1228 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1229 break;
1230 case llvm::Triple::mips64el:
1231 LibDirs.append(
1232 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1233 TripleAliases.append(
1234 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1235 MultiarchLibDirs.append(
1236 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1237 MultiarchTripleAliases.append(
1238 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001239 break;
1240 case llvm::Triple::ppc:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001241 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth866faab2012-01-25 07:21:38 +00001242 TripleAliases.append(
1243 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1244 MultiarchLibDirs.append(
1245 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1246 MultiarchTripleAliases.append(
1247 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1248 break;
1249 case llvm::Triple::ppc64:
1250 LibDirs.append(
1251 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1252 TripleAliases.append(
1253 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1254 MultiarchLibDirs.append(
1255 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1256 MultiarchTripleAliases.append(
1257 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1258 break;
1259
1260 default:
1261 // By default, just rely on the standard lib directories and the original
1262 // triple.
1263 break;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001264 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001265
1266 // Always append the drivers target triple to the end, in case it doesn't
1267 // match any of our aliases.
1268 TripleAliases.push_back(TargetTriple.str());
1269
1270 // Also include the multiarch variant if it's different.
1271 if (TargetTriple.str() != MultiarchTriple.str())
1272 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001273}
1274
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001275// FIXME: There is the same routine in the Tools.cpp.
1276static bool hasMipsN32ABIArg(const ArgList &Args) {
1277 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00001278 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001279}
1280
1281static StringRef getTargetMultiarchSuffix(llvm::Triple::ArchType TargetArch,
1282 const ArgList &Args) {
1283 if (TargetArch == llvm::Triple::x86_64 ||
1284 TargetArch == llvm::Triple::ppc64)
1285 return "/64";
1286
1287 if (TargetArch == llvm::Triple::mips64 ||
1288 TargetArch == llvm::Triple::mips64el) {
1289 if (hasMipsN32ABIArg(Args))
1290 return "/n32";
1291 else
1292 return "/64";
1293 }
1294
1295 return "/32";
1296}
1297
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001298void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001299 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1300 const std::string &LibDir,
Chandler Carruth866faab2012-01-25 07:21:38 +00001301 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001302 // There are various different suffixes involving the triple we
1303 // check for. We also record what is necessary to walk from each back
1304 // up to the lib directory.
Chandler Carruth866faab2012-01-25 07:21:38 +00001305 const std::string LibSuffixes[] = {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001306 "/gcc/" + CandidateTriple.str(),
1307 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1308
Hal Finkelf3587912012-09-18 22:25:07 +00001309 // The Freescale PPC SDK has the gcc libraries in
1310 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1311 "/" + CandidateTriple.str(),
1312
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001313 // Ubuntu has a strange mis-matched pair of triples that this happens to
1314 // match.
1315 // FIXME: It may be worthwhile to generalize this and look for a second
1316 // triple.
Chandler Carruth6e46ca22011-11-09 03:46:20 +00001317 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001318 };
1319 const std::string InstallSuffixes[] = {
1320 "/../../..",
1321 "/../../../..",
Hal Finkelf3587912012-09-18 22:25:07 +00001322 "/../..",
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001323 "/../../../.."
1324 };
1325 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth866faab2012-01-25 07:21:38 +00001326 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1327 (TargetArch != llvm::Triple::x86));
1328 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1329 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001330 llvm::error_code EC;
Chandler Carruth866faab2012-01-25 07:21:38 +00001331 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001332 !EC && LI != LE; LI = LI.increment(EC)) {
1333 StringRef VersionText = llvm::sys::path::filename(LI->path());
1334 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1335 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1336 if (CandidateVersion < MinVersion)
1337 continue;
1338 if (CandidateVersion <= Version)
1339 continue;
Hal Finkel221e11e2011-12-08 05:50:03 +00001340
1341 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth64cee062012-01-24 19:21:42 +00001342 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth866faab2012-01-25 07:21:38 +00001343 // libs in a subdirectory named 64. The simple logic we follow is that
1344 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1345 // we use that. If not, and if not a multiarch triple, we look for
1346 // crtbegin.o without the subdirectory.
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001347 StringRef MultiarchSuffix = getTargetMultiarchSuffix(TargetArch, Args);
Chandler Carruth866faab2012-01-25 07:21:38 +00001348 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1349 GCCMultiarchSuffix = MultiarchSuffix.str();
1350 } else {
1351 if (NeedsMultiarchSuffix ||
1352 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1353 continue;
1354 GCCMultiarchSuffix.clear();
1355 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001356
1357 Version = CandidateVersion;
Chandler Carruth4d9d7682012-01-24 19:28:29 +00001358 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001359 // FIXME: We hack together the directory name here instead of
1360 // using LI to ensure stable path separators across Windows and
1361 // Linux.
Chandler Carruth866faab2012-01-25 07:21:38 +00001362 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth64cee062012-01-24 19:21:42 +00001363 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001364 IsValid = true;
1365 }
1366 }
1367}
1368
Rafael Espindola1af7c212012-02-19 01:38:32 +00001369Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1370 const ArgList &Args)
1371 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbar88979912010-08-01 22:29:51 +00001372 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001373 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001374 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001375}
1376
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001377Generic_GCC::~Generic_GCC() {
1378 // Free tool implementations.
1379 for (llvm::DenseMap<unsigned, Tool*>::iterator
1380 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1381 delete it->second;
1382}
1383
Mike Stump11289f42009-09-09 15:08:12 +00001384Tool &Generic_GCC::SelectTool(const Compilation &C,
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001385 const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001386 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001387 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001388 Key = Action::AnalyzeJobClass;
1389 else
1390 Key = JA.getKind();
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001391
1392 Tool *&T = Tools[Key];
1393 if (!T) {
1394 switch (Key) {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +00001395 case Action::InputClass:
1396 case Action::BindArchClass:
David Blaikie83d382b2011-09-23 05:06:16 +00001397 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001398 case Action::PreprocessJobClass:
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001399 T = new tools::gcc::Preprocess(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001400 case Action::PrecompileJobClass:
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001401 T = new tools::gcc::Precompile(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001402 case Action::AnalyzeJobClass:
Ted Kremenekf7639e12012-03-06 20:06:33 +00001403 case Action::MigrateJobClass:
Bob Wilson0c79f3c2012-11-08 01:03:34 +00001404 T = new tools::Clang(*this); break;
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001405 case Action::CompileJobClass:
1406 T = new tools::gcc::Compile(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001407 case Action::AssembleJobClass:
1408 T = new tools::gcc::Assemble(*this); break;
1409 case Action::LinkJobClass:
1410 T = new tools::gcc::Link(*this); break;
Mike Stump11289f42009-09-09 15:08:12 +00001411
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +00001412 // This is a bit ungeneric, but the only platform using a driver
1413 // driver is Darwin.
1414 case Action::LipoJobClass:
1415 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar88299622010-06-04 18:28:36 +00001416 case Action::DsymutilJobClass:
1417 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopher551ef452011-08-23 17:56:55 +00001418 case Action::VerifyJobClass:
1419 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001420 }
1421 }
1422
1423 return *T;
1424}
1425
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001426bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001427 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001428}
1429
Chandler Carruth76a943b2012-11-19 03:52:03 +00001430bool Generic_GCC::isPICDefault() const {
1431 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001432}
1433
Chandler Carruth76a943b2012-11-19 03:52:03 +00001434bool Generic_GCC::isPICDefaultForced() const {
1435 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001436}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001437
Tony Linthicum76329bf2011-12-12 21:14:55 +00001438/// Hexagon Toolchain
1439
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001440std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1441
1442 // Locate the rest of the toolchain ...
1443 if (strlen(GCC_INSTALL_PREFIX))
1444 return std::string(GCC_INSTALL_PREFIX);
1445
1446 std::string InstallRelDir = InstalledDir + "/../../gnu";
1447 if (llvm::sys::fs::exists(InstallRelDir))
1448 return InstallRelDir;
1449
1450 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1451 if (llvm::sys::fs::exists(PrefixRelDir))
1452 return PrefixRelDir;
1453
1454 return InstallRelDir;
1455}
1456
Matthew Curtise689b052012-12-06 15:46:07 +00001457static void GetHexagonLibraryPaths(
1458 const ArgList &Args,
1459 const std::string Ver,
1460 const std::string MarchString,
1461 const std::string &InstalledDir,
1462 ToolChain::path_list *LibPaths)
1463{
1464 bool buildingLib = Args.hasArg(options::OPT_shared);
1465
1466 //----------------------------------------------------------------------------
1467 // -L Args
1468 //----------------------------------------------------------------------------
1469 for (arg_iterator
1470 it = Args.filtered_begin(options::OPT_L),
1471 ie = Args.filtered_end();
1472 it != ie;
1473 ++it) {
1474 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1475 LibPaths->push_back((*it)->getValue(i));
1476 }
1477
1478 //----------------------------------------------------------------------------
1479 // Other standard paths
1480 //----------------------------------------------------------------------------
1481 const std::string MarchSuffix = "/" + MarchString;
1482 const std::string G0Suffix = "/G0";
1483 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1484 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1485
1486 // lib/gcc/hexagon/...
1487 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1488 if (buildingLib) {
1489 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1490 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1491 }
1492 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1493 LibPaths->push_back(LibGCCHexagonDir + Ver);
1494
1495 // lib/gcc/...
1496 LibPaths->push_back(RootDir + "lib/gcc");
1497
1498 // hexagon/lib/...
1499 std::string HexagonLibDir = RootDir + "hexagon/lib";
1500 if (buildingLib) {
1501 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1502 LibPaths->push_back(HexagonLibDir + G0Suffix);
1503 }
1504 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1505 LibPaths->push_back(HexagonLibDir);
1506}
1507
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001508Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1509 const ArgList &Args)
1510 : Linux(D, Triple, Args) {
1511 const std::string InstalledDir(getDriver().getInstalledDir());
1512 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1513
1514 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1515 // program paths
1516 const std::string BinDir(GnuDir + "/bin");
1517 if (llvm::sys::fs::exists(BinDir))
1518 getProgramPaths().push_back(BinDir);
1519
1520 // Determine version of GCC libraries and headers to use.
1521 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1522 llvm::error_code ec;
1523 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1524 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1525 !ec && di != de; di = di.increment(ec)) {
1526 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1527 if (MaxVersion < cv)
1528 MaxVersion = cv;
1529 }
1530 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001531
1532 ToolChain::path_list *LibPaths= &getFilePaths();
1533
1534 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1535 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1536 // support 'linux' we'll need to fix this up
1537 LibPaths->clear();
1538
1539 GetHexagonLibraryPaths(
1540 Args,
1541 GetGCCLibAndIncVersion(),
1542 GetTargetCPU(Args),
1543 InstalledDir,
1544 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001545}
1546
1547Hexagon_TC::~Hexagon_TC() {
1548 // Free tool implementations.
1549 for (llvm::DenseMap<unsigned, Tool*>::iterator
1550 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1551 delete it->second;
1552}
1553
1554Tool &Hexagon_TC::SelectTool(const Compilation &C,
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001555 const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001556 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001557 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001558 Key = Action::AnalyzeJobClass;
1559 else
1560 Key = JA.getKind();
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001561
Tony Linthicum76329bf2011-12-12 21:14:55 +00001562 Tool *&T = Tools[Key];
1563 if (!T) {
1564 switch (Key) {
1565 case Action::InputClass:
1566 case Action::BindArchClass:
1567 assert(0 && "Invalid tool kind.");
1568 case Action::AnalyzeJobClass:
1569 T = new tools::Clang(*this); break;
1570 case Action::AssembleJobClass:
1571 T = new tools::hexagon::Assemble(*this); break;
1572 case Action::LinkJobClass:
1573 T = new tools::hexagon::Link(*this); break;
1574 default:
1575 assert(false && "Unsupported action for Hexagon target.");
1576 }
1577 }
1578
1579 return *T;
1580}
1581
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001582void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1583 ArgStringList &CC1Args) const {
1584 const Driver &D = getDriver();
1585
1586 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1587 DriverArgs.hasArg(options::OPT_nostdlibinc))
1588 return;
1589
1590 llvm::sys::Path InstallDir(D.InstalledDir);
1591 std::string Ver(GetGCCLibAndIncVersion());
1592 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1593 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1594 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1595 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1596 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001597}
1598
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001599void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1600 ArgStringList &CC1Args) const {
1601
1602 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1603 DriverArgs.hasArg(options::OPT_nostdincxx))
1604 return;
1605
1606 const Driver &D = getDriver();
1607 std::string Ver(GetGCCLibAndIncVersion());
1608 llvm::sys::Path IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
1609
1610 IncludeDir.appendComponent("hexagon/include/c++/");
1611 IncludeDir.appendComponent(Ver);
1612 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001613}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001614
Matthew Curtise689b052012-12-06 15:46:07 +00001615ToolChain::CXXStdlibType
1616Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1617 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1618 if (!A)
1619 return ToolChain::CST_Libstdcxx;
1620
1621 StringRef Value = A->getValue();
1622 if (Value != "libstdc++") {
1623 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1624 << A->getAsString(Args);
1625 }
1626
1627 return ToolChain::CST_Libstdcxx;
1628}
1629
1630static Arg *GetLastHexagonArchArg(const ArgList &Args)
Matthew Curtisf10a5952012-12-06 14:16:43 +00001631{
1632 Arg *A = NULL;
1633
1634 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1635 it != ie; ++it) {
1636 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
1637 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1638 A = *it;
1639 A->claim();
1640 } else if ((*it)->getOption().matches(options::OPT_m_Joined)) {
1641 StringRef Value = (*it)->getValue(0);
1642 if (Value.startswith("v")) {
1643 A = *it;
1644 A->claim();
1645 }
1646 }
1647 }
1648 return A;
1649}
1650
1651StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1652{
1653 // Select the default CPU (v4) if none was given or detection failed.
1654 Arg *A = GetLastHexagonArchArg (Args);
1655 if (A) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001656 StringRef WhichHexagon = A->getValue();
Matthew Curtisf10a5952012-12-06 14:16:43 +00001657 if (WhichHexagon.startswith("hexagon"))
1658 return WhichHexagon.substr(sizeof("hexagon") - 1);
1659 if (WhichHexagon != "")
1660 return WhichHexagon;
1661 }
1662
1663 return "v4";
1664}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001665// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001666
Chris Lattner09797542010-03-04 21:07:38 +00001667/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1668/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1669/// Currently does not support anything else but compilation.
1670
Chandler Carruthd7fa2e02012-01-31 02:21:20 +00001671TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1672 : ToolChain(D, Triple) {
Chris Lattner09797542010-03-04 21:07:38 +00001673 // Path mangling to find libexec
1674 std::string Path(getDriver().Dir);
1675
1676 Path += "/../libexec";
1677 getProgramPaths().push_back(Path);
1678}
1679
1680TCEToolChain::~TCEToolChain() {
1681 for (llvm::DenseMap<unsigned, Tool*>::iterator
1682 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1683 delete it->second;
1684}
1685
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001686bool TCEToolChain::IsMathErrnoDefault() const {
1687 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001688}
1689
Chandler Carruth76a943b2012-11-19 03:52:03 +00001690bool TCEToolChain::isPICDefault() const {
1691 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001692}
1693
Chandler Carruth76a943b2012-11-19 03:52:03 +00001694bool TCEToolChain::isPICDefaultForced() const {
1695 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001696}
1697
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001698Tool &TCEToolChain::SelectTool(const Compilation &C,
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001699 const JobAction &JA) const {
Chris Lattner09797542010-03-04 21:07:38 +00001700 Action::ActionClass Key;
1701 Key = Action::AnalyzeJobClass;
1702
1703 Tool *&T = Tools[Key];
1704 if (!T) {
1705 switch (Key) {
1706 case Action::PreprocessJobClass:
1707 T = new tools::gcc::Preprocess(*this); break;
1708 case Action::AnalyzeJobClass:
1709 T = new tools::Clang(*this); break;
1710 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001711 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner09797542010-03-04 21:07:38 +00001712 }
1713 }
1714 return *T;
1715}
1716
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001717/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1718
Rafael Espindola1af7c212012-02-19 01:38:32 +00001719OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1720 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001721 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001722 getFilePaths().push_back("/usr/lib");
1723}
1724
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001725Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001726 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001727 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001728 Key = Action::AnalyzeJobClass;
1729 else
1730 Key = JA.getKind();
1731
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001732 Tool *&T = Tools[Key];
1733 if (!T) {
1734 switch (Key) {
Rafael Espindola96aef792010-11-07 23:13:01 +00001735 case Action::AssembleJobClass: {
Rafael Espindola248e2192013-03-18 17:52:57 +00001736 if (useIntegratedAs(C.getArgs()))
Rafael Espindola96aef792010-11-07 23:13:01 +00001737 T = new tools::ClangAs(*this);
1738 else
1739 T = new tools::openbsd::Assemble(*this);
1740 break;
1741 }
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001742 case Action::LinkJobClass:
1743 T = new tools::openbsd::Link(*this); break;
1744 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001745 T = &Generic_GCC::SelectTool(C, JA);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001746 }
1747 }
1748
1749 return *T;
1750}
1751
Eli Friedman9fa28852012-08-08 23:57:20 +00001752/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1753
1754Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1755 : Generic_ELF(D, Triple, Args) {
1756 getFilePaths().push_back(getDriver().Dir + "/../lib");
1757 getFilePaths().push_back("/usr/lib");
1758}
1759
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001760Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001761 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001762 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001763 Key = Action::AnalyzeJobClass;
1764 else
1765 Key = JA.getKind();
1766
Eli Friedman9fa28852012-08-08 23:57:20 +00001767 Tool *&T = Tools[Key];
1768 if (!T) {
1769 switch (Key) {
1770 case Action::AssembleJobClass: {
Rafael Espindola248e2192013-03-18 17:52:57 +00001771 if (useIntegratedAs(C.getArgs()))
Eli Friedman9fa28852012-08-08 23:57:20 +00001772 T = new tools::ClangAs(*this);
1773 else
1774 T = new tools::bitrig::Assemble(*this);
1775 break;
1776 }
1777 case Action::LinkJobClass:
1778 T = new tools::bitrig::Link(*this); break;
1779 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001780 T = &Generic_GCC::SelectTool(C, JA);
Eli Friedman9fa28852012-08-08 23:57:20 +00001781 }
1782 }
1783
1784 return *T;
1785}
1786
1787void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1788 ArgStringList &CC1Args) const {
1789 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1790 DriverArgs.hasArg(options::OPT_nostdincxx))
1791 return;
1792
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001793 switch (GetCXXStdlibType(DriverArgs)) {
1794 case ToolChain::CST_Libcxx:
1795 addSystemInclude(DriverArgs, CC1Args,
1796 getDriver().SysRoot + "/usr/include/c++/");
1797 break;
1798 case ToolChain::CST_Libstdcxx:
1799 addSystemInclude(DriverArgs, CC1Args,
1800 getDriver().SysRoot + "/usr/include/c++/stdc++");
1801 addSystemInclude(DriverArgs, CC1Args,
1802 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001803
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001804 StringRef Triple = getTriple().str();
1805 if (Triple.startswith("amd64"))
1806 addSystemInclude(DriverArgs, CC1Args,
1807 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1808 Triple.substr(5));
1809 else
1810 addSystemInclude(DriverArgs, CC1Args,
1811 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1812 Triple);
1813 break;
1814 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001815}
1816
1817void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1818 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001819 switch (GetCXXStdlibType(Args)) {
1820 case ToolChain::CST_Libcxx:
1821 CmdArgs.push_back("-lc++");
1822 CmdArgs.push_back("-lcxxrt");
1823 // Include supc++ to provide Unwind until provided by libcxx.
1824 CmdArgs.push_back("-lgcc");
1825 break;
1826 case ToolChain::CST_Libstdcxx:
1827 CmdArgs.push_back("-lstdc++");
1828 break;
1829 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001830}
1831
Daniel Dunbare24297c2009-03-30 21:06:03 +00001832/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1833
Rafael Espindola1af7c212012-02-19 01:38:32 +00001834FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1835 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001836
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001837 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1838 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001839 if ((Triple.getArch() == llvm::Triple::x86 ||
1840 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001841 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001842 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1843 else
1844 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001845}
1846
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001847Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001848 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001849 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001850 Key = Action::AnalyzeJobClass;
1851 else
1852 Key = JA.getKind();
1853
Daniel Dunbare24297c2009-03-30 21:06:03 +00001854 Tool *&T = Tools[Key];
1855 if (!T) {
1856 switch (Key) {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00001857 case Action::AssembleJobClass:
Rafael Espindola248e2192013-03-18 17:52:57 +00001858 if (useIntegratedAs(C.getArgs()))
Roman Divacky137426a2010-11-08 17:46:39 +00001859 T = new tools::ClangAs(*this);
1860 else
1861 T = new tools::freebsd::Assemble(*this);
Roman Divackyb45d2672010-11-08 19:39:10 +00001862 break;
Daniel Dunbard854c8d2009-04-01 19:36:32 +00001863 case Action::LinkJobClass:
1864 T = new tools::freebsd::Link(*this); break;
Daniel Dunbare24297c2009-03-30 21:06:03 +00001865 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001866 T = &Generic_GCC::SelectTool(C, JA);
Daniel Dunbare24297c2009-03-30 21:06:03 +00001867 }
1868 }
1869
1870 return *T;
1871}
Daniel Dunbarcc912342009-05-02 18:28:39 +00001872
Rafael Espindola0f207ed2012-12-13 04:17:14 +00001873bool FreeBSD::UseSjLjExceptions() const {
1874 // FreeBSD uses SjLj exceptions on ARM oabi.
1875 switch (getTriple().getEnvironment()) {
1876 case llvm::Triple::GNUEABI:
1877 case llvm::Triple::EABI:
1878 return false;
1879
1880 default:
1881 return (getTriple().getArch() == llvm::Triple::arm ||
1882 getTriple().getArch() == llvm::Triple::thumb);
1883 }
1884}
1885
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001886/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1887
Rafael Espindola1af7c212012-02-19 01:38:32 +00001888NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1889 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001890
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001891 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001892 // When targeting a 32-bit platform, try the special directory used on
1893 // 64-bit hosts, and only fall back to the main library directory if that
1894 // doesn't work.
1895 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1896 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00001897 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001898 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001899
1900 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001901 }
1902}
1903
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001904Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001905 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001906 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001907 Key = Action::AnalyzeJobClass;
1908 else
1909 Key = JA.getKind();
1910
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001911 Tool *&T = Tools[Key];
1912 if (!T) {
1913 switch (Key) {
1914 case Action::AssembleJobClass:
Rafael Espindola248e2192013-03-18 17:52:57 +00001915 if (useIntegratedAs(C.getArgs()))
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001916 T = new tools::ClangAs(*this);
1917 else
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00001918 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001919 break;
1920 case Action::LinkJobClass:
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00001921 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00001922 break;
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001923 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001924 T = &Generic_GCC::SelectTool(C, JA);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001925 }
1926 }
1927
1928 return *T;
1929}
1930
Chris Lattner3e2ee142010-07-07 16:01:42 +00001931/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1932
Rafael Espindola1af7c212012-02-19 01:38:32 +00001933Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1934 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00001935 getFilePaths().push_back(getDriver().Dir + "/../lib");
1936 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00001937}
1938
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001939Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001940 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001941 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001942 Key = Action::AnalyzeJobClass;
1943 else
1944 Key = JA.getKind();
Chris Lattner3e2ee142010-07-07 16:01:42 +00001945
1946 Tool *&T = Tools[Key];
1947 if (!T) {
1948 switch (Key) {
1949 case Action::AssembleJobClass:
1950 T = new tools::minix::Assemble(*this); break;
1951 case Action::LinkJobClass:
1952 T = new tools::minix::Link(*this); break;
1953 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001954 T = &Generic_GCC::SelectTool(C, JA);
Chris Lattner3e2ee142010-07-07 16:01:42 +00001955 }
1956 }
1957
1958 return *T;
1959}
1960
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001961/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1962
Rafael Espindola1af7c212012-02-19 01:38:32 +00001963AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1964 const ArgList &Args)
1965 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001966
Daniel Dunbar88979912010-08-01 22:29:51 +00001967 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001968 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001969 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001970
Daniel Dunbar083edf72009-12-21 18:54:17 +00001971 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001972 getFilePaths().push_back("/usr/lib");
1973 getFilePaths().push_back("/usr/sfw/lib");
1974 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00001975 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001976
1977}
1978
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001979Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001980 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001981 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001982 Key = Action::AnalyzeJobClass;
1983 else
1984 Key = JA.getKind();
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001985
1986 Tool *&T = Tools[Key];
1987 if (!T) {
1988 switch (Key) {
1989 case Action::AssembleJobClass:
1990 T = new tools::auroraux::Assemble(*this); break;
1991 case Action::LinkJobClass:
1992 T = new tools::auroraux::Link(*this); break;
1993 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001994 T = &Generic_GCC::SelectTool(C, JA);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001995 }
1996 }
1997
1998 return *T;
1999}
2000
David Chisnallf571cde2012-02-15 13:39:01 +00002001/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2002
Rafael Espindola1af7c212012-02-19 01:38:32 +00002003Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2004 const ArgList &Args)
2005 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00002006
2007 getProgramPaths().push_back(getDriver().getInstalledDir());
2008 if (getDriver().getInstalledDir() != getDriver().Dir)
2009 getProgramPaths().push_back(getDriver().Dir);
2010
2011 getFilePaths().push_back(getDriver().Dir + "/../lib");
2012 getFilePaths().push_back("/usr/lib");
2013}
2014
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002015Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002016 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00002017 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002018 Key = Action::AnalyzeJobClass;
2019 else
2020 Key = JA.getKind();
David Chisnallf571cde2012-02-15 13:39:01 +00002021
2022 Tool *&T = Tools[Key];
2023 if (!T) {
2024 switch (Key) {
2025 case Action::AssembleJobClass:
2026 T = new tools::solaris::Assemble(*this); break;
2027 case Action::LinkJobClass:
2028 T = new tools::solaris::Link(*this); break;
2029 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002030 T = &Generic_GCC::SelectTool(C, JA);
David Chisnallf571cde2012-02-15 13:39:01 +00002031 }
2032 }
2033
2034 return *T;
2035}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002036
Eli Friedman5cd659f2009-05-26 07:52:18 +00002037/// Linux toolchain (very bare-bones at the moment).
2038
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002039enum LinuxDistro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002040 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002041 DebianLenny,
2042 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00002043 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002044 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00002045 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00002046 RHEL4,
2047 RHEL5,
2048 RHEL6,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002049 Fedora13,
2050 Fedora14,
Eric Christopher534b6a02011-04-06 18:22:53 +00002051 Fedora15,
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002052 Fedora16,
Eric Christopher534b6a02011-04-06 18:22:53 +00002053 FedoraRawhide,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002054 OpenSuse11_3,
David Chisnallb8f65e22011-05-19 13:26:33 +00002055 OpenSuse11_4,
2056 OpenSuse12_1,
Douglas Gregorf7b88412012-04-30 23:42:57 +00002057 OpenSuse12_2,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00002058 UbuntuHardy,
2059 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00002060 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00002061 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002062 UbuntuLucid,
2063 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00002064 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00002065 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002066 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00002067 UbuntuQuantal,
2068 UbuntuRaring,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002069 UnknownDistro
2070};
2071
Chris Lattner84e38552011-05-22 05:36:06 +00002072static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002073 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
2074 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002075}
2076
2077static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregorf7b88412012-04-30 23:42:57 +00002078 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002079}
2080
2081static bool IsDebian(enum LinuxDistro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002082 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002083}
2084
2085static bool IsUbuntu(enum LinuxDistro Distro) {
Rafael Espindola62e87702012-12-13 20:26:05 +00002086 return Distro >= UbuntuHardy && Distro <= UbuntuRaring;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002087}
2088
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002089static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00002090 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002091 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002092 StringRef Data = File.get()->getBuffer();
2093 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002094 Data.split(Lines, "\n");
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002095 LinuxDistro Version = UnknownDistro;
2096 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2097 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
2098 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
2099 .Case("hardy", UbuntuHardy)
2100 .Case("intrepid", UbuntuIntrepid)
2101 .Case("jaunty", UbuntuJaunty)
2102 .Case("karmic", UbuntuKarmic)
2103 .Case("lucid", UbuntuLucid)
2104 .Case("maverick", UbuntuMaverick)
2105 .Case("natty", UbuntuNatty)
2106 .Case("oneiric", UbuntuOneiric)
2107 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002108 .Case("quantal", UbuntuQuantal)
2109 .Case("raring", UbuntuRaring)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002110 .Default(UnknownDistro);
2111 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002112 }
2113
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002114 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002115 StringRef Data = File.get()->getBuffer();
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002116 if (Data.startswith("Fedora release 16"))
2117 return Fedora16;
2118 else if (Data.startswith("Fedora release 15"))
Eric Christopher534b6a02011-04-06 18:22:53 +00002119 return Fedora15;
2120 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002121 return Fedora14;
Eric Christopher534b6a02011-04-06 18:22:53 +00002122 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002123 return Fedora13;
Eric Christopher534b6a02011-04-06 18:22:53 +00002124 else if (Data.startswith("Fedora release") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002125 Data.find("Rawhide") != StringRef::npos)
Eric Christopher534b6a02011-04-06 18:22:53 +00002126 return FedoraRawhide;
Chris Lattner84e38552011-05-22 05:36:06 +00002127 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002128 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002129 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002130 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002131 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002132 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002133 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002134 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002135 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002136 Data.find("release 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002137 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002138 return UnknownDistro;
2139 }
2140
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002141 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002142 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002143 if (Data[0] == '5')
2144 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002145 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002146 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002147 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002148 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002149 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2150 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002151 return UnknownDistro;
2152 }
2153
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002154 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
2155 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
2156 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2157 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2158 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregorf7b88412012-04-30 23:42:57 +00002159 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002160 .Default(UnknownDistro);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002161
Michael J. Spencerf6efe582011-01-10 02:34:13 +00002162 bool Exists;
2163 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola12479842010-11-11 02:07:13 +00002164 return Exherbo;
2165
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002166 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2167 return ArchLinux;
2168
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002169 return UnknownDistro;
2170}
2171
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002172/// \brief Get our best guess at the multiarch triple for a target.
2173///
2174/// Debian-based systems are starting to use a multiarch setup where they use
2175/// a target-triple directory in the library and header search paths.
2176/// Unfortunately, this triple does not align with the vanilla target triple,
2177/// so we provide a rough mapping here.
2178static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2179 StringRef SysRoot) {
2180 // For most architectures, just use whatever we have rather than trying to be
2181 // clever.
2182 switch (TargetTriple.getArch()) {
2183 default:
2184 return TargetTriple.str();
2185
2186 // We use the existence of '/lib/<triple>' as a directory to detect some
2187 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002188 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2189 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002190 case llvm::Triple::arm:
2191 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002192 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2193 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2194 return "arm-linux-gnueabihf";
2195 } else {
2196 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2197 return "arm-linux-gnueabi";
2198 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002199 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002200 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002201 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2202 return "i386-linux-gnu";
2203 return TargetTriple.str();
2204 case llvm::Triple::x86_64:
2205 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2206 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002207 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002208 case llvm::Triple::aarch64:
2209 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2210 return "aarch64-linux-gnu";
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002211 case llvm::Triple::mips:
2212 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2213 return "mips-linux-gnu";
2214 return TargetTriple.str();
2215 case llvm::Triple::mipsel:
2216 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2217 return "mipsel-linux-gnu";
2218 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002219 case llvm::Triple::ppc:
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00002220 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2221 return "powerpc-linux-gnuspe";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002222 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2223 return "powerpc-linux-gnu";
2224 return TargetTriple.str();
2225 case llvm::Triple::ppc64:
2226 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2227 return "powerpc64-linux-gnu";
2228 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002229 }
2230}
2231
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002232static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2233 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2234}
2235
Simon Atanasyand4413882012-09-14 11:27:24 +00002236static bool isMipsArch(llvm::Triple::ArchType Arch) {
2237 return Arch == llvm::Triple::mips ||
2238 Arch == llvm::Triple::mipsel ||
2239 Arch == llvm::Triple::mips64 ||
2240 Arch == llvm::Triple::mips64el;
2241}
2242
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002243static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2244 const ArgList &Args) {
2245 if (Arch != llvm::Triple::mips &&
2246 Arch != llvm::Triple::mipsel)
2247 return false;
2248
2249 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2250 options::OPT_mcpu_EQ,
2251 options::OPT_mips_CPUs_Group);
2252
2253 if (!A)
2254 return false;
2255
2256 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2257 return A->getOption().matches(options::OPT_mips32r2);
2258
Richard Smithbd55daf2012-11-01 04:30:05 +00002259 return A->getValue() == StringRef("mips32r2");
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002260}
2261
Simon Atanasyand4413882012-09-14 11:27:24 +00002262static StringRef getMultilibDir(const llvm::Triple &Triple,
2263 const ArgList &Args) {
2264 if (!isMipsArch(Triple.getArch()))
2265 return Triple.isArch32Bit() ? "lib32" : "lib64";
2266
2267 // lib32 directory has a special meaning on MIPS targets.
2268 // It contains N32 ABI binaries. Use this folder if produce
2269 // code for N32 ABI only.
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00002270 if (hasMipsN32ABIArg(Args))
Simon Atanasyand4413882012-09-14 11:27:24 +00002271 return "lib32";
2272
2273 return Triple.isArch32Bit() ? "lib" : "lib64";
2274}
2275
Rafael Espindola1af7c212012-02-19 01:38:32 +00002276Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2277 : Generic_ELF(D, Triple, Args) {
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002278 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruth2a649c72011-10-03 06:41:08 +00002279 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002280
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002281 // OpenSuse stores the linker with the compiler, add that to the search
2282 // path.
2283 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002284 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002285 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002286
2287 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002288
2289 LinuxDistro Distro = DetectLinuxDistro(Arch);
2290
Chris Lattnerd075c822011-05-22 16:45:07 +00002291 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002292 ExtraOpts.push_back("-z");
2293 ExtraOpts.push_back("relro");
2294 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002295
Douglas Gregord9bb1522011-03-06 19:11:49 +00002296 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002297 ExtraOpts.push_back("-X");
2298
Logan Chienc6fd8202012-09-02 09:30:11 +00002299 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002300
Chandler Carruth0b842912011-12-09 04:45:18 +00002301 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2302 // and the MIPS ABI require .dynsym to be sorted in different ways.
2303 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2304 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002305 // Android loader does not support .gnu.hash.
Simon Atanasyand4413882012-09-14 11:27:24 +00002306 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002307 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2308 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002309 ExtraOpts.push_back("--hash-style=gnu");
2310
2311 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2312 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2313 ExtraOpts.push_back("--hash-style=both");
2314 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002315
Chris Lattner84e38552011-05-22 05:36:06 +00002316 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002317 ExtraOpts.push_back("--no-add-needed");
2318
Eli Friedmanf7600942011-06-02 21:36:53 +00002319 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002320 Distro == DebianJessie || IsOpenSuse(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002321 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002322 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002323 ExtraOpts.push_back("--build-id");
2324
Chris Lattnerd075c822011-05-22 16:45:07 +00002325 if (IsOpenSuse(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002326 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002327
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002328 // The selection of paths to try here is designed to match the patterns which
2329 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2330 // This was determined by running GCC in a fake filesystem, creating all
2331 // possible permutations of these directories, and seeing which ones it added
2332 // to the link paths.
2333 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002334
Simon Atanasyand4413882012-09-14 11:27:24 +00002335 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002336 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002337
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002338 // Add the multilib suffixed paths where they are available.
2339 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002340 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002341 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002342
2343 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2344 addPathIfExists(GCCInstallation.getInstallPath() +
2345 GCCInstallation.getMultiarchSuffix() +
2346 "/mips-r2",
2347 Paths);
2348 else
2349 addPathIfExists((GCCInstallation.getInstallPath() +
2350 GCCInstallation.getMultiarchSuffix()),
2351 Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002352
2353 // If the GCC installation we found is inside of the sysroot, we want to
2354 // prefer libraries installed in the parent prefix of the GCC installation.
2355 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002356 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002357 // This usually happens when there is an external cross compiler on the
2358 // host system, and a more minimal sysroot available that is the target of
2359 // the cross.
2360 if (StringRef(LibPath).startswith(SysRoot)) {
2361 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2362 Paths);
2363 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2364 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2365 }
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002366 // On Android, libraries in the parent prefix of the GCC installation are
2367 // preferred to the ones under sysroot.
2368 if (IsAndroid) {
2369 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2370 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002371 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002372 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2373 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2374 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2375 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2376
2377 // Try walking via the GCC triple path in case of multiarch GCC
2378 // installations with strange symlinks.
2379 if (GCCInstallation.isValid())
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002380 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002381 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002382
Chandler Carrutha7b44142011-10-16 10:54:30 +00002383 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002384 if (GCCInstallation.isValid()) {
2385 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002386 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth866faab2012-01-25 07:21:38 +00002387 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002388 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002389
2390 if (StringRef(LibPath).startswith(SysRoot)) {
2391 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2392 addPathIfExists(LibPath, Paths);
2393 }
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002394 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002395 addPathIfExists(SysRoot + "/lib", Paths);
2396 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002397}
2398
2399bool Linux::HasNativeLLVMSupport() const {
2400 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002401}
2402
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002403Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002404 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00002405 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002406 Key = Action::AnalyzeJobClass;
2407 else
2408 Key = JA.getKind();
2409
Rafael Espindola92b00932010-08-10 00:25:48 +00002410 Tool *&T = Tools[Key];
2411 if (!T) {
2412 switch (Key) {
2413 case Action::AssembleJobClass:
Rafael Espindola248e2192013-03-18 17:52:57 +00002414 if (useIntegratedAs(C.getArgs()))
Rafael Espindola96aef792010-11-07 23:13:01 +00002415 T = new tools::ClangAs(*this);
2416 else
2417 T = new tools::linuxtools::Assemble(*this);
2418 break;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002419 case Action::LinkJobClass:
2420 T = new tools::linuxtools::Link(*this); break;
Rafael Espindola92b00932010-08-10 00:25:48 +00002421 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002422 T = &Generic_GCC::SelectTool(C, JA);
Rafael Espindola92b00932010-08-10 00:25:48 +00002423 }
2424 }
2425
2426 return *T;
2427}
2428
Chandler Carruth05fb5852012-11-21 23:40:23 +00002429void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2430 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002431 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carruth05fb5852012-11-21 23:40:23 +00002432 bool UseInitArrayDefault
2433 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002434 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002435 getTriple().getEnvironment() == llvm::Triple::Android;
2436 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2437 options::OPT_fno_use_init_array,
2438 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002439 CC1Args.push_back("-fuse-init-array");
2440}
2441
Chandler Carrutha796f532011-11-05 20:17:13 +00002442void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2443 ArgStringList &CC1Args) const {
2444 const Driver &D = getDriver();
2445
2446 if (DriverArgs.hasArg(options::OPT_nostdinc))
2447 return;
2448
2449 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2450 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2451
2452 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carrutha796f532011-11-05 20:17:13 +00002453 llvm::sys::Path P(D.ResourceDir);
2454 P.appendComponent("include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002455 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002456 }
2457
2458 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2459 return;
2460
2461 // Check for configure-time C include directories.
2462 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2463 if (CIncludeDirs != "") {
2464 SmallVector<StringRef, 5> dirs;
2465 CIncludeDirs.split(dirs, ":");
2466 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2467 I != E; ++I) {
2468 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2469 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2470 }
2471 return;
2472 }
2473
2474 // Lacking those, try to detect the correct set of system includes for the
2475 // target triple.
2476
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002477 // Implement generic Debian multiarch support.
2478 const StringRef X86_64MultiarchIncludeDirs[] = {
2479 "/usr/include/x86_64-linux-gnu",
2480
2481 // FIXME: These are older forms of multiarch. It's not clear that they're
2482 // in use in any released version of Debian, so we should consider
2483 // removing them.
2484 "/usr/include/i686-linux-gnu/64",
2485 "/usr/include/i486-linux-gnu/64"
2486 };
2487 const StringRef X86MultiarchIncludeDirs[] = {
2488 "/usr/include/i386-linux-gnu",
2489
2490 // FIXME: These are older forms of multiarch. It's not clear that they're
2491 // in use in any released version of Debian, so we should consider
2492 // removing them.
2493 "/usr/include/x86_64-linux-gnu/32",
2494 "/usr/include/i686-linux-gnu",
2495 "/usr/include/i486-linux-gnu"
2496 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002497 const StringRef AArch64MultiarchIncludeDirs[] = {
2498 "/usr/include/aarch64-linux-gnu"
2499 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002500 const StringRef ARMMultiarchIncludeDirs[] = {
2501 "/usr/include/arm-linux-gnueabi"
2502 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002503 const StringRef ARMHFMultiarchIncludeDirs[] = {
2504 "/usr/include/arm-linux-gnueabihf"
2505 };
Eli Friedman7771c832011-11-11 03:05:19 +00002506 const StringRef MIPSMultiarchIncludeDirs[] = {
2507 "/usr/include/mips-linux-gnu"
2508 };
2509 const StringRef MIPSELMultiarchIncludeDirs[] = {
2510 "/usr/include/mipsel-linux-gnu"
2511 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002512 const StringRef PPCMultiarchIncludeDirs[] = {
2513 "/usr/include/powerpc-linux-gnu"
2514 };
2515 const StringRef PPC64MultiarchIncludeDirs[] = {
2516 "/usr/include/powerpc64-linux-gnu"
2517 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002518 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002519 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002520 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002521 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002522 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002523 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2524 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002525 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002526 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2527 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2528 else
2529 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002530 } else if (getTriple().getArch() == llvm::Triple::mips) {
2531 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2532 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2533 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002534 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2535 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2536 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2537 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002538 }
2539 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2540 E = MultiarchIncludeDirs.end();
2541 I != E; ++I) {
Chandler Carruth6e46ca22011-11-09 03:46:20 +00002542 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002543 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2544 break;
2545 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002546 }
2547
2548 if (getTriple().getOS() == llvm::Triple::RTEMS)
2549 return;
2550
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002551 // Add an include of '/include' directly. This isn't provided by default by
2552 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2553 // add even when Clang is acting as-if it were a system compiler.
2554 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2555
Chandler Carrutha796f532011-11-05 20:17:13 +00002556 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2557}
2558
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002559/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002560/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2561 const ArgList &DriverArgs,
2562 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002563 if (!llvm::sys::fs::exists(Base))
2564 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002565 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002566 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002567 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002568 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002569}
2570
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002571/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2572/// libstdc++ installation.
2573/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2574 Twine TargetArchDir,
2575 Twine MultiLibSuffix,
2576 const ArgList &DriverArgs,
2577 ArgStringList &CC1Args) {
2578 if (!addLibStdCXXIncludePaths(Base+Suffix, TargetArchDir + MultiLibSuffix,
2579 DriverArgs, CC1Args))
2580 return false;
2581
2582 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
2583 + MultiLibSuffix);
2584 return true;
2585}
2586
Chandler Carrutha796f532011-11-05 20:17:13 +00002587void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2588 ArgStringList &CC1Args) const {
2589 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2590 DriverArgs.hasArg(options::OPT_nostdincxx))
2591 return;
2592
Chandler Carruthf4701732011-11-07 09:01:17 +00002593 // Check if libc++ has been enabled and provide its include paths if so.
2594 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2595 // libc++ is always installed at a fixed path on Linux currently.
2596 addSystemInclude(DriverArgs, CC1Args,
2597 getDriver().SysRoot + "/usr/include/c++/v1");
2598 return;
2599 }
2600
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002601 // We need a detected GCC installation on Linux to provide libstdc++'s
2602 // headers. We handled the libc++ case above.
2603 if (!GCCInstallation.isValid())
2604 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002605
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002606 // By default, look for the C++ headers in an include directory adjacent to
2607 // the lib directory of the GCC installation. Note that this is expect to be
2608 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2609 StringRef LibDir = GCCInstallation.getParentLibPath();
2610 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola66aa0452012-06-19 01:26:10 +00002611 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002612 StringRef TripleStr = GCCInstallation.getTriple().str();
2613
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002614 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2615 "/c++/" + Version.str(),
2616 TripleStr,
2617 GCCInstallation.getMultiarchSuffix(),
2618 DriverArgs, CC1Args))
2619 return;
2620
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002621 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002622 // Gentoo is weird and places its headers inside the GCC install, so if the
2623 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002624 InstallDir.str() + "/include/g++-v4",
2625 // Android standalone toolchain has C++ headers in yet another place.
2626 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkelf3587912012-09-18 22:25:07 +00002627 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2628 // without a subdirectory corresponding to the gcc version.
2629 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002630 };
2631
2632 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2633 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2634 GCCInstallation.getMultiarchSuffix()),
2635 DriverArgs, CC1Args))
2636 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002637 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002638}
2639
Daniel Dunbarcc912342009-05-02 18:28:39 +00002640/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2641
Rafael Espindola1af7c212012-02-19 01:38:32 +00002642DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2643 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002644
2645 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002646 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002647 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002648 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002649
Daniel Dunbar083edf72009-12-21 18:54:17 +00002650 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002651 getFilePaths().push_back("/usr/lib");
2652 getFilePaths().push_back("/usr/lib/gcc41");
2653}
2654
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002655Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002656 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00002657 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002658 Key = Action::AnalyzeJobClass;
2659 else
2660 Key = JA.getKind();
Daniel Dunbarcc912342009-05-02 18:28:39 +00002661
2662 Tool *&T = Tools[Key];
2663 if (!T) {
2664 switch (Key) {
2665 case Action::AssembleJobClass:
2666 T = new tools::dragonfly::Assemble(*this); break;
2667 case Action::LinkJobClass:
2668 T = new tools::dragonfly::Link(*this); break;
2669 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002670 T = &Generic_GCC::SelectTool(C, JA);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002671 }
2672 }
2673
2674 return *T;
2675}