blob: 9cc29cd5459304bfd2eb3cd5d1257d339b709fa8 [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
Rafael Espindola14ea13c2011-06-02 22:18:46 +000012#ifdef HAVE_CLANG_CONFIG_H
13# include "clang/Config/config.h"
14#endif
15
Daniel Dunbarf3cad362009-03-25 04:13:45 +000016#include "clang/Driver/Arg.h"
17#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000018#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000019#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000020#include "clang/Driver/DriverDiagnostic.h"
John McCall9f084a32011-07-06 00:26:06 +000021#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000022#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000023#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000024#include "clang/Driver/Options.h"
Douglas Gregor34916db2010-09-03 17:16:03 +000025#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000026
Daniel Dunbar00577ad2010-08-23 22:35:37 +000027#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000028#include "llvm/ADT/StringExtras.h"
Bob Wilsona59956b2011-10-07 00:37:57 +000029#include "llvm/ADT/StringSwitch.h"
John McCallf85e1932011-06-15 23:02:42 +000030#include "llvm/ADT/STLExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000031#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000032#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000033#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000034#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000035#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000036#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000037
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000038#include <cstdlib> // ::getenv
39
Dylan Noblesmith89bb6142011-06-23 13:50:47 +000040#include "llvm/Config/config.h" // for CXX_INCLUDE_ROOT
41
Daniel Dunbar39176082009-03-20 00:20:03 +000042using namespace clang::driver;
43using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000044using namespace clang;
Daniel Dunbar39176082009-03-20 00:20:03 +000045
Daniel Dunbarf3955282009-09-04 18:34:51 +000046/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000047
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000048Darwin::Darwin(const Driver &D, const llvm::Triple& Triple)
49 : ToolChain(D, Triple), TargetInitialized(false),
Bob Wilson163b1512011-10-07 17:54:41 +000050 ARCRuntimeForSimulator(ARCSimulator_None),
51 LibCXXForSimulator(LibCXXSimulator_None)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000052{
Bob Wilson10853772012-01-31 21:30:03 +000053 // Compute the initial Darwin version from the triple
54 unsigned Major, Minor, Micro;
55 Triple.getOSVersion(Major, Minor, Micro);
56 switch (Triple.getOS()) {
57 default: assert(0 && "unexpected OS for Darwin triple");
58 case llvm::Triple::Darwin:
59 // Default to darwin4, i.e., MacOSX 10.0.0.
60 if (Major == 0)
61 Major = 4;
62 if (Major < 4)
63 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
64 Triple.getOSName();
65 Micro = 0;
66 Minor = Major - 4;
67 Major = 10;
68 break;
69 case llvm::Triple::MacOSX:
70 // Default to MacOSX 10.
71 if (Major == 0)
72 Major = 10;
73 if (Major != 10)
74 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
75 Triple.getOSName();
76 break;
77 case llvm::Triple::IOS:
78 // Ignore the version from the triple.
79 Major = 10;
80 Minor = 0;
81 Micro = 0;
82 break;
83 }
84 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
85 // It should be removed when we stop supporting that.
86 DarwinVersion[0] = Minor + 4;
87 DarwinVersion[1] = Micro;
88 DarwinVersion[2] = 0;
Daniel Dunbar02633b52009-03-26 16:23:12 +000089 llvm::raw_string_ostream(MacosxVersionMin)
Bob Wilson10853772012-01-31 21:30:03 +000090 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000091}
92
Daniel Dunbar41800112010-08-02 05:43:56 +000093types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
94 types::ID Ty = types::lookupTypeForExtension(Ext);
95
96 // Darwin always preprocesses assembly files (unless -x is used explicitly).
97 if (Ty == types::TY_PP_Asm)
98 return types::TY_Asm;
99
100 return Ty;
101}
102
Daniel Dunbarb993f5d2010-09-17 00:24:52 +0000103bool Darwin::HasNativeLLVMSupport() const {
104 return true;
105}
106
John McCall9f084a32011-07-06 00:26:06 +0000107bool Darwin::hasARCRuntime() const {
John McCallf85e1932011-06-15 23:02:42 +0000108 // FIXME: Remove this once there is a proper way to detect an ARC runtime
109 // for the simulator.
110 switch (ARCRuntimeForSimulator) {
111 case ARCSimulator_None:
112 break;
113 case ARCSimulator_HasARCRuntime:
114 return true;
115 case ARCSimulator_NoARCRuntime:
116 return false;
117 }
118
119 if (isTargetIPhoneOS())
120 return !isIPhoneOSVersionLT(5);
121 else
122 return !isMacosxVersionLT(10, 7);
123}
124
John McCall9f084a32011-07-06 00:26:06 +0000125/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
126void Darwin::configureObjCRuntime(ObjCRuntime &runtime) const {
127 if (runtime.getKind() != ObjCRuntime::NeXT)
128 return ToolChain::configureObjCRuntime(runtime);
129
130 runtime.HasARC = runtime.HasWeak = hasARCRuntime();
John McCall256a76e2011-07-06 01:22:26 +0000131
132 // So far, objc_terminate is only available in iOS 5.
133 // FIXME: do the simulator logic properly.
134 if (!ARCRuntimeForSimulator && isTargetIPhoneOS())
135 runtime.HasTerminate = !isIPhoneOSVersionLT(5);
136 else
137 runtime.HasTerminate = false;
John McCall9f084a32011-07-06 00:26:06 +0000138}
139
John McCall13db5cf2011-09-09 20:41:01 +0000140/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
141bool Darwin::hasBlocksRuntime() const {
142 if (isTargetIPhoneOS())
143 return !isIPhoneOSVersionLT(3, 2);
144 else
145 return !isMacosxVersionLT(10, 6);
146}
147
Chris Lattner5f9e2722011-07-23 10:55:15 +0000148static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000149 return llvm::StringSwitch<const char*>(Value)
150 .Case("armv6k", "armv6")
151 .Case("armv5tej", "armv5")
152 .Case("xscale", "xscale")
153 .Case("armv4t", "armv4t")
154 .Case("armv7", "armv7")
155 .Cases("armv7a", "armv7-a", "armv7")
156 .Cases("armv7r", "armv7-r", "armv7")
157 .Cases("armv7m", "armv7-m", "armv7")
158 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000159}
160
Chris Lattner5f9e2722011-07-23 10:55:15 +0000161static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000162 return llvm::StringSwitch<const char *>(Value)
163 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
164 .Cases("arm10e", "arm10tdmi", "armv5")
165 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
166 .Case("xscale", "xscale")
167 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s",
168 "arm1176jzf-s", "cortex-m0", "armv6")
169 .Cases("cortex-a8", "cortex-r4", "cortex-m3", "cortex-a9", "armv7")
170 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000171}
172
Chris Lattner5f9e2722011-07-23 10:55:15 +0000173StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000174 switch (getTriple().getArch()) {
175 default:
176 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000177
Douglas Gregorf0594d82011-03-06 19:11:49 +0000178 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000179 case llvm::Triple::arm: {
180 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
181 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
182 return Arch;
183
184 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
185 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
186 return Arch;
187
188 return "arm";
189 }
190 }
191}
192
Daniel Dunbarf3955282009-09-04 18:34:51 +0000193Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000194 // Free tool implementations.
195 for (llvm::DenseMap<unsigned, Tool*>::iterator
196 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
197 delete it->second;
198}
199
Chad Rosier61ab80a2011-09-20 20:44:06 +0000200std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
201 types::ID InputType) const {
202 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000203
204 // If the target isn't initialized (e.g., an unknown Darwin platform, return
205 // the default triple).
206 if (!isTargetInitialized())
207 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000208
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000209 unsigned Version[3];
210 getTargetVersion(Version);
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000211
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000212 llvm::SmallString<16> Str;
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000213 llvm::raw_svector_ostream(Str)
Daniel Dunbar659d23a2011-04-19 23:34:17 +0000214 << (isTargetIPhoneOS() ? "ios" : "macosx")
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000215 << Version[0] << "." << Version[1] << "." << Version[2];
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000216 Triple.setOSName(Str.str());
217
218 return Triple.getTriple();
219}
220
David Blaikie99ba9e32011-12-20 02:48:34 +0000221void Generic_ELF::anchor() {}
222
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000223Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
224 const ActionList &Inputs) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000225 Action::ActionClass Key;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000226
227 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
228 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
229 if (Inputs.size() == 1 &&
230 types::isCXX(Inputs[0]->getType()) &&
Bob Wilson905c45f2011-10-14 05:03:44 +0000231 getTriple().isOSDarwin() &&
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000232 getTriple().getArch() == llvm::Triple::x86 &&
Bob Wilsona544aee2011-08-13 23:48:55 +0000233 (C.getArgs().getLastArg(options::OPT_fapple_kext) ||
234 C.getArgs().getLastArg(options::OPT_mkernel)))
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000235 Key = JA.getKind();
236 else
237 Key = Action::AnalyzeJobClass;
238 } else
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000239 Key = JA.getKind();
240
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000241 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
242 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000243 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000244
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000245 Tool *&T = Tools[Key];
246 if (!T) {
247 switch (Key) {
248 case Action::InputClass:
249 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000250 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000251 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000252 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000253 case Action::AnalyzeJobClass:
254 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000255 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000256 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000257 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000258 case Action::AssembleJobClass: {
259 if (UseIntegratedAs)
260 T = new tools::ClangAs(*this);
261 else
262 T = new tools::darwin::Assemble(*this);
263 break;
264 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000265 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000266 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000267 case Action::LipoJobClass:
268 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000269 case Action::DsymutilJobClass:
270 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000271 case Action::VerifyJobClass:
272 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000273 }
274 }
275
276 return *T;
277}
278
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000279
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000280DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
281 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000282{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000283 getProgramPaths().push_back(getDriver().getInstalledDir());
284 if (getDriver().getInstalledDir() != getDriver().Dir)
285 getProgramPaths().push_back(getDriver().Dir);
286
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000287 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000288 getProgramPaths().push_back(getDriver().getInstalledDir());
289 if (getDriver().getInstalledDir() != getDriver().Dir)
290 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000291
292 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000293 // also add the GCC libexec paths. This is legacy code that can be removed
294 // once fallback is no longer useful.
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000295 AddGCCLibexecPath(DarwinVersion[0]);
296 AddGCCLibexecPath(DarwinVersion[0] - 2);
297 AddGCCLibexecPath(DarwinVersion[0] - 1);
298 AddGCCLibexecPath(DarwinVersion[0] + 1);
299 AddGCCLibexecPath(DarwinVersion[0] + 2);
300}
301
302void DarwinClang::AddGCCLibexecPath(unsigned darwinVersion) {
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000303 std::string ToolChainDir = "i686-apple-darwin";
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000304 ToolChainDir += llvm::utostr(darwinVersion);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000305 ToolChainDir += "/4.2.1";
306
307 std::string Path = getDriver().Dir;
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000308 Path += "/../llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000309 Path += ToolChainDir;
310 getProgramPaths().push_back(Path);
311
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000312 Path = "/usr/llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000313 Path += ToolChainDir;
314 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000315}
316
317void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
318 ArgStringList &CmdArgs) const {
319 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000320
321 // Unfortunately, we still might depend on a few of the libraries that are
322 // only available in the gcc library directory (in particular
323 // libstdc++.dylib). For now, hardcode the path to the known install location.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000324 // FIXME: This should get ripped out someday. However, when building on
325 // 10.6 (darwin10), we're still relying on this to find libstdc++.dylib.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000326 llvm::sys::Path P(getDriver().Dir);
327 P.eraseComponent(); // .../usr/bin -> ../usr
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000328 P.appendComponent("llvm-gcc-4.2");
Daniel Dunbar424b6612010-06-30 23:56:13 +0000329 P.appendComponent("lib");
330 P.appendComponent("gcc");
331 switch (getTriple().getArch()) {
332 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000333 llvm_unreachable("Invalid Darwin arch!");
Daniel Dunbar424b6612010-06-30 23:56:13 +0000334 case llvm::Triple::x86:
335 case llvm::Triple::x86_64:
336 P.appendComponent("i686-apple-darwin10");
337 break;
338 case llvm::Triple::arm:
339 case llvm::Triple::thumb:
340 P.appendComponent("arm-apple-darwin10");
341 break;
342 case llvm::Triple::ppc:
343 case llvm::Triple::ppc64:
344 P.appendComponent("powerpc-apple-darwin10");
345 break;
346 }
347 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000348
349 // Determine the arch specific GCC subdirectory.
350 const char *ArchSpecificDir = 0;
351 switch (getTriple().getArch()) {
352 default:
353 break;
354 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000355 case llvm::Triple::thumb: {
356 std::string Triple = ComputeLLVMTriple(Args);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000357 StringRef TripleStr = Triple;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000358 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
359 ArchSpecificDir = "v5";
360 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
361 ArchSpecificDir = "v6";
362 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
363 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000364 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000365 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000366 case llvm::Triple::ppc64:
367 ArchSpecificDir = "ppc64";
368 break;
369 case llvm::Triple::x86_64:
370 ArchSpecificDir = "x86_64";
371 break;
372 }
373
374 if (ArchSpecificDir) {
375 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000376 bool Exists;
377 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000378 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
379 P.eraseComponent();
380 }
381
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000382 bool Exists;
383 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000384 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000385}
386
John McCallf85e1932011-06-15 23:02:42 +0000387void DarwinClang::AddLinkARCArgs(const ArgList &Args,
388 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000389
390 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000391 llvm::sys::Path P(getDriver().ClangExecutable);
392 P.eraseComponent(); // 'clang'
393 P.eraseComponent(); // 'bin'
394 P.appendComponent("lib");
395 P.appendComponent("arc");
396 P.appendComponent("libarclite_");
397 std::string s = P.str();
398 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000399 if (isTargetIOSSimulator())
400 s += "iphonesimulator";
401 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000402 s += "iphoneos";
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000403 // FIXME: Remove this once we depend fully on -mios-simulator-version-min.
John McCallf85e1932011-06-15 23:02:42 +0000404 else if (ARCRuntimeForSimulator != ARCSimulator_None)
405 s += "iphonesimulator";
406 else
407 s += "macosx";
408 s += ".a";
409
410 CmdArgs.push_back(Args.MakeArgString(s));
411}
412
Eric Christopher3404fe72011-06-22 17:41:40 +0000413void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000414 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000415 const char *DarwinStaticLib) const {
416 llvm::sys::Path P(getDriver().ResourceDir);
417 P.appendComponent("lib");
418 P.appendComponent("darwin");
419 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000420
Eric Christopher3404fe72011-06-22 17:41:40 +0000421 // For now, allow missing resource libraries to support developers who may
422 // not have compiler-rt checked out or integrated into their build.
423 bool Exists;
424 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
425 CmdArgs.push_back(Args.MakeArgString(P.str()));
426}
427
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000428void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
429 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000430 // Darwin only supports the compiler-rt based runtime libraries.
431 switch (GetRuntimeLibType(Args)) {
432 case ToolChain::RLT_CompilerRT:
433 break;
434 default:
435 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
436 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue(Args) << "darwin";
437 return;
438 }
439
Daniel Dunbareec99102010-01-22 03:38:14 +0000440 // Darwin doesn't support real static executables, don't link any runtime
441 // libraries with -static.
442 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000443 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000444
445 // Reject -static-libgcc for now, we can deal with this when and if someone
446 // cares. This is useful in situations where someone wants to statically link
447 // something like libstdc++, and needs its runtime support routines.
448 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000449 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000450 << A->getAsString(Args);
451 return;
452 }
453
Daniel Dunbarf4714872011-11-17 00:36:57 +0000454 // If we are building profile support, link that library in.
455 if (Args.hasArg(options::OPT_fprofile_arcs) ||
456 Args.hasArg(options::OPT_fprofile_generate) ||
457 Args.hasArg(options::OPT_fcreate_profile) ||
458 Args.hasArg(options::OPT_coverage)) {
459 // Select the appropriate runtime library for the target.
460 if (isTargetIPhoneOS()) {
461 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
462 } else {
463 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
464 }
465 }
466
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000467 // Add ASAN runtime library, if required. Dynamic libraries and bundles
468 // should not be linked with the runtime library.
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000469 if (Args.hasFlag(options::OPT_faddress_sanitizer,
470 options::OPT_fno_address_sanitizer, false)) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000471 if (Args.hasArg(options::OPT_dynamiclib) ||
472 Args.hasArg(options::OPT_bundle)) return;
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000473 if (isTargetIPhoneOS()) {
474 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
475 << "-faddress-sanitizer";
476 } else {
477 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
478
479 // The ASAN runtime library requires C++ and CoreFoundation.
480 AddCXXStdlibLibArgs(Args, CmdArgs);
481 CmdArgs.push_back("-framework");
482 CmdArgs.push_back("CoreFoundation");
483 }
484 }
485
Daniel Dunbareec99102010-01-22 03:38:14 +0000486 // Otherwise link libSystem, then the dynamic runtime library, and finally any
487 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000488 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000489
490 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000491 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000492 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
493 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000494 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
495 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
496 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000497
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000498 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000499 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000500 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000501 // The dynamic runtime library was merged with libSystem for 10.6 and
502 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000503 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000504 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000505 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000506 CmdArgs.push_back("-lgcc_s.10.5");
507
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000508 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000509 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000510 // omitted from 10.4.dylib.
511 //
512 // Unfortunately, that turned out to not be true, because Darwin system
513 // headers can still use eprintf on i386, and it is not exported from
514 // libSystem. Therefore, we still must provide a runtime library just for
515 // the tiny tiny handful of projects that *might* use that symbol.
516 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000517 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000518 } else {
519 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000520 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
521 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000522 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000523 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000524}
525
Argyrios Kyrtzidisdceb11f2011-10-18 00:22:49 +0000526static inline StringRef SimulatorVersionDefineName() {
527 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
528}
529
530/// \brief Parse the simulator version define:
531/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
532// and return the grouped values as integers, e.g:
533// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
534// will return Major=4, Minor=2, Micro=1.
535static bool GetVersionFromSimulatorDefine(StringRef define,
536 unsigned &Major, unsigned &Minor,
537 unsigned &Micro) {
538 assert(define.startswith(SimulatorVersionDefineName()));
539 StringRef name, version;
540 llvm::tie(name, version) = define.split('=');
541 if (version.empty())
542 return false;
543 std::string verstr = version.str();
544 char *end;
545 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
546 if (*end != '\0')
547 return false;
548 Major = num / 10000;
549 num = num % 10000;
550 Minor = num / 100;
551 Micro = num % 100;
552 return true;
553}
554
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000555void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000556 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000557
Daniel Dunbar26031372010-01-27 00:56:25 +0000558 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000559 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
560 Arg *iOSSimVersion = Args.getLastArg(
561 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000562
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000563 // FIXME: HACK! When compiling for the simulator we don't get a
564 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
565 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
566 // define passed in command-line.
567 if (!iOSVersion && !iOSSimVersion) {
568 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
569 ie = Args.filtered_end(); it != ie; ++it) {
570 StringRef define = (*it)->getValue(Args);
571 if (define.startswith(SimulatorVersionDefineName())) {
572 unsigned Major = 0, Minor = 0, Micro = 0;
573 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
574 Major < 10 && Minor < 100 && Micro < 100) {
575 ARCRuntimeForSimulator = Major < 5 ? ARCSimulator_NoARCRuntime
576 : ARCSimulator_HasARCRuntime;
577 LibCXXForSimulator = Major < 5 ? LibCXXSimulator_NotAvailable
578 : LibCXXSimulator_Available;
579 }
580 break;
581 }
582 }
583 }
584
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000585 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000586 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000587 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000588 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
589 iOSVersion = iOSSimVersion = 0;
590 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000591 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000592 << iOSVersion->getAsString(Args)
593 << iOSSimVersion->getAsString(Args);
594 iOSSimVersion = 0;
595 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000596 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000597 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000598 StringRef OSXTarget;
599 StringRef iOSTarget;
600 StringRef iOSSimTarget;
601 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
602 OSXTarget = env;
603 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
604 iOSTarget = env;
605 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
606 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000607
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000608 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000609 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
610 // based on isysroot.
611 if (iOSTarget.empty()) {
612 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
613 StringRef first, second;
614 StringRef isysroot = A->getValue(Args);
615 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
616 if (second != "")
617 iOSTarget = second.substr(0,3);
618 }
619 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000620
Chad Rosier4f8de272011-09-28 00:46:32 +0000621 // If no OSX or iOS target has been specified and we're compiling for armv7,
622 // go ahead as assume we're targeting iOS.
623 if (OSXTarget.empty() && iOSTarget.empty())
624 if (getDarwinArchName(Args) == "armv7")
625 iOSTarget = "0.0";
626
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000627 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000628 //
629 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000630
631 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000632 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000633 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000634 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000635 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000636 "IPHONEOS_DEPLOYMENT_TARGET");
637 }
638
639 // Allow conflicts among OSX and iOS for historical reasons, but choose the
640 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000641 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000642 if (getTriple().getArch() == llvm::Triple::arm ||
643 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000644 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000645 else
Chad Rosiera4884972011-08-31 20:56:25 +0000646 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000647 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000648
Chad Rosiera4884972011-08-31 20:56:25 +0000649 if (!OSXTarget.empty()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000650 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000651 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
652 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000653 } else if (!iOSTarget.empty()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000654 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000655 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
656 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000657 } else if (!iOSSimTarget.empty()) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000658 const Option *O = Opts.getOption(
659 options::OPT_mios_simulator_version_min_EQ);
660 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
661 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000662 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000663 // Otherwise, assume we are targeting OS X.
664 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000665 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
666 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000667 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000670 // Reject invalid architecture combinations.
671 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
672 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000673 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000674 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
675 }
676
Daniel Dunbar26031372010-01-27 00:56:25 +0000677 // Set the tool chain target information.
678 unsigned Major, Minor, Micro;
679 bool HadExtra;
680 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000681 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar26031372010-01-27 00:56:25 +0000682 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
683 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000684 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000685 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000686 << OSXVersion->getAsString(Args);
687 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000688 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
689 assert(Version && "Unknown target platform!");
Eli Friedman983d8352012-01-11 02:41:15 +0000690 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
691 Micro, HadExtra) || HadExtra ||
692 Major >= 10 || Minor >= 100 || Micro >= 100)
693 getDriver().Diag(diag::err_drv_invalid_version_number)
694 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000695 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000696
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000697 bool IsIOSSim = bool(iOSSimVersion);
698
699 // In GCC, the simulator historically was treated as being OS X in some
700 // contexts, like determining the link logic, despite generally being called
701 // with an iOS deployment target. For compatibility, we detect the
702 // simulator as iOS + x86, and treat it differently in a few contexts.
703 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
704 getTriple().getArch() == llvm::Triple::x86_64))
705 IsIOSSim = true;
706
707 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000708}
709
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000710void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000711 ArgStringList &CmdArgs) const {
712 CXXStdlibType Type = GetCXXStdlibType(Args);
713
714 switch (Type) {
715 case ToolChain::CST_Libcxx:
716 CmdArgs.push_back("-lc++");
717 break;
718
719 case ToolChain::CST_Libstdcxx: {
720 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
721 // it was previously found in the gcc lib dir. However, for all the Darwin
722 // platforms we care about it was -lstdc++.6, so we search for that
723 // explicitly if we can't see an obvious -lstdc++ candidate.
724
725 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000726 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000727 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
728 llvm::sys::Path P(A->getValue(Args));
729 P.appendComponent("usr");
730 P.appendComponent("lib");
731 P.appendComponent("libstdc++.dylib");
732
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000733 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000734 P.eraseComponent();
735 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000736 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000737 CmdArgs.push_back(Args.MakeArgString(P.str()));
738 return;
739 }
740 }
741 }
742
743 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000744 // FIXME: This should be removed someday when we don't have to care about
745 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000746 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
747 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000748 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
749 return;
750 }
751
752 // Otherwise, let the linker search.
753 CmdArgs.push_back("-lstdc++");
754 break;
755 }
756 }
757}
758
Shantonu Sen7433fed2010-09-17 18:39:08 +0000759void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
760 ArgStringList &CmdArgs) const {
761
762 // For Darwin platforms, use the compiler-rt-based support library
763 // instead of the gcc-provided one (which is also incidentally
764 // only present in the gcc lib dir, which makes it hard to find).
765
766 llvm::sys::Path P(getDriver().ResourceDir);
767 P.appendComponent("lib");
768 P.appendComponent("darwin");
769 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000770
Shantonu Sen7433fed2010-09-17 18:39:08 +0000771 // For now, allow missing resource libraries to support developers who may
772 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000773 bool Exists;
774 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000775 CmdArgs.push_back(Args.MakeArgString(P.str()));
776}
777
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000778DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
779 const char *BoundArch) const {
780 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
781 const OptTable &Opts = getDriver().getOpts();
782
783 // FIXME: We really want to get out of the tool chain level argument
784 // translation business, as it makes the driver functionality much
785 // more opaque. For now, we follow gcc closely solely for the
786 // purpose of easily achieving feature parity & testability. Once we
787 // have something that works, we should reevaluate each translation
788 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000789
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000790 for (ArgList::const_iterator it = Args.begin(),
791 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000792 Arg *A = *it;
793
794 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000795 // Skip this argument unless the architecture matches either the toolchain
796 // triple arch, or the arch being bound.
797 //
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000798 // FIXME: Canonicalize name.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000799 StringRef XarchArch = A->getValue(Args, 0);
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000800 if (!(XarchArch == getArchName() ||
801 (BoundArch && XarchArch == BoundArch)))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000802 continue;
803
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000804 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000805 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
806 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000807 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000809 // If the argument parsing failed or more than one argument was
810 // consumed, the -Xarch_ argument's parameter tried to consume
811 // extra arguments. Emit an error and ignore.
812 //
813 // We also want to disallow any options which would alter the
814 // driver behavior; that isn't going to work in our model. We
815 // use isDriverOption() as an approximation, although things
816 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000817 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000818 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000819 << A->getAsString(Args);
820 continue;
821 } else if (XarchArg->getOption().isDriverOption()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000822 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000823 << A->getAsString(Args);
824 continue;
825 }
826
Daniel Dunbar478edc22009-03-29 22:29:05 +0000827 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000828 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000829
830 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000831
832 // Linker input arguments require custom handling. The problem is that we
833 // have already constructed the phase actions, so we can not treat them as
834 // "input arguments".
835 if (A->getOption().isLinkerInput()) {
836 // Convert the argument into individual Zlinker_input_args.
837 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
838 DAL->AddSeparateArg(OriginalArg,
839 Opts.getOption(options::OPT_Zlinker_input),
840 A->getValue(Args, i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000841
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000842 }
843 continue;
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000846
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000847 // Sob. These is strictly gcc compatible for the time being. Apple
848 // gcc translates options twice, which means that self-expanding
849 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000850 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000851 default:
852 DAL->append(A);
853 break;
854
855 case options::OPT_mkernel:
856 case options::OPT_fapple_kext:
857 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000858 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000859 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000861 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000862 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
863 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000864 break;
865
866 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000867 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
868 DAL->AddFlagArg(A,
869 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000870 break;
871
872 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000873 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
874 DAL->AddFlagArg(A,
875 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000876 break;
877
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000878 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000879 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000880 break;
881
882 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000883 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000884 break;
885
886 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000887 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000888 break;
889
890 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000891 DAL->AddFlagArg(A,
892 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000893 break;
894
895 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000896 DAL->AddFlagArg(A,
897 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000898 break;
899
900 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000901 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000902 break;
903
904 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000905 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000906 break;
907 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000908 }
909
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000910 if (getTriple().getArch() == llvm::Triple::x86 ||
911 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000912 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000913 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000914
915 // Add the arch options based on the particular spelling of -arch, to match
916 // how the driver driver works.
917 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000918 StringRef Name = BoundArch;
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000919 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
920 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
921
922 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
923 // which defines the list of which architectures we accept.
924 if (Name == "ppc")
925 ;
926 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000927 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000928 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000929 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000930 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000931 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000932 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000933 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000934 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000935 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000936 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000937 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000938 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000939 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000940 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000941 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000942
943 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000944 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000945
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000946 else if (Name == "i386")
947 ;
948 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000949 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000950 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000951 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000952 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000953 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000954 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000955 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000956 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000957 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000958 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000959 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000960 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000961 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000962
963 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000964 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000965
966 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000967 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000968 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000969 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000970 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000971 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000972 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000973 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000974 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000975 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000976 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000977 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000978
979 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000980 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000981 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000982
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000983 // Add an explicit version min argument for the deployment target. We do this
984 // after argument translation because -Xarch_ arguments may add a version min
985 // argument.
986 AddDeploymentTarget(*DAL);
987
Bob Wilson163b1512011-10-07 17:54:41 +0000988 // Validate the C++ standard library choice.
989 CXXStdlibType Type = GetCXXStdlibType(*DAL);
990 if (Type == ToolChain::CST_Libcxx) {
991 switch (LibCXXForSimulator) {
992 case LibCXXSimulator_None:
993 // Handle non-simulator cases.
994 if (isTargetIPhoneOS()) {
995 if (isIPhoneOSVersionLT(5, 0)) {
996 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
997 << "iOS 5.0";
998 }
Bob Wilson163b1512011-10-07 17:54:41 +0000999 }
1000 break;
1001 case LibCXXSimulator_NotAvailable:
1002 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
1003 << "iOS 5.0";
1004 break;
1005 case LibCXXSimulator_Available:
1006 break;
1007 }
1008 }
1009
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +00001010 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +00001011}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001012
Daniel Dunbarf3955282009-09-04 18:34:51 +00001013bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001014 // FIXME: Gross; we should probably have some separate target
1015 // definition, possibly even reusing the one in clang.
1016 return getArchName() == "x86_64";
1017}
1018
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001019bool Darwin::UseDwarfDebugFlags() const {
1020 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
1021 return S[0] != '\0';
1022 return false;
1023}
1024
Daniel Dunbarb2987d12010-02-10 18:49:11 +00001025bool Darwin::UseSjLjExceptions() const {
1026 // Darwin uses SjLj exceptions on ARM.
1027 return (getTriple().getArch() == llvm::Triple::arm ||
1028 getTriple().getArch() == llvm::Triple::thumb);
1029}
1030
Daniel Dunbarf3955282009-09-04 18:34:51 +00001031const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001032 return "pic";
1033}
1034
Daniel Dunbarf3955282009-09-04 18:34:51 +00001035const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001036 if (getArchName() == "x86_64")
1037 return "pic";
1038 return 0;
1039}
1040
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00001041bool Darwin::SupportsProfiling() const {
1042 // Profiling instrumentation is only supported on x86.
1043 return getArchName() == "i386" || getArchName() == "x86_64";
1044}
1045
Daniel Dunbar43a9b322010-04-10 16:20:23 +00001046bool Darwin::SupportsObjCGC() const {
1047 // Garbage collection is supported everywhere except on iPhone OS.
1048 return !isTargetIPhoneOS();
1049}
1050
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001051std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +00001052Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
1053 types::ID InputType) const {
1054 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001055}
1056
Daniel Dunbar39176082009-03-20 00:20:03 +00001057/// Generic_GCC - A tool chain using the 'gcc' command to perform
1058/// all subcommands; this relies on gcc translating the majority of
1059/// command line options.
1060
Chandler Carruth19347ed2011-11-06 23:39:34 +00001061/// \brief Parse a GCCVersion object out of a string of text.
1062///
1063/// This is the primary means of forming GCCVersion objects.
1064/*static*/
1065Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
1066 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
1067 std::pair<StringRef, StringRef> First = VersionText.split('.');
1068 std::pair<StringRef, StringRef> Second = First.second.split('.');
1069
1070 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
1071 if (First.first.getAsInteger(10, GoodVersion.Major) ||
1072 GoodVersion.Major < 0)
1073 return BadVersion;
1074 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
1075 GoodVersion.Minor < 0)
1076 return BadVersion;
1077
1078 // First look for a number prefix and parse that if present. Otherwise just
1079 // stash the entire patch string in the suffix, and leave the number
1080 // unspecified. This covers versions strings such as:
1081 // 4.4
1082 // 4.4.0
1083 // 4.4.x
1084 // 4.4.2-rc4
1085 // 4.4.x-patched
1086 // And retains any patch number it finds.
1087 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1088 if (!PatchText.empty()) {
1089 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
1090 // Try to parse the number and any suffix.
1091 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1092 GoodVersion.Patch < 0)
1093 return BadVersion;
1094 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
1095 }
1096 }
1097
1098 return GoodVersion;
1099}
1100
1101/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
1102bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
1103 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
1104 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
1105
1106 // Note that we rank versions with *no* patch specified is better than ones
1107 // hard-coding a patch version. Thus if the RHS has no patch, it always
1108 // wins, and the LHS only wins if it has no patch and the RHS does have
1109 // a patch.
1110 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
1111 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
1112
1113 // Finally, between completely tied version numbers, the version with the
1114 // suffix loses as we prefer full releases.
1115 if (RHS.PatchSuffix.empty()) return true;
1116 return false;
1117}
1118
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001119// FIXME: Factor this helper into llvm::Triple itself.
1120static llvm::Triple getMultiarchAlternateTriple(llvm::Triple Triple) {
1121 switch (Triple.getArch()) {
1122 default: break;
1123 case llvm::Triple::x86: Triple.setArchName("x86_64"); break;
1124 case llvm::Triple::x86_64: Triple.setArchName("i386"); break;
1125 case llvm::Triple::ppc: Triple.setArchName("powerpc64"); break;
1126 case llvm::Triple::ppc64: Triple.setArchName("powerpc"); break;
1127 }
1128 return Triple;
1129}
1130
Chandler Carruth19347ed2011-11-06 23:39:34 +00001131/// \brief Construct a GCCInstallationDetector from the driver.
1132///
1133/// This performs all of the autodetection and sets up the various paths.
1134/// Once constructed, a GCCInstallation is esentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001135///
1136/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1137/// should instead pull the target out of the driver. This is currently
1138/// necessary because the driver doesn't store the final version of the target
1139/// triple.
1140Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1141 const Driver &D,
1142 const llvm::Triple &TargetTriple)
1143 : IsValid(false) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001144 // FIXME: Using CXX_INCLUDE_ROOT is here is a bit of a hack, but
1145 // avoids adding yet another option to configure/cmake.
1146 // It would probably be cleaner to break it in two variables
1147 // CXX_GCC_ROOT with just /foo/bar
1148 // CXX_GCC_VER with 4.5.2
1149 // Then we would have
1150 // CXX_INCLUDE_ROOT = CXX_GCC_ROOT/include/c++/CXX_GCC_VER
1151 // and this function would return
1152 // CXX_GCC_ROOT/lib/gcc/CXX_INCLUDE_ARCH/CXX_GCC_VER
1153 llvm::SmallString<128> CxxIncludeRoot(CXX_INCLUDE_ROOT);
1154 if (CxxIncludeRoot != "") {
1155 // This is of the form /foo/bar/include/c++/4.5.2/
1156 if (CxxIncludeRoot.back() == '/')
1157 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the /
1158 StringRef Version = llvm::sys::path::filename(CxxIncludeRoot);
1159 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the version
1160 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the c++
1161 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the include
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001162 GCCInstallPath = CxxIncludeRoot.str();
1163 GCCInstallPath.append("/lib/gcc/");
1164 GCCInstallPath.append(CXX_INCLUDE_ARCH);
1165 GCCInstallPath.append("/");
1166 GCCInstallPath.append(Version);
1167 GCCParentLibPath = GCCInstallPath + "/../../..";
Chandler Carruth19347ed2011-11-06 23:39:34 +00001168 IsValid = true;
1169 return;
1170 }
1171
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001172 llvm::Triple MultiarchTriple = getMultiarchAlternateTriple(TargetTriple);
1173 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001174 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001175 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001176 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001177 SmallVector<StringRef, 10> CandidateTripleAliases;
1178 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1179 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1180 CandidateTripleAliases,
1181 CandidateMultiarchLibDirs,
1182 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001183
1184 // Compute the set of prefixes for our search.
1185 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1186 D.PrefixDirs.end());
1187 Prefixes.push_back(D.SysRoot);
1188 Prefixes.push_back(D.SysRoot + "/usr");
1189 Prefixes.push_back(D.InstalledDir + "/..");
1190
1191 // Loop over the various components which exist and select the best GCC
1192 // installation available. GCC installs are ranked by version number.
1193 Version = GCCVersion::Parse("0.0.0");
1194 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1195 if (!llvm::sys::fs::exists(Prefixes[i]))
1196 continue;
1197 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1198 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1199 if (!llvm::sys::fs::exists(LibDir))
1200 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001201 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
1202 ScanLibDirForGCCTriple(TargetArch, LibDir, CandidateTripleAliases[k]);
1203 }
1204 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1205 const std::string LibDir
1206 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1207 if (!llvm::sys::fs::exists(LibDir))
1208 continue;
1209 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1210 ++k)
1211 ScanLibDirForGCCTriple(TargetArch, LibDir,
1212 CandidateMultiarchTripleAliases[k],
1213 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001214 }
1215 }
1216}
1217
1218/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001219 const llvm::Triple &TargetTriple,
1220 const llvm::Triple &MultiarchTriple,
1221 SmallVectorImpl<StringRef> &LibDirs,
1222 SmallVectorImpl<StringRef> &TripleAliases,
1223 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1224 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1225 // Declare a bunch of static data sets that we'll select between below. These
1226 // are specifically designed to always refer to string literals to avoid any
1227 // lifetime or initialization issues.
1228 static const char *const ARMLibDirs[] = { "/lib" };
1229 static const char *const ARMTriples[] = {
1230 "arm-linux-gnueabi",
1231 "arm-linux-androideabi"
1232 };
1233
1234 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1235 static const char *const X86_64Triples[] = {
1236 "x86_64-linux-gnu",
1237 "x86_64-unknown-linux-gnu",
1238 "x86_64-pc-linux-gnu",
1239 "x86_64-redhat-linux6E",
1240 "x86_64-redhat-linux",
1241 "x86_64-suse-linux",
1242 "x86_64-manbo-linux-gnu",
1243 "x86_64-linux-gnu",
1244 "x86_64-slackware-linux"
1245 };
1246 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1247 static const char *const X86Triples[] = {
1248 "i686-linux-gnu",
1249 "i686-pc-linux-gnu",
1250 "i486-linux-gnu",
1251 "i386-linux-gnu",
1252 "i686-redhat-linux",
1253 "i586-redhat-linux",
1254 "i386-redhat-linux",
1255 "i586-suse-linux",
1256 "i486-slackware-linux"
1257 };
1258
1259 static const char *const MIPSLibDirs[] = { "/lib" };
1260 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1261 static const char *const MIPSELLibDirs[] = { "/lib" };
1262 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu" };
1263
1264 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1265 static const char *const PPCTriples[] = {
1266 "powerpc-linux-gnu",
1267 "powerpc-unknown-linux-gnu",
1268 "powerpc-suse-linux"
1269 };
1270 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1271 static const char *const PPC64Triples[] = {
1272 "powerpc64-unknown-linux-gnu",
1273 "powerpc64-suse-linux",
1274 "ppc64-redhat-linux"
1275 };
1276
1277 switch (TargetTriple.getArch()) {
1278 case llvm::Triple::arm:
1279 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001280 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001281 TripleAliases.append(
1282 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1283 break;
1284 case llvm::Triple::x86_64:
1285 LibDirs.append(
1286 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1287 TripleAliases.append(
1288 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1289 MultiarchLibDirs.append(
1290 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1291 MultiarchTripleAliases.append(
1292 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1293 break;
1294 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001295 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001296 TripleAliases.append(
1297 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1298 MultiarchLibDirs.append(
1299 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1300 MultiarchTripleAliases.append(
1301 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1302 break;
1303 case llvm::Triple::mips:
1304 LibDirs.append(
1305 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1306 TripleAliases.append(
1307 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1308 break;
1309 case llvm::Triple::mipsel:
1310 LibDirs.append(
1311 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1312 TripleAliases.append(
1313 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
1314 break;
1315 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001316 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001317 TripleAliases.append(
1318 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1319 MultiarchLibDirs.append(
1320 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1321 MultiarchTripleAliases.append(
1322 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1323 break;
1324 case llvm::Triple::ppc64:
1325 LibDirs.append(
1326 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1327 TripleAliases.append(
1328 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1329 MultiarchLibDirs.append(
1330 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1331 MultiarchTripleAliases.append(
1332 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1333 break;
1334
1335 default:
1336 // By default, just rely on the standard lib directories and the original
1337 // triple.
1338 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001339 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001340
1341 // Always append the drivers target triple to the end, in case it doesn't
1342 // match any of our aliases.
1343 TripleAliases.push_back(TargetTriple.str());
1344
1345 // Also include the multiarch variant if it's different.
1346 if (TargetTriple.str() != MultiarchTriple.str())
1347 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001348}
1349
1350void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001351 llvm::Triple::ArchType TargetArch, const std::string &LibDir,
1352 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001353 // There are various different suffixes involving the triple we
1354 // check for. We also record what is necessary to walk from each back
1355 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001356 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001357 "/gcc/" + CandidateTriple.str(),
1358 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1359
1360 // Ubuntu has a strange mis-matched pair of triples that this happens to
1361 // match.
1362 // FIXME: It may be worthwhile to generalize this and look for a second
1363 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001364 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001365 };
1366 const std::string InstallSuffixes[] = {
1367 "/../../..",
1368 "/../../../..",
1369 "/../../../.."
1370 };
1371 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001372 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1373 (TargetArch != llvm::Triple::x86));
1374 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1375 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001376 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001377 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001378 !EC && LI != LE; LI = LI.increment(EC)) {
1379 StringRef VersionText = llvm::sys::path::filename(LI->path());
1380 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1381 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1382 if (CandidateVersion < MinVersion)
1383 continue;
1384 if (CandidateVersion <= Version)
1385 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001386
1387 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001388 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001389 // libs in a subdirectory named 64. The simple logic we follow is that
1390 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1391 // we use that. If not, and if not a multiarch triple, we look for
1392 // crtbegin.o without the subdirectory.
1393 StringRef MultiarchSuffix
1394 = (TargetArch == llvm::Triple::x86_64 ||
1395 TargetArch == llvm::Triple::ppc64) ? "/64" : "/32";
1396 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1397 GCCMultiarchSuffix = MultiarchSuffix.str();
1398 } else {
1399 if (NeedsMultiarchSuffix ||
1400 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1401 continue;
1402 GCCMultiarchSuffix.clear();
1403 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001404
1405 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001406 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001407 // FIXME: We hack together the directory name here instead of
1408 // using LI to ensure stable path separators across Windows and
1409 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001410 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001411 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001412 IsValid = true;
1413 }
1414 }
1415}
1416
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001417Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple)
1418 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001419 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001420 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001421 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001422}
1423
Daniel Dunbar39176082009-03-20 00:20:03 +00001424Generic_GCC::~Generic_GCC() {
1425 // Free tool implementations.
1426 for (llvm::DenseMap<unsigned, Tool*>::iterator
1427 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1428 delete it->second;
1429}
1430
Mike Stump1eb44332009-09-09 15:08:12 +00001431Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001432 const JobAction &JA,
1433 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +00001434 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001435 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +00001436 Key = Action::AnalyzeJobClass;
1437 else
1438 Key = JA.getKind();
1439
1440 Tool *&T = Tools[Key];
1441 if (!T) {
1442 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001443 case Action::InputClass:
1444 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001445 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001446 case Action::PreprocessJobClass:
1447 T = new tools::gcc::Preprocess(*this); break;
1448 case Action::PrecompileJobClass:
1449 T = new tools::gcc::Precompile(*this); break;
1450 case Action::AnalyzeJobClass:
1451 T = new tools::Clang(*this); break;
1452 case Action::CompileJobClass:
1453 T = new tools::gcc::Compile(*this); break;
1454 case Action::AssembleJobClass:
1455 T = new tools::gcc::Assemble(*this); break;
1456 case Action::LinkJobClass:
1457 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001459 // This is a bit ungeneric, but the only platform using a driver
1460 // driver is Darwin.
1461 case Action::LipoJobClass:
1462 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001463 case Action::DsymutilJobClass:
1464 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001465 case Action::VerifyJobClass:
1466 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001467 }
1468 }
1469
1470 return *T;
1471}
1472
Daniel Dunbar39176082009-03-20 00:20:03 +00001473bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001474 // FIXME: Gross; we should probably have some separate target
1475 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +00001476 return getArchName() == "x86_64";
1477}
1478
1479const char *Generic_GCC::GetDefaultRelocationModel() const {
1480 return "static";
1481}
1482
1483const char *Generic_GCC::GetForcedPicModel() const {
1484 return 0;
1485}
Tony Linthicum96319392011-12-12 21:14:55 +00001486/// Hexagon Toolchain
1487
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001488Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple& Triple)
1489 : ToolChain(D, Triple) {
Tony Linthicum96319392011-12-12 21:14:55 +00001490 getProgramPaths().push_back(getDriver().getInstalledDir());
1491 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1492 getProgramPaths().push_back(getDriver().Dir);
1493}
1494
1495Hexagon_TC::~Hexagon_TC() {
1496 // Free tool implementations.
1497 for (llvm::DenseMap<unsigned, Tool*>::iterator
1498 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1499 delete it->second;
1500}
1501
1502Tool &Hexagon_TC::SelectTool(const Compilation &C,
1503 const JobAction &JA,
1504 const ActionList &Inputs) const {
1505 Action::ActionClass Key;
1506 // if (JA.getKind () == Action::CompileJobClass)
1507 // Key = JA.getKind ();
1508 // else
1509
1510 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1511 Key = Action::AnalyzeJobClass;
1512 else
1513 Key = JA.getKind();
1514 // if ((JA.getKind () == Action::CompileJobClass)
1515 // && (JA.getType () != types::TY_LTO_BC)) {
1516 // Key = JA.getKind ();
1517 // }
1518
1519 Tool *&T = Tools[Key];
1520 if (!T) {
1521 switch (Key) {
1522 case Action::InputClass:
1523 case Action::BindArchClass:
1524 assert(0 && "Invalid tool kind.");
1525 case Action::AnalyzeJobClass:
1526 T = new tools::Clang(*this); break;
1527 case Action::AssembleJobClass:
1528 T = new tools::hexagon::Assemble(*this); break;
1529 case Action::LinkJobClass:
1530 T = new tools::hexagon::Link(*this); break;
1531 default:
1532 assert(false && "Unsupported action for Hexagon target.");
1533 }
1534 }
1535
1536 return *T;
1537}
1538
1539bool Hexagon_TC::IsUnwindTablesDefault() const {
1540 // FIXME: Gross; we should probably have some separate target
1541 // definition, possibly even reusing the one in clang.
1542 return getArchName() == "x86_64";
1543}
1544
1545const char *Hexagon_TC::GetDefaultRelocationModel() const {
1546 return "static";
1547}
1548
1549const char *Hexagon_TC::GetForcedPicModel() const {
1550 return 0;
1551} // End Hexagon
1552
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001553
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001554/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1555/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1556/// Currently does not support anything else but compilation.
1557
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001558TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1559 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001560 // Path mangling to find libexec
1561 std::string Path(getDriver().Dir);
1562
1563 Path += "/../libexec";
1564 getProgramPaths().push_back(Path);
1565}
1566
1567TCEToolChain::~TCEToolChain() {
1568 for (llvm::DenseMap<unsigned, Tool*>::iterator
1569 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1570 delete it->second;
1571}
1572
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001573bool TCEToolChain::IsMathErrnoDefault() const {
1574 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001575}
1576
1577bool TCEToolChain::IsUnwindTablesDefault() const {
1578 return false;
1579}
1580
1581const char *TCEToolChain::GetDefaultRelocationModel() const {
1582 return "static";
1583}
1584
1585const char *TCEToolChain::GetForcedPicModel() const {
1586 return 0;
1587}
1588
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001589Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001590 const JobAction &JA,
1591 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001592 Action::ActionClass Key;
1593 Key = Action::AnalyzeJobClass;
1594
1595 Tool *&T = Tools[Key];
1596 if (!T) {
1597 switch (Key) {
1598 case Action::PreprocessJobClass:
1599 T = new tools::gcc::Preprocess(*this); break;
1600 case Action::AnalyzeJobClass:
1601 T = new tools::Clang(*this); break;
1602 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001603 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001604 }
1605 }
1606 return *T;
1607}
1608
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001609/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1610
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001611OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple)
1612 : Generic_ELF(D, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001613 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001614 getFilePaths().push_back("/usr/lib");
1615}
1616
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001617Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1618 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001619 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001620 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001621 Key = Action::AnalyzeJobClass;
1622 else
1623 Key = JA.getKind();
1624
Rafael Espindoladda5b922010-11-07 23:13:01 +00001625 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1626 options::OPT_no_integrated_as,
1627 IsIntegratedAssemblerDefault());
1628
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001629 Tool *&T = Tools[Key];
1630 if (!T) {
1631 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001632 case Action::AssembleJobClass: {
1633 if (UseIntegratedAs)
1634 T = new tools::ClangAs(*this);
1635 else
1636 T = new tools::openbsd::Assemble(*this);
1637 break;
1638 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001639 case Action::LinkJobClass:
1640 T = new tools::openbsd::Link(*this); break;
1641 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001642 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001643 }
1644 }
1645
1646 return *T;
1647}
1648
Daniel Dunbar75358d22009-03-30 21:06:03 +00001649/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1650
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001651FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple)
1652 : Generic_ELF(D, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001653
Chandler Carruth24248e32012-01-26 01:35:15 +00001654 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1655 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001656 if ((Triple.getArch() == llvm::Triple::x86 ||
1657 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001658 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001659 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1660 else
1661 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001662}
1663
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001664Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1665 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001666 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001667 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001668 Key = Action::AnalyzeJobClass;
1669 else
1670 Key = JA.getKind();
1671
Roman Divacky67dece72010-11-08 17:46:39 +00001672 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1673 options::OPT_no_integrated_as,
1674 IsIntegratedAssemblerDefault());
1675
Daniel Dunbar75358d22009-03-30 21:06:03 +00001676 Tool *&T = Tools[Key];
1677 if (!T) {
1678 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001679 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001680 if (UseIntegratedAs)
1681 T = new tools::ClangAs(*this);
1682 else
1683 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001684 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001685 case Action::LinkJobClass:
1686 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001687 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001688 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001689 }
1690 }
1691
1692 return *T;
1693}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001694
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001695/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1696
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001697NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple)
1698 : Generic_ELF(D, Triple) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001699
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001700 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001701 // When targeting a 32-bit platform, try the special directory used on
1702 // 64-bit hosts, and only fall back to the main library directory if that
1703 // doesn't work.
1704 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1705 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001706 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001707 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001708
1709 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001710 }
1711}
1712
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001713Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1714 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001715 Action::ActionClass Key;
1716 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1717 Key = Action::AnalyzeJobClass;
1718 else
1719 Key = JA.getKind();
1720
1721 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1722 options::OPT_no_integrated_as,
1723 IsIntegratedAssemblerDefault());
1724
1725 Tool *&T = Tools[Key];
1726 if (!T) {
1727 switch (Key) {
1728 case Action::AssembleJobClass:
1729 if (UseIntegratedAs)
1730 T = new tools::ClangAs(*this);
1731 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001732 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001733 break;
1734 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001735 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001736 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001737 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001738 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001739 }
1740 }
1741
1742 return *T;
1743}
1744
Chris Lattner38e317d2010-07-07 16:01:42 +00001745/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1746
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001747Minix::Minix(const Driver &D, const llvm::Triple& Triple)
1748 : Generic_ELF(D, Triple) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001749 getFilePaths().push_back(getDriver().Dir + "/../lib");
1750 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001751}
1752
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001753Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1754 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001755 Action::ActionClass Key;
1756 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1757 Key = Action::AnalyzeJobClass;
1758 else
1759 Key = JA.getKind();
1760
1761 Tool *&T = Tools[Key];
1762 if (!T) {
1763 switch (Key) {
1764 case Action::AssembleJobClass:
1765 T = new tools::minix::Assemble(*this); break;
1766 case Action::LinkJobClass:
1767 T = new tools::minix::Link(*this); break;
1768 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001769 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001770 }
1771 }
1772
1773 return *T;
1774}
1775
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001776/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1777
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001778AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple)
1779 : Generic_GCC(D, Triple) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001780
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001781 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001782 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001783 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001784
Daniel Dunbaree788e72009-12-21 18:54:17 +00001785 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001786 getFilePaths().push_back("/usr/lib");
1787 getFilePaths().push_back("/usr/sfw/lib");
1788 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001789 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001790
1791}
1792
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001793Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1794 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001795 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001796 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001797 Key = Action::AnalyzeJobClass;
1798 else
1799 Key = JA.getKind();
1800
1801 Tool *&T = Tools[Key];
1802 if (!T) {
1803 switch (Key) {
1804 case Action::AssembleJobClass:
1805 T = new tools::auroraux::Assemble(*this); break;
1806 case Action::LinkJobClass:
1807 T = new tools::auroraux::Link(*this); break;
1808 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001809 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001810 }
1811 }
1812
1813 return *T;
1814}
1815
1816
Eli Friedman6b3454a2009-05-26 07:52:18 +00001817/// Linux toolchain (very bare-bones at the moment).
1818
Rafael Espindolac1da9812010-11-07 20:14:31 +00001819enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001820 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001821 DebianLenny,
1822 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001823 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001824 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001825 RHEL4,
1826 RHEL5,
1827 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001828 Fedora13,
1829 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001830 Fedora15,
1831 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001832 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001833 OpenSuse11_4,
1834 OpenSuse12_1,
Douglas Gregor814638e2011-03-14 15:39:50 +00001835 UbuntuHardy,
1836 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001837 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001838 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001839 UbuntuLucid,
1840 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001841 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001842 UbuntuOneiric,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001843 UnknownDistro
1844};
1845
Chris Lattnerd753b562011-05-22 05:36:06 +00001846static bool IsRedhat(enum LinuxDistro Distro) {
Eric Christopher8f1cc072011-04-06 18:22:53 +00001847 return Distro == Fedora13 || Distro == Fedora14 ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001848 Distro == Fedora15 || Distro == FedoraRawhide ||
1849 Distro == RHEL4 || Distro == RHEL5 || Distro == RHEL6;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001850}
1851
1852static bool IsOpenSuse(enum LinuxDistro Distro) {
David Chisnallde5c0482011-05-19 13:26:33 +00001853 return Distro == OpenSuse11_3 || Distro == OpenSuse11_4 ||
1854 Distro == OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001855}
1856
1857static bool IsDebian(enum LinuxDistro Distro) {
Eli Friedman0b200f62011-06-02 21:36:53 +00001858 return Distro == DebianLenny || Distro == DebianSqueeze ||
1859 Distro == DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001860}
1861
1862static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001863 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001864 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Ted Kremenek43ac2972011-04-05 22:04:27 +00001865 Distro == UbuntuJaunty || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001866 Distro == UbuntuNatty || Distro == UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001867}
1868
Rafael Espindolac1da9812010-11-07 20:14:31 +00001869static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001870 llvm::OwningPtr<llvm::MemoryBuffer> File;
1871 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001872 StringRef Data = File.get()->getBuffer();
1873 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001874 Data.split(Lines, "\n");
1875 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001876 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1877 return UbuntuHardy;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001878 else if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1879 return UbuntuIntrepid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001880 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001881 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001882 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1883 return UbuntuKarmic;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001884 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1885 return UbuntuLucid;
1886 else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1887 return UbuntuMaverick;
1888 else if (Lines[i] == "DISTRIB_CODENAME=natty")
1889 return UbuntuNatty;
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001890 else if (Lines[i] == "DISTRIB_CODENAME=oneiric")
1891 return UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001892 }
1893 return UnknownDistro;
1894 }
1895
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001896 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001897 StringRef Data = File.get()->getBuffer();
Eric Christopher8f1cc072011-04-06 18:22:53 +00001898 if (Data.startswith("Fedora release 15"))
1899 return Fedora15;
1900 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001901 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001902 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001903 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001904 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001905 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001906 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001907 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001908 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001909 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001910 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1911 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001912 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001913 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001914 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1915 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001916 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001917 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001918 return UnknownDistro;
1919 }
1920
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001921 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001922 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001923 if (Data[0] == '5')
1924 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001925 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00001926 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001927 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00001928 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001929 return UnknownDistro;
1930 }
1931
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001932 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001933 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001934 if (Data.startswith("openSUSE 11.3"))
1935 return OpenSuse11_3;
David Chisnallde5c0482011-05-19 13:26:33 +00001936 else if (Data.startswith("openSUSE 11.4"))
1937 return OpenSuse11_4;
1938 else if (Data.startswith("openSUSE 12.1"))
1939 return OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001940 return UnknownDistro;
1941 }
1942
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001943 bool Exists;
1944 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001945 return Exherbo;
1946
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001947 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1948 return ArchLinux;
1949
Rafael Espindolac1da9812010-11-07 20:14:31 +00001950 return UnknownDistro;
1951}
1952
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001953/// \brief Get our best guess at the multiarch triple for a target.
1954///
1955/// Debian-based systems are starting to use a multiarch setup where they use
1956/// a target-triple directory in the library and header search paths.
1957/// Unfortunately, this triple does not align with the vanilla target triple,
1958/// so we provide a rough mapping here.
1959static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
1960 StringRef SysRoot) {
1961 // For most architectures, just use whatever we have rather than trying to be
1962 // clever.
1963 switch (TargetTriple.getArch()) {
1964 default:
1965 return TargetTriple.str();
1966
1967 // We use the existence of '/lib/<triple>' as a directory to detect some
1968 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00001969 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
1970 // regardless of what the actual target triple is.
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001971 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001972 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
1973 return "i386-linux-gnu";
1974 return TargetTriple.str();
1975 case llvm::Triple::x86_64:
1976 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
1977 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001978 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00001979 case llvm::Triple::mips:
1980 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
1981 return "mips-linux-gnu";
1982 return TargetTriple.str();
1983 case llvm::Triple::mipsel:
1984 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
1985 return "mipsel-linux-gnu";
1986 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001987 }
1988}
1989
Chandler Carruth00646ba2012-01-25 11:24:24 +00001990static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
1991 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
1992}
1993
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001994Linux::Linux(const Driver &D, const llvm::Triple &Triple)
1995 : Generic_ELF(D, Triple) {
Chandler Carruth89088792012-01-24 20:08:17 +00001996 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00001997 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001998
Rafael Espindolaab784082011-09-01 16:25:49 +00001999 // OpenSuse stores the linker with the compiler, add that to the search
2000 // path.
2001 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002002 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002003 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002004
2005 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002006
2007 LinuxDistro Distro = DetectLinuxDistro(Arch);
2008
Chris Lattner64a89172011-05-22 16:45:07 +00002009 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002010 ExtraOpts.push_back("-z");
2011 ExtraOpts.push_back("relro");
2012 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002013
Douglas Gregorf0594d82011-03-06 19:11:49 +00002014 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002015 ExtraOpts.push_back("-X");
2016
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002017 const bool IsMips = Arch == llvm::Triple::mips ||
2018 Arch == llvm::Triple::mipsel ||
2019 Arch == llvm::Triple::mips64 ||
2020 Arch == llvm::Triple::mips64el;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002021
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002022 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::ANDROIDEABI;
2023
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002024 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2025 // and the MIPS ABI require .dynsym to be sorted in different ways.
2026 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2027 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002028 // Android loader does not support .gnu.hash.
2029 if (!IsMips && !IsAndroid) {
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002030 if (IsRedhat(Distro) || IsOpenSuse(Distro) || Distro == UbuntuMaverick ||
2031 Distro == UbuntuNatty || Distro == UbuntuOneiric)
2032 ExtraOpts.push_back("--hash-style=gnu");
2033
2034 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2035 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2036 ExtraOpts.push_back("--hash-style=both");
2037 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002038
Chris Lattnerd753b562011-05-22 05:36:06 +00002039 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002040 ExtraOpts.push_back("--no-add-needed");
2041
Eli Friedman0b200f62011-06-02 21:36:53 +00002042 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002043 IsOpenSuse(Distro) ||
2044 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
2045 Distro == UbuntuLucid ||
Eli Friedman0b200f62011-06-02 21:36:53 +00002046 Distro == UbuntuMaverick || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00002047 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002048 ExtraOpts.push_back("--build-id");
2049
Chris Lattner64a89172011-05-22 16:45:07 +00002050 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002051 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002052
Chandler Carruthd2deee12011-10-03 05:28:29 +00002053 // The selection of paths to try here is designed to match the patterns which
2054 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2055 // This was determined by running GCC in a fake filesystem, creating all
2056 // possible permutations of these directories, and seeing which ones it added
2057 // to the link paths.
2058 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002059
Chandler Carruth89088792012-01-24 20:08:17 +00002060 const bool Is32Bits = (Arch == llvm::Triple::x86 ||
2061 Arch == llvm::Triple::mips ||
2062 Arch == llvm::Triple::mipsel ||
2063 Arch == llvm::Triple::ppc);
2064
Chandler Carruthd2deee12011-10-03 05:28:29 +00002065 const std::string Multilib = Is32Bits ? "lib32" : "lib64";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002066 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002067
Chandler Carruthd1f73062011-11-06 23:09:05 +00002068 // Add the multilib suffixed paths where they are available.
2069 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002070 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002071 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002072 addPathIfExists((GCCInstallation.getInstallPath() +
2073 GCCInstallation.getMultiarchSuffix()),
2074 Paths);
Chandler Carruthfa5be912012-01-24 19:28:29 +00002075 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
Chandler Carruthd1f73062011-11-06 23:09:05 +00002076 Paths);
2077 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2078 addPathIfExists(LibPath + "/../" + Multilib, Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002079 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002080 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2081 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2082 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2083 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2084
2085 // Try walking via the GCC triple path in case of multiarch GCC
2086 // installations with strange symlinks.
2087 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002088 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002089 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002090
Chandler Carruth7a09d012011-10-16 10:54:30 +00002091 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002092 if (GCCInstallation.isValid()) {
2093 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002094 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002095 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002096 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruthfa5be912012-01-24 19:28:29 +00002097 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
Chandler Carruth048e6492011-10-03 18:16:54 +00002098 addPathIfExists(LibPath, Paths);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002099 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002100 addPathIfExists(SysRoot + "/lib", Paths);
2101 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002102}
2103
2104bool Linux::HasNativeLLVMSupport() const {
2105 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002106}
2107
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002108Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2109 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002110 Action::ActionClass Key;
2111 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2112 Key = Action::AnalyzeJobClass;
2113 else
2114 Key = JA.getKind();
2115
Rafael Espindoladda5b922010-11-07 23:13:01 +00002116 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2117 options::OPT_no_integrated_as,
2118 IsIntegratedAssemblerDefault());
2119
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002120 Tool *&T = Tools[Key];
2121 if (!T) {
2122 switch (Key) {
2123 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002124 if (UseIntegratedAs)
2125 T = new tools::ClangAs(*this);
2126 else
2127 T = new tools::linuxtools::Assemble(*this);
2128 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002129 case Action::LinkJobClass:
2130 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002131 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002132 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002133 }
2134 }
2135
2136 return *T;
2137}
2138
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002139void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2140 ArgStringList &CC1Args) const {
2141 const Driver &D = getDriver();
2142
2143 if (DriverArgs.hasArg(options::OPT_nostdinc))
2144 return;
2145
2146 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2147 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2148
2149 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002150 llvm::sys::Path P(D.ResourceDir);
2151 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002152 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002153 }
2154
2155 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2156 return;
2157
2158 // Check for configure-time C include directories.
2159 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2160 if (CIncludeDirs != "") {
2161 SmallVector<StringRef, 5> dirs;
2162 CIncludeDirs.split(dirs, ":");
2163 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2164 I != E; ++I) {
2165 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2166 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2167 }
2168 return;
2169 }
2170
2171 // Lacking those, try to detect the correct set of system includes for the
2172 // target triple.
2173
Chandler Carrutha4630892011-11-06 08:21:07 +00002174 // Implement generic Debian multiarch support.
2175 const StringRef X86_64MultiarchIncludeDirs[] = {
2176 "/usr/include/x86_64-linux-gnu",
2177
2178 // FIXME: These are older forms of multiarch. It's not clear that they're
2179 // in use in any released version of Debian, so we should consider
2180 // removing them.
2181 "/usr/include/i686-linux-gnu/64",
2182 "/usr/include/i486-linux-gnu/64"
2183 };
2184 const StringRef X86MultiarchIncludeDirs[] = {
2185 "/usr/include/i386-linux-gnu",
2186
2187 // FIXME: These are older forms of multiarch. It's not clear that they're
2188 // in use in any released version of Debian, so we should consider
2189 // removing them.
2190 "/usr/include/x86_64-linux-gnu/32",
2191 "/usr/include/i686-linux-gnu",
2192 "/usr/include/i486-linux-gnu"
2193 };
2194 const StringRef ARMMultiarchIncludeDirs[] = {
2195 "/usr/include/arm-linux-gnueabi"
2196 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002197 const StringRef MIPSMultiarchIncludeDirs[] = {
2198 "/usr/include/mips-linux-gnu"
2199 };
2200 const StringRef MIPSELMultiarchIncludeDirs[] = {
2201 "/usr/include/mipsel-linux-gnu"
2202 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002203 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002204 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002205 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002206 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002207 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002208 } else if (getTriple().getArch() == llvm::Triple::arm) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002209 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002210 } else if (getTriple().getArch() == llvm::Triple::mips) {
2211 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2212 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2213 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002214 }
2215 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2216 E = MultiarchIncludeDirs.end();
2217 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002218 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002219 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2220 break;
2221 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002222 }
2223
2224 if (getTriple().getOS() == llvm::Triple::RTEMS)
2225 return;
2226
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002227 // Add an include of '/include' directly. This isn't provided by default by
2228 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2229 // add even when Clang is acting as-if it were a system compiler.
2230 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2231
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002232 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2233}
2234
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002235/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2236/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2237 const ArgList &DriverArgs,
2238 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002239 if (!llvm::sys::fs::exists(Base))
2240 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002241 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002242 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002243 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002244 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002245}
2246
2247void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2248 ArgStringList &CC1Args) const {
2249 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2250 DriverArgs.hasArg(options::OPT_nostdincxx))
2251 return;
2252
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002253 // Check if libc++ has been enabled and provide its include paths if so.
2254 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2255 // libc++ is always installed at a fixed path on Linux currently.
2256 addSystemInclude(DriverArgs, CC1Args,
2257 getDriver().SysRoot + "/usr/include/c++/v1");
2258 return;
2259 }
2260
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002261 const llvm::Triple &TargetTriple = getTriple();
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002262
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002263 StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002264 if (!CxxIncludeRoot.empty()) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002265 StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002266 if (CxxIncludeArch.empty())
2267 CxxIncludeArch = TargetTriple.str();
2268
2269 addLibStdCXXIncludePaths(
2270 CxxIncludeRoot,
Chandler Carruthb37fe612011-11-06 23:39:37 +00002271 CxxIncludeArch + (isTarget64Bit() ? CXX_INCLUDE_64BIT_DIR
2272 : CXX_INCLUDE_32BIT_DIR),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002273 DriverArgs, CC1Args);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002274 return;
2275 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002276
Chandler Carruthfc52f752012-01-25 08:04:13 +00002277 // We need a detected GCC installation on Linux to provide libstdc++'s
2278 // headers. We handled the libc++ case above.
2279 if (!GCCInstallation.isValid())
2280 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002281
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002282 // By default, look for the C++ headers in an include directory adjacent to
2283 // the lib directory of the GCC installation. Note that this is expect to be
2284 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2285 StringRef LibDir = GCCInstallation.getParentLibPath();
2286 StringRef InstallDir = GCCInstallation.getInstallPath();
2287 StringRef Version = GCCInstallation.getVersion();
2288 if (!addLibStdCXXIncludePaths(LibDir + "/../include/c++/" + Version,
Chandler Carruthfc52f752012-01-25 08:04:13 +00002289 (GCCInstallation.getTriple().str() +
2290 GCCInstallation.getMultiarchSuffix()),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002291 DriverArgs, CC1Args)) {
2292 // Gentoo is weird and places its headers inside the GCC install, so if the
2293 // first attempt to find the headers fails, try this pattern.
2294 addLibStdCXXIncludePaths(InstallDir + "/include/g++-v4",
Chandler Carruthfc52f752012-01-25 08:04:13 +00002295 (GCCInstallation.getTriple().str() +
2296 GCCInstallation.getMultiarchSuffix()),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002297 DriverArgs, CC1Args);
2298 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002299}
2300
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002301/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2302
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00002303DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple)
2304 : Generic_ELF(D, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002305
2306 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002307 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002308 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002309 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002310
Daniel Dunbaree788e72009-12-21 18:54:17 +00002311 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002312 getFilePaths().push_back("/usr/lib");
2313 getFilePaths().push_back("/usr/lib/gcc41");
2314}
2315
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002316Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2317 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002318 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00002319 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002320 Key = Action::AnalyzeJobClass;
2321 else
2322 Key = JA.getKind();
2323
2324 Tool *&T = Tools[Key];
2325 if (!T) {
2326 switch (Key) {
2327 case Action::AssembleJobClass:
2328 T = new tools::dragonfly::Assemble(*this); break;
2329 case Action::LinkJobClass:
2330 T = new tools::dragonfly::Link(*this); break;
2331 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002332 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002333 }
2334 }
2335
2336 return *T;
2337}