blob: 975a6c1fda7a5d7f39966acff48454d3d086f04b [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 Dunbarec069ed2009-03-25 06:58:31 +000020#include "llvm/Support/raw_ostream.h"
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000021#include "llvm/System/Path.h"
22
Daniel Dunbar39176082009-03-20 00:20:03 +000023using namespace clang::driver;
24using namespace clang::driver::toolchains;
25
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000026/// Darwin_X86 - Darwin tool chain for i386 and x86_64.
27
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000028Darwin_X86::Darwin_X86(const HostInfo &Host, const char *Arch,
29 const char *Platform, const char *OS,
30 const unsigned (&_DarwinVersion)[3],
31 const unsigned (&_GCCVersion)[3])
32 : ToolChain(Host, Arch, Platform, OS)
33{
34 DarwinVersion[0] = _DarwinVersion[0];
35 DarwinVersion[1] = _DarwinVersion[1];
36 DarwinVersion[2] = _DarwinVersion[2];
37 GCCVersion[0] = _GCCVersion[0];
38 GCCVersion[1] = _GCCVersion[1];
39 GCCVersion[2] = _GCCVersion[2];
40
Daniel Dunbar02633b52009-03-26 16:23:12 +000041 llvm::raw_string_ostream(MacosxVersionMin)
42 << "10." << DarwinVersion[0] - 4 << '.' << DarwinVersion[1];
43
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000044 ToolChainDir = "i686-apple-darwin";
45 ToolChainDir += llvm::utostr(DarwinVersion[0]);
46 ToolChainDir += "/";
47 ToolChainDir += llvm::utostr(GCCVersion[0]);
48 ToolChainDir += '.';
49 ToolChainDir += llvm::utostr(GCCVersion[1]);
50 ToolChainDir += '.';
51 ToolChainDir += llvm::utostr(GCCVersion[2]);
52
53 std::string Path;
54 if (getArchName() == "x86_64") {
55 Path = getHost().getDriver().Dir;
56 Path += "/../lib/gcc/";
57 Path += getToolChainDir();
58 Path += "/x86_64";
59 getFilePaths().push_back(Path);
60
61 Path = "/usr/lib/gcc/";
62 Path += getToolChainDir();
63 Path += "/x86_64";
64 getFilePaths().push_back(Path);
65 }
66
67 Path = getHost().getDriver().Dir;
68 Path += "/../lib/gcc/";
69 Path += getToolChainDir();
70 getFilePaths().push_back(Path);
71
72 Path = "/usr/lib/gcc/";
73 Path += getToolChainDir();
74 getFilePaths().push_back(Path);
75
76 Path = getHost().getDriver().Dir;
77 Path += "/../libexec/gcc/";
78 Path += getToolChainDir();
79 getProgramPaths().push_back(Path);
80
81 Path = "/usr/libexec/gcc/";
82 Path += getToolChainDir();
83 getProgramPaths().push_back(Path);
84
Daniel Dunbar82fa7c52009-03-24 04:07:10 +000085 Path = getHost().getDriver().Dir;
86 Path += "/../libexec";
87 getProgramPaths().push_back(Path);
88
Daniel Dunbarc50b00d2009-03-23 16:15:50 +000089 getProgramPaths().push_back(getHost().getDriver().Dir);
90}
91
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +000092Darwin_X86::~Darwin_X86() {
93 // Free tool implementations.
94 for (llvm::DenseMap<unsigned, Tool*>::iterator
95 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
96 delete it->second;
97}
98
99Tool &Darwin_X86::SelectTool(const Compilation &C,
100 const JobAction &JA) const {
101 Action::ActionClass Key;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +0000102 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getArchName()))
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000103 Key = Action::AnalyzeJobClass;
104 else
105 Key = JA.getKind();
106
107 Tool *&T = Tools[Key];
108 if (!T) {
109 switch (Key) {
110 case Action::InputClass:
111 case Action::BindArchClass:
112 assert(0 && "Invalid tool kind.");
113 case Action::PreprocessJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000114 T = new tools::darwin::Preprocess(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000115 case Action::AnalyzeJobClass:
116 T = new tools::Clang(*this); break;
Daniel Dunbar9120f172009-03-29 22:27:40 +0000117 case Action::PrecompileJobClass:
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000118 case Action::CompileJobClass:
Daniel Dunbar9120f172009-03-29 22:27:40 +0000119 T = new tools::darwin::Compile(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000120 case Action::AssembleJobClass:
Daniel Dunbar8cac5f72009-03-20 16:06:39 +0000121 T = new tools::darwin::Assemble(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000122 case Action::LinkJobClass:
Daniel Dunbar02633b52009-03-26 16:23:12 +0000123 T = new tools::darwin::Link(*this, MacosxVersionMin.c_str()); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000124 case Action::LipoJobClass:
125 T = new tools::darwin::Lipo(*this); break;
126 }
127 }
128
129 return *T;
130}
131
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000132DerivedArgList *Darwin_X86::TranslateArgs(InputArgList &Args) const {
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000133 DerivedArgList *DAL = new DerivedArgList(Args, false);
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000134 const OptTable &Opts = getHost().getDriver().getOpts();
135
136 // FIXME: We really want to get out of the tool chain level argument
137 // translation business, as it makes the driver functionality much
138 // more opaque. For now, we follow gcc closely solely for the
139 // purpose of easily achieving feature parity & testability. Once we
140 // have something that works, we should reevaluate each translation
141 // and try to push it down into tool specific logic.
142
Daniel Dunbarff8857a2009-04-10 20:11:50 +0000143 Arg *OSXVersion =
144 Args.getLastArg(options::OPT_mmacosx_version_min_EQ, false);
145 Arg *iPhoneVersion =
146 Args.getLastArg(options::OPT_miphoneos_version_min_EQ, false);
147 if (OSXVersion && iPhoneVersion) {
148 getHost().getDriver().Diag(clang::diag::err_drv_argument_not_allowed_with)
149 << OSXVersion->getAsString(Args)
150 << iPhoneVersion->getAsString(Args);
151 } else if (!OSXVersion && !iPhoneVersion) {
152 // Chose the default version based on the arch.
153 //
154 // FIXME: This will need to be fixed when we merge in arm support.
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000155 const Option *O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000156 DAL->append(DAL->MakeJoinedArg(0, O, MacosxVersionMin.c_str()));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000157 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000158
159 for (ArgList::iterator it = Args.begin(), ie = Args.end(); it != ie; ++it) {
160 Arg *A = *it;
161
162 if (A->getOption().matches(options::OPT_Xarch__)) {
163 // FIXME: Canonicalize name.
164 if (getArchName() != A->getValue(Args, 0))
165 continue;
166
167 // FIXME: The arg is leaked here, and we should have a nicer
168 // interface for this.
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000169 unsigned Prev, Index = Prev = A->getIndex() + 1;
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000170 Arg *XarchArg = Opts.ParseOneArg(Args, Index);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000171
172 // If the argument parsing failed or more than one argument was
173 // consumed, the -Xarch_ argument's parameter tried to consume
174 // extra arguments. Emit an error and ignore.
175 //
176 // We also want to disallow any options which would alter the
177 // driver behavior; that isn't going to work in our model. We
178 // use isDriverOption() as an approximation, although things
179 // like -O4 are going to slip through.
180 if (!XarchArg || Index > Prev + 1 ||
181 XarchArg->getOption().isDriverOption()) {
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000182 getHost().getDriver().Diag(clang::diag::err_drv_invalid_Xarch_argument)
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000183 << A->getAsString(Args);
184 continue;
185 }
186
Daniel Dunbar478edc22009-03-29 22:29:05 +0000187 XarchArg->setBaseArg(A);
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000188 A = XarchArg;
189 }
190
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000191 // Sob. These is strictly gcc compatible for the time being. Apple
192 // gcc translates options twice, which means that self-expanding
193 // options add duplicates.
194 options::ID id = A->getOption().getId();
195 switch (id) {
196 default:
197 DAL->append(A);
198 break;
199
200 case options::OPT_mkernel:
201 case options::OPT_fapple_kext:
202 DAL->append(A);
Daniel Dunbar478edc22009-03-29 22:29:05 +0000203 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
204 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000205 break;
206
207 case options::OPT_dependency_file:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000208 DAL->append(DAL->MakeSeparateArg(A, Opts.getOption(options::OPT_MF),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000209 A->getValue(Args)));
210 break;
211
212 case options::OPT_gfull:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000213 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
214 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000215 Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols)));
216 break;
217
218 case options::OPT_gused:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000219 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_g_Flag)));
220 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000221 Opts.getOption(options::OPT_feliminate_unused_debug_symbols)));
222 break;
223
224 case options::OPT_fterminated_vtables:
225 case options::OPT_findirect_virtual_calls:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000226 DAL->append(DAL->MakeFlagArg(A,
227 Opts.getOption(options::OPT_fapple_kext)));
228 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_static)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000229 break;
230
231 case options::OPT_shared:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000232 DAL->append(DAL->MakeFlagArg(A, Opts.getOption(options::OPT_dynamiclib)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000233 break;
234
235 case options::OPT_fconstant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000236 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000237 Opts.getOption(options::OPT_mconstant_cfstrings)));
238 break;
239
240 case options::OPT_fno_constant_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000241 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000242 Opts.getOption(options::OPT_mno_constant_cfstrings)));
243 break;
244
245 case options::OPT_Wnonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000246 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000247 Opts.getOption(options::OPT_mwarn_nonportable_cfstrings)));
248 break;
249
250 case options::OPT_Wno_nonportable_cfstrings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000251 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000252 Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings)));
253 break;
254
255 case options::OPT_fpascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000256 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000257 Opts.getOption(options::OPT_mpascal_strings)));
258 break;
259
260 case options::OPT_fno_pascal_strings:
Daniel Dunbar478edc22009-03-29 22:29:05 +0000261 DAL->append(DAL->MakeFlagArg(A,
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000262 Opts.getOption(options::OPT_mno_pascal_strings)));
263 break;
264 }
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000265 }
266
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000267 // FIXME: Actually, gcc always adds this, but it is filtered for
268 // duplicates somewhere. This also changes the order of things, so
269 // look it up.
270 if (getArchName() == "x86_64")
271 if (!Args.hasArg(options::OPT_m64, false))
Daniel Dunbar478edc22009-03-29 22:29:05 +0000272 DAL->append(DAL->MakeFlagArg(0, Opts.getOption(options::OPT_m64)));
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000273
274 if (!Args.hasArg(options::OPT_mtune_EQ, false))
Daniel Dunbar478edc22009-03-29 22:29:05 +0000275 DAL->append(DAL->MakeJoinedArg(0, Opts.getOption(options::OPT_mtune_EQ),
Daniel Dunbarec069ed2009-03-25 06:58:31 +0000276 "core2"));
277
Daniel Dunbar4e7e9cf2009-03-25 06:12:34 +0000278 return DAL;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000279}
280
281bool Darwin_X86::IsMathErrnoDefault() const {
282 return false;
283}
284
285bool Darwin_X86::IsUnwindTablesDefault() const {
286 // FIXME: Gross; we should probably have some separate target
287 // definition, possibly even reusing the one in clang.
288 return getArchName() == "x86_64";
289}
290
291const char *Darwin_X86::GetDefaultRelocationModel() const {
292 return "pic";
293}
294
295const char *Darwin_X86::GetForcedPicModel() const {
296 if (getArchName() == "x86_64")
297 return "pic";
298 return 0;
299}
300
Daniel Dunbar39176082009-03-20 00:20:03 +0000301/// Generic_GCC - A tool chain using the 'gcc' command to perform
302/// all subcommands; this relies on gcc translating the majority of
303/// command line options.
304
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000305Generic_GCC::Generic_GCC(const HostInfo &Host, const char *Arch,
306 const char *Platform, const char *OS)
307 : ToolChain(Host, Arch, Platform, OS)
308{
Daniel Dunbar82fa7c52009-03-24 04:07:10 +0000309 std::string Path(getHost().getDriver().Dir);
310 Path += "/../libexec";
311 getProgramPaths().push_back(Path);
312
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000313 getProgramPaths().push_back(getHost().getDriver().Dir);
314}
315
Daniel Dunbar39176082009-03-20 00:20:03 +0000316Generic_GCC::~Generic_GCC() {
317 // Free tool implementations.
318 for (llvm::DenseMap<unsigned, Tool*>::iterator
319 it = Tools.begin(), ie = Tools.end(); it != ie; ++it)
320 delete it->second;
321}
322
323Tool &Generic_GCC::SelectTool(const Compilation &C,
324 const JobAction &JA) const {
325 Action::ActionClass Key;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +0000326 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getArchName()))
Daniel Dunbar39176082009-03-20 00:20:03 +0000327 Key = Action::AnalyzeJobClass;
328 else
329 Key = JA.getKind();
330
331 Tool *&T = Tools[Key];
332 if (!T) {
333 switch (Key) {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000334 case Action::InputClass:
335 case Action::BindArchClass:
Daniel Dunbar39176082009-03-20 00:20:03 +0000336 assert(0 && "Invalid tool kind.");
337 case Action::PreprocessJobClass:
338 T = new tools::gcc::Preprocess(*this); break;
339 case Action::PrecompileJobClass:
340 T = new tools::gcc::Precompile(*this); break;
341 case Action::AnalyzeJobClass:
342 T = new tools::Clang(*this); break;
343 case Action::CompileJobClass:
344 T = new tools::gcc::Compile(*this); break;
345 case Action::AssembleJobClass:
346 T = new tools::gcc::Assemble(*this); break;
347 case Action::LinkJobClass:
348 T = new tools::gcc::Link(*this); break;
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000349
350 // This is a bit ungeneric, but the only platform using a driver
351 // driver is Darwin.
352 case Action::LipoJobClass:
353 T = new tools::darwin::Lipo(*this); break;
Daniel Dunbar39176082009-03-20 00:20:03 +0000354 }
355 }
356
357 return *T;
358}
359
360bool Generic_GCC::IsMathErrnoDefault() const {
361 return true;
362}
363
364bool Generic_GCC::IsUnwindTablesDefault() const {
Daniel Dunbar8eddb3f2009-03-20 00:57:52 +0000365 // FIXME: Gross; we should probably have some separate target
366 // definition, possibly even reusing the one in clang.
Daniel Dunbar39176082009-03-20 00:20:03 +0000367 return getArchName() == "x86_64";
368}
369
370const char *Generic_GCC::GetDefaultRelocationModel() const {
371 return "static";
372}
373
374const char *Generic_GCC::GetForcedPicModel() const {
375 return 0;
376}
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000377
378DerivedArgList *Generic_GCC::TranslateArgs(InputArgList &Args) const {
379 return new DerivedArgList(Args, true);
380}
Daniel Dunbar75358d22009-03-30 21:06:03 +0000381
382/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
383
384FreeBSD::FreeBSD(const HostInfo &Host, const char *Arch,
385 const char *Platform, const char *OS, bool Lib32)
386 : Generic_GCC(Host, Arch, Platform, OS) {
Daniel Dunbarbc534662009-04-02 18:30:04 +0000387 if (Lib32) {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000388 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib32");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000389 getFilePaths().push_back("/usr/lib32");
390 } else {
Daniel Dunbar75358d22009-03-30 21:06:03 +0000391 getFilePaths().push_back(getHost().getDriver().Dir + "/../lib");
Daniel Dunbarbc534662009-04-02 18:30:04 +0000392 getFilePaths().push_back("/usr/lib");
393 }
Daniel Dunbar75358d22009-03-30 21:06:03 +0000394}
395
396Tool &FreeBSD::SelectTool(const Compilation &C, const JobAction &JA) const {
397 Action::ActionClass Key;
398 if (getHost().getDriver().ShouldUseClangCompiler(C, JA, getArchName()))
399 Key = Action::AnalyzeJobClass;
400 else
401 Key = JA.getKind();
402
403 Tool *&T = Tools[Key];
404 if (!T) {
405 switch (Key) {
Daniel Dunbar68a31d42009-03-31 17:45:15 +0000406 case Action::AssembleJobClass:
407 T = new tools::freebsd::Assemble(*this); break;
Daniel Dunbar008f54a2009-04-01 19:36:32 +0000408 case Action::LinkJobClass:
409 T = new tools::freebsd::Link(*this); break;
Daniel Dunbar75358d22009-03-30 21:06:03 +0000410 default:
411 T = &Generic_GCC::SelectTool(C, JA);
412 }
413 }
414
415 return *T;
416}