blob: 398e5dddbf3f9f0a1f54425b4d754f9dc8564353 [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 Dunbar4e7e9cf2009-03-25 06:12:34 +000017#include "clang/Driver/Option.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000018
19#include "llvm/ADT/StringExtras.h"
Daniel Dunbar84ec96c2009-09-09 22:33:15 +000020#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarec069ed2009-03-25 06:58:31 +000021#include "llvm/Support/raw_ostream.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000022#include "llvm/System/Path.h"
23
Daniel Dunbarf36a06a2009-04-10 21:00:07 +000024#include <cstdlib> // ::getenv
25
Daniel Dunbar39176082009-03-20 00:20:03 +000026using namespace clang::driver;
27using namespace clang::driver::toolchains;
28
Daniel Dunbarf3955282009-09-04 18:34:51 +000029/// Darwin - Darwin tool chain for i386 and x86_64.
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000030
Daniel Dunbarf3955282009-09-04 18:34:51 +000031Darwin::Darwin(const HostInfo &Host, const llvm::Triple& Triple,
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000032 const unsigned (&_DarwinVersion)[3], bool _IsIPhoneOS)
33 : ToolChain(Host, Triple),
34 IsIPhoneOS(_IsIPhoneOS)
35{
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000036 DarwinVersion[0] = _DarwinVersion[0];
37 DarwinVersion[1] = _DarwinVersion[1];
38 DarwinVersion[2] = _DarwinVersion[2];
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000039
Daniel Dunbar02633b52009-03-26 16:23:12 +000040 llvm::raw_string_ostream(MacosxVersionMin)
Benjamin Kramer87f9fd92009-10-21 21:05:07 +000041 << "10." << std::max(0, (int)DarwinVersion[0] - 4) << '.'
42 << DarwinVersion[1];
Daniel Dunbar02633b52009-03-26 16:23:12 +000043
Daniel Dunbar30392de2009-09-04 18:35:21 +000044 // FIXME: Lift default up.
45 IPhoneOSVersionMin = "3.0";
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000046}
47
48DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
49 const unsigned (&DarwinVersion)[3],
Ted Kremenek55bac532009-10-07 03:21:11 +000050 const unsigned (&_GCCVersion)[3], bool IsIPhoneOS)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000051 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
52{
53 GCCVersion[0] = _GCCVersion[0];
54 GCCVersion[1] = _GCCVersion[1];
55 GCCVersion[2] = _GCCVersion[2];
56
57 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000058 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +000059 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000060 ToolChainDir += "/";
61 ToolChainDir += llvm::utostr(GCCVersion[0]);
62 ToolChainDir += '.';
63 ToolChainDir += llvm::utostr(GCCVersion[1]);
64 ToolChainDir += '.';
65 ToolChainDir += llvm::utostr(GCCVersion[2]);
66
Daniel Dunbar74782b02009-10-20 19:25:43 +000067 // Try the next major version if that tool chain dir is invalid.
68 if (!llvm::sys::Path(ToolChainDir).exists()) {
69 std::string Next = "i686-apple-darwin";
70 Next += llvm::utostr(DarwinVersion[0] + 1);
71 Next += "/";
72 Next += llvm::utostr(GCCVersion[0]);
73 Next += '.';
74 Next += llvm::utostr(GCCVersion[1]);
75 Next += '.';
76 Next += llvm::utostr(GCCVersion[2]);
77
78 // Use that if it exists, otherwise hope the user isn't linking.
79 //
80 // FIXME: Drop dependency on gcc's tool chain.
81 if (llvm::sys::Path(Next).exists())
82 ToolChainDir = Next;
83 }
84
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000085 std::string Path;
86 if (getArchName() == "x86_64") {
87 Path = getHost().getDriver().Dir;
88 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000089 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000090 Path += "/x86_64";
91 getFilePaths().push_back(Path);
92
93 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000094 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000095 Path += "/x86_64";
96 getFilePaths().push_back(Path);
97 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000099 Path = getHost().getDriver().Dir;
100 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000101 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000102 getFilePaths().push_back(Path);
103
104 Path = "/usr/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 = getHost().getDriver().Dir;
109 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000110 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000111 getProgramPaths().push_back(Path);
112
113 Path = "/usr/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
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000117 Path = getHost().getDriver().Dir;
118 Path += "/../libexec";
119 getProgramPaths().push_back(Path);
120
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000121 getProgramPaths().push_back(getHost().getDriver().Dir);
122}
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 Dunbara6046be2009-09-08 23:36:55 +0000133 if (getHost().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) ||
202 // FIXME: -fexceptions -fno-exceptions means no exceptions
203 Args.hasArg(options::OPT_fexceptions) ||
204 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{
Daniel Dunbara92ba272009-09-29 18:52:10 +0000234 // Add the relative libexec dir (for clang-cc).
235 //
236 // FIXME: We should sink clang-cc into libexec/clang/<version>/.
237 std::string Path = getHost().getDriver().Dir;
238 Path += "/../libexec";
239 getProgramPaths().push_back(Path);
240
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000241 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
242 getProgramPaths().push_back(getHost().getDriver().Dir);
243}
244
245void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
246 ArgStringList &CmdArgs) const {
247 // The Clang toolchain uses explicit paths for internal libraries.
248}
249
250void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
251 ArgStringList &CmdArgs) const {
252 // Check for static linking.
253 if (Args.hasArg(options::OPT_static)) {
254 // FIXME: We need to have compiler-rt available (perhaps as
255 // libclang_static.a) to link against.
256 return;
257 }
258
259 // Reject -static-libgcc for now, we can deal with this when and if someone
260 // cares. This is useful in situations where someone wants to statically link
261 // something like libstdc++, and needs its runtime support routines.
262 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
263 getHost().getDriver().Diag(clang::diag::err_drv_unsupported_opt)
264 << A->getAsString(Args);
265 return;
266 }
267
268 // Otherwise link libSystem, which should have the support routines.
269 //
270 // FIXME: This is only true for 10.6 and beyond. Legacy support isn't
271 // critical, but it should work... we should just link in the static
272 // compiler-rt library.
273 CmdArgs.push_back("-lSystem");
274}
275
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000276void Darwin::getMacosxVersionMin(const ArgList &Args,
277 unsigned (&Res)[3]) const {
278 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
279 bool HadExtra;
280 if (!Driver::GetReleaseVersion(A->getValue(Args), Res[0], Res[1], Res[2],
281 HadExtra) ||
282 HadExtra) {
283 const Driver &D = getHost().getDriver();
284 D.Diag(clang::diag::err_drv_invalid_version_number)
285 << A->getAsString(Args);
286 }
287 } else
288 return getMacosxVersion(Res);
289}
290
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000291DerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
292 const char *BoundArch) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000293 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000294 const OptTable &Opts = getHost().getDriver().getOpts();
295
296 // FIXME: We really want to get out of the tool chain level argument
297 // translation business, as it makes the driver functionality much
298 // more opaque. For now, we follow gcc closely solely for the
299 // purpose of easily achieving feature parity & testability. Once we
300 // have something that works, we should reevaluate each translation
Mike Stump1eb44332009-09-09 15:08:12 +0000301 // and try to push it down into tool specific logic.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000302
Mike Stump1eb44332009-09-09 15:08:12 +0000303 Arg *OSXVersion =
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000304 Args.getLastArg(options::OPT_mmacosx_version_min_EQ, false);
305 Arg *iPhoneVersion =
Mike Stump1eb44332009-09-09 15:08:12 +0000306 Args.getLastArg(options::OPT_miphoneos_version_min_EQ, false);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000307 if (OSXVersion && iPhoneVersion) {
308 getHost().getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
309 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000310 << iPhoneVersion->getAsString(Args);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000311 } else if (!OSXVersion && !iPhoneVersion) {
312 // Chose the default version based on the arch.
313 //
Daniel Dunbar30392de2009-09-04 18:35:21 +0000314 // FIXME: Are there iPhone overrides for this?
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000315
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000316 if (!isIPhoneOS()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000317 // Look for MACOSX_DEPLOYMENT_TARGET, otherwise use the version
318 // from the host.
319 const char *Version = ::getenv("MACOSX_DEPLOYMENT_TARGET");
320 if (!Version)
321 Version = MacosxVersionMin.c_str();
322 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
323 DAL->append(DAL->MakeJoinedArg(0, O, Version));
324 } else {
325 const char *Version = IPhoneOSVersionMin.c_str();
326 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
327 DAL->append(DAL->MakeJoinedArg(0, O, Version));
328 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000331 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
332 Arg *A = *it;
333
334 if (A->getOption().matches(options::OPT_Xarch__)) {
335 // FIXME: Canonicalize name.
336 if (getArchName() != A->getValue(Args, 0))
337 continue;
338
339 // FIXME: The arg is leaked here, and we should have a nicer
340 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000341 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000342 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000344 // If the argument parsing failed or more than one argument was
345 // consumed, the -Xarch_ argument's parameter tried to consume
346 // extra arguments. Emit an error and ignore.
347 //
348 // We also want to disallow any options which would alter the
349 // driver behavior; that isn't going to work in our model. We
350 // use isDriverOption() as an approximation, although things
351 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000352 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000353 XarchArg->getOption().isDriverOption()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000354 getHost().getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000355 << A->getAsString(Args);
356 continue;
357 }
358
Daniel Dunbar478edc22009-03-29 22:29:05 +0000359 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000360 A = XarchArg;
Mike Stump1eb44332009-09-09 15:08:12 +0000361 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000362
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000363 // Sob. These is strictly gcc compatible for the time being. Apple
364 // gcc translates options twice, which means that self-expanding
365 // options add duplicates.
366 options::ID id = A->getOption().getId();
367 switch (id) {
368 default:
369 DAL->append(A);
370 break;
371
372 case options::OPT_mkernel:
373 case options::OPT_fapple_kext:
374 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000375 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
376 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000377 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000379 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000380 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000381 A->getValue(Args)));
382 break;
383
384 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000385 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
386 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000387 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
388 break;
389
390 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000391 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
392 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000393 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
394 break;
395
396 case options::OPT_fterminated_vtables:
397 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000398 DAL->append(DAL->MakeFlagArg(A,
399 Opts.getOption(options::OPT_fapple_kext)));
400 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000401 break;
402
403 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000404 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000405 break;
406
407 case options::OPT_fconstant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000408 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000409 Opts.getOption(options::OPT_mconstant_cfstrings)));
410 break;
411
412 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000413 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000414 Opts.getOption(options::OPT_mno_constant_cfstrings)));
415 break;
416
417 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000418 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000419 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
420 break;
421
422 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000423 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000424 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
425 break;
426
427 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000428 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000429 Opts.getOption(options::OPT_mpascal_strings)));
430 break;
431
432 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000433 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000434 Opts.getOption(options::OPT_mno_pascal_strings)));
435 break;
436 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000437 }
438
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000439 if (getTriple().getArch() == llvm::Triple::x86 ||
440 getTriple().getArch() == llvm::Triple::x86_64)
441 if (!Args.hasArg(options::OPT_mtune_EQ, false))
442 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
443 "core2"));
444
445 // Add the arch options based on the particular spelling of -arch, to match
446 // how the driver driver works.
447 if (BoundArch) {
448 llvm::StringRef Name = BoundArch;
449 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
450 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
451
452 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
453 // which defines the list of which architectures we accept.
454 if (Name == "ppc")
455 ;
456 else if (Name == "ppc601")
457 DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
458 else if (Name == "ppc603")
459 DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
460 else if (Name == "ppc604")
461 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
462 else if (Name == "ppc604e")
463 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
464 else if (Name == "ppc750")
465 DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
466 else if (Name == "ppc7400")
467 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
468 else if (Name == "ppc7450")
469 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
470 else if (Name == "ppc970")
471 DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
472
473 else if (Name == "ppc64")
Daniel Dunbar478edc22009-03-29 22:29:05 +0000474 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000475
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000476 else if (Name == "i386")
477 ;
478 else if (Name == "i486")
479 DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
480 else if (Name == "i586")
481 DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
482 else if (Name == "i686")
483 DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
484 else if (Name == "pentium")
485 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
486 else if (Name == "pentium2")
487 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
488 else if (Name == "pentpro")
489 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
490 else if (Name == "pentIIm3")
491 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
492
493 else if (Name == "x86_64")
494 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
495
496 else if (Name == "arm")
497 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
498 else if (Name == "armv4t")
499 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
500 else if (Name == "armv5")
501 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
502 else if (Name == "xscale")
503 DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
504 else if (Name == "armv6")
505 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
506 else if (Name == "armv7")
507 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
508
509 else
510 llvm::llvm_unreachable("invalid Darwin arch");
511 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000512
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000513 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000514}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000515
Daniel Dunbarf3955282009-09-04 18:34:51 +0000516bool Darwin::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000517 return false;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000518}
519
Daniel Dunbarf3955282009-09-04 18:34:51 +0000520bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000521 // FIXME: Gross; we should probably have some separate target
522 // definition, possibly even reusing the one in clang.
523 return getArchName() == "x86_64";
524}
525
Daniel Dunbarf3955282009-09-04 18:34:51 +0000526const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000527 return "pic";
528}
529
Daniel Dunbarf3955282009-09-04 18:34:51 +0000530const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000531 if (getArchName() == "x86_64")
532 return "pic";
533 return 0;
534}
535
Daniel Dunbar39176082009-03-20 00:20:03 +0000536/// Generic_GCC - A tool chain using the 'gcc' command to perform
537/// all subcommands; this relies on gcc translating the majority of
538/// command line options.
539
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000540Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000541 : ToolChain(Host, Triple) {
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000542 std::string Path(getHost().getDriver().Dir);
543 Path += "/../libexec";
544 getProgramPaths().push_back(Path);
545
Mike Stump1eb44332009-09-09 15:08:12 +0000546 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000547}
548
Daniel Dunbar39176082009-03-20 00:20:03 +0000549Generic_GCC::~Generic_GCC() {
550 // Free tool implementations.
551 for (llvm::DenseMap<unsigned, Tool*>::iterator
552 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
553 delete it->second;
554}
555
Mike Stump1eb44332009-09-09 15:08:12 +0000556Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000557 const JobAction &JA) const {
558 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000559 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000560 Key = Action::AnalyzeJobClass;
561 else
562 Key = JA.getKind();
563
564 Tool *&T = Tools[Key];
565 if (!T) {
566 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000567 case Action::InputClass:
568 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000569 assert(0 && "Invalid tool kind.");
570 case Action::PreprocessJobClass:
571 T = new tools::gcc::Preprocess(*this); break;
572 case Action::PrecompileJobClass:
573 T = new tools::gcc::Precompile(*this); break;
574 case Action::AnalyzeJobClass:
575 T = new tools::Clang(*this); break;
576 case Action::CompileJobClass:
577 T = new tools::gcc::Compile(*this); break;
578 case Action::AssembleJobClass:
579 T = new tools::gcc::Assemble(*this); break;
580 case Action::LinkJobClass:
581 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000583 // This is a bit ungeneric, but the only platform using a driver
584 // driver is Darwin.
585 case Action::LipoJobClass:
586 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000587 }
588 }
589
590 return *T;
591}
592
Daniel Dunbarf3955282009-09-04 18:34:51 +0000593bool Generic_GCC::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000594 return true;
Daniel Dunbar39176082009-03-20 00:20:03 +0000595}
596
597bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000598 // FIXME: Gross; we should probably have some separate target
599 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000600 return getArchName() == "x86_64";
601}
602
603const char *Generic_GCC::GetDefaultRelocationModel() const {
604 return "static";
605}
606
607const char *Generic_GCC::GetForcedPicModel() const {
608 return 0;
609}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000610
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000611DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
612 const char *BoundArch) const {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000613 return new DerivedArgList(Args, true);
614}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000615
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000616/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
617
618OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
619 : Generic_GCC(Host, Triple) {
620 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
621 getFilePaths().push_back("/usr/lib");
622}
623
624Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
625 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000626 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000627 Key = Action::AnalyzeJobClass;
628 else
629 Key = JA.getKind();
630
631 Tool *&T = Tools[Key];
632 if (!T) {
633 switch (Key) {
634 case Action::AssembleJobClass:
635 T = new tools::openbsd::Assemble(*this); break;
636 case Action::LinkJobClass:
637 T = new tools::openbsd::Link(*this); break;
638 default:
639 T = &Generic_GCC::SelectTool(C, JA);
640 }
641 }
642
643 return *T;
644}
645
Daniel Dunbar75358d22009-03-30 21:06:03 +0000646/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
647
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000648FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
649 : Generic_GCC(Host, Triple) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000650 if (Lib32) {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000651 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000652 getFilePaths().push_back("/usr/lib32");
653 } else {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000654 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000655 getFilePaths().push_back("/usr/lib");
656 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000657}
658
659Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
660 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000661 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000662 Key = Action::AnalyzeJobClass;
663 else
664 Key = JA.getKind();
665
666 Tool *&T = Tools[Key];
667 if (!T) {
668 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000669 case Action::AssembleJobClass:
670 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000671 case Action::LinkJobClass:
672 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000673 default:
674 T = &Generic_GCC::SelectTool(C, JA);
675 }
676 }
677
678 return *T;
679}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000680
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000681/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
682
683AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
684 : Generic_GCC(Host, Triple) {
685
686 // Path mangling to find libexec
687 std::string Path(getHost().getDriver().Dir);
688
689 Path += "/../libexec";
690 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000691 getProgramPaths().push_back(getHost().getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000692
693 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
694 getFilePaths().push_back("/usr/lib");
695 getFilePaths().push_back("/usr/sfw/lib");
696 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000697 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000698
699}
700
701Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
702 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000703 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000704 Key = Action::AnalyzeJobClass;
705 else
706 Key = JA.getKind();
707
708 Tool *&T = Tools[Key];
709 if (!T) {
710 switch (Key) {
711 case Action::AssembleJobClass:
712 T = new tools::auroraux::Assemble(*this); break;
713 case Action::LinkJobClass:
714 T = new tools::auroraux::Link(*this); break;
715 default:
716 T = &Generic_GCC::SelectTool(C, JA);
717 }
718 }
719
720 return *T;
721}
722
723
Eli Friedman6b3454a2009-05-26 07:52:18 +0000724/// Linux toolchain (very bare-bones at the moment).
725
726Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
727 : Generic_GCC(Host, Triple) {
728 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib/clang/1.0/");
729 getFilePaths().push_back("/lib/");
730 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +0000731
732 // Depending on the Linux distribution, any combination of lib{,32,64} is
733 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
734 // openSUSE uses lib and lib64 for the same purpose.
735 getFilePaths().push_back("/lib32/");
736 getFilePaths().push_back("/usr/lib32/");
737 getFilePaths().push_back("/lib64/");
738 getFilePaths().push_back("/usr/lib64/");
739
Eli Friedman6b3454a2009-05-26 07:52:18 +0000740 // FIXME: Figure out some way to get gcc's libdir
741 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
742 // crtbegin.o/crtend.o/etc., and want static versions of various
743 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
744 // get away with using shared versions in /usr/lib, though.
745 // We could fall back to the approach we used for includes (a massive
746 // list), but that's messy at best.
747}
748
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000749/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
750
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000751DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
752 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000753
754 // Path mangling to find libexec
755 std::string Path(getHost().getDriver().Dir);
756
757 Path += "/../libexec";
758 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000759 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000760
761 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
762 getFilePaths().push_back("/usr/lib");
763 getFilePaths().push_back("/usr/lib/gcc41");
764}
765
766Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
767 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000768 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000769 Key = Action::AnalyzeJobClass;
770 else
771 Key = JA.getKind();
772
773 Tool *&T = Tools[Key];
774 if (!T) {
775 switch (Key) {
776 case Action::AssembleJobClass:
777 T = new tools::dragonfly::Assemble(*this); break;
778 case Action::LinkJobClass:
779 T = new tools::dragonfly::Link(*this); break;
780 default:
781 T = &Generic_GCC::SelectTool(C, JA);
782 }
783 }
784
785 return *T;
786}