blob: 53b78d238b59a5eec8ca9b5dec58d6d6032c3f7d [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
11
Daniel Dunbarf3cad362009-03-25 04:13:45 +000012#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000014#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000015#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000016#include "clang/Driver/DriverDiagnostic.h"
John McCall9f084a32011-07-06 00:26:06 +000017#include "clang/Driver/ObjCRuntime.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000018#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000019#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000020#include "clang/Driver/Options.h"
Douglas Gregor34916db2010-09-03 17:16:03 +000021#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000022
Daniel Dunbar00577ad2010-08-23 22:35:37 +000023#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000024#include "llvm/ADT/StringExtras.h"
Bob Wilsona59956b2011-10-07 00:37:57 +000025#include "llvm/ADT/StringSwitch.h"
John McCallf85e1932011-06-15 23:02:42 +000026#include "llvm/ADT/STLExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000027#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000028#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000029#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000030#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000031#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000032#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000033
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000034#include <cstdlib> // ::getenv
35
Dylan Noblesmithcc8a9452012-02-14 15:54:49 +000036#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX
Dylan Noblesmith89bb6142011-06-23 13:50:47 +000037
Daniel Dunbar39176082009-03-20 00:20:03 +000038using namespace clang::driver;
39using namespace clang::driver::toolchains;
Chris Lattner5f9e2722011-07-23 10:55:15 +000040using namespace clang;
Daniel Dunbar39176082009-03-20 00:20:03 +000041
Daniel Dunbarf3955282009-09-04 18:34:51 +000042/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000043
Chandler Carruth1d16f0f2012-01-31 02:21:20 +000044Darwin::Darwin(const Driver &D, const llvm::Triple& Triple)
45 : ToolChain(D, Triple), TargetInitialized(false),
Bob Wilson163b1512011-10-07 17:54:41 +000046 ARCRuntimeForSimulator(ARCSimulator_None),
47 LibCXXForSimulator(LibCXXSimulator_None)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000048{
Bob Wilson10853772012-01-31 21:30:03 +000049 // Compute the initial Darwin version from the triple
50 unsigned Major, Minor, Micro;
Bob Wilson4c5ffb32012-01-31 22:43:59 +000051 if (!Triple.getMacOSXVersion(Major, Minor, Micro))
52 getDriver().Diag(diag::err_drv_invalid_darwin_version) <<
53 Triple.getOSName();
54 llvm::raw_string_ostream(MacosxVersionMin)
55 << Major << '.' << Minor << '.' << Micro;
56
Bob Wilson10853772012-01-31 21:30:03 +000057 // FIXME: DarwinVersion is only used to find GCC's libexec directory.
58 // It should be removed when we stop supporting that.
59 DarwinVersion[0] = Minor + 4;
60 DarwinVersion[1] = Micro;
61 DarwinVersion[2] = 0;
Chad Rosierc793ea42012-05-09 18:46:30 +000062
63 // Compute the initial iOS version from the triple
Chad Rosier8c990272012-05-09 18:51:13 +000064 Triple.getiOSVersion(Major, Minor, Micro);
Chad Rosierc793ea42012-05-09 18:46:30 +000065 llvm::raw_string_ostream(iOSVersionMin)
66 << Major << '.' << Minor << '.' << Micro;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000067}
68
Daniel Dunbar41800112010-08-02 05:43:56 +000069types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
70 types::ID Ty = types::lookupTypeForExtension(Ext);
71
72 // Darwin always preprocesses assembly files (unless -x is used explicitly).
73 if (Ty == types::TY_PP_Asm)
74 return types::TY_Asm;
75
76 return Ty;
77}
78
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000079bool Darwin::HasNativeLLVMSupport() const {
80 return true;
81}
82
John McCall9f084a32011-07-06 00:26:06 +000083bool Darwin::hasARCRuntime() const {
John McCallf85e1932011-06-15 23:02:42 +000084 // FIXME: Remove this once there is a proper way to detect an ARC runtime
85 // for the simulator.
86 switch (ARCRuntimeForSimulator) {
87 case ARCSimulator_None:
88 break;
89 case ARCSimulator_HasARCRuntime:
90 return true;
91 case ARCSimulator_NoARCRuntime:
92 return false;
93 }
94
95 if (isTargetIPhoneOS())
96 return !isIPhoneOSVersionLT(5);
97 else
98 return !isMacosxVersionLT(10, 7);
99}
100
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000101bool Darwin::hasSubscriptingRuntime() const {
102 return !isTargetIPhoneOS() && !isMacosxVersionLT(10, 8);
103}
104
John McCall9f084a32011-07-06 00:26:06 +0000105/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
106void Darwin::configureObjCRuntime(ObjCRuntime &runtime) const {
107 if (runtime.getKind() != ObjCRuntime::NeXT)
108 return ToolChain::configureObjCRuntime(runtime);
109
110 runtime.HasARC = runtime.HasWeak = hasARCRuntime();
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000111 runtime.HasSubscripting = hasSubscriptingRuntime();
John McCall256a76e2011-07-06 01:22:26 +0000112
113 // So far, objc_terminate is only available in iOS 5.
114 // FIXME: do the simulator logic properly.
115 if (!ARCRuntimeForSimulator && isTargetIPhoneOS())
116 runtime.HasTerminate = !isIPhoneOSVersionLT(5);
117 else
118 runtime.HasTerminate = false;
John McCall9f084a32011-07-06 00:26:06 +0000119}
120
John McCall13db5cf2011-09-09 20:41:01 +0000121/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
122bool Darwin::hasBlocksRuntime() const {
123 if (isTargetIPhoneOS())
124 return !isIPhoneOSVersionLT(3, 2);
125 else
126 return !isMacosxVersionLT(10, 6);
127}
128
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000130 return llvm::StringSwitch<const char*>(Value)
131 .Case("armv6k", "armv6")
132 .Case("armv5tej", "armv5")
133 .Case("xscale", "xscale")
134 .Case("armv4t", "armv4t")
135 .Case("armv7", "armv7")
136 .Cases("armv7a", "armv7-a", "armv7")
137 .Cases("armv7r", "armv7-r", "armv7")
138 .Cases("armv7m", "armv7-m", "armv7")
139 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000140}
141
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000143 return llvm::StringSwitch<const char *>(Value)
144 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
145 .Cases("arm10e", "arm10tdmi", "armv5")
146 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
147 .Case("xscale", "xscale")
148 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s",
149 "arm1176jzf-s", "cortex-m0", "armv6")
150 .Cases("cortex-a8", "cortex-r4", "cortex-m3", "cortex-a9", "armv7")
151 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000152}
153
Chris Lattner5f9e2722011-07-23 10:55:15 +0000154StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000155 switch (getTriple().getArch()) {
156 default:
157 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000158
Douglas Gregorf0594d82011-03-06 19:11:49 +0000159 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000160 case llvm::Triple::arm: {
161 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
162 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
163 return Arch;
164
165 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
166 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
167 return Arch;
168
169 return "arm";
170 }
171 }
172}
173
Daniel Dunbarf3955282009-09-04 18:34:51 +0000174Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000175 // Free tool implementations.
176 for (llvm::DenseMap<unsigned, Tool*>::iterator
177 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
178 delete it->second;
179}
180
Chad Rosier61ab80a2011-09-20 20:44:06 +0000181std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
182 types::ID InputType) const {
183 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000184
185 // If the target isn't initialized (e.g., an unknown Darwin platform, return
186 // the default triple).
187 if (!isTargetInitialized())
188 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000189
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000190 SmallString<16> Str;
Benjamin Kramer09c9a562012-03-10 20:55:36 +0000191 Str += isTargetIPhoneOS() ? "ios" : "macosx";
192 Str += getTargetVersion().getAsString();
193 Triple.setOSName(Str);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000194
195 return Triple.getTriple();
196}
197
David Blaikie99ba9e32011-12-20 02:48:34 +0000198void Generic_ELF::anchor() {}
199
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000200Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
201 const ActionList &Inputs) const {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000202 Action::ActionClass Key = JA.getKind();
203 bool useClang = false;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000204
205 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000206 useClang = true;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000207 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000208 if (!getDriver().shouldForceClangUse() &&
209 Inputs.size() == 1 &&
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000210 types::isCXX(Inputs[0]->getType()) &&
Bob Wilson905c45f2011-10-14 05:03:44 +0000211 getTriple().isOSDarwin() &&
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000212 getTriple().getArch() == llvm::Triple::x86 &&
Bob Wilsona544aee2011-08-13 23:48:55 +0000213 (C.getArgs().getLastArg(options::OPT_fapple_kext) ||
214 C.getArgs().getLastArg(options::OPT_mkernel)))
Argyrios Kyrtzidisd6277fb2012-05-21 20:11:54 +0000215 useClang = false;
216 }
217
218 // FIXME: This seems like a hacky way to choose clang frontend.
219 if (useClang)
220 Key = Action::AnalyzeJobClass;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000221
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000222 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
223 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000224 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000225
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000226 Tool *&T = Tools[Key];
227 if (!T) {
228 switch (Key) {
229 case Action::InputClass:
230 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000231 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000232 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000233 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000234 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000235 case Action::MigrateJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000236 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000237 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000238 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000239 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000240 case Action::AssembleJobClass: {
241 if (UseIntegratedAs)
242 T = new tools::ClangAs(*this);
243 else
244 T = new tools::darwin::Assemble(*this);
245 break;
246 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000247 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000248 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000249 case Action::LipoJobClass:
250 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000251 case Action::DsymutilJobClass:
252 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000253 case Action::VerifyJobClass:
254 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000255 }
256 }
257
258 return *T;
259}
260
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000261
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000262DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
263 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000264{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000265 getProgramPaths().push_back(getDriver().getInstalledDir());
266 if (getDriver().getInstalledDir() != getDriver().Dir)
267 getProgramPaths().push_back(getDriver().Dir);
268
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000269 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000270 getProgramPaths().push_back(getDriver().getInstalledDir());
271 if (getDriver().getInstalledDir() != getDriver().Dir)
272 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000273
274 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000275 // also add the GCC libexec paths. This is legacy code that can be removed
276 // once fallback is no longer useful.
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000277 AddGCCLibexecPath(DarwinVersion[0]);
278 AddGCCLibexecPath(DarwinVersion[0] - 2);
279 AddGCCLibexecPath(DarwinVersion[0] - 1);
280 AddGCCLibexecPath(DarwinVersion[0] + 1);
281 AddGCCLibexecPath(DarwinVersion[0] + 2);
282}
283
284void DarwinClang::AddGCCLibexecPath(unsigned darwinVersion) {
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000285 std::string ToolChainDir = "i686-apple-darwin";
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000286 ToolChainDir += llvm::utostr(darwinVersion);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000287 ToolChainDir += "/4.2.1";
288
289 std::string Path = getDriver().Dir;
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000290 Path += "/../llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000291 Path += ToolChainDir;
292 getProgramPaths().push_back(Path);
293
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000294 Path = "/usr/llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000295 Path += ToolChainDir;
296 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000297}
298
John McCallf85e1932011-06-15 23:02:42 +0000299void DarwinClang::AddLinkARCArgs(const ArgList &Args,
300 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000301
302 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000303 llvm::sys::Path P(getDriver().ClangExecutable);
304 P.eraseComponent(); // 'clang'
305 P.eraseComponent(); // 'bin'
306 P.appendComponent("lib");
307 P.appendComponent("arc");
308 P.appendComponent("libarclite_");
309 std::string s = P.str();
310 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000311 if (isTargetIOSSimulator())
312 s += "iphonesimulator";
313 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000314 s += "iphoneos";
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000315 // FIXME: Remove this once we depend fully on -mios-simulator-version-min.
John McCallf85e1932011-06-15 23:02:42 +0000316 else if (ARCRuntimeForSimulator != ARCSimulator_None)
317 s += "iphonesimulator";
318 else
319 s += "macosx";
320 s += ".a";
321
322 CmdArgs.push_back(Args.MakeArgString(s));
323}
324
Eric Christopher3404fe72011-06-22 17:41:40 +0000325void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000326 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000327 const char *DarwinStaticLib) const {
328 llvm::sys::Path P(getDriver().ResourceDir);
329 P.appendComponent("lib");
330 P.appendComponent("darwin");
331 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000332
Eric Christopher3404fe72011-06-22 17:41:40 +0000333 // For now, allow missing resource libraries to support developers who may
334 // not have compiler-rt checked out or integrated into their build.
335 bool Exists;
336 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
337 CmdArgs.push_back(Args.MakeArgString(P.str()));
338}
339
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000340void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
341 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000342 // Darwin only supports the compiler-rt based runtime libraries.
343 switch (GetRuntimeLibType(Args)) {
344 case ToolChain::RLT_CompilerRT:
345 break;
346 default:
347 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
348 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue(Args) << "darwin";
349 return;
350 }
351
Daniel Dunbareec99102010-01-22 03:38:14 +0000352 // Darwin doesn't support real static executables, don't link any runtime
353 // libraries with -static.
354 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000355 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000356
357 // Reject -static-libgcc for now, we can deal with this when and if someone
358 // cares. This is useful in situations where someone wants to statically link
359 // something like libstdc++, and needs its runtime support routines.
360 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000361 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000362 << A->getAsString(Args);
363 return;
364 }
365
Daniel Dunbarf4714872011-11-17 00:36:57 +0000366 // If we are building profile support, link that library in.
367 if (Args.hasArg(options::OPT_fprofile_arcs) ||
368 Args.hasArg(options::OPT_fprofile_generate) ||
369 Args.hasArg(options::OPT_fcreate_profile) ||
370 Args.hasArg(options::OPT_coverage)) {
371 // Select the appropriate runtime library for the target.
372 if (isTargetIPhoneOS()) {
373 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
374 } else {
375 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
376 }
377 }
378
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000379 // Add ASAN runtime library, if required. Dynamic libraries and bundles
380 // should not be linked with the runtime library.
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000381 if (Args.hasFlag(options::OPT_faddress_sanitizer,
382 options::OPT_fno_address_sanitizer, false)) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000383 if (Args.hasArg(options::OPT_dynamiclib) ||
384 Args.hasArg(options::OPT_bundle)) return;
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000385 if (isTargetIPhoneOS()) {
386 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
387 << "-faddress-sanitizer";
388 } else {
389 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
390
391 // The ASAN runtime library requires C++ and CoreFoundation.
392 AddCXXStdlibLibArgs(Args, CmdArgs);
393 CmdArgs.push_back("-framework");
394 CmdArgs.push_back("CoreFoundation");
395 }
396 }
397
Daniel Dunbareec99102010-01-22 03:38:14 +0000398 // Otherwise link libSystem, then the dynamic runtime library, and finally any
399 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000400 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000401
402 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000403 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000404 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
405 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000406 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
407 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
408 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000409
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000410 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000411 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000412 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000413 // The dynamic runtime library was merged with libSystem for 10.6 and
414 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000415 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000416 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000417 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000418 CmdArgs.push_back("-lgcc_s.10.5");
419
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000420 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000421 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000422 // omitted from 10.4.dylib.
423 //
424 // Unfortunately, that turned out to not be true, because Darwin system
425 // headers can still use eprintf on i386, and it is not exported from
426 // libSystem. Therefore, we still must provide a runtime library just for
427 // the tiny tiny handful of projects that *might* use that symbol.
428 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000429 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000430 } else {
431 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000432 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
433 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000434 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000435 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000436}
437
Argyrios Kyrtzidisdceb11f2011-10-18 00:22:49 +0000438static inline StringRef SimulatorVersionDefineName() {
439 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
440}
441
442/// \brief Parse the simulator version define:
443/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
444// and return the grouped values as integers, e.g:
445// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
446// will return Major=4, Minor=2, Micro=1.
447static bool GetVersionFromSimulatorDefine(StringRef define,
448 unsigned &Major, unsigned &Minor,
449 unsigned &Micro) {
450 assert(define.startswith(SimulatorVersionDefineName()));
451 StringRef name, version;
452 llvm::tie(name, version) = define.split('=');
453 if (version.empty())
454 return false;
455 std::string verstr = version.str();
456 char *end;
457 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
458 if (*end != '\0')
459 return false;
460 Major = num / 10000;
461 num = num % 10000;
462 Minor = num / 100;
463 Micro = num % 100;
464 return true;
465}
466
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000467void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000468 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000469
Daniel Dunbar26031372010-01-27 00:56:25 +0000470 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000471 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
472 Arg *iOSSimVersion = Args.getLastArg(
473 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000474
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000475 // FIXME: HACK! When compiling for the simulator we don't get a
476 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
477 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
478 // define passed in command-line.
479 if (!iOSVersion && !iOSSimVersion) {
480 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
481 ie = Args.filtered_end(); it != ie; ++it) {
482 StringRef define = (*it)->getValue(Args);
483 if (define.startswith(SimulatorVersionDefineName())) {
484 unsigned Major = 0, Minor = 0, Micro = 0;
485 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
486 Major < 10 && Minor < 100 && Micro < 100) {
487 ARCRuntimeForSimulator = Major < 5 ? ARCSimulator_NoARCRuntime
488 : ARCSimulator_HasARCRuntime;
489 LibCXXForSimulator = Major < 5 ? LibCXXSimulator_NotAvailable
490 : LibCXXSimulator_Available;
491 }
492 break;
493 }
494 }
495 }
496
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000497 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000498 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000499 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000500 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
501 iOSVersion = iOSSimVersion = 0;
502 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000503 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000504 << iOSVersion->getAsString(Args)
505 << iOSSimVersion->getAsString(Args);
506 iOSSimVersion = 0;
507 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000508 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000509 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000510 StringRef OSXTarget;
511 StringRef iOSTarget;
512 StringRef iOSSimTarget;
513 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
514 OSXTarget = env;
515 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
516 iOSTarget = env;
517 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
518 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000519
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000520 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000521 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
Gabor Greif241cbe42012-04-18 10:59:08 +0000522 // based on -isysroot.
Chad Rosiera4884972011-08-31 20:56:25 +0000523 if (iOSTarget.empty()) {
524 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
525 StringRef first, second;
526 StringRef isysroot = A->getValue(Args);
527 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
528 if (second != "")
529 iOSTarget = second.substr(0,3);
530 }
531 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000532
Chad Rosier4f8de272011-09-28 00:46:32 +0000533 // If no OSX or iOS target has been specified and we're compiling for armv7,
534 // go ahead as assume we're targeting iOS.
Chad Rosier49033202012-05-09 18:55:57 +0000535 if (OSXTarget.empty() && iOSTarget.empty() &&
536 getDarwinArchName(Args) == "armv7")
Chad Rosier87ca5582012-05-09 18:09:58 +0000537 iOSTarget = iOSVersionMin;
Chad Rosier4f8de272011-09-28 00:46:32 +0000538
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000539 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000540 //
541 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000542
543 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000544 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000545 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000546 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000547 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000548 "IPHONEOS_DEPLOYMENT_TARGET");
549 }
550
551 // Allow conflicts among OSX and iOS for historical reasons, but choose the
552 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000553 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000554 if (getTriple().getArch() == llvm::Triple::arm ||
555 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000556 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000557 else
Chad Rosiera4884972011-08-31 20:56:25 +0000558 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000559 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000560
Chad Rosiera4884972011-08-31 20:56:25 +0000561 if (!OSXTarget.empty()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000562 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000563 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
564 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000565 } else if (!iOSTarget.empty()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000566 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000567 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
568 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000569 } else if (!iOSSimTarget.empty()) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000570 const Option *O = Opts.getOption(
571 options::OPT_mios_simulator_version_min_EQ);
572 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
573 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000574 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000575 // Otherwise, assume we are targeting OS X.
576 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000577 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
578 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000579 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000582 // Reject invalid architecture combinations.
583 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
584 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000585 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000586 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
587 }
588
Daniel Dunbar26031372010-01-27 00:56:25 +0000589 // Set the tool chain target information.
590 unsigned Major, Minor, Micro;
591 bool HadExtra;
592 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000593 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar26031372010-01-27 00:56:25 +0000594 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
595 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000596 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000597 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000598 << OSXVersion->getAsString(Args);
599 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000600 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
601 assert(Version && "Unknown target platform!");
Eli Friedman983d8352012-01-11 02:41:15 +0000602 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
603 Micro, HadExtra) || HadExtra ||
604 Major >= 10 || Minor >= 100 || Micro >= 100)
605 getDriver().Diag(diag::err_drv_invalid_version_number)
606 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000607 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000608
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000609 bool IsIOSSim = bool(iOSSimVersion);
610
611 // In GCC, the simulator historically was treated as being OS X in some
612 // contexts, like determining the link logic, despite generally being called
613 // with an iOS deployment target. For compatibility, we detect the
614 // simulator as iOS + x86, and treat it differently in a few contexts.
615 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
616 getTriple().getArch() == llvm::Triple::x86_64))
617 IsIOSSim = true;
618
619 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000620}
621
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000622void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000623 ArgStringList &CmdArgs) const {
624 CXXStdlibType Type = GetCXXStdlibType(Args);
625
626 switch (Type) {
627 case ToolChain::CST_Libcxx:
628 CmdArgs.push_back("-lc++");
629 break;
630
631 case ToolChain::CST_Libstdcxx: {
632 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
633 // it was previously found in the gcc lib dir. However, for all the Darwin
634 // platforms we care about it was -lstdc++.6, so we search for that
635 // explicitly if we can't see an obvious -lstdc++ candidate.
636
637 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000638 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000639 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
640 llvm::sys::Path P(A->getValue(Args));
641 P.appendComponent("usr");
642 P.appendComponent("lib");
643 P.appendComponent("libstdc++.dylib");
644
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000645 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000646 P.eraseComponent();
647 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000648 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000649 CmdArgs.push_back(Args.MakeArgString(P.str()));
650 return;
651 }
652 }
653 }
654
655 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000656 // FIXME: This should be removed someday when we don't have to care about
657 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000658 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
659 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000660 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
661 return;
662 }
663
664 // Otherwise, let the linker search.
665 CmdArgs.push_back("-lstdc++");
666 break;
667 }
668 }
669}
670
Shantonu Sen7433fed2010-09-17 18:39:08 +0000671void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
672 ArgStringList &CmdArgs) const {
673
674 // For Darwin platforms, use the compiler-rt-based support library
675 // instead of the gcc-provided one (which is also incidentally
676 // only present in the gcc lib dir, which makes it hard to find).
677
678 llvm::sys::Path P(getDriver().ResourceDir);
679 P.appendComponent("lib");
680 P.appendComponent("darwin");
681 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000682
Shantonu Sen7433fed2010-09-17 18:39:08 +0000683 // For now, allow missing resource libraries to support developers who may
684 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000685 bool Exists;
686 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000687 CmdArgs.push_back(Args.MakeArgString(P.str()));
688}
689
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000690DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
691 const char *BoundArch) const {
692 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
693 const OptTable &Opts = getDriver().getOpts();
694
695 // FIXME: We really want to get out of the tool chain level argument
696 // translation business, as it makes the driver functionality much
697 // more opaque. For now, we follow gcc closely solely for the
698 // purpose of easily achieving feature parity & testability. Once we
699 // have something that works, we should reevaluate each translation
700 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000701
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000702 for (ArgList::const_iterator it = Args.begin(),
703 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000704 Arg *A = *it;
705
706 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000707 // Skip this argument unless the architecture matches either the toolchain
708 // triple arch, or the arch being bound.
709 //
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000710 // FIXME: Canonicalize name.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000711 StringRef XarchArch = A->getValue(Args, 0);
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000712 if (!(XarchArch == getArchName() ||
713 (BoundArch && XarchArch == BoundArch)))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000714 continue;
715
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000716 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000717 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
718 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000719 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000721 // If the argument parsing failed or more than one argument was
722 // consumed, the -Xarch_ argument's parameter tried to consume
723 // extra arguments. Emit an error and ignore.
724 //
725 // We also want to disallow any options which would alter the
726 // driver behavior; that isn't going to work in our model. We
727 // use isDriverOption() as an approximation, although things
728 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000729 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000730 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000731 << A->getAsString(Args);
732 continue;
733 } else if (XarchArg->getOption().isDriverOption()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000734 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000735 << A->getAsString(Args);
736 continue;
737 }
738
Daniel Dunbar478edc22009-03-29 22:29:05 +0000739 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000740 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000741
742 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000743
744 // Linker input arguments require custom handling. The problem is that we
745 // have already constructed the phase actions, so we can not treat them as
746 // "input arguments".
747 if (A->getOption().isLinkerInput()) {
748 // Convert the argument into individual Zlinker_input_args.
749 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
750 DAL->AddSeparateArg(OriginalArg,
751 Opts.getOption(options::OPT_Zlinker_input),
752 A->getValue(Args, i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000753
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000754 }
755 continue;
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000758
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000759 // Sob. These is strictly gcc compatible for the time being. Apple
760 // gcc translates options twice, which means that self-expanding
761 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000762 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000763 default:
764 DAL->append(A);
765 break;
766
767 case options::OPT_mkernel:
768 case options::OPT_fapple_kext:
769 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000770 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000771 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000773 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000774 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
775 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000776 break;
777
778 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000779 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
780 DAL->AddFlagArg(A,
781 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000782 break;
783
784 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000785 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
786 DAL->AddFlagArg(A,
787 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000788 break;
789
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000790 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000791 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000792 break;
793
794 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000795 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000796 break;
797
798 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000799 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000800 break;
801
802 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000803 DAL->AddFlagArg(A,
804 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000805 break;
806
807 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000808 DAL->AddFlagArg(A,
809 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000810 break;
811
812 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000813 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000814 break;
815
816 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000817 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000818 break;
819 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000820 }
821
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000822 if (getTriple().getArch() == llvm::Triple::x86 ||
823 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000824 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000825 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000826
827 // Add the arch options based on the particular spelling of -arch, to match
Chad Rosierc97e96a2012-04-27 14:58:16 +0000828 // how the driver driver works.
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000829 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000830 StringRef Name = BoundArch;
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000831 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
832 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
833
834 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
835 // which defines the list of which architectures we accept.
836 if (Name == "ppc")
837 ;
838 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000839 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000840 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000841 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000842 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000843 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000844 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000845 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000846 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000847 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000848 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000849 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000850 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000851 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000852 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000853 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000854
855 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000856 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000857
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000858 else if (Name == "i386")
859 ;
860 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000861 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000862 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000863 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000864 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000865 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000866 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000867 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000868 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000869 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000870 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000871 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000872 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000873 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000874
875 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000876 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000877
878 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000879 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000880 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000881 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000882 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000883 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000884 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000885 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000886 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000887 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000888 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000889 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000890
891 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000892 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000893 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000894
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000895 // Add an explicit version min argument for the deployment target. We do this
896 // after argument translation because -Xarch_ arguments may add a version min
897 // argument.
Chad Rosier8202fb82012-04-27 19:51:11 +0000898 if (BoundArch)
899 AddDeploymentTarget(*DAL);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000900
Bob Wilson163b1512011-10-07 17:54:41 +0000901 // Validate the C++ standard library choice.
902 CXXStdlibType Type = GetCXXStdlibType(*DAL);
903 if (Type == ToolChain::CST_Libcxx) {
904 switch (LibCXXForSimulator) {
905 case LibCXXSimulator_None:
906 // Handle non-simulator cases.
907 if (isTargetIPhoneOS()) {
908 if (isIPhoneOSVersionLT(5, 0)) {
909 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
910 << "iOS 5.0";
911 }
Bob Wilson163b1512011-10-07 17:54:41 +0000912 }
913 break;
914 case LibCXXSimulator_NotAvailable:
915 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
916 << "iOS 5.0";
917 break;
918 case LibCXXSimulator_Available:
919 break;
920 }
921 }
922
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000923 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000924}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000925
Daniel Dunbarf3955282009-09-04 18:34:51 +0000926bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000927 // FIXME: Gross; we should probably have some separate target
928 // definition, possibly even reusing the one in clang.
929 return getArchName() == "x86_64";
930}
931
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000932bool Darwin::UseDwarfDebugFlags() const {
933 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
934 return S[0] != '\0';
935 return false;
936}
937
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000938bool Darwin::UseSjLjExceptions() const {
939 // Darwin uses SjLj exceptions on ARM.
940 return (getTriple().getArch() == llvm::Triple::arm ||
941 getTriple().getArch() == llvm::Triple::thumb);
942}
943
Daniel Dunbarf3955282009-09-04 18:34:51 +0000944const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000945 return "pic";
946}
947
Daniel Dunbarf3955282009-09-04 18:34:51 +0000948const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000949 if (getArchName() == "x86_64")
950 return "pic";
951 return 0;
952}
953
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000954bool Darwin::SupportsProfiling() const {
955 // Profiling instrumentation is only supported on x86.
956 return getArchName() == "i386" || getArchName() == "x86_64";
957}
958
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000959bool Darwin::SupportsObjCGC() const {
960 // Garbage collection is supported everywhere except on iPhone OS.
961 return !isTargetIPhoneOS();
962}
963
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +0000964bool Darwin::SupportsObjCARC() const {
965 return isTargetIPhoneOS() || !isMacosxVersionLT(10, 6);
966}
967
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000968std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +0000969Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
970 types::ID InputType) const {
971 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000972}
973
Daniel Dunbar39176082009-03-20 00:20:03 +0000974/// Generic_GCC - A tool chain using the 'gcc' command to perform
975/// all subcommands; this relies on gcc translating the majority of
976/// command line options.
977
Chandler Carruth19347ed2011-11-06 23:39:34 +0000978/// \brief Parse a GCCVersion object out of a string of text.
979///
980/// This is the primary means of forming GCCVersion objects.
981/*static*/
982Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
983 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
984 std::pair<StringRef, StringRef> First = VersionText.split('.');
985 std::pair<StringRef, StringRef> Second = First.second.split('.');
986
987 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
988 if (First.first.getAsInteger(10, GoodVersion.Major) ||
989 GoodVersion.Major < 0)
990 return BadVersion;
991 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
992 GoodVersion.Minor < 0)
993 return BadVersion;
994
995 // First look for a number prefix and parse that if present. Otherwise just
996 // stash the entire patch string in the suffix, and leave the number
997 // unspecified. This covers versions strings such as:
998 // 4.4
999 // 4.4.0
1000 // 4.4.x
1001 // 4.4.2-rc4
1002 // 4.4.x-patched
1003 // And retains any patch number it finds.
1004 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1005 if (!PatchText.empty()) {
1006 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
1007 // Try to parse the number and any suffix.
1008 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1009 GoodVersion.Patch < 0)
1010 return BadVersion;
1011 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
1012 }
1013 }
1014
1015 return GoodVersion;
1016}
1017
1018/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
1019bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
1020 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
1021 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
1022
1023 // Note that we rank versions with *no* patch specified is better than ones
1024 // hard-coding a patch version. Thus if the RHS has no patch, it always
1025 // wins, and the LHS only wins if it has no patch and the RHS does have
1026 // a patch.
1027 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
1028 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
Gabor Greif241cbe42012-04-18 10:59:08 +00001029 if (PatchSuffix == RHS.PatchSuffix) return false;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001030
1031 // Finally, between completely tied version numbers, the version with the
1032 // suffix loses as we prefer full releases.
1033 if (RHS.PatchSuffix.empty()) return true;
1034 return false;
1035}
1036
Rafael Espindola0e659592012-02-19 01:38:32 +00001037static StringRef getGCCToolchainDir(const ArgList &Args) {
1038 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1039 if (A)
1040 return A->getValue(Args);
1041 return GCC_INSTALL_PREFIX;
1042}
1043
Chandler Carruth19347ed2011-11-06 23:39:34 +00001044/// \brief Construct a GCCInstallationDetector from the driver.
1045///
1046/// This performs all of the autodetection and sets up the various paths.
Gabor Greif0407a042012-04-17 11:16:26 +00001047/// Once constructed, a GCCInstallationDetector is essentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001048///
1049/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1050/// should instead pull the target out of the driver. This is currently
1051/// necessary because the driver doesn't store the final version of the target
1052/// triple.
1053Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1054 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001055 const llvm::Triple &TargetTriple,
1056 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001057 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001058 llvm::Triple MultiarchTriple
1059 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1060 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001061 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001062 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001063 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001064 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001065 SmallVector<StringRef, 10> CandidateTripleAliases;
1066 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1067 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1068 CandidateTripleAliases,
1069 CandidateMultiarchLibDirs,
1070 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001071
1072 // Compute the set of prefixes for our search.
1073 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1074 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001075
Rafael Espindola0e659592012-02-19 01:38:32 +00001076 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1077 if (GCCToolchainDir != "") {
1078 if (GCCToolchainDir.back() == '/')
1079 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001080
Rafael Espindola0e659592012-02-19 01:38:32 +00001081 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001082 } else {
1083 Prefixes.push_back(D.SysRoot);
1084 Prefixes.push_back(D.SysRoot + "/usr");
1085 Prefixes.push_back(D.InstalledDir + "/..");
1086 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001087
1088 // Loop over the various components which exist and select the best GCC
1089 // installation available. GCC installs are ranked by version number.
1090 Version = GCCVersion::Parse("0.0.0");
1091 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1092 if (!llvm::sys::fs::exists(Prefixes[i]))
1093 continue;
1094 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1095 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1096 if (!llvm::sys::fs::exists(LibDir))
1097 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001098 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
1099 ScanLibDirForGCCTriple(TargetArch, LibDir, CandidateTripleAliases[k]);
1100 }
1101 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1102 const std::string LibDir
1103 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1104 if (!llvm::sys::fs::exists(LibDir))
1105 continue;
1106 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1107 ++k)
1108 ScanLibDirForGCCTriple(TargetArch, LibDir,
1109 CandidateMultiarchTripleAliases[k],
1110 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001111 }
1112 }
1113}
1114
1115/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001116 const llvm::Triple &TargetTriple,
1117 const llvm::Triple &MultiarchTriple,
1118 SmallVectorImpl<StringRef> &LibDirs,
1119 SmallVectorImpl<StringRef> &TripleAliases,
1120 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1121 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1122 // Declare a bunch of static data sets that we'll select between below. These
1123 // are specifically designed to always refer to string literals to avoid any
1124 // lifetime or initialization issues.
1125 static const char *const ARMLibDirs[] = { "/lib" };
1126 static const char *const ARMTriples[] = {
1127 "arm-linux-gnueabi",
1128 "arm-linux-androideabi"
1129 };
1130
1131 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1132 static const char *const X86_64Triples[] = {
1133 "x86_64-linux-gnu",
1134 "x86_64-unknown-linux-gnu",
1135 "x86_64-pc-linux-gnu",
1136 "x86_64-redhat-linux6E",
1137 "x86_64-redhat-linux",
1138 "x86_64-suse-linux",
1139 "x86_64-manbo-linux-gnu",
1140 "x86_64-linux-gnu",
1141 "x86_64-slackware-linux"
1142 };
1143 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1144 static const char *const X86Triples[] = {
1145 "i686-linux-gnu",
1146 "i686-pc-linux-gnu",
1147 "i486-linux-gnu",
1148 "i386-linux-gnu",
1149 "i686-redhat-linux",
1150 "i586-redhat-linux",
1151 "i386-redhat-linux",
1152 "i586-suse-linux",
Gabor Greif91720912012-05-15 11:21:03 +00001153 "i486-slackware-linux",
1154 "i686-montavista-linux"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001155 };
1156
1157 static const char *const MIPSLibDirs[] = { "/lib" };
1158 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1159 static const char *const MIPSELLibDirs[] = { "/lib" };
1160 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu" };
1161
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001162 static const char *const MIPS64LibDirs[] = { "/lib64", "/lib" };
1163 static const char *const MIPS64Triples[] = { "mips64-linux-gnu" };
1164 static const char *const MIPS64ELLibDirs[] = { "/lib64", "/lib" };
1165 static const char *const MIPS64ELTriples[] = { "mips64el-linux-gnu" };
1166
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001167 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1168 static const char *const PPCTriples[] = {
1169 "powerpc-linux-gnu",
1170 "powerpc-unknown-linux-gnu",
Gabor Greif91720912012-05-15 11:21:03 +00001171 "powerpc-suse-linux",
1172 "powerpc-montavista-linuxspe"
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001173 };
1174 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1175 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001176 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001177 "powerpc64-unknown-linux-gnu",
1178 "powerpc64-suse-linux",
1179 "ppc64-redhat-linux"
1180 };
1181
1182 switch (TargetTriple.getArch()) {
1183 case llvm::Triple::arm:
1184 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001185 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001186 TripleAliases.append(
1187 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1188 break;
1189 case llvm::Triple::x86_64:
1190 LibDirs.append(
1191 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1192 TripleAliases.append(
1193 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1194 MultiarchLibDirs.append(
1195 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1196 MultiarchTripleAliases.append(
1197 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1198 break;
1199 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001200 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001201 TripleAliases.append(
1202 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1203 MultiarchLibDirs.append(
1204 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1205 MultiarchTripleAliases.append(
1206 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1207 break;
1208 case llvm::Triple::mips:
1209 LibDirs.append(
1210 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1211 TripleAliases.append(
1212 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001213 MultiarchLibDirs.append(
1214 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1215 MultiarchTripleAliases.append(
1216 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001217 break;
1218 case llvm::Triple::mipsel:
1219 LibDirs.append(
1220 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1221 TripleAliases.append(
1222 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001223 MultiarchLibDirs.append(
1224 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1225 MultiarchTripleAliases.append(
1226 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1227 break;
1228 case llvm::Triple::mips64:
1229 LibDirs.append(
1230 MIPS64LibDirs, MIPS64LibDirs + llvm::array_lengthof(MIPS64LibDirs));
1231 TripleAliases.append(
1232 MIPS64Triples, MIPS64Triples + llvm::array_lengthof(MIPS64Triples));
1233 MultiarchLibDirs.append(
1234 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1235 MultiarchTripleAliases.append(
1236 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1237 break;
1238 case llvm::Triple::mips64el:
1239 LibDirs.append(
1240 MIPS64ELLibDirs, MIPS64ELLibDirs + llvm::array_lengthof(MIPS64ELLibDirs));
1241 TripleAliases.append(
1242 MIPS64ELTriples, MIPS64ELTriples + llvm::array_lengthof(MIPS64ELTriples));
1243 MultiarchLibDirs.append(
1244 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1245 MultiarchTripleAliases.append(
1246 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001247 break;
1248 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001249 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001250 TripleAliases.append(
1251 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1252 MultiarchLibDirs.append(
1253 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1254 MultiarchTripleAliases.append(
1255 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1256 break;
1257 case llvm::Triple::ppc64:
1258 LibDirs.append(
1259 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1260 TripleAliases.append(
1261 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1262 MultiarchLibDirs.append(
1263 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1264 MultiarchTripleAliases.append(
1265 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1266 break;
1267
1268 default:
1269 // By default, just rely on the standard lib directories and the original
1270 // triple.
1271 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001272 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001273
1274 // Always append the drivers target triple to the end, in case it doesn't
1275 // match any of our aliases.
1276 TripleAliases.push_back(TargetTriple.str());
1277
1278 // Also include the multiarch variant if it's different.
1279 if (TargetTriple.str() != MultiarchTriple.str())
1280 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001281}
1282
1283void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001284 llvm::Triple::ArchType TargetArch, const std::string &LibDir,
1285 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001286 // There are various different suffixes involving the triple we
1287 // check for. We also record what is necessary to walk from each back
1288 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001289 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001290 "/gcc/" + CandidateTriple.str(),
1291 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1292
1293 // Ubuntu has a strange mis-matched pair of triples that this happens to
1294 // match.
1295 // FIXME: It may be worthwhile to generalize this and look for a second
1296 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001297 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001298 };
1299 const std::string InstallSuffixes[] = {
1300 "/../../..",
1301 "/../../../..",
1302 "/../../../.."
1303 };
1304 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001305 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1306 (TargetArch != llvm::Triple::x86));
1307 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1308 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001309 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001310 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001311 !EC && LI != LE; LI = LI.increment(EC)) {
1312 StringRef VersionText = llvm::sys::path::filename(LI->path());
1313 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1314 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1315 if (CandidateVersion < MinVersion)
1316 continue;
1317 if (CandidateVersion <= Version)
1318 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001319
1320 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001321 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001322 // libs in a subdirectory named 64. The simple logic we follow is that
1323 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1324 // we use that. If not, and if not a multiarch triple, we look for
1325 // crtbegin.o without the subdirectory.
1326 StringRef MultiarchSuffix
1327 = (TargetArch == llvm::Triple::x86_64 ||
Simon Atanasyanb8c43812012-04-26 19:57:02 +00001328 TargetArch == llvm::Triple::ppc64 ||
1329 TargetArch == llvm::Triple::mips64 ||
1330 TargetArch == llvm::Triple::mips64el) ? "/64" : "/32";
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001331 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1332 GCCMultiarchSuffix = MultiarchSuffix.str();
1333 } else {
1334 if (NeedsMultiarchSuffix ||
1335 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1336 continue;
1337 GCCMultiarchSuffix.clear();
1338 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001339
1340 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001341 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001342 // FIXME: We hack together the directory name here instead of
1343 // using LI to ensure stable path separators across Windows and
1344 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001345 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001346 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001347 IsValid = true;
1348 }
1349 }
1350}
1351
Rafael Espindola0e659592012-02-19 01:38:32 +00001352Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1353 const ArgList &Args)
1354 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001355 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001356 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001357 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001358}
1359
Daniel Dunbar39176082009-03-20 00:20:03 +00001360Generic_GCC::~Generic_GCC() {
1361 // Free tool implementations.
1362 for (llvm::DenseMap<unsigned, Tool*>::iterator
1363 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1364 delete it->second;
1365}
1366
Mike Stump1eb44332009-09-09 15:08:12 +00001367Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001368 const JobAction &JA,
1369 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +00001370 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001371 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +00001372 Key = Action::AnalyzeJobClass;
1373 else
1374 Key = JA.getKind();
1375
1376 Tool *&T = Tools[Key];
1377 if (!T) {
1378 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001379 case Action::InputClass:
1380 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001381 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001382 case Action::PreprocessJobClass:
1383 T = new tools::gcc::Preprocess(*this); break;
1384 case Action::PrecompileJobClass:
1385 T = new tools::gcc::Precompile(*this); break;
1386 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001387 case Action::MigrateJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001388 T = new tools::Clang(*this); break;
1389 case Action::CompileJobClass:
1390 T = new tools::gcc::Compile(*this); break;
1391 case Action::AssembleJobClass:
1392 T = new tools::gcc::Assemble(*this); break;
1393 case Action::LinkJobClass:
1394 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001396 // This is a bit ungeneric, but the only platform using a driver
1397 // driver is Darwin.
1398 case Action::LipoJobClass:
1399 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001400 case Action::DsymutilJobClass:
1401 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001402 case Action::VerifyJobClass:
1403 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001404 }
1405 }
1406
1407 return *T;
1408}
1409
Daniel Dunbar39176082009-03-20 00:20:03 +00001410bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001411 // FIXME: Gross; we should probably have some separate target
1412 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +00001413 return getArchName() == "x86_64";
1414}
1415
1416const char *Generic_GCC::GetDefaultRelocationModel() const {
1417 return "static";
1418}
1419
1420const char *Generic_GCC::GetForcedPicModel() const {
1421 return 0;
1422}
Tony Linthicum96319392011-12-12 21:14:55 +00001423/// Hexagon Toolchain
1424
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001425Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple& Triple)
1426 : ToolChain(D, Triple) {
Tony Linthicum96319392011-12-12 21:14:55 +00001427 getProgramPaths().push_back(getDriver().getInstalledDir());
1428 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1429 getProgramPaths().push_back(getDriver().Dir);
1430}
1431
1432Hexagon_TC::~Hexagon_TC() {
1433 // Free tool implementations.
1434 for (llvm::DenseMap<unsigned, Tool*>::iterator
1435 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1436 delete it->second;
1437}
1438
1439Tool &Hexagon_TC::SelectTool(const Compilation &C,
1440 const JobAction &JA,
1441 const ActionList &Inputs) const {
1442 Action::ActionClass Key;
1443 // if (JA.getKind () == Action::CompileJobClass)
1444 // Key = JA.getKind ();
1445 // else
1446
1447 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1448 Key = Action::AnalyzeJobClass;
1449 else
1450 Key = JA.getKind();
1451 // if ((JA.getKind () == Action::CompileJobClass)
1452 // && (JA.getType () != types::TY_LTO_BC)) {
1453 // Key = JA.getKind ();
1454 // }
1455
1456 Tool *&T = Tools[Key];
1457 if (!T) {
1458 switch (Key) {
1459 case Action::InputClass:
1460 case Action::BindArchClass:
1461 assert(0 && "Invalid tool kind.");
1462 case Action::AnalyzeJobClass:
1463 T = new tools::Clang(*this); break;
1464 case Action::AssembleJobClass:
1465 T = new tools::hexagon::Assemble(*this); break;
1466 case Action::LinkJobClass:
1467 T = new tools::hexagon::Link(*this); break;
1468 default:
1469 assert(false && "Unsupported action for Hexagon target.");
1470 }
1471 }
1472
1473 return *T;
1474}
1475
1476bool Hexagon_TC::IsUnwindTablesDefault() const {
1477 // FIXME: Gross; we should probably have some separate target
1478 // definition, possibly even reusing the one in clang.
1479 return getArchName() == "x86_64";
1480}
1481
1482const char *Hexagon_TC::GetDefaultRelocationModel() const {
1483 return "static";
1484}
1485
1486const char *Hexagon_TC::GetForcedPicModel() const {
1487 return 0;
1488} // End Hexagon
1489
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001490
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001491/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1492/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1493/// Currently does not support anything else but compilation.
1494
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001495TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1496 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001497 // Path mangling to find libexec
1498 std::string Path(getDriver().Dir);
1499
1500 Path += "/../libexec";
1501 getProgramPaths().push_back(Path);
1502}
1503
1504TCEToolChain::~TCEToolChain() {
1505 for (llvm::DenseMap<unsigned, Tool*>::iterator
1506 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1507 delete it->second;
1508}
1509
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001510bool TCEToolChain::IsMathErrnoDefault() const {
1511 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001512}
1513
1514bool TCEToolChain::IsUnwindTablesDefault() const {
1515 return false;
1516}
1517
1518const char *TCEToolChain::GetDefaultRelocationModel() const {
1519 return "static";
1520}
1521
1522const char *TCEToolChain::GetForcedPicModel() const {
1523 return 0;
1524}
1525
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001526Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001527 const JobAction &JA,
1528 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001529 Action::ActionClass Key;
1530 Key = Action::AnalyzeJobClass;
1531
1532 Tool *&T = Tools[Key];
1533 if (!T) {
1534 switch (Key) {
1535 case Action::PreprocessJobClass:
1536 T = new tools::gcc::Preprocess(*this); break;
1537 case Action::AnalyzeJobClass:
1538 T = new tools::Clang(*this); break;
1539 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001540 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001541 }
1542 }
1543 return *T;
1544}
1545
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001546/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1547
Rafael Espindola0e659592012-02-19 01:38:32 +00001548OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1549 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001550 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001551 getFilePaths().push_back("/usr/lib");
1552}
1553
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001554Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1555 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001556 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001557 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001558 Key = Action::AnalyzeJobClass;
1559 else
1560 Key = JA.getKind();
1561
Rafael Espindoladda5b922010-11-07 23:13:01 +00001562 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1563 options::OPT_no_integrated_as,
1564 IsIntegratedAssemblerDefault());
1565
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001566 Tool *&T = Tools[Key];
1567 if (!T) {
1568 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001569 case Action::AssembleJobClass: {
1570 if (UseIntegratedAs)
1571 T = new tools::ClangAs(*this);
1572 else
1573 T = new tools::openbsd::Assemble(*this);
1574 break;
1575 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001576 case Action::LinkJobClass:
1577 T = new tools::openbsd::Link(*this); break;
1578 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001579 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001580 }
1581 }
1582
1583 return *T;
1584}
1585
Daniel Dunbar75358d22009-03-30 21:06:03 +00001586/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1587
Rafael Espindola0e659592012-02-19 01:38:32 +00001588FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1589 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001590
Chandler Carruth24248e32012-01-26 01:35:15 +00001591 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1592 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001593 if ((Triple.getArch() == llvm::Triple::x86 ||
1594 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001595 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001596 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1597 else
1598 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001599}
1600
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001601Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1602 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001603 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001604 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001605 Key = Action::AnalyzeJobClass;
1606 else
1607 Key = JA.getKind();
1608
Roman Divacky67dece72010-11-08 17:46:39 +00001609 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1610 options::OPT_no_integrated_as,
1611 IsIntegratedAssemblerDefault());
1612
Daniel Dunbar75358d22009-03-30 21:06:03 +00001613 Tool *&T = Tools[Key];
1614 if (!T) {
1615 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001616 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001617 if (UseIntegratedAs)
1618 T = new tools::ClangAs(*this);
1619 else
1620 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001621 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001622 case Action::LinkJobClass:
1623 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001624 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001625 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001626 }
1627 }
1628
1629 return *T;
1630}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001631
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001632/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1633
Rafael Espindola0e659592012-02-19 01:38:32 +00001634NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1635 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001636
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001637 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001638 // When targeting a 32-bit platform, try the special directory used on
1639 // 64-bit hosts, and only fall back to the main library directory if that
1640 // doesn't work.
1641 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1642 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001643 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001644 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001645
1646 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001647 }
1648}
1649
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001650Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1651 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001652 Action::ActionClass Key;
1653 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1654 Key = Action::AnalyzeJobClass;
1655 else
1656 Key = JA.getKind();
1657
1658 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1659 options::OPT_no_integrated_as,
1660 IsIntegratedAssemblerDefault());
1661
1662 Tool *&T = Tools[Key];
1663 if (!T) {
1664 switch (Key) {
1665 case Action::AssembleJobClass:
1666 if (UseIntegratedAs)
1667 T = new tools::ClangAs(*this);
1668 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001669 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001670 break;
1671 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001672 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001673 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001674 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001675 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001676 }
1677 }
1678
1679 return *T;
1680}
1681
Chris Lattner38e317d2010-07-07 16:01:42 +00001682/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1683
Rafael Espindola0e659592012-02-19 01:38:32 +00001684Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1685 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001686 getFilePaths().push_back(getDriver().Dir + "/../lib");
1687 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001688}
1689
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001690Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1691 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001692 Action::ActionClass Key;
1693 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1694 Key = Action::AnalyzeJobClass;
1695 else
1696 Key = JA.getKind();
1697
1698 Tool *&T = Tools[Key];
1699 if (!T) {
1700 switch (Key) {
1701 case Action::AssembleJobClass:
1702 T = new tools::minix::Assemble(*this); break;
1703 case Action::LinkJobClass:
1704 T = new tools::minix::Link(*this); break;
1705 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001706 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001707 }
1708 }
1709
1710 return *T;
1711}
1712
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001713/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1714
Rafael Espindola0e659592012-02-19 01:38:32 +00001715AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1716 const ArgList &Args)
1717 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001718
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001719 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001720 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001721 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001722
Daniel Dunbaree788e72009-12-21 18:54:17 +00001723 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001724 getFilePaths().push_back("/usr/lib");
1725 getFilePaths().push_back("/usr/sfw/lib");
1726 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001727 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001728
1729}
1730
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001731Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1732 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001733 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001734 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001735 Key = Action::AnalyzeJobClass;
1736 else
1737 Key = JA.getKind();
1738
1739 Tool *&T = Tools[Key];
1740 if (!T) {
1741 switch (Key) {
1742 case Action::AssembleJobClass:
1743 T = new tools::auroraux::Assemble(*this); break;
1744 case Action::LinkJobClass:
1745 T = new tools::auroraux::Link(*this); break;
1746 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001747 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001748 }
1749 }
1750
1751 return *T;
1752}
1753
David Chisnall31c46902012-02-15 13:39:01 +00001754/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1755
Rafael Espindola0e659592012-02-19 01:38:32 +00001756Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1757 const ArgList &Args)
1758 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001759
1760 getProgramPaths().push_back(getDriver().getInstalledDir());
1761 if (getDriver().getInstalledDir() != getDriver().Dir)
1762 getProgramPaths().push_back(getDriver().Dir);
1763
1764 getFilePaths().push_back(getDriver().Dir + "/../lib");
1765 getFilePaths().push_back("/usr/lib");
1766}
1767
1768Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
1769 const ActionList &Inputs) const {
1770 Action::ActionClass Key;
1771 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1772 Key = Action::AnalyzeJobClass;
1773 else
1774 Key = JA.getKind();
1775
1776 Tool *&T = Tools[Key];
1777 if (!T) {
1778 switch (Key) {
1779 case Action::AssembleJobClass:
1780 T = new tools::solaris::Assemble(*this); break;
1781 case Action::LinkJobClass:
1782 T = new tools::solaris::Link(*this); break;
1783 default:
1784 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1785 }
1786 }
1787
1788 return *T;
1789}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001790
Eli Friedman6b3454a2009-05-26 07:52:18 +00001791/// Linux toolchain (very bare-bones at the moment).
1792
Rafael Espindolac1da9812010-11-07 20:14:31 +00001793enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001794 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001795 DebianLenny,
1796 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001797 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001798 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001799 RHEL4,
1800 RHEL5,
1801 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001802 Fedora13,
1803 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001804 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001805 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001806 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001807 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001808 OpenSuse11_4,
1809 OpenSuse12_1,
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001810 OpenSuse12_2,
Douglas Gregor814638e2011-03-14 15:39:50 +00001811 UbuntuHardy,
1812 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001813 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001814 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001815 UbuntuLucid,
1816 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001817 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001818 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001819 UbuntuPrecise,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001820 UnknownDistro
1821};
1822
Chris Lattnerd753b562011-05-22 05:36:06 +00001823static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001824 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1825 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001826}
1827
1828static bool IsOpenSuse(enum LinuxDistro Distro) {
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001829 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_2;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001830}
1831
1832static bool IsDebian(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001833 return Distro >= DebianLenny && Distro <= DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001834}
1835
1836static bool IsUbuntu(enum LinuxDistro Distro) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001837 return Distro >= UbuntuHardy && Distro <= UbuntuPrecise;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001838}
1839
Rafael Espindolac1da9812010-11-07 20:14:31 +00001840static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001841 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001842 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001843 StringRef Data = File.get()->getBuffer();
1844 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001845 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001846 LinuxDistro Version = UnknownDistro;
1847 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
1848 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
1849 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
1850 .Case("hardy", UbuntuHardy)
1851 .Case("intrepid", UbuntuIntrepid)
1852 .Case("jaunty", UbuntuJaunty)
1853 .Case("karmic", UbuntuKarmic)
1854 .Case("lucid", UbuntuLucid)
1855 .Case("maverick", UbuntuMaverick)
1856 .Case("natty", UbuntuNatty)
1857 .Case("oneiric", UbuntuOneiric)
1858 .Case("precise", UbuntuPrecise)
1859 .Default(UnknownDistro);
1860 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001861 }
1862
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001863 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001864 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001865 if (Data.startswith("Fedora release 16"))
1866 return Fedora16;
1867 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00001868 return Fedora15;
1869 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001870 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001871 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001872 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001873 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001874 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001875 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001876 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001877 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001878 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001879 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1880 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001881 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001882 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001883 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1884 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001885 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001886 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001887 return UnknownDistro;
1888 }
1889
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001890 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001891 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001892 if (Data[0] == '5')
1893 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001894 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00001895 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001896 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00001897 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001898 return UnknownDistro;
1899 }
1900
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001901 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
1902 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
1903 .StartsWith("openSUSE 11.3", OpenSuse11_3)
1904 .StartsWith("openSUSE 11.4", OpenSuse11_4)
1905 .StartsWith("openSUSE 12.1", OpenSuse12_1)
Douglas Gregor4e1b2922012-04-30 23:42:57 +00001906 .StartsWith("openSUSE 12.2", OpenSuse12_2)
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001907 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001908
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001909 bool Exists;
1910 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001911 return Exherbo;
1912
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001913 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1914 return ArchLinux;
1915
Rafael Espindolac1da9812010-11-07 20:14:31 +00001916 return UnknownDistro;
1917}
1918
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001919/// \brief Get our best guess at the multiarch triple for a target.
1920///
1921/// Debian-based systems are starting to use a multiarch setup where they use
1922/// a target-triple directory in the library and header search paths.
1923/// Unfortunately, this triple does not align with the vanilla target triple,
1924/// so we provide a rough mapping here.
1925static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
1926 StringRef SysRoot) {
1927 // For most architectures, just use whatever we have rather than trying to be
1928 // clever.
1929 switch (TargetTriple.getArch()) {
1930 default:
1931 return TargetTriple.str();
1932
1933 // We use the existence of '/lib/<triple>' as a directory to detect some
1934 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00001935 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
1936 // regardless of what the actual target triple is.
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001937 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001938 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
1939 return "i386-linux-gnu";
1940 return TargetTriple.str();
1941 case llvm::Triple::x86_64:
1942 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
1943 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001944 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00001945 case llvm::Triple::mips:
1946 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
1947 return "mips-linux-gnu";
1948 return TargetTriple.str();
1949 case llvm::Triple::mipsel:
1950 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
1951 return "mipsel-linux-gnu";
1952 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00001953 case llvm::Triple::ppc:
1954 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
1955 return "powerpc-linux-gnu";
1956 return TargetTriple.str();
1957 case llvm::Triple::ppc64:
1958 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
1959 return "powerpc64-linux-gnu";
1960 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001961 }
1962}
1963
Chandler Carruth00646ba2012-01-25 11:24:24 +00001964static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
1965 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
1966}
1967
Rafael Espindola0e659592012-02-19 01:38:32 +00001968Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
1969 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00001970 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00001971 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001972
Rafael Espindolaab784082011-09-01 16:25:49 +00001973 // OpenSuse stores the linker with the compiler, add that to the search
1974 // path.
1975 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00001976 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00001977 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00001978
1979 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00001980
1981 LinuxDistro Distro = DetectLinuxDistro(Arch);
1982
Chris Lattner64a89172011-05-22 16:45:07 +00001983 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00001984 ExtraOpts.push_back("-z");
1985 ExtraOpts.push_back("relro");
1986 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001987
Douglas Gregorf0594d82011-03-06 19:11:49 +00001988 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001989 ExtraOpts.push_back("-X");
1990
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00001991 const bool IsMips = Arch == llvm::Triple::mips ||
1992 Arch == llvm::Triple::mipsel ||
1993 Arch == llvm::Triple::mips64 ||
1994 Arch == llvm::Triple::mips64el;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001995
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00001996 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::ANDROIDEABI;
1997
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00001998 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
1999 // and the MIPS ABI require .dynsym to be sorted in different ways.
2000 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2001 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002002 // Android loader does not support .gnu.hash.
2003 if (!IsMips && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002004 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2005 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002006 ExtraOpts.push_back("--hash-style=gnu");
2007
2008 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2009 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2010 ExtraOpts.push_back("--hash-style=both");
2011 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002012
Chris Lattnerd753b562011-05-22 05:36:06 +00002013 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002014 ExtraOpts.push_back("--no-add-needed");
2015
Eli Friedman0b200f62011-06-02 21:36:53 +00002016 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002017 IsOpenSuse(Distro) ||
2018 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002019 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002020 ExtraOpts.push_back("--build-id");
2021
Chris Lattner64a89172011-05-22 16:45:07 +00002022 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002023 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002024
Chandler Carruthd2deee12011-10-03 05:28:29 +00002025 // The selection of paths to try here is designed to match the patterns which
2026 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2027 // This was determined by running GCC in a fake filesystem, creating all
2028 // possible permutations of these directories, and seeing which ones it added
2029 // to the link paths.
2030 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002031
Chandler Carruthd747efa2012-02-11 03:31:12 +00002032 const std::string Multilib = Triple.isArch32Bit() ? "lib32" : "lib64";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002033 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002034
Chandler Carruthd1f73062011-11-06 23:09:05 +00002035 // Add the multilib suffixed paths where they are available.
2036 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002037 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002038 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002039 addPathIfExists((GCCInstallation.getInstallPath() +
2040 GCCInstallation.getMultiarchSuffix()),
2041 Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002042
2043 // If the GCC installation we found is inside of the sysroot, we want to
2044 // prefer libraries installed in the parent prefix of the GCC installation.
2045 // It is important to *not* use these paths when the GCC installation is
Gabor Greif241cbe42012-04-18 10:59:08 +00002046 // outside of the system root as that can pick up unintended libraries.
Chandler Carruth9f314372012-04-06 16:32:06 +00002047 // This usually happens when there is an external cross compiler on the
2048 // host system, and a more minimal sysroot available that is the target of
2049 // the cross.
2050 if (StringRef(LibPath).startswith(SysRoot)) {
2051 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
2052 Paths);
2053 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2054 addPathIfExists(LibPath + "/../" + Multilib, Paths);
2055 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002056 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002057 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2058 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2059 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2060 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2061
2062 // Try walking via the GCC triple path in case of multiarch GCC
2063 // installations with strange symlinks.
2064 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002065 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002066 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002067
Chandler Carruth7a09d012011-10-16 10:54:30 +00002068 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002069 if (GCCInstallation.isValid()) {
2070 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002071 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002072 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002073 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruth9f314372012-04-06 16:32:06 +00002074
2075 if (StringRef(LibPath).startswith(SysRoot)) {
2076 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
2077 addPathIfExists(LibPath, Paths);
2078 }
Chandler Carruthd2deee12011-10-03 05:28:29 +00002079 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002080 addPathIfExists(SysRoot + "/lib", Paths);
2081 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002082}
2083
2084bool Linux::HasNativeLLVMSupport() const {
2085 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002086}
2087
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002088Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2089 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002090 Action::ActionClass Key;
2091 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2092 Key = Action::AnalyzeJobClass;
2093 else
2094 Key = JA.getKind();
2095
Rafael Espindoladda5b922010-11-07 23:13:01 +00002096 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2097 options::OPT_no_integrated_as,
2098 IsIntegratedAssemblerDefault());
2099
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002100 Tool *&T = Tools[Key];
2101 if (!T) {
2102 switch (Key) {
2103 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002104 if (UseIntegratedAs)
2105 T = new tools::ClangAs(*this);
2106 else
2107 T = new tools::linuxtools::Assemble(*this);
2108 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002109 case Action::LinkJobClass:
2110 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002111 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002112 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002113 }
2114 }
2115
2116 return *T;
2117}
2118
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002119void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2120 ArgStringList &CC1Args) const {
2121 const Driver &D = getDriver();
2122
2123 if (DriverArgs.hasArg(options::OPT_nostdinc))
2124 return;
2125
2126 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2127 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2128
2129 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002130 llvm::sys::Path P(D.ResourceDir);
2131 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002132 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002133 }
2134
2135 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2136 return;
2137
2138 // Check for configure-time C include directories.
2139 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2140 if (CIncludeDirs != "") {
2141 SmallVector<StringRef, 5> dirs;
2142 CIncludeDirs.split(dirs, ":");
2143 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2144 I != E; ++I) {
2145 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2146 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2147 }
2148 return;
2149 }
2150
2151 // Lacking those, try to detect the correct set of system includes for the
2152 // target triple.
2153
Chandler Carrutha4630892011-11-06 08:21:07 +00002154 // Implement generic Debian multiarch support.
2155 const StringRef X86_64MultiarchIncludeDirs[] = {
2156 "/usr/include/x86_64-linux-gnu",
2157
2158 // FIXME: These are older forms of multiarch. It's not clear that they're
2159 // in use in any released version of Debian, so we should consider
2160 // removing them.
2161 "/usr/include/i686-linux-gnu/64",
2162 "/usr/include/i486-linux-gnu/64"
2163 };
2164 const StringRef X86MultiarchIncludeDirs[] = {
2165 "/usr/include/i386-linux-gnu",
2166
2167 // FIXME: These are older forms of multiarch. It's not clear that they're
2168 // in use in any released version of Debian, so we should consider
2169 // removing them.
2170 "/usr/include/x86_64-linux-gnu/32",
2171 "/usr/include/i686-linux-gnu",
2172 "/usr/include/i486-linux-gnu"
2173 };
2174 const StringRef ARMMultiarchIncludeDirs[] = {
2175 "/usr/include/arm-linux-gnueabi"
2176 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002177 const StringRef MIPSMultiarchIncludeDirs[] = {
2178 "/usr/include/mips-linux-gnu"
2179 };
2180 const StringRef MIPSELMultiarchIncludeDirs[] = {
2181 "/usr/include/mipsel-linux-gnu"
2182 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002183 const StringRef PPCMultiarchIncludeDirs[] = {
2184 "/usr/include/powerpc-linux-gnu"
2185 };
2186 const StringRef PPC64MultiarchIncludeDirs[] = {
2187 "/usr/include/powerpc64-linux-gnu"
2188 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002189 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002190 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002191 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002192 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002193 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002194 } else if (getTriple().getArch() == llvm::Triple::arm) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002195 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002196 } else if (getTriple().getArch() == llvm::Triple::mips) {
2197 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2198 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2199 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002200 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2201 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2202 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2203 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002204 }
2205 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2206 E = MultiarchIncludeDirs.end();
2207 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002208 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002209 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2210 break;
2211 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002212 }
2213
2214 if (getTriple().getOS() == llvm::Triple::RTEMS)
2215 return;
2216
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002217 // Add an include of '/include' directly. This isn't provided by default by
2218 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2219 // add even when Clang is acting as-if it were a system compiler.
2220 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2221
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002222 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2223}
2224
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002225/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2226/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2227 const ArgList &DriverArgs,
2228 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002229 if (!llvm::sys::fs::exists(Base))
2230 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002231 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002232 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002233 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002234 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002235}
2236
2237void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2238 ArgStringList &CC1Args) const {
2239 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2240 DriverArgs.hasArg(options::OPT_nostdincxx))
2241 return;
2242
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002243 // Check if libc++ has been enabled and provide its include paths if so.
2244 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2245 // libc++ is always installed at a fixed path on Linux currently.
2246 addSystemInclude(DriverArgs, CC1Args,
2247 getDriver().SysRoot + "/usr/include/c++/v1");
2248 return;
2249 }
2250
Chandler Carruthfc52f752012-01-25 08:04:13 +00002251 // We need a detected GCC installation on Linux to provide libstdc++'s
2252 // headers. We handled the libc++ case above.
2253 if (!GCCInstallation.isValid())
2254 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002255
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002256 // By default, look for the C++ headers in an include directory adjacent to
2257 // the lib directory of the GCC installation. Note that this is expect to be
2258 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2259 StringRef LibDir = GCCInstallation.getParentLibPath();
2260 StringRef InstallDir = GCCInstallation.getInstallPath();
2261 StringRef Version = GCCInstallation.getVersion();
2262 if (!addLibStdCXXIncludePaths(LibDir + "/../include/c++/" + Version,
Chandler Carruthfc52f752012-01-25 08:04:13 +00002263 (GCCInstallation.getTriple().str() +
2264 GCCInstallation.getMultiarchSuffix()),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002265 DriverArgs, CC1Args)) {
2266 // Gentoo is weird and places its headers inside the GCC install, so if the
2267 // first attempt to find the headers fails, try this pattern.
2268 addLibStdCXXIncludePaths(InstallDir + "/include/g++-v4",
Chandler Carruthfc52f752012-01-25 08:04:13 +00002269 (GCCInstallation.getTriple().str() +
2270 GCCInstallation.getMultiarchSuffix()),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002271 DriverArgs, CC1Args);
2272 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002273}
2274
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002275/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2276
Rafael Espindola0e659592012-02-19 01:38:32 +00002277DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2278 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002279
2280 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002281 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002282 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002283 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002284
Daniel Dunbaree788e72009-12-21 18:54:17 +00002285 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002286 getFilePaths().push_back("/usr/lib");
2287 getFilePaths().push_back("/usr/lib/gcc41");
2288}
2289
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002290Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2291 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002292 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00002293 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002294 Key = Action::AnalyzeJobClass;
2295 else
2296 Key = JA.getKind();
2297
2298 Tool *&T = Tools[Key];
2299 if (!T) {
2300 switch (Key) {
2301 case Action::AssembleJobClass:
2302 T = new tools::dragonfly::Assemble(*this); break;
2303 case Action::LinkJobClass:
2304 T = new tools::dragonfly::Link(*this); break;
2305 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002306 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002307 }
2308 }
2309
2310 return *T;
2311}