blob: 14ca5532632f0d7d69a60049be1a4bf9b942a561 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar59e5e882009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
11
Rafael Espindola0ddfbe22011-06-02 22:18:46 +000012#ifdef HAVE_CLANG_CONFIG_H
13# include "clang/Config/config.h"
14#endif
15
Daniel Dunbardac54a82009-03-25 04:13:45 +000016#include "clang/Driver/Arg.h"
17#include "clang/Driver/ArgList.h"
Daniel Dunbar6232d342010-05-20 21:48:38 +000018#include "clang/Driver/Compilation.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000019#include "clang/Driver/Driver.h"
Daniel Dunbaraabb0b12009-03-25 06:12:34 +000020#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000021#include "clang/Driver/HostInfo.h"
Daniel Dunbaraa767372009-11-19 00:15:11 +000022#include "clang/Driver/OptTable.h"
Daniel Dunbaraabb0b12009-03-25 06:12:34 +000023#include "clang/Driver/Option.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000024#include "clang/Driver/Options.h"
Douglas Gregor63682352010-09-03 17:16:03 +000025#include "clang/Basic/Version.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000026
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +000027#include "llvm/ADT/SmallString.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000028#include "llvm/ADT/StringExtras.h"
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +000029#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000030#include "llvm/Support/FileSystem.h"
Rafael Espindolac8f008f2010-11-07 20:14:31 +000031#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar0af75a12009-03-25 06:58:31 +000032#include "llvm/Support/raw_ostream.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000033#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000034#include "llvm/Support/system_error.h"
Daniel Dunbar76ce7412009-03-23 16:15:50 +000035
Daniel Dunbarb5023e92009-04-10 21:00:07 +000036#include <cstdlib> // ::getenv
37
Daniel Dunbar59e5e882009-03-20 00:20:03 +000038using namespace clang::driver;
39using namespace clang::driver::toolchains;
40
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +000041/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +000042
Daniel Dunbar71c723d2010-08-02 05:44:07 +000043Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
Daniel Dunbar7c8701752010-01-27 00:56:44 +000044 : ToolChain(Host, Triple), TargetInitialized(false)
Daniel Dunbar6276f992009-09-18 08:15:13 +000045{
Daniel Dunbar71c723d2010-08-02 05:44:07 +000046 // Compute the initial Darwin version based on the host.
47 bool HadExtra;
48 std::string OSName = Triple.getOSName();
Daniel Dunbar54805782011-02-25 21:20:15 +000049 if (!Driver::GetReleaseVersion(&OSName.c_str()[6],
Daniel Dunbar71c723d2010-08-02 05:44:07 +000050 DarwinVersion[0], DarwinVersion[1],
51 DarwinVersion[2], HadExtra))
52 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
53
Daniel Dunbarc1964212009-03-26 16:23:12 +000054 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar71c723d2010-08-02 05:44:07 +000055 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
56 << DarwinVersion[1];
Daniel Dunbar6276f992009-09-18 08:15:13 +000057}
58
Daniel Dunbarcc7df6c2010-08-02 05:43:56 +000059types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
60 types::ID Ty = types::lookupTypeForExtension(Ext);
61
62 // Darwin always preprocesses assembly files (unless -x is used explicitly).
63 if (Ty == types::TY_PP_Asm)
64 return types::TY_Asm;
65
66 return Ty;
67}
68
Daniel Dunbar62123a12010-09-17 00:24:52 +000069bool Darwin::HasNativeLLVMSupport() const {
70 return true;
71}
72
Daniel Dunbardcc3b652010-01-22 02:04:58 +000073// FIXME: Can we tablegen this?
74static const char *GetArmArchForMArch(llvm::StringRef Value) {
75 if (Value == "armv6k")
76 return "armv6";
77
78 if (Value == "armv5tej")
79 return "armv5";
80
81 if (Value == "xscale")
82 return "xscale";
83
84 if (Value == "armv4t")
85 return "armv4t";
86
87 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
88 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
89 Value == "armv7m")
90 return "armv7";
91
92 return 0;
93}
94
95// FIXME: Can we tablegen this?
96static const char *GetArmArchForMCpu(llvm::StringRef Value) {
97 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
98 Value == "arm946e-s" || Value == "arm966e-s" ||
99 Value == "arm968e-s" || Value == "arm10e" ||
100 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
101 Value == "arm1026ej-s")
102 return "armv5";
103
104 if (Value == "xscale")
105 return "xscale";
106
107 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
Bob Wilsond9249412011-03-21 20:40:05 +0000108 Value == "arm1176jz-s" || Value == "arm1176jzf-s" ||
109 Value == "cortex-m0" )
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000110 return "armv6";
111
112 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
113 return "armv7";
114
115 return 0;
116}
117
118llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
119 switch (getTriple().getArch()) {
120 default:
121 return getArchName();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000122
Douglas Gregord9bb1522011-03-06 19:11:49 +0000123 case llvm::Triple::thumb:
Daniel Dunbardcc3b652010-01-22 02:04:58 +0000124 case llvm::Triple::arm: {
125 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
126 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
127 return Arch;
128
129 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
130 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
131 return Arch;
132
133 return "arm";
134 }
135 }
136}
137
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000138Darwin::~Darwin() {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000139 // Free tool implementations.
140 for (llvm::DenseMap<unsigned, Tool*>::iterator
141 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
142 delete it->second;
143}
144
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000145std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
146 llvm::Triple Triple(ComputeLLVMTriple(Args));
147
148 // If the target isn't initialized (e.g., an unknown Darwin platform, return
149 // the default triple).
150 if (!isTargetInitialized())
151 return Triple.getTriple();
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000152
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000153 unsigned Version[3];
154 getTargetVersion(Version);
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000155
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000156 llvm::SmallString<16> Str;
Daniel Dunbar5c568282011-04-19 21:45:47 +0000157 llvm::raw_svector_ostream(Str)
Daniel Dunbar308cfd02011-04-19 23:34:17 +0000158 << (isTargetIPhoneOS() ? "ios" : "macosx")
Daniel Dunbar5c568282011-04-19 21:45:47 +0000159 << Version[0] << "." << Version[1] << "." << Version[2];
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000160 Triple.setOSName(Str.str());
161
162 return Triple.getTriple();
163}
164
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +0000165Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
166 const ActionList &Inputs) const {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000167 Action::ActionClass Key;
Daniel Dunbar151a3722011-03-18 20:14:03 +0000168
169 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
170 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
171 if (Inputs.size() == 1 &&
172 types::isCXX(Inputs[0]->getType()) &&
173 getTriple().getOS() == llvm::Triple::Darwin &&
174 getTriple().getArch() == llvm::Triple::x86 &&
175 C.getArgs().getLastArg(options::OPT_fapple_kext))
176 Key = JA.getKind();
177 else
178 Key = Action::AnalyzeJobClass;
179 } else
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000180 Key = JA.getKind();
181
Daniel Dunbar6232d342010-05-20 21:48:38 +0000182 // FIXME: This doesn't belong here, but ideally we will support static soon
183 // anyway.
184 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
185 C.getArgs().hasArg(options::OPT_static) ||
186 C.getArgs().hasArg(options::OPT_fapple_kext));
187 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
188 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
189 options::OPT_no_integrated_as,
190 IsIADefault);
191
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000192 Tool *&T = Tools[Key];
193 if (!T) {
194 switch (Key) {
195 case Action::InputClass:
196 case Action::BindArchClass:
197 assert(0 && "Invalid tool kind.");
198 case Action::PreprocessJobClass:
Daniel Dunbarf64f5302009-03-29 22:27:40 +0000199 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000200 case Action::AnalyzeJobClass:
201 T = new tools::Clang(*this); break;
Daniel Dunbarf64f5302009-03-29 22:27:40 +0000202 case Action::PrecompileJobClass:
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000203 case Action::CompileJobClass:
Daniel Dunbarf64f5302009-03-29 22:27:40 +0000204 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar6232d342010-05-20 21:48:38 +0000205 case Action::AssembleJobClass: {
206 if (UseIntegratedAs)
207 T = new tools::ClangAs(*this);
208 else
209 T = new tools::darwin::Assemble(*this);
210 break;
211 }
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000212 case Action::LinkJobClass:
Daniel Dunbar5095b292009-09-04 17:39:02 +0000213 T = new tools::darwin::Link(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000214 case Action::LipoJobClass:
215 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar88299622010-06-04 18:28:36 +0000216 case Action::DsymutilJobClass:
217 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000218 }
219 }
220
221 return *T;
222}
223
Daniel Dunbar26d482a2009-09-18 08:15:03 +0000224
Daniel Dunbar71c723d2010-08-02 05:44:07 +0000225DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
226 : Darwin(Host, Triple)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000227{
Daniel Dunbarc76a9e62011-03-18 19:25:15 +0000228 std::string UsrPrefix = "llvm-gcc-4.2/";
229
Daniel Dunbar00aff042010-09-17 08:22:12 +0000230 getProgramPaths().push_back(getDriver().getInstalledDir());
231 if (getDriver().getInstalledDir() != getDriver().Dir)
232 getProgramPaths().push_back(getDriver().Dir);
233
Daniel Dunbar6276f992009-09-18 08:15:13 +0000234 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbar88979912010-08-01 22:29:51 +0000235 getProgramPaths().push_back(getDriver().getInstalledDir());
236 if (getDriver().getInstalledDir() != getDriver().Dir)
237 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar00aff042010-09-17 08:22:12 +0000238
239 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbarc76a9e62011-03-18 19:25:15 +0000240 // also add the GCC libexec paths. This is legacy code that can be removed
241 // once fallback is no longer useful.
Daniel Dunbar00aff042010-09-17 08:22:12 +0000242 std::string ToolChainDir = "i686-apple-darwin";
243 ToolChainDir += llvm::utostr(DarwinVersion[0]);
244 ToolChainDir += "/4.2.1";
245
246 std::string Path = getDriver().Dir;
Daniel Dunbarc76a9e62011-03-18 19:25:15 +0000247 Path += "/../" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar00aff042010-09-17 08:22:12 +0000248 Path += ToolChainDir;
249 getProgramPaths().push_back(Path);
250
Daniel Dunbarc76a9e62011-03-18 19:25:15 +0000251 Path = "/usr/" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar00aff042010-09-17 08:22:12 +0000252 Path += ToolChainDir;
253 getProgramPaths().push_back(Path);
Daniel Dunbar6276f992009-09-18 08:15:13 +0000254}
255
256void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
257 ArgStringList &CmdArgs) const {
258 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar9dcde4b2010-06-30 23:56:13 +0000259
260 // Unfortunately, we still might depend on a few of the libraries that are
261 // only available in the gcc library directory (in particular
262 // libstdc++.dylib). For now, hardcode the path to the known install location.
263 llvm::sys::Path P(getDriver().Dir);
264 P.eraseComponent(); // .../usr/bin -> ../usr
265 P.appendComponent("lib");
266 P.appendComponent("gcc");
267 switch (getTriple().getArch()) {
268 default:
269 assert(0 && "Invalid Darwin arch!");
270 case llvm::Triple::x86:
271 case llvm::Triple::x86_64:
272 P.appendComponent("i686-apple-darwin10");
273 break;
274 case llvm::Triple::arm:
275 case llvm::Triple::thumb:
276 P.appendComponent("arm-apple-darwin10");
277 break;
278 case llvm::Triple::ppc:
279 case llvm::Triple::ppc64:
280 P.appendComponent("powerpc-apple-darwin10");
281 break;
282 }
283 P.appendComponent("4.2.1");
Daniel Dunbar09a48512010-08-23 20:58:52 +0000284
285 // Determine the arch specific GCC subdirectory.
286 const char *ArchSpecificDir = 0;
287 switch (getTriple().getArch()) {
288 default:
289 break;
290 case llvm::Triple::arm:
Daniel Dunbare9f71512010-08-26 00:55:52 +0000291 case llvm::Triple::thumb: {
292 std::string Triple = ComputeLLVMTriple(Args);
293 llvm::StringRef TripleStr = Triple;
294 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
295 ArchSpecificDir = "v5";
296 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
297 ArchSpecificDir = "v6";
298 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
299 ArchSpecificDir = "v7";
Daniel Dunbar09a48512010-08-23 20:58:52 +0000300 break;
Daniel Dunbare9f71512010-08-26 00:55:52 +0000301 }
Daniel Dunbar09a48512010-08-23 20:58:52 +0000302 case llvm::Triple::ppc64:
303 ArchSpecificDir = "ppc64";
304 break;
305 case llvm::Triple::x86_64:
306 ArchSpecificDir = "x86_64";
307 break;
308 }
309
310 if (ArchSpecificDir) {
311 P.appendComponent(ArchSpecificDir);
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000312 bool Exists;
313 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar09a48512010-08-23 20:58:52 +0000314 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
315 P.eraseComponent();
316 }
317
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000318 bool Exists;
319 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar9dcde4b2010-06-30 23:56:13 +0000320 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar6276f992009-09-18 08:15:13 +0000321}
322
323void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
324 ArgStringList &CmdArgs) const {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000325 // Darwin doesn't support real static executables, don't link any runtime
326 // libraries with -static.
327 if (Args.hasArg(options::OPT_static))
Daniel Dunbar6276f992009-09-18 08:15:13 +0000328 return;
Daniel Dunbar6276f992009-09-18 08:15:13 +0000329
330 // Reject -static-libgcc for now, we can deal with this when and if someone
331 // cares. This is useful in situations where someone wants to statically link
332 // something like libstdc++, and needs its runtime support routines.
333 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000334 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar6276f992009-09-18 08:15:13 +0000335 << A->getAsString(Args);
336 return;
337 }
338
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000339 // Otherwise link libSystem, then the dynamic runtime library, and finally any
340 // target specific static runtime library.
Daniel Dunbar6276f992009-09-18 08:15:13 +0000341 CmdArgs.push_back("-lSystem");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000342
343 // Select the dynamic runtime library and the target specific static library.
344 const char *DarwinStaticLib = 0;
Daniel Dunbar15c89422010-01-27 00:56:37 +0000345 if (isTargetIPhoneOS()) {
Daniel Dunbar2f31fb92011-04-30 04:25:16 +0000346 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
347 // it never went into the SDK.
348 if (!isTargetIOSSimulator())
349 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000350
Daniel Dunbard1076382011-04-18 23:48:36 +0000351 // We currently always need a static runtime library for iOS.
352 DarwinStaticLib = "libclang_rt.ios.a";
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000353 } else {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000354 // The dynamic runtime library was merged with libSystem for 10.6 and
355 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000356 if (isMacosxVersionLT(10, 5))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000357 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbar6d23b2f2010-01-27 00:57:03 +0000358 else if (isMacosxVersionLT(10, 6))
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000359 CmdArgs.push_back("-lgcc_s.10.5");
360
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000361 // For OS X, we thought we would only need a static runtime library when
Chris Lattner57540c52011-04-15 05:22:18 +0000362 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000363 // omitted from 10.4.dylib.
364 //
365 // Unfortunately, that turned out to not be true, because Darwin system
366 // headers can still use eprintf on i386, and it is not exported from
367 // libSystem. Therefore, we still must provide a runtime library just for
368 // the tiny tiny handful of projects that *might* use that symbol.
369 if (isMacosxVersionLT(10, 5)) {
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000370 DarwinStaticLib = "libclang_rt.10.4.a";
Daniel Dunbarda4f6b52010-09-22 00:03:52 +0000371 } else {
372 if (getTriple().getArch() == llvm::Triple::x86)
373 DarwinStaticLib = "libclang_rt.eprintf.a";
374 }
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000375 }
376
377 /// Add the target specific static library, if needed.
378 if (DarwinStaticLib) {
379 llvm::sys::Path P(getDriver().ResourceDir);
380 P.appendComponent("lib");
381 P.appendComponent("darwin");
382 P.appendComponent(DarwinStaticLib);
383
384 // For now, allow missing resource libraries to support developers who may
385 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000386 bool Exists;
387 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar7cde09a2010-01-22 03:38:14 +0000388 CmdArgs.push_back(Args.MakeArgString(P.str()));
389 }
Daniel Dunbar6276f992009-09-18 08:15:13 +0000390}
391
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000392void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000393 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000394
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000395 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000396 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
397 Arg *iOSSimVersion = Args.getLastArg(
398 options::OPT_mios_simulator_version_min_EQ);
399 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000400 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarc8b7af82009-04-10 20:11:50 +0000401 << OSXVersion->getAsString(Args)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000402 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
403 iOSVersion = iOSSimVersion = 0;
404 } else if (iOSVersion && iOSSimVersion) {
405 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
406 << iOSVersion->getAsString(Args)
407 << iOSSimVersion->getAsString(Args);
408 iOSSimVersion = 0;
409 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
410 // If not deployment target was specified on the command line, check for
Daniel Dunbard54669d2010-01-26 01:45:19 +0000411 // environment defines.
412 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000413 const char *iOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
414 const char *iOSSimTarget = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET");
Daniel Dunbarb5023e92009-04-10 21:00:07 +0000415
Daniel Dunbard54669d2010-01-26 01:45:19 +0000416 // Ignore empty strings.
417 if (OSXTarget && OSXTarget[0] == '\0')
418 OSXTarget = 0;
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000419 if (iOSTarget && iOSTarget[0] == '\0')
420 iOSTarget = 0;
421 if (iOSSimTarget && iOSSimTarget[0] == '\0')
422 iOSSimTarget = 0;
Daniel Dunbard54669d2010-01-26 01:45:19 +0000423
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000424 // Handle conflicting deployment targets
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000425 //
426 // FIXME: Don't hardcode default here.
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000427
428 // Do not allow conflicts with the iOS simulator target.
429 if (iOSSimTarget && (OSXTarget || iOSTarget)) {
430 getDriver().Diag(clang::diag::err_drv_conflicting_deployment_targets)
431 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
432 << (OSXTarget ? "MACOSX_DEPLOYMENT_TARGET" :
433 "IPHONEOS_DEPLOYMENT_TARGET");
434 }
435
436 // Allow conflicts among OSX and iOS for historical reasons, but choose the
437 // default platform.
438 if (OSXTarget && iOSTarget) {
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000439 if (getTriple().getArch() == llvm::Triple::arm ||
440 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbare543a5e2010-03-20 08:47:42 +0000441 OSXTarget = 0;
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000442 else
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000443 iOSTarget = 0;
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000444 }
Daniel Dunbar65969842010-01-29 17:02:25 +0000445
Daniel Dunbarffa70e82010-02-02 17:31:12 +0000446 if (OSXTarget) {
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000447 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000448 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
449 Args.append(OSXVersion);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000450 } else if (iOSTarget) {
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000451 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000452 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
453 Args.append(iOSVersion);
454 } else if (iOSSimTarget) {
455 const Option *O = Opts.getOption(
456 options::OPT_mios_simulator_version_min_EQ);
457 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
458 Args.append(iOSSimVersion);
Daniel Dunbard54669d2010-01-26 01:45:19 +0000459 } else {
Daniel Dunbarb2447152010-07-15 16:18:06 +0000460 // Otherwise, assume we are targeting OS X.
461 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000462 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
463 Args.append(OSXVersion);
Daniel Dunbar84e727f2009-09-04 18:35:21 +0000464 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Daniel Dunbara9cbb6b92011-04-30 04:20:40 +0000467 // Reject invalid architecture combinations.
468 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
469 getTriple().getArch() != llvm::Triple::x86_64)) {
470 getDriver().Diag(clang::diag::err_drv_invalid_arch_for_deployment_target)
471 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
472 }
473
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000474 // Set the tool chain target information.
475 unsigned Major, Minor, Micro;
476 bool HadExtra;
477 if (OSXVersion) {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000478 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000479 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
480 Micro, HadExtra) || HadExtra ||
Daniel Dunbarbbd48222011-04-21 21:27:33 +0000481 Major != 10 || Minor >= 100 || Micro >= 100)
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000482 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
483 << OSXVersion->getAsString(Args);
484 } else {
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000485 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
486 assert(Version && "Unknown target platform!");
487 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000488 Micro, HadExtra) || HadExtra ||
489 Major >= 10 || Minor >= 100 || Micro >= 100)
490 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000491 << Version->getAsString(Args);
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000492 }
Daniel Dunbar9aaeb642011-04-30 04:15:58 +0000493
Daniel Dunbarb1189432011-04-30 04:18:16 +0000494 bool IsIOSSim = bool(iOSSimVersion);
495
496 // In GCC, the simulator historically was treated as being OS X in some
497 // contexts, like determining the link logic, despite generally being called
498 // with an iOS deployment target. For compatibility, we detect the
499 // simulator as iOS + x86, and treat it differently in a few contexts.
500 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
501 getTriple().getArch() == llvm::Triple::x86_64))
502 IsIOSSim = true;
503
504 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000505}
506
Daniel Dunbar3f7796f2010-09-17 01:20:05 +0000507void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000508 ArgStringList &CmdArgs) const {
509 CXXStdlibType Type = GetCXXStdlibType(Args);
510
511 switch (Type) {
512 case ToolChain::CST_Libcxx:
513 CmdArgs.push_back("-lc++");
514 break;
515
516 case ToolChain::CST_Libstdcxx: {
517 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
518 // it was previously found in the gcc lib dir. However, for all the Darwin
519 // platforms we care about it was -lstdc++.6, so we search for that
520 // explicitly if we can't see an obvious -lstdc++ candidate.
521
522 // Check in the sysroot first.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000523 bool Exists;
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000524 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
525 llvm::sys::Path P(A->getValue(Args));
526 P.appendComponent("usr");
527 P.appendComponent("lib");
528 P.appendComponent("libstdc++.dylib");
529
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000530 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000531 P.eraseComponent();
532 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000533 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000534 CmdArgs.push_back(Args.MakeArgString(P.str()));
535 return;
536 }
537 }
538 }
539
540 // Otherwise, look in the root.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000541 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
542 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbar8fa86b12010-09-17 01:16:06 +0000543 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
544 return;
545 }
546
547 // Otherwise, let the linker search.
548 CmdArgs.push_back("-lstdc++");
549 break;
550 }
551 }
552}
553
Shantonu Senafeb03b2010-09-17 18:39:08 +0000554void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
555 ArgStringList &CmdArgs) const {
556
557 // For Darwin platforms, use the compiler-rt-based support library
558 // instead of the gcc-provided one (which is also incidentally
559 // only present in the gcc lib dir, which makes it hard to find).
560
561 llvm::sys::Path P(getDriver().ResourceDir);
562 P.appendComponent("lib");
563 P.appendComponent("darwin");
564 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000565
Shantonu Senafeb03b2010-09-17 18:39:08 +0000566 // For now, allow missing resource libraries to support developers who may
567 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000568 bool Exists;
569 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Senafeb03b2010-09-17 18:39:08 +0000570 CmdArgs.push_back(Args.MakeArgString(P.str()));
571}
572
Daniel Dunbarb2b8a912010-07-19 17:11:33 +0000573DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
574 const char *BoundArch) const {
575 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
576 const OptTable &Opts = getDriver().getOpts();
577
578 // FIXME: We really want to get out of the tool chain level argument
579 // translation business, as it makes the driver functionality much
580 // more opaque. For now, we follow gcc closely solely for the
581 // purpose of easily achieving feature parity & testability. Once we
582 // have something that works, we should reevaluate each translation
583 // and try to push it down into tool specific logic.
Daniel Dunbar3b8e50d2010-01-27 00:56:25 +0000584
Daniel Dunbar775d4062010-06-11 22:00:26 +0000585 for (ArgList::const_iterator it = Args.begin(),
586 ie = Args.end(); it != ie; ++it) {
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000587 Arg *A = *it;
588
589 if (A->getOption().matches(options::OPT_Xarch__)) {
590 // FIXME: Canonicalize name.
591 if (getArchName() != A->getValue(Args, 0))
592 continue;
593
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000594 Arg *OriginalArg = A;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000595 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
596 unsigned Prev = Index;
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000597 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump11289f42009-09-09 15:08:12 +0000598
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000599 // If the argument parsing failed or more than one argument was
600 // consumed, the -Xarch_ argument's parameter tried to consume
601 // extra arguments. Emit an error and ignore.
602 //
603 // We also want to disallow any options which would alter the
604 // driver behavior; that isn't going to work in our model. We
605 // use isDriverOption() as an approximation, although things
606 // like -O4 are going to slip through.
Daniel Dunbar5a784c82011-04-21 17:41:34 +0000607 if (!XarchArg || Index > Prev + 1) {
Daniel Dunbar6914a982011-04-21 17:32:21 +0000608 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_with_args)
609 << A->getAsString(Args);
610 continue;
611 } else if (XarchArg->getOption().isDriverOption()) {
612 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000613 << A->getAsString(Args);
614 continue;
615 }
616
Daniel Dunbar53b406f2009-03-29 22:29:05 +0000617 XarchArg->setBaseArg(A);
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000618 A = XarchArg;
Daniel Dunbar3f1a1ff2010-06-14 21:23:08 +0000619
620 DAL->AddSynthesizedArg(A);
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000621
622 // Linker input arguments require custom handling. The problem is that we
623 // have already constructed the phase actions, so we can not treat them as
624 // "input arguments".
625 if (A->getOption().isLinkerInput()) {
626 // Convert the argument into individual Zlinker_input_args.
627 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
628 DAL->AddSeparateArg(OriginalArg,
629 Opts.getOption(options::OPT_Zlinker_input),
630 A->getValue(Args, i));
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000631
Daniel Dunbar1094bb12011-02-19 05:33:51 +0000632 }
633 continue;
634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000636
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000637 // Sob. These is strictly gcc compatible for the time being. Apple
638 // gcc translates options twice, which means that self-expanding
639 // options add duplicates.
Daniel Dunbar8c009572009-11-19 04:14:53 +0000640 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000641 default:
642 DAL->append(A);
643 break;
644
645 case options::OPT_mkernel:
646 case options::OPT_fapple_kext:
647 DAL->append(A);
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000648 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
649 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000650 break;
Mike Stump11289f42009-09-09 15:08:12 +0000651
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000652 case options::OPT_dependency_file:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000653 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
654 A->getValue(Args));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000655 break;
656
657 case options::OPT_gfull:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000658 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
659 DAL->AddFlagArg(A,
660 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000661 break;
662
663 case options::OPT_gused:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000664 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
665 DAL->AddFlagArg(A,
666 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000667 break;
668
669 case options::OPT_fterminated_vtables:
670 case options::OPT_findirect_virtual_calls:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000671 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
672 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000673 break;
674
675 case options::OPT_shared:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000676 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000677 break;
678
679 case options::OPT_fconstant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000680 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000681 break;
682
683 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000684 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000685 break;
686
687 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000688 DAL->AddFlagArg(A,
689 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000690 break;
691
692 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000693 DAL->AddFlagArg(A,
694 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000695 break;
696
697 case options::OPT_fpascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000698 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000699 break;
700
701 case options::OPT_fno_pascal_strings:
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000702 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000703 break;
704 }
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000705 }
706
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000707 if (getTriple().getArch() == llvm::Triple::x86 ||
708 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbarfffd1812009-11-19 04:00:53 +0000709 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000710 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000711
712 // Add the arch options based on the particular spelling of -arch, to match
713 // how the driver driver works.
714 if (BoundArch) {
715 llvm::StringRef Name = BoundArch;
716 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
717 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
718
719 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
720 // which defines the list of which architectures we accept.
721 if (Name == "ppc")
722 ;
723 else if (Name == "ppc601")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000724 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000725 else if (Name == "ppc603")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000726 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000727 else if (Name == "ppc604")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000728 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000729 else if (Name == "ppc604e")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000730 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000731 else if (Name == "ppc750")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000732 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000733 else if (Name == "ppc7400")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000734 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000735 else if (Name == "ppc7450")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000736 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000737 else if (Name == "ppc970")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000738 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000739
740 else if (Name == "ppc64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000741 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000742
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000743 else if (Name == "i386")
744 ;
745 else if (Name == "i486")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000746 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000747 else if (Name == "i586")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000748 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000749 else if (Name == "i686")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000750 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000751 else if (Name == "pentium")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000752 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000753 else if (Name == "pentium2")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000754 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000755 else if (Name == "pentpro")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000756 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000757 else if (Name == "pentIIm3")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000758 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000759
760 else if (Name == "x86_64")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000761 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000762
763 else if (Name == "arm")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000764 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000765 else if (Name == "armv4t")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000766 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000767 else if (Name == "armv5")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000768 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000769 else if (Name == "xscale")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000770 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000771 else if (Name == "armv6")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000772 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000773 else if (Name == "armv7")
Daniel Dunbar2d6e9ee2010-06-14 20:20:41 +0000774 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000775
776 else
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000777 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar3c7b9ca2009-09-09 22:33:15 +0000778 }
Daniel Dunbar0af75a12009-03-25 06:58:31 +0000779
Daniel Dunbar354e96d2010-07-19 17:11:36 +0000780 // Add an explicit version min argument for the deployment target. We do this
781 // after argument translation because -Xarch_ arguments may add a version min
782 // argument.
783 AddDeploymentTarget(*DAL);
784
Daniel Dunbaraabb0b12009-03-25 06:12:34 +0000785 return DAL;
Mike Stump11289f42009-09-09 15:08:12 +0000786}
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000787
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000788bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000789 // FIXME: Gross; we should probably have some separate target
790 // definition, possibly even reusing the one in clang.
791 return getArchName() == "x86_64";
792}
793
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000794bool Darwin::UseDwarfDebugFlags() const {
795 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
796 return S[0] != '\0';
797 return false;
798}
799
Daniel Dunbar3241d402010-02-10 18:49:11 +0000800bool Darwin::UseSjLjExceptions() const {
801 // Darwin uses SjLj exceptions on ARM.
802 return (getTriple().getArch() == llvm::Triple::arm ||
803 getTriple().getArch() == llvm::Triple::thumb);
804}
805
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000806const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000807 return "pic";
808}
809
Daniel Dunbarf0a5b9b2009-09-04 18:34:51 +0000810const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000811 if (getArchName() == "x86_64")
812 return "pic";
813 return 0;
814}
815
Daniel Dunbar733b0f82011-03-01 18:49:30 +0000816bool Darwin::SupportsProfiling() const {
817 // Profiling instrumentation is only supported on x86.
818 return getArchName() == "i386" || getArchName() == "x86_64";
819}
820
Daniel Dunbar16334e12010-04-10 16:20:23 +0000821bool Darwin::SupportsObjCGC() const {
822 // Garbage collection is supported everywhere except on iPhone OS.
823 return !isTargetIPhoneOS();
824}
825
Daniel Dunbar82eb4ce2010-08-23 22:35:37 +0000826std::string
827Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
828 return ComputeLLVMTriple(Args);
829}
830
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000831/// Generic_GCC - A tool chain using the 'gcc' command to perform
832/// all subcommands; this relies on gcc translating the majority of
833/// command line options.
834
Daniel Dunbar51c7f972009-05-22 02:53:45 +0000835Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump11289f42009-09-09 15:08:12 +0000836 : ToolChain(Host, Triple) {
Daniel Dunbar88979912010-08-01 22:29:51 +0000837 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +0000838 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +0000839 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar76ce7412009-03-23 16:15:50 +0000840}
841
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000842Generic_GCC::~Generic_GCC() {
843 // Free tool implementations.
844 for (llvm::DenseMap<unsigned, Tool*>::iterator
845 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
846 delete it->second;
847}
848
Mike Stump11289f42009-09-09 15:08:12 +0000849Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +0000850 const JobAction &JA,
851 const ActionList &Inputs) const {
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000852 Action::ActionClass Key;
Daniel Dunbar083edf72009-12-21 18:54:17 +0000853 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000854 Key = Action::AnalyzeJobClass;
855 else
856 Key = JA.getKind();
857
858 Tool *&T = Tools[Key];
859 if (!T) {
860 switch (Key) {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000861 case Action::InputClass:
862 case Action::BindArchClass:
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000863 assert(0 && "Invalid tool kind.");
864 case Action::PreprocessJobClass:
865 T = new tools::gcc::Preprocess(*this); break;
866 case Action::PrecompileJobClass:
867 T = new tools::gcc::Precompile(*this); break;
868 case Action::AnalyzeJobClass:
869 T = new tools::Clang(*this); break;
870 case Action::CompileJobClass:
871 T = new tools::gcc::Compile(*this); break;
872 case Action::AssembleJobClass:
873 T = new tools::gcc::Assemble(*this); break;
874 case Action::LinkJobClass:
875 T = new tools::gcc::Link(*this); break;
Mike Stump11289f42009-09-09 15:08:12 +0000876
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000877 // This is a bit ungeneric, but the only platform using a driver
878 // driver is Darwin.
879 case Action::LipoJobClass:
880 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar88299622010-06-04 18:28:36 +0000881 case Action::DsymutilJobClass:
882 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000883 }
884 }
885
886 return *T;
887}
888
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000889bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar03e0a4f2009-03-20 00:57:52 +0000890 // FIXME: Gross; we should probably have some separate target
891 // definition, possibly even reusing the one in clang.
Daniel Dunbar59e5e882009-03-20 00:20:03 +0000892 return getArchName() == "x86_64";
893}
894
895const char *Generic_GCC::GetDefaultRelocationModel() const {
896 return "static";
897}
898
899const char *Generic_GCC::GetForcedPicModel() const {
900 return 0;
901}
Daniel Dunbardac54a82009-03-25 04:13:45 +0000902
Chris Lattner09797542010-03-04 21:07:38 +0000903/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
904/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
905/// Currently does not support anything else but compilation.
906
907TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
908 : ToolChain(Host, Triple) {
909 // Path mangling to find libexec
910 std::string Path(getDriver().Dir);
911
912 Path += "/../libexec";
913 getProgramPaths().push_back(Path);
914}
915
916TCEToolChain::~TCEToolChain() {
917 for (llvm::DenseMap<unsigned, Tool*>::iterator
918 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
919 delete it->second;
920}
921
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000922bool TCEToolChain::IsMathErrnoDefault() const {
923 return true;
Chris Lattner09797542010-03-04 21:07:38 +0000924}
925
926bool TCEToolChain::IsUnwindTablesDefault() const {
927 return false;
928}
929
930const char *TCEToolChain::GetDefaultRelocationModel() const {
931 return "static";
932}
933
934const char *TCEToolChain::GetForcedPicModel() const {
935 return 0;
936}
937
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +0000938Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +0000939 const JobAction &JA,
940 const ActionList &Inputs) const {
Chris Lattner09797542010-03-04 21:07:38 +0000941 Action::ActionClass Key;
942 Key = Action::AnalyzeJobClass;
943
944 Tool *&T = Tools[Key];
945 if (!T) {
946 switch (Key) {
947 case Action::PreprocessJobClass:
948 T = new tools::gcc::Preprocess(*this); break;
949 case Action::AnalyzeJobClass:
950 T = new tools::Clang(*this); break;
951 default:
952 assert(false && "Unsupported action for TCE target.");
953 }
954 }
955 return *T;
956}
957
Daniel Dunbar10de9e62009-06-29 20:52:51 +0000958/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
959
960OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolaacc87092010-10-29 20:14:02 +0000961 : Generic_ELF(Host, Triple) {
Daniel Dunbar083edf72009-12-21 18:54:17 +0000962 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar10de9e62009-06-29 20:52:51 +0000963 getFilePaths().push_back("/usr/lib");
964}
965
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +0000966Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
967 const ActionList &Inputs) const {
Daniel Dunbar10de9e62009-06-29 20:52:51 +0000968 Action::ActionClass Key;
Daniel Dunbar083edf72009-12-21 18:54:17 +0000969 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar10de9e62009-06-29 20:52:51 +0000970 Key = Action::AnalyzeJobClass;
971 else
972 Key = JA.getKind();
973
Rafael Espindola96aef792010-11-07 23:13:01 +0000974 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
975 options::OPT_no_integrated_as,
976 IsIntegratedAssemblerDefault());
977
Daniel Dunbar10de9e62009-06-29 20:52:51 +0000978 Tool *&T = Tools[Key];
979 if (!T) {
980 switch (Key) {
Rafael Espindola96aef792010-11-07 23:13:01 +0000981 case Action::AssembleJobClass: {
982 if (UseIntegratedAs)
983 T = new tools::ClangAs(*this);
984 else
985 T = new tools::openbsd::Assemble(*this);
986 break;
987 }
Daniel Dunbar10de9e62009-06-29 20:52:51 +0000988 case Action::LinkJobClass:
989 T = new tools::openbsd::Link(*this); break;
990 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +0000991 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar10de9e62009-06-29 20:52:51 +0000992 }
993 }
994
995 return *T;
996}
997
Daniel Dunbare24297c2009-03-30 21:06:03 +0000998/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
999
Daniel Dunbara18a4872010-08-02 05:43:59 +00001000FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolaacc87092010-10-29 20:14:02 +00001001 : Generic_ELF(Host, Triple) {
Daniel Dunbara18a4872010-08-02 05:43:59 +00001002
1003 // Determine if we are compiling 32-bit code on an x86_64 platform.
1004 bool Lib32 = false;
1005 if (Triple.getArch() == llvm::Triple::x86 &&
1006 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1007 llvm::Triple::x86_64)
1008 Lib32 = true;
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001009
Roman Divacky00859c22011-06-04 07:37:31 +00001010 if (Triple.getArch() == llvm::Triple::ppc &&
1011 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1012 llvm::Triple::ppc64)
1013 Lib32 = true;
1014
Daniel Dunbarb48a4aa2009-04-02 18:30:04 +00001015 if (Lib32) {
Daniel Dunbarb48a4aa2009-04-02 18:30:04 +00001016 getFilePaths().push_back("/usr/lib32");
1017 } else {
Daniel Dunbarb48a4aa2009-04-02 18:30:04 +00001018 getFilePaths().push_back("/usr/lib");
1019 }
Daniel Dunbare24297c2009-03-30 21:06:03 +00001020}
1021
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001022Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1023 const ActionList &Inputs) const {
Daniel Dunbare24297c2009-03-30 21:06:03 +00001024 Action::ActionClass Key;
Daniel Dunbar083edf72009-12-21 18:54:17 +00001025 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbare24297c2009-03-30 21:06:03 +00001026 Key = Action::AnalyzeJobClass;
1027 else
1028 Key = JA.getKind();
1029
Roman Divacky137426a2010-11-08 17:46:39 +00001030 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1031 options::OPT_no_integrated_as,
1032 IsIntegratedAssemblerDefault());
1033
Daniel Dunbare24297c2009-03-30 21:06:03 +00001034 Tool *&T = Tools[Key];
1035 if (!T) {
1036 switch (Key) {
Daniel Dunbar8eb473c2009-03-31 17:45:15 +00001037 case Action::AssembleJobClass:
Roman Divacky137426a2010-11-08 17:46:39 +00001038 if (UseIntegratedAs)
1039 T = new tools::ClangAs(*this);
1040 else
1041 T = new tools::freebsd::Assemble(*this);
Roman Divackyb45d2672010-11-08 19:39:10 +00001042 break;
Daniel Dunbard854c8d2009-04-01 19:36:32 +00001043 case Action::LinkJobClass:
1044 T = new tools::freebsd::Link(*this); break;
Daniel Dunbare24297c2009-03-30 21:06:03 +00001045 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001046 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbare24297c2009-03-30 21:06:03 +00001047 }
1048 }
1049
1050 return *T;
1051}
Daniel Dunbarcc912342009-05-02 18:28:39 +00001052
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001053/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1054
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00001055NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
1056 const llvm::Triple& ToolTriple)
1057 : Generic_ELF(Host, Triple), ToolTriple(ToolTriple) {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001058
1059 // Determine if we are compiling 32-bit code on an x86_64 platform.
1060 bool Lib32 = false;
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00001061 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
1062 Triple.getArch() == llvm::Triple::x86)
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001063 Lib32 = true;
1064
Joerg Sonnenbergerbc923f32011-03-21 13:59:26 +00001065 if (getDriver().UseStdLib) {
1066 if (Lib32)
1067 getFilePaths().push_back("=/usr/lib/i386");
1068 else
1069 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001070 }
1071}
1072
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001073Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1074 const ActionList &Inputs) const {
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001075 Action::ActionClass Key;
1076 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1077 Key = Action::AnalyzeJobClass;
1078 else
1079 Key = JA.getKind();
1080
1081 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1082 options::OPT_no_integrated_as,
1083 IsIntegratedAssemblerDefault());
1084
1085 Tool *&T = Tools[Key];
1086 if (!T) {
1087 switch (Key) {
1088 case Action::AssembleJobClass:
1089 if (UseIntegratedAs)
1090 T = new tools::ClangAs(*this);
1091 else
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00001092 T = new tools::netbsd::Assemble(*this, ToolTriple);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001093 break;
1094 case Action::LinkJobClass:
Joerg Sonnenberger637603a2011-05-16 13:35:02 +00001095 T = new tools::netbsd::Link(*this, ToolTriple);
1096 break;
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001097 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001098 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00001099 }
1100 }
1101
1102 return *T;
1103}
1104
Chris Lattner3e2ee142010-07-07 16:01:42 +00001105/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1106
1107Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1108 : Generic_GCC(Host, Triple) {
1109 getFilePaths().push_back(getDriver().Dir + "/../lib");
1110 getFilePaths().push_back("/usr/lib");
1111 getFilePaths().push_back("/usr/gnu/lib");
1112 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1113}
1114
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001115Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1116 const ActionList &Inputs) const {
Chris Lattner3e2ee142010-07-07 16:01:42 +00001117 Action::ActionClass Key;
1118 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1119 Key = Action::AnalyzeJobClass;
1120 else
1121 Key = JA.getKind();
1122
1123 Tool *&T = Tools[Key];
1124 if (!T) {
1125 switch (Key) {
1126 case Action::AssembleJobClass:
1127 T = new tools::minix::Assemble(*this); break;
1128 case Action::LinkJobClass:
1129 T = new tools::minix::Link(*this); break;
1130 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001131 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner3e2ee142010-07-07 16:01:42 +00001132 }
1133 }
1134
1135 return *T;
1136}
1137
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001138/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1139
1140AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1141 : Generic_GCC(Host, Triple) {
1142
Daniel Dunbar88979912010-08-01 22:29:51 +00001143 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001144 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001145 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001146
Daniel Dunbar083edf72009-12-21 18:54:17 +00001147 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001148 getFilePaths().push_back("/usr/lib");
1149 getFilePaths().push_back("/usr/sfw/lib");
1150 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghand8712d92009-10-15 07:44:07 +00001151 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001152
1153}
1154
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001155Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1156 const ActionList &Inputs) const {
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001157 Action::ActionClass Key;
Daniel Dunbar083edf72009-12-21 18:54:17 +00001158 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001159 Key = Action::AnalyzeJobClass;
1160 else
1161 Key = JA.getKind();
1162
1163 Tool *&T = Tools[Key];
1164 if (!T) {
1165 switch (Key) {
1166 case Action::AssembleJobClass:
1167 T = new tools::auroraux::Assemble(*this); break;
1168 case Action::LinkJobClass:
1169 T = new tools::auroraux::Link(*this); break;
1170 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001171 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghan856e4ff2009-08-22 01:06:46 +00001172 }
1173 }
1174
1175 return *T;
1176}
1177
1178
Eli Friedman5cd659f2009-05-26 07:52:18 +00001179/// Linux toolchain (very bare-bones at the moment).
1180
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001181enum LinuxDistro {
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00001182 ArchLinux,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001183 DebianLenny,
1184 DebianSqueeze,
Eli Friedmanf7600942011-06-02 21:36:53 +00001185 DebianWheezy,
Rafael Espindola12479842010-11-11 02:07:13 +00001186 Exherbo,
Chris Lattner84e38552011-05-22 05:36:06 +00001187 RHEL4,
1188 RHEL5,
1189 RHEL6,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001190 Fedora13,
1191 Fedora14,
Eric Christopher534b6a02011-04-06 18:22:53 +00001192 Fedora15,
1193 FedoraRawhide,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001194 OpenSuse11_3,
David Chisnallb8f65e22011-05-19 13:26:33 +00001195 OpenSuse11_4,
1196 OpenSuse12_1,
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00001197 UbuntuHardy,
1198 UbuntuIntrepid,
Rafael Espindola66b291a2010-11-10 05:00:22 +00001199 UbuntuJaunty,
Zhongxing Xu14776cf2010-11-15 09:01:52 +00001200 UbuntuKarmic,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001201 UbuntuLucid,
1202 UbuntuMaverick,
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001203 UbuntuNatty,
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001204 UnknownDistro
1205};
1206
Chris Lattner84e38552011-05-22 05:36:06 +00001207static bool IsRedhat(enum LinuxDistro Distro) {
Eric Christopher534b6a02011-04-06 18:22:53 +00001208 return Distro == Fedora13 || Distro == Fedora14 ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00001209 Distro == Fedora15 || Distro == FedoraRawhide ||
1210 Distro == RHEL4 || Distro == RHEL5 || Distro == RHEL6;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001211}
1212
1213static bool IsOpenSuse(enum LinuxDistro Distro) {
David Chisnallb8f65e22011-05-19 13:26:33 +00001214 return Distro == OpenSuse11_3 || Distro == OpenSuse11_4 ||
1215 Distro == OpenSuse12_1;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001216}
1217
1218static bool IsDebian(enum LinuxDistro Distro) {
Eli Friedmanf7600942011-06-02 21:36:53 +00001219 return Distro == DebianLenny || Distro == DebianSqueeze ||
1220 Distro == DebianWheezy;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001221}
1222
1223static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00001224 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001225 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001226 Distro == UbuntuJaunty || Distro == UbuntuKarmic ||
1227 Distro == UbuntuNatty;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001228}
1229
1230static bool IsDebianBased(enum LinuxDistro Distro) {
1231 return IsDebian(Distro) || IsUbuntu(Distro);
1232}
1233
1234static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola12479842010-11-11 02:07:13 +00001235 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001236 bool Exists;
1237 if (Distro == Exherbo &&
1238 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola12479842010-11-11 02:07:13 +00001239 return false;
1240
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001241 return true;
Rafael Espindola12479842010-11-11 02:07:13 +00001242 }
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001243 if (Arch == llvm::Triple::ppc64)
1244 return true;
Eric Christopher55d49892011-06-03 13:28:31 +00001245 if ((Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) &&
1246 IsDebianBased(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001247 return true;
1248 return false;
1249}
1250
1251static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00001252 llvm::OwningPtr<llvm::MemoryBuffer> File;
1253 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
1254 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001255 llvm::SmallVector<llvm::StringRef, 8> Lines;
1256 Data.split(Lines, "\n");
1257 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
Douglas Gregor0a36f4d2011-03-14 15:39:50 +00001258 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1259 return UbuntuHardy;
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001260 else if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1261 return UbuntuIntrepid;
Rafael Espindola66b291a2010-11-10 05:00:22 +00001262 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00001263 return UbuntuJaunty;
Zhongxing Xu14776cf2010-11-15 09:01:52 +00001264 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1265 return UbuntuKarmic;
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001266 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1267 return UbuntuLucid;
1268 else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1269 return UbuntuMaverick;
1270 else if (Lines[i] == "DISTRIB_CODENAME=natty")
1271 return UbuntuNatty;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001272 }
1273 return UnknownDistro;
1274 }
1275
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00001276 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1277 llvm::StringRef Data = File.get()->getBuffer();
Eric Christopher534b6a02011-04-06 18:22:53 +00001278 if (Data.startswith("Fedora release 15"))
1279 return Fedora15;
1280 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001281 return Fedora14;
Eric Christopher534b6a02011-04-06 18:22:53 +00001282 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001283 return Fedora13;
Eric Christopher534b6a02011-04-06 18:22:53 +00001284 else if (Data.startswith("Fedora release") &&
1285 Data.find("Rawhide") != llvm::StringRef::npos)
1286 return FedoraRawhide;
Chris Lattner84e38552011-05-22 05:36:06 +00001287 else if (Data.startswith("Red Hat Enterprise Linux") &&
1288 Data.find("release 6") != llvm::StringRef::npos)
1289 return RHEL6;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00001290 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1291 Data.startswith("CentOS")) &&
Chris Lattner84e38552011-05-22 05:36:06 +00001292 Data.find("release 5") != llvm::StringRef::npos)
1293 return RHEL5;
Rafael Espindolad8f92c82011-06-03 15:23:24 +00001294 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1295 Data.startswith("CentOS")) &&
Chris Lattner84e38552011-05-22 05:36:06 +00001296 Data.find("release 4") != llvm::StringRef::npos)
1297 return RHEL4;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001298 return UnknownDistro;
1299 }
1300
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00001301 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1302 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001303 if (Data[0] == '5')
1304 return DebianLenny;
1305 else if (Data.startswith("squeeze/sid"))
1306 return DebianSqueeze;
Eli Friedmanf7600942011-06-02 21:36:53 +00001307 else if (Data.startswith("wheezy/sid"))
1308 return DebianWheezy;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001309 return UnknownDistro;
1310 }
1311
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00001312 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1313 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001314 if (Data.startswith("openSUSE 11.3"))
1315 return OpenSuse11_3;
David Chisnallb8f65e22011-05-19 13:26:33 +00001316 else if (Data.startswith("openSUSE 11.4"))
1317 return OpenSuse11_4;
1318 else if (Data.startswith("openSUSE 12.1"))
1319 return OpenSuse12_1;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001320 return UnknownDistro;
1321 }
1322
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001323 bool Exists;
1324 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola12479842010-11-11 02:07:13 +00001325 return Exherbo;
1326
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00001327 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1328 return ArchLinux;
1329
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001330 return UnknownDistro;
1331}
1332
Rafael Espindola0ddfbe22011-06-02 22:18:46 +00001333static std::string findGCCBaseLibDir(const std::string &GccTriple) {
1334 // FIXME: Using CXX_INCLUDE_ROOT is here is a bit of a hack, but
1335 // avoids adding yet another option to configure/cmake.
1336 // It would probably be cleaner to break it in two variables
1337 // CXX_GCC_ROOT with just /foo/bar
1338 // CXX_GCC_VER with 4.5.2
1339 // Then we would have
1340 // CXX_INCLUDE_ROOT = CXX_GCC_ROOT/include/c++/CXX_GCC_VER
1341 // and this function would return
1342 // CXX_GCC_ROOT/lib/gcc/CXX_INCLUDE_ARCH/CXX_GCC_VER
1343 llvm::SmallString<128> CxxIncludeRoot(CXX_INCLUDE_ROOT);
1344 if (CxxIncludeRoot != "") {
1345 // This is of the form /foo/bar/include/c++/4.5.2/
1346 if (CxxIncludeRoot.back() == '/')
1347 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the /
1348 llvm::StringRef Version = llvm::sys::path::filename(CxxIncludeRoot);
1349 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the version
1350 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the c++
1351 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the include
1352 std::string ret(CxxIncludeRoot.c_str());
1353 ret.append("/lib/gcc/");
1354 ret.append(CXX_INCLUDE_ARCH);
1355 ret.append("/");
1356 ret.append(Version);
1357 return ret;
1358 }
1359 static const char* GccVersions[] = {"4.6.0", "4.6",
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001360 "4.5.2", "4.5.1", "4.5",
1361 "4.4.5", "4.4.4", "4.4.3", "4.4",
1362 "4.3.4", "4.3.3", "4.3.2", "4.3",
1363 "4.2.4", "4.2.3", "4.2.2", "4.2.1",
1364 "4.2", "4.1.1"};
Rafael Espindola0ddfbe22011-06-02 22:18:46 +00001365 bool Exists;
1366 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1367 std::string Suffix = GccTriple + "/" + GccVersions[i];
1368 std::string t1 = "/usr/lib/gcc/" + Suffix;
1369 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists)
1370 return t1;
1371 std::string t2 = "/usr/lib64/gcc/" + Suffix;
1372 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists)
1373 return t2;
1374 std::string t3 = "/usr/lib/" + GccTriple + "/gcc/" + Suffix;
1375 if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists)
1376 return t3;
1377 }
1378 return "";
1379}
1380
Nick Lewycky6da90772010-12-31 17:31:54 +00001381Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolaacc87092010-10-29 20:14:02 +00001382 : Generic_ELF(Host, Triple) {
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001383 llvm::Triple::ArchType Arch =
1384 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Daniel Dunbarb46b5bb2009-08-06 01:47:11 +00001385
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001386 std::string Suffix32 = "";
1387 if (Arch == llvm::Triple::x86_64)
1388 Suffix32 = "/32";
Daniel Dunbarb46b5bb2009-08-06 01:47:11 +00001389
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001390 std::string Suffix64 = "";
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001391 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001392 Suffix64 = "/64";
1393
1394 std::string Lib32 = "lib";
1395
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001396 bool Exists;
1397 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001398 Lib32 = "lib32";
1399
1400 std::string Lib64 = "lib";
Michael J. Spencer634f13e2011-01-12 23:54:48 +00001401 bool Symlink;
Chris Lattner8cf302a2011-01-13 01:35:58 +00001402 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1403 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001404 Lib64 = "lib64";
1405
1406 std::string GccTriple = "";
Douglas Gregord9bb1522011-03-06 19:11:49 +00001407 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001408 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1409 Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001410 GccTriple = "arm-linux-gnueabi";
1411 } else if (Arch == llvm::Triple::x86_64) {
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001412 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1413 Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001414 GccTriple = "x86_64-linux-gnu";
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001415 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1416 Exists) && Exists)
Rafael Espindola2abb1622010-11-17 00:25:26 +00001417 GccTriple = "x86_64-unknown-linux-gnu";
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001418 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1419 Exists) && Exists)
Rafael Espindola12479842010-11-11 02:07:13 +00001420 GccTriple = "x86_64-pc-linux-gnu";
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001421 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1422 Exists) && Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001423 GccTriple = "x86_64-redhat-linux";
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001424 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1425 Exists) && Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001426 GccTriple = "x86_64-suse-linux";
Nick Lewycky22197c12011-02-01 23:03:29 +00001427 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1428 Exists) && Exists)
1429 GccTriple = "x86_64-manbo-linux-gnu";
Nico Weberadf8ba92011-04-25 03:17:35 +00001430 else if (!llvm::sys::fs::exists("/usr/lib/x86_64-linux-gnu/gcc",
1431 Exists) && Exists)
1432 GccTriple = "x86_64-linux-gnu";
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001433 } else if (Arch == llvm::Triple::x86) {
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001434 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001435 GccTriple = "i686-linux-gnu";
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001436 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1437 Exists)
Nuno Lopeseb156602010-11-19 17:26:57 +00001438 GccTriple = "i686-pc-linux-gnu";
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001439 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1440 Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001441 GccTriple = "i486-linux-gnu";
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001442 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1443 Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001444 GccTriple = "i686-redhat-linux";
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001445 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1446 Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001447 GccTriple = "i586-suse-linux";
Ted Kremenek00abe8e2011-04-18 17:50:19 +00001448 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-slackware-linux", Exists)
1449 && Exists)
1450 GccTriple = "i486-slackware-linux";
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001451 } else if (Arch == llvm::Triple::ppc) {
1452 if (!llvm::sys::fs::exists("/usr/lib/powerpc-linux-gnu", Exists) && Exists)
1453 GccTriple = "powerpc-linux-gnu";
Eric Christopher55d49892011-06-03 13:28:31 +00001454 else if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc-unknown-linux-gnu",
1455 Exists) && Exists)
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001456 GccTriple = "powerpc-unknown-linux-gnu";
1457 } else if (Arch == llvm::Triple::ppc64) {
Eric Christopher55d49892011-06-03 13:28:31 +00001458 if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc64-unknown-linux-gnu",
1459 Exists) && Exists)
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001460 GccTriple = "powerpc64-unknown-linux-gnu";
Eric Christopher55d49892011-06-03 13:28:31 +00001461 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/"
1462 "powerpc64-unknown-linux-gnu", Exists) &&
1463 Exists)
Ted Kremenek43d47cc2011-04-05 22:04:27 +00001464 GccTriple = "powerpc64-unknown-linux-gnu";
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001465 }
1466
Rafael Espindola0ddfbe22011-06-02 22:18:46 +00001467 std::string Base = findGCCBaseLibDir(GccTriple);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001468 path_list &Paths = getFilePaths();
Eric Christopher55d49892011-06-03 13:28:31 +00001469 bool Is32Bits = (getArch() == llvm::Triple::x86 ||
1470 getArch() == llvm::Triple::ppc);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001471
1472 std::string Suffix;
1473 std::string Lib;
1474
1475 if (Is32Bits) {
1476 Suffix = Suffix32;
1477 Lib = Lib32;
1478 } else {
1479 Suffix = Suffix64;
1480 Lib = Lib64;
1481 }
1482
1483 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
Michael J. Spencerf6efe582011-01-10 02:34:13 +00001484 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001485 Linker = LinkerPath.str();
1486 else
1487 Linker = GetProgramPath("ld");
1488
1489 LinuxDistro Distro = DetectLinuxDistro(Arch);
1490
Chris Lattnerd075c822011-05-22 16:45:07 +00001491 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindolac5688622010-11-08 14:48:47 +00001492 ExtraOpts.push_back("-z");
1493 ExtraOpts.push_back("relro");
1494 }
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001495
Douglas Gregord9bb1522011-03-06 19:11:49 +00001496 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001497 ExtraOpts.push_back("-X");
1498
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001499 if (IsRedhat(Distro) || IsOpenSuse(Distro) || Distro == UbuntuMaverick ||
Chris Lattnerd075c822011-05-22 16:45:07 +00001500 Distro == UbuntuNatty)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001501 ExtraOpts.push_back("--hash-style=gnu");
1502
NAKAMURA Takumi8b73b3e2011-06-03 03:49:51 +00001503 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
Chris Lattnerd075c822011-05-22 16:45:07 +00001504 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001505 ExtraOpts.push_back("--hash-style=both");
1506
Chris Lattner84e38552011-05-22 05:36:06 +00001507 if (IsRedhat(Distro))
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001508 ExtraOpts.push_back("--no-add-needed");
1509
Eli Friedmanf7600942011-06-02 21:36:53 +00001510 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindolad8f92c82011-06-03 15:23:24 +00001511 IsOpenSuse(Distro) ||
1512 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
1513 Distro == UbuntuLucid ||
Eli Friedmanf7600942011-06-02 21:36:53 +00001514 Distro == UbuntuMaverick || Distro == UbuntuKarmic ||
1515 Distro == UbuntuNatty)
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001516 ExtraOpts.push_back("--build-id");
1517
Chris Lattnerd075c822011-05-22 16:45:07 +00001518 if (IsOpenSuse(Distro))
Chandler Carruthe5d9d902011-05-24 07:51:17 +00001519 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattnerd075c822011-05-22 16:45:07 +00001520
Chandler Carruth6a4e8e32011-02-25 06:39:53 +00001521 if (Distro == ArchLinux)
1522 Lib = "lib";
1523
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001524 Paths.push_back(Base + Suffix);
1525 if (HasMultilib(Arch, Distro)) {
1526 if (IsOpenSuse(Distro) && Is32Bits)
1527 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1528 Paths.push_back(Base + "/../../../../" + Lib);
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001529 }
Rafael Espindola30490212011-06-03 15:39:42 +00001530
1531 // FIXME: This is in here to find crt1.o. It is provided by libc, and
1532 // libc (like gcc), can be installed in any directory. Once we are
1533 // fetching this from a config file, we should have a libc prefix.
1534 Paths.push_back("/lib/../" + Lib);
1535 Paths.push_back("/usr/lib/../" + Lib);
1536
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001537 if (!Suffix.empty())
1538 Paths.push_back(Base);
1539 if (IsOpenSuse(Distro))
1540 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1541 Paths.push_back(Base + "/../../..");
1542 if (Arch == getArch() && IsUbuntu(Distro))
1543 Paths.push_back("/usr/lib/" + GccTriple);
1544}
1545
1546bool Linux::HasNativeLLVMSupport() const {
1547 return true;
Eli Friedman5cd659f2009-05-26 07:52:18 +00001548}
1549
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001550Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
1551 const ActionList &Inputs) const {
Rafael Espindola92b00932010-08-10 00:25:48 +00001552 Action::ActionClass Key;
1553 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1554 Key = Action::AnalyzeJobClass;
1555 else
1556 Key = JA.getKind();
1557
Rafael Espindola96aef792010-11-07 23:13:01 +00001558 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1559 options::OPT_no_integrated_as,
1560 IsIntegratedAssemblerDefault());
1561
Rafael Espindola92b00932010-08-10 00:25:48 +00001562 Tool *&T = Tools[Key];
1563 if (!T) {
1564 switch (Key) {
1565 case Action::AssembleJobClass:
Rafael Espindola96aef792010-11-07 23:13:01 +00001566 if (UseIntegratedAs)
1567 T = new tools::ClangAs(*this);
1568 else
1569 T = new tools::linuxtools::Assemble(*this);
1570 break;
Rafael Espindolac8f008f2010-11-07 20:14:31 +00001571 case Action::LinkJobClass:
1572 T = new tools::linuxtools::Link(*this); break;
Rafael Espindola92b00932010-08-10 00:25:48 +00001573 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001574 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindola92b00932010-08-10 00:25:48 +00001575 }
1576 }
1577
1578 return *T;
1579}
1580
Daniel Dunbarcc912342009-05-02 18:28:39 +00001581/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1582
Daniel Dunbar51c7f972009-05-22 02:53:45 +00001583DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolaacc87092010-10-29 20:14:02 +00001584 : Generic_ELF(Host, Triple) {
Daniel Dunbarcc912342009-05-02 18:28:39 +00001585
1586 // Path mangling to find libexec
Daniel Dunbar88979912010-08-01 22:29:51 +00001587 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer51477bd2011-03-01 22:50:47 +00001588 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbar88979912010-08-01 22:29:51 +00001589 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarcc912342009-05-02 18:28:39 +00001590
Daniel Dunbar083edf72009-12-21 18:54:17 +00001591 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarcc912342009-05-02 18:28:39 +00001592 getFilePaths().push_back("/usr/lib");
1593 getFilePaths().push_back("/usr/lib/gcc41");
1594}
1595
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001596Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
1597 const ActionList &Inputs) const {
Daniel Dunbarcc912342009-05-02 18:28:39 +00001598 Action::ActionClass Key;
Daniel Dunbar083edf72009-12-21 18:54:17 +00001599 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarcc912342009-05-02 18:28:39 +00001600 Key = Action::AnalyzeJobClass;
1601 else
1602 Key = JA.getKind();
1603
1604 Tool *&T = Tools[Key];
1605 if (!T) {
1606 switch (Key) {
1607 case Action::AssembleJobClass:
1608 T = new tools::dragonfly::Assemble(*this); break;
1609 case Action::LinkJobClass:
1610 T = new tools::dragonfly::Link(*this); break;
1611 default:
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001612 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarcc912342009-05-02 18:28:39 +00001613 }
1614 }
1615
1616 return *T;
1617}
Michael J. Spencerb186bc32010-08-21 21:55:07 +00001618
1619Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1620 : ToolChain(Host, Triple) {
1621}
1622
Daniel Dunbar1e1c3ca2011-03-18 20:14:00 +00001623Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA,
1624 const ActionList &Inputs) const {
Michael J. Spencerb186bc32010-08-21 21:55:07 +00001625 Action::ActionClass Key;
1626 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1627 Key = Action::AnalyzeJobClass;
1628 else
1629 Key = JA.getKind();
1630
1631 Tool *&T = Tools[Key];
1632 if (!T) {
1633 switch (Key) {
1634 case Action::InputClass:
1635 case Action::BindArchClass:
Chandler Carruth6f33bd92010-08-22 06:56:37 +00001636 case Action::LipoJobClass:
1637 case Action::DsymutilJobClass:
Michael J. Spencerb186bc32010-08-21 21:55:07 +00001638 assert(0 && "Invalid tool kind.");
1639 case Action::PreprocessJobClass:
1640 case Action::PrecompileJobClass:
1641 case Action::AnalyzeJobClass:
1642 case Action::CompileJobClass:
1643 T = new tools::Clang(*this); break;
1644 case Action::AssembleJobClass:
1645 T = new tools::ClangAs(*this); break;
1646 case Action::LinkJobClass:
1647 T = new tools::visualstudio::Link(*this); break;
1648 }
1649 }
1650
1651 return *T;
1652}
1653
1654bool Windows::IsIntegratedAssemblerDefault() const {
1655 return true;
1656}
1657
1658bool Windows::IsUnwindTablesDefault() const {
1659 // FIXME: Gross; we should probably have some separate target
1660 // definition, possibly even reusing the one in clang.
1661 return getArchName() == "x86_64";
1662}
1663
1664const char *Windows::GetDefaultRelocationModel() const {
1665 return "static";
1666}
1667
1668const char *Windows::GetForcedPicModel() const {
1669 if (getArchName() == "x86_64")
1670 return "pic";
1671 return 0;
1672}