blob: a34751260d447aff9d169145c4a53dea2e08cf4b [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 {
Bob Wilson377e5c12012-11-09 01:59:30 +000085 if (isTargetIPhoneOS())
John McCall260611a2012-06-20 06:18:46 +000086 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
Bob Wilson377e5c12012-11-09 01:59:30 +000087 if (isNonFragile)
88 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
89 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
John McCall9f084a32011-07-06 00:26:06 +000090}
91
John McCall13db5cf2011-09-09 20:41:01 +000092/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
93bool Darwin::hasBlocksRuntime() const {
94 if (isTargetIPhoneOS())
95 return !isIPhoneOSVersionLT(3, 2);
96 else
97 return !isMacosxVersionLT(10, 6);
98}
99
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000101 return llvm::StringSwitch<const char*>(Value)
102 .Case("armv6k", "armv6")
103 .Case("armv5tej", "armv5")
104 .Case("xscale", "xscale")
105 .Case("armv4t", "armv4t")
106 .Case("armv7", "armv7")
107 .Cases("armv7a", "armv7-a", "armv7")
108 .Cases("armv7r", "armv7-r", "armv7")
109 .Cases("armv7m", "armv7-m", "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000110 .Cases("armv7f", "armv7-f", "armv7f")
111 .Cases("armv7k", "armv7-k", "armv7k")
112 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000113 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000114}
115
Chris Lattner5f9e2722011-07-23 10:55:15 +0000116static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000117 return llvm::StringSwitch<const char *>(Value)
118 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
119 .Cases("arm10e", "arm10tdmi", "armv5")
120 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
121 .Case("xscale", "xscale")
122 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s",
123 "arm1176jzf-s", "cortex-m0", "armv6")
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000124 .Cases("cortex-a8", "cortex-r4", "cortex-m3", "cortex-a9", "cortex-a15",
125 "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000126 .Case("cortex-a9-mp", "armv7f")
127 .Case("swift", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000128 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000129}
130
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000132 switch (getTriple().getArch()) {
133 default:
134 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000135
Douglas Gregorf0594d82011-03-06 19:11:49 +0000136 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000137 case llvm::Triple::arm: {
138 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000139 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000140 return Arch;
141
142 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000143 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000144 return Arch;
145
146 return "arm";
147 }
148 }
149}
150
Daniel Dunbarf3955282009-09-04 18:34:51 +0000151Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000152 // Free tool implementations.
153 for (llvm::DenseMap<unsigned, Tool*>::iterator
154 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
155 delete it->second;
156}
157
Chad Rosier61ab80a2011-09-20 20:44:06 +0000158std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
159 types::ID InputType) const {
160 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000161
162 // If the target isn't initialized (e.g., an unknown Darwin platform, return
163 // the default triple).
164 if (!isTargetInitialized())
165 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000166
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000167 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000168 Str += isTargetIPhoneOS() ? "ios" : "macosx";
169 Str += getTargetVersion().getAsString();
170 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000171
172 return Triple.getTriple();
173}
174
David Blaikie99ba9e32011-12-20 02:48:34 +0000175void Generic_ELF::anchor() {}
176
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000177Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
178 const ActionList &Inputs) const {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000179 Action::ActionClass Key = JA.getKind();
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000180 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
181 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000182 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000183
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000184 Tool *&T = Tools[Key];
185 if (!T) {
186 switch (Key) {
187 case Action::InputClass:
188 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000189 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000190 case Action::PreprocessJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000191 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000192 case Action::MigrateJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000193 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000194 case Action::CompileJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +0000195 T = new tools::Clang(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000196 case Action::AssembleJobClass: {
197 if (UseIntegratedAs)
198 T = new tools::ClangAs(*this);
199 else
200 T = new tools::darwin::Assemble(*this);
201 break;
202 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000203 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000204 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000205 case Action::LipoJobClass:
206 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000207 case Action::DsymutilJobClass:
208 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000209 case Action::VerifyJobClass:
210 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000211 }
212 }
213
214 return *T;
215}
216
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000217
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000218DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
219 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000220{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000221 getProgramPaths().push_back(getDriver().getInstalledDir());
222 if (getDriver().getInstalledDir() != getDriver().Dir)
223 getProgramPaths().push_back(getDriver().Dir);
224
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000225 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000226 getProgramPaths().push_back(getDriver().getInstalledDir());
227 if (getDriver().getInstalledDir() != getDriver().Dir)
228 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000229}
230
John McCallf85e1932011-06-15 23:02:42 +0000231void DarwinClang::AddLinkARCArgs(const ArgList &Args,
232 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000233
234 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000235 llvm::sys::Path P(getDriver().ClangExecutable);
236 P.eraseComponent(); // 'clang'
237 P.eraseComponent(); // 'bin'
238 P.appendComponent("lib");
239 P.appendComponent("arc");
240 P.appendComponent("libarclite_");
241 std::string s = P.str();
242 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000243 if (isTargetIOSSimulator())
244 s += "iphonesimulator";
245 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000246 s += "iphoneos";
John McCallf85e1932011-06-15 23:02:42 +0000247 else
248 s += "macosx";
249 s += ".a";
250
251 CmdArgs.push_back(Args.MakeArgString(s));
252}
253
Eric Christopher3404fe72011-06-22 17:41:40 +0000254void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000255 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000256 const char *DarwinStaticLib) const {
257 llvm::sys::Path P(getDriver().ResourceDir);
258 P.appendComponent("lib");
259 P.appendComponent("darwin");
260 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000261
Eric Christopher3404fe72011-06-22 17:41:40 +0000262 // For now, allow missing resource libraries to support developers who may
263 // not have compiler-rt checked out or integrated into their build.
264 bool Exists;
265 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
266 CmdArgs.push_back(Args.MakeArgString(P.str()));
267}
268
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000269void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
270 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000271 // Darwin only supports the compiler-rt based runtime libraries.
272 switch (GetRuntimeLibType(Args)) {
273 case ToolChain::RLT_CompilerRT:
274 break;
275 default:
276 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000277 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000278 return;
279 }
280
Daniel Dunbareec99102010-01-22 03:38:14 +0000281 // Darwin doesn't support real static executables, don't link any runtime
282 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000283 if (Args.hasArg(options::OPT_static) ||
284 Args.hasArg(options::OPT_fapple_kext) ||
285 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000286 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000287
288 // Reject -static-libgcc for now, we can deal with this when and if someone
289 // cares. This is useful in situations where someone wants to statically link
290 // something like libstdc++, and needs its runtime support routines.
291 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000292 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000293 << A->getAsString(Args);
294 return;
295 }
296
Daniel Dunbarf4714872011-11-17 00:36:57 +0000297 // If we are building profile support, link that library in.
298 if (Args.hasArg(options::OPT_fprofile_arcs) ||
299 Args.hasArg(options::OPT_fprofile_generate) ||
300 Args.hasArg(options::OPT_fcreate_profile) ||
301 Args.hasArg(options::OPT_coverage)) {
302 // Select the appropriate runtime library for the target.
303 if (isTargetIPhoneOS()) {
304 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
305 } else {
306 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
307 }
308 }
309
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000310 SanitizerArgs Sanitize(getDriver(), Args);
311
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000312 // Add ASAN runtime library, if required. Dynamic libraries and bundles
313 // should not be linked with the runtime library.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000314 if (Sanitize.needsAsanRt()) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000315 if (Args.hasArg(options::OPT_dynamiclib) ||
316 Args.hasArg(options::OPT_bundle)) return;
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000317 if (isTargetIPhoneOS()) {
318 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000319 << "-fsanitize=address";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000320 } else {
321 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
322
323 // The ASAN runtime library requires C++ and CoreFoundation.
324 AddCXXStdlibLibArgs(Args, CmdArgs);
325 CmdArgs.push_back("-framework");
326 CmdArgs.push_back("CoreFoundation");
327 }
328 }
329
Daniel Dunbareec99102010-01-22 03:38:14 +0000330 // Otherwise link libSystem, then the dynamic runtime library, and finally any
331 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000332 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000333
334 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000335 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000336 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
337 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000338 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
339 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
340 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000341
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000342 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000343 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000344 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000345 // The dynamic runtime library was merged with libSystem for 10.6 and
346 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000347 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000348 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000349 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000350 CmdArgs.push_back("-lgcc_s.10.5");
351
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000352 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000353 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000354 // omitted from 10.4.dylib.
355 //
356 // Unfortunately, that turned out to not be true, because Darwin system
357 // headers can still use eprintf on i386, and it is not exported from
358 // libSystem. Therefore, we still must provide a runtime library just for
359 // the tiny tiny handful of projects that *might* use that symbol.
360 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000361 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000362 } else {
363 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000364 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
365 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000366 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000367 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000368}
369
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000370void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000371 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000372
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000373 // Support allowing the SDKROOT environment variable used by xcrun and other
374 // Xcode tools to define the default sysroot, by making it the default for
375 // isysroot.
376 if (!Args.hasArg(options::OPT_isysroot)) {
377 if (char *env = ::getenv("SDKROOT")) {
378 // We only use this value as the default if it is an absolute path and
379 // exists.
380 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env)) {
381 Args.append(Args.MakeSeparateArg(
382 0, Opts.getOption(options::OPT_isysroot), env));
383 }
384 }
385 }
386
Daniel Dunbar26031372010-01-27 00:56:25 +0000387 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000388 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
389 Arg *iOSSimVersion = Args.getLastArg(
390 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000391
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000392 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000393 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000394 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000395 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
396 iOSVersion = iOSSimVersion = 0;
397 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000398 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000399 << iOSVersion->getAsString(Args)
400 << iOSSimVersion->getAsString(Args);
401 iOSSimVersion = 0;
402 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000403 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000404 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000405 StringRef OSXTarget;
406 StringRef iOSTarget;
407 StringRef iOSSimTarget;
408 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
409 OSXTarget = env;
410 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
411 iOSTarget = env;
412 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
413 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000414
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000415 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000416 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000417 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000418 if (iOSTarget.empty()) {
419 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
420 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000421 StringRef isysroot = A->getValue();
Chad Rosiera4884972011-08-31 20:56:25 +0000422 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
423 if (second != "")
424 iOSTarget = second.substr(0,3);
425 }
426 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000427
Chad Rosier4f8de272011-09-28 00:46:32 +0000428 // If no OSX or iOS target has been specified and we're compiling for armv7,
429 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000430 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000431 (getDarwinArchName(Args) == "armv7" ||
432 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000433 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000434
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000435 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000436 //
437 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000438
439 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000440 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000441 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000442 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000443 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000444 "IPHONEOS_DEPLOYMENT_TARGET");
445 }
446
447 // Allow conflicts among OSX and iOS for historical reasons, but choose the
448 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000449 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000450 if (getTriple().getArch() == llvm::Triple::arm ||
451 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000452 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000453 else
Chad Rosiera4884972011-08-31 20:56:25 +0000454 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000455 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000456
Chad Rosiera4884972011-08-31 20:56:25 +0000457 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000458 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000459 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
460 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000461 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000462 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000463 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
464 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000465 } else if (!iOSSimTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000466 const Option O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000467 options::OPT_mios_simulator_version_min_EQ);
468 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
469 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000470 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000471 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000472 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000473 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
474 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000475 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000478 // Reject invalid architecture combinations.
479 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
480 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000481 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000482 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
483 }
484
Daniel Dunbar26031372010-01-27 00:56:25 +0000485 // Set the tool chain target information.
486 unsigned Major, Minor, Micro;
487 bool HadExtra;
488 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000489 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000490 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000491 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000492 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000493 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000494 << OSXVersion->getAsString(Args);
495 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000496 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
497 assert(Version && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000498 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000499 Micro, HadExtra) || HadExtra ||
500 Major >= 10 || Minor >= 100 || Micro >= 100)
501 getDriver().Diag(diag::err_drv_invalid_version_number)
502 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000503 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000504
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000505 bool IsIOSSim = bool(iOSSimVersion);
506
507 // In GCC, the simulator historically was treated as being OS X in some
508 // contexts, like determining the link logic, despite generally being called
509 // with an iOS deployment target. For compatibility, we detect the
510 // simulator as iOS + x86, and treat it differently in a few contexts.
511 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
512 getTriple().getArch() == llvm::Triple::x86_64))
513 IsIOSSim = true;
514
515 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000516}
517
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000518void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000519 ArgStringList &CmdArgs) const {
520 CXXStdlibType Type = GetCXXStdlibType(Args);
521
522 switch (Type) {
523 case ToolChain::CST_Libcxx:
524 CmdArgs.push_back("-lc++");
525 break;
526
527 case ToolChain::CST_Libstdcxx: {
528 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
529 // it was previously found in the gcc lib dir. However, for all the Darwin
530 // platforms we care about it was -lstdc++.6, so we search for that
531 // explicitly if we can't see an obvious -lstdc++ candidate.
532
533 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000534 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000535 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000536 llvm::sys::Path P(A->getValue());
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000537 P.appendComponent("usr");
538 P.appendComponent("lib");
539 P.appendComponent("libstdc++.dylib");
540
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000541 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000542 P.eraseComponent();
543 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000544 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000545 CmdArgs.push_back(Args.MakeArgString(P.str()));
546 return;
547 }
548 }
549 }
550
551 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000552 // FIXME: This should be removed someday when we don't have to care about
553 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000554 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
555 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000556 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
557 return;
558 }
559
560 // Otherwise, let the linker search.
561 CmdArgs.push_back("-lstdc++");
562 break;
563 }
564 }
565}
566
Shantonu Sen7433fed2010-09-17 18:39:08 +0000567void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
568 ArgStringList &CmdArgs) const {
569
570 // For Darwin platforms, use the compiler-rt-based support library
571 // instead of the gcc-provided one (which is also incidentally
572 // only present in the gcc lib dir, which makes it hard to find).
573
574 llvm::sys::Path P(getDriver().ResourceDir);
575 P.appendComponent("lib");
576 P.appendComponent("darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000577
578 // Use the newer cc_kext for iOS ARM after 6.0.
579 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
580 !isIPhoneOSVersionLT(6, 0)) {
581 P.appendComponent("libclang_rt.cc_kext.a");
582 } else {
583 P.appendComponent("libclang_rt.cc_kext_ios5.a");
584 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000585
Shantonu Sen7433fed2010-09-17 18:39:08 +0000586 // For now, allow missing resource libraries to support developers who may
587 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000588 bool Exists;
589 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000590 CmdArgs.push_back(Args.MakeArgString(P.str()));
591}
592
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000593DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
594 const char *BoundArch) const {
595 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
596 const OptTable &Opts = getDriver().getOpts();
597
598 // FIXME: We really want to get out of the tool chain level argument
599 // translation business, as it makes the driver functionality much
600 // more opaque. For now, we follow gcc closely solely for the
601 // purpose of easily achieving feature parity & testability. Once we
602 // have something that works, we should reevaluate each translation
603 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000604
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000605 for (ArgList::const_iterator it = Args.begin(),
606 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000607 Arg *A = *it;
608
609 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000610 // Skip this argument unless the architecture matches either the toolchain
611 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000612 llvm::Triple::ArchType XarchArch =
Richard Smith1d489cf2012-11-01 04:30:05 +0000613 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000614 if (!(XarchArch == getArch() ||
615 (BoundArch && XarchArch ==
Rafael Espindolacfed8282012-10-31 18:51:07 +0000616 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000617 continue;
618
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000619 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000620 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000621 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000622 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000624 // If the argument parsing failed or more than one argument was
625 // consumed, the -Xarch_ argument's parameter tried to consume
626 // extra arguments. Emit an error and ignore.
627 //
628 // We also want to disallow any options which would alter the
629 // driver behavior; that isn't going to work in our model. We
630 // use isDriverOption() as an approximation, although things
631 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000632 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000633 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000634 << A->getAsString(Args);
635 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000636 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000637 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000638 << A->getAsString(Args);
639 continue;
640 }
641
Daniel Dunbar478edc22009-03-29 22:29:05 +0000642 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000643 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000644
645 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000646
647 // Linker input arguments require custom handling. The problem is that we
648 // have already constructed the phase actions, so we can not treat them as
649 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000650 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000651 // Convert the argument into individual Zlinker_input_args.
652 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
653 DAL->AddSeparateArg(OriginalArg,
654 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000655 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000656
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000657 }
658 continue;
659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000661
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000662 // Sob. These is strictly gcc compatible for the time being. Apple
663 // gcc translates options twice, which means that self-expanding
664 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000665 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000666 default:
667 DAL->append(A);
668 break;
669
670 case options::OPT_mkernel:
671 case options::OPT_fapple_kext:
672 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000673 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000674 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000676 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000677 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000678 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000679 break;
680
681 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000682 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
683 DAL->AddFlagArg(A,
684 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000685 break;
686
687 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000688 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
689 DAL->AddFlagArg(A,
690 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000691 break;
692
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000693 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000694 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000695 break;
696
697 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000698 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000699 break;
700
701 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000702 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000703 break;
704
705 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000706 DAL->AddFlagArg(A,
707 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000708 break;
709
710 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000711 DAL->AddFlagArg(A,
712 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000713 break;
714
715 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000716 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000717 break;
718
719 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000720 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000721 break;
722 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000723 }
724
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000725 if (getTriple().getArch() == llvm::Triple::x86 ||
726 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000727 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000728 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000729
730 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000731 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000732 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000733 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000734 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
735 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000736
737 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
738 // which defines the list of which architectures we accept.
739 if (Name == "ppc")
740 ;
741 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000742 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000743 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000744 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000745 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000746 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000747 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000748 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000749 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000750 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000751 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000752 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000753 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000754 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000755 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000756 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000757
758 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000759 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000760
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000761 else if (Name == "i386")
762 ;
763 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000764 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000765 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000766 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000767 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000768 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000769 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000770 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000771 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000772 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000773 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000774 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000775 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000776 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000777
778 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000779 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000780
781 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000782 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000783 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000784 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000785 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000786 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000787 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000788 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000789 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000790 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000791 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000792 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson336bfa32012-09-29 23:52:50 +0000793 else if (Name == "armv7f")
794 DAL->AddJoinedArg(0, MArch, "armv7f");
795 else if (Name == "armv7k")
796 DAL->AddJoinedArg(0, MArch, "armv7k");
797 else if (Name == "armv7s")
798 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000799
800 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000801 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000802 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000803
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000804 // Add an explicit version min argument for the deployment target. We do this
805 // after argument translation because -Xarch_ arguments may add a version min
806 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000807 if (BoundArch)
808 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000809
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000810 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
811 // FIXME: It would be far better to avoid inserting those -static arguments,
812 // but we can't check the deployment target in the translation code until
813 // it is set here.
814 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
815 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
816 Arg *A = *it;
817 ++it;
818 if (A->getOption().getID() != options::OPT_mkernel &&
819 A->getOption().getID() != options::OPT_fapple_kext)
820 continue;
821 assert(it != ie && "unexpected argument translation");
822 A = *it;
823 assert(A->getOption().getID() == options::OPT_static &&
824 "missing expected -static argument");
825 it = DAL->getArgs().erase(it);
826 }
827 }
828
Bob Wilson163b1512011-10-07 17:54:41 +0000829 // Validate the C++ standard library choice.
830 CXXStdlibType Type = GetCXXStdlibType(*DAL);
831 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000832 // Check whether the target provides libc++.
833 StringRef where;
834
835 // Complain about targetting iOS < 5.0 in any way.
Bob Wilson377e5c12012-11-09 01:59:30 +0000836 if (isTargetIPhoneOS() && isIPhoneOSVersionLT(5, 0))
837 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000838
839 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000840 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000841 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000842 }
843 }
844
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000845 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000846}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000847
Daniel Dunbarf3955282009-09-04 18:34:51 +0000848bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000849 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000850}
851
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000852bool Darwin::UseDwarfDebugFlags() const {
853 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
854 return S[0] != '\0';
855 return false;
856}
857
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000858bool Darwin::UseSjLjExceptions() const {
859 // Darwin uses SjLj exceptions on ARM.
860 return (getTriple().getArch() == llvm::Triple::arm ||
861 getTriple().getArch() == llvm::Triple::thumb);
862}
863
Daniel Dunbarf3955282009-09-04 18:34:51 +0000864const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000865 return "pic";
866}
867
Daniel Dunbarf3955282009-09-04 18:34:51 +0000868const char *Darwin::GetForcedPicModel() const {
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000869 if (getArch() == llvm::Triple::x86_64)
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000870 return "pic";
871 return 0;
872}
873
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000874bool Darwin::SupportsProfiling() const {
875 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000876 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000877}
878
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000879bool Darwin::SupportsObjCGC() const {
880 // Garbage collection is supported everywhere except on iPhone OS.
881 return !isTargetIPhoneOS();
882}
883
John McCall0a7dd782012-08-21 02:47:43 +0000884void Darwin::CheckObjCARC() const {
885 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
886 return;
John McCall80fd37a2012-08-27 01:56:21 +0000887 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000888}
889
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000890std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000891Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
892 types::ID InputType) const {
893 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000894}
895
Daniel Dunbar39176082009-03-20 00:20:03 +0000896/// Generic_GCC - A tool chain using the 'gcc' command to perform
897/// all subcommands; this relies on gcc translating the majority of
898/// command line options.
899
Chandler Carruth19347ed2011-11-06 23:39:34 +0000900/// \brief Parse a GCCVersion object out of a string of text.
901///
902/// This is the primary means of forming GCCVersion objects.
903/*static*/
904Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
905 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
906 std::pair<StringRef, StringRef> First = VersionText.split('.');
907 std::pair<StringRef, StringRef> Second = First.second.split('.');
908
909 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
910 if (First.first.getAsInteger(10, GoodVersion.Major) ||
911 GoodVersion.Major < 0)
912 return BadVersion;
913 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
914 GoodVersion.Minor < 0)
915 return BadVersion;
916
917 // First look for a number prefix and parse that if present. Otherwise just
918 // stash the entire patch string in the suffix, and leave the number
919 // unspecified. This covers versions strings such as:
920 // 4.4
921 // 4.4.0
922 // 4.4.x
923 // 4.4.2-rc4
924 // 4.4.x-patched
925 // And retains any patch number it finds.
926 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
927 if (!PatchText.empty()) {
928 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
929 // Try to parse the number and any suffix.
930 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
931 GoodVersion.Patch < 0)
932 return BadVersion;
933 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
934 }
935 }
936
937 return GoodVersion;
938}
939
940/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
941bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
942 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
943 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
944
945 // Note that we rank versions with *no* patch specified is better than ones
946 // hard-coding a patch version. Thus if the RHS has no patch, it always
947 // wins, and the LHS only wins if it has no patch and the RHS does have
948 // a patch.
949 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
950 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
Gabor Greif241cbe42012-04-18 10:59:08 +0000951 if (PatchSuffix == RHS.PatchSuffix) return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000952
953 // Finally, between completely tied version numbers, the version with the
954 // suffix loses as we prefer full releases.
955 if (RHS.PatchSuffix.empty()) return true;
956 return false;
957}
958
Rafael Espindola0e659592012-02-19 01:38:32 +0000959static StringRef getGCCToolchainDir(const ArgList &Args) {
960 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
961 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +0000962 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +0000963 return GCC_INSTALL_PREFIX;
964}
965
Chandler Carruth19347ed2011-11-06 23:39:34 +0000966/// \brief Construct a GCCInstallationDetector from the driver.
967///
968/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +0000969/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +0000970///
971/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
972/// should instead pull the target out of the driver. This is currently
973/// necessary because the driver doesn't store the final version of the target
974/// triple.
975Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
976 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +0000977 const llvm::Triple &TargetTriple,
978 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +0000979 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +0000980 llvm::Triple MultiarchTriple
981 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
982 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +0000983 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +0000984 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +0000985 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +0000986 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +0000987 SmallVector<StringRef, 10> CandidateTripleAliases;
988 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
989 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
990 CandidateTripleAliases,
991 CandidateMultiarchLibDirs,
992 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +0000993
994 // Compute the set of prefixes for our search.
995 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
996 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +0000997
Rafael Espindola0e659592012-02-19 01:38:32 +0000998 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
999 if (GCCToolchainDir != "") {
1000 if (GCCToolchainDir.back() == '/')
1001 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001002
Rafael Espindola0e659592012-02-19 01:38:32 +00001003 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001004 } else {
1005 Prefixes.push_back(D.SysRoot);
1006 Prefixes.push_back(D.SysRoot + "/usr");
1007 Prefixes.push_back(D.InstalledDir + "/..");
1008 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001009
1010 // Loop over the various components which exist and select the best GCC
1011 // installation available. GCC installs are ranked by version number.
1012 Version = GCCVersion::Parse("0.0.0");
1013 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1014 if (!llvm::sys::fs::exists(Prefixes[i]))
1015 continue;
1016 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1017 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1018 if (!llvm::sys::fs::exists(LibDir))
1019 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001020 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001021 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1022 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001023 }
1024 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1025 const std::string LibDir
1026 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1027 if (!llvm::sys::fs::exists(LibDir))
1028 continue;
1029 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1030 ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001031 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001032 CandidateMultiarchTripleAliases[k],
1033 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001034 }
1035 }
1036}
1037
1038/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001039 const llvm::Triple &TargetTriple,
1040 const llvm::Triple &MultiarchTriple,
1041 SmallVectorImpl<StringRef> &LibDirs,
1042 SmallVectorImpl<StringRef> &TripleAliases,
1043 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1044 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1045 // Declare a bunch of static data sets that we'll select between below. These
1046 // are specifically designed to always refer to string literals to avoid any
1047 // lifetime or initialization issues.
1048 static const char *const ARMLibDirs[] = { "/lib" };
1049 static const char *const ARMTriples[] = {
1050 "arm-linux-gnueabi",
1051 "arm-linux-androideabi"
1052 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001053 static const char *const ARMHFTriples[] = {
1054 "arm-linux-gnueabihf",
1055 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001056
1057 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1058 static const char *const X86_64Triples[] = {
1059 "x86_64-linux-gnu",
1060 "x86_64-unknown-linux-gnu",
1061 "x86_64-pc-linux-gnu",
1062 "x86_64-redhat-linux6E",
1063 "x86_64-redhat-linux",
1064 "x86_64-suse-linux",
1065 "x86_64-manbo-linux-gnu",
1066 "x86_64-linux-gnu",
1067 "x86_64-slackware-linux"
1068 };
1069 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1070 static const char *const X86Triples[] = {
1071 "i686-linux-gnu",
1072 "i686-pc-linux-gnu",
1073 "i486-linux-gnu",
1074 "i386-linux-gnu",
1075 "i686-redhat-linux",
1076 "i586-redhat-linux",
1077 "i386-redhat-linux",
1078 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001079 "i486-slackware-linux",
1080 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001081 };
1082
1083 static const char *const MIPSLibDirs[] = { "/lib" };
1084 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1085 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001086 static const char *const MIPSELTriples[] = {
1087 "mipsel-linux-gnu",
1088 "mipsel-linux-android"
1089 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001090
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001091 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1092 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1093 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1094 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1095
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001096 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1097 static const char *const PPCTriples[] = {
1098 "powerpc-linux-gnu",
1099 "powerpc-unknown-linux-gnu",
Gabor Greif91720912012-05-15 11:21:03 +00001100 "powerpc-suse-linux",
1101 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001102 };
1103 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1104 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001105 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001106 "powerpc64-unknown-linux-gnu",
1107 "powerpc64-suse-linux",
1108 "ppc64-redhat-linux"
1109 };
1110
1111 switch (TargetTriple.getArch()) {
1112 case llvm::Triple::arm:
1113 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001114 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001115 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1116 TripleAliases.append(
1117 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1118 } else {
1119 TripleAliases.append(
1120 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1121 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001122 break;
1123 case llvm::Triple::x86_64:
1124 LibDirs.append(
1125 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1126 TripleAliases.append(
1127 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1128 MultiarchLibDirs.append(
1129 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1130 MultiarchTripleAliases.append(
1131 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1132 break;
1133 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001134 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001135 TripleAliases.append(
1136 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1137 MultiarchLibDirs.append(
1138 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1139 MultiarchTripleAliases.append(
1140 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1141 break;
1142 case llvm::Triple::mips:
1143 LibDirs.append(
1144 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1145 TripleAliases.append(
1146 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001147 MultiarchLibDirs.append(
1148 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1149 MultiarchTripleAliases.append(
1150 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001151 break;
1152 case llvm::Triple::mipsel:
1153 LibDirs.append(
1154 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1155 TripleAliases.append(
1156 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001157 MultiarchLibDirs.append(
1158 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1159 MultiarchTripleAliases.append(
1160 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1161 break;
1162 case llvm::Triple::mips64:
1163 LibDirs.append(
1164 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1165 TripleAliases.append(
1166 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1167 MultiarchLibDirs.append(
1168 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1169 MultiarchTripleAliases.append(
1170 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1171 break;
1172 case llvm::Triple::mips64el:
1173 LibDirs.append(
1174 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1175 TripleAliases.append(
1176 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1177 MultiarchLibDirs.append(
1178 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1179 MultiarchTripleAliases.append(
1180 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001181 break;
1182 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001183 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001184 TripleAliases.append(
1185 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1186 MultiarchLibDirs.append(
1187 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1188 MultiarchTripleAliases.append(
1189 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1190 break;
1191 case llvm::Triple::ppc64:
1192 LibDirs.append(
1193 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1194 TripleAliases.append(
1195 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1196 MultiarchLibDirs.append(
1197 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1198 MultiarchTripleAliases.append(
1199 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1200 break;
1201
1202 default:
1203 // By default, just rely on the standard lib directories and the original
1204 // triple.
1205 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001206 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001207
1208 // Always append the drivers target triple to the end, in case it doesn't
1209 // match any of our aliases.
1210 TripleAliases.push_back(TargetTriple.str());
1211
1212 // Also include the multiarch variant if it's different.
1213 if (TargetTriple.str() != MultiarchTriple.str())
1214 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001215}
1216
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001217// FIXME: There is the same routine in the Tools.cpp.
1218static bool hasMipsN32ABIArg(const ArgList &Args) {
1219 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00001220 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001221}
1222
1223static StringRef getTargetMultiarchSuffix(llvm::Triple::ArchType TargetArch,
1224 const ArgList &Args) {
1225 if (TargetArch == llvm::Triple::x86_64 ||
1226 TargetArch == llvm::Triple::ppc64)
1227 return "/64";
1228
1229 if (TargetArch == llvm::Triple::mips64 ||
1230 TargetArch == llvm::Triple::mips64el) {
1231 if (hasMipsN32ABIArg(Args))
1232 return "/n32";
1233 else
1234 return "/64";
1235 }
1236
1237 return "/32";
1238}
1239
Chandler Carruth19347ed2011-11-06 23:39:34 +00001240void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001241 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1242 const std::string &LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001243 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001244 // There are various different suffixes involving the triple we
1245 // check for. We also record what is necessary to walk from each back
1246 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001247 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001248 "/gcc/" + CandidateTriple.str(),
1249 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1250
Hal Finkel02014b42012-09-18 22:25:07 +00001251 // The Freescale PPC SDK has the gcc libraries in
1252 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1253 "/" + CandidateTriple.str(),
1254
Chandler Carruth19347ed2011-11-06 23:39:34 +00001255 // Ubuntu has a strange mis-matched pair of triples that this happens to
1256 // match.
1257 // FIXME: It may be worthwhile to generalize this and look for a second
1258 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001259 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001260 };
1261 const std::string InstallSuffixes[] = {
1262 "/../../..",
1263 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001264 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001265 "/../../../.."
1266 };
1267 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001268 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1269 (TargetArch != llvm::Triple::x86));
1270 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1271 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001272 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001273 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001274 !EC && LI != LE; LI = LI.increment(EC)) {
1275 StringRef VersionText = llvm::sys::path::filename(LI->path());
1276 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1277 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1278 if (CandidateVersion < MinVersion)
1279 continue;
1280 if (CandidateVersion <= Version)
1281 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001282
1283 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001284 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001285 // libs in a subdirectory named 64. The simple logic we follow is that
1286 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1287 // we use that. If not, and if not a multiarch triple, we look for
1288 // crtbegin.o without the subdirectory.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001289 StringRef MultiarchSuffix = getTargetMultiarchSuffix(TargetArch, Args);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001290 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1291 GCCMultiarchSuffix = MultiarchSuffix.str();
1292 } else {
1293 if (NeedsMultiarchSuffix ||
1294 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1295 continue;
1296 GCCMultiarchSuffix.clear();
1297 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001298
1299 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001300 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001301 // FIXME: We hack together the directory name here instead of
1302 // using LI to ensure stable path separators across Windows and
1303 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001304 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001305 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001306 IsValid = true;
1307 }
1308 }
1309}
1310
Rafael Espindola0e659592012-02-19 01:38:32 +00001311Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1312 const ArgList &Args)
1313 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001314 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001315 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001316 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001317}
1318
Daniel Dunbar39176082009-03-20 00:20:03 +00001319Generic_GCC::~Generic_GCC() {
1320 // Free tool implementations.
1321 for (llvm::DenseMap<unsigned, Tool*>::iterator
1322 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1323 delete it->second;
1324}
1325
Mike Stump1eb44332009-09-09 15:08:12 +00001326Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001327 const JobAction &JA,
1328 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001329 Action::ActionClass Key = JA.getKind();
Daniel Dunbar39176082009-03-20 00:20:03 +00001330
1331 Tool *&T = Tools[Key];
1332 if (!T) {
1333 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001334 case Action::InputClass:
1335 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001336 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001337 case Action::PreprocessJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001338 case Action::PrecompileJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001339 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001340 case Action::MigrateJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001341 case Action::CompileJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001342 T = new tools::Clang(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001343 case Action::AssembleJobClass:
1344 T = new tools::gcc::Assemble(*this); break;
1345 case Action::LinkJobClass:
1346 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001348 // This is a bit ungeneric, but the only platform using a driver
1349 // driver is Darwin.
1350 case Action::LipoJobClass:
1351 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001352 case Action::DsymutilJobClass:
1353 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001354 case Action::VerifyJobClass:
1355 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001356 }
1357 }
1358
1359 return *T;
1360}
1361
Daniel Dunbar39176082009-03-20 00:20:03 +00001362bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00001363 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00001364}
1365
1366const char *Generic_GCC::GetDefaultRelocationModel() const {
1367 return "static";
1368}
1369
1370const char *Generic_GCC::GetForcedPicModel() const {
1371 return 0;
1372}
Tony Linthicum96319392011-12-12 21:14:55 +00001373/// Hexagon Toolchain
1374
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001375Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple& Triple)
1376 : ToolChain(D, Triple) {
Tony Linthicum96319392011-12-12 21:14:55 +00001377 getProgramPaths().push_back(getDriver().getInstalledDir());
1378 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1379 getProgramPaths().push_back(getDriver().Dir);
1380}
1381
1382Hexagon_TC::~Hexagon_TC() {
1383 // Free tool implementations.
1384 for (llvm::DenseMap<unsigned, Tool*>::iterator
1385 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1386 delete it->second;
1387}
1388
1389Tool &Hexagon_TC::SelectTool(const Compilation &C,
1390 const JobAction &JA,
1391 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001392 Action::ActionClass Key = JA.getKind();
Tony Linthicum96319392011-12-12 21:14:55 +00001393 Tool *&T = Tools[Key];
1394 if (!T) {
1395 switch (Key) {
1396 case Action::InputClass:
1397 case Action::BindArchClass:
1398 assert(0 && "Invalid tool kind.");
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001399 case Action::PreprocessJobClass:
1400 case Action::PrecompileJobClass:
Tony Linthicum96319392011-12-12 21:14:55 +00001401 case Action::AnalyzeJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001402 case Action::MigrateJobClass:
1403 case Action::CompileJobClass:
Tony Linthicum96319392011-12-12 21:14:55 +00001404 T = new tools::Clang(*this); break;
1405 case Action::AssembleJobClass:
1406 T = new tools::hexagon::Assemble(*this); break;
1407 case Action::LinkJobClass:
1408 T = new tools::hexagon::Link(*this); break;
1409 default:
1410 assert(false && "Unsupported action for Hexagon target.");
1411 }
1412 }
1413
1414 return *T;
1415}
1416
Tony Linthicum96319392011-12-12 21:14:55 +00001417const char *Hexagon_TC::GetDefaultRelocationModel() const {
1418 return "static";
1419}
1420
1421const char *Hexagon_TC::GetForcedPicModel() const {
1422 return 0;
1423} // End Hexagon
1424
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001425
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001426/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1427/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1428/// Currently does not support anything else but compilation.
1429
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001430TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1431 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001432 // Path mangling to find libexec
1433 std::string Path(getDriver().Dir);
1434
1435 Path += "/../libexec";
1436 getProgramPaths().push_back(Path);
1437}
1438
1439TCEToolChain::~TCEToolChain() {
1440 for (llvm::DenseMap<unsigned, Tool*>::iterator
1441 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1442 delete it->second;
1443}
1444
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001445bool TCEToolChain::IsMathErrnoDefault() const {
1446 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001447}
1448
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001449const char *TCEToolChain::GetDefaultRelocationModel() const {
1450 return "static";
1451}
1452
1453const char *TCEToolChain::GetForcedPicModel() const {
1454 return 0;
1455}
1456
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001457Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001458 const JobAction &JA,
1459 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001460 Action::ActionClass Key;
1461 Key = Action::AnalyzeJobClass;
1462
1463 Tool *&T = Tools[Key];
1464 if (!T) {
1465 switch (Key) {
1466 case Action::PreprocessJobClass:
1467 T = new tools::gcc::Preprocess(*this); break;
1468 case Action::AnalyzeJobClass:
1469 T = new tools::Clang(*this); break;
1470 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001471 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001472 }
1473 }
1474 return *T;
1475}
1476
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001477/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1478
Rafael Espindola0e659592012-02-19 01:38:32 +00001479OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1480 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001481 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001482 getFilePaths().push_back("/usr/lib");
1483}
1484
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001485Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1486 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001487 Action::ActionClass Key = JA.getKind();
Rafael Espindoladda5b922010-11-07 23:13:01 +00001488 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1489 options::OPT_no_integrated_as,
1490 IsIntegratedAssemblerDefault());
1491
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001492 Tool *&T = Tools[Key];
1493 if (!T) {
1494 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001495 case Action::AssembleJobClass: {
1496 if (UseIntegratedAs)
1497 T = new tools::ClangAs(*this);
1498 else
1499 T = new tools::openbsd::Assemble(*this);
1500 break;
1501 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001502 case Action::LinkJobClass:
1503 T = new tools::openbsd::Link(*this); break;
1504 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001505 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001506 }
1507 }
1508
1509 return *T;
1510}
1511
Eli Friedman42f74f22012-08-08 23:57:20 +00001512/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1513
1514Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1515 : Generic_ELF(D, Triple, Args) {
1516 getFilePaths().push_back(getDriver().Dir + "/../lib");
1517 getFilePaths().push_back("/usr/lib");
1518}
1519
1520Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA,
1521 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001522 Action::ActionClass Key = JA.getKind();
Eli Friedman42f74f22012-08-08 23:57:20 +00001523 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1524 options::OPT_no_integrated_as,
1525 IsIntegratedAssemblerDefault());
1526
1527 Tool *&T = Tools[Key];
1528 if (!T) {
1529 switch (Key) {
1530 case Action::AssembleJobClass: {
1531 if (UseIntegratedAs)
1532 T = new tools::ClangAs(*this);
1533 else
1534 T = new tools::bitrig::Assemble(*this);
1535 break;
1536 }
1537 case Action::LinkJobClass:
1538 T = new tools::bitrig::Link(*this); break;
1539 default:
1540 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1541 }
1542 }
1543
1544 return *T;
1545}
1546
1547void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1548 ArgStringList &CC1Args) const {
1549 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1550 DriverArgs.hasArg(options::OPT_nostdincxx))
1551 return;
1552
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001553 switch (GetCXXStdlibType(DriverArgs)) {
1554 case ToolChain::CST_Libcxx:
1555 addSystemInclude(DriverArgs, CC1Args,
1556 getDriver().SysRoot + "/usr/include/c++/");
1557 break;
1558 case ToolChain::CST_Libstdcxx:
1559 addSystemInclude(DriverArgs, CC1Args,
1560 getDriver().SysRoot + "/usr/include/c++/stdc++");
1561 addSystemInclude(DriverArgs, CC1Args,
1562 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001563
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001564 StringRef Triple = getTriple().str();
1565 if (Triple.startswith("amd64"))
1566 addSystemInclude(DriverArgs, CC1Args,
1567 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1568 Triple.substr(5));
1569 else
1570 addSystemInclude(DriverArgs, CC1Args,
1571 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1572 Triple);
1573 break;
1574 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001575}
1576
1577void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1578 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001579 switch (GetCXXStdlibType(Args)) {
1580 case ToolChain::CST_Libcxx:
1581 CmdArgs.push_back("-lc++");
1582 CmdArgs.push_back("-lcxxrt");
1583 // Include supc++ to provide Unwind until provided by libcxx.
1584 CmdArgs.push_back("-lgcc");
1585 break;
1586 case ToolChain::CST_Libstdcxx:
1587 CmdArgs.push_back("-lstdc++");
1588 break;
1589 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001590}
1591
Daniel Dunbar75358d22009-03-30 21:06:03 +00001592/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1593
Rafael Espindola0e659592012-02-19 01:38:32 +00001594FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1595 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001596
Chandler Carruth24248e32012-01-26 01:35:15 +00001597 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1598 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001599 if ((Triple.getArch() == llvm::Triple::x86 ||
1600 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001601 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001602 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1603 else
1604 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001605}
1606
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001607Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1608 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001609 Action::ActionClass Key = JA.getKind();
Roman Divacky67dece72010-11-08 17:46:39 +00001610 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1611 options::OPT_no_integrated_as,
1612 IsIntegratedAssemblerDefault());
1613
Daniel Dunbar75358d22009-03-30 21:06:03 +00001614 Tool *&T = Tools[Key];
1615 if (!T) {
1616 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001617 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001618 if (UseIntegratedAs)
1619 T = new tools::ClangAs(*this);
1620 else
1621 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001622 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001623 case Action::LinkJobClass:
1624 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001625 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001626 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001627 }
1628 }
1629
1630 return *T;
1631}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001632
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001633/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1634
Rafael Espindola0e659592012-02-19 01:38:32 +00001635NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1636 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001637
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001638 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001639 // When targeting a 32-bit platform, try the special directory used on
1640 // 64-bit hosts, and only fall back to the main library directory if that
1641 // doesn't work.
1642 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1643 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001644 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001645 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001646
1647 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001648 }
1649}
1650
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001651Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1652 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001653 Action::ActionClass Key = JA.getKind();
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001654 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1655 options::OPT_no_integrated_as,
1656 IsIntegratedAssemblerDefault());
1657
1658 Tool *&T = Tools[Key];
1659 if (!T) {
1660 switch (Key) {
1661 case Action::AssembleJobClass:
1662 if (UseIntegratedAs)
1663 T = new tools::ClangAs(*this);
1664 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001665 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001666 break;
1667 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001668 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001669 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001670 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001671 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001672 }
1673 }
1674
1675 return *T;
1676}
1677
Chris Lattner38e317d2010-07-07 16:01:42 +00001678/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1679
Rafael Espindola0e659592012-02-19 01:38:32 +00001680Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1681 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001682 getFilePaths().push_back(getDriver().Dir + "/../lib");
1683 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001684}
1685
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001686Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1687 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001688 Action::ActionClass Key = JA.getKind();
Chris Lattner38e317d2010-07-07 16:01:42 +00001689
1690 Tool *&T = Tools[Key];
1691 if (!T) {
1692 switch (Key) {
1693 case Action::AssembleJobClass:
1694 T = new tools::minix::Assemble(*this); break;
1695 case Action::LinkJobClass:
1696 T = new tools::minix::Link(*this); break;
1697 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001698 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001699 }
1700 }
1701
1702 return *T;
1703}
1704
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001705/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1706
Rafael Espindola0e659592012-02-19 01:38:32 +00001707AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1708 const ArgList &Args)
1709 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001710
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001711 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001712 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001713 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001714
Daniel Dunbaree788e72009-12-21 18:54:17 +00001715 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001716 getFilePaths().push_back("/usr/lib");
1717 getFilePaths().push_back("/usr/sfw/lib");
1718 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001719 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001720
1721}
1722
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001723Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1724 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001725 Action::ActionClass Key = JA.getKind();
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001726
1727 Tool *&T = Tools[Key];
1728 if (!T) {
1729 switch (Key) {
1730 case Action::AssembleJobClass:
1731 T = new tools::auroraux::Assemble(*this); break;
1732 case Action::LinkJobClass:
1733 T = new tools::auroraux::Link(*this); break;
1734 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001735 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001736 }
1737 }
1738
1739 return *T;
1740}
1741
David Chisnall31c46902012-02-15 13:39:01 +00001742/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1743
Rafael Espindola0e659592012-02-19 01:38:32 +00001744Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1745 const ArgList &Args)
1746 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001747
1748 getProgramPaths().push_back(getDriver().getInstalledDir());
1749 if (getDriver().getInstalledDir() != getDriver().Dir)
1750 getProgramPaths().push_back(getDriver().Dir);
1751
1752 getFilePaths().push_back(getDriver().Dir + "/../lib");
1753 getFilePaths().push_back("/usr/lib");
1754}
1755
1756Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
1757 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001758 Action::ActionClass Key = JA.getKind();
David Chisnall31c46902012-02-15 13:39:01 +00001759
1760 Tool *&T = Tools[Key];
1761 if (!T) {
1762 switch (Key) {
1763 case Action::AssembleJobClass:
1764 T = new tools::solaris::Assemble(*this); break;
1765 case Action::LinkJobClass:
1766 T = new tools::solaris::Link(*this); break;
1767 default:
1768 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1769 }
1770 }
1771
1772 return *T;
1773}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001774
Eli Friedman6b3454a2009-05-26 07:52:18 +00001775/// Linux toolchain (very bare-bones at the moment).
1776
Rafael Espindolac1da9812010-11-07 20:14:31 +00001777enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001778 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001779 DebianLenny,
1780 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001781 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001782 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001783 RHEL4,
1784 RHEL5,
1785 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001786 Fedora13,
1787 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001788 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001789 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001790 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001791 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001792 OpenSuse11_4,
1793 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001794 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00001795 UbuntuHardy,
1796 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001797 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001798 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001799 UbuntuLucid,
1800 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001801 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001802 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001803 UbuntuPrecise,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001804 UnknownDistro
1805};
1806
Chris Lattnerd753b562011-05-22 05:36:06 +00001807static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001808 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1809 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001810}
1811
1812static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001813 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001814}
1815
1816static bool IsDebian(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001817 return Distro >= DebianLenny && Distro <= DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001818}
1819
1820static bool IsUbuntu(enum LinuxDistro Distro) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001821 return Distro >= UbuntuHardy && Distro <= UbuntuPrecise;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001822}
1823
Rafael Espindolac1da9812010-11-07 20:14:31 +00001824static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001825 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001826 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001827 StringRef Data = File.get()->getBuffer();
1828 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001829 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001830 LinuxDistro Version = UnknownDistro;
1831 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
1832 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
1833 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
1834 .Case("hardy", UbuntuHardy)
1835 .Case("intrepid", UbuntuIntrepid)
1836 .Case("jaunty", UbuntuJaunty)
1837 .Case("karmic", UbuntuKarmic)
1838 .Case("lucid", UbuntuLucid)
1839 .Case("maverick", UbuntuMaverick)
1840 .Case("natty", UbuntuNatty)
1841 .Case("oneiric", UbuntuOneiric)
1842 .Case("precise", UbuntuPrecise)
1843 .Default(UnknownDistro);
1844 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001845 }
1846
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001847 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001848 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001849 if (Data.startswith("Fedora release 16"))
1850 return Fedora16;
1851 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00001852 return Fedora15;
1853 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001854 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001855 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001856 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001857 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001858 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001859 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001860 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001861 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001862 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001863 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1864 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001865 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001866 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001867 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1868 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001869 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001870 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001871 return UnknownDistro;
1872 }
1873
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001874 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001875 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001876 if (Data[0] == '5')
1877 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001878 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00001879 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001880 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00001881 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001882 return UnknownDistro;
1883 }
1884
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001885 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
1886 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
1887 .StartsWith("openSUSE 11.3", OpenSuse11_3)
1888 .StartsWith("openSUSE 11.4", OpenSuse11_4)
1889 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001890 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001891 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001892
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001893 bool Exists;
1894 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001895 return Exherbo;
1896
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001897 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1898 return ArchLinux;
1899
Rafael Espindolac1da9812010-11-07 20:14:31 +00001900 return UnknownDistro;
1901}
1902
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001903/// \brief Get our best guess at the multiarch triple for a target.
1904///
1905/// Debian-based systems are starting to use a multiarch setup where they use
1906/// a target-triple directory in the library and header search paths.
1907/// Unfortunately, this triple does not align with the vanilla target triple,
1908/// so we provide a rough mapping here.
1909static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
1910 StringRef SysRoot) {
1911 // For most architectures, just use whatever we have rather than trying to be
1912 // clever.
1913 switch (TargetTriple.getArch()) {
1914 default:
1915 return TargetTriple.str();
1916
1917 // We use the existence of '/lib/<triple>' as a directory to detect some
1918 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00001919 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
1920 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00001921 case llvm::Triple::arm:
1922 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00001923 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1924 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
1925 return "arm-linux-gnueabihf";
1926 } else {
1927 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
1928 return "arm-linux-gnueabi";
1929 }
Chad Rosier0337efd2012-07-11 19:08:21 +00001930 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001931 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001932 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
1933 return "i386-linux-gnu";
1934 return TargetTriple.str();
1935 case llvm::Triple::x86_64:
1936 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
1937 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001938 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00001939 case llvm::Triple::mips:
1940 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
1941 return "mips-linux-gnu";
1942 return TargetTriple.str();
1943 case llvm::Triple::mipsel:
1944 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
1945 return "mipsel-linux-gnu";
1946 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00001947 case llvm::Triple::ppc:
1948 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
1949 return "powerpc-linux-gnu";
1950 return TargetTriple.str();
1951 case llvm::Triple::ppc64:
1952 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
1953 return "powerpc64-linux-gnu";
1954 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001955 }
1956}
1957
Chandler Carruth00646ba2012-01-25 11:24:24 +00001958static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
1959 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
1960}
1961
Simon Atanasyan7a918882012-09-14 11:27:24 +00001962static bool isMipsArch(llvm::Triple::ArchType Arch) {
1963 return Arch == llvm::Triple::mips ||
1964 Arch == llvm::Triple::mipsel ||
1965 Arch == llvm::Triple::mips64 ||
1966 Arch == llvm::Triple::mips64el;
1967}
1968
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001969static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
1970 const ArgList &Args) {
1971 if (Arch != llvm::Triple::mips &&
1972 Arch != llvm::Triple::mipsel)
1973 return false;
1974
1975 Arg *A = Args.getLastArg(options::OPT_march_EQ,
1976 options::OPT_mcpu_EQ,
1977 options::OPT_mips_CPUs_Group);
1978
1979 if (!A)
1980 return false;
1981
1982 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
1983 return A->getOption().matches(options::OPT_mips32r2);
1984
Richard Smith1d489cf2012-11-01 04:30:05 +00001985 return A->getValue() == StringRef("mips32r2");
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001986}
1987
Simon Atanasyan7a918882012-09-14 11:27:24 +00001988static StringRef getMultilibDir(const llvm::Triple &Triple,
1989 const ArgList &Args) {
1990 if (!isMipsArch(Triple.getArch()))
1991 return Triple.isArch32Bit() ? "lib32" : "lib64";
1992
1993 // lib32 directory has a special meaning on MIPS targets.
1994 // It contains N32 ABI binaries. Use this folder if produce
1995 // code for N32 ABI only.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001996 if (hasMipsN32ABIArg(Args))
Simon Atanasyan7a918882012-09-14 11:27:24 +00001997 return "lib32";
1998
1999 return Triple.isArch32Bit() ? "lib" : "lib64";
2000}
2001
Rafael Espindola0e659592012-02-19 01:38:32 +00002002Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2003 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002004 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00002005 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002006
Rafael Espindolaab784082011-09-01 16:25:49 +00002007 // OpenSuse stores the linker with the compiler, add that to the search
2008 // path.
2009 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002010 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002011 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002012
2013 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002014
2015 LinuxDistro Distro = DetectLinuxDistro(Arch);
2016
Chris Lattner64a89172011-05-22 16:45:07 +00002017 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002018 ExtraOpts.push_back("-z");
2019 ExtraOpts.push_back("relro");
2020 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002021
Douglas Gregorf0594d82011-03-06 19:11:49 +00002022 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002023 ExtraOpts.push_back("-X");
2024
Logan Chien94a71422012-09-02 09:30:11 +00002025 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002026
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002027 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2028 // and the MIPS ABI require .dynsym to be sorted in different ways.
2029 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2030 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002031 // Android loader does not support .gnu.hash.
Simon Atanasyan7a918882012-09-14 11:27:24 +00002032 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002033 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2034 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002035 ExtraOpts.push_back("--hash-style=gnu");
2036
2037 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2038 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2039 ExtraOpts.push_back("--hash-style=both");
2040 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002041
Chris Lattnerd753b562011-05-22 05:36:06 +00002042 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002043 ExtraOpts.push_back("--no-add-needed");
2044
Eli Friedman0b200f62011-06-02 21:36:53 +00002045 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002046 IsOpenSuse(Distro) ||
2047 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002048 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002049 ExtraOpts.push_back("--build-id");
2050
Chris Lattner64a89172011-05-22 16:45:07 +00002051 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002052 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002053
Chandler Carruthd2deee12011-10-03 05:28:29 +00002054 // The selection of paths to try here is designed to match the patterns which
2055 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2056 // This was determined by running GCC in a fake filesystem, creating all
2057 // possible permutations of these directories, and seeing which ones it added
2058 // to the link paths.
2059 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002060
Simon Atanasyan7a918882012-09-14 11:27:24 +00002061 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002062 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002063
Chandler Carruthd1f73062011-11-06 23:09:05 +00002064 // Add the multilib suffixed paths where they are available.
2065 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002066 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002067 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002068
2069 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2070 addPathIfExists(GCCInstallation.getInstallPath() +
2071 GCCInstallation.getMultiarchSuffix() +
2072 "/mips-r2",
2073 Paths);
2074 else
2075 addPathIfExists((GCCInstallation.getInstallPath() +
2076 GCCInstallation.getMultiarchSuffix()),
2077 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002078
2079 // If the GCC installation we found is inside of the sysroot, we want to
2080 // prefer libraries installed in the parent prefix of the GCC installation.
2081 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002082 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002083 // This usually happens when there is an external cross compiler on the
2084 // host system, and a more minimal sysroot available that is the target of
2085 // the cross.
2086 if (StringRef(LibPath).startswith(SysRoot)) {
2087 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2088 Paths);
2089 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2090 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2091 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002092 // On Android, libraries in the parent prefix of the GCC installation are
2093 // preferred to the ones under sysroot.
2094 if (IsAndroid) {
2095 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2096 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002097 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002098 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2099 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2100 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2101 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2102
2103 // Try walking via the GCC triple path in case of multiarch GCC
2104 // installations with strange symlinks.
2105 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002106 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002107 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002108
Chandler Carruth7a09d012011-10-16 10:54:30 +00002109 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002110 if (GCCInstallation.isValid()) {
2111 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002112 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002113 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002114 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002115
2116 if (StringRef(LibPath).startswith(SysRoot)) {
2117 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2118 addPathIfExists(LibPath, Paths);
2119 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002120 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002121 addPathIfExists(SysRoot + "/lib", Paths);
2122 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002123}
2124
2125bool Linux::HasNativeLLVMSupport() const {
2126 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002127}
2128
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002129Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2130 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00002131 Action::ActionClass Key = JA.getKind();
Rafael Espindoladda5b922010-11-07 23:13:01 +00002132 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2133 options::OPT_no_integrated_as,
2134 IsIntegratedAssemblerDefault());
2135
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002136 Tool *&T = Tools[Key];
2137 if (!T) {
2138 switch (Key) {
2139 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002140 if (UseIntegratedAs)
2141 T = new tools::ClangAs(*this);
2142 else
2143 T = new tools::linuxtools::Assemble(*this);
2144 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002145 case Action::LinkJobClass:
2146 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002147 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002148 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002149 }
2150 }
2151
2152 return *T;
2153}
2154
Rafael Espindola8af669f2012-06-19 01:26:10 +00002155void Linux::addClangTargetOptions(ArgStringList &CC1Args) const {
2156 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
2157 if (V >= Generic_GCC::GCCVersion::Parse("4.7.0"))
2158 CC1Args.push_back("-fuse-init-array");
2159}
2160
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002161void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2162 ArgStringList &CC1Args) const {
2163 const Driver &D = getDriver();
2164
2165 if (DriverArgs.hasArg(options::OPT_nostdinc))
2166 return;
2167
2168 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2169 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2170
2171 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002172 llvm::sys::Path P(D.ResourceDir);
2173 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002174 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002175 }
2176
2177 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2178 return;
2179
2180 // Check for configure-time C include directories.
2181 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2182 if (CIncludeDirs != "") {
2183 SmallVector<StringRef, 5> dirs;
2184 CIncludeDirs.split(dirs, ":");
2185 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2186 I != E; ++I) {
2187 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2188 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2189 }
2190 return;
2191 }
2192
2193 // Lacking those, try to detect the correct set of system includes for the
2194 // target triple.
2195
Chandler Carrutha4630892011-11-06 08:21:07 +00002196 // Implement generic Debian multiarch support.
2197 const StringRef X86_64MultiarchIncludeDirs[] = {
2198 "/usr/include/x86_64-linux-gnu",
2199
2200 // FIXME: These are older forms of multiarch. It's not clear that they're
2201 // in use in any released version of Debian, so we should consider
2202 // removing them.
2203 "/usr/include/i686-linux-gnu/64",
2204 "/usr/include/i486-linux-gnu/64"
2205 };
2206 const StringRef X86MultiarchIncludeDirs[] = {
2207 "/usr/include/i386-linux-gnu",
2208
2209 // FIXME: These are older forms of multiarch. It's not clear that they're
2210 // in use in any released version of Debian, so we should consider
2211 // removing them.
2212 "/usr/include/x86_64-linux-gnu/32",
2213 "/usr/include/i686-linux-gnu",
2214 "/usr/include/i486-linux-gnu"
2215 };
2216 const StringRef ARMMultiarchIncludeDirs[] = {
2217 "/usr/include/arm-linux-gnueabi"
2218 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002219 const StringRef ARMHFMultiarchIncludeDirs[] = {
2220 "/usr/include/arm-linux-gnueabihf"
2221 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002222 const StringRef MIPSMultiarchIncludeDirs[] = {
2223 "/usr/include/mips-linux-gnu"
2224 };
2225 const StringRef MIPSELMultiarchIncludeDirs[] = {
2226 "/usr/include/mipsel-linux-gnu"
2227 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002228 const StringRef PPCMultiarchIncludeDirs[] = {
2229 "/usr/include/powerpc-linux-gnu"
2230 };
2231 const StringRef PPC64MultiarchIncludeDirs[] = {
2232 "/usr/include/powerpc64-linux-gnu"
2233 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002234 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002235 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002236 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002237 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002238 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002239 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002240 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2241 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2242 else
2243 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002244 } else if (getTriple().getArch() == llvm::Triple::mips) {
2245 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2246 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2247 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002248 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2249 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2250 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2251 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002252 }
2253 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2254 E = MultiarchIncludeDirs.end();
2255 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002256 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002257 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2258 break;
2259 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002260 }
2261
2262 if (getTriple().getOS() == llvm::Triple::RTEMS)
2263 return;
2264
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002265 // Add an include of '/include' directly. This isn't provided by default by
2266 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2267 // add even when Clang is acting as-if it were a system compiler.
2268 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2269
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002270 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2271}
2272
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002273/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2274/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2275 const ArgList &DriverArgs,
2276 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002277 if (!llvm::sys::fs::exists(Base))
2278 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002279 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002280 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002281 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002282 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002283}
2284
2285void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2286 ArgStringList &CC1Args) const {
2287 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2288 DriverArgs.hasArg(options::OPT_nostdincxx))
2289 return;
2290
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002291 // Check if libc++ has been enabled and provide its include paths if so.
2292 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2293 // libc++ is always installed at a fixed path on Linux currently.
2294 addSystemInclude(DriverArgs, CC1Args,
2295 getDriver().SysRoot + "/usr/include/c++/v1");
2296 return;
2297 }
2298
Chandler Carruthfc52f752012-01-25 08:04:13 +00002299 // We need a detected GCC installation on Linux to provide libstdc++'s
2300 // headers. We handled the libc++ case above.
2301 if (!GCCInstallation.isValid())
2302 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002303
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002304 // By default, look for the C++ headers in an include directory adjacent to
2305 // the lib directory of the GCC installation. Note that this is expect to be
2306 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2307 StringRef LibDir = GCCInstallation.getParentLibPath();
2308 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002309 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002310 StringRef TripleStr = GCCInstallation.getTriple().str();
2311
2312 const std::string IncludePathCandidates[] = {
2313 LibDir.str() + "/../include/c++/" + Version.str(),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002314 // Gentoo is weird and places its headers inside the GCC install, so if the
2315 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002316 InstallDir.str() + "/include/g++-v4",
2317 // Android standalone toolchain has C++ headers in yet another place.
2318 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002319 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2320 // without a subdirectory corresponding to the gcc version.
2321 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002322 };
2323
2324 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2325 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2326 GCCInstallation.getMultiarchSuffix()),
2327 DriverArgs, CC1Args))
2328 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002329 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002330}
2331
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002332/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2333
Rafael Espindola0e659592012-02-19 01:38:32 +00002334DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2335 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002336
2337 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002338 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002339 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002340 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002341
Daniel Dunbaree788e72009-12-21 18:54:17 +00002342 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002343 getFilePaths().push_back("/usr/lib");
2344 getFilePaths().push_back("/usr/lib/gcc41");
2345}
2346
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002347Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2348 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00002349 Action::ActionClass Key = JA.getKind();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002350
2351 Tool *&T = Tools[Key];
2352 if (!T) {
2353 switch (Key) {
2354 case Action::AssembleJobClass:
2355 T = new tools::dragonfly::Assemble(*this); break;
2356 case Action::LinkJobClass:
2357 T = new tools::dragonfly::Link(*this); break;
2358 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002359 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002360 }
2361 }
2362
2363 return *T;
2364}