blob: c7a8cba0b100f907ae243a703964308614b887c1 [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
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000034#include <cstdlib> // ::getenv
35
Dylan Noblesmithcc8a9452012-02-14 15:54:49 +000036#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
Dylan Noblesmith89bb6142011-06-23 13:50:47 +000037
Daniel Dunbar39176082009-03-20 00:20:03 +000038using namespace clang::driver;
39using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000040using namespace clang;
Daniel Dunbar39176082009-03-20 00:20:03 +000041
Daniel Dunbarf3955282009-09-04 18:34:51 +000042/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000043
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000044Darwin::Darwin(const Driver &D, const llvm::Triple& Triple)
John McCall260611a2012-06-20 06:18:46 +000045 : ToolChain(D, Triple), TargetInitialized(false)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000046{
Bob Wilson10853772012-01-31 21:30:03 +000047 // Compute the initial Darwin version from the triple
48 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-01-31 22:43:59 +000049 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
50 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
51 Triple.getOSName();
52 llvm::raw_string_ostream(MacosxVersionMin)
53 << Major << '.' << Minor << '.' << Micro;
54
Bob Wilson10853772012-01-31 21:30:03 +000055 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
56 // It should be removed when we stop supporting that.
57 DarwinVersion[0] = Minor + 4;
58 DarwinVersion[1] = Micro;
59 DarwinVersion[2] = 0;
Chad Rosierc793ea42012-05-09 18:46:30 +000060
61 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000062 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000063 llvm::raw_string_ostream(iOSVersionMin)
64 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000065}
66
Daniel Dunbar41800112010-08-02 05:43:56 +000067types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
68 types::ID Ty = types::lookupTypeForExtension(Ext);
69
70 // Darwin always preprocesses assembly files (unless -x is used explicitly).
71 if (Ty == types::TY_PP_Asm)
72 return types::TY_Asm;
73
74 return Ty;
75}
76
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000077bool Darwin::HasNativeLLVMSupport() const {
78 return true;
79}
80
John McCall9f084a32011-07-06 00:26:06 +000081/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
John McCall260611a2012-06-20 06:18:46 +000082ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
83 if (isTargetIPhoneOS()) {
84 return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
85 } else if (TargetSimulatorVersionFromDefines != VersionTuple()) {
86 return ObjCRuntime(ObjCRuntime::iOS, TargetSimulatorVersionFromDefines);
87 } else {
88 if (isNonFragile) {
89 return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
90 } else {
91 return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
92 }
93 }
John McCall9f084a32011-07-06 00:26:06 +000094}
95
John McCall13db5cf2011-09-09 20:41:01 +000096/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
97bool Darwin::hasBlocksRuntime() const {
98 if (isTargetIPhoneOS())
99 return !isIPhoneOSVersionLT(3, 2);
100 else
101 return !isMacosxVersionLT(10, 6);
102}
103
Chris Lattner5f9e2722011-07-23 10:55:15 +0000104static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000105 return llvm::StringSwitch<const char*>(Value)
106 .Case("armv6k", "armv6")
107 .Case("armv5tej", "armv5")
108 .Case("xscale", "xscale")
109 .Case("armv4t", "armv4t")
110 .Case("armv7", "armv7")
111 .Cases("armv7a", "armv7-a", "armv7")
112 .Cases("armv7r", "armv7-r", "armv7")
113 .Cases("armv7m", "armv7-m", "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000114 .Cases("armv7f", "armv7-f", "armv7f")
115 .Cases("armv7k", "armv7-k", "armv7k")
116 .Cases("armv7s", "armv7-s", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000117 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000118}
119
Chris Lattner5f9e2722011-07-23 10:55:15 +0000120static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000121 return llvm::StringSwitch<const char *>(Value)
122 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
123 .Cases("arm10e", "arm10tdmi", "armv5")
124 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
125 .Case("xscale", "xscale")
126 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s",
127 "arm1176jzf-s", "cortex-m0", "armv6")
Silviu Baranga2df67ea2012-09-13 15:06:00 +0000128 .Cases("cortex-a8", "cortex-r4", "cortex-m3", "cortex-a9", "cortex-a15",
129 "armv7")
Bob Wilson336bfa32012-09-29 23:52:50 +0000130 .Case("cortex-a9-mp", "armv7f")
131 .Case("swift", "armv7s")
Bob Wilsona59956b2011-10-07 00:37:57 +0000132 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000133}
134
Chris Lattner5f9e2722011-07-23 10:55:15 +0000135StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000136 switch (getTriple().getArch()) {
137 default:
138 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000139
Douglas Gregorf0594d82011-03-06 19:11:49 +0000140 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000141 case llvm::Triple::arm: {
142 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
143 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
144 return Arch;
145
146 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
147 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
148 return Arch;
149
150 return "arm";
151 }
152 }
153}
154
Daniel Dunbarf3955282009-09-04 18:34:51 +0000155Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000156 // Free tool implementations.
157 for (llvm::DenseMap<unsigned, Tool*>::iterator
158 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
159 delete it->second;
160}
161
Chad Rosier61ab80a2011-09-20 20:44:06 +0000162std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
163 types::ID InputType) const {
164 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000165
166 // If the target isn't initialized (e.g., an unknown Darwin platform, return
167 // the default triple).
168 if (!isTargetInitialized())
169 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000170
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000171 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000172 Str += isTargetIPhoneOS() ? "ios" : "macosx";
173 Str += getTargetVersion().getAsString();
174 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000175
176 return Triple.getTriple();
177}
178
David Blaikie99ba9e32011-12-20 02:48:34 +0000179void Generic_ELF::anchor() {}
180
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000181Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
182 const ActionList &Inputs) const {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000183 Action::ActionClass Key = JA.getKind();
184 bool useClang = false;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000185
186 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000187 useClang = true;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000188 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000189 if (!getDriver().shouldForceClangUse() &&
190 Inputs.size() == 1 &&
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000191 types::isCXX(Inputs[0]->getType()) &&
Bob Wilson905c45f2011-10-14 05:03:44 +0000192 getTriple().isOSDarwin() &&
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000193 getTriple().getArch() == llvm::Triple::x86 &&
Bob Wilsona544aee2011-08-13 23:48:55 +0000194 (C.getArgs().getLastArg(options::OPT_fapple_kext) ||
195 C.getArgs().getLastArg(options::OPT_mkernel)))
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000196 useClang = false;
197 }
198
199 // FIXME: This seems like a hacky way to choose clang frontend.
200 if (useClang)
201 Key = Action::AnalyzeJobClass;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000202
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000203 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
204 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000205 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000206
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000207 Tool *&T = Tools[Key];
208 if (!T) {
209 switch (Key) {
210 case Action::InputClass:
211 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000212 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000213 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000214 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000215 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000216 case Action::MigrateJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000217 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000218 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000219 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000220 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000221 case Action::AssembleJobClass: {
222 if (UseIntegratedAs)
223 T = new tools::ClangAs(*this);
224 else
225 T = new tools::darwin::Assemble(*this);
226 break;
227 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000228 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000229 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000230 case Action::LipoJobClass:
231 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000232 case Action::DsymutilJobClass:
233 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000234 case Action::VerifyJobClass:
235 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000236 }
237 }
238
239 return *T;
240}
241
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000242
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000243DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
244 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000245{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000246 getProgramPaths().push_back(getDriver().getInstalledDir());
247 if (getDriver().getInstalledDir() != getDriver().Dir)
248 getProgramPaths().push_back(getDriver().Dir);
249
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000250 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000251 getProgramPaths().push_back(getDriver().getInstalledDir());
252 if (getDriver().getInstalledDir() != getDriver().Dir)
253 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000254
255 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000256 // also add the GCC libexec paths. This is legacy code that can be removed
257 // once fallback is no longer useful.
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000258 AddGCCLibexecPath(DarwinVersion[0]);
259 AddGCCLibexecPath(DarwinVersion[0] - 2);
260 AddGCCLibexecPath(DarwinVersion[0] - 1);
261 AddGCCLibexecPath(DarwinVersion[0] + 1);
262 AddGCCLibexecPath(DarwinVersion[0] + 2);
263}
264
265void DarwinClang::AddGCCLibexecPath(unsigned darwinVersion) {
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000266 std::string ToolChainDir = "i686-apple-darwin";
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000267 ToolChainDir += llvm::utostr(darwinVersion);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000268 ToolChainDir += "/4.2.1";
269
270 std::string Path = getDriver().Dir;
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000271 Path += "/../llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000272 Path += ToolChainDir;
273 getProgramPaths().push_back(Path);
274
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000275 Path = "/usr/llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000276 Path += ToolChainDir;
277 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000278}
279
John McCallf85e1932011-06-15 23:02:42 +0000280void DarwinClang::AddLinkARCArgs(const ArgList &Args,
281 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000282
283 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000284 llvm::sys::Path P(getDriver().ClangExecutable);
285 P.eraseComponent(); // 'clang'
286 P.eraseComponent(); // 'bin'
287 P.appendComponent("lib");
288 P.appendComponent("arc");
289 P.appendComponent("libarclite_");
290 std::string s = P.str();
291 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000292 if (isTargetIOSSimulator())
293 s += "iphonesimulator";
294 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000295 s += "iphoneos";
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000296 // FIXME: Remove this once we depend fully on -mios-simulator-version-min.
John McCall260611a2012-06-20 06:18:46 +0000297 else if (TargetSimulatorVersionFromDefines != VersionTuple())
John McCallf85e1932011-06-15 23:02:42 +0000298 s += "iphonesimulator";
299 else
300 s += "macosx";
301 s += ".a";
302
303 CmdArgs.push_back(Args.MakeArgString(s));
304}
305
Eric Christopher3404fe72011-06-22 17:41:40 +0000306void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000307 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000308 const char *DarwinStaticLib) const {
309 llvm::sys::Path P(getDriver().ResourceDir);
310 P.appendComponent("lib");
311 P.appendComponent("darwin");
312 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000313
Eric Christopher3404fe72011-06-22 17:41:40 +0000314 // For now, allow missing resource libraries to support developers who may
315 // not have compiler-rt checked out or integrated into their build.
316 bool Exists;
317 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
318 CmdArgs.push_back(Args.MakeArgString(P.str()));
319}
320
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000321void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
322 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000323 // Darwin only supports the compiler-rt based runtime libraries.
324 switch (GetRuntimeLibType(Args)) {
325 case ToolChain::RLT_CompilerRT:
326 break;
327 default:
328 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
329 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue(Args) << "darwin";
330 return;
331 }
332
Daniel Dunbareec99102010-01-22 03:38:14 +0000333 // Darwin doesn't support real static executables, don't link any runtime
334 // libraries with -static.
Nico Weberdc0d4e22012-10-15 20:37:01 +0000335 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000336 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000337
338 // Reject -static-libgcc for now, we can deal with this when and if someone
339 // cares. This is useful in situations where someone wants to statically link
340 // something like libstdc++, and needs its runtime support routines.
341 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000342 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000343 << A->getAsString(Args);
344 return;
345 }
346
Daniel Dunbarf4714872011-11-17 00:36:57 +0000347 // If we are building profile support, link that library in.
348 if (Args.hasArg(options::OPT_fprofile_arcs) ||
349 Args.hasArg(options::OPT_fprofile_generate) ||
350 Args.hasArg(options::OPT_fcreate_profile) ||
351 Args.hasArg(options::OPT_coverage)) {
352 // Select the appropriate runtime library for the target.
353 if (isTargetIPhoneOS()) {
354 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
355 } else {
356 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
357 }
358 }
359
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000360 // Add ASAN runtime library, if required. Dynamic libraries and bundles
361 // should not be linked with the runtime library.
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000362 if (Args.hasFlag(options::OPT_faddress_sanitizer,
363 options::OPT_fno_address_sanitizer, false)) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000364 if (Args.hasArg(options::OPT_dynamiclib) ||
365 Args.hasArg(options::OPT_bundle)) return;
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000366 if (isTargetIPhoneOS()) {
367 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
368 << "-faddress-sanitizer";
369 } else {
370 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
371
372 // The ASAN runtime library requires C++ and CoreFoundation.
373 AddCXXStdlibLibArgs(Args, CmdArgs);
374 CmdArgs.push_back("-framework");
375 CmdArgs.push_back("CoreFoundation");
376 }
377 }
378
Daniel Dunbareec99102010-01-22 03:38:14 +0000379 // Otherwise link libSystem, then the dynamic runtime library, and finally any
380 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000381 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000382
383 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000384 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000385 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
386 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000387 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
388 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
389 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000390
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000391 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000392 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000393 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000394 // The dynamic runtime library was merged with libSystem for 10.6 and
395 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000396 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000397 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000398 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000399 CmdArgs.push_back("-lgcc_s.10.5");
400
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000401 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000402 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000403 // omitted from 10.4.dylib.
404 //
405 // Unfortunately, that turned out to not be true, because Darwin system
406 // headers can still use eprintf on i386, and it is not exported from
407 // libSystem. Therefore, we still must provide a runtime library just for
408 // the tiny tiny handful of projects that *might* use that symbol.
409 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000410 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000411 } else {
412 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000413 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
414 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000415 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000416 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000417}
418
Argyrios Kyrtzidisdceb11f2011-10-18 00:22:49 +0000419static inline StringRef SimulatorVersionDefineName() {
420 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
421}
422
423/// \brief Parse the simulator version define:
424/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
425// and return the grouped values as integers, e.g:
426// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
427// will return Major=4, Minor=2, Micro=1.
428static bool GetVersionFromSimulatorDefine(StringRef define,
429 unsigned &Major, unsigned &Minor,
430 unsigned &Micro) {
431 assert(define.startswith(SimulatorVersionDefineName()));
432 StringRef name, version;
433 llvm::tie(name, version) = define.split('=');
434 if (version.empty())
435 return false;
436 std::string verstr = version.str();
437 char *end;
438 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
439 if (*end != '\0')
440 return false;
441 Major = num / 10000;
442 num = num % 10000;
443 Minor = num / 100;
444 Micro = num % 100;
445 return true;
446}
447
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000448void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000449 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000450
Daniel Dunbar9101bc52012-08-17 18:43:50 +0000451 // Support allowing the SDKROOT environment variable used by xcrun and other
452 // Xcode tools to define the default sysroot, by making it the default for
453 // isysroot.
454 if (!Args.hasArg(options::OPT_isysroot)) {
455 if (char *env = ::getenv("SDKROOT")) {
456 // We only use this value as the default if it is an absolute path and
457 // exists.
458 if (llvm::sys::path::is_absolute(env) && llvm::sys::fs::exists(env)) {
459 Args.append(Args.MakeSeparateArg(
460 0, Opts.getOption(options::OPT_isysroot), env));
461 }
462 }
463 }
464
Daniel Dunbar26031372010-01-27 00:56:25 +0000465 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000466 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
467 Arg *iOSSimVersion = Args.getLastArg(
468 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000469
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000470 // FIXME: HACK! When compiling for the simulator we don't get a
471 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
472 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
473 // define passed in command-line.
474 if (!iOSVersion && !iOSSimVersion) {
475 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
476 ie = Args.filtered_end(); it != ie; ++it) {
477 StringRef define = (*it)->getValue(Args);
478 if (define.startswith(SimulatorVersionDefineName())) {
479 unsigned Major = 0, Minor = 0, Micro = 0;
480 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
481 Major < 10 && Minor < 100 && Micro < 100) {
John McCall260611a2012-06-20 06:18:46 +0000482 TargetSimulatorVersionFromDefines = VersionTuple(Major, Minor, Micro);
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000483 }
Bob Wilsona1ec3db2012-07-19 01:35:55 +0000484 // When using the define to indicate the simulator, we force
485 // 10.6 macosx target.
Eric Christopherbc0a9252012-10-10 22:34:46 +0000486 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Bob Wilsona1ec3db2012-07-19 01:35:55 +0000487 OSXVersion = Args.MakeJoinedArg(0, O, "10.6");
488 Args.append(OSXVersion);
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000489 break;
490 }
491 }
492 }
493
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000494 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000495 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000496 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000497 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
498 iOSVersion = iOSSimVersion = 0;
499 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000500 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000501 << iOSVersion->getAsString(Args)
502 << iOSSimVersion->getAsString(Args);
503 iOSSimVersion = 0;
504 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000505 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000506 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000507 StringRef OSXTarget;
508 StringRef iOSTarget;
509 StringRef iOSSimTarget;
510 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
511 OSXTarget = env;
512 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
513 iOSTarget = env;
514 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
515 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000516
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000517 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000518 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000519 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000520 if (iOSTarget.empty()) {
521 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
522 StringRef first, second;
523 StringRef isysroot = A->getValue(Args);
524 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
525 if (second != "")
526 iOSTarget = second.substr(0,3);
527 }
528 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000529
Chad Rosier4f8de272011-09-28 00:46:32 +0000530 // If no OSX or iOS target has been specified and we're compiling for armv7,
531 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000532 if (OSXTarget.empty() && iOSTarget.empty() &&
Bob Wilson336bfa32012-09-29 23:52:50 +0000533 (getDarwinArchName(Args) == "armv7" ||
534 getDarwinArchName(Args) == "armv7s"))
Chad Rosier87ca5582012-05-09 18:09:58 +0000535 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000536
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000537 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000538 //
539 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000540
541 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000542 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000543 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000544 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000545 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000546 "IPHONEOS_DEPLOYMENT_TARGET");
547 }
548
549 // Allow conflicts among OSX and iOS for historical reasons, but choose the
550 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000551 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000552 if (getTriple().getArch() == llvm::Triple::arm ||
553 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000554 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000555 else
Chad Rosiera4884972011-08-31 20:56:25 +0000556 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000557 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000558
Chad Rosiera4884972011-08-31 20:56:25 +0000559 if (!OSXTarget.empty()) {
Eric Christopherbc0a9252012-10-10 22:34:46 +0000560 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000561 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
562 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000563 } else if (!iOSTarget.empty()) {
Eric Christopherbc0a9252012-10-10 22:34:46 +0000564 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000565 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
566 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000567 } else if (!iOSSimTarget.empty()) {
Eric Christopherbc0a9252012-10-10 22:34:46 +0000568 const Option *O = Opts.getOption(
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000569 options::OPT_mios_simulator_version_min_EQ);
570 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
571 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000572 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000573 // Otherwise, assume we are targeting OS X.
Eric Christopherbc0a9252012-10-10 22:34:46 +0000574 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000575 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
576 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000577 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000580 // Reject invalid architecture combinations.
581 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
582 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000583 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000584 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
585 }
586
Daniel Dunbar26031372010-01-27 00:56:25 +0000587 // Set the tool chain target information.
588 unsigned Major, Minor, Micro;
589 bool HadExtra;
590 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000591 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar26031372010-01-27 00:56:25 +0000592 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
593 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000594 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000595 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000596 << OSXVersion->getAsString(Args);
597 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000598 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
599 assert(Version && "Unknown target platform!");
Eli Friedman983d8352012-01-11 02:41:15 +0000600 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
601 Micro, HadExtra) || HadExtra ||
602 Major >= 10 || Minor >= 100 || Micro >= 100)
603 getDriver().Diag(diag::err_drv_invalid_version_number)
604 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000605 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000606
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000607 bool IsIOSSim = bool(iOSSimVersion);
608
609 // In GCC, the simulator historically was treated as being OS X in some
610 // contexts, like determining the link logic, despite generally being called
611 // with an iOS deployment target. For compatibility, we detect the
612 // simulator as iOS + x86, and treat it differently in a few contexts.
613 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
614 getTriple().getArch() == llvm::Triple::x86_64))
615 IsIOSSim = true;
616
617 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000618}
619
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000620void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000621 ArgStringList &CmdArgs) const {
622 CXXStdlibType Type = GetCXXStdlibType(Args);
623
624 switch (Type) {
625 case ToolChain::CST_Libcxx:
626 CmdArgs.push_back("-lc++");
627 break;
628
629 case ToolChain::CST_Libstdcxx: {
630 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
631 // it was previously found in the gcc lib dir. However, for all the Darwin
632 // platforms we care about it was -lstdc++.6, so we search for that
633 // explicitly if we can't see an obvious -lstdc++ candidate.
634
635 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000636 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000637 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
638 llvm::sys::Path P(A->getValue(Args));
639 P.appendComponent("usr");
640 P.appendComponent("lib");
641 P.appendComponent("libstdc++.dylib");
642
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000643 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000644 P.eraseComponent();
645 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000646 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000647 CmdArgs.push_back(Args.MakeArgString(P.str()));
648 return;
649 }
650 }
651 }
652
653 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000654 // FIXME: This should be removed someday when we don't have to care about
655 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000656 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
657 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000658 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
659 return;
660 }
661
662 // Otherwise, let the linker search.
663 CmdArgs.push_back("-lstdc++");
664 break;
665 }
666 }
667}
668
Shantonu Sen7433fed2010-09-17 18:39:08 +0000669void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
670 ArgStringList &CmdArgs) const {
671
672 // For Darwin platforms, use the compiler-rt-based support library
673 // instead of the gcc-provided one (which is also incidentally
674 // only present in the gcc lib dir, which makes it hard to find).
675
676 llvm::sys::Path P(getDriver().ResourceDir);
677 P.appendComponent("lib");
678 P.appendComponent("darwin");
Nico Weberdc0d4e22012-10-15 20:37:01 +0000679 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000680
Shantonu Sen7433fed2010-09-17 18:39:08 +0000681 // For now, allow missing resource libraries to support developers who may
682 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000683 bool Exists;
684 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000685 CmdArgs.push_back(Args.MakeArgString(P.str()));
686}
687
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000688DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
689 const char *BoundArch) const {
690 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
691 const OptTable &Opts = getDriver().getOpts();
692
693 // FIXME: We really want to get out of the tool chain level argument
694 // translation business, as it makes the driver functionality much
695 // more opaque. For now, we follow gcc closely solely for the
696 // purpose of easily achieving feature parity & testability. Once we
697 // have something that works, we should reevaluate each translation
698 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000699
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000700 for (ArgList::const_iterator it = Args.begin(),
701 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000702 Arg *A = *it;
703
704 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000705 // Skip this argument unless the architecture matches either the toolchain
706 // triple arch, or the arch being bound.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000707 llvm::Triple::ArchType XarchArch =
708 llvm::Triple::getArchTypeForDarwinArchName(A->getValue(Args, 0));
709 if (!(XarchArch == getArch() ||
710 (BoundArch && XarchArch ==
711 llvm::Triple::getArchTypeForDarwinArchName(BoundArch))))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000712 continue;
713
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000714 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000715 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
716 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000717 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000719 // If the argument parsing failed or more than one argument was
720 // consumed, the -Xarch_ argument's parameter tried to consume
721 // extra arguments. Emit an error and ignore.
722 //
723 // We also want to disallow any options which would alter the
724 // driver behavior; that isn't going to work in our model. We
725 // use isDriverOption() as an approximation, although things
726 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000727 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000728 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000729 << A->getAsString(Args);
730 continue;
731 } else if (XarchArg->getOption().isDriverOption()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000732 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000733 << A->getAsString(Args);
734 continue;
735 }
736
Daniel Dunbar478edc22009-03-29 22:29:05 +0000737 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000738 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000739
740 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000741
742 // Linker input arguments require custom handling. The problem is that we
743 // have already constructed the phase actions, so we can not treat them as
744 // "input arguments".
745 if (A->getOption().isLinkerInput()) {
746 // Convert the argument into individual Zlinker_input_args.
747 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
748 DAL->AddSeparateArg(OriginalArg,
749 Opts.getOption(options::OPT_Zlinker_input),
750 A->getValue(Args, i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000751
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000752 }
753 continue;
754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000756
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000757 // Sob. These is strictly gcc compatible for the time being. Apple
758 // gcc translates options twice, which means that self-expanding
759 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000760 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000761 default:
762 DAL->append(A);
763 break;
764
765 case options::OPT_mkernel:
766 case options::OPT_fapple_kext:
767 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000768 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000769 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000771 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000772 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
773 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000774 break;
775
776 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000777 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
778 DAL->AddFlagArg(A,
779 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000780 break;
781
782 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000783 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
784 DAL->AddFlagArg(A,
785 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000786 break;
787
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000788 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000789 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000790 break;
791
792 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000793 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000794 break;
795
796 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000797 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000798 break;
799
800 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000801 DAL->AddFlagArg(A,
802 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000803 break;
804
805 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000806 DAL->AddFlagArg(A,
807 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000808 break;
809
810 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000811 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000812 break;
813
814 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000815 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000816 break;
817 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000818 }
819
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000820 if (getTriple().getArch() == llvm::Triple::x86 ||
821 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000822 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000823 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000824
825 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000826 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000827 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000828 StringRef Name = BoundArch;
Eric Christopherbc0a9252012-10-10 22:34:46 +0000829 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
830 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000831
832 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
833 // which defines the list of which architectures we accept.
834 if (Name == "ppc")
835 ;
836 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000837 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000838 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000839 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000840 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000841 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000842 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000843 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000844 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000845 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000846 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000847 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000848 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000849 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000850 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000851 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000852
853 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000854 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000855
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000856 else if (Name == "i386")
857 ;
858 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000859 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000860 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000861 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000862 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000863 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000864 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000865 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000866 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000867 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000868 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000869 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000870 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000871 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000872
873 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000874 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000875
876 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000877 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000878 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000879 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000880 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000881 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000882 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000883 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000884 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000885 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000886 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000887 DAL->AddJoinedArg(0, MArch, "armv7a");
Bob Wilson336bfa32012-09-29 23:52:50 +0000888 else if (Name == "armv7f")
889 DAL->AddJoinedArg(0, MArch, "armv7f");
890 else if (Name == "armv7k")
891 DAL->AddJoinedArg(0, MArch, "armv7k");
892 else if (Name == "armv7s")
893 DAL->AddJoinedArg(0, MArch, "armv7s");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000894
895 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000896 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000897 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000898
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000899 // Add an explicit version min argument for the deployment target. We do this
900 // after argument translation because -Xarch_ arguments may add a version min
901 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000902 if (BoundArch)
903 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000904
Bob Wilson163b1512011-10-07 17:54:41 +0000905 // Validate the C++ standard library choice.
906 CXXStdlibType Type = GetCXXStdlibType(*DAL);
907 if (Type == ToolChain::CST_Libcxx) {
John McCall260611a2012-06-20 06:18:46 +0000908 // Check whether the target provides libc++.
909 StringRef where;
910
911 // Complain about targetting iOS < 5.0 in any way.
John McCalle4860152012-06-21 17:46:38 +0000912 if (TargetSimulatorVersionFromDefines != VersionTuple()) {
913 if (TargetSimulatorVersionFromDefines < VersionTuple(5, 0))
914 where = "iOS 5.0";
915 } else if (isTargetIPhoneOS()) {
916 if (isIPhoneOSVersionLT(5, 0))
917 where = "iOS 5.0";
John McCall260611a2012-06-20 06:18:46 +0000918 }
919
920 if (where != StringRef()) {
Bob Wilson163b1512011-10-07 17:54:41 +0000921 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
John McCall260611a2012-06-20 06:18:46 +0000922 << where;
Bob Wilson163b1512011-10-07 17:54:41 +0000923 }
924 }
925
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000926 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000927}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000928
Daniel Dunbarf3955282009-09-04 18:34:51 +0000929bool Darwin::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +0000930 return getArch() == llvm::Triple::x86_64;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000931}
932
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000933bool Darwin::UseDwarfDebugFlags() const {
934 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
935 return S[0] != '\0';
936 return false;
937}
938
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000939bool Darwin::UseSjLjExceptions() const {
940 // Darwin uses SjLj exceptions on ARM.
941 return (getTriple().getArch() == llvm::Triple::arm ||
942 getTriple().getArch() == llvm::Triple::thumb);
943}
944
Daniel Dunbarf3955282009-09-04 18:34:51 +0000945const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000946 return "pic";
947}
948
Daniel Dunbarf3955282009-09-04 18:34:51 +0000949const char *Darwin::GetForcedPicModel() const {
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000950 if (getArch() == llvm::Triple::x86_64)
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000951 return "pic";
952 return 0;
953}
954
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000955bool Darwin::SupportsProfiling() const {
956 // Profiling instrumentation is only supported on x86.
Rafael Espindola64f7ad92012-10-07 04:44:33 +0000957 return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000958}
959
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000960bool Darwin::SupportsObjCGC() const {
961 // Garbage collection is supported everywhere except on iPhone OS.
962 return !isTargetIPhoneOS();
963}
964
John McCall0a7dd782012-08-21 02:47:43 +0000965void Darwin::CheckObjCARC() const {
966 if (isTargetIPhoneOS() || !isMacosxVersionLT(10, 6))
967 return;
John McCall80fd37a2012-08-27 01:56:21 +0000968 getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000969}
970
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000971std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000972Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
973 types::ID InputType) const {
974 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000975}
976
Daniel Dunbar39176082009-03-20 00:20:03 +0000977/// Generic_GCC - A tool chain using the 'gcc' command to perform
978/// all subcommands; this relies on gcc translating the majority of
979/// command line options.
980
Chandler Carruth19347ed2011-11-06 23:39:34 +0000981/// \brief Parse a GCCVersion object out of a string of text.
982///
983/// This is the primary means of forming GCCVersion objects.
984/*static*/
985Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
986 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
987 std::pair<StringRef, StringRef> First = VersionText.split('.');
988 std::pair<StringRef, StringRef> Second = First.second.split('.');
989
990 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
991 if (First.first.getAsInteger(10, GoodVersion.Major) ||
992 GoodVersion.Major < 0)
993 return BadVersion;
994 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
995 GoodVersion.Minor < 0)
996 return BadVersion;
997
998 // First look for a number prefix and parse that if present. Otherwise just
999 // stash the entire patch string in the suffix, and leave the number
1000 // unspecified. This covers versions strings such as:
1001 // 4.4
1002 // 4.4.0
1003 // 4.4.x
1004 // 4.4.2-rc4
1005 // 4.4.x-patched
1006 // And retains any patch number it finds.
1007 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1008 if (!PatchText.empty()) {
1009 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
1010 // Try to parse the number and any suffix.
1011 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1012 GoodVersion.Patch < 0)
1013 return BadVersion;
1014 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
1015 }
1016 }
1017
1018 return GoodVersion;
1019}
1020
1021/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
1022bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
1023 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
1024 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
1025
1026 // Note that we rank versions with *no* patch specified is better than ones
1027 // hard-coding a patch version. Thus if the RHS has no patch, it always
1028 // wins, and the LHS only wins if it has no patch and the RHS does have
1029 // a patch.
1030 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
1031 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
Gabor Greif241cbe42012-04-18 10:59:08 +00001032 if (PatchSuffix == RHS.PatchSuffix) return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001033
1034 // Finally, between completely tied version numbers, the version with the
1035 // suffix loses as we prefer full releases.
1036 if (RHS.PatchSuffix.empty()) return true;
1037 return false;
1038}
1039
Rafael Espindola0e659592012-02-19 01:38:32 +00001040static StringRef getGCCToolchainDir(const ArgList &Args) {
1041 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1042 if (A)
1043 return A->getValue(Args);
1044 return GCC_INSTALL_PREFIX;
1045}
1046
Chandler Carruth19347ed2011-11-06 23:39:34 +00001047/// \brief Construct a GCCInstallationDetector from the driver.
1048///
1049/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +00001050/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001051///
1052/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1053/// should instead pull the target out of the driver. This is currently
1054/// necessary because the driver doesn't store the final version of the target
1055/// triple.
1056Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1057 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001058 const llvm::Triple &TargetTriple,
1059 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001060 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001061 llvm::Triple MultiarchTriple
1062 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1063 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001064 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001065 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001066 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001067 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001068 SmallVector<StringRef, 10> CandidateTripleAliases;
1069 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1070 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1071 CandidateTripleAliases,
1072 CandidateMultiarchLibDirs,
1073 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001074
1075 // Compute the set of prefixes for our search.
1076 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1077 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001078
Rafael Espindola0e659592012-02-19 01:38:32 +00001079 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1080 if (GCCToolchainDir != "") {
1081 if (GCCToolchainDir.back() == '/')
1082 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001083
Rafael Espindola0e659592012-02-19 01:38:32 +00001084 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001085 } else {
1086 Prefixes.push_back(D.SysRoot);
1087 Prefixes.push_back(D.SysRoot + "/usr");
1088 Prefixes.push_back(D.InstalledDir + "/..");
1089 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001090
1091 // Loop over the various components which exist and select the best GCC
1092 // installation available. GCC installs are ranked by version number.
1093 Version = GCCVersion::Parse("0.0.0");
1094 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1095 if (!llvm::sys::fs::exists(Prefixes[i]))
1096 continue;
1097 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1098 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1099 if (!llvm::sys::fs::exists(LibDir))
1100 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001101 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
1102 ScanLibDirForGCCTriple(TargetArch, LibDir, CandidateTripleAliases[k]);
1103 }
1104 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1105 const std::string LibDir
1106 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1107 if (!llvm::sys::fs::exists(LibDir))
1108 continue;
1109 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1110 ++k)
1111 ScanLibDirForGCCTriple(TargetArch, LibDir,
1112 CandidateMultiarchTripleAliases[k],
1113 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001114 }
1115 }
1116}
1117
1118/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001119 const llvm::Triple &TargetTriple,
1120 const llvm::Triple &MultiarchTriple,
1121 SmallVectorImpl<StringRef> &LibDirs,
1122 SmallVectorImpl<StringRef> &TripleAliases,
1123 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1124 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1125 // Declare a bunch of static data sets that we'll select between below. These
1126 // are specifically designed to always refer to string literals to avoid any
1127 // lifetime or initialization issues.
1128 static const char *const ARMLibDirs[] = { "/lib" };
1129 static const char *const ARMTriples[] = {
1130 "arm-linux-gnueabi",
1131 "arm-linux-androideabi"
1132 };
Jiangning Liuff104a12012-07-31 08:06:29 +00001133 static const char *const ARMHFTriples[] = {
1134 "arm-linux-gnueabihf",
1135 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001136
1137 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1138 static const char *const X86_64Triples[] = {
1139 "x86_64-linux-gnu",
1140 "x86_64-unknown-linux-gnu",
1141 "x86_64-pc-linux-gnu",
1142 "x86_64-redhat-linux6E",
1143 "x86_64-redhat-linux",
1144 "x86_64-suse-linux",
1145 "x86_64-manbo-linux-gnu",
1146 "x86_64-linux-gnu",
1147 "x86_64-slackware-linux"
1148 };
1149 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1150 static const char *const X86Triples[] = {
1151 "i686-linux-gnu",
1152 "i686-pc-linux-gnu",
1153 "i486-linux-gnu",
1154 "i386-linux-gnu",
1155 "i686-redhat-linux",
1156 "i586-redhat-linux",
1157 "i386-redhat-linux",
1158 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001159 "i486-slackware-linux",
1160 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001161 };
1162
1163 static const char *const MIPSLibDirs[] = { "/lib" };
1164 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1165 static const char *const MIPSELLibDirs[] = { "/lib" };
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00001166 static const char *const MIPSELTriples[] = {
1167 "mipsel-linux-gnu",
1168 "mipsel-linux-android"
1169 };
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001170
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001171 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1172 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1173 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1174 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1175
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001176 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1177 static const char *const PPCTriples[] = {
1178 "powerpc-linux-gnu",
1179 "powerpc-unknown-linux-gnu",
Gabor Greif91720912012-05-15 11:21:03 +00001180 "powerpc-suse-linux",
1181 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001182 };
1183 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1184 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001185 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001186 "powerpc64-unknown-linux-gnu",
1187 "powerpc64-suse-linux",
1188 "ppc64-redhat-linux"
1189 };
1190
1191 switch (TargetTriple.getArch()) {
1192 case llvm::Triple::arm:
1193 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001194 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Jiangning Liuff104a12012-07-31 08:06:29 +00001195 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
1196 TripleAliases.append(
1197 ARMHFTriples, ARMHFTriples + llvm::array_lengthof(ARMHFTriples));
1198 } else {
1199 TripleAliases.append(
1200 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1201 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001202 break;
1203 case llvm::Triple::x86_64:
1204 LibDirs.append(
1205 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1206 TripleAliases.append(
1207 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1208 MultiarchLibDirs.append(
1209 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1210 MultiarchTripleAliases.append(
1211 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1212 break;
1213 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001214 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001215 TripleAliases.append(
1216 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1217 MultiarchLibDirs.append(
1218 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1219 MultiarchTripleAliases.append(
1220 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1221 break;
1222 case llvm::Triple::mips:
1223 LibDirs.append(
1224 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1225 TripleAliases.append(
1226 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001227 MultiarchLibDirs.append(
1228 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1229 MultiarchTripleAliases.append(
1230 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001231 break;
1232 case llvm::Triple::mipsel:
1233 LibDirs.append(
1234 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1235 TripleAliases.append(
1236 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001237 MultiarchLibDirs.append(
1238 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1239 MultiarchTripleAliases.append(
1240 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1241 break;
1242 case llvm::Triple::mips64:
1243 LibDirs.append(
1244 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1245 TripleAliases.append(
1246 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1247 MultiarchLibDirs.append(
1248 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1249 MultiarchTripleAliases.append(
1250 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1251 break;
1252 case llvm::Triple::mips64el:
1253 LibDirs.append(
1254 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1255 TripleAliases.append(
1256 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1257 MultiarchLibDirs.append(
1258 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1259 MultiarchTripleAliases.append(
1260 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001261 break;
1262 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001263 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001264 TripleAliases.append(
1265 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1266 MultiarchLibDirs.append(
1267 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1268 MultiarchTripleAliases.append(
1269 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1270 break;
1271 case llvm::Triple::ppc64:
1272 LibDirs.append(
1273 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1274 TripleAliases.append(
1275 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1276 MultiarchLibDirs.append(
1277 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1278 MultiarchTripleAliases.append(
1279 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1280 break;
1281
1282 default:
1283 // By default, just rely on the standard lib directories and the original
1284 // triple.
1285 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001286 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001287
1288 // Always append the drivers target triple to the end, in case it doesn't
1289 // match any of our aliases.
1290 TripleAliases.push_back(TargetTriple.str());
1291
1292 // Also include the multiarch variant if it's different.
1293 if (TargetTriple.str() != MultiarchTriple.str())
1294 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001295}
1296
1297void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001298 llvm::Triple::ArchType TargetArch, const std::string &LibDir,
1299 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001300 // There are various different suffixes involving the triple we
1301 // check for. We also record what is necessary to walk from each back
1302 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001303 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001304 "/gcc/" + CandidateTriple.str(),
1305 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1306
Hal Finkel02014b42012-09-18 22:25:07 +00001307 // The Freescale PPC SDK has the gcc libraries in
1308 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well.
1309 "/" + CandidateTriple.str(),
1310
Chandler Carruth19347ed2011-11-06 23:39:34 +00001311 // Ubuntu has a strange mis-matched pair of triples that this happens to
1312 // match.
1313 // FIXME: It may be worthwhile to generalize this and look for a second
1314 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001315 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001316 };
1317 const std::string InstallSuffixes[] = {
1318 "/../../..",
1319 "/../../../..",
Hal Finkel02014b42012-09-18 22:25:07 +00001320 "/../..",
Chandler Carruth19347ed2011-11-06 23:39:34 +00001321 "/../../../.."
1322 };
1323 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001324 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1325 (TargetArch != llvm::Triple::x86));
1326 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1327 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001328 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001329 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001330 !EC && LI != LE; LI = LI.increment(EC)) {
1331 StringRef VersionText = llvm::sys::path::filename(LI->path());
1332 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1333 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1334 if (CandidateVersion < MinVersion)
1335 continue;
1336 if (CandidateVersion <= Version)
1337 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001338
1339 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001340 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001341 // libs in a subdirectory named 64. The simple logic we follow is that
1342 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1343 // we use that. If not, and if not a multiarch triple, we look for
1344 // crtbegin.o without the subdirectory.
1345 StringRef MultiarchSuffix
1346 = (TargetArch == llvm::Triple::x86_64 ||
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001347 TargetArch == llvm::Triple::ppc64 ||
1348 TargetArch == llvm::Triple::mips64 ||
1349 TargetArch == llvm::Triple::mips64el) ? "/64" : "/32";
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001350 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1351 GCCMultiarchSuffix = MultiarchSuffix.str();
1352 } else {
1353 if (NeedsMultiarchSuffix ||
1354 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1355 continue;
1356 GCCMultiarchSuffix.clear();
1357 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001358
1359 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001360 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001361 // FIXME: We hack together the directory name here instead of
1362 // using LI to ensure stable path separators across Windows and
1363 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001364 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001365 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001366 IsValid = true;
1367 }
1368 }
1369}
1370
Rafael Espindola0e659592012-02-19 01:38:32 +00001371Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1372 const ArgList &Args)
1373 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001374 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001375 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001376 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001377}
1378
Daniel Dunbar39176082009-03-20 00:20:03 +00001379Generic_GCC::~Generic_GCC() {
1380 // Free tool implementations.
1381 for (llvm::DenseMap<unsigned, Tool*>::iterator
1382 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1383 delete it->second;
1384}
1385
Mike Stump1eb44332009-09-09 15:08:12 +00001386Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001387 const JobAction &JA,
1388 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +00001389 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001390 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +00001391 Key = Action::AnalyzeJobClass;
1392 else
1393 Key = JA.getKind();
1394
1395 Tool *&T = Tools[Key];
1396 if (!T) {
1397 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001398 case Action::InputClass:
1399 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001400 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001401 case Action::PreprocessJobClass:
1402 T = new tools::gcc::Preprocess(*this); break;
1403 case Action::PrecompileJobClass:
1404 T = new tools::gcc::Precompile(*this); break;
1405 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001406 case Action::MigrateJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001407 T = new tools::Clang(*this); break;
1408 case Action::CompileJobClass:
1409 T = new tools::gcc::Compile(*this); break;
1410 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 {
1459 Action::ActionClass Key;
1460 // if (JA.getKind () == Action::CompileJobClass)
1461 // Key = JA.getKind ();
1462 // else
1463
1464 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1465 Key = Action::AnalyzeJobClass;
1466 else
1467 Key = JA.getKind();
1468 // if ((JA.getKind () == Action::CompileJobClass)
1469 // && (JA.getType () != types::TY_LTO_BC)) {
1470 // Key = JA.getKind ();
1471 // }
1472
1473 Tool *&T = Tools[Key];
1474 if (!T) {
1475 switch (Key) {
1476 case Action::InputClass:
1477 case Action::BindArchClass:
1478 assert(0 && "Invalid tool kind.");
1479 case Action::AnalyzeJobClass:
1480 T = new tools::Clang(*this); break;
1481 case Action::AssembleJobClass:
1482 T = new tools::hexagon::Assemble(*this); break;
1483 case Action::LinkJobClass:
1484 T = new tools::hexagon::Link(*this); break;
1485 default:
1486 assert(false && "Unsupported action for Hexagon target.");
1487 }
1488 }
1489
1490 return *T;
1491}
1492
Tony Linthicum96319392011-12-12 21:14:55 +00001493const char *Hexagon_TC::GetDefaultRelocationModel() const {
1494 return "static";
1495}
1496
1497const char *Hexagon_TC::GetForcedPicModel() const {
1498 return 0;
1499} // End Hexagon
1500
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001501
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001502/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1503/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1504/// Currently does not support anything else but compilation.
1505
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001506TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1507 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001508 // Path mangling to find libexec
1509 std::string Path(getDriver().Dir);
1510
1511 Path += "/../libexec";
1512 getProgramPaths().push_back(Path);
1513}
1514
1515TCEToolChain::~TCEToolChain() {
1516 for (llvm::DenseMap<unsigned, Tool*>::iterator
1517 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1518 delete it->second;
1519}
1520
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001521bool TCEToolChain::IsMathErrnoDefault() const {
1522 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001523}
1524
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001525const char *TCEToolChain::GetDefaultRelocationModel() const {
1526 return "static";
1527}
1528
1529const char *TCEToolChain::GetForcedPicModel() const {
1530 return 0;
1531}
1532
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001533Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001534 const JobAction &JA,
1535 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001536 Action::ActionClass Key;
1537 Key = Action::AnalyzeJobClass;
1538
1539 Tool *&T = Tools[Key];
1540 if (!T) {
1541 switch (Key) {
1542 case Action::PreprocessJobClass:
1543 T = new tools::gcc::Preprocess(*this); break;
1544 case Action::AnalyzeJobClass:
1545 T = new tools::Clang(*this); break;
1546 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001547 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001548 }
1549 }
1550 return *T;
1551}
1552
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001553/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1554
Rafael Espindola0e659592012-02-19 01:38:32 +00001555OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1556 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001557 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001558 getFilePaths().push_back("/usr/lib");
1559}
1560
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001561Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1562 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001563 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001564 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001565 Key = Action::AnalyzeJobClass;
1566 else
1567 Key = JA.getKind();
1568
Rafael Espindoladda5b922010-11-07 23:13:01 +00001569 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1570 options::OPT_no_integrated_as,
1571 IsIntegratedAssemblerDefault());
1572
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001573 Tool *&T = Tools[Key];
1574 if (!T) {
1575 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001576 case Action::AssembleJobClass: {
1577 if (UseIntegratedAs)
1578 T = new tools::ClangAs(*this);
1579 else
1580 T = new tools::openbsd::Assemble(*this);
1581 break;
1582 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001583 case Action::LinkJobClass:
1584 T = new tools::openbsd::Link(*this); break;
1585 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001586 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001587 }
1588 }
1589
1590 return *T;
1591}
1592
Eli Friedman42f74f22012-08-08 23:57:20 +00001593/// Bitrig - Bitrig tool chain which can call as(1) and ld(1) directly.
1594
1595Bitrig::Bitrig(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1596 : Generic_ELF(D, Triple, Args) {
1597 getFilePaths().push_back(getDriver().Dir + "/../lib");
1598 getFilePaths().push_back("/usr/lib");
1599}
1600
1601Tool &Bitrig::SelectTool(const Compilation &C, const JobAction &JA,
1602 const ActionList &Inputs) const {
1603 Action::ActionClass Key;
1604 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1605 Key = Action::AnalyzeJobClass;
1606 else
1607 Key = JA.getKind();
1608
1609 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1610 options::OPT_no_integrated_as,
1611 IsIntegratedAssemblerDefault());
1612
1613 Tool *&T = Tools[Key];
1614 if (!T) {
1615 switch (Key) {
1616 case Action::AssembleJobClass: {
1617 if (UseIntegratedAs)
1618 T = new tools::ClangAs(*this);
1619 else
1620 T = new tools::bitrig::Assemble(*this);
1621 break;
1622 }
1623 case Action::LinkJobClass:
1624 T = new tools::bitrig::Link(*this); break;
1625 default:
1626 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1627 }
1628 }
1629
1630 return *T;
1631}
1632
1633void Bitrig::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
1634 ArgStringList &CC1Args) const {
1635 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1636 DriverArgs.hasArg(options::OPT_nostdincxx))
1637 return;
1638
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001639 switch (GetCXXStdlibType(DriverArgs)) {
1640 case ToolChain::CST_Libcxx:
1641 addSystemInclude(DriverArgs, CC1Args,
1642 getDriver().SysRoot + "/usr/include/c++/");
1643 break;
1644 case ToolChain::CST_Libstdcxx:
1645 addSystemInclude(DriverArgs, CC1Args,
1646 getDriver().SysRoot + "/usr/include/c++/stdc++");
1647 addSystemInclude(DriverArgs, CC1Args,
1648 getDriver().SysRoot + "/usr/include/c++/stdc++/backward");
Eli Friedman42f74f22012-08-08 23:57:20 +00001649
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001650 StringRef Triple = getTriple().str();
1651 if (Triple.startswith("amd64"))
1652 addSystemInclude(DriverArgs, CC1Args,
1653 getDriver().SysRoot + "/usr/include/c++/stdc++/x86_64" +
1654 Triple.substr(5));
1655 else
1656 addSystemInclude(DriverArgs, CC1Args,
1657 getDriver().SysRoot + "/usr/include/c++/stdc++/" +
1658 Triple);
1659 break;
1660 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001661}
1662
1663void Bitrig::AddCXXStdlibLibArgs(const ArgList &Args,
1664 ArgStringList &CmdArgs) const {
Chandler Carruth8e6881d2012-10-08 21:31:38 +00001665 switch (GetCXXStdlibType(Args)) {
1666 case ToolChain::CST_Libcxx:
1667 CmdArgs.push_back("-lc++");
1668 CmdArgs.push_back("-lcxxrt");
1669 // Include supc++ to provide Unwind until provided by libcxx.
1670 CmdArgs.push_back("-lgcc");
1671 break;
1672 case ToolChain::CST_Libstdcxx:
1673 CmdArgs.push_back("-lstdc++");
1674 break;
1675 }
Eli Friedman42f74f22012-08-08 23:57:20 +00001676}
1677
Daniel Dunbar75358d22009-03-30 21:06:03 +00001678/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1679
Rafael Espindola0e659592012-02-19 01:38:32 +00001680FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1681 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001682
Chandler Carruth24248e32012-01-26 01:35:15 +00001683 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1684 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001685 if ((Triple.getArch() == llvm::Triple::x86 ||
1686 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001687 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001688 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1689 else
1690 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001691}
1692
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001693Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1694 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001695 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001696 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001697 Key = Action::AnalyzeJobClass;
1698 else
1699 Key = JA.getKind();
1700
Roman Divacky67dece72010-11-08 17:46:39 +00001701 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1702 options::OPT_no_integrated_as,
1703 IsIntegratedAssemblerDefault());
1704
Daniel Dunbar75358d22009-03-30 21:06:03 +00001705 Tool *&T = Tools[Key];
1706 if (!T) {
1707 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001708 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001709 if (UseIntegratedAs)
1710 T = new tools::ClangAs(*this);
1711 else
1712 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001713 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001714 case Action::LinkJobClass:
1715 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001716 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001717 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001718 }
1719 }
1720
1721 return *T;
1722}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001723
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001724/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1725
Rafael Espindola0e659592012-02-19 01:38:32 +00001726NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1727 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001728
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001729 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001730 // When targeting a 32-bit platform, try the special directory used on
1731 // 64-bit hosts, and only fall back to the main library directory if that
1732 // doesn't work.
1733 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1734 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001735 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001736 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001737
1738 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001739 }
1740}
1741
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001742Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1743 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001744 Action::ActionClass Key;
1745 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1746 Key = Action::AnalyzeJobClass;
1747 else
1748 Key = JA.getKind();
1749
1750 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1751 options::OPT_no_integrated_as,
1752 IsIntegratedAssemblerDefault());
1753
1754 Tool *&T = Tools[Key];
1755 if (!T) {
1756 switch (Key) {
1757 case Action::AssembleJobClass:
1758 if (UseIntegratedAs)
1759 T = new tools::ClangAs(*this);
1760 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001761 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001762 break;
1763 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001764 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001765 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001766 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001767 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001768 }
1769 }
1770
1771 return *T;
1772}
1773
Chris Lattner38e317d2010-07-07 16:01:42 +00001774/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1775
Rafael Espindola0e659592012-02-19 01:38:32 +00001776Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1777 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001778 getFilePaths().push_back(getDriver().Dir + "/../lib");
1779 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001780}
1781
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001782Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1783 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001784 Action::ActionClass Key;
1785 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1786 Key = Action::AnalyzeJobClass;
1787 else
1788 Key = JA.getKind();
1789
1790 Tool *&T = Tools[Key];
1791 if (!T) {
1792 switch (Key) {
1793 case Action::AssembleJobClass:
1794 T = new tools::minix::Assemble(*this); break;
1795 case Action::LinkJobClass:
1796 T = new tools::minix::Link(*this); break;
1797 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001798 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001799 }
1800 }
1801
1802 return *T;
1803}
1804
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001805/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1806
Rafael Espindola0e659592012-02-19 01:38:32 +00001807AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1808 const ArgList &Args)
1809 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001810
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001811 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001812 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001813 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001814
Daniel Dunbaree788e72009-12-21 18:54:17 +00001815 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001816 getFilePaths().push_back("/usr/lib");
1817 getFilePaths().push_back("/usr/sfw/lib");
1818 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001819 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001820
1821}
1822
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001823Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1824 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001825 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001826 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001827 Key = Action::AnalyzeJobClass;
1828 else
1829 Key = JA.getKind();
1830
1831 Tool *&T = Tools[Key];
1832 if (!T) {
1833 switch (Key) {
1834 case Action::AssembleJobClass:
1835 T = new tools::auroraux::Assemble(*this); break;
1836 case Action::LinkJobClass:
1837 T = new tools::auroraux::Link(*this); break;
1838 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001839 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001840 }
1841 }
1842
1843 return *T;
1844}
1845
David Chisnall31c46902012-02-15 13:39:01 +00001846/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1847
Rafael Espindola0e659592012-02-19 01:38:32 +00001848Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1849 const ArgList &Args)
1850 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001851
1852 getProgramPaths().push_back(getDriver().getInstalledDir());
1853 if (getDriver().getInstalledDir() != getDriver().Dir)
1854 getProgramPaths().push_back(getDriver().Dir);
1855
1856 getFilePaths().push_back(getDriver().Dir + "/../lib");
1857 getFilePaths().push_back("/usr/lib");
1858}
1859
1860Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
1861 const ActionList &Inputs) const {
1862 Action::ActionClass Key;
1863 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1864 Key = Action::AnalyzeJobClass;
1865 else
1866 Key = JA.getKind();
1867
1868 Tool *&T = Tools[Key];
1869 if (!T) {
1870 switch (Key) {
1871 case Action::AssembleJobClass:
1872 T = new tools::solaris::Assemble(*this); break;
1873 case Action::LinkJobClass:
1874 T = new tools::solaris::Link(*this); break;
1875 default:
1876 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1877 }
1878 }
1879
1880 return *T;
1881}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001882
Eli Friedman6b3454a2009-05-26 07:52:18 +00001883/// Linux toolchain (very bare-bones at the moment).
1884
Rafael Espindolac1da9812010-11-07 20:14:31 +00001885enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001886 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001887 DebianLenny,
1888 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001889 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001890 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001891 RHEL4,
1892 RHEL5,
1893 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001894 Fedora13,
1895 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001896 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001897 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001898 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001899 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001900 OpenSuse11_4,
1901 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001902 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00001903 UbuntuHardy,
1904 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001905 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001906 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001907 UbuntuLucid,
1908 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001909 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001910 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001911 UbuntuPrecise,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001912 UnknownDistro
1913};
1914
Chris Lattnerd753b562011-05-22 05:36:06 +00001915static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001916 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1917 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001918}
1919
1920static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001921 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001922}
1923
1924static bool IsDebian(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001925 return Distro >= DebianLenny && Distro <= DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001926}
1927
1928static bool IsUbuntu(enum LinuxDistro Distro) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001929 return Distro >= UbuntuHardy && Distro <= UbuntuPrecise;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001930}
1931
Rafael Espindolac1da9812010-11-07 20:14:31 +00001932static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001933 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001934 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001935 StringRef Data = File.get()->getBuffer();
1936 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001937 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001938 LinuxDistro Version = UnknownDistro;
1939 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
1940 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
1941 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
1942 .Case("hardy", UbuntuHardy)
1943 .Case("intrepid", UbuntuIntrepid)
1944 .Case("jaunty", UbuntuJaunty)
1945 .Case("karmic", UbuntuKarmic)
1946 .Case("lucid", UbuntuLucid)
1947 .Case("maverick", UbuntuMaverick)
1948 .Case("natty", UbuntuNatty)
1949 .Case("oneiric", UbuntuOneiric)
1950 .Case("precise", UbuntuPrecise)
1951 .Default(UnknownDistro);
1952 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001953 }
1954
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001955 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001956 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001957 if (Data.startswith("Fedora release 16"))
1958 return Fedora16;
1959 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00001960 return Fedora15;
1961 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001962 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001963 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001964 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001965 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001966 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001967 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001968 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001969 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001970 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001971 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1972 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001973 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001974 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001975 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1976 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001977 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001978 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001979 return UnknownDistro;
1980 }
1981
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001982 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001983 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001984 if (Data[0] == '5')
1985 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001986 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00001987 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001988 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00001989 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001990 return UnknownDistro;
1991 }
1992
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001993 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
1994 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
1995 .StartsWith("openSUSE 11.3", OpenSuse11_3)
1996 .StartsWith("openSUSE 11.4", OpenSuse11_4)
1997 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001998 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001999 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002000
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00002001 bool Exists;
2002 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00002003 return Exherbo;
2004
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002005 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
2006 return ArchLinux;
2007
Rafael Espindolac1da9812010-11-07 20:14:31 +00002008 return UnknownDistro;
2009}
2010
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002011/// \brief Get our best guess at the multiarch triple for a target.
2012///
2013/// Debian-based systems are starting to use a multiarch setup where they use
2014/// a target-triple directory in the library and header search paths.
2015/// Unfortunately, this triple does not align with the vanilla target triple,
2016/// so we provide a rough mapping here.
2017static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
2018 StringRef SysRoot) {
2019 // For most architectures, just use whatever we have rather than trying to be
2020 // clever.
2021 switch (TargetTriple.getArch()) {
2022 default:
2023 return TargetTriple.str();
2024
2025 // We use the existence of '/lib/<triple>' as a directory to detect some
2026 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00002027 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
2028 // regardless of what the actual target triple is.
Chad Rosier0337efd2012-07-11 19:08:21 +00002029 case llvm::Triple::arm:
2030 case llvm::Triple::thumb:
Jiangning Liuff104a12012-07-31 08:06:29 +00002031 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) {
2032 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabihf"))
2033 return "arm-linux-gnueabihf";
2034 } else {
2035 if (llvm::sys::fs::exists(SysRoot + "/lib/arm-linux-gnueabi"))
2036 return "arm-linux-gnueabi";
2037 }
Chad Rosier0337efd2012-07-11 19:08:21 +00002038 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002039 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002040 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
2041 return "i386-linux-gnu";
2042 return TargetTriple.str();
2043 case llvm::Triple::x86_64:
2044 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
2045 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002046 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00002047 case llvm::Triple::mips:
2048 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
2049 return "mips-linux-gnu";
2050 return TargetTriple.str();
2051 case llvm::Triple::mipsel:
2052 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
2053 return "mipsel-linux-gnu";
2054 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00002055 case llvm::Triple::ppc:
2056 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
2057 return "powerpc-linux-gnu";
2058 return TargetTriple.str();
2059 case llvm::Triple::ppc64:
2060 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
2061 return "powerpc64-linux-gnu";
2062 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002063 }
2064}
2065
Chandler Carruth00646ba2012-01-25 11:24:24 +00002066static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
2067 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
2068}
2069
Simon Atanasyan7a918882012-09-14 11:27:24 +00002070static bool isMipsArch(llvm::Triple::ArchType Arch) {
2071 return Arch == llvm::Triple::mips ||
2072 Arch == llvm::Triple::mipsel ||
2073 Arch == llvm::Triple::mips64 ||
2074 Arch == llvm::Triple::mips64el;
2075}
2076
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002077static bool isMipsR2Arch(llvm::Triple::ArchType Arch,
2078 const ArgList &Args) {
2079 if (Arch != llvm::Triple::mips &&
2080 Arch != llvm::Triple::mipsel)
2081 return false;
2082
2083 Arg *A = Args.getLastArg(options::OPT_march_EQ,
2084 options::OPT_mcpu_EQ,
2085 options::OPT_mips_CPUs_Group);
2086
2087 if (!A)
2088 return false;
2089
2090 if (A->getOption().matches(options::OPT_mips_CPUs_Group))
2091 return A->getOption().matches(options::OPT_mips32r2);
2092
2093 return A->getValue(Args) == StringRef("mips32r2");
2094}
2095
Simon Atanasyan7a918882012-09-14 11:27:24 +00002096static StringRef getMultilibDir(const llvm::Triple &Triple,
2097 const ArgList &Args) {
2098 if (!isMipsArch(Triple.getArch()))
2099 return Triple.isArch32Bit() ? "lib32" : "lib64";
2100
2101 // lib32 directory has a special meaning on MIPS targets.
2102 // It contains N32 ABI binaries. Use this folder if produce
2103 // code for N32 ABI only.
2104 Arg *A = Args.getLastArg(options::OPT_mabi_EQ);
2105 if (A && (A->getValue(Args) == StringRef("n32")))
2106 return "lib32";
2107
2108 return Triple.isArch32Bit() ? "lib" : "lib64";
2109}
2110
Rafael Espindola0e659592012-02-19 01:38:32 +00002111Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
2112 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00002113 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00002114 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002115
Rafael Espindolaab784082011-09-01 16:25:49 +00002116 // OpenSuse stores the linker with the compiler, add that to the search
2117 // path.
2118 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002119 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002120 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002121
2122 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002123
2124 LinuxDistro Distro = DetectLinuxDistro(Arch);
2125
Chris Lattner64a89172011-05-22 16:45:07 +00002126 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002127 ExtraOpts.push_back("-z");
2128 ExtraOpts.push_back("relro");
2129 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002130
Douglas Gregorf0594d82011-03-06 19:11:49 +00002131 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002132 ExtraOpts.push_back("-X");
2133
Logan Chien94a71422012-09-02 09:30:11 +00002134 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002135
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002136 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2137 // and the MIPS ABI require .dynsym to be sorted in different ways.
2138 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2139 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002140 // Android loader does not support .gnu.hash.
Simon Atanasyan7a918882012-09-14 11:27:24 +00002141 if (!isMipsArch(Arch) && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002142 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2143 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002144 ExtraOpts.push_back("--hash-style=gnu");
2145
2146 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2147 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2148 ExtraOpts.push_back("--hash-style=both");
2149 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002150
Chris Lattnerd753b562011-05-22 05:36:06 +00002151 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002152 ExtraOpts.push_back("--no-add-needed");
2153
Eli Friedman0b200f62011-06-02 21:36:53 +00002154 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002155 IsOpenSuse(Distro) ||
2156 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002157 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002158 ExtraOpts.push_back("--build-id");
2159
Chris Lattner64a89172011-05-22 16:45:07 +00002160 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002161 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002162
Chandler Carruthd2deee12011-10-03 05:28:29 +00002163 // The selection of paths to try here is designed to match the patterns which
2164 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2165 // This was determined by running GCC in a fake filesystem, creating all
2166 // possible permutations of these directories, and seeing which ones it added
2167 // to the link paths.
2168 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002169
Simon Atanasyan7a918882012-09-14 11:27:24 +00002170 const std::string Multilib = getMultilibDir(Triple, Args);
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002171 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002172
Chandler Carruthd1f73062011-11-06 23:09:05 +00002173 // Add the multilib suffixed paths where they are available.
2174 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002175 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002176 const std::string &LibPath = GCCInstallation.getParentLibPath();
Simon Atanasyanf8d9bd52012-10-03 17:46:38 +00002177
2178 if (IsAndroid && isMipsR2Arch(Triple.getArch(), Args))
2179 addPathIfExists(GCCInstallation.getInstallPath() +
2180 GCCInstallation.getMultiarchSuffix() +
2181 "/mips-r2",
2182 Paths);
2183 else
2184 addPathIfExists((GCCInstallation.getInstallPath() +
2185 GCCInstallation.getMultiarchSuffix()),
2186 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002187
2188 // If the GCC installation we found is inside of the sysroot, we want to
2189 // prefer libraries installed in the parent prefix of the GCC installation.
2190 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002191 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002192 // This usually happens when there is an external cross compiler on the
2193 // host system, and a more minimal sysroot available that is the target of
2194 // the cross.
2195 if (StringRef(LibPath).startswith(SysRoot)) {
2196 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2197 Paths);
2198 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2199 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2200 }
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002201 // On Android, libraries in the parent prefix of the GCC installation are
2202 // preferred to the ones under sysroot.
2203 if (IsAndroid) {
2204 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2205 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002206 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002207 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2208 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2209 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2210 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2211
2212 // Try walking via the GCC triple path in case of multiarch GCC
2213 // installations with strange symlinks.
2214 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002215 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002216 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002217
Chandler Carruth7a09d012011-10-16 10:54:30 +00002218 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002219 if (GCCInstallation.isValid()) {
2220 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002221 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002222 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002223 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002224
2225 if (StringRef(LibPath).startswith(SysRoot)) {
2226 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2227 addPathIfExists(LibPath, Paths);
2228 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002229 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002230 addPathIfExists(SysRoot + "/lib", Paths);
2231 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002232}
2233
2234bool Linux::HasNativeLLVMSupport() const {
2235 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002236}
2237
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002238Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2239 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002240 Action::ActionClass Key;
2241 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2242 Key = Action::AnalyzeJobClass;
2243 else
2244 Key = JA.getKind();
2245
Rafael Espindoladda5b922010-11-07 23:13:01 +00002246 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2247 options::OPT_no_integrated_as,
2248 IsIntegratedAssemblerDefault());
2249
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002250 Tool *&T = Tools[Key];
2251 if (!T) {
2252 switch (Key) {
2253 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002254 if (UseIntegratedAs)
2255 T = new tools::ClangAs(*this);
2256 else
2257 T = new tools::linuxtools::Assemble(*this);
2258 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002259 case Action::LinkJobClass:
2260 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002261 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002262 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002263 }
2264 }
2265
2266 return *T;
2267}
2268
Rafael Espindola8af669f2012-06-19 01:26:10 +00002269void Linux::addClangTargetOptions(ArgStringList &CC1Args) const {
2270 const Generic_GCC::GCCVersion &V = GCCInstallation.getVersion();
2271 if (V >= Generic_GCC::GCCVersion::Parse("4.7.0"))
2272 CC1Args.push_back("-fuse-init-array");
2273}
2274
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002275void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2276 ArgStringList &CC1Args) const {
2277 const Driver &D = getDriver();
2278
2279 if (DriverArgs.hasArg(options::OPT_nostdinc))
2280 return;
2281
2282 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2283 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2284
2285 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002286 llvm::sys::Path P(D.ResourceDir);
2287 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002288 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002289 }
2290
2291 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2292 return;
2293
2294 // Check for configure-time C include directories.
2295 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2296 if (CIncludeDirs != "") {
2297 SmallVector<StringRef, 5> dirs;
2298 CIncludeDirs.split(dirs, ":");
2299 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2300 I != E; ++I) {
2301 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2302 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2303 }
2304 return;
2305 }
2306
2307 // Lacking those, try to detect the correct set of system includes for the
2308 // target triple.
2309
Chandler Carrutha4630892011-11-06 08:21:07 +00002310 // Implement generic Debian multiarch support.
2311 const StringRef X86_64MultiarchIncludeDirs[] = {
2312 "/usr/include/x86_64-linux-gnu",
2313
2314 // FIXME: These are older forms of multiarch. It's not clear that they're
2315 // in use in any released version of Debian, so we should consider
2316 // removing them.
2317 "/usr/include/i686-linux-gnu/64",
2318 "/usr/include/i486-linux-gnu/64"
2319 };
2320 const StringRef X86MultiarchIncludeDirs[] = {
2321 "/usr/include/i386-linux-gnu",
2322
2323 // FIXME: These are older forms of multiarch. It's not clear that they're
2324 // in use in any released version of Debian, so we should consider
2325 // removing them.
2326 "/usr/include/x86_64-linux-gnu/32",
2327 "/usr/include/i686-linux-gnu",
2328 "/usr/include/i486-linux-gnu"
2329 };
2330 const StringRef ARMMultiarchIncludeDirs[] = {
2331 "/usr/include/arm-linux-gnueabi"
2332 };
Jiangning Liuff104a12012-07-31 08:06:29 +00002333 const StringRef ARMHFMultiarchIncludeDirs[] = {
2334 "/usr/include/arm-linux-gnueabihf"
2335 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002336 const StringRef MIPSMultiarchIncludeDirs[] = {
2337 "/usr/include/mips-linux-gnu"
2338 };
2339 const StringRef MIPSELMultiarchIncludeDirs[] = {
2340 "/usr/include/mipsel-linux-gnu"
2341 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002342 const StringRef PPCMultiarchIncludeDirs[] = {
2343 "/usr/include/powerpc-linux-gnu"
2344 };
2345 const StringRef PPC64MultiarchIncludeDirs[] = {
2346 "/usr/include/powerpc64-linux-gnu"
2347 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002348 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002349 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002350 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002351 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002352 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002353 } else if (getTriple().getArch() == llvm::Triple::arm) {
Jiangning Liuff104a12012-07-31 08:06:29 +00002354 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF)
2355 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs;
2356 else
2357 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002358 } else if (getTriple().getArch() == llvm::Triple::mips) {
2359 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2360 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2361 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002362 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2363 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2364 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2365 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002366 }
2367 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2368 E = MultiarchIncludeDirs.end();
2369 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002370 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002371 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2372 break;
2373 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002374 }
2375
2376 if (getTriple().getOS() == llvm::Triple::RTEMS)
2377 return;
2378
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002379 // Add an include of '/include' directly. This isn't provided by default by
2380 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2381 // add even when Clang is acting as-if it were a system compiler.
2382 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2383
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002384 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2385}
2386
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002387/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2388/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2389 const ArgList &DriverArgs,
2390 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002391 if (!llvm::sys::fs::exists(Base))
2392 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002393 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002394 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002395 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002396 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002397}
2398
2399void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2400 ArgStringList &CC1Args) const {
2401 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2402 DriverArgs.hasArg(options::OPT_nostdincxx))
2403 return;
2404
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002405 // Check if libc++ has been enabled and provide its include paths if so.
2406 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2407 // libc++ is always installed at a fixed path on Linux currently.
2408 addSystemInclude(DriverArgs, CC1Args,
2409 getDriver().SysRoot + "/usr/include/c++/v1");
2410 return;
2411 }
2412
Chandler Carruthfc52f752012-01-25 08:04:13 +00002413 // We need a detected GCC installation on Linux to provide libstdc++'s
2414 // headers. We handled the libc++ case above.
2415 if (!GCCInstallation.isValid())
2416 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002417
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002418 // By default, look for the C++ headers in an include directory adjacent to
2419 // the lib directory of the GCC installation. Note that this is expect to be
2420 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2421 StringRef LibDir = GCCInstallation.getParentLibPath();
2422 StringRef InstallDir = GCCInstallation.getInstallPath();
Rafael Espindola8af669f2012-06-19 01:26:10 +00002423 StringRef Version = GCCInstallation.getVersion().Text;
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002424 StringRef TripleStr = GCCInstallation.getTriple().str();
2425
2426 const std::string IncludePathCandidates[] = {
2427 LibDir.str() + "/../include/c++/" + Version.str(),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002428 // Gentoo is weird and places its headers inside the GCC install, so if the
2429 // first attempt to find the headers fails, try this pattern.
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002430 InstallDir.str() + "/include/g++-v4",
2431 // Android standalone toolchain has C++ headers in yet another place.
2432 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.str(),
Hal Finkel02014b42012-09-18 22:25:07 +00002433 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
2434 // without a subdirectory corresponding to the gcc version.
2435 LibDir.str() + "/../include/c++",
Evgeniy Stepanov1d01afe2012-09-03 09:05:50 +00002436 };
2437
2438 for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) {
2439 if (addLibStdCXXIncludePaths(IncludePathCandidates[i], (TripleStr +
2440 GCCInstallation.getMultiarchSuffix()),
2441 DriverArgs, CC1Args))
2442 break;
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002443 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002444}
2445
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002446/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2447
Rafael Espindola0e659592012-02-19 01:38:32 +00002448DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2449 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002450
2451 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002452 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002453 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002454 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002455
Daniel Dunbaree788e72009-12-21 18:54:17 +00002456 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002457 getFilePaths().push_back("/usr/lib");
2458 getFilePaths().push_back("/usr/lib/gcc41");
2459}
2460
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002461Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2462 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002463 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00002464 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002465 Key = Action::AnalyzeJobClass;
2466 else
2467 Key = JA.getKind();
2468
2469 Tool *&T = Tools[Key];
2470 if (!T) {
2471 switch (Key) {
2472 case Action::AssembleJobClass:
2473 T = new tools::dragonfly::Assemble(*this); break;
2474 case Action::LinkJobClass:
2475 T = new tools::dragonfly::Link(*this); break;
2476 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002477 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002478 }
2479 }
2480
2481 return *T;
2482}