blob: 8a9381ec9c75a5e67b1df3a59560c7197fdf89f2 [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();
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000186 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
187 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000188 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000189
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000190 Tool *&T = Tools[Key];
191 if (!T) {
192 switch (Key) {
193 case Action::InputClass:
194 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000195 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000196 case Action::PreprocessJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000197 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000198 case Action::MigrateJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000199 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000200 case Action::CompileJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +0000201 T = new tools::Clang(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000202 case Action::AssembleJobClass: {
203 if (UseIntegratedAs)
204 T = new tools::ClangAs(*this);
205 else
206 T = new tools::darwin::Assemble(*this);
207 break;
208 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000209 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000210 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000211 case Action::LipoJobClass:
212 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000213 case Action::DsymutilJobClass:
214 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000215 case Action::VerifyJobClass:
216 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000217 }
218 }
219
220 return *T;
221}
222
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000223
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000224DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
225 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000226{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000227 getProgramPaths().push_back(getDriver().getInstalledDir());
228 if (getDriver().getInstalledDir() != getDriver().Dir)
229 getProgramPaths().push_back(getDriver().Dir);
230
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000231 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000232 getProgramPaths().push_back(getDriver().getInstalledDir());
233 if (getDriver().getInstalledDir() != getDriver().Dir)
234 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000235
236 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000237 // also add the GCC libexec paths. This is legacy code that can be removed
238 // once fallback is no longer useful.
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000239 AddGCCLibexecPath(DarwinVersion[0]);
240 AddGCCLibexecPath(DarwinVersion[0] - 2);
241 AddGCCLibexecPath(DarwinVersion[0] - 1);
242 AddGCCLibexecPath(DarwinVersion[0] + 1);
243 AddGCCLibexecPath(DarwinVersion[0] + 2);
244}
245
246void DarwinClang::AddGCCLibexecPath(unsigned darwinVersion) {
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000247 std::string ToolChainDir = "i686-apple-darwin";
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000248 ToolChainDir += llvm::utostr(darwinVersion);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000249 ToolChainDir += "/4.2.1";
250
251 std::string Path = getDriver().Dir;
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000252 Path += "/../llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000253 Path += ToolChainDir;
254 getProgramPaths().push_back(Path);
255
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000256 Path = "/usr/llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000257 Path += ToolChainDir;
258 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000259}
260
John McCallf85e1932011-06-15 23:02:42 +0000261void DarwinClang::AddLinkARCArgs(const ArgList &Args,
262 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000263
264 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000265 llvm::sys::Path P(getDriver().ClangExecutable);
266 P.eraseComponent(); // 'clang'
267 P.eraseComponent(); // 'bin'
268 P.appendComponent("lib");
269 P.appendComponent("arc");
270 P.appendComponent("libarclite_");
271 std::string s = P.str();
272 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000273 if (isTargetIOSSimulator())
274 s += "iphonesimulator";
275 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000276 s += "iphoneos";
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000277 // FIXME: Remove this once we depend fully on -mios-simulator-version-min.
John McCall260611a2012-06-20 06:18:46 +0000278 else if (TargetSimulatorVersionFromDefines != VersionTuple())
John McCallf85e1932011-06-15 23:02:42 +0000279 s += "iphonesimulator";
280 else
281 s += "macosx";
282 s += ".a";
283
284 CmdArgs.push_back(Args.MakeArgString(s));
285}
286
Eric Christopher3404fe72011-06-22 17:41:40 +0000287void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000288 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000289 const char *DarwinStaticLib) const {
290 llvm::sys::Path P(getDriver().ResourceDir);
291 P.appendComponent("lib");
292 P.appendComponent("darwin");
293 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000294
Eric Christopher3404fe72011-06-22 17:41:40 +0000295 // For now, allow missing resource libraries to support developers who may
296 // not have compiler-rt checked out or integrated into their build.
297 bool Exists;
298 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
299 CmdArgs.push_back(Args.MakeArgString(P.str()));
300}
301
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000302void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
303 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000304 // Darwin only supports the compiler-rt based runtime libraries.
305 switch (GetRuntimeLibType(Args)) {
306 case ToolChain::RLT_CompilerRT:
307 break;
308 default:
309 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000310 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000311 return;
312 }
313
Daniel Dunbareec99102010-01-22 03:38:14 +0000314 // Darwin doesn't support real static executables, don't link any runtime
315 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000316 if (Args.hasArg(options::OPT_static) ||
317 Args.hasArg(options::OPT_fapple_kext) ||
318 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000319 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000320
321 // Reject -static-libgcc for now, we can deal with this when and if someone
322 // cares. This is useful in situations where someone wants to statically link
323 // something like libstdc++, and needs its runtime support routines.
324 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000325 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000326 << A->getAsString(Args);
327 return;
328 }
329
Daniel Dunbarf4714872011-11-17 00:36:57 +0000330 // If we are building profile support, link that library in.
331 if (Args.hasArg(options::OPT_fprofile_arcs) ||
332 Args.hasArg(options::OPT_fprofile_generate) ||
333 Args.hasArg(options::OPT_fcreate_profile) ||
334 Args.hasArg(options::OPT_coverage)) {
335 // Select the appropriate runtime library for the target.
336 if (isTargetIPhoneOS()) {
337 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
338 } else {
339 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
340 }
341 }
342
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000343 SanitizerArgs Sanitize(getDriver(), Args);
344
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000345 // Add ASAN runtime library, if required. Dynamic libraries and bundles
346 // should not be linked with the runtime library.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000347 if (Sanitize.needsAsanRt()) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000348 if (Args.hasArg(options::OPT_dynamiclib) ||
349 Args.hasArg(options::OPT_bundle)) return;
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000350 if (isTargetIPhoneOS()) {
351 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000352 << "-fsanitize=address";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000353 } else {
354 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
355
356 // The ASAN runtime library requires C++ and CoreFoundation.
357 AddCXXStdlibLibArgs(Args, CmdArgs);
358 CmdArgs.push_back("-framework");
359 CmdArgs.push_back("CoreFoundation");
360 }
361 }
362
Daniel Dunbareec99102010-01-22 03:38:14 +0000363 // Otherwise link libSystem, then the dynamic runtime library, and finally any
364 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000365 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000366
367 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000368 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000369 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
370 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000371 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
372 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
373 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000374
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000375 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000376 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000377 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000378 // The dynamic runtime library was merged with libSystem for 10.6 and
379 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000380 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000381 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000382 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000383 CmdArgs.push_back("-lgcc_s.10.5");
384
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000385 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000386 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000387 // omitted from 10.4.dylib.
388 //
389 // Unfortunately, that turned out to not be true, because Darwin system
390 // headers can still use eprintf on i386, and it is not exported from
391 // libSystem. Therefore, we still must provide a runtime library just for
392 // the tiny tiny handful of projects that *might* use that symbol.
393 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000394 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000395 } else {
396 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000397 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
398 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000399 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000400 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000401}
402
Argyrios Kyrtzidisdceb11f2011-10-18 00:22:49 +0000403static inline StringRef SimulatorVersionDefineName() {
404 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
405}
406
407/// \brief Parse the simulator version define:
408/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
409// and return the grouped values as integers, e.g:
410// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
411// will return Major=4, Minor=2, Micro=1.
412static bool GetVersionFromSimulatorDefine(StringRef define,
413 unsigned &Major, unsigned &Minor,
414 unsigned &Micro) {
415 assert(define.startswith(SimulatorVersionDefineName()));
416 StringRef name, version;
417 llvm::tie(name, version) = define.split('=');
418 if (version.empty())
419 return false;
420 std::string verstr = version.str();
421 char *end;
422 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
423 if (*end != '\0')
424 return false;
425 Major = num / 10000;
426 num = num % 10000;
427 Minor = num / 100;
428 Micro = num % 100;
429 return true;
430}
431
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000432void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000433 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000434
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000435 // Support allowing the SDKROOT environment variable used by xcrun and other
436 // Xcode tools to define the default sysroot, by making it the default for
437 // isysroot.
438 if (!Args.hasArg(options::OPT_isysroot)) {
439 if (char *env = ::getenv("SDKROOT")) {
440 // We only use this value as the default if it is an absolute path and
441 // exists.
442 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env)) {
443 Args.append(Args.MakeSeparateArg(
444 0, Opts.getOption(options::OPT_isysroot), env));
445 }
446 }
447 }
448
Daniel Dunbar26031372010-01-27 00:56:25 +0000449 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000450 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
451 Arg *iOSSimVersion = Args.getLastArg(
452 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000453
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000454 // FIXME: HACK! When compiling for the simulator we don't get a
455 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
456 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
457 // define passed in command-line.
458 if (!iOSVersion && !iOSSimVersion) {
459 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
460 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000461 StringRef define = (*it)->getValue();
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000462 if (define.startswith(SimulatorVersionDefineName())) {
463 unsigned Major = 0, Minor = 0, Micro = 0;
464 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
465 Major < 10 && Minor < 100 && Micro < 100) {
John McCall260611a2012-06-20 06:18:46 +0000466 TargetSimulatorVersionFromDefines = VersionTuple(Major, Minor, Micro);
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000467 }
Bob Wilsona1ec3db2012-07-19 01:35:55 +0000468 // When using the define to indicate the simulator, we force
469 // 10.6 macosx target.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000470 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Bob Wilsona1ec3db2012-07-19 01:35:55 +0000471 OSXVersion = Args.MakeJoinedArg(0, O, "10.6");
472 Args.append(OSXVersion);
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000473 break;
474 }
475 }
476 }
477
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000478 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000479 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000480 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000481 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
482 iOSVersion = iOSSimVersion = 0;
483 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000484 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000485 << iOSVersion->getAsString(Args)
486 << iOSSimVersion->getAsString(Args);
487 iOSSimVersion = 0;
488 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000489 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000490 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000491 StringRef OSXTarget;
492 StringRef iOSTarget;
493 StringRef iOSSimTarget;
494 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
495 OSXTarget = env;
496 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
497 iOSTarget = env;
498 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
499 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000500
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000501 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000502 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000503 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000504 if (iOSTarget.empty()) {
505 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
506 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000507 StringRef isysroot = A->getValue();
Chad Rosiera4884972011-08-31 20:56:25 +0000508 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
509 if (second != "")
510 iOSTarget = second.substr(0,3);
511 }
512 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000513
Chad Rosier4f8de272011-09-28 00:46:32 +0000514 // If no OSX or iOS target has been specified and we're compiling for armv7,
515 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000516 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000517 (getDarwinArchName(Args) == "armv7" ||
518 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000519 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000520
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000521 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000522 //
523 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000524
525 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000526 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000527 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000528 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000529 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000530 "IPHONEOS_DEPLOYMENT_TARGET");
531 }
532
533 // Allow conflicts among OSX and iOS for historical reasons, but choose the
534 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000535 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000536 if (getTriple().getArch() == llvm::Triple::arm ||
537 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000538 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000539 else
Chad Rosiera4884972011-08-31 20:56:25 +0000540 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000541 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000542
Chad Rosiera4884972011-08-31 20:56:25 +0000543 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000544 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000545 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
546 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000547 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000548 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000549 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
550 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000551 } else if (!iOSSimTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000552 const Option O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000553 options::OPT_mios_simulator_version_min_EQ);
554 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
555 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000556 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000557 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000558 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000559 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
560 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000561 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000562 }
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000564 // Reject invalid architecture combinations.
565 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
566 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000567 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000568 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
569 }
570
Daniel Dunbar26031372010-01-27 00:56:25 +0000571 // Set the tool chain target information.
572 unsigned Major, Minor, Micro;
573 bool HadExtra;
574 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000575 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000576 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000577 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000578 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000579 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000580 << OSXVersion->getAsString(Args);
581 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000582 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
583 assert(Version && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000584 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000585 Micro, HadExtra) || HadExtra ||
586 Major >= 10 || Minor >= 100 || Micro >= 100)
587 getDriver().Diag(diag::err_drv_invalid_version_number)
588 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000589 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000590
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000591 bool IsIOSSim = bool(iOSSimVersion);
592
593 // In GCC, the simulator historically was treated as being OS X in some
594 // contexts, like determining the link logic, despite generally being called
595 // with an iOS deployment target. For compatibility, we detect the
596 // simulator as iOS + x86, and treat it differently in a few contexts.
597 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
598 getTriple().getArch() == llvm::Triple::x86_64))
599 IsIOSSim = true;
600
601 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000602}
603
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000604void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000605 ArgStringList &CmdArgs) const {
606 CXXStdlibType Type = GetCXXStdlibType(Args);
607
608 switch (Type) {
609 case ToolChain::CST_Libcxx:
610 CmdArgs.push_back("-lc++");
611 break;
612
613 case ToolChain::CST_Libstdcxx: {
614 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
615 // it was previously found in the gcc lib dir. However, for all the Darwin
616 // platforms we care about it was -lstdc++.6, so we search for that
617 // explicitly if we can't see an obvious -lstdc++ candidate.
618
619 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000620 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000621 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000622 llvm::sys::Path P(A->getValue());
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000623 P.appendComponent("usr");
624 P.appendComponent("lib");
625 P.appendComponent("libstdc++.dylib");
626
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000627 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000628 P.eraseComponent();
629 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000630 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000631 CmdArgs.push_back(Args.MakeArgString(P.str()));
632 return;
633 }
634 }
635 }
636
637 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000638 // FIXME: This should be removed someday when we don't have to care about
639 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000640 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
641 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000642 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
643 return;
644 }
645
646 // Otherwise, let the linker search.
647 CmdArgs.push_back("-lstdc++");
648 break;
649 }
650 }
651}
652
Shantonu Sen7433fed2010-09-17 18:39:08 +0000653void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
654 ArgStringList &CmdArgs) const {
655
656 // For Darwin platforms, use the compiler-rt-based support library
657 // instead of the gcc-provided one (which is also incidentally
658 // only present in the gcc lib dir, which makes it hard to find).
659
660 llvm::sys::Path P(getDriver().ResourceDir);
661 P.appendComponent("lib");
662 P.appendComponent("darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000663
664 // Use the newer cc_kext for iOS ARM after 6.0.
665 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
666 !isIPhoneOSVersionLT(6, 0)) {
667 P.appendComponent("libclang_rt.cc_kext.a");
668 } else {
669 P.appendComponent("libclang_rt.cc_kext_ios5.a");
670 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000671
Shantonu Sen7433fed2010-09-17 18:39:08 +0000672 // For now, allow missing resource libraries to support developers who may
673 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000674 bool Exists;
675 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000676 CmdArgs.push_back(Args.MakeArgString(P.str()));
677}
678
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000679DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
680 const char *BoundArch) const {
681 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
682 const OptTable &Opts = getDriver().getOpts();
683
684 // FIXME: We really want to get out of the tool chain level argument
685 // translation business, as it makes the driver functionality much
686 // more opaque. For now, we follow gcc closely solely for the
687 // purpose of easily achieving feature parity & testability. Once we
688 // have something that works, we should reevaluate each translation
689 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000690
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000691 for (ArgList::const_iterator it = Args.begin(),
692 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000693 Arg *A = *it;
694
695 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000696 // Skip this argument unless the architecture matches either the toolchain
697 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000698 llvm::Triple::ArchType XarchArch =
Richard Smith1d489cf2012-11-01 04:30:05 +0000699 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000700 if (!(XarchArch == getArch() ||
701 (BoundArch && XarchArch ==
Rafael Espindolacfed8282012-10-31 18:51:07 +0000702 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000703 continue;
704
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000705 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000706 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000707 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000708 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000710 // If the argument parsing failed or more than one argument was
711 // consumed, the -Xarch_ argument's parameter tried to consume
712 // extra arguments. Emit an error and ignore.
713 //
714 // We also want to disallow any options which would alter the
715 // driver behavior; that isn't going to work in our model. We
716 // use isDriverOption() as an approximation, although things
717 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000718 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000719 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000720 << A->getAsString(Args);
721 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000722 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000723 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000724 << A->getAsString(Args);
725 continue;
726 }
727
Daniel Dunbar478edc22009-03-29 22:29:05 +0000728 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000729 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000730
731 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000732
733 // Linker input arguments require custom handling. The problem is that we
734 // have already constructed the phase actions, so we can not treat them as
735 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000736 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000737 // Convert the argument into individual Zlinker_input_args.
738 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
739 DAL->AddSeparateArg(OriginalArg,
740 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000741 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000742
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000743 }
744 continue;
745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000747
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000748 // Sob. These is strictly gcc compatible for the time being. Apple
749 // gcc translates options twice, which means that self-expanding
750 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000751 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000752 default:
753 DAL->append(A);
754 break;
755
756 case options::OPT_mkernel:
757 case options::OPT_fapple_kext:
758 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000759 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000760 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000762 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000763 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000764 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000765 break;
766
767 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000768 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
769 DAL->AddFlagArg(A,
770 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000771 break;
772
773 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000774 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
775 DAL->AddFlagArg(A,
776 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000777 break;
778
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000779 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000780 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000781 break;
782
783 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000784 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000785 break;
786
787 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000788 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000789 break;
790
791 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000792 DAL->AddFlagArg(A,
793 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000794 break;
795
796 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000797 DAL->AddFlagArg(A,
798 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000799 break;
800
801 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000802 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000803 break;
804
805 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000806 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000807 break;
808 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000809 }
810
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000811 if (getTriple().getArch() == llvm::Triple::x86 ||
812 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000813 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000814 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000815
816 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000817 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000818 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000819 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000820 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
821 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000822
823 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
824 // which defines the list of which architectures we accept.
825 if (Name == "ppc")
826 ;
827 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000828 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000829 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000830 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000831 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000832 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000833 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000834 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000835 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000836 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000837 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000838 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000839 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000840 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000841 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000842 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000843
844 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000845 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000846
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000847 else if (Name == "i386")
848 ;
849 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000850 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000851 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000852 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000853 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000854 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000855 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000856 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000857 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000858 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000859 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000860 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000861 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000862 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000863
864 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000865 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000866
867 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000868 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000869 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000870 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000871 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000872 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000873 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000874 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000875 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000876 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000877 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000878 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson336bfa32012-09-29 23:52:50 +0000879 else if (Name == "armv7f")
880 DAL->AddJoinedArg(0, MArch, "armv7f");
881 else if (Name == "armv7k")
882 DAL->AddJoinedArg(0, MArch, "armv7k");
883 else if (Name == "armv7s")
884 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000885
886 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000887 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000888 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000889
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000890 // Add an explicit version min argument for the deployment target. We do this
891 // after argument translation because -Xarch_ arguments may add a version min
892 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000893 if (BoundArch)
894 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000895
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000896 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
897 // FIXME: It would be far better to avoid inserting those -static arguments,
898 // but we can't check the deployment target in the translation code until
899 // it is set here.
900 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
901 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
902 Arg *A = *it;
903 ++it;
904 if (A->getOption().getID() != options::OPT_mkernel &&
905 A->getOption().getID() != options::OPT_fapple_kext)
906 continue;
907 assert(it != ie && "unexpected argument translation");
908 A = *it;
909 assert(A->getOption().getID() == options::OPT_static &&
910 "missing expected -static argument");
911 it = DAL->getArgs().erase(it);
912 }
913 }
914
Bob Wilson163b1512011-10-07 17:54:41 +0000915 // Validate the C++ standard library choice.
916 CXXStdlibType Type = GetCXXStdlibType(*DAL);
917 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000918 // Check whether the target provides libc++.
919 StringRef where;
920
921 // Complain about targetting iOS < 5.0 in any way.
John McCalle4860152012-06-21 17:46:38 +0000922 if (TargetSimulatorVersionFromDefines != VersionTuple()) {
923 if (TargetSimulatorVersionFromDefines < VersionTuple(5, 0))
924 where = "iOS 5.0";
925 } else if (isTargetIPhoneOS()) {
926 if (isIPhoneOSVersionLT(5, 0))
927 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000928 }
929
930 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000931 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000932 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000933 }
934 }
935
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000936 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000937}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000938
Daniel Dunbarf3955282009-09-04 18:34:51 +0000939bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000940 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000941}
942
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000943bool Darwin::UseDwarfDebugFlags() const {
944 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
945 return S[0] != '\0';
946 return false;
947}
948
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000949bool Darwin::UseSjLjExceptions() const {
950 // Darwin uses SjLj exceptions on ARM.
951 return (getTriple().getArch() == llvm::Triple::arm ||
952 getTriple().getArch() == llvm::Triple::thumb);
953}
954
Daniel Dunbarf3955282009-09-04 18:34:51 +0000955const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000956 return "pic";
957}
958
Daniel Dunbarf3955282009-09-04 18:34:51 +0000959const char *Darwin::GetForcedPicModel() const {
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000960 if (getArch() == llvm::Triple::x86_64)
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000961 return "pic";
962 return 0;
963}
964
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000965bool Darwin::SupportsProfiling() const {
966 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000967 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000968}
969
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000970bool Darwin::SupportsObjCGC() const {
971 // Garbage collection is supported everywhere except on iPhone OS.
972 return !isTargetIPhoneOS();
973}
974
John McCall0a7dd782012-08-21 02:47:43 +0000975void Darwin::CheckObjCARC() const {
976 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
977 return;
John McCall80fd37a2012-08-27 01:56:21 +0000978 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000979}
980
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000981std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000982Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
983 types::ID InputType) const {
984 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000985}
986
Daniel Dunbar39176082009-03-20 00:20:03 +0000987/// Generic_GCC - A tool chain using the 'gcc' command to perform
988/// all subcommands; this relies on gcc translating the majority of
989/// command line options.
990
Chandler Carruth19347ed2011-11-06 23:39:34 +0000991/// \brief Parse a GCCVersion object out of a string of text.
992///
993/// This is the primary means of forming GCCVersion objects.
994/*static*/
995Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
996 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
997 std::pair<StringRef, StringRef> First = VersionText.split('.');
998 std::pair<StringRef, StringRef> Second = First.second.split('.');
999
1000 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
1001 if (First.first.getAsInteger(10, GoodVersion.Major) ||
1002 GoodVersion.Major < 0)
1003 return BadVersion;
1004 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
1005 GoodVersion.Minor < 0)
1006 return BadVersion;
1007
1008 // First look for a number prefix and parse that if present. Otherwise just
1009 // stash the entire patch string in the suffix, and leave the number
1010 // unspecified. This covers versions strings such as:
1011 // 4.4
1012 // 4.4.0
1013 // 4.4.x
1014 // 4.4.2-rc4
1015 // 4.4.x-patched
1016 // And retains any patch number it finds.
1017 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1018 if (!PatchText.empty()) {
1019 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
1020 // Try to parse the number and any suffix.
1021 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1022 GoodVersion.Patch < 0)
1023 return BadVersion;
1024 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
1025 }
1026 }
1027
1028 return GoodVersion;
1029}
1030
1031/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
1032bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
1033 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
1034 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
1035
1036 // Note that we rank versions with *no* patch specified is better than ones
1037 // hard-coding a patch version. Thus if the RHS has no patch, it always
1038 // wins, and the LHS only wins if it has no patch and the RHS does have
1039 // a patch.
1040 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
1041 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
Gabor Greif241cbe42012-04-18 10:59:08 +00001042 if (PatchSuffix == RHS.PatchSuffix) return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001043
1044 // Finally, between completely tied version numbers, the version with the
1045 // suffix loses as we prefer full releases.
1046 if (RHS.PatchSuffix.empty()) return true;
1047 return false;
1048}
1049
Rafael Espindola0e659592012-02-19 01:38:32 +00001050static StringRef getGCCToolchainDir(const ArgList &Args) {
1051 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1052 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +00001053 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +00001054 return GCC_INSTALL_PREFIX;
1055}
1056
Chandler Carruth19347ed2011-11-06 23:39:34 +00001057/// \brief Construct a GCCInstallationDetector from the driver.
1058///
1059/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +00001060/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001061///
1062/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1063/// should instead pull the target out of the driver. This is currently
1064/// necessary because the driver doesn't store the final version of the target
1065/// triple.
1066Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1067 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001068 const llvm::Triple &TargetTriple,
1069 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001070 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001071 llvm::Triple MultiarchTriple
1072 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1073 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001074 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001075 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001076 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001077 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001078 SmallVector<StringRef, 10> CandidateTripleAliases;
1079 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1080 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1081 CandidateTripleAliases,
1082 CandidateMultiarchLibDirs,
1083 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001084
1085 // Compute the set of prefixes for our search.
1086 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1087 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001088
Rafael Espindola0e659592012-02-19 01:38:32 +00001089 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1090 if (GCCToolchainDir != "") {
1091 if (GCCToolchainDir.back() == '/')
1092 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001093
Rafael Espindola0e659592012-02-19 01:38:32 +00001094 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001095 } else {
1096 Prefixes.push_back(D.SysRoot);
1097 Prefixes.push_back(D.SysRoot + "/usr");
1098 Prefixes.push_back(D.InstalledDir + "/..");
1099 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001100
1101 // Loop over the various components which exist and select the best GCC
1102 // installation available. GCC installs are ranked by version number.
1103 Version = GCCVersion::Parse("0.0.0");
1104 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1105 if (!llvm::sys::fs::exists(Prefixes[i]))
1106 continue;
1107 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1108 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1109 if (!llvm::sys::fs::exists(LibDir))
1110 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001111 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001112 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1113 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001114 }
1115 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1116 const std::string LibDir
1117 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1118 if (!llvm::sys::fs::exists(LibDir))
1119 continue;
1120 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1121 ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001122 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001123 CandidateMultiarchTripleAliases[k],
1124 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001125 }
1126 }
1127}
1128
1129/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001130 const llvm::Triple &TargetTriple,
1131 const llvm::Triple &MultiarchTriple,
1132 SmallVectorImpl<StringRef> &LibDirs,
1133 SmallVectorImpl<StringRef> &TripleAliases,
1134 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1135 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1136 // Declare a bunch of static data sets that we'll select between below. These
1137 // are specifically designed to always refer to string literals to avoid any
1138 // lifetime or initialization issues.
1139 static const char *const ARMLibDirs[] = { "/lib" };
1140 static const char *const ARMTriples[] = {
1141 "arm-linux-gnueabi",
1142 "arm-linux-androideabi"
1143 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001144 static const char *const ARMHFTriples[] = {
1145 "arm-linux-gnueabihf",
1146 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001147
1148 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1149 static const char *const X86_64Triples[] = {
1150 "x86_64-linux-gnu",
1151 "x86_64-unknown-linux-gnu",
1152 "x86_64-pc-linux-gnu",
1153 "x86_64-redhat-linux6E",
1154 "x86_64-redhat-linux",
1155 "x86_64-suse-linux",
1156 "x86_64-manbo-linux-gnu",
1157 "x86_64-linux-gnu",
1158 "x86_64-slackware-linux"
1159 };
1160 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1161 static const char *const X86Triples[] = {
1162 "i686-linux-gnu",
1163 "i686-pc-linux-gnu",
1164 "i486-linux-gnu",
1165 "i386-linux-gnu",
1166 "i686-redhat-linux",
1167 "i586-redhat-linux",
1168 "i386-redhat-linux",
1169 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001170 "i486-slackware-linux",
1171 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001172 };
1173
1174 static const char *const MIPSLibDirs[] = { "/lib" };
1175 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1176 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001177 static const char *const MIPSELTriples[] = {
1178 "mipsel-linux-gnu",
1179 "mipsel-linux-android"
1180 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001181
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001182 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1183 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1184 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1185 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1186
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001187 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1188 static const char *const PPCTriples[] = {
1189 "powerpc-linux-gnu",
1190 "powerpc-unknown-linux-gnu",
Gabor Greif91720912012-05-15 11:21:03 +00001191 "powerpc-suse-linux",
1192 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001193 };
1194 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1195 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001196 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001197 "powerpc64-unknown-linux-gnu",
1198 "powerpc64-suse-linux",
1199 "ppc64-redhat-linux"
1200 };
1201
1202 switch (TargetTriple.getArch()) {
1203 case llvm::Triple::arm:
1204 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001205 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001206 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1207 TripleAliases.append(
1208 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1209 } else {
1210 TripleAliases.append(
1211 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1212 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001213 break;
1214 case llvm::Triple::x86_64:
1215 LibDirs.append(
1216 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1217 TripleAliases.append(
1218 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1219 MultiarchLibDirs.append(
1220 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1221 MultiarchTripleAliases.append(
1222 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1223 break;
1224 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001225 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001226 TripleAliases.append(
1227 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1228 MultiarchLibDirs.append(
1229 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1230 MultiarchTripleAliases.append(
1231 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1232 break;
1233 case llvm::Triple::mips:
1234 LibDirs.append(
1235 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1236 TripleAliases.append(
1237 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001238 MultiarchLibDirs.append(
1239 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1240 MultiarchTripleAliases.append(
1241 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001242 break;
1243 case llvm::Triple::mipsel:
1244 LibDirs.append(
1245 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1246 TripleAliases.append(
1247 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001248 MultiarchLibDirs.append(
1249 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1250 MultiarchTripleAliases.append(
1251 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1252 break;
1253 case llvm::Triple::mips64:
1254 LibDirs.append(
1255 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1256 TripleAliases.append(
1257 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1258 MultiarchLibDirs.append(
1259 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1260 MultiarchTripleAliases.append(
1261 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1262 break;
1263 case llvm::Triple::mips64el:
1264 LibDirs.append(
1265 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1266 TripleAliases.append(
1267 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1268 MultiarchLibDirs.append(
1269 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1270 MultiarchTripleAliases.append(
1271 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001272 break;
1273 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001274 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001275 TripleAliases.append(
1276 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1277 MultiarchLibDirs.append(
1278 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1279 MultiarchTripleAliases.append(
1280 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1281 break;
1282 case llvm::Triple::ppc64:
1283 LibDirs.append(
1284 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1285 TripleAliases.append(
1286 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1287 MultiarchLibDirs.append(
1288 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1289 MultiarchTripleAliases.append(
1290 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1291 break;
1292
1293 default:
1294 // By default, just rely on the standard lib directories and the original
1295 // triple.
1296 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001297 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001298
1299 // Always append the drivers target triple to the end, in case it doesn't
1300 // match any of our aliases.
1301 TripleAliases.push_back(TargetTriple.str());
1302
1303 // Also include the multiarch variant if it's different.
1304 if (TargetTriple.str() != MultiarchTriple.str())
1305 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001306}
1307
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001308// FIXME: There is the same routine in the Tools.cpp.
1309static bool hasMipsN32ABIArg(const ArgList &Args) {
1310 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00001311 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001312}
1313
1314static StringRef getTargetMultiarchSuffix(llvm::Triple::ArchType TargetArch,
1315 const ArgList &Args) {
1316 if (TargetArch == llvm::Triple::x86_64 ||
1317 TargetArch == llvm::Triple::ppc64)
1318 return "/64";
1319
1320 if (TargetArch == llvm::Triple::mips64 ||
1321 TargetArch == llvm::Triple::mips64el) {
1322 if (hasMipsN32ABIArg(Args))
1323 return "/n32";
1324 else
1325 return "/64";
1326 }
1327
1328 return "/32";
1329}
1330
Chandler Carruth19347ed2011-11-06 23:39:34 +00001331void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001332 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1333 const std::string &LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001334 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001335 // There are various different suffixes involving the triple we
1336 // check for. We also record what is necessary to walk from each back
1337 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001338 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001339 "/gcc/" + CandidateTriple.str(),
1340 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1341
Hal Finkel02014b42012-09-18 22:25:07 +00001342 // The Freescale PPC SDK has the gcc libraries in
1343 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1344 "/" + CandidateTriple.str(),
1345
Chandler Carruth19347ed2011-11-06 23:39:34 +00001346 // Ubuntu has a strange mis-matched pair of triples that this happens to
1347 // match.
1348 // FIXME: It may be worthwhile to generalize this and look for a second
1349 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001350 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001351 };
1352 const std::string InstallSuffixes[] = {
1353 "/../../..",
1354 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001355 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001356 "/../../../.."
1357 };
1358 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001359 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1360 (TargetArch != llvm::Triple::x86));
1361 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1362 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001363 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001364 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001365 !EC && LI != LE; LI = LI.increment(EC)) {
1366 StringRef VersionText = llvm::sys::path::filename(LI->path());
1367 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1368 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1369 if (CandidateVersion < MinVersion)
1370 continue;
1371 if (CandidateVersion <= Version)
1372 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001373
1374 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001375 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001376 // libs in a subdirectory named 64. The simple logic we follow is that
1377 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1378 // we use that. If not, and if not a multiarch triple, we look for
1379 // crtbegin.o without the subdirectory.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001380 StringRef MultiarchSuffix = getTargetMultiarchSuffix(TargetArch, Args);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001381 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1382 GCCMultiarchSuffix = MultiarchSuffix.str();
1383 } else {
1384 if (NeedsMultiarchSuffix ||
1385 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1386 continue;
1387 GCCMultiarchSuffix.clear();
1388 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001389
1390 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001391 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001392 // FIXME: We hack together the directory name here instead of
1393 // using LI to ensure stable path separators across Windows and
1394 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001395 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001396 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001397 IsValid = true;
1398 }
1399 }
1400}
1401
Rafael Espindola0e659592012-02-19 01:38:32 +00001402Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1403 const ArgList &Args)
1404 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001405 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001406 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001407 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001408}
1409
Daniel Dunbar39176082009-03-20 00:20:03 +00001410Generic_GCC::~Generic_GCC() {
1411 // Free tool implementations.
1412 for (llvm::DenseMap<unsigned, Tool*>::iterator
1413 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1414 delete it->second;
1415}
1416
Mike Stump1eb44332009-09-09 15:08:12 +00001417Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001418 const JobAction &JA,
1419 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001420 Action::ActionClass Key = JA.getKind();
Daniel Dunbar39176082009-03-20 00:20:03 +00001421
1422 Tool *&T = Tools[Key];
1423 if (!T) {
1424 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001425 case Action::InputClass:
1426 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001427 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001428 case Action::PreprocessJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001429 case Action::PrecompileJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001430 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001431 case Action::MigrateJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001432 case Action::CompileJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001433 T = new tools::Clang(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001434 case Action::AssembleJobClass:
1435 T = new tools::gcc::Assemble(*this); break;
1436 case Action::LinkJobClass:
1437 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001439 // This is a bit ungeneric, but the only platform using a driver
1440 // driver is Darwin.
1441 case Action::LipoJobClass:
1442 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001443 case Action::DsymutilJobClass:
1444 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001445 case Action::VerifyJobClass:
1446 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001447 }
1448 }
1449
1450 return *T;
1451}
1452
Daniel Dunbar39176082009-03-20 00:20:03 +00001453bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00001454 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00001455}
1456
1457const char *Generic_GCC::GetDefaultRelocationModel() const {
1458 return "static";
1459}
1460
1461const char *Generic_GCC::GetForcedPicModel() const {
1462 return 0;
1463}
Tony Linthicum96319392011-12-12 21:14:55 +00001464/// Hexagon Toolchain
1465
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001466Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple& Triple)
1467 : ToolChain(D, Triple) {
Tony Linthicum96319392011-12-12 21:14:55 +00001468 getProgramPaths().push_back(getDriver().getInstalledDir());
1469 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1470 getProgramPaths().push_back(getDriver().Dir);
1471}
1472
1473Hexagon_TC::~Hexagon_TC() {
1474 // Free tool implementations.
1475 for (llvm::DenseMap<unsigned, Tool*>::iterator
1476 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1477 delete it->second;
1478}
1479
1480Tool &Hexagon_TC::SelectTool(const Compilation &C,
1481 const JobAction &JA,
1482 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001483 Action::ActionClass Key = JA.getKind();
Tony Linthicum96319392011-12-12 21:14:55 +00001484 Tool *&T = Tools[Key];
1485 if (!T) {
1486 switch (Key) {
1487 case Action::InputClass:
1488 case Action::BindArchClass:
1489 assert(0 && "Invalid tool kind.");
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001490 case Action::PreprocessJobClass:
1491 case Action::PrecompileJobClass:
Tony Linthicum96319392011-12-12 21:14:55 +00001492 case Action::AnalyzeJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001493 case Action::MigrateJobClass:
1494 case Action::CompileJobClass:
Tony Linthicum96319392011-12-12 21:14:55 +00001495 T = new tools::Clang(*this); break;
1496 case Action::AssembleJobClass:
1497 T = new tools::hexagon::Assemble(*this); break;
1498 case Action::LinkJobClass:
1499 T = new tools::hexagon::Link(*this); break;
1500 default:
1501 assert(false && "Unsupported action for Hexagon target.");
1502 }
1503 }
1504
1505 return *T;
1506}
1507
Tony Linthicum96319392011-12-12 21:14:55 +00001508const char *Hexagon_TC::GetDefaultRelocationModel() const {
1509 return "static";
1510}
1511
1512const char *Hexagon_TC::GetForcedPicModel() const {
1513 return 0;
1514} // End Hexagon
1515
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001516
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001517/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1518/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1519/// Currently does not support anything else but compilation.
1520
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001521TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1522 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001523 // Path mangling to find libexec
1524 std::string Path(getDriver().Dir);
1525
1526 Path += "/../libexec";
1527 getProgramPaths().push_back(Path);
1528}
1529
1530TCEToolChain::~TCEToolChain() {
1531 for (llvm::DenseMap<unsigned, Tool*>::iterator
1532 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1533 delete it->second;
1534}
1535
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001536bool TCEToolChain::IsMathErrnoDefault() const {
1537 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001538}
1539
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001540const char *TCEToolChain::GetDefaultRelocationModel() const {
1541 return "static";
1542}
1543
1544const char *TCEToolChain::GetForcedPicModel() const {
1545 return 0;
1546}
1547
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001548Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001549 const JobAction &JA,
1550 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001551 Action::ActionClass Key;
1552 Key = Action::AnalyzeJobClass;
1553
1554 Tool *&T = Tools[Key];
1555 if (!T) {
1556 switch (Key) {
1557 case Action::PreprocessJobClass:
1558 T = new tools::gcc::Preprocess(*this); break;
1559 case Action::AnalyzeJobClass:
1560 T = new tools::Clang(*this); break;
1561 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001562 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001563 }
1564 }
1565 return *T;
1566}
1567
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001568/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1569
Rafael Espindola0e659592012-02-19 01:38:32 +00001570OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1571 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001572 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001573 getFilePaths().push_back("/usr/lib");
1574}
1575
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001576Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1577 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001578 Action::ActionClass Key = JA.getKind();
Rafael Espindoladda5b922010-11-07 23:13:01 +00001579 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1580 options::OPT_no_integrated_as,
1581 IsIntegratedAssemblerDefault());
1582
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001583 Tool *&T = Tools[Key];
1584 if (!T) {
1585 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001586 case Action::AssembleJobClass: {
1587 if (UseIntegratedAs)
1588 T = new tools::ClangAs(*this);
1589 else
1590 T = new tools::openbsd::Assemble(*this);
1591 break;
1592 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001593 case Action::LinkJobClass:
1594 T = new tools::openbsd::Link(*this); break;
1595 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001596 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001597 }
1598 }
1599
1600 return *T;
1601}
1602
Eli Friedman42f74f22012-08-08 23:57:20 +00001603/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1604
1605Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1606 : Generic_ELF(D, Triple, Args) {
1607 getFilePaths().push_back(getDriver().Dir + "/../lib");
1608 getFilePaths().push_back("/usr/lib");
1609}
1610
1611Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA,
1612 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001613 Action::ActionClass Key = JA.getKind();
Eli Friedman42f74f22012-08-08 23:57:20 +00001614 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1615 options::OPT_no_integrated_as,
1616 IsIntegratedAssemblerDefault());
1617
1618 Tool *&T = Tools[Key];
1619 if (!T) {
1620 switch (Key) {
1621 case Action::AssembleJobClass: {
1622 if (UseIntegratedAs)
1623 T = new tools::ClangAs(*this);
1624 else
1625 T = new tools::bitrig::Assemble(*this);
1626 break;
1627 }
1628 case Action::LinkJobClass:
1629 T = new tools::bitrig::Link(*this); break;
1630 default:
1631 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1632 }
1633 }
1634
1635 return *T;
1636}
1637
1638void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1639 ArgStringList &CC1Args) const {
1640 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1641 DriverArgs.hasArg(options::OPT_nostdincxx))
1642 return;
1643
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001644 switch (GetCXXStdlibType(DriverArgs)) {
1645 case ToolChain::CST_Libcxx:
1646 addSystemInclude(DriverArgs, CC1Args,
1647 getDriver().SysRoot + "/usr/include/c++/");
1648 break;
1649 case ToolChain::CST_Libstdcxx:
1650 addSystemInclude(DriverArgs, CC1Args,
1651 getDriver().SysRoot + "/usr/include/c++/stdc++");
1652 addSystemInclude(DriverArgs, CC1Args,
1653 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001654
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001655 StringRef Triple = getTriple().str();
1656 if (Triple.startswith("amd64"))
1657 addSystemInclude(DriverArgs, CC1Args,
1658 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1659 Triple.substr(5));
1660 else
1661 addSystemInclude(DriverArgs, CC1Args,
1662 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1663 Triple);
1664 break;
1665 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001666}
1667
1668void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1669 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001670 switch (GetCXXStdlibType(Args)) {
1671 case ToolChain::CST_Libcxx:
1672 CmdArgs.push_back("-lc++");
1673 CmdArgs.push_back("-lcxxrt");
1674 // Include supc++ to provide Unwind until provided by libcxx.
1675 CmdArgs.push_back("-lgcc");
1676 break;
1677 case ToolChain::CST_Libstdcxx:
1678 CmdArgs.push_back("-lstdc++");
1679 break;
1680 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001681}
1682
Daniel Dunbar75358d22009-03-30 21:06:03 +00001683/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1684
Rafael Espindola0e659592012-02-19 01:38:32 +00001685FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1686 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001687
Chandler Carruth24248e32012-01-26 01:35:15 +00001688 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1689 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001690 if ((Triple.getArch() == llvm::Triple::x86 ||
1691 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001692 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001693 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1694 else
1695 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001696}
1697
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001698Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1699 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001700 Action::ActionClass Key = JA.getKind();
Roman Divacky67dece72010-11-08 17:46:39 +00001701 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1702 options::OPT_no_integrated_as,
1703 IsIntegratedAssemblerDefault());
1704
Daniel Dunbar75358d22009-03-30 21:06:03 +00001705 Tool *&T = Tools[Key];
1706 if (!T) {
1707 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001708 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001709 if (UseIntegratedAs)
1710 T = new tools::ClangAs(*this);
1711 else
1712 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001713 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001714 case Action::LinkJobClass:
1715 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001716 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001717 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001718 }
1719 }
1720
1721 return *T;
1722}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001723
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001724/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1725
Rafael Espindola0e659592012-02-19 01:38:32 +00001726NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1727 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001728
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001729 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001730 // When targeting a 32-bit platform, try the special directory used on
1731 // 64-bit hosts, and only fall back to the main library directory if that
1732 // doesn't work.
1733 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1734 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001735 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001736 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001737
1738 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001739 }
1740}
1741
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001742Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1743 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001744 Action::ActionClass Key = JA.getKind();
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001745 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1746 options::OPT_no_integrated_as,
1747 IsIntegratedAssemblerDefault());
1748
1749 Tool *&T = Tools[Key];
1750 if (!T) {
1751 switch (Key) {
1752 case Action::AssembleJobClass:
1753 if (UseIntegratedAs)
1754 T = new tools::ClangAs(*this);
1755 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001756 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001757 break;
1758 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001759 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001760 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001761 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001762 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001763 }
1764 }
1765
1766 return *T;
1767}
1768
Chris Lattner38e317d2010-07-07 16:01:42 +00001769/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1770
Rafael Espindola0e659592012-02-19 01:38:32 +00001771Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1772 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001773 getFilePaths().push_back(getDriver().Dir + "/../lib");
1774 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001775}
1776
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001777Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1778 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001779 Action::ActionClass Key = JA.getKind();
Chris Lattner38e317d2010-07-07 16:01:42 +00001780
1781 Tool *&T = Tools[Key];
1782 if (!T) {
1783 switch (Key) {
1784 case Action::AssembleJobClass:
1785 T = new tools::minix::Assemble(*this); break;
1786 case Action::LinkJobClass:
1787 T = new tools::minix::Link(*this); break;
1788 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001789 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001790 }
1791 }
1792
1793 return *T;
1794}
1795
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001796/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1797
Rafael Espindola0e659592012-02-19 01:38:32 +00001798AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1799 const ArgList &Args)
1800 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001801
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001802 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001803 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001804 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001805
Daniel Dunbaree788e72009-12-21 18:54:17 +00001806 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001807 getFilePaths().push_back("/usr/lib");
1808 getFilePaths().push_back("/usr/sfw/lib");
1809 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001810 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001811
1812}
1813
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001814Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1815 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001816 Action::ActionClass Key = JA.getKind();
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001817
1818 Tool *&T = Tools[Key];
1819 if (!T) {
1820 switch (Key) {
1821 case Action::AssembleJobClass:
1822 T = new tools::auroraux::Assemble(*this); break;
1823 case Action::LinkJobClass:
1824 T = new tools::auroraux::Link(*this); break;
1825 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001826 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001827 }
1828 }
1829
1830 return *T;
1831}
1832
David Chisnall31c46902012-02-15 13:39:01 +00001833/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1834
Rafael Espindola0e659592012-02-19 01:38:32 +00001835Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1836 const ArgList &Args)
1837 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001838
1839 getProgramPaths().push_back(getDriver().getInstalledDir());
1840 if (getDriver().getInstalledDir() != getDriver().Dir)
1841 getProgramPaths().push_back(getDriver().Dir);
1842
1843 getFilePaths().push_back(getDriver().Dir + "/../lib");
1844 getFilePaths().push_back("/usr/lib");
1845}
1846
1847Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
1848 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001849 Action::ActionClass Key = JA.getKind();
David Chisnall31c46902012-02-15 13:39:01 +00001850
1851 Tool *&T = Tools[Key];
1852 if (!T) {
1853 switch (Key) {
1854 case Action::AssembleJobClass:
1855 T = new tools::solaris::Assemble(*this); break;
1856 case Action::LinkJobClass:
1857 T = new tools::solaris::Link(*this); break;
1858 default:
1859 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1860 }
1861 }
1862
1863 return *T;
1864}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001865
Eli Friedman6b3454a2009-05-26 07:52:18 +00001866/// Linux toolchain (very bare-bones at the moment).
1867
Rafael Espindolac1da9812010-11-07 20:14:31 +00001868enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001869 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001870 DebianLenny,
1871 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001872 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001873 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001874 RHEL4,
1875 RHEL5,
1876 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001877 Fedora13,
1878 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001879 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001880 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001881 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001882 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001883 OpenSuse11_4,
1884 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001885 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00001886 UbuntuHardy,
1887 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001888 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001889 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001890 UbuntuLucid,
1891 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001892 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001893 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001894 UbuntuPrecise,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001895 UnknownDistro
1896};
1897
Chris Lattnerd753b562011-05-22 05:36:06 +00001898static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001899 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1900 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001901}
1902
1903static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001904 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001905}
1906
1907static bool IsDebian(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001908 return Distro >= DebianLenny && Distro <= DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001909}
1910
1911static bool IsUbuntu(enum LinuxDistro Distro) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001912 return Distro >= UbuntuHardy && Distro <= UbuntuPrecise;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001913}
1914
Rafael Espindolac1da9812010-11-07 20:14:31 +00001915static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001916 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001917 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001918 StringRef Data = File.get()->getBuffer();
1919 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001920 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001921 LinuxDistro Version = UnknownDistro;
1922 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
1923 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
1924 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
1925 .Case("hardy", UbuntuHardy)
1926 .Case("intrepid", UbuntuIntrepid)
1927 .Case("jaunty", UbuntuJaunty)
1928 .Case("karmic", UbuntuKarmic)
1929 .Case("lucid", UbuntuLucid)
1930 .Case("maverick", UbuntuMaverick)
1931 .Case("natty", UbuntuNatty)
1932 .Case("oneiric", UbuntuOneiric)
1933 .Case("precise", UbuntuPrecise)
1934 .Default(UnknownDistro);
1935 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001936 }
1937
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001938 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001939 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001940 if (Data.startswith("Fedora release 16"))
1941 return Fedora16;
1942 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00001943 return Fedora15;
1944 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001945 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001946 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001947 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001948 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001949 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001950 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001951 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001952 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001953 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001954 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1955 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001956 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001957 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001958 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1959 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001960 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001961 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001962 return UnknownDistro;
1963 }
1964
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001965 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001966 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001967 if (Data[0] == '5')
1968 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001969 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00001970 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001971 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00001972 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001973 return UnknownDistro;
1974 }
1975
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001976 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
1977 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
1978 .StartsWith("openSUSE 11.3", OpenSuse11_3)
1979 .StartsWith("openSUSE 11.4", OpenSuse11_4)
1980 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001981 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001982 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001983
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001984 bool Exists;
1985 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001986 return Exherbo;
1987
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001988 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1989 return ArchLinux;
1990
Rafael Espindolac1da9812010-11-07 20:14:31 +00001991 return UnknownDistro;
1992}
1993
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001994/// \brief Get our best guess at the multiarch triple for a target.
1995///
1996/// Debian-based systems are starting to use a multiarch setup where they use
1997/// a target-triple directory in the library and header search paths.
1998/// Unfortunately, this triple does not align with the vanilla target triple,
1999/// so we provide a rough mapping here.
2000static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2001 StringRef SysRoot) {
2002 // For most architectures, just use whatever we have rather than trying to be
2003 // clever.
2004 switch (TargetTriple.getArch()) {
2005 default:
2006 return TargetTriple.str();
2007
2008 // We use the existence of '/lib/<triple>' as a directory to detect some
2009 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00002010 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2011 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00002012 case llvm::Triple::arm:
2013 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00002014 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2015 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2016 return "arm-linux-gnueabihf";
2017 } else {
2018 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2019 return "arm-linux-gnueabi";
2020 }
Chad Rosier0337efd2012-07-11 19:08:21 +00002021 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002022 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002023 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2024 return "i386-linux-gnu";
2025 return TargetTriple.str();
2026 case llvm::Triple::x86_64:
2027 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2028 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002029 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00002030 case llvm::Triple::mips:
2031 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2032 return "mips-linux-gnu";
2033 return TargetTriple.str();
2034 case llvm::Triple::mipsel:
2035 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2036 return "mipsel-linux-gnu";
2037 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002038 case llvm::Triple::ppc:
2039 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2040 return "powerpc-linux-gnu";
2041 return TargetTriple.str();
2042 case llvm::Triple::ppc64:
2043 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2044 return "powerpc64-linux-gnu";
2045 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002046 }
2047}
2048
Chandler Carruth00646ba2012-01-25 11:24:24 +00002049static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2050 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2051}
2052
Simon Atanasyan7a918882012-09-14 11:27:24 +00002053static bool isMipsArch(llvm::Triple::ArchType Arch) {
2054 return Arch == llvm::Triple::mips ||
2055 Arch == llvm::Triple::mipsel ||
2056 Arch == llvm::Triple::mips64 ||
2057 Arch == llvm::Triple::mips64el;
2058}
2059
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002060static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2061 const ArgList &Args) {
2062 if (Arch != llvm::Triple::mips &&
2063 Arch != llvm::Triple::mipsel)
2064 return false;
2065
2066 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2067 options::OPT_mcpu_EQ,
2068 options::OPT_mips_CPUs_Group);
2069
2070 if (!A)
2071 return false;
2072
2073 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2074 return A->getOption().matches(options::OPT_mips32r2);
2075
Richard Smith1d489cf2012-11-01 04:30:05 +00002076 return A->getValue() == StringRef("mips32r2");
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002077}
2078
Simon Atanasyan7a918882012-09-14 11:27:24 +00002079static StringRef getMultilibDir(const llvm::Triple &Triple,
2080 const ArgList &Args) {
2081 if (!isMipsArch(Triple.getArch()))
2082 return Triple.isArch32Bit() ? "lib32" : "lib64";
2083
2084 // lib32 directory has a special meaning on MIPS targets.
2085 // It contains N32 ABI binaries. Use this folder if produce
2086 // code for N32 ABI only.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00002087 if (hasMipsN32ABIArg(Args))
Simon Atanasyan7a918882012-09-14 11:27:24 +00002088 return "lib32";
2089
2090 return Triple.isArch32Bit() ? "lib" : "lib64";
2091}
2092
Rafael Espindola0e659592012-02-19 01:38:32 +00002093Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2094 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002095 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00002096 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002097
Rafael Espindolaab784082011-09-01 16:25:49 +00002098 // OpenSuse stores the linker with the compiler, add that to the search
2099 // path.
2100 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002101 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002102 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002103
2104 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002105
2106 LinuxDistro Distro = DetectLinuxDistro(Arch);
2107
Chris Lattner64a89172011-05-22 16:45:07 +00002108 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002109 ExtraOpts.push_back("-z");
2110 ExtraOpts.push_back("relro");
2111 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002112
Douglas Gregorf0594d82011-03-06 19:11:49 +00002113 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002114 ExtraOpts.push_back("-X");
2115
Logan Chien94a71422012-09-02 09:30:11 +00002116 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002117
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002118 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2119 // and the MIPS ABI require .dynsym to be sorted in different ways.
2120 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2121 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002122 // Android loader does not support .gnu.hash.
Simon Atanasyan7a918882012-09-14 11:27:24 +00002123 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002124 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2125 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002126 ExtraOpts.push_back("--hash-style=gnu");
2127
2128 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2129 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2130 ExtraOpts.push_back("--hash-style=both");
2131 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002132
Chris Lattnerd753b562011-05-22 05:36:06 +00002133 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002134 ExtraOpts.push_back("--no-add-needed");
2135
Eli Friedman0b200f62011-06-02 21:36:53 +00002136 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002137 IsOpenSuse(Distro) ||
2138 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002139 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002140 ExtraOpts.push_back("--build-id");
2141
Chris Lattner64a89172011-05-22 16:45:07 +00002142 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002143 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002144
Chandler Carruthd2deee12011-10-03 05:28:29 +00002145 // The selection of paths to try here is designed to match the patterns which
2146 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2147 // This was determined by running GCC in a fake filesystem, creating all
2148 // possible permutations of these directories, and seeing which ones it added
2149 // to the link paths.
2150 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002151
Simon Atanasyan7a918882012-09-14 11:27:24 +00002152 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002153 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002154
Chandler Carruthd1f73062011-11-06 23:09:05 +00002155 // Add the multilib suffixed paths where they are available.
2156 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002157 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002158 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002159
2160 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2161 addPathIfExists(GCCInstallation.getInstallPath() +
2162 GCCInstallation.getMultiarchSuffix() +
2163 "/mips-r2",
2164 Paths);
2165 else
2166 addPathIfExists((GCCInstallation.getInstallPath() +
2167 GCCInstallation.getMultiarchSuffix()),
2168 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002169
2170 // If the GCC installation we found is inside of the sysroot, we want to
2171 // prefer libraries installed in the parent prefix of the GCC installation.
2172 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002173 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002174 // This usually happens when there is an external cross compiler on the
2175 // host system, and a more minimal sysroot available that is the target of
2176 // the cross.
2177 if (StringRef(LibPath).startswith(SysRoot)) {
2178 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2179 Paths);
2180 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2181 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2182 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002183 // On Android, libraries in the parent prefix of the GCC installation are
2184 // preferred to the ones under sysroot.
2185 if (IsAndroid) {
2186 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2187 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002188 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002189 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2190 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2191 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2192 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2193
2194 // Try walking via the GCC triple path in case of multiarch GCC
2195 // installations with strange symlinks.
2196 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002197 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002198 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002199
Chandler Carruth7a09d012011-10-16 10:54:30 +00002200 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002201 if (GCCInstallation.isValid()) {
2202 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002203 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002204 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002205 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002206
2207 if (StringRef(LibPath).startswith(SysRoot)) {
2208 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2209 addPathIfExists(LibPath, Paths);
2210 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002211 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002212 addPathIfExists(SysRoot + "/lib", Paths);
2213 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002214}
2215
2216bool Linux::HasNativeLLVMSupport() const {
2217 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002218}
2219
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002220Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2221 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00002222 Action::ActionClass Key = JA.getKind();
Rafael Espindoladda5b922010-11-07 23:13:01 +00002223 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2224 options::OPT_no_integrated_as,
2225 IsIntegratedAssemblerDefault());
2226
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002227 Tool *&T = Tools[Key];
2228 if (!T) {
2229 switch (Key) {
2230 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002231 if (UseIntegratedAs)
2232 T = new tools::ClangAs(*this);
2233 else
2234 T = new tools::linuxtools::Assemble(*this);
2235 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002236 case Action::LinkJobClass:
2237 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002238 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002239 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002240 }
2241 }
2242
2243 return *T;
2244}
2245
Rafael Espindola8af669f2012-06-19 01:26:10 +00002246void Linux::addClangTargetOptions(ArgStringList &CC1Args) const {
2247 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
2248 if (V >= Generic_GCC::GCCVersion::Parse("4.7.0"))
2249 CC1Args.push_back("-fuse-init-array");
2250}
2251
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002252void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2253 ArgStringList &CC1Args) const {
2254 const Driver &D = getDriver();
2255
2256 if (DriverArgs.hasArg(options::OPT_nostdinc))
2257 return;
2258
2259 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2260 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2261
2262 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002263 llvm::sys::Path P(D.ResourceDir);
2264 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002265 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002266 }
2267
2268 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2269 return;
2270
2271 // Check for configure-time C include directories.
2272 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2273 if (CIncludeDirs != "") {
2274 SmallVector<StringRef, 5> dirs;
2275 CIncludeDirs.split(dirs, ":");
2276 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2277 I != E; ++I) {
2278 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2279 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2280 }
2281 return;
2282 }
2283
2284 // Lacking those, try to detect the correct set of system includes for the
2285 // target triple.
2286
Chandler Carrutha4630892011-11-06 08:21:07 +00002287 // Implement generic Debian multiarch support.
2288 const StringRef X86_64MultiarchIncludeDirs[] = {
2289 "/usr/include/x86_64-linux-gnu",
2290
2291 // FIXME: These are older forms of multiarch. It's not clear that they're
2292 // in use in any released version of Debian, so we should consider
2293 // removing them.
2294 "/usr/include/i686-linux-gnu/64",
2295 "/usr/include/i486-linux-gnu/64"
2296 };
2297 const StringRef X86MultiarchIncludeDirs[] = {
2298 "/usr/include/i386-linux-gnu",
2299
2300 // FIXME: These are older forms of multiarch. It's not clear that they're
2301 // in use in any released version of Debian, so we should consider
2302 // removing them.
2303 "/usr/include/x86_64-linux-gnu/32",
2304 "/usr/include/i686-linux-gnu",
2305 "/usr/include/i486-linux-gnu"
2306 };
2307 const StringRef ARMMultiarchIncludeDirs[] = {
2308 "/usr/include/arm-linux-gnueabi"
2309 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002310 const StringRef ARMHFMultiarchIncludeDirs[] = {
2311 "/usr/include/arm-linux-gnueabihf"
2312 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002313 const StringRef MIPSMultiarchIncludeDirs[] = {
2314 "/usr/include/mips-linux-gnu"
2315 };
2316 const StringRef MIPSELMultiarchIncludeDirs[] = {
2317 "/usr/include/mipsel-linux-gnu"
2318 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002319 const StringRef PPCMultiarchIncludeDirs[] = {
2320 "/usr/include/powerpc-linux-gnu"
2321 };
2322 const StringRef PPC64MultiarchIncludeDirs[] = {
2323 "/usr/include/powerpc64-linux-gnu"
2324 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002325 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002326 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002327 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002328 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002329 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002330 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002331 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2332 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2333 else
2334 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002335 } else if (getTriple().getArch() == llvm::Triple::mips) {
2336 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2337 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2338 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002339 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2340 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2341 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2342 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002343 }
2344 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2345 E = MultiarchIncludeDirs.end();
2346 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002347 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002348 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2349 break;
2350 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002351 }
2352
2353 if (getTriple().getOS() == llvm::Triple::RTEMS)
2354 return;
2355
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002356 // Add an include of '/include' directly. This isn't provided by default by
2357 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2358 // add even when Clang is acting as-if it were a system compiler.
2359 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2360
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002361 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2362}
2363
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002364/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2365/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2366 const ArgList &DriverArgs,
2367 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002368 if (!llvm::sys::fs::exists(Base))
2369 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002370 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002371 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002372 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002373 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002374}
2375
2376void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2377 ArgStringList &CC1Args) const {
2378 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2379 DriverArgs.hasArg(options::OPT_nostdincxx))
2380 return;
2381
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002382 // Check if libc++ has been enabled and provide its include paths if so.
2383 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2384 // libc++ is always installed at a fixed path on Linux currently.
2385 addSystemInclude(DriverArgs, CC1Args,
2386 getDriver().SysRoot + "/usr/include/c++/v1");
2387 return;
2388 }
2389
Chandler Carruthfc52f752012-01-25 08:04:13 +00002390 // We need a detected GCC installation on Linux to provide libstdc++'s
2391 // headers. We handled the libc++ case above.
2392 if (!GCCInstallation.isValid())
2393 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002394
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002395 // By default, look for the C++ headers in an include directory adjacent to
2396 // the lib directory of the GCC installation. Note that this is expect to be
2397 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2398 StringRef LibDir = GCCInstallation.getParentLibPath();
2399 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002400 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002401 StringRef TripleStr = GCCInstallation.getTriple().str();
2402
2403 const std::string IncludePathCandidates[] = {
2404 LibDir.str() + "/../include/c++/" + Version.str(),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002405 // Gentoo is weird and places its headers inside the GCC install, so if the
2406 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002407 InstallDir.str() + "/include/g++-v4",
2408 // Android standalone toolchain has C++ headers in yet another place.
2409 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002410 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2411 // without a subdirectory corresponding to the gcc version.
2412 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002413 };
2414
2415 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2416 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2417 GCCInstallation.getMultiarchSuffix()),
2418 DriverArgs, CC1Args))
2419 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002420 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002421}
2422
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002423/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2424
Rafael Espindola0e659592012-02-19 01:38:32 +00002425DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2426 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002427
2428 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002429 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002430 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002431 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002432
Daniel Dunbaree788e72009-12-21 18:54:17 +00002433 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002434 getFilePaths().push_back("/usr/lib");
2435 getFilePaths().push_back("/usr/lib/gcc41");
2436}
2437
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002438Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2439 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00002440 Action::ActionClass Key = JA.getKind();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002441
2442 Tool *&T = Tools[Key];
2443 if (!T) {
2444 switch (Key) {
2445 case Action::AssembleJobClass:
2446 T = new tools::dragonfly::Assemble(*this); break;
2447 case Action::LinkJobClass:
2448 T = new tools::dragonfly::Link(*this); break;
2449 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002450 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002451 }
2452 }
2453
2454 return *T;
2455}