blob: 6acbc19beaa74e7713bb50309f52bf666e64b1ef [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 Dunbar30392de2009-09-04 18:35:21 +000057
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
67 std::string Path;
68 if (getArchName() == "x86_64") {
69 Path = getHost().getDriver().Dir;
70 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000071 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000072 Path += "/x86_64";
73 getFilePaths().push_back(Path);
74
75 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000076 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000077 Path += "/x86_64";
78 getFilePaths().push_back(Path);
79 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000081 Path = getHost().getDriver().Dir;
82 Path += "/../lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000083 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000084 getFilePaths().push_back(Path);
85
86 Path = "/usr/lib/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000087 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000088 getFilePaths().push_back(Path);
89
90 Path = getHost().getDriver().Dir;
91 Path += "/../libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000092 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000093 getProgramPaths().push_back(Path);
94
95 Path = "/usr/libexec/gcc/";
Daniel Dunbar6b200b22009-09-18 08:14:36 +000096 Path += ToolChainDir;
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000097 getProgramPaths().push_back(Path);
98
Daniel Dunbar82fa7c52009-03-24 04:07:10 +000099 Path = getHost().getDriver().Dir;
100 Path += "/../libexec";
101 getProgramPaths().push_back(Path);
102
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000103 getProgramPaths().push_back(getHost().getDriver().Dir);
104}
105
Daniel Dunbarf3955282009-09-04 18:34:51 +0000106Darwin::~Darwin() {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000107 // Free tool implementations.
108 for (llvm::DenseMap<unsigned, Tool*>::iterator
109 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
110 delete it->second;
111}
112
Daniel Dunbarf3955282009-09-04 18:34:51 +0000113Tool &Darwin::SelectTool(const Compilation &C, const JobAction &JA) const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000114 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000115 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000116 Key = Action::AnalyzeJobClass;
117 else
118 Key = JA.getKind();
119
120 Tool *&T = Tools[Key];
121 if (!T) {
122 switch (Key) {
123 case Action::InputClass:
124 case Action::BindArchClass:
125 assert(0 && "Invalid tool kind.");
126 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000127 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000128 case Action::AnalyzeJobClass:
129 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000130 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000131 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000132 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000133 case Action::AssembleJobClass:
Daniel Dunbar8cac5f72009-03-20 16:06:39 +0000134 T = new tools::darwin::Assemble(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000135 case Action::LinkJobClass:
Daniel Dunbar8f289622009-09-04 17:39:02 +0000136 T = new tools::darwin::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000137 case Action::LipoJobClass:
138 T = new tools::darwin::Lipo(*this); break;
139 }
140 }
141
142 return *T;
143}
144
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000145void DarwinGCC::AddLinkSearchPathArgs(const ArgList &Args,
146 ArgStringList &CmdArgs) const {
Daniel Dunbar6b200b22009-09-18 08:14:36 +0000147 // FIXME: Derive these correctly.
148 if (getArchName() == "x86_64") {
149 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
150 "/x86_64"));
151 // Intentionally duplicated for (temporary) gcc bug compatibility.
152 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
153 "/x86_64"));
154 }
155 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/" + ToolChainDir));
156 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
157 // Intentionally duplicated for (temporary) gcc bug compatibility.
158 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir));
159 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
160 "/../../../" + ToolChainDir));
161 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc/" + ToolChainDir +
162 "/../../.."));
163}
164
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000165void DarwinGCC::AddLinkRuntimeLibArgs(const ArgList &Args,
166 ArgStringList &CmdArgs) const {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000167 unsigned MacosxVersionMin[3];
168 getMacosxVersionMin(Args, MacosxVersionMin);
169
170 // Derived from libgcc and lib specs but refactored.
171 if (Args.hasArg(options::OPT_static)) {
172 CmdArgs.push_back("-lgcc_static");
173 } else {
174 if (Args.hasArg(options::OPT_static_libgcc)) {
175 CmdArgs.push_back("-lgcc_eh");
176 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
177 // Derived from darwin_iphoneos_libgcc spec.
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000178 if (isIPhoneOS()) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000179 CmdArgs.push_back("-lgcc_s.1");
180 } else {
181 CmdArgs.push_back("-lgcc_s.10.5");
182 }
183 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
184 // FIXME: -fexceptions -fno-exceptions means no exceptions
185 Args.hasArg(options::OPT_fexceptions) ||
186 Args.hasArg(options::OPT_fgnu_runtime)) {
187 // FIXME: This is probably broken on 10.3?
188 if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
189 CmdArgs.push_back("-lgcc_s.10.4");
190 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
191 CmdArgs.push_back("-lgcc_s.10.5");
192 } else {
193 if (isMacosxVersionLT(MacosxVersionMin, 10, 3, 9))
194 ; // Do nothing.
195 else if (isMacosxVersionLT(MacosxVersionMin, 10, 5))
196 CmdArgs.push_back("-lgcc_s.10.4");
197 else if (isMacosxVersionLT(MacosxVersionMin, 10, 6))
198 CmdArgs.push_back("-lgcc_s.10.5");
199 }
200
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000201 if (isIPhoneOS() || isMacosxVersionLT(MacosxVersionMin, 10, 6)) {
Daniel Dunbar6cd41542009-09-18 08:15:03 +0000202 CmdArgs.push_back("-lgcc");
203 CmdArgs.push_back("-lSystem");
204 } else {
205 CmdArgs.push_back("-lSystem");
206 CmdArgs.push_back("-lgcc");
207 }
208 }
209}
210
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000211DarwinClang::DarwinClang(const HostInfo &Host, const llvm::Triple& Triple,
212 const unsigned (&DarwinVersion)[3],
213 bool IsIPhoneOS)
214 : Darwin(Host, Triple, DarwinVersion, IsIPhoneOS)
215{
Daniel Dunbara92ba272009-09-29 18:52:10 +0000216 // Add the relative libexec dir (for clang-cc).
217 //
218 // FIXME: We should sink clang-cc into libexec/clang/<version>/.
219 std::string Path = getHost().getDriver().Dir;
220 Path += "/../libexec";
221 getProgramPaths().push_back(Path);
222
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000223 // We expect 'as', 'ld', etc. to be adjacent to our install dir.
224 getProgramPaths().push_back(getHost().getDriver().Dir);
225}
226
227void DarwinClang::AddLinkSearchPathArgs(const ArgList &Args,
228 ArgStringList &CmdArgs) const {
229 // The Clang toolchain uses explicit paths for internal libraries.
230}
231
232void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
233 ArgStringList &CmdArgs) const {
234 // Check for static linking.
235 if (Args.hasArg(options::OPT_static)) {
236 // FIXME: We need to have compiler-rt available (perhaps as
237 // libclang_static.a) to link against.
238 return;
239 }
240
241 // Reject -static-libgcc for now, we can deal with this when and if someone
242 // cares. This is useful in situations where someone wants to statically link
243 // something like libstdc++, and needs its runtime support routines.
244 if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
245 getHost().getDriver().Diag(clang::diag::err_drv_unsupported_opt)
246 << A->getAsString(Args);
247 return;
248 }
249
250 // Otherwise link libSystem, which should have the support routines.
251 //
252 // FIXME: This is only true for 10.6 and beyond. Legacy support isn't
253 // critical, but it should work... we should just link in the static
254 // compiler-rt library.
255 CmdArgs.push_back("-lSystem");
256}
257
Daniel Dunbar48d5aae2009-09-18 08:14:46 +0000258void Darwin::getMacosxVersionMin(const ArgList &Args,
259 unsigned (&Res)[3]) const {
260 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
261 bool HadExtra;
262 if (!Driver::GetReleaseVersion(A->getValue(Args), Res[0], Res[1], Res[2],
263 HadExtra) ||
264 HadExtra) {
265 const Driver &D = getHost().getDriver();
266 D.Diag(clang::diag::err_drv_invalid_version_number)
267 << A->getAsString(Args);
268 }
269 } else
270 return getMacosxVersion(Res);
271}
272
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000273DerivedArgList *Darwin::TranslateArgs(InputArgList &Args,
274 const char *BoundArch) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000275 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000276 const OptTable &Opts = getHost().getDriver().getOpts();
277
278 // FIXME: We really want to get out of the tool chain level argument
279 // translation business, as it makes the driver functionality much
280 // more opaque. For now, we follow gcc closely solely for the
281 // purpose of easily achieving feature parity & testability. Once we
282 // have something that works, we should reevaluate each translation
Mike Stump1eb44332009-09-09 15:08:12 +0000283 // and try to push it down into tool specific logic.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000284
Mike Stump1eb44332009-09-09 15:08:12 +0000285 Arg *OSXVersion =
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000286 Args.getLastArg(options::OPT_mmacosx_version_min_EQ, false);
287 Arg *iPhoneVersion =
Mike Stump1eb44332009-09-09 15:08:12 +0000288 Args.getLastArg(options::OPT_miphoneos_version_min_EQ, false);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000289 if (OSXVersion && iPhoneVersion) {
290 getHost().getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
291 << OSXVersion->getAsString(Args)
Mike Stump1eb44332009-09-09 15:08:12 +0000292 << iPhoneVersion->getAsString(Args);
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000293 } else if (!OSXVersion && !iPhoneVersion) {
294 // Chose the default version based on the arch.
295 //
Daniel Dunbar30392de2009-09-04 18:35:21 +0000296 // FIXME: Are there iPhone overrides for this?
Daniel Dunbarf36a06a2009-04-10 21:00:07 +0000297
Daniel Dunbar1d4612b2009-09-18 08:15:13 +0000298 if (!isIPhoneOS()) {
Daniel Dunbar30392de2009-09-04 18:35:21 +0000299 // Look for MACOSX_DEPLOYMENT_TARGET, otherwise use the version
300 // from the host.
301 const char *Version = ::getenv("MACOSX_DEPLOYMENT_TARGET");
302 if (!Version)
303 Version = MacosxVersionMin.c_str();
304 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
305 DAL->append(DAL->MakeJoinedArg(0, O, Version));
306 } else {
307 const char *Version = IPhoneOSVersionMin.c_str();
308 const Option *O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
309 DAL->append(DAL->MakeJoinedArg(0, O, Version));
310 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000313 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
314 Arg *A = *it;
315
316 if (A->getOption().matches(options::OPT_Xarch__)) {
317 // FIXME: Canonicalize name.
318 if (getArchName() != A->getValue(Args, 0))
319 continue;
320
321 // FIXME: The arg is leaked here, and we should have a nicer
322 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000323 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000324 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000326 // If the argument parsing failed or more than one argument was
327 // consumed, the -Xarch_ argument's parameter tried to consume
328 // extra arguments. Emit an error and ignore.
329 //
330 // We also want to disallow any options which would alter the
331 // driver behavior; that isn't going to work in our model. We
332 // use isDriverOption() as an approximation, although things
333 // like -O4 are going to slip through.
Mike Stump1eb44332009-09-09 15:08:12 +0000334 if (!XarchArg || Index > Prev + 1 ||
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000335 XarchArg->getOption().isDriverOption()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000336 getHost().getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000337 << A->getAsString(Args);
338 continue;
339 }
340
Daniel Dunbar478edc22009-03-29 22:29:05 +0000341 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000342 A = XarchArg;
Mike Stump1eb44332009-09-09 15:08:12 +0000343 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000344
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000345 // Sob. These is strictly gcc compatible for the time being. Apple
346 // gcc translates options twice, which means that self-expanding
347 // options add duplicates.
348 options::ID id = A->getOption().getId();
349 switch (id) {
350 default:
351 DAL->append(A);
352 break;
353
354 case options::OPT_mkernel:
355 case options::OPT_fapple_kext:
356 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000357 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
358 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000359 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000361 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000362 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000363 A->getValue(Args)));
364 break;
365
366 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000367 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
368 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000369 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
370 break;
371
372 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000373 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
374 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000375 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
376 break;
377
378 case options::OPT_fterminated_vtables:
379 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000380 DAL->append(DAL->MakeFlagArg(A,
381 Opts.getOption(options::OPT_fapple_kext)));
382 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000383 break;
384
385 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000386 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000387 break;
388
389 case options::OPT_fconstant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000390 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000391 Opts.getOption(options::OPT_mconstant_cfstrings)));
392 break;
393
394 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000395 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000396 Opts.getOption(options::OPT_mno_constant_cfstrings)));
397 break;
398
399 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000400 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000401 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
402 break;
403
404 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000405 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000406 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
407 break;
408
409 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000410 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000411 Opts.getOption(options::OPT_mpascal_strings)));
412 break;
413
414 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000415 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000416 Opts.getOption(options::OPT_mno_pascal_strings)));
417 break;
418 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000419 }
420
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000421 if (getTriple().getArch() == llvm::Triple::x86 ||
422 getTriple().getArch() == llvm::Triple::x86_64)
423 if (!Args.hasArg(options::OPT_mtune_EQ, false))
424 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
425 "core2"));
426
427 // Add the arch options based on the particular spelling of -arch, to match
428 // how the driver driver works.
429 if (BoundArch) {
430 llvm::StringRef Name = BoundArch;
431 const Option *MCpu = Opts.getOption(options::OPT_mcpu_EQ);
432 const Option *MArch = Opts.getOption(options::OPT_march_EQ);
433
434 // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
435 // which defines the list of which architectures we accept.
436 if (Name == "ppc")
437 ;
438 else if (Name == "ppc601")
439 DAL->append(DAL->MakeJoinedArg(0, MCpu, "601"));
440 else if (Name == "ppc603")
441 DAL->append(DAL->MakeJoinedArg(0, MCpu, "603"));
442 else if (Name == "ppc604")
443 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604"));
444 else if (Name == "ppc604e")
445 DAL->append(DAL->MakeJoinedArg(0, MCpu, "604e"));
446 else if (Name == "ppc750")
447 DAL->append(DAL->MakeJoinedArg(0, MCpu, "750"));
448 else if (Name == "ppc7400")
449 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7400"));
450 else if (Name == "ppc7450")
451 DAL->append(DAL->MakeJoinedArg(0, MCpu, "7450"));
452 else if (Name == "ppc970")
453 DAL->append(DAL->MakeJoinedArg(0, MCpu, "970"));
454
455 else if (Name == "ppc64")
Daniel Dunbar478edc22009-03-29 22:29:05 +0000456 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000457
Daniel Dunbar84ec96c2009-09-09 22:33:15 +0000458 else if (Name == "i386")
459 ;
460 else if (Name == "i486")
461 DAL->append(DAL->MakeJoinedArg(0, MArch, "i486"));
462 else if (Name == "i586")
463 DAL->append(DAL->MakeJoinedArg(0, MArch, "i586"));
464 else if (Name == "i686")
465 DAL->append(DAL->MakeJoinedArg(0, MArch, "i686"));
466 else if (Name == "pentium")
467 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium"));
468 else if (Name == "pentium2")
469 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
470 else if (Name == "pentpro")
471 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentiumpro"));
472 else if (Name == "pentIIm3")
473 DAL->append(DAL->MakeJoinedArg(0, MArch, "pentium2"));
474
475 else if (Name == "x86_64")
476 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
477
478 else if (Name == "arm")
479 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
480 else if (Name == "armv4t")
481 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv4t"));
482 else if (Name == "armv5")
483 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv5tej"));
484 else if (Name == "xscale")
485 DAL->append(DAL->MakeJoinedArg(0, MArch, "xscale"));
486 else if (Name == "armv6")
487 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv6k"));
488 else if (Name == "armv7")
489 DAL->append(DAL->MakeJoinedArg(0, MArch, "armv7a"));
490
491 else
492 llvm::llvm_unreachable("invalid Darwin arch");
493 }
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000494
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000495 return DAL;
Mike Stump1eb44332009-09-09 15:08:12 +0000496}
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000497
Daniel Dunbarf3955282009-09-04 18:34:51 +0000498bool Darwin::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000499 return false;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000500}
501
Daniel Dunbarf3955282009-09-04 18:34:51 +0000502bool Darwin::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000503 // FIXME: Gross; we should probably have some separate target
504 // definition, possibly even reusing the one in clang.
505 return getArchName() == "x86_64";
506}
507
Daniel Dunbarf3955282009-09-04 18:34:51 +0000508const char *Darwin::GetDefaultRelocationModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000509 return "pic";
510}
511
Daniel Dunbarf3955282009-09-04 18:34:51 +0000512const char *Darwin::GetForcedPicModel() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000513 if (getArchName() == "x86_64")
514 return "pic";
515 return 0;
516}
517
Daniel Dunbar39176082009-03-20 00:20:03 +0000518/// Generic_GCC - A tool chain using the 'gcc' command to perform
519/// all subcommands; this relies on gcc translating the majority of
520/// command line options.
521
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000522Generic_GCC::Generic_GCC(const HostInfo &Host, const llvm::Triple& Triple)
Mike Stump1eb44332009-09-09 15:08:12 +0000523 : ToolChain(Host, Triple) {
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000524 std::string Path(getHost().getDriver().Dir);
525 Path += "/../libexec";
526 getProgramPaths().push_back(Path);
527
Mike Stump1eb44332009-09-09 15:08:12 +0000528 getProgramPaths().push_back(getHost().getDriver().Dir);
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000529}
530
Daniel Dunbar39176082009-03-20 00:20:03 +0000531Generic_GCC::~Generic_GCC() {
532 // Free tool implementations.
533 for (llvm::DenseMap<unsigned, Tool*>::iterator
534 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
535 delete it->second;
536}
537
Mike Stump1eb44332009-09-09 15:08:12 +0000538Tool &Generic_GCC::SelectTool(const Compilation &C,
Daniel Dunbar39176082009-03-20 00:20:03 +0000539 const JobAction &JA) const {
540 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000541 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000542 Key = Action::AnalyzeJobClass;
543 else
544 Key = JA.getKind();
545
546 Tool *&T = Tools[Key];
547 if (!T) {
548 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000549 case Action::InputClass:
550 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000551 assert(0 && "Invalid tool kind.");
552 case Action::PreprocessJobClass:
553 T = new tools::gcc::Preprocess(*this); break;
554 case Action::PrecompileJobClass:
555 T = new tools::gcc::Precompile(*this); break;
556 case Action::AnalyzeJobClass:
557 T = new tools::Clang(*this); break;
558 case Action::CompileJobClass:
559 T = new tools::gcc::Compile(*this); break;
560 case Action::AssembleJobClass:
561 T = new tools::gcc::Assemble(*this); break;
562 case Action::LinkJobClass:
563 T = new tools::gcc::Link(*this); break;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000565 // This is a bit ungeneric, but the only platform using a driver
566 // driver is Darwin.
567 case Action::LipoJobClass:
568 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000569 }
570 }
571
572 return *T;
573}
574
Daniel Dunbarf3955282009-09-04 18:34:51 +0000575bool Generic_GCC::IsMathErrnoDefault() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000576 return true;
Daniel Dunbar39176082009-03-20 00:20:03 +0000577}
578
579bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000580 // FIXME: Gross; we should probably have some separate target
581 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000582 return getArchName() == "x86_64";
583}
584
585const char *Generic_GCC::GetDefaultRelocationModel() const {
586 return "static";
587}
588
589const char *Generic_GCC::GetForcedPicModel() const {
590 return 0;
591}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000592
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +0000593DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args,
594 const char *BoundArch) const {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000595 return new DerivedArgList(Args, true);
596}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000597
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000598/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
599
600OpenBSD::OpenBSD(const HostInfo &Host, const llvm::Triple& Triple)
601 : Generic_GCC(Host, Triple) {
602 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
603 getFilePaths().push_back("/usr/lib");
604}
605
606Tool &OpenBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
607 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000608 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +0000609 Key = Action::AnalyzeJobClass;
610 else
611 Key = JA.getKind();
612
613 Tool *&T = Tools[Key];
614 if (!T) {
615 switch (Key) {
616 case Action::AssembleJobClass:
617 T = new tools::openbsd::Assemble(*this); break;
618 case Action::LinkJobClass:
619 T = new tools::openbsd::Link(*this); break;
620 default:
621 T = &Generic_GCC::SelectTool(C, JA);
622 }
623 }
624
625 return *T;
626}
627
Daniel Dunbar75358d22009-03-30 21:06:03 +0000628/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
629
Daniel Dunbarcb8ab232009-05-22 02:53:45 +0000630FreeBSD::FreeBSD(const HostInfo &Host, const llvm::Triple& Triple, bool Lib32)
631 : Generic_GCC(Host, Triple) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000632 if (Lib32) {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000633 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000634 getFilePaths().push_back("/usr/lib32");
635 } else {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000636 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000637 getFilePaths().push_back("/usr/lib");
638 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000639}
640
641Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
642 Action::ActionClass Key;
Daniel Dunbara6046be2009-09-08 23:36:55 +0000643 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getTriple()))
Daniel Dunbar75358d22009-03-30 21:06:03 +0000644 Key = Action::AnalyzeJobClass;
645 else
646 Key = JA.getKind();
647
648 Tool *&T = Tools[Key];
649 if (!T) {
650 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000651 case Action::AssembleJobClass:
652 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000653 case Action::LinkJobClass:
654 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000655 default:
656 T = &Generic_GCC::SelectTool(C, JA);
657 }
658 }
659
660 return *T;
661}
Daniel Dunbar11e1b402009-05-02 18:28:39 +0000662
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000663/// AuroraUX - AuroraUX tool chain which can call as(1) and ld(1) directly.
664
665AuroraUX::AuroraUX(const HostInfo &Host, const llvm::Triple& Triple)
666 : Generic_GCC(Host, Triple) {
667
668 // Path mangling to find libexec
669 std::string Path(getHost().getDriver().Dir);
670
671 Path += "/../libexec";
672 getProgramPaths().push_back(Path);
Mike Stump1eb44332009-09-09 15:08:12 +0000673 getProgramPaths().push_back(getHost().getDriver().Dir);
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000674
675 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
676 getFilePaths().push_back("/usr/lib");
677 getFilePaths().push_back("/usr/sfw/lib");
678 getFilePaths().push_back("/opt/gcc4/lib");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +0000679 getFilePaths().push_back("/opt/gcc4/lib/gcc/i386-pc-solaris2.11/4.2.4");
Edward O'Callaghane7925a02009-08-22 01:06:46 +0000680
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}