blob: 1c72c7f5253b938227411cae889eff2319f56273 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth00a3ccc2012-12-04 12:24:59 +000010// FIXME: This needs to be listed first until we fix the broken include guards
11// in these files and the LLVM config.h files.
12#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
13
Daniel Dunbar39176082009-03-20 00:20:03 +000014#include "ToolChains.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "SanitizerArgs.h"
16#include "clang/Basic/ObjCRuntime.h"
17#include "clang/Basic/Version.h"
Daniel Dunbarf3cad362009-03-25 04:13:45 +000018#include "clang/Driver/Arg.h"
19#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000020#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000021#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000022#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000023#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000024#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000025#include "clang/Driver/Options.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000026#include "llvm/ADT/STLExtras.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000027#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000028#include "llvm/ADT/StringExtras.h"
Bob Wilsona59956b2011-10-07 00:37:57 +000029#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000030#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000031#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000032#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000033#include "llvm/Support/Path.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000035#include "llvm/Support/system_error.h"
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000036#include <cstdlib> // ::getenv
37
Daniel Dunbar39176082009-03-20 00:20:03 +000038using namespace clang::driver;
39using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000040using namespace clang;
Daniel Dunbar39176082009-03-20 00:20:03 +000041
Daniel Dunbarf3955282009-09-04 18:34:51 +000042/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000043
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000044Darwin::Darwin(const Driver &D, const llvm::Triple& Triple)
John McCall260611a2012-06-20 06:18:46 +000045 : ToolChain(D, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000046{
Bob Wilson10853772012-01-31 21:30:03 +000047 // Compute the initial Darwin version from the triple
48 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-01-31 22:43:59 +000049 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
50 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
51 Triple.getOSName();
52 llvm::raw_string_ostream(MacosxVersionMin)
53 << Major << '.' << Minor << '.' << Micro;
54
Bob Wilson10853772012-01-31 21:30:03 +000055 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
56 // It should be removed when we stop supporting that.
57 DarwinVersion[0] = Minor + 4;
58 DarwinVersion[1] = Micro;
59 DarwinVersion[2] = 0;
Chad Rosierc793ea42012-05-09 18:46:30 +000060
61 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000062 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000063 llvm::raw_string_ostream(iOSVersionMin)
64 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000065}
66
Daniel Dunbar41800112010-08-02 05:43:56 +000067types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
68 types::ID Ty = types::lookupTypeForExtension(Ext);
69
70 // Darwin always preprocesses assembly files (unless -x is used explicitly).
71 if (Ty == types::TY_PP_Asm)
72 return types::TY_Asm;
73
74 return Ty;
75}
76
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000077bool Darwin::HasNativeLLVMSupport() const {
78 return true;
79}
80
John McCall9f084a32011-07-06 00:26:06 +000081/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall260611a2012-06-20 06:18:46 +000082ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
Bob Wilson377e5c12012-11-09 01:59:30 +000083 if (isTargetIPhoneOS())
John McCall260611a2012-06-20 06:18:46 +000084 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson377e5c12012-11-09 01:59:30 +000085 if (isNonFragile)
86 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
87 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall9f084a32011-07-06 00:26:06 +000088}
89
John McCall13db5cf2011-09-09 20:41:01 +000090/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
91bool Darwin::hasBlocksRuntime() const {
92 if (isTargetIPhoneOS())
93 return !isIPhoneOSVersionLT(3, 2);
94 else
95 return !isMacosxVersionLT(10, 6);
96}
97
Chris Lattner5f9e2722011-07-23 10:55:15 +000098static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +000099 return llvm::StringSwitch<const char*>(Value)
100 .Case("armv6k", "armv6")
101 .Case("armv5tej", "armv5")
102 .Case("xscale", "xscale")
103 .Case("armv4t", "armv4t")
104 .Case("armv7", "armv7")
105 .Cases("armv7a", "armv7-a", "armv7")
106 .Cases("armv7r", "armv7-r", "armv7")
107 .Cases("armv7m", "armv7-m", "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000108 .Cases("armv7f", "armv7-f", "armv7f")
109 .Cases("armv7k", "armv7-k", "armv7k")
110 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000111 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000112}
113
Chris Lattner5f9e2722011-07-23 10:55:15 +0000114static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000115 return llvm::StringSwitch<const char *>(Value)
116 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
117 .Cases("arm10e", "arm10tdmi", "armv5")
118 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
119 .Case("xscale", "xscale")
120 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s",
121 "arm1176jzf-s", "cortex-m0", "armv6")
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000122 .Cases("cortex-a8", "cortex-r4", "cortex-m3", "cortex-a9", "cortex-a15",
123 "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000124 .Case("cortex-a9-mp", "armv7f")
125 .Case("swift", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000126 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000127}
128
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000130 switch (getTriple().getArch()) {
131 default:
132 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000133
Douglas Gregorf0594d82011-03-06 19:11:49 +0000134 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000135 case llvm::Triple::arm: {
136 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000137 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000138 return Arch;
139
140 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000141 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000142 return Arch;
143
144 return "arm";
145 }
146 }
147}
148
Daniel Dunbarf3955282009-09-04 18:34:51 +0000149Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000150 // Free tool implementations.
151 for (llvm::DenseMap<unsigned, Tool*>::iterator
152 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
153 delete it->second;
154}
155
Chad Rosier61ab80a2011-09-20 20:44:06 +0000156std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
157 types::ID InputType) const {
158 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000159
160 // If the target isn't initialized (e.g., an unknown Darwin platform, return
161 // the default triple).
162 if (!isTargetInitialized())
163 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000164
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000165 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000166 Str += isTargetIPhoneOS() ? "ios" : "macosx";
167 Str += getTargetVersion().getAsString();
168 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000169
170 return Triple.getTriple();
171}
172
David Blaikie99ba9e32011-12-20 02:48:34 +0000173void Generic_ELF::anchor() {}
174
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000175Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
176 const ActionList &Inputs) const {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000177 Action::ActionClass Key = JA.getKind();
Nick Lewycky5bab9ae2012-11-15 05:36:36 +0000178
179 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
180 // FIXME: This seems like a hacky way to choose clang frontend.
181 Key = Action::AnalyzeJobClass;
182 }
183
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000184 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
185 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000186 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000187
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000188 Tool *&T = Tools[Key];
189 if (!T) {
190 switch (Key) {
191 case Action::InputClass:
192 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000193 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000194 case Action::PreprocessJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000195 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000196 case Action::MigrateJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000197 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000198 case Action::CompileJobClass:
Bob Wilson66b8a662012-11-23 06:14:39 +0000199 T = new tools::Clang(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000200 case Action::AssembleJobClass: {
201 if (UseIntegratedAs)
202 T = new tools::ClangAs(*this);
203 else
204 T = new tools::darwin::Assemble(*this);
205 break;
206 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000207 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000208 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000209 case Action::LipoJobClass:
210 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000211 case Action::DsymutilJobClass:
212 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000213 case Action::VerifyJobClass:
214 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000215 }
216 }
217
218 return *T;
219}
220
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000221
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000222DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
223 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000224{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000225 getProgramPaths().push_back(getDriver().getInstalledDir());
226 if (getDriver().getInstalledDir() != getDriver().Dir)
227 getProgramPaths().push_back(getDriver().Dir);
228
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000229 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000230 getProgramPaths().push_back(getDriver().getInstalledDir());
231 if (getDriver().getInstalledDir() != getDriver().Dir)
232 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000233}
234
John McCallf85e1932011-06-15 23:02:42 +0000235void DarwinClang::AddLinkARCArgs(const ArgList &Args,
236 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000237
238 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000239 llvm::sys::Path P(getDriver().ClangExecutable);
240 P.eraseComponent(); // 'clang'
241 P.eraseComponent(); // 'bin'
242 P.appendComponent("lib");
243 P.appendComponent("arc");
244 P.appendComponent("libarclite_");
245 std::string s = P.str();
246 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000247 if (isTargetIOSSimulator())
248 s += "iphonesimulator";
249 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000250 s += "iphoneos";
John McCallf85e1932011-06-15 23:02:42 +0000251 else
252 s += "macosx";
253 s += ".a";
254
255 CmdArgs.push_back(Args.MakeArgString(s));
256}
257
Eric Christopher3404fe72011-06-22 17:41:40 +0000258void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000259 ArgStringList &CmdArgs,
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000260 const char *DarwinStaticLib,
261 bool AlwaysLink) const {
Eric Christopher3404fe72011-06-22 17:41:40 +0000262 llvm::sys::Path P(getDriver().ResourceDir);
263 P.appendComponent("lib");
264 P.appendComponent("darwin");
265 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000266
Eric Christopher3404fe72011-06-22 17:41:40 +0000267 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000268 // not have compiler-rt checked out or integrated into their build (unless
269 // we explicitly force linking with this library).
Eric Christopher3404fe72011-06-22 17:41:40 +0000270 bool Exists;
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000271 if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
Eric Christopher3404fe72011-06-22 17:41:40 +0000272 CmdArgs.push_back(Args.MakeArgString(P.str()));
273}
274
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000275void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
276 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000277 // Darwin only supports the compiler-rt based runtime libraries.
278 switch (GetRuntimeLibType(Args)) {
279 case ToolChain::RLT_CompilerRT:
280 break;
281 default:
282 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000283 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000284 return;
285 }
286
Daniel Dunbareec99102010-01-22 03:38:14 +0000287 // Darwin doesn't support real static executables, don't link any runtime
288 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000289 if (Args.hasArg(options::OPT_static) ||
290 Args.hasArg(options::OPT_fapple_kext) ||
291 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000292 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000293
294 // Reject -static-libgcc for now, we can deal with this when and if someone
295 // cares. This is useful in situations where someone wants to statically link
296 // something like libstdc++, and needs its runtime support routines.
297 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000298 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000299 << A->getAsString(Args);
300 return;
301 }
302
Daniel Dunbarf4714872011-11-17 00:36:57 +0000303 // If we are building profile support, link that library in.
304 if (Args.hasArg(options::OPT_fprofile_arcs) ||
305 Args.hasArg(options::OPT_fprofile_generate) ||
306 Args.hasArg(options::OPT_fcreate_profile) ||
307 Args.hasArg(options::OPT_coverage)) {
308 // Select the appropriate runtime library for the target.
309 if (isTargetIPhoneOS()) {
310 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
311 } else {
312 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
313 }
314 }
315
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000316 SanitizerArgs Sanitize(getDriver(), Args);
317
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000318 // Add Ubsan runtime library, if required.
319 if (Sanitize.needsUbsanRt()) {
320 if (Args.hasArg(options::OPT_dynamiclib) ||
321 Args.hasArg(options::OPT_bundle)) {
322 // Assume the binary will provide the Ubsan runtime.
323 } else if (isTargetIPhoneOS()) {
324 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
325 << "-fsanitize=undefined";
326 } else {
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000327 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000328
329 // The Ubsan runtime library requires C++.
330 AddCXXStdlibLibArgs(Args, CmdArgs);
331 }
332 }
333
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000334 // Add ASAN runtime library, if required. Dynamic libraries and bundles
335 // should not be linked with the runtime library.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000336 if (Sanitize.needsAsanRt()) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000337 if (Args.hasArg(options::OPT_dynamiclib) ||
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000338 Args.hasArg(options::OPT_bundle)) {
339 // Assume the binary will provide the ASan runtime.
340 } else if (isTargetIPhoneOS()) {
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000341 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000342 << "-fsanitize=address";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000343 } else {
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000344 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a", true);
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000345
346 // The ASAN runtime library requires C++ and CoreFoundation.
347 AddCXXStdlibLibArgs(Args, CmdArgs);
348 CmdArgs.push_back("-framework");
349 CmdArgs.push_back("CoreFoundation");
350 }
351 }
352
Daniel Dunbareec99102010-01-22 03:38:14 +0000353 // Otherwise link libSystem, then the dynamic runtime library, and finally any
354 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000355 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000356
357 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000358 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000359 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
360 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000361 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
362 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
363 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000364
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000365 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000366 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000367 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000368 // The dynamic runtime library was merged with libSystem for 10.6 and
369 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000370 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000371 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000372 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000373 CmdArgs.push_back("-lgcc_s.10.5");
374
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000375 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000376 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000377 // omitted from 10.4.dylib.
378 //
379 // Unfortunately, that turned out to not be true, because Darwin system
380 // headers can still use eprintf on i386, and it is not exported from
381 // libSystem. Therefore, we still must provide a runtime library just for
382 // the tiny tiny handful of projects that *might* use that symbol.
383 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000384 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000385 } else {
386 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000387 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
388 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000389 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000390 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000391}
392
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000393void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000394 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000395
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000396 // Support allowing the SDKROOT environment variable used by xcrun and other
397 // Xcode tools to define the default sysroot, by making it the default for
398 // isysroot.
399 if (!Args.hasArg(options::OPT_isysroot)) {
400 if (char *env = ::getenv("SDKROOT")) {
401 // We only use this value as the default if it is an absolute path and
402 // exists.
403 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env)) {
404 Args.append(Args.MakeSeparateArg(
405 0, Opts.getOption(options::OPT_isysroot), env));
406 }
407 }
408 }
409
Daniel Dunbar26031372010-01-27 00:56:25 +0000410 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000411 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
412 Arg *iOSSimVersion = Args.getLastArg(
413 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000414
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000415 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000416 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000417 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000418 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
419 iOSVersion = iOSSimVersion = 0;
420 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000421 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000422 << iOSVersion->getAsString(Args)
423 << iOSSimVersion->getAsString(Args);
424 iOSSimVersion = 0;
425 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000426 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000427 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000428 StringRef OSXTarget;
429 StringRef iOSTarget;
430 StringRef iOSSimTarget;
431 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
432 OSXTarget = env;
433 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
434 iOSTarget = env;
435 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
436 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000437
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000438 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000439 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000440 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000441 if (iOSTarget.empty()) {
442 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
443 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000444 StringRef isysroot = A->getValue();
Chad Rosiera4884972011-08-31 20:56:25 +0000445 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
446 if (second != "")
447 iOSTarget = second.substr(0,3);
448 }
449 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000450
Chad Rosier4f8de272011-09-28 00:46:32 +0000451 // If no OSX or iOS target has been specified and we're compiling for armv7,
452 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000453 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000454 (getDarwinArchName(Args) == "armv7" ||
455 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000456 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000457
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000458 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000459 //
460 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000461
462 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000463 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000464 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000465 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000466 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000467 "IPHONEOS_DEPLOYMENT_TARGET");
468 }
469
470 // Allow conflicts among OSX and iOS for historical reasons, but choose the
471 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000472 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000473 if (getTriple().getArch() == llvm::Triple::arm ||
474 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000475 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000476 else
Chad Rosiera4884972011-08-31 20:56:25 +0000477 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000478 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000479
Chad Rosiera4884972011-08-31 20:56:25 +0000480 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000481 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000482 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
483 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000484 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000485 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000486 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
487 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000488 } else if (!iOSSimTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000489 const Option O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000490 options::OPT_mios_simulator_version_min_EQ);
491 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
492 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000493 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000494 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000495 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000496 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
497 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000498 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000501 // Reject invalid architecture combinations.
502 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
503 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000504 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000505 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
506 }
507
Daniel Dunbar26031372010-01-27 00:56:25 +0000508 // Set the tool chain target information.
509 unsigned Major, Minor, Micro;
510 bool HadExtra;
511 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000512 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000513 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000514 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000515 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000516 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000517 << OSXVersion->getAsString(Args);
518 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000519 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
520 assert(Version && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000521 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000522 Micro, HadExtra) || HadExtra ||
523 Major >= 10 || Minor >= 100 || Micro >= 100)
524 getDriver().Diag(diag::err_drv_invalid_version_number)
525 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000526 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000527
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000528 bool IsIOSSim = bool(iOSSimVersion);
529
530 // In GCC, the simulator historically was treated as being OS X in some
531 // contexts, like determining the link logic, despite generally being called
532 // with an iOS deployment target. For compatibility, we detect the
533 // simulator as iOS + x86, and treat it differently in a few contexts.
534 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
535 getTriple().getArch() == llvm::Triple::x86_64))
536 IsIOSSim = true;
537
538 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000539}
540
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000541void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000542 ArgStringList &CmdArgs) const {
543 CXXStdlibType Type = GetCXXStdlibType(Args);
544
545 switch (Type) {
546 case ToolChain::CST_Libcxx:
547 CmdArgs.push_back("-lc++");
548 break;
549
550 case ToolChain::CST_Libstdcxx: {
551 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
552 // it was previously found in the gcc lib dir. However, for all the Darwin
553 // platforms we care about it was -lstdc++.6, so we search for that
554 // explicitly if we can't see an obvious -lstdc++ candidate.
555
556 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000557 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000558 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000559 llvm::sys::Path P(A->getValue());
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000560 P.appendComponent("usr");
561 P.appendComponent("lib");
562 P.appendComponent("libstdc++.dylib");
563
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000564 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000565 P.eraseComponent();
566 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000567 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000568 CmdArgs.push_back(Args.MakeArgString(P.str()));
569 return;
570 }
571 }
572 }
573
574 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000575 // FIXME: This should be removed someday when we don't have to care about
576 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000577 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
578 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000579 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
580 return;
581 }
582
583 // Otherwise, let the linker search.
584 CmdArgs.push_back("-lstdc++");
585 break;
586 }
587 }
588}
589
Shantonu Sen7433fed2010-09-17 18:39:08 +0000590void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
591 ArgStringList &CmdArgs) const {
592
593 // For Darwin platforms, use the compiler-rt-based support library
594 // instead of the gcc-provided one (which is also incidentally
595 // only present in the gcc lib dir, which makes it hard to find).
596
597 llvm::sys::Path P(getDriver().ResourceDir);
598 P.appendComponent("lib");
599 P.appendComponent("darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000600
601 // Use the newer cc_kext for iOS ARM after 6.0.
602 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
603 !isIPhoneOSVersionLT(6, 0)) {
604 P.appendComponent("libclang_rt.cc_kext.a");
605 } else {
606 P.appendComponent("libclang_rt.cc_kext_ios5.a");
607 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000608
Shantonu Sen7433fed2010-09-17 18:39:08 +0000609 // For now, allow missing resource libraries to support developers who may
610 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000611 bool Exists;
612 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000613 CmdArgs.push_back(Args.MakeArgString(P.str()));
614}
615
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000616DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
617 const char *BoundArch) const {
618 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
619 const OptTable &Opts = getDriver().getOpts();
620
621 // FIXME: We really want to get out of the tool chain level argument
622 // translation business, as it makes the driver functionality much
623 // more opaque. For now, we follow gcc closely solely for the
624 // purpose of easily achieving feature parity & testability. Once we
625 // have something that works, we should reevaluate each translation
626 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000627
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000628 for (ArgList::const_iterator it = Args.begin(),
629 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000630 Arg *A = *it;
631
632 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000633 // Skip this argument unless the architecture matches either the toolchain
634 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000635 llvm::Triple::ArchType XarchArch =
Richard Smith1d489cf2012-11-01 04:30:05 +0000636 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000637 if (!(XarchArch == getArch() ||
638 (BoundArch && XarchArch ==
Rafael Espindolacfed8282012-10-31 18:51:07 +0000639 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000640 continue;
641
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000642 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000643 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000644 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000645 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000647 // If the argument parsing failed or more than one argument was
648 // consumed, the -Xarch_ argument's parameter tried to consume
649 // extra arguments. Emit an error and ignore.
650 //
651 // We also want to disallow any options which would alter the
652 // driver behavior; that isn't going to work in our model. We
653 // use isDriverOption() as an approximation, although things
654 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000655 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000656 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000657 << A->getAsString(Args);
658 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000659 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000660 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000661 << A->getAsString(Args);
662 continue;
663 }
664
Daniel Dunbar478edc22009-03-29 22:29:05 +0000665 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000666 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000667
668 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000669
670 // Linker input arguments require custom handling. The problem is that we
671 // have already constructed the phase actions, so we can not treat them as
672 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000673 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000674 // Convert the argument into individual Zlinker_input_args.
675 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
676 DAL->AddSeparateArg(OriginalArg,
677 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000678 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000679
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000680 }
681 continue;
682 }
Mike Stump1eb44332009-09-09 15:08:12 +0000683 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000684
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000685 // Sob. These is strictly gcc compatible for the time being. Apple
686 // gcc translates options twice, which means that self-expanding
687 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000688 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000689 default:
690 DAL->append(A);
691 break;
692
693 case options::OPT_mkernel:
694 case options::OPT_fapple_kext:
695 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000696 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000697 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000699 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000700 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000701 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000702 break;
703
704 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000705 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
706 DAL->AddFlagArg(A,
707 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000708 break;
709
710 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000711 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
712 DAL->AddFlagArg(A,
713 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000714 break;
715
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000716 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000717 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000718 break;
719
720 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000721 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000722 break;
723
724 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000725 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000726 break;
727
728 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000729 DAL->AddFlagArg(A,
730 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000731 break;
732
733 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000734 DAL->AddFlagArg(A,
735 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000736 break;
737
738 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000739 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000740 break;
741
742 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000743 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000744 break;
745 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000746 }
747
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000748 if (getTriple().getArch() == llvm::Triple::x86 ||
749 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000750 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000751 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000752
753 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000754 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000755 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000756 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000757 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
758 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000759
760 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
761 // which defines the list of which architectures we accept.
762 if (Name == "ppc")
763 ;
764 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000765 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000766 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000767 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000768 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000769 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000770 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000771 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000772 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000773 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000774 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000775 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000776 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000777 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000778 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000779 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000780
781 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000782 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000783
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000784 else if (Name == "i386")
785 ;
786 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000787 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000788 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000789 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000790 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000791 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000792 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000793 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000794 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000795 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000796 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000797 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000798 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000799 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000800
801 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000802 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000803
804 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000805 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000806 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000807 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000808 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000809 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000810 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000811 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000812 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000813 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000814 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000815 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson336bfa32012-09-29 23:52:50 +0000816 else if (Name == "armv7f")
817 DAL->AddJoinedArg(0, MArch, "armv7f");
818 else if (Name == "armv7k")
819 DAL->AddJoinedArg(0, MArch, "armv7k");
820 else if (Name == "armv7s")
821 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000822
823 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000824 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000825 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000826
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000827 // Add an explicit version min argument for the deployment target. We do this
828 // after argument translation because -Xarch_ arguments may add a version min
829 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000830 if (BoundArch)
831 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000832
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000833 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
834 // FIXME: It would be far better to avoid inserting those -static arguments,
835 // but we can't check the deployment target in the translation code until
836 // it is set here.
837 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
838 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
839 Arg *A = *it;
840 ++it;
841 if (A->getOption().getID() != options::OPT_mkernel &&
842 A->getOption().getID() != options::OPT_fapple_kext)
843 continue;
844 assert(it != ie && "unexpected argument translation");
845 A = *it;
846 assert(A->getOption().getID() == options::OPT_static &&
847 "missing expected -static argument");
848 it = DAL->getArgs().erase(it);
849 }
850 }
851
Bob Wilson163b1512011-10-07 17:54:41 +0000852 // Validate the C++ standard library choice.
853 CXXStdlibType Type = GetCXXStdlibType(*DAL);
854 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000855 // Check whether the target provides libc++.
856 StringRef where;
857
858 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson377e5c12012-11-09 01:59:30 +0000859 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
860 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000861
862 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000863 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000864 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000865 }
866 }
867
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000868 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000869}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000870
Daniel Dunbarf3955282009-09-04 18:34:51 +0000871bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000872 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000873}
874
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000875bool Darwin::UseDwarfDebugFlags() const {
876 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
877 return S[0] != '\0';
878 return false;
879}
880
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000881bool Darwin::UseSjLjExceptions() const {
882 // Darwin uses SjLj exceptions on ARM.
883 return (getTriple().getArch() == llvm::Triple::arm ||
884 getTriple().getArch() == llvm::Triple::thumb);
885}
886
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000887bool Darwin::isPICDefault() const {
888 return true;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000889}
890
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000891bool Darwin::isPICDefaultForced() const {
892 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000893}
894
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000895bool Darwin::SupportsProfiling() const {
896 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000897 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000898}
899
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000900bool Darwin::SupportsObjCGC() const {
901 // Garbage collection is supported everywhere except on iPhone OS.
902 return !isTargetIPhoneOS();
903}
904
John McCall0a7dd782012-08-21 02:47:43 +0000905void Darwin::CheckObjCARC() const {
906 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
907 return;
John McCall80fd37a2012-08-27 01:56:21 +0000908 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000909}
910
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000911std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000912Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
913 types::ID InputType) const {
914 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000915}
916
Daniel Dunbar39176082009-03-20 00:20:03 +0000917/// Generic_GCC - A tool chain using the 'gcc' command to perform
918/// all subcommands; this relies on gcc translating the majority of
919/// command line options.
920
Chandler Carruth19347ed2011-11-06 23:39:34 +0000921/// \brief Parse a GCCVersion object out of a string of text.
922///
923/// This is the primary means of forming GCCVersion objects.
924/*static*/
925Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
926 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
927 std::pair<StringRef, StringRef> First = VersionText.split('.');
928 std::pair<StringRef, StringRef> Second = First.second.split('.');
929
930 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
931 if (First.first.getAsInteger(10, GoodVersion.Major) ||
932 GoodVersion.Major < 0)
933 return BadVersion;
934 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
935 GoodVersion.Minor < 0)
936 return BadVersion;
937
938 // First look for a number prefix and parse that if present. Otherwise just
939 // stash the entire patch string in the suffix, and leave the number
940 // unspecified. This covers versions strings such as:
941 // 4.4
942 // 4.4.0
943 // 4.4.x
944 // 4.4.2-rc4
945 // 4.4.x-patched
946 // And retains any patch number it finds.
947 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
948 if (!PatchText.empty()) {
949 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
950 // Try to parse the number and any suffix.
951 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
952 GoodVersion.Patch < 0)
953 return BadVersion;
954 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
955 }
956 }
957
958 return GoodVersion;
959}
960
961/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
962bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
963 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
964 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
965
966 // Note that we rank versions with *no* patch specified is better than ones
967 // hard-coding a patch version. Thus if the RHS has no patch, it always
968 // wins, and the LHS only wins if it has no patch and the RHS does have
969 // a patch.
970 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
971 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
Gabor Greif241cbe42012-04-18 10:59:08 +0000972 if (PatchSuffix == RHS.PatchSuffix) return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000973
974 // Finally, between completely tied version numbers, the version with the
975 // suffix loses as we prefer full releases.
976 if (RHS.PatchSuffix.empty()) return true;
977 return false;
978}
979
Rafael Espindola0e659592012-02-19 01:38:32 +0000980static StringRef getGCCToolchainDir(const ArgList &Args) {
981 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
982 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +0000983 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +0000984 return GCC_INSTALL_PREFIX;
985}
986
Chandler Carruth19347ed2011-11-06 23:39:34 +0000987/// \brief Construct a GCCInstallationDetector from the driver.
988///
989/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +0000990/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +0000991///
992/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
993/// should instead pull the target out of the driver. This is currently
994/// necessary because the driver doesn't store the final version of the target
995/// triple.
996Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
997 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +0000998 const llvm::Triple &TargetTriple,
999 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001000 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001001 llvm::Triple MultiarchTriple
1002 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1003 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001004 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001005 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001006 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001007 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001008 SmallVector<StringRef, 10> CandidateTripleAliases;
1009 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1010 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1011 CandidateTripleAliases,
1012 CandidateMultiarchLibDirs,
1013 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001014
1015 // Compute the set of prefixes for our search.
1016 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1017 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001018
Rafael Espindola0e659592012-02-19 01:38:32 +00001019 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1020 if (GCCToolchainDir != "") {
1021 if (GCCToolchainDir.back() == '/')
1022 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001023
Rafael Espindola0e659592012-02-19 01:38:32 +00001024 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001025 } else {
1026 Prefixes.push_back(D.SysRoot);
1027 Prefixes.push_back(D.SysRoot + "/usr");
1028 Prefixes.push_back(D.InstalledDir + "/..");
1029 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001030
1031 // Loop over the various components which exist and select the best GCC
1032 // installation available. GCC installs are ranked by version number.
1033 Version = GCCVersion::Parse("0.0.0");
1034 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1035 if (!llvm::sys::fs::exists(Prefixes[i]))
1036 continue;
1037 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1038 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1039 if (!llvm::sys::fs::exists(LibDir))
1040 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001041 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001042 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1043 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001044 }
1045 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1046 const std::string LibDir
1047 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1048 if (!llvm::sys::fs::exists(LibDir))
1049 continue;
1050 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1051 ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001052 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001053 CandidateMultiarchTripleAliases[k],
1054 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001055 }
1056 }
1057}
1058
1059/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001060 const llvm::Triple &TargetTriple,
1061 const llvm::Triple &MultiarchTriple,
1062 SmallVectorImpl<StringRef> &LibDirs,
1063 SmallVectorImpl<StringRef> &TripleAliases,
1064 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1065 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1066 // Declare a bunch of static data sets that we'll select between below. These
1067 // are specifically designed to always refer to string literals to avoid any
1068 // lifetime or initialization issues.
1069 static const char *const ARMLibDirs[] = { "/lib" };
1070 static const char *const ARMTriples[] = {
1071 "arm-linux-gnueabi",
1072 "arm-linux-androideabi"
1073 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001074 static const char *const ARMHFTriples[] = {
1075 "arm-linux-gnueabihf",
1076 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001077
1078 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1079 static const char *const X86_64Triples[] = {
1080 "x86_64-linux-gnu",
1081 "x86_64-unknown-linux-gnu",
1082 "x86_64-pc-linux-gnu",
1083 "x86_64-redhat-linux6E",
1084 "x86_64-redhat-linux",
1085 "x86_64-suse-linux",
1086 "x86_64-manbo-linux-gnu",
1087 "x86_64-linux-gnu",
1088 "x86_64-slackware-linux"
1089 };
1090 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1091 static const char *const X86Triples[] = {
1092 "i686-linux-gnu",
1093 "i686-pc-linux-gnu",
1094 "i486-linux-gnu",
1095 "i386-linux-gnu",
1096 "i686-redhat-linux",
1097 "i586-redhat-linux",
1098 "i386-redhat-linux",
1099 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001100 "i486-slackware-linux",
1101 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001102 };
1103
1104 static const char *const MIPSLibDirs[] = { "/lib" };
1105 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1106 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001107 static const char *const MIPSELTriples[] = {
1108 "mipsel-linux-gnu",
1109 "mipsel-linux-android"
1110 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001111
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001112 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1113 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1114 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1115 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1116
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001117 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1118 static const char *const PPCTriples[] = {
1119 "powerpc-linux-gnu",
1120 "powerpc-unknown-linux-gnu",
Gabor Greif91720912012-05-15 11:21:03 +00001121 "powerpc-suse-linux",
1122 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001123 };
1124 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1125 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001126 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001127 "powerpc64-unknown-linux-gnu",
1128 "powerpc64-suse-linux",
1129 "ppc64-redhat-linux"
1130 };
1131
1132 switch (TargetTriple.getArch()) {
1133 case llvm::Triple::arm:
1134 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001135 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001136 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1137 TripleAliases.append(
1138 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1139 } else {
1140 TripleAliases.append(
1141 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1142 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001143 break;
1144 case llvm::Triple::x86_64:
1145 LibDirs.append(
1146 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1147 TripleAliases.append(
1148 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1149 MultiarchLibDirs.append(
1150 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1151 MultiarchTripleAliases.append(
1152 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1153 break;
1154 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001155 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001156 TripleAliases.append(
1157 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1158 MultiarchLibDirs.append(
1159 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1160 MultiarchTripleAliases.append(
1161 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1162 break;
1163 case llvm::Triple::mips:
1164 LibDirs.append(
1165 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1166 TripleAliases.append(
1167 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001168 MultiarchLibDirs.append(
1169 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1170 MultiarchTripleAliases.append(
1171 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001172 break;
1173 case llvm::Triple::mipsel:
1174 LibDirs.append(
1175 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1176 TripleAliases.append(
1177 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001178 MultiarchLibDirs.append(
1179 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1180 MultiarchTripleAliases.append(
1181 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1182 break;
1183 case llvm::Triple::mips64:
1184 LibDirs.append(
1185 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1186 TripleAliases.append(
1187 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1188 MultiarchLibDirs.append(
1189 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1190 MultiarchTripleAliases.append(
1191 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1192 break;
1193 case llvm::Triple::mips64el:
1194 LibDirs.append(
1195 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1196 TripleAliases.append(
1197 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1198 MultiarchLibDirs.append(
1199 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1200 MultiarchTripleAliases.append(
1201 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001202 break;
1203 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001204 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001205 TripleAliases.append(
1206 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1207 MultiarchLibDirs.append(
1208 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1209 MultiarchTripleAliases.append(
1210 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1211 break;
1212 case llvm::Triple::ppc64:
1213 LibDirs.append(
1214 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1215 TripleAliases.append(
1216 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1217 MultiarchLibDirs.append(
1218 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1219 MultiarchTripleAliases.append(
1220 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1221 break;
1222
1223 default:
1224 // By default, just rely on the standard lib directories and the original
1225 // triple.
1226 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001227 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001228
1229 // Always append the drivers target triple to the end, in case it doesn't
1230 // match any of our aliases.
1231 TripleAliases.push_back(TargetTriple.str());
1232
1233 // Also include the multiarch variant if it's different.
1234 if (TargetTriple.str() != MultiarchTriple.str())
1235 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001236}
1237
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001238// FIXME: There is the same routine in the Tools.cpp.
1239static bool hasMipsN32ABIArg(const ArgList &Args) {
1240 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00001241 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001242}
1243
1244static StringRef getTargetMultiarchSuffix(llvm::Triple::ArchType TargetArch,
1245 const ArgList &Args) {
1246 if (TargetArch == llvm::Triple::x86_64 ||
1247 TargetArch == llvm::Triple::ppc64)
1248 return "/64";
1249
1250 if (TargetArch == llvm::Triple::mips64 ||
1251 TargetArch == llvm::Triple::mips64el) {
1252 if (hasMipsN32ABIArg(Args))
1253 return "/n32";
1254 else
1255 return "/64";
1256 }
1257
1258 return "/32";
1259}
1260
Chandler Carruth19347ed2011-11-06 23:39:34 +00001261void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001262 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1263 const std::string &LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001264 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001265 // There are various different suffixes involving the triple we
1266 // check for. We also record what is necessary to walk from each back
1267 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001268 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001269 "/gcc/" + CandidateTriple.str(),
1270 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1271
Hal Finkel02014b42012-09-18 22:25:07 +00001272 // The Freescale PPC SDK has the gcc libraries in
1273 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1274 "/" + CandidateTriple.str(),
1275
Chandler Carruth19347ed2011-11-06 23:39:34 +00001276 // Ubuntu has a strange mis-matched pair of triples that this happens to
1277 // match.
1278 // FIXME: It may be worthwhile to generalize this and look for a second
1279 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001280 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001281 };
1282 const std::string InstallSuffixes[] = {
1283 "/../../..",
1284 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001285 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001286 "/../../../.."
1287 };
1288 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001289 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1290 (TargetArch != llvm::Triple::x86));
1291 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1292 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001293 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001294 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001295 !EC && LI != LE; LI = LI.increment(EC)) {
1296 StringRef VersionText = llvm::sys::path::filename(LI->path());
1297 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1298 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1299 if (CandidateVersion < MinVersion)
1300 continue;
1301 if (CandidateVersion <= Version)
1302 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001303
1304 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001305 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001306 // libs in a subdirectory named 64. The simple logic we follow is that
1307 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1308 // we use that. If not, and if not a multiarch triple, we look for
1309 // crtbegin.o without the subdirectory.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001310 StringRef MultiarchSuffix = getTargetMultiarchSuffix(TargetArch, Args);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001311 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1312 GCCMultiarchSuffix = MultiarchSuffix.str();
1313 } else {
1314 if (NeedsMultiarchSuffix ||
1315 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1316 continue;
1317 GCCMultiarchSuffix.clear();
1318 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001319
1320 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001321 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001322 // FIXME: We hack together the directory name here instead of
1323 // using LI to ensure stable path separators across Windows and
1324 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001325 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001326 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001327 IsValid = true;
1328 }
1329 }
1330}
1331
Rafael Espindola0e659592012-02-19 01:38:32 +00001332Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1333 const ArgList &Args)
1334 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001335 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001336 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001337 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001338}
1339
Daniel Dunbar39176082009-03-20 00:20:03 +00001340Generic_GCC::~Generic_GCC() {
1341 // Free tool implementations.
1342 for (llvm::DenseMap<unsigned, Tool*>::iterator
1343 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1344 delete it->second;
1345}
1346
Mike Stump1eb44332009-09-09 15:08:12 +00001347Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001348 const JobAction &JA,
1349 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001350 Action::ActionClass Key;
1351 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1352 Key = Action::AnalyzeJobClass;
1353 else
1354 Key = JA.getKind();
Daniel Dunbar39176082009-03-20 00:20:03 +00001355
1356 Tool *&T = Tools[Key];
1357 if (!T) {
1358 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001359 case Action::InputClass:
1360 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001361 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001362 case Action::PreprocessJobClass:
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001363 T = new tools::gcc::Preprocess(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001364 case Action::PrecompileJobClass:
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001365 T = new tools::gcc::Precompile(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001366 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001367 case Action::MigrateJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001368 T = new tools::Clang(*this); break;
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001369 case Action::CompileJobClass:
1370 T = new tools::gcc::Compile(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001371 case Action::AssembleJobClass:
1372 T = new tools::gcc::Assemble(*this); break;
1373 case Action::LinkJobClass:
1374 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001376 // This is a bit ungeneric, but the only platform using a driver
1377 // driver is Darwin.
1378 case Action::LipoJobClass:
1379 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001380 case Action::DsymutilJobClass:
1381 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001382 case Action::VerifyJobClass:
1383 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001384 }
1385 }
1386
1387 return *T;
1388}
1389
Daniel Dunbar39176082009-03-20 00:20:03 +00001390bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00001391 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00001392}
1393
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001394bool Generic_GCC::isPICDefault() const {
1395 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00001396}
1397
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001398bool Generic_GCC::isPICDefaultForced() const {
1399 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00001400}
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001401
Tony Linthicum96319392011-12-12 21:14:55 +00001402/// Hexagon Toolchain
1403
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001404Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple& Triple)
1405 : ToolChain(D, Triple) {
Tony Linthicum96319392011-12-12 21:14:55 +00001406 getProgramPaths().push_back(getDriver().getInstalledDir());
1407 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1408 getProgramPaths().push_back(getDriver().Dir);
1409}
1410
1411Hexagon_TC::~Hexagon_TC() {
1412 // Free tool implementations.
1413 for (llvm::DenseMap<unsigned, Tool*>::iterator
1414 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1415 delete it->second;
1416}
1417
1418Tool &Hexagon_TC::SelectTool(const Compilation &C,
1419 const JobAction &JA,
1420 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001421 Action::ActionClass Key;
1422 // if (JA.getKind () == Action::CompileJobClass)
1423 // Key = JA.getKind ();
1424 // else
1425
1426 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1427 Key = Action::AnalyzeJobClass;
1428 else
1429 Key = JA.getKind();
1430 // if ((JA.getKind () == Action::CompileJobClass)
1431 // && (JA.getType () != types::TY_LTO_BC)) {
1432 // Key = JA.getKind ();
1433 // }
1434
Tony Linthicum96319392011-12-12 21:14:55 +00001435 Tool *&T = Tools[Key];
1436 if (!T) {
1437 switch (Key) {
1438 case Action::InputClass:
1439 case Action::BindArchClass:
1440 assert(0 && "Invalid tool kind.");
1441 case Action::AnalyzeJobClass:
1442 T = new tools::Clang(*this); break;
1443 case Action::AssembleJobClass:
1444 T = new tools::hexagon::Assemble(*this); break;
1445 case Action::LinkJobClass:
1446 T = new tools::hexagon::Link(*this); break;
1447 default:
1448 assert(false && "Unsupported action for Hexagon target.");
1449 }
1450 }
1451
1452 return *T;
1453}
1454
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001455bool Hexagon_TC::isPICDefault() const {
1456 return false;
Tony Linthicum96319392011-12-12 21:14:55 +00001457}
1458
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001459bool Hexagon_TC::isPICDefaultForced() const {
1460 return false;
1461}
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001462
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001463/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1464/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1465/// Currently does not support anything else but compilation.
1466
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001467TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1468 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001469 // Path mangling to find libexec
1470 std::string Path(getDriver().Dir);
1471
1472 Path += "/../libexec";
1473 getProgramPaths().push_back(Path);
1474}
1475
1476TCEToolChain::~TCEToolChain() {
1477 for (llvm::DenseMap<unsigned, Tool*>::iterator
1478 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1479 delete it->second;
1480}
1481
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001482bool TCEToolChain::IsMathErrnoDefault() const {
1483 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001484}
1485
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001486bool TCEToolChain::isPICDefault() const {
1487 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001488}
1489
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001490bool TCEToolChain::isPICDefaultForced() const {
1491 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001492}
1493
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001494Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001495 const JobAction &JA,
1496 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001497 Action::ActionClass Key;
1498 Key = Action::AnalyzeJobClass;
1499
1500 Tool *&T = Tools[Key];
1501 if (!T) {
1502 switch (Key) {
1503 case Action::PreprocessJobClass:
1504 T = new tools::gcc::Preprocess(*this); break;
1505 case Action::AnalyzeJobClass:
1506 T = new tools::Clang(*this); break;
1507 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001508 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001509 }
1510 }
1511 return *T;
1512}
1513
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001514/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1515
Rafael Espindola0e659592012-02-19 01:38:32 +00001516OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1517 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001518 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001519 getFilePaths().push_back("/usr/lib");
1520}
1521
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001522Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1523 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001524 Action::ActionClass Key;
1525 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1526 Key = Action::AnalyzeJobClass;
1527 else
1528 Key = JA.getKind();
1529
Rafael Espindoladda5b922010-11-07 23:13:01 +00001530 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1531 options::OPT_no_integrated_as,
1532 IsIntegratedAssemblerDefault());
1533
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001534 Tool *&T = Tools[Key];
1535 if (!T) {
1536 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001537 case Action::AssembleJobClass: {
1538 if (UseIntegratedAs)
1539 T = new tools::ClangAs(*this);
1540 else
1541 T = new tools::openbsd::Assemble(*this);
1542 break;
1543 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001544 case Action::LinkJobClass:
1545 T = new tools::openbsd::Link(*this); break;
1546 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001547 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001548 }
1549 }
1550
1551 return *T;
1552}
1553
Eli Friedman42f74f22012-08-08 23:57:20 +00001554/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1555
1556Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1557 : Generic_ELF(D, Triple, Args) {
1558 getFilePaths().push_back(getDriver().Dir + "/../lib");
1559 getFilePaths().push_back("/usr/lib");
1560}
1561
1562Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA,
1563 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001564 Action::ActionClass Key;
1565 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1566 Key = Action::AnalyzeJobClass;
1567 else
1568 Key = JA.getKind();
1569
Eli Friedman42f74f22012-08-08 23:57:20 +00001570 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1571 options::OPT_no_integrated_as,
1572 IsIntegratedAssemblerDefault());
1573
1574 Tool *&T = Tools[Key];
1575 if (!T) {
1576 switch (Key) {
1577 case Action::AssembleJobClass: {
1578 if (UseIntegratedAs)
1579 T = new tools::ClangAs(*this);
1580 else
1581 T = new tools::bitrig::Assemble(*this);
1582 break;
1583 }
1584 case Action::LinkJobClass:
1585 T = new tools::bitrig::Link(*this); break;
1586 default:
1587 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1588 }
1589 }
1590
1591 return *T;
1592}
1593
1594void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1595 ArgStringList &CC1Args) const {
1596 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1597 DriverArgs.hasArg(options::OPT_nostdincxx))
1598 return;
1599
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001600 switch (GetCXXStdlibType(DriverArgs)) {
1601 case ToolChain::CST_Libcxx:
1602 addSystemInclude(DriverArgs, CC1Args,
1603 getDriver().SysRoot + "/usr/include/c++/");
1604 break;
1605 case ToolChain::CST_Libstdcxx:
1606 addSystemInclude(DriverArgs, CC1Args,
1607 getDriver().SysRoot + "/usr/include/c++/stdc++");
1608 addSystemInclude(DriverArgs, CC1Args,
1609 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001610
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001611 StringRef Triple = getTriple().str();
1612 if (Triple.startswith("amd64"))
1613 addSystemInclude(DriverArgs, CC1Args,
1614 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1615 Triple.substr(5));
1616 else
1617 addSystemInclude(DriverArgs, CC1Args,
1618 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1619 Triple);
1620 break;
1621 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001622}
1623
1624void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1625 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001626 switch (GetCXXStdlibType(Args)) {
1627 case ToolChain::CST_Libcxx:
1628 CmdArgs.push_back("-lc++");
1629 CmdArgs.push_back("-lcxxrt");
1630 // Include supc++ to provide Unwind until provided by libcxx.
1631 CmdArgs.push_back("-lgcc");
1632 break;
1633 case ToolChain::CST_Libstdcxx:
1634 CmdArgs.push_back("-lstdc++");
1635 break;
1636 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001637}
1638
Daniel Dunbar75358d22009-03-30 21:06:03 +00001639/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1640
Rafael Espindola0e659592012-02-19 01:38:32 +00001641FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1642 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001643
Chandler Carruth24248e32012-01-26 01:35:15 +00001644 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1645 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001646 if ((Triple.getArch() == llvm::Triple::x86 ||
1647 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001648 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001649 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1650 else
1651 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001652}
1653
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001654Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1655 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001656 Action::ActionClass Key;
1657 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1658 Key = Action::AnalyzeJobClass;
1659 else
1660 Key = JA.getKind();
1661
Roman Divacky67dece72010-11-08 17:46:39 +00001662 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1663 options::OPT_no_integrated_as,
1664 IsIntegratedAssemblerDefault());
1665
Daniel Dunbar75358d22009-03-30 21:06:03 +00001666 Tool *&T = Tools[Key];
1667 if (!T) {
1668 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001669 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001670 if (UseIntegratedAs)
1671 T = new tools::ClangAs(*this);
1672 else
1673 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001674 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001675 case Action::LinkJobClass:
1676 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001677 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001678 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001679 }
1680 }
1681
1682 return *T;
1683}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001684
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001685/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1686
Rafael Espindola0e659592012-02-19 01:38:32 +00001687NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1688 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001689
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001690 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001691 // When targeting a 32-bit platform, try the special directory used on
1692 // 64-bit hosts, and only fall back to the main library directory if that
1693 // doesn't work.
1694 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1695 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001696 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001697 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001698
1699 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001700 }
1701}
1702
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001703Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1704 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001705 Action::ActionClass Key;
1706 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1707 Key = Action::AnalyzeJobClass;
1708 else
1709 Key = JA.getKind();
1710
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001711 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1712 options::OPT_no_integrated_as,
1713 IsIntegratedAssemblerDefault());
1714
1715 Tool *&T = Tools[Key];
1716 if (!T) {
1717 switch (Key) {
1718 case Action::AssembleJobClass:
1719 if (UseIntegratedAs)
1720 T = new tools::ClangAs(*this);
1721 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001722 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001723 break;
1724 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001725 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001726 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001727 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001728 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001729 }
1730 }
1731
1732 return *T;
1733}
1734
Chris Lattner38e317d2010-07-07 16:01:42 +00001735/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1736
Rafael Espindola0e659592012-02-19 01:38:32 +00001737Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1738 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001739 getFilePaths().push_back(getDriver().Dir + "/../lib");
1740 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001741}
1742
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001743Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1744 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001745 Action::ActionClass Key;
1746 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1747 Key = Action::AnalyzeJobClass;
1748 else
1749 Key = JA.getKind();
Chris Lattner38e317d2010-07-07 16:01:42 +00001750
1751 Tool *&T = Tools[Key];
1752 if (!T) {
1753 switch (Key) {
1754 case Action::AssembleJobClass:
1755 T = new tools::minix::Assemble(*this); break;
1756 case Action::LinkJobClass:
1757 T = new tools::minix::Link(*this); break;
1758 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001759 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001760 }
1761 }
1762
1763 return *T;
1764}
1765
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001766/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1767
Rafael Espindola0e659592012-02-19 01:38:32 +00001768AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1769 const ArgList &Args)
1770 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001771
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001772 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001773 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001774 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001775
Daniel Dunbaree788e72009-12-21 18:54:17 +00001776 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001777 getFilePaths().push_back("/usr/lib");
1778 getFilePaths().push_back("/usr/sfw/lib");
1779 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001780 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001781
1782}
1783
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001784Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1785 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001786 Action::ActionClass Key;
1787 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1788 Key = Action::AnalyzeJobClass;
1789 else
1790 Key = JA.getKind();
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001791
1792 Tool *&T = Tools[Key];
1793 if (!T) {
1794 switch (Key) {
1795 case Action::AssembleJobClass:
1796 T = new tools::auroraux::Assemble(*this); break;
1797 case Action::LinkJobClass:
1798 T = new tools::auroraux::Link(*this); break;
1799 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001800 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001801 }
1802 }
1803
1804 return *T;
1805}
1806
David Chisnall31c46902012-02-15 13:39:01 +00001807/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1808
Rafael Espindola0e659592012-02-19 01:38:32 +00001809Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1810 const ArgList &Args)
1811 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001812
1813 getProgramPaths().push_back(getDriver().getInstalledDir());
1814 if (getDriver().getInstalledDir() != getDriver().Dir)
1815 getProgramPaths().push_back(getDriver().Dir);
1816
1817 getFilePaths().push_back(getDriver().Dir + "/../lib");
1818 getFilePaths().push_back("/usr/lib");
1819}
1820
1821Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
1822 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001823 Action::ActionClass Key;
1824 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1825 Key = Action::AnalyzeJobClass;
1826 else
1827 Key = JA.getKind();
David Chisnall31c46902012-02-15 13:39:01 +00001828
1829 Tool *&T = Tools[Key];
1830 if (!T) {
1831 switch (Key) {
1832 case Action::AssembleJobClass:
1833 T = new tools::solaris::Assemble(*this); break;
1834 case Action::LinkJobClass:
1835 T = new tools::solaris::Link(*this); break;
1836 default:
1837 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1838 }
1839 }
1840
1841 return *T;
1842}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001843
Eli Friedman6b3454a2009-05-26 07:52:18 +00001844/// Linux toolchain (very bare-bones at the moment).
1845
Rafael Espindolac1da9812010-11-07 20:14:31 +00001846enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001847 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001848 DebianLenny,
1849 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001850 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001851 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001852 RHEL4,
1853 RHEL5,
1854 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001855 Fedora13,
1856 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001857 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001858 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001859 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001860 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001861 OpenSuse11_4,
1862 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001863 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00001864 UbuntuHardy,
1865 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001866 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001867 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001868 UbuntuLucid,
1869 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001870 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001871 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001872 UbuntuPrecise,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001873 UnknownDistro
1874};
1875
Chris Lattnerd753b562011-05-22 05:36:06 +00001876static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001877 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1878 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001879}
1880
1881static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001882 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001883}
1884
1885static bool IsDebian(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001886 return Distro >= DebianLenny && Distro <= DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001887}
1888
1889static bool IsUbuntu(enum LinuxDistro Distro) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001890 return Distro >= UbuntuHardy && Distro <= UbuntuPrecise;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001891}
1892
Rafael Espindolac1da9812010-11-07 20:14:31 +00001893static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001894 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001895 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001896 StringRef Data = File.get()->getBuffer();
1897 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001898 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001899 LinuxDistro Version = UnknownDistro;
1900 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
1901 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
1902 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
1903 .Case("hardy", UbuntuHardy)
1904 .Case("intrepid", UbuntuIntrepid)
1905 .Case("jaunty", UbuntuJaunty)
1906 .Case("karmic", UbuntuKarmic)
1907 .Case("lucid", UbuntuLucid)
1908 .Case("maverick", UbuntuMaverick)
1909 .Case("natty", UbuntuNatty)
1910 .Case("oneiric", UbuntuOneiric)
1911 .Case("precise", UbuntuPrecise)
1912 .Default(UnknownDistro);
1913 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001914 }
1915
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001916 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001917 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001918 if (Data.startswith("Fedora release 16"))
1919 return Fedora16;
1920 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00001921 return Fedora15;
1922 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001923 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001924 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001925 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001926 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001927 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001928 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001929 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001930 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001931 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001932 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1933 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001934 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001935 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001936 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1937 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001938 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001939 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001940 return UnknownDistro;
1941 }
1942
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001943 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001944 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001945 if (Data[0] == '5')
1946 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001947 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00001948 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001949 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00001950 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001951 return UnknownDistro;
1952 }
1953
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001954 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
1955 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
1956 .StartsWith("openSUSE 11.3", OpenSuse11_3)
1957 .StartsWith("openSUSE 11.4", OpenSuse11_4)
1958 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001959 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001960 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001961
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001962 bool Exists;
1963 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001964 return Exherbo;
1965
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001966 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1967 return ArchLinux;
1968
Rafael Espindolac1da9812010-11-07 20:14:31 +00001969 return UnknownDistro;
1970}
1971
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001972/// \brief Get our best guess at the multiarch triple for a target.
1973///
1974/// Debian-based systems are starting to use a multiarch setup where they use
1975/// a target-triple directory in the library and header search paths.
1976/// Unfortunately, this triple does not align with the vanilla target triple,
1977/// so we provide a rough mapping here.
1978static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
1979 StringRef SysRoot) {
1980 // For most architectures, just use whatever we have rather than trying to be
1981 // clever.
1982 switch (TargetTriple.getArch()) {
1983 default:
1984 return TargetTriple.str();
1985
1986 // We use the existence of '/lib/<triple>' as a directory to detect some
1987 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00001988 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
1989 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00001990 case llvm::Triple::arm:
1991 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00001992 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1993 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
1994 return "arm-linux-gnueabihf";
1995 } else {
1996 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
1997 return "arm-linux-gnueabi";
1998 }
Chad Rosier0337efd2012-07-11 19:08:21 +00001999 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002000 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002001 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2002 return "i386-linux-gnu";
2003 return TargetTriple.str();
2004 case llvm::Triple::x86_64:
2005 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2006 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002007 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00002008 case llvm::Triple::mips:
2009 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2010 return "mips-linux-gnu";
2011 return TargetTriple.str();
2012 case llvm::Triple::mipsel:
2013 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2014 return "mipsel-linux-gnu";
2015 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002016 case llvm::Triple::ppc:
2017 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2018 return "powerpc-linux-gnu";
2019 return TargetTriple.str();
2020 case llvm::Triple::ppc64:
2021 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2022 return "powerpc64-linux-gnu";
2023 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002024 }
2025}
2026
Chandler Carruth00646ba2012-01-25 11:24:24 +00002027static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2028 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2029}
2030
Simon Atanasyan7a918882012-09-14 11:27:24 +00002031static bool isMipsArch(llvm::Triple::ArchType Arch) {
2032 return Arch == llvm::Triple::mips ||
2033 Arch == llvm::Triple::mipsel ||
2034 Arch == llvm::Triple::mips64 ||
2035 Arch == llvm::Triple::mips64el;
2036}
2037
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002038static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2039 const ArgList &Args) {
2040 if (Arch != llvm::Triple::mips &&
2041 Arch != llvm::Triple::mipsel)
2042 return false;
2043
2044 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2045 options::OPT_mcpu_EQ,
2046 options::OPT_mips_CPUs_Group);
2047
2048 if (!A)
2049 return false;
2050
2051 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2052 return A->getOption().matches(options::OPT_mips32r2);
2053
Richard Smith1d489cf2012-11-01 04:30:05 +00002054 return A->getValue() == StringRef("mips32r2");
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002055}
2056
Simon Atanasyan7a918882012-09-14 11:27:24 +00002057static StringRef getMultilibDir(const llvm::Triple &Triple,
2058 const ArgList &Args) {
2059 if (!isMipsArch(Triple.getArch()))
2060 return Triple.isArch32Bit() ? "lib32" : "lib64";
2061
2062 // lib32 directory has a special meaning on MIPS targets.
2063 // It contains N32 ABI binaries. Use this folder if produce
2064 // code for N32 ABI only.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00002065 if (hasMipsN32ABIArg(Args))
Simon Atanasyan7a918882012-09-14 11:27:24 +00002066 return "lib32";
2067
2068 return Triple.isArch32Bit() ? "lib" : "lib64";
2069}
2070
Rafael Espindola0e659592012-02-19 01:38:32 +00002071Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2072 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002073 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00002074 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002075
Rafael Espindolaab784082011-09-01 16:25:49 +00002076 // OpenSuse stores the linker with the compiler, add that to the search
2077 // path.
2078 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002079 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002080 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002081
2082 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002083
2084 LinuxDistro Distro = DetectLinuxDistro(Arch);
2085
Chris Lattner64a89172011-05-22 16:45:07 +00002086 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002087 ExtraOpts.push_back("-z");
2088 ExtraOpts.push_back("relro");
2089 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002090
Douglas Gregorf0594d82011-03-06 19:11:49 +00002091 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002092 ExtraOpts.push_back("-X");
2093
Logan Chien94a71422012-09-02 09:30:11 +00002094 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002095
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002096 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2097 // and the MIPS ABI require .dynsym to be sorted in different ways.
2098 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2099 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002100 // Android loader does not support .gnu.hash.
Simon Atanasyan7a918882012-09-14 11:27:24 +00002101 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002102 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2103 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002104 ExtraOpts.push_back("--hash-style=gnu");
2105
2106 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2107 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2108 ExtraOpts.push_back("--hash-style=both");
2109 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002110
Chris Lattnerd753b562011-05-22 05:36:06 +00002111 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002112 ExtraOpts.push_back("--no-add-needed");
2113
Eli Friedman0b200f62011-06-02 21:36:53 +00002114 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002115 IsOpenSuse(Distro) ||
2116 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002117 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002118 ExtraOpts.push_back("--build-id");
2119
Chris Lattner64a89172011-05-22 16:45:07 +00002120 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002121 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002122
Chandler Carruthd2deee12011-10-03 05:28:29 +00002123 // The selection of paths to try here is designed to match the patterns which
2124 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2125 // This was determined by running GCC in a fake filesystem, creating all
2126 // possible permutations of these directories, and seeing which ones it added
2127 // to the link paths.
2128 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002129
Simon Atanasyan7a918882012-09-14 11:27:24 +00002130 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002131 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002132
Chandler Carruthd1f73062011-11-06 23:09:05 +00002133 // Add the multilib suffixed paths where they are available.
2134 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002135 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002136 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002137
2138 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2139 addPathIfExists(GCCInstallation.getInstallPath() +
2140 GCCInstallation.getMultiarchSuffix() +
2141 "/mips-r2",
2142 Paths);
2143 else
2144 addPathIfExists((GCCInstallation.getInstallPath() +
2145 GCCInstallation.getMultiarchSuffix()),
2146 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002147
2148 // If the GCC installation we found is inside of the sysroot, we want to
2149 // prefer libraries installed in the parent prefix of the GCC installation.
2150 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002151 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002152 // This usually happens when there is an external cross compiler on the
2153 // host system, and a more minimal sysroot available that is the target of
2154 // the cross.
2155 if (StringRef(LibPath).startswith(SysRoot)) {
2156 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2157 Paths);
2158 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2159 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2160 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002161 // On Android, libraries in the parent prefix of the GCC installation are
2162 // preferred to the ones under sysroot.
2163 if (IsAndroid) {
2164 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2165 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002166 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002167 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2168 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2169 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2170 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2171
2172 // Try walking via the GCC triple path in case of multiarch GCC
2173 // installations with strange symlinks.
2174 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002175 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002176 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002177
Chandler Carruth7a09d012011-10-16 10:54:30 +00002178 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002179 if (GCCInstallation.isValid()) {
2180 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002181 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002182 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002183 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002184
2185 if (StringRef(LibPath).startswith(SysRoot)) {
2186 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2187 addPathIfExists(LibPath, Paths);
2188 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002189 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002190 addPathIfExists(SysRoot + "/lib", Paths);
2191 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002192}
2193
2194bool Linux::HasNativeLLVMSupport() const {
2195 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002196}
2197
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002198Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2199 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00002200 Action::ActionClass Key;
2201 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2202 Key = Action::AnalyzeJobClass;
2203 else
2204 Key = JA.getKind();
2205
Rafael Espindoladda5b922010-11-07 23:13:01 +00002206 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2207 options::OPT_no_integrated_as,
2208 IsIntegratedAssemblerDefault());
2209
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002210 Tool *&T = Tools[Key];
2211 if (!T) {
2212 switch (Key) {
2213 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002214 if (UseIntegratedAs)
2215 T = new tools::ClangAs(*this);
2216 else
2217 T = new tools::linuxtools::Assemble(*this);
2218 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002219 case Action::LinkJobClass:
2220 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002221 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002222 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002223 }
2224 }
2225
2226 return *T;
2227}
2228
Chandler Carrutha6b25812012-11-21 23:40:23 +00002229void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2230 ArgStringList &CC1Args) const {
Rafael Espindola8af669f2012-06-19 01:26:10 +00002231 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carrutha6b25812012-11-21 23:40:23 +00002232 bool UseInitArrayDefault
2233 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
2234 getTriple().getEnvironment() == llvm::Triple::Android;
2235 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2236 options::OPT_fno_use_init_array,
2237 UseInitArrayDefault))
Rafael Espindola8af669f2012-06-19 01:26:10 +00002238 CC1Args.push_back("-fuse-init-array");
2239}
2240
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002241void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2242 ArgStringList &CC1Args) const {
2243 const Driver &D = getDriver();
2244
2245 if (DriverArgs.hasArg(options::OPT_nostdinc))
2246 return;
2247
2248 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2249 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2250
2251 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002252 llvm::sys::Path P(D.ResourceDir);
2253 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002254 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002255 }
2256
2257 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2258 return;
2259
2260 // Check for configure-time C include directories.
2261 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2262 if (CIncludeDirs != "") {
2263 SmallVector<StringRef, 5> dirs;
2264 CIncludeDirs.split(dirs, ":");
2265 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2266 I != E; ++I) {
2267 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2268 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2269 }
2270 return;
2271 }
2272
2273 // Lacking those, try to detect the correct set of system includes for the
2274 // target triple.
2275
Chandler Carrutha4630892011-11-06 08:21:07 +00002276 // Implement generic Debian multiarch support.
2277 const StringRef X86_64MultiarchIncludeDirs[] = {
2278 "/usr/include/x86_64-linux-gnu",
2279
2280 // FIXME: These are older forms of multiarch. It's not clear that they're
2281 // in use in any released version of Debian, so we should consider
2282 // removing them.
2283 "/usr/include/i686-linux-gnu/64",
2284 "/usr/include/i486-linux-gnu/64"
2285 };
2286 const StringRef X86MultiarchIncludeDirs[] = {
2287 "/usr/include/i386-linux-gnu",
2288
2289 // FIXME: These are older forms of multiarch. It's not clear that they're
2290 // in use in any released version of Debian, so we should consider
2291 // removing them.
2292 "/usr/include/x86_64-linux-gnu/32",
2293 "/usr/include/i686-linux-gnu",
2294 "/usr/include/i486-linux-gnu"
2295 };
2296 const StringRef ARMMultiarchIncludeDirs[] = {
2297 "/usr/include/arm-linux-gnueabi"
2298 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002299 const StringRef ARMHFMultiarchIncludeDirs[] = {
2300 "/usr/include/arm-linux-gnueabihf"
2301 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002302 const StringRef MIPSMultiarchIncludeDirs[] = {
2303 "/usr/include/mips-linux-gnu"
2304 };
2305 const StringRef MIPSELMultiarchIncludeDirs[] = {
2306 "/usr/include/mipsel-linux-gnu"
2307 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002308 const StringRef PPCMultiarchIncludeDirs[] = {
2309 "/usr/include/powerpc-linux-gnu"
2310 };
2311 const StringRef PPC64MultiarchIncludeDirs[] = {
2312 "/usr/include/powerpc64-linux-gnu"
2313 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002314 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002315 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002316 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002317 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002318 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002319 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002320 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2321 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2322 else
2323 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002324 } else if (getTriple().getArch() == llvm::Triple::mips) {
2325 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2326 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2327 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002328 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2329 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2330 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2331 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002332 }
2333 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2334 E = MultiarchIncludeDirs.end();
2335 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002336 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002337 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2338 break;
2339 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002340 }
2341
2342 if (getTriple().getOS() == llvm::Triple::RTEMS)
2343 return;
2344
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002345 // Add an include of '/include' directly. This isn't provided by default by
2346 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2347 // add even when Clang is acting as-if it were a system compiler.
2348 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2349
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002350 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2351}
2352
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002353/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2354/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2355 const ArgList &DriverArgs,
2356 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002357 if (!llvm::sys::fs::exists(Base))
2358 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002359 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002360 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002361 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002362 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002363}
2364
2365void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2366 ArgStringList &CC1Args) const {
2367 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2368 DriverArgs.hasArg(options::OPT_nostdincxx))
2369 return;
2370
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002371 // Check if libc++ has been enabled and provide its include paths if so.
2372 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2373 // libc++ is always installed at a fixed path on Linux currently.
2374 addSystemInclude(DriverArgs, CC1Args,
2375 getDriver().SysRoot + "/usr/include/c++/v1");
2376 return;
2377 }
2378
Chandler Carruthfc52f752012-01-25 08:04:13 +00002379 // We need a detected GCC installation on Linux to provide libstdc++'s
2380 // headers. We handled the libc++ case above.
2381 if (!GCCInstallation.isValid())
2382 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002383
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002384 // By default, look for the C++ headers in an include directory adjacent to
2385 // the lib directory of the GCC installation. Note that this is expect to be
2386 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2387 StringRef LibDir = GCCInstallation.getParentLibPath();
2388 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002389 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002390 StringRef TripleStr = GCCInstallation.getTriple().str();
2391
2392 const std::string IncludePathCandidates[] = {
2393 LibDir.str() + "/../include/c++/" + Version.str(),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002394 // Gentoo is weird and places its headers inside the GCC install, so if the
2395 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002396 InstallDir.str() + "/include/g++-v4",
2397 // Android standalone toolchain has C++ headers in yet another place.
2398 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002399 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2400 // without a subdirectory corresponding to the gcc version.
2401 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002402 };
2403
2404 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2405 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2406 GCCInstallation.getMultiarchSuffix()),
2407 DriverArgs, CC1Args))
2408 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002409 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002410}
2411
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002412/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2413
Rafael Espindola0e659592012-02-19 01:38:32 +00002414DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2415 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002416
2417 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002418 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002419 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002420 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002421
Daniel Dunbaree788e72009-12-21 18:54:17 +00002422 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002423 getFilePaths().push_back("/usr/lib");
2424 getFilePaths().push_back("/usr/lib/gcc41");
2425}
2426
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002427Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2428 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00002429 Action::ActionClass Key;
2430 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2431 Key = Action::AnalyzeJobClass;
2432 else
2433 Key = JA.getKind();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002434
2435 Tool *&T = Tools[Key];
2436 if (!T) {
2437 switch (Key) {
2438 case Action::AssembleJobClass:
2439 T = new tools::dragonfly::Assemble(*this); break;
2440 case Action::LinkJobClass:
2441 T = new tools::dragonfly::Link(*this); break;
2442 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002443 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002444 }
2445 }
2446
2447 return *T;
2448}