blob: a9a2d0fbc3c5f6e7a25e28311eb69f3457a1130b [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 Dunbarc50b00d2009-03-23 16:15:50 +000019
20#include "llvm/ADT/StringExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000021#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000022#include "llvm/Support/raw_ostream.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000023#include "llvm/System/Path.h"
24
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000025#include <cstdlib> // ::getenv
26
Daniel Dunbar39176082009-03-20 00:20:03 +000027using namespace clang::driver;
28using namespace clang::driver::toolchains;
29
Daniel Dunbarf3955282009-09-04 18:34:51 +000030/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000031
Daniel Dunbarf3955282009-09-04 18:34:51 +000032Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple,
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000033 const unsigned (&_DarwinVersion)[3], bool _IsIPhoneOS)
34 : ToolChain(Host, Triple),
35 IsIPhoneOS(_IsIPhoneOS)
36{
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000037 DarwinVersion[0] = _DarwinVersion[0];
38 DarwinVersion[1] = _DarwinVersion[1];
39 DarwinVersion[2] = _DarwinVersion[2];
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000040
Daniel Dunbar02633b52009-03-26 16:23:12 +000041 llvm::raw_string_ostream(MacosxVersionMin)
Benjamin Kramer87f9fd92009-10-21 21:05:07 +000042 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
43 << DarwinVersion[1];
Daniel Dunbar02633b52009-03-26 16:23:12 +000044
Daniel Dunbar30392de2009-09-04 18:35:21 +000045 // FIXME: Lift default up.
46 IPhoneOSVersionMin = "3.0";
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000047}
48
49DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
50 const unsigned (&DarwinVersion)[3],
Ted Kremenek55bac532009-10-07 03:21:11 +000051 const unsigned (&_GCCVersion)[3], bool IsIPhoneOS)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000052 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
53{
54 GCCVersion[0] = _GCCVersion[0];
55 GCCVersion[1] = _GCCVersion[1];
56 GCCVersion[2] = _GCCVersion[2];
57
58 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000059 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +000060 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000061 ToolChainDir += "/";
62 ToolChainDir += llvm::utostr(GCCVersion[0]);
63 ToolChainDir += '.';
64 ToolChainDir += llvm::utostr(GCCVersion[1]);
65 ToolChainDir += '.';
66 ToolChainDir += llvm::utostr(GCCVersion[2]);
67
Daniel Dunbar74782b02009-10-20 19:25:43 +000068 // Try the next major version if that tool chain dir is invalid.
Daniel Dunbar080fb192009-10-22 00:12:00 +000069 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
70 if (!llvm::sys::Path(Tmp).exists()) {
Daniel Dunbar74782b02009-10-20 19:25:43 +000071 std::string Next = "i686-apple-darwin";
72 Next += llvm::utostr(DarwinVersion[0] + 1);
73 Next += "/";
74 Next += llvm::utostr(GCCVersion[0]);
75 Next += '.';
76 Next += llvm::utostr(GCCVersion[1]);
77 Next += '.';
78 Next += llvm::utostr(GCCVersion[2]);
79
80 // Use that if it exists, otherwise hope the user isn't linking.
81 //
82 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +000083 Tmp = "/usr/lib/gcc/" + Next;
84 if (llvm::sys::Path(Tmp).exists())
Daniel Dunbar74782b02009-10-20 19:25:43 +000085 ToolChainDir = Next;
86 }
87
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000088 std::string Path;
89 if (getArchName() == "x86_64") {
90 Path = getHost().getDriver().Dir;
91 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000092 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000093 Path += "/x86_64";
94 getFilePaths().push_back(Path);
95
96 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000097 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000098 Path += "/x86_64";
99 getFilePaths().push_back(Path);
100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000102 Path = getHost().getDriver().Dir;
103 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000104 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000105 getFilePaths().push_back(Path);
106
107 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000108 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000109 getFilePaths().push_back(Path);
110
111 Path = getHost().getDriver().Dir;
112 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000113 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000114 getProgramPaths().push_back(Path);
115
116 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000117 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000118 getProgramPaths().push_back(Path);
119
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000120 Path = getHost().getDriver().Dir;
121 Path += "/../libexec";
122 getProgramPaths().push_back(Path);
123
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000124 getProgramPaths().push_back(getHost().getDriver().Dir);
125}
126
Daniel Dunbarf3955282009-09-04 18:34:51 +0000127Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000128 // Free tool implementations.
129 for (llvm::DenseMap<unsigned, Tool*>::iterator
130 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
131 delete it->second;
132}
133
Daniel Dunbarf3955282009-09-04 18:34:51 +0000134Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000135 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000136 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000137 Key = Action::AnalyzeJobClass;
138 else
139 Key = JA.getKind();
140
141 Tool *&T = Tools[Key];
142 if (!T) {
143 switch (Key) {
144 case Action::InputClass:
145 case Action::BindArchClass:
146 assert(0 && "Invalid tool kind.");
147 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000148 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000149 case Action::AnalyzeJobClass:
150 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000151 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000152 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000153 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000154 case Action::AssembleJobClass:
Daniel Dunbar8cac5f72009-03-20 16:06:39 +0000155 T = new tools::darwin::Assemble(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000156 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000157 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000158 case Action::LipoJobClass:
159 T = new tools::darwin::Lipo(*this); break;
160 }
161 }
162
163 return *T;
164}
165
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000166void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
167 ArgStringList &CmdArgs) const {
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000168 // FIXME: Derive these correctly.
169 if (getArchName() == "x86_64") {
170 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
171 "/x86_64"));
172 // Intentionally duplicated for (temporary) gcc bug compatibility.
173 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
174 "/x86_64"));
175 }
176 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
177 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
178 // Intentionally duplicated for (temporary) gcc bug compatibility.
179 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
180 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
181 "/../../../" + ToolChainDir));
182 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
183 "/../../.."));
184}
185
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000186void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
187 ArgStringList &CmdArgs) const {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000188 unsigned MacosxVersionMin[3];
189 getMacosxVersionMin(Args, MacosxVersionMin);
190
191 // Derived from libgcc and lib specs but refactored.
192 if (Args.hasArg(options::OPT_static)) {
193 CmdArgs.push_back("-lgcc_static");
194 } else {
195 if (Args.hasArg(options::OPT_static_libgcc)) {
196 CmdArgs.push_back("-lgcc_eh");
197 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
198 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000199 if (isIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000200 CmdArgs.push_back("-lgcc_s.1");
201 } else {
202 CmdArgs.push_back("-lgcc_s.10.5");
203 }
204 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
205 // FIXME: -fexceptions -fno-exceptions means no exceptions
206 Args.hasArg(options::OPT_fexceptions) ||
207 Args.hasArg(options::OPT_fgnu_runtime)) {
208 // FIXME: This is probably broken on 10.3?
209 if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
210 CmdArgs.push_back("-lgcc_s.10.4");
211 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
212 CmdArgs.push_back("-lgcc_s.10.5");
213 } else {
214 if (isMacosxVersionLT(MacosxVersionMin, 10, 3, 9))
215 ; // Do nothing.
216 else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
217 CmdArgs.push_back("-lgcc_s.10.4");
218 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
219 CmdArgs.push_back("-lgcc_s.10.5");
220 }
221
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000222 if (isIPhoneOS() || isMacosxVersionLT(MacosxVersionMin, 10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000223 CmdArgs.push_back("-lgcc");
224 CmdArgs.push_back("-lSystem");
225 } else {
226 CmdArgs.push_back("-lSystem");
227 CmdArgs.push_back("-lgcc");
228 }
229 }
230}
231
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000232DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
233 const unsigned (&DarwinVersion)[3],
234 bool IsIPhoneOS)
235 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
236{
Daniel Dunbara92ba272009-09-29 18:52:10 +0000237 // Add the relative libexec dir (for clang-cc).
238 //
239 // FIXME: We should sink clang-cc into libexec/clang/<version>/.
240 std::string Path = getHost().getDriver().Dir;
241 Path += "/../libexec";
242 getProgramPaths().push_back(Path);
243
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000244 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
245 getProgramPaths().push_back(getHost().getDriver().Dir);
246}
247
248void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
249 ArgStringList &CmdArgs) const {
250 // The Clang toolchain uses explicit paths for internal libraries.
251}
252
253void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
254 ArgStringList &CmdArgs) const {
255 // Check for static linking.
256 if (Args.hasArg(options::OPT_static)) {
257 // FIXME: We need to have compiler-rt available (perhaps as
258 // libclang_static.a) to link against.
259 return;
260 }
261
262 // Reject -static-libgcc for now, we can deal with this when and if someone
263 // cares. This is useful in situations where someone wants to statically link
264 // something like libstdc++, and needs its runtime support routines.
265 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
266 getHost().getDriver().Diag(clang::diag::err_drv_unsupported_opt)
267 << A->getAsString(Args);
268 return;
269 }
270
271 // Otherwise link libSystem, which should have the support routines.
272 //
273 // FIXME: This is only true for 10.6 and beyond. Legacy support isn't
274 // critical, but it should work... we should just link in the static
275 // compiler-rt library.
276 CmdArgs.push_back("-lSystem");
277}
278
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000279void Darwin::getMacosxVersionMin(const ArgList &Args,
280 unsigned (&Res)[3]) const {
281 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
282 bool HadExtra;
283 if (!Driver::GetReleaseVersion(A->getValue(Args), Res[0], Res[1], Res[2],
284 HadExtra) ||
285 HadExtra) {
286 const Driver &D = getHost().getDriver();
287 D.Diag(clang::diag::err_drv_invalid_version_number)
288 << A->getAsString(Args);
289 }
290 } else
291 return getMacosxVersion(Res);
292}
293
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000294DerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
295 const char *BoundArch) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000296 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000297 const OptTable &Opts = getHost().getDriver().getOpts();
298
299 // FIXME: We really want to get out of the tool chain level argument
300 // translation business, as it makes the driver functionality much
301 // more opaque. For now, we follow gcc closely solely for the
302 // purpose of easily achieving feature parity & testability. Once we
303 // have something that works, we should reevaluate each translation
Mike Stump1eb44332009-09-09 15:08:12 +0000304 // and try to push it down into tool specific logic.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000305
Mike Stump1eb44332009-09-09 15:08:12 +0000306 Arg *OSXVersion =
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000307 Args.getLastArgNoClaim(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000308 Arg *iPhoneVersion =
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000309 Args.getLastArgNoClaim(options::OPT_miphoneos_version_min_EQ);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000310 if (OSXVersion && iPhoneVersion) {
311 getHost().getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
312 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000313 << iPhoneVersion->getAsString(Args);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000314 } else if (!OSXVersion && !iPhoneVersion) {
315 // Chose the default version based on the arch.
316 //
Daniel Dunbar30392de2009-09-04 18:35:21 +0000317 // FIXME: Are there iPhone overrides for this?
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000318
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000319 if (!isIPhoneOS()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000320 // Look for MACOSX_DEPLOYMENT_TARGET, otherwise use the version
321 // from the host.
322 const char *Version = ::getenv("MACOSX_DEPLOYMENT_TARGET");
323 if (!Version)
324 Version = MacosxVersionMin.c_str();
325 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
326 DAL->append(DAL->MakeJoinedArg(0, O, Version));
327 } else {
328 const char *Version = IPhoneOSVersionMin.c_str();
329 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
330 DAL->append(DAL->MakeJoinedArg(0, O, Version));
331 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000334 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
335 Arg *A = *it;
336
337 if (A->getOption().matches(options::OPT_Xarch__)) {
338 // FIXME: Canonicalize name.
339 if (getArchName() != A->getValue(Args, 0))
340 continue;
341
342 // FIXME: The arg is leaked here, and we should have a nicer
343 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000344 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000345 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000347 // If the argument parsing failed or more than one argument was
348 // consumed, the -Xarch_ argument's parameter tried to consume
349 // extra arguments. Emit an error and ignore.
350 //
351 // We also want to disallow any options which would alter the
352 // driver behavior; that isn't going to work in our model. We
353 // use isDriverOption() as an approximation, although things
354 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000355 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000356 XarchArg->getOption().isDriverOption()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000357 getHost().getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000358 << A->getAsString(Args);
359 continue;
360 }
361
Daniel Dunbar478edc22009-03-29 22:29:05 +0000362 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000363 A = XarchArg;
Mike Stump1eb44332009-09-09 15:08:12 +0000364 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000365
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000366 // Sob. These is strictly gcc compatible for the time being. Apple
367 // gcc translates options twice, which means that self-expanding
368 // options add duplicates.
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000369 switch ((options::ID) A->getOption().getID()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000370 default:
371 DAL->append(A);
372 break;
373
374 case options::OPT_mkernel:
375 case options::OPT_fapple_kext:
376 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000377 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
378 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000379 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000381 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000382 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000383 A->getValue(Args)));
384 break;
385
386 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000387 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
388 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000389 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
390 break;
391
392 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000393 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
394 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000395 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
396 break;
397
398 case options::OPT_fterminated_vtables:
399 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000400 DAL->append(DAL->MakeFlagArg(A,
401 Opts.getOption(options::OPT_fapple_kext)));
402 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000403 break;
404
405 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000406 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000407 break;
408
409 case options::OPT_fconstant_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_mconstant_cfstrings)));
412 break;
413
414 case options::OPT_fno_constant_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_constant_cfstrings)));
417 break;
418
419 case options::OPT_Wnonportable_cfstrings:
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_mwarn_nonportable_cfstrings)));
422 break;
423
424 case options::OPT_Wno_nonportable_cfstrings:
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_warn_nonportable_cfstrings)));
427 break;
428
429 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000430 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000431 Opts.getOption(options::OPT_mpascal_strings)));
432 break;
433
434 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000435 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000436 Opts.getOption(options::OPT_mno_pascal_strings)));
437 break;
438 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000439 }
440
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000441 if (getTriple().getArch() == llvm::Triple::x86 ||
442 getTriple().getArch() == llvm::Triple::x86_64)
Daniel Dunbare4bdae72009-11-19 04:00:53 +0000443 if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000444 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
445 "core2"));
446
447 // Add the arch options based on the particular spelling of -arch, to match
448 // how the driver driver works.
449 if (BoundArch) {
450 llvm::StringRef Name = BoundArch;
451 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
452 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
453
454 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
455 // which defines the list of which architectures we accept.
456 if (Name == "ppc")
457 ;
458 else if (Name == "ppc601")
459 DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
460 else if (Name == "ppc603")
461 DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
462 else if (Name == "ppc604")
463 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
464 else if (Name == "ppc604e")
465 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
466 else if (Name == "ppc750")
467 DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
468 else if (Name == "ppc7400")
469 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
470 else if (Name == "ppc7450")
471 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
472 else if (Name == "ppc970")
473 DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
474
475 else if (Name == "ppc64")
Daniel Dunbar478edc22009-03-29 22:29:05 +0000476 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000477
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000478 else if (Name == "i386")
479 ;
480 else if (Name == "i486")
481 DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
482 else if (Name == "i586")
483 DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
484 else if (Name == "i686")
485 DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
486 else if (Name == "pentium")
487 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
488 else if (Name == "pentium2")
489 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
490 else if (Name == "pentpro")
491 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
492 else if (Name == "pentIIm3")
493 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
494
495 else if (Name == "x86_64")
496 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
497
498 else if (Name == "arm")
499 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
500 else if (Name == "armv4t")
501 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
502 else if (Name == "armv5")
503 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
504 else if (Name == "xscale")
505 DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
506 else if (Name == "armv6")
507 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
508 else if (Name == "armv7")
509 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
510
511 else
512 llvm::llvm_unreachable("invalid Darwin arch");
513 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000514
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000515 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000516}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000517
Daniel Dunbarf3955282009-09-04 18:34:51 +0000518bool Darwin::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000519 return false;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000520}
521
Daniel Dunbarf3955282009-09-04 18:34:51 +0000522bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000523 // FIXME: Gross; we should probably have some separate target
524 // definition, possibly even reusing the one in clang.
525 return getArchName() == "x86_64";
526}
527
Daniel Dunbarf3955282009-09-04 18:34:51 +0000528const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000529 return "pic";
530}
531
Daniel Dunbarf3955282009-09-04 18:34:51 +0000532const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000533 if (getArchName() == "x86_64")
534 return "pic";
535 return 0;
536}
537
Daniel Dunbar39176082009-03-20 00:20:03 +0000538/// Generic_GCC - A tool chain using the 'gcc' command to perform
539/// all subcommands; this relies on gcc translating the majority of
540/// command line options.
541
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000542Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000543 : ToolChain(Host, Triple) {
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000544 std::string Path(getHost().getDriver().Dir);
545 Path += "/../libexec";
546 getProgramPaths().push_back(Path);
547
Mike Stump1eb44332009-09-09 15:08:12 +0000548 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000549}
550
Daniel Dunbar39176082009-03-20 00:20:03 +0000551Generic_GCC::~Generic_GCC() {
552 // Free tool implementations.
553 for (llvm::DenseMap<unsigned, Tool*>::iterator
554 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
555 delete it->second;
556}
557
Mike Stump1eb44332009-09-09 15:08:12 +0000558Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000559 const JobAction &JA) const {
560 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000561 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000562 Key = Action::AnalyzeJobClass;
563 else
564 Key = JA.getKind();
565
566 Tool *&T = Tools[Key];
567 if (!T) {
568 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000569 case Action::InputClass:
570 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000571 assert(0 && "Invalid tool kind.");
572 case Action::PreprocessJobClass:
573 T = new tools::gcc::Preprocess(*this); break;
574 case Action::PrecompileJobClass:
575 T = new tools::gcc::Precompile(*this); break;
576 case Action::AnalyzeJobClass:
577 T = new tools::Clang(*this); break;
578 case Action::CompileJobClass:
579 T = new tools::gcc::Compile(*this); break;
580 case Action::AssembleJobClass:
581 T = new tools::gcc::Assemble(*this); break;
582 case Action::LinkJobClass:
583 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000585 // This is a bit ungeneric, but the only platform using a driver
586 // driver is Darwin.
587 case Action::LipoJobClass:
588 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000589 }
590 }
591
592 return *T;
593}
594
Daniel Dunbarf3955282009-09-04 18:34:51 +0000595bool Generic_GCC::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000596 return true;
Daniel Dunbar39176082009-03-20 00:20:03 +0000597}
598
599bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000600 // FIXME: Gross; we should probably have some separate target
601 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000602 return getArchName() == "x86_64";
603}
604
605const char *Generic_GCC::GetDefaultRelocationModel() const {
606 return "static";
607}
608
609const char *Generic_GCC::GetForcedPicModel() const {
610 return 0;
611}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000612
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000613DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
614 const char *BoundArch) const {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000615 return new DerivedArgList(Args, true);
616}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000617
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000618/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
619
620OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
621 : Generic_GCC(Host, Triple) {
622 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
623 getFilePaths().push_back("/usr/lib");
624}
625
626Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
627 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000628 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000629 Key = Action::AnalyzeJobClass;
630 else
631 Key = JA.getKind();
632
633 Tool *&T = Tools[Key];
634 if (!T) {
635 switch (Key) {
636 case Action::AssembleJobClass:
637 T = new tools::openbsd::Assemble(*this); break;
638 case Action::LinkJobClass:
639 T = new tools::openbsd::Link(*this); break;
640 default:
641 T = &Generic_GCC::SelectTool(C, JA);
642 }
643 }
644
645 return *T;
646}
647
Daniel Dunbar75358d22009-03-30 21:06:03 +0000648/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
649
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000650FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
651 : Generic_GCC(Host, Triple) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000652 if (Lib32) {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000653 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000654 getFilePaths().push_back("/usr/lib32");
655 } else {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000656 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000657 getFilePaths().push_back("/usr/lib");
658 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000659}
660
661Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
662 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000663 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000664 Key = Action::AnalyzeJobClass;
665 else
666 Key = JA.getKind();
667
668 Tool *&T = Tools[Key];
669 if (!T) {
670 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000671 case Action::AssembleJobClass:
672 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000673 case Action::LinkJobClass:
674 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000675 default:
676 T = &Generic_GCC::SelectTool(C, JA);
677 }
678 }
679
680 return *T;
681}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000682
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000683/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
684
685AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
686 : Generic_GCC(Host, Triple) {
687
688 // Path mangling to find libexec
689 std::string Path(getHost().getDriver().Dir);
690
691 Path += "/../libexec";
692 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000693 getProgramPaths().push_back(getHost().getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000694
695 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
696 getFilePaths().push_back("/usr/lib");
697 getFilePaths().push_back("/usr/sfw/lib");
698 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000699 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000700
701}
702
703Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
704 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000705 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000706 Key = Action::AnalyzeJobClass;
707 else
708 Key = JA.getKind();
709
710 Tool *&T = Tools[Key];
711 if (!T) {
712 switch (Key) {
713 case Action::AssembleJobClass:
714 T = new tools::auroraux::Assemble(*this); break;
715 case Action::LinkJobClass:
716 T = new tools::auroraux::Link(*this); break;
717 default:
718 T = &Generic_GCC::SelectTool(C, JA);
719 }
720 }
721
722 return *T;
723}
724
725
Eli Friedman6b3454a2009-05-26 07:52:18 +0000726/// Linux toolchain (very bare-bones at the moment).
727
728Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
729 : Generic_GCC(Host, Triple) {
730 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib/clang/1.0/");
731 getFilePaths().push_back("/lib/");
732 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +0000733
734 // Depending on the Linux distribution, any combination of lib{,32,64} is
735 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
736 // openSUSE uses lib and lib64 for the same purpose.
737 getFilePaths().push_back("/lib32/");
738 getFilePaths().push_back("/usr/lib32/");
739 getFilePaths().push_back("/lib64/");
740 getFilePaths().push_back("/usr/lib64/");
741
Eli Friedman6b3454a2009-05-26 07:52:18 +0000742 // FIXME: Figure out some way to get gcc's libdir
743 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
744 // crtbegin.o/crtend.o/etc., and want static versions of various
745 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
746 // get away with using shared versions in /usr/lib, though.
747 // We could fall back to the approach we used for includes (a massive
748 // list), but that's messy at best.
749}
750
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000751/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
752
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000753DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
754 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000755
756 // Path mangling to find libexec
757 std::string Path(getHost().getDriver().Dir);
758
759 Path += "/../libexec";
760 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000761 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000762
763 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
764 getFilePaths().push_back("/usr/lib");
765 getFilePaths().push_back("/usr/lib/gcc41");
766}
767
768Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
769 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000770 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000771 Key = Action::AnalyzeJobClass;
772 else
773 Key = JA.getKind();
774
775 Tool *&T = Tools[Key];
776 if (!T) {
777 switch (Key) {
778 case Action::AssembleJobClass:
779 T = new tools::dragonfly::Assemble(*this); break;
780 case Action::LinkJobClass:
781 T = new tools::dragonfly::Link(*this); break;
782 default:
783 T = &Generic_GCC::SelectTool(C, JA);
784 }
785 }
786
787 return *T;
788}