blob: 4cd96beb05e2de5bdc1df1227f05f8a7e2deedd9 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
Daniel Dunbar39176082009-03-20 00:20:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
11
Rafael Espindola14ea13c2011-06-02 22:18:46 +000012#ifdef HAVE_CLANG_CONFIG_H
13# include "clang/Config/config.h"
14#endif
15
Daniel Dunbarf3cad362009-03-25 04:13:45 +000016#include "clang/Driver/Arg.h"
17#include "clang/Driver/ArgList.h"
Daniel Dunbar0f602de2010-05-20 21:48:38 +000018#include "clang/Driver/Compilation.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000019#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000020#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000021#include "clang/Driver/HostInfo.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000022#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000023#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000024#include "clang/Driver/Options.h"
Douglas Gregor34916db2010-09-03 17:16:03 +000025#include "clang/Basic/Version.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000026
Daniel Dunbar00577ad2010-08-23 22:35:37 +000027#include "llvm/ADT/SmallString.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000028#include "llvm/ADT/StringExtras.h"
John McCallf85e1932011-06-15 23:02:42 +000029#include "llvm/ADT/STLExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000030#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000031#include "llvm/Support/FileSystem.h"
Rafael Espindolac1da9812010-11-07 20:14:31 +000032#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000033#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000034#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000035#include "llvm/Support/system_error.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000036
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000037#include <cstdlib> // ::getenv
38
Daniel Dunbar39176082009-03-20 00:20:03 +000039using namespace clang::driver;
40using namespace clang::driver::toolchains;
41
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
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000044Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple)
John McCallf85e1932011-06-15 23:02:42 +000045 : ToolChain(Host, Triple), TargetInitialized(false),
46 ARCRuntimeForSimulator(ARCSimulator_None)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000047{
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000048 // Compute the initial Darwin version based on the host.
49 bool HadExtra;
50 std::string OSName = Triple.getOSName();
Daniel Dunbar34f9e292011-02-25 21:20:15 +000051 if (!Driver::GetReleaseVersion(&OSName.c_str()[6],
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000052 DarwinVersion[0], DarwinVersion[1],
53 DarwinVersion[2], HadExtra))
54 getDriver().Diag(clang::diag::err_drv_invalid_darwin_version) << OSName;
55
Daniel Dunbar02633b52009-03-26 16:23:12 +000056 llvm::raw_string_ostream(MacosxVersionMin)
Daniel Dunbar25b58eb2010-08-02 05:44:07 +000057 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
58 << DarwinVersion[1];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000059}
60
Daniel Dunbar41800112010-08-02 05:43:56 +000061types::ID Darwin::LookupTypeForExtension(const char *Ext) const {
62 types::ID Ty = types::lookupTypeForExtension(Ext);
63
64 // Darwin always preprocesses assembly files (unless -x is used explicitly).
65 if (Ty == types::TY_PP_Asm)
66 return types::TY_Asm;
67
68 return Ty;
69}
70
Daniel Dunbarb993f5d2010-09-17 00:24:52 +000071bool Darwin::HasNativeLLVMSupport() const {
72 return true;
73}
74
John McCallf85e1932011-06-15 23:02:42 +000075/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
76bool Darwin::HasARCRuntime() const {
77 // FIXME: Remove this once there is a proper way to detect an ARC runtime
78 // for the simulator.
79 switch (ARCRuntimeForSimulator) {
80 case ARCSimulator_None:
81 break;
82 case ARCSimulator_HasARCRuntime:
83 return true;
84 case ARCSimulator_NoARCRuntime:
85 return false;
86 }
87
88 if (isTargetIPhoneOS())
89 return !isIPhoneOSVersionLT(5);
90 else
91 return !isMacosxVersionLT(10, 7);
92}
93
Daniel Dunbareeff4062010-01-22 02:04:58 +000094// FIXME: Can we tablegen this?
95static const char *GetArmArchForMArch(llvm::StringRef Value) {
96 if (Value == "armv6k")
97 return "armv6";
98
99 if (Value == "armv5tej")
100 return "armv5";
101
102 if (Value == "xscale")
103 return "xscale";
104
105 if (Value == "armv4t")
106 return "armv4t";
107
108 if (Value == "armv7" || Value == "armv7-a" || Value == "armv7-r" ||
109 Value == "armv7-m" || Value == "armv7a" || Value == "armv7r" ||
110 Value == "armv7m")
111 return "armv7";
112
113 return 0;
114}
115
116// FIXME: Can we tablegen this?
117static const char *GetArmArchForMCpu(llvm::StringRef Value) {
118 if (Value == "arm10tdmi" || Value == "arm1020t" || Value == "arm9e" ||
119 Value == "arm946e-s" || Value == "arm966e-s" ||
120 Value == "arm968e-s" || Value == "arm10e" ||
121 Value == "arm1020e" || Value == "arm1022e" || Value == "arm926ej-s" ||
122 Value == "arm1026ej-s")
123 return "armv5";
124
125 if (Value == "xscale")
126 return "xscale";
127
128 if (Value == "arm1136j-s" || Value == "arm1136jf-s" ||
Bob Wilson1ec0ade2011-03-21 20:40:05 +0000129 Value == "arm1176jz-s" || Value == "arm1176jzf-s" ||
130 Value == "cortex-m0" )
Daniel Dunbareeff4062010-01-22 02:04:58 +0000131 return "armv6";
132
133 if (Value == "cortex-a8" || Value == "cortex-r4" || Value == "cortex-m3")
134 return "armv7";
135
136 return 0;
137}
138
139llvm::StringRef Darwin::getDarwinArchName(const ArgList &Args) const {
140 switch (getTriple().getArch()) {
141 default:
142 return getArchName();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000143
Douglas Gregorf0594d82011-03-06 19:11:49 +0000144 case llvm::Triple::thumb:
Daniel Dunbareeff4062010-01-22 02:04:58 +0000145 case llvm::Triple::arm: {
146 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
147 if (const char *Arch = GetArmArchForMArch(A->getValue(Args)))
148 return Arch;
149
150 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
151 if (const char *Arch = GetArmArchForMCpu(A->getValue(Args)))
152 return Arch;
153
154 return "arm";
155 }
156 }
157}
158
Daniel Dunbarf3955282009-09-04 18:34:51 +0000159Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000160 // Free tool implementations.
161 for (llvm::DenseMap<unsigned, Tool*>::iterator
162 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
163 delete it->second;
164}
165
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000166std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args) const {
167 llvm::Triple Triple(ComputeLLVMTriple(Args));
168
169 // If the target isn't initialized (e.g., an unknown Darwin platform, return
170 // the default triple).
171 if (!isTargetInitialized())
172 return Triple.getTriple();
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000173
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000174 unsigned Version[3];
175 getTargetVersion(Version);
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000176
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000177 llvm::SmallString<16> Str;
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000178 llvm::raw_svector_ostream(Str)
Daniel Dunbar659d23a2011-04-19 23:34:17 +0000179 << (isTargetIPhoneOS() ? "ios" : "macosx")
Daniel Dunbar729f38e2011-04-19 21:45:47 +0000180 << Version[0] << "." << Version[1] << "." << Version[2];
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000181 Triple.setOSName(Str.str());
182
183 return Triple.getTriple();
184}
185
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000186Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA,
187 const ActionList &Inputs) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000188 Action::ActionClass Key;
Daniel Dunbar5ce872f2011-03-18 20:14:03 +0000189
190 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple())) {
191 // Fallback to llvm-gcc for i386 kext compiles, we don't support that ABI.
192 if (Inputs.size() == 1 &&
193 types::isCXX(Inputs[0]->getType()) &&
194 getTriple().getOS() == llvm::Triple::Darwin &&
195 getTriple().getArch() == llvm::Triple::x86 &&
196 C.getArgs().getLastArg(options::OPT_fapple_kext))
197 Key = JA.getKind();
198 else
199 Key = Action::AnalyzeJobClass;
200 } else
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000201 Key = JA.getKind();
202
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000203 // FIXME: This doesn't belong here, but ideally we will support static soon
204 // anyway.
205 bool HasStatic = (C.getArgs().hasArg(options::OPT_mkernel) ||
206 C.getArgs().hasArg(options::OPT_static) ||
207 C.getArgs().hasArg(options::OPT_fapple_kext));
208 bool IsIADefault = IsIntegratedAssemblerDefault() && !HasStatic;
209 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
210 options::OPT_no_integrated_as,
211 IsIADefault);
212
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000213 Tool *&T = Tools[Key];
214 if (!T) {
215 switch (Key) {
216 case Action::InputClass:
217 case Action::BindArchClass:
218 assert(0 && "Invalid tool kind.");
219 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000220 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000221 case Action::AnalyzeJobClass:
222 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000223 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000224 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000225 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar0f602de2010-05-20 21:48:38 +0000226 case Action::AssembleJobClass: {
227 if (UseIntegratedAs)
228 T = new tools::ClangAs(*this);
229 else
230 T = new tools::darwin::Assemble(*this);
231 break;
232 }
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000233 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000234 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000235 case Action::LipoJobClass:
236 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000237 case Action::DsymutilJobClass:
238 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000239 }
240 }
241
242 return *T;
243}
244
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000245
Daniel Dunbar25b58eb2010-08-02 05:44:07 +0000246DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple)
247 : Darwin(Host, Triple)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000248{
Daniel Dunbar47023092011-03-18 19:25:15 +0000249 std::string UsrPrefix = "llvm-gcc-4.2/";
250
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000251 getProgramPaths().push_back(getDriver().getInstalledDir());
252 if (getDriver().getInstalledDir() != getDriver().Dir)
253 getProgramPaths().push_back(getDriver().Dir);
254
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000255 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000256 getProgramPaths().push_back(getDriver().getInstalledDir());
257 if (getDriver().getInstalledDir() != getDriver().Dir)
258 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000259
260 // For fallback, we need to know how to find the GCC cc1 executables, so we
Daniel Dunbar47023092011-03-18 19:25:15 +0000261 // also add the GCC libexec paths. This is legacy code that can be removed
262 // once fallback is no longer useful.
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000263 std::string ToolChainDir = "i686-apple-darwin";
264 ToolChainDir += llvm::utostr(DarwinVersion[0]);
265 ToolChainDir += "/4.2.1";
266
267 std::string Path = getDriver().Dir;
Daniel Dunbar47023092011-03-18 19:25:15 +0000268 Path += "/../" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000269 Path += ToolChainDir;
270 getProgramPaths().push_back(Path);
271
Daniel Dunbar47023092011-03-18 19:25:15 +0000272 Path = "/usr/" + UsrPrefix + "libexec/gcc/";
Daniel Dunbar0e50ee42010-09-17 08:22:12 +0000273 Path += ToolChainDir;
274 getProgramPaths().push_back(Path);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000275}
276
277void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
278 ArgStringList &CmdArgs) const {
279 // The Clang toolchain uses explicit paths for internal libraries.
Daniel Dunbar424b6612010-06-30 23:56:13 +0000280
281 // Unfortunately, we still might depend on a few of the libraries that are
282 // only available in the gcc library directory (in particular
283 // libstdc++.dylib). For now, hardcode the path to the known install location.
284 llvm::sys::Path P(getDriver().Dir);
285 P.eraseComponent(); // .../usr/bin -> ../usr
286 P.appendComponent("lib");
287 P.appendComponent("gcc");
288 switch (getTriple().getArch()) {
289 default:
290 assert(0 && "Invalid Darwin arch!");
291 case llvm::Triple::x86:
292 case llvm::Triple::x86_64:
293 P.appendComponent("i686-apple-darwin10");
294 break;
295 case llvm::Triple::arm:
296 case llvm::Triple::thumb:
297 P.appendComponent("arm-apple-darwin10");
298 break;
299 case llvm::Triple::ppc:
300 case llvm::Triple::ppc64:
301 P.appendComponent("powerpc-apple-darwin10");
302 break;
303 }
304 P.appendComponent("4.2.1");
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000305
306 // Determine the arch specific GCC subdirectory.
307 const char *ArchSpecificDir = 0;
308 switch (getTriple().getArch()) {
309 default:
310 break;
311 case llvm::Triple::arm:
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000312 case llvm::Triple::thumb: {
313 std::string Triple = ComputeLLVMTriple(Args);
314 llvm::StringRef TripleStr = Triple;
315 if (TripleStr.startswith("armv5") || TripleStr.startswith("thumbv5"))
316 ArchSpecificDir = "v5";
317 else if (TripleStr.startswith("armv6") || TripleStr.startswith("thumbv6"))
318 ArchSpecificDir = "v6";
319 else if (TripleStr.startswith("armv7") || TripleStr.startswith("thumbv7"))
320 ArchSpecificDir = "v7";
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000321 break;
Daniel Dunbar3a0e3922010-08-26 00:55:52 +0000322 }
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000323 case llvm::Triple::ppc64:
324 ArchSpecificDir = "ppc64";
325 break;
326 case llvm::Triple::x86_64:
327 ArchSpecificDir = "x86_64";
328 break;
329 }
330
331 if (ArchSpecificDir) {
332 P.appendComponent(ArchSpecificDir);
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000333 bool Exists;
334 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbareab3bc42010-08-23 20:58:52 +0000335 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
336 P.eraseComponent();
337 }
338
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000339 bool Exists;
340 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Daniel Dunbar424b6612010-06-30 23:56:13 +0000341 CmdArgs.push_back(Args.MakeArgString("-L" + P.str()));
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000342}
343
John McCallf85e1932011-06-15 23:02:42 +0000344void DarwinClang::AddLinkARCArgs(const ArgList &Args,
345 ArgStringList &CmdArgs) const {
346
347 CmdArgs.push_back("-force_load");
348 llvm::sys::Path P(getDriver().ClangExecutable);
349 P.eraseComponent(); // 'clang'
350 P.eraseComponent(); // 'bin'
351 P.appendComponent("lib");
352 P.appendComponent("arc");
353 P.appendComponent("libarclite_");
354 std::string s = P.str();
355 // Mash in the platform.
356 if (isTargetIPhoneOS())
357 s += "iphoneos";
358 // FIXME: isTargetIphoneOSSimulator() is not returning true.
359 else if (ARCRuntimeForSimulator != ARCSimulator_None)
360 s += "iphonesimulator";
361 else
362 s += "macosx";
363 s += ".a";
364
365 CmdArgs.push_back(Args.MakeArgString(s));
366}
367
Eric Christopher3404fe72011-06-22 17:41:40 +0000368void DarwinClang::AddLinkRuntimeLib(const ArgList &Args,
369 ArgStringList &CmdArgs,
370 const char *DarwinStaticLib) const {
371 llvm::sys::Path P(getDriver().ResourceDir);
372 P.appendComponent("lib");
373 P.appendComponent("darwin");
374 P.appendComponent(DarwinStaticLib);
375
376 // For now, allow missing resource libraries to support developers who may
377 // not have compiler-rt checked out or integrated into their build.
378 bool Exists;
379 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
380 CmdArgs.push_back(Args.MakeArgString(P.str()));
381}
382
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000383void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
384 ArgStringList &CmdArgs) const {
Daniel Dunbareec99102010-01-22 03:38:14 +0000385 // Darwin doesn't support real static executables, don't link any runtime
386 // libraries with -static.
387 if (Args.hasArg(options::OPT_static))
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000388 return;
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000389
390 // Reject -static-libgcc for now, we can deal with this when and if someone
391 // cares. This is useful in situations where someone wants to statically link
392 // something like libstdc++, and needs its runtime support routines.
393 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000394 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000395 << A->getAsString(Args);
396 return;
397 }
398
Daniel Dunbareec99102010-01-22 03:38:14 +0000399 // Otherwise link libSystem, then the dynamic runtime library, and finally any
400 // target specific static runtime library.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000401 CmdArgs.push_back("-lSystem");
Daniel Dunbareec99102010-01-22 03:38:14 +0000402
403 // Select the dynamic runtime library and the target specific static library.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +0000404 if (isTargetIPhoneOS()) {
Daniel Dunbar87e945f2011-04-30 04:25:16 +0000405 // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
406 // it never went into the SDK.
407 if (!isTargetIOSSimulator())
408 CmdArgs.push_back("-lgcc_s.1");
Daniel Dunbareec99102010-01-22 03:38:14 +0000409
Daniel Dunbar3cceec52011-04-18 23:48:36 +0000410 // We currently always need a static runtime library for iOS.
Eric Christopher3404fe72011-06-22 17:41:40 +0000411 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
Daniel Dunbareec99102010-01-22 03:38:14 +0000412 } else {
Daniel Dunbareec99102010-01-22 03:38:14 +0000413 // The dynamic runtime library was merged with libSystem for 10.6 and
414 // beyond; only 10.4 and 10.5 need an additional runtime library.
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000415 if (isMacosxVersionLT(10, 5))
Daniel Dunbareec99102010-01-22 03:38:14 +0000416 CmdArgs.push_back("-lgcc_s.10.4");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +0000417 else if (isMacosxVersionLT(10, 6))
Daniel Dunbareec99102010-01-22 03:38:14 +0000418 CmdArgs.push_back("-lgcc_s.10.5");
419
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000420 // For OS X, we thought we would only need a static runtime library when
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000421 // targeting 10.4, to provide versions of the static functions which were
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000422 // omitted from 10.4.dylib.
423 //
424 // Unfortunately, that turned out to not be true, because Darwin system
425 // headers can still use eprintf on i386, and it is not exported from
426 // libSystem. Therefore, we still must provide a runtime library just for
427 // the tiny tiny handful of projects that *might* use that symbol.
428 if (isMacosxVersionLT(10, 5)) {
Eric Christopher3404fe72011-06-22 17:41:40 +0000429 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000430 } else {
431 if (getTriple().getArch() == llvm::Triple::x86)
Eric Christopher3404fe72011-06-22 17:41:40 +0000432 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.eprintf.a");
433 AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
Daniel Dunbar885b1db2010-09-22 00:03:52 +0000434 }
Daniel Dunbareec99102010-01-22 03:38:14 +0000435 }
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000436}
437
John McCallf85e1932011-06-15 23:02:42 +0000438static inline llvm::StringRef SimulatorVersionDefineName() {
439 return "__IPHONE_OS_VERSION_MIN_REQUIRED";
440}
441
442/// \brief Parse the simulator version define:
443/// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9])
444// and return the grouped values as integers, e.g:
445// __IPHONE_OS_VERSION_MIN_REQUIRED=40201
446// will return Major=4, Minor=2, Micro=1.
447static bool GetVersionFromSimulatorDefine(llvm::StringRef define,
448 unsigned &Major, unsigned &Minor,
449 unsigned &Micro) {
450 assert(define.startswith(SimulatorVersionDefineName()));
451 llvm::StringRef name, version;
452 llvm::tie(name, version) = define.split('=');
453 if (version.empty())
454 return false;
455 std::string verstr = version.str();
456 char *end;
457 unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10);
458 if (*end != '\0')
459 return false;
460 Major = num / 10000;
461 num = num % 10000;
462 Minor = num / 100;
463 Micro = num % 100;
464 return true;
465}
466
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000467void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000468 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000469
Daniel Dunbar26031372010-01-27 00:56:25 +0000470 Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000471 Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ);
472 Arg *iOSSimVersion = Args.getLastArg(
473 options::OPT_mios_simulator_version_min_EQ);
John McCallf85e1932011-06-15 23:02:42 +0000474
475 // FIXME: HACK! When compiling for the simulator we don't get a
476 // '-miphoneos-version-min' to help us know whether there is an ARC runtime
477 // or not; try to parse a __IPHONE_OS_VERSION_MIN_REQUIRED
478 // define passed in command-line.
479 if (!iOSVersion) {
480 for (arg_iterator it = Args.filtered_begin(options::OPT_D),
481 ie = Args.filtered_end(); it != ie; ++it) {
482 llvm::StringRef define = (*it)->getValue(Args);
483 if (define.startswith(SimulatorVersionDefineName())) {
484 unsigned Major, Minor, Micro;
485 if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
486 Major < 10 && Minor < 100 && Micro < 100) {
487 ARCRuntimeForSimulator = Major < 5 ? ARCSimulator_NoARCRuntime
488 : ARCSimulator_HasARCRuntime;
489 }
490 break;
491 }
492 }
493 }
494
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000495 if (OSXVersion && (iOSVersion || iOSSimVersion)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000496 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000497 << OSXVersion->getAsString(Args)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000498 << (iOSVersion ? iOSVersion : iOSSimVersion)->getAsString(Args);
499 iOSVersion = iOSSimVersion = 0;
500 } else if (iOSVersion && iOSSimVersion) {
501 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
502 << iOSVersion->getAsString(Args)
503 << iOSSimVersion->getAsString(Args);
504 iOSSimVersion = 0;
505 } else if (!OSXVersion && !iOSVersion && !iOSSimVersion) {
506 // If not deployment target was specified on the command line, check for
Daniel Dunbar816bc312010-01-26 01:45:19 +0000507 // environment defines.
508 const char *OSXTarget = ::getenv("MACOSX_DEPLOYMENT_TARGET");
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000509 const char *iOSTarget = ::getenv("IPHONEOS_DEPLOYMENT_TARGET");
510 const char *iOSSimTarget = ::getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET");
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000511
Daniel Dunbar816bc312010-01-26 01:45:19 +0000512 // Ignore empty strings.
513 if (OSXTarget && OSXTarget[0] == '\0')
514 OSXTarget = 0;
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000515 if (iOSTarget && iOSTarget[0] == '\0')
516 iOSTarget = 0;
517 if (iOSSimTarget && iOSSimTarget[0] == '\0')
518 iOSSimTarget = 0;
Daniel Dunbar816bc312010-01-26 01:45:19 +0000519
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000520 // Handle conflicting deployment targets
Daniel Dunbar39053672010-02-02 17:31:12 +0000521 //
522 // FIXME: Don't hardcode default here.
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000523
524 // Do not allow conflicts with the iOS simulator target.
525 if (iOSSimTarget && (OSXTarget || iOSTarget)) {
526 getDriver().Diag(clang::diag::err_drv_conflicting_deployment_targets)
527 << "IOS_SIMULATOR_DEPLOYMENT_TARGET"
528 << (OSXTarget ? "MACOSX_DEPLOYMENT_TARGET" :
529 "IPHONEOS_DEPLOYMENT_TARGET");
530 }
531
532 // Allow conflicts among OSX and iOS for historical reasons, but choose the
533 // default platform.
534 if (OSXTarget && iOSTarget) {
Daniel Dunbar39053672010-02-02 17:31:12 +0000535 if (getTriple().getArch() == llvm::Triple::arm ||
536 getTriple().getArch() == llvm::Triple::thumb)
Daniel Dunbar84d1e6e2010-03-20 08:47:42 +0000537 OSXTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000538 else
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000539 iOSTarget = 0;
Daniel Dunbar39053672010-02-02 17:31:12 +0000540 }
Daniel Dunbar1a3c1d92010-01-29 17:02:25 +0000541
Daniel Dunbar39053672010-02-02 17:31:12 +0000542 if (OSXTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000543 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000544 OSXVersion = Args.MakeJoinedArg(0, O, OSXTarget);
545 Args.append(OSXVersion);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000546 } else if (iOSTarget) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000547 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000548 iOSVersion = Args.MakeJoinedArg(0, O, iOSTarget);
549 Args.append(iOSVersion);
550 } else if (iOSSimTarget) {
551 const Option *O = Opts.getOption(
552 options::OPT_mios_simulator_version_min_EQ);
553 iOSSimVersion = Args.MakeJoinedArg(0, O, iOSSimTarget);
554 Args.append(iOSSimVersion);
Daniel Dunbar816bc312010-01-26 01:45:19 +0000555 } else {
Daniel Dunbar2bb38d02010-07-15 16:18:06 +0000556 // Otherwise, assume we are targeting OS X.
557 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000558 OSXVersion = Args.MakeJoinedArg(0, O, MacosxVersionMin);
559 Args.append(OSXVersion);
Daniel Dunbar30392de2009-09-04 18:35:21 +0000560 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000561 }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Daniel Dunbar3fd823b2011-04-30 04:20:40 +0000563 // Reject invalid architecture combinations.
564 if (iOSSimVersion && (getTriple().getArch() != llvm::Triple::x86 &&
565 getTriple().getArch() != llvm::Triple::x86_64)) {
566 getDriver().Diag(clang::diag::err_drv_invalid_arch_for_deployment_target)
567 << getTriple().getArchName() << iOSSimVersion->getAsString(Args);
568 }
569
Daniel Dunbar26031372010-01-27 00:56:25 +0000570 // Set the tool chain target information.
571 unsigned Major, Minor, Micro;
572 bool HadExtra;
573 if (OSXVersion) {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000574 assert((!iOSVersion && !iOSSimVersion) && "Unknown target platform!");
Daniel Dunbar26031372010-01-27 00:56:25 +0000575 if (!Driver::GetReleaseVersion(OSXVersion->getValue(Args), Major, Minor,
576 Micro, HadExtra) || HadExtra ||
Daniel Dunbar8a3a7f32011-04-21 21:27:33 +0000577 Major != 10 || Minor >= 100 || Micro >= 100)
Daniel Dunbar26031372010-01-27 00:56:25 +0000578 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
579 << OSXVersion->getAsString(Args);
580 } else {
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000581 const Arg *Version = iOSVersion ? iOSVersion : iOSSimVersion;
582 assert(Version && "Unknown target platform!");
583 if (!Driver::GetReleaseVersion(Version->getValue(Args), Major, Minor,
Daniel Dunbar26031372010-01-27 00:56:25 +0000584 Micro, HadExtra) || HadExtra ||
585 Major >= 10 || Minor >= 100 || Micro >= 100)
586 getDriver().Diag(clang::diag::err_drv_invalid_version_number)
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000587 << Version->getAsString(Args);
Daniel Dunbar26031372010-01-27 00:56:25 +0000588 }
Daniel Dunbar9d609f22011-04-30 04:15:58 +0000589
Daniel Dunbar5f5c37b2011-04-30 04:18:16 +0000590 bool IsIOSSim = bool(iOSSimVersion);
591
592 // In GCC, the simulator historically was treated as being OS X in some
593 // contexts, like determining the link logic, despite generally being called
594 // with an iOS deployment target. For compatibility, we detect the
595 // simulator as iOS + x86, and treat it differently in a few contexts.
596 if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
597 getTriple().getArch() == llvm::Triple::x86_64))
598 IsIOSSim = true;
599
600 setTarget(/*IsIPhoneOS=*/ !OSXVersion, Major, Minor, Micro, IsIOSSim);
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000601}
602
Daniel Dunbar132e35d2010-09-17 01:20:05 +0000603void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000604 ArgStringList &CmdArgs) const {
605 CXXStdlibType Type = GetCXXStdlibType(Args);
606
607 switch (Type) {
608 case ToolChain::CST_Libcxx:
609 CmdArgs.push_back("-lc++");
610 break;
611
612 case ToolChain::CST_Libstdcxx: {
613 // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
614 // it was previously found in the gcc lib dir. However, for all the Darwin
615 // platforms we care about it was -lstdc++.6, so we search for that
616 // explicitly if we can't see an obvious -lstdc++ candidate.
617
618 // Check in the sysroot first.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000619 bool Exists;
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000620 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
621 llvm::sys::Path P(A->getValue(Args));
622 P.appendComponent("usr");
623 P.appendComponent("lib");
624 P.appendComponent("libstdc++.dylib");
625
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000626 if (llvm::sys::fs::exists(P.str(), Exists) || !Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000627 P.eraseComponent();
628 P.appendComponent("libstdc++.6.dylib");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000629 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists) {
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000630 CmdArgs.push_back(Args.MakeArgString(P.str()));
631 return;
632 }
633 }
634 }
635
636 // Otherwise, look in the root.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000637 if ((llvm::sys::fs::exists("/usr/lib/libstdc++.dylib", Exists) || !Exists)&&
638 (!llvm::sys::fs::exists("/usr/lib/libstdc++.6.dylib", Exists) && Exists)){
Daniel Dunbarefe91ea2010-09-17 01:16:06 +0000639 CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
640 return;
641 }
642
643 // Otherwise, let the linker search.
644 CmdArgs.push_back("-lstdc++");
645 break;
646 }
647 }
648}
649
Shantonu Sen7433fed2010-09-17 18:39:08 +0000650void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
651 ArgStringList &CmdArgs) const {
652
653 // For Darwin platforms, use the compiler-rt-based support library
654 // instead of the gcc-provided one (which is also incidentally
655 // only present in the gcc lib dir, which makes it hard to find).
656
657 llvm::sys::Path P(getDriver().ResourceDir);
658 P.appendComponent("lib");
659 P.appendComponent("darwin");
660 P.appendComponent("libclang_rt.cc_kext.a");
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000661
Shantonu Sen7433fed2010-09-17 18:39:08 +0000662 // For now, allow missing resource libraries to support developers who may
663 // not have compiler-rt checked out or integrated into their build.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000664 bool Exists;
665 if (!llvm::sys::fs::exists(P.str(), Exists) && Exists)
Shantonu Sen7433fed2010-09-17 18:39:08 +0000666 CmdArgs.push_back(Args.MakeArgString(P.str()));
667}
668
Daniel Dunbarc0e665e2010-07-19 17:11:33 +0000669DerivedArgList *Darwin::TranslateArgs(const DerivedArgList &Args,
670 const char *BoundArch) const {
671 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
672 const OptTable &Opts = getDriver().getOpts();
673
674 // FIXME: We really want to get out of the tool chain level argument
675 // translation business, as it makes the driver functionality much
676 // more opaque. For now, we follow gcc closely solely for the
677 // purpose of easily achieving feature parity & testability. Once we
678 // have something that works, we should reevaluate each translation
679 // and try to push it down into tool specific logic.
Daniel Dunbar26031372010-01-27 00:56:25 +0000680
Daniel Dunbar279c1db2010-06-11 22:00:26 +0000681 for (ArgList::const_iterator it = Args.begin(),
682 ie = Args.end(); it != ie; ++it) {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000683 Arg *A = *it;
684
685 if (A->getOption().matches(options::OPT_Xarch__)) {
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000686 // Skip this argument unless the architecture matches either the toolchain
687 // triple arch, or the arch being bound.
688 //
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000689 // FIXME: Canonicalize name.
Daniel Dunbar2a45fa72011-06-21 00:20:17 +0000690 llvm::StringRef XarchArch = A->getValue(Args, 0);
691 if (!(XarchArch == getArchName() ||
692 (BoundArch && XarchArch == BoundArch)))
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000693 continue;
694
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000695 Arg *OriginalArg = A;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000696 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(Args, 1));
697 unsigned Prev = Index;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000698 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000700 // If the argument parsing failed or more than one argument was
701 // consumed, the -Xarch_ argument's parameter tried to consume
702 // extra arguments. Emit an error and ignore.
703 //
704 // We also want to disallow any options which would alter the
705 // driver behavior; that isn't going to work in our model. We
706 // use isDriverOption() as an approximation, although things
707 // like -O4 are going to slip through.
Daniel Dunbar0e02f6e2011-04-21 17:41:34 +0000708 if (!XarchArg || Index > Prev + 1) {
Daniel Dunbar7e9293b2011-04-21 17:32:21 +0000709 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_with_args)
710 << A->getAsString(Args);
711 continue;
712 } else if (XarchArg->getOption().isDriverOption()) {
713 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument_isdriver)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000714 << A->getAsString(Args);
715 continue;
716 }
717
Daniel Dunbar478edc22009-03-29 22:29:05 +0000718 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000719 A = XarchArg;
Daniel Dunbar0e100312010-06-14 21:23:08 +0000720
721 DAL->AddSynthesizedArg(A);
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000722
723 // Linker input arguments require custom handling. The problem is that we
724 // have already constructed the phase actions, so we can not treat them as
725 // "input arguments".
726 if (A->getOption().isLinkerInput()) {
727 // Convert the argument into individual Zlinker_input_args.
728 for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) {
729 DAL->AddSeparateArg(OriginalArg,
730 Opts.getOption(options::OPT_Zlinker_input),
731 A->getValue(Args, i));
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +0000732
Daniel Dunbar8ac38d72011-02-19 05:33:51 +0000733 }
734 continue;
735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000737
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000738 // Sob. These is strictly gcc compatible for the time being. Apple
739 // gcc translates options twice, which means that self-expanding
740 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000741 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000742 default:
743 DAL->append(A);
744 break;
745
746 case options::OPT_mkernel:
747 case options::OPT_fapple_kext:
748 DAL->append(A);
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000749 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
750 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000751 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000753 case options::OPT_dependency_file:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000754 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF),
755 A->getValue(Args));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000756 break;
757
758 case options::OPT_gfull:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000759 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
760 DAL->AddFlagArg(A,
761 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000762 break;
763
764 case options::OPT_gused:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000765 DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
766 DAL->AddFlagArg(A,
767 Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000768 break;
769
770 case options::OPT_fterminated_vtables:
771 case options::OPT_findirect_virtual_calls:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000772 DAL->AddFlagArg(A, Opts.getOption(options::OPT_fapple_kext));
773 DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000774 break;
775
776 case options::OPT_shared:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000777 DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000778 break;
779
780 case options::OPT_fconstant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000781 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000782 break;
783
784 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000785 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000786 break;
787
788 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000789 DAL->AddFlagArg(A,
790 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000791 break;
792
793 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000794 DAL->AddFlagArg(A,
795 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000796 break;
797
798 case options::OPT_fpascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000799 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000800 break;
801
802 case options::OPT_fno_pascal_strings:
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000803 DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000804 break;
805 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000806 }
807
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000808 if (getTriple().getArch() == llvm::Triple::x86 ||
809 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000810 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000811 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ), "core2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000812
813 // Add the arch options based on the particular spelling of -arch, to match
814 // how the driver driver works.
815 if (BoundArch) {
816 llvm::StringRef Name = BoundArch;
817 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
818 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
819
820 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
821 // which defines the list of which architectures we accept.
822 if (Name == "ppc")
823 ;
824 else if (Name == "ppc601")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000825 DAL->AddJoinedArg(0, MCpu, "601");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000826 else if (Name == "ppc603")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000827 DAL->AddJoinedArg(0, MCpu, "603");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000828 else if (Name == "ppc604")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000829 DAL->AddJoinedArg(0, MCpu, "604");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000830 else if (Name == "ppc604e")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000831 DAL->AddJoinedArg(0, MCpu, "604e");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000832 else if (Name == "ppc750")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000833 DAL->AddJoinedArg(0, MCpu, "750");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000834 else if (Name == "ppc7400")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000835 DAL->AddJoinedArg(0, MCpu, "7400");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000836 else if (Name == "ppc7450")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000837 DAL->AddJoinedArg(0, MCpu, "7450");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000838 else if (Name == "ppc970")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000839 DAL->AddJoinedArg(0, MCpu, "970");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000840
841 else if (Name == "ppc64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000842 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000843
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000844 else if (Name == "i386")
845 ;
846 else if (Name == "i486")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000847 DAL->AddJoinedArg(0, MArch, "i486");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000848 else if (Name == "i586")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000849 DAL->AddJoinedArg(0, MArch, "i586");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000850 else if (Name == "i686")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000851 DAL->AddJoinedArg(0, MArch, "i686");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000852 else if (Name == "pentium")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000853 DAL->AddJoinedArg(0, MArch, "pentium");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000854 else if (Name == "pentium2")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000855 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000856 else if (Name == "pentpro")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000857 DAL->AddJoinedArg(0, MArch, "pentiumpro");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000858 else if (Name == "pentIIm3")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000859 DAL->AddJoinedArg(0, MArch, "pentium2");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000860
861 else if (Name == "x86_64")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000862 DAL->AddFlagArg(0, Opts.getOption(options::OPT_m64));
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000863
864 else if (Name == "arm")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000865 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000866 else if (Name == "armv4t")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000867 DAL->AddJoinedArg(0, MArch, "armv4t");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000868 else if (Name == "armv5")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000869 DAL->AddJoinedArg(0, MArch, "armv5tej");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000870 else if (Name == "xscale")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000871 DAL->AddJoinedArg(0, MArch, "xscale");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000872 else if (Name == "armv6")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000873 DAL->AddJoinedArg(0, MArch, "armv6k");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000874 else if (Name == "armv7")
Daniel Dunbar9d0863b2010-06-14 20:20:41 +0000875 DAL->AddJoinedArg(0, MArch, "armv7a");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000876
877 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000878 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000879 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000880
Daniel Dunbar60baf0f2010-07-19 17:11:36 +0000881 // Add an explicit version min argument for the deployment target. We do this
882 // after argument translation because -Xarch_ arguments may add a version min
883 // argument.
884 AddDeploymentTarget(*DAL);
885
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000886 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000887}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000888
Daniel Dunbarf3955282009-09-04 18:34:51 +0000889bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000890 // FIXME: Gross; we should probably have some separate target
891 // definition, possibly even reusing the one in clang.
892 return getArchName() == "x86_64";
893}
894
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000895bool Darwin::UseDwarfDebugFlags() const {
896 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
897 return S[0] != '\0';
898 return false;
899}
900
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000901bool Darwin::UseSjLjExceptions() const {
902 // Darwin uses SjLj exceptions on ARM.
903 return (getTriple().getArch() == llvm::Triple::arm ||
904 getTriple().getArch() == llvm::Triple::thumb);
905}
906
Daniel Dunbarf3955282009-09-04 18:34:51 +0000907const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000908 return "pic";
909}
910
Daniel Dunbarf3955282009-09-04 18:34:51 +0000911const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000912 if (getArchName() == "x86_64")
913 return "pic";
914 return 0;
915}
916
Daniel Dunbarbbe8e3e2011-03-01 18:49:30 +0000917bool Darwin::SupportsProfiling() const {
918 // Profiling instrumentation is only supported on x86.
919 return getArchName() == "i386" || getArchName() == "x86_64";
920}
921
Daniel Dunbar43a9b322010-04-10 16:20:23 +0000922bool Darwin::SupportsObjCGC() const {
923 // Garbage collection is supported everywhere except on iPhone OS.
924 return !isTargetIPhoneOS();
925}
926
Daniel Dunbar00577ad2010-08-23 22:35:37 +0000927std::string
928Darwin_Generic_GCC::ComputeEffectiveClangTriple(const ArgList &Args) const {
929 return ComputeLLVMTriple(Args);
930}
931
Daniel Dunbar39176082009-03-20 00:20:03 +0000932/// Generic_GCC - A tool chain using the 'gcc' command to perform
933/// all subcommands; this relies on gcc translating the majority of
934/// command line options.
935
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000936Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000937 : ToolChain(Host, Triple) {
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000938 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +0000939 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000940 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000941}
942
Daniel Dunbar39176082009-03-20 00:20:03 +0000943Generic_GCC::~Generic_GCC() {
944 // Free tool implementations.
945 for (llvm::DenseMap<unsigned, Tool*>::iterator
946 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
947 delete it->second;
948}
949
Mike Stump1eb44332009-09-09 15:08:12 +0000950Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +0000951 const JobAction &JA,
952 const ActionList &Inputs) const {
Daniel Dunbar39176082009-03-20 00:20:03 +0000953 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000954 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000955 Key = Action::AnalyzeJobClass;
956 else
957 Key = JA.getKind();
958
959 Tool *&T = Tools[Key];
960 if (!T) {
961 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000962 case Action::InputClass:
963 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000964 assert(0 && "Invalid tool kind.");
965 case Action::PreprocessJobClass:
966 T = new tools::gcc::Preprocess(*this); break;
967 case Action::PrecompileJobClass:
968 T = new tools::gcc::Precompile(*this); break;
969 case Action::AnalyzeJobClass:
970 T = new tools::Clang(*this); break;
971 case Action::CompileJobClass:
972 T = new tools::gcc::Compile(*this); break;
973 case Action::AssembleJobClass:
974 T = new tools::gcc::Assemble(*this); break;
975 case Action::LinkJobClass:
976 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000978 // This is a bit ungeneric, but the only platform using a driver
979 // driver is Darwin.
980 case Action::LipoJobClass:
981 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar6e0f2542010-06-04 18:28:36 +0000982 case Action::DsymutilJobClass:
983 T = new tools::darwin::Dsymutil(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000984 }
985 }
986
987 return *T;
988}
989
Daniel Dunbar39176082009-03-20 00:20:03 +0000990bool Generic_GCC::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.
Daniel Dunbar39176082009-03-20 00:20:03 +0000993 return getArchName() == "x86_64";
994}
995
996const char *Generic_GCC::GetDefaultRelocationModel() const {
997 return "static";
998}
999
1000const char *Generic_GCC::GetForcedPicModel() const {
1001 return 0;
1002}
Daniel Dunbarf3cad362009-03-25 04:13:45 +00001003
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001004/// TCEToolChain - A tool chain using the llvm bitcode tools to perform
1005/// all subcommands. See http://tce.cs.tut.fi for our peculiar target.
1006/// Currently does not support anything else but compilation.
1007
1008TCEToolChain::TCEToolChain(const HostInfo &Host, const llvm::Triple& Triple)
1009 : ToolChain(Host, Triple) {
1010 // Path mangling to find libexec
1011 std::string Path(getDriver().Dir);
1012
1013 Path += "/../libexec";
1014 getProgramPaths().push_back(Path);
1015}
1016
1017TCEToolChain::~TCEToolChain() {
1018 for (llvm::DenseMap<unsigned, Tool*>::iterator
1019 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
1020 delete it->second;
1021}
1022
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001023bool TCEToolChain::IsMathErrnoDefault() const {
1024 return true;
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001025}
1026
1027bool TCEToolChain::IsUnwindTablesDefault() const {
1028 return false;
1029}
1030
1031const char *TCEToolChain::GetDefaultRelocationModel() const {
1032 return "static";
1033}
1034
1035const char *TCEToolChain::GetForcedPicModel() const {
1036 return 0;
1037}
1038
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001039Tool &TCEToolChain::SelectTool(const Compilation &C,
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001040 const JobAction &JA,
1041 const ActionList &Inputs) const {
Chris Lattner3a47c4e2010-03-04 21:07:38 +00001042 Action::ActionClass Key;
1043 Key = Action::AnalyzeJobClass;
1044
1045 Tool *&T = Tools[Key];
1046 if (!T) {
1047 switch (Key) {
1048 case Action::PreprocessJobClass:
1049 T = new tools::gcc::Preprocess(*this); break;
1050 case Action::AnalyzeJobClass:
1051 T = new tools::Clang(*this); break;
1052 default:
1053 assert(false && "Unsupported action for TCE target.");
1054 }
1055 }
1056 return *T;
1057}
1058
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001059/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
1060
1061OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001062 : Generic_ELF(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001063 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001064 getFilePaths().push_back("/usr/lib");
1065}
1066
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001067Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA,
1068 const ActionList &Inputs) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001069 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001070 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001071 Key = Action::AnalyzeJobClass;
1072 else
1073 Key = JA.getKind();
1074
Rafael Espindoladda5b922010-11-07 23:13:01 +00001075 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1076 options::OPT_no_integrated_as,
1077 IsIntegratedAssemblerDefault());
1078
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001079 Tool *&T = Tools[Key];
1080 if (!T) {
1081 switch (Key) {
Rafael Espindoladda5b922010-11-07 23:13:01 +00001082 case Action::AssembleJobClass: {
1083 if (UseIntegratedAs)
1084 T = new tools::ClangAs(*this);
1085 else
1086 T = new tools::openbsd::Assemble(*this);
1087 break;
1088 }
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001089 case Action::LinkJobClass:
1090 T = new tools::openbsd::Link(*this); break;
1091 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001092 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00001093 }
1094 }
1095
1096 return *T;
1097}
1098
Daniel Dunbar75358d22009-03-30 21:06:03 +00001099/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
1100
Daniel Dunbar214afe92010-08-02 05:43:59 +00001101FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001102 : Generic_ELF(Host, Triple) {
Daniel Dunbar214afe92010-08-02 05:43:59 +00001103
1104 // Determine if we are compiling 32-bit code on an x86_64 platform.
1105 bool Lib32 = false;
1106 if (Triple.getArch() == llvm::Triple::x86 &&
1107 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1108 llvm::Triple::x86_64)
1109 Lib32 = true;
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001110
Roman Divacky3393cef2011-06-04 07:37:31 +00001111 if (Triple.getArch() == llvm::Triple::ppc &&
1112 llvm::Triple(getDriver().DefaultHostTriple).getArch() ==
1113 llvm::Triple::ppc64)
1114 Lib32 = true;
1115
Daniel Dunbarbc534662009-04-02 18:30:04 +00001116 if (Lib32) {
Daniel Dunbarbc534662009-04-02 18:30:04 +00001117 getFilePaths().push_back("/usr/lib32");
1118 } else {
Daniel Dunbarbc534662009-04-02 18:30:04 +00001119 getFilePaths().push_back("/usr/lib");
1120 }
Daniel Dunbar75358d22009-03-30 21:06:03 +00001121}
1122
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001123Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA,
1124 const ActionList &Inputs) const {
Daniel Dunbar75358d22009-03-30 21:06:03 +00001125 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001126 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +00001127 Key = Action::AnalyzeJobClass;
1128 else
1129 Key = JA.getKind();
1130
Roman Divacky67dece72010-11-08 17:46:39 +00001131 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1132 options::OPT_no_integrated_as,
1133 IsIntegratedAssemblerDefault());
1134
Daniel Dunbar75358d22009-03-30 21:06:03 +00001135 Tool *&T = Tools[Key];
1136 if (!T) {
1137 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00001138 case Action::AssembleJobClass:
Roman Divacky67dece72010-11-08 17:46:39 +00001139 if (UseIntegratedAs)
1140 T = new tools::ClangAs(*this);
1141 else
1142 T = new tools::freebsd::Assemble(*this);
Roman Divackyfe3a7ea2010-11-08 19:39:10 +00001143 break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +00001144 case Action::LinkJobClass:
1145 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +00001146 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001147 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar75358d22009-03-30 21:06:03 +00001148 }
1149 }
1150
1151 return *T;
1152}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001153
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001154/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
1155
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001156NetBSD::NetBSD(const HostInfo &Host, const llvm::Triple& Triple,
1157 const llvm::Triple& ToolTriple)
1158 : Generic_ELF(Host, Triple), ToolTriple(ToolTriple) {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001159
1160 // Determine if we are compiling 32-bit code on an x86_64 platform.
1161 bool Lib32 = false;
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001162 if (ToolTriple.getArch() == llvm::Triple::x86_64 &&
1163 Triple.getArch() == llvm::Triple::x86)
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001164 Lib32 = true;
1165
Joerg Sonnenberger05e59302011-03-21 13:59:26 +00001166 if (getDriver().UseStdLib) {
1167 if (Lib32)
1168 getFilePaths().push_back("=/usr/lib/i386");
1169 else
1170 getFilePaths().push_back("=/usr/lib");
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001171 }
1172}
1173
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001174Tool &NetBSD::SelectTool(const Compilation &C, const JobAction &JA,
1175 const ActionList &Inputs) const {
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001176 Action::ActionClass Key;
1177 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1178 Key = Action::AnalyzeJobClass;
1179 else
1180 Key = JA.getKind();
1181
1182 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1183 options::OPT_no_integrated_as,
1184 IsIntegratedAssemblerDefault());
1185
1186 Tool *&T = Tools[Key];
1187 if (!T) {
1188 switch (Key) {
1189 case Action::AssembleJobClass:
1190 if (UseIntegratedAs)
1191 T = new tools::ClangAs(*this);
1192 else
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001193 T = new tools::netbsd::Assemble(*this, ToolTriple);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001194 break;
1195 case Action::LinkJobClass:
Joerg Sonnenberger182564c2011-05-16 13:35:02 +00001196 T = new tools::netbsd::Link(*this, ToolTriple);
1197 break;
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001198 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001199 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Benjamin Kramer8e50a962011-02-02 18:59:27 +00001200 }
1201 }
1202
1203 return *T;
1204}
1205
Chris Lattner38e317d2010-07-07 16:01:42 +00001206/// Minix - Minix tool chain which can call as(1) and ld(1) directly.
1207
1208Minix::Minix(const HostInfo &Host, const llvm::Triple& Triple)
1209 : Generic_GCC(Host, Triple) {
1210 getFilePaths().push_back(getDriver().Dir + "/../lib");
1211 getFilePaths().push_back("/usr/lib");
1212 getFilePaths().push_back("/usr/gnu/lib");
1213 getFilePaths().push_back("/usr/gnu/lib/gcc/i686-pc-minix/4.4.3");
1214}
1215
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001216Tool &Minix::SelectTool(const Compilation &C, const JobAction &JA,
1217 const ActionList &Inputs) const {
Chris Lattner38e317d2010-07-07 16:01:42 +00001218 Action::ActionClass Key;
1219 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1220 Key = Action::AnalyzeJobClass;
1221 else
1222 Key = JA.getKind();
1223
1224 Tool *&T = Tools[Key];
1225 if (!T) {
1226 switch (Key) {
1227 case Action::AssembleJobClass:
1228 T = new tools::minix::Assemble(*this); break;
1229 case Action::LinkJobClass:
1230 T = new tools::minix::Link(*this); break;
1231 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001232 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Chris Lattner38e317d2010-07-07 16:01:42 +00001233 }
1234 }
1235
1236 return *T;
1237}
1238
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001239/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
1240
1241AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
1242 : Generic_GCC(Host, Triple) {
1243
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001244 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001245 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001246 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001247
Daniel Dunbaree788e72009-12-21 18:54:17 +00001248 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001249 getFilePaths().push_back("/usr/lib");
1250 getFilePaths().push_back("/usr/sfw/lib");
1251 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00001252 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001253
1254}
1255
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001256Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA,
1257 const ActionList &Inputs) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001258 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001259 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001260 Key = Action::AnalyzeJobClass;
1261 else
1262 Key = JA.getKind();
1263
1264 Tool *&T = Tools[Key];
1265 if (!T) {
1266 switch (Key) {
1267 case Action::AssembleJobClass:
1268 T = new tools::auroraux::Assemble(*this); break;
1269 case Action::LinkJobClass:
1270 T = new tools::auroraux::Link(*this); break;
1271 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001272 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Edward O'Callaghane7925a02009-08-22 01:06:46 +00001273 }
1274 }
1275
1276 return *T;
1277}
1278
1279
Eli Friedman6b3454a2009-05-26 07:52:18 +00001280/// Linux toolchain (very bare-bones at the moment).
1281
Rafael Espindolac1da9812010-11-07 20:14:31 +00001282enum LinuxDistro {
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001283 ArchLinux,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001284 DebianLenny,
1285 DebianSqueeze,
Eli Friedman0b200f62011-06-02 21:36:53 +00001286 DebianWheezy,
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001287 Exherbo,
Chris Lattnerd753b562011-05-22 05:36:06 +00001288 RHEL4,
1289 RHEL5,
1290 RHEL6,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001291 Fedora13,
1292 Fedora14,
Eric Christopher8f1cc072011-04-06 18:22:53 +00001293 Fedora15,
1294 FedoraRawhide,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001295 OpenSuse11_3,
David Chisnallde5c0482011-05-19 13:26:33 +00001296 OpenSuse11_4,
1297 OpenSuse12_1,
Douglas Gregor814638e2011-03-14 15:39:50 +00001298 UbuntuHardy,
1299 UbuntuIntrepid,
Rafael Espindola021aaa42010-11-10 05:00:22 +00001300 UbuntuJaunty,
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001301 UbuntuKarmic,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001302 UbuntuLucid,
1303 UbuntuMaverick,
Ted Kremenek43ac2972011-04-05 22:04:27 +00001304 UbuntuNatty,
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001305 UbuntuOneiric,
Rafael Espindolac1da9812010-11-07 20:14:31 +00001306 UnknownDistro
1307};
1308
Chris Lattnerd753b562011-05-22 05:36:06 +00001309static bool IsRedhat(enum LinuxDistro Distro) {
Eric Christopher8f1cc072011-04-06 18:22:53 +00001310 return Distro == Fedora13 || Distro == Fedora14 ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001311 Distro == Fedora15 || Distro == FedoraRawhide ||
1312 Distro == RHEL4 || Distro == RHEL5 || Distro == RHEL6;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001313}
1314
1315static bool IsOpenSuse(enum LinuxDistro Distro) {
David Chisnallde5c0482011-05-19 13:26:33 +00001316 return Distro == OpenSuse11_3 || Distro == OpenSuse11_4 ||
1317 Distro == OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001318}
1319
1320static bool IsDebian(enum LinuxDistro Distro) {
Eli Friedman0b200f62011-06-02 21:36:53 +00001321 return Distro == DebianLenny || Distro == DebianSqueeze ||
1322 Distro == DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001323}
1324
1325static bool IsUbuntu(enum LinuxDistro Distro) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001326 return Distro == UbuntuHardy || Distro == UbuntuIntrepid ||
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001327 Distro == UbuntuLucid || Distro == UbuntuMaverick ||
Ted Kremenek43ac2972011-04-05 22:04:27 +00001328 Distro == UbuntuJaunty || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001329 Distro == UbuntuNatty || Distro == UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001330}
1331
1332static bool IsDebianBased(enum LinuxDistro Distro) {
1333 return IsDebian(Distro) || IsUbuntu(Distro);
1334}
1335
1336static bool HasMultilib(llvm::Triple::ArchType Arch, enum LinuxDistro Distro) {
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001337 if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001338 bool Exists;
1339 if (Distro == Exherbo &&
1340 (llvm::sys::fs::exists("/usr/lib32/libc.so", Exists) || !Exists))
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001341 return false;
1342
Rafael Espindolac1da9812010-11-07 20:14:31 +00001343 return true;
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001344 }
Ted Kremenek43ac2972011-04-05 22:04:27 +00001345 if (Arch == llvm::Triple::ppc64)
1346 return true;
Eric Christopher9af535a2011-06-03 13:28:31 +00001347 if ((Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc) &&
1348 IsDebianBased(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001349 return true;
1350 return false;
1351}
1352
1353static LinuxDistro DetectLinuxDistro(llvm::Triple::ArchType Arch) {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001354 llvm::OwningPtr<llvm::MemoryBuffer> File;
1355 if (!llvm::MemoryBuffer::getFile("/etc/lsb-release", File)) {
1356 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001357 llvm::SmallVector<llvm::StringRef, 8> Lines;
1358 Data.split(Lines, "\n");
1359 for (unsigned int i = 0, s = Lines.size(); i < s; ++ i) {
Douglas Gregor814638e2011-03-14 15:39:50 +00001360 if (Lines[i] == "DISTRIB_CODENAME=hardy")
1361 return UbuntuHardy;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001362 else if (Lines[i] == "DISTRIB_CODENAME=intrepid")
1363 return UbuntuIntrepid;
Rafael Espindola021aaa42010-11-10 05:00:22 +00001364 else if (Lines[i] == "DISTRIB_CODENAME=jaunty")
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001365 return UbuntuJaunty;
Zhongxing Xu5ede8072010-11-15 09:01:52 +00001366 else if (Lines[i] == "DISTRIB_CODENAME=karmic")
1367 return UbuntuKarmic;
Ted Kremenek43ac2972011-04-05 22:04:27 +00001368 else if (Lines[i] == "DISTRIB_CODENAME=lucid")
1369 return UbuntuLucid;
1370 else if (Lines[i] == "DISTRIB_CODENAME=maverick")
1371 return UbuntuMaverick;
1372 else if (Lines[i] == "DISTRIB_CODENAME=natty")
1373 return UbuntuNatty;
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001374 else if (Lines[i] == "DISTRIB_CODENAME=oneiric")
1375 return UbuntuOneiric;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001376 }
1377 return UnknownDistro;
1378 }
1379
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001380 if (!llvm::MemoryBuffer::getFile("/etc/redhat-release", File)) {
1381 llvm::StringRef Data = File.get()->getBuffer();
Eric Christopher8f1cc072011-04-06 18:22:53 +00001382 if (Data.startswith("Fedora release 15"))
1383 return Fedora15;
1384 else if (Data.startswith("Fedora release 14"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001385 return Fedora14;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001386 else if (Data.startswith("Fedora release 13"))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001387 return Fedora13;
Eric Christopher8f1cc072011-04-06 18:22:53 +00001388 else if (Data.startswith("Fedora release") &&
1389 Data.find("Rawhide") != llvm::StringRef::npos)
1390 return FedoraRawhide;
Chris Lattnerd753b562011-05-22 05:36:06 +00001391 else if (Data.startswith("Red Hat Enterprise Linux") &&
1392 Data.find("release 6") != llvm::StringRef::npos)
1393 return RHEL6;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001394 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1395 Data.startswith("CentOS")) &&
Chris Lattnerd753b562011-05-22 05:36:06 +00001396 Data.find("release 5") != llvm::StringRef::npos)
1397 return RHEL5;
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001398 else if ((Data.startswith("Red Hat Enterprise Linux") ||
1399 Data.startswith("CentOS")) &&
Chris Lattnerd753b562011-05-22 05:36:06 +00001400 Data.find("release 4") != llvm::StringRef::npos)
1401 return RHEL4;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001402 return UnknownDistro;
1403 }
1404
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001405 if (!llvm::MemoryBuffer::getFile("/etc/debian_version", File)) {
1406 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001407 if (Data[0] == '5')
1408 return DebianLenny;
1409 else if (Data.startswith("squeeze/sid"))
1410 return DebianSqueeze;
Eli Friedman0b200f62011-06-02 21:36:53 +00001411 else if (Data.startswith("wheezy/sid"))
1412 return DebianWheezy;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001413 return UnknownDistro;
1414 }
1415
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00001416 if (!llvm::MemoryBuffer::getFile("/etc/SuSE-release", File)) {
1417 llvm::StringRef Data = File.get()->getBuffer();
Rafael Espindolac1da9812010-11-07 20:14:31 +00001418 if (Data.startswith("openSUSE 11.3"))
1419 return OpenSuse11_3;
David Chisnallde5c0482011-05-19 13:26:33 +00001420 else if (Data.startswith("openSUSE 11.4"))
1421 return OpenSuse11_4;
1422 else if (Data.startswith("openSUSE 12.1"))
1423 return OpenSuse12_1;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001424 return UnknownDistro;
1425 }
1426
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001427 bool Exists;
1428 if (!llvm::sys::fs::exists("/etc/exherbo-release", Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001429 return Exherbo;
1430
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001431 if (!llvm::sys::fs::exists("/etc/arch-release", Exists) && Exists)
1432 return ArchLinux;
1433
Rafael Espindolac1da9812010-11-07 20:14:31 +00001434 return UnknownDistro;
1435}
1436
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001437static std::string findGCCBaseLibDir(const std::string &GccTriple) {
Chandler Carruth169dd312011-06-08 10:14:38 +00001438 // FIXME: Using CXX_INCLUDE_ROOT is here is a bit of a hack, but
1439 // avoids adding yet another option to configure/cmake.
1440 // It would probably be cleaner to break it in two variables
1441 // CXX_GCC_ROOT with just /foo/bar
1442 // CXX_GCC_VER with 4.5.2
1443 // Then we would have
1444 // CXX_INCLUDE_ROOT = CXX_GCC_ROOT/include/c++/CXX_GCC_VER
1445 // and this function would return
1446 // CXX_GCC_ROOT/lib/gcc/CXX_INCLUDE_ARCH/CXX_GCC_VER
1447 llvm::SmallString<128> CxxIncludeRoot(CXX_INCLUDE_ROOT);
1448 if (CxxIncludeRoot != "") {
1449 // This is of the form /foo/bar/include/c++/4.5.2/
1450 if (CxxIncludeRoot.back() == '/')
1451 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the /
1452 llvm::StringRef Version = llvm::sys::path::filename(CxxIncludeRoot);
1453 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the version
1454 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the c++
1455 llvm::sys::path::remove_filename(CxxIncludeRoot); // remove the include
1456 std::string ret(CxxIncludeRoot.c_str());
1457 ret.append("/lib/gcc/");
1458 ret.append(CXX_INCLUDE_ARCH);
1459 ret.append("/");
1460 ret.append(Version);
1461 return ret;
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001462 }
1463 static const char* GccVersions[] = {"4.6.0", "4.6",
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001464 "4.5.2", "4.5.1", "4.5",
1465 "4.4.5", "4.4.4", "4.4.3", "4.4",
1466 "4.3.4", "4.3.3", "4.3.2", "4.3",
1467 "4.2.4", "4.2.3", "4.2.2", "4.2.1",
1468 "4.2", "4.1.1"};
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001469 bool Exists;
1470 for (unsigned i = 0; i < sizeof(GccVersions)/sizeof(char*); ++i) {
1471 std::string Suffix = GccTriple + "/" + GccVersions[i];
1472 std::string t1 = "/usr/lib/gcc/" + Suffix;
1473 if (!llvm::sys::fs::exists(t1 + "/crtbegin.o", Exists) && Exists)
1474 return t1;
1475 std::string t2 = "/usr/lib64/gcc/" + Suffix;
1476 if (!llvm::sys::fs::exists(t2 + "/crtbegin.o", Exists) && Exists)
1477 return t2;
1478 std::string t3 = "/usr/lib/" + GccTriple + "/gcc/" + Suffix;
1479 if (!llvm::sys::fs::exists(t3 + "/crtbegin.o", Exists) && Exists)
1480 return t3;
1481 }
1482 return "";
1483}
1484
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001485Linux::Linux(const HostInfo &Host, const llvm::Triple &Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001486 : Generic_ELF(Host, Triple) {
Rafael Espindolac1da9812010-11-07 20:14:31 +00001487 llvm::Triple::ArchType Arch =
1488 llvm::Triple(getDriver().DefaultHostTriple).getArch();
Daniel Dunbara9822de2009-08-06 01:47:11 +00001489
Rafael Espindolac1da9812010-11-07 20:14:31 +00001490 std::string Suffix32 = "";
1491 if (Arch == llvm::Triple::x86_64)
1492 Suffix32 = "/32";
Daniel Dunbara9822de2009-08-06 01:47:11 +00001493
Rafael Espindolac1da9812010-11-07 20:14:31 +00001494 std::string Suffix64 = "";
Ted Kremenek43ac2972011-04-05 22:04:27 +00001495 if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::ppc)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001496 Suffix64 = "/64";
1497
1498 std::string Lib32 = "lib";
1499
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001500 bool Exists;
1501 if (!llvm::sys::fs::exists("/lib32", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001502 Lib32 = "lib32";
1503
1504 std::string Lib64 = "lib";
Michael J. Spencer2dea7c72011-01-12 23:54:48 +00001505 bool Symlink;
Chris Lattner48aef362011-01-13 01:35:58 +00001506 if (!llvm::sys::fs::exists("/lib64", Exists) && Exists &&
1507 (llvm::sys::fs::is_symlink("/lib64", Symlink) || !Symlink))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001508 Lib64 = "lib64";
1509
1510 std::string GccTriple = "";
Douglas Gregorf0594d82011-03-06 19:11:49 +00001511 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001512 if (!llvm::sys::fs::exists("/usr/lib/gcc/arm-linux-gnueabi", Exists) &&
1513 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001514 GccTriple = "arm-linux-gnueabi";
1515 } else if (Arch == llvm::Triple::x86_64) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001516 if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-linux-gnu", Exists) &&
1517 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001518 GccTriple = "x86_64-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001519 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-unknown-linux-gnu",
1520 Exists) && Exists)
Rafael Espindola53dd00b2010-11-17 00:25:26 +00001521 GccTriple = "x86_64-unknown-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001522 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-pc-linux-gnu",
1523 Exists) && Exists)
Rafael Espindola0a84aee2010-11-11 02:07:13 +00001524 GccTriple = "x86_64-pc-linux-gnu";
NAKAMURA Takumic3703982011-06-16 12:43:57 +00001525 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux6E",
1526 Exists) && Exists)
1527 GccTriple = "x86_64-redhat-linux6E";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001528 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-redhat-linux",
1529 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001530 GccTriple = "x86_64-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001531 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/x86_64-suse-linux",
1532 Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001533 GccTriple = "x86_64-suse-linux";
Nick Lewyckye10f9002011-02-01 23:03:29 +00001534 else if (!llvm::sys::fs::exists("/usr/lib/gcc/x86_64-manbo-linux-gnu",
1535 Exists) && Exists)
1536 GccTriple = "x86_64-manbo-linux-gnu";
Nico Weber80585d82011-04-25 03:17:35 +00001537 else if (!llvm::sys::fs::exists("/usr/lib/x86_64-linux-gnu/gcc",
1538 Exists) && Exists)
1539 GccTriple = "x86_64-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001540 } else if (Arch == llvm::Triple::x86) {
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001541 if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-linux-gnu", Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001542 GccTriple = "i686-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001543 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-pc-linux-gnu", Exists) &&
1544 Exists)
Nuno Lopes2a69ddd2010-11-19 17:26:57 +00001545 GccTriple = "i686-pc-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001546 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-linux-gnu", Exists) &&
1547 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001548 GccTriple = "i486-linux-gnu";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001549 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i686-redhat-linux", Exists) &&
1550 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001551 GccTriple = "i686-redhat-linux";
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001552 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i586-suse-linux", Exists) &&
1553 Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001554 GccTriple = "i586-suse-linux";
Ted Kremenekd130c7d2011-04-18 17:50:19 +00001555 else if (!llvm::sys::fs::exists("/usr/lib/gcc/i486-slackware-linux", Exists)
1556 && Exists)
1557 GccTriple = "i486-slackware-linux";
Ted Kremenek43ac2972011-04-05 22:04:27 +00001558 } else if (Arch == llvm::Triple::ppc) {
1559 if (!llvm::sys::fs::exists("/usr/lib/powerpc-linux-gnu", Exists) && Exists)
1560 GccTriple = "powerpc-linux-gnu";
Eric Christopher9af535a2011-06-03 13:28:31 +00001561 else if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc-unknown-linux-gnu",
1562 Exists) && Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001563 GccTriple = "powerpc-unknown-linux-gnu";
1564 } else if (Arch == llvm::Triple::ppc64) {
Eric Christopher9af535a2011-06-03 13:28:31 +00001565 if (!llvm::sys::fs::exists("/usr/lib/gcc/powerpc64-unknown-linux-gnu",
1566 Exists) && Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001567 GccTriple = "powerpc64-unknown-linux-gnu";
Eric Christopher9af535a2011-06-03 13:28:31 +00001568 else if (!llvm::sys::fs::exists("/usr/lib64/gcc/"
1569 "powerpc64-unknown-linux-gnu", Exists) &&
1570 Exists)
Ted Kremenek43ac2972011-04-05 22:04:27 +00001571 GccTriple = "powerpc64-unknown-linux-gnu";
Rafael Espindolac1da9812010-11-07 20:14:31 +00001572 }
1573
Rafael Espindola14ea13c2011-06-02 22:18:46 +00001574 std::string Base = findGCCBaseLibDir(GccTriple);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001575 path_list &Paths = getFilePaths();
Eric Christopher9af535a2011-06-03 13:28:31 +00001576 bool Is32Bits = (getArch() == llvm::Triple::x86 ||
1577 getArch() == llvm::Triple::ppc);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001578
1579 std::string Suffix;
1580 std::string Lib;
1581
1582 if (Is32Bits) {
1583 Suffix = Suffix32;
1584 Lib = Lib32;
1585 } else {
1586 Suffix = Suffix64;
1587 Lib = Lib64;
1588 }
1589
1590 llvm::sys::Path LinkerPath(Base + "/../../../../" + GccTriple + "/bin/ld");
Michael J. Spencer32bef4e2011-01-10 02:34:13 +00001591 if (!llvm::sys::fs::exists(LinkerPath.str(), Exists) && Exists)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001592 Linker = LinkerPath.str();
1593 else
1594 Linker = GetProgramPath("ld");
1595
1596 LinuxDistro Distro = DetectLinuxDistro(Arch);
1597
Chris Lattner64a89172011-05-22 16:45:07 +00001598 if (IsOpenSuse(Distro) || IsUbuntu(Distro)) {
Rafael Espindola94c80222010-11-08 14:48:47 +00001599 ExtraOpts.push_back("-z");
1600 ExtraOpts.push_back("relro");
1601 }
Rafael Espindolac1da9812010-11-07 20:14:31 +00001602
Douglas Gregorf0594d82011-03-06 19:11:49 +00001603 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001604 ExtraOpts.push_back("-X");
1605
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001606 if (IsRedhat(Distro) || IsOpenSuse(Distro) || Distro == UbuntuMaverick ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001607 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001608 ExtraOpts.push_back("--hash-style=gnu");
1609
NAKAMURA Takumi304ed3f2011-06-03 03:49:51 +00001610 if (IsDebian(Distro) || IsOpenSuse(Distro) || Distro == UbuntuLucid ||
Chris Lattner64a89172011-05-22 16:45:07 +00001611 Distro == UbuntuJaunty || Distro == UbuntuKarmic)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001612 ExtraOpts.push_back("--hash-style=both");
1613
Chris Lattnerd753b562011-05-22 05:36:06 +00001614 if (IsRedhat(Distro))
Rafael Espindolac1da9812010-11-07 20:14:31 +00001615 ExtraOpts.push_back("--no-add-needed");
1616
Eli Friedman0b200f62011-06-02 21:36:53 +00001617 if (Distro == DebianSqueeze || Distro == DebianWheezy ||
Rafael Espindola5a640ef2011-06-03 15:23:24 +00001618 IsOpenSuse(Distro) ||
1619 (IsRedhat(Distro) && Distro != RHEL4 && Distro != RHEL5) ||
1620 Distro == UbuntuLucid ||
Eli Friedman0b200f62011-06-02 21:36:53 +00001621 Distro == UbuntuMaverick || Distro == UbuntuKarmic ||
Benjamin Kramer25a857b2011-06-05 16:08:59 +00001622 Distro == UbuntuNatty || Distro == UbuntuOneiric)
Rafael Espindolac1da9812010-11-07 20:14:31 +00001623 ExtraOpts.push_back("--build-id");
1624
Chris Lattner64a89172011-05-22 16:45:07 +00001625 if (IsOpenSuse(Distro))
Chandler Carruthf0b60ec2011-05-24 07:51:17 +00001626 ExtraOpts.push_back("--enable-new-dtags");
Chris Lattner64a89172011-05-22 16:45:07 +00001627
Chandler Carruth3fd345a2011-02-25 06:39:53 +00001628 if (Distro == ArchLinux)
1629 Lib = "lib";
1630
Rafael Espindolac1da9812010-11-07 20:14:31 +00001631 Paths.push_back(Base + Suffix);
1632 if (HasMultilib(Arch, Distro)) {
1633 if (IsOpenSuse(Distro) && Is32Bits)
1634 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib/../lib");
1635 Paths.push_back(Base + "/../../../../" + Lib);
Rafael Espindolac1da9812010-11-07 20:14:31 +00001636 }
Rafael Espindolac7409a02011-06-03 15:39:42 +00001637
1638 // FIXME: This is in here to find crt1.o. It is provided by libc, and
1639 // libc (like gcc), can be installed in any directory. Once we are
1640 // fetching this from a config file, we should have a libc prefix.
1641 Paths.push_back("/lib/../" + Lib);
1642 Paths.push_back("/usr/lib/../" + Lib);
1643
Rafael Espindolac1da9812010-11-07 20:14:31 +00001644 if (!Suffix.empty())
1645 Paths.push_back(Base);
1646 if (IsOpenSuse(Distro))
1647 Paths.push_back(Base + "/../../../../" + GccTriple + "/lib");
1648 Paths.push_back(Base + "/../../..");
1649 if (Arch == getArch() && IsUbuntu(Distro))
1650 Paths.push_back("/usr/lib/" + GccTriple);
1651}
1652
1653bool Linux::HasNativeLLVMSupport() const {
1654 return true;
Eli Friedman6b3454a2009-05-26 07:52:18 +00001655}
1656
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001657Tool &Linux::SelectTool(const Compilation &C, const JobAction &JA,
1658 const ActionList &Inputs) const {
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001659 Action::ActionClass Key;
1660 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1661 Key = Action::AnalyzeJobClass;
1662 else
1663 Key = JA.getKind();
1664
Rafael Espindoladda5b922010-11-07 23:13:01 +00001665 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
1666 options::OPT_no_integrated_as,
1667 IsIntegratedAssemblerDefault());
1668
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001669 Tool *&T = Tools[Key];
1670 if (!T) {
1671 switch (Key) {
1672 case Action::AssembleJobClass:
Rafael Espindoladda5b922010-11-07 23:13:01 +00001673 if (UseIntegratedAs)
1674 T = new tools::ClangAs(*this);
1675 else
1676 T = new tools::linuxtools::Assemble(*this);
1677 break;
Rafael Espindolac1da9812010-11-07 20:14:31 +00001678 case Action::LinkJobClass:
1679 T = new tools::linuxtools::Link(*this); break;
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001680 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001681 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Rafael Espindolaba30bbe2010-08-10 00:25:48 +00001682 }
1683 }
1684
1685 return *T;
1686}
1687
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001688/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
1689
Daniel Dunbarcb8ab232009-05-22 02:53:45 +00001690DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
Rafael Espindolae43cfa12010-10-29 20:14:02 +00001691 : Generic_ELF(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001692
1693 // Path mangling to find libexec
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001694 getProgramPaths().push_back(getDriver().getInstalledDir());
Benjamin Kramer86643b82011-03-01 22:50:47 +00001695 if (getDriver().getInstalledDir() != getDriver().Dir)
Daniel Dunbaredf29b02010-08-01 22:29:51 +00001696 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001697
Daniel Dunbaree788e72009-12-21 18:54:17 +00001698 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001699 getFilePaths().push_back("/usr/lib");
1700 getFilePaths().push_back("/usr/lib/gcc41");
1701}
1702
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001703Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA,
1704 const ActionList &Inputs) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001705 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +00001706 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001707 Key = Action::AnalyzeJobClass;
1708 else
1709 Key = JA.getKind();
1710
1711 Tool *&T = Tools[Key];
1712 if (!T) {
1713 switch (Key) {
1714 case Action::AssembleJobClass:
1715 T = new tools::dragonfly::Assemble(*this); break;
1716 case Action::LinkJobClass:
1717 T = new tools::dragonfly::Link(*this); break;
1718 default:
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001719 T = &Generic_GCC::SelectTool(C, JA, Inputs);
Daniel Dunbar11e1b402009-05-02 18:28:39 +00001720 }
1721 }
1722
1723 return *T;
1724}
Michael J. Spencerff58e362010-08-21 21:55:07 +00001725
1726Windows::Windows(const HostInfo &Host, const llvm::Triple& Triple)
1727 : ToolChain(Host, Triple) {
1728}
1729
Daniel Dunbarac0659a2011-03-18 20:14:00 +00001730Tool &Windows::SelectTool(const Compilation &C, const JobAction &JA,
1731 const ActionList &Inputs) const {
Michael J. Spencerff58e362010-08-21 21:55:07 +00001732 Action::ActionClass Key;
1733 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
1734 Key = Action::AnalyzeJobClass;
1735 else
1736 Key = JA.getKind();
1737
1738 Tool *&T = Tools[Key];
1739 if (!T) {
1740 switch (Key) {
1741 case Action::InputClass:
1742 case Action::BindArchClass:
Chandler Carruthe97673f2010-08-22 06:56:37 +00001743 case Action::LipoJobClass:
1744 case Action::DsymutilJobClass:
Michael J. Spencerff58e362010-08-21 21:55:07 +00001745 assert(0 && "Invalid tool kind.");
1746 case Action::PreprocessJobClass:
1747 case Action::PrecompileJobClass:
1748 case Action::AnalyzeJobClass:
1749 case Action::CompileJobClass:
1750 T = new tools::Clang(*this); break;
1751 case Action::AssembleJobClass:
1752 T = new tools::ClangAs(*this); break;
1753 case Action::LinkJobClass:
1754 T = new tools::visualstudio::Link(*this); break;
1755 }
1756 }
1757
1758 return *T;
1759}
1760
1761bool Windows::IsIntegratedAssemblerDefault() const {
1762 return true;
1763}
1764
1765bool Windows::IsUnwindTablesDefault() const {
1766 // FIXME: Gross; we should probably have some separate target
1767 // definition, possibly even reusing the one in clang.
1768 return getArchName() == "x86_64";
1769}
1770
1771const char *Windows::GetDefaultRelocationModel() const {
1772 return "static";
1773}
1774
1775const char *Windows::GetForcedPicModel() const {
1776 if (getArchName() == "x86_64")
1777 return "pic";
1778 return 0;
1779}