blob: 035af219759d3a9a32f83ce112b466b38ecb5983 [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
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +0000179Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
180 const ActionList &Inputs) const {
Argyrios Kyrtzidis3169e802012-05-21 20:11:54 +0000181 Action::ActionClass Key = JA.getKind();
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +0000182
183 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
184 // FIXME: This seems like a hacky way to choose clang frontend.
185 Key = Action::AnalyzeJobClass;
186 }
187
Daniel Dunbar6232d342010-05-20 21:48:38 +0000188 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
189 options::OPT_no_integrated_as,
Bob Wilsond32fba22011-10-30 00:20:28 +0000190 IsIntegratedAssemblerDefault());
Daniel Dunbar6232d342010-05-20 21:48:38 +0000191
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000192 Tool *&T = Tools[Key];
193 if (!T) {
194 switch (Key) {
195 case Action::InputClass:
196 case Action::BindArchClass:
David Blaikie83d382b2011-09-23 05:06:16 +0000197 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000198 case Action::PreprocessJobClass:
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000199 case Action::AnalyzeJobClass:
Ted Kremenekf7639e12012-03-06 20:06:33 +0000200 case Action::MigrateJobClass:
Daniel Dunbarf64f5302009-03-29 22:27:40 +0000201 case Action::PrecompileJobClass:
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000202 case Action::CompileJobClass:
Bob Wilsondecc03e2012-11-23 06:14:39 +0000203 T = new tools::Clang(*this); break;
Daniel Dunbar6232d342010-05-20 21:48:38 +0000204 case Action::AssembleJobClass: {
205 if (UseIntegratedAs)
206 T = new tools::ClangAs(*this);
207 else
208 T = new tools::darwin::Assemble(*this);
209 break;
210 }
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000211 case Action::LinkJobClass:
Daniel Dunbar5095b292009-09-04 17:39:02 +0000212 T = new tools::darwin::Link(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000213 case Action::LipoJobClass:
214 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar88299622010-06-04 18:28:36 +0000215 case Action::DsymutilJobClass:
216 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopher551ef452011-08-23 17:56:55 +0000217 case Action::VerifyJobClass:
218 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000219 }
220 }
221
222 return *T;
223}
224
Daniel Dunbar26d482a2009-09-18 08:15:03 +0000225
Chandler Carruthd7fa2e02012-01-31 02:21:20 +0000226DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
227 : Darwin(D, Triple)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000228{
Daniel Dunbar00aff042010-09-17 08:22:12 +0000229 getProgramPaths().push_back(getDriver().getInstalledDir());
230 if (getDriver().getInstalledDir() != getDriver().Dir)
231 getProgramPaths().push_back(getDriver().Dir);
232
Daniel Dunbar6276f992009-09-18 08:15:13 +0000233 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbar88979912010-08-01 22:29:51 +0000234 getProgramPaths().push_back(getDriver().getInstalledDir());
235 if (getDriver().getInstalledDir() != getDriver().Dir)
236 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar6276f992009-09-18 08:15:13 +0000237}
238
John McCall31168b02011-06-15 23:02:42 +0000239void DarwinClang::AddLinkARCArgs(const ArgList &Args,
240 ArgStringList &CmdArgs) const {
Eric Christopher551ef452011-08-23 17:56:55 +0000241
242 CmdArgs.push_back("-force_load");
John McCall31168b02011-06-15 23:02:42 +0000243 llvm::sys::Path P(getDriver().ClangExecutable);
244 P.eraseComponent(); // 'clang'
245 P.eraseComponent(); // 'bin'
246 P.appendComponent("lib");
247 P.appendComponent("arc");
248 P.appendComponent("libarclite_");
249 std::string s = P.str();
250 // Mash in the platform.
Argyrios Kyrtzidis058b4512011-10-18 17:40:15 +0000251 if (isTargetIOSSimulator())
252 s += "iphonesimulator";
253 else if (isTargetIPhoneOS())
John McCall31168b02011-06-15 23:02:42 +0000254 s += "iphoneos";
John McCall31168b02011-06-15 23:02:42 +0000255 else
256 s += "macosx";
257 s += ".a";
258
259 CmdArgs.push_back(Args.MakeArgString(s));
260}
261
Eric Christopherc235d0c62011-06-22 17:41:40 +0000262void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopher551ef452011-08-23 17:56:55 +0000263 ArgStringList &CmdArgs,
Alexey Samsonov8368b372012-11-21 14:17:42 +0000264 const char *DarwinStaticLib,
265 bool AlwaysLink) const {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000266 llvm::sys::Path P(getDriver().ResourceDir);
267 P.appendComponent("lib");
268 P.appendComponent("darwin");
269 P.appendComponent(DarwinStaticLib);
Eric Christopher551ef452011-08-23 17:56:55 +0000270
Eric Christopherc235d0c62011-06-22 17:41:40 +0000271 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov8368b372012-11-21 14:17:42 +0000272 // not have compiler-rt checked out or integrated into their build (unless
273 // we explicitly force linking with this library).
Eric Christopherc235d0c62011-06-22 17:41:40 +0000274 bool Exists;
Alexey Samsonov8368b372012-11-21 14:17:42 +0000275 if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
Eric Christopherc235d0c62011-06-22 17:41:40 +0000276 CmdArgs.push_back(Args.MakeArgString(P.str()));
277}
278
Daniel Dunbar6276f992009-09-18 08:15:13 +0000279void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
280 ArgStringList &CmdArgs) const {
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000281 // Darwin only supports the compiler-rt based runtime libraries.
282 switch (GetRuntimeLibType(Args)) {
283 case ToolChain::RLT_CompilerRT:
284 break;
285 default:
286 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smithbd55daf2012-11-01 04:30:05 +0000287 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarf4916cd2011-12-07 23:03:15 +0000288 return;
289 }
290
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000291 // Darwin doesn't support real static executables, don't link any runtime
292 // libraries with -static.
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000293 if (Args.hasArg(options::OPT_static) ||
294 Args.hasArg(options::OPT_fapple_kext) ||
295 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar6276f992009-09-18 08:15:13 +0000296 return;
Daniel Dunbar6276f992009-09-18 08:15:13 +0000297
298 // Reject -static-libgcc for now, we can deal with this when and if someone
299 // cares. This is useful in situations where someone wants to statically link
300 // something like libstdc++, and needs its runtime support routines.
301 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000302 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000303 << A->getAsString(Args);
304 return;
305 }
306
Daniel Dunbar4f41440c2011-11-17 00:36:57 +0000307 // If we are building profile support, link that library in.
308 if (Args.hasArg(options::OPT_fprofile_arcs) ||
309 Args.hasArg(options::OPT_fprofile_generate) ||
310 Args.hasArg(options::OPT_fcreate_profile) ||
311 Args.hasArg(options::OPT_coverage)) {
312 // Select the appropriate runtime library for the target.
313 if (isTargetIPhoneOS()) {
314 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
315 } else {
316 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
317 }
318 }
319
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000320 SanitizerArgs Sanitize(getDriver(), Args);
321
Alexey Samsonovcc429802012-11-16 12:53:14 +0000322 // Add Ubsan runtime library, if required.
323 if (Sanitize.needsUbsanRt()) {
Alexey Samsonov969be242013-01-21 08:45:02 +0000324 if (isTargetIPhoneOS()) {
Alexey Samsonovcc429802012-11-16 12:53:14 +0000325 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
326 << "-fsanitize=undefined";
327 } else {
Alexey Samsonov8368b372012-11-21 14:17:42 +0000328 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonovcc429802012-11-16 12:53:14 +0000329
330 // The Ubsan runtime library requires C++.
331 AddCXXStdlibLibArgs(Args, CmdArgs);
332 }
333 }
334
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000335 // Add ASAN runtime library, if required. Dynamic libraries and bundles
336 // should not be linked with the runtime library.
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000337 if (Sanitize.needsAsanRt()) {
Kostya Serebryany0b692ce2011-12-06 19:18:44 +0000338 if (Args.hasArg(options::OPT_dynamiclib) ||
Alexey Samsonovcc429802012-11-16 12:53:14 +0000339 Args.hasArg(options::OPT_bundle)) {
340 // Assume the binary will provide the ASan runtime.
341 } else if (isTargetIPhoneOS()) {
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000342 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonov627b10f2012-11-06 15:09:03 +0000343 << "-fsanitize=address";
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000344 } else {
Alexander Potapenkof6b56972013-01-22 09:16:03 +0000345 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx_dynamic.dylib", true);
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000346
Alexander Potapenkof6b56972013-01-22 09:16:03 +0000347 // The ASAN runtime library requires C++.
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000348 AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbar1d6469f2011-12-01 23:40:18 +0000349 }
350 }
351
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000352 // Otherwise link libSystem, then the dynamic runtime library, and finally any
353 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000354 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000355
356 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar15c89422010-01-27 00:56:37 +0000357 if (isTargetIPhoneOS()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000358 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
359 // it never went into the SDK.
Bob Wilson102be442011-10-07 17:54:41 +0000360 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
361 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
362 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000363
Daniel Dunbard1076382011-04-18 23:48:36 +0000364 // We currently always need a static runtime library for iOS.
Eric Christopherc235d0c62011-06-22 17:41:40 +0000365 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000366 } else {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000367 // The dynamic runtime library was merged with libSystem for 10.6 and
368 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000369 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000370 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000371 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000372 CmdArgs.push_back("-lgcc_s.10.5");
373
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000374 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000375 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000376 // omitted from 10.4.dylib.
377 //
378 // Unfortunately, that turned out to not be true, because Darwin system
379 // headers can still use eprintf on i386, and it is not exported from
380 // libSystem. Therefore, we still must provide a runtime library just for
381 // the tiny tiny handful of projects that *might* use that symbol.
382 if (isMacosxVersionLT(10, 5)) {
Eric Christopherc235d0c62011-06-22 17:41:40 +0000383 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000384 } else {
385 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopherc235d0c62011-06-22 17:41:40 +0000386 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
387 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000388 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000389 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000390}
391
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000392void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000393 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000394
Daniel Dunbar455a0492012-08-17 18:43:50 +0000395 // Support allowing the SDKROOT environment variable used by xcrun and other
396 // Xcode tools to define the default sysroot, by making it the default for
397 // isysroot.
Chad Rosier6c2b11c2012-12-19 23:41:50 +0000398 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
399 // Warn if the path does not exist.
400 bool Exists;
401 if (llvm::sys::fs::exists(A->getValue(), Exists) || !Exists)
402 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
403 } else {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000404 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbarb2543042013-01-15 20:33:56 +0000405 // We only use this value as the default if it is an absolute path,
406 // exists, and it is not the root path.
407 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
408 StringRef(env) != "/") {
Daniel Dunbar455a0492012-08-17 18:43:50 +0000409 Args.append(Args.MakeSeparateArg(
410 0, Opts.getOption(options::OPT_isysroot), env));
411 }
412 }
413 }
414
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000415 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000416 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
417 Arg *iOSSimVersion = Args.getLastArg(
418 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman027e9c32012-01-11 02:41:15 +0000419
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000420 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000421 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000422 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000423 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
424 iOSVersion = iOSSimVersion = 0;
425 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000426 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000427 << iOSVersion->getAsString(Args)
428 << iOSSimVersion->getAsString(Args);
429 iOSSimVersion = 0;
430 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosier64707fe2011-08-31 20:56:25 +0000431 // If no deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000432 // environment defines.
Chad Rosier64707fe2011-08-31 20:56:25 +0000433 StringRef OSXTarget;
434 StringRef iOSTarget;
435 StringRef iOSSimTarget;
436 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
437 OSXTarget = env;
438 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
439 iOSTarget = env;
440 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
441 iOSSimTarget = env;
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000442
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000443 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosier64707fe2011-08-31 20:56:25 +0000444 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif5d3231c2012-04-18 10:59:08 +0000445 // based on -isysroot.
Chad Rosier64707fe2011-08-31 20:56:25 +0000446 if (iOSTarget.empty()) {
447 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
448 StringRef first, second;
Richard Smithbd55daf2012-11-01 04:30:05 +0000449 StringRef isysroot = A->getValue();
Chad Rosier64707fe2011-08-31 20:56:25 +0000450 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
451 if (second != "")
452 iOSTarget = second.substr(0,3);
453 }
454 }
Daniel Dunbard54669d2010-01-26 01:45:19 +0000455
Chad Rosierfe6fd362011-09-28 00:46:32 +0000456 // If no OSX or iOS target has been specified and we're compiling for armv7,
457 // go ahead as assume we're targeting iOS.
Chad Rosier7b1fee12012-05-09 18:55:57 +0000458 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilsond7cf1042012-09-29 23:52:50 +0000459 (getDarwinArchName(Args) == "armv7" ||
460 getDarwinArchName(Args) == "armv7s"))
Chad Rosierf761fe92012-05-09 18:09:58 +0000461 iOSTarget = iOSVersionMin;
Chad Rosierfe6fd362011-09-28 00:46:32 +0000462
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000463 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000464 //
465 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000466
467 // Do not allow conflicts with the iOS simulator target.
Chad Rosier64707fe2011-08-31 20:56:25 +0000468 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000469 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000470 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosier64707fe2011-08-31 20:56:25 +0000471 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000472 "IPHONEOS_DEPLOYMENT_TARGET");
473 }
474
475 // Allow conflicts among OSX and iOS for historical reasons, but choose the
476 // default platform.
Chad Rosier64707fe2011-08-31 20:56:25 +0000477 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000478 if (getTriple().getArch() == llvm::Triple::arm ||
479 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosier64707fe2011-08-31 20:56:25 +0000480 OSXTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000481 else
Chad Rosier64707fe2011-08-31 20:56:25 +0000482 iOSTarget = "";
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000483 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000484
Chad Rosier64707fe2011-08-31 20:56:25 +0000485 if (!OSXTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000486 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000487 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
488 Args.append(OSXVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000489 } else if (!iOSTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000490 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000491 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
492 Args.append(iOSVersion);
Chad Rosier64707fe2011-08-31 20:56:25 +0000493 } else if (!iOSSimTarget.empty()) {
Michael J. Spencerfc790902012-10-19 22:36:40 +0000494 const Option O = Opts.getOption(
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000495 options::OPT_mios_simulator_version_min_EQ);
496 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
497 Args.append(iOSSimVersion);
Daniel Dunbard54669d2010-01-26 01:45:19 +0000498 } else {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000499 // Otherwise, assume we are targeting OS X.
Michael J. Spencerfc790902012-10-19 22:36:40 +0000500 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000501 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
502 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000503 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000504 }
Mike Stump11289f42009-09-09 15:08:12 +0000505
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000506 // Reject invalid architecture combinations.
507 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
508 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000509 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000510 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
511 }
512
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000513 // Set the tool chain target information.
514 unsigned Major, Minor, Micro;
515 bool HadExtra;
516 if (OSXVersion) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000517 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000518 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000519 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000520 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000521 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000522 << OSXVersion->getAsString(Args);
523 } else {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000524 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
525 assert(Version && "Unknown target platform!");
Richard Smithbd55daf2012-11-01 04:30:05 +0000526 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman027e9c32012-01-11 02:41:15 +0000527 Micro, HadExtra) || HadExtra ||
528 Major >= 10 || Minor >= 100 || Micro >= 100)
529 getDriver().Diag(diag::err_drv_invalid_version_number)
530 << Version->getAsString(Args);
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000531 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000532
Daniel Dunbarb1189432011-04-30 04:18:16 +0000533 bool IsIOSSim = bool(iOSSimVersion);
534
535 // In GCC, the simulator historically was treated as being OS X in some
536 // contexts, like determining the link logic, despite generally being called
537 // with an iOS deployment target. For compatibility, we detect the
538 // simulator as iOS + x86, and treat it differently in a few contexts.
539 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
540 getTriple().getArch() == llvm::Triple::x86_64))
541 IsIOSSim = true;
542
543 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000544}
545
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000546void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000547 ArgStringList &CmdArgs) const {
548 CXXStdlibType Type = GetCXXStdlibType(Args);
549
550 switch (Type) {
551 case ToolChain::CST_Libcxx:
552 CmdArgs.push_back("-lc++");
553 break;
554
555 case ToolChain::CST_Libstdcxx: {
556 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
557 // it was previously found in the gcc lib dir. However, for all the Darwin
558 // platforms we care about it was -lstdc++.6, so we search for that
559 // explicitly if we can't see an obvious -lstdc++ candidate.
560
561 // Check in the sysroot first.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000562 bool Exists;
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000563 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smithbd55daf2012-11-01 04:30:05 +0000564 llvm::sys::Path P(A->getValue());
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000565 P.appendComponent("usr");
566 P.appendComponent("lib");
567 P.appendComponent("libstdc++.dylib");
568
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000569 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000570 P.eraseComponent();
571 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000572 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000573 CmdArgs.push_back(Args.MakeArgString(P.str()));
574 return;
575 }
576 }
577 }
578
579 // Otherwise, look in the root.
Bob Wilson1a9ad0f2011-11-11 07:47:04 +0000580 // FIXME: This should be removed someday when we don't have to care about
581 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000582 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
583 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000584 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
585 return;
586 }
587
588 // Otherwise, let the linker search.
589 CmdArgs.push_back("-lstdc++");
590 break;
591 }
592 }
593}
594
Shantonu Senafeb03b2010-09-17 18:39:08 +0000595void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
596 ArgStringList &CmdArgs) const {
597
598 // For Darwin platforms, use the compiler-rt-based support library
599 // instead of the gcc-provided one (which is also incidentally
600 // only present in the gcc lib dir, which makes it hard to find).
601
602 llvm::sys::Path P(getDriver().ResourceDir);
603 P.appendComponent("lib");
604 P.appendComponent("darwin");
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000605
606 // Use the newer cc_kext for iOS ARM after 6.0.
607 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
608 !isIPhoneOSVersionLT(6, 0)) {
609 P.appendComponent("libclang_rt.cc_kext.a");
610 } else {
611 P.appendComponent("libclang_rt.cc_kext_ios5.a");
612 }
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000613
Shantonu Senafeb03b2010-09-17 18:39:08 +0000614 // For now, allow missing resource libraries to support developers who may
615 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000616 bool Exists;
617 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Senafeb03b2010-09-17 18:39:08 +0000618 CmdArgs.push_back(Args.MakeArgString(P.str()));
619}
620
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000621DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
622 const char *BoundArch) const {
623 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
624 const OptTable &Opts = getDriver().getOpts();
625
626 // FIXME: We really want to get out of the tool chain level argument
627 // translation business, as it makes the driver functionality much
628 // more opaque. For now, we follow gcc closely solely for the
629 // purpose of easily achieving feature parity & testability. Once we
630 // have something that works, we should reevaluate each translation
631 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000632
Daniel Dunbar775d4062010-06-11 22:00:26 +0000633 for (ArgList::const_iterator it = Args.begin(),
634 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000635 Arg *A = *it;
636
637 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar471c4f82011-06-21 00:20:17 +0000638 // Skip this argument unless the architecture matches either the toolchain
639 // triple arch, or the arch being bound.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000640 llvm::Triple::ArchType XarchArch =
Richard Smithbd55daf2012-11-01 04:30:05 +0000641 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000642 if (!(XarchArch == getArch() ||
643 (BoundArch && XarchArch ==
Rafael Espindoladcbf6982012-10-31 18:51:07 +0000644 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000645 continue;
646
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000647 Arg *OriginalArg = A;
Richard Smithbd55daf2012-11-01 04:30:05 +0000648 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000649 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000650 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000651
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000652 // If the argument parsing failed or more than one argument was
653 // consumed, the -Xarch_ argument's parameter tried to consume
654 // extra arguments. Emit an error and ignore.
655 //
656 // We also want to disallow any options which would alter the
657 // driver behavior; that isn't going to work in our model. We
658 // use isDriverOption() as an approximation, although things
659 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000660 if (!XarchArg || Index > Prev + 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000661 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar6914a982011-04-21 17:32:21 +0000662 << A->getAsString(Args);
663 continue;
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000664 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000665 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000666 << A->getAsString(Args);
667 continue;
668 }
669
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000670 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000671 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000672
673 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000674
675 // Linker input arguments require custom handling. The problem is that we
676 // have already constructed the phase actions, so we can not treat them as
677 // "input arguments".
Michael J. Spencer66e2b202012-10-19 22:37:06 +0000678 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000679 // Convert the argument into individual Zlinker_input_args.
680 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
681 DAL->AddSeparateArg(OriginalArg,
682 Opts.getOption(options::OPT_Zlinker_input),
Richard Smithbd55daf2012-11-01 04:30:05 +0000683 A->getValue(i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000684
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000685 }
686 continue;
687 }
Mike Stump11289f42009-09-09 15:08:12 +0000688 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000689
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000690 // Sob. These is strictly gcc compatible for the time being. Apple
691 // gcc translates options twice, which means that self-expanding
692 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000693 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000694 default:
695 DAL->append(A);
696 break;
697
698 case options::OPT_mkernel:
699 case options::OPT_fapple_kext:
700 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000701 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000702 break;
Mike Stump11289f42009-09-09 15:08:12 +0000703
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000704 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000705 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smithbd55daf2012-11-01 04:30:05 +0000706 A->getValue());
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000707 break;
708
709 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000710 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
711 DAL->AddFlagArg(A,
712 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000713 break;
714
715 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000716 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
717 DAL->AddFlagArg(A,
718 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000719 break;
720
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000721 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000722 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000723 break;
724
725 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000726 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000727 break;
728
729 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000730 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000731 break;
732
733 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000734 DAL->AddFlagArg(A,
735 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000736 break;
737
738 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000739 DAL->AddFlagArg(A,
740 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000741 break;
742
743 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000744 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000745 break;
746
747 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000748 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000749 break;
750 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000751 }
752
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000753 if (getTriple().getArch() == llvm::Triple::x86 ||
754 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000755 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000756 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000757
758 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosier7c5d9082012-04-27 14:58:16 +0000759 // how the driver driver works.
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000760 if (BoundArch) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000761 StringRef Name = BoundArch;
Michael J. Spencerfc790902012-10-19 22:36:40 +0000762 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
763 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000764
765 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
766 // which defines the list of which architectures we accept.
767 if (Name == "ppc")
768 ;
769 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000770 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000771 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000772 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000773 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000774 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000775 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000776 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000777 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000778 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000779 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000780 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000781 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000782 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000783 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000784 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000785
786 else if (Name == "ppc64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000787 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000788
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000789 else if (Name == "i386")
790 ;
791 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000792 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000793 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000794 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000795 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000796 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000797 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000798 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000799 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000800 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000801 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000802 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000803 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000804 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000805
806 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000807 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000808
809 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000810 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000811 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000812 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000813 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000814 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000815 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000816 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000817 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000818 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson743bf672013-03-04 22:37:49 +0000819 else if (Name == "armv6m")
820 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000821 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000822 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson743bf672013-03-04 22:37:49 +0000823 else if (Name == "armv7em")
824 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000825 else if (Name == "armv7f")
826 DAL->AddJoinedArg(0, MArch, "armv7f");
827 else if (Name == "armv7k")
828 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson743bf672013-03-04 22:37:49 +0000829 else if (Name == "armv7m")
830 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilsond7cf1042012-09-29 23:52:50 +0000831 else if (Name == "armv7s")
832 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000833
834 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000835 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000836 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000837
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000838 // Add an explicit version min argument for the deployment target. We do this
839 // after argument translation because -Xarch_ arguments may add a version min
840 // argument.
Chad Rosier98ab91c2012-04-27 19:51:11 +0000841 if (BoundArch)
842 AddDeploymentTarget(*DAL);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000843
Daniel Dunbarbd847cc2012-10-15 22:23:53 +0000844 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
845 // FIXME: It would be far better to avoid inserting those -static arguments,
846 // but we can't check the deployment target in the translation code until
847 // it is set here.
848 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
849 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
850 Arg *A = *it;
851 ++it;
852 if (A->getOption().getID() != options::OPT_mkernel &&
853 A->getOption().getID() != options::OPT_fapple_kext)
854 continue;
855 assert(it != ie && "unexpected argument translation");
856 A = *it;
857 assert(A->getOption().getID() == options::OPT_static &&
858 "missing expected -static argument");
859 it = DAL->getArgs().erase(it);
860 }
861 }
862
Bob Wilson102be442011-10-07 17:54:41 +0000863 // Validate the C++ standard library choice.
864 CXXStdlibType Type = GetCXXStdlibType(*DAL);
865 if (Type == ToolChain::CST_Libcxx) {
John McCall5fb5df92012-06-20 06:18:46 +0000866 // Check whether the target provides libc++.
867 StringRef where;
868
869 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson5ad5a952012-11-09 01:59:30 +0000870 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
871 where = "iOS 5.0";
John McCall5fb5df92012-06-20 06:18:46 +0000872
873 if (where != StringRef()) {
Bob Wilson102be442011-10-07 17:54:41 +0000874 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall5fb5df92012-06-20 06:18:46 +0000875 << where;
Bob Wilson102be442011-10-07 17:54:41 +0000876 }
877 }
878
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000879 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000880}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000881
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000882bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolae8bd4e52012-10-07 03:23:40 +0000883 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000884}
885
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000886bool Darwin::UseDwarfDebugFlags() const {
887 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
888 return S[0] != '\0';
889 return false;
890}
891
Daniel Dunbar3241d402010-02-10 18:49:11 +0000892bool Darwin::UseSjLjExceptions() const {
893 // Darwin uses SjLj exceptions on ARM.
894 return (getTriple().getArch() == llvm::Triple::arm ||
895 getTriple().getArch() == llvm::Triple::thumb);
896}
897
Chandler Carruth76a943b2012-11-19 03:52:03 +0000898bool Darwin::isPICDefault() const {
899 return true;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000900}
901
Chandler Carruth76a943b2012-11-19 03:52:03 +0000902bool Darwin::isPICDefaultForced() const {
903 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000904}
905
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000906bool Darwin::SupportsProfiling() const {
907 // Profiling instrumentation is only supported on x86.
Rafael Espindola35ca7d92012-10-07 04:44:33 +0000908 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000909}
910
Daniel Dunbar16334e12010-04-10 16:20:23 +0000911bool Darwin::SupportsObjCGC() const {
912 // Garbage collection is supported everywhere except on iPhone OS.
913 return !isTargetIPhoneOS();
914}
915
John McCall3deb1ad2012-08-21 02:47:43 +0000916void Darwin::CheckObjCARC() const {
917 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
918 return;
John McCall93207072012-08-27 01:56:21 +0000919 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis3dbeb552012-02-29 03:43:52 +0000920}
921
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000922std::string
Chad Rosierd3a0f952011-09-20 20:44:06 +0000923Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
924 types::ID InputType) const {
925 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000926}
927
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000928/// Generic_GCC - A tool chain using the 'gcc' command to perform
929/// all subcommands; this relies on gcc translating the majority of
930/// command line options.
931
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000932/// \brief Parse a GCCVersion object out of a string of text.
933///
934/// This is the primary means of forming GCCVersion objects.
935/*static*/
936Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
937 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
938 std::pair<StringRef, StringRef> First = VersionText.split('.');
939 std::pair<StringRef, StringRef> Second = First.second.split('.');
940
941 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
942 if (First.first.getAsInteger(10, GoodVersion.Major) ||
943 GoodVersion.Major < 0)
944 return BadVersion;
945 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
946 GoodVersion.Minor < 0)
947 return BadVersion;
948
949 // First look for a number prefix and parse that if present. Otherwise just
950 // stash the entire patch string in the suffix, and leave the number
951 // unspecified. This covers versions strings such as:
952 // 4.4
953 // 4.4.0
954 // 4.4.x
955 // 4.4.2-rc4
956 // 4.4.x-patched
957 // And retains any patch number it finds.
958 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
959 if (!PatchText.empty()) {
Will Dietza38608b2013-01-10 22:20:02 +0000960 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000961 // Try to parse the number and any suffix.
962 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
963 GoodVersion.Patch < 0)
964 return BadVersion;
965 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
966 }
967 }
968
969 return GoodVersion;
970}
971
972/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
973bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000974 if (Major != RHS.Major)
975 return Major < RHS.Major;
976 if (Minor != RHS.Minor)
977 return Minor < RHS.Minor;
978 if (Patch != RHS.Patch) {
979 // Note that versions without a specified patch sort higher than those with
980 // a patch.
981 if (RHS.Patch == -1)
982 return true;
983 if (Patch == -1)
984 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000985
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000986 // Otherwise just sort on the patch itself.
987 return Patch < RHS.Patch;
988 }
989 if (PatchSuffix != RHS.PatchSuffix) {
990 // Sort empty suffixes higher.
991 if (RHS.PatchSuffix.empty())
992 return true;
993 if (PatchSuffix.empty())
Chandler Carruth19e8bea2012-12-29 13:00:47 +0000994 return false;
Chandler Carruth4c90fba2011-11-06 23:39:34 +0000995
Chandler Carruth5193dfc2012-12-29 12:01:08 +0000996 // Provide a lexicographic sort to make this a total ordering.
997 return PatchSuffix < RHS.PatchSuffix;
998 }
999
1000 // The versions are equal.
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001001 return false;
1002}
1003
Rafael Espindola1af7c212012-02-19 01:38:32 +00001004static StringRef getGCCToolchainDir(const ArgList &Args) {
1005 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1006 if (A)
Richard Smithbd55daf2012-11-01 04:30:05 +00001007 return A->getValue();
Rafael Espindola1af7c212012-02-19 01:38:32 +00001008 return GCC_INSTALL_PREFIX;
1009}
1010
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001011/// \brief Construct a GCCInstallationDetector from the driver.
1012///
1013/// This performs all of the autodetection and sets up the various paths.
Gabor Greif8a45d572012-04-17 11:16:26 +00001014/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth866faab2012-01-25 07:21:38 +00001015///
1016/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1017/// should instead pull the target out of the driver. This is currently
1018/// necessary because the driver doesn't store the final version of the target
1019/// triple.
1020Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1021 const Driver &D,
Rafael Espindola1af7c212012-02-19 01:38:32 +00001022 const llvm::Triple &TargetTriple,
1023 const ArgList &Args)
Chandler Carruth866faab2012-01-25 07:21:38 +00001024 : IsValid(false) {
Chandler Carruth779579b2012-02-13 02:02:09 +00001025 llvm::Triple MultiarchTriple
1026 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1027 : TargetTriple.get32BitArchVariant();
Chandler Carruth866faab2012-01-25 07:21:38 +00001028 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001029 // The library directories which may contain GCC installations.
Chandler Carruth866faab2012-01-25 07:21:38 +00001030 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001031 // The compatible GCC triples for this particular architecture.
Chandler Carruth866faab2012-01-25 07:21:38 +00001032 SmallVector<StringRef, 10> CandidateTripleAliases;
1033 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1034 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1035 CandidateTripleAliases,
1036 CandidateMultiarchLibDirs,
1037 CandidateMultiarchTripleAliases);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001038
1039 // Compute the set of prefixes for our search.
1040 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1041 D.PrefixDirs.end());
Rafael Espindolac29af942012-02-03 01:01:20 +00001042
Rafael Espindola1af7c212012-02-19 01:38:32 +00001043 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1044 if (GCCToolchainDir != "") {
1045 if (GCCToolchainDir.back() == '/')
1046 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindolac29af942012-02-03 01:01:20 +00001047
Rafael Espindola1af7c212012-02-19 01:38:32 +00001048 Prefixes.push_back(GCCToolchainDir);
Rafael Espindolac29af942012-02-03 01:01:20 +00001049 } else {
1050 Prefixes.push_back(D.SysRoot);
1051 Prefixes.push_back(D.SysRoot + "/usr");
1052 Prefixes.push_back(D.InstalledDir + "/..");
1053 }
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001054
1055 // Loop over the various components which exist and select the best GCC
1056 // installation available. GCC installs are ranked by version number.
1057 Version = GCCVersion::Parse("0.0.0");
1058 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1059 if (!llvm::sys::fs::exists(Prefixes[i]))
1060 continue;
1061 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1062 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1063 if (!llvm::sys::fs::exists(LibDir))
1064 continue;
Chandler Carruth866faab2012-01-25 07:21:38 +00001065 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001066 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1067 CandidateTripleAliases[k]);
Chandler Carruth866faab2012-01-25 07:21:38 +00001068 }
1069 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1070 const std::string LibDir
1071 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1072 if (!llvm::sys::fs::exists(LibDir))
1073 continue;
1074 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1075 ++k)
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00001076 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth866faab2012-01-25 07:21:38 +00001077 CandidateMultiarchTripleAliases[k],
1078 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth4c90fba2011-11-06 23:39:34 +00001079 }
1080 }
1081}
1082
1083/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth866faab2012-01-25 07:21:38 +00001084 const llvm::Triple &TargetTriple,
1085 const llvm::Triple &MultiarchTriple,
1086 SmallVectorImpl<StringRef> &LibDirs,
1087 SmallVectorImpl<StringRef> &TripleAliases,
1088 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1089 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1090 // Declare a bunch of static data sets that we'll select between below. These
1091 // are specifically designed to always refer to string literals to avoid any
1092 // lifetime or initialization issues.
Tim Northover9bb857a2013-01-31 12:13:10 +00001093 static const char *const AArch64LibDirs[] = { "/lib" };
1094 static const char *const AArch64Triples[] = {
1095 "aarch64-none-linux-gnu",
1096 "aarch64-linux-gnu"
1097 };
1098
Chandler Carruth866faab2012-01-25 07:21:38 +00001099 static const char *const ARMLibDirs[] = { "/lib" };
1100 static const char *const ARMTriples[] = {
1101 "arm-linux-gnueabi",
1102 "arm-linux-androideabi"
1103 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00001104 static const char *const ARMHFTriples[] = {
1105 "arm-linux-gnueabihf",
1106 };
Chandler Carruth866faab2012-01-25 07:21:38 +00001107
1108 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1109 static const char *const X86_64Triples[] = {
1110 "x86_64-linux-gnu",
1111 "x86_64-unknown-linux-gnu",
1112 "x86_64-pc-linux-gnu",
1113 "x86_64-redhat-linux6E",
1114 "x86_64-redhat-linux",
1115 "x86_64-suse-linux",
1116 "x86_64-manbo-linux-gnu",
1117 "x86_64-linux-gnu",
1118 "x86_64-slackware-linux"
1119 };
1120 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1121 static const char *const X86Triples[] = {
1122 "i686-linux-gnu",
1123 "i686-pc-linux-gnu",
1124 "i486-linux-gnu",
1125 "i386-linux-gnu",
NAKAMURA Takumi55c12892012-12-07 17:13:18 +00001126 "i386-redhat-linux6E",
Chandler Carruth866faab2012-01-25 07:21:38 +00001127 "i686-redhat-linux",
1128 "i586-redhat-linux",
1129 "i386-redhat-linux",
1130 "i586-suse-linux",
Gabor Greif93047632012-05-15 11:21:03 +00001131 "i486-slackware-linux",
1132 "i686-montavista-linux"
Chandler Carruth866faab2012-01-25 07:21:38 +00001133 };
1134
1135 static const char *const MIPSLibDirs[] = { "/lib" };
1136 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1137 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyan53fefd12012-10-03 17:46:38 +00001138 static const char *const MIPSELTriples[] = {
1139 "mipsel-linux-gnu",
1140 "mipsel-linux-android"
1141 };
Chandler Carruth866faab2012-01-25 07:21:38 +00001142
Simon Atanasyan9bb634d2012-04-26 19:57:02 +00001143 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1144 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1145 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1146 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1147
Chandler Carruth866faab2012-01-25 07:21:38 +00001148 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1149 static const char *const PPCTriples[] = {
1150 "powerpc-linux-gnu",
1151 "powerpc-unknown-linux-gnu",
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,
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001389 const JobAction &JA,
1390 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001391 Action::ActionClass Key;
1392 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1393 Key = Action::AnalyzeJobClass;
1394 else
1395 Key = JA.getKind();
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001396
1397 Tool *&T = Tools[Key];
1398 if (!T) {
1399 switch (Key) {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +00001400 case Action::InputClass:
1401 case Action::BindArchClass:
David Blaikie83d382b2011-09-23 05:06:16 +00001402 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001403 case Action::PreprocessJobClass:
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001404 T = new tools::gcc::Preprocess(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001405 case Action::PrecompileJobClass:
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001406 T = new tools::gcc::Precompile(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001407 case Action::AnalyzeJobClass:
Ted Kremenekf7639e12012-03-06 20:06:33 +00001408 case Action::MigrateJobClass:
Bob Wilson0c79f3c2012-11-08 01:03:34 +00001409 T = new tools::Clang(*this); break;
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001410 case Action::CompileJobClass:
1411 T = new tools::gcc::Compile(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001412 case Action::AssembleJobClass:
1413 T = new tools::gcc::Assemble(*this); break;
1414 case Action::LinkJobClass:
1415 T = new tools::gcc::Link(*this); break;
Mike Stump11289f42009-09-09 15:08:12 +00001416
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +00001417 // This is a bit ungeneric, but the only platform using a driver
1418 // driver is Darwin.
1419 case Action::LipoJobClass:
1420 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar88299622010-06-04 18:28:36 +00001421 case Action::DsymutilJobClass:
1422 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopher551ef452011-08-23 17:56:55 +00001423 case Action::VerifyJobClass:
1424 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001425 }
1426 }
1427
1428 return *T;
1429}
1430
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001431bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola08f1ebb2012-09-22 15:04:11 +00001432 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001433}
1434
Chandler Carruth76a943b2012-11-19 03:52:03 +00001435bool Generic_GCC::isPICDefault() const {
1436 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001437}
1438
Chandler Carruth76a943b2012-11-19 03:52:03 +00001439bool Generic_GCC::isPICDefaultForced() const {
1440 return false;
Daniel Dunbar59e5e882009-03-20 00:20:03 +00001441}
Chandler Carruth76a943b2012-11-19 03:52:03 +00001442
Tony Linthicum76329bf2011-12-12 21:14:55 +00001443/// Hexagon Toolchain
1444
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001445std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1446
1447 // Locate the rest of the toolchain ...
1448 if (strlen(GCC_INSTALL_PREFIX))
1449 return std::string(GCC_INSTALL_PREFIX);
1450
1451 std::string InstallRelDir = InstalledDir + "/../../gnu";
1452 if (llvm::sys::fs::exists(InstallRelDir))
1453 return InstallRelDir;
1454
1455 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1456 if (llvm::sys::fs::exists(PrefixRelDir))
1457 return PrefixRelDir;
1458
1459 return InstallRelDir;
1460}
1461
Matthew Curtise689b052012-12-06 15:46:07 +00001462static void GetHexagonLibraryPaths(
1463 const ArgList &Args,
1464 const std::string Ver,
1465 const std::string MarchString,
1466 const std::string &InstalledDir,
1467 ToolChain::path_list *LibPaths)
1468{
1469 bool buildingLib = Args.hasArg(options::OPT_shared);
1470
1471 //----------------------------------------------------------------------------
1472 // -L Args
1473 //----------------------------------------------------------------------------
1474 for (arg_iterator
1475 it = Args.filtered_begin(options::OPT_L),
1476 ie = Args.filtered_end();
1477 it != ie;
1478 ++it) {
1479 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1480 LibPaths->push_back((*it)->getValue(i));
1481 }
1482
1483 //----------------------------------------------------------------------------
1484 // Other standard paths
1485 //----------------------------------------------------------------------------
1486 const std::string MarchSuffix = "/" + MarchString;
1487 const std::string G0Suffix = "/G0";
1488 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1489 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1490
1491 // lib/gcc/hexagon/...
1492 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1493 if (buildingLib) {
1494 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1495 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1496 }
1497 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1498 LibPaths->push_back(LibGCCHexagonDir + Ver);
1499
1500 // lib/gcc/...
1501 LibPaths->push_back(RootDir + "lib/gcc");
1502
1503 // hexagon/lib/...
1504 std::string HexagonLibDir = RootDir + "hexagon/lib";
1505 if (buildingLib) {
1506 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1507 LibPaths->push_back(HexagonLibDir + G0Suffix);
1508 }
1509 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1510 LibPaths->push_back(HexagonLibDir);
1511}
1512
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001513Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1514 const ArgList &Args)
1515 : Linux(D, Triple, Args) {
1516 const std::string InstalledDir(getDriver().getInstalledDir());
1517 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1518
1519 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1520 // program paths
1521 const std::string BinDir(GnuDir + "/bin");
1522 if (llvm::sys::fs::exists(BinDir))
1523 getProgramPaths().push_back(BinDir);
1524
1525 // Determine version of GCC libraries and headers to use.
1526 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1527 llvm::error_code ec;
1528 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1529 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1530 !ec && di != de; di = di.increment(ec)) {
1531 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1532 if (MaxVersion < cv)
1533 MaxVersion = cv;
1534 }
1535 GCCLibAndIncVersion = MaxVersion;
Matthew Curtise689b052012-12-06 15:46:07 +00001536
1537 ToolChain::path_list *LibPaths= &getFilePaths();
1538
1539 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1540 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1541 // support 'linux' we'll need to fix this up
1542 LibPaths->clear();
1543
1544 GetHexagonLibraryPaths(
1545 Args,
1546 GetGCCLibAndIncVersion(),
1547 GetTargetCPU(Args),
1548 InstalledDir,
1549 LibPaths);
Tony Linthicum76329bf2011-12-12 21:14:55 +00001550}
1551
1552Hexagon_TC::~Hexagon_TC() {
1553 // Free tool implementations.
1554 for (llvm::DenseMap<unsigned, Tool*>::iterator
1555 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1556 delete it->second;
1557}
1558
1559Tool &Hexagon_TC::SelectTool(const Compilation &C,
1560 const JobAction &JA,
1561 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001562 Action::ActionClass Key;
1563 // if (JA.getKind () == Action::CompileJobClass)
1564 // Key = JA.getKind ();
1565 // else
1566
1567 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1568 Key = Action::AnalyzeJobClass;
1569 else
1570 Key = JA.getKind();
1571 // if ((JA.getKind () == Action::CompileJobClass)
1572 // && (JA.getType () != types::TY_LTO_BC)) {
1573 // Key = JA.getKind ();
1574 // }
1575
Tony Linthicum76329bf2011-12-12 21:14:55 +00001576 Tool *&T = Tools[Key];
1577 if (!T) {
1578 switch (Key) {
1579 case Action::InputClass:
1580 case Action::BindArchClass:
1581 assert(0 && "Invalid tool kind.");
1582 case Action::AnalyzeJobClass:
1583 T = new tools::Clang(*this); break;
1584 case Action::AssembleJobClass:
1585 T = new tools::hexagon::Assemble(*this); break;
1586 case Action::LinkJobClass:
1587 T = new tools::hexagon::Link(*this); break;
1588 default:
1589 assert(false && "Unsupported action for Hexagon target.");
1590 }
1591 }
1592
1593 return *T;
1594}
1595
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001596void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1597 ArgStringList &CC1Args) const {
1598 const Driver &D = getDriver();
1599
1600 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1601 DriverArgs.hasArg(options::OPT_nostdlibinc))
1602 return;
1603
1604 llvm::sys::Path InstallDir(D.InstalledDir);
1605 std::string Ver(GetGCCLibAndIncVersion());
1606 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1607 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1608 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1609 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1610 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum76329bf2011-12-12 21:14:55 +00001611}
1612
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001613void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1614 ArgStringList &CC1Args) const {
1615
1616 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1617 DriverArgs.hasArg(options::OPT_nostdincxx))
1618 return;
1619
1620 const Driver &D = getDriver();
1621 std::string Ver(GetGCCLibAndIncVersion());
1622 llvm::sys::Path IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
1623
1624 IncludeDir.appendComponent("hexagon/include/c++/");
1625 IncludeDir.appendComponent(Ver);
1626 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth76a943b2012-11-19 03:52:03 +00001627}
Matthew Curtisf10a5952012-12-06 14:16:43 +00001628
Matthew Curtise689b052012-12-06 15:46:07 +00001629ToolChain::CXXStdlibType
1630Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1631 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1632 if (!A)
1633 return ToolChain::CST_Libstdcxx;
1634
1635 StringRef Value = A->getValue();
1636 if (Value != "libstdc++") {
1637 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1638 << A->getAsString(Args);
1639 }
1640
1641 return ToolChain::CST_Libstdcxx;
1642}
1643
1644static Arg *GetLastHexagonArchArg(const ArgList &Args)
Matthew Curtisf10a5952012-12-06 14:16:43 +00001645{
1646 Arg *A = NULL;
1647
1648 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1649 it != ie; ++it) {
1650 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
1651 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1652 A = *it;
1653 A->claim();
1654 } else if ((*it)->getOption().matches(options::OPT_m_Joined)) {
1655 StringRef Value = (*it)->getValue(0);
1656 if (Value.startswith("v")) {
1657 A = *it;
1658 A->claim();
1659 }
1660 }
1661 }
1662 return A;
1663}
1664
1665StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1666{
1667 // Select the default CPU (v4) if none was given or detection failed.
1668 Arg *A = GetLastHexagonArchArg (Args);
1669 if (A) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001670 StringRef WhichHexagon = A->getValue();
Matthew Curtisf10a5952012-12-06 14:16:43 +00001671 if (WhichHexagon.startswith("hexagon"))
1672 return WhichHexagon.substr(sizeof("hexagon") - 1);
1673 if (WhichHexagon != "")
1674 return WhichHexagon;
1675 }
1676
1677 return "v4";
1678}
Matthew Curtis22dd8da2012-12-06 12:43:18 +00001679// End Hexagon
Daniel Dunbardac54a82009-03-25 04:13:45 +00001680
Chris Lattner09797542010-03-04 21:07:38 +00001681/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1682/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1683/// Currently does not support anything else but compilation.
1684
Chandler Carruthd7fa2e02012-01-31 02:21:20 +00001685TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1686 : ToolChain(D, Triple) {
Chris Lattner09797542010-03-04 21:07:38 +00001687 // Path mangling to find libexec
1688 std::string Path(getDriver().Dir);
1689
1690 Path += "/../libexec";
1691 getProgramPaths().push_back(Path);
1692}
1693
1694TCEToolChain::~TCEToolChain() {
1695 for (llvm::DenseMap<unsigned, Tool*>::iterator
1696 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1697 delete it->second;
1698}
1699
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001700bool TCEToolChain::IsMathErrnoDefault() const {
1701 return true;
Chris Lattner09797542010-03-04 21:07:38 +00001702}
1703
Chandler Carruth76a943b2012-11-19 03:52:03 +00001704bool TCEToolChain::isPICDefault() const {
1705 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001706}
1707
Chandler Carruth76a943b2012-11-19 03:52:03 +00001708bool TCEToolChain::isPICDefaultForced() const {
1709 return false;
Chris Lattner09797542010-03-04 21:07:38 +00001710}
1711
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001712Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001713 const JobAction &JA,
1714 const ActionList &Inputs) const {
Chris Lattner09797542010-03-04 21:07:38 +00001715 Action::ActionClass Key;
1716 Key = Action::AnalyzeJobClass;
1717
1718 Tool *&T = Tools[Key];
1719 if (!T) {
1720 switch (Key) {
1721 case Action::PreprocessJobClass:
1722 T = new tools::gcc::Preprocess(*this); break;
1723 case Action::AnalyzeJobClass:
1724 T = new tools::Clang(*this); break;
1725 default:
David Blaikie83d382b2011-09-23 05:06:16 +00001726 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner09797542010-03-04 21:07:38 +00001727 }
1728 }
1729 return *T;
1730}
1731
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001732/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1733
Rafael Espindola1af7c212012-02-19 01:38:32 +00001734OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1735 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar083edf72009-12-21 18:54:17 +00001736 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001737 getFilePaths().push_back("/usr/lib");
1738}
1739
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001740Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1741 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001742 Action::ActionClass Key;
1743 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1744 Key = Action::AnalyzeJobClass;
1745 else
1746 Key = JA.getKind();
1747
Rafael Espindola96aef792010-11-07 23:13:01 +00001748 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1749 options::OPT_no_integrated_as,
1750 IsIntegratedAssemblerDefault());
1751
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001752 Tool *&T = Tools[Key];
1753 if (!T) {
1754 switch (Key) {
Rafael Espindola96aef792010-11-07 23:13:01 +00001755 case Action::AssembleJobClass: {
1756 if (UseIntegratedAs)
1757 T = new tools::ClangAs(*this);
1758 else
1759 T = new tools::openbsd::Assemble(*this);
1760 break;
1761 }
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001762 case Action::LinkJobClass:
1763 T = new tools::openbsd::Link(*this); break;
1764 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001765 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +00001766 }
1767 }
1768
1769 return *T;
1770}
1771
Eli Friedman9fa28852012-08-08 23:57:20 +00001772/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1773
1774Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1775 : Generic_ELF(D, Triple, Args) {
1776 getFilePaths().push_back(getDriver().Dir + "/../lib");
1777 getFilePaths().push_back("/usr/lib");
1778}
1779
1780Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA,
1781 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001782 Action::ActionClass Key;
1783 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1784 Key = Action::AnalyzeJobClass;
1785 else
1786 Key = JA.getKind();
1787
Eli Friedman9fa28852012-08-08 23:57:20 +00001788 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1789 options::OPT_no_integrated_as,
1790 IsIntegratedAssemblerDefault());
1791
1792 Tool *&T = Tools[Key];
1793 if (!T) {
1794 switch (Key) {
1795 case Action::AssembleJobClass: {
1796 if (UseIntegratedAs)
1797 T = new tools::ClangAs(*this);
1798 else
1799 T = new tools::bitrig::Assemble(*this);
1800 break;
1801 }
1802 case Action::LinkJobClass:
1803 T = new tools::bitrig::Link(*this); break;
1804 default:
1805 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1806 }
1807 }
1808
1809 return *T;
1810}
1811
1812void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1813 ArgStringList &CC1Args) const {
1814 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1815 DriverArgs.hasArg(options::OPT_nostdincxx))
1816 return;
1817
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001818 switch (GetCXXStdlibType(DriverArgs)) {
1819 case ToolChain::CST_Libcxx:
1820 addSystemInclude(DriverArgs, CC1Args,
1821 getDriver().SysRoot + "/usr/include/c++/");
1822 break;
1823 case ToolChain::CST_Libstdcxx:
1824 addSystemInclude(DriverArgs, CC1Args,
1825 getDriver().SysRoot + "/usr/include/c++/stdc++");
1826 addSystemInclude(DriverArgs, CC1Args,
1827 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman9fa28852012-08-08 23:57:20 +00001828
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001829 StringRef Triple = getTriple().str();
1830 if (Triple.startswith("amd64"))
1831 addSystemInclude(DriverArgs, CC1Args,
1832 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1833 Triple.substr(5));
1834 else
1835 addSystemInclude(DriverArgs, CC1Args,
1836 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1837 Triple);
1838 break;
1839 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001840}
1841
1842void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1843 ArgStringList &CmdArgs) const {
Chandler Carruthc0f5cd12012-10-08 21:31:38 +00001844 switch (GetCXXStdlibType(Args)) {
1845 case ToolChain::CST_Libcxx:
1846 CmdArgs.push_back("-lc++");
1847 CmdArgs.push_back("-lcxxrt");
1848 // Include supc++ to provide Unwind until provided by libcxx.
1849 CmdArgs.push_back("-lgcc");
1850 break;
1851 case ToolChain::CST_Libstdcxx:
1852 CmdArgs.push_back("-lstdc++");
1853 break;
1854 }
Eli Friedman9fa28852012-08-08 23:57:20 +00001855}
1856
Daniel Dunbare24297c2009-03-30 21:06:03 +00001857/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1858
Rafael Espindola1af7c212012-02-19 01:38:32 +00001859FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1860 : Generic_ELF(D, Triple, Args) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001861
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001862 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1863 // back to '/usr/lib' if it doesn't exist.
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001864 if ((Triple.getArch() == llvm::Triple::x86 ||
1865 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth0b1756b2012-01-26 01:35:15 +00001866 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00001867 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1868 else
1869 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbare24297c2009-03-30 21:06:03 +00001870}
1871
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001872Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1873 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001874 Action::ActionClass Key;
1875 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1876 Key = Action::AnalyzeJobClass;
1877 else
1878 Key = JA.getKind();
1879
Roman Divacky137426a2010-11-08 17:46:39 +00001880 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1881 options::OPT_no_integrated_as,
1882 IsIntegratedAssemblerDefault());
1883
Daniel Dunbare24297c2009-03-30 21:06:03 +00001884 Tool *&T = Tools[Key];
1885 if (!T) {
1886 switch (Key) {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00001887 case Action::AssembleJobClass:
Roman Divacky137426a2010-11-08 17:46:39 +00001888 if (UseIntegratedAs)
1889 T = new tools::ClangAs(*this);
1890 else
1891 T = new tools::freebsd::Assemble(*this);
Roman Divackyb45d2672010-11-08 19:39:10 +00001892 break;
Daniel Dunbard854c8d2009-04-01 19:36:32 +00001893 case Action::LinkJobClass:
1894 T = new tools::freebsd::Link(*this); break;
Daniel Dunbare24297c2009-03-30 21:06:03 +00001895 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001896 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbare24297c2009-03-30 21:06:03 +00001897 }
1898 }
1899
1900 return *T;
1901}
Daniel Dunbarcc912342009-05-02 18:28:39 +00001902
Rafael Espindola0f207ed2012-12-13 04:17:14 +00001903bool FreeBSD::UseSjLjExceptions() const {
1904 // FreeBSD uses SjLj exceptions on ARM oabi.
1905 switch (getTriple().getEnvironment()) {
1906 case llvm::Triple::GNUEABI:
1907 case llvm::Triple::EABI:
1908 return false;
1909
1910 default:
1911 return (getTriple().getArch() == llvm::Triple::arm ||
1912 getTriple().getArch() == llvm::Triple::thumb);
1913 }
1914}
1915
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001916/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1917
Rafael Espindola1af7c212012-02-19 01:38:32 +00001918NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1919 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001920
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001921 if (getDriver().UseStdLib) {
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001922 // When targeting a 32-bit platform, try the special directory used on
1923 // 64-bit hosts, and only fall back to the main library directory if that
1924 // doesn't work.
1925 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1926 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenbergerf8ce8572012-01-26 21:58:37 +00001927 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001928 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth1ccbed82012-01-25 11:18:20 +00001929
1930 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001931 }
1932}
1933
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001934Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1935 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001936 Action::ActionClass Key;
1937 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1938 Key = Action::AnalyzeJobClass;
1939 else
1940 Key = JA.getKind();
1941
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001942 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1943 options::OPT_no_integrated_as,
1944 IsIntegratedAssemblerDefault());
1945
1946 Tool *&T = Tools[Key];
1947 if (!T) {
1948 switch (Key) {
1949 case Action::AssembleJobClass:
1950 if (UseIntegratedAs)
1951 T = new tools::ClangAs(*this);
1952 else
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00001953 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001954 break;
1955 case Action::LinkJobClass:
Joerg Sonnenbergerd64c60e2012-01-26 22:27:52 +00001956 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00001957 break;
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001958 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001959 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001960 }
1961 }
1962
1963 return *T;
1964}
1965
Chris Lattner3e2ee142010-07-07 16:01:42 +00001966/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1967
Rafael Espindola1af7c212012-02-19 01:38:32 +00001968Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1969 : Generic_ELF(D, Triple, Args) {
Chris Lattner3e2ee142010-07-07 16:01:42 +00001970 getFilePaths().push_back(getDriver().Dir + "/../lib");
1971 getFilePaths().push_back("/usr/lib");
Chris Lattner3e2ee142010-07-07 16:01:42 +00001972}
1973
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001974Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1975 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00001976 Action::ActionClass Key;
1977 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1978 Key = Action::AnalyzeJobClass;
1979 else
1980 Key = JA.getKind();
Chris Lattner3e2ee142010-07-07 16:01:42 +00001981
1982 Tool *&T = Tools[Key];
1983 if (!T) {
1984 switch (Key) {
1985 case Action::AssembleJobClass:
1986 T = new tools::minix::Assemble(*this); break;
1987 case Action::LinkJobClass:
1988 T = new tools::minix::Link(*this); break;
1989 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001990 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00001991 }
1992 }
1993
1994 return *T;
1995}
1996
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001997/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1998
Rafael Espindola1af7c212012-02-19 01:38:32 +00001999AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
2000 const ArgList &Args)
2001 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002002
Daniel Dunbar88979912010-08-01 22:29:51 +00002003 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002004 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002005 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002006
Daniel Dunbar083edf72009-12-21 18:54:17 +00002007 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002008 getFilePaths().push_back("/usr/lib");
2009 getFilePaths().push_back("/usr/sfw/lib");
2010 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00002011 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002012
2013}
2014
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00002015Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
2016 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002017 Action::ActionClass Key;
2018 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2019 Key = Action::AnalyzeJobClass;
2020 else
2021 Key = JA.getKind();
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002022
2023 Tool *&T = Tools[Key];
2024 if (!T) {
2025 switch (Key) {
2026 case Action::AssembleJobClass:
2027 T = new tools::auroraux::Assemble(*this); break;
2028 case Action::LinkJobClass:
2029 T = new tools::auroraux::Link(*this); break;
2030 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00002031 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002032 }
2033 }
2034
2035 return *T;
2036}
2037
David Chisnallf571cde2012-02-15 13:39:01 +00002038/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2039
Rafael Espindola1af7c212012-02-19 01:38:32 +00002040Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2041 const ArgList &Args)
2042 : Generic_GCC(D, Triple, Args) {
David Chisnallf571cde2012-02-15 13:39:01 +00002043
2044 getProgramPaths().push_back(getDriver().getInstalledDir());
2045 if (getDriver().getInstalledDir() != getDriver().Dir)
2046 getProgramPaths().push_back(getDriver().Dir);
2047
2048 getFilePaths().push_back(getDriver().Dir + "/../lib");
2049 getFilePaths().push_back("/usr/lib");
2050}
2051
2052Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
2053 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002054 Action::ActionClass Key;
2055 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2056 Key = Action::AnalyzeJobClass;
2057 else
2058 Key = JA.getKind();
David Chisnallf571cde2012-02-15 13:39:01 +00002059
2060 Tool *&T = Tools[Key];
2061 if (!T) {
2062 switch (Key) {
2063 case Action::AssembleJobClass:
2064 T = new tools::solaris::Assemble(*this); break;
2065 case Action::LinkJobClass:
2066 T = new tools::solaris::Link(*this); break;
2067 default:
2068 T = &Generic_GCC::SelectTool(C, JA, Inputs);
2069 }
2070 }
2071
2072 return *T;
2073}
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00002074
Eli Friedman5cd659f2009-05-26 07:52:18 +00002075/// Linux toolchain (very bare-bones at the moment).
2076
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002077enum LinuxDistro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002078 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002079 DebianLenny,
2080 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00002081 DebianWheezy,
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002082 DebianJessie,
Rafael Espindola12479842010-11-11 02:07:13 +00002083 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00002084 RHEL4,
2085 RHEL5,
2086 RHEL6,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002087 Fedora13,
2088 Fedora14,
Eric Christopher534b6a02011-04-06 18:22:53 +00002089 Fedora15,
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002090 Fedora16,
Eric Christopher534b6a02011-04-06 18:22:53 +00002091 FedoraRawhide,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002092 OpenSuse11_3,
David Chisnallb8f65e22011-05-19 13:26:33 +00002093 OpenSuse11_4,
2094 OpenSuse12_1,
Douglas Gregorf7b88412012-04-30 23:42:57 +00002095 OpenSuse12_2,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00002096 UbuntuHardy,
2097 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00002098 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00002099 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002100 UbuntuLucid,
2101 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00002102 UbuntuNatty,
Benjamin Kramerf90b5de2011-06-05 16:08:59 +00002103 UbuntuOneiric,
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002104 UbuntuPrecise,
Rafael Espindola62e87702012-12-13 20:26:05 +00002105 UbuntuQuantal,
2106 UbuntuRaring,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002107 UnknownDistro
2108};
2109
Chris Lattner84e38552011-05-22 05:36:06 +00002110static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002111 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
2112 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002113}
2114
2115static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregorf7b88412012-04-30 23:42:57 +00002116 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002117}
2118
2119static bool IsDebian(enum LinuxDistro Distro) {
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002120 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002121}
2122
2123static bool IsUbuntu(enum LinuxDistro Distro) {
Rafael Espindola62e87702012-12-13 20:26:05 +00002124 return Distro >= UbuntuHardy && Distro <= UbuntuRaring;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002125}
2126
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002127static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +00002128 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002129 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002130 StringRef Data = File.get()->getBuffer();
2131 SmallVector<StringRef, 8> Lines;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002132 Data.split(Lines, "\n");
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002133 LinuxDistro Version = UnknownDistro;
2134 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2135 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
2136 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
2137 .Case("hardy", UbuntuHardy)
2138 .Case("intrepid", UbuntuIntrepid)
2139 .Case("jaunty", UbuntuJaunty)
2140 .Case("karmic", UbuntuKarmic)
2141 .Case("lucid", UbuntuLucid)
2142 .Case("maverick", UbuntuMaverick)
2143 .Case("natty", UbuntuNatty)
2144 .Case("oneiric", UbuntuOneiric)
2145 .Case("precise", UbuntuPrecise)
Rafael Espindola62e87702012-12-13 20:26:05 +00002146 .Case("quantal", UbuntuQuantal)
2147 .Case("raring", UbuntuRaring)
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002148 .Default(UnknownDistro);
2149 return Version;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002150 }
2151
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002152 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002153 StringRef Data = File.get()->getBuffer();
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002154 if (Data.startswith("Fedora release 16"))
2155 return Fedora16;
2156 else if (Data.startswith("Fedora release 15"))
Eric Christopher534b6a02011-04-06 18:22:53 +00002157 return Fedora15;
2158 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002159 return Fedora14;
Eric Christopher534b6a02011-04-06 18:22:53 +00002160 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002161 return Fedora13;
Eric Christopher534b6a02011-04-06 18:22:53 +00002162 else if (Data.startswith("Fedora release") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002163 Data.find("Rawhide") != StringRef::npos)
Eric Christopher534b6a02011-04-06 18:22:53 +00002164 return FedoraRawhide;
Chris Lattner84e38552011-05-22 05:36:06 +00002165 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002166 Data.find("release 6") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002167 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002168 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002169 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002170 Data.find("release 5") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002171 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002172 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopherc4b0be92013-02-05 07:29:49 +00002173 Data.startswith("CentOS")) &&
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002174 Data.find("release 4") != StringRef::npos)
Chris Lattner84e38552011-05-22 05:36:06 +00002175 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002176 return UnknownDistro;
2177 }
2178
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002179 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002180 StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002181 if (Data[0] == '5')
2182 return DebianLenny;
Rafael Espindola1510c852011-12-28 18:17:14 +00002183 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002184 return DebianSqueeze;
Rafael Espindola1510c852011-12-28 18:17:14 +00002185 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedmanf7600942011-06-02 21:36:53 +00002186 return DebianWheezy;
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002187 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2188 return DebianJessie;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002189 return UnknownDistro;
2190 }
2191
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002192 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
2193 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
2194 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2195 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2196 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregorf7b88412012-04-30 23:42:57 +00002197 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramer3bb42f32012-02-06 15:33:06 +00002198 .Default(UnknownDistro);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002199
Michael J. Spencerf6efe582011-01-10 02:34:13 +00002200 bool Exists;
2201 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola12479842010-11-11 02:07:13 +00002202 return Exherbo;
2203
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002204 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2205 return ArchLinux;
2206
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002207 return UnknownDistro;
2208}
2209
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002210/// \brief Get our best guess at the multiarch triple for a target.
2211///
2212/// Debian-based systems are starting to use a multiarch setup where they use
2213/// a target-triple directory in the library and header search paths.
2214/// Unfortunately, this triple does not align with the vanilla target triple,
2215/// so we provide a rough mapping here.
2216static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2217 StringRef SysRoot) {
2218 // For most architectures, just use whatever we have rather than trying to be
2219 // clever.
2220 switch (TargetTriple.getArch()) {
2221 default:
2222 return TargetTriple.str();
2223
2224 // We use the existence of '/lib/<triple>' as a directory to detect some
2225 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth4c042fe2011-10-31 09:06:40 +00002226 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2227 // regardless of what the actual target triple is.
Chad Rosier4dce73a2012-07-11 19:08:21 +00002228 case llvm::Triple::arm:
2229 case llvm::Triple::thumb:
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002230 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2231 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2232 return "arm-linux-gnueabihf";
2233 } else {
2234 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2235 return "arm-linux-gnueabi";
2236 }
Chad Rosier4dce73a2012-07-11 19:08:21 +00002237 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002238 case llvm::Triple::x86:
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002239 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2240 return "i386-linux-gnu";
2241 return TargetTriple.str();
2242 case llvm::Triple::x86_64:
2243 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2244 return "x86_64-linux-gnu";
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002245 return TargetTriple.str();
Tim Northover9bb857a2013-01-31 12:13:10 +00002246 case llvm::Triple::aarch64:
2247 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2248 return "aarch64-linux-gnu";
Eli Friedman27b8c4f2011-11-08 19:43:37 +00002249 case llvm::Triple::mips:
2250 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2251 return "mips-linux-gnu";
2252 return TargetTriple.str();
2253 case llvm::Triple::mipsel:
2254 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2255 return "mipsel-linux-gnu";
2256 return TargetTriple.str();
Chandler Carruthaf3c2092012-02-26 09:03:21 +00002257 case llvm::Triple::ppc:
2258 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2259 return "powerpc-linux-gnu";
2260 return TargetTriple.str();
2261 case llvm::Triple::ppc64:
2262 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2263 return "powerpc64-linux-gnu";
2264 return TargetTriple.str();
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002265 }
2266}
2267
Chandler Carruthf7bf3db2012-01-25 11:24:24 +00002268static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2269 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2270}
2271
Simon Atanasyand4413882012-09-14 11:27:24 +00002272static bool isMipsArch(llvm::Triple::ArchType Arch) {
2273 return Arch == llvm::Triple::mips ||
2274 Arch == llvm::Triple::mipsel ||
2275 Arch == llvm::Triple::mips64 ||
2276 Arch == llvm::Triple::mips64el;
2277}
2278
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002279static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2280 const ArgList &Args) {
2281 if (Arch != llvm::Triple::mips &&
2282 Arch != llvm::Triple::mipsel)
2283 return false;
2284
2285 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2286 options::OPT_mcpu_EQ,
2287 options::OPT_mips_CPUs_Group);
2288
2289 if (!A)
2290 return false;
2291
2292 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2293 return A->getOption().matches(options::OPT_mips32r2);
2294
Richard Smithbd55daf2012-11-01 04:30:05 +00002295 return A->getValue() == StringRef("mips32r2");
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002296}
2297
Simon Atanasyand4413882012-09-14 11:27:24 +00002298static StringRef getMultilibDir(const llvm::Triple &Triple,
2299 const ArgList &Args) {
2300 if (!isMipsArch(Triple.getArch()))
2301 return Triple.isArch32Bit() ? "lib32" : "lib64";
2302
2303 // lib32 directory has a special meaning on MIPS targets.
2304 // It contains N32 ABI binaries. Use this folder if produce
2305 // code for N32 ABI only.
Simon Atanasyan2d1b1ad2012-10-21 11:44:57 +00002306 if (hasMipsN32ABIArg(Args))
Simon Atanasyand4413882012-09-14 11:27:24 +00002307 return "lib32";
2308
2309 return Triple.isArch32Bit() ? "lib" : "lib64";
2310}
2311
Rafael Espindola1af7c212012-02-19 01:38:32 +00002312Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2313 : Generic_ELF(D, Triple, Args) {
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002314 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruth2a649c72011-10-03 06:41:08 +00002315 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002316
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002317 // OpenSuse stores the linker with the compiler, add that to the search
2318 // path.
2319 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruth4be70dd2011-11-06 09:21:54 +00002320 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002321 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindola5f344ff2011-09-01 16:25:49 +00002322
2323 Linker = GetProgramPath("ld");
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002324
2325 LinuxDistro Distro = DetectLinuxDistro(Arch);
2326
Chris Lattnerd075c822011-05-22 16:45:07 +00002327 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00002328 ExtraOpts.push_back("-z");
2329 ExtraOpts.push_back("relro");
2330 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002331
Douglas Gregord9bb1522011-03-06 19:11:49 +00002332 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002333 ExtraOpts.push_back("-X");
2334
Logan Chienc6fd8202012-09-02 09:30:11 +00002335 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002336
Chandler Carruth0b842912011-12-09 04:45:18 +00002337 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2338 // and the MIPS ABI require .dynsym to be sorted in different ways.
2339 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2340 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov2ca1aa52012-01-13 09:30:38 +00002341 // Android loader does not support .gnu.hash.
Simon Atanasyand4413882012-09-14 11:27:24 +00002342 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002343 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2344 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruth0b842912011-12-09 04:45:18 +00002345 ExtraOpts.push_back("--hash-style=gnu");
2346
2347 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2348 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2349 ExtraOpts.push_back("--hash-style=both");
2350 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002351
Chris Lattner84e38552011-05-22 05:36:06 +00002352 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002353 ExtraOpts.push_back("--no-add-needed");
2354
Eli Friedmanf7600942011-06-02 21:36:53 +00002355 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Sylvestre Ledrubdef2892013-01-06 08:09:29 +00002356 Distro == DebianJessie || IsOpenSuse(Distro) ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00002357 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer7c3f09d2012-02-06 14:36:09 +00002358 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002359 ExtraOpts.push_back("--build-id");
2360
Chris Lattnerd075c822011-05-22 16:45:07 +00002361 if (IsOpenSuse(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00002362 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00002363
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002364 // The selection of paths to try here is designed to match the patterns which
2365 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2366 // This was determined by running GCC in a fake filesystem, creating all
2367 // possible permutations of these directories, and seeing which ones it added
2368 // to the link paths.
2369 path_list &Paths = getFilePaths();
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00002370
Simon Atanasyand4413882012-09-14 11:27:24 +00002371 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthfb7fa242011-10-31 08:42:24 +00002372 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002373
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002374 // Add the multilib suffixed paths where they are available.
2375 if (GCCInstallation.isValid()) {
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002376 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth96bae7b2012-01-24 20:08:17 +00002377 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyan53fefd12012-10-03 17:46:38 +00002378
2379 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2380 addPathIfExists(GCCInstallation.getInstallPath() +
2381 GCCInstallation.getMultiarchSuffix() +
2382 "/mips-r2",
2383 Paths);
2384 else
2385 addPathIfExists((GCCInstallation.getInstallPath() +
2386 GCCInstallation.getMultiarchSuffix()),
2387 Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002388
2389 // If the GCC installation we found is inside of the sysroot, we want to
2390 // prefer libraries installed in the parent prefix of the GCC installation.
2391 // It is important to *not* use these paths when the GCC installation is
Gabor Greif5d3231c2012-04-18 10:59:08 +00002392 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth69a125b2012-04-06 16:32:06 +00002393 // This usually happens when there is an external cross compiler on the
2394 // host system, and a more minimal sysroot available that is the target of
2395 // the cross.
2396 if (StringRef(LibPath).startswith(SysRoot)) {
2397 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2398 Paths);
2399 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2400 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2401 }
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002402 // On Android, libraries in the parent prefix of the GCC installation are
2403 // preferred to the ones under sysroot.
2404 if (IsAndroid) {
2405 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2406 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002407 }
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002408 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2409 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2410 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2411 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2412
2413 // Try walking via the GCC triple path in case of multiarch GCC
2414 // installations with strange symlinks.
2415 if (GCCInstallation.isValid())
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002416 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd0b93d62011-11-06 23:09:05 +00002417 "/../../" + Multilib, Paths);
Rafael Espindola30490212011-06-03 15:39:42 +00002418
Chandler Carrutha7b44142011-10-16 10:54:30 +00002419 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002420 if (GCCInstallation.isValid()) {
2421 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth4d9d7682012-01-24 19:28:29 +00002422 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth866faab2012-01-25 07:21:38 +00002423 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth3f0c8f72011-10-03 18:16:54 +00002424 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth69a125b2012-04-06 16:32:06 +00002425
2426 if (StringRef(LibPath).startswith(SysRoot)) {
2427 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2428 addPathIfExists(LibPath, Paths);
2429 }
Chandler Carruth413e5ac2011-10-03 05:28:29 +00002430 }
Chandler Carruth2a649c72011-10-03 06:41:08 +00002431 addPathIfExists(SysRoot + "/lib", Paths);
2432 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002433}
2434
2435bool Linux::HasNativeLLVMSupport() const {
2436 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00002437}
2438
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00002439Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2440 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002441 Action::ActionClass Key;
2442 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2443 Key = Action::AnalyzeJobClass;
2444 else
2445 Key = JA.getKind();
2446
Rafael Espindola96aef792010-11-07 23:13:01 +00002447 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2448 options::OPT_no_integrated_as,
2449 IsIntegratedAssemblerDefault());
2450
Rafael Espindola92b00932010-08-10 00:25:48 +00002451 Tool *&T = Tools[Key];
2452 if (!T) {
2453 switch (Key) {
2454 case Action::AssembleJobClass:
Rafael Espindola96aef792010-11-07 23:13:01 +00002455 if (UseIntegratedAs)
2456 T = new tools::ClangAs(*this);
2457 else
2458 T = new tools::linuxtools::Assemble(*this);
2459 break;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00002460 case Action::LinkJobClass:
2461 T = new tools::linuxtools::Link(*this); break;
Rafael Espindola92b00932010-08-10 00:25:48 +00002462 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00002463 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindola92b00932010-08-10 00:25:48 +00002464 }
2465 }
2466
2467 return *T;
2468}
2469
Chandler Carruth05fb5852012-11-21 23:40:23 +00002470void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2471 ArgStringList &CC1Args) const {
Rafael Espindola66aa0452012-06-19 01:26:10 +00002472 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carruth05fb5852012-11-21 23:40:23 +00002473 bool UseInitArrayDefault
2474 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
Tim Northover9bb857a2013-01-31 12:13:10 +00002475 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carruth05fb5852012-11-21 23:40:23 +00002476 getTriple().getEnvironment() == llvm::Triple::Android;
2477 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2478 options::OPT_fno_use_init_array,
2479 UseInitArrayDefault))
Rafael Espindola66aa0452012-06-19 01:26:10 +00002480 CC1Args.push_back("-fuse-init-array");
2481}
2482
Chandler Carrutha796f532011-11-05 20:17:13 +00002483void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2484 ArgStringList &CC1Args) const {
2485 const Driver &D = getDriver();
2486
2487 if (DriverArgs.hasArg(options::OPT_nostdinc))
2488 return;
2489
2490 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2491 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2492
2493 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carrutha796f532011-11-05 20:17:13 +00002494 llvm::sys::Path P(D.ResourceDir);
2495 P.appendComponent("include");
Chandler Carrutha62ba812011-11-07 09:17:31 +00002496 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carrutha796f532011-11-05 20:17:13 +00002497 }
2498
2499 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2500 return;
2501
2502 // Check for configure-time C include directories.
2503 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2504 if (CIncludeDirs != "") {
2505 SmallVector<StringRef, 5> dirs;
2506 CIncludeDirs.split(dirs, ":");
2507 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2508 I != E; ++I) {
2509 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2510 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2511 }
2512 return;
2513 }
2514
2515 // Lacking those, try to detect the correct set of system includes for the
2516 // target triple.
2517
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002518 // Implement generic Debian multiarch support.
2519 const StringRef X86_64MultiarchIncludeDirs[] = {
2520 "/usr/include/x86_64-linux-gnu",
2521
2522 // FIXME: These are older forms of multiarch. It's not clear that they're
2523 // in use in any released version of Debian, so we should consider
2524 // removing them.
2525 "/usr/include/i686-linux-gnu/64",
2526 "/usr/include/i486-linux-gnu/64"
2527 };
2528 const StringRef X86MultiarchIncludeDirs[] = {
2529 "/usr/include/i386-linux-gnu",
2530
2531 // FIXME: These are older forms of multiarch. It's not clear that they're
2532 // in use in any released version of Debian, so we should consider
2533 // removing them.
2534 "/usr/include/x86_64-linux-gnu/32",
2535 "/usr/include/i686-linux-gnu",
2536 "/usr/include/i486-linux-gnu"
2537 };
Tim Northover9bb857a2013-01-31 12:13:10 +00002538 const StringRef AArch64MultiarchIncludeDirs[] = {
2539 "/usr/include/aarch64-linux-gnu"
2540 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002541 const StringRef ARMMultiarchIncludeDirs[] = {
2542 "/usr/include/arm-linux-gnueabi"
2543 };
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002544 const StringRef ARMHFMultiarchIncludeDirs[] = {
2545 "/usr/include/arm-linux-gnueabihf"
2546 };
Eli Friedman7771c832011-11-11 03:05:19 +00002547 const StringRef MIPSMultiarchIncludeDirs[] = {
2548 "/usr/include/mips-linux-gnu"
2549 };
2550 const StringRef MIPSELMultiarchIncludeDirs[] = {
2551 "/usr/include/mipsel-linux-gnu"
2552 };
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002553 const StringRef PPCMultiarchIncludeDirs[] = {
2554 "/usr/include/powerpc-linux-gnu"
2555 };
2556 const StringRef PPC64MultiarchIncludeDirs[] = {
2557 "/usr/include/powerpc64-linux-gnu"
2558 };
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002559 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002560 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002561 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002562 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002563 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northover9bb857a2013-01-31 12:13:10 +00002564 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2565 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carrutha796f532011-11-05 20:17:13 +00002566 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liu61b06cb2012-07-31 08:06:29 +00002567 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2568 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2569 else
2570 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedman7771c832011-11-11 03:05:19 +00002571 } else if (getTriple().getArch() == llvm::Triple::mips) {
2572 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2573 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2574 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth2e9d7312012-02-26 09:21:43 +00002575 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2576 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2577 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2578 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002579 }
2580 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2581 E = MultiarchIncludeDirs.end();
2582 I != E; ++I) {
Chandler Carruth6e46ca22011-11-09 03:46:20 +00002583 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carruth5b77c6c2011-11-06 08:21:07 +00002584 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2585 break;
2586 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002587 }
2588
2589 if (getTriple().getOS() == llvm::Triple::RTEMS)
2590 return;
2591
Chandler Carruth475ab6a2011-11-08 17:19:47 +00002592 // Add an include of '/include' directly. This isn't provided by default by
2593 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2594 // add even when Clang is acting as-if it were a system compiler.
2595 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2596
Chandler Carrutha796f532011-11-05 20:17:13 +00002597 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2598}
2599
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002600/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth1fc603e2011-12-17 23:10:01 +00002601/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2602 const ArgList &DriverArgs,
2603 ArgStringList &CC1Args) {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002604 if (!llvm::sys::fs::exists(Base))
2605 return false;
Chandler Carrutha796f532011-11-05 20:17:13 +00002606 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002607 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carrutha796f532011-11-05 20:17:13 +00002608 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002609 return true;
Chandler Carrutha796f532011-11-05 20:17:13 +00002610}
2611
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002612/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2613/// libstdc++ installation.
2614/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2615 Twine TargetArchDir,
2616 Twine MultiLibSuffix,
2617 const ArgList &DriverArgs,
2618 ArgStringList &CC1Args) {
2619 if (!addLibStdCXXIncludePaths(Base+Suffix, TargetArchDir + MultiLibSuffix,
2620 DriverArgs, CC1Args))
2621 return false;
2622
2623 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
2624 + MultiLibSuffix);
2625 return true;
2626}
2627
Chandler Carrutha796f532011-11-05 20:17:13 +00002628void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2629 ArgStringList &CC1Args) const {
2630 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2631 DriverArgs.hasArg(options::OPT_nostdincxx))
2632 return;
2633
Chandler Carruthf4701732011-11-07 09:01:17 +00002634 // Check if libc++ has been enabled and provide its include paths if so.
2635 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2636 // libc++ is always installed at a fixed path on Linux currently.
2637 addSystemInclude(DriverArgs, CC1Args,
2638 getDriver().SysRoot + "/usr/include/c++/v1");
2639 return;
2640 }
2641
Chandler Carrutha1f1fd32012-01-25 08:04:13 +00002642 // We need a detected GCC installation on Linux to provide libstdc++'s
2643 // headers. We handled the libc++ case above.
2644 if (!GCCInstallation.isValid())
2645 return;
Chandler Carrutha796f532011-11-05 20:17:13 +00002646
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002647 // By default, look for the C++ headers in an include directory adjacent to
2648 // the lib directory of the GCC installation. Note that this is expect to be
2649 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2650 StringRef LibDir = GCCInstallation.getParentLibPath();
2651 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola66aa0452012-06-19 01:26:10 +00002652 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002653 StringRef TripleStr = GCCInstallation.getTriple().str();
2654
Dmitri Gribenko15225ae2013-03-06 17:14:05 +00002655 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2656 "/c++/" + Version.str(),
2657 TripleStr,
2658 GCCInstallation.getMultiarchSuffix(),
2659 DriverArgs, CC1Args))
2660 return;
2661
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002662 const std::string IncludePathCandidates[] = {
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002663 // Gentoo is weird and places its headers inside the GCC install, so if the
2664 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002665 InstallDir.str() + "/include/g++-v4",
2666 // Android standalone toolchain has C++ headers in yet another place.
2667 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkelf3587912012-09-18 22:25:07 +00002668 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2669 // without a subdirectory corresponding to the gcc version.
2670 LibDir.str() + "/../include/c++",
Evgeniy Stepanov763671e2012-09-03 09:05:50 +00002671 };
2672
2673 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2674 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2675 GCCInstallation.getMultiarchSuffix()),
2676 DriverArgs, CC1Args))
2677 break;
Chandler Carruthf5d4df92011-11-06 10:31:01 +00002678 }
Chandler Carrutha796f532011-11-05 20:17:13 +00002679}
2680
Daniel Dunbarcc912342009-05-02 18:28:39 +00002681/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2682
Rafael Espindola1af7c212012-02-19 01:38:32 +00002683DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2684 : Generic_ELF(D, Triple, Args) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00002685
2686 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00002687 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00002688 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00002689 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002690
Daniel Dunbar083edf72009-12-21 18:54:17 +00002691 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00002692 getFilePaths().push_back("/usr/lib");
2693 getFilePaths().push_back("/usr/lib/gcc41");
2694}
2695
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00002696Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2697 const ActionList &Inputs) const {
Nick Lewycky5cc9ebb2012-11-15 05:36:36 +00002698 Action::ActionClass Key;
2699 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2700 Key = Action::AnalyzeJobClass;
2701 else
2702 Key = JA.getKind();
Daniel Dunbarcc912342009-05-02 18:28:39 +00002703
2704 Tool *&T = Tools[Key];
2705 if (!T) {
2706 switch (Key) {
2707 case Action::AssembleJobClass:
2708 T = new tools::dragonfly::Assemble(*this); break;
2709 case Action::LinkJobClass:
2710 T = new tools::dragonfly::Link(*this); break;
2711 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00002712 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00002713 }
2714 }
2715
2716 return *T;
2717}