blob: a0fefcdfc12a19166e28f93b8f01a35f2bf30c2c [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],
Ted Kremenek55bac532009-10-07 03:21:11 +000049 const unsigned (&_GCCVersion)[3], 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];
55
56 // Set up the tool chain paths to match gcc.
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000057 ToolChainDir = "i686-apple-darwin";
Ted Kremenek55bac532009-10-07 03:21:11 +000058 ToolChainDir += llvm::utostr(DarwinVersion[0]);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000059 ToolChainDir += "/";
60 ToolChainDir += llvm::utostr(GCCVersion[0]);
61 ToolChainDir += '.';
62 ToolChainDir += llvm::utostr(GCCVersion[1]);
63 ToolChainDir += '.';
64 ToolChainDir += llvm::utostr(GCCVersion[2]);
65
Daniel Dunbar74782b02009-10-20 19:25:43 +000066 // Try the next major version if that tool chain dir is invalid.
67 if (!llvm::sys::Path(ToolChainDir).exists()) {
68 std::string Next = "i686-apple-darwin";
69 Next += llvm::utostr(DarwinVersion[0] + 1);
70 Next += "/";
71 Next += llvm::utostr(GCCVersion[0]);
72 Next += '.';
73 Next += llvm::utostr(GCCVersion[1]);
74 Next += '.';
75 Next += llvm::utostr(GCCVersion[2]);
76
77 // Use that if it exists, otherwise hope the user isn't linking.
78 //
79 // FIXME: Drop dependency on gcc's tool chain.
80 if (llvm::sys::Path(Next).exists())
81 ToolChainDir = Next;
82 }
83
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000084 std::string Path;
85 if (getArchName() == "x86_64") {
86 Path = getHost().getDriver().Dir;
87 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000088 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000089 Path += "/x86_64";
90 getFilePaths().push_back(Path);
91
92 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000093 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000094 Path += "/x86_64";
95 getFilePaths().push_back(Path);
96 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000098 Path = getHost().getDriver().Dir;
99 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000100 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000101 getFilePaths().push_back(Path);
102
103 Path = "/usr/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 = getHost().getDriver().Dir;
108 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000109 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000110 getProgramPaths().push_back(Path);
111
112 Path = "/usr/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
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000116 Path = getHost().getDriver().Dir;
117 Path += "/../libexec";
118 getProgramPaths().push_back(Path);
119
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000120 getProgramPaths().push_back(getHost().getDriver().Dir);
121}
122
Daniel Dunbarf3955282009-09-04 18:34:51 +0000123Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000124 // Free tool implementations.
125 for (llvm::DenseMap<unsigned, Tool*>::iterator
126 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
127 delete it->second;
128}
129
Daniel Dunbarf3955282009-09-04 18:34:51 +0000130Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000131 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000132 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000133 Key = Action::AnalyzeJobClass;
134 else
135 Key = JA.getKind();
136
137 Tool *&T = Tools[Key];
138 if (!T) {
139 switch (Key) {
140 case Action::InputClass:
141 case Action::BindArchClass:
142 assert(0 && "Invalid tool kind.");
143 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000144 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000145 case Action::AnalyzeJobClass:
146 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000147 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000148 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000149 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000150 case Action::AssembleJobClass:
Daniel Dunbar8cac5f72009-03-20 16:06:39 +0000151 T = new tools::darwin::Assemble(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000152 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000153 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000154 case Action::LipoJobClass:
155 T = new tools::darwin::Lipo(*this); break;
156 }
157 }
158
159 return *T;
160}
161
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000162void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
163 ArgStringList &CmdArgs) const {
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000164 // FIXME: Derive these correctly.
165 if (getArchName() == "x86_64") {
166 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
167 "/x86_64"));
168 // Intentionally duplicated for (temporary) gcc bug compatibility.
169 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
170 "/x86_64"));
171 }
172 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
173 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
174 // Intentionally duplicated for (temporary) gcc bug compatibility.
175 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
176 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
177 "/../../../" + ToolChainDir));
178 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
179 "/../../.."));
180}
181
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000182void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
183 ArgStringList &CmdArgs) const {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000184 unsigned MacosxVersionMin[3];
185 getMacosxVersionMin(Args, MacosxVersionMin);
186
187 // Derived from libgcc and lib specs but refactored.
188 if (Args.hasArg(options::OPT_static)) {
189 CmdArgs.push_back("-lgcc_static");
190 } else {
191 if (Args.hasArg(options::OPT_static_libgcc)) {
192 CmdArgs.push_back("-lgcc_eh");
193 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
194 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000195 if (isIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000196 CmdArgs.push_back("-lgcc_s.1");
197 } else {
198 CmdArgs.push_back("-lgcc_s.10.5");
199 }
200 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
201 // FIXME: -fexceptions -fno-exceptions means no exceptions
202 Args.hasArg(options::OPT_fexceptions) ||
203 Args.hasArg(options::OPT_fgnu_runtime)) {
204 // FIXME: This is probably broken on 10.3?
205 if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
206 CmdArgs.push_back("-lgcc_s.10.4");
207 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
208 CmdArgs.push_back("-lgcc_s.10.5");
209 } else {
210 if (isMacosxVersionLT(MacosxVersionMin, 10, 3, 9))
211 ; // Do nothing.
212 else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
213 CmdArgs.push_back("-lgcc_s.10.4");
214 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
215 CmdArgs.push_back("-lgcc_s.10.5");
216 }
217
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000218 if (isIPhoneOS() || isMacosxVersionLT(MacosxVersionMin, 10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000219 CmdArgs.push_back("-lgcc");
220 CmdArgs.push_back("-lSystem");
221 } else {
222 CmdArgs.push_back("-lSystem");
223 CmdArgs.push_back("-lgcc");
224 }
225 }
226}
227
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000228DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
229 const unsigned (&DarwinVersion)[3],
230 bool IsIPhoneOS)
231 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
232{
Daniel Dunbara92ba272009-09-29 18:52:10 +0000233 // Add the relative libexec dir (for clang-cc).
234 //
235 // FIXME: We should sink clang-cc into libexec/clang/<version>/.
236 std::string Path = getHost().getDriver().Dir;
237 Path += "/../libexec";
238 getProgramPaths().push_back(Path);
239
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000240 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
241 getProgramPaths().push_back(getHost().getDriver().Dir);
242}
243
244void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
245 ArgStringList &CmdArgs) const {
246 // The Clang toolchain uses explicit paths for internal libraries.
247}
248
249void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
250 ArgStringList &CmdArgs) const {
251 // Check for static linking.
252 if (Args.hasArg(options::OPT_static)) {
253 // FIXME: We need to have compiler-rt available (perhaps as
254 // libclang_static.a) to link against.
255 return;
256 }
257
258 // Reject -static-libgcc for now, we can deal with this when and if someone
259 // cares. This is useful in situations where someone wants to statically link
260 // something like libstdc++, and needs its runtime support routines.
261 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
262 getHost().getDriver().Diag(clang::diag::err_drv_unsupported_opt)
263 << A->getAsString(Args);
264 return;
265 }
266
267 // Otherwise link libSystem, which should have the support routines.
268 //
269 // FIXME: This is only true for 10.6 and beyond. Legacy support isn't
270 // critical, but it should work... we should just link in the static
271 // compiler-rt library.
272 CmdArgs.push_back("-lSystem");
273}
274
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000275void Darwin::getMacosxVersionMin(const ArgList &Args,
276 unsigned (&Res)[3]) const {
277 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
278 bool HadExtra;
279 if (!Driver::GetReleaseVersion(A->getValue(Args), Res[0], Res[1], Res[2],
280 HadExtra) ||
281 HadExtra) {
282 const Driver &D = getHost().getDriver();
283 D.Diag(clang::diag::err_drv_invalid_version_number)
284 << A->getAsString(Args);
285 }
286 } else
287 return getMacosxVersion(Res);
288}
289
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000290DerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
291 const char *BoundArch) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000292 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000293 const OptTable &Opts = getHost().getDriver().getOpts();
294
295 // FIXME: We really want to get out of the tool chain level argument
296 // translation business, as it makes the driver functionality much
297 // more opaque. For now, we follow gcc closely solely for the
298 // purpose of easily achieving feature parity & testability. Once we
299 // have something that works, we should reevaluate each translation
Mike Stump1eb44332009-09-09 15:08:12 +0000300 // and try to push it down into tool specific logic.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000301
Mike Stump1eb44332009-09-09 15:08:12 +0000302 Arg *OSXVersion =
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000303 Args.getLastArg(options::OPT_mmacosx_version_min_EQ, false);
304 Arg *iPhoneVersion =
Mike Stump1eb44332009-09-09 15:08:12 +0000305 Args.getLastArg(options::OPT_miphoneos_version_min_EQ, false);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000306 if (OSXVersion && iPhoneVersion) {
307 getHost().getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
308 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000309 << iPhoneVersion->getAsString(Args);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000310 } else if (!OSXVersion && !iPhoneVersion) {
311 // Chose the default version based on the arch.
312 //
Daniel Dunbar30392de2009-09-04 18:35:21 +0000313 // FIXME: Are there iPhone overrides for this?
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000314
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000315 if (!isIPhoneOS()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000316 // Look for MACOSX_DEPLOYMENT_TARGET, otherwise use the version
317 // from the host.
318 const char *Version = ::getenv("MACOSX_DEPLOYMENT_TARGET");
319 if (!Version)
320 Version = MacosxVersionMin.c_str();
321 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
322 DAL->append(DAL->MakeJoinedArg(0, O, Version));
323 } else {
324 const char *Version = IPhoneOSVersionMin.c_str();
325 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
326 DAL->append(DAL->MakeJoinedArg(0, O, Version));
327 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000330 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
331 Arg *A = *it;
332
333 if (A->getOption().matches(options::OPT_Xarch__)) {
334 // FIXME: Canonicalize name.
335 if (getArchName() != A->getValue(Args, 0))
336 continue;
337
338 // FIXME: The arg is leaked here, and we should have a nicer
339 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000340 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000341 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000343 // If the argument parsing failed or more than one argument was
344 // consumed, the -Xarch_ argument's parameter tried to consume
345 // extra arguments. Emit an error and ignore.
346 //
347 // We also want to disallow any options which would alter the
348 // driver behavior; that isn't going to work in our model. We
349 // use isDriverOption() as an approximation, although things
350 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000351 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000352 XarchArg->getOption().isDriverOption()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000353 getHost().getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000354 << A->getAsString(Args);
355 continue;
356 }
357
Daniel Dunbar478edc22009-03-29 22:29:05 +0000358 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000359 A = XarchArg;
Mike Stump1eb44332009-09-09 15:08:12 +0000360 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000361
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000362 // Sob. These is strictly gcc compatible for the time being. Apple
363 // gcc translates options twice, which means that self-expanding
364 // options add duplicates.
365 options::ID id = A->getOption().getId();
366 switch (id) {
367 default:
368 DAL->append(A);
369 break;
370
371 case options::OPT_mkernel:
372 case options::OPT_fapple_kext:
373 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000374 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
375 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000376 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000378 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000379 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000380 A->getValue(Args)));
381 break;
382
383 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000384 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
385 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000386 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
387 break;
388
389 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000390 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
391 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000392 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
393 break;
394
395 case options::OPT_fterminated_vtables:
396 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000397 DAL->append(DAL->MakeFlagArg(A,
398 Opts.getOption(options::OPT_fapple_kext)));
399 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000400 break;
401
402 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000403 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000404 break;
405
406 case options::OPT_fconstant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000407 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000408 Opts.getOption(options::OPT_mconstant_cfstrings)));
409 break;
410
411 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000412 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000413 Opts.getOption(options::OPT_mno_constant_cfstrings)));
414 break;
415
416 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000417 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000418 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
419 break;
420
421 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000422 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000423 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
424 break;
425
426 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000427 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000428 Opts.getOption(options::OPT_mpascal_strings)));
429 break;
430
431 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000432 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000433 Opts.getOption(options::OPT_mno_pascal_strings)));
434 break;
435 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000436 }
437
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000438 if (getTriple().getArch() == llvm::Triple::x86 ||
439 getTriple().getArch() == llvm::Triple::x86_64)
440 if (!Args.hasArg(options::OPT_mtune_EQ, false))
441 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
442 "core2"));
443
444 // Add the arch options based on the particular spelling of -arch, to match
445 // how the driver driver works.
446 if (BoundArch) {
447 llvm::StringRef Name = BoundArch;
448 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
449 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
450
451 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
452 // which defines the list of which architectures we accept.
453 if (Name == "ppc")
454 ;
455 else if (Name == "ppc601")
456 DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
457 else if (Name == "ppc603")
458 DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
459 else if (Name == "ppc604")
460 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
461 else if (Name == "ppc604e")
462 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
463 else if (Name == "ppc750")
464 DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
465 else if (Name == "ppc7400")
466 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
467 else if (Name == "ppc7450")
468 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
469 else if (Name == "ppc970")
470 DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
471
472 else if (Name == "ppc64")
Daniel Dunbar478edc22009-03-29 22:29:05 +0000473 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000474
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000475 else if (Name == "i386")
476 ;
477 else if (Name == "i486")
478 DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
479 else if (Name == "i586")
480 DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
481 else if (Name == "i686")
482 DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
483 else if (Name == "pentium")
484 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
485 else if (Name == "pentium2")
486 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
487 else if (Name == "pentpro")
488 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
489 else if (Name == "pentIIm3")
490 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
491
492 else if (Name == "x86_64")
493 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
494
495 else if (Name == "arm")
496 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
497 else if (Name == "armv4t")
498 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
499 else if (Name == "armv5")
500 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
501 else if (Name == "xscale")
502 DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
503 else if (Name == "armv6")
504 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
505 else if (Name == "armv7")
506 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
507
508 else
509 llvm::llvm_unreachable("invalid Darwin arch");
510 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000511
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000512 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000513}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000514
Daniel Dunbarf3955282009-09-04 18:34:51 +0000515bool Darwin::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000516 return false;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000517}
518
Daniel Dunbarf3955282009-09-04 18:34:51 +0000519bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000520 // FIXME: Gross; we should probably have some separate target
521 // definition, possibly even reusing the one in clang.
522 return getArchName() == "x86_64";
523}
524
Daniel Dunbarf3955282009-09-04 18:34:51 +0000525const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000526 return "pic";
527}
528
Daniel Dunbarf3955282009-09-04 18:34:51 +0000529const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000530 if (getArchName() == "x86_64")
531 return "pic";
532 return 0;
533}
534
Daniel Dunbar39176082009-03-20 00:20:03 +0000535/// Generic_GCC - A tool chain using the 'gcc' command to perform
536/// all subcommands; this relies on gcc translating the majority of
537/// command line options.
538
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000539Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000540 : ToolChain(Host, Triple) {
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000541 std::string Path(getHost().getDriver().Dir);
542 Path += "/../libexec";
543 getProgramPaths().push_back(Path);
544
Mike Stump1eb44332009-09-09 15:08:12 +0000545 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000546}
547
Daniel Dunbar39176082009-03-20 00:20:03 +0000548Generic_GCC::~Generic_GCC() {
549 // Free tool implementations.
550 for (llvm::DenseMap<unsigned, Tool*>::iterator
551 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
552 delete it->second;
553}
554
Mike Stump1eb44332009-09-09 15:08:12 +0000555Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000556 const JobAction &JA) const {
557 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000558 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000559 Key = Action::AnalyzeJobClass;
560 else
561 Key = JA.getKind();
562
563 Tool *&T = Tools[Key];
564 if (!T) {
565 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000566 case Action::InputClass:
567 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000568 assert(0 && "Invalid tool kind.");
569 case Action::PreprocessJobClass:
570 T = new tools::gcc::Preprocess(*this); break;
571 case Action::PrecompileJobClass:
572 T = new tools::gcc::Precompile(*this); break;
573 case Action::AnalyzeJobClass:
574 T = new tools::Clang(*this); break;
575 case Action::CompileJobClass:
576 T = new tools::gcc::Compile(*this); break;
577 case Action::AssembleJobClass:
578 T = new tools::gcc::Assemble(*this); break;
579 case Action::LinkJobClass:
580 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000582 // This is a bit ungeneric, but the only platform using a driver
583 // driver is Darwin.
584 case Action::LipoJobClass:
585 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000586 }
587 }
588
589 return *T;
590}
591
Daniel Dunbarf3955282009-09-04 18:34:51 +0000592bool Generic_GCC::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000593 return true;
Daniel Dunbar39176082009-03-20 00:20:03 +0000594}
595
596bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000597 // FIXME: Gross; we should probably have some separate target
598 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000599 return getArchName() == "x86_64";
600}
601
602const char *Generic_GCC::GetDefaultRelocationModel() const {
603 return "static";
604}
605
606const char *Generic_GCC::GetForcedPicModel() const {
607 return 0;
608}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000609
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000610DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
611 const char *BoundArch) const {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000612 return new DerivedArgList(Args, true);
613}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000614
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000615/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
616
617OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
618 : Generic_GCC(Host, Triple) {
619 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
620 getFilePaths().push_back("/usr/lib");
621}
622
623Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
624 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000625 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000626 Key = Action::AnalyzeJobClass;
627 else
628 Key = JA.getKind();
629
630 Tool *&T = Tools[Key];
631 if (!T) {
632 switch (Key) {
633 case Action::AssembleJobClass:
634 T = new tools::openbsd::Assemble(*this); break;
635 case Action::LinkJobClass:
636 T = new tools::openbsd::Link(*this); break;
637 default:
638 T = &Generic_GCC::SelectTool(C, JA);
639 }
640 }
641
642 return *T;
643}
644
Daniel Dunbar75358d22009-03-30 21:06:03 +0000645/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
646
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000647FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
648 : Generic_GCC(Host, Triple) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000649 if (Lib32) {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000650 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000651 getFilePaths().push_back("/usr/lib32");
652 } else {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000653 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000654 getFilePaths().push_back("/usr/lib");
655 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000656}
657
658Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
659 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000660 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000661 Key = Action::AnalyzeJobClass;
662 else
663 Key = JA.getKind();
664
665 Tool *&T = Tools[Key];
666 if (!T) {
667 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000668 case Action::AssembleJobClass:
669 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000670 case Action::LinkJobClass:
671 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000672 default:
673 T = &Generic_GCC::SelectTool(C, JA);
674 }
675 }
676
677 return *T;
678}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000679
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000680/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
681
682AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
683 : Generic_GCC(Host, Triple) {
684
685 // Path mangling to find libexec
686 std::string Path(getHost().getDriver().Dir);
687
688 Path += "/../libexec";
689 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000690 getProgramPaths().push_back(getHost().getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000691
692 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
693 getFilePaths().push_back("/usr/lib");
694 getFilePaths().push_back("/usr/sfw/lib");
695 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000696 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000697
698}
699
700Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
701 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000702 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000703 Key = Action::AnalyzeJobClass;
704 else
705 Key = JA.getKind();
706
707 Tool *&T = Tools[Key];
708 if (!T) {
709 switch (Key) {
710 case Action::AssembleJobClass:
711 T = new tools::auroraux::Assemble(*this); break;
712 case Action::LinkJobClass:
713 T = new tools::auroraux::Link(*this); break;
714 default:
715 T = &Generic_GCC::SelectTool(C, JA);
716 }
717 }
718
719 return *T;
720}
721
722
Eli Friedman6b3454a2009-05-26 07:52:18 +0000723/// Linux toolchain (very bare-bones at the moment).
724
725Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
726 : Generic_GCC(Host, Triple) {
727 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib/clang/1.0/");
728 getFilePaths().push_back("/lib/");
729 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +0000730
731 // Depending on the Linux distribution, any combination of lib{,32,64} is
732 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
733 // openSUSE uses lib and lib64 for the same purpose.
734 getFilePaths().push_back("/lib32/");
735 getFilePaths().push_back("/usr/lib32/");
736 getFilePaths().push_back("/lib64/");
737 getFilePaths().push_back("/usr/lib64/");
738
Eli Friedman6b3454a2009-05-26 07:52:18 +0000739 // FIXME: Figure out some way to get gcc's libdir
740 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
741 // crtbegin.o/crtend.o/etc., and want static versions of various
742 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
743 // get away with using shared versions in /usr/lib, though.
744 // We could fall back to the approach we used for includes (a massive
745 // list), but that's messy at best.
746}
747
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000748/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
749
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000750DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
751 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000752
753 // Path mangling to find libexec
754 std::string Path(getHost().getDriver().Dir);
755
756 Path += "/../libexec";
757 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000758 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000759
760 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
761 getFilePaths().push_back("/usr/lib");
762 getFilePaths().push_back("/usr/lib/gcc41");
763}
764
765Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
766 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000767 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000768 Key = Action::AnalyzeJobClass;
769 else
770 Key = JA.getKind();
771
772 Tool *&T = Tools[Key];
773 if (!T) {
774 switch (Key) {
775 case Action::AssembleJobClass:
776 T = new tools::dragonfly::Assemble(*this); break;
777 case Action::LinkJobClass:
778 T = new tools::dragonfly::Link(*this); break;
779 default:
780 T = &Generic_GCC::SelectTool(C, JA);
781 }
782 }
783
784 return *T;
785}