blob: 149b8a1f34c5496ec4ab7e286472a89f89121054 [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"
11
Daniel Dunbarf3cad362009-03-25 04:13:45 +000012#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000014#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000017#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000018#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000019#include "clang/Driver/Options.h"
John McCall260611a2012-06-20 06:18:46 +000020#include "clang/Basic/ObjCRuntime.h"
Douglas Gregor34916db2010-09-03 17:16:03 +000021#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000022
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"
John McCallf85e1932011-06-15 23:02:42 +000026#include "llvm/ADT/STLExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000027#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000028#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000029#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000031#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000032#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000033
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000034#include "SanitizerArgs.h"
35
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000036#include <cstdlib> // ::getenv
37
Dylan Noblesmithcc8a9452012-02-14 15:54:49 +000038#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
Dylan Noblesmith89bb6142011-06-23 13:50:47 +000039
Daniel Dunbar39176082009-03-20 00:20:03 +000040using namespace clang::driver;
41using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000042using namespace clang;
Daniel Dunbar39176082009-03-20 00:20:03 +000043
Daniel Dunbarf3955282009-09-04 18:34:51 +000044/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000045
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000046Darwin::Darwin(const Driver &D, const llvm::Triple& Triple)
John McCall260611a2012-06-20 06:18:46 +000047 : ToolChain(D, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000048{
Bob Wilson10853772012-01-31 21:30:03 +000049 // Compute the initial Darwin version from the triple
50 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-01-31 22:43:59 +000051 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
52 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
53 Triple.getOSName();
54 llvm::raw_string_ostream(MacosxVersionMin)
55 << Major << '.' << Minor << '.' << Micro;
56
Bob Wilson10853772012-01-31 21:30:03 +000057 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
58 // It should be removed when we stop supporting that.
59 DarwinVersion[0] = Minor + 4;
60 DarwinVersion[1] = Micro;
61 DarwinVersion[2] = 0;
Chad Rosierc793ea42012-05-09 18:46:30 +000062
63 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000064 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000065 llvm::raw_string_ostream(iOSVersionMin)
66 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000067}
68
Daniel Dunbar41800112010-08-02 05:43:56 +000069types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
70 types::ID Ty = types::lookupTypeForExtension(Ext);
71
72 // Darwin always preprocesses assembly files (unless -x is used explicitly).
73 if (Ty == types::TY_PP_Asm)
74 return types::TY_Asm;
75
76 return Ty;
77}
78
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000079bool Darwin::HasNativeLLVMSupport() const {
80 return true;
81}
82
John McCall9f084a32011-07-06 00:26:06 +000083/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall260611a2012-06-20 06:18:46 +000084ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
85 if (isTargetIPhoneOS()) {
86 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
87 } else if (TargetSimulatorVersionFromDefines != VersionTuple()) {
88 return ObjCRuntime(ObjCRuntime::iOS, TargetSimulatorVersionFromDefines);
89 } else {
90 if (isNonFragile) {
91 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
92 } else {
93 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
94 }
95 }
John McCall9f084a32011-07-06 00:26:06 +000096}
97
John McCall13db5cf2011-09-09 20:41:01 +000098/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
99bool Darwin::hasBlocksRuntime() const {
100 if (isTargetIPhoneOS())
101 return !isIPhoneOSVersionLT(3, 2);
102 else
103 return !isMacosxVersionLT(10, 6);
104}
105
Chris Lattner5f9e2722011-07-23 10:55:15 +0000106static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000107 return llvm::StringSwitch<const char*>(Value)
108 .Case("armv6k", "armv6")
109 .Case("armv5tej", "armv5")
110 .Case("xscale", "xscale")
111 .Case("armv4t", "armv4t")
112 .Case("armv7", "armv7")
113 .Cases("armv7a", "armv7-a", "armv7")
114 .Cases("armv7r", "armv7-r", "armv7")
115 .Cases("armv7m", "armv7-m", "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000116 .Cases("armv7f", "armv7-f", "armv7f")
117 .Cases("armv7k", "armv7-k", "armv7k")
118 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000119 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000120}
121
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000123 return llvm::StringSwitch<const char *>(Value)
124 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
125 .Cases("arm10e", "arm10tdmi", "armv5")
126 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
127 .Case("xscale", "xscale")
128 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s",
129 "arm1176jzf-s", "cortex-m0", "armv6")
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000130 .Cases("cortex-a8", "cortex-r4", "cortex-m3", "cortex-a9", "cortex-a15",
131 "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000132 .Case("cortex-a9-mp", "armv7f")
133 .Case("swift", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000134 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000135}
136
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000138 switch (getTriple().getArch()) {
139 default:
140 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000141
Douglas Gregorf0594d82011-03-06 19:11:49 +0000142 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000143 case llvm::Triple::arm: {
144 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000145 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000146 return Arch;
147
148 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000149 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000150 return Arch;
151
152 return "arm";
153 }
154 }
155}
156
Daniel Dunbarf3955282009-09-04 18:34:51 +0000157Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000158 // Free tool implementations.
159 for (llvm::DenseMap<unsigned, Tool*>::iterator
160 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
161 delete it->second;
162}
163
Chad Rosier61ab80a2011-09-20 20:44:06 +0000164std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
165 types::ID InputType) const {
166 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000167
168 // If the target isn't initialized (e.g., an unknown Darwin platform, return
169 // the default triple).
170 if (!isTargetInitialized())
171 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000172
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000173 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000174 Str += isTargetIPhoneOS() ? "ios" : "macosx";
175 Str += getTargetVersion().getAsString();
176 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000177
178 return Triple.getTriple();
179}
180
David Blaikie99ba9e32011-12-20 02:48:34 +0000181void Generic_ELF::anchor() {}
182
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000183Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
184 const ActionList &Inputs) const {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000185 Action::ActionClass Key = JA.getKind();
186 bool useClang = false;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000187
188 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000189 useClang = true;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000190 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000191 if (!getDriver().shouldForceClangUse() &&
192 Inputs.size() == 1 &&
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000193 types::isCXX(Inputs[0]->getType()) &&
Bob Wilson905c45f2011-10-14 05:03:44 +0000194 getTriple().isOSDarwin() &&
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000195 getTriple().getArch() == llvm::Triple::x86 &&
Bob Wilsona544aee2011-08-13 23:48:55 +0000196 (C.getArgs().getLastArg(options::OPT_fapple_kext) ||
197 C.getArgs().getLastArg(options::OPT_mkernel)))
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000198 useClang = false;
199 }
200
201 // FIXME: This seems like a hacky way to choose clang frontend.
202 if (useClang)
203 Key = Action::AnalyzeJobClass;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000204
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000205 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
206 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000207 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000208
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000209 Tool *&T = Tools[Key];
210 if (!T) {
211 switch (Key) {
212 case Action::InputClass:
213 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000214 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000215 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000216 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000217 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000218 case Action::MigrateJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000219 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000220 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000221 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000222 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000223 case Action::AssembleJobClass: {
224 if (UseIntegratedAs)
225 T = new tools::ClangAs(*this);
226 else
227 T = new tools::darwin::Assemble(*this);
228 break;
229 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000230 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000231 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000232 case Action::LipoJobClass:
233 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000234 case Action::DsymutilJobClass:
235 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000236 case Action::VerifyJobClass:
237 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000238 }
239 }
240
241 return *T;
242}
243
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000244
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000245DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
246 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000247{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000248 getProgramPaths().push_back(getDriver().getInstalledDir());
249 if (getDriver().getInstalledDir() != getDriver().Dir)
250 getProgramPaths().push_back(getDriver().Dir);
251
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000252 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000253 getProgramPaths().push_back(getDriver().getInstalledDir());
254 if (getDriver().getInstalledDir() != getDriver().Dir)
255 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000256
257 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000258 // also add the GCC libexec paths. This is legacy code that can be removed
259 // once fallback is no longer useful.
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000260 AddGCCLibexecPath(DarwinVersion[0]);
261 AddGCCLibexecPath(DarwinVersion[0] - 2);
262 AddGCCLibexecPath(DarwinVersion[0] - 1);
263 AddGCCLibexecPath(DarwinVersion[0] + 1);
264 AddGCCLibexecPath(DarwinVersion[0] + 2);
265}
266
267void DarwinClang::AddGCCLibexecPath(unsigned darwinVersion) {
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000268 std::string ToolChainDir = "i686-apple-darwin";
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000269 ToolChainDir += llvm::utostr(darwinVersion);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000270 ToolChainDir += "/4.2.1";
271
272 std::string Path = getDriver().Dir;
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000273 Path += "/../llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000274 Path += ToolChainDir;
275 getProgramPaths().push_back(Path);
276
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000277 Path = "/usr/llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000278 Path += ToolChainDir;
279 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000280}
281
John McCallf85e1932011-06-15 23:02:42 +0000282void DarwinClang::AddLinkARCArgs(const ArgList &Args,
283 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000284
285 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000286 llvm::sys::Path P(getDriver().ClangExecutable);
287 P.eraseComponent(); // 'clang'
288 P.eraseComponent(); // 'bin'
289 P.appendComponent("lib");
290 P.appendComponent("arc");
291 P.appendComponent("libarclite_");
292 std::string s = P.str();
293 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000294 if (isTargetIOSSimulator())
295 s += "iphonesimulator";
296 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000297 s += "iphoneos";
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000298 // FIXME: Remove this once we depend fully on -mios-simulator-version-min.
John McCall260611a2012-06-20 06:18:46 +0000299 else if (TargetSimulatorVersionFromDefines != VersionTuple())
John McCallf85e1932011-06-15 23:02:42 +0000300 s += "iphonesimulator";
301 else
302 s += "macosx";
303 s += ".a";
304
305 CmdArgs.push_back(Args.MakeArgString(s));
306}
307
Eric Christopher3404fe72011-06-22 17:41:40 +0000308void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000309 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000310 const char *DarwinStaticLib) const {
311 llvm::sys::Path P(getDriver().ResourceDir);
312 P.appendComponent("lib");
313 P.appendComponent("darwin");
314 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000315
Eric Christopher3404fe72011-06-22 17:41:40 +0000316 // For now, allow missing resource libraries to support developers who may
317 // not have compiler-rt checked out or integrated into their build.
318 bool Exists;
319 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
320 CmdArgs.push_back(Args.MakeArgString(P.str()));
321}
322
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000323void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
324 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000325 // Darwin only supports the compiler-rt based runtime libraries.
326 switch (GetRuntimeLibType(Args)) {
327 case ToolChain::RLT_CompilerRT:
328 break;
329 default:
330 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000331 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000332 return;
333 }
334
Daniel Dunbareec99102010-01-22 03:38:14 +0000335 // Darwin doesn't support real static executables, don't link any runtime
336 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000337 if (Args.hasArg(options::OPT_static) ||
338 Args.hasArg(options::OPT_fapple_kext) ||
339 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000340 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000341
342 // Reject -static-libgcc for now, we can deal with this when and if someone
343 // cares. This is useful in situations where someone wants to statically link
344 // something like libstdc++, and needs its runtime support routines.
345 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000346 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000347 << A->getAsString(Args);
348 return;
349 }
350
Daniel Dunbarf4714872011-11-17 00:36:57 +0000351 // If we are building profile support, link that library in.
352 if (Args.hasArg(options::OPT_fprofile_arcs) ||
353 Args.hasArg(options::OPT_fprofile_generate) ||
354 Args.hasArg(options::OPT_fcreate_profile) ||
355 Args.hasArg(options::OPT_coverage)) {
356 // Select the appropriate runtime library for the target.
357 if (isTargetIPhoneOS()) {
358 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
359 } else {
360 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
361 }
362 }
363
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000364 SanitizerArgs Sanitize(getDriver(), Args);
365
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000366 // Add ASAN runtime library, if required. Dynamic libraries and bundles
367 // should not be linked with the runtime library.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000368 if (Sanitize.needsAsanRt()) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000369 if (Args.hasArg(options::OPT_dynamiclib) ||
370 Args.hasArg(options::OPT_bundle)) return;
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000371 if (isTargetIPhoneOS()) {
372 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000373 << "-fsanitize=address";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000374 } else {
375 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
376
377 // The ASAN runtime library requires C++ and CoreFoundation.
378 AddCXXStdlibLibArgs(Args, CmdArgs);
379 CmdArgs.push_back("-framework");
380 CmdArgs.push_back("CoreFoundation");
381 }
382 }
383
Daniel Dunbareec99102010-01-22 03:38:14 +0000384 // Otherwise link libSystem, then the dynamic runtime library, and finally any
385 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000386 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000387
388 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000389 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000390 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
391 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000392 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
393 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
394 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000395
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000396 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000397 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000398 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000399 // The dynamic runtime library was merged with libSystem for 10.6 and
400 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000401 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000402 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000403 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000404 CmdArgs.push_back("-lgcc_s.10.5");
405
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000406 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000407 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000408 // omitted from 10.4.dylib.
409 //
410 // Unfortunately, that turned out to not be true, because Darwin system
411 // headers can still use eprintf on i386, and it is not exported from
412 // libSystem. Therefore, we still must provide a runtime library just for
413 // the tiny tiny handful of projects that *might* use that symbol.
414 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000415 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000416 } else {
417 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000418 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
419 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000420 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000421 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000422}
423
Argyrios Kyrtzidisdceb11f2011-10-18 00:22:49 +0000424static inline StringRef SimulatorVersionDefineName() {
425 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
426}
427
428/// \brief Parse the simulator version define:
429/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
430// and return the grouped values as integers, e.g:
431// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
432// will return Major=4, Minor=2, Micro=1.
433static bool GetVersionFromSimulatorDefine(StringRef define,
434 unsigned &Major, unsigned &Minor,
435 unsigned &Micro) {
436 assert(define.startswith(SimulatorVersionDefineName()));
437 StringRef name, version;
438 llvm::tie(name, version) = define.split('=');
439 if (version.empty())
440 return false;
441 std::string verstr = version.str();
442 char *end;
443 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
444 if (*end != '\0')
445 return false;
446 Major = num / 10000;
447 num = num % 10000;
448 Minor = num / 100;
449 Micro = num % 100;
450 return true;
451}
452
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000453void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000454 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000455
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000456 // Support allowing the SDKROOT environment variable used by xcrun and other
457 // Xcode tools to define the default sysroot, by making it the default for
458 // isysroot.
459 if (!Args.hasArg(options::OPT_isysroot)) {
460 if (char *env = ::getenv("SDKROOT")) {
461 // We only use this value as the default if it is an absolute path and
462 // exists.
463 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env)) {
464 Args.append(Args.MakeSeparateArg(
465 0, Opts.getOption(options::OPT_isysroot), env));
466 }
467 }
468 }
469
Daniel Dunbar26031372010-01-27 00:56:25 +0000470 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000471 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
472 Arg *iOSSimVersion = Args.getLastArg(
473 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000474
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000475 // FIXME: HACK! When compiling for the simulator we don't get a
476 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
477 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
478 // define passed in command-line.
479 if (!iOSVersion && !iOSSimVersion) {
480 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
481 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000482 StringRef define = (*it)->getValue();
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000483 if (define.startswith(SimulatorVersionDefineName())) {
484 unsigned Major = 0, Minor = 0, Micro = 0;
485 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
486 Major < 10 && Minor < 100 && Micro < 100) {
John McCall260611a2012-06-20 06:18:46 +0000487 TargetSimulatorVersionFromDefines = VersionTuple(Major, Minor, Micro);
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000488 }
Bob Wilsona1ec3db2012-07-19 01:35:55 +0000489 // When using the define to indicate the simulator, we force
490 // 10.6 macosx target.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000491 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Bob Wilsona1ec3db2012-07-19 01:35:55 +0000492 OSXVersion = Args.MakeJoinedArg(0, O, "10.6");
493 Args.append(OSXVersion);
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000494 break;
495 }
496 }
497 }
498
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000499 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000500 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000501 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000502 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
503 iOSVersion = iOSSimVersion = 0;
504 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000505 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000506 << iOSVersion->getAsString(Args)
507 << iOSSimVersion->getAsString(Args);
508 iOSSimVersion = 0;
509 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000510 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000511 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000512 StringRef OSXTarget;
513 StringRef iOSTarget;
514 StringRef iOSSimTarget;
515 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
516 OSXTarget = env;
517 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
518 iOSTarget = env;
519 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
520 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000521
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000522 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000523 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000524 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000525 if (iOSTarget.empty()) {
526 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
527 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000528 StringRef isysroot = A->getValue();
Chad Rosiera4884972011-08-31 20:56:25 +0000529 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
530 if (second != "")
531 iOSTarget = second.substr(0,3);
532 }
533 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000534
Chad Rosier4f8de272011-09-28 00:46:32 +0000535 // If no OSX or iOS target has been specified and we're compiling for armv7,
536 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000537 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000538 (getDarwinArchName(Args) == "armv7" ||
539 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000540 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000541
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000542 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000543 //
544 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000545
546 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000547 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000548 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000549 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000550 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000551 "IPHONEOS_DEPLOYMENT_TARGET");
552 }
553
554 // Allow conflicts among OSX and iOS for historical reasons, but choose the
555 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000556 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000557 if (getTriple().getArch() == llvm::Triple::arm ||
558 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000559 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000560 else
Chad Rosiera4884972011-08-31 20:56:25 +0000561 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000562 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000563
Chad Rosiera4884972011-08-31 20:56:25 +0000564 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000565 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000566 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
567 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000568 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000569 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000570 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
571 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000572 } else if (!iOSSimTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000573 const Option O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000574 options::OPT_mios_simulator_version_min_EQ);
575 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
576 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000577 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000578 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000579 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000580 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
581 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000582 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000585 // Reject invalid architecture combinations.
586 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
587 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000588 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000589 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
590 }
591
Daniel Dunbar26031372010-01-27 00:56:25 +0000592 // Set the tool chain target information.
593 unsigned Major, Minor, Micro;
594 bool HadExtra;
595 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000596 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000597 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000598 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000599 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000600 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000601 << OSXVersion->getAsString(Args);
602 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000603 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
604 assert(Version && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000605 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000606 Micro, HadExtra) || HadExtra ||
607 Major >= 10 || Minor >= 100 || Micro >= 100)
608 getDriver().Diag(diag::err_drv_invalid_version_number)
609 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000610 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000611
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000612 bool IsIOSSim = bool(iOSSimVersion);
613
614 // In GCC, the simulator historically was treated as being OS X in some
615 // contexts, like determining the link logic, despite generally being called
616 // with an iOS deployment target. For compatibility, we detect the
617 // simulator as iOS + x86, and treat it differently in a few contexts.
618 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
619 getTriple().getArch() == llvm::Triple::x86_64))
620 IsIOSSim = true;
621
622 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000623}
624
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000625void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000626 ArgStringList &CmdArgs) const {
627 CXXStdlibType Type = GetCXXStdlibType(Args);
628
629 switch (Type) {
630 case ToolChain::CST_Libcxx:
631 CmdArgs.push_back("-lc++");
632 break;
633
634 case ToolChain::CST_Libstdcxx: {
635 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
636 // it was previously found in the gcc lib dir. However, for all the Darwin
637 // platforms we care about it was -lstdc++.6, so we search for that
638 // explicitly if we can't see an obvious -lstdc++ candidate.
639
640 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000641 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000642 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000643 llvm::sys::Path P(A->getValue());
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000644 P.appendComponent("usr");
645 P.appendComponent("lib");
646 P.appendComponent("libstdc++.dylib");
647
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000648 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000649 P.eraseComponent();
650 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000651 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000652 CmdArgs.push_back(Args.MakeArgString(P.str()));
653 return;
654 }
655 }
656 }
657
658 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000659 // FIXME: This should be removed someday when we don't have to care about
660 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000661 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
662 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000663 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
664 return;
665 }
666
667 // Otherwise, let the linker search.
668 CmdArgs.push_back("-lstdc++");
669 break;
670 }
671 }
672}
673
Shantonu Sen7433fed2010-09-17 18:39:08 +0000674void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
675 ArgStringList &CmdArgs) const {
676
677 // For Darwin platforms, use the compiler-rt-based support library
678 // instead of the gcc-provided one (which is also incidentally
679 // only present in the gcc lib dir, which makes it hard to find).
680
681 llvm::sys::Path P(getDriver().ResourceDir);
682 P.appendComponent("lib");
683 P.appendComponent("darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000684
685 // Use the newer cc_kext for iOS ARM after 6.0.
686 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
687 !isIPhoneOSVersionLT(6, 0)) {
688 P.appendComponent("libclang_rt.cc_kext.a");
689 } else {
690 P.appendComponent("libclang_rt.cc_kext_ios5.a");
691 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000692
Shantonu Sen7433fed2010-09-17 18:39:08 +0000693 // For now, allow missing resource libraries to support developers who may
694 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000695 bool Exists;
696 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000697 CmdArgs.push_back(Args.MakeArgString(P.str()));
698}
699
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000700DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
701 const char *BoundArch) const {
702 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
703 const OptTable &Opts = getDriver().getOpts();
704
705 // FIXME: We really want to get out of the tool chain level argument
706 // translation business, as it makes the driver functionality much
707 // more opaque. For now, we follow gcc closely solely for the
708 // purpose of easily achieving feature parity & testability. Once we
709 // have something that works, we should reevaluate each translation
710 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000711
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000712 for (ArgList::const_iterator it = Args.begin(),
713 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000714 Arg *A = *it;
715
716 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000717 // Skip this argument unless the architecture matches either the toolchain
718 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000719 llvm::Triple::ArchType XarchArch =
Richard Smith1d489cf2012-11-01 04:30:05 +0000720 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000721 if (!(XarchArch == getArch() ||
722 (BoundArch && XarchArch ==
Rafael Espindolacfed8282012-10-31 18:51:07 +0000723 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000724 continue;
725
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000726 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000727 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000728 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000729 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000731 // If the argument parsing failed or more than one argument was
732 // consumed, the -Xarch_ argument's parameter tried to consume
733 // extra arguments. Emit an error and ignore.
734 //
735 // We also want to disallow any options which would alter the
736 // driver behavior; that isn't going to work in our model. We
737 // use isDriverOption() as an approximation, although things
738 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000739 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000740 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000741 << A->getAsString(Args);
742 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000743 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000744 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000745 << A->getAsString(Args);
746 continue;
747 }
748
Daniel Dunbar478edc22009-03-29 22:29:05 +0000749 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000750 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000751
752 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000753
754 // Linker input arguments require custom handling. The problem is that we
755 // have already constructed the phase actions, so we can not treat them as
756 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000757 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000758 // Convert the argument into individual Zlinker_input_args.
759 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
760 DAL->AddSeparateArg(OriginalArg,
761 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000762 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000763
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000764 }
765 continue;
766 }
Mike Stump1eb44332009-09-09 15:08:12 +0000767 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000768
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000769 // Sob. These is strictly gcc compatible for the time being. Apple
770 // gcc translates options twice, which means that self-expanding
771 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000772 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000773 default:
774 DAL->append(A);
775 break;
776
777 case options::OPT_mkernel:
778 case options::OPT_fapple_kext:
779 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000780 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000781 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000783 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000784 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000785 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000786 break;
787
788 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000789 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
790 DAL->AddFlagArg(A,
791 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000792 break;
793
794 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000795 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
796 DAL->AddFlagArg(A,
797 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000798 break;
799
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000800 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000801 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000802 break;
803
804 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000805 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000806 break;
807
808 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000809 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000810 break;
811
812 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000813 DAL->AddFlagArg(A,
814 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000815 break;
816
817 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000818 DAL->AddFlagArg(A,
819 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000820 break;
821
822 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000823 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000824 break;
825
826 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000827 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000828 break;
829 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000830 }
831
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000832 if (getTriple().getArch() == llvm::Triple::x86 ||
833 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000834 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000835 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000836
837 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000838 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000839 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000840 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000841 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
842 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000843
844 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
845 // which defines the list of which architectures we accept.
846 if (Name == "ppc")
847 ;
848 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000849 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000850 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000851 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000852 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000853 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000854 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000855 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000856 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000857 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000858 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000859 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000860 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000861 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000862 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000863 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000864
865 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000866 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000867
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000868 else if (Name == "i386")
869 ;
870 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000871 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000872 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000873 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000874 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000875 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000876 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000877 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000878 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000879 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000880 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000881 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000882 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000883 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000884
885 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000886 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000887
888 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000889 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000890 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000891 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000892 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000893 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000894 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000895 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000896 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000897 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000898 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000899 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson336bfa32012-09-29 23:52:50 +0000900 else if (Name == "armv7f")
901 DAL->AddJoinedArg(0, MArch, "armv7f");
902 else if (Name == "armv7k")
903 DAL->AddJoinedArg(0, MArch, "armv7k");
904 else if (Name == "armv7s")
905 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000906
907 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000908 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000909 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000910
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000911 // Add an explicit version min argument for the deployment target. We do this
912 // after argument translation because -Xarch_ arguments may add a version min
913 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000914 if (BoundArch)
915 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000916
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000917 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
918 // FIXME: It would be far better to avoid inserting those -static arguments,
919 // but we can't check the deployment target in the translation code until
920 // it is set here.
921 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
922 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
923 Arg *A = *it;
924 ++it;
925 if (A->getOption().getID() != options::OPT_mkernel &&
926 A->getOption().getID() != options::OPT_fapple_kext)
927 continue;
928 assert(it != ie && "unexpected argument translation");
929 A = *it;
930 assert(A->getOption().getID() == options::OPT_static &&
931 "missing expected -static argument");
932 it = DAL->getArgs().erase(it);
933 }
934 }
935
Bob Wilson163b1512011-10-07 17:54:41 +0000936 // Validate the C++ standard library choice.
937 CXXStdlibType Type = GetCXXStdlibType(*DAL);
938 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000939 // Check whether the target provides libc++.
940 StringRef where;
941
942 // Complain about targetting iOS < 5.0 in any way.
John McCalle4860152012-06-21 17:46:38 +0000943 if (TargetSimulatorVersionFromDefines != VersionTuple()) {
944 if (TargetSimulatorVersionFromDefines < VersionTuple(5, 0))
945 where = "iOS 5.0";
946 } else if (isTargetIPhoneOS()) {
947 if (isIPhoneOSVersionLT(5, 0))
948 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000949 }
950
951 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000952 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000953 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000954 }
955 }
956
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000957 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000958}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000959
Daniel Dunbarf3955282009-09-04 18:34:51 +0000960bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000961 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000962}
963
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000964bool Darwin::UseDwarfDebugFlags() const {
965 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
966 return S[0] != '\0';
967 return false;
968}
969
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000970bool Darwin::UseSjLjExceptions() const {
971 // Darwin uses SjLj exceptions on ARM.
972 return (getTriple().getArch() == llvm::Triple::arm ||
973 getTriple().getArch() == llvm::Triple::thumb);
974}
975
Daniel Dunbarf3955282009-09-04 18:34:51 +0000976const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000977 return "pic";
978}
979
Daniel Dunbarf3955282009-09-04 18:34:51 +0000980const char *Darwin::GetForcedPicModel() const {
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000981 if (getArch() == llvm::Triple::x86_64)
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000982 return "pic";
983 return 0;
984}
985
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000986bool Darwin::SupportsProfiling() const {
987 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000988 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000989}
990
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000991bool Darwin::SupportsObjCGC() const {
992 // Garbage collection is supported everywhere except on iPhone OS.
993 return !isTargetIPhoneOS();
994}
995
John McCall0a7dd782012-08-21 02:47:43 +0000996void Darwin::CheckObjCARC() const {
997 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
998 return;
John McCall80fd37a2012-08-27 01:56:21 +0000999 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +00001000}
1001
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001002std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +00001003Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
1004 types::ID InputType) const {
1005 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001006}
1007
Daniel Dunbar39176082009-03-20 00:20:03 +00001008/// Generic_GCC - A tool chain using the 'gcc' command to perform
1009/// all subcommands; this relies on gcc translating the majority of
1010/// command line options.
1011
Chandler Carruth19347ed2011-11-06 23:39:34 +00001012/// \brief Parse a GCCVersion object out of a string of text.
1013///
1014/// This is the primary means of forming GCCVersion objects.
1015/*static*/
1016Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
1017 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
1018 std::pair<StringRef, StringRef> First = VersionText.split('.');
1019 std::pair<StringRef, StringRef> Second = First.second.split('.');
1020
1021 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
1022 if (First.first.getAsInteger(10, GoodVersion.Major) ||
1023 GoodVersion.Major < 0)
1024 return BadVersion;
1025 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
1026 GoodVersion.Minor < 0)
1027 return BadVersion;
1028
1029 // First look for a number prefix and parse that if present. Otherwise just
1030 // stash the entire patch string in the suffix, and leave the number
1031 // unspecified. This covers versions strings such as:
1032 // 4.4
1033 // 4.4.0
1034 // 4.4.x
1035 // 4.4.2-rc4
1036 // 4.4.x-patched
1037 // And retains any patch number it finds.
1038 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1039 if (!PatchText.empty()) {
1040 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
1041 // Try to parse the number and any suffix.
1042 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1043 GoodVersion.Patch < 0)
1044 return BadVersion;
1045 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
1046 }
1047 }
1048
1049 return GoodVersion;
1050}
1051
1052/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
1053bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
1054 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
1055 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
1056
1057 // Note that we rank versions with *no* patch specified is better than ones
1058 // hard-coding a patch version. Thus if the RHS has no patch, it always
1059 // wins, and the LHS only wins if it has no patch and the RHS does have
1060 // a patch.
1061 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
1062 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
Gabor Greif241cbe42012-04-18 10:59:08 +00001063 if (PatchSuffix == RHS.PatchSuffix) return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001064
1065 // Finally, between completely tied version numbers, the version with the
1066 // suffix loses as we prefer full releases.
1067 if (RHS.PatchSuffix.empty()) return true;
1068 return false;
1069}
1070
Rafael Espindola0e659592012-02-19 01:38:32 +00001071static StringRef getGCCToolchainDir(const ArgList &Args) {
1072 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1073 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +00001074 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +00001075 return GCC_INSTALL_PREFIX;
1076}
1077
Chandler Carruth19347ed2011-11-06 23:39:34 +00001078/// \brief Construct a GCCInstallationDetector from the driver.
1079///
1080/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +00001081/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001082///
1083/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1084/// should instead pull the target out of the driver. This is currently
1085/// necessary because the driver doesn't store the final version of the target
1086/// triple.
1087Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1088 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001089 const llvm::Triple &TargetTriple,
1090 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001091 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001092 llvm::Triple MultiarchTriple
1093 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1094 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001095 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001096 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001097 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001098 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001099 SmallVector<StringRef, 10> CandidateTripleAliases;
1100 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1101 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1102 CandidateTripleAliases,
1103 CandidateMultiarchLibDirs,
1104 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001105
1106 // Compute the set of prefixes for our search.
1107 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1108 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001109
Rafael Espindola0e659592012-02-19 01:38:32 +00001110 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1111 if (GCCToolchainDir != "") {
1112 if (GCCToolchainDir.back() == '/')
1113 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001114
Rafael Espindola0e659592012-02-19 01:38:32 +00001115 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001116 } else {
1117 Prefixes.push_back(D.SysRoot);
1118 Prefixes.push_back(D.SysRoot + "/usr");
1119 Prefixes.push_back(D.InstalledDir + "/..");
1120 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001121
1122 // Loop over the various components which exist and select the best GCC
1123 // installation available. GCC installs are ranked by version number.
1124 Version = GCCVersion::Parse("0.0.0");
1125 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1126 if (!llvm::sys::fs::exists(Prefixes[i]))
1127 continue;
1128 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1129 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1130 if (!llvm::sys::fs::exists(LibDir))
1131 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001132 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001133 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1134 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001135 }
1136 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1137 const std::string LibDir
1138 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1139 if (!llvm::sys::fs::exists(LibDir))
1140 continue;
1141 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1142 ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001143 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001144 CandidateMultiarchTripleAliases[k],
1145 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001146 }
1147 }
1148}
1149
1150/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001151 const llvm::Triple &TargetTriple,
1152 const llvm::Triple &MultiarchTriple,
1153 SmallVectorImpl<StringRef> &LibDirs,
1154 SmallVectorImpl<StringRef> &TripleAliases,
1155 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1156 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1157 // Declare a bunch of static data sets that we'll select between below. These
1158 // are specifically designed to always refer to string literals to avoid any
1159 // lifetime or initialization issues.
1160 static const char *const ARMLibDirs[] = { "/lib" };
1161 static const char *const ARMTriples[] = {
1162 "arm-linux-gnueabi",
1163 "arm-linux-androideabi"
1164 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001165 static const char *const ARMHFTriples[] = {
1166 "arm-linux-gnueabihf",
1167 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001168
1169 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1170 static const char *const X86_64Triples[] = {
1171 "x86_64-linux-gnu",
1172 "x86_64-unknown-linux-gnu",
1173 "x86_64-pc-linux-gnu",
1174 "x86_64-redhat-linux6E",
1175 "x86_64-redhat-linux",
1176 "x86_64-suse-linux",
1177 "x86_64-manbo-linux-gnu",
1178 "x86_64-linux-gnu",
1179 "x86_64-slackware-linux"
1180 };
1181 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1182 static const char *const X86Triples[] = {
1183 "i686-linux-gnu",
1184 "i686-pc-linux-gnu",
1185 "i486-linux-gnu",
1186 "i386-linux-gnu",
1187 "i686-redhat-linux",
1188 "i586-redhat-linux",
1189 "i386-redhat-linux",
1190 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001191 "i486-slackware-linux",
1192 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001193 };
1194
1195 static const char *const MIPSLibDirs[] = { "/lib" };
1196 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1197 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001198 static const char *const MIPSELTriples[] = {
1199 "mipsel-linux-gnu",
1200 "mipsel-linux-android"
1201 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001202
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001203 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1204 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1205 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1206 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1207
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001208 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1209 static const char *const PPCTriples[] = {
1210 "powerpc-linux-gnu",
1211 "powerpc-unknown-linux-gnu",
Gabor Greif91720912012-05-15 11:21:03 +00001212 "powerpc-suse-linux",
1213 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001214 };
1215 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1216 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001217 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001218 "powerpc64-unknown-linux-gnu",
1219 "powerpc64-suse-linux",
1220 "ppc64-redhat-linux"
1221 };
1222
1223 switch (TargetTriple.getArch()) {
1224 case llvm::Triple::arm:
1225 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001226 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001227 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1228 TripleAliases.append(
1229 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1230 } else {
1231 TripleAliases.append(
1232 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1233 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001234 break;
1235 case llvm::Triple::x86_64:
1236 LibDirs.append(
1237 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1238 TripleAliases.append(
1239 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1240 MultiarchLibDirs.append(
1241 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1242 MultiarchTripleAliases.append(
1243 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1244 break;
1245 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001246 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001247 TripleAliases.append(
1248 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1249 MultiarchLibDirs.append(
1250 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1251 MultiarchTripleAliases.append(
1252 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1253 break;
1254 case llvm::Triple::mips:
1255 LibDirs.append(
1256 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1257 TripleAliases.append(
1258 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001259 MultiarchLibDirs.append(
1260 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1261 MultiarchTripleAliases.append(
1262 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001263 break;
1264 case llvm::Triple::mipsel:
1265 LibDirs.append(
1266 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1267 TripleAliases.append(
1268 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001269 MultiarchLibDirs.append(
1270 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1271 MultiarchTripleAliases.append(
1272 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1273 break;
1274 case llvm::Triple::mips64:
1275 LibDirs.append(
1276 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1277 TripleAliases.append(
1278 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1279 MultiarchLibDirs.append(
1280 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1281 MultiarchTripleAliases.append(
1282 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1283 break;
1284 case llvm::Triple::mips64el:
1285 LibDirs.append(
1286 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1287 TripleAliases.append(
1288 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1289 MultiarchLibDirs.append(
1290 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1291 MultiarchTripleAliases.append(
1292 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001293 break;
1294 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001295 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001296 TripleAliases.append(
1297 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1298 MultiarchLibDirs.append(
1299 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1300 MultiarchTripleAliases.append(
1301 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1302 break;
1303 case llvm::Triple::ppc64:
1304 LibDirs.append(
1305 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1306 TripleAliases.append(
1307 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1308 MultiarchLibDirs.append(
1309 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1310 MultiarchTripleAliases.append(
1311 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1312 break;
1313
1314 default:
1315 // By default, just rely on the standard lib directories and the original
1316 // triple.
1317 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001318 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001319
1320 // Always append the drivers target triple to the end, in case it doesn't
1321 // match any of our aliases.
1322 TripleAliases.push_back(TargetTriple.str());
1323
1324 // Also include the multiarch variant if it's different.
1325 if (TargetTriple.str() != MultiarchTriple.str())
1326 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001327}
1328
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001329// FIXME: There is the same routine in the Tools.cpp.
1330static bool hasMipsN32ABIArg(const ArgList &Args) {
1331 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00001332 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001333}
1334
1335static StringRef getTargetMultiarchSuffix(llvm::Triple::ArchType TargetArch,
1336 const ArgList &Args) {
1337 if (TargetArch == llvm::Triple::x86_64 ||
1338 TargetArch == llvm::Triple::ppc64)
1339 return "/64";
1340
1341 if (TargetArch == llvm::Triple::mips64 ||
1342 TargetArch == llvm::Triple::mips64el) {
1343 if (hasMipsN32ABIArg(Args))
1344 return "/n32";
1345 else
1346 return "/64";
1347 }
1348
1349 return "/32";
1350}
1351
Chandler Carruth19347ed2011-11-06 23:39:34 +00001352void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001353 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1354 const std::string &LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001355 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001356 // There are various different suffixes involving the triple we
1357 // check for. We also record what is necessary to walk from each back
1358 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001359 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001360 "/gcc/" + CandidateTriple.str(),
1361 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1362
Hal Finkel02014b42012-09-18 22:25:07 +00001363 // The Freescale PPC SDK has the gcc libraries in
1364 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1365 "/" + CandidateTriple.str(),
1366
Chandler Carruth19347ed2011-11-06 23:39:34 +00001367 // Ubuntu has a strange mis-matched pair of triples that this happens to
1368 // match.
1369 // FIXME: It may be worthwhile to generalize this and look for a second
1370 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001371 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001372 };
1373 const std::string InstallSuffixes[] = {
1374 "/../../..",
1375 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001376 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001377 "/../../../.."
1378 };
1379 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001380 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1381 (TargetArch != llvm::Triple::x86));
1382 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1383 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001384 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001385 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001386 !EC && LI != LE; LI = LI.increment(EC)) {
1387 StringRef VersionText = llvm::sys::path::filename(LI->path());
1388 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1389 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1390 if (CandidateVersion < MinVersion)
1391 continue;
1392 if (CandidateVersion <= Version)
1393 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001394
1395 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001396 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001397 // libs in a subdirectory named 64. The simple logic we follow is that
1398 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1399 // we use that. If not, and if not a multiarch triple, we look for
1400 // crtbegin.o without the subdirectory.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001401 StringRef MultiarchSuffix = getTargetMultiarchSuffix(TargetArch, Args);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001402 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1403 GCCMultiarchSuffix = MultiarchSuffix.str();
1404 } else {
1405 if (NeedsMultiarchSuffix ||
1406 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1407 continue;
1408 GCCMultiarchSuffix.clear();
1409 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001410
1411 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001412 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001413 // FIXME: We hack together the directory name here instead of
1414 // using LI to ensure stable path separators across Windows and
1415 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001416 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001417 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001418 IsValid = true;
1419 }
1420 }
1421}
1422
Rafael Espindola0e659592012-02-19 01:38:32 +00001423Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1424 const ArgList &Args)
1425 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001426 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001427 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001428 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001429}
1430
Daniel Dunbar39176082009-03-20 00:20:03 +00001431Generic_GCC::~Generic_GCC() {
1432 // Free tool implementations.
1433 for (llvm::DenseMap<unsigned, Tool*>::iterator
1434 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1435 delete it->second;
1436}
1437
Mike Stump1eb44332009-09-09 15:08:12 +00001438Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001439 const JobAction &JA,
1440 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +00001441 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001442 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +00001443 Key = Action::AnalyzeJobClass;
1444 else
1445 Key = JA.getKind();
1446
1447 Tool *&T = Tools[Key];
1448 if (!T) {
1449 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001450 case Action::InputClass:
1451 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001452 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001453 case Action::PreprocessJobClass:
1454 T = new tools::gcc::Preprocess(*this); break;
1455 case Action::PrecompileJobClass:
1456 T = new tools::gcc::Precompile(*this); break;
1457 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001458 case Action::MigrateJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001459 T = new tools::Clang(*this); break;
1460 case Action::CompileJobClass:
1461 T = new tools::gcc::Compile(*this); break;
1462 case Action::AssembleJobClass:
1463 T = new tools::gcc::Assemble(*this); break;
1464 case Action::LinkJobClass:
1465 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001467 // This is a bit ungeneric, but the only platform using a driver
1468 // driver is Darwin.
1469 case Action::LipoJobClass:
1470 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001471 case Action::DsymutilJobClass:
1472 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001473 case Action::VerifyJobClass:
1474 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001475 }
1476 }
1477
1478 return *T;
1479}
1480
Daniel Dunbar39176082009-03-20 00:20:03 +00001481bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00001482 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00001483}
1484
1485const char *Generic_GCC::GetDefaultRelocationModel() const {
1486 return "static";
1487}
1488
1489const char *Generic_GCC::GetForcedPicModel() const {
1490 return 0;
1491}
Tony Linthicum96319392011-12-12 21:14:55 +00001492/// Hexagon Toolchain
1493
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001494Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple& Triple)
1495 : ToolChain(D, Triple) {
Tony Linthicum96319392011-12-12 21:14:55 +00001496 getProgramPaths().push_back(getDriver().getInstalledDir());
1497 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1498 getProgramPaths().push_back(getDriver().Dir);
1499}
1500
1501Hexagon_TC::~Hexagon_TC() {
1502 // Free tool implementations.
1503 for (llvm::DenseMap<unsigned, Tool*>::iterator
1504 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1505 delete it->second;
1506}
1507
1508Tool &Hexagon_TC::SelectTool(const Compilation &C,
1509 const JobAction &JA,
1510 const ActionList &Inputs) const {
1511 Action::ActionClass Key;
1512 // if (JA.getKind () == Action::CompileJobClass)
1513 // Key = JA.getKind ();
1514 // else
1515
1516 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1517 Key = Action::AnalyzeJobClass;
1518 else
1519 Key = JA.getKind();
1520 // if ((JA.getKind () == Action::CompileJobClass)
1521 // && (JA.getType () != types::TY_LTO_BC)) {
1522 // Key = JA.getKind ();
1523 // }
1524
1525 Tool *&T = Tools[Key];
1526 if (!T) {
1527 switch (Key) {
1528 case Action::InputClass:
1529 case Action::BindArchClass:
1530 assert(0 && "Invalid tool kind.");
1531 case Action::AnalyzeJobClass:
1532 T = new tools::Clang(*this); break;
1533 case Action::AssembleJobClass:
1534 T = new tools::hexagon::Assemble(*this); break;
1535 case Action::LinkJobClass:
1536 T = new tools::hexagon::Link(*this); break;
1537 default:
1538 assert(false && "Unsupported action for Hexagon target.");
1539 }
1540 }
1541
1542 return *T;
1543}
1544
Tony Linthicum96319392011-12-12 21:14:55 +00001545const char *Hexagon_TC::GetDefaultRelocationModel() const {
1546 return "static";
1547}
1548
1549const char *Hexagon_TC::GetForcedPicModel() const {
1550 return 0;
1551} // End Hexagon
1552
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001553
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001554/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1555/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1556/// Currently does not support anything else but compilation.
1557
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001558TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1559 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001560 // Path mangling to find libexec
1561 std::string Path(getDriver().Dir);
1562
1563 Path += "/../libexec";
1564 getProgramPaths().push_back(Path);
1565}
1566
1567TCEToolChain::~TCEToolChain() {
1568 for (llvm::DenseMap<unsigned, Tool*>::iterator
1569 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1570 delete it->second;
1571}
1572
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001573bool TCEToolChain::IsMathErrnoDefault() const {
1574 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001575}
1576
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001577const char *TCEToolChain::GetDefaultRelocationModel() const {
1578 return "static";
1579}
1580
1581const char *TCEToolChain::GetForcedPicModel() const {
1582 return 0;
1583}
1584
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001585Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001586 const JobAction &JA,
1587 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001588 Action::ActionClass Key;
1589 Key = Action::AnalyzeJobClass;
1590
1591 Tool *&T = Tools[Key];
1592 if (!T) {
1593 switch (Key) {
1594 case Action::PreprocessJobClass:
1595 T = new tools::gcc::Preprocess(*this); break;
1596 case Action::AnalyzeJobClass:
1597 T = new tools::Clang(*this); break;
1598 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001599 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001600 }
1601 }
1602 return *T;
1603}
1604
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001605/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1606
Rafael Espindola0e659592012-02-19 01:38:32 +00001607OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1608 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001609 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001610 getFilePaths().push_back("/usr/lib");
1611}
1612
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001613Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1614 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001615 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001616 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001617 Key = Action::AnalyzeJobClass;
1618 else
1619 Key = JA.getKind();
1620
Rafael Espindoladda5b922010-11-07 23:13:01 +00001621 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1622 options::OPT_no_integrated_as,
1623 IsIntegratedAssemblerDefault());
1624
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001625 Tool *&T = Tools[Key];
1626 if (!T) {
1627 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001628 case Action::AssembleJobClass: {
1629 if (UseIntegratedAs)
1630 T = new tools::ClangAs(*this);
1631 else
1632 T = new tools::openbsd::Assemble(*this);
1633 break;
1634 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001635 case Action::LinkJobClass:
1636 T = new tools::openbsd::Link(*this); break;
1637 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001638 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001639 }
1640 }
1641
1642 return *T;
1643}
1644
Eli Friedman42f74f22012-08-08 23:57:20 +00001645/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1646
1647Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1648 : Generic_ELF(D, Triple, Args) {
1649 getFilePaths().push_back(getDriver().Dir + "/../lib");
1650 getFilePaths().push_back("/usr/lib");
1651}
1652
1653Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA,
1654 const ActionList &Inputs) const {
1655 Action::ActionClass Key;
1656 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1657 Key = Action::AnalyzeJobClass;
1658 else
1659 Key = JA.getKind();
1660
1661 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1662 options::OPT_no_integrated_as,
1663 IsIntegratedAssemblerDefault());
1664
1665 Tool *&T = Tools[Key];
1666 if (!T) {
1667 switch (Key) {
1668 case Action::AssembleJobClass: {
1669 if (UseIntegratedAs)
1670 T = new tools::ClangAs(*this);
1671 else
1672 T = new tools::bitrig::Assemble(*this);
1673 break;
1674 }
1675 case Action::LinkJobClass:
1676 T = new tools::bitrig::Link(*this); break;
1677 default:
1678 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1679 }
1680 }
1681
1682 return *T;
1683}
1684
1685void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1686 ArgStringList &CC1Args) const {
1687 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1688 DriverArgs.hasArg(options::OPT_nostdincxx))
1689 return;
1690
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001691 switch (GetCXXStdlibType(DriverArgs)) {
1692 case ToolChain::CST_Libcxx:
1693 addSystemInclude(DriverArgs, CC1Args,
1694 getDriver().SysRoot + "/usr/include/c++/");
1695 break;
1696 case ToolChain::CST_Libstdcxx:
1697 addSystemInclude(DriverArgs, CC1Args,
1698 getDriver().SysRoot + "/usr/include/c++/stdc++");
1699 addSystemInclude(DriverArgs, CC1Args,
1700 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001701
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001702 StringRef Triple = getTriple().str();
1703 if (Triple.startswith("amd64"))
1704 addSystemInclude(DriverArgs, CC1Args,
1705 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1706 Triple.substr(5));
1707 else
1708 addSystemInclude(DriverArgs, CC1Args,
1709 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1710 Triple);
1711 break;
1712 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001713}
1714
1715void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1716 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001717 switch (GetCXXStdlibType(Args)) {
1718 case ToolChain::CST_Libcxx:
1719 CmdArgs.push_back("-lc++");
1720 CmdArgs.push_back("-lcxxrt");
1721 // Include supc++ to provide Unwind until provided by libcxx.
1722 CmdArgs.push_back("-lgcc");
1723 break;
1724 case ToolChain::CST_Libstdcxx:
1725 CmdArgs.push_back("-lstdc++");
1726 break;
1727 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001728}
1729
Daniel Dunbar75358d22009-03-30 21:06:03 +00001730/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1731
Rafael Espindola0e659592012-02-19 01:38:32 +00001732FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1733 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001734
Chandler Carruth24248e32012-01-26 01:35:15 +00001735 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1736 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001737 if ((Triple.getArch() == llvm::Triple::x86 ||
1738 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001739 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001740 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1741 else
1742 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001743}
1744
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001745Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1746 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001747 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001748 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001749 Key = Action::AnalyzeJobClass;
1750 else
1751 Key = JA.getKind();
1752
Roman Divacky67dece72010-11-08 17:46:39 +00001753 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1754 options::OPT_no_integrated_as,
1755 IsIntegratedAssemblerDefault());
1756
Daniel Dunbar75358d22009-03-30 21:06:03 +00001757 Tool *&T = Tools[Key];
1758 if (!T) {
1759 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001760 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001761 if (UseIntegratedAs)
1762 T = new tools::ClangAs(*this);
1763 else
1764 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001765 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001766 case Action::LinkJobClass:
1767 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001768 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001769 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001770 }
1771 }
1772
1773 return *T;
1774}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001775
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001776/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1777
Rafael Espindola0e659592012-02-19 01:38:32 +00001778NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1779 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001780
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001781 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001782 // When targeting a 32-bit platform, try the special directory used on
1783 // 64-bit hosts, and only fall back to the main library directory if that
1784 // doesn't work.
1785 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1786 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001787 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001788 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001789
1790 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001791 }
1792}
1793
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001794Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1795 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001796 Action::ActionClass Key;
1797 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1798 Key = Action::AnalyzeJobClass;
1799 else
1800 Key = JA.getKind();
1801
1802 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1803 options::OPT_no_integrated_as,
1804 IsIntegratedAssemblerDefault());
1805
1806 Tool *&T = Tools[Key];
1807 if (!T) {
1808 switch (Key) {
1809 case Action::AssembleJobClass:
1810 if (UseIntegratedAs)
1811 T = new tools::ClangAs(*this);
1812 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001813 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001814 break;
1815 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001816 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001817 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001818 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001819 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001820 }
1821 }
1822
1823 return *T;
1824}
1825
Chris Lattner38e317d2010-07-07 16:01:42 +00001826/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1827
Rafael Espindola0e659592012-02-19 01:38:32 +00001828Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1829 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001830 getFilePaths().push_back(getDriver().Dir + "/../lib");
1831 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001832}
1833
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001834Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1835 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001836 Action::ActionClass Key;
1837 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1838 Key = Action::AnalyzeJobClass;
1839 else
1840 Key = JA.getKind();
1841
1842 Tool *&T = Tools[Key];
1843 if (!T) {
1844 switch (Key) {
1845 case Action::AssembleJobClass:
1846 T = new tools::minix::Assemble(*this); break;
1847 case Action::LinkJobClass:
1848 T = new tools::minix::Link(*this); break;
1849 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001850 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001851 }
1852 }
1853
1854 return *T;
1855}
1856
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001857/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1858
Rafael Espindola0e659592012-02-19 01:38:32 +00001859AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1860 const ArgList &Args)
1861 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001862
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001863 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001864 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001865 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001866
Daniel Dunbaree788e72009-12-21 18:54:17 +00001867 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001868 getFilePaths().push_back("/usr/lib");
1869 getFilePaths().push_back("/usr/sfw/lib");
1870 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001871 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001872
1873}
1874
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001875Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1876 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001877 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001878 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001879 Key = Action::AnalyzeJobClass;
1880 else
1881 Key = JA.getKind();
1882
1883 Tool *&T = Tools[Key];
1884 if (!T) {
1885 switch (Key) {
1886 case Action::AssembleJobClass:
1887 T = new tools::auroraux::Assemble(*this); break;
1888 case Action::LinkJobClass:
1889 T = new tools::auroraux::Link(*this); break;
1890 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001891 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001892 }
1893 }
1894
1895 return *T;
1896}
1897
David Chisnall31c46902012-02-15 13:39:01 +00001898/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1899
Rafael Espindola0e659592012-02-19 01:38:32 +00001900Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1901 const ArgList &Args)
1902 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001903
1904 getProgramPaths().push_back(getDriver().getInstalledDir());
1905 if (getDriver().getInstalledDir() != getDriver().Dir)
1906 getProgramPaths().push_back(getDriver().Dir);
1907
1908 getFilePaths().push_back(getDriver().Dir + "/../lib");
1909 getFilePaths().push_back("/usr/lib");
1910}
1911
1912Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
1913 const ActionList &Inputs) const {
1914 Action::ActionClass Key;
1915 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1916 Key = Action::AnalyzeJobClass;
1917 else
1918 Key = JA.getKind();
1919
1920 Tool *&T = Tools[Key];
1921 if (!T) {
1922 switch (Key) {
1923 case Action::AssembleJobClass:
1924 T = new tools::solaris::Assemble(*this); break;
1925 case Action::LinkJobClass:
1926 T = new tools::solaris::Link(*this); break;
1927 default:
1928 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1929 }
1930 }
1931
1932 return *T;
1933}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001934
Eli Friedman6b3454a2009-05-26 07:52:18 +00001935/// Linux toolchain (very bare-bones at the moment).
1936
Rafael Espindolac1da9812010-11-07 20:14:31 +00001937enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001938 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001939 DebianLenny,
1940 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001941 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001942 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001943 RHEL4,
1944 RHEL5,
1945 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001946 Fedora13,
1947 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001948 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001949 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001950 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001951 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001952 OpenSuse11_4,
1953 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001954 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00001955 UbuntuHardy,
1956 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001957 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001958 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001959 UbuntuLucid,
1960 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001961 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001962 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001963 UbuntuPrecise,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001964 UnknownDistro
1965};
1966
Chris Lattnerd753b562011-05-22 05:36:06 +00001967static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001968 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1969 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001970}
1971
1972static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001973 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001974}
1975
1976static bool IsDebian(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001977 return Distro >= DebianLenny && Distro <= DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001978}
1979
1980static bool IsUbuntu(enum LinuxDistro Distro) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001981 return Distro >= UbuntuHardy && Distro <= UbuntuPrecise;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001982}
1983
Rafael Espindolac1da9812010-11-07 20:14:31 +00001984static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001985 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001986 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001987 StringRef Data = File.get()->getBuffer();
1988 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001989 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001990 LinuxDistro Version = UnknownDistro;
1991 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
1992 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
1993 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
1994 .Case("hardy", UbuntuHardy)
1995 .Case("intrepid", UbuntuIntrepid)
1996 .Case("jaunty", UbuntuJaunty)
1997 .Case("karmic", UbuntuKarmic)
1998 .Case("lucid", UbuntuLucid)
1999 .Case("maverick", UbuntuMaverick)
2000 .Case("natty", UbuntuNatty)
2001 .Case("oneiric", UbuntuOneiric)
2002 .Case("precise", UbuntuPrecise)
2003 .Default(UnknownDistro);
2004 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002005 }
2006
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002007 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002008 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002009 if (Data.startswith("Fedora release 16"))
2010 return Fedora16;
2011 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00002012 return Fedora15;
2013 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002014 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00002015 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002016 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00002017 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002018 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00002019 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00002020 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002021 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002022 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002023 else if ((Data.startswith("Red Hat Enterprise Linux") ||
2024 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002025 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002026 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002027 else if ((Data.startswith("Red Hat Enterprise Linux") ||
2028 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00002029 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00002030 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002031 return UnknownDistro;
2032 }
2033
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002034 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002035 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00002036 if (Data[0] == '5')
2037 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002038 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00002039 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00002040 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00002041 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002042 return UnknownDistro;
2043 }
2044
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002045 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
2046 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
2047 .StartsWith("openSUSE 11.3", OpenSuse11_3)
2048 .StartsWith("openSUSE 11.4", OpenSuse11_4)
2049 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00002050 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00002051 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002052
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00002053 bool Exists;
2054 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00002055 return Exherbo;
2056
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002057 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2058 return ArchLinux;
2059
Rafael Espindolac1da9812010-11-07 20:14:31 +00002060 return UnknownDistro;
2061}
2062
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002063/// \brief Get our best guess at the multiarch triple for a target.
2064///
2065/// Debian-based systems are starting to use a multiarch setup where they use
2066/// a target-triple directory in the library and header search paths.
2067/// Unfortunately, this triple does not align with the vanilla target triple,
2068/// so we provide a rough mapping here.
2069static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2070 StringRef SysRoot) {
2071 // For most architectures, just use whatever we have rather than trying to be
2072 // clever.
2073 switch (TargetTriple.getArch()) {
2074 default:
2075 return TargetTriple.str();
2076
2077 // We use the existence of '/lib/<triple>' as a directory to detect some
2078 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00002079 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2080 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00002081 case llvm::Triple::arm:
2082 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00002083 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2084 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2085 return "arm-linux-gnueabihf";
2086 } else {
2087 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2088 return "arm-linux-gnueabi";
2089 }
Chad Rosier0337efd2012-07-11 19:08:21 +00002090 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002091 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002092 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2093 return "i386-linux-gnu";
2094 return TargetTriple.str();
2095 case llvm::Triple::x86_64:
2096 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2097 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002098 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00002099 case llvm::Triple::mips:
2100 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2101 return "mips-linux-gnu";
2102 return TargetTriple.str();
2103 case llvm::Triple::mipsel:
2104 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2105 return "mipsel-linux-gnu";
2106 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002107 case llvm::Triple::ppc:
2108 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2109 return "powerpc-linux-gnu";
2110 return TargetTriple.str();
2111 case llvm::Triple::ppc64:
2112 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2113 return "powerpc64-linux-gnu";
2114 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002115 }
2116}
2117
Chandler Carruth00646ba2012-01-25 11:24:24 +00002118static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2119 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2120}
2121
Simon Atanasyan7a918882012-09-14 11:27:24 +00002122static bool isMipsArch(llvm::Triple::ArchType Arch) {
2123 return Arch == llvm::Triple::mips ||
2124 Arch == llvm::Triple::mipsel ||
2125 Arch == llvm::Triple::mips64 ||
2126 Arch == llvm::Triple::mips64el;
2127}
2128
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002129static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2130 const ArgList &Args) {
2131 if (Arch != llvm::Triple::mips &&
2132 Arch != llvm::Triple::mipsel)
2133 return false;
2134
2135 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2136 options::OPT_mcpu_EQ,
2137 options::OPT_mips_CPUs_Group);
2138
2139 if (!A)
2140 return false;
2141
2142 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2143 return A->getOption().matches(options::OPT_mips32r2);
2144
Richard Smith1d489cf2012-11-01 04:30:05 +00002145 return A->getValue() == StringRef("mips32r2");
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002146}
2147
Simon Atanasyan7a918882012-09-14 11:27:24 +00002148static StringRef getMultilibDir(const llvm::Triple &Triple,
2149 const ArgList &Args) {
2150 if (!isMipsArch(Triple.getArch()))
2151 return Triple.isArch32Bit() ? "lib32" : "lib64";
2152
2153 // lib32 directory has a special meaning on MIPS targets.
2154 // It contains N32 ABI binaries. Use this folder if produce
2155 // code for N32 ABI only.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00002156 if (hasMipsN32ABIArg(Args))
Simon Atanasyan7a918882012-09-14 11:27:24 +00002157 return "lib32";
2158
2159 return Triple.isArch32Bit() ? "lib" : "lib64";
2160}
2161
Rafael Espindola0e659592012-02-19 01:38:32 +00002162Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2163 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002164 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00002165 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002166
Rafael Espindolaab784082011-09-01 16:25:49 +00002167 // OpenSuse stores the linker with the compiler, add that to the search
2168 // path.
2169 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002170 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002171 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002172
2173 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002174
2175 LinuxDistro Distro = DetectLinuxDistro(Arch);
2176
Chris Lattner64a89172011-05-22 16:45:07 +00002177 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002178 ExtraOpts.push_back("-z");
2179 ExtraOpts.push_back("relro");
2180 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002181
Douglas Gregorf0594d82011-03-06 19:11:49 +00002182 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002183 ExtraOpts.push_back("-X");
2184
Logan Chien94a71422012-09-02 09:30:11 +00002185 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002186
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002187 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2188 // and the MIPS ABI require .dynsym to be sorted in different ways.
2189 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2190 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002191 // Android loader does not support .gnu.hash.
Simon Atanasyan7a918882012-09-14 11:27:24 +00002192 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002193 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2194 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002195 ExtraOpts.push_back("--hash-style=gnu");
2196
2197 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2198 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2199 ExtraOpts.push_back("--hash-style=both");
2200 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002201
Chris Lattnerd753b562011-05-22 05:36:06 +00002202 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002203 ExtraOpts.push_back("--no-add-needed");
2204
Eli Friedman0b200f62011-06-02 21:36:53 +00002205 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002206 IsOpenSuse(Distro) ||
2207 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002208 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002209 ExtraOpts.push_back("--build-id");
2210
Chris Lattner64a89172011-05-22 16:45:07 +00002211 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002212 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002213
Chandler Carruthd2deee12011-10-03 05:28:29 +00002214 // The selection of paths to try here is designed to match the patterns which
2215 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2216 // This was determined by running GCC in a fake filesystem, creating all
2217 // possible permutations of these directories, and seeing which ones it added
2218 // to the link paths.
2219 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002220
Simon Atanasyan7a918882012-09-14 11:27:24 +00002221 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002222 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002223
Chandler Carruthd1f73062011-11-06 23:09:05 +00002224 // Add the multilib suffixed paths where they are available.
2225 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002226 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002227 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002228
2229 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2230 addPathIfExists(GCCInstallation.getInstallPath() +
2231 GCCInstallation.getMultiarchSuffix() +
2232 "/mips-r2",
2233 Paths);
2234 else
2235 addPathIfExists((GCCInstallation.getInstallPath() +
2236 GCCInstallation.getMultiarchSuffix()),
2237 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002238
2239 // If the GCC installation we found is inside of the sysroot, we want to
2240 // prefer libraries installed in the parent prefix of the GCC installation.
2241 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002242 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002243 // This usually happens when there is an external cross compiler on the
2244 // host system, and a more minimal sysroot available that is the target of
2245 // the cross.
2246 if (StringRef(LibPath).startswith(SysRoot)) {
2247 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2248 Paths);
2249 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2250 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2251 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002252 // On Android, libraries in the parent prefix of the GCC installation are
2253 // preferred to the ones under sysroot.
2254 if (IsAndroid) {
2255 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2256 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002257 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002258 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2259 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2260 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2261 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2262
2263 // Try walking via the GCC triple path in case of multiarch GCC
2264 // installations with strange symlinks.
2265 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002266 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002267 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002268
Chandler Carruth7a09d012011-10-16 10:54:30 +00002269 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002270 if (GCCInstallation.isValid()) {
2271 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002272 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002273 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002274 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002275
2276 if (StringRef(LibPath).startswith(SysRoot)) {
2277 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2278 addPathIfExists(LibPath, Paths);
2279 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002280 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002281 addPathIfExists(SysRoot + "/lib", Paths);
2282 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002283}
2284
2285bool Linux::HasNativeLLVMSupport() const {
2286 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002287}
2288
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002289Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2290 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002291 Action::ActionClass Key;
2292 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2293 Key = Action::AnalyzeJobClass;
2294 else
2295 Key = JA.getKind();
2296
Rafael Espindoladda5b922010-11-07 23:13:01 +00002297 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2298 options::OPT_no_integrated_as,
2299 IsIntegratedAssemblerDefault());
2300
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002301 Tool *&T = Tools[Key];
2302 if (!T) {
2303 switch (Key) {
2304 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002305 if (UseIntegratedAs)
2306 T = new tools::ClangAs(*this);
2307 else
2308 T = new tools::linuxtools::Assemble(*this);
2309 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002310 case Action::LinkJobClass:
2311 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002312 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002313 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002314 }
2315 }
2316
2317 return *T;
2318}
2319
Rafael Espindola8af669f2012-06-19 01:26:10 +00002320void Linux::addClangTargetOptions(ArgStringList &CC1Args) const {
2321 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
2322 if (V >= Generic_GCC::GCCVersion::Parse("4.7.0"))
2323 CC1Args.push_back("-fuse-init-array");
2324}
2325
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002326void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2327 ArgStringList &CC1Args) const {
2328 const Driver &D = getDriver();
2329
2330 if (DriverArgs.hasArg(options::OPT_nostdinc))
2331 return;
2332
2333 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2334 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2335
2336 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002337 llvm::sys::Path P(D.ResourceDir);
2338 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002339 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002340 }
2341
2342 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2343 return;
2344
2345 // Check for configure-time C include directories.
2346 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2347 if (CIncludeDirs != "") {
2348 SmallVector<StringRef, 5> dirs;
2349 CIncludeDirs.split(dirs, ":");
2350 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2351 I != E; ++I) {
2352 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2353 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2354 }
2355 return;
2356 }
2357
2358 // Lacking those, try to detect the correct set of system includes for the
2359 // target triple.
2360
Chandler Carrutha4630892011-11-06 08:21:07 +00002361 // Implement generic Debian multiarch support.
2362 const StringRef X86_64MultiarchIncludeDirs[] = {
2363 "/usr/include/x86_64-linux-gnu",
2364
2365 // FIXME: These are older forms of multiarch. It's not clear that they're
2366 // in use in any released version of Debian, so we should consider
2367 // removing them.
2368 "/usr/include/i686-linux-gnu/64",
2369 "/usr/include/i486-linux-gnu/64"
2370 };
2371 const StringRef X86MultiarchIncludeDirs[] = {
2372 "/usr/include/i386-linux-gnu",
2373
2374 // FIXME: These are older forms of multiarch. It's not clear that they're
2375 // in use in any released version of Debian, so we should consider
2376 // removing them.
2377 "/usr/include/x86_64-linux-gnu/32",
2378 "/usr/include/i686-linux-gnu",
2379 "/usr/include/i486-linux-gnu"
2380 };
2381 const StringRef ARMMultiarchIncludeDirs[] = {
2382 "/usr/include/arm-linux-gnueabi"
2383 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002384 const StringRef ARMHFMultiarchIncludeDirs[] = {
2385 "/usr/include/arm-linux-gnueabihf"
2386 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002387 const StringRef MIPSMultiarchIncludeDirs[] = {
2388 "/usr/include/mips-linux-gnu"
2389 };
2390 const StringRef MIPSELMultiarchIncludeDirs[] = {
2391 "/usr/include/mipsel-linux-gnu"
2392 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002393 const StringRef PPCMultiarchIncludeDirs[] = {
2394 "/usr/include/powerpc-linux-gnu"
2395 };
2396 const StringRef PPC64MultiarchIncludeDirs[] = {
2397 "/usr/include/powerpc64-linux-gnu"
2398 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002399 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002400 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002401 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002402 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002403 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002404 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002405 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2406 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2407 else
2408 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002409 } else if (getTriple().getArch() == llvm::Triple::mips) {
2410 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2411 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2412 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002413 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2414 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2415 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2416 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002417 }
2418 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2419 E = MultiarchIncludeDirs.end();
2420 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002421 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002422 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2423 break;
2424 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002425 }
2426
2427 if (getTriple().getOS() == llvm::Triple::RTEMS)
2428 return;
2429
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002430 // Add an include of '/include' directly. This isn't provided by default by
2431 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2432 // add even when Clang is acting as-if it were a system compiler.
2433 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2434
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002435 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2436}
2437
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002438/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2439/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2440 const ArgList &DriverArgs,
2441 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002442 if (!llvm::sys::fs::exists(Base))
2443 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002444 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002445 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002446 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002447 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002448}
2449
2450void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2451 ArgStringList &CC1Args) const {
2452 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2453 DriverArgs.hasArg(options::OPT_nostdincxx))
2454 return;
2455
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002456 // Check if libc++ has been enabled and provide its include paths if so.
2457 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2458 // libc++ is always installed at a fixed path on Linux currently.
2459 addSystemInclude(DriverArgs, CC1Args,
2460 getDriver().SysRoot + "/usr/include/c++/v1");
2461 return;
2462 }
2463
Chandler Carruthfc52f752012-01-25 08:04:13 +00002464 // We need a detected GCC installation on Linux to provide libstdc++'s
2465 // headers. We handled the libc++ case above.
2466 if (!GCCInstallation.isValid())
2467 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002468
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002469 // By default, look for the C++ headers in an include directory adjacent to
2470 // the lib directory of the GCC installation. Note that this is expect to be
2471 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2472 StringRef LibDir = GCCInstallation.getParentLibPath();
2473 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002474 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002475 StringRef TripleStr = GCCInstallation.getTriple().str();
2476
2477 const std::string IncludePathCandidates[] = {
2478 LibDir.str() + "/../include/c++/" + Version.str(),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002479 // Gentoo is weird and places its headers inside the GCC install, so if the
2480 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002481 InstallDir.str() + "/include/g++-v4",
2482 // Android standalone toolchain has C++ headers in yet another place.
2483 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002484 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2485 // without a subdirectory corresponding to the gcc version.
2486 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002487 };
2488
2489 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2490 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2491 GCCInstallation.getMultiarchSuffix()),
2492 DriverArgs, CC1Args))
2493 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002494 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002495}
2496
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002497/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2498
Rafael Espindola0e659592012-02-19 01:38:32 +00002499DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2500 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002501
2502 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002503 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002504 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002505 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002506
Daniel Dunbaree788e72009-12-21 18:54:17 +00002507 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002508 getFilePaths().push_back("/usr/lib");
2509 getFilePaths().push_back("/usr/lib/gcc41");
2510}
2511
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002512Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2513 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002514 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00002515 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002516 Key = Action::AnalyzeJobClass;
2517 else
2518 Key = JA.getKind();
2519
2520 Tool *&T = Tools[Key];
2521 if (!T) {
2522 switch (Key) {
2523 case Action::AssembleJobClass:
2524 T = new tools::dragonfly::Assemble(*this); break;
2525 case Action::LinkJobClass:
2526 T = new tools::dragonfly::Link(*this); break;
2527 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002528 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002529 }
2530 }
2531
2532 return *T;
2533}