blob: a45722315e4337649ddf6df9294579cea79a0466 [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;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000062}
63
Daniel Dunbar41800112010-08-02 05:43:56 +000064types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
65 types::ID Ty = types::lookupTypeForExtension(Ext);
66
67 // Darwin always preprocesses assembly files (unless -x is used explicitly).
68 if (Ty == types::TY_PP_Asm)
69 return types::TY_Asm;
70
71 return Ty;
72}
73
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000074bool Darwin::HasNativeLLVMSupport() const {
75 return true;
76}
77
John McCall9f084a32011-07-06 00:26:06 +000078bool Darwin::hasARCRuntime() const {
John McCallf85e1932011-06-15 23:02:42 +000079 // FIXME: Remove this once there is a proper way to detect an ARC runtime
80 // for the simulator.
81 switch (ARCRuntimeForSimulator) {
82 case ARCSimulator_None:
83 break;
84 case ARCSimulator_HasARCRuntime:
85 return true;
86 case ARCSimulator_NoARCRuntime:
87 return false;
88 }
89
90 if (isTargetIPhoneOS())
91 return !isIPhoneOSVersionLT(5);
92 else
93 return !isMacosxVersionLT(10, 7);
94}
95
Ted Kremenekebcb57a2012-03-06 20:05:56 +000096bool Darwin::hasSubscriptingRuntime() const {
97 return !isTargetIPhoneOS() && !isMacosxVersionLT(10, 8);
98}
99
John McCall9f084a32011-07-06 00:26:06 +0000100/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
101void Darwin::configureObjCRuntime(ObjCRuntime &runtime) const {
102 if (runtime.getKind() != ObjCRuntime::NeXT)
103 return ToolChain::configureObjCRuntime(runtime);
104
105 runtime.HasARC = runtime.HasWeak = hasARCRuntime();
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000106 runtime.HasSubscripting = hasSubscriptingRuntime();
John McCall256a76e2011-07-06 01:22:26 +0000107
108 // So far, objc_terminate is only available in iOS 5.
109 // FIXME: do the simulator logic properly.
110 if (!ARCRuntimeForSimulator && isTargetIPhoneOS())
111 runtime.HasTerminate = !isIPhoneOSVersionLT(5);
112 else
113 runtime.HasTerminate = false;
John McCall9f084a32011-07-06 00:26:06 +0000114}
115
John McCall13db5cf2011-09-09 20:41:01 +0000116/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
117bool Darwin::hasBlocksRuntime() const {
118 if (isTargetIPhoneOS())
119 return !isIPhoneOSVersionLT(3, 2);
120 else
121 return !isMacosxVersionLT(10, 6);
122}
123
Chris Lattner5f9e2722011-07-23 10:55:15 +0000124static const char *GetArmArchForMArch(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000125 return llvm::StringSwitch<const char*>(Value)
126 .Case("armv6k", "armv6")
127 .Case("armv5tej", "armv5")
128 .Case("xscale", "xscale")
129 .Case("armv4t", "armv4t")
130 .Case("armv7", "armv7")
131 .Cases("armv7a", "armv7-a", "armv7")
132 .Cases("armv7r", "armv7-r", "armv7")
133 .Cases("armv7m", "armv7-m", "armv7")
134 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000135}
136
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137static const char *GetArmArchForMCpu(StringRef Value) {
Bob Wilsona59956b2011-10-07 00:37:57 +0000138 return llvm::StringSwitch<const char *>(Value)
139 .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
140 .Cases("arm10e", "arm10tdmi", "armv5")
141 .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
142 .Case("xscale", "xscale")
143 .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s",
144 "arm1176jzf-s", "cortex-m0", "armv6")
145 .Cases("cortex-a8", "cortex-r4", "cortex-m3", "cortex-a9", "armv7")
146 .Default(0);
Daniel Dunbareeff4062010-01-22 02:04:58 +0000147}
148
Chris Lattner5f9e2722011-07-23 10:55:15 +0000149StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +0000150 switch (getTriple().getArch()) {
151 default:
152 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000153
Douglas Gregorf0594d82011-03-06 19:11:49 +0000154 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000155 case llvm::Triple::arm: {
156 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
157 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
158 return Arch;
159
160 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
161 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
162 return Arch;
163
164 return "arm";
165 }
166 }
167}
168
Daniel Dunbarf3955282009-09-04 18:34:51 +0000169Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000170 // Free tool implementations.
171 for (llvm::DenseMap<unsigned, Tool*>::iterator
172 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
173 delete it->second;
174}
175
Chad Rosier61ab80a2011-09-20 20:44:06 +0000176std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
177 types::ID InputType) const {
178 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000179
180 // If the target isn't initialized (e.g., an unknown Darwin platform, return
181 // the default triple).
182 if (!isTargetInitialized())
183 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000184
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000185 unsigned Version[3];
186 getTargetVersion(Version);
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000187
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000188 SmallString<16> Str;
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000189 llvm::raw_svector_ostream(Str)
Daniel Dunbar659d23a2011-04-19 23:34:17 +0000190 << (isTargetIPhoneOS() ? "ios" : "macosx")
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000191 << Version[0] << "." << Version[1] << "." << Version[2];
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000192 Triple.setOSName(Str.str());
193
194 return Triple.getTriple();
195}
196
David Blaikie99ba9e32011-12-20 02:48:34 +0000197void Generic_ELF::anchor() {}
198
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000199Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
200 const ActionList &Inputs) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000201 Action::ActionClass Key;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000202
203 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
204 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
205 if (Inputs.size() == 1 &&
206 types::isCXX(Inputs[0]->getType()) &&
Bob Wilson905c45f2011-10-14 05:03:44 +0000207 getTriple().isOSDarwin() &&
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000208 getTriple().getArch() == llvm::Triple::x86 &&
Bob Wilsona544aee2011-08-13 23:48:55 +0000209 (C.getArgs().getLastArg(options::OPT_fapple_kext) ||
210 C.getArgs().getLastArg(options::OPT_mkernel)))
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000211 Key = JA.getKind();
212 else
213 Key = Action::AnalyzeJobClass;
214 } else
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000215 Key = JA.getKind();
216
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000217 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
218 options::OPT_no_integrated_as,
Bob Wilson1a1764b2011-10-30 00:20:28 +0000219 IsIntegratedAssemblerDefault());
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000220
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000221 Tool *&T = Tools[Key];
222 if (!T) {
223 switch (Key) {
224 case Action::InputClass:
225 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000226 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000227 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000228 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000229 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +0000230 case Action::MigrateJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000231 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000232 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000233 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000234 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000235 case Action::AssembleJobClass: {
236 if (UseIntegratedAs)
237 T = new tools::ClangAs(*this);
238 else
239 T = new tools::darwin::Assemble(*this);
240 break;
241 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000242 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000243 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000244 case Action::LipoJobClass:
245 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000246 case Action::DsymutilJobClass:
247 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +0000248 case Action::VerifyJobClass:
249 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000250 }
251 }
252
253 return *T;
254}
255
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000256
Chandler Carruth1d16f0f2012-01-31 02:21:20 +0000257DarwinClang::DarwinClang(const Driver &D, const llvm::Triple& Triple)
258 : Darwin(D, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000259{
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000260 getProgramPaths().push_back(getDriver().getInstalledDir());
261 if (getDriver().getInstalledDir() != getDriver().Dir)
262 getProgramPaths().push_back(getDriver().Dir);
263
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000264 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000265 getProgramPaths().push_back(getDriver().getInstalledDir());
266 if (getDriver().getInstalledDir() != getDriver().Dir)
267 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000268
269 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000270 // also add the GCC libexec paths. This is legacy code that can be removed
271 // once fallback is no longer useful.
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000272 AddGCCLibexecPath(DarwinVersion[0]);
273 AddGCCLibexecPath(DarwinVersion[0] - 2);
274 AddGCCLibexecPath(DarwinVersion[0] - 1);
275 AddGCCLibexecPath(DarwinVersion[0] + 1);
276 AddGCCLibexecPath(DarwinVersion[0] + 2);
277}
278
279void DarwinClang::AddGCCLibexecPath(unsigned darwinVersion) {
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000280 std::string ToolChainDir = "i686-apple-darwin";
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000281 ToolChainDir += llvm::utostr(darwinVersion);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000282 ToolChainDir += "/4.2.1";
283
284 std::string Path = getDriver().Dir;
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000285 Path += "/../llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000286 Path += ToolChainDir;
287 getProgramPaths().push_back(Path);
288
Bob Wilson8aa76ea2011-09-20 22:00:38 +0000289 Path = "/usr/llvm-gcc-4.2/libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000290 Path += ToolChainDir;
291 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000292}
293
294void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
295 ArgStringList &CmdArgs) const {
296 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000297
298 // Unfortunately, we still might depend on a few of the libraries that are
299 // only available in the gcc library directory (in particular
300 // libstdc++.dylib). For now, hardcode the path to the known install location.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000301 // FIXME: This should get ripped out someday. However, when building on
302 // 10.6 (darwin10), we're still relying on this to find libstdc++.dylib.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000303 llvm::sys::Path P(getDriver().Dir);
304 P.eraseComponent(); // .../usr/bin -> ../usr
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000305 P.appendComponent("llvm-gcc-4.2");
Daniel Dunbar424b6612010-06-30 23:56:13 +0000306 P.appendComponent("lib");
307 P.appendComponent("gcc");
308 switch (getTriple().getArch()) {
309 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000310 llvm_unreachable("Invalid Darwin arch!");
Daniel Dunbar424b6612010-06-30 23:56:13 +0000311 case llvm::Triple::x86:
312 case llvm::Triple::x86_64:
313 P.appendComponent("i686-apple-darwin10");
314 break;
315 case llvm::Triple::arm:
316 case llvm::Triple::thumb:
317 P.appendComponent("arm-apple-darwin10");
318 break;
319 case llvm::Triple::ppc:
320 case llvm::Triple::ppc64:
321 P.appendComponent("powerpc-apple-darwin10");
322 break;
323 }
324 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000325
326 // Determine the arch specific GCC subdirectory.
327 const char *ArchSpecificDir = 0;
328 switch (getTriple().getArch()) {
329 default:
330 break;
331 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000332 case llvm::Triple::thumb: {
333 std::string Triple = ComputeLLVMTriple(Args);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000334 StringRef TripleStr = Triple;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000335 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
336 ArchSpecificDir = "v5";
337 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
338 ArchSpecificDir = "v6";
339 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
340 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000341 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000342 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000343 case llvm::Triple::ppc64:
344 ArchSpecificDir = "ppc64";
345 break;
346 case llvm::Triple::x86_64:
347 ArchSpecificDir = "x86_64";
348 break;
349 }
350
351 if (ArchSpecificDir) {
352 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000353 bool Exists;
354 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000355 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
356 P.eraseComponent();
357 }
358
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000359 bool Exists;
360 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000361 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000362}
363
John McCallf85e1932011-06-15 23:02:42 +0000364void DarwinClang::AddLinkARCArgs(const ArgList &Args,
365 ArgStringList &CmdArgs) const {
Eric Christopherf8571862011-08-23 17:56:55 +0000366
367 CmdArgs.push_back("-force_load");
John McCallf85e1932011-06-15 23:02:42 +0000368 llvm::sys::Path P(getDriver().ClangExecutable);
369 P.eraseComponent(); // 'clang'
370 P.eraseComponent(); // 'bin'
371 P.appendComponent("lib");
372 P.appendComponent("arc");
373 P.appendComponent("libarclite_");
374 std::string s = P.str();
375 // Mash in the platform.
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000376 if (isTargetIOSSimulator())
377 s += "iphonesimulator";
378 else if (isTargetIPhoneOS())
John McCallf85e1932011-06-15 23:02:42 +0000379 s += "iphoneos";
Argyrios Kyrtzidisc19981c2011-10-18 17:40:15 +0000380 // FIXME: Remove this once we depend fully on -mios-simulator-version-min.
John McCallf85e1932011-06-15 23:02:42 +0000381 else if (ARCRuntimeForSimulator != ARCSimulator_None)
382 s += "iphonesimulator";
383 else
384 s += "macosx";
385 s += ".a";
386
387 CmdArgs.push_back(Args.MakeArgString(s));
388}
389
Eric Christopher3404fe72011-06-22 17:41:40 +0000390void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
Eric Christopherf8571862011-08-23 17:56:55 +0000391 ArgStringList &CmdArgs,
Eric Christopher3404fe72011-06-22 17:41:40 +0000392 const char *DarwinStaticLib) const {
393 llvm::sys::Path P(getDriver().ResourceDir);
394 P.appendComponent("lib");
395 P.appendComponent("darwin");
396 P.appendComponent(DarwinStaticLib);
Eric Christopherf8571862011-08-23 17:56:55 +0000397
Eric Christopher3404fe72011-06-22 17:41:40 +0000398 // For now, allow missing resource libraries to support developers who may
399 // not have compiler-rt checked out or integrated into their build.
400 bool Exists;
401 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
402 CmdArgs.push_back(Args.MakeArgString(P.str()));
403}
404
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000405void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
406 ArgStringList &CmdArgs) const {
Daniel Dunbarc24767c2011-12-07 23:03:15 +0000407 // Darwin only supports the compiler-rt based runtime libraries.
408 switch (GetRuntimeLibType(Args)) {
409 case ToolChain::RLT_CompilerRT:
410 break;
411 default:
412 getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform)
413 << Args.getLastArg(options::OPT_rtlib_EQ)->getValue(Args) << "darwin";
414 return;
415 }
416
Daniel Dunbareec99102010-01-22 03:38:14 +0000417 // Darwin doesn't support real static executables, don't link any runtime
418 // libraries with -static.
419 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000420 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000421
422 // Reject -static-libgcc for now, we can deal with this when and if someone
423 // cares. This is useful in situations where someone wants to statically link
424 // something like libstdc++, and needs its runtime support routines.
425 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000426 getDriver().Diag(diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000427 << A->getAsString(Args);
428 return;
429 }
430
Daniel Dunbarf4714872011-11-17 00:36:57 +0000431 // If we are building profile support, link that library in.
432 if (Args.hasArg(options::OPT_fprofile_arcs) ||
433 Args.hasArg(options::OPT_fprofile_generate) ||
434 Args.hasArg(options::OPT_fcreate_profile) ||
435 Args.hasArg(options::OPT_coverage)) {
436 // Select the appropriate runtime library for the target.
437 if (isTargetIPhoneOS()) {
438 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_ios.a");
439 } else {
440 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.profile_osx.a");
441 }
442 }
443
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000444 // Add ASAN runtime library, if required. Dynamic libraries and bundles
445 // should not be linked with the runtime library.
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000446 if (Args.hasFlag(options::OPT_faddress_sanitizer,
447 options::OPT_fno_address_sanitizer, false)) {
Kostya Serebryany7b5f1012011-12-06 19:18:44 +0000448 if (Args.hasArg(options::OPT_dynamiclib) ||
449 Args.hasArg(options::OPT_bundle)) return;
Daniel Dunbar94b54ea2011-12-01 23:40:18 +0000450 if (isTargetIPhoneOS()) {
451 getDriver().Diag(diag::err_drv_clang_unsupported_per_platform)
452 << "-faddress-sanitizer";
453 } else {
454 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.asan_osx.a");
455
456 // The ASAN runtime library requires C++ and CoreFoundation.
457 AddCXXStdlibLibArgs(Args, CmdArgs);
458 CmdArgs.push_back("-framework");
459 CmdArgs.push_back("CoreFoundation");
460 }
461 }
462
Daniel Dunbareec99102010-01-22 03:38:14 +0000463 // Otherwise link libSystem, then the dynamic runtime library, and finally any
464 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000465 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000466
467 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000468 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000469 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
470 // it never went into the SDK.
Bob Wilson163b1512011-10-07 17:54:41 +0000471 // Linking against libgcc_s.1 isn't needed for iOS 5.0+
472 if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator())
473 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000474
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000475 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000476 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000477 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000478 // The dynamic runtime library was merged with libSystem for 10.6 and
479 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000480 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000481 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000482 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000483 CmdArgs.push_back("-lgcc_s.10.5");
484
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000485 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000486 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000487 // omitted from 10.4.dylib.
488 //
489 // Unfortunately, that turned out to not be true, because Darwin system
490 // headers can still use eprintf on i386, and it is not exported from
491 // libSystem. Therefore, we still must provide a runtime library just for
492 // the tiny tiny handful of projects that *might* use that symbol.
493 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000494 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000495 } else {
496 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000497 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
498 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000499 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000500 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000501}
502
Argyrios Kyrtzidisdceb11f2011-10-18 00:22:49 +0000503static inline StringRef SimulatorVersionDefineName() {
504 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
505}
506
507/// \brief Parse the simulator version define:
508/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
509// and return the grouped values as integers, e.g:
510// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
511// will return Major=4, Minor=2, Micro=1.
512static bool GetVersionFromSimulatorDefine(StringRef define,
513 unsigned &Major, unsigned &Minor,
514 unsigned &Micro) {
515 assert(define.startswith(SimulatorVersionDefineName()));
516 StringRef name, version;
517 llvm::tie(name, version) = define.split('=');
518 if (version.empty())
519 return false;
520 std::string verstr = version.str();
521 char *end;
522 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
523 if (*end != '\0')
524 return false;
525 Major = num / 10000;
526 num = num % 10000;
527 Minor = num / 100;
528 Micro = num % 100;
529 return true;
530}
531
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000532void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000533 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000534
Daniel Dunbar26031372010-01-27 00:56:25 +0000535 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000536 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
537 Arg *iOSSimVersion = Args.getLastArg(
538 options::OPT_mios_simulator_version_min_EQ);
Eli Friedman983d8352012-01-11 02:41:15 +0000539
Bob Wilsonc01dfc12012-01-26 03:37:03 +0000540 // FIXME: HACK! When compiling for the simulator we don't get a
541 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
542 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
543 // define passed in command-line.
544 if (!iOSVersion && !iOSSimVersion) {
545 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
546 ie = Args.filtered_end(); it != ie; ++it) {
547 StringRef define = (*it)->getValue(Args);
548 if (define.startswith(SimulatorVersionDefineName())) {
549 unsigned Major = 0, Minor = 0, Micro = 0;
550 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
551 Major < 10 && Minor < 100 && Micro < 100) {
552 ARCRuntimeForSimulator = Major < 5 ? ARCSimulator_NoARCRuntime
553 : ARCSimulator_HasARCRuntime;
554 LibCXXForSimulator = Major < 5 ? LibCXXSimulator_NotAvailable
555 : LibCXXSimulator_Available;
556 }
557 break;
558 }
559 }
560 }
561
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000562 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000563 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000564 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000565 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
566 iOSVersion = iOSSimVersion = 0;
567 } else if (iOSVersion && iOSSimVersion) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000568 getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000569 << iOSVersion->getAsString(Args)
570 << iOSSimVersion->getAsString(Args);
571 iOSSimVersion = 0;
572 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
Chad Rosiera4884972011-08-31 20:56:25 +0000573 // If no deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000574 // environment defines.
Chad Rosiera4884972011-08-31 20:56:25 +0000575 StringRef OSXTarget;
576 StringRef iOSTarget;
577 StringRef iOSSimTarget;
578 if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
579 OSXTarget = env;
580 if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
581 iOSTarget = env;
582 if (char *env = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"))
583 iOSSimTarget = env;
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000584
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000585 // If no '-miphoneos-version-min' specified on the command line and
Chad Rosiera4884972011-08-31 20:56:25 +0000586 // IPHONEOS_DEPLOYMENT_TARGET is not defined, see if we can set the default
587 // based on isysroot.
588 if (iOSTarget.empty()) {
589 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
590 StringRef first, second;
591 StringRef isysroot = A->getValue(Args);
592 llvm::tie(first, second) = isysroot.split(StringRef("SDKs/iPhoneOS"));
593 if (second != "")
594 iOSTarget = second.substr(0,3);
595 }
596 }
Daniel Dunbar816bc312010-01-26 01:45:19 +0000597
Chad Rosier4f8de272011-09-28 00:46:32 +0000598 // If no OSX or iOS target has been specified and we're compiling for armv7,
599 // go ahead as assume we're targeting iOS.
600 if (OSXTarget.empty() && iOSTarget.empty())
601 if (getDarwinArchName(Args) == "armv7")
602 iOSTarget = "0.0";
603
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000604 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000605 //
606 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000607
608 // Do not allow conflicts with the iOS simulator target.
Chad Rosiera4884972011-08-31 20:56:25 +0000609 if (!iOSSimTarget.empty() && (!OSXTarget.empty() || !iOSTarget.empty())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000610 getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000611 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
Chad Rosiera4884972011-08-31 20:56:25 +0000612 << (!OSXTarget.empty() ? "MACOSX_DEPLOYMENT_TARGET" :
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000613 "IPHONEOS_DEPLOYMENT_TARGET");
614 }
615
616 // Allow conflicts among OSX and iOS for historical reasons, but choose the
617 // default platform.
Chad Rosiera4884972011-08-31 20:56:25 +0000618 if (!OSXTarget.empty() && !iOSTarget.empty()) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000619 if (getTriple().getArch() == llvm::Triple::arm ||
620 getTriple().getArch() == llvm::Triple::thumb)
Chad Rosiera4884972011-08-31 20:56:25 +0000621 OSXTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000622 else
Chad Rosiera4884972011-08-31 20:56:25 +0000623 iOSTarget = "";
Daniel Dunbar39053672010-02-02 17:31:12 +0000624 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000625
Chad Rosiera4884972011-08-31 20:56:25 +0000626 if (!OSXTarget.empty()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000627 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000628 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
629 Args.append(OSXVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000630 } else if (!iOSTarget.empty()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000631 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000632 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
633 Args.append(iOSVersion);
Chad Rosiera4884972011-08-31 20:56:25 +0000634 } else if (!iOSSimTarget.empty()) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000635 const Option *O = Opts.getOption(
636 options::OPT_mios_simulator_version_min_EQ);
637 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
638 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000639 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000640 // Otherwise, assume we are targeting OS X.
641 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000642 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
643 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000644 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000645 }
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000647 // Reject invalid architecture combinations.
648 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
649 getTriple().getArch() != llvm::Triple::x86_64)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000650 getDriver().Diag(diag::err_drv_invalid_arch_for_deployment_target)
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000651 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
652 }
653
Daniel Dunbar26031372010-01-27 00:56:25 +0000654 // Set the tool chain target information.
655 unsigned Major, Minor, Micro;
656 bool HadExtra;
657 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000658 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar26031372010-01-27 00:56:25 +0000659 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
660 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000661 Major != 10 || Minor >= 100 || Micro >= 100)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000662 getDriver().Diag(diag::err_drv_invalid_version_number)
Daniel Dunbar26031372010-01-27 00:56:25 +0000663 << OSXVersion->getAsString(Args);
664 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000665 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
666 assert(Version && "Unknown target platform!");
Eli Friedman983d8352012-01-11 02:41:15 +0000667 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
668 Micro, HadExtra) || HadExtra ||
669 Major >= 10 || Minor >= 100 || Micro >= 100)
670 getDriver().Diag(diag::err_drv_invalid_version_number)
671 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000672 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000673
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000674 bool IsIOSSim = bool(iOSSimVersion);
675
676 // In GCC, the simulator historically was treated as being OS X in some
677 // contexts, like determining the link logic, despite generally being called
678 // with an iOS deployment target. For compatibility, we detect the
679 // simulator as iOS + x86, and treat it differently in a few contexts.
680 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
681 getTriple().getArch() == llvm::Triple::x86_64))
682 IsIOSSim = true;
683
684 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000685}
686
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000687void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000688 ArgStringList &CmdArgs) const {
689 CXXStdlibType Type = GetCXXStdlibType(Args);
690
691 switch (Type) {
692 case ToolChain::CST_Libcxx:
693 CmdArgs.push_back("-lc++");
694 break;
695
696 case ToolChain::CST_Libstdcxx: {
697 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
698 // it was previously found in the gcc lib dir. However, for all the Darwin
699 // platforms we care about it was -lstdc++.6, so we search for that
700 // explicitly if we can't see an obvious -lstdc++ candidate.
701
702 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000703 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000704 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
705 llvm::sys::Path P(A->getValue(Args));
706 P.appendComponent("usr");
707 P.appendComponent("lib");
708 P.appendComponent("libstdc++.dylib");
709
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000710 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000711 P.eraseComponent();
712 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000713 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000714 CmdArgs.push_back(Args.MakeArgString(P.str()));
715 return;
716 }
717 }
718 }
719
720 // Otherwise, look in the root.
Bob Wilson5a5dcdc2011-11-11 07:47:04 +0000721 // FIXME: This should be removed someday when we don't have to care about
722 // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000723 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
724 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000725 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
726 return;
727 }
728
729 // Otherwise, let the linker search.
730 CmdArgs.push_back("-lstdc++");
731 break;
732 }
733 }
734}
735
Shantonu Sen7433fed2010-09-17 18:39:08 +0000736void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
737 ArgStringList &CmdArgs) const {
738
739 // For Darwin platforms, use the compiler-rt-based support library
740 // instead of the gcc-provided one (which is also incidentally
741 // only present in the gcc lib dir, which makes it hard to find).
742
743 llvm::sys::Path P(getDriver().ResourceDir);
744 P.appendComponent("lib");
745 P.appendComponent("darwin");
746 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000747
Shantonu Sen7433fed2010-09-17 18:39:08 +0000748 // For now, allow missing resource libraries to support developers who may
749 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000750 bool Exists;
751 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000752 CmdArgs.push_back(Args.MakeArgString(P.str()));
753}
754
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000755DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
756 const char *BoundArch) const {
757 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
758 const OptTable &Opts = getDriver().getOpts();
759
760 // FIXME: We really want to get out of the tool chain level argument
761 // translation business, as it makes the driver functionality much
762 // more opaque. For now, we follow gcc closely solely for the
763 // purpose of easily achieving feature parity & testability. Once we
764 // have something that works, we should reevaluate each translation
765 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000766
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000767 for (ArgList::const_iterator it = Args.begin(),
768 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000769 Arg *A = *it;
770
771 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000772 // Skip this argument unless the architecture matches either the toolchain
773 // triple arch, or the arch being bound.
774 //
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000775 // FIXME: Canonicalize name.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000776 StringRef XarchArch = A->getValue(Args, 0);
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000777 if (!(XarchArch == getArchName() ||
778 (BoundArch && XarchArch == BoundArch)))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000779 continue;
780
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000781 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000782 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
783 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000784 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000786 // If the argument parsing failed or more than one argument was
787 // consumed, the -Xarch_ argument's parameter tried to consume
788 // extra arguments. Emit an error and ignore.
789 //
790 // We also want to disallow any options which would alter the
791 // driver behavior; that isn't going to work in our model. We
792 // use isDriverOption() as an approximation, although things
793 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000794 if (!XarchArg || Index > Prev + 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000795 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000796 << A->getAsString(Args);
797 continue;
798 } else if (XarchArg->getOption().isDriverOption()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000799 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000800 << A->getAsString(Args);
801 continue;
802 }
803
Daniel Dunbar478edc22009-03-29 22:29:05 +0000804 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000805 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000806
807 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000808
809 // Linker input arguments require custom handling. The problem is that we
810 // have already constructed the phase actions, so we can not treat them as
811 // "input arguments".
812 if (A->getOption().isLinkerInput()) {
813 // Convert the argument into individual Zlinker_input_args.
814 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
815 DAL->AddSeparateArg(OriginalArg,
816 Opts.getOption(options::OPT_Zlinker_input),
817 A->getValue(Args, i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000818
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000819 }
820 continue;
821 }
Mike Stump1eb44332009-09-09 15:08:12 +0000822 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000823
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000824 // Sob. These is strictly gcc compatible for the time being. Apple
825 // gcc translates options twice, which means that self-expanding
826 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000827 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000828 default:
829 DAL->append(A);
830 break;
831
832 case options::OPT_mkernel:
833 case options::OPT_fapple_kext:
834 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000835 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000836 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000838 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000839 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
840 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000841 break;
842
843 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000844 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
845 DAL->AddFlagArg(A,
846 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000847 break;
848
849 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000850 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
851 DAL->AddFlagArg(A,
852 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000853 break;
854
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000855 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000856 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000857 break;
858
859 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000860 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000861 break;
862
863 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000864 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000865 break;
866
867 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000868 DAL->AddFlagArg(A,
869 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000870 break;
871
872 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000873 DAL->AddFlagArg(A,
874 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000875 break;
876
877 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000878 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000879 break;
880
881 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000882 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000883 break;
884 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000885 }
886
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000887 if (getTriple().getArch() == llvm::Triple::x86 ||
888 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000889 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000890 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000891
892 // Add the arch options based on the particular spelling of -arch, to match
893 // how the driver driver works.
894 if (BoundArch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000895 StringRef Name = BoundArch;
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000896 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
897 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
898
899 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
900 // which defines the list of which architectures we accept.
901 if (Name == "ppc")
902 ;
903 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000904 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000905 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000906 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000907 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000908 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000909 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000910 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000911 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000912 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000913 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000914 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000915 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000916 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000917 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000918 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000919
920 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000921 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000922
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000923 else if (Name == "i386")
924 ;
925 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000926 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000927 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000928 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000929 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000930 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000931 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000932 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000933 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000934 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000935 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000936 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000937 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000938 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000939
940 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000941 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000942
943 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000944 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000945 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000946 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000947 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000948 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000949 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000950 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000951 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000952 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000953 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000954 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000955
956 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000957 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000958 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000959
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000960 // Add an explicit version min argument for the deployment target. We do this
961 // after argument translation because -Xarch_ arguments may add a version min
962 // argument.
963 AddDeploymentTarget(*DAL);
964
Bob Wilson163b1512011-10-07 17:54:41 +0000965 // Validate the C++ standard library choice.
966 CXXStdlibType Type = GetCXXStdlibType(*DAL);
967 if (Type == ToolChain::CST_Libcxx) {
968 switch (LibCXXForSimulator) {
969 case LibCXXSimulator_None:
970 // Handle non-simulator cases.
971 if (isTargetIPhoneOS()) {
972 if (isIPhoneOSVersionLT(5, 0)) {
973 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
974 << "iOS 5.0";
975 }
Bob Wilson163b1512011-10-07 17:54:41 +0000976 }
977 break;
978 case LibCXXSimulator_NotAvailable:
979 getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment)
980 << "iOS 5.0";
981 break;
982 case LibCXXSimulator_Available:
983 break;
984 }
985 }
986
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000987 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000988}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000989
Daniel Dunbarf3955282009-09-04 18:34:51 +0000990bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000991 // FIXME: Gross; we should probably have some separate target
992 // definition, possibly even reusing the one in clang.
993 return getArchName() == "x86_64";
994}
995
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000996bool Darwin::UseDwarfDebugFlags() const {
997 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
998 return S[0] != '\0';
999 return false;
1000}
1001
Daniel Dunbarb2987d12010-02-10 18:49:11 +00001002bool Darwin::UseSjLjExceptions() const {
1003 // Darwin uses SjLj exceptions on ARM.
1004 return (getTriple().getArch() == llvm::Triple::arm ||
1005 getTriple().getArch() == llvm::Triple::thumb);
1006}
1007
Daniel Dunbarf3955282009-09-04 18:34:51 +00001008const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001009 return "pic";
1010}
1011
Daniel Dunbarf3955282009-09-04 18:34:51 +00001012const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001013 if (getArchName() == "x86_64")
1014 return "pic";
1015 return 0;
1016}
1017
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +00001018bool Darwin::SupportsProfiling() const {
1019 // Profiling instrumentation is only supported on x86.
1020 return getArchName() == "i386" || getArchName() == "x86_64";
1021}
1022
Daniel Dunbar43a9b322010-04-10 16:20:23 +00001023bool Darwin::SupportsObjCGC() const {
1024 // Garbage collection is supported everywhere except on iPhone OS.
1025 return !isTargetIPhoneOS();
1026}
1027
Argyrios Kyrtzidis5840dd92012-02-29 03:43:52 +00001028bool Darwin::SupportsObjCARC() const {
1029 return isTargetIPhoneOS() || !isMacosxVersionLT(10, 6);
1030}
1031
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001032std::string
Chad Rosier61ab80a2011-09-20 20:44:06 +00001033Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args,
1034 types::ID InputType) const {
1035 return ComputeLLVMTriple(Args, InputType);
Daniel Dunbar00577ad2010-08-23 22:35:37 +00001036}
1037
Daniel Dunbar39176082009-03-20 00:20:03 +00001038/// Generic_GCC - A tool chain using the 'gcc' command to perform
1039/// all subcommands; this relies on gcc translating the majority of
1040/// command line options.
1041
Chandler Carruth19347ed2011-11-06 23:39:34 +00001042/// \brief Parse a GCCVersion object out of a string of text.
1043///
1044/// This is the primary means of forming GCCVersion objects.
1045/*static*/
1046Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) {
1047 const GCCVersion BadVersion = { VersionText.str(), -1, -1, -1, "" };
1048 std::pair<StringRef, StringRef> First = VersionText.split('.');
1049 std::pair<StringRef, StringRef> Second = First.second.split('.');
1050
1051 GCCVersion GoodVersion = { VersionText.str(), -1, -1, -1, "" };
1052 if (First.first.getAsInteger(10, GoodVersion.Major) ||
1053 GoodVersion.Major < 0)
1054 return BadVersion;
1055 if (Second.first.getAsInteger(10, GoodVersion.Minor) ||
1056 GoodVersion.Minor < 0)
1057 return BadVersion;
1058
1059 // First look for a number prefix and parse that if present. Otherwise just
1060 // stash the entire patch string in the suffix, and leave the number
1061 // unspecified. This covers versions strings such as:
1062 // 4.4
1063 // 4.4.0
1064 // 4.4.x
1065 // 4.4.2-rc4
1066 // 4.4.x-patched
1067 // And retains any patch number it finds.
1068 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str();
1069 if (!PatchText.empty()) {
1070 if (unsigned EndNumber = PatchText.find_first_not_of("0123456789")) {
1071 // Try to parse the number and any suffix.
1072 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
1073 GoodVersion.Patch < 0)
1074 return BadVersion;
1075 GoodVersion.PatchSuffix = PatchText.substr(EndNumber).str();
1076 }
1077 }
1078
1079 return GoodVersion;
1080}
1081
1082/// \brief Less-than for GCCVersion, implementing a Strict Weak Ordering.
1083bool Generic_GCC::GCCVersion::operator<(const GCCVersion &RHS) const {
1084 if (Major < RHS.Major) return true; if (Major > RHS.Major) return false;
1085 if (Minor < RHS.Minor) return true; if (Minor > RHS.Minor) return false;
1086
1087 // Note that we rank versions with *no* patch specified is better than ones
1088 // hard-coding a patch version. Thus if the RHS has no patch, it always
1089 // wins, and the LHS only wins if it has no patch and the RHS does have
1090 // a patch.
1091 if (RHS.Patch == -1) return true; if (Patch == -1) return false;
1092 if (Patch < RHS.Patch) return true; if (Patch > RHS.Patch) return false;
1093
1094 // Finally, between completely tied version numbers, the version with the
1095 // suffix loses as we prefer full releases.
1096 if (RHS.PatchSuffix.empty()) return true;
1097 return false;
1098}
1099
Rafael Espindola0e659592012-02-19 01:38:32 +00001100static StringRef getGCCToolchainDir(const ArgList &Args) {
1101 const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain);
1102 if (A)
1103 return A->getValue(Args);
1104 return GCC_INSTALL_PREFIX;
1105}
1106
Chandler Carruth19347ed2011-11-06 23:39:34 +00001107/// \brief Construct a GCCInstallationDetector from the driver.
1108///
1109/// This performs all of the autodetection and sets up the various paths.
1110/// Once constructed, a GCCInstallation is esentially immutable.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001111///
1112/// FIXME: We shouldn't need an explicit TargetTriple parameter here, and
1113/// should instead pull the target out of the driver. This is currently
1114/// necessary because the driver doesn't store the final version of the target
1115/// triple.
1116Generic_GCC::GCCInstallationDetector::GCCInstallationDetector(
1117 const Driver &D,
Rafael Espindola0e659592012-02-19 01:38:32 +00001118 const llvm::Triple &TargetTriple,
1119 const ArgList &Args)
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001120 : IsValid(false) {
Chandler Carruth9b338a72012-02-13 02:02:09 +00001121 llvm::Triple MultiarchTriple
1122 = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant()
1123 : TargetTriple.get32BitArchVariant();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001124 llvm::Triple::ArchType TargetArch = TargetTriple.getArch();
Chandler Carruth19347ed2011-11-06 23:39:34 +00001125 // The library directories which may contain GCC installations.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001126 SmallVector<StringRef, 4> CandidateLibDirs, CandidateMultiarchLibDirs;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001127 // The compatible GCC triples for this particular architecture.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001128 SmallVector<StringRef, 10> CandidateTripleAliases;
1129 SmallVector<StringRef, 10> CandidateMultiarchTripleAliases;
1130 CollectLibDirsAndTriples(TargetTriple, MultiarchTriple, CandidateLibDirs,
1131 CandidateTripleAliases,
1132 CandidateMultiarchLibDirs,
1133 CandidateMultiarchTripleAliases);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001134
1135 // Compute the set of prefixes for our search.
1136 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(),
1137 D.PrefixDirs.end());
Rafael Espindola353300c2012-02-03 01:01:20 +00001138
Rafael Espindola0e659592012-02-19 01:38:32 +00001139 StringRef GCCToolchainDir = getGCCToolchainDir(Args);
1140 if (GCCToolchainDir != "") {
1141 if (GCCToolchainDir.back() == '/')
1142 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the /
Rafael Espindola353300c2012-02-03 01:01:20 +00001143
Rafael Espindola0e659592012-02-19 01:38:32 +00001144 Prefixes.push_back(GCCToolchainDir);
Rafael Espindola353300c2012-02-03 01:01:20 +00001145 } else {
1146 Prefixes.push_back(D.SysRoot);
1147 Prefixes.push_back(D.SysRoot + "/usr");
1148 Prefixes.push_back(D.InstalledDir + "/..");
1149 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001150
1151 // Loop over the various components which exist and select the best GCC
1152 // installation available. GCC installs are ranked by version number.
1153 Version = GCCVersion::Parse("0.0.0");
1154 for (unsigned i = 0, ie = Prefixes.size(); i < ie; ++i) {
1155 if (!llvm::sys::fs::exists(Prefixes[i]))
1156 continue;
1157 for (unsigned j = 0, je = CandidateLibDirs.size(); j < je; ++j) {
1158 const std::string LibDir = Prefixes[i] + CandidateLibDirs[j].str();
1159 if (!llvm::sys::fs::exists(LibDir))
1160 continue;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001161 for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k)
1162 ScanLibDirForGCCTriple(TargetArch, LibDir, CandidateTripleAliases[k]);
1163 }
1164 for (unsigned j = 0, je = CandidateMultiarchLibDirs.size(); j < je; ++j) {
1165 const std::string LibDir
1166 = Prefixes[i] + CandidateMultiarchLibDirs[j].str();
1167 if (!llvm::sys::fs::exists(LibDir))
1168 continue;
1169 for (unsigned k = 0, ke = CandidateMultiarchTripleAliases.size(); k < ke;
1170 ++k)
1171 ScanLibDirForGCCTriple(TargetArch, LibDir,
1172 CandidateMultiarchTripleAliases[k],
1173 /*NeedsMultiarchSuffix=*/true);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001174 }
1175 }
1176}
1177
1178/*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001179 const llvm::Triple &TargetTriple,
1180 const llvm::Triple &MultiarchTriple,
1181 SmallVectorImpl<StringRef> &LibDirs,
1182 SmallVectorImpl<StringRef> &TripleAliases,
1183 SmallVectorImpl<StringRef> &MultiarchLibDirs,
1184 SmallVectorImpl<StringRef> &MultiarchTripleAliases) {
1185 // Declare a bunch of static data sets that we'll select between below. These
1186 // are specifically designed to always refer to string literals to avoid any
1187 // lifetime or initialization issues.
1188 static const char *const ARMLibDirs[] = { "/lib" };
1189 static const char *const ARMTriples[] = {
1190 "arm-linux-gnueabi",
1191 "arm-linux-androideabi"
1192 };
1193
1194 static const char *const X86_64LibDirs[] = { "/lib64", "/lib" };
1195 static const char *const X86_64Triples[] = {
1196 "x86_64-linux-gnu",
1197 "x86_64-unknown-linux-gnu",
1198 "x86_64-pc-linux-gnu",
1199 "x86_64-redhat-linux6E",
1200 "x86_64-redhat-linux",
1201 "x86_64-suse-linux",
1202 "x86_64-manbo-linux-gnu",
1203 "x86_64-linux-gnu",
1204 "x86_64-slackware-linux"
1205 };
1206 static const char *const X86LibDirs[] = { "/lib32", "/lib" };
1207 static const char *const X86Triples[] = {
1208 "i686-linux-gnu",
1209 "i686-pc-linux-gnu",
1210 "i486-linux-gnu",
1211 "i386-linux-gnu",
1212 "i686-redhat-linux",
1213 "i586-redhat-linux",
1214 "i386-redhat-linux",
1215 "i586-suse-linux",
1216 "i486-slackware-linux"
1217 };
1218
1219 static const char *const MIPSLibDirs[] = { "/lib" };
1220 static const char *const MIPSTriples[] = { "mips-linux-gnu" };
1221 static const char *const MIPSELLibDirs[] = { "/lib" };
1222 static const char *const MIPSELTriples[] = { "mipsel-linux-gnu" };
1223
1224 static const char *const PPCLibDirs[] = { "/lib32", "/lib" };
1225 static const char *const PPCTriples[] = {
1226 "powerpc-linux-gnu",
1227 "powerpc-unknown-linux-gnu",
1228 "powerpc-suse-linux"
1229 };
1230 static const char *const PPC64LibDirs[] = { "/lib64", "/lib" };
1231 static const char *const PPC64Triples[] = {
Chandler Carruth155c54c2012-02-26 09:03:21 +00001232 "powerpc64-linux-gnu",
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001233 "powerpc64-unknown-linux-gnu",
1234 "powerpc64-suse-linux",
1235 "ppc64-redhat-linux"
1236 };
1237
1238 switch (TargetTriple.getArch()) {
1239 case llvm::Triple::arm:
1240 case llvm::Triple::thumb:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001241 LibDirs.append(ARMLibDirs, ARMLibDirs + llvm::array_lengthof(ARMLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001242 TripleAliases.append(
1243 ARMTriples, ARMTriples + llvm::array_lengthof(ARMTriples));
1244 break;
1245 case llvm::Triple::x86_64:
1246 LibDirs.append(
1247 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1248 TripleAliases.append(
1249 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1250 MultiarchLibDirs.append(
1251 X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
1252 MultiarchTripleAliases.append(
1253 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1254 break;
1255 case llvm::Triple::x86:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001256 LibDirs.append(X86LibDirs, X86LibDirs + llvm::array_lengthof(X86LibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001257 TripleAliases.append(
1258 X86Triples, X86Triples + llvm::array_lengthof(X86Triples));
1259 MultiarchLibDirs.append(
1260 X86_64LibDirs, X86_64LibDirs + llvm::array_lengthof(X86_64LibDirs));
1261 MultiarchTripleAliases.append(
1262 X86_64Triples, X86_64Triples + llvm::array_lengthof(X86_64Triples));
1263 break;
1264 case llvm::Triple::mips:
1265 LibDirs.append(
1266 MIPSLibDirs, MIPSLibDirs + llvm::array_lengthof(MIPSLibDirs));
1267 TripleAliases.append(
1268 MIPSTriples, MIPSTriples + llvm::array_lengthof(MIPSTriples));
1269 break;
1270 case llvm::Triple::mipsel:
1271 LibDirs.append(
1272 MIPSELLibDirs, MIPSELLibDirs + llvm::array_lengthof(MIPSELLibDirs));
1273 TripleAliases.append(
1274 MIPSELTriples, MIPSELTriples + llvm::array_lengthof(MIPSELTriples));
1275 break;
1276 case llvm::Triple::ppc:
Chandler Carruth19347ed2011-11-06 23:39:34 +00001277 LibDirs.append(PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001278 TripleAliases.append(
1279 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1280 MultiarchLibDirs.append(
1281 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1282 MultiarchTripleAliases.append(
1283 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1284 break;
1285 case llvm::Triple::ppc64:
1286 LibDirs.append(
1287 PPC64LibDirs, PPC64LibDirs + llvm::array_lengthof(PPC64LibDirs));
1288 TripleAliases.append(
1289 PPC64Triples, PPC64Triples + llvm::array_lengthof(PPC64Triples));
1290 MultiarchLibDirs.append(
1291 PPCLibDirs, PPCLibDirs + llvm::array_lengthof(PPCLibDirs));
1292 MultiarchTripleAliases.append(
1293 PPCTriples, PPCTriples + llvm::array_lengthof(PPCTriples));
1294 break;
1295
1296 default:
1297 // By default, just rely on the standard lib directories and the original
1298 // triple.
1299 break;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001300 }
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001301
1302 // Always append the drivers target triple to the end, in case it doesn't
1303 // match any of our aliases.
1304 TripleAliases.push_back(TargetTriple.str());
1305
1306 // Also include the multiarch variant if it's different.
1307 if (TargetTriple.str() != MultiarchTriple.str())
1308 MultiarchTripleAliases.push_back(MultiarchTriple.str());
Chandler Carruth19347ed2011-11-06 23:39:34 +00001309}
1310
1311void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple(
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001312 llvm::Triple::ArchType TargetArch, const std::string &LibDir,
1313 StringRef CandidateTriple, bool NeedsMultiarchSuffix) {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001314 // There are various different suffixes involving the triple we
1315 // check for. We also record what is necessary to walk from each back
1316 // up to the lib directory.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001317 const std::string LibSuffixes[] = {
Chandler Carruth19347ed2011-11-06 23:39:34 +00001318 "/gcc/" + CandidateTriple.str(),
1319 "/" + CandidateTriple.str() + "/gcc/" + CandidateTriple.str(),
1320
1321 // Ubuntu has a strange mis-matched pair of triples that this happens to
1322 // match.
1323 // FIXME: It may be worthwhile to generalize this and look for a second
1324 // triple.
Chandler Carruthd936d9d2011-11-09 03:46:20 +00001325 "/i386-linux-gnu/gcc/" + CandidateTriple.str()
Chandler Carruth19347ed2011-11-06 23:39:34 +00001326 };
1327 const std::string InstallSuffixes[] = {
1328 "/../../..",
1329 "/../../../..",
1330 "/../../../.."
1331 };
1332 // Only look at the final, weird Ubuntu suffix for i386-linux-gnu.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001333 const unsigned NumLibSuffixes = (llvm::array_lengthof(LibSuffixes) -
1334 (TargetArch != llvm::Triple::x86));
1335 for (unsigned i = 0; i < NumLibSuffixes; ++i) {
1336 StringRef LibSuffix = LibSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001337 llvm::error_code EC;
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001338 for (llvm::sys::fs::directory_iterator LI(LibDir + LibSuffix, EC), LE;
Chandler Carruth19347ed2011-11-06 23:39:34 +00001339 !EC && LI != LE; LI = LI.increment(EC)) {
1340 StringRef VersionText = llvm::sys::path::filename(LI->path());
1341 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText);
1342 static const GCCVersion MinVersion = { "4.1.1", 4, 1, 1, "" };
1343 if (CandidateVersion < MinVersion)
1344 continue;
1345 if (CandidateVersion <= Version)
1346 continue;
Hal Finkel2e55df42011-12-08 05:50:03 +00001347
1348 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001349 // in what would normally be GCCInstallPath and put the 64-bit
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001350 // libs in a subdirectory named 64. The simple logic we follow is that
1351 // *if* there is a subdirectory of the right name with crtbegin.o in it,
1352 // we use that. If not, and if not a multiarch triple, we look for
1353 // crtbegin.o without the subdirectory.
1354 StringRef MultiarchSuffix
1355 = (TargetArch == llvm::Triple::x86_64 ||
1356 TargetArch == llvm::Triple::ppc64) ? "/64" : "/32";
1357 if (llvm::sys::fs::exists(LI->path() + MultiarchSuffix + "/crtbegin.o")) {
1358 GCCMultiarchSuffix = MultiarchSuffix.str();
1359 } else {
1360 if (NeedsMultiarchSuffix ||
1361 !llvm::sys::fs::exists(LI->path() + "/crtbegin.o"))
1362 continue;
1363 GCCMultiarchSuffix.clear();
1364 }
Chandler Carruth19347ed2011-11-06 23:39:34 +00001365
1366 Version = CandidateVersion;
Chandler Carruthfa5be912012-01-24 19:28:29 +00001367 GCCTriple.setTriple(CandidateTriple);
Chandler Carruth19347ed2011-11-06 23:39:34 +00001368 // FIXME: We hack together the directory name here instead of
1369 // using LI to ensure stable path separators across Windows and
1370 // Linux.
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00001371 GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str();
Chandler Carruth5d84bb42012-01-24 19:21:42 +00001372 GCCParentLibPath = GCCInstallPath + InstallSuffixes[i];
Chandler Carruth19347ed2011-11-06 23:39:34 +00001373 IsValid = true;
1374 }
1375 }
1376}
1377
Rafael Espindola0e659592012-02-19 01:38:32 +00001378Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple& Triple,
1379 const ArgList &Args)
1380 : ToolChain(D, Triple), GCCInstallation(getDriver(), Triple, Args) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001381 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001382 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001383 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001384}
1385
Daniel Dunbar39176082009-03-20 00:20:03 +00001386Generic_GCC::~Generic_GCC() {
1387 // Free tool implementations.
1388 for (llvm::DenseMap<unsigned, Tool*>::iterator
1389 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1390 delete it->second;
1391}
1392
Mike Stump1eb44332009-09-09 15:08:12 +00001393Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001394 const JobAction &JA,
1395 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +00001396 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001397 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +00001398 Key = Action::AnalyzeJobClass;
1399 else
1400 Key = JA.getKind();
1401
1402 Tool *&T = Tools[Key];
1403 if (!T) {
1404 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001405 case Action::InputClass:
1406 case Action::BindArchClass:
David Blaikieb219cfc2011-09-23 05:06:16 +00001407 llvm_unreachable("Invalid tool kind.");
Daniel Dunbar39176082009-03-20 00:20:03 +00001408 case Action::PreprocessJobClass:
1409 T = new tools::gcc::Preprocess(*this); break;
1410 case Action::PrecompileJobClass:
1411 T = new tools::gcc::Precompile(*this); break;
1412 case Action::AnalyzeJobClass:
Ted Kremenek30660a82012-03-06 20:06:33 +00001413 case Action::MigrateJobClass:
Daniel Dunbar39176082009-03-20 00:20:03 +00001414 T = new tools::Clang(*this); break;
1415 case Action::CompileJobClass:
1416 T = new tools::gcc::Compile(*this); break;
1417 case Action::AssembleJobClass:
1418 T = new tools::gcc::Assemble(*this); break;
1419 case Action::LinkJobClass:
1420 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001422 // This is a bit ungeneric, but the only platform using a driver
1423 // driver is Darwin.
1424 case Action::LipoJobClass:
1425 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00001426 case Action::DsymutilJobClass:
1427 T = new tools::darwin::Dsymutil(*this); break;
Eric Christopherf8571862011-08-23 17:56:55 +00001428 case Action::VerifyJobClass:
1429 T = new tools::darwin::VerifyDebug(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +00001430 }
1431 }
1432
1433 return *T;
1434}
1435
Daniel Dunbar39176082009-03-20 00:20:03 +00001436bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +00001437 // FIXME: Gross; we should probably have some separate target
1438 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +00001439 return getArchName() == "x86_64";
1440}
1441
1442const char *Generic_GCC::GetDefaultRelocationModel() const {
1443 return "static";
1444}
1445
1446const char *Generic_GCC::GetForcedPicModel() const {
1447 return 0;
1448}
Tony Linthicum96319392011-12-12 21:14:55 +00001449/// Hexagon Toolchain
1450
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001451Hexagon_TC::Hexagon_TC(const Driver &D, const llvm::Triple& Triple)
1452 : ToolChain(D, Triple) {
Tony Linthicum96319392011-12-12 21:14:55 +00001453 getProgramPaths().push_back(getDriver().getInstalledDir());
1454 if (getDriver().getInstalledDir() != getDriver().Dir.c_str())
1455 getProgramPaths().push_back(getDriver().Dir);
1456}
1457
1458Hexagon_TC::~Hexagon_TC() {
1459 // Free tool implementations.
1460 for (llvm::DenseMap<unsigned, Tool*>::iterator
1461 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1462 delete it->second;
1463}
1464
1465Tool &Hexagon_TC::SelectTool(const Compilation &C,
1466 const JobAction &JA,
1467 const ActionList &Inputs) const {
1468 Action::ActionClass Key;
1469 // if (JA.getKind () == Action::CompileJobClass)
1470 // Key = JA.getKind ();
1471 // else
1472
1473 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1474 Key = Action::AnalyzeJobClass;
1475 else
1476 Key = JA.getKind();
1477 // if ((JA.getKind () == Action::CompileJobClass)
1478 // && (JA.getType () != types::TY_LTO_BC)) {
1479 // Key = JA.getKind ();
1480 // }
1481
1482 Tool *&T = Tools[Key];
1483 if (!T) {
1484 switch (Key) {
1485 case Action::InputClass:
1486 case Action::BindArchClass:
1487 assert(0 && "Invalid tool kind.");
1488 case Action::AnalyzeJobClass:
1489 T = new tools::Clang(*this); break;
1490 case Action::AssembleJobClass:
1491 T = new tools::hexagon::Assemble(*this); break;
1492 case Action::LinkJobClass:
1493 T = new tools::hexagon::Link(*this); break;
1494 default:
1495 assert(false && "Unsupported action for Hexagon target.");
1496 }
1497 }
1498
1499 return *T;
1500}
1501
1502bool Hexagon_TC::IsUnwindTablesDefault() const {
1503 // FIXME: Gross; we should probably have some separate target
1504 // definition, possibly even reusing the one in clang.
1505 return getArchName() == "x86_64";
1506}
1507
1508const char *Hexagon_TC::GetDefaultRelocationModel() const {
1509 return "static";
1510}
1511
1512const char *Hexagon_TC::GetForcedPicModel() const {
1513 return 0;
1514} // End Hexagon
1515
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001516
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001517/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1518/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1519/// Currently does not support anything else but compilation.
1520
Chandler Carruth1d16f0f2012-01-31 02:21:20 +00001521TCEToolChain::TCEToolChain(const Driver &D, const llvm::Triple& Triple)
1522 : ToolChain(D, Triple) {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001523 // Path mangling to find libexec
1524 std::string Path(getDriver().Dir);
1525
1526 Path += "/../libexec";
1527 getProgramPaths().push_back(Path);
1528}
1529
1530TCEToolChain::~TCEToolChain() {
1531 for (llvm::DenseMap<unsigned, Tool*>::iterator
1532 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1533 delete it->second;
1534}
1535
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001536bool TCEToolChain::IsMathErrnoDefault() const {
1537 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001538}
1539
1540bool TCEToolChain::IsUnwindTablesDefault() const {
1541 return false;
1542}
1543
1544const char *TCEToolChain::GetDefaultRelocationModel() const {
1545 return "static";
1546}
1547
1548const char *TCEToolChain::GetForcedPicModel() const {
1549 return 0;
1550}
1551
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001552Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001553 const JobAction &JA,
1554 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001555 Action::ActionClass Key;
1556 Key = Action::AnalyzeJobClass;
1557
1558 Tool *&T = Tools[Key];
1559 if (!T) {
1560 switch (Key) {
1561 case Action::PreprocessJobClass:
1562 T = new tools::gcc::Preprocess(*this); break;
1563 case Action::AnalyzeJobClass:
1564 T = new tools::Clang(*this); break;
1565 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001566 llvm_unreachable("Unsupported action for TCE target.");
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001567 }
1568 }
1569 return *T;
1570}
1571
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001572/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1573
Rafael Espindola0e659592012-02-19 01:38:32 +00001574OpenBSD::OpenBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1575 : Generic_ELF(D, Triple, Args) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001576 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001577 getFilePaths().push_back("/usr/lib");
1578}
1579
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001580Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1581 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001582 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001583 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001584 Key = Action::AnalyzeJobClass;
1585 else
1586 Key = JA.getKind();
1587
Rafael Espindoladda5b922010-11-07 23:13:01 +00001588 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1589 options::OPT_no_integrated_as,
1590 IsIntegratedAssemblerDefault());
1591
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001592 Tool *&T = Tools[Key];
1593 if (!T) {
1594 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001595 case Action::AssembleJobClass: {
1596 if (UseIntegratedAs)
1597 T = new tools::ClangAs(*this);
1598 else
1599 T = new tools::openbsd::Assemble(*this);
1600 break;
1601 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001602 case Action::LinkJobClass:
1603 T = new tools::openbsd::Link(*this); break;
1604 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001605 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001606 }
1607 }
1608
1609 return *T;
1610}
1611
Daniel Dunbar75358d22009-03-30 21:06:03 +00001612/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1613
Rafael Espindola0e659592012-02-19 01:38:32 +00001614FreeBSD::FreeBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1615 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001616
Chandler Carruth24248e32012-01-26 01:35:15 +00001617 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
1618 // back to '/usr/lib' if it doesn't exist.
Chandler Carruth00646ba2012-01-25 11:24:24 +00001619 if ((Triple.getArch() == llvm::Triple::x86 ||
1620 Triple.getArch() == llvm::Triple::ppc) &&
Chandler Carruth24248e32012-01-26 01:35:15 +00001621 llvm::sys::fs::exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
Chandler Carruth00646ba2012-01-25 11:24:24 +00001622 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
1623 else
1624 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
Daniel Dunbar75358d22009-03-30 21:06:03 +00001625}
1626
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001627Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1628 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001629 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001630 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001631 Key = Action::AnalyzeJobClass;
1632 else
1633 Key = JA.getKind();
1634
Roman Divacky67dece72010-11-08 17:46:39 +00001635 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1636 options::OPT_no_integrated_as,
1637 IsIntegratedAssemblerDefault());
1638
Daniel Dunbar75358d22009-03-30 21:06:03 +00001639 Tool *&T = Tools[Key];
1640 if (!T) {
1641 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001642 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001643 if (UseIntegratedAs)
1644 T = new tools::ClangAs(*this);
1645 else
1646 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001647 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001648 case Action::LinkJobClass:
1649 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001650 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001651 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001652 }
1653 }
1654
1655 return *T;
1656}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001657
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001658/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1659
Rafael Espindola0e659592012-02-19 01:38:32 +00001660NetBSD::NetBSD(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1661 : Generic_ELF(D, Triple, Args) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001662
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001663 if (getDriver().UseStdLib) {
Chandler Carruth32f88be2012-01-25 11:18:20 +00001664 // When targeting a 32-bit platform, try the special directory used on
1665 // 64-bit hosts, and only fall back to the main library directory if that
1666 // doesn't work.
1667 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
1668 // what all logic is needed to emulate the '=' prefix here.
Joerg Sonnenberger66de97f2012-01-26 21:58:37 +00001669 if (Triple.getArch() == llvm::Triple::x86)
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001670 getFilePaths().push_back("=/usr/lib/i386");
Chandler Carruth32f88be2012-01-25 11:18:20 +00001671
1672 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001673 }
1674}
1675
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001676Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1677 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001678 Action::ActionClass Key;
1679 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1680 Key = Action::AnalyzeJobClass;
1681 else
1682 Key = JA.getKind();
1683
1684 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1685 options::OPT_no_integrated_as,
1686 IsIntegratedAssemblerDefault());
1687
1688 Tool *&T = Tools[Key];
1689 if (!T) {
1690 switch (Key) {
1691 case Action::AssembleJobClass:
1692 if (UseIntegratedAs)
1693 T = new tools::ClangAs(*this);
1694 else
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001695 T = new tools::netbsd::Assemble(*this);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001696 break;
1697 case Action::LinkJobClass:
Joerg Sonnenberger1bd91372012-01-26 22:27:52 +00001698 T = new tools::netbsd::Link(*this);
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001699 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001700 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001701 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001702 }
1703 }
1704
1705 return *T;
1706}
1707
Chris Lattner38e317d2010-07-07 16:01:42 +00001708/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1709
Rafael Espindola0e659592012-02-19 01:38:32 +00001710Minix::Minix(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
1711 : Generic_ELF(D, Triple, Args) {
Chris Lattner38e317d2010-07-07 16:01:42 +00001712 getFilePaths().push_back(getDriver().Dir + "/../lib");
1713 getFilePaths().push_back("/usr/lib");
Chris Lattner38e317d2010-07-07 16:01:42 +00001714}
1715
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001716Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1717 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001718 Action::ActionClass Key;
1719 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1720 Key = Action::AnalyzeJobClass;
1721 else
1722 Key = JA.getKind();
1723
1724 Tool *&T = Tools[Key];
1725 if (!T) {
1726 switch (Key) {
1727 case Action::AssembleJobClass:
1728 T = new tools::minix::Assemble(*this); break;
1729 case Action::LinkJobClass:
1730 T = new tools::minix::Link(*this); break;
1731 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001732 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001733 }
1734 }
1735
1736 return *T;
1737}
1738
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001739/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1740
Rafael Espindola0e659592012-02-19 01:38:32 +00001741AuroraUX::AuroraUX(const Driver &D, const llvm::Triple& Triple,
1742 const ArgList &Args)
1743 : Generic_GCC(D, Triple, Args) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001744
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001745 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001746 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001747 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001748
Daniel Dunbaree788e72009-12-21 18:54:17 +00001749 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001750 getFilePaths().push_back("/usr/lib");
1751 getFilePaths().push_back("/usr/sfw/lib");
1752 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001753 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001754
1755}
1756
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001757Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1758 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001759 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001760 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001761 Key = Action::AnalyzeJobClass;
1762 else
1763 Key = JA.getKind();
1764
1765 Tool *&T = Tools[Key];
1766 if (!T) {
1767 switch (Key) {
1768 case Action::AssembleJobClass:
1769 T = new tools::auroraux::Assemble(*this); break;
1770 case Action::LinkJobClass:
1771 T = new tools::auroraux::Link(*this); break;
1772 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001773 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001774 }
1775 }
1776
1777 return *T;
1778}
1779
David Chisnall31c46902012-02-15 13:39:01 +00001780/// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
1781
Rafael Espindola0e659592012-02-19 01:38:32 +00001782Solaris::Solaris(const Driver &D, const llvm::Triple& Triple,
1783 const ArgList &Args)
1784 : Generic_GCC(D, Triple, Args) {
David Chisnall31c46902012-02-15 13:39:01 +00001785
1786 getProgramPaths().push_back(getDriver().getInstalledDir());
1787 if (getDriver().getInstalledDir() != getDriver().Dir)
1788 getProgramPaths().push_back(getDriver().Dir);
1789
1790 getFilePaths().push_back(getDriver().Dir + "/../lib");
1791 getFilePaths().push_back("/usr/lib");
1792}
1793
1794Tool &Solaris::SelectTool(const Compilation &C, const JobAction &JA,
1795 const ActionList &Inputs) const {
1796 Action::ActionClass Key;
1797 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1798 Key = Action::AnalyzeJobClass;
1799 else
1800 Key = JA.getKind();
1801
1802 Tool *&T = Tools[Key];
1803 if (!T) {
1804 switch (Key) {
1805 case Action::AssembleJobClass:
1806 T = new tools::solaris::Assemble(*this); break;
1807 case Action::LinkJobClass:
1808 T = new tools::solaris::Link(*this); break;
1809 default:
1810 T = &Generic_GCC::SelectTool(C, JA, Inputs);
1811 }
1812 }
1813
1814 return *T;
1815}
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001816
Eli Friedman6b3454a2009-05-26 07:52:18 +00001817/// Linux toolchain (very bare-bones at the moment).
1818
Rafael Espindolac1da9812010-11-07 20:14:31 +00001819enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001820 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001821 DebianLenny,
1822 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001823 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001824 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001825 RHEL4,
1826 RHEL5,
1827 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001828 Fedora13,
1829 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001830 Fedora15,
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001831 Fedora16,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001832 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001833 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001834 OpenSuse11_4,
1835 OpenSuse12_1,
Douglas Gregor814638e2011-03-14 15:39:50 +00001836 UbuntuHardy,
1837 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001838 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001839 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001840 UbuntuLucid,
1841 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001842 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001843 UbuntuOneiric,
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001844 UbuntuPrecise,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001845 UnknownDistro
1846};
1847
Chris Lattnerd753b562011-05-22 05:36:06 +00001848static bool IsRedhat(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001849 return (Distro >= Fedora13 && Distro <= FedoraRawhide) ||
1850 (Distro >= RHEL4 && Distro <= RHEL6);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001851}
1852
1853static bool IsOpenSuse(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001854 return Distro >= OpenSuse11_3 && Distro <= OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001855}
1856
1857static bool IsDebian(enum LinuxDistro Distro) {
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001858 return Distro >= DebianLenny && Distro <= DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001859}
1860
1861static bool IsUbuntu(enum LinuxDistro Distro) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001862 return Distro >= UbuntuHardy && Distro <= UbuntuPrecise;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001863}
1864
Rafael Espindolac1da9812010-11-07 20:14:31 +00001865static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001866 OwningPtr<llvm::MemoryBuffer> File;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001867 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001868 StringRef Data = File.get()->getBuffer();
1869 SmallVector<StringRef, 8> Lines;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001870 Data.split(Lines, "\n");
Benjamin Kramer668ecd92012-02-06 14:36:09 +00001871 LinuxDistro Version = UnknownDistro;
1872 for (unsigned i = 0, s = Lines.size(); i != s; ++i)
1873 if (Version == UnknownDistro && Lines[i].startswith("DISTRIB_CODENAME="))
1874 Version = llvm::StringSwitch<LinuxDistro>(Lines[i].substr(17))
1875 .Case("hardy", UbuntuHardy)
1876 .Case("intrepid", UbuntuIntrepid)
1877 .Case("jaunty", UbuntuJaunty)
1878 .Case("karmic", UbuntuKarmic)
1879 .Case("lucid", UbuntuLucid)
1880 .Case("maverick", UbuntuMaverick)
1881 .Case("natty", UbuntuNatty)
1882 .Case("oneiric", UbuntuOneiric)
1883 .Case("precise", UbuntuPrecise)
1884 .Default(UnknownDistro);
1885 return Version;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001886 }
1887
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001888 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001889 StringRef Data = File.get()->getBuffer();
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001890 if (Data.startswith("Fedora release 16"))
1891 return Fedora16;
1892 else if (Data.startswith("Fedora release 15"))
Eric Christopher8f1cc072011-04-06 18:22:53 +00001893 return Fedora15;
1894 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001895 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001896 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001897 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001898 else if (Data.startswith("Fedora release") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001899 Data.find("Rawhide") != StringRef::npos)
Eric Christopher8f1cc072011-04-06 18:22:53 +00001900 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001901 else if (Data.startswith("Red Hat Enterprise Linux") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001902 Data.find("release 6") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001903 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001904 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1905 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001906 Data.find("release 5") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001907 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001908 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1909 Data.startswith("CentOS")) &&
Chris Lattner5f9e2722011-07-23 10:55:15 +00001910 Data.find("release 4") != StringRef::npos)
Chris Lattnerd753b562011-05-22 05:36:06 +00001911 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001912 return UnknownDistro;
1913 }
1914
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001915 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001916 StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001917 if (Data[0] == '5')
1918 return DebianLenny;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001919 else if (Data.startswith("squeeze/sid") || Data[0] == '6')
Rafael Espindolac1da9812010-11-07 20:14:31 +00001920 return DebianSqueeze;
Rafael Espindola0e743b12011-12-28 18:17:14 +00001921 else if (Data.startswith("wheezy/sid") || Data[0] == '7')
Eli Friedman0b200f62011-06-02 21:36:53 +00001922 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001923 return UnknownDistro;
1924 }
1925
Benjamin Kramerafe55fb2012-02-06 15:33:06 +00001926 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File))
1927 return llvm::StringSwitch<LinuxDistro>(File.get()->getBuffer())
1928 .StartsWith("openSUSE 11.3", OpenSuse11_3)
1929 .StartsWith("openSUSE 11.4", OpenSuse11_4)
1930 .StartsWith("openSUSE 12.1", OpenSuse12_1)
1931 .Default(UnknownDistro);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001932
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001933 bool Exists;
1934 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001935 return Exherbo;
1936
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001937 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1938 return ArchLinux;
1939
Rafael Espindolac1da9812010-11-07 20:14:31 +00001940 return UnknownDistro;
1941}
1942
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001943/// \brief Get our best guess at the multiarch triple for a target.
1944///
1945/// Debian-based systems are starting to use a multiarch setup where they use
1946/// a target-triple directory in the library and header search paths.
1947/// Unfortunately, this triple does not align with the vanilla target triple,
1948/// so we provide a rough mapping here.
1949static std::string getMultiarchTriple(const llvm::Triple TargetTriple,
1950 StringRef SysRoot) {
1951 // For most architectures, just use whatever we have rather than trying to be
1952 // clever.
1953 switch (TargetTriple.getArch()) {
1954 default:
1955 return TargetTriple.str();
1956
1957 // We use the existence of '/lib/<triple>' as a directory to detect some
1958 // common linux triples that don't quite match the Clang triple for both
Chandler Carruth236e0b62011-10-31 09:06:40 +00001959 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
1960 // regardless of what the actual target triple is.
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001961 case llvm::Triple::x86:
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001962 if (llvm::sys::fs::exists(SysRoot + "/lib/i386-linux-gnu"))
1963 return "i386-linux-gnu";
1964 return TargetTriple.str();
1965 case llvm::Triple::x86_64:
1966 if (llvm::sys::fs::exists(SysRoot + "/lib/x86_64-linux-gnu"))
1967 return "x86_64-linux-gnu";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001968 return TargetTriple.str();
Eli Friedman5bea4f62011-11-08 19:43:37 +00001969 case llvm::Triple::mips:
1970 if (llvm::sys::fs::exists(SysRoot + "/lib/mips-linux-gnu"))
1971 return "mips-linux-gnu";
1972 return TargetTriple.str();
1973 case llvm::Triple::mipsel:
1974 if (llvm::sys::fs::exists(SysRoot + "/lib/mipsel-linux-gnu"))
1975 return "mipsel-linux-gnu";
1976 return TargetTriple.str();
Chandler Carruth155c54c2012-02-26 09:03:21 +00001977 case llvm::Triple::ppc:
1978 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc-linux-gnu"))
1979 return "powerpc-linux-gnu";
1980 return TargetTriple.str();
1981 case llvm::Triple::ppc64:
1982 if (llvm::sys::fs::exists(SysRoot + "/lib/powerpc64-linux-gnu"))
1983 return "powerpc64-linux-gnu";
1984 return TargetTriple.str();
Chandler Carruthdeb73f82011-10-31 08:42:24 +00001985 }
1986}
1987
Chandler Carruth00646ba2012-01-25 11:24:24 +00001988static void addPathIfExists(Twine Path, ToolChain::path_list &Paths) {
1989 if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str());
1990}
1991
Rafael Espindola0e659592012-02-19 01:38:32 +00001992Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
1993 : Generic_ELF(D, Triple, Args) {
Chandler Carruth89088792012-01-24 20:08:17 +00001994 llvm::Triple::ArchType Arch = Triple.getArch();
Chandler Carruthfde8d142011-10-03 06:41:08 +00001995 const std::string &SysRoot = getDriver().SysRoot;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001996
Rafael Espindolaab784082011-09-01 16:25:49 +00001997 // OpenSuse stores the linker with the compiler, add that to the search
1998 // path.
1999 ToolChain::path_list &PPaths = getProgramPaths();
Chandler Carruthfa134592011-11-06 09:21:54 +00002000 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
Chandler Carruthfa5be912012-01-24 19:28:29 +00002001 GCCInstallation.getTriple().str() + "/bin").str());
Rafael Espindolaab784082011-09-01 16:25:49 +00002002
2003 Linker = GetProgramPath("ld");
Rafael Espindolac1da9812010-11-07 20:14:31 +00002004
2005 LinuxDistro Distro = DetectLinuxDistro(Arch);
2006
Chris Lattner64a89172011-05-22 16:45:07 +00002007 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00002008 ExtraOpts.push_back("-z");
2009 ExtraOpts.push_back("relro");
2010 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002011
Douglas Gregorf0594d82011-03-06 19:11:49 +00002012 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00002013 ExtraOpts.push_back("-X");
2014
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002015 const bool IsMips = Arch == llvm::Triple::mips ||
2016 Arch == llvm::Triple::mipsel ||
2017 Arch == llvm::Triple::mips64 ||
2018 Arch == llvm::Triple::mips64el;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002019
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002020 const bool IsAndroid = Triple.getEnvironment() == llvm::Triple::ANDROIDEABI;
2021
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002022 // Do not use 'gnu' hash style for Mips targets because .gnu.hash
2023 // and the MIPS ABI require .dynsym to be sorted in different ways.
2024 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
2025 // ABI requires a mapping between the GOT and the symbol table.
Evgeniy Stepanov704e7322012-01-13 09:30:38 +00002026 // Android loader does not support .gnu.hash.
2027 if (!IsMips && !IsAndroid) {
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002028 if (IsRedhat(Distro) || IsOpenSuse(Distro) ||
2029 (IsUbuntu(Distro) && Distro >= UbuntuMaverick))
Chandler Carruthd4e6e7e2011-12-09 04:45:18 +00002030 ExtraOpts.push_back("--hash-style=gnu");
2031
2032 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
2033 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
2034 ExtraOpts.push_back("--hash-style=both");
2035 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00002036
Chris Lattnerd753b562011-05-22 05:36:06 +00002037 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002038 ExtraOpts.push_back("--no-add-needed");
2039
Eli Friedman0b200f62011-06-02 21:36:53 +00002040 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00002041 IsOpenSuse(Distro) ||
2042 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
Benjamin Kramer668ecd92012-02-06 14:36:09 +00002043 (IsUbuntu(Distro) && Distro >= UbuntuKarmic))
Rafael Espindolac1da9812010-11-07 20:14:31 +00002044 ExtraOpts.push_back("--build-id");
2045
Chris Lattner64a89172011-05-22 16:45:07 +00002046 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00002047 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00002048
Chandler Carruthd2deee12011-10-03 05:28:29 +00002049 // The selection of paths to try here is designed to match the patterns which
2050 // the GCC driver itself uses, as this is part of the GCC-compatible driver.
2051 // This was determined by running GCC in a fake filesystem, creating all
2052 // possible permutations of these directories, and seeing which ones it added
2053 // to the link paths.
2054 path_list &Paths = getFilePaths();
Chandler Carruth3fd345a2011-02-25 06:39:53 +00002055
Chandler Carruthd747efa2012-02-11 03:31:12 +00002056 const std::string Multilib = Triple.isArch32Bit() ? "lib32" : "lib64";
Chandler Carruthdeb73f82011-10-31 08:42:24 +00002057 const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002058
Chandler Carruthd1f73062011-11-06 23:09:05 +00002059 // Add the multilib suffixed paths where they are available.
2060 if (GCCInstallation.isValid()) {
Chandler Carruthfa5be912012-01-24 19:28:29 +00002061 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth89088792012-01-24 20:08:17 +00002062 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002063 addPathIfExists((GCCInstallation.getInstallPath() +
2064 GCCInstallation.getMultiarchSuffix()),
2065 Paths);
Chandler Carruthfa5be912012-01-24 19:28:29 +00002066 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib,
Chandler Carruthd1f73062011-11-06 23:09:05 +00002067 Paths);
2068 addPathIfExists(LibPath + "/" + MultiarchTriple, Paths);
2069 addPathIfExists(LibPath + "/../" + Multilib, Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002070 }
Chandler Carruthd1f73062011-11-06 23:09:05 +00002071 addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths);
2072 addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths);
2073 addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
2074 addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths);
2075
2076 // Try walking via the GCC triple path in case of multiarch GCC
2077 // installations with strange symlinks.
2078 if (GCCInstallation.isValid())
Chandler Carruthfa5be912012-01-24 19:28:29 +00002079 addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() +
Chandler Carruthd1f73062011-11-06 23:09:05 +00002080 "/../../" + Multilib, Paths);
Rafael Espindolac7409a02011-06-03 15:39:42 +00002081
Chandler Carruth7a09d012011-10-16 10:54:30 +00002082 // Add the non-multilib suffixed paths (if potentially different).
Chandler Carruth048e6492011-10-03 18:16:54 +00002083 if (GCCInstallation.isValid()) {
2084 const std::string &LibPath = GCCInstallation.getParentLibPath();
Chandler Carruthfa5be912012-01-24 19:28:29 +00002085 const llvm::Triple &GCCTriple = GCCInstallation.getTriple();
Chandler Carruth1c6f04a2012-01-25 07:21:38 +00002086 if (!GCCInstallation.getMultiarchSuffix().empty())
Chandler Carruth048e6492011-10-03 18:16:54 +00002087 addPathIfExists(GCCInstallation.getInstallPath(), Paths);
Chandler Carruthfa5be912012-01-24 19:28:29 +00002088 addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib", Paths);
Chandler Carruth048e6492011-10-03 18:16:54 +00002089 addPathIfExists(LibPath, Paths);
Chandler Carruthd2deee12011-10-03 05:28:29 +00002090 }
Chandler Carruthfde8d142011-10-03 06:41:08 +00002091 addPathIfExists(SysRoot + "/lib", Paths);
2092 addPathIfExists(SysRoot + "/usr/lib", Paths);
Rafael Espindolac1da9812010-11-07 20:14:31 +00002093}
2094
2095bool Linux::HasNativeLLVMSupport() const {
2096 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00002097}
2098
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002099Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
2100 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002101 Action::ActionClass Key;
2102 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
2103 Key = Action::AnalyzeJobClass;
2104 else
2105 Key = JA.getKind();
2106
Rafael Espindoladda5b922010-11-07 23:13:01 +00002107 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
2108 options::OPT_no_integrated_as,
2109 IsIntegratedAssemblerDefault());
2110
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002111 Tool *&T = Tools[Key];
2112 if (!T) {
2113 switch (Key) {
2114 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00002115 if (UseIntegratedAs)
2116 T = new tools::ClangAs(*this);
2117 else
2118 T = new tools::linuxtools::Assemble(*this);
2119 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00002120 case Action::LinkJobClass:
2121 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002122 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002123 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00002124 }
2125 }
2126
2127 return *T;
2128}
2129
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002130void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2131 ArgStringList &CC1Args) const {
2132 const Driver &D = getDriver();
2133
2134 if (DriverArgs.hasArg(options::OPT_nostdinc))
2135 return;
2136
2137 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
2138 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
2139
2140 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002141 llvm::sys::Path P(D.ResourceDir);
2142 P.appendComponent("include");
Chandler Carruth07643082011-11-07 09:17:31 +00002143 addSystemInclude(DriverArgs, CC1Args, P.str());
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002144 }
2145
2146 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
2147 return;
2148
2149 // Check for configure-time C include directories.
2150 StringRef CIncludeDirs(C_INCLUDE_DIRS);
2151 if (CIncludeDirs != "") {
2152 SmallVector<StringRef, 5> dirs;
2153 CIncludeDirs.split(dirs, ":");
2154 for (SmallVectorImpl<StringRef>::iterator I = dirs.begin(), E = dirs.end();
2155 I != E; ++I) {
2156 StringRef Prefix = llvm::sys::path::is_absolute(*I) ? D.SysRoot : "";
2157 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + *I);
2158 }
2159 return;
2160 }
2161
2162 // Lacking those, try to detect the correct set of system includes for the
2163 // target triple.
2164
Chandler Carrutha4630892011-11-06 08:21:07 +00002165 // Implement generic Debian multiarch support.
2166 const StringRef X86_64MultiarchIncludeDirs[] = {
2167 "/usr/include/x86_64-linux-gnu",
2168
2169 // FIXME: These are older forms of multiarch. It's not clear that they're
2170 // in use in any released version of Debian, so we should consider
2171 // removing them.
2172 "/usr/include/i686-linux-gnu/64",
2173 "/usr/include/i486-linux-gnu/64"
2174 };
2175 const StringRef X86MultiarchIncludeDirs[] = {
2176 "/usr/include/i386-linux-gnu",
2177
2178 // FIXME: These are older forms of multiarch. It's not clear that they're
2179 // in use in any released version of Debian, so we should consider
2180 // removing them.
2181 "/usr/include/x86_64-linux-gnu/32",
2182 "/usr/include/i686-linux-gnu",
2183 "/usr/include/i486-linux-gnu"
2184 };
2185 const StringRef ARMMultiarchIncludeDirs[] = {
2186 "/usr/include/arm-linux-gnueabi"
2187 };
Eli Friedmand7df7852011-11-11 03:05:19 +00002188 const StringRef MIPSMultiarchIncludeDirs[] = {
2189 "/usr/include/mips-linux-gnu"
2190 };
2191 const StringRef MIPSELMultiarchIncludeDirs[] = {
2192 "/usr/include/mipsel-linux-gnu"
2193 };
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002194 const StringRef PPCMultiarchIncludeDirs[] = {
2195 "/usr/include/powerpc-linux-gnu"
2196 };
2197 const StringRef PPC64MultiarchIncludeDirs[] = {
2198 "/usr/include/powerpc64-linux-gnu"
2199 };
Chandler Carrutha4630892011-11-06 08:21:07 +00002200 ArrayRef<StringRef> MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002201 if (getTriple().getArch() == llvm::Triple::x86_64) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002202 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002203 } else if (getTriple().getArch() == llvm::Triple::x86) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002204 MultiarchIncludeDirs = X86MultiarchIncludeDirs;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002205 } else if (getTriple().getArch() == llvm::Triple::arm) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002206 MultiarchIncludeDirs = ARMMultiarchIncludeDirs;
Eli Friedmand7df7852011-11-11 03:05:19 +00002207 } else if (getTriple().getArch() == llvm::Triple::mips) {
2208 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs;
2209 } else if (getTriple().getArch() == llvm::Triple::mipsel) {
2210 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs;
Chandler Carruth079d2bb2012-02-26 09:21:43 +00002211 } else if (getTriple().getArch() == llvm::Triple::ppc) {
2212 MultiarchIncludeDirs = PPCMultiarchIncludeDirs;
2213 } else if (getTriple().getArch() == llvm::Triple::ppc64) {
2214 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs;
Chandler Carrutha4630892011-11-06 08:21:07 +00002215 }
2216 for (ArrayRef<StringRef>::iterator I = MultiarchIncludeDirs.begin(),
2217 E = MultiarchIncludeDirs.end();
2218 I != E; ++I) {
Chandler Carruthd936d9d2011-11-09 03:46:20 +00002219 if (llvm::sys::fs::exists(D.SysRoot + *I)) {
Chandler Carrutha4630892011-11-06 08:21:07 +00002220 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + *I);
2221 break;
2222 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002223 }
2224
2225 if (getTriple().getOS() == llvm::Triple::RTEMS)
2226 return;
2227
Chandler Carruthc44bc2d2011-11-08 17:19:47 +00002228 // Add an include of '/include' directly. This isn't provided by default by
2229 // system GCCs, but is often used with cross-compiling GCCs, and harmless to
2230 // add even when Clang is acting as-if it were a system compiler.
2231 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include");
2232
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002233 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
2234}
2235
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00002236/// \brief Helper to add the thre variant paths for a libstdc++ installation.
2237/*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir,
2238 const ArgList &DriverArgs,
2239 ArgStringList &CC1Args) {
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002240 if (!llvm::sys::fs::exists(Base))
2241 return false;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002242 addSystemInclude(DriverArgs, CC1Args, Base);
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002243 addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir);
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002244 addSystemInclude(DriverArgs, CC1Args, Base + "/backward");
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002245 return true;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002246}
2247
2248void Linux::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
2249 ArgStringList &CC1Args) const {
2250 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
2251 DriverArgs.hasArg(options::OPT_nostdincxx))
2252 return;
2253
Chandler Carrutheb35ffc2011-11-07 09:01:17 +00002254 // Check if libc++ has been enabled and provide its include paths if so.
2255 if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx) {
2256 // libc++ is always installed at a fixed path on Linux currently.
2257 addSystemInclude(DriverArgs, CC1Args,
2258 getDriver().SysRoot + "/usr/include/c++/v1");
2259 return;
2260 }
2261
Chandler Carruthfc52f752012-01-25 08:04:13 +00002262 // We need a detected GCC installation on Linux to provide libstdc++'s
2263 // headers. We handled the libc++ case above.
2264 if (!GCCInstallation.isValid())
2265 return;
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002266
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002267 // By default, look for the C++ headers in an include directory adjacent to
2268 // the lib directory of the GCC installation. Note that this is expect to be
2269 // equivalent to '/usr/include/c++/X.Y' in almost all cases.
2270 StringRef LibDir = GCCInstallation.getParentLibPath();
2271 StringRef InstallDir = GCCInstallation.getInstallPath();
2272 StringRef Version = GCCInstallation.getVersion();
2273 if (!addLibStdCXXIncludePaths(LibDir + "/../include/c++/" + Version,
Chandler Carruthfc52f752012-01-25 08:04:13 +00002274 (GCCInstallation.getTriple().str() +
2275 GCCInstallation.getMultiarchSuffix()),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002276 DriverArgs, CC1Args)) {
2277 // Gentoo is weird and places its headers inside the GCC install, so if the
2278 // first attempt to find the headers fails, try this pattern.
2279 addLibStdCXXIncludePaths(InstallDir + "/include/g++-v4",
Chandler Carruthfc52f752012-01-25 08:04:13 +00002280 (GCCInstallation.getTriple().str() +
2281 GCCInstallation.getMultiarchSuffix()),
Chandler Carruthabaa1d72011-11-06 10:31:01 +00002282 DriverArgs, CC1Args);
2283 }
Chandler Carruth7d7e9f92011-11-05 20:17:13 +00002284}
2285
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002286/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
2287
Rafael Espindola0e659592012-02-19 01:38:32 +00002288DragonFly::DragonFly(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
2289 : Generic_ELF(D, Triple, Args) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002290
2291 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002292 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00002293 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00002294 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002295
Daniel Dunbaree788e72009-12-21 18:54:17 +00002296 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002297 getFilePaths().push_back("/usr/lib");
2298 getFilePaths().push_back("/usr/lib/gcc41");
2299}
2300
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002301Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
2302 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002303 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00002304 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002305 Key = Action::AnalyzeJobClass;
2306 else
2307 Key = JA.getKind();
2308
2309 Tool *&T = Tools[Key];
2310 if (!T) {
2311 switch (Key) {
2312 case Action::AssembleJobClass:
2313 T = new tools::dragonfly::Assemble(*this); break;
2314 case Action::LinkJobClass:
2315 T = new tools::dragonfly::Link(*this); break;
2316 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00002317 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00002318 }
2319 }
2320
2321 return *T;
2322}