blob: b83e399411b11393f725088b18345b951c0f6146 [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.
Daniel Dunbar080fb192009-10-22 00:12:00 +000068 std::string Tmp = "/usr/lib/gcc/" + ToolChainDir;
69 if (!llvm::sys::Path(Tmp).exists()) {
Daniel Dunbar74782b02009-10-20 19:25:43 +000070 std::string Next = "i686-apple-darwin";
71 Next += llvm::utostr(DarwinVersion[0] + 1);
72 Next += "/";
73 Next += llvm::utostr(GCCVersion[0]);
74 Next += '.';
75 Next += llvm::utostr(GCCVersion[1]);
76 Next += '.';
77 Next += llvm::utostr(GCCVersion[2]);
78
79 // Use that if it exists, otherwise hope the user isn't linking.
80 //
81 // FIXME: Drop dependency on gcc's tool chain.
Daniel Dunbar080fb192009-10-22 00:12:00 +000082 Tmp = "/usr/lib/gcc/" + Next;
83 if (llvm::sys::Path(Tmp).exists())
Daniel Dunbar74782b02009-10-20 19:25:43 +000084 ToolChainDir = Next;
85 }
86
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000087 std::string Path;
88 if (getArchName() == "x86_64") {
89 Path = getHost().getDriver().Dir;
90 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000091 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000092 Path += "/x86_64";
93 getFilePaths().push_back(Path);
94
95 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000096 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000097 Path += "/x86_64";
98 getFilePaths().push_back(Path);
99 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000101 Path = getHost().getDriver().Dir;
102 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000103 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000104 getFilePaths().push_back(Path);
105
106 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000107 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000108 getFilePaths().push_back(Path);
109
110 Path = getHost().getDriver().Dir;
111 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000112 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000113 getProgramPaths().push_back(Path);
114
115 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000116 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000117 getProgramPaths().push_back(Path);
118
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000119 Path = getHost().getDriver().Dir;
120 Path += "/../libexec";
121 getProgramPaths().push_back(Path);
122
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000123 getProgramPaths().push_back(getHost().getDriver().Dir);
124}
125
Daniel Dunbarf3955282009-09-04 18:34:51 +0000126Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000127 // Free tool implementations.
128 for (llvm::DenseMap<unsigned, Tool*>::iterator
129 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
130 delete it->second;
131}
132
Daniel Dunbarf3955282009-09-04 18:34:51 +0000133Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000134 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000135 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000136 Key = Action::AnalyzeJobClass;
137 else
138 Key = JA.getKind();
139
140 Tool *&T = Tools[Key];
141 if (!T) {
142 switch (Key) {
143 case Action::InputClass:
144 case Action::BindArchClass:
145 assert(0 && "Invalid tool kind.");
146 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000147 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000148 case Action::AnalyzeJobClass:
149 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000150 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000151 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000152 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000153 case Action::AssembleJobClass:
Daniel Dunbar8cac5f72009-03-20 16:06:39 +0000154 T = new tools::darwin::Assemble(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000155 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000156 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000157 case Action::LipoJobClass:
158 T = new tools::darwin::Lipo(*this); break;
159 }
160 }
161
162 return *T;
163}
164
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000165void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
166 ArgStringList &CmdArgs) const {
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000167 // FIXME: Derive these correctly.
168 if (getArchName() == "x86_64") {
169 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
170 "/x86_64"));
171 // Intentionally duplicated for (temporary) gcc bug compatibility.
172 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
173 "/x86_64"));
174 }
175 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
176 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
177 // Intentionally duplicated for (temporary) gcc bug compatibility.
178 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
179 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
180 "/../../../" + ToolChainDir));
181 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
182 "/../../.."));
183}
184
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000185void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
186 ArgStringList &CmdArgs) const {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000187 unsigned MacosxVersionMin[3];
188 getMacosxVersionMin(Args, MacosxVersionMin);
189
190 // Derived from libgcc and lib specs but refactored.
191 if (Args.hasArg(options::OPT_static)) {
192 CmdArgs.push_back("-lgcc_static");
193 } else {
194 if (Args.hasArg(options::OPT_static_libgcc)) {
195 CmdArgs.push_back("-lgcc_eh");
196 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
197 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000198 if (isIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000199 CmdArgs.push_back("-lgcc_s.1");
200 } else {
201 CmdArgs.push_back("-lgcc_s.10.5");
202 }
203 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
204 // FIXME: -fexceptions -fno-exceptions means no exceptions
205 Args.hasArg(options::OPT_fexceptions) ||
206 Args.hasArg(options::OPT_fgnu_runtime)) {
207 // FIXME: This is probably broken on 10.3?
208 if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
209 CmdArgs.push_back("-lgcc_s.10.4");
210 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
211 CmdArgs.push_back("-lgcc_s.10.5");
212 } else {
213 if (isMacosxVersionLT(MacosxVersionMin, 10, 3, 9))
214 ; // Do nothing.
215 else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
216 CmdArgs.push_back("-lgcc_s.10.4");
217 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
218 CmdArgs.push_back("-lgcc_s.10.5");
219 }
220
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000221 if (isIPhoneOS() || isMacosxVersionLT(MacosxVersionMin, 10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000222 CmdArgs.push_back("-lgcc");
223 CmdArgs.push_back("-lSystem");
224 } else {
225 CmdArgs.push_back("-lSystem");
226 CmdArgs.push_back("-lgcc");
227 }
228 }
229}
230
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000231DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
232 const unsigned (&DarwinVersion)[3],
233 bool IsIPhoneOS)
234 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
235{
Daniel Dunbara92ba272009-09-29 18:52:10 +0000236 // Add the relative libexec dir (for clang-cc).
237 //
238 // FIXME: We should sink clang-cc into libexec/clang/<version>/.
239 std::string Path = getHost().getDriver().Dir;
240 Path += "/../libexec";
241 getProgramPaths().push_back(Path);
242
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000243 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
244 getProgramPaths().push_back(getHost().getDriver().Dir);
245}
246
247void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
248 ArgStringList &CmdArgs) const {
249 // The Clang toolchain uses explicit paths for internal libraries.
250}
251
252void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
253 ArgStringList &CmdArgs) const {
254 // Check for static linking.
255 if (Args.hasArg(options::OPT_static)) {
256 // FIXME: We need to have compiler-rt available (perhaps as
257 // libclang_static.a) to link against.
258 return;
259 }
260
261 // Reject -static-libgcc for now, we can deal with this when and if someone
262 // cares. This is useful in situations where someone wants to statically link
263 // something like libstdc++, and needs its runtime support routines.
264 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
265 getHost().getDriver().Diag(clang::diag::err_drv_unsupported_opt)
266 << A->getAsString(Args);
267 return;
268 }
269
270 // Otherwise link libSystem, which should have the support routines.
271 //
272 // FIXME: This is only true for 10.6 and beyond. Legacy support isn't
273 // critical, but it should work... we should just link in the static
274 // compiler-rt library.
275 CmdArgs.push_back("-lSystem");
276}
277
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000278void Darwin::getMacosxVersionMin(const ArgList &Args,
279 unsigned (&Res)[3]) const {
280 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
281 bool HadExtra;
282 if (!Driver::GetReleaseVersion(A->getValue(Args), Res[0], Res[1], Res[2],
283 HadExtra) ||
284 HadExtra) {
285 const Driver &D = getHost().getDriver();
286 D.Diag(clang::diag::err_drv_invalid_version_number)
287 << A->getAsString(Args);
288 }
289 } else
290 return getMacosxVersion(Res);
291}
292
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000293DerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
294 const char *BoundArch) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000295 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000296 const OptTable &Opts = getHost().getDriver().getOpts();
297
298 // FIXME: We really want to get out of the tool chain level argument
299 // translation business, as it makes the driver functionality much
300 // more opaque. For now, we follow gcc closely solely for the
301 // purpose of easily achieving feature parity & testability. Once we
302 // have something that works, we should reevaluate each translation
Mike Stump1eb44332009-09-09 15:08:12 +0000303 // and try to push it down into tool specific logic.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000304
Mike Stump1eb44332009-09-09 15:08:12 +0000305 Arg *OSXVersion =
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000306 Args.getLastArg(options::OPT_mmacosx_version_min_EQ, false);
307 Arg *iPhoneVersion =
Mike Stump1eb44332009-09-09 15:08:12 +0000308 Args.getLastArg(options::OPT_miphoneos_version_min_EQ, false);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000309 if (OSXVersion && iPhoneVersion) {
310 getHost().getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
311 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000312 << iPhoneVersion->getAsString(Args);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000313 } else if (!OSXVersion && !iPhoneVersion) {
314 // Chose the default version based on the arch.
315 //
Daniel Dunbar30392de2009-09-04 18:35:21 +0000316 // FIXME: Are there iPhone overrides for this?
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000317
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000318 if (!isIPhoneOS()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000319 // Look for MACOSX_DEPLOYMENT_TARGET, otherwise use the version
320 // from the host.
321 const char *Version = ::getenv("MACOSX_DEPLOYMENT_TARGET");
322 if (!Version)
323 Version = MacosxVersionMin.c_str();
324 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
325 DAL->append(DAL->MakeJoinedArg(0, O, Version));
326 } else {
327 const char *Version = IPhoneOSVersionMin.c_str();
328 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
329 DAL->append(DAL->MakeJoinedArg(0, O, Version));
330 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000333 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
334 Arg *A = *it;
335
336 if (A->getOption().matches(options::OPT_Xarch__)) {
337 // FIXME: Canonicalize name.
338 if (getArchName() != A->getValue(Args, 0))
339 continue;
340
341 // FIXME: The arg is leaked here, and we should have a nicer
342 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000343 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000344 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000346 // If the argument parsing failed or more than one argument was
347 // consumed, the -Xarch_ argument's parameter tried to consume
348 // extra arguments. Emit an error and ignore.
349 //
350 // We also want to disallow any options which would alter the
351 // driver behavior; that isn't going to work in our model. We
352 // use isDriverOption() as an approximation, although things
353 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000354 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000355 XarchArg->getOption().isDriverOption()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000356 getHost().getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000357 << A->getAsString(Args);
358 continue;
359 }
360
Daniel Dunbar478edc22009-03-29 22:29:05 +0000361 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000362 A = XarchArg;
Mike Stump1eb44332009-09-09 15:08:12 +0000363 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000364
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000365 // Sob. These is strictly gcc compatible for the time being. Apple
366 // gcc translates options twice, which means that self-expanding
367 // options add duplicates.
Daniel Dunbar1d189e12009-11-18 20:19:19 +0000368 switch ((options::ID) A->getOption().getId()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000369 default:
370 DAL->append(A);
371 break;
372
373 case options::OPT_mkernel:
374 case options::OPT_fapple_kext:
375 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000376 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
377 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000378 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000380 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000381 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000382 A->getValue(Args)));
383 break;
384
385 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000386 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
387 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000388 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
389 break;
390
391 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000392 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
393 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000394 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
395 break;
396
397 case options::OPT_fterminated_vtables:
398 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000399 DAL->append(DAL->MakeFlagArg(A,
400 Opts.getOption(options::OPT_fapple_kext)));
401 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000402 break;
403
404 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000405 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000406 break;
407
408 case options::OPT_fconstant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000409 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000410 Opts.getOption(options::OPT_mconstant_cfstrings)));
411 break;
412
413 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000414 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000415 Opts.getOption(options::OPT_mno_constant_cfstrings)));
416 break;
417
418 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000419 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000420 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
421 break;
422
423 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000424 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000425 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
426 break;
427
428 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000429 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000430 Opts.getOption(options::OPT_mpascal_strings)));
431 break;
432
433 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000434 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000435 Opts.getOption(options::OPT_mno_pascal_strings)));
436 break;
437 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000438 }
439
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000440 if (getTriple().getArch() == llvm::Triple::x86 ||
441 getTriple().getArch() == llvm::Triple::x86_64)
442 if (!Args.hasArg(options::OPT_mtune_EQ, false))
443 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
444 "core2"));
445
446 // Add the arch options based on the particular spelling of -arch, to match
447 // how the driver driver works.
448 if (BoundArch) {
449 llvm::StringRef Name = BoundArch;
450 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
451 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
452
453 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
454 // which defines the list of which architectures we accept.
455 if (Name == "ppc")
456 ;
457 else if (Name == "ppc601")
458 DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
459 else if (Name == "ppc603")
460 DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
461 else if (Name == "ppc604")
462 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
463 else if (Name == "ppc604e")
464 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
465 else if (Name == "ppc750")
466 DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
467 else if (Name == "ppc7400")
468 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
469 else if (Name == "ppc7450")
470 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
471 else if (Name == "ppc970")
472 DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
473
474 else if (Name == "ppc64")
Daniel Dunbar478edc22009-03-29 22:29:05 +0000475 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000476
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000477 else if (Name == "i386")
478 ;
479 else if (Name == "i486")
480 DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
481 else if (Name == "i586")
482 DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
483 else if (Name == "i686")
484 DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
485 else if (Name == "pentium")
486 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
487 else if (Name == "pentium2")
488 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
489 else if (Name == "pentpro")
490 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
491 else if (Name == "pentIIm3")
492 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
493
494 else if (Name == "x86_64")
495 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
496
497 else if (Name == "arm")
498 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
499 else if (Name == "armv4t")
500 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
501 else if (Name == "armv5")
502 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
503 else if (Name == "xscale")
504 DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
505 else if (Name == "armv6")
506 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
507 else if (Name == "armv7")
508 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
509
510 else
511 llvm::llvm_unreachable("invalid Darwin arch");
512 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000513
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000514 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000515}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000516
Daniel Dunbarf3955282009-09-04 18:34:51 +0000517bool Darwin::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000518 return false;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000519}
520
Daniel Dunbarf3955282009-09-04 18:34:51 +0000521bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000522 // FIXME: Gross; we should probably have some separate target
523 // definition, possibly even reusing the one in clang.
524 return getArchName() == "x86_64";
525}
526
Daniel Dunbarf3955282009-09-04 18:34:51 +0000527const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000528 return "pic";
529}
530
Daniel Dunbarf3955282009-09-04 18:34:51 +0000531const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000532 if (getArchName() == "x86_64")
533 return "pic";
534 return 0;
535}
536
Daniel Dunbar39176082009-03-20 00:20:03 +0000537/// Generic_GCC - A tool chain using the 'gcc' command to perform
538/// all subcommands; this relies on gcc translating the majority of
539/// command line options.
540
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000541Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000542 : ToolChain(Host, Triple) {
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000543 std::string Path(getHost().getDriver().Dir);
544 Path += "/../libexec";
545 getProgramPaths().push_back(Path);
546
Mike Stump1eb44332009-09-09 15:08:12 +0000547 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000548}
549
Daniel Dunbar39176082009-03-20 00:20:03 +0000550Generic_GCC::~Generic_GCC() {
551 // Free tool implementations.
552 for (llvm::DenseMap<unsigned, Tool*>::iterator
553 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
554 delete it->second;
555}
556
Mike Stump1eb44332009-09-09 15:08:12 +0000557Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000558 const JobAction &JA) const {
559 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000560 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000561 Key = Action::AnalyzeJobClass;
562 else
563 Key = JA.getKind();
564
565 Tool *&T = Tools[Key];
566 if (!T) {
567 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000568 case Action::InputClass:
569 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000570 assert(0 && "Invalid tool kind.");
571 case Action::PreprocessJobClass:
572 T = new tools::gcc::Preprocess(*this); break;
573 case Action::PrecompileJobClass:
574 T = new tools::gcc::Precompile(*this); break;
575 case Action::AnalyzeJobClass:
576 T = new tools::Clang(*this); break;
577 case Action::CompileJobClass:
578 T = new tools::gcc::Compile(*this); break;
579 case Action::AssembleJobClass:
580 T = new tools::gcc::Assemble(*this); break;
581 case Action::LinkJobClass:
582 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000584 // This is a bit ungeneric, but the only platform using a driver
585 // driver is Darwin.
586 case Action::LipoJobClass:
587 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000588 }
589 }
590
591 return *T;
592}
593
Daniel Dunbarf3955282009-09-04 18:34:51 +0000594bool Generic_GCC::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000595 return true;
Daniel Dunbar39176082009-03-20 00:20:03 +0000596}
597
598bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000599 // FIXME: Gross; we should probably have some separate target
600 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000601 return getArchName() == "x86_64";
602}
603
604const char *Generic_GCC::GetDefaultRelocationModel() const {
605 return "static";
606}
607
608const char *Generic_GCC::GetForcedPicModel() const {
609 return 0;
610}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000611
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000612DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
613 const char *BoundArch) const {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000614 return new DerivedArgList(Args, true);
615}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000616
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000617/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
618
619OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
620 : Generic_GCC(Host, Triple) {
621 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
622 getFilePaths().push_back("/usr/lib");
623}
624
625Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
626 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000627 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000628 Key = Action::AnalyzeJobClass;
629 else
630 Key = JA.getKind();
631
632 Tool *&T = Tools[Key];
633 if (!T) {
634 switch (Key) {
635 case Action::AssembleJobClass:
636 T = new tools::openbsd::Assemble(*this); break;
637 case Action::LinkJobClass:
638 T = new tools::openbsd::Link(*this); break;
639 default:
640 T = &Generic_GCC::SelectTool(C, JA);
641 }
642 }
643
644 return *T;
645}
646
Daniel Dunbar75358d22009-03-30 21:06:03 +0000647/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
648
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000649FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
650 : Generic_GCC(Host, Triple) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000651 if (Lib32) {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000652 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000653 getFilePaths().push_back("/usr/lib32");
654 } else {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000655 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000656 getFilePaths().push_back("/usr/lib");
657 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000658}
659
660Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
661 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000662 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000663 Key = Action::AnalyzeJobClass;
664 else
665 Key = JA.getKind();
666
667 Tool *&T = Tools[Key];
668 if (!T) {
669 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000670 case Action::AssembleJobClass:
671 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000672 case Action::LinkJobClass:
673 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000674 default:
675 T = &Generic_GCC::SelectTool(C, JA);
676 }
677 }
678
679 return *T;
680}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000681
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000682/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
683
684AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
685 : Generic_GCC(Host, Triple) {
686
687 // Path mangling to find libexec
688 std::string Path(getHost().getDriver().Dir);
689
690 Path += "/../libexec";
691 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000692 getProgramPaths().push_back(getHost().getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000693
694 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
695 getFilePaths().push_back("/usr/lib");
696 getFilePaths().push_back("/usr/sfw/lib");
697 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000698 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000699
700}
701
702Tool &AuroraUX::SelectTool(const Compilation &C, const JobAction &JA) const {
703 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000704 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000705 Key = Action::AnalyzeJobClass;
706 else
707 Key = JA.getKind();
708
709 Tool *&T = Tools[Key];
710 if (!T) {
711 switch (Key) {
712 case Action::AssembleJobClass:
713 T = new tools::auroraux::Assemble(*this); break;
714 case Action::LinkJobClass:
715 T = new tools::auroraux::Link(*this); break;
716 default:
717 T = &Generic_GCC::SelectTool(C, JA);
718 }
719 }
720
721 return *T;
722}
723
724
Eli Friedman6b3454a2009-05-26 07:52:18 +0000725/// Linux toolchain (very bare-bones at the moment).
726
727Linux::Linux(const HostInfo &Host, const llvm::Triple& Triple)
728 : Generic_GCC(Host, Triple) {
729 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib/clang/1.0/");
730 getFilePaths().push_back("/lib/");
731 getFilePaths().push_back("/usr/lib/");
Daniel Dunbara9822de2009-08-06 01:47:11 +0000732
733 // Depending on the Linux distribution, any combination of lib{,32,64} is
734 // possible. E.g. Debian uses lib and lib32 for mixed i386/x86-64 systems,
735 // openSUSE uses lib and lib64 for the same purpose.
736 getFilePaths().push_back("/lib32/");
737 getFilePaths().push_back("/usr/lib32/");
738 getFilePaths().push_back("/lib64/");
739 getFilePaths().push_back("/usr/lib64/");
740
Eli Friedman6b3454a2009-05-26 07:52:18 +0000741 // FIXME: Figure out some way to get gcc's libdir
742 // (e.g. /usr/lib/gcc/i486-linux-gnu/4.3/ for Ubuntu 32-bit); we need
743 // crtbegin.o/crtend.o/etc., and want static versions of various
744 // libraries. If we had our own crtbegin.o/crtend.o/etc, we could probably
745 // get away with using shared versions in /usr/lib, though.
746 // We could fall back to the approach we used for includes (a massive
747 // list), but that's messy at best.
748}
749
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000750/// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
751
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000752DragonFly::DragonFly(const HostInfo &Host, const llvm::Triple& Triple)
753 : Generic_GCC(Host, Triple) {
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000754
755 // Path mangling to find libexec
756 std::string Path(getHost().getDriver().Dir);
757
758 Path += "/../libexec";
759 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000760 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000761
762 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
763 getFilePaths().push_back("/usr/lib");
764 getFilePaths().push_back("/usr/lib/gcc41");
765}
766
767Tool &DragonFly::SelectTool(const Compilation &C, const JobAction &JA) const {
768 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000769 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000770 Key = Action::AnalyzeJobClass;
771 else
772 Key = JA.getKind();
773
774 Tool *&T = Tools[Key];
775 if (!T) {
776 switch (Key) {
777 case Action::AssembleJobClass:
778 T = new tools::dragonfly::Assemble(*this); break;
779 case Action::LinkJobClass:
780 T = new tools::dragonfly::Link(*this); break;
781 default:
782 T = &Generic_GCC::SelectTool(C, JA);
783 }
784 }
785
786 return *T;
787}