blob: 665d61568e2475aee03e63a5eb77ff20baf25d68 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000011#include "SanitizerArgs.h"
12#include "clang/Basic/ObjCRuntime.h"
13#include "clang/Basic/Version.h"
Daniel Dunbarf3cad362009-03-25 04:13:45 +000014#include "clang/Driver/Arg.h"
15#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000016#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000017#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000018#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000019#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000020#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000021#include "clang/Driver/Options.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "llvm/ADT/STLExtras.h"
Daniel Dunbar00577ad2010-08-23 22:35:37 +000023#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000024#include "llvm/ADT/StringExtras.h"
Bob Wilsona59956b2011-10-07 00:37:57 +000025#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000026#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000027#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000031#include "llvm/Support/system_error.h"
NAKAMURA Takumi55426352012-12-04 14:31:59 +000032
33// FIXME: This needs to be listed last until we fix the broken include guards
34// in these files and the LLVM config.h files.
35#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
36
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000037#include <cstdlib> // ::getenv
38
Daniel Dunbar39176082009-03-20 00:20:03 +000039using namespace clang::driver;
40using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000041using namespace clang;
Daniel Dunbar39176082009-03-20 00:20:03 +000042
Daniel Dunbarf3955282009-09-04 18:34:51 +000043/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000044
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000045Darwin::Darwin(const Driver &D, const llvm::Triple& Triple)
John McCall260611a2012-06-20 06:18:46 +000046 : ToolChain(D, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000047{
Bob Wilson10853772012-01-31 21:30:03 +000048 // Compute the initial Darwin version from the triple
49 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-01-31 22:43:59 +000050 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
51 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
52 Triple.getOSName();
53 llvm::raw_string_ostream(MacosxVersionMin)
54 << Major << '.' << Minor << '.' << Micro;
55
Bob Wilson10853772012-01-31 21:30:03 +000056 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
57 // It should be removed when we stop supporting that.
58 DarwinVersion[0] = Minor + 4;
59 DarwinVersion[1] = Micro;
60 DarwinVersion[2] = 0;
Chad Rosierc793ea42012-05-09 18:46:30 +000061
62 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000063 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000064 llvm::raw_string_ostream(iOSVersionMin)
65 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000066}
67
Daniel Dunbar41800112010-08-02 05:43:56 +000068types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
69 types::ID Ty = types::lookupTypeForExtension(Ext);
70
71 // Darwin always preprocesses assembly files (unless -x is used explicitly).
72 if (Ty == types::TY_PP_Asm)
73 return types::TY_Asm;
74
75 return Ty;
76}
77
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000078bool Darwin::HasNativeLLVMSupport() const {
79 return true;
80}
81
John McCall9f084a32011-07-06 00:26:06 +000082/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall260611a2012-06-20 06:18:46 +000083ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
Bob Wilson377e5c12012-11-09 01:59:30 +000084 if (isTargetIPhoneOS())
John McCall260611a2012-06-20 06:18:46 +000085 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson377e5c12012-11-09 01:59:30 +000086 if (isNonFragile)
87 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
88 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall9f084a32011-07-06 00:26:06 +000089}
90
John McCall13db5cf2011-09-09 20:41:01 +000091/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
92bool Darwin::hasBlocksRuntime() const {
93 if (isTargetIPhoneOS())
94 return !isIPhoneOSVersionLT(3, 2);
95 else
96 return !isMacosxVersionLT(10, 6);
97}
98
Chris Lattner5f9e2722011-07-23 10:55:15 +000099static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000100 return llvm::StringSwitch<const char*>(Value)
101 .Case("armv6k", "armv6")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000102 .Case("armv6m", "armv6m")
Bob Wilsona59956b2011-10-07 00:37:57 +0000103 .Case("armv5tej", "armv5")
104 .Case("xscale", "xscale")
105 .Case("armv4t", "armv4t")
106 .Case("armv7", "armv7")
107 .Cases("armv7a", "armv7-a", "armv7")
108 .Cases("armv7r", "armv7-r", "armv7")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000109 .Cases("armv7em", "armv7e-m", "armv7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000110 .Cases("armv7f", "armv7-f", "armv7f")
111 .Cases("armv7k", "armv7-k", "armv7k")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000112 .Cases("armv7m", "armv7-m", "armv7m")
Bob Wilson336bfa32012-09-29 23:52:50 +0000113 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000114 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000115}
116
Chris Lattner5f9e2722011-07-23 10:55:15 +0000117static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000118 return llvm::StringSwitch<const char *>(Value)
119 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
120 .Cases("arm10e", "arm10tdmi", "armv5")
121 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
122 .Case("xscale", "xscale")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000123 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6")
124 .Case("cortex-m0", "armv6m")
125 .Cases("cortex-a8", "cortex-r4", "cortex-a9", "cortex-a15", "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000126 .Case("cortex-a9-mp", "armv7f")
Bob Wilson2503ebd2013-03-04 22:37:49 +0000127 .Case("cortex-m3", "armv7m")
128 .Case("cortex-m4", "armv7em")
Bob Wilson336bfa32012-09-29 23:52:50 +0000129 .Case("swift", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000130 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000131}
132
Chris Lattner5f9e2722011-07-23 10:55:15 +0000133StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000134 switch (getTriple().getArch()) {
135 default:
136 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000137
Douglas Gregorf0594d82011-03-06 19:11:49 +0000138 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000139 case llvm::Triple::arm: {
140 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000141 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000142 return Arch;
143
144 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000145 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000146 return Arch;
147
148 return "arm";
149 }
150 }
151}
152
Daniel Dunbarf3955282009-09-04 18:34:51 +0000153Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000154 // Free tool implementations.
155 for (llvm::DenseMap<unsigned, Tool*>::iterator
156 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
157 delete it->second;
158}
159
Chad Rosier61ab80a2011-09-20 20:44:06 +0000160std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
161 types::ID InputType) const {
162 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000163
164 // If the target isn't initialized (e.g., an unknown Darwin platform, return
165 // the default triple).
166 if (!isTargetInitialized())
167 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000168
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000169 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000170 Str += isTargetIPhoneOS() ? "ios" : "macosx";
171 Str += getTargetVersion().getAsString();
172 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000173
174 return Triple.getTriple();
175}
176
David Blaikie99ba9e32011-12-20 02:48:34 +0000177void Generic_ELF::anchor() {}
178
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000179Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
180 const ActionList &Inputs) const {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000181 Action::ActionClass Key = JA.getKind();
Nick Lewycky5bab9ae2012-11-15 05:36:36 +0000182
183 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
184 // FIXME: This seems like a hacky way to choose clang frontend.
185 Key = Action::AnalyzeJobClass;
186 }
187
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000188 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
189 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000190 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000191
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000192 Tool *&T = Tools[Key];
193 if (!T) {
194 switch (Key) {
195 case Action::InputClass:
196 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000197 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000198 case Action::PreprocessJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000199 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000200 case Action::MigrateJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000201 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000202 case Action::CompileJobClass:
Bob Wilson66b8a662012-11-23 06:14:39 +0000203 T = new tools::Clang(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000204 case Action::AssembleJobClass: {
205 if (UseIntegratedAs)
206 T = new tools::ClangAs(*this);
207 else
208 T = new tools::darwin::Assemble(*this);
209 break;
210 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000211 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000212 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000213 case Action::LipoJobClass:
214 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000215 case Action::DsymutilJobClass:
216 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000217 case Action::VerifyJobClass:
218 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000219 }
220 }
221
222 return *T;
223}
224
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000225
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000226DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
227 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000228{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000229 getProgramPaths().push_back(getDriver().getInstalledDir());
230 if (getDriver().getInstalledDir() != getDriver().Dir)
231 getProgramPaths().push_back(getDriver().Dir);
232
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000233 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000234 getProgramPaths().push_back(getDriver().getInstalledDir());
235 if (getDriver().getInstalledDir() != getDriver().Dir)
236 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000237}
238
John McCallf85e1932011-06-15 23:02:42 +0000239void DarwinClang::AddLinkARCArgs(const ArgList &Args,
240 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000241
242 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000243 llvm::sys::Path P(getDriver().ClangExecutable);
244 P.eraseComponent(); // 'clang'
245 P.eraseComponent(); // 'bin'
246 P.appendComponent("lib");
247 P.appendComponent("arc");
248 P.appendComponent("libarclite_");
249 std::string s = P.str();
250 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000251 if (isTargetIOSSimulator())
252 s += "iphonesimulator";
253 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000254 s += "iphoneos";
John McCallf85e1932011-06-15 23:02:42 +0000255 else
256 s += "macosx";
257 s += ".a";
258
259 CmdArgs.push_back(Args.MakeArgString(s));
260}
261
Eric Christopher3404fe72011-06-22 17:41:40 +0000262void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000263 ArgStringList &CmdArgs,
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000264 const char *DarwinStaticLib,
265 bool AlwaysLink) const {
Eric Christopher3404fe72011-06-22 17:41:40 +0000266 llvm::sys::Path P(getDriver().ResourceDir);
267 P.appendComponent("lib");
268 P.appendComponent("darwin");
269 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000270
Eric Christopher3404fe72011-06-22 17:41:40 +0000271 // For now, allow missing resource libraries to support developers who may
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000272 // not have compiler-rt checked out or integrated into their build (unless
273 // we explicitly force linking with this library).
Eric Christopher3404fe72011-06-22 17:41:40 +0000274 bool Exists;
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000275 if (AlwaysLink || (!llvm::sys::fs::exists(P.str(), Exists) && Exists))
Eric Christopher3404fe72011-06-22 17:41:40 +0000276 CmdArgs.push_back(Args.MakeArgString(P.str()));
277}
278
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000279void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
280 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000281 // Darwin only supports the compiler-rt based runtime libraries.
282 switch (GetRuntimeLibType(Args)) {
283 case ToolChain::RLT_CompilerRT:
284 break;
285 default:
286 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000287 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000288 return;
289 }
290
Daniel Dunbareec99102010-01-22 03:38:14 +0000291 // Darwin doesn't support real static executables, don't link any runtime
292 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000293 if (Args.hasArg(options::OPT_static) ||
294 Args.hasArg(options::OPT_fapple_kext) ||
295 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000296 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000297
298 // Reject -static-libgcc for now, we can deal with this when and if someone
299 // cares. This is useful in situations where someone wants to statically link
300 // something like libstdc++, and needs its runtime support routines.
301 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000302 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000303 << A->getAsString(Args);
304 return;
305 }
306
Daniel Dunbarf4714872011-11-17 00:36:57 +0000307 // If we are building profile support, link that library in.
308 if (Args.hasArg(options::OPT_fprofile_arcs) ||
309 Args.hasArg(options::OPT_fprofile_generate) ||
310 Args.hasArg(options::OPT_fcreate_profile) ||
311 Args.hasArg(options::OPT_coverage)) {
312 // Select the appropriate runtime library for the target.
313 if (isTargetIPhoneOS()) {
314 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
315 } else {
316 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
317 }
318 }
319
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000320 SanitizerArgs Sanitize(getDriver(), Args);
321
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000322 // Add Ubsan runtime library, if required.
323 if (Sanitize.needsUbsanRt()) {
Alexey Samsonov2cb3d302013-01-21 08:45:02 +0000324 if (isTargetIPhoneOS()) {
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000325 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
326 << "-fsanitize=undefined";
327 } else {
Alexey Samsonov69b77d72012-11-21 14:17:42 +0000328 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ubsan_osx.a", true);
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000329
330 // The Ubsan runtime library requires C++.
331 AddCXXStdlibLibArgs(Args, CmdArgs);
332 }
333 }
334
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000335 // Add ASAN runtime library, if required. Dynamic libraries and bundles
336 // should not be linked with the runtime library.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000337 if (Sanitize.needsAsanRt()) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000338 if (Args.hasArg(options::OPT_dynamiclib) ||
Alexey Samsonov75fcb192012-11-16 12:53:14 +0000339 Args.hasArg(options::OPT_bundle)) {
340 // Assume the binary will provide the ASan runtime.
341 } else if (isTargetIPhoneOS()) {
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000342 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000343 << "-fsanitize=address";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000344 } else {
Alexander Potapenko454028e2013-01-22 09:16:03 +0000345 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx_dynamic.dylib", true);
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000346
Alexander Potapenko454028e2013-01-22 09:16:03 +0000347 // The ASAN runtime library requires C++.
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000348 AddCXXStdlibLibArgs(Args, CmdArgs);
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000349 }
350 }
351
Daniel Dunbareec99102010-01-22 03:38:14 +0000352 // Otherwise link libSystem, then the dynamic runtime library, and finally any
353 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000354 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000355
356 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000357 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000358 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
359 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000360 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
361 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
362 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000363
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000364 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000365 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000366 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000367 // The dynamic runtime library was merged with libSystem for 10.6 and
368 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000369 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000370 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000371 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000372 CmdArgs.push_back("-lgcc_s.10.5");
373
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000374 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000375 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000376 // omitted from 10.4.dylib.
377 //
378 // Unfortunately, that turned out to not be true, because Darwin system
379 // headers can still use eprintf on i386, and it is not exported from
380 // libSystem. Therefore, we still must provide a runtime library just for
381 // the tiny tiny handful of projects that *might* use that symbol.
382 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000383 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000384 } else {
385 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000386 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
387 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000388 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000389 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000390}
391
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000392void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000393 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000394
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000395 // Support allowing the SDKROOT environment variable used by xcrun and other
396 // Xcode tools to define the default sysroot, by making it the default for
397 // isysroot.
Chad Rosierd7dfd982012-12-19 23:41:50 +0000398 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
399 // Warn if the path does not exist.
400 bool Exists;
401 if (llvm::sys::fs::exists(A->getValue(), Exists) || !Exists)
402 getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
403 } else {
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000404 if (char *env = ::getenv("SDKROOT")) {
Daniel Dunbar32142542013-01-15 20:33:56 +0000405 // We only use this value as the default if it is an absolute path,
406 // exists, and it is not the root path.
407 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env) &&
408 StringRef(env) != "/") {
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000409 Args.append(Args.MakeSeparateArg(
410 0, Opts.getOption(options::OPT_isysroot), env));
411 }
412 }
413 }
414
Daniel Dunbar26031372010-01-27 00:56:25 +0000415 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000416 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
417 Arg *iOSSimVersion = Args.getLastArg(
418 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000419
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000420 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000421 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000422 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000423 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
424 iOSVersion = iOSSimVersion = 0;
425 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000426 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000427 << iOSVersion->getAsString(Args)
428 << iOSSimVersion->getAsString(Args);
429 iOSSimVersion = 0;
430 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000431 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000432 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000433 StringRef OSXTarget;
434 StringRef iOSTarget;
435 StringRef iOSSimTarget;
436 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
437 OSXTarget = env;
438 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
439 iOSTarget = env;
440 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
441 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000442
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000443 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000444 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000445 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000446 if (iOSTarget.empty()) {
447 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
448 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000449 StringRef isysroot = A->getValue();
Chad Rosiera4884972011-08-31 20:56:25 +0000450 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
451 if (second != "")
452 iOSTarget = second.substr(0,3);
453 }
454 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000455
Chad Rosier4f8de272011-09-28 00:46:32 +0000456 // If no OSX or iOS target has been specified and we're compiling for armv7,
457 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000458 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000459 (getDarwinArchName(Args) == "armv7" ||
460 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000461 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000462
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000463 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000464 //
465 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000466
467 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000468 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000469 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000470 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000471 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000472 "IPHONEOS_DEPLOYMENT_TARGET");
473 }
474
475 // Allow conflicts among OSX and iOS for historical reasons, but choose the
476 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000477 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000478 if (getTriple().getArch() == llvm::Triple::arm ||
479 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000480 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000481 else
Chad Rosiera4884972011-08-31 20:56:25 +0000482 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000483 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000484
Chad Rosiera4884972011-08-31 20:56:25 +0000485 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000486 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000487 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
488 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000489 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000490 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000491 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
492 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000493 } else if (!iOSSimTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000494 const Option O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000495 options::OPT_mios_simulator_version_min_EQ);
496 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
497 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000498 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000499 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000500 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000501 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
502 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000503 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000506 // Reject invalid architecture combinations.
507 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
508 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000509 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000510 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
511 }
512
Daniel Dunbar26031372010-01-27 00:56:25 +0000513 // Set the tool chain target information.
514 unsigned Major, Minor, Micro;
515 bool HadExtra;
516 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000517 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000518 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000519 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000520 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000521 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000522 << OSXVersion->getAsString(Args);
523 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000524 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
525 assert(Version && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000526 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000527 Micro, HadExtra) || HadExtra ||
528 Major >= 10 || Minor >= 100 || Micro >= 100)
529 getDriver().Diag(diag::err_drv_invalid_version_number)
530 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000531 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000532
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000533 bool IsIOSSim = bool(iOSSimVersion);
534
535 // In GCC, the simulator historically was treated as being OS X in some
536 // contexts, like determining the link logic, despite generally being called
537 // with an iOS deployment target. For compatibility, we detect the
538 // simulator as iOS + x86, and treat it differently in a few contexts.
539 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
540 getTriple().getArch() == llvm::Triple::x86_64))
541 IsIOSSim = true;
542
543 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000544}
545
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000546void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000547 ArgStringList &CmdArgs) const {
548 CXXStdlibType Type = GetCXXStdlibType(Args);
549
550 switch (Type) {
551 case ToolChain::CST_Libcxx:
552 CmdArgs.push_back("-lc++");
553 break;
554
555 case ToolChain::CST_Libstdcxx: {
556 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
557 // it was previously found in the gcc lib dir. However, for all the Darwin
558 // platforms we care about it was -lstdc++.6, so we search for that
559 // explicitly if we can't see an obvious -lstdc++ candidate.
560
561 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000562 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000563 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000564 llvm::sys::Path P(A->getValue());
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000565 P.appendComponent("usr");
566 P.appendComponent("lib");
567 P.appendComponent("libstdc++.dylib");
568
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000569 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000570 P.eraseComponent();
571 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000572 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000573 CmdArgs.push_back(Args.MakeArgString(P.str()));
574 return;
575 }
576 }
577 }
578
579 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000580 // FIXME: This should be removed someday when we don't have to care about
581 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000582 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
583 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000584 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
585 return;
586 }
587
588 // Otherwise, let the linker search.
589 CmdArgs.push_back("-lstdc++");
590 break;
591 }
592 }
593}
594
Shantonu Sen7433fed2010-09-17 18:39:08 +0000595void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
596 ArgStringList &CmdArgs) const {
597
598 // For Darwin platforms, use the compiler-rt-based support library
599 // instead of the gcc-provided one (which is also incidentally
600 // only present in the gcc lib dir, which makes it hard to find).
601
602 llvm::sys::Path P(getDriver().ResourceDir);
603 P.appendComponent("lib");
604 P.appendComponent("darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000605
606 // Use the newer cc_kext for iOS ARM after 6.0.
607 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
608 !isIPhoneOSVersionLT(6, 0)) {
609 P.appendComponent("libclang_rt.cc_kext.a");
610 } else {
611 P.appendComponent("libclang_rt.cc_kext_ios5.a");
612 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000613
Shantonu Sen7433fed2010-09-17 18:39:08 +0000614 // For now, allow missing resource libraries to support developers who may
615 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000616 bool Exists;
617 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000618 CmdArgs.push_back(Args.MakeArgString(P.str()));
619}
620
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000621DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
622 const char *BoundArch) const {
623 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
624 const OptTable &Opts = getDriver().getOpts();
625
626 // FIXME: We really want to get out of the tool chain level argument
627 // translation business, as it makes the driver functionality much
628 // more opaque. For now, we follow gcc closely solely for the
629 // purpose of easily achieving feature parity & testability. Once we
630 // have something that works, we should reevaluate each translation
631 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000632
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000633 for (ArgList::const_iterator it = Args.begin(),
634 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000635 Arg *A = *it;
636
637 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000638 // Skip this argument unless the architecture matches either the toolchain
639 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000640 llvm::Triple::ArchType XarchArch =
Richard Smith1d489cf2012-11-01 04:30:05 +0000641 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000642 if (!(XarchArch == getArch() ||
643 (BoundArch && XarchArch ==
Rafael Espindolacfed8282012-10-31 18:51:07 +0000644 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000645 continue;
646
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000647 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000648 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000649 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000650 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000652 // If the argument parsing failed or more than one argument was
653 // consumed, the -Xarch_ argument's parameter tried to consume
654 // extra arguments. Emit an error and ignore.
655 //
656 // We also want to disallow any options which would alter the
657 // driver behavior; that isn't going to work in our model. We
658 // use isDriverOption() as an approximation, although things
659 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000660 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000661 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000662 << A->getAsString(Args);
663 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000664 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000665 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000666 << A->getAsString(Args);
667 continue;
668 }
669
Daniel Dunbar478edc22009-03-29 22:29:05 +0000670 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000671 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000672
673 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000674
675 // Linker input arguments require custom handling. The problem is that we
676 // have already constructed the phase actions, so we can not treat them as
677 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000678 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000679 // Convert the argument into individual Zlinker_input_args.
680 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
681 DAL->AddSeparateArg(OriginalArg,
682 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000683 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000684
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000685 }
686 continue;
687 }
Mike Stump1eb44332009-09-09 15:08:12 +0000688 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000689
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000690 // Sob. These is strictly gcc compatible for the time being. Apple
691 // gcc translates options twice, which means that self-expanding
692 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000693 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000694 default:
695 DAL->append(A);
696 break;
697
698 case options::OPT_mkernel:
699 case options::OPT_fapple_kext:
700 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000701 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000702 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000704 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000705 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000706 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000707 break;
708
709 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000710 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
711 DAL->AddFlagArg(A,
712 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000713 break;
714
715 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000716 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
717 DAL->AddFlagArg(A,
718 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000719 break;
720
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000721 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000722 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000723 break;
724
725 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000726 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000727 break;
728
729 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000730 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000731 break;
732
733 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000734 DAL->AddFlagArg(A,
735 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000736 break;
737
738 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000739 DAL->AddFlagArg(A,
740 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000741 break;
742
743 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000744 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000745 break;
746
747 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000748 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000749 break;
750 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000751 }
752
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000753 if (getTriple().getArch() == llvm::Triple::x86 ||
754 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000755 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000756 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000757
758 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000759 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000760 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000761 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000762 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
763 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000764
765 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
766 // which defines the list of which architectures we accept.
767 if (Name == "ppc")
768 ;
769 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000770 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000771 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000772 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000773 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000774 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000775 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000776 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000777 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000778 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000779 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000780 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000781 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000782 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000783 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000784 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000785
786 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000787 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000788
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000789 else if (Name == "i386")
790 ;
791 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000792 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000793 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000794 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000795 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000796 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000797 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000798 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000799 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000800 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000801 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000802 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000803 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000804 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000805
806 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000807 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000808
809 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000810 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000811 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000812 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000813 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000814 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000815 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000816 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000817 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000818 DAL->AddJoinedArg(0, MArch, "armv6k");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000819 else if (Name == "armv6m")
820 DAL->AddJoinedArg(0, MArch, "armv6m");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000821 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000822 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000823 else if (Name == "armv7em")
824 DAL->AddJoinedArg(0, MArch, "armv7em");
Bob Wilson336bfa32012-09-29 23:52:50 +0000825 else if (Name == "armv7f")
826 DAL->AddJoinedArg(0, MArch, "armv7f");
827 else if (Name == "armv7k")
828 DAL->AddJoinedArg(0, MArch, "armv7k");
Bob Wilson2503ebd2013-03-04 22:37:49 +0000829 else if (Name == "armv7m")
830 DAL->AddJoinedArg(0, MArch, "armv7m");
Bob Wilson336bfa32012-09-29 23:52:50 +0000831 else if (Name == "armv7s")
832 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000833
834 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000835 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000836 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000837
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000838 // Add an explicit version min argument for the deployment target. We do this
839 // after argument translation because -Xarch_ arguments may add a version min
840 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000841 if (BoundArch)
842 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000843
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000844 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
845 // FIXME: It would be far better to avoid inserting those -static arguments,
846 // but we can't check the deployment target in the translation code until
847 // it is set here.
848 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
849 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
850 Arg *A = *it;
851 ++it;
852 if (A->getOption().getID() != options::OPT_mkernel &&
853 A->getOption().getID() != options::OPT_fapple_kext)
854 continue;
855 assert(it != ie && "unexpected argument translation");
856 A = *it;
857 assert(A->getOption().getID() == options::OPT_static &&
858 "missing expected -static argument");
859 it = DAL->getArgs().erase(it);
860 }
861 }
862
Bob Wilson163b1512011-10-07 17:54:41 +0000863 // Validate the C++ standard library choice.
864 CXXStdlibType Type = GetCXXStdlibType(*DAL);
865 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000866 // Check whether the target provides libc++.
867 StringRef where;
868
869 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson377e5c12012-11-09 01:59:30 +0000870 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
871 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000872
873 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000874 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000875 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000876 }
877 }
878
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000879 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000880}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000881
Daniel Dunbarf3955282009-09-04 18:34:51 +0000882bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000883 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000884}
885
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000886bool Darwin::UseDwarfDebugFlags() const {
887 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
888 return S[0] != '\0';
889 return false;
890}
891
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000892bool Darwin::UseSjLjExceptions() const {
893 // Darwin uses SjLj exceptions on ARM.
894 return (getTriple().getArch() == llvm::Triple::arm ||
895 getTriple().getArch() == llvm::Triple::thumb);
896}
897
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000898bool Darwin::isPICDefault() const {
899 return true;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000900}
901
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000902bool Darwin::isPICDefaultForced() const {
903 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000904}
905
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000906bool Darwin::SupportsProfiling() const {
907 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000908 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000909}
910
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000911bool Darwin::SupportsObjCGC() const {
912 // Garbage collection is supported everywhere except on iPhone OS.
913 return !isTargetIPhoneOS();
914}
915
John McCall0a7dd782012-08-21 02:47:43 +0000916void Darwin::CheckObjCARC() const {
917 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
918 return;
John McCall80fd37a2012-08-27 01:56:21 +0000919 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000920}
921
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000922std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000923Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
924 types::ID InputType) const {
925 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000926}
927
Daniel Dunbar39176082009-03-20 00:20:03 +0000928/// Generic_GCC - A tool chain using the 'gcc' command to perform
929/// all subcommands; this relies on gcc translating the majority of
930/// command line options.
931
Chandler Carruth19347ed2011-11-06 23:39:34 +0000932/// \brief Parse a GCCVersion object out of a string of text.
933///
934/// This is the primary means of forming GCCVersion objects.
935/*static*/
936Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
937 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
938 std::pair<StringRef, StringRef> First = VersionText.split('.');
939 std::pair<StringRef, StringRef> Second = First.second.split('.');
940
941 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
942 if (First.first.getAsInteger(10, GoodVersion.Major) ||
943 GoodVersion.Major < 0)
944 return BadVersion;
945 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
946 GoodVersion.Minor < 0)
947 return BadVersion;
948
949 // First look for a number prefix and parse that if present. Otherwise just
950 // stash the entire patch string in the suffix, and leave the number
951 // unspecified. This covers versions strings such as:
952 // 4.4
953 // 4.4.0
954 // 4.4.x
955 // 4.4.2-rc4
956 // 4.4.x-patched
957 // And retains any patch number it finds.
958 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
959 if (!PatchText.empty()) {
Will Dietzca1ad502013-01-10 22:20:02 +0000960 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
Chandler Carruth19347ed2011-11-06 23:39:34 +0000961 // Try to parse the number and any suffix.
962 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
963 GoodVersion.Patch < 0)
964 return BadVersion;
965 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
966 }
967 }
968
969 return GoodVersion;
970}
971
972/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
973bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
Chandler Carruthf1757652012-12-29 12:01:08 +0000974 if (Major != RHS.Major)
975 return Major < RHS.Major;
976 if (Minor != RHS.Minor)
977 return Minor < RHS.Minor;
978 if (Patch != RHS.Patch) {
979 // Note that versions without a specified patch sort higher than those with
980 // a patch.
981 if (RHS.Patch == -1)
982 return true;
983 if (Patch == -1)
984 return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000985
Chandler Carruthf1757652012-12-29 12:01:08 +0000986 // Otherwise just sort on the patch itself.
987 return Patch < RHS.Patch;
988 }
989 if (PatchSuffix != RHS.PatchSuffix) {
990 // Sort empty suffixes higher.
991 if (RHS.PatchSuffix.empty())
992 return true;
993 if (PatchSuffix.empty())
Chandler Carruth5ba0c8e2012-12-29 13:00:47 +0000994 return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000995
Chandler Carruthf1757652012-12-29 12:01:08 +0000996 // Provide a lexicographic sort to make this a total ordering.
997 return PatchSuffix < RHS.PatchSuffix;
998 }
999
1000 // The versions are equal.
Chandler Carruth19347ed2011-11-06 23:39:34 +00001001 return false;
1002}
1003
Rafael Espindola0e659592012-02-19 01:38:32 +00001004static StringRef getGCCToolchainDir(const ArgList &Args) {
1005 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1006 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +00001007 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +00001008 return GCC_INSTALL_PREFIX;
1009}
1010
Chandler Carruth19347ed2011-11-06 23:39:34 +00001011/// \brief Construct a GCCInstallationDetector from the driver.
1012///
1013/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +00001014/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001015///
1016/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1017/// should instead pull the target out of the driver. This is currently
1018/// necessary because the driver doesn't store the final version of the target
1019/// triple.
1020Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1021 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001022 const llvm::Triple &TargetTriple,
1023 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001024 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001025 llvm::Triple MultiarchTriple
1026 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1027 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001028 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001029 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001030 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001031 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001032 SmallVector<StringRef, 10> CandidateTripleAliases;
1033 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1034 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1035 CandidateTripleAliases,
1036 CandidateMultiarchLibDirs,
1037 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001038
1039 // Compute the set of prefixes for our search.
1040 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1041 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001042
Rafael Espindola0e659592012-02-19 01:38:32 +00001043 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1044 if (GCCToolchainDir != "") {
1045 if (GCCToolchainDir.back() == '/')
1046 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001047
Rafael Espindola0e659592012-02-19 01:38:32 +00001048 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001049 } else {
1050 Prefixes.push_back(D.SysRoot);
1051 Prefixes.push_back(D.SysRoot + "/usr");
1052 Prefixes.push_back(D.InstalledDir + "/..");
1053 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001054
1055 // Loop over the various components which exist and select the best GCC
1056 // installation available. GCC installs are ranked by version number.
1057 Version = GCCVersion::Parse("0.0.0");
1058 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1059 if (!llvm::sys::fs::exists(Prefixes[i]))
1060 continue;
1061 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1062 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1063 if (!llvm::sys::fs::exists(LibDir))
1064 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001065 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001066 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1067 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001068 }
1069 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1070 const std::string LibDir
1071 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1072 if (!llvm::sys::fs::exists(LibDir))
1073 continue;
1074 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1075 ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001076 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001077 CandidateMultiarchTripleAliases[k],
1078 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001079 }
1080 }
1081}
1082
1083/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001084 const llvm::Triple &TargetTriple,
1085 const llvm::Triple &MultiarchTriple,
1086 SmallVectorImpl<StringRef> &LibDirs,
1087 SmallVectorImpl<StringRef> &TripleAliases,
1088 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1089 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1090 // Declare a bunch of static data sets that we'll select between below. These
1091 // are specifically designed to always refer to string literals to avoid any
1092 // lifetime or initialization issues.
Tim Northoverc264e162013-01-31 12:13:10 +00001093 static const char *const AArch64LibDirs[] = { "/lib" };
1094 static const char *const AArch64Triples[] = {
1095 "aarch64-none-linux-gnu",
1096 "aarch64-linux-gnu"
1097 };
1098
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001099 static const char *const ARMLibDirs[] = { "/lib" };
1100 static const char *const ARMTriples[] = {
1101 "arm-linux-gnueabi",
1102 "arm-linux-androideabi"
1103 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001104 static const char *const ARMHFTriples[] = {
1105 "arm-linux-gnueabihf",
1106 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001107
1108 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1109 static const char *const X86_64Triples[] = {
1110 "x86_64-linux-gnu",
1111 "x86_64-unknown-linux-gnu",
1112 "x86_64-pc-linux-gnu",
1113 "x86_64-redhat-linux6E",
1114 "x86_64-redhat-linux",
1115 "x86_64-suse-linux",
1116 "x86_64-manbo-linux-gnu",
1117 "x86_64-linux-gnu",
1118 "x86_64-slackware-linux"
1119 };
1120 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1121 static const char *const X86Triples[] = {
1122 "i686-linux-gnu",
1123 "i686-pc-linux-gnu",
1124 "i486-linux-gnu",
1125 "i386-linux-gnu",
NAKAMURA Takumi2a907f82012-12-07 17:13:18 +00001126 "i386-redhat-linux6E",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001127 "i686-redhat-linux",
1128 "i586-redhat-linux",
1129 "i386-redhat-linux",
1130 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001131 "i486-slackware-linux",
1132 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001133 };
1134
1135 static const char *const MIPSLibDirs[] = { "/lib" };
1136 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1137 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001138 static const char *const MIPSELTriples[] = {
1139 "mipsel-linux-gnu",
1140 "mipsel-linux-android"
1141 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001142
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001143 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1144 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1145 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1146 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1147
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001148 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1149 static const char *const PPCTriples[] = {
1150 "powerpc-linux-gnu",
1151 "powerpc-unknown-linux-gnu",
Sylvestre Ledrub7e86be2013-03-15 16:22:43 +00001152 "powerpc-linux-gnuspe",
Gabor Greif91720912012-05-15 11:21:03 +00001153 "powerpc-suse-linux",
1154 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001155 };
1156 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1157 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001158 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001159 "powerpc64-unknown-linux-gnu",
1160 "powerpc64-suse-linux",
1161 "ppc64-redhat-linux"
1162 };
1163
1164 switch (TargetTriple.getArch()) {
Tim Northoverc264e162013-01-31 12:13:10 +00001165 case llvm::Triple::aarch64:
Eric Christopher4f4e2af2013-02-05 07:29:49 +00001166 LibDirs.append(AArch64LibDirs, AArch64LibDirs
Tim Northoverc264e162013-01-31 12:13:10 +00001167 + llvm::array_lengthof(AArch64LibDirs));
1168 TripleAliases.append(
1169 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1170 MultiarchLibDirs.append(
1171 AArch64LibDirs, AArch64LibDirs + llvm::array_lengthof(AArch64LibDirs));
1172 MultiarchTripleAliases.append(
1173 AArch64Triples, AArch64Triples + llvm::array_lengthof(AArch64Triples));
1174 break;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001175 case llvm::Triple::arm:
1176 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001177 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001178 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1179 TripleAliases.append(
1180 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1181 } else {
1182 TripleAliases.append(
1183 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1184 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001185 break;
1186 case llvm::Triple::x86_64:
1187 LibDirs.append(
1188 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1189 TripleAliases.append(
1190 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1191 MultiarchLibDirs.append(
1192 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1193 MultiarchTripleAliases.append(
1194 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1195 break;
1196 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001197 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001198 TripleAliases.append(
1199 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1200 MultiarchLibDirs.append(
1201 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1202 MultiarchTripleAliases.append(
1203 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1204 break;
1205 case llvm::Triple::mips:
1206 LibDirs.append(
1207 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1208 TripleAliases.append(
1209 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001210 MultiarchLibDirs.append(
1211 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1212 MultiarchTripleAliases.append(
1213 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001214 break;
1215 case llvm::Triple::mipsel:
1216 LibDirs.append(
1217 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1218 TripleAliases.append(
1219 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001220 MultiarchLibDirs.append(
1221 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1222 MultiarchTripleAliases.append(
1223 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1224 break;
1225 case llvm::Triple::mips64:
1226 LibDirs.append(
1227 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1228 TripleAliases.append(
1229 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1230 MultiarchLibDirs.append(
1231 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1232 MultiarchTripleAliases.append(
1233 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1234 break;
1235 case llvm::Triple::mips64el:
1236 LibDirs.append(
1237 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1238 TripleAliases.append(
1239 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1240 MultiarchLibDirs.append(
1241 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1242 MultiarchTripleAliases.append(
1243 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001244 break;
1245 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001246 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001247 TripleAliases.append(
1248 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1249 MultiarchLibDirs.append(
1250 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1251 MultiarchTripleAliases.append(
1252 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1253 break;
1254 case llvm::Triple::ppc64:
1255 LibDirs.append(
1256 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1257 TripleAliases.append(
1258 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1259 MultiarchLibDirs.append(
1260 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1261 MultiarchTripleAliases.append(
1262 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1263 break;
1264
1265 default:
1266 // By default, just rely on the standard lib directories and the original
1267 // triple.
1268 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001269 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001270
1271 // Always append the drivers target triple to the end, in case it doesn't
1272 // match any of our aliases.
1273 TripleAliases.push_back(TargetTriple.str());
1274
1275 // Also include the multiarch variant if it's different.
1276 if (TargetTriple.str() != MultiarchTriple.str())
1277 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001278}
1279
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001280// FIXME: There is the same routine in the Tools.cpp.
1281static bool hasMipsN32ABIArg(const ArgList &Args) {
1282 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00001283 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001284}
1285
1286static StringRef getTargetMultiarchSuffix(llvm::Triple::ArchType TargetArch,
1287 const ArgList &Args) {
1288 if (TargetArch == llvm::Triple::x86_64 ||
1289 TargetArch == llvm::Triple::ppc64)
1290 return "/64";
1291
1292 if (TargetArch == llvm::Triple::mips64 ||
1293 TargetArch == llvm::Triple::mips64el) {
1294 if (hasMipsN32ABIArg(Args))
1295 return "/n32";
1296 else
1297 return "/64";
1298 }
1299
1300 return "/32";
1301}
1302
Chandler Carruth19347ed2011-11-06 23:39:34 +00001303void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001304 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1305 const std::string &LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001306 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001307 // There are various different suffixes involving the triple we
1308 // check for. We also record what is necessary to walk from each back
1309 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001310 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001311 "/gcc/" + CandidateTriple.str(),
1312 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1313
Hal Finkel02014b42012-09-18 22:25:07 +00001314 // The Freescale PPC SDK has the gcc libraries in
1315 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1316 "/" + CandidateTriple.str(),
1317
Chandler Carruth19347ed2011-11-06 23:39:34 +00001318 // Ubuntu has a strange mis-matched pair of triples that this happens to
1319 // match.
1320 // FIXME: It may be worthwhile to generalize this and look for a second
1321 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001322 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001323 };
1324 const std::string InstallSuffixes[] = {
1325 "/../../..",
1326 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001327 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001328 "/../../../.."
1329 };
1330 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001331 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1332 (TargetArch != llvm::Triple::x86));
1333 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1334 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001335 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001336 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001337 !EC && LI != LE; LI = LI.increment(EC)) {
1338 StringRef VersionText = llvm::sys::path::filename(LI->path());
1339 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1340 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1341 if (CandidateVersion < MinVersion)
1342 continue;
1343 if (CandidateVersion <= Version)
1344 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001345
1346 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001347 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001348 // libs in a subdirectory named 64. The simple logic we follow is that
1349 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1350 // we use that. If not, and if not a multiarch triple, we look for
1351 // crtbegin.o without the subdirectory.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001352 StringRef MultiarchSuffix = getTargetMultiarchSuffix(TargetArch, Args);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001353 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1354 GCCMultiarchSuffix = MultiarchSuffix.str();
1355 } else {
1356 if (NeedsMultiarchSuffix ||
1357 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1358 continue;
1359 GCCMultiarchSuffix.clear();
1360 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001361
1362 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001363 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001364 // FIXME: We hack together the directory name here instead of
1365 // using LI to ensure stable path separators across Windows and
1366 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001367 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001368 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001369 IsValid = true;
1370 }
1371 }
1372}
1373
Rafael Espindola0e659592012-02-19 01:38:32 +00001374Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1375 const ArgList &Args)
1376 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001377 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001378 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001379 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001380}
1381
Daniel Dunbar39176082009-03-20 00:20:03 +00001382Generic_GCC::~Generic_GCC() {
1383 // Free tool implementations.
1384 for (llvm::DenseMap<unsigned, Tool*>::iterator
1385 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1386 delete it->second;
1387}
1388
Mike Stump1eb44332009-09-09 15:08:12 +00001389Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001390 const JobAction &JA,
1391 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001392 Action::ActionClass Key;
1393 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1394 Key = Action::AnalyzeJobClass;
1395 else
1396 Key = JA.getKind();
Daniel Dunbar39176082009-03-20 00:20:03 +00001397
1398 Tool *&T = Tools[Key];
1399 if (!T) {
1400 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001401 case Action::InputClass:
1402 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001403 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001404 case Action::PreprocessJobClass:
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001405 T = new tools::gcc::Preprocess(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001406 case Action::PrecompileJobClass:
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001407 T = new tools::gcc::Precompile(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001408 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001409 case Action::MigrateJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001410 T = new tools::Clang(*this); break;
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001411 case Action::CompileJobClass:
1412 T = new tools::gcc::Compile(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001413 case Action::AssembleJobClass:
1414 T = new tools::gcc::Assemble(*this); break;
1415 case Action::LinkJobClass:
1416 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001418 // This is a bit ungeneric, but the only platform using a driver
1419 // driver is Darwin.
1420 case Action::LipoJobClass:
1421 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001422 case Action::DsymutilJobClass:
1423 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001424 case Action::VerifyJobClass:
1425 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001426 }
1427 }
1428
1429 return *T;
1430}
1431
Daniel Dunbar39176082009-03-20 00:20:03 +00001432bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00001433 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00001434}
1435
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001436bool Generic_GCC::isPICDefault() const {
1437 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00001438}
1439
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001440bool Generic_GCC::isPICDefaultForced() const {
1441 return false;
Daniel Dunbar39176082009-03-20 00:20:03 +00001442}
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001443
Tony Linthicum96319392011-12-12 21:14:55 +00001444/// Hexagon Toolchain
1445
Matthew Curtisb3489a02012-12-06 12:43:18 +00001446std::string Hexagon_TC::GetGnuDir(const std::string &InstalledDir) {
1447
1448 // Locate the rest of the toolchain ...
1449 if (strlen(GCC_INSTALL_PREFIX))
1450 return std::string(GCC_INSTALL_PREFIX);
1451
1452 std::string InstallRelDir = InstalledDir + "/../../gnu";
1453 if (llvm::sys::fs::exists(InstallRelDir))
1454 return InstallRelDir;
1455
1456 std::string PrefixRelDir = std::string(LLVM_PREFIX) + "/../gnu";
1457 if (llvm::sys::fs::exists(PrefixRelDir))
1458 return PrefixRelDir;
1459
1460 return InstallRelDir;
1461}
1462
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001463static void GetHexagonLibraryPaths(
1464 const ArgList &Args,
1465 const std::string Ver,
1466 const std::string MarchString,
1467 const std::string &InstalledDir,
1468 ToolChain::path_list *LibPaths)
1469{
1470 bool buildingLib = Args.hasArg(options::OPT_shared);
1471
1472 //----------------------------------------------------------------------------
1473 // -L Args
1474 //----------------------------------------------------------------------------
1475 for (arg_iterator
1476 it = Args.filtered_begin(options::OPT_L),
1477 ie = Args.filtered_end();
1478 it != ie;
1479 ++it) {
1480 for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
1481 LibPaths->push_back((*it)->getValue(i));
1482 }
1483
1484 //----------------------------------------------------------------------------
1485 // Other standard paths
1486 //----------------------------------------------------------------------------
1487 const std::string MarchSuffix = "/" + MarchString;
1488 const std::string G0Suffix = "/G0";
1489 const std::string MarchG0Suffix = MarchSuffix + G0Suffix;
1490 const std::string RootDir = Hexagon_TC::GetGnuDir(InstalledDir) + "/";
1491
1492 // lib/gcc/hexagon/...
1493 std::string LibGCCHexagonDir = RootDir + "lib/gcc/hexagon/";
1494 if (buildingLib) {
1495 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchG0Suffix);
1496 LibPaths->push_back(LibGCCHexagonDir + Ver + G0Suffix);
1497 }
1498 LibPaths->push_back(LibGCCHexagonDir + Ver + MarchSuffix);
1499 LibPaths->push_back(LibGCCHexagonDir + Ver);
1500
1501 // lib/gcc/...
1502 LibPaths->push_back(RootDir + "lib/gcc");
1503
1504 // hexagon/lib/...
1505 std::string HexagonLibDir = RootDir + "hexagon/lib";
1506 if (buildingLib) {
1507 LibPaths->push_back(HexagonLibDir + MarchG0Suffix);
1508 LibPaths->push_back(HexagonLibDir + G0Suffix);
1509 }
1510 LibPaths->push_back(HexagonLibDir + MarchSuffix);
1511 LibPaths->push_back(HexagonLibDir);
1512}
1513
Matthew Curtisb3489a02012-12-06 12:43:18 +00001514Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple &Triple,
1515 const ArgList &Args)
1516 : Linux(D, Triple, Args) {
1517 const std::string InstalledDir(getDriver().getInstalledDir());
1518 const std::string GnuDir = Hexagon_TC::GetGnuDir(InstalledDir);
1519
1520 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
1521 // program paths
1522 const std::string BinDir(GnuDir + "/bin");
1523 if (llvm::sys::fs::exists(BinDir))
1524 getProgramPaths().push_back(BinDir);
1525
1526 // Determine version of GCC libraries and headers to use.
1527 const std::string HexagonDir(GnuDir + "/lib/gcc/hexagon");
1528 llvm::error_code ec;
1529 GCCVersion MaxVersion= GCCVersion::Parse("0.0.0");
1530 for (llvm::sys::fs::directory_iterator di(HexagonDir, ec), de;
1531 !ec && di != de; di = di.increment(ec)) {
1532 GCCVersion cv = GCCVersion::Parse(llvm::sys::path::filename(di->path()));
1533 if (MaxVersion < cv)
1534 MaxVersion = cv;
1535 }
1536 GCCLibAndIncVersion = MaxVersion;
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001537
1538 ToolChain::path_list *LibPaths= &getFilePaths();
1539
1540 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
1541 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
1542 // support 'linux' we'll need to fix this up
1543 LibPaths->clear();
1544
1545 GetHexagonLibraryPaths(
1546 Args,
1547 GetGCCLibAndIncVersion(),
1548 GetTargetCPU(Args),
1549 InstalledDir,
1550 LibPaths);
Tony Linthicum96319392011-12-12 21:14:55 +00001551}
1552
1553Hexagon_TC::~Hexagon_TC() {
1554 // Free tool implementations.
1555 for (llvm::DenseMap<unsigned, Tool*>::iterator
1556 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1557 delete it->second;
1558}
1559
1560Tool &Hexagon_TC::SelectTool(const Compilation &C,
1561 const JobAction &JA,
1562 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001563 Action::ActionClass Key;
1564 // if (JA.getKind () == Action::CompileJobClass)
1565 // Key = JA.getKind ();
1566 // else
1567
1568 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1569 Key = Action::AnalyzeJobClass;
1570 else
1571 Key = JA.getKind();
1572 // if ((JA.getKind () == Action::CompileJobClass)
1573 // && (JA.getType () != types::TY_LTO_BC)) {
1574 // Key = JA.getKind ();
1575 // }
1576
Tony Linthicum96319392011-12-12 21:14:55 +00001577 Tool *&T = Tools[Key];
1578 if (!T) {
1579 switch (Key) {
1580 case Action::InputClass:
1581 case Action::BindArchClass:
1582 assert(0 && "Invalid tool kind.");
1583 case Action::AnalyzeJobClass:
1584 T = new tools::Clang(*this); break;
1585 case Action::AssembleJobClass:
1586 T = new tools::hexagon::Assemble(*this); break;
1587 case Action::LinkJobClass:
1588 T = new tools::hexagon::Link(*this); break;
1589 default:
1590 assert(false && "Unsupported action for Hexagon target.");
1591 }
1592 }
1593
1594 return *T;
1595}
1596
Matthew Curtisb3489a02012-12-06 12:43:18 +00001597void Hexagon_TC::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1598 ArgStringList &CC1Args) const {
1599 const Driver &D = getDriver();
1600
1601 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
1602 DriverArgs.hasArg(options::OPT_nostdlibinc))
1603 return;
1604
1605 llvm::sys::Path InstallDir(D.InstalledDir);
1606 std::string Ver(GetGCCLibAndIncVersion());
1607 std::string GnuDir = Hexagon_TC::GetGnuDir(D.InstalledDir);
1608 std::string HexagonDir(GnuDir + "/lib/gcc/hexagon/" + Ver);
1609 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include");
1610 addExternCSystemInclude(DriverArgs, CC1Args, HexagonDir + "/include-fixed");
1611 addExternCSystemInclude(DriverArgs, CC1Args, GnuDir + "/hexagon/include");
Tony Linthicum96319392011-12-12 21:14:55 +00001612}
1613
Matthew Curtisb3489a02012-12-06 12:43:18 +00001614void Hexagon_TC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1615 ArgStringList &CC1Args) const {
1616
1617 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1618 DriverArgs.hasArg(options::OPT_nostdincxx))
1619 return;
1620
1621 const Driver &D = getDriver();
1622 std::string Ver(GetGCCLibAndIncVersion());
1623 llvm::sys::Path IncludeDir(Hexagon_TC::GetGnuDir(D.InstalledDir));
1624
1625 IncludeDir.appendComponent("hexagon/include/c++/");
1626 IncludeDir.appendComponent(Ver);
1627 addSystemInclude(DriverArgs, CC1Args, IncludeDir.str());
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001628}
Matthew Curtis67814152012-12-06 14:16:43 +00001629
Matthew Curtis5fdf3502012-12-06 15:46:07 +00001630ToolChain::CXXStdlibType
1631Hexagon_TC::GetCXXStdlibType(const ArgList &Args) const {
1632 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
1633 if (!A)
1634 return ToolChain::CST_Libstdcxx;
1635
1636 StringRef Value = A->getValue();
1637 if (Value != "libstdc++") {
1638 getDriver().Diag(diag::err_drv_invalid_stdlib_name)
1639 << A->getAsString(Args);
1640 }
1641
1642 return ToolChain::CST_Libstdcxx;
1643}
1644
1645static Arg *GetLastHexagonArchArg(const ArgList &Args)
Matthew Curtis67814152012-12-06 14:16:43 +00001646{
1647 Arg *A = NULL;
1648
1649 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
1650 it != ie; ++it) {
1651 if ((*it)->getOption().matches(options::OPT_march_EQ) ||
1652 (*it)->getOption().matches(options::OPT_mcpu_EQ)) {
1653 A = *it;
1654 A->claim();
1655 } else if ((*it)->getOption().matches(options::OPT_m_Joined)) {
1656 StringRef Value = (*it)->getValue(0);
1657 if (Value.startswith("v")) {
1658 A = *it;
1659 A->claim();
1660 }
1661 }
1662 }
1663 return A;
1664}
1665
1666StringRef Hexagon_TC::GetTargetCPU(const ArgList &Args)
1667{
1668 // Select the default CPU (v4) if none was given or detection failed.
1669 Arg *A = GetLastHexagonArchArg (Args);
1670 if (A) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001671 StringRef WhichHexagon = A->getValue();
Matthew Curtis67814152012-12-06 14:16:43 +00001672 if (WhichHexagon.startswith("hexagon"))
1673 return WhichHexagon.substr(sizeof("hexagon") - 1);
1674 if (WhichHexagon != "")
1675 return WhichHexagon;
1676 }
1677
1678 return "v4";
1679}
Matthew Curtisb3489a02012-12-06 12:43:18 +00001680// End Hexagon
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001681
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001682/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1683/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1684/// Currently does not support anything else but compilation.
1685
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001686TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1687 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001688 // Path mangling to find libexec
1689 std::string Path(getDriver().Dir);
1690
1691 Path += "/../libexec";
1692 getProgramPaths().push_back(Path);
1693}
1694
1695TCEToolChain::~TCEToolChain() {
1696 for (llvm::DenseMap<unsigned, Tool*>::iterator
1697 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1698 delete it->second;
1699}
1700
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001701bool TCEToolChain::IsMathErrnoDefault() const {
1702 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001703}
1704
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001705bool TCEToolChain::isPICDefault() const {
1706 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001707}
1708
Chandler Carruth7ce816a2012-11-19 03:52:03 +00001709bool TCEToolChain::isPICDefaultForced() const {
1710 return false;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001711}
1712
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001713Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001714 const JobAction &JA,
1715 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001716 Action::ActionClass Key;
1717 Key = Action::AnalyzeJobClass;
1718
1719 Tool *&T = Tools[Key];
1720 if (!T) {
1721 switch (Key) {
1722 case Action::PreprocessJobClass:
1723 T = new tools::gcc::Preprocess(*this); break;
1724 case Action::AnalyzeJobClass:
1725 T = new tools::Clang(*this); break;
1726 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001727 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001728 }
1729 }
1730 return *T;
1731}
1732
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001733/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1734
Rafael Espindola0e659592012-02-19 01:38:32 +00001735OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1736 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001737 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001738 getFilePaths().push_back("/usr/lib");
1739}
1740
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001741Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1742 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001743 Action::ActionClass Key;
1744 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1745 Key = Action::AnalyzeJobClass;
1746 else
1747 Key = JA.getKind();
1748
Rafael Espindoladda5b922010-11-07 23:13:01 +00001749 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1750 options::OPT_no_integrated_as,
1751 IsIntegratedAssemblerDefault());
1752
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001753 Tool *&T = Tools[Key];
1754 if (!T) {
1755 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001756 case Action::AssembleJobClass: {
1757 if (UseIntegratedAs)
1758 T = new tools::ClangAs(*this);
1759 else
1760 T = new tools::openbsd::Assemble(*this);
1761 break;
1762 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001763 case Action::LinkJobClass:
1764 T = new tools::openbsd::Link(*this); break;
1765 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001766 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001767 }
1768 }
1769
1770 return *T;
1771}
1772
Eli Friedman42f74f22012-08-08 23:57:20 +00001773/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1774
1775Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1776 : Generic_ELF(D, Triple, Args) {
1777 getFilePaths().push_back(getDriver().Dir + "/../lib");
1778 getFilePaths().push_back("/usr/lib");
1779}
1780
1781Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA,
1782 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001783 Action::ActionClass Key;
1784 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1785 Key = Action::AnalyzeJobClass;
1786 else
1787 Key = JA.getKind();
1788
Eli Friedman42f74f22012-08-08 23:57:20 +00001789 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1790 options::OPT_no_integrated_as,
1791 IsIntegratedAssemblerDefault());
1792
1793 Tool *&T = Tools[Key];
1794 if (!T) {
1795 switch (Key) {
1796 case Action::AssembleJobClass: {
1797 if (UseIntegratedAs)
1798 T = new tools::ClangAs(*this);
1799 else
1800 T = new tools::bitrig::Assemble(*this);
1801 break;
1802 }
1803 case Action::LinkJobClass:
1804 T = new tools::bitrig::Link(*this); break;
1805 default:
1806 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1807 }
1808 }
1809
1810 return *T;
1811}
1812
1813void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1814 ArgStringList &CC1Args) const {
1815 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1816 DriverArgs.hasArg(options::OPT_nostdincxx))
1817 return;
1818
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001819 switch (GetCXXStdlibType(DriverArgs)) {
1820 case ToolChain::CST_Libcxx:
1821 addSystemInclude(DriverArgs, CC1Args,
1822 getDriver().SysRoot + "/usr/include/c++/");
1823 break;
1824 case ToolChain::CST_Libstdcxx:
1825 addSystemInclude(DriverArgs, CC1Args,
1826 getDriver().SysRoot + "/usr/include/c++/stdc++");
1827 addSystemInclude(DriverArgs, CC1Args,
1828 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001829
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001830 StringRef Triple = getTriple().str();
1831 if (Triple.startswith("amd64"))
1832 addSystemInclude(DriverArgs, CC1Args,
1833 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1834 Triple.substr(5));
1835 else
1836 addSystemInclude(DriverArgs, CC1Args,
1837 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1838 Triple);
1839 break;
1840 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001841}
1842
1843void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1844 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001845 switch (GetCXXStdlibType(Args)) {
1846 case ToolChain::CST_Libcxx:
1847 CmdArgs.push_back("-lc++");
1848 CmdArgs.push_back("-lcxxrt");
1849 // Include supc++ to provide Unwind until provided by libcxx.
1850 CmdArgs.push_back("-lgcc");
1851 break;
1852 case ToolChain::CST_Libstdcxx:
1853 CmdArgs.push_back("-lstdc++");
1854 break;
1855 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001856}
1857
Daniel Dunbar75358d22009-03-30 21:06:03 +00001858/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1859
Rafael Espindola0e659592012-02-19 01:38:32 +00001860FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1861 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001862
Chandler Carruth24248e32012-01-26 01:35:15 +00001863 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1864 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001865 if ((Triple.getArch() == llvm::Triple::x86 ||
1866 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001867 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001868 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1869 else
1870 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001871}
1872
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001873Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1874 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001875 Action::ActionClass Key;
1876 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1877 Key = Action::AnalyzeJobClass;
1878 else
1879 Key = JA.getKind();
1880
Roman Divacky67dece72010-11-08 17:46:39 +00001881 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1882 options::OPT_no_integrated_as,
1883 IsIntegratedAssemblerDefault());
1884
Daniel Dunbar75358d22009-03-30 21:06:03 +00001885 Tool *&T = Tools[Key];
1886 if (!T) {
1887 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001888 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001889 if (UseIntegratedAs)
1890 T = new tools::ClangAs(*this);
1891 else
1892 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001893 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001894 case Action::LinkJobClass:
1895 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001896 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001897 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001898 }
1899 }
1900
1901 return *T;
1902}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001903
Rafael Espindola27fa2362012-12-13 04:17:14 +00001904bool FreeBSD::UseSjLjExceptions() const {
1905 // FreeBSD uses SjLj exceptions on ARM oabi.
1906 switch (getTriple().getEnvironment()) {
1907 case llvm::Triple::GNUEABI:
1908 case llvm::Triple::EABI:
1909 return false;
1910
1911 default:
1912 return (getTriple().getArch() == llvm::Triple::arm ||
1913 getTriple().getArch() == llvm::Triple::thumb);
1914 }
1915}
1916
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001917/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1918
Rafael Espindola0e659592012-02-19 01:38:32 +00001919NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1920 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001921
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001922 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001923 // When targeting a 32-bit platform, try the special directory used on
1924 // 64-bit hosts, and only fall back to the main library directory if that
1925 // doesn't work.
1926 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1927 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001928 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001929 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001930
1931 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001932 }
1933}
1934
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001935Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1936 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001937 Action::ActionClass Key;
1938 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1939 Key = Action::AnalyzeJobClass;
1940 else
1941 Key = JA.getKind();
1942
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001943 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1944 options::OPT_no_integrated_as,
1945 IsIntegratedAssemblerDefault());
1946
1947 Tool *&T = Tools[Key];
1948 if (!T) {
1949 switch (Key) {
1950 case Action::AssembleJobClass:
1951 if (UseIntegratedAs)
1952 T = new tools::ClangAs(*this);
1953 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001954 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001955 break;
1956 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001957 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001958 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001959 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001960 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001961 }
1962 }
1963
1964 return *T;
1965}
1966
Chris Lattner38e317d2010-07-07 16:01:42 +00001967/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1968
Rafael Espindola0e659592012-02-19 01:38:32 +00001969Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1970 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001971 getFilePaths().push_back(getDriver().Dir + "/../lib");
1972 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001973}
1974
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001975Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1976 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00001977 Action::ActionClass Key;
1978 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1979 Key = Action::AnalyzeJobClass;
1980 else
1981 Key = JA.getKind();
Chris Lattner38e317d2010-07-07 16:01:42 +00001982
1983 Tool *&T = Tools[Key];
1984 if (!T) {
1985 switch (Key) {
1986 case Action::AssembleJobClass:
1987 T = new tools::minix::Assemble(*this); break;
1988 case Action::LinkJobClass:
1989 T = new tools::minix::Link(*this); break;
1990 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001991 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001992 }
1993 }
1994
1995 return *T;
1996}
1997
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001998/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1999
Rafael Espindola0e659592012-02-19 01:38:32 +00002000AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
2001 const ArgList &Args)
2002 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002003
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002004 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002005 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002006 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002007
Daniel Dunbaree788e72009-12-21 18:54:17 +00002008 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002009 getFilePaths().push_back("/usr/lib");
2010 getFilePaths().push_back("/usr/sfw/lib");
2011 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002012 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002013
2014}
2015
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002016Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
2017 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00002018 Action::ActionClass Key;
2019 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2020 Key = Action::AnalyzeJobClass;
2021 else
2022 Key = JA.getKind();
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002023
2024 Tool *&T = Tools[Key];
2025 if (!T) {
2026 switch (Key) {
2027 case Action::AssembleJobClass:
2028 T = new tools::auroraux::Assemble(*this); break;
2029 case Action::LinkJobClass:
2030 T = new tools::auroraux::Link(*this); break;
2031 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002032 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002033 }
2034 }
2035
2036 return *T;
2037}
2038
David Chisnall31c46902012-02-15 13:39:01 +00002039/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
2040
Rafael Espindola0e659592012-02-19 01:38:32 +00002041Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
2042 const ArgList &Args)
2043 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00002044
2045 getProgramPaths().push_back(getDriver().getInstalledDir());
2046 if (getDriver().getInstalledDir() != getDriver().Dir)
2047 getProgramPaths().push_back(getDriver().Dir);
2048
2049 getFilePaths().push_back(getDriver().Dir + "/../lib");
2050 getFilePaths().push_back("/usr/lib");
2051}
2052
2053Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
2054 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00002055 Action::ActionClass Key;
2056 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2057 Key = Action::AnalyzeJobClass;
2058 else
2059 Key = JA.getKind();
David Chisnall31c46902012-02-15 13:39:01 +00002060
2061 Tool *&T = Tools[Key];
2062 if (!T) {
2063 switch (Key) {
2064 case Action::AssembleJobClass:
2065 T = new tools::solaris::Assemble(*this); break;
2066 case Action::LinkJobClass:
2067 T = new tools::solaris::Link(*this); break;
2068 default:
2069 T = &Generic_GCC::SelectTool(C, JA, Inputs);
2070 }
2071 }
2072
2073 return *T;
2074}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002075
Eli Friedman6b3454a2009-05-26 07:52:18 +00002076/// Linux toolchain (very bare-bones at the moment).
2077
Rafael Espindolac1da9812010-11-07 20:14:31 +00002078enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002079 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002080 DebianLenny,
2081 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00002082 DebianWheezy,
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002083 DebianJessie,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00002084 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00002085 RHEL4,
2086 RHEL5,
2087 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002088 Fedora13,
2089 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00002090 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002091 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00002092 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002093 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00002094 OpenSuse11_4,
2095 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002096 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00002097 UbuntuHardy,
2098 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00002099 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00002100 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002101 UbuntuLucid,
2102 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00002103 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00002104 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002105 UbuntuPrecise,
Rafael Espindolade39d172012-12-13 20:26:05 +00002106 UbuntuQuantal,
2107 UbuntuRaring,
Rafael Espindolac1da9812010-11-07 20:14:31 +00002108 UnknownDistro
2109};
2110
Chris Lattnerd753b562011-05-22 05:36:06 +00002111static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002112 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
2113 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002114}
2115
2116static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002117 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002118}
2119
2120static bool IsDebian(enum LinuxDistro Distro) {
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002121 return Distro >= DebianLenny && Distro <= DebianJessie;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002122}
2123
2124static bool IsUbuntu(enum LinuxDistro Distro) {
Rafael Espindolade39d172012-12-13 20:26:05 +00002125 return Distro >= UbuntuHardy && Distro <= UbuntuRaring;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002126}
2127
Rafael Espindolac1da9812010-11-07 20:14:31 +00002128static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00002129 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002130 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002131 StringRef Data = File.get()->getBuffer();
2132 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002133 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002134 LinuxDistro Version = UnknownDistro;
2135 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
2136 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
2137 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
2138 .Case("hardy", UbuntuHardy)
2139 .Case("intrepid", UbuntuIntrepid)
2140 .Case("jaunty", UbuntuJaunty)
2141 .Case("karmic", UbuntuKarmic)
2142 .Case("lucid", UbuntuLucid)
2143 .Case("maverick", UbuntuMaverick)
2144 .Case("natty", UbuntuNatty)
2145 .Case("oneiric", UbuntuOneiric)
2146 .Case("precise", UbuntuPrecise)
Rafael Espindolade39d172012-12-13 20:26:05 +00002147 .Case("quantal", UbuntuQuantal)
2148 .Case("raring", UbuntuRaring)
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002149 .Default(UnknownDistro);
2150 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002151 }
2152
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002153 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002154 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002155 if (Data.startswith("Fedora release 16"))
2156 return Fedora16;
2157 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00002158 return Fedora15;
2159 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002160 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00002161 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002162 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00002163 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002164 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00002165 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00002166 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002167 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002168 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002169 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopher4f4e2af2013-02-05 07:29:49 +00002170 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002171 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002172 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002173 else if ((Data.startswith("Red Hat Enterprise Linux") ||
Eric Christopher4f4e2af2013-02-05 07:29:49 +00002174 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002175 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002176 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002177 return UnknownDistro;
2178 }
2179
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002180 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002181 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002182 if (Data[0] == '5')
2183 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002184 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00002185 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002186 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00002187 return DebianWheezy;
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002188 else if (Data.startswith("jessie/sid") || Data[0] == '8')
2189 return DebianJessie;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002190 return UnknownDistro;
2191 }
2192
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002193 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
2194 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
2195 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2196 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2197 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002198 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002199 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002200
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00002201 bool Exists;
2202 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00002203 return Exherbo;
2204
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002205 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2206 return ArchLinux;
2207
Rafael Espindolac1da9812010-11-07 20:14:31 +00002208 return UnknownDistro;
2209}
2210
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002211/// \brief Get our best guess at the multiarch triple for a target.
2212///
2213/// Debian-based systems are starting to use a multiarch setup where they use
2214/// a target-triple directory in the library and header search paths.
2215/// Unfortunately, this triple does not align with the vanilla target triple,
2216/// so we provide a rough mapping here.
2217static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2218 StringRef SysRoot) {
2219 // For most architectures, just use whatever we have rather than trying to be
2220 // clever.
2221 switch (TargetTriple.getArch()) {
2222 default:
2223 return TargetTriple.str();
2224
2225 // We use the existence of '/lib/<triple>' as a directory to detect some
2226 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00002227 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2228 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00002229 case llvm::Triple::arm:
2230 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00002231 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2232 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2233 return "arm-linux-gnueabihf";
2234 } else {
2235 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2236 return "arm-linux-gnueabi";
2237 }
Chad Rosier0337efd2012-07-11 19:08:21 +00002238 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002239 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002240 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2241 return "i386-linux-gnu";
2242 return TargetTriple.str();
2243 case llvm::Triple::x86_64:
2244 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2245 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002246 return TargetTriple.str();
Tim Northoverc264e162013-01-31 12:13:10 +00002247 case llvm::Triple::aarch64:
2248 if (llvm::sys::fs::exists(SysRoot + "/lib/aarch64-linux-gnu"))
2249 return "aarch64-linux-gnu";
Eli Friedman5bea4f62011-11-08 19:43:37 +00002250 case llvm::Triple::mips:
2251 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2252 return "mips-linux-gnu";
2253 return TargetTriple.str();
2254 case llvm::Triple::mipsel:
2255 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2256 return "mipsel-linux-gnu";
2257 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002258 case llvm::Triple::ppc:
Sylvestre Ledrub7e86be2013-03-15 16:22:43 +00002259 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
2260 return "powerpc-linux-gnuspe";
Chandler Carruth155c54c2012-02-26 09:03:21 +00002261 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2262 return "powerpc-linux-gnu";
2263 return TargetTriple.str();
2264 case llvm::Triple::ppc64:
2265 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2266 return "powerpc64-linux-gnu";
2267 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002268 }
2269}
2270
Chandler Carruth00646ba2012-01-25 11:24:24 +00002271static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2272 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2273}
2274
Simon Atanasyan7a918882012-09-14 11:27:24 +00002275static bool isMipsArch(llvm::Triple::ArchType Arch) {
2276 return Arch == llvm::Triple::mips ||
2277 Arch == llvm::Triple::mipsel ||
2278 Arch == llvm::Triple::mips64 ||
2279 Arch == llvm::Triple::mips64el;
2280}
2281
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002282static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2283 const ArgList &Args) {
2284 if (Arch != llvm::Triple::mips &&
2285 Arch != llvm::Triple::mipsel)
2286 return false;
2287
2288 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2289 options::OPT_mcpu_EQ,
2290 options::OPT_mips_CPUs_Group);
2291
2292 if (!A)
2293 return false;
2294
2295 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2296 return A->getOption().matches(options::OPT_mips32r2);
2297
Richard Smith1d489cf2012-11-01 04:30:05 +00002298 return A->getValue() == StringRef("mips32r2");
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002299}
2300
Simon Atanasyan7a918882012-09-14 11:27:24 +00002301static StringRef getMultilibDir(const llvm::Triple &Triple,
2302 const ArgList &Args) {
2303 if (!isMipsArch(Triple.getArch()))
2304 return Triple.isArch32Bit() ? "lib32" : "lib64";
2305
2306 // lib32 directory has a special meaning on MIPS targets.
2307 // It contains N32 ABI binaries. Use this folder if produce
2308 // code for N32 ABI only.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00002309 if (hasMipsN32ABIArg(Args))
Simon Atanasyan7a918882012-09-14 11:27:24 +00002310 return "lib32";
2311
2312 return Triple.isArch32Bit() ? "lib" : "lib64";
2313}
2314
Rafael Espindola0e659592012-02-19 01:38:32 +00002315Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2316 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002317 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00002318 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002319
Rafael Espindolaab784082011-09-01 16:25:49 +00002320 // OpenSuse stores the linker with the compiler, add that to the search
2321 // path.
2322 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002323 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002324 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002325
2326 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002327
2328 LinuxDistro Distro = DetectLinuxDistro(Arch);
2329
Chris Lattner64a89172011-05-22 16:45:07 +00002330 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002331 ExtraOpts.push_back("-z");
2332 ExtraOpts.push_back("relro");
2333 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002334
Douglas Gregorf0594d82011-03-06 19:11:49 +00002335 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002336 ExtraOpts.push_back("-X");
2337
Logan Chien94a71422012-09-02 09:30:11 +00002338 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002339
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002340 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2341 // and the MIPS ABI require .dynsym to be sorted in different ways.
2342 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2343 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002344 // Android loader does not support .gnu.hash.
Simon Atanasyan7a918882012-09-14 11:27:24 +00002345 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002346 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2347 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002348 ExtraOpts.push_back("--hash-style=gnu");
2349
2350 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2351 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2352 ExtraOpts.push_back("--hash-style=both");
2353 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002354
Chris Lattnerd753b562011-05-22 05:36:06 +00002355 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002356 ExtraOpts.push_back("--no-add-needed");
2357
Eli Friedman0b200f62011-06-02 21:36:53 +00002358 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Sylvestre Ledrub1718522013-01-06 08:09:29 +00002359 Distro == DebianJessie || IsOpenSuse(Distro) ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002360 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002361 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002362 ExtraOpts.push_back("--build-id");
2363
Chris Lattner64a89172011-05-22 16:45:07 +00002364 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002365 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002366
Chandler Carruthd2deee12011-10-03 05:28:29 +00002367 // The selection of paths to try here is designed to match the patterns which
2368 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2369 // This was determined by running GCC in a fake filesystem, creating all
2370 // possible permutations of these directories, and seeing which ones it added
2371 // to the link paths.
2372 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002373
Simon Atanasyan7a918882012-09-14 11:27:24 +00002374 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002375 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002376
Chandler Carruthd1f73062011-11-06 23:09:05 +00002377 // Add the multilib suffixed paths where they are available.
2378 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002379 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002380 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002381
2382 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2383 addPathIfExists(GCCInstallation.getInstallPath() +
2384 GCCInstallation.getMultiarchSuffix() +
2385 "/mips-r2",
2386 Paths);
2387 else
2388 addPathIfExists((GCCInstallation.getInstallPath() +
2389 GCCInstallation.getMultiarchSuffix()),
2390 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002391
2392 // If the GCC installation we found is inside of the sysroot, we want to
2393 // prefer libraries installed in the parent prefix of the GCC installation.
2394 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002395 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002396 // This usually happens when there is an external cross compiler on the
2397 // host system, and a more minimal sysroot available that is the target of
2398 // the cross.
2399 if (StringRef(LibPath).startswith(SysRoot)) {
2400 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2401 Paths);
2402 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2403 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2404 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002405 // On Android, libraries in the parent prefix of the GCC installation are
2406 // preferred to the ones under sysroot.
2407 if (IsAndroid) {
2408 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2409 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002410 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002411 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2412 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2413 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2414 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2415
2416 // Try walking via the GCC triple path in case of multiarch GCC
2417 // installations with strange symlinks.
2418 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002419 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002420 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002421
Chandler Carruth7a09d012011-10-16 10:54:30 +00002422 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002423 if (GCCInstallation.isValid()) {
2424 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002425 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002426 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002427 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002428
2429 if (StringRef(LibPath).startswith(SysRoot)) {
2430 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2431 addPathIfExists(LibPath, Paths);
2432 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002433 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002434 addPathIfExists(SysRoot + "/lib", Paths);
2435 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002436}
2437
2438bool Linux::HasNativeLLVMSupport() const {
2439 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002440}
2441
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002442Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2443 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00002444 Action::ActionClass Key;
2445 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2446 Key = Action::AnalyzeJobClass;
2447 else
2448 Key = JA.getKind();
2449
Rafael Espindoladda5b922010-11-07 23:13:01 +00002450 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2451 options::OPT_no_integrated_as,
2452 IsIntegratedAssemblerDefault());
2453
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002454 Tool *&T = Tools[Key];
2455 if (!T) {
2456 switch (Key) {
2457 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002458 if (UseIntegratedAs)
2459 T = new tools::ClangAs(*this);
2460 else
2461 T = new tools::linuxtools::Assemble(*this);
2462 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002463 case Action::LinkJobClass:
2464 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002465 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002466 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002467 }
2468 }
2469
2470 return *T;
2471}
2472
Chandler Carrutha6b25812012-11-21 23:40:23 +00002473void Linux::addClangTargetOptions(const ArgList &DriverArgs,
2474 ArgStringList &CC1Args) const {
Rafael Espindola8af669f2012-06-19 01:26:10 +00002475 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
Chandler Carrutha6b25812012-11-21 23:40:23 +00002476 bool UseInitArrayDefault
2477 = V >= Generic_GCC::GCCVersion::Parse("4.7.0") ||
Tim Northoverc264e162013-01-31 12:13:10 +00002478 getTriple().getArch() == llvm::Triple::aarch64 ||
Chandler Carrutha6b25812012-11-21 23:40:23 +00002479 getTriple().getEnvironment() == llvm::Triple::Android;
2480 if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
2481 options::OPT_fno_use_init_array,
2482 UseInitArrayDefault))
Rafael Espindola8af669f2012-06-19 01:26:10 +00002483 CC1Args.push_back("-fuse-init-array");
2484}
2485
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002486void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2487 ArgStringList &CC1Args) const {
2488 const Driver &D = getDriver();
2489
2490 if (DriverArgs.hasArg(options::OPT_nostdinc))
2491 return;
2492
2493 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2494 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2495
2496 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002497 llvm::sys::Path P(D.ResourceDir);
2498 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002499 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002500 }
2501
2502 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2503 return;
2504
2505 // Check for configure-time C include directories.
2506 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2507 if (CIncludeDirs != "") {
2508 SmallVector<StringRef, 5> dirs;
2509 CIncludeDirs.split(dirs, ":");
2510 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2511 I != E; ++I) {
2512 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2513 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2514 }
2515 return;
2516 }
2517
2518 // Lacking those, try to detect the correct set of system includes for the
2519 // target triple.
2520
Chandler Carrutha4630892011-11-06 08:21:07 +00002521 // Implement generic Debian multiarch support.
2522 const StringRef X86_64MultiarchIncludeDirs[] = {
2523 "/usr/include/x86_64-linux-gnu",
2524
2525 // FIXME: These are older forms of multiarch. It's not clear that they're
2526 // in use in any released version of Debian, so we should consider
2527 // removing them.
2528 "/usr/include/i686-linux-gnu/64",
2529 "/usr/include/i486-linux-gnu/64"
2530 };
2531 const StringRef X86MultiarchIncludeDirs[] = {
2532 "/usr/include/i386-linux-gnu",
2533
2534 // FIXME: These are older forms of multiarch. It's not clear that they're
2535 // in use in any released version of Debian, so we should consider
2536 // removing them.
2537 "/usr/include/x86_64-linux-gnu/32",
2538 "/usr/include/i686-linux-gnu",
2539 "/usr/include/i486-linux-gnu"
2540 };
Tim Northoverc264e162013-01-31 12:13:10 +00002541 const StringRef AArch64MultiarchIncludeDirs[] = {
2542 "/usr/include/aarch64-linux-gnu"
2543 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002544 const StringRef ARMMultiarchIncludeDirs[] = {
2545 "/usr/include/arm-linux-gnueabi"
2546 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002547 const StringRef ARMHFMultiarchIncludeDirs[] = {
2548 "/usr/include/arm-linux-gnueabihf"
2549 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002550 const StringRef MIPSMultiarchIncludeDirs[] = {
2551 "/usr/include/mips-linux-gnu"
2552 };
2553 const StringRef MIPSELMultiarchIncludeDirs[] = {
2554 "/usr/include/mipsel-linux-gnu"
2555 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002556 const StringRef PPCMultiarchIncludeDirs[] = {
2557 "/usr/include/powerpc-linux-gnu"
2558 };
2559 const StringRef PPC64MultiarchIncludeDirs[] = {
2560 "/usr/include/powerpc64-linux-gnu"
2561 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002562 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002563 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002564 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002565 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002566 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Tim Northoverc264e162013-01-31 12:13:10 +00002567 } else if (getTriple().getArch() == llvm::Triple::aarch64) {
2568 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002569 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002570 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2571 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2572 else
2573 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002574 } else if (getTriple().getArch() == llvm::Triple::mips) {
2575 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2576 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2577 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002578 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2579 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2580 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2581 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002582 }
2583 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2584 E = MultiarchIncludeDirs.end();
2585 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002586 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002587 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2588 break;
2589 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002590 }
2591
2592 if (getTriple().getOS() == llvm::Triple::RTEMS)
2593 return;
2594
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002595 // Add an include of '/include' directly. This isn't provided by default by
2596 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2597 // add even when Clang is acting as-if it were a system compiler.
2598 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2599
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002600 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2601}
2602
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002603/// \brief Helper to add the three variant paths for a libstdc++ installation.
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002604/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2605 const ArgList &DriverArgs,
2606 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002607 if (!llvm::sys::fs::exists(Base))
2608 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002609 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002610 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002611 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002612 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002613}
2614
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002615/// \brief Helper to add an extra variant path for an (Ubuntu) multilib
2616/// libstdc++ installation.
2617/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix,
2618 Twine TargetArchDir,
2619 Twine MultiLibSuffix,
2620 const ArgList &DriverArgs,
2621 ArgStringList &CC1Args) {
2622 if (!addLibStdCXXIncludePaths(Base+Suffix, TargetArchDir + MultiLibSuffix,
2623 DriverArgs, CC1Args))
2624 return false;
2625
2626 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix
2627 + MultiLibSuffix);
2628 return true;
2629}
2630
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002631void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2632 ArgStringList &CC1Args) const {
2633 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2634 DriverArgs.hasArg(options::OPT_nostdincxx))
2635 return;
2636
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002637 // Check if libc++ has been enabled and provide its include paths if so.
2638 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2639 // libc++ is always installed at a fixed path on Linux currently.
2640 addSystemInclude(DriverArgs, CC1Args,
2641 getDriver().SysRoot + "/usr/include/c++/v1");
2642 return;
2643 }
2644
Chandler Carruthfc52f752012-01-25 08:04:13 +00002645 // We need a detected GCC installation on Linux to provide libstdc++'s
2646 // headers. We handled the libc++ case above.
2647 if (!GCCInstallation.isValid())
2648 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002649
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002650 // By default, look for the C++ headers in an include directory adjacent to
2651 // the lib directory of the GCC installation. Note that this is expect to be
2652 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2653 StringRef LibDir = GCCInstallation.getParentLibPath();
2654 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002655 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002656 StringRef TripleStr = GCCInstallation.getTriple().str();
2657
Dmitri Gribenkof2e7c352013-03-06 17:14:05 +00002658 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include",
2659 "/c++/" + Version.str(),
2660 TripleStr,
2661 GCCInstallation.getMultiarchSuffix(),
2662 DriverArgs, CC1Args))
2663 return;
2664
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002665 const std::string IncludePathCandidates[] = {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002666 // Gentoo is weird and places its headers inside the GCC install, so if the
2667 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002668 InstallDir.str() + "/include/g++-v4",
2669 // Android standalone toolchain has C++ headers in yet another place.
2670 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002671 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2672 // without a subdirectory corresponding to the gcc version.
2673 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002674 };
2675
2676 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2677 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2678 GCCInstallation.getMultiarchSuffix()),
2679 DriverArgs, CC1Args))
2680 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002681 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002682}
2683
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002684/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2685
Rafael Espindola0e659592012-02-19 01:38:32 +00002686DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2687 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002688
2689 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002690 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002691 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002692 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002693
Daniel Dunbaree788e72009-12-21 18:54:17 +00002694 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002695 getFilePaths().push_back("/usr/lib");
2696 getFilePaths().push_back("/usr/lib/gcc41");
2697}
2698
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002699Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2700 const ActionList &Inputs) const {
Nick Lewycky5bab9ae2012-11-15 05:36:36 +00002701 Action::ActionClass Key;
2702 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2703 Key = Action::AnalyzeJobClass;
2704 else
2705 Key = JA.getKind();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002706
2707 Tool *&T = Tools[Key];
2708 if (!T) {
2709 switch (Key) {
2710 case Action::AssembleJobClass:
2711 T = new tools::dragonfly::Assemble(*this); break;
2712 case Action::LinkJobClass:
2713 T = new tools::dragonfly::Link(*this); break;
2714 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002715 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002716 }
2717 }
2718
2719 return *T;
2720}