blob: c8038d18888f509d96f7db5b614e7256c941f53c [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 Dunbar6232d342010-05-20 21:48:38 +0000187 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
188 options::OPT_no_integrated_as,
Bob Wilsond32fba22011-10-30 00:20:28 +0000189 IsIntegratedAssemblerDefault());
Daniel Dunbar6232d342010-05-20 21:48:38 +0000190
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000191 Tool *&T = Tools[Key];
192 if (!T) {
193 switch (Key) {
194 case Action::InputClass:
195 case Action::BindArchClass:
David Blaikie83d382b2011-09-23 05:06:16 +0000196 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000197 case Action::PreprocessJobClass:
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000198 case Action::AnalyzeJobClass:
Ted Kremenekf7639e12012-03-06 20:06:33 +0000199 case Action::MigrateJobClass:
Daniel Dunbarf64f5302009-03-29 22:27:40 +0000200 case Action::PrecompileJobClass:
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000201 case Action::CompileJobClass:
Bob Wilsondecc03e2012-11-23 06:14:39 +0000202 T = new tools::Clang(*this); break;
Daniel Dunbar6232d342010-05-20 21:48:38 +0000203 case Action::AssembleJobClass: {
204 if (UseIntegratedAs)
205 T = new tools::ClangAs(*this);
206 else
207 T = new tools::darwin::Assemble(*this);
208 break;
209 }
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000210 case Action::LinkJobClass:
Daniel Dunbar5095b292009-09-04 17:39:02 +0000211 T = new tools::darwin::Link(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000212 case Action::LipoJobClass:
213 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar88299622010-06-04 18:28:36 +0000214 case Action::DsymutilJobClass:
215 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopher551ef452011-08-23 17:56:55 +0000216 case Action::VerifyJobClass:
217 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000218 }
219 }
220
221 return *T;
222}
223
Daniel Dunbar26d482a2009-09-18 08:15:03 +0000224
Chandler Carruthd7fa2e02012-01-31 02:21:20 +0000225DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
226 : Darwin(D, Triple)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000227{
Daniel Dunbar00aff042010-09-17 08:22:12 +0000228 getProgramPaths().push_back(getDriver().getInstalledDir());
229 if (getDriver().getInstalledDir() != getDriver().Dir)
230 getProgramPaths().push_back(getDriver().Dir);
231
Daniel Dunbar6276f992009-09-18 08:15:13 +0000232 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbar88979912010-08-01 22:29:51 +0000233 getProgramPaths().push_back(getDriver().getInstalledDir());
234 if (getDriver().getInstalledDir() != getDriver().Dir)
235 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar6276f992009-09-18 08:15:13 +0000236}
237
John McCall31168b02011-06-15 23:02:42 +0000238void DarwinClang::AddLinkARCArgs(const ArgList &Args,
239 ArgStringList &CmdArgs) const {
Eric Christopher551ef452011-08-23 17:56:55 +0000240
241 CmdArgs.push_back("-force_load");
John McCall31168b02011-06-15 23:02:42 +0000242 llvm::sys::Path P(getDriver().ClangExecutable);
243 P.eraseComponent(); // 'clang'
244 P.eraseComponent(); // 'bin'
245 P.appendComponent("lib");
246 P.appendComponent("arc");
247 P.appendComponent("libarclite_");
248 std::string s = P.str();
249 // Mash in the platform.
Argyrios Kyrtzidis058b4512011-10-18 17:40:15 +0000250 if (isTargetIOSSimulator())
251 s += "iphonesimulator";
252 else if (isTargetIPhoneOS())
John McCall31168b02011-06-15 23:02:42 +0000253 s += "iphoneos";
John McCall31168b02011-06-15 23:02:42 +0000254 else
255 s += "macosx";
256 s += ".a";
257
258 CmdArgs.push_back(Args.MakeArgString(s));
259}
260
Eric Christopherc235d0c62011-06-22 17:41:40 +0000261void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopher551ef452011-08-23 17:56:55 +0000262 ArgStringList &CmdArgs,
Alexey Samsonov8368b372012-11-21 14:17:42 +0000263 const char *DarwinStaticLib,
264 bool AlwaysLink) const {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000265 llvm::sys::Path P(getDriver().ResourceDir);
266 P.appendComponent("lib");
267 P.appendComponent("darwin");
268 P.appendComponent(DarwinStaticLib);
Eric Christopher551ef452011-08-23 17:56:55 +0000269
Eric Christopherc235d0c62011-06-22 17:41:40 +0000270 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov8368b372012-11-21 14:17:42 +0000271 // not have compiler-rt checked out or integrated into their build (unless
272 // we explicitly force linking with this library).
Eric Christopherc235d0c62011-06-22 17:41:40 +0000273 bool Exists;
Alexey Samsonov8368b372012-11-21 14:17:42 +0000274 if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
Eric Christopherc235d0c62011-06-22 17:41:40 +0000275 CmdArgs.push_back(Args.MakeArgString(P.str()));
276}
277
Daniel Dunbar6276f992009-09-18 08:15:13 +0000278void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
279 ArgStringList &CmdArgs) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000280 // Darwin only supports the compiler-rt based runtime libraries.
281 switch (GetRuntimeLibType(Args)) {
282 case ToolChain::RLT_CompilerRT:
283 break;
284 default:
285 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smithbd55daf2012-11-01 04:30:05 +0000286 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000287 return;
288 }
289
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000290 // Darwin doesn't support real static executables, don't link any runtime
291 // libraries with -static.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000292 if (Args.hasArg(options::OPT_static) ||
293 Args.hasArg(options::OPT_fapple_kext) ||
294 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar6276f992009-09-18 08:15:13 +0000295 return;
Daniel Dunbar6276f992009-09-18 08:15:13 +0000296
297 // Reject -static-libgcc for now, we can deal with this when and if someone
298 // cares. This is useful in situations where someone wants to statically link
299 // something like libstdc++, and needs its runtime support routines.
300 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000301 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000302 << A->getAsString(Args);
303 return;
304 }
305
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000306 // If we are building profile support, link that library in.
307 if (Args.hasArg(options::OPT_fprofile_arcs) ||
308 Args.hasArg(options::OPT_fprofile_generate) ||
309 Args.hasArg(options::OPT_fcreate_profile) ||
310 Args.hasArg(options::OPT_coverage)) {
311 // Select the appropriate runtime library for the target.
312 if (isTargetIPhoneOS()) {
313 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
314 } else {
315 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
316 }
317 }
318
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000319 SanitizerArgs Sanitize(getDriver(), Args);
320
Alexey Samsonovcc429802012-11-16 12:53:14 +0000321 // Add Ubsan runtime library, if required.
322 if (Sanitize.needsUbsanRt()) {
Alexey Samsonov969be242013-01-21 08:45:02 +0000323 if (isTargetIPhoneOS()) {
Alexey Samsonovcc429802012-11-16 12:53:14 +0000324 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
325 << "-fsanitize=undefined";
326 } else {
Alexey Samsonov8368b372012-11-21 14:17:42 +0000327 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonovcc429802012-11-16 12:53:14 +0000328
329 // The Ubsan runtime library requires C++.
330 AddCXXStdlibLibArgs(Args, CmdArgs);
331 }
332 }
333
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000334 // Add ASAN runtime library, if required. Dynamic libraries and bundles
335 // should not be linked with the runtime library.
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000336 if (Sanitize.needsAsanRt()) {
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000337 if (Args.hasArg(options::OPT_dynamiclib) ||
Alexey Samsonovcc429802012-11-16 12:53:14 +0000338 Args.hasArg(options::OPT_bundle)) {
339 // Assume the binary will provide the ASan runtime.
340 } else if (isTargetIPhoneOS()) {
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000341 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000342 << "-fsanitize=address";
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000343 } else {
Alexander Potapenkof6b56972013-01-22 09:16:03 +0000344 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx_dynamic.dylib", true);
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000345
Alexander Potapenkof6b56972013-01-22 09:16:03 +0000346 // The ASAN runtime library requires C++.
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000347 AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000348 }
349 }
350
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000351 // Otherwise link libSystem, then the dynamic runtime library, and finally any
352 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000353 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000354
355 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar15c89422010-01-27 00:56:37 +0000356 if (isTargetIPhoneOS()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000357 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
358 // it never went into the SDK.
Bob Wilson102be442011-10-07 17:54:41 +0000359 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
360 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
361 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000362
Daniel Dunbard1076382011-04-18 23:48:36 +0000363 // We currently always need a static runtime library for iOS.
Eric Christopherc235d0c62011-06-22 17:41:40 +0000364 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000365 } else {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000366 // The dynamic runtime library was merged with libSystem for 10.6 and
367 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000368 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000369 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000370 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000371 CmdArgs.push_back("-lgcc_s.10.5");
372
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000373 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000374 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000375 // omitted from 10.4.dylib.
376 //
377 // Unfortunately, that turned out to not be true, because Darwin system
378 // headers can still use eprintf on i386, and it is not exported from
379 // libSystem. Therefore, we still must provide a runtime library just for
380 // the tiny tiny handful of projects that *might* use that symbol.
381 if (isMacosxVersionLT(10, 5)) {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000382 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000383 } else {
384 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopherc235d0c62011-06-22 17:41:40 +0000385 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
386 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000387 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000388 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000389}
390
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000391void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000392 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000393
Daniel Dunbar455a0492012-08-17 18:43:50 +0000394 // Support allowing the SDKROOT environment variable used by xcrun and other
395 // Xcode tools to define the default sysroot, by making it the default for
396 // isysroot.
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000397 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
398 // Warn if the path does not exist.
399 bool Exists;
400 if (llvm::sys::fs::exists(A->getValue(), Exists) || !Exists)
401 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
402 } else {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000403 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbarb2543042013-01-15 20:33:56 +0000404 // We only use this value as the default if it is an absolute path,
405 // exists, and it is not the root path.
406 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
407 StringRef(env) != "/") {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000408 Args.append(Args.MakeSeparateArg(
409 0, Opts.getOption(options::OPT_isysroot), env));
410 }
411 }
412 }
413
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000414 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000415 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
416 Arg *iOSSimVersion = Args.getLastArg(
417 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman027e9c32012-01-11 02:41:15 +0000418
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000419 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000420 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000421 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000422 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
423 iOSVersion = iOSSimVersion = 0;
424 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000425 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000426 << iOSVersion->getAsString(Args)
427 << iOSSimVersion->getAsString(Args);
428 iOSSimVersion = 0;
429 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosier64707fe2011-08-31 20:56:25 +0000430 // If no deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000431 // environment defines.
Chad Rosier64707fe2011-08-31 20:56:25 +0000432 StringRef OSXTarget;
433 StringRef iOSTarget;
434 StringRef iOSSimTarget;
435 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
436 OSXTarget = env;
437 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
438 iOSTarget = env;
439 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
440 iOSSimTarget = env;
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000441
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000442 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosier64707fe2011-08-31 20:56:25 +0000443 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif5d3231c2012-04-18 10:59:08 +0000444 // based on -isysroot.
Chad Rosier64707fe2011-08-31 20:56:25 +0000445 if (iOSTarget.empty()) {
446 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
447 StringRef first, second;
Richard Smithbd55daf2012-11-01 04:30:05 +0000448 StringRef isysroot = A->getValue();
Chad Rosier64707fe2011-08-31 20:56:25 +0000449 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
450 if (second != "")
451 iOSTarget = second.substr(0,3);
452 }
453 }
Daniel Dunbard54669d2010-01-26 01:45:19 +0000454
Chad Rosierfe6fd362011-09-28 00:46:32 +0000455 // If no OSX or iOS target has been specified and we're compiling for armv7,
456 // go ahead as assume we're targeting iOS.
Chad Rosier7b1fee12012-05-09 18:55:57 +0000457 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilsond7cf1042012-09-29 23:52:50 +0000458 (getDarwinArchName(Args) == "armv7" ||
459 getDarwinArchName(Args) == "armv7s"))
Chad Rosierf761fe92012-05-09 18:09:58 +0000460 iOSTarget = iOSVersionMin;
Chad Rosierfe6fd362011-09-28 00:46:32 +0000461
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000462 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000463 //
464 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000465
466 // Do not allow conflicts with the iOS simulator target.
Chad Rosier64707fe2011-08-31 20:56:25 +0000467 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000468 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000469 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosier64707fe2011-08-31 20:56:25 +0000470 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000471 "IPHONEOS_DEPLOYMENT_TARGET");
472 }
473
474 // Allow conflicts among OSX and iOS for historical reasons, but choose the
475 // default platform.
Chad Rosier64707fe2011-08-31 20:56:25 +0000476 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000477 if (getTriple().getArch() == llvm::Triple::arm ||
478 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosier64707fe2011-08-31 20:56:25 +0000479 OSXTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000480 else
Chad Rosier64707fe2011-08-31 20:56:25 +0000481 iOSTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000482 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000483
Chad Rosier64707fe2011-08-31 20:56:25 +0000484 if (!OSXTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000485 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000486 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
487 Args.append(OSXVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000488 } else if (!iOSTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000489 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000490 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
491 Args.append(iOSVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000492 } else if (!iOSSimTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000493 const Option O = Opts.getOption(
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000494 options::OPT_mios_simulator_version_min_EQ);
495 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
496 Args.append(iOSSimVersion);
Daniel Dunbard54669d2010-01-26 01:45:19 +0000497 } else {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000498 // Otherwise, assume we are targeting OS X.
Michael J. Spencerfc790902012-10-19 22:36:40 +0000499 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000500 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
501 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000502 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000505 // Reject invalid architecture combinations.
506 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
507 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000508 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000509 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
510 }
511
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000512 // Set the tool chain target information.
513 unsigned Major, Minor, Micro;
514 bool HadExtra;
515 if (OSXVersion) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000516 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000517 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000518 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000519 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000520 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000521 << OSXVersion->getAsString(Args);
522 } else {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000523 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
524 assert(Version && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000525 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman027e9c32012-01-11 02:41:15 +0000526 Micro, HadExtra) || HadExtra ||
527 Major >= 10 || Minor >= 100 || Micro >= 100)
528 getDriver().Diag(diag::err_drv_invalid_version_number)
529 << Version->getAsString(Args);
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000530 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000531
Daniel Dunbarb1189432011-04-30 04:18:16 +0000532 bool IsIOSSim = bool(iOSSimVersion);
533
534 // In GCC, the simulator historically was treated as being OS X in some
535 // contexts, like determining the link logic, despite generally being called
536 // with an iOS deployment target. For compatibility, we detect the
537 // simulator as iOS + x86, and treat it differently in a few contexts.
538 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
539 getTriple().getArch() == llvm::Triple::x86_64))
540 IsIOSSim = true;
541
542 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000543}
544
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000545void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000546 ArgStringList &CmdArgs) const {
547 CXXStdlibType Type = GetCXXStdlibType(Args);
548
549 switch (Type) {
550 case ToolChain::CST_Libcxx:
551 CmdArgs.push_back("-lc++");
552 break;
553
554 case ToolChain::CST_Libstdcxx: {
555 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
556 // it was previously found in the gcc lib dir. However, for all the Darwin
557 // platforms we care about it was -lstdc++.6, so we search for that
558 // explicitly if we can't see an obvious -lstdc++ candidate.
559
560 // Check in the sysroot first.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000561 bool Exists;
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000562 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000563 llvm::sys::Path P(A->getValue());
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000564 P.appendComponent("usr");
565 P.appendComponent("lib");
566 P.appendComponent("libstdc++.dylib");
567
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000568 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000569 P.eraseComponent();
570 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000571 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000572 CmdArgs.push_back(Args.MakeArgString(P.str()));
573 return;
574 }
575 }
576 }
577
578 // Otherwise, look in the root.
Bob Wilson1a9ad0f2011-11-11 07:47:04 +0000579 // FIXME: This should be removed someday when we don't have to care about
580 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000581 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
582 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000583 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
584 return;
585 }
586
587 // Otherwise, let the linker search.
588 CmdArgs.push_back("-lstdc++");
589 break;
590 }
591 }
592}
593
Shantonu Senafeb03b2010-09-17 18:39:08 +0000594void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
595 ArgStringList &CmdArgs) const {
596
597 // For Darwin platforms, use the compiler-rt-based support library
598 // instead of the gcc-provided one (which is also incidentally
599 // only present in the gcc lib dir, which makes it hard to find).
600
601 llvm::sys::Path P(getDriver().ResourceDir);
602 P.appendComponent("lib");
603 P.appendComponent("darwin");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000604
605 // Use the newer cc_kext for iOS ARM after 6.0.
606 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
607 !isIPhoneOSVersionLT(6, 0)) {
608 P.appendComponent("libclang_rt.cc_kext.a");
609 } else {
610 P.appendComponent("libclang_rt.cc_kext_ios5.a");
611 }
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000612
Shantonu Senafeb03b2010-09-17 18:39:08 +0000613 // For now, allow missing resource libraries to support developers who may
614 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000615 bool Exists;
616 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Senafeb03b2010-09-17 18:39:08 +0000617 CmdArgs.push_back(Args.MakeArgString(P.str()));
618}
619
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000620DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
621 const char *BoundArch) const {
622 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
623 const OptTable &Opts = getDriver().getOpts();
624
625 // FIXME: We really want to get out of the tool chain level argument
626 // translation business, as it makes the driver functionality much
627 // more opaque. For now, we follow gcc closely solely for the
628 // purpose of easily achieving feature parity & testability. Once we
629 // have something that works, we should reevaluate each translation
630 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000631
Daniel Dunbar775d4062010-06-11 22:00:26 +0000632 for (ArgList::const_iterator it = Args.begin(),
633 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000634 Arg *A = *it;
635
636 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar471c4f82011-06-21 00:20:17 +0000637 // Skip this argument unless the architecture matches either the toolchain
638 // triple arch, or the arch being bound.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000639 llvm::Triple::ArchType XarchArch =
Richard Smithbd55daf2012-11-01 04:30:05 +0000640 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000641 if (!(XarchArch == getArch() ||
642 (BoundArch && XarchArch ==
Rafael Espindoladcbf6982012-10-31 18:51:07 +0000643 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000644 continue;
645
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000646 Arg *OriginalArg = A;
Richard Smithbd55daf2012-11-01 04:30:05 +0000647 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000648 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000649 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000650
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000651 // If the argument parsing failed or more than one argument was
652 // consumed, the -Xarch_ argument's parameter tried to consume
653 // extra arguments. Emit an error and ignore.
654 //
655 // We also want to disallow any options which would alter the
656 // driver behavior; that isn't going to work in our model. We
657 // use isDriverOption() as an approximation, although things
658 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000659 if (!XarchArg || Index > Prev + 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000660 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar6914a982011-04-21 17:32:21 +0000661 << A->getAsString(Args);
662 continue;
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000663 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000664 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000665 << A->getAsString(Args);
666 continue;
667 }
668
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000669 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000670 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000671
672 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000673
674 // Linker input arguments require custom handling. The problem is that we
675 // have already constructed the phase actions, so we can not treat them as
676 // "input arguments".
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000677 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000678 // Convert the argument into individual Zlinker_input_args.
679 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
680 DAL->AddSeparateArg(OriginalArg,
681 Opts.getOption(options::OPT_Zlinker_input),
Richard Smithbd55daf2012-11-01 04:30:05 +0000682 A->getValue(i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000683
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000684 }
685 continue;
686 }
Mike Stump11289f42009-09-09 15:08:12 +0000687 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000688
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000689 // Sob. These is strictly gcc compatible for the time being. Apple
690 // gcc translates options twice, which means that self-expanding
691 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000692 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000693 default:
694 DAL->append(A);
695 break;
696
697 case options::OPT_mkernel:
698 case options::OPT_fapple_kext:
699 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000700 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000701 break;
Mike Stump11289f42009-09-09 15:08:12 +0000702
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000703 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000704 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smithbd55daf2012-11-01 04:30:05 +0000705 A->getValue());
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000706 break;
707
708 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000709 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
710 DAL->AddFlagArg(A,
711 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000712 break;
713
714 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000715 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
716 DAL->AddFlagArg(A,
717 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000718 break;
719
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000720 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000721 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000722 break;
723
724 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000725 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000726 break;
727
728 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000729 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000730 break;
731
732 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000733 DAL->AddFlagArg(A,
734 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000735 break;
736
737 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000738 DAL->AddFlagArg(A,
739 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000740 break;
741
742 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000743 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000744 break;
745
746 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000747 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000748 break;
749 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000750 }
751
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000752 if (getTriple().getArch() == llvm::Triple::x86 ||
753 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000754 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000755 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000756
757 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosier7c5d9082012-04-27 14:58:16 +0000758 // how the driver driver works.
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000759 if (BoundArch) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000760 StringRef Name = BoundArch;
Michael J. Spencerfc790902012-10-19 22:36:40 +0000761 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
762 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000763
764 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
765 // which defines the list of which architectures we accept.
766 if (Name == "ppc")
767 ;
768 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000769 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000770 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000771 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000772 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000773 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000774 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000775 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000776 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000777 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000778 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000779 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000780 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000781 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000782 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000783 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000784
785 else if (Name == "ppc64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000786 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000787
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000788 else if (Name == "i386")
789 ;
790 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000791 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000792 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000793 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000794 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000795 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000796 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000797 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000798 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000799 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000800 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000801 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000802 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000803 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000804
805 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000806 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000807
808 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000809 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000810 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000811 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000812 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000813 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000814 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000815 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000816 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000817 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000818 else if (Name == "armv6m")
819 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000820 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000821 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000822 else if (Name == "armv7em")
823 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000824 else if (Name == "armv7f")
825 DAL->AddJoinedArg(0, MArch, "armv7f");
826 else if (Name == "armv7k")
827 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000828 else if (Name == "armv7m")
829 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000830 else if (Name == "armv7s")
831 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000832
833 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000834 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000835 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000836
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000837 // Add an explicit version min argument for the deployment target. We do this
838 // after argument translation because -Xarch_ arguments may add a version min
839 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000840 if (BoundArch)
841 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000842
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000843 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
844 // FIXME: It would be far better to avoid inserting those -static arguments,
845 // but we can't check the deployment target in the translation code until
846 // it is set here.
847 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
848 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
849 Arg *A = *it;
850 ++it;
851 if (A->getOption().getID() != options::OPT_mkernel &&
852 A->getOption().getID() != options::OPT_fapple_kext)
853 continue;
854 assert(it != ie && "unexpected argument translation");
855 A = *it;
856 assert(A->getOption().getID() == options::OPT_static &&
857 "missing expected -static argument");
858 it = DAL->getArgs().erase(it);
859 }
860 }
861
Bob Wilson102be442011-10-07 17:54:41 +0000862 // Validate the C++ standard library choice.
863 CXXStdlibType Type = GetCXXStdlibType(*DAL);
864 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000865 // Check whether the target provides libc++.
866 StringRef where;
867
868 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson5ad5a952012-11-09 01:59:30 +0000869 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
870 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000871
872 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000873 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000874 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000875 }
876 }
877
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000878 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000879}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000880
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000881bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000882 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000883}
884
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000885bool Darwin::UseDwarfDebugFlags() const {
886 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
887 return S[0] != '\0';
888 return false;
889}
890
Daniel Dunbar3241d402010-02-10 18:49:11 +0000891bool Darwin::UseSjLjExceptions() const {
892 // Darwin uses SjLj exceptions on ARM.
893 return (getTriple().getArch() == llvm::Triple::arm ||
894 getTriple().getArch() == llvm::Triple::thumb);
895}
896
Chandler Carruth76a943b2012-11-19 03:52:03 +0000897bool Darwin::isPICDefault() const {
898 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000899}
900
Chandler Carruth76a943b2012-11-19 03:52:03 +0000901bool Darwin::isPICDefaultForced() const {
902 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000903}
904
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000905bool Darwin::SupportsProfiling() const {
906 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000907 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000908}
909
Daniel Dunbar16334e12010-04-10 16:20:23 +0000910bool Darwin::SupportsObjCGC() const {
911 // Garbage collection is supported everywhere except on iPhone OS.
912 return !isTargetIPhoneOS();
913}
914
John McCall3deb1ad2012-08-21 02:47:43 +0000915void Darwin::CheckObjCARC() const {
916 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
917 return;
John McCall93207072012-08-27 01:56:21 +0000918 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000919}
920
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000921std::string
Chad Rosierd3a0f952011-09-20 20:44:06 +0000922Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
923 types::ID InputType) const {
924 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000925}
926
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000927/// Generic_GCC - A tool chain using the 'gcc' command to perform
928/// all subcommands; this relies on gcc translating the majority of
929/// command line options.
930
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000931/// \brief Parse a GCCVersion object out of a string of text.
932///
933/// This is the primary means of forming GCCVersion objects.
934/*static*/
935Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
936 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
937 std::pair<StringRef, StringRef> First = VersionText.split('.');
938 std::pair<StringRef, StringRef> Second = First.second.split('.');
939
940 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
941 if (First.first.getAsInteger(10, GoodVersion.Major) ||
942 GoodVersion.Major < 0)
943 return BadVersion;
944 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
945 GoodVersion.Minor < 0)
946 return BadVersion;
947
948 // First look for a number prefix and parse that if present. Otherwise just
949 // stash the entire patch string in the suffix, and leave the number
950 // unspecified. This covers versions strings such as:
951 // 4.4
952 // 4.4.0
953 // 4.4.x
954 // 4.4.2-rc4
955 // 4.4.x-patched
956 // And retains any patch number it finds.
957 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
958 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +0000959 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000960 // Try to parse the number and any suffix.
961 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
962 GoodVersion.Patch < 0)
963 return BadVersion;
964 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
965 }
966 }
967
968 return GoodVersion;
969}
970
971/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
972bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000973 if (Major != RHS.Major)
974 return Major < RHS.Major;
975 if (Minor != RHS.Minor)
976 return Minor < RHS.Minor;
977 if (Patch != RHS.Patch) {
978 // Note that versions without a specified patch sort higher than those with
979 // a patch.
980 if (RHS.Patch == -1)
981 return true;
982 if (Patch == -1)
983 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000984
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000985 // Otherwise just sort on the patch itself.
986 return Patch < RHS.Patch;
987 }
988 if (PatchSuffix != RHS.PatchSuffix) {
989 // Sort empty suffixes higher.
990 if (RHS.PatchSuffix.empty())
991 return true;
992 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +0000993 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000994
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000995 // Provide a lexicographic sort to make this a total ordering.
996 return PatchSuffix < RHS.PatchSuffix;
997 }
998
999 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001000 return false;
1001}
1002
Rafael Espindola1af7c212012-02-19 01:38:32 +00001003static StringRef getGCCToolchainDir(const ArgList &Args) {
1004 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1005 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +00001006 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +00001007 return GCC_INSTALL_PREFIX;
1008}
1009
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001010/// \brief Construct a GCCInstallationDetector from the driver.
1011///
1012/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +00001013/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +00001014///
1015/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1016/// should instead pull the target out of the driver. This is currently
1017/// necessary because the driver doesn't store the final version of the target
1018/// triple.
1019Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1020 const Driver &D,
Rafael Espindola1af7c212012-02-19 01:38:32 +00001021 const llvm::Triple &TargetTriple,
1022 const ArgList &Args)
Chandler Carruth866faab2012-01-25 07:21:38 +00001023 : IsValid(false) {
Chandler Carruth779579b2012-02-13 02:02:09 +00001024 llvm::Triple MultiarchTriple
1025 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1026 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001027 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001028 // The library directories which may contain GCC installations.
Chandler Carruth866faab2012-01-25 07:21:38 +00001029 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001030 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001031 SmallVector<StringRef, 10> CandidateTripleAliases;
1032 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1033 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1034 CandidateTripleAliases,
1035 CandidateMultiarchLibDirs,
1036 CandidateMultiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001037
1038 // Compute the set of prefixes for our search.
1039 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1040 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001041
Rafael Espindola1af7c212012-02-19 01:38:32 +00001042 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1043 if (GCCToolchainDir != "") {
1044 if (GCCToolchainDir.back() == '/')
1045 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001046
Rafael Espindola1af7c212012-02-19 01:38:32 +00001047 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001048 } else {
1049 Prefixes.push_back(D.SysRoot);
1050 Prefixes.push_back(D.SysRoot + "/usr");
1051 Prefixes.push_back(D.InstalledDir + "/..");
1052 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001053
1054 // Loop over the various components which exist and select the best GCC
1055 // installation available. GCC installs are ranked by version number.
1056 Version = GCCVersion::Parse("0.0.0");
1057 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1058 if (!llvm::sys::fs::exists(Prefixes[i]))
1059 continue;
1060 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1061 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1062 if (!llvm::sys::fs::exists(LibDir))
1063 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001064 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001065 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1066 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001067 }
1068 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1069 const std::string LibDir
1070 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1071 if (!llvm::sys::fs::exists(LibDir))
1072 continue;
1073 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1074 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001075 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth866faab2012-01-25 07:21:38 +00001076 CandidateMultiarchTripleAliases[k],
1077 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001078 }
1079 }
1080}
1081
1082/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth866faab2012-01-25 07:21:38 +00001083 const llvm::Triple &TargetTriple,
1084 const llvm::Triple &MultiarchTriple,
1085 SmallVectorImpl<StringRef> &LibDirs,
1086 SmallVectorImpl<StringRef> &TripleAliases,
1087 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1088 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1089 // Declare a bunch of static data sets that we'll select between below. These
1090 // are specifically designed to always refer to string literals to avoid any
1091 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001092 static const char *const AArch64LibDirs[] = { "/lib" };
1093 static const char *const AArch64Triples[] = {
1094 "aarch64-none-linux-gnu",
1095 "aarch64-linux-gnu"
1096 };
1097
Chandler Carruth866faab2012-01-25 07:21:38 +00001098 static const char *const ARMLibDirs[] = { "/lib" };
1099 static const char *const ARMTriples[] = {
1100 "arm-linux-gnueabi",
1101 "arm-linux-androideabi"
1102 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001103 static const char *const ARMHFTriples[] = {
1104 "arm-linux-gnueabihf",
1105 };
Chandler Carruth866faab2012-01-25 07:21:38 +00001106
1107 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1108 static const char *const X86_64Triples[] = {
1109 "x86_64-linux-gnu",
1110 "x86_64-unknown-linux-gnu",
1111 "x86_64-pc-linux-gnu",
1112 "x86_64-redhat-linux6E",
1113 "x86_64-redhat-linux",
1114 "x86_64-suse-linux",
1115 "x86_64-manbo-linux-gnu",
1116 "x86_64-linux-gnu",
1117 "x86_64-slackware-linux"
1118 };
1119 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1120 static const char *const X86Triples[] = {
1121 "i686-linux-gnu",
1122 "i686-pc-linux-gnu",
1123 "i486-linux-gnu",
1124 "i386-linux-gnu",
NAKAMURA Takumi55c12892012-12-07 17:13:18 +00001125 "i386-redhat-linux6E",
Chandler Carruth866faab2012-01-25 07:21:38 +00001126 "i686-redhat-linux",
1127 "i586-redhat-linux",
1128 "i386-redhat-linux",
1129 "i586-suse-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001130 "i486-slackware-linux",
1131 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001132 };
1133
1134 static const char *const MIPSLibDirs[] = { "/lib" };
1135 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1136 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyan53fefd12012-10-03 17:46:38 +00001137 static const char *const MIPSELTriples[] = {
1138 "mipsel-linux-gnu",
1139 "mipsel-linux-android"
1140 };
Chandler Carruth866faab2012-01-25 07:21:38 +00001141
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001142 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1143 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1144 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1145 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1146
Chandler Carruth866faab2012-01-25 07:21:38 +00001147 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1148 static const char *const PPCTriples[] = {
1149 "powerpc-linux-gnu",
1150 "powerpc-unknown-linux-gnu",
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00001151 "powerpc-linux-gnuspe",
Gabor Greif93047632012-05-15 11:21:03 +00001152 "powerpc-suse-linux",
1153 "powerpc-montavista-linuxspe"
Chandler Carruth866faab2012-01-25 07:21:38 +00001154 };
1155 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1156 static const char *const PPC64Triples[] = {
Chandler Carruthaf3c2092012-02-26 09:03:21 +00001157 "powerpc64-linux-gnu",
Chandler Carruth866faab2012-01-25 07:21:38 +00001158 "powerpc64-unknown-linux-gnu",
1159 "powerpc64-suse-linux",
1160 "ppc64-redhat-linux"
1161 };
1162
1163 switch (TargetTriple.getArch()) {
Tim Northover9bb857a2013-01-31 12:13:10 +00001164 case llvm::Triple::aarch64:
Eric Christopherc4b0be92013-02-05 07:29:49 +00001165 LibDirs.append(AArch64LibDirs, AArch64LibDirs
Tim Northover9bb857a2013-01-31 12:13:10 +00001166 + llvm::array_lengthof(AArch64LibDirs));
1167 TripleAliases.append(
1168 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1169 MultiarchLibDirs.append(
1170 AArch64LibDirs, AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1171 MultiarchTripleAliases.append(
1172 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1173 break;
Chandler Carruth866faab2012-01-25 07:21:38 +00001174 case llvm::Triple::arm:
1175 case llvm::Triple::thumb:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001176 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001177 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1178 TripleAliases.append(
1179 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1180 } else {
1181 TripleAliases.append(
1182 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1183 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001184 break;
1185 case llvm::Triple::x86_64:
1186 LibDirs.append(
1187 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1188 TripleAliases.append(
1189 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1190 MultiarchLibDirs.append(
1191 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1192 MultiarchTripleAliases.append(
1193 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1194 break;
1195 case llvm::Triple::x86:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001196 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth866faab2012-01-25 07:21:38 +00001197 TripleAliases.append(
1198 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1199 MultiarchLibDirs.append(
1200 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1201 MultiarchTripleAliases.append(
1202 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1203 break;
1204 case llvm::Triple::mips:
1205 LibDirs.append(
1206 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1207 TripleAliases.append(
1208 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001209 MultiarchLibDirs.append(
1210 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1211 MultiarchTripleAliases.append(
1212 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001213 break;
1214 case llvm::Triple::mipsel:
1215 LibDirs.append(
1216 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1217 TripleAliases.append(
1218 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001219 MultiarchLibDirs.append(
1220 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1221 MultiarchTripleAliases.append(
1222 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1223 break;
1224 case llvm::Triple::mips64:
1225 LibDirs.append(
1226 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1227 TripleAliases.append(
1228 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1229 MultiarchLibDirs.append(
1230 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1231 MultiarchTripleAliases.append(
1232 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1233 break;
1234 case llvm::Triple::mips64el:
1235 LibDirs.append(
1236 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1237 TripleAliases.append(
1238 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1239 MultiarchLibDirs.append(
1240 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1241 MultiarchTripleAliases.append(
1242 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth866faab2012-01-25 07:21:38 +00001243 break;
1244 case llvm::Triple::ppc:
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001245 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth866faab2012-01-25 07:21:38 +00001246 TripleAliases.append(
1247 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1248 MultiarchLibDirs.append(
1249 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1250 MultiarchTripleAliases.append(
1251 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1252 break;
1253 case llvm::Triple::ppc64:
1254 LibDirs.append(
1255 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1256 TripleAliases.append(
1257 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1258 MultiarchLibDirs.append(
1259 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1260 MultiarchTripleAliases.append(
1261 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1262 break;
1263
1264 default:
1265 // By default, just rely on the standard lib directories and the original
1266 // triple.
1267 break;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001268 }
Chandler Carruth866faab2012-01-25 07:21:38 +00001269
1270 // Always append the drivers target triple to the end, in case it doesn't
1271 // match any of our aliases.
1272 TripleAliases.push_back(TargetTriple.str());
1273
1274 // Also include the multiarch variant if it's different.
1275 if (TargetTriple.str() != MultiarchTriple.str())
1276 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001277}
1278
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001279// FIXME: There is the same routine in the Tools.cpp.
1280static bool hasMipsN32ABIArg(const ArgList &Args) {
1281 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smithbd55daf2012-11-01 04:30:05 +00001282 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001283}
1284
1285static StringRef getTargetMultiarchSuffix(llvm::Triple::ArchType TargetArch,
1286 const ArgList &Args) {
1287 if (TargetArch == llvm::Triple::x86_64 ||
1288 TargetArch == llvm::Triple::ppc64)
1289 return "/64";
1290
1291 if (TargetArch == llvm::Triple::mips64 ||
1292 TargetArch == llvm::Triple::mips64el) {
1293 if (hasMipsN32ABIArg(Args))
1294 return "/n32";
1295 else
1296 return "/64";
1297 }
1298
1299 return "/32";
1300}
1301
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001302void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001303 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1304 const std::string &LibDir,
Chandler Carruth866faab2012-01-25 07:21:38 +00001305 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001306 // There are various different suffixes involving the triple we
1307 // check for. We also record what is necessary to walk from each back
1308 // up to the lib directory.
Chandler Carruth866faab2012-01-25 07:21:38 +00001309 const std::string LibSuffixes[] = {
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001310 "/gcc/" + CandidateTriple.str(),
1311 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1312
Hal Finkelf3587912012-09-18 22:25:07 +00001313 // The Freescale PPC SDK has the gcc libraries in
1314 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1315 "/" + CandidateTriple.str(),
1316
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001317 // Ubuntu has a strange mis-matched pair of triples that this happens to
1318 // match.
1319 // FIXME: It may be worthwhile to generalize this and look for a second
1320 // triple.
Chandler Carruth6e46ca22011-11-09 03:46:20 +00001321 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001322 };
1323 const std::string InstallSuffixes[] = {
1324 "/../../..",
1325 "/../../../..",
Hal Finkelf3587912012-09-18 22:25:07 +00001326 "/../..",
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001327 "/../../../.."
1328 };
1329 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth866faab2012-01-25 07:21:38 +00001330 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1331 (TargetArch != llvm::Triple::x86));
1332 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1333 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001334 llvm::error_code EC;
Chandler Carruth866faab2012-01-25 07:21:38 +00001335 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001336 !EC && LI != LE; LI = LI.increment(EC)) {
1337 StringRef VersionText = llvm::sys::path::filename(LI->path());
1338 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1339 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1340 if (CandidateVersion < MinVersion)
1341 continue;
1342 if (CandidateVersion <= Version)
1343 continue;
Hal Finkel221e11e2011-12-08 05:50:03 +00001344
1345 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth64cee062012-01-24 19:21:42 +00001346 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth866faab2012-01-25 07:21:38 +00001347 // libs in a subdirectory named 64. The simple logic we follow is that
1348 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1349 // we use that. If not, and if not a multiarch triple, we look for
1350 // crtbegin.o without the subdirectory.
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001351 StringRef MultiarchSuffix = getTargetMultiarchSuffix(TargetArch, Args);
Chandler Carruth866faab2012-01-25 07:21:38 +00001352 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1353 GCCMultiarchSuffix = MultiarchSuffix.str();
1354 } else {
1355 if (NeedsMultiarchSuffix ||
1356 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1357 continue;
1358 GCCMultiarchSuffix.clear();
1359 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001360
1361 Version = CandidateVersion;
Chandler Carruth4d9d7682012-01-24 19:28:29 +00001362 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001363 // FIXME: We hack together the directory name here instead of
1364 // using LI to ensure stable path separators across Windows and
1365 // Linux.
Chandler Carruth866faab2012-01-25 07:21:38 +00001366 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth64cee062012-01-24 19:21:42 +00001367 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001368 IsValid = true;
1369 }
1370 }
1371}
1372
Rafael Espindola1af7c212012-02-19 01:38:32 +00001373Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1374 const ArgList &Args)
1375 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbar88979912010-08-01 22:29:51 +00001376 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001377 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001378 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +00001379}
1380
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001381Generic_GCC::~Generic_GCC() {
1382 // Free tool implementations.
1383 for (llvm::DenseMap<unsigned, Tool*>::iterator
1384 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1385 delete it->second;
1386}
1387
Mike Stump11289f42009-09-09 15:08:12 +00001388Tool &Generic_GCC::SelectTool(const Compilation &C,
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001389 const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001390 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001391 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001392 Key = Action::AnalyzeJobClass;
1393 else
1394 Key = JA.getKind();
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001395
1396 Tool *&T = Tools[Key];
1397 if (!T) {
1398 switch (Key) {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +00001399 case Action::InputClass:
1400 case Action::BindArchClass:
David Blaikie83d382b2011-09-23 05:06:16 +00001401 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001402 case Action::PreprocessJobClass:
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001403 T = new tools::gcc::Preprocess(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001404 case Action::PrecompileJobClass:
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001405 T = new tools::gcc::Precompile(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001406 case Action::AnalyzeJobClass:
Ted Kremenekf7639e12012-03-06 20:06:33 +00001407 case Action::MigrateJobClass:
Bob Wilson0c79f3c2012-11-08 01:03:34 +00001408 T = new tools::Clang(*this); break;
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001409 case Action::CompileJobClass:
1410 T = new tools::gcc::Compile(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001411 case Action::AssembleJobClass:
1412 T = new tools::gcc::Assemble(*this); break;
1413 case Action::LinkJobClass:
1414 T = new tools::gcc::Link(*this); break;
Mike Stump11289f42009-09-09 15:08:12 +00001415
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +00001416 // This is a bit ungeneric, but the only platform using a driver
1417 // driver is Darwin.
1418 case Action::LipoJobClass:
1419 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar88299622010-06-04 18:28:36 +00001420 case Action::DsymutilJobClass:
1421 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopher551ef452011-08-23 17:56:55 +00001422 case Action::VerifyJobClass:
1423 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001424 }
1425 }
1426
1427 return *T;
1428}
1429
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001430bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001431 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001432}
1433
Chandler Carruth76a943b2012-11-19 03:52:03 +00001434bool Generic_GCC::isPICDefault() const {
1435 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001436}
1437
Chandler Carruth76a943b2012-11-19 03:52:03 +00001438bool Generic_GCC::isPICDefaultForced() const {
1439 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001440}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001441
Tony Linthicum76329bf2011-12-12 21:14:55 +00001442/// Hexagon Toolchain
1443
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001444std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1445
1446 // Locate the rest of the toolchain ...
1447 if (strlen(GCC_INSTALL_PREFIX))
1448 return std::string(GCC_INSTALL_PREFIX);
1449
1450 std::string InstallRelDir = InstalledDir + "/../../gnu";
1451 if (llvm::sys::fs::exists(InstallRelDir))
1452 return InstallRelDir;
1453
1454 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1455 if (llvm::sys::fs::exists(PrefixRelDir))
1456 return PrefixRelDir;
1457
1458 return InstallRelDir;
1459}
1460
Matthew Curtise689b052012-12-06 15:46:07 +00001461static void GetHexagonLibraryPaths(
1462 const ArgList &Args,
1463 const std::string Ver,
1464 const std::string MarchString,
1465 const std::string &InstalledDir,
1466 ToolChain::path_list *LibPaths)
1467{
1468 bool buildingLib = Args.hasArg(options::OPT_shared);
1469
1470 //----------------------------------------------------------------------------
1471 // -L Args
1472 //----------------------------------------------------------------------------
1473 for (arg_iterator
1474 it = Args.filtered_begin(options::OPT_L),
1475 ie = Args.filtered_end();
1476 it != ie;
1477 ++it) {
1478 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1479 LibPaths->push_back((*it)->getValue(i));
1480 }
1481
1482 //----------------------------------------------------------------------------
1483 // Other standard paths
1484 //----------------------------------------------------------------------------
1485 const std::string MarchSuffix = "/" + MarchString;
1486 const std::string G0Suffix = "/G0";
1487 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1488 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1489
1490 // lib/gcc/hexagon/...
1491 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1492 if (buildingLib) {
1493 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1494 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1495 }
1496 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1497 LibPaths->push_back(LibGCCHexagonDir + Ver);
1498
1499 // lib/gcc/...
1500 LibPaths->push_back(RootDir + "lib/gcc");
1501
1502 // hexagon/lib/...
1503 std::string HexagonLibDir = RootDir + "hexagon/lib";
1504 if (buildingLib) {
1505 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1506 LibPaths->push_back(HexagonLibDir + G0Suffix);
1507 }
1508 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1509 LibPaths->push_back(HexagonLibDir);
1510}
1511
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001512Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1513 const ArgList &Args)
1514 : Linux(D, Triple, Args) {
1515 const std::string InstalledDir(getDriver().getInstalledDir());
1516 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1517
1518 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1519 // program paths
1520 const std::string BinDir(GnuDir + "/bin");
1521 if (llvm::sys::fs::exists(BinDir))
1522 getProgramPaths().push_back(BinDir);
1523
1524 // Determine version of GCC libraries and headers to use.
1525 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1526 llvm::error_code ec;
1527 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1528 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1529 !ec && di != de; di = di.increment(ec)) {
1530 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1531 if (MaxVersion < cv)
1532 MaxVersion = cv;
1533 }
1534 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001535
1536 ToolChain::path_list *LibPaths= &getFilePaths();
1537
1538 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1539 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1540 // support 'linux' we'll need to fix this up
1541 LibPaths->clear();
1542
1543 GetHexagonLibraryPaths(
1544 Args,
1545 GetGCCLibAndIncVersion(),
1546 GetTargetCPU(Args),
1547 InstalledDir,
1548 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001549}
1550
1551Hexagon_TC::~Hexagon_TC() {
1552 // Free tool implementations.
1553 for (llvm::DenseMap<unsigned, Tool*>::iterator
1554 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1555 delete it->second;
1556}
1557
1558Tool &Hexagon_TC::SelectTool(const Compilation &C,
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001559 const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001560 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001561 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001562 Key = Action::AnalyzeJobClass;
1563 else
1564 Key = JA.getKind();
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001565
Tony Linthicum76329bf2011-12-12 21:14:55 +00001566 Tool *&T = Tools[Key];
1567 if (!T) {
1568 switch (Key) {
1569 case Action::InputClass:
1570 case Action::BindArchClass:
1571 assert(0 && "Invalid tool kind.");
1572 case Action::AnalyzeJobClass:
1573 T = new tools::Clang(*this); break;
1574 case Action::AssembleJobClass:
1575 T = new tools::hexagon::Assemble(*this); break;
1576 case Action::LinkJobClass:
1577 T = new tools::hexagon::Link(*this); break;
1578 default:
1579 assert(false && "Unsupported action for Hexagon target.");
1580 }
1581 }
1582
1583 return *T;
1584}
1585
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001586void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1587 ArgStringList &CC1Args) const {
1588 const Driver &D = getDriver();
1589
1590 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1591 DriverArgs.hasArg(options::OPT_nostdlibinc))
1592 return;
1593
1594 llvm::sys::Path InstallDir(D.InstalledDir);
1595 std::string Ver(GetGCCLibAndIncVersion());
1596 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1597 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1598 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1599 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1600 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001601}
1602
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001603void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1604 ArgStringList &CC1Args) const {
1605
1606 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1607 DriverArgs.hasArg(options::OPT_nostdincxx))
1608 return;
1609
1610 const Driver &D = getDriver();
1611 std::string Ver(GetGCCLibAndIncVersion());
1612 llvm::sys::Path IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
1613
1614 IncludeDir.appendComponent("hexagon/include/c++/");
1615 IncludeDir.appendComponent(Ver);
1616 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001617}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001618
Matthew Curtise689b052012-12-06 15:46:07 +00001619ToolChain::CXXStdlibType
1620Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1621 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1622 if (!A)
1623 return ToolChain::CST_Libstdcxx;
1624
1625 StringRef Value = A->getValue();
1626 if (Value != "libstdc++") {
1627 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1628 << A->getAsString(Args);
1629 }
1630
1631 return ToolChain::CST_Libstdcxx;
1632}
1633
1634static Arg *GetLastHexagonArchArg(const ArgList &Args)
Matthew Curtisf10a5952012-12-06 14:16:43 +00001635{
1636 Arg *A = NULL;
1637
1638 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1639 it != ie; ++it) {
1640 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
1641 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1642 A = *it;
1643 A->claim();
1644 } else if ((*it)->getOption().matches(options::OPT_m_Joined)) {
1645 StringRef Value = (*it)->getValue(0);
1646 if (Value.startswith("v")) {
1647 A = *it;
1648 A->claim();
1649 }
1650 }
1651 }
1652 return A;
1653}
1654
1655StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1656{
1657 // Select the default CPU (v4) if none was given or detection failed.
1658 Arg *A = GetLastHexagonArchArg (Args);
1659 if (A) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001660 StringRef WhichHexagon = A->getValue();
Matthew Curtisf10a5952012-12-06 14:16:43 +00001661 if (WhichHexagon.startswith("hexagon"))
1662 return WhichHexagon.substr(sizeof("hexagon") - 1);
1663 if (WhichHexagon != "")
1664 return WhichHexagon;
1665 }
1666
1667 return "v4";
1668}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001669// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001670
Chris Lattner09797542010-03-04 21:07:38 +00001671/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1672/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1673/// Currently does not support anything else but compilation.
1674
Chandler Carruthd7fa2e02012-01-31 02:21:20 +00001675TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1676 : ToolChain(D, Triple) {
Chris Lattner09797542010-03-04 21:07:38 +00001677 // Path mangling to find libexec
1678 std::string Path(getDriver().Dir);
1679
1680 Path += "/../libexec";
1681 getProgramPaths().push_back(Path);
1682}
1683
1684TCEToolChain::~TCEToolChain() {
1685 for (llvm::DenseMap<unsigned, Tool*>::iterator
1686 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1687 delete it->second;
1688}
1689
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001690bool TCEToolChain::IsMathErrnoDefault() const {
1691 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001692}
1693
Chandler Carruth76a943b2012-11-19 03:52:03 +00001694bool TCEToolChain::isPICDefault() const {
1695 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001696}
1697
Chandler Carruth76a943b2012-11-19 03:52:03 +00001698bool TCEToolChain::isPICDefaultForced() const {
1699 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001700}
1701
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001702Tool &TCEToolChain::SelectTool(const Compilation &C,
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001703 const JobAction &JA) const {
Chris Lattner09797542010-03-04 21:07:38 +00001704 Action::ActionClass Key;
1705 Key = Action::AnalyzeJobClass;
1706
1707 Tool *&T = Tools[Key];
1708 if (!T) {
1709 switch (Key) {
1710 case Action::PreprocessJobClass:
1711 T = new tools::gcc::Preprocess(*this); break;
1712 case Action::AnalyzeJobClass:
1713 T = new tools::Clang(*this); break;
1714 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001715 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner09797542010-03-04 21:07:38 +00001716 }
1717 }
1718 return *T;
1719}
1720
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001721/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1722
Rafael Espindola1af7c212012-02-19 01:38:32 +00001723OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1724 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001725 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001726 getFilePaths().push_back("/usr/lib");
1727}
1728
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001729Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001730 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001731 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001732 Key = Action::AnalyzeJobClass;
1733 else
1734 Key = JA.getKind();
1735
Rafael Espindola96aef792010-11-07 23:13:01 +00001736 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1737 options::OPT_no_integrated_as,
1738 IsIntegratedAssemblerDefault());
1739
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001740 Tool *&T = Tools[Key];
1741 if (!T) {
1742 switch (Key) {
Rafael Espindola96aef792010-11-07 23:13:01 +00001743 case Action::AssembleJobClass: {
1744 if (UseIntegratedAs)
1745 T = new tools::ClangAs(*this);
1746 else
1747 T = new tools::openbsd::Assemble(*this);
1748 break;
1749 }
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001750 case Action::LinkJobClass:
1751 T = new tools::openbsd::Link(*this); break;
1752 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001753 T = &Generic_GCC::SelectTool(C, JA);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001754 }
1755 }
1756
1757 return *T;
1758}
1759
Eli Friedman9fa28852012-08-08 23:57:20 +00001760/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1761
1762Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1763 : Generic_ELF(D, Triple, Args) {
1764 getFilePaths().push_back(getDriver().Dir + "/../lib");
1765 getFilePaths().push_back("/usr/lib");
1766}
1767
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001768Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001769 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001770 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001771 Key = Action::AnalyzeJobClass;
1772 else
1773 Key = JA.getKind();
1774
Eli Friedman9fa28852012-08-08 23:57:20 +00001775 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1776 options::OPT_no_integrated_as,
1777 IsIntegratedAssemblerDefault());
1778
1779 Tool *&T = Tools[Key];
1780 if (!T) {
1781 switch (Key) {
1782 case Action::AssembleJobClass: {
1783 if (UseIntegratedAs)
1784 T = new tools::ClangAs(*this);
1785 else
1786 T = new tools::bitrig::Assemble(*this);
1787 break;
1788 }
1789 case Action::LinkJobClass:
1790 T = new tools::bitrig::Link(*this); break;
1791 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001792 T = &Generic_GCC::SelectTool(C, JA);
Eli Friedman9fa28852012-08-08 23:57:20 +00001793 }
1794 }
1795
1796 return *T;
1797}
1798
1799void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1800 ArgStringList &CC1Args) const {
1801 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1802 DriverArgs.hasArg(options::OPT_nostdincxx))
1803 return;
1804
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001805 switch (GetCXXStdlibType(DriverArgs)) {
1806 case ToolChain::CST_Libcxx:
1807 addSystemInclude(DriverArgs, CC1Args,
1808 getDriver().SysRoot + "/usr/include/c++/");
1809 break;
1810 case ToolChain::CST_Libstdcxx:
1811 addSystemInclude(DriverArgs, CC1Args,
1812 getDriver().SysRoot + "/usr/include/c++/stdc++");
1813 addSystemInclude(DriverArgs, CC1Args,
1814 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001815
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001816 StringRef Triple = getTriple().str();
1817 if (Triple.startswith("amd64"))
1818 addSystemInclude(DriverArgs, CC1Args,
1819 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1820 Triple.substr(5));
1821 else
1822 addSystemInclude(DriverArgs, CC1Args,
1823 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1824 Triple);
1825 break;
1826 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001827}
1828
1829void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1830 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001831 switch (GetCXXStdlibType(Args)) {
1832 case ToolChain::CST_Libcxx:
1833 CmdArgs.push_back("-lc++");
1834 CmdArgs.push_back("-lcxxrt");
1835 // Include supc++ to provide Unwind until provided by libcxx.
1836 CmdArgs.push_back("-lgcc");
1837 break;
1838 case ToolChain::CST_Libstdcxx:
1839 CmdArgs.push_back("-lstdc++");
1840 break;
1841 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001842}
1843
Daniel Dunbare24297c2009-03-30 21:06:03 +00001844/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1845
Rafael Espindola1af7c212012-02-19 01:38:32 +00001846FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1847 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001848
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001849 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1850 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001851 if ((Triple.getArch() == llvm::Triple::x86 ||
1852 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001853 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001854 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1855 else
1856 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001857}
1858
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001859Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001860 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001861 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001862 Key = Action::AnalyzeJobClass;
1863 else
1864 Key = JA.getKind();
1865
Roman Divacky137426a2010-11-08 17:46:39 +00001866 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1867 options::OPT_no_integrated_as,
1868 IsIntegratedAssemblerDefault());
1869
Daniel Dunbare24297c2009-03-30 21:06:03 +00001870 Tool *&T = Tools[Key];
1871 if (!T) {
1872 switch (Key) {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00001873 case Action::AssembleJobClass:
Roman Divacky137426a2010-11-08 17:46:39 +00001874 if (UseIntegratedAs)
1875 T = new tools::ClangAs(*this);
1876 else
1877 T = new tools::freebsd::Assemble(*this);
Roman Divackyb45d2672010-11-08 19:39:10 +00001878 break;
Daniel Dunbard854c8d2009-04-01 19:36:32 +00001879 case Action::LinkJobClass:
1880 T = new tools::freebsd::Link(*this); break;
Daniel Dunbare24297c2009-03-30 21:06:03 +00001881 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001882 T = &Generic_GCC::SelectTool(C, JA);
Daniel Dunbare24297c2009-03-30 21:06:03 +00001883 }
1884 }
1885
1886 return *T;
1887}
Daniel Dunbarcc912342009-05-02 18:28:39 +00001888
Rafael Espindola0f207ed2012-12-13 04:17:14 +00001889bool FreeBSD::UseSjLjExceptions() const {
1890 // FreeBSD uses SjLj exceptions on ARM oabi.
1891 switch (getTriple().getEnvironment()) {
1892 case llvm::Triple::GNUEABI:
1893 case llvm::Triple::EABI:
1894 return false;
1895
1896 default:
1897 return (getTriple().getArch() == llvm::Triple::arm ||
1898 getTriple().getArch() == llvm::Triple::thumb);
1899 }
1900}
1901
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001902/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1903
Rafael Espindola1af7c212012-02-19 01:38:32 +00001904NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1905 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001906
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001907 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001908 // When targeting a 32-bit platform, try the special directory used on
1909 // 64-bit hosts, and only fall back to the main library directory if that
1910 // doesn't work.
1911 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1912 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00001913 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001914 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001915
1916 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001917 }
1918}
1919
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001920Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001921 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001922 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001923 Key = Action::AnalyzeJobClass;
1924 else
1925 Key = JA.getKind();
1926
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001927 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1928 options::OPT_no_integrated_as,
1929 IsIntegratedAssemblerDefault());
1930
1931 Tool *&T = Tools[Key];
1932 if (!T) {
1933 switch (Key) {
1934 case Action::AssembleJobClass:
1935 if (UseIntegratedAs)
1936 T = new tools::ClangAs(*this);
1937 else
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00001938 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001939 break;
1940 case Action::LinkJobClass:
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00001941 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00001942 break;
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001943 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001944 T = &Generic_GCC::SelectTool(C, JA);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001945 }
1946 }
1947
1948 return *T;
1949}
1950
Chris Lattner3e2ee142010-07-07 16:01:42 +00001951/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1952
Rafael Espindola1af7c212012-02-19 01:38:32 +00001953Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1954 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00001955 getFilePaths().push_back(getDriver().Dir + "/../lib");
1956 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00001957}
1958
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001959Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001960 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00001961 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001962 Key = Action::AnalyzeJobClass;
1963 else
1964 Key = JA.getKind();
Chris Lattner3e2ee142010-07-07 16:01:42 +00001965
1966 Tool *&T = Tools[Key];
1967 if (!T) {
1968 switch (Key) {
1969 case Action::AssembleJobClass:
1970 T = new tools::minix::Assemble(*this); break;
1971 case Action::LinkJobClass:
1972 T = new tools::minix::Link(*this); break;
1973 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001974 T = &Generic_GCC::SelectTool(C, JA);
Chris Lattner3e2ee142010-07-07 16:01:42 +00001975 }
1976 }
1977
1978 return *T;
1979}
1980
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001981/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1982
Rafael Espindola1af7c212012-02-19 01:38:32 +00001983AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1984 const ArgList &Args)
1985 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001986
Daniel Dunbar88979912010-08-01 22:29:51 +00001987 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001988 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001989 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001990
Daniel Dunbar083edf72009-12-21 18:54:17 +00001991 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001992 getFilePaths().push_back("/usr/lib");
1993 getFilePaths().push_back("/usr/sfw/lib");
1994 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00001995 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001996
1997}
1998
Rafael Espindola88b55ea2013-03-18 17:25:58 +00001999Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002000 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00002001 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002002 Key = Action::AnalyzeJobClass;
2003 else
2004 Key = JA.getKind();
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002005
2006 Tool *&T = Tools[Key];
2007 if (!T) {
2008 switch (Key) {
2009 case Action::AssembleJobClass:
2010 T = new tools::auroraux::Assemble(*this); break;
2011 case Action::LinkJobClass:
2012 T = new tools::auroraux::Link(*this); break;
2013 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002014 T = &Generic_GCC::SelectTool(C, JA);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002015 }
2016 }
2017
2018 return *T;
2019}
2020
David Chisnallf571cde2012-02-15 13:39:01 +00002021/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2022
Rafael Espindola1af7c212012-02-19 01:38:32 +00002023Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2024 const ArgList &Args)
2025 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00002026
2027 getProgramPaths().push_back(getDriver().getInstalledDir());
2028 if (getDriver().getInstalledDir() != getDriver().Dir)
2029 getProgramPaths().push_back(getDriver().Dir);
2030
2031 getFilePaths().push_back(getDriver().Dir + "/../lib");
2032 getFilePaths().push_back("/usr/lib");
2033}
2034
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002035Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002036 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00002037 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002038 Key = Action::AnalyzeJobClass;
2039 else
2040 Key = JA.getKind();
David Chisnallf571cde2012-02-15 13:39:01 +00002041
2042 Tool *&T = Tools[Key];
2043 if (!T) {
2044 switch (Key) {
2045 case Action::AssembleJobClass:
2046 T = new tools::solaris::Assemble(*this); break;
2047 case Action::LinkJobClass:
2048 T = new tools::solaris::Link(*this); break;
2049 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002050 T = &Generic_GCC::SelectTool(C, JA);
David Chisnallf571cde2012-02-15 13:39:01 +00002051 }
2052 }
2053
2054 return *T;
2055}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002056
Eli Friedman5cd659f2009-05-26 07:52:18 +00002057/// Linux toolchain (very bare-bones at the moment).
2058
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002059enum LinuxDistro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002060 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002061 DebianLenny,
2062 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00002063 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002064 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00002065 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00002066 RHEL4,
2067 RHEL5,
2068 RHEL6,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002069 Fedora13,
2070 Fedora14,
Eric Christopher534b6a02011-04-06 18:22:53 +00002071 Fedora15,
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002072 Fedora16,
Eric Christopher534b6a02011-04-06 18:22:53 +00002073 FedoraRawhide,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002074 OpenSuse11_3,
David Chisnallb8f65e22011-05-19 13:26:33 +00002075 OpenSuse11_4,
2076 OpenSuse12_1,
Douglas Gregorf7b88412012-04-30 23:42:57 +00002077 OpenSuse12_2,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00002078 UbuntuHardy,
2079 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00002080 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00002081 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002082 UbuntuLucid,
2083 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00002084 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00002085 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002086 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00002087 UbuntuQuantal,
2088 UbuntuRaring,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002089 UnknownDistro
2090};
2091
Chris Lattner84e38552011-05-22 05:36:06 +00002092static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002093 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
2094 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002095}
2096
2097static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregorf7b88412012-04-30 23:42:57 +00002098 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002099}
2100
2101static bool IsDebian(enum LinuxDistro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002102 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002103}
2104
2105static bool IsUbuntu(enum LinuxDistro Distro) {
Rafael Espindola62e87702012-12-13 20:26:05 +00002106 return Distro >= UbuntuHardy && Distro <= UbuntuRaring;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002107}
2108
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002109static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00002110 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002111 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002112 StringRef Data = File.get()->getBuffer();
2113 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002114 Data.split(Lines, "\n");
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002115 LinuxDistro Version = UnknownDistro;
2116 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2117 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
2118 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
2119 .Case("hardy", UbuntuHardy)
2120 .Case("intrepid", UbuntuIntrepid)
2121 .Case("jaunty", UbuntuJaunty)
2122 .Case("karmic", UbuntuKarmic)
2123 .Case("lucid", UbuntuLucid)
2124 .Case("maverick", UbuntuMaverick)
2125 .Case("natty", UbuntuNatty)
2126 .Case("oneiric", UbuntuOneiric)
2127 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002128 .Case("quantal", UbuntuQuantal)
2129 .Case("raring", UbuntuRaring)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002130 .Default(UnknownDistro);
2131 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002132 }
2133
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002134 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002135 StringRef Data = File.get()->getBuffer();
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002136 if (Data.startswith("Fedora release 16"))
2137 return Fedora16;
2138 else if (Data.startswith("Fedora release 15"))
Eric Christopher534b6a02011-04-06 18:22:53 +00002139 return Fedora15;
2140 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002141 return Fedora14;
Eric Christopher534b6a02011-04-06 18:22:53 +00002142 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002143 return Fedora13;
Eric Christopher534b6a02011-04-06 18:22:53 +00002144 else if (Data.startswith("Fedora release") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002145 Data.find("Rawhide") != StringRef::npos)
Eric Christopher534b6a02011-04-06 18:22:53 +00002146 return FedoraRawhide;
Chris Lattner84e38552011-05-22 05:36:06 +00002147 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002148 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002149 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002150 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002151 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002152 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002153 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002154 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002155 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002156 Data.find("release 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002157 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002158 return UnknownDistro;
2159 }
2160
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002161 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002162 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002163 if (Data[0] == '5')
2164 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002165 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002166 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002167 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002168 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002169 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2170 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002171 return UnknownDistro;
2172 }
2173
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002174 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
2175 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
2176 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2177 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2178 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregorf7b88412012-04-30 23:42:57 +00002179 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002180 .Default(UnknownDistro);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002181
Michael J. Spencerf6efe582011-01-10 02:34:13 +00002182 bool Exists;
2183 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola12479842010-11-11 02:07:13 +00002184 return Exherbo;
2185
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002186 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2187 return ArchLinux;
2188
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002189 return UnknownDistro;
2190}
2191
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002192/// \brief Get our best guess at the multiarch triple for a target.
2193///
2194/// Debian-based systems are starting to use a multiarch setup where they use
2195/// a target-triple directory in the library and header search paths.
2196/// Unfortunately, this triple does not align with the vanilla target triple,
2197/// so we provide a rough mapping here.
2198static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2199 StringRef SysRoot) {
2200 // For most architectures, just use whatever we have rather than trying to be
2201 // clever.
2202 switch (TargetTriple.getArch()) {
2203 default:
2204 return TargetTriple.str();
2205
2206 // We use the existence of '/lib/<triple>' as a directory to detect some
2207 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002208 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2209 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002210 case llvm::Triple::arm:
2211 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002212 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2213 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2214 return "arm-linux-gnueabihf";
2215 } else {
2216 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2217 return "arm-linux-gnueabi";
2218 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002219 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002220 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002221 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2222 return "i386-linux-gnu";
2223 return TargetTriple.str();
2224 case llvm::Triple::x86_64:
2225 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2226 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002227 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002228 case llvm::Triple::aarch64:
2229 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2230 return "aarch64-linux-gnu";
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002231 case llvm::Triple::mips:
2232 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2233 return "mips-linux-gnu";
2234 return TargetTriple.str();
2235 case llvm::Triple::mipsel:
2236 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2237 return "mipsel-linux-gnu";
2238 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002239 case llvm::Triple::ppc:
Sylvestre Ledrue0bf5812013-03-15 16:22:43 +00002240 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2241 return "powerpc-linux-gnuspe";
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002242 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2243 return "powerpc-linux-gnu";
2244 return TargetTriple.str();
2245 case llvm::Triple::ppc64:
2246 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2247 return "powerpc64-linux-gnu";
2248 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002249 }
2250}
2251
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002252static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2253 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2254}
2255
Simon Atanasyand4413882012-09-14 11:27:24 +00002256static bool isMipsArch(llvm::Triple::ArchType Arch) {
2257 return Arch == llvm::Triple::mips ||
2258 Arch == llvm::Triple::mipsel ||
2259 Arch == llvm::Triple::mips64 ||
2260 Arch == llvm::Triple::mips64el;
2261}
2262
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002263static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2264 const ArgList &Args) {
2265 if (Arch != llvm::Triple::mips &&
2266 Arch != llvm::Triple::mipsel)
2267 return false;
2268
2269 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2270 options::OPT_mcpu_EQ,
2271 options::OPT_mips_CPUs_Group);
2272
2273 if (!A)
2274 return false;
2275
2276 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2277 return A->getOption().matches(options::OPT_mips32r2);
2278
Richard Smithbd55daf2012-11-01 04:30:05 +00002279 return A->getValue() == StringRef("mips32r2");
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002280}
2281
Simon Atanasyand4413882012-09-14 11:27:24 +00002282static StringRef getMultilibDir(const llvm::Triple &Triple,
2283 const ArgList &Args) {
2284 if (!isMipsArch(Triple.getArch()))
2285 return Triple.isArch32Bit() ? "lib32" : "lib64";
2286
2287 // lib32 directory has a special meaning on MIPS targets.
2288 // It contains N32 ABI binaries. Use this folder if produce
2289 // code for N32 ABI only.
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00002290 if (hasMipsN32ABIArg(Args))
Simon Atanasyand4413882012-09-14 11:27:24 +00002291 return "lib32";
2292
2293 return Triple.isArch32Bit() ? "lib" : "lib64";
2294}
2295
Rafael Espindola1af7c212012-02-19 01:38:32 +00002296Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2297 : Generic_ELF(D, Triple, Args) {
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002298 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruth2a649c72011-10-03 06:41:08 +00002299 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002300
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002301 // OpenSuse stores the linker with the compiler, add that to the search
2302 // path.
2303 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002304 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002305 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002306
2307 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002308
2309 LinuxDistro Distro = DetectLinuxDistro(Arch);
2310
Chris Lattnerd075c822011-05-22 16:45:07 +00002311 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002312 ExtraOpts.push_back("-z");
2313 ExtraOpts.push_back("relro");
2314 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002315
Douglas Gregord9bb1522011-03-06 19:11:49 +00002316 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002317 ExtraOpts.push_back("-X");
2318
Logan Chienc6fd8202012-09-02 09:30:11 +00002319 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002320
Chandler Carruth0b842912011-12-09 04:45:18 +00002321 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2322 // and the MIPS ABI require .dynsym to be sorted in different ways.
2323 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2324 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002325 // Android loader does not support .gnu.hash.
Simon Atanasyand4413882012-09-14 11:27:24 +00002326 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002327 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2328 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002329 ExtraOpts.push_back("--hash-style=gnu");
2330
2331 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2332 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2333 ExtraOpts.push_back("--hash-style=both");
2334 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002335
Chris Lattner84e38552011-05-22 05:36:06 +00002336 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002337 ExtraOpts.push_back("--no-add-needed");
2338
Eli Friedmanf7600942011-06-02 21:36:53 +00002339 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002340 Distro == DebianJessie || IsOpenSuse(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002341 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002342 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002343 ExtraOpts.push_back("--build-id");
2344
Chris Lattnerd075c822011-05-22 16:45:07 +00002345 if (IsOpenSuse(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002346 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002347
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002348 // The selection of paths to try here is designed to match the patterns which
2349 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2350 // This was determined by running GCC in a fake filesystem, creating all
2351 // possible permutations of these directories, and seeing which ones it added
2352 // to the link paths.
2353 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002354
Simon Atanasyand4413882012-09-14 11:27:24 +00002355 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002356 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002357
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002358 // Add the multilib suffixed paths where they are available.
2359 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002360 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002361 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002362
2363 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2364 addPathIfExists(GCCInstallation.getInstallPath() +
2365 GCCInstallation.getMultiarchSuffix() +
2366 "/mips-r2",
2367 Paths);
2368 else
2369 addPathIfExists((GCCInstallation.getInstallPath() +
2370 GCCInstallation.getMultiarchSuffix()),
2371 Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002372
2373 // If the GCC installation we found is inside of the sysroot, we want to
2374 // prefer libraries installed in the parent prefix of the GCC installation.
2375 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002376 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002377 // This usually happens when there is an external cross compiler on the
2378 // host system, and a more minimal sysroot available that is the target of
2379 // the cross.
2380 if (StringRef(LibPath).startswith(SysRoot)) {
2381 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2382 Paths);
2383 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2384 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2385 }
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002386 // On Android, libraries in the parent prefix of the GCC installation are
2387 // preferred to the ones under sysroot.
2388 if (IsAndroid) {
2389 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2390 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002391 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002392 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2393 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2394 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2395 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2396
2397 // Try walking via the GCC triple path in case of multiarch GCC
2398 // installations with strange symlinks.
2399 if (GCCInstallation.isValid())
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002400 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002401 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002402
Chandler Carrutha7b44142011-10-16 10:54:30 +00002403 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002404 if (GCCInstallation.isValid()) {
2405 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002406 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth866faab2012-01-25 07:21:38 +00002407 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002408 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002409
2410 if (StringRef(LibPath).startswith(SysRoot)) {
2411 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2412 addPathIfExists(LibPath, Paths);
2413 }
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002414 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002415 addPathIfExists(SysRoot + "/lib", Paths);
2416 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002417}
2418
2419bool Linux::HasNativeLLVMSupport() const {
2420 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002421}
2422
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002423Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002424 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00002425 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002426 Key = Action::AnalyzeJobClass;
2427 else
2428 Key = JA.getKind();
2429
Rafael Espindola96aef792010-11-07 23:13:01 +00002430 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2431 options::OPT_no_integrated_as,
2432 IsIntegratedAssemblerDefault());
2433
Rafael Espindola92b00932010-08-10 00:25:48 +00002434 Tool *&T = Tools[Key];
2435 if (!T) {
2436 switch (Key) {
2437 case Action::AssembleJobClass:
Rafael Espindola96aef792010-11-07 23:13:01 +00002438 if (UseIntegratedAs)
2439 T = new tools::ClangAs(*this);
2440 else
2441 T = new tools::linuxtools::Assemble(*this);
2442 break;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002443 case Action::LinkJobClass:
2444 T = new tools::linuxtools::Link(*this); break;
Rafael Espindola92b00932010-08-10 00:25:48 +00002445 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002446 T = &Generic_GCC::SelectTool(C, JA);
Rafael Espindola92b00932010-08-10 00:25:48 +00002447 }
2448 }
2449
2450 return *T;
2451}
2452
Chandler Carruth05fb5852012-11-21 23:40:23 +00002453void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2454 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002455 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carruth05fb5852012-11-21 23:40:23 +00002456 bool UseInitArrayDefault
2457 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002458 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002459 getTriple().getEnvironment() == llvm::Triple::Android;
2460 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2461 options::OPT_fno_use_init_array,
2462 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002463 CC1Args.push_back("-fuse-init-array");
2464}
2465
Chandler Carrutha796f532011-11-05 20:17:13 +00002466void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2467 ArgStringList &CC1Args) const {
2468 const Driver &D = getDriver();
2469
2470 if (DriverArgs.hasArg(options::OPT_nostdinc))
2471 return;
2472
2473 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2474 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2475
2476 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carrutha796f532011-11-05 20:17:13 +00002477 llvm::sys::Path P(D.ResourceDir);
2478 P.appendComponent("include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002479 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002480 }
2481
2482 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2483 return;
2484
2485 // Check for configure-time C include directories.
2486 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2487 if (CIncludeDirs != "") {
2488 SmallVector<StringRef, 5> dirs;
2489 CIncludeDirs.split(dirs, ":");
2490 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2491 I != E; ++I) {
2492 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2493 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2494 }
2495 return;
2496 }
2497
2498 // Lacking those, try to detect the correct set of system includes for the
2499 // target triple.
2500
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002501 // Implement generic Debian multiarch support.
2502 const StringRef X86_64MultiarchIncludeDirs[] = {
2503 "/usr/include/x86_64-linux-gnu",
2504
2505 // FIXME: These are older forms of multiarch. It's not clear that they're
2506 // in use in any released version of Debian, so we should consider
2507 // removing them.
2508 "/usr/include/i686-linux-gnu/64",
2509 "/usr/include/i486-linux-gnu/64"
2510 };
2511 const StringRef X86MultiarchIncludeDirs[] = {
2512 "/usr/include/i386-linux-gnu",
2513
2514 // FIXME: These are older forms of multiarch. It's not clear that they're
2515 // in use in any released version of Debian, so we should consider
2516 // removing them.
2517 "/usr/include/x86_64-linux-gnu/32",
2518 "/usr/include/i686-linux-gnu",
2519 "/usr/include/i486-linux-gnu"
2520 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002521 const StringRef AArch64MultiarchIncludeDirs[] = {
2522 "/usr/include/aarch64-linux-gnu"
2523 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002524 const StringRef ARMMultiarchIncludeDirs[] = {
2525 "/usr/include/arm-linux-gnueabi"
2526 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002527 const StringRef ARMHFMultiarchIncludeDirs[] = {
2528 "/usr/include/arm-linux-gnueabihf"
2529 };
Eli Friedman7771c832011-11-11 03:05:19 +00002530 const StringRef MIPSMultiarchIncludeDirs[] = {
2531 "/usr/include/mips-linux-gnu"
2532 };
2533 const StringRef MIPSELMultiarchIncludeDirs[] = {
2534 "/usr/include/mipsel-linux-gnu"
2535 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002536 const StringRef PPCMultiarchIncludeDirs[] = {
2537 "/usr/include/powerpc-linux-gnu"
2538 };
2539 const StringRef PPC64MultiarchIncludeDirs[] = {
2540 "/usr/include/powerpc64-linux-gnu"
2541 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002542 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002543 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002544 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002545 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002546 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002547 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2548 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002549 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002550 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2551 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2552 else
2553 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002554 } else if (getTriple().getArch() == llvm::Triple::mips) {
2555 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2556 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2557 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002558 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2559 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2560 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2561 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002562 }
2563 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2564 E = MultiarchIncludeDirs.end();
2565 I != E; ++I) {
Chandler Carruth6e46ca22011-11-09 03:46:20 +00002566 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002567 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2568 break;
2569 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002570 }
2571
2572 if (getTriple().getOS() == llvm::Triple::RTEMS)
2573 return;
2574
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002575 // Add an include of '/include' directly. This isn't provided by default by
2576 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2577 // add even when Clang is acting as-if it were a system compiler.
2578 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2579
Chandler Carrutha796f532011-11-05 20:17:13 +00002580 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2581}
2582
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002583/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002584/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2585 const ArgList &DriverArgs,
2586 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002587 if (!llvm::sys::fs::exists(Base))
2588 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002589 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002590 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002591 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002592 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002593}
2594
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002595/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2596/// libstdc++ installation.
2597/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2598 Twine TargetArchDir,
2599 Twine MultiLibSuffix,
2600 const ArgList &DriverArgs,
2601 ArgStringList &CC1Args) {
2602 if (!addLibStdCXXIncludePaths(Base+Suffix, TargetArchDir + MultiLibSuffix,
2603 DriverArgs, CC1Args))
2604 return false;
2605
2606 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
2607 + MultiLibSuffix);
2608 return true;
2609}
2610
Chandler Carrutha796f532011-11-05 20:17:13 +00002611void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2612 ArgStringList &CC1Args) const {
2613 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2614 DriverArgs.hasArg(options::OPT_nostdincxx))
2615 return;
2616
Chandler Carruthf4701732011-11-07 09:01:17 +00002617 // Check if libc++ has been enabled and provide its include paths if so.
2618 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2619 // libc++ is always installed at a fixed path on Linux currently.
2620 addSystemInclude(DriverArgs, CC1Args,
2621 getDriver().SysRoot + "/usr/include/c++/v1");
2622 return;
2623 }
2624
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002625 // We need a detected GCC installation on Linux to provide libstdc++'s
2626 // headers. We handled the libc++ case above.
2627 if (!GCCInstallation.isValid())
2628 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002629
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002630 // By default, look for the C++ headers in an include directory adjacent to
2631 // the lib directory of the GCC installation. Note that this is expect to be
2632 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2633 StringRef LibDir = GCCInstallation.getParentLibPath();
2634 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola66aa0452012-06-19 01:26:10 +00002635 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002636 StringRef TripleStr = GCCInstallation.getTriple().str();
2637
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002638 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2639 "/c++/" + Version.str(),
2640 TripleStr,
2641 GCCInstallation.getMultiarchSuffix(),
2642 DriverArgs, CC1Args))
2643 return;
2644
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002645 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002646 // Gentoo is weird and places its headers inside the GCC install, so if the
2647 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002648 InstallDir.str() + "/include/g++-v4",
2649 // Android standalone toolchain has C++ headers in yet another place.
2650 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkelf3587912012-09-18 22:25:07 +00002651 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2652 // without a subdirectory corresponding to the gcc version.
2653 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002654 };
2655
2656 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2657 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2658 GCCInstallation.getMultiarchSuffix()),
2659 DriverArgs, CC1Args))
2660 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002661 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002662}
2663
Daniel Dunbarcc912342009-05-02 18:28:39 +00002664/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2665
Rafael Espindola1af7c212012-02-19 01:38:32 +00002666DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2667 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002668
2669 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002670 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002671 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002672 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002673
Daniel Dunbar083edf72009-12-21 18:54:17 +00002674 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002675 getFilePaths().push_back("/usr/lib");
2676 getFilePaths().push_back("/usr/lib/gcc41");
2677}
2678
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002679Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002680 Action::ActionClass Key;
Rafael Espindola2f69d402013-03-18 15:33:26 +00002681 if (getDriver().ShouldUseClangCompiler(JA))
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002682 Key = Action::AnalyzeJobClass;
2683 else
2684 Key = JA.getKind();
Daniel Dunbarcc912342009-05-02 18:28:39 +00002685
2686 Tool *&T = Tools[Key];
2687 if (!T) {
2688 switch (Key) {
2689 case Action::AssembleJobClass:
2690 T = new tools::dragonfly::Assemble(*this); break;
2691 case Action::LinkJobClass:
2692 T = new tools::dragonfly::Link(*this); break;
2693 default:
Rafael Espindola88b55ea2013-03-18 17:25:58 +00002694 T = &Generic_GCC::SelectTool(C, JA);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002695 }
2696 }
2697
2698 return *T;
2699}