blob: 443ce80ae8f5f5666e851769b6dc77a8ea102a87 [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)
41 << "10." << DarwinVersion[0] - 4 << '.' << DarwinVersion[1];
42
Daniel Dunbar30392de2009-09-04 18:35:21 +000043 // FIXME: Lift default up.
44 IPhoneOSVersionMin = "3.0";
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000045}
46
47DarwinGCC::DarwinGCC(const HostInfo &Host, const llvm::Triple& Triple,
48 const unsigned (&DarwinVersion)[3],
Mike Stump44da8212009-10-07 01:11:54 +000049 const unsigned (&_GCCVersion)[4], bool IsIPhoneOS)
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000050 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
51{
52 GCCVersion[0] = _GCCVersion[0];
53 GCCVersion[1] = _GCCVersion[1];
54 GCCVersion[2] = _GCCVersion[2];
Mike Stump44da8212009-10-07 01:11:54 +000055 GCCVersion[3] = _GCCVersion[3];
Daniel Dunbar1d4612b2009-09-18 08:15:13 +000056
57 // Set up the tool chain paths to match gcc.
Daniel Dunbar30392de2009-09-04 18:35:21 +000058
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000059 ToolChainDir = "i686-apple-darwin";
Mike Stump44da8212009-10-07 01:11:54 +000060 ToolChainDir += llvm::utostr(GCCVersion[3]);
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
68 std::string Path;
69 if (getArchName() == "x86_64") {
70 Path = getHost().getDriver().Dir;
71 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000072 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000073 Path += "/x86_64";
74 getFilePaths().push_back(Path);
75
76 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000077 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000078 Path += "/x86_64";
79 getFilePaths().push_back(Path);
80 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000082 Path = getHost().getDriver().Dir;
83 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000084 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000085 getFilePaths().push_back(Path);
86
87 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000088 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000089 getFilePaths().push_back(Path);
90
91 Path = getHost().getDriver().Dir;
92 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000093 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000094 getProgramPaths().push_back(Path);
95
96 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000097 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000098 getProgramPaths().push_back(Path);
99
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000100 Path = getHost().getDriver().Dir;
101 Path += "/../libexec";
102 getProgramPaths().push_back(Path);
103
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000104 getProgramPaths().push_back(getHost().getDriver().Dir);
105}
106
Daniel Dunbarf3955282009-09-04 18:34:51 +0000107Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000108 // Free tool implementations.
109 for (llvm::DenseMap<unsigned, Tool*>::iterator
110 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
111 delete it->second;
112}
113
Daniel Dunbarf3955282009-09-04 18:34:51 +0000114Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000115 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000116 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000117 Key = Action::AnalyzeJobClass;
118 else
119 Key = JA.getKind();
120
121 Tool *&T = Tools[Key];
122 if (!T) {
123 switch (Key) {
124 case Action::InputClass:
125 case Action::BindArchClass:
126 assert(0 && "Invalid tool kind.");
127 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000128 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000129 case Action::AnalyzeJobClass:
130 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000131 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000132 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000133 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000134 case Action::AssembleJobClass:
Daniel Dunbar8cac5f72009-03-20 16:06:39 +0000135 T = new tools::darwin::Assemble(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000136 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000137 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000138 case Action::LipoJobClass:
139 T = new tools::darwin::Lipo(*this); break;
140 }
141 }
142
143 return *T;
144}
145
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000146void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
147 ArgStringList &CmdArgs) const {
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000148 // FIXME: Derive these correctly.
149 if (getArchName() == "x86_64") {
150 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
151 "/x86_64"));
152 // Intentionally duplicated for (temporary) gcc bug compatibility.
153 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
154 "/x86_64"));
155 }
156 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
157 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
158 // Intentionally duplicated for (temporary) gcc bug compatibility.
159 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
160 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
161 "/../../../" + ToolChainDir));
162 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
163 "/../../.."));
164}
165
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000166void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
167 ArgStringList &CmdArgs) const {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000168 unsigned MacosxVersionMin[3];
169 getMacosxVersionMin(Args, MacosxVersionMin);
170
171 // Derived from libgcc and lib specs but refactored.
172 if (Args.hasArg(options::OPT_static)) {
173 CmdArgs.push_back("-lgcc_static");
174 } else {
175 if (Args.hasArg(options::OPT_static_libgcc)) {
176 CmdArgs.push_back("-lgcc_eh");
177 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
178 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000179 if (isIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000180 CmdArgs.push_back("-lgcc_s.1");
181 } else {
182 CmdArgs.push_back("-lgcc_s.10.5");
183 }
184 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
185 // FIXME: -fexceptions -fno-exceptions means no exceptions
186 Args.hasArg(options::OPT_fexceptions) ||
187 Args.hasArg(options::OPT_fgnu_runtime)) {
188 // FIXME: This is probably broken on 10.3?
189 if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
190 CmdArgs.push_back("-lgcc_s.10.4");
191 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
192 CmdArgs.push_back("-lgcc_s.10.5");
193 } else {
194 if (isMacosxVersionLT(MacosxVersionMin, 10, 3, 9))
195 ; // Do nothing.
196 else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
197 CmdArgs.push_back("-lgcc_s.10.4");
198 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
199 CmdArgs.push_back("-lgcc_s.10.5");
200 }
201
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000202 if (isIPhoneOS() || isMacosxVersionLT(MacosxVersionMin, 10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000203 CmdArgs.push_back("-lgcc");
204 CmdArgs.push_back("-lSystem");
205 } else {
206 CmdArgs.push_back("-lSystem");
207 CmdArgs.push_back("-lgcc");
208 }
209 }
210}
211
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000212DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
213 const unsigned (&DarwinVersion)[3],
214 bool IsIPhoneOS)
215 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
216{
Daniel Dunbara92ba272009-09-29 18:52:10 +0000217 // Add the relative libexec dir (for clang-cc).
218 //
219 // FIXME: We should sink clang-cc into libexec/clang/<version>/.
220 std::string Path = getHost().getDriver().Dir;
221 Path += "/../libexec";
222 getProgramPaths().push_back(Path);
223
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000224 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
225 getProgramPaths().push_back(getHost().getDriver().Dir);
226}
227
228void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
229 ArgStringList &CmdArgs) const {
230 // The Clang toolchain uses explicit paths for internal libraries.
231}
232
233void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
234 ArgStringList &CmdArgs) const {
235 // Check for static linking.
236 if (Args.hasArg(options::OPT_static)) {
237 // FIXME: We need to have compiler-rt available (perhaps as
238 // libclang_static.a) to link against.
239 return;
240 }
241
242 // Reject -static-libgcc for now, we can deal with this when and if someone
243 // cares. This is useful in situations where someone wants to statically link
244 // something like libstdc++, and needs its runtime support routines.
245 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
246 getHost().getDriver().Diag(clang::diag::err_drv_unsupported_opt)
247 << A->getAsString(Args);
248 return;
249 }
250
251 // Otherwise link libSystem, which should have the support routines.
252 //
253 // FIXME: This is only true for 10.6 and beyond. Legacy support isn't
254 // critical, but it should work... we should just link in the static
255 // compiler-rt library.
256 CmdArgs.push_back("-lSystem");
257}
258
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000259void Darwin::getMacosxVersionMin(const ArgList &Args,
260 unsigned (&Res)[3]) const {
261 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
262 bool HadExtra;
263 if (!Driver::GetReleaseVersion(A->getValue(Args), Res[0], Res[1], Res[2],
264 HadExtra) ||
265 HadExtra) {
266 const Driver &D = getHost().getDriver();
267 D.Diag(clang::diag::err_drv_invalid_version_number)
268 << A->getAsString(Args);
269 }
270 } else
271 return getMacosxVersion(Res);
272}
273
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000274DerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
275 const char *BoundArch) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000276 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000277 const OptTable &Opts = getHost().getDriver().getOpts();
278
279 // FIXME: We really want to get out of the tool chain level argument
280 // translation business, as it makes the driver functionality much
281 // more opaque. For now, we follow gcc closely solely for the
282 // purpose of easily achieving feature parity & testability. Once we
283 // have something that works, we should reevaluate each translation
Mike Stump1eb44332009-09-09 15:08:12 +0000284 // and try to push it down into tool specific logic.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000285
Mike Stump1eb44332009-09-09 15:08:12 +0000286 Arg *OSXVersion =
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000287 Args.getLastArg(options::OPT_mmacosx_version_min_EQ, false);
288 Arg *iPhoneVersion =
Mike Stump1eb44332009-09-09 15:08:12 +0000289 Args.getLastArg(options::OPT_miphoneos_version_min_EQ, false);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000290 if (OSXVersion && iPhoneVersion) {
291 getHost().getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
292 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000293 << iPhoneVersion->getAsString(Args);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000294 } else if (!OSXVersion && !iPhoneVersion) {
295 // Chose the default version based on the arch.
296 //
Daniel Dunbar30392de2009-09-04 18:35:21 +0000297 // FIXME: Are there iPhone overrides for this?
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000298
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000299 if (!isIPhoneOS()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000300 // Look for MACOSX_DEPLOYMENT_TARGET, otherwise use the version
301 // from the host.
302 const char *Version = ::getenv("MACOSX_DEPLOYMENT_TARGET");
303 if (!Version)
304 Version = MacosxVersionMin.c_str();
305 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
306 DAL->append(DAL->MakeJoinedArg(0, O, Version));
307 } else {
308 const char *Version = IPhoneOSVersionMin.c_str();
309 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
310 DAL->append(DAL->MakeJoinedArg(0, O, Version));
311 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000314 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
315 Arg *A = *it;
316
317 if (A->getOption().matches(options::OPT_Xarch__)) {
318 // FIXME: Canonicalize name.
319 if (getArchName() != A->getValue(Args, 0))
320 continue;
321
322 // FIXME: The arg is leaked here, and we should have a nicer
323 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000324 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000325 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000327 // If the argument parsing failed or more than one argument was
328 // consumed, the -Xarch_ argument's parameter tried to consume
329 // extra arguments. Emit an error and ignore.
330 //
331 // We also want to disallow any options which would alter the
332 // driver behavior; that isn't going to work in our model. We
333 // use isDriverOption() as an approximation, although things
334 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000335 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000336 XarchArg->getOption().isDriverOption()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000337 getHost().getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000338 << A->getAsString(Args);
339 continue;
340 }
341
Daniel Dunbar478edc22009-03-29 22:29:05 +0000342 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000343 A = XarchArg;
Mike Stump1eb44332009-09-09 15:08:12 +0000344 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000345
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000346 // Sob. These is strictly gcc compatible for the time being. Apple
347 // gcc translates options twice, which means that self-expanding
348 // options add duplicates.
349 options::ID id = A->getOption().getId();
350 switch (id) {
351 default:
352 DAL->append(A);
353 break;
354
355 case options::OPT_mkernel:
356 case options::OPT_fapple_kext:
357 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000358 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
359 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000360 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000362 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000363 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000364 A->getValue(Args)));
365 break;
366
367 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000368 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
369 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000370 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
371 break;
372
373 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000374 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
375 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000376 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
377 break;
378
379 case options::OPT_fterminated_vtables:
380 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000381 DAL->append(DAL->MakeFlagArg(A,
382 Opts.getOption(options::OPT_fapple_kext)));
383 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000384 break;
385
386 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000387 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000388 break;
389
390 case options::OPT_fconstant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000391 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000392 Opts.getOption(options::OPT_mconstant_cfstrings)));
393 break;
394
395 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000396 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000397 Opts.getOption(options::OPT_mno_constant_cfstrings)));
398 break;
399
400 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000401 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000402 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
403 break;
404
405 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000406 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000407 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
408 break;
409
410 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000411 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000412 Opts.getOption(options::OPT_mpascal_strings)));
413 break;
414
415 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000416 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000417 Opts.getOption(options::OPT_mno_pascal_strings)));
418 break;
419 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000420 }
421
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000422 if (getTriple().getArch() == llvm::Triple::x86 ||
423 getTriple().getArch() == llvm::Triple::x86_64)
424 if (!Args.hasArg(options::OPT_mtune_EQ, false))
425 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
426 "core2"));
427
428 // Add the arch options based on the particular spelling of -arch, to match
429 // how the driver driver works.
430 if (BoundArch) {
431 llvm::StringRef Name = BoundArch;
432 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
433 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
434
435 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
436 // which defines the list of which architectures we accept.
437 if (Name == "ppc")
438 ;
439 else if (Name == "ppc601")
440 DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
441 else if (Name == "ppc603")
442 DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
443 else if (Name == "ppc604")
444 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
445 else if (Name == "ppc604e")
446 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
447 else if (Name == "ppc750")
448 DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
449 else if (Name == "ppc7400")
450 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
451 else if (Name == "ppc7450")
452 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
453 else if (Name == "ppc970")
454 DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
455
456 else if (Name == "ppc64")
Daniel Dunbar478edc22009-03-29 22:29:05 +0000457 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000458
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000459 else if (Name == "i386")
460 ;
461 else if (Name == "i486")
462 DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
463 else if (Name == "i586")
464 DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
465 else if (Name == "i686")
466 DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
467 else if (Name == "pentium")
468 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
469 else if (Name == "pentium2")
470 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
471 else if (Name == "pentpro")
472 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
473 else if (Name == "pentIIm3")
474 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
475
476 else if (Name == "x86_64")
477 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
478
479 else if (Name == "arm")
480 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
481 else if (Name == "armv4t")
482 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
483 else if (Name == "armv5")
484 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
485 else if (Name == "xscale")
486 DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
487 else if (Name == "armv6")
488 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
489 else if (Name == "armv7")
490 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
491
492 else
493 llvm::llvm_unreachable("invalid Darwin arch");
494 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000495
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000496 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000497}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000498
Daniel Dunbarf3955282009-09-04 18:34:51 +0000499bool Darwin::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000500 return false;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000501}
502
Daniel Dunbarf3955282009-09-04 18:34:51 +0000503bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000504 // FIXME: Gross; we should probably have some separate target
505 // definition, possibly even reusing the one in clang.
506 return getArchName() == "x86_64";
507}
508
Daniel Dunbarf3955282009-09-04 18:34:51 +0000509const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000510 return "pic";
511}
512
Daniel Dunbarf3955282009-09-04 18:34:51 +0000513const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000514 if (getArchName() == "x86_64")
515 return "pic";
516 return 0;
517}
518
Daniel Dunbar39176082009-03-20 00:20:03 +0000519/// Generic_GCC - A tool chain using the 'gcc' command to perform
520/// all subcommands; this relies on gcc translating the majority of
521/// command line options.
522
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000523Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000524 : ToolChain(Host, Triple) {
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000525 std::string Path(getHost().getDriver().Dir);
526 Path += "/../libexec";
527 getProgramPaths().push_back(Path);
528
Mike Stump1eb44332009-09-09 15:08:12 +0000529 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000530}
531
Daniel Dunbar39176082009-03-20 00:20:03 +0000532Generic_GCC::~Generic_GCC() {
533 // Free tool implementations.
534 for (llvm::DenseMap<unsigned, Tool*>::iterator
535 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
536 delete it->second;
537}
538
Mike Stump1eb44332009-09-09 15:08:12 +0000539Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000540 const JobAction &JA) const {
541 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000542 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000543 Key = Action::AnalyzeJobClass;
544 else
545 Key = JA.getKind();
546
547 Tool *&T = Tools[Key];
548 if (!T) {
549 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000550 case Action::InputClass:
551 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000552 assert(0 && "Invalid tool kind.");
553 case Action::PreprocessJobClass:
554 T = new tools::gcc::Preprocess(*this); break;
555 case Action::PrecompileJobClass:
556 T = new tools::gcc::Precompile(*this); break;
557 case Action::AnalyzeJobClass:
558 T = new tools::Clang(*this); break;
559 case Action::CompileJobClass:
560 T = new tools::gcc::Compile(*this); break;
561 case Action::AssembleJobClass:
562 T = new tools::gcc::Assemble(*this); break;
563 case Action::LinkJobClass:
564 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000566 // This is a bit ungeneric, but the only platform using a driver
567 // driver is Darwin.
568 case Action::LipoJobClass:
569 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000570 }
571 }
572
573 return *T;
574}
575
Daniel Dunbarf3955282009-09-04 18:34:51 +0000576bool Generic_GCC::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000577 return true;
Daniel Dunbar39176082009-03-20 00:20:03 +0000578}
579
580bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000581 // FIXME: Gross; we should probably have some separate target
582 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000583 return getArchName() == "x86_64";
584}
585
586const char *Generic_GCC::GetDefaultRelocationModel() const {
587 return "static";
588}
589
590const char *Generic_GCC::GetForcedPicModel() const {
591 return 0;
592}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000593
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000594DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
595 const char *BoundArch) const {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000596 return new DerivedArgList(Args, true);
597}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000598
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000599/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
600
601OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
602 : Generic_GCC(Host, Triple) {
603 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
604 getFilePaths().push_back("/usr/lib");
605}
606
607Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
608 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000609 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000610 Key = Action::AnalyzeJobClass;
611 else
612 Key = JA.getKind();
613
614 Tool *&T = Tools[Key];
615 if (!T) {
616 switch (Key) {
617 case Action::AssembleJobClass:
618 T = new tools::openbsd::Assemble(*this); break;
619 case Action::LinkJobClass:
620 T = new tools::openbsd::Link(*this); break;
621 default:
622 T = &Generic_GCC::SelectTool(C, JA);
623 }
624 }
625
626 return *T;
627}
628
Daniel Dunbar75358d22009-03-30 21:06:03 +0000629/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
630
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000631FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
632 : Generic_GCC(Host, Triple) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000633 if (Lib32) {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000634 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000635 getFilePaths().push_back("/usr/lib32");
636 } else {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000637 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000638 getFilePaths().push_back("/usr/lib");
639 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000640}
641
642Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
643 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000644 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000645 Key = Action::AnalyzeJobClass;
646 else
647 Key = JA.getKind();
648
649 Tool *&T = Tools[Key];
650 if (!T) {
651 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000652 case Action::AssembleJobClass:
653 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000654 case Action::LinkJobClass:
655 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000656 default:
657 T = &Generic_GCC::SelectTool(C, JA);
658 }
659 }
660
661 return *T;
662}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000663
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000664/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
665
666AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
667 : Generic_GCC(Host, Triple) {
668
669 // Path mangling to find libexec
670 std::string Path(getHost().getDriver().Dir);
671
672 Path += "/../libexec";
673 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000674 getProgramPaths().push_back(getHost().getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000675
676 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
677 getFilePaths().push_back("/usr/lib");
678 getFilePaths().push_back("/usr/sfw/lib");
679 getFilePaths().push_back("/opt/gcc4/lib");
680
681}
682
683Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
684 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000685 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000686 Key = Action::AnalyzeJobClass;
687 else
688 Key = JA.getKind();
689
690 Tool *&T = Tools[Key];
691 if (!T) {
692 switch (Key) {
693 case Action::AssembleJobClass:
694 T = new tools::auroraux::Assemble(*this); break;
695 case Action::LinkJobClass:
696 T = new tools::auroraux::Link(*this); break;
697 default:
698 T = &Generic_GCC::SelectTool(C, JA);
699 }
700 }
701
702 return *T;
703}
704
705
Eli Friedman6b3454a2009-05-26 07:52:18 +0000706/// Linux toolchain (very bare-bones at the moment).
707
708Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
709 : Generic_GCC(Host, Triple) {
710 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib/clang/1.0/");
711 getFilePaths().push_back("/lib/");
712 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +0000713
714 // Depending on the Linux distribution, any combination of lib{,32,64} is
715 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
716 // openSUSE uses lib and lib64 for the same purpose.
717 getFilePaths().push_back("/lib32/");
718 getFilePaths().push_back("/usr/lib32/");
719 getFilePaths().push_back("/lib64/");
720 getFilePaths().push_back("/usr/lib64/");
721
Eli Friedman6b3454a2009-05-26 07:52:18 +0000722 // FIXME: Figure out some way to get gcc's libdir
723 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
724 // crtbegin.o/crtend.o/etc., and want static versions of various
725 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
726 // get away with using shared versions in /usr/lib, though.
727 // We could fall back to the approach we used for includes (a massive
728 // list), but that's messy at best.
729}
730
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000731/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
732
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000733DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
734 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000735
736 // Path mangling to find libexec
737 std::string Path(getHost().getDriver().Dir);
738
739 Path += "/../libexec";
740 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000741 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000742
743 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
744 getFilePaths().push_back("/usr/lib");
745 getFilePaths().push_back("/usr/lib/gcc41");
746}
747
748Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
749 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000750 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000751 Key = Action::AnalyzeJobClass;
752 else
753 Key = JA.getKind();
754
755 Tool *&T = Tools[Key];
756 if (!T) {
757 switch (Key) {
758 case Action::AssembleJobClass:
759 T = new tools::dragonfly::Assemble(*this); break;
760 case Action::LinkJobClass:
761 T = new tools::dragonfly::Link(*this); break;
762 default:
763 T = &Generic_GCC::SelectTool(C, JA);
764 }
765 }
766
767 return *T;
768}