blob: a05d85aa1e09332a7ab5237134febec22c45fc1f [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") {
Daniel Dunbaree788e72009-12-21 18:54:17 +000091 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000092 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 Dunbaree788e72009-12-21 18:54:17 +0000103 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000104 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
Daniel Dunbaree788e72009-12-21 18:54:17 +0000112 Path = getDriver().Dir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000113 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 Dunbaree788e72009-12-21 18:54:17 +0000121 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000122}
123
Daniel Dunbarf3955282009-09-04 18:34:51 +0000124Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000125 // Free tool implementations.
126 for (llvm::DenseMap<unsigned, Tool*>::iterator
127 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
128 delete it->second;
129}
130
Daniel Dunbarf3955282009-09-04 18:34:51 +0000131Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000132 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000133 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000134 Key = Action::AnalyzeJobClass;
135 else
136 Key = JA.getKind();
137
138 Tool *&T = Tools[Key];
139 if (!T) {
140 switch (Key) {
141 case Action::InputClass:
142 case Action::BindArchClass:
143 assert(0 && "Invalid tool kind.");
144 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000145 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000146 case Action::AnalyzeJobClass:
147 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000148 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000149 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000150 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000151 case Action::AssembleJobClass:
Daniel Dunbar8cac5f72009-03-20 16:06:39 +0000152 T = new tools::darwin::Assemble(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000153 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000154 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000155 case Action::LipoJobClass:
156 T = new tools::darwin::Lipo(*this); break;
157 }
158 }
159
160 return *T;
161}
162
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000163void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
164 ArgStringList &CmdArgs) const {
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000165 // FIXME: Derive these correctly.
166 if (getArchName() == "x86_64") {
167 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
168 "/x86_64"));
169 // Intentionally duplicated for (temporary) gcc bug compatibility.
170 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
171 "/x86_64"));
172 }
173 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
174 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
175 // Intentionally duplicated for (temporary) gcc bug compatibility.
176 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
177 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
178 "/../../../" + ToolChainDir));
179 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
180 "/../../.."));
181}
182
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000183void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
184 ArgStringList &CmdArgs) const {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000185 unsigned MacosxVersionMin[3];
186 getMacosxVersionMin(Args, MacosxVersionMin);
187
188 // Derived from libgcc and lib specs but refactored.
189 if (Args.hasArg(options::OPT_static)) {
190 CmdArgs.push_back("-lgcc_static");
191 } else {
192 if (Args.hasArg(options::OPT_static_libgcc)) {
193 CmdArgs.push_back("-lgcc_eh");
194 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
195 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000196 if (isIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000197 CmdArgs.push_back("-lgcc_s.1");
198 } else {
199 CmdArgs.push_back("-lgcc_s.10.5");
200 }
201 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
Daniel Dunbar8a0d94d2010-01-10 00:46:10 +0000202 Args.hasFlag(options::OPT_fexceptions,
203 options::OPT_fno_exceptions) ||
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000204 Args.hasArg(options::OPT_fgnu_runtime)) {
205 // FIXME: This is probably broken on 10.3?
206 if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
207 CmdArgs.push_back("-lgcc_s.10.4");
208 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
209 CmdArgs.push_back("-lgcc_s.10.5");
210 } else {
211 if (isMacosxVersionLT(MacosxVersionMin, 10, 3, 9))
212 ; // Do nothing.
213 else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
214 CmdArgs.push_back("-lgcc_s.10.4");
215 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
216 CmdArgs.push_back("-lgcc_s.10.5");
217 }
218
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000219 if (isIPhoneOS() || isMacosxVersionLT(MacosxVersionMin, 10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000220 CmdArgs.push_back("-lgcc");
221 CmdArgs.push_back("-lSystem");
222 } else {
223 CmdArgs.push_back("-lSystem");
224 CmdArgs.push_back("-lgcc");
225 }
226 }
227}
228
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000229DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
230 const unsigned (&DarwinVersion)[3],
231 bool IsIPhoneOS)
232 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
233{
234 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
Daniel Dunbaree788e72009-12-21 18:54:17 +0000235 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000236}
237
238void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
239 ArgStringList &CmdArgs) const {
240 // The Clang toolchain uses explicit paths for internal libraries.
241}
242
243void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
244 ArgStringList &CmdArgs) const {
245 // Check for static linking.
246 if (Args.hasArg(options::OPT_static)) {
247 // FIXME: We need to have compiler-rt available (perhaps as
248 // libclang_static.a) to link against.
249 return;
250 }
251
252 // Reject -static-libgcc for now, we can deal with this when and if someone
253 // cares. This is useful in situations where someone wants to statically link
254 // something like libstdc++, and needs its runtime support routines.
255 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000256 getDriver().Diag(clang::diag::err_drv_unsupported_opt)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000257 << A->getAsString(Args);
258 return;
259 }
260
261 // Otherwise link libSystem, which should have the support routines.
262 //
263 // FIXME: This is only true for 10.6 and beyond. Legacy support isn't
264 // critical, but it should work... we should just link in the static
265 // compiler-rt library.
266 CmdArgs.push_back("-lSystem");
267}
268
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000269void Darwin::getMacosxVersionMin(const ArgList &Args,
270 unsigned (&Res)[3]) const {
271 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
272 bool HadExtra;
273 if (!Driver::GetReleaseVersion(A->getValue(Args), Res[0], Res[1], Res[2],
274 HadExtra) ||
275 HadExtra) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000276 const Driver &D = getDriver();
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000277 D.Diag(clang::diag::err_drv_invalid_version_number)
278 << A->getAsString(Args);
279 }
280 } else
281 return getMacosxVersion(Res);
282}
283
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000284DerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
285 const char *BoundArch) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000286 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbaree788e72009-12-21 18:54:17 +0000287 const OptTable &Opts = getDriver().getOpts();
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000288
289 // FIXME: We really want to get out of the tool chain level argument
290 // translation business, as it makes the driver functionality much
291 // more opaque. For now, we follow gcc closely solely for the
292 // purpose of easily achieving feature parity & testability. Once we
293 // have something that works, we should reevaluate each translation
Mike Stump1eb44332009-09-09 15:08:12 +0000294 // and try to push it down into tool specific logic.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000295
Mike Stump1eb44332009-09-09 15:08:12 +0000296 Arg *OSXVersion =
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000297 Args.getLastArgNoClaim(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000298 Arg *iPhoneVersion =
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000299 Args.getLastArgNoClaim(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000300 if (OSXVersion && iPhoneVersion) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000301 getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000302 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000303 << iPhoneVersion->getAsString(Args);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000304 } else if (!OSXVersion && !iPhoneVersion) {
305 // Chose the default version based on the arch.
306 //
Daniel Dunbar30392de2009-09-04 18:35:21 +0000307 // FIXME: Are there iPhone overrides for this?
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000308
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000309 if (!isIPhoneOS()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000310 // Look for MACOSX_DEPLOYMENT_TARGET, otherwise use the version
311 // from the host.
312 const char *Version = ::getenv("MACOSX_DEPLOYMENT_TARGET");
313 if (!Version)
314 Version = MacosxVersionMin.c_str();
315 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
316 DAL->append(DAL->MakeJoinedArg(0, O, Version));
317 } else {
318 const char *Version = IPhoneOSVersionMin.c_str();
319 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
320 DAL->append(DAL->MakeJoinedArg(0, O, Version));
321 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000324 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
325 Arg *A = *it;
326
327 if (A->getOption().matches(options::OPT_Xarch__)) {
328 // FIXME: Canonicalize name.
329 if (getArchName() != A->getValue(Args, 0))
330 continue;
331
332 // FIXME: The arg is leaked here, and we should have a nicer
333 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000334 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000335 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000337 // If the argument parsing failed or more than one argument was
338 // consumed, the -Xarch_ argument's parameter tried to consume
339 // extra arguments. Emit an error and ignore.
340 //
341 // We also want to disallow any options which would alter the
342 // driver behavior; that isn't going to work in our model. We
343 // use isDriverOption() as an approximation, although things
344 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000345 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000346 XarchArg->getOption().isDriverOption()) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000347 getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000348 << A->getAsString(Args);
349 continue;
350 }
351
Daniel Dunbar478edc22009-03-29 22:29:05 +0000352 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000353 A = XarchArg;
Mike Stump1eb44332009-09-09 15:08:12 +0000354 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000355
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000356 // Sob. These is strictly gcc compatible for the time being. Apple
357 // gcc translates options twice, which means that self-expanding
358 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000359 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000360 default:
361 DAL->append(A);
362 break;
363
364 case options::OPT_mkernel:
365 case options::OPT_fapple_kext:
366 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000367 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
368 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000369 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000371 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000372 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000373 A->getValue(Args)));
374 break;
375
376 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000377 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
378 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000379 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
380 break;
381
382 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000383 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
384 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000385 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
386 break;
387
388 case options::OPT_fterminated_vtables:
389 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000390 DAL->append(DAL->MakeFlagArg(A,
391 Opts.getOption(options::OPT_fapple_kext)));
392 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000393 break;
394
395 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000396 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000397 break;
398
399 case options::OPT_fconstant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000400 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000401 Opts.getOption(options::OPT_mconstant_cfstrings)));
402 break;
403
404 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000405 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000406 Opts.getOption(options::OPT_mno_constant_cfstrings)));
407 break;
408
409 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000410 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000411 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
412 break;
413
414 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000415 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000416 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
417 break;
418
419 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000420 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000421 Opts.getOption(options::OPT_mpascal_strings)));
422 break;
423
424 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000425 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000426 Opts.getOption(options::OPT_mno_pascal_strings)));
427 break;
428 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000429 }
430
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000431 if (getTriple().getArch() == llvm::Triple::x86 ||
432 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000433 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000434 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
435 "core2"));
436
437 // Add the arch options based on the particular spelling of -arch, to match
438 // how the driver driver works.
439 if (BoundArch) {
440 llvm::StringRef Name = BoundArch;
441 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
442 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
443
444 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
445 // which defines the list of which architectures we accept.
446 if (Name == "ppc")
447 ;
448 else if (Name == "ppc601")
449 DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
450 else if (Name == "ppc603")
451 DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
452 else if (Name == "ppc604")
453 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
454 else if (Name == "ppc604e")
455 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
456 else if (Name == "ppc750")
457 DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
458 else if (Name == "ppc7400")
459 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
460 else if (Name == "ppc7450")
461 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
462 else if (Name == "ppc970")
463 DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
464
465 else if (Name == "ppc64")
Daniel Dunbar478edc22009-03-29 22:29:05 +0000466 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000467
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000468 else if (Name == "i386")
469 ;
470 else if (Name == "i486")
471 DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
472 else if (Name == "i586")
473 DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
474 else if (Name == "i686")
475 DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
476 else if (Name == "pentium")
477 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
478 else if (Name == "pentium2")
479 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
480 else if (Name == "pentpro")
481 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
482 else if (Name == "pentIIm3")
483 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
484
485 else if (Name == "x86_64")
486 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
487
488 else if (Name == "arm")
489 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
490 else if (Name == "armv4t")
491 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
492 else if (Name == "armv5")
493 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
494 else if (Name == "xscale")
495 DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
496 else if (Name == "armv6")
497 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
498 else if (Name == "armv7")
499 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
500
501 else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000502 llvm_unreachable("invalid Darwin arch");
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000503 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000504
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000505 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000506}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000507
Daniel Dunbarf3955282009-09-04 18:34:51 +0000508bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000509 // FIXME: Gross; we should probably have some separate target
510 // definition, possibly even reusing the one in clang.
511 return getArchName() == "x86_64";
512}
513
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000514bool Darwin::UseDwarfDebugFlags() const {
515 if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
516 return S[0] != '\0';
517 return false;
518}
519
Daniel Dunbarf3955282009-09-04 18:34:51 +0000520const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000521 return "pic";
522}
523
Daniel Dunbarf3955282009-09-04 18:34:51 +0000524const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000525 if (getArchName() == "x86_64")
526 return "pic";
527 return 0;
528}
529
Daniel Dunbar39176082009-03-20 00:20:03 +0000530/// Generic_GCC - A tool chain using the 'gcc' command to perform
531/// all subcommands; this relies on gcc translating the majority of
532/// command line options.
533
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000534Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000535 : ToolChain(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000536 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000537}
538
Daniel Dunbar39176082009-03-20 00:20:03 +0000539Generic_GCC::~Generic_GCC() {
540 // Free tool implementations.
541 for (llvm::DenseMap<unsigned, Tool*>::iterator
542 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
543 delete it->second;
544}
545
Mike Stump1eb44332009-09-09 15:08:12 +0000546Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000547 const JobAction &JA) const {
548 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000549 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000550 Key = Action::AnalyzeJobClass;
551 else
552 Key = JA.getKind();
553
554 Tool *&T = Tools[Key];
555 if (!T) {
556 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000557 case Action::InputClass:
558 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000559 assert(0 && "Invalid tool kind.");
560 case Action::PreprocessJobClass:
561 T = new tools::gcc::Preprocess(*this); break;
562 case Action::PrecompileJobClass:
563 T = new tools::gcc::Precompile(*this); break;
564 case Action::AnalyzeJobClass:
565 T = new tools::Clang(*this); break;
566 case Action::CompileJobClass:
567 T = new tools::gcc::Compile(*this); break;
568 case Action::AssembleJobClass:
569 T = new tools::gcc::Assemble(*this); break;
570 case Action::LinkJobClass:
571 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000573 // This is a bit ungeneric, but the only platform using a driver
574 // driver is Darwin.
575 case Action::LipoJobClass:
576 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000577 }
578 }
579
580 return *T;
581}
582
Daniel Dunbar39176082009-03-20 00:20:03 +0000583bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000584 // FIXME: Gross; we should probably have some separate target
585 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000586 return getArchName() == "x86_64";
587}
588
589const char *Generic_GCC::GetDefaultRelocationModel() const {
590 return "static";
591}
592
593const char *Generic_GCC::GetForcedPicModel() const {
594 return 0;
595}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000596
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000597DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
598 const char *BoundArch) const {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000599 return new DerivedArgList(Args, true);
600}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000601
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000602/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
603
604OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
605 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000606 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000607 getFilePaths().push_back("/usr/lib");
608}
609
610Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
611 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000612 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000613 Key = Action::AnalyzeJobClass;
614 else
615 Key = JA.getKind();
616
617 Tool *&T = Tools[Key];
618 if (!T) {
619 switch (Key) {
620 case Action::AssembleJobClass:
621 T = new tools::openbsd::Assemble(*this); break;
622 case Action::LinkJobClass:
623 T = new tools::openbsd::Link(*this); break;
624 default:
625 T = &Generic_GCC::SelectTool(C, JA);
626 }
627 }
628
629 return *T;
630}
631
Daniel Dunbar75358d22009-03-30 21:06:03 +0000632/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
633
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000634FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
635 : Generic_GCC(Host, Triple) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000636 if (Lib32) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000637 getFilePaths().push_back(getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000638 getFilePaths().push_back("/usr/lib32");
639 } else {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000640 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000641 getFilePaths().push_back("/usr/lib");
642 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000643}
644
645Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
646 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000647 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000648 Key = Action::AnalyzeJobClass;
649 else
650 Key = JA.getKind();
651
652 Tool *&T = Tools[Key];
653 if (!T) {
654 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000655 case Action::AssembleJobClass:
656 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000657 case Action::LinkJobClass:
658 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000659 default:
660 T = &Generic_GCC::SelectTool(C, JA);
661 }
662 }
663
664 return *T;
665}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000666
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000667/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
668
669AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
670 : Generic_GCC(Host, Triple) {
671
Daniel Dunbaree788e72009-12-21 18:54:17 +0000672 getProgramPaths().push_back(getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000673
Daniel Dunbaree788e72009-12-21 18:54:17 +0000674 getFilePaths().push_back(getDriver().Dir + "/../lib");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000675 getFilePaths().push_back("/usr/lib");
676 getFilePaths().push_back("/usr/sfw/lib");
677 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000678 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000679
680}
681
682Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
683 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000684 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000685 Key = Action::AnalyzeJobClass;
686 else
687 Key = JA.getKind();
688
689 Tool *&T = Tools[Key];
690 if (!T) {
691 switch (Key) {
692 case Action::AssembleJobClass:
693 T = new tools::auroraux::Assemble(*this); break;
694 case Action::LinkJobClass:
695 T = new tools::auroraux::Link(*this); break;
696 default:
697 T = &Generic_GCC::SelectTool(C, JA);
698 }
699 }
700
701 return *T;
702}
703
704
Eli Friedman6b3454a2009-05-26 07:52:18 +0000705/// Linux toolchain (very bare-bones at the moment).
706
707Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
708 : Generic_GCC(Host, Triple) {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000709 getFilePaths().push_back(getDriver().Dir + "/../lib/clang/1.0/");
Eli Friedman6b3454a2009-05-26 07:52:18 +0000710 getFilePaths().push_back("/lib/");
711 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +0000712
713 // Depending on the Linux distribution, any combination of lib{,32,64} is
714 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
715 // openSUSE uses lib and lib64 for the same purpose.
716 getFilePaths().push_back("/lib32/");
717 getFilePaths().push_back("/usr/lib32/");
718 getFilePaths().push_back("/lib64/");
719 getFilePaths().push_back("/usr/lib64/");
720
Eli Friedman6b3454a2009-05-26 07:52:18 +0000721 // FIXME: Figure out some way to get gcc's libdir
722 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
723 // crtbegin.o/crtend.o/etc., and want static versions of various
724 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
725 // get away with using shared versions in /usr/lib, though.
726 // We could fall back to the approach we used for includes (a massive
727 // list), but that's messy at best.
728}
729
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000730/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
731
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000732DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
733 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000734
735 // Path mangling to find libexec
Daniel Dunbaree788e72009-12-21 18:54:17 +0000736 getProgramPaths().push_back(getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000737
Daniel Dunbaree788e72009-12-21 18:54:17 +0000738 getFilePaths().push_back(getDriver().Dir + "/../lib");
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000739 getFilePaths().push_back("/usr/lib");
740 getFilePaths().push_back("/usr/lib/gcc41");
741}
742
743Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
744 Action::ActionClass Key;
Daniel Dunbaree788e72009-12-21 18:54:17 +0000745 if (getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000746 Key = Action::AnalyzeJobClass;
747 else
748 Key = JA.getKind();
749
750 Tool *&T = Tools[Key];
751 if (!T) {
752 switch (Key) {
753 case Action::AssembleJobClass:
754 T = new tools::dragonfly::Assemble(*this); break;
755 case Action::LinkJobClass:
756 T = new tools::dragonfly::Link(*this); break;
757 default:
758 T = &Generic_GCC::SelectTool(C, JA);
759 }
760 }
761
762 return *T;
763}