blob: 420573dea2a8b6d688d5a600592dc16cd1314335 [file] [log] [blame]
Daniel Dunbar39176082009-03-20 00:20:03 +00001//===--- ToolChains.cpp - ToolChain Implementations ---------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
11
Daniel Dunbarf3cad362009-03-25 04:13:45 +000012#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000014#include "clang/Driver/Driver.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000015#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000016#include "clang/Driver/HostInfo.h"
Daniel Dunbar27e738d2009-11-19 00:15:11 +000017#include "clang/Driver/OptTable.h"
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +000018#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000019#include "clang/Driver/Options.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000020
21#include "llvm/ADT/StringExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000022#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000024#include "llvm/System/Path.h"
25
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000026#include <cstdlib> // ::getenv
27
Daniel Dunbar39176082009-03-20 00:20:03 +000028using namespace clang::driver;
29using namespace clang::driver::toolchains;
30
Daniel Dunbarf3955282009-09-04 18:34:51 +000031/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000032
Daniel Dunbarf3955282009-09-04 18:34:51 +000033Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple,
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000034 const unsigned (&_DarwinVersion)[3], bool _IsIPhoneOS)
35 : ToolChain(Host, Triple),
36 IsIPhoneOS(_IsIPhoneOS)
37{
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000038 DarwinVersion[0] = _DarwinVersion[0];
39 DarwinVersion[1] = _DarwinVersion[1];
40 DarwinVersion[2] = _DarwinVersion[2];
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000041
Daniel Dunbar02633b52009-03-26 16:23:12 +000042 llvm::raw_string_ostream(MacosxVersionMin)
Benjamin Kramer87f9fd92009-10-21 21:05:07 +000043 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
44 << DarwinVersion[1];
Daniel Dunbar02633b52009-03-26 16:23:12 +000045
Daniel Dunbar30392de2009-09-04 18:35:21 +000046 // FIXME: Lift default up.
47 IPhoneOSVersionMin = "3.0";
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000048}
49
50DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
51 const unsigned (&DarwinVersion)[3],
Ted Kremenek55bac532009-10-07 03:21:11 +000052 const unsigned (&_GCCVersion)[3], bool IsIPhoneOS)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000053 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
54{
55 GCCVersion[0] = _GCCVersion[0];
56 GCCVersion[1] = _GCCVersion[1];
57 GCCVersion[2] = _GCCVersion[2];
58
59 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000060 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +000061 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000062 ToolChainDir += "/";
63 ToolChainDir += llvm::utostr(GCCVersion[0]);
64 ToolChainDir += '.';
65 ToolChainDir += llvm::utostr(GCCVersion[1]);
66 ToolChainDir += '.';
67 ToolChainDir += llvm::utostr(GCCVersion[2]);
68
Daniel Dunbar74782b02009-10-20 19:25:43 +000069 // Try the next major version if that tool chain dir is invalid.
Daniel Dunbar080fb192009-10-22 00:12:00 +000070 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
71 if (!llvm::sys::Path(Tmp).exists()) {
Daniel Dunbar74782b02009-10-20 19:25:43 +000072 std::string Next = "i686-apple-darwin";
73 Next += llvm::utostr(DarwinVersion[0] + 1);
74 Next += "/";
75 Next += llvm::utostr(GCCVersion[0]);
76 Next += '.';
77 Next += llvm::utostr(GCCVersion[1]);
78 Next += '.';
79 Next += llvm::utostr(GCCVersion[2]);
80
81 // Use that if it exists, otherwise hope the user isn't linking.
82 //
83 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +000084 Tmp = "/usr/lib/gcc/" + Next;
85 if (llvm::sys::Path(Tmp).exists())
Daniel Dunbar74782b02009-10-20 19:25:43 +000086 ToolChainDir = Next;
87 }
88
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000089 std::string Path;
90 if (getArchName() == "x86_64") {
91 Path = getHost().getDriver().Dir;
92 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000093 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000094 Path += "/x86_64";
95 getFilePaths().push_back(Path);
96
97 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000098 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000099 Path += "/x86_64";
100 getFilePaths().push_back(Path);
101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000103 Path = getHost().getDriver().Dir;
104 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000105 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000106 getFilePaths().push_back(Path);
107
108 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000109 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000110 getFilePaths().push_back(Path);
111
112 Path = getHost().getDriver().Dir;
113 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000114 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000115 getProgramPaths().push_back(Path);
116
117 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000118 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000119 getProgramPaths().push_back(Path);
120
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000121 Path = getHost().getDriver().Dir;
122 Path += "/../libexec";
123 getProgramPaths().push_back(Path);
124
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000125 getProgramPaths().push_back(getHost().getDriver().Dir);
126}
127
Daniel Dunbarf3955282009-09-04 18:34:51 +0000128Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000129 // Free tool implementations.
130 for (llvm::DenseMap<unsigned, Tool*>::iterator
131 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
132 delete it->second;
133}
134
Daniel Dunbarf3955282009-09-04 18:34:51 +0000135Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000136 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000137 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000138 Key = Action::AnalyzeJobClass;
139 else
140 Key = JA.getKind();
141
142 Tool *&T = Tools[Key];
143 if (!T) {
144 switch (Key) {
145 case Action::InputClass:
146 case Action::BindArchClass:
147 assert(0 && "Invalid tool kind.");
148 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000149 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000150 case Action::AnalyzeJobClass:
151 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000152 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000153 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000154 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000155 case Action::AssembleJobClass:
Daniel Dunbar8cac5f72009-03-20 16:06:39 +0000156 T = new tools::darwin::Assemble(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000157 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000158 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000159 case Action::LipoJobClass:
160 T = new tools::darwin::Lipo(*this); break;
161 }
162 }
163
164 return *T;
165}
166
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000167void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
168 ArgStringList &CmdArgs) const {
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000169 // FIXME: Derive these correctly.
170 if (getArchName() == "x86_64") {
171 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
172 "/x86_64"));
173 // Intentionally duplicated for (temporary) gcc bug compatibility.
174 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
175 "/x86_64"));
176 }
177 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
178 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
179 // Intentionally duplicated for (temporary) gcc bug compatibility.
180 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
181 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
182 "/../../../" + ToolChainDir));
183 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
184 "/../../.."));
185}
186
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000187void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
188 ArgStringList &CmdArgs) const {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000189 unsigned MacosxVersionMin[3];
190 getMacosxVersionMin(Args, MacosxVersionMin);
191
192 // Derived from libgcc and lib specs but refactored.
193 if (Args.hasArg(options::OPT_static)) {
194 CmdArgs.push_back("-lgcc_static");
195 } else {
196 if (Args.hasArg(options::OPT_static_libgcc)) {
197 CmdArgs.push_back("-lgcc_eh");
198 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
199 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000200 if (isIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000201 CmdArgs.push_back("-lgcc_s.1");
202 } else {
203 CmdArgs.push_back("-lgcc_s.10.5");
204 }
205 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
206 // FIXME: -fexceptions -fno-exceptions means no exceptions
207 Args.hasArg(options::OPT_fexceptions) ||
208 Args.hasArg(options::OPT_fgnu_runtime)) {
209 // FIXME: This is probably broken on 10.3?
210 if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
211 CmdArgs.push_back("-lgcc_s.10.4");
212 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
213 CmdArgs.push_back("-lgcc_s.10.5");
214 } else {
215 if (isMacosxVersionLT(MacosxVersionMin, 10, 3, 9))
216 ; // Do nothing.
217 else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
218 CmdArgs.push_back("-lgcc_s.10.4");
219 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
220 CmdArgs.push_back("-lgcc_s.10.5");
221 }
222
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000223 if (isIPhoneOS() || isMacosxVersionLT(MacosxVersionMin, 10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000224 CmdArgs.push_back("-lgcc");
225 CmdArgs.push_back("-lSystem");
226 } else {
227 CmdArgs.push_back("-lSystem");
228 CmdArgs.push_back("-lgcc");
229 }
230 }
231}
232
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000233DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
234 const unsigned (&DarwinVersion)[3],
235 bool IsIPhoneOS)
236 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
237{
Daniel Dunbara92ba272009-09-29 18:52:10 +0000238 // Add the relative libexec dir (for clang-cc).
239 //
240 // FIXME: We should sink clang-cc into libexec/clang/<version>/.
241 std::string Path = getHost().getDriver().Dir;
242 Path += "/../libexec";
243 getProgramPaths().push_back(Path);
244
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000245 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
246 getProgramPaths().push_back(getHost().getDriver().Dir);
247}
248
249void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
250 ArgStringList &CmdArgs) const {
251 // The Clang toolchain uses explicit paths for internal libraries.
252}
253
254void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
255 ArgStringList &CmdArgs) const {
256 // Check for static linking.
257 if (Args.hasArg(options::OPT_static)) {
258 // FIXME: We need to have compiler-rt available (perhaps as
259 // libclang_static.a) to link against.
260 return;
261 }
262
263 // Reject -static-libgcc for now, we can deal with this when and if someone
264 // cares. This is useful in situations where someone wants to statically link
265 // something like libstdc++, and needs its runtime support routines.
266 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
267 getHost().getDriver().Diag(clang::diag::err_drv_unsupported_opt)
268 << A->getAsString(Args);
269 return;
270 }
271
272 // Otherwise link libSystem, which should have the support routines.
273 //
274 // FIXME: This is only true for 10.6 and beyond. Legacy support isn't
275 // critical, but it should work... we should just link in the static
276 // compiler-rt library.
277 CmdArgs.push_back("-lSystem");
278}
279
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000280void Darwin::getMacosxVersionMin(const ArgList &Args,
281 unsigned (&Res)[3]) const {
282 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
283 bool HadExtra;
284 if (!Driver::GetReleaseVersion(A->getValue(Args), Res[0], Res[1], Res[2],
285 HadExtra) ||
286 HadExtra) {
287 const Driver &D = getHost().getDriver();
288 D.Diag(clang::diag::err_drv_invalid_version_number)
289 << A->getAsString(Args);
290 }
291 } else
292 return getMacosxVersion(Res);
293}
294
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000295DerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
296 const char *BoundArch) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000297 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000298 const OptTable &Opts = getHost().getDriver().getOpts();
299
300 // FIXME: We really want to get out of the tool chain level argument
301 // translation business, as it makes the driver functionality much
302 // more opaque. For now, we follow gcc closely solely for the
303 // purpose of easily achieving feature parity & testability. Once we
304 // have something that works, we should reevaluate each translation
Mike Stump1eb44332009-09-09 15:08:12 +0000305 // and try to push it down into tool specific logic.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000306
Mike Stump1eb44332009-09-09 15:08:12 +0000307 Arg *OSXVersion =
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000308 Args.getLastArgNoClaim(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000309 Arg *iPhoneVersion =
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000310 Args.getLastArgNoClaim(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000311 if (OSXVersion && iPhoneVersion) {
312 getHost().getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
313 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000314 << iPhoneVersion->getAsString(Args);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000315 } else if (!OSXVersion && !iPhoneVersion) {
316 // Chose the default version based on the arch.
317 //
Daniel Dunbar30392de2009-09-04 18:35:21 +0000318 // FIXME: Are there iPhone overrides for this?
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000319
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000320 if (!isIPhoneOS()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000321 // Look for MACOSX_DEPLOYMENT_TARGET, otherwise use the version
322 // from the host.
323 const char *Version = ::getenv("MACOSX_DEPLOYMENT_TARGET");
324 if (!Version)
325 Version = MacosxVersionMin.c_str();
326 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
327 DAL->append(DAL->MakeJoinedArg(0, O, Version));
328 } else {
329 const char *Version = IPhoneOSVersionMin.c_str();
330 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
331 DAL->append(DAL->MakeJoinedArg(0, O, Version));
332 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000335 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
336 Arg *A = *it;
337
338 if (A->getOption().matches(options::OPT_Xarch__)) {
339 // FIXME: Canonicalize name.
340 if (getArchName() != A->getValue(Args, 0))
341 continue;
342
343 // FIXME: The arg is leaked here, and we should have a nicer
344 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000345 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000346 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000348 // If the argument parsing failed or more than one argument was
349 // consumed, the -Xarch_ argument's parameter tried to consume
350 // extra arguments. Emit an error and ignore.
351 //
352 // We also want to disallow any options which would alter the
353 // driver behavior; that isn't going to work in our model. We
354 // use isDriverOption() as an approximation, although things
355 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000356 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000357 XarchArg->getOption().isDriverOption()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000358 getHost().getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000359 << A->getAsString(Args);
360 continue;
361 }
362
Daniel Dunbar478edc22009-03-29 22:29:05 +0000363 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000364 A = XarchArg;
Mike Stump1eb44332009-09-09 15:08:12 +0000365 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000366
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000367 // Sob. These is strictly gcc compatible for the time being. Apple
368 // gcc translates options twice, which means that self-expanding
369 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000370 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000371 default:
372 DAL->append(A);
373 break;
374
375 case options::OPT_mkernel:
376 case options::OPT_fapple_kext:
377 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000378 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
379 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000380 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000382 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000383 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000384 A->getValue(Args)));
385 break;
386
387 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000388 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
389 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000390 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
391 break;
392
393 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000394 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
395 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000396 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
397 break;
398
399 case options::OPT_fterminated_vtables:
400 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000401 DAL->append(DAL->MakeFlagArg(A,
402 Opts.getOption(options::OPT_fapple_kext)));
403 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000404 break;
405
406 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000407 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000408 break;
409
410 case options::OPT_fconstant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000411 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000412 Opts.getOption(options::OPT_mconstant_cfstrings)));
413 break;
414
415 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000416 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000417 Opts.getOption(options::OPT_mno_constant_cfstrings)));
418 break;
419
420 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000421 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000422 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
423 break;
424
425 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000426 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000427 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
428 break;
429
430 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000431 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000432 Opts.getOption(options::OPT_mpascal_strings)));
433 break;
434
435 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000436 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000437 Opts.getOption(options::OPT_mno_pascal_strings)));
438 break;
439 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000440 }
441
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000442 if (getTriple().getArch() == llvm::Triple::x86 ||
443 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000444 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000445 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
446 "core2"));
447
448 // Add the arch options based on the particular spelling of -arch, to match
449 // how the driver driver works.
450 if (BoundArch) {
451 llvm::StringRef Name = BoundArch;
452 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
453 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
454
455 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
456 // which defines the list of which architectures we accept.
457 if (Name == "ppc")
458 ;
459 else if (Name == "ppc601")
460 DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
461 else if (Name == "ppc603")
462 DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
463 else if (Name == "ppc604")
464 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
465 else if (Name == "ppc604e")
466 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
467 else if (Name == "ppc750")
468 DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
469 else if (Name == "ppc7400")
470 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
471 else if (Name == "ppc7450")
472 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
473 else if (Name == "ppc970")
474 DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
475
476 else if (Name == "ppc64")
Daniel Dunbar478edc22009-03-29 22:29:05 +0000477 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000478
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000479 else if (Name == "i386")
480 ;
481 else if (Name == "i486")
482 DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
483 else if (Name == "i586")
484 DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
485 else if (Name == "i686")
486 DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
487 else if (Name == "pentium")
488 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
489 else if (Name == "pentium2")
490 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
491 else if (Name == "pentpro")
492 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
493 else if (Name == "pentIIm3")
494 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
495
496 else if (Name == "x86_64")
497 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
498
499 else if (Name == "arm")
500 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
501 else if (Name == "armv4t")
502 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
503 else if (Name == "armv5")
504 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
505 else if (Name == "xscale")
506 DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
507 else if (Name == "armv6")
508 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
509 else if (Name == "armv7")
510 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
511
512 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000513 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000514 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000515
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000516 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000517}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000518
Daniel Dunbarf3955282009-09-04 18:34:51 +0000519bool Darwin::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000520 return false;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000521}
522
Daniel Dunbarf3955282009-09-04 18:34:51 +0000523bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000524 // FIXME: Gross; we should probably have some separate target
525 // definition, possibly even reusing the one in clang.
526 return getArchName() == "x86_64";
527}
528
Daniel Dunbarf3955282009-09-04 18:34:51 +0000529const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000530 return "pic";
531}
532
Daniel Dunbarf3955282009-09-04 18:34:51 +0000533const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000534 if (getArchName() == "x86_64")
535 return "pic";
536 return 0;
537}
538
Daniel Dunbar39176082009-03-20 00:20:03 +0000539/// Generic_GCC - A tool chain using the 'gcc' command to perform
540/// all subcommands; this relies on gcc translating the majority of
541/// command line options.
542
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000543Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000544 : ToolChain(Host, Triple) {
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000545 std::string Path(getHost().getDriver().Dir);
546 Path += "/../libexec";
547 getProgramPaths().push_back(Path);
548
Mike Stump1eb44332009-09-09 15:08:12 +0000549 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000550}
551
Daniel Dunbar39176082009-03-20 00:20:03 +0000552Generic_GCC::~Generic_GCC() {
553 // Free tool implementations.
554 for (llvm::DenseMap<unsigned, Tool*>::iterator
555 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
556 delete it->second;
557}
558
Mike Stump1eb44332009-09-09 15:08:12 +0000559Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000560 const JobAction &JA) const {
561 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000562 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000563 Key = Action::AnalyzeJobClass;
564 else
565 Key = JA.getKind();
566
567 Tool *&T = Tools[Key];
568 if (!T) {
569 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000570 case Action::InputClass:
571 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000572 assert(0 && "Invalid tool kind.");
573 case Action::PreprocessJobClass:
574 T = new tools::gcc::Preprocess(*this); break;
575 case Action::PrecompileJobClass:
576 T = new tools::gcc::Precompile(*this); break;
577 case Action::AnalyzeJobClass:
578 T = new tools::Clang(*this); break;
579 case Action::CompileJobClass:
580 T = new tools::gcc::Compile(*this); break;
581 case Action::AssembleJobClass:
582 T = new tools::gcc::Assemble(*this); break;
583 case Action::LinkJobClass:
584 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000586 // This is a bit ungeneric, but the only platform using a driver
587 // driver is Darwin.
588 case Action::LipoJobClass:
589 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000590 }
591 }
592
593 return *T;
594}
595
Daniel Dunbarf3955282009-09-04 18:34:51 +0000596bool Generic_GCC::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000597 return true;
Daniel Dunbar39176082009-03-20 00:20:03 +0000598}
599
600bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000601 // FIXME: Gross; we should probably have some separate target
602 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000603 return getArchName() == "x86_64";
604}
605
606const char *Generic_GCC::GetDefaultRelocationModel() const {
607 return "static";
608}
609
610const char *Generic_GCC::GetForcedPicModel() const {
611 return 0;
612}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000613
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000614DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
615 const char *BoundArch) const {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000616 return new DerivedArgList(Args, true);
617}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000618
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000619/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
620
621OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
622 : Generic_GCC(Host, Triple) {
623 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
624 getFilePaths().push_back("/usr/lib");
625}
626
627Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
628 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000629 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000630 Key = Action::AnalyzeJobClass;
631 else
632 Key = JA.getKind();
633
634 Tool *&T = Tools[Key];
635 if (!T) {
636 switch (Key) {
637 case Action::AssembleJobClass:
638 T = new tools::openbsd::Assemble(*this); break;
639 case Action::LinkJobClass:
640 T = new tools::openbsd::Link(*this); break;
641 default:
642 T = &Generic_GCC::SelectTool(C, JA);
643 }
644 }
645
646 return *T;
647}
648
Daniel Dunbar75358d22009-03-30 21:06:03 +0000649/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
650
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000651FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
652 : Generic_GCC(Host, Triple) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000653 if (Lib32) {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000654 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000655 getFilePaths().push_back("/usr/lib32");
656 } else {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000657 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000658 getFilePaths().push_back("/usr/lib");
659 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000660}
661
662Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
663 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000664 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000665 Key = Action::AnalyzeJobClass;
666 else
667 Key = JA.getKind();
668
669 Tool *&T = Tools[Key];
670 if (!T) {
671 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000672 case Action::AssembleJobClass:
673 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000674 case Action::LinkJobClass:
675 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000676 default:
677 T = &Generic_GCC::SelectTool(C, JA);
678 }
679 }
680
681 return *T;
682}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000683
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000684/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
685
686AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
687 : Generic_GCC(Host, Triple) {
688
689 // Path mangling to find libexec
690 std::string Path(getHost().getDriver().Dir);
691
692 Path += "/../libexec";
693 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000694 getProgramPaths().push_back(getHost().getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000695
696 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
697 getFilePaths().push_back("/usr/lib");
698 getFilePaths().push_back("/usr/sfw/lib");
699 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000700 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000701
702}
703
704Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
705 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000706 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000707 Key = Action::AnalyzeJobClass;
708 else
709 Key = JA.getKind();
710
711 Tool *&T = Tools[Key];
712 if (!T) {
713 switch (Key) {
714 case Action::AssembleJobClass:
715 T = new tools::auroraux::Assemble(*this); break;
716 case Action::LinkJobClass:
717 T = new tools::auroraux::Link(*this); break;
718 default:
719 T = &Generic_GCC::SelectTool(C, JA);
720 }
721 }
722
723 return *T;
724}
725
726
Eli Friedman6b3454a2009-05-26 07:52:18 +0000727/// Linux toolchain (very bare-bones at the moment).
728
729Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
730 : Generic_GCC(Host, Triple) {
731 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib/clang/1.0/");
732 getFilePaths().push_back("/lib/");
733 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +0000734
735 // Depending on the Linux distribution, any combination of lib{,32,64} is
736 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
737 // openSUSE uses lib and lib64 for the same purpose.
738 getFilePaths().push_back("/lib32/");
739 getFilePaths().push_back("/usr/lib32/");
740 getFilePaths().push_back("/lib64/");
741 getFilePaths().push_back("/usr/lib64/");
742
Eli Friedman6b3454a2009-05-26 07:52:18 +0000743 // FIXME: Figure out some way to get gcc's libdir
744 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
745 // crtbegin.o/crtend.o/etc., and want static versions of various
746 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
747 // get away with using shared versions in /usr/lib, though.
748 // We could fall back to the approach we used for includes (a massive
749 // list), but that's messy at best.
750}
751
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000752/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
753
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000754DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
755 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000756
757 // Path mangling to find libexec
758 std::string Path(getHost().getDriver().Dir);
759
760 Path += "/../libexec";
761 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000762 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000763
764 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
765 getFilePaths().push_back("/usr/lib");
766 getFilePaths().push_back("/usr/lib/gcc41");
767}
768
769Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
770 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000771 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000772 Key = Action::AnalyzeJobClass;
773 else
774 Key = JA.getKind();
775
776 Tool *&T = Tools[Key];
777 if (!T) {
778 switch (Key) {
779 case Action::AssembleJobClass:
780 T = new tools::dragonfly::Assemble(*this); break;
781 case Action::LinkJobClass:
782 T = new tools::dragonfly::Link(*this); break;
783 default:
784 T = &Generic_GCC::SelectTool(C, JA);
785 }
786 }
787
788 return *T;
789}