blob: 49685c088093c16dd64d8940151b4068876929e3 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
11
Daniel Dunbarf3cad362009-03-25 04:13:45 +000012#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000014#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000017#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000018#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000019#include "clang/Driver/Options.h"
John McCall260611a2012-06-20 06:18:46 +000020#include "clang/Basic/ObjCRuntime.h"
Douglas Gregor34916db2010-09-03 17:16:03 +000021#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000022
Daniel Dunbar00577ad2010-08-23 22:35:37 +000023#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000024#include "llvm/ADT/StringExtras.h"
Bob Wilsona59956b2011-10-07 00:37:57 +000025#include "llvm/ADT/StringSwitch.h"
John McCallf85e1932011-06-15 23:02:42 +000026#include "llvm/ADT/STLExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000027#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000028#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000029#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000031#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000032#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000033
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000034#include "SanitizerArgs.h"
35
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000036#include <cstdlib> // ::getenv
37
Dylan Noblesmithcc8a9452012-02-14 15:54:49 +000038#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
Dylan Noblesmith89bb6142011-06-23 13:50:47 +000039
Daniel Dunbar39176082009-03-20 00:20:03 +000040using namespace clang::driver;
41using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000042using namespace clang;
Daniel Dunbar39176082009-03-20 00:20:03 +000043
Daniel Dunbarf3955282009-09-04 18:34:51 +000044/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000045
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000046Darwin::Darwin(const Driver &D, const llvm::Triple& Triple)
John McCall260611a2012-06-20 06:18:46 +000047 : ToolChain(D, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000048{
Bob Wilson10853772012-01-31 21:30:03 +000049 // Compute the initial Darwin version from the triple
50 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-01-31 22:43:59 +000051 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
52 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
53 Triple.getOSName();
54 llvm::raw_string_ostream(MacosxVersionMin)
55 << Major << '.' << Minor << '.' << Micro;
56
Bob Wilson10853772012-01-31 21:30:03 +000057 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
58 // It should be removed when we stop supporting that.
59 DarwinVersion[0] = Minor + 4;
60 DarwinVersion[1] = Micro;
61 DarwinVersion[2] = 0;
Chad Rosierc793ea42012-05-09 18:46:30 +000062
63 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000064 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000065 llvm::raw_string_ostream(iOSVersionMin)
66 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000067}
68
Daniel Dunbar41800112010-08-02 05:43:56 +000069types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
70 types::ID Ty = types::lookupTypeForExtension(Ext);
71
72 // Darwin always preprocesses assembly files (unless -x is used explicitly).
73 if (Ty == types::TY_PP_Asm)
74 return types::TY_Asm;
75
76 return Ty;
77}
78
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000079bool Darwin::HasNativeLLVMSupport() const {
80 return true;
81}
82
John McCall9f084a32011-07-06 00:26:06 +000083/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall260611a2012-06-20 06:18:46 +000084ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
85 if (isTargetIPhoneOS()) {
86 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
87 } else if (TargetSimulatorVersionFromDefines != VersionTuple()) {
88 return ObjCRuntime(ObjCRuntime::iOS, TargetSimulatorVersionFromDefines);
89 } else {
90 if (isNonFragile) {
91 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
92 } else {
93 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
94 }
95 }
John McCall9f084a32011-07-06 00:26:06 +000096}
97
John McCall13db5cf2011-09-09 20:41:01 +000098/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
99bool Darwin::hasBlocksRuntime() const {
100 if (isTargetIPhoneOS())
101 return !isIPhoneOSVersionLT(3, 2);
102 else
103 return !isMacosxVersionLT(10, 6);
104}
105
Chris Lattner5f9e2722011-07-23 10:55:15 +0000106static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000107 return llvm::StringSwitch<const char*>(Value)
108 .Case("armv6k", "armv6")
109 .Case("armv5tej", "armv5")
110 .Case("xscale", "xscale")
111 .Case("armv4t", "armv4t")
112 .Case("armv7", "armv7")
113 .Cases("armv7a", "armv7-a", "armv7")
114 .Cases("armv7r", "armv7-r", "armv7")
115 .Cases("armv7m", "armv7-m", "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000116 .Cases("armv7f", "armv7-f", "armv7f")
117 .Cases("armv7k", "armv7-k", "armv7k")
118 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000119 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000120}
121
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000123 return llvm::StringSwitch<const char *>(Value)
124 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
125 .Cases("arm10e", "arm10tdmi", "armv5")
126 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
127 .Case("xscale", "xscale")
128 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s",
129 "arm1176jzf-s", "cortex-m0", "armv6")
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000130 .Cases("cortex-a8", "cortex-r4", "cortex-m3", "cortex-a9", "cortex-a15",
131 "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000132 .Case("cortex-a9-mp", "armv7f")
133 .Case("swift", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000134 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000135}
136
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000138 switch (getTriple().getArch()) {
139 default:
140 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000141
Douglas Gregorf0594d82011-03-06 19:11:49 +0000142 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000143 case llvm::Triple::arm: {
144 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000145 if (const char *Arch = GetArmArchForMArch(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000146 return Arch;
147
148 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000149 if (const char *Arch = GetArmArchForMCpu(A->getValue()))
Daniel Dunbareeff4062010-01-22 02:04:58 +0000150 return Arch;
151
152 return "arm";
153 }
154 }
155}
156
Daniel Dunbarf3955282009-09-04 18:34:51 +0000157Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000158 // Free tool implementations.
159 for (llvm::DenseMap<unsigned, Tool*>::iterator
160 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
161 delete it->second;
162}
163
Chad Rosier61ab80a2011-09-20 20:44:06 +0000164std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
165 types::ID InputType) const {
166 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000167
168 // If the target isn't initialized (e.g., an unknown Darwin platform, return
169 // the default triple).
170 if (!isTargetInitialized())
171 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000172
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000173 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000174 Str += isTargetIPhoneOS() ? "ios" : "macosx";
175 Str += getTargetVersion().getAsString();
176 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000177
178 return Triple.getTriple();
179}
180
David Blaikie99ba9e32011-12-20 02:48:34 +0000181void Generic_ELF::anchor() {}
182
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000183Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
184 const ActionList &Inputs) const {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000185 Action::ActionClass Key = JA.getKind();
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000186 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
187 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000188 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000189
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000190 Tool *&T = Tools[Key];
191 if (!T) {
192 switch (Key) {
193 case Action::InputClass:
194 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000195 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000196 case Action::PreprocessJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000197 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000198 case Action::MigrateJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000199 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000200 case Action::CompileJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +0000201 T = new tools::Clang(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000202 case Action::AssembleJobClass: {
203 if (UseIntegratedAs)
204 T = new tools::ClangAs(*this);
205 else
206 T = new tools::darwin::Assemble(*this);
207 break;
208 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000209 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000210 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000211 case Action::LipoJobClass:
212 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000213 case Action::DsymutilJobClass:
214 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000215 case Action::VerifyJobClass:
216 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000217 }
218 }
219
220 return *T;
221}
222
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000223
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000224DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
225 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000226{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000227 getProgramPaths().push_back(getDriver().getInstalledDir());
228 if (getDriver().getInstalledDir() != getDriver().Dir)
229 getProgramPaths().push_back(getDriver().Dir);
230
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000231 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000232 getProgramPaths().push_back(getDriver().getInstalledDir());
233 if (getDriver().getInstalledDir() != getDriver().Dir)
234 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000235}
236
John McCallf85e1932011-06-15 23:02:42 +0000237void DarwinClang::AddLinkARCArgs(const ArgList &Args,
238 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000239
240 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000241 llvm::sys::Path P(getDriver().ClangExecutable);
242 P.eraseComponent(); // 'clang'
243 P.eraseComponent(); // 'bin'
244 P.appendComponent("lib");
245 P.appendComponent("arc");
246 P.appendComponent("libarclite_");
247 std::string s = P.str();
248 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000249 if (isTargetIOSSimulator())
250 s += "iphonesimulator";
251 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000252 s += "iphoneos";
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000253 // FIXME: Remove this once we depend fully on -mios-simulator-version-min.
John McCall260611a2012-06-20 06:18:46 +0000254 else if (TargetSimulatorVersionFromDefines != VersionTuple())
John McCallf85e1932011-06-15 23:02:42 +0000255 s += "iphonesimulator";
256 else
257 s += "macosx";
258 s += ".a";
259
260 CmdArgs.push_back(Args.MakeArgString(s));
261}
262
Eric Christopher3404fe72011-06-22 17:41:40 +0000263void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000264 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000265 const char *DarwinStaticLib) const {
266 llvm::sys::Path P(getDriver().ResourceDir);
267 P.appendComponent("lib");
268 P.appendComponent("darwin");
269 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000270
Eric Christopher3404fe72011-06-22 17:41:40 +0000271 // For now, allow missing resource libraries to support developers who may
272 // not have compiler-rt checked out or integrated into their build.
273 bool Exists;
274 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
275 CmdArgs.push_back(Args.MakeArgString(P.str()));
276}
277
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000278void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
279 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000280 // Darwin only supports the compiler-rt based runtime libraries.
281 switch (GetRuntimeLibType(Args)) {
282 case ToolChain::RLT_CompilerRT:
283 break;
284 default:
285 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
Richard Smith1d489cf2012-11-01 04:30:05 +0000286 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "darwin";
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000287 return;
288 }
289
Daniel Dunbareec99102010-01-22 03:38:14 +0000290 // Darwin doesn't support real static executables, don't link any runtime
291 // libraries with -static.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000292 if (Args.hasArg(options::OPT_static) ||
293 Args.hasArg(options::OPT_fapple_kext) ||
294 Args.hasArg(options::OPT_mkernel))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000295 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000296
297 // Reject -static-libgcc for now, we can deal with this when and if someone
298 // cares. This is useful in situations where someone wants to statically link
299 // something like libstdc++, and needs its runtime support routines.
300 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000301 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000302 << A->getAsString(Args);
303 return;
304 }
305
Daniel Dunbarf4714872011-11-17 00:36:57 +0000306 // If we are building profile support, link that library in.
307 if (Args.hasArg(options::OPT_fprofile_arcs) ||
308 Args.hasArg(options::OPT_fprofile_generate) ||
309 Args.hasArg(options::OPT_fcreate_profile) ||
310 Args.hasArg(options::OPT_coverage)) {
311 // Select the appropriate runtime library for the target.
312 if (isTargetIPhoneOS()) {
313 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
314 } else {
315 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
316 }
317 }
318
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000319 SanitizerArgs Sanitize(getDriver(), Args);
320
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000321 // Add ASAN runtime library, if required. Dynamic libraries and bundles
322 // should not be linked with the runtime library.
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000323 if (Sanitize.needsAsanRt()) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000324 if (Args.hasArg(options::OPT_dynamiclib) ||
325 Args.hasArg(options::OPT_bundle)) return;
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000326 if (isTargetIPhoneOS()) {
327 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000328 << "-fsanitize=address";
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000329 } else {
330 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
331
332 // The ASAN runtime library requires C++ and CoreFoundation.
333 AddCXXStdlibLibArgs(Args, CmdArgs);
334 CmdArgs.push_back("-framework");
335 CmdArgs.push_back("CoreFoundation");
336 }
337 }
338
Daniel Dunbareec99102010-01-22 03:38:14 +0000339 // Otherwise link libSystem, then the dynamic runtime library, and finally any
340 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000341 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000342
343 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000344 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000345 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
346 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000347 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
348 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
349 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000350
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000351 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000352 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000353 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000354 // The dynamic runtime library was merged with libSystem for 10.6 and
355 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000356 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000357 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000358 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000359 CmdArgs.push_back("-lgcc_s.10.5");
360
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000361 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000362 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000363 // omitted from 10.4.dylib.
364 //
365 // Unfortunately, that turned out to not be true, because Darwin system
366 // headers can still use eprintf on i386, and it is not exported from
367 // libSystem. Therefore, we still must provide a runtime library just for
368 // the tiny tiny handful of projects that *might* use that symbol.
369 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000370 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000371 } else {
372 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000373 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
374 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000375 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000376 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000377}
378
Argyrios Kyrtzidisdceb11f2011-10-18 00:22:49 +0000379static inline StringRef SimulatorVersionDefineName() {
380 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
381}
382
383/// \brief Parse the simulator version define:
384/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
385// and return the grouped values as integers, e.g:
386// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
387// will return Major=4, Minor=2, Micro=1.
388static bool GetVersionFromSimulatorDefine(StringRef define,
389 unsigned &Major, unsigned &Minor,
390 unsigned &Micro) {
391 assert(define.startswith(SimulatorVersionDefineName()));
392 StringRef name, version;
393 llvm::tie(name, version) = define.split('=');
394 if (version.empty())
395 return false;
396 std::string verstr = version.str();
397 char *end;
398 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
399 if (*end != '\0')
400 return false;
401 Major = num / 10000;
402 num = num % 10000;
403 Minor = num / 100;
404 Micro = num % 100;
405 return true;
406}
407
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000408void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000409 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000410
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000411 // Support allowing the SDKROOT environment variable used by xcrun and other
412 // Xcode tools to define the default sysroot, by making it the default for
413 // isysroot.
414 if (!Args.hasArg(options::OPT_isysroot)) {
415 if (char *env = ::getenv("SDKROOT")) {
416 // We only use this value as the default if it is an absolute path and
417 // exists.
418 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env)) {
419 Args.append(Args.MakeSeparateArg(
420 0, Opts.getOption(options::OPT_isysroot), env));
421 }
422 }
423 }
424
Daniel Dunbar26031372010-01-27 00:56:25 +0000425 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000426 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
427 Arg *iOSSimVersion = Args.getLastArg(
428 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000429
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000430 // FIXME: HACK! When compiling for the simulator we don't get a
431 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
432 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
433 // define passed in command-line.
434 if (!iOSVersion && !iOSSimVersion) {
435 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
436 ie = Args.filtered_end(); it != ie; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000437 StringRef define = (*it)->getValue();
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000438 if (define.startswith(SimulatorVersionDefineName())) {
439 unsigned Major = 0, Minor = 0, Micro = 0;
440 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
441 Major < 10 && Minor < 100 && Micro < 100) {
John McCall260611a2012-06-20 06:18:46 +0000442 TargetSimulatorVersionFromDefines = VersionTuple(Major, Minor, Micro);
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000443 }
Bob Wilsona1ec3db2012-07-19 01:35:55 +0000444 // When using the define to indicate the simulator, we force
445 // 10.6 macosx target.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000446 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Bob Wilsona1ec3db2012-07-19 01:35:55 +0000447 OSXVersion = Args.MakeJoinedArg(0, O, "10.6");
448 Args.append(OSXVersion);
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000449 break;
450 }
451 }
452 }
453
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000454 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000455 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000456 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000457 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
458 iOSVersion = iOSSimVersion = 0;
459 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000460 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000461 << iOSVersion->getAsString(Args)
462 << iOSSimVersion->getAsString(Args);
463 iOSSimVersion = 0;
464 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000465 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000466 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000467 StringRef OSXTarget;
468 StringRef iOSTarget;
469 StringRef iOSSimTarget;
470 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
471 OSXTarget = env;
472 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
473 iOSTarget = env;
474 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
475 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000476
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000477 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000478 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000479 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000480 if (iOSTarget.empty()) {
481 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
482 StringRef first, second;
Richard Smith1d489cf2012-11-01 04:30:05 +0000483 StringRef isysroot = A->getValue();
Chad Rosiera4884972011-08-31 20:56:25 +0000484 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
485 if (second != "")
486 iOSTarget = second.substr(0,3);
487 }
488 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000489
Chad Rosier4f8de272011-09-28 00:46:32 +0000490 // If no OSX or iOS target has been specified and we're compiling for armv7,
491 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000492 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000493 (getDarwinArchName(Args) == "armv7" ||
494 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000495 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000496
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000497 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000498 //
499 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000500
501 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000502 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000503 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000504 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000505 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000506 "IPHONEOS_DEPLOYMENT_TARGET");
507 }
508
509 // Allow conflicts among OSX and iOS for historical reasons, but choose the
510 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000511 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000512 if (getTriple().getArch() == llvm::Triple::arm ||
513 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000514 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000515 else
Chad Rosiera4884972011-08-31 20:56:25 +0000516 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000517 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000518
Chad Rosiera4884972011-08-31 20:56:25 +0000519 if (!OSXTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000520 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000521 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
522 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000523 } else if (!iOSTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000524 const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000525 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
526 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000527 } else if (!iOSSimTarget.empty()) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000528 const Option O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000529 options::OPT_mios_simulator_version_min_EQ);
530 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
531 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000532 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000533 // Otherwise, assume we are targeting OS X.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000534 const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000535 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
536 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000537 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000540 // Reject invalid architecture combinations.
541 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
542 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000543 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000544 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
545 }
546
Daniel Dunbar26031372010-01-27 00:56:25 +0000547 // Set the tool chain target information.
548 unsigned Major, Minor, Micro;
549 bool HadExtra;
550 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000551 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000552 if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000553 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000554 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000555 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000556 << OSXVersion->getAsString(Args);
557 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000558 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
559 assert(Version && "Unknown target platform!");
Richard Smith1d489cf2012-11-01 04:30:05 +0000560 if (!Driver::GetReleaseVersion(Version->getValue(), Major, Minor,
Eli Friedman983d8352012-01-11 02:41:15 +0000561 Micro, HadExtra) || HadExtra ||
562 Major >= 10 || Minor >= 100 || Micro >= 100)
563 getDriver().Diag(diag::err_drv_invalid_version_number)
564 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000565 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000566
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000567 bool IsIOSSim = bool(iOSSimVersion);
568
569 // In GCC, the simulator historically was treated as being OS X in some
570 // contexts, like determining the link logic, despite generally being called
571 // with an iOS deployment target. For compatibility, we detect the
572 // simulator as iOS + x86, and treat it differently in a few contexts.
573 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
574 getTriple().getArch() == llvm::Triple::x86_64))
575 IsIOSSim = true;
576
577 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000578}
579
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000580void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000581 ArgStringList &CmdArgs) const {
582 CXXStdlibType Type = GetCXXStdlibType(Args);
583
584 switch (Type) {
585 case ToolChain::CST_Libcxx:
586 CmdArgs.push_back("-lc++");
587 break;
588
589 case ToolChain::CST_Libstdcxx: {
590 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
591 // it was previously found in the gcc lib dir. However, for all the Darwin
592 // platforms we care about it was -lstdc++.6, so we search for that
593 // explicitly if we can't see an obvious -lstdc++ candidate.
594
595 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000596 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000597 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000598 llvm::sys::Path P(A->getValue());
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000599 P.appendComponent("usr");
600 P.appendComponent("lib");
601 P.appendComponent("libstdc++.dylib");
602
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000603 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000604 P.eraseComponent();
605 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000606 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000607 CmdArgs.push_back(Args.MakeArgString(P.str()));
608 return;
609 }
610 }
611 }
612
613 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000614 // FIXME: This should be removed someday when we don't have to care about
615 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000616 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
617 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000618 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
619 return;
620 }
621
622 // Otherwise, let the linker search.
623 CmdArgs.push_back("-lstdc++");
624 break;
625 }
626 }
627}
628
Shantonu Sen7433fed2010-09-17 18:39:08 +0000629void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
630 ArgStringList &CmdArgs) const {
631
632 // For Darwin platforms, use the compiler-rt-based support library
633 // instead of the gcc-provided one (which is also incidentally
634 // only present in the gcc lib dir, which makes it hard to find).
635
636 llvm::sys::Path P(getDriver().ResourceDir);
637 P.appendComponent("lib");
638 P.appendComponent("darwin");
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000639
640 // Use the newer cc_kext for iOS ARM after 6.0.
641 if (!isTargetIPhoneOS() || isTargetIOSSimulator() ||
642 !isIPhoneOSVersionLT(6, 0)) {
643 P.appendComponent("libclang_rt.cc_kext.a");
644 } else {
645 P.appendComponent("libclang_rt.cc_kext_ios5.a");
646 }
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000647
Shantonu Sen7433fed2010-09-17 18:39:08 +0000648 // For now, allow missing resource libraries to support developers who may
649 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000650 bool Exists;
651 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000652 CmdArgs.push_back(Args.MakeArgString(P.str()));
653}
654
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000655DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
656 const char *BoundArch) const {
657 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
658 const OptTable &Opts = getDriver().getOpts();
659
660 // FIXME: We really want to get out of the tool chain level argument
661 // translation business, as it makes the driver functionality much
662 // more opaque. For now, we follow gcc closely solely for the
663 // purpose of easily achieving feature parity & testability. Once we
664 // have something that works, we should reevaluate each translation
665 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000666
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000667 for (ArgList::const_iterator it = Args.begin(),
668 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000669 Arg *A = *it;
670
671 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000672 // Skip this argument unless the architecture matches either the toolchain
673 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000674 llvm::Triple::ArchType XarchArch =
Richard Smith1d489cf2012-11-01 04:30:05 +0000675 tools::darwin::getArchTypeForDarwinArchName(A->getValue(0));
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000676 if (!(XarchArch == getArch() ||
677 (BoundArch && XarchArch ==
Rafael Espindolacfed8282012-10-31 18:51:07 +0000678 tools::darwin::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000679 continue;
680
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000681 Arg *OriginalArg = A;
Richard Smith1d489cf2012-11-01 04:30:05 +0000682 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
Daniel Dunbar0e100312010-06-14 21:23:08 +0000683 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000684 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000686 // If the argument parsing failed or more than one argument was
687 // consumed, the -Xarch_ argument's parameter tried to consume
688 // extra arguments. Emit an error and ignore.
689 //
690 // We also want to disallow any options which would alter the
691 // driver behavior; that isn't going to work in our model. We
692 // use isDriverOption() as an approximation, although things
693 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000694 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000695 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000696 << A->getAsString(Args);
697 continue;
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000698 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000699 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000700 << A->getAsString(Args);
701 continue;
702 }
703
Daniel Dunbar478edc22009-03-29 22:29:05 +0000704 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000705 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000706
707 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000708
709 // Linker input arguments require custom handling. The problem is that we
710 // have already constructed the phase actions, so we can not treat them as
711 // "input arguments".
Michael J. Spencer91e06da2012-10-19 22:37:06 +0000712 if (A->getOption().hasFlag(options::LinkerInput)) {
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000713 // Convert the argument into individual Zlinker_input_args.
714 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
715 DAL->AddSeparateArg(OriginalArg,
716 Opts.getOption(options::OPT_Zlinker_input),
Richard Smith1d489cf2012-11-01 04:30:05 +0000717 A->getValue(i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000718
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000719 }
720 continue;
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000723
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000724 // Sob. These is strictly gcc compatible for the time being. Apple
725 // gcc translates options twice, which means that self-expanding
726 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000727 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000728 default:
729 DAL->append(A);
730 break;
731
732 case options::OPT_mkernel:
733 case options::OPT_fapple_kext:
734 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000735 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000736 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000738 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000739 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
Richard Smith1d489cf2012-11-01 04:30:05 +0000740 A->getValue());
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000741 break;
742
743 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000744 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
745 DAL->AddFlagArg(A,
746 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000747 break;
748
749 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000750 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
751 DAL->AddFlagArg(A,
752 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000753 break;
754
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000755 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000756 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000757 break;
758
759 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000760 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000761 break;
762
763 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000764 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000765 break;
766
767 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000768 DAL->AddFlagArg(A,
769 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000770 break;
771
772 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000773 DAL->AddFlagArg(A,
774 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000775 break;
776
777 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000778 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000779 break;
780
781 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000782 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000783 break;
784 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000785 }
786
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000787 if (getTriple().getArch() == llvm::Triple::x86 ||
788 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000789 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000790 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000791
792 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000793 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000794 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000795 StringRef Name = BoundArch;
Michael J. Spencere4151c52012-10-19 22:36:40 +0000796 const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
797 const Option MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000798
799 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
800 // which defines the list of which architectures we accept.
801 if (Name == "ppc")
802 ;
803 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000804 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000805 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000806 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000807 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000808 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000809 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000810 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000811 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000812 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000813 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000814 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000815 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000816 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000817 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000818 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000819
820 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000821 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000822
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000823 else if (Name == "i386")
824 ;
825 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000826 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000827 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000828 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000829 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000830 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000831 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000832 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000833 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000834 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000835 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000836 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000837 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000838 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000839
840 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000841 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000842
843 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000844 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000845 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000846 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000847 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000848 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000849 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000850 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000851 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000852 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000853 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000854 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson336bfa32012-09-29 23:52:50 +0000855 else if (Name == "armv7f")
856 DAL->AddJoinedArg(0, MArch, "armv7f");
857 else if (Name == "armv7k")
858 DAL->AddJoinedArg(0, MArch, "armv7k");
859 else if (Name == "armv7s")
860 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000861
862 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000863 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000864 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000865
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000866 // Add an explicit version min argument for the deployment target. We do this
867 // after argument translation because -Xarch_ arguments may add a version min
868 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000869 if (BoundArch)
870 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000871
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000872 // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
873 // FIXME: It would be far better to avoid inserting those -static arguments,
874 // but we can't check the deployment target in the translation code until
875 // it is set here.
876 if (isTargetIPhoneOS() && !isIPhoneOSVersionLT(6, 0)) {
877 for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
878 Arg *A = *it;
879 ++it;
880 if (A->getOption().getID() != options::OPT_mkernel &&
881 A->getOption().getID() != options::OPT_fapple_kext)
882 continue;
883 assert(it != ie && "unexpected argument translation");
884 A = *it;
885 assert(A->getOption().getID() == options::OPT_static &&
886 "missing expected -static argument");
887 it = DAL->getArgs().erase(it);
888 }
889 }
890
Bob Wilson163b1512011-10-07 17:54:41 +0000891 // Validate the C++ standard library choice.
892 CXXStdlibType Type = GetCXXStdlibType(*DAL);
893 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000894 // Check whether the target provides libc++.
895 StringRef where;
896
897 // Complain about targetting iOS < 5.0 in any way.
John McCalle4860152012-06-21 17:46:38 +0000898 if (TargetSimulatorVersionFromDefines != VersionTuple()) {
899 if (TargetSimulatorVersionFromDefines < VersionTuple(5, 0))
900 where = "iOS 5.0";
901 } else if (isTargetIPhoneOS()) {
902 if (isIPhoneOSVersionLT(5, 0))
903 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000904 }
905
906 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000907 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000908 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000909 }
910 }
911
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000912 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000913}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000914
Daniel Dunbarf3955282009-09-04 18:34:51 +0000915bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000916 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000917}
918
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000919bool Darwin::UseDwarfDebugFlags() const {
920 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
921 return S[0] != '\0';
922 return false;
923}
924
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000925bool Darwin::UseSjLjExceptions() const {
926 // Darwin uses SjLj exceptions on ARM.
927 return (getTriple().getArch() == llvm::Triple::arm ||
928 getTriple().getArch() == llvm::Triple::thumb);
929}
930
Daniel Dunbarf3955282009-09-04 18:34:51 +0000931const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000932 return "pic";
933}
934
Daniel Dunbarf3955282009-09-04 18:34:51 +0000935const char *Darwin::GetForcedPicModel() const {
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000936 if (getArch() == llvm::Triple::x86_64)
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000937 return "pic";
938 return 0;
939}
940
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000941bool Darwin::SupportsProfiling() const {
942 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000943 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000944}
945
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000946bool Darwin::SupportsObjCGC() const {
947 // Garbage collection is supported everywhere except on iPhone OS.
948 return !isTargetIPhoneOS();
949}
950
John McCall0a7dd782012-08-21 02:47:43 +0000951void Darwin::CheckObjCARC() const {
952 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
953 return;
John McCall80fd37a2012-08-27 01:56:21 +0000954 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000955}
956
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000957std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000958Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
959 types::ID InputType) const {
960 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000961}
962
Daniel Dunbar39176082009-03-20 00:20:03 +0000963/// Generic_GCC - A tool chain using the 'gcc' command to perform
964/// all subcommands; this relies on gcc translating the majority of
965/// command line options.
966
Chandler Carruth19347ed2011-11-06 23:39:34 +0000967/// \brief Parse a GCCVersion object out of a string of text.
968///
969/// This is the primary means of forming GCCVersion objects.
970/*static*/
971Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
972 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
973 std::pair<StringRef, StringRef> First = VersionText.split('.');
974 std::pair<StringRef, StringRef> Second = First.second.split('.');
975
976 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
977 if (First.first.getAsInteger(10, GoodVersion.Major) ||
978 GoodVersion.Major < 0)
979 return BadVersion;
980 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
981 GoodVersion.Minor < 0)
982 return BadVersion;
983
984 // First look for a number prefix and parse that if present. Otherwise just
985 // stash the entire patch string in the suffix, and leave the number
986 // unspecified. This covers versions strings such as:
987 // 4.4
988 // 4.4.0
989 // 4.4.x
990 // 4.4.2-rc4
991 // 4.4.x-patched
992 // And retains any patch number it finds.
993 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
994 if (!PatchText.empty()) {
995 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
996 // Try to parse the number and any suffix.
997 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
998 GoodVersion.Patch < 0)
999 return BadVersion;
1000 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
1001 }
1002 }
1003
1004 return GoodVersion;
1005}
1006
1007/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
1008bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
1009 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
1010 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
1011
1012 // Note that we rank versions with *no* patch specified is better than ones
1013 // hard-coding a patch version. Thus if the RHS has no patch, it always
1014 // wins, and the LHS only wins if it has no patch and the RHS does have
1015 // a patch.
1016 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
1017 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
Gabor Greif241cbe42012-04-18 10:59:08 +00001018 if (PatchSuffix == RHS.PatchSuffix) return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001019
1020 // Finally, between completely tied version numbers, the version with the
1021 // suffix loses as we prefer full releases.
1022 if (RHS.PatchSuffix.empty()) return true;
1023 return false;
1024}
1025
Rafael Espindola0e659592012-02-19 01:38:32 +00001026static StringRef getGCCToolchainDir(const ArgList &Args) {
1027 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1028 if (A)
Richard Smith1d489cf2012-11-01 04:30:05 +00001029 return A->getValue();
Rafael Espindola0e659592012-02-19 01:38:32 +00001030 return GCC_INSTALL_PREFIX;
1031}
1032
Chandler Carruth19347ed2011-11-06 23:39:34 +00001033/// \brief Construct a GCCInstallationDetector from the driver.
1034///
1035/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +00001036/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001037///
1038/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1039/// should instead pull the target out of the driver. This is currently
1040/// necessary because the driver doesn't store the final version of the target
1041/// triple.
1042Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1043 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001044 const llvm::Triple &TargetTriple,
1045 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001046 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001047 llvm::Triple MultiarchTriple
1048 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1049 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001050 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001051 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001052 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001053 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001054 SmallVector<StringRef, 10> CandidateTripleAliases;
1055 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1056 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1057 CandidateTripleAliases,
1058 CandidateMultiarchLibDirs,
1059 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001060
1061 // Compute the set of prefixes for our search.
1062 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1063 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001064
Rafael Espindola0e659592012-02-19 01:38:32 +00001065 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1066 if (GCCToolchainDir != "") {
1067 if (GCCToolchainDir.back() == '/')
1068 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001069
Rafael Espindola0e659592012-02-19 01:38:32 +00001070 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001071 } else {
1072 Prefixes.push_back(D.SysRoot);
1073 Prefixes.push_back(D.SysRoot + "/usr");
1074 Prefixes.push_back(D.InstalledDir + "/..");
1075 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001076
1077 // Loop over the various components which exist and select the best GCC
1078 // installation available. GCC installs are ranked by version number.
1079 Version = GCCVersion::Parse("0.0.0");
1080 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1081 if (!llvm::sys::fs::exists(Prefixes[i]))
1082 continue;
1083 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1084 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1085 if (!llvm::sys::fs::exists(LibDir))
1086 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001087 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001088 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
1089 CandidateTripleAliases[k]);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001090 }
1091 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1092 const std::string LibDir
1093 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1094 if (!llvm::sys::fs::exists(LibDir))
1095 continue;
1096 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1097 ++k)
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001098 ScanLibDirForGCCTriple(TargetArch, Args, LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001099 CandidateMultiarchTripleAliases[k],
1100 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001101 }
1102 }
1103}
1104
1105/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001106 const llvm::Triple &TargetTriple,
1107 const llvm::Triple &MultiarchTriple,
1108 SmallVectorImpl<StringRef> &LibDirs,
1109 SmallVectorImpl<StringRef> &TripleAliases,
1110 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1111 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1112 // Declare a bunch of static data sets that we'll select between below. These
1113 // are specifically designed to always refer to string literals to avoid any
1114 // lifetime or initialization issues.
1115 static const char *const ARMLibDirs[] = { "/lib" };
1116 static const char *const ARMTriples[] = {
1117 "arm-linux-gnueabi",
1118 "arm-linux-androideabi"
1119 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001120 static const char *const ARMHFTriples[] = {
1121 "arm-linux-gnueabihf",
1122 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001123
1124 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1125 static const char *const X86_64Triples[] = {
1126 "x86_64-linux-gnu",
1127 "x86_64-unknown-linux-gnu",
1128 "x86_64-pc-linux-gnu",
1129 "x86_64-redhat-linux6E",
1130 "x86_64-redhat-linux",
1131 "x86_64-suse-linux",
1132 "x86_64-manbo-linux-gnu",
1133 "x86_64-linux-gnu",
1134 "x86_64-slackware-linux"
1135 };
1136 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1137 static const char *const X86Triples[] = {
1138 "i686-linux-gnu",
1139 "i686-pc-linux-gnu",
1140 "i486-linux-gnu",
1141 "i386-linux-gnu",
1142 "i686-redhat-linux",
1143 "i586-redhat-linux",
1144 "i386-redhat-linux",
1145 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001146 "i486-slackware-linux",
1147 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001148 };
1149
1150 static const char *const MIPSLibDirs[] = { "/lib" };
1151 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1152 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001153 static const char *const MIPSELTriples[] = {
1154 "mipsel-linux-gnu",
1155 "mipsel-linux-android"
1156 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001157
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001158 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1159 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1160 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1161 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1162
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001163 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1164 static const char *const PPCTriples[] = {
1165 "powerpc-linux-gnu",
1166 "powerpc-unknown-linux-gnu",
Gabor Greif91720912012-05-15 11:21:03 +00001167 "powerpc-suse-linux",
1168 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001169 };
1170 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1171 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001172 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001173 "powerpc64-unknown-linux-gnu",
1174 "powerpc64-suse-linux",
1175 "ppc64-redhat-linux"
1176 };
1177
1178 switch (TargetTriple.getArch()) {
1179 case llvm::Triple::arm:
1180 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001181 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001182 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1183 TripleAliases.append(
1184 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1185 } else {
1186 TripleAliases.append(
1187 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1188 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001189 break;
1190 case llvm::Triple::x86_64:
1191 LibDirs.append(
1192 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1193 TripleAliases.append(
1194 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1195 MultiarchLibDirs.append(
1196 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1197 MultiarchTripleAliases.append(
1198 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1199 break;
1200 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001201 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001202 TripleAliases.append(
1203 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1204 MultiarchLibDirs.append(
1205 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1206 MultiarchTripleAliases.append(
1207 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1208 break;
1209 case llvm::Triple::mips:
1210 LibDirs.append(
1211 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1212 TripleAliases.append(
1213 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001214 MultiarchLibDirs.append(
1215 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1216 MultiarchTripleAliases.append(
1217 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001218 break;
1219 case llvm::Triple::mipsel:
1220 LibDirs.append(
1221 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1222 TripleAliases.append(
1223 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001224 MultiarchLibDirs.append(
1225 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1226 MultiarchTripleAliases.append(
1227 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1228 break;
1229 case llvm::Triple::mips64:
1230 LibDirs.append(
1231 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1232 TripleAliases.append(
1233 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1234 MultiarchLibDirs.append(
1235 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1236 MultiarchTripleAliases.append(
1237 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1238 break;
1239 case llvm::Triple::mips64el:
1240 LibDirs.append(
1241 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1242 TripleAliases.append(
1243 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1244 MultiarchLibDirs.append(
1245 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1246 MultiarchTripleAliases.append(
1247 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001248 break;
1249 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001250 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001251 TripleAliases.append(
1252 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1253 MultiarchLibDirs.append(
1254 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1255 MultiarchTripleAliases.append(
1256 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1257 break;
1258 case llvm::Triple::ppc64:
1259 LibDirs.append(
1260 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1261 TripleAliases.append(
1262 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1263 MultiarchLibDirs.append(
1264 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1265 MultiarchTripleAliases.append(
1266 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1267 break;
1268
1269 default:
1270 // By default, just rely on the standard lib directories and the original
1271 // triple.
1272 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001273 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001274
1275 // Always append the drivers target triple to the end, in case it doesn't
1276 // match any of our aliases.
1277 TripleAliases.push_back(TargetTriple.str());
1278
1279 // Also include the multiarch variant if it's different.
1280 if (TargetTriple.str() != MultiarchTriple.str())
1281 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001282}
1283
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001284// FIXME: There is the same routine in the Tools.cpp.
1285static bool hasMipsN32ABIArg(const ArgList &Args) {
1286 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
Richard Smith1d489cf2012-11-01 04:30:05 +00001287 return A && (A->getValue() == StringRef("n32"));
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001288}
1289
1290static StringRef getTargetMultiarchSuffix(llvm::Triple::ArchType TargetArch,
1291 const ArgList &Args) {
1292 if (TargetArch == llvm::Triple::x86_64 ||
1293 TargetArch == llvm::Triple::ppc64)
1294 return "/64";
1295
1296 if (TargetArch == llvm::Triple::mips64 ||
1297 TargetArch == llvm::Triple::mips64el) {
1298 if (hasMipsN32ABIArg(Args))
1299 return "/n32";
1300 else
1301 return "/64";
1302 }
1303
1304 return "/32";
1305}
1306
Chandler Carruth19347ed2011-11-06 23:39:34 +00001307void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001308 llvm::Triple::ArchType TargetArch, const ArgList &Args,
1309 const std::string &LibDir,
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001310 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001311 // There are various different suffixes involving the triple we
1312 // check for. We also record what is necessary to walk from each back
1313 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001314 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001315 "/gcc/" + CandidateTriple.str(),
1316 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1317
Hal Finkel02014b42012-09-18 22:25:07 +00001318 // The Freescale PPC SDK has the gcc libraries in
1319 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1320 "/" + CandidateTriple.str(),
1321
Chandler Carruth19347ed2011-11-06 23:39:34 +00001322 // Ubuntu has a strange mis-matched pair of triples that this happens to
1323 // match.
1324 // FIXME: It may be worthwhile to generalize this and look for a second
1325 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001326 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001327 };
1328 const std::string InstallSuffixes[] = {
1329 "/../../..",
1330 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001331 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001332 "/../../../.."
1333 };
1334 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001335 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1336 (TargetArch != llvm::Triple::x86));
1337 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1338 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001339 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001340 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001341 !EC && LI != LE; LI = LI.increment(EC)) {
1342 StringRef VersionText = llvm::sys::path::filename(LI->path());
1343 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1344 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1345 if (CandidateVersion < MinVersion)
1346 continue;
1347 if (CandidateVersion <= Version)
1348 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001349
1350 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001351 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001352 // libs in a subdirectory named 64. The simple logic we follow is that
1353 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1354 // we use that. If not, and if not a multiarch triple, we look for
1355 // crtbegin.o without the subdirectory.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00001356 StringRef MultiarchSuffix = getTargetMultiarchSuffix(TargetArch, Args);
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001357 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1358 GCCMultiarchSuffix = MultiarchSuffix.str();
1359 } else {
1360 if (NeedsMultiarchSuffix ||
1361 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1362 continue;
1363 GCCMultiarchSuffix.clear();
1364 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001365
1366 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001367 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001368 // FIXME: We hack together the directory name here instead of
1369 // using LI to ensure stable path separators across Windows and
1370 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001371 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001372 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001373 IsValid = true;
1374 }
1375 }
1376}
1377
Rafael Espindola0e659592012-02-19 01:38:32 +00001378Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1379 const ArgList &Args)
1380 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001381 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001382 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001383 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001384}
1385
Daniel Dunbar39176082009-03-20 00:20:03 +00001386Generic_GCC::~Generic_GCC() {
1387 // Free tool implementations.
1388 for (llvm::DenseMap<unsigned, Tool*>::iterator
1389 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1390 delete it->second;
1391}
1392
Mike Stump1eb44332009-09-09 15:08:12 +00001393Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001394 const JobAction &JA,
1395 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001396 Action::ActionClass Key = JA.getKind();
Daniel Dunbar39176082009-03-20 00:20:03 +00001397
1398 Tool *&T = Tools[Key];
1399 if (!T) {
1400 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001401 case Action::InputClass:
1402 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001403 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001404 case Action::PreprocessJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001405 case Action::PrecompileJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001406 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001407 case Action::MigrateJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001408 case Action::CompileJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001409 T = new tools::Clang(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001410 case Action::AssembleJobClass:
1411 T = new tools::gcc::Assemble(*this); break;
1412 case Action::LinkJobClass:
1413 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001415 // This is a bit ungeneric, but the only platform using a driver
1416 // driver is Darwin.
1417 case Action::LipoJobClass:
1418 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001419 case Action::DsymutilJobClass:
1420 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001421 case Action::VerifyJobClass:
1422 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001423 }
1424 }
1425
1426 return *T;
1427}
1428
Daniel Dunbar39176082009-03-20 00:20:03 +00001429bool Generic_GCC::IsUnwindTablesDefault() const {
Rafael Espindola6f009b62012-09-22 15:04:11 +00001430 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar39176082009-03-20 00:20:03 +00001431}
1432
1433const char *Generic_GCC::GetDefaultRelocationModel() const {
1434 return "static";
1435}
1436
1437const char *Generic_GCC::GetForcedPicModel() const {
1438 return 0;
1439}
Tony Linthicum96319392011-12-12 21:14:55 +00001440/// Hexagon Toolchain
1441
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001442Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple& Triple)
1443 : ToolChain(D, Triple) {
Tony Linthicum96319392011-12-12 21:14:55 +00001444 getProgramPaths().push_back(getDriver().getInstalledDir());
1445 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1446 getProgramPaths().push_back(getDriver().Dir);
1447}
1448
1449Hexagon_TC::~Hexagon_TC() {
1450 // Free tool implementations.
1451 for (llvm::DenseMap<unsigned, Tool*>::iterator
1452 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1453 delete it->second;
1454}
1455
1456Tool &Hexagon_TC::SelectTool(const Compilation &C,
1457 const JobAction &JA,
1458 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001459 Action::ActionClass Key = JA.getKind();
Tony Linthicum96319392011-12-12 21:14:55 +00001460 Tool *&T = Tools[Key];
1461 if (!T) {
1462 switch (Key) {
1463 case Action::InputClass:
1464 case Action::BindArchClass:
1465 assert(0 && "Invalid tool kind.");
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001466 case Action::PreprocessJobClass:
1467 case Action::PrecompileJobClass:
Tony Linthicum96319392011-12-12 21:14:55 +00001468 case Action::AnalyzeJobClass:
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001469 case Action::MigrateJobClass:
1470 case Action::CompileJobClass:
Tony Linthicum96319392011-12-12 21:14:55 +00001471 T = new tools::Clang(*this); break;
1472 case Action::AssembleJobClass:
1473 T = new tools::hexagon::Assemble(*this); break;
1474 case Action::LinkJobClass:
1475 T = new tools::hexagon::Link(*this); break;
1476 default:
1477 assert(false && "Unsupported action for Hexagon target.");
1478 }
1479 }
1480
1481 return *T;
1482}
1483
Tony Linthicum96319392011-12-12 21:14:55 +00001484const char *Hexagon_TC::GetDefaultRelocationModel() const {
1485 return "static";
1486}
1487
1488const char *Hexagon_TC::GetForcedPicModel() const {
1489 return 0;
1490} // End Hexagon
1491
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001492
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001493/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1494/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1495/// Currently does not support anything else but compilation.
1496
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001497TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1498 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001499 // Path mangling to find libexec
1500 std::string Path(getDriver().Dir);
1501
1502 Path += "/../libexec";
1503 getProgramPaths().push_back(Path);
1504}
1505
1506TCEToolChain::~TCEToolChain() {
1507 for (llvm::DenseMap<unsigned, Tool*>::iterator
1508 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1509 delete it->second;
1510}
1511
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001512bool TCEToolChain::IsMathErrnoDefault() const {
1513 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001514}
1515
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001516const char *TCEToolChain::GetDefaultRelocationModel() const {
1517 return "static";
1518}
1519
1520const char *TCEToolChain::GetForcedPicModel() const {
1521 return 0;
1522}
1523
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001524Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001525 const JobAction &JA,
1526 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001527 Action::ActionClass Key;
1528 Key = Action::AnalyzeJobClass;
1529
1530 Tool *&T = Tools[Key];
1531 if (!T) {
1532 switch (Key) {
1533 case Action::PreprocessJobClass:
1534 T = new tools::gcc::Preprocess(*this); break;
1535 case Action::AnalyzeJobClass:
1536 T = new tools::Clang(*this); break;
1537 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001538 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001539 }
1540 }
1541 return *T;
1542}
1543
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001544/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1545
Rafael Espindola0e659592012-02-19 01:38:32 +00001546OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1547 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001548 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001549 getFilePaths().push_back("/usr/lib");
1550}
1551
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001552Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1553 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001554 Action::ActionClass Key = JA.getKind();
Rafael Espindoladda5b922010-11-07 23:13:01 +00001555 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1556 options::OPT_no_integrated_as,
1557 IsIntegratedAssemblerDefault());
1558
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001559 Tool *&T = Tools[Key];
1560 if (!T) {
1561 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001562 case Action::AssembleJobClass: {
1563 if (UseIntegratedAs)
1564 T = new tools::ClangAs(*this);
1565 else
1566 T = new tools::openbsd::Assemble(*this);
1567 break;
1568 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001569 case Action::LinkJobClass:
1570 T = new tools::openbsd::Link(*this); break;
1571 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001572 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001573 }
1574 }
1575
1576 return *T;
1577}
1578
Eli Friedman42f74f22012-08-08 23:57:20 +00001579/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1580
1581Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1582 : Generic_ELF(D, Triple, Args) {
1583 getFilePaths().push_back(getDriver().Dir + "/../lib");
1584 getFilePaths().push_back("/usr/lib");
1585}
1586
1587Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA,
1588 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001589 Action::ActionClass Key = JA.getKind();
Eli Friedman42f74f22012-08-08 23:57:20 +00001590 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1591 options::OPT_no_integrated_as,
1592 IsIntegratedAssemblerDefault());
1593
1594 Tool *&T = Tools[Key];
1595 if (!T) {
1596 switch (Key) {
1597 case Action::AssembleJobClass: {
1598 if (UseIntegratedAs)
1599 T = new tools::ClangAs(*this);
1600 else
1601 T = new tools::bitrig::Assemble(*this);
1602 break;
1603 }
1604 case Action::LinkJobClass:
1605 T = new tools::bitrig::Link(*this); break;
1606 default:
1607 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1608 }
1609 }
1610
1611 return *T;
1612}
1613
1614void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1615 ArgStringList &CC1Args) const {
1616 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1617 DriverArgs.hasArg(options::OPT_nostdincxx))
1618 return;
1619
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001620 switch (GetCXXStdlibType(DriverArgs)) {
1621 case ToolChain::CST_Libcxx:
1622 addSystemInclude(DriverArgs, CC1Args,
1623 getDriver().SysRoot + "/usr/include/c++/");
1624 break;
1625 case ToolChain::CST_Libstdcxx:
1626 addSystemInclude(DriverArgs, CC1Args,
1627 getDriver().SysRoot + "/usr/include/c++/stdc++");
1628 addSystemInclude(DriverArgs, CC1Args,
1629 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001630
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001631 StringRef Triple = getTriple().str();
1632 if (Triple.startswith("amd64"))
1633 addSystemInclude(DriverArgs, CC1Args,
1634 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1635 Triple.substr(5));
1636 else
1637 addSystemInclude(DriverArgs, CC1Args,
1638 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1639 Triple);
1640 break;
1641 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001642}
1643
1644void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1645 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001646 switch (GetCXXStdlibType(Args)) {
1647 case ToolChain::CST_Libcxx:
1648 CmdArgs.push_back("-lc++");
1649 CmdArgs.push_back("-lcxxrt");
1650 // Include supc++ to provide Unwind until provided by libcxx.
1651 CmdArgs.push_back("-lgcc");
1652 break;
1653 case ToolChain::CST_Libstdcxx:
1654 CmdArgs.push_back("-lstdc++");
1655 break;
1656 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001657}
1658
Daniel Dunbar75358d22009-03-30 21:06:03 +00001659/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1660
Rafael Espindola0e659592012-02-19 01:38:32 +00001661FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1662 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001663
Chandler Carruth24248e32012-01-26 01:35:15 +00001664 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1665 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001666 if ((Triple.getArch() == llvm::Triple::x86 ||
1667 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001668 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001669 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1670 else
1671 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001672}
1673
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001674Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1675 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001676 Action::ActionClass Key = JA.getKind();
Roman Divacky67dece72010-11-08 17:46:39 +00001677 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1678 options::OPT_no_integrated_as,
1679 IsIntegratedAssemblerDefault());
1680
Daniel Dunbar75358d22009-03-30 21:06:03 +00001681 Tool *&T = Tools[Key];
1682 if (!T) {
1683 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001684 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001685 if (UseIntegratedAs)
1686 T = new tools::ClangAs(*this);
1687 else
1688 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001689 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001690 case Action::LinkJobClass:
1691 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001692 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001693 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001694 }
1695 }
1696
1697 return *T;
1698}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001699
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001700/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1701
Rafael Espindola0e659592012-02-19 01:38:32 +00001702NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1703 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001704
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001705 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001706 // When targeting a 32-bit platform, try the special directory used on
1707 // 64-bit hosts, and only fall back to the main library directory if that
1708 // doesn't work.
1709 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1710 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001711 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001712 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001713
1714 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001715 }
1716}
1717
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001718Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1719 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001720 Action::ActionClass Key = JA.getKind();
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001721 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1722 options::OPT_no_integrated_as,
1723 IsIntegratedAssemblerDefault());
1724
1725 Tool *&T = Tools[Key];
1726 if (!T) {
1727 switch (Key) {
1728 case Action::AssembleJobClass:
1729 if (UseIntegratedAs)
1730 T = new tools::ClangAs(*this);
1731 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001732 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001733 break;
1734 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001735 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001736 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001737 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001738 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001739 }
1740 }
1741
1742 return *T;
1743}
1744
Chris Lattner38e317d2010-07-07 16:01:42 +00001745/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1746
Rafael Espindola0e659592012-02-19 01:38:32 +00001747Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1748 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001749 getFilePaths().push_back(getDriver().Dir + "/../lib");
1750 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001751}
1752
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001753Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1754 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001755 Action::ActionClass Key = JA.getKind();
Chris Lattner38e317d2010-07-07 16:01:42 +00001756
1757 Tool *&T = Tools[Key];
1758 if (!T) {
1759 switch (Key) {
1760 case Action::AssembleJobClass:
1761 T = new tools::minix::Assemble(*this); break;
1762 case Action::LinkJobClass:
1763 T = new tools::minix::Link(*this); break;
1764 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001765 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001766 }
1767 }
1768
1769 return *T;
1770}
1771
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001772/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1773
Rafael Espindola0e659592012-02-19 01:38:32 +00001774AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1775 const ArgList &Args)
1776 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001777
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001778 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001779 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001780 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001781
Daniel Dunbaree788e72009-12-21 18:54:17 +00001782 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001783 getFilePaths().push_back("/usr/lib");
1784 getFilePaths().push_back("/usr/sfw/lib");
1785 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001786 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001787
1788}
1789
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001790Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1791 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001792 Action::ActionClass Key = JA.getKind();
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001793
1794 Tool *&T = Tools[Key];
1795 if (!T) {
1796 switch (Key) {
1797 case Action::AssembleJobClass:
1798 T = new tools::auroraux::Assemble(*this); break;
1799 case Action::LinkJobClass:
1800 T = new tools::auroraux::Link(*this); break;
1801 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001802 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001803 }
1804 }
1805
1806 return *T;
1807}
1808
David Chisnall31c46902012-02-15 13:39:01 +00001809/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1810
Rafael Espindola0e659592012-02-19 01:38:32 +00001811Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1812 const ArgList &Args)
1813 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001814
1815 getProgramPaths().push_back(getDriver().getInstalledDir());
1816 if (getDriver().getInstalledDir() != getDriver().Dir)
1817 getProgramPaths().push_back(getDriver().Dir);
1818
1819 getFilePaths().push_back(getDriver().Dir + "/../lib");
1820 getFilePaths().push_back("/usr/lib");
1821}
1822
1823Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
1824 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00001825 Action::ActionClass Key = JA.getKind();
David Chisnall31c46902012-02-15 13:39:01 +00001826
1827 Tool *&T = Tools[Key];
1828 if (!T) {
1829 switch (Key) {
1830 case Action::AssembleJobClass:
1831 T = new tools::solaris::Assemble(*this); break;
1832 case Action::LinkJobClass:
1833 T = new tools::solaris::Link(*this); break;
1834 default:
1835 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1836 }
1837 }
1838
1839 return *T;
1840}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001841
Eli Friedman6b3454a2009-05-26 07:52:18 +00001842/// Linux toolchain (very bare-bones at the moment).
1843
Rafael Espindolac1da9812010-11-07 20:14:31 +00001844enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001845 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001846 DebianLenny,
1847 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001848 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001849 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001850 RHEL4,
1851 RHEL5,
1852 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001853 Fedora13,
1854 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001855 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001856 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001857 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001858 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001859 OpenSuse11_4,
1860 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001861 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00001862 UbuntuHardy,
1863 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001864 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001865 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001866 UbuntuLucid,
1867 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001868 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001869 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001870 UbuntuPrecise,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001871 UnknownDistro
1872};
1873
Chris Lattnerd753b562011-05-22 05:36:06 +00001874static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001875 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1876 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001877}
1878
1879static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001880 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001881}
1882
1883static bool IsDebian(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001884 return Distro >= DebianLenny && Distro <= DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001885}
1886
1887static bool IsUbuntu(enum LinuxDistro Distro) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001888 return Distro >= UbuntuHardy && Distro <= UbuntuPrecise;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001889}
1890
Rafael Espindolac1da9812010-11-07 20:14:31 +00001891static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001892 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001893 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001894 StringRef Data = File.get()->getBuffer();
1895 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001896 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001897 LinuxDistro Version = UnknownDistro;
1898 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
1899 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
1900 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
1901 .Case("hardy", UbuntuHardy)
1902 .Case("intrepid", UbuntuIntrepid)
1903 .Case("jaunty", UbuntuJaunty)
1904 .Case("karmic", UbuntuKarmic)
1905 .Case("lucid", UbuntuLucid)
1906 .Case("maverick", UbuntuMaverick)
1907 .Case("natty", UbuntuNatty)
1908 .Case("oneiric", UbuntuOneiric)
1909 .Case("precise", UbuntuPrecise)
1910 .Default(UnknownDistro);
1911 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001912 }
1913
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001914 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001915 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001916 if (Data.startswith("Fedora release 16"))
1917 return Fedora16;
1918 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00001919 return Fedora15;
1920 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001921 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001922 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001923 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001924 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001925 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001926 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001927 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001928 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001929 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001930 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1931 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001932 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001933 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001934 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1935 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001936 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001937 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001938 return UnknownDistro;
1939 }
1940
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001941 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001942 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001943 if (Data[0] == '5')
1944 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001945 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00001946 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001947 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00001948 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001949 return UnknownDistro;
1950 }
1951
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001952 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
1953 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
1954 .StartsWith("openSUSE 11.3", OpenSuse11_3)
1955 .StartsWith("openSUSE 11.4", OpenSuse11_4)
1956 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001957 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001958 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001959
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001960 bool Exists;
1961 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001962 return Exherbo;
1963
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001964 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1965 return ArchLinux;
1966
Rafael Espindolac1da9812010-11-07 20:14:31 +00001967 return UnknownDistro;
1968}
1969
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001970/// \brief Get our best guess at the multiarch triple for a target.
1971///
1972/// Debian-based systems are starting to use a multiarch setup where they use
1973/// a target-triple directory in the library and header search paths.
1974/// Unfortunately, this triple does not align with the vanilla target triple,
1975/// so we provide a rough mapping here.
1976static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
1977 StringRef SysRoot) {
1978 // For most architectures, just use whatever we have rather than trying to be
1979 // clever.
1980 switch (TargetTriple.getArch()) {
1981 default:
1982 return TargetTriple.str();
1983
1984 // We use the existence of '/lib/<triple>' as a directory to detect some
1985 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00001986 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
1987 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00001988 case llvm::Triple::arm:
1989 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00001990 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1991 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
1992 return "arm-linux-gnueabihf";
1993 } else {
1994 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
1995 return "arm-linux-gnueabi";
1996 }
Chad Rosier0337efd2012-07-11 19:08:21 +00001997 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001998 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001999 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2000 return "i386-linux-gnu";
2001 return TargetTriple.str();
2002 case llvm::Triple::x86_64:
2003 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2004 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002005 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00002006 case llvm::Triple::mips:
2007 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2008 return "mips-linux-gnu";
2009 return TargetTriple.str();
2010 case llvm::Triple::mipsel:
2011 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2012 return "mipsel-linux-gnu";
2013 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002014 case llvm::Triple::ppc:
2015 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2016 return "powerpc-linux-gnu";
2017 return TargetTriple.str();
2018 case llvm::Triple::ppc64:
2019 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2020 return "powerpc64-linux-gnu";
2021 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002022 }
2023}
2024
Chandler Carruth00646ba2012-01-25 11:24:24 +00002025static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2026 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2027}
2028
Simon Atanasyan7a918882012-09-14 11:27:24 +00002029static bool isMipsArch(llvm::Triple::ArchType Arch) {
2030 return Arch == llvm::Triple::mips ||
2031 Arch == llvm::Triple::mipsel ||
2032 Arch == llvm::Triple::mips64 ||
2033 Arch == llvm::Triple::mips64el;
2034}
2035
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002036static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2037 const ArgList &Args) {
2038 if (Arch != llvm::Triple::mips &&
2039 Arch != llvm::Triple::mipsel)
2040 return false;
2041
2042 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2043 options::OPT_mcpu_EQ,
2044 options::OPT_mips_CPUs_Group);
2045
2046 if (!A)
2047 return false;
2048
2049 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2050 return A->getOption().matches(options::OPT_mips32r2);
2051
Richard Smith1d489cf2012-11-01 04:30:05 +00002052 return A->getValue() == StringRef("mips32r2");
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002053}
2054
Simon Atanasyan7a918882012-09-14 11:27:24 +00002055static StringRef getMultilibDir(const llvm::Triple &Triple,
2056 const ArgList &Args) {
2057 if (!isMipsArch(Triple.getArch()))
2058 return Triple.isArch32Bit() ? "lib32" : "lib64";
2059
2060 // lib32 directory has a special meaning on MIPS targets.
2061 // It contains N32 ABI binaries. Use this folder if produce
2062 // code for N32 ABI only.
Simon Atanasyanf4bd3292012-10-21 11:44:57 +00002063 if (hasMipsN32ABIArg(Args))
Simon Atanasyan7a918882012-09-14 11:27:24 +00002064 return "lib32";
2065
2066 return Triple.isArch32Bit() ? "lib" : "lib64";
2067}
2068
Rafael Espindola0e659592012-02-19 01:38:32 +00002069Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2070 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002071 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00002072 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002073
Rafael Espindolaab784082011-09-01 16:25:49 +00002074 // OpenSuse stores the linker with the compiler, add that to the search
2075 // path.
2076 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002077 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002078 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002079
2080 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002081
2082 LinuxDistro Distro = DetectLinuxDistro(Arch);
2083
Chris Lattner64a89172011-05-22 16:45:07 +00002084 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002085 ExtraOpts.push_back("-z");
2086 ExtraOpts.push_back("relro");
2087 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002088
Douglas Gregorf0594d82011-03-06 19:11:49 +00002089 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002090 ExtraOpts.push_back("-X");
2091
Logan Chien94a71422012-09-02 09:30:11 +00002092 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002093
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002094 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2095 // and the MIPS ABI require .dynsym to be sorted in different ways.
2096 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2097 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002098 // Android loader does not support .gnu.hash.
Simon Atanasyan7a918882012-09-14 11:27:24 +00002099 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002100 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2101 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002102 ExtraOpts.push_back("--hash-style=gnu");
2103
2104 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2105 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2106 ExtraOpts.push_back("--hash-style=both");
2107 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002108
Chris Lattnerd753b562011-05-22 05:36:06 +00002109 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002110 ExtraOpts.push_back("--no-add-needed");
2111
Eli Friedman0b200f62011-06-02 21:36:53 +00002112 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002113 IsOpenSuse(Distro) ||
2114 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002115 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002116 ExtraOpts.push_back("--build-id");
2117
Chris Lattner64a89172011-05-22 16:45:07 +00002118 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002119 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002120
Chandler Carruthd2deee12011-10-03 05:28:29 +00002121 // The selection of paths to try here is designed to match the patterns which
2122 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2123 // This was determined by running GCC in a fake filesystem, creating all
2124 // possible permutations of these directories, and seeing which ones it added
2125 // to the link paths.
2126 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002127
Simon Atanasyan7a918882012-09-14 11:27:24 +00002128 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002129 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002130
Chandler Carruthd1f73062011-11-06 23:09:05 +00002131 // Add the multilib suffixed paths where they are available.
2132 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002133 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002134 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002135
2136 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2137 addPathIfExists(GCCInstallation.getInstallPath() +
2138 GCCInstallation.getMultiarchSuffix() +
2139 "/mips-r2",
2140 Paths);
2141 else
2142 addPathIfExists((GCCInstallation.getInstallPath() +
2143 GCCInstallation.getMultiarchSuffix()),
2144 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002145
2146 // If the GCC installation we found is inside of the sysroot, we want to
2147 // prefer libraries installed in the parent prefix of the GCC installation.
2148 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002149 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002150 // This usually happens when there is an external cross compiler on the
2151 // host system, and a more minimal sysroot available that is the target of
2152 // the cross.
2153 if (StringRef(LibPath).startswith(SysRoot)) {
2154 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2155 Paths);
2156 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2157 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2158 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002159 // On Android, libraries in the parent prefix of the GCC installation are
2160 // preferred to the ones under sysroot.
2161 if (IsAndroid) {
2162 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2163 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002164 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002165 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2166 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2167 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2168 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2169
2170 // Try walking via the GCC triple path in case of multiarch GCC
2171 // installations with strange symlinks.
2172 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002173 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002174 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002175
Chandler Carruth7a09d012011-10-16 10:54:30 +00002176 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002177 if (GCCInstallation.isValid()) {
2178 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002179 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002180 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002181 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002182
2183 if (StringRef(LibPath).startswith(SysRoot)) {
2184 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2185 addPathIfExists(LibPath, Paths);
2186 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002187 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002188 addPathIfExists(SysRoot + "/lib", Paths);
2189 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002190}
2191
2192bool Linux::HasNativeLLVMSupport() const {
2193 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002194}
2195
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002196Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2197 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00002198 Action::ActionClass Key = JA.getKind();
Rafael Espindoladda5b922010-11-07 23:13:01 +00002199 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2200 options::OPT_no_integrated_as,
2201 IsIntegratedAssemblerDefault());
2202
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002203 Tool *&T = Tools[Key];
2204 if (!T) {
2205 switch (Key) {
2206 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002207 if (UseIntegratedAs)
2208 T = new tools::ClangAs(*this);
2209 else
2210 T = new tools::linuxtools::Assemble(*this);
2211 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002212 case Action::LinkJobClass:
2213 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002214 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002215 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002216 }
2217 }
2218
2219 return *T;
2220}
2221
Rafael Espindola8af669f2012-06-19 01:26:10 +00002222void Linux::addClangTargetOptions(ArgStringList &CC1Args) const {
2223 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
2224 if (V >= Generic_GCC::GCCVersion::Parse("4.7.0"))
2225 CC1Args.push_back("-fuse-init-array");
2226}
2227
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002228void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2229 ArgStringList &CC1Args) const {
2230 const Driver &D = getDriver();
2231
2232 if (DriverArgs.hasArg(options::OPT_nostdinc))
2233 return;
2234
2235 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2236 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2237
2238 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002239 llvm::sys::Path P(D.ResourceDir);
2240 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002241 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002242 }
2243
2244 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2245 return;
2246
2247 // Check for configure-time C include directories.
2248 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2249 if (CIncludeDirs != "") {
2250 SmallVector<StringRef, 5> dirs;
2251 CIncludeDirs.split(dirs, ":");
2252 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2253 I != E; ++I) {
2254 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2255 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2256 }
2257 return;
2258 }
2259
2260 // Lacking those, try to detect the correct set of system includes for the
2261 // target triple.
2262
Chandler Carrutha4630892011-11-06 08:21:07 +00002263 // Implement generic Debian multiarch support.
2264 const StringRef X86_64MultiarchIncludeDirs[] = {
2265 "/usr/include/x86_64-linux-gnu",
2266
2267 // FIXME: These are older forms of multiarch. It's not clear that they're
2268 // in use in any released version of Debian, so we should consider
2269 // removing them.
2270 "/usr/include/i686-linux-gnu/64",
2271 "/usr/include/i486-linux-gnu/64"
2272 };
2273 const StringRef X86MultiarchIncludeDirs[] = {
2274 "/usr/include/i386-linux-gnu",
2275
2276 // FIXME: These are older forms of multiarch. It's not clear that they're
2277 // in use in any released version of Debian, so we should consider
2278 // removing them.
2279 "/usr/include/x86_64-linux-gnu/32",
2280 "/usr/include/i686-linux-gnu",
2281 "/usr/include/i486-linux-gnu"
2282 };
2283 const StringRef ARMMultiarchIncludeDirs[] = {
2284 "/usr/include/arm-linux-gnueabi"
2285 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002286 const StringRef ARMHFMultiarchIncludeDirs[] = {
2287 "/usr/include/arm-linux-gnueabihf"
2288 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002289 const StringRef MIPSMultiarchIncludeDirs[] = {
2290 "/usr/include/mips-linux-gnu"
2291 };
2292 const StringRef MIPSELMultiarchIncludeDirs[] = {
2293 "/usr/include/mipsel-linux-gnu"
2294 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002295 const StringRef PPCMultiarchIncludeDirs[] = {
2296 "/usr/include/powerpc-linux-gnu"
2297 };
2298 const StringRef PPC64MultiarchIncludeDirs[] = {
2299 "/usr/include/powerpc64-linux-gnu"
2300 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002301 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002302 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002303 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002304 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002305 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002306 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002307 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2308 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2309 else
2310 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002311 } else if (getTriple().getArch() == llvm::Triple::mips) {
2312 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2313 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2314 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002315 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2316 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2317 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2318 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002319 }
2320 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2321 E = MultiarchIncludeDirs.end();
2322 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002323 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002324 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2325 break;
2326 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002327 }
2328
2329 if (getTriple().getOS() == llvm::Triple::RTEMS)
2330 return;
2331
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002332 // Add an include of '/include' directly. This isn't provided by default by
2333 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2334 // add even when Clang is acting as-if it were a system compiler.
2335 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2336
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002337 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2338}
2339
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002340/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2341/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2342 const ArgList &DriverArgs,
2343 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002344 if (!llvm::sys::fs::exists(Base))
2345 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002346 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002347 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002348 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002349 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002350}
2351
2352void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2353 ArgStringList &CC1Args) const {
2354 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2355 DriverArgs.hasArg(options::OPT_nostdincxx))
2356 return;
2357
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002358 // Check if libc++ has been enabled and provide its include paths if so.
2359 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2360 // libc++ is always installed at a fixed path on Linux currently.
2361 addSystemInclude(DriverArgs, CC1Args,
2362 getDriver().SysRoot + "/usr/include/c++/v1");
2363 return;
2364 }
2365
Chandler Carruthfc52f752012-01-25 08:04:13 +00002366 // We need a detected GCC installation on Linux to provide libstdc++'s
2367 // headers. We handled the libc++ case above.
2368 if (!GCCInstallation.isValid())
2369 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002370
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002371 // By default, look for the C++ headers in an include directory adjacent to
2372 // the lib directory of the GCC installation. Note that this is expect to be
2373 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2374 StringRef LibDir = GCCInstallation.getParentLibPath();
2375 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002376 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002377 StringRef TripleStr = GCCInstallation.getTriple().str();
2378
2379 const std::string IncludePathCandidates[] = {
2380 LibDir.str() + "/../include/c++/" + Version.str(),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002381 // Gentoo is weird and places its headers inside the GCC install, so if the
2382 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002383 InstallDir.str() + "/include/g++-v4",
2384 // Android standalone toolchain has C++ headers in yet another place.
2385 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002386 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2387 // without a subdirectory corresponding to the gcc version.
2388 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002389 };
2390
2391 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2392 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2393 GCCInstallation.getMultiarchSuffix()),
2394 DriverArgs, CC1Args))
2395 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002396 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002397}
2398
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002399/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2400
Rafael Espindola0e659592012-02-19 01:38:32 +00002401DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2402 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002403
2404 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002405 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002406 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002407 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002408
Daniel Dunbaree788e72009-12-21 18:54:17 +00002409 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002410 getFilePaths().push_back("/usr/lib");
2411 getFilePaths().push_back("/usr/lib/gcc41");
2412}
2413
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002414Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2415 const ActionList &Inputs) const {
Bob Wilson85b7f7d2012-11-08 01:03:34 +00002416 Action::ActionClass Key = JA.getKind();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002417
2418 Tool *&T = Tools[Key];
2419 if (!T) {
2420 switch (Key) {
2421 case Action::AssembleJobClass:
2422 T = new tools::dragonfly::Assemble(*this); break;
2423 case Action::LinkJobClass:
2424 T = new tools::dragonfly::Link(*this); break;
2425 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002426 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002427 }
2428 }
2429
2430 return *T;
2431}