blob: f9a82ff78d437e0de429b768c9087ed594fabe9f [file] [log] [blame]
Daniel Dunbar63c4da92009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
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
Daniel Dunbar63c4da92009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar63c4da92009-03-02 19:59:07 +000011
Daniel Dunbardb62cc32009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar93468492009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbard25acaa2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000024
Daniel Dunbarb1873cd2009-03-13 20:33:35 +000025#include "llvm/ADT/StringSet.h"
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000026#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000028#include "llvm/System/Path.h"
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +000029#include "llvm/System/Program.h"
Daniel Dunbar494646b2009-03-13 12:19:02 +000030
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000031#include "InputInfo.h"
32
Daniel Dunbar494646b2009-03-13 12:19:02 +000033#include <map>
34
Daniel Dunbard6f0e372009-03-04 20:49:20 +000035using namespace clang::driver;
Chris Lattnerc272d262009-03-26 05:56:24 +000036using namespace clang;
Daniel Dunbard6f0e372009-03-04 20:49:20 +000037
Daniel Dunbard25acaa2009-03-10 23:41:59 +000038Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000039 const char *_DefaultHostTriple,
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000040 const char *_DefaultImageName,
Daniel Dunbar93468492009-03-12 08:55:43 +000041 Diagnostic &_Diags)
42 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000043 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000044 DefaultImageName(_DefaultImageName),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000045 Host(0),
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +000046 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar126da5e2009-04-01 23:34:41 +000047 CCCGenericGCCName("gcc"), CCCUseClang(true), CCCUseClangCXX(false),
48 CCCUseClangCPP(true),
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +000049 SuppressMissingInputWarning(false)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000050{
Daniel Dunbar0ea85f72009-03-24 19:02:31 +000051 // Only use clang on i386 and x86_64 by default.
52 CCCClangArchs.insert("i386");
53 CCCClangArchs.insert("x86_64");
Daniel Dunbar63c4da92009-03-02 19:59:07 +000054}
55
56Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000057 delete Opts;
Daniel Dunbar2f8b37e2009-03-18 01:09:40 +000058 delete Host;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000059}
60
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000061InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
62 const char **ArgEnd) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000063 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000064 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000065
Daniel Dunbar85cb3592009-03-13 11:38:42 +000066 // FIXME: Handle '@' args (or at least error on them).
67
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000068 unsigned Index = 0, End = ArgEnd - ArgBegin;
69 while (Index < End) {
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000070 // gcc's handling of empty arguments doesn't make
71 // sense, but this is not a common use case. :)
72 //
73 // We just ignore them here (note that other things may
74 // still take them as arguments).
75 if (Args->getArgString(Index)[0] == '\0') {
76 ++Index;
77 continue;
78 }
79
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000080 unsigned Prev = Index;
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000081 Arg *A = getOpts().ParseOneArg(*Args, Index);
82 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbardb62cc32009-03-12 07:58:46 +000083
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000084 // Check for missing argument error.
85 if (!A) {
86 assert(Index >= End && "Unexpected parser error.");
87 Diag(clang::diag::err_drv_missing_argument)
88 << Args->getArgString(Prev)
89 << (Index - Prev - 1);
90 break;
Daniel Dunbardb62cc32009-03-12 07:58:46 +000091 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000092
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000093 if (A->getOption().isUnsupported()) {
94 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
95 continue;
96 }
97 Args->append(A);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000098 }
99
100 return Args;
101}
102
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000103Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000104 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
105
Daniel Dunbarcc006892009-03-13 00:51:18 +0000106 // FIXME: Handle environment options which effect driver behavior,
107 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
108 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
109
110 // FIXME: What are we going to do with -V and -b?
111
112 // FIXME: Handle CCC_ADD_ARGS.
113
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000114 // FIXME: This stuff needs to go into the Compilation, not the
115 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000116 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000117
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000118 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000119 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000120
121 // Read -ccc args.
122 //
123 // FIXME: We need to figure out where this behavior should
124 // live. Most of it should be outside in the client; the parts that
125 // aren't should have proper options, either by introducing new ones
126 // or by overloading gcc ones like -V or -b.
127 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
128 const char *Opt = *Start + 5;
129
130 if (!strcmp(Opt, "print-options")) {
131 CCCPrintOptions = true;
132 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000133 CCCPrintActions = true;
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000134 } else if (!strcmp(Opt, "print-bindings")) {
135 CCCPrintBindings = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000136 } else if (!strcmp(Opt, "cxx")) {
137 CCCIsCXX = true;
138 } else if (!strcmp(Opt, "echo")) {
139 CCCEcho = true;
140
Daniel Dunbar126da5e2009-04-01 23:34:41 +0000141 } else if (!strcmp(Opt, "gcc-name")) {
142 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
143 CCCGenericGCCName = *++Start;
144
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000145 } else if (!strcmp(Opt, "clang-cxx")) {
146 CCCUseClangCXX = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000147 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000148 CCCUseClang = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000149 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000150 CCCUseClangCPP = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000151 } else if (!strcmp(Opt, "clang-archs")) {
152 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
153 const char *Cur = *++Start;
154
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000155 CCCClangArchs.clear();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000156 for (;;) {
157 const char *Next = strchr(Cur, ',');
158
159 if (Next) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000160 if (Cur != Next)
161 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000162 Cur = Next + 1;
163 } else {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000164 if (*Cur != '\0')
165 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000166 break;
167 }
168 }
169
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000170 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000171 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000172 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000173
174 } else {
175 // FIXME: Error handling.
176 llvm::errs() << "invalid option: " << *Start << "\n";
177 exit(1);
178 }
179 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000180
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000181 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000182
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000183 Host = GetHostInfo(HostTriple);
Daniel Dunbarcc006892009-03-13 00:51:18 +0000184
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000185 // The compilation takes ownership of Args.
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000186 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000187
188 // FIXME: This behavior shouldn't be here.
189 if (CCCPrintOptions) {
190 PrintOptions(C->getArgs());
191 return C;
192 }
193
194 if (!HandleImmediateArgs(*C))
195 return C;
196
197 // Construct the list of abstract actions to perform for this
198 // compilation. We avoid passing a Compilation here simply to
199 // enforce the abstraction that pipelining is not host or toolchain
200 // dependent (other than the driver driver test).
201 if (Host->useDriverDriver())
202 BuildUniversalActions(C->getArgs(), C->getActions());
203 else
204 BuildActions(C->getArgs(), C->getActions());
205
206 if (CCCPrintActions) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000207 PrintActions(*C);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000208 return C;
209 }
210
211 BuildJobs(*C);
Daniel Dunbarc413f822009-03-15 01:38:15 +0000212
213 return C;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000214}
215
Daniel Dunbara790d372009-03-12 18:24:49 +0000216void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000217 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000218 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000219 it != ie; ++it, ++i) {
220 Arg *A = *it;
221 llvm::errs() << "Option " << i << " - "
222 << "Name: \"" << A->getOption().getName() << "\", "
223 << "Values: {";
224 for (unsigned j = 0; j < A->getNumValues(); ++j) {
225 if (j)
226 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000227 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000228 }
229 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000230 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000231}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000232
Daniel Dunbare627b682009-03-31 21:38:17 +0000233static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
234 std::string Name = Opts.getOptionName(Id);
235
236 // Add metavar, if used.
237 switch (Opts.getOptionKind(Id)) {
238 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
239 assert(0 && "Invalid option with help text.");
240
241 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
242 assert(0 && "Cannot print metavar for this kind of option.");
243
244 case Option::FlagClass:
245 break;
246
247 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
248 Name += ' ';
249 // FALLTHROUGH
250 case Option::JoinedClass: case Option::CommaJoinedClass:
251 Name += Opts.getOptionMetaVar(Id);
252 break;
253 }
254
255 return Name;
256}
257
258void Driver::PrintHelp() const {
259 llvm::raw_ostream &OS = llvm::outs();
260
261 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
262 OS << '\n';
263 OS << "USAGE: " << Name << " [options] <input files>\n";
264 OS << '\n';
265 OS << "OPTIONS:\n";
266
267 // Render help text into (option, help) pairs.
268 std::vector< std::pair<std::string, const char*> > OptionHelp;
269
270 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
271 options::ID Id = (options::ID) i;
272 if (const char *Text = getOpts().getOptionHelpText(Id))
273 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
274 Text));
275 }
276
277 // Find the maximum option length.
278 unsigned OptionFieldWidth = 0;
279 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
280 // Limit the amount of padding we are willing to give up for
281 // alignment.
282 unsigned Length = OptionHelp[i].first.size();
283 if (Length <= 23)
284 OptionFieldWidth = std::max(OptionFieldWidth, Length);
285 }
286
287 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
288 const std::string &Option = OptionHelp[i].first;
289 OS << " " << Option;
290 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
291 OS << ' ';
292 OS << ' ' << OptionHelp[i].second << '\n';
293 }
294
295 OS.flush();
296}
297
Daniel Dunbar0ded7de2009-03-26 16:09:13 +0000298void Driver::PrintVersion(const Compilation &C) const {
Mike Stump74d94472009-03-18 14:00:02 +0000299 static char buf[] = "$URL$";
300 char *zap = strstr(buf, "/lib/Driver");
301 if (zap)
302 *zap = 0;
303 zap = strstr(buf, "/clang/tools/clang");
304 if (zap)
305 *zap = 0;
Mike Stumpbc927292009-03-18 15:19:35 +0000306 const char *vers = buf+6;
Mike Stump3fc58f02009-03-18 18:45:55 +0000307 // FIXME: Add cmake support and remove #ifdef
308#ifdef SVN_REVISION
309 const char *revision = SVN_REVISION;
310#else
311 const char *revision = "";
312#endif
Daniel Dunbarcc006892009-03-13 00:51:18 +0000313 // FIXME: The following handlers should use a callback mechanism, we
314 // don't know what the client would like to do.
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000315
316 // FIXME: Do not hardcode clang version.
Mike Stump1207fae2009-03-18 21:19:11 +0000317 llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
Daniel Dunbar0ded7de2009-03-26 16:09:13 +0000318
319 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar049b7242009-03-30 06:36:42 +0000320 llvm::errs() << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbarcc006892009-03-13 00:51:18 +0000321}
322
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000323bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000324 // The order these options are handled in in gcc is all over the
325 // place, but we don't expect inconsistencies w.r.t. that to matter
326 // in practice.
Daniel Dunbare627b682009-03-31 21:38:17 +0000327
Daniel Dunbar9a553c42009-04-04 05:17:38 +0000328 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
329 // FIXME: Do not hardcode clang version.
330 llvm::outs() << "1.0\n";
331 return false;
332 }
333
Daniel Dunbare627b682009-03-31 21:38:17 +0000334 if (C.getArgs().hasArg(options::OPT__help)) {
335 PrintHelp();
336 return false;
337 }
338
Daniel Dunbar3b7c84c2009-04-02 15:05:41 +0000339 if (C.getArgs().hasArg(options::OPT__version)) {
340 PrintVersion(C);
341 return false;
342 }
343
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000344 if (C.getArgs().hasArg(options::OPT_v) ||
345 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar0ded7de2009-03-26 16:09:13 +0000346 PrintVersion(C);
Daniel Dunbarcc006892009-03-13 00:51:18 +0000347 SuppressMissingInputWarning = true;
348 }
349
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000350 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000351 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
352 llvm::outs() << "programs: =";
353 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
354 ie = TC.getProgramPaths().end(); it != ie; ++it) {
355 if (it != TC.getProgramPaths().begin())
356 llvm::outs() << ':';
357 llvm::outs() << *it;
358 }
359 llvm::outs() << "\n";
360 llvm::outs() << "libraries: =";
361 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
362 ie = TC.getFilePaths().end(); it != ie; ++it) {
363 if (it != TC.getFilePaths().begin())
364 llvm::outs() << ':';
365 llvm::outs() << *it;
366 }
367 llvm::outs() << "\n";
Daniel Dunbare627b682009-03-31 21:38:17 +0000368 return false;
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000369 }
370
Daniel Dunbarcc006892009-03-13 00:51:18 +0000371 // FIXME: The following handlers should use a callback mechanism, we
372 // don't know what the client would like to do.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000373 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
374 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
375 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000376 return false;
377 }
378
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000379 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
380 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
381 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000382 return false;
383 }
384
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000385 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbaref68dd32009-03-27 14:26:33 +0000386 llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000387 return false;
388 }
389
390 return true;
391}
392
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000393static unsigned PrintActions1(const Compilation &C,
Daniel Dunbar494646b2009-03-13 12:19:02 +0000394 Action *A,
395 std::map<Action*, unsigned> &Ids) {
396 if (Ids.count(A))
397 return Ids[A];
398
399 std::string str;
400 llvm::raw_string_ostream os(str);
401
402 os << Action::getClassName(A->getKind()) << ", ";
403 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000404 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000405 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000406 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
407 C.getDefaultToolChain().getArchName()) << '"'
408 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000409 } else {
410 os << "{";
411 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000412 os << PrintActions1(C, *it, Ids);
Daniel Dunbar494646b2009-03-13 12:19:02 +0000413 ++it;
414 if (it != ie)
415 os << ", ";
416 }
417 os << "}";
418 }
419
420 unsigned Id = Ids.size();
421 Ids[A] = Id;
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000422 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000423 << types::getTypeName(A->getType()) << "\n";
424
425 return Id;
426}
427
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000428void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbar494646b2009-03-13 12:19:02 +0000429 std::map<Action*, unsigned> Ids;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000430 for (ActionList::const_iterator it = C.getActions().begin(),
431 ie = C.getActions().end(); it != ie; ++it)
432 PrintActions1(C, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000433}
434
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000435void Driver::BuildUniversalActions(const ArgList &Args,
436 ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000437 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000438 // Collect the list of architectures. Duplicates are allowed, but
439 // should only be handled once (in the order seen).
440 llvm::StringSet<> ArchNames;
441 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000442 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
443 it != ie; ++it) {
444 Arg *A = *it;
445
446 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000447 const char *Name = A->getValue(Args);
448
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000449 // FIXME: We need to handle canonicalization of the specified
450 // arch?
451
Daniel Dunbara345a442009-03-19 07:55:12 +0000452 A->claim();
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000453 if (ArchNames.insert(Name))
454 Archs.push_back(Name);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000455 }
456 }
457
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000458 // When there is no explicit arch for this platform, make sure we
459 // still bind the architecture (to the default) so that -Xarch_ is
460 // handled correctly.
461 if (!Archs.size())
462 Archs.push_back(0);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000463
464 // FIXME: We killed off some others but these aren't yet detected in
465 // a functional manner. If we added information to jobs about which
466 // "auxiliary" files they wrote then we could detect the conflict
467 // these cause downstream.
468 if (Archs.size() > 1) {
469 // No recovery needed, the point of this is just to prevent
470 // overwriting the same files.
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000471 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
472 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000473 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000474 }
475
476 ActionList SingleActions;
477 BuildActions(Args, SingleActions);
478
479 // Add in arch binding and lipo (if necessary) for every top level
480 // action.
481 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
482 Action *Act = SingleActions[i];
483
484 // Make sure we can lipo this kind of output. If not (and it is an
485 // actual output) then we disallow, since we can't create an
486 // output file with the right name without overwriting it. We
487 // could remove this oddity by just changing the output names to
488 // include the arch, which would also fix
489 // -save-temps. Compatibility wins for now.
490
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000491 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000492 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
493 << types::getTypeName(Act->getType());
494
495 ActionList Inputs;
Daniel Dunbara345a442009-03-19 07:55:12 +0000496 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000497 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000498
499 // Lipo if necessary, We do it this way because we need to set the
500 // arch flag so that -Xarch_ gets overwritten.
501 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
502 Actions.append(Inputs.begin(), Inputs.end());
503 else
504 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
505 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000506}
507
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000508void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000509 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000510 // Start by constructing the list of inputs and their types.
511
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000512 // Track the current user specified (-x) input. We also explicitly
513 // track the argument used to set the type; we only want to claim
514 // the type when we actually use it, so we warn about unused -x
515 // arguments.
516 types::ID InputType = types::TY_Nothing;
517 Arg *InputTypeArg = 0;
518
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000519 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
520 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
521 it != ie; ++it) {
522 Arg *A = *it;
523
524 if (isa<InputOption>(A->getOption())) {
525 const char *Value = A->getValue(Args);
526 types::ID Ty = types::TY_INVALID;
527
528 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000529 if (InputType == types::TY_Nothing) {
530 // If there was an explicit arg for this, claim it.
531 if (InputTypeArg)
532 InputTypeArg->claim();
533
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000534 // stdin must be handled specially.
535 if (memcmp(Value, "-", 2) == 0) {
536 // If running with -E, treat as a C input (this changes the
537 // builtin macros, for example). This may be overridden by
538 // -ObjC below.
539 //
540 // Otherwise emit an error but still use a valid type to
541 // avoid spurious errors (e.g., no inputs).
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000542 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbard724e332009-03-12 09:13:48 +0000543 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000544 Ty = types::TY_C;
545 } else {
546 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000547 // if not found. We use a host hook here because Darwin at
548 // least has its own idea of what .s is.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000549 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000550 Ty = Host->lookupTypeForExtension(Ext + 1);
551
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000552 if (Ty == types::TY_INVALID)
553 Ty = types::TY_Object;
554 }
555
556 // -ObjC and -ObjC++ override the default language, but only
557 // -for "source files". We just treat everything that isn't a
558 // -linker input as a source file.
559 //
560 // FIXME: Clean this up if we move the phase sequence into the
561 // type.
562 if (Ty != types::TY_Object) {
563 if (Args.hasArg(options::OPT_ObjC))
564 Ty = types::TY_ObjC;
565 else if (Args.hasArg(options::OPT_ObjCXX))
566 Ty = types::TY_ObjCXX;
567 }
568 } else {
569 assert(InputTypeArg && "InputType set w/o InputTypeArg");
570 InputTypeArg->claim();
571 Ty = InputType;
572 }
573
574 // Check that the file exists. It isn't clear this is worth
575 // doing, since the tool presumably does this anyway, and this
576 // just adds an extra stat to the equation, but this is gcc
577 // compatible.
578 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000579 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000580 else
581 Inputs.push_back(std::make_pair(Ty, A));
582
583 } else if (A->getOption().isLinkerInput()) {
584 // Just treat as object type, we could make a special type for
585 // this if necessary.
586 Inputs.push_back(std::make_pair(types::TY_Object, A));
587
588 } else if (A->getOption().getId() == options::OPT_x) {
589 InputTypeArg = A;
590 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
591
592 // Follow gcc behavior and treat as linker input for invalid -x
593 // options. Its not clear why we shouldn't just revert to
594 // unknown; but this isn't very important, we might as well be
595 // bug comatible.
596 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000597 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000598 InputType = types::TY_Object;
599 }
600 }
601 }
602
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000603 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000604 Diag(clang::diag::err_drv_no_input_files);
605 return;
606 }
607
608 // Determine which compilation mode we are in. We look for options
609 // which affect the phase, starting with the earliest phases, and
610 // record which option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000611 Arg *FinalPhaseArg = 0;
612 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000613
614 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000615 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
616 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
617 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
618 FinalPhase = phases::Preprocess;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000619
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000620 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
621 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
622 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000623 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
624 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000625
626 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000627 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
628 FinalPhase = phases::Assemble;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000629
630 // Otherwise do everything.
631 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000632 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000633
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000634 // Reject -Z* at the top level, these options should never have been
635 // exposed by gcc.
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000636 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar73225932009-03-20 06:14:23 +0000637 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000638
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000639 // Construct the actions to perform.
640 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000641 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000642 types::ID InputType = Inputs[i].first;
643 const Arg *InputArg = Inputs[i].second;
644
645 unsigned NumSteps = types::getNumCompilationPhases(InputType);
646 assert(NumSteps && "Invalid number of steps!");
647
648 // If the first step comes after the final phase we are doing as
649 // part of this compilation, warn the user about it.
650 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
651 if (InitialPhase > FinalPhase) {
Daniel Dunbar923e7032009-03-19 07:57:08 +0000652 // Claim here to avoid the more general unused warning.
653 InputArg->claim();
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000654 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar73225932009-03-20 06:14:23 +0000655 << InputArg->getAsString(Args)
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000656 << getPhaseName(InitialPhase)
657 << FinalPhaseArg->getOption().getName();
658 continue;
659 }
660
661 // Build the pipeline for this file.
662 Action *Current = new InputAction(*InputArg, InputType);
663 for (unsigned i = 0; i != NumSteps; ++i) {
664 phases::ID Phase = types::getCompilationPhase(InputType, i);
665
666 // We are done if this step is past what the user requested.
667 if (Phase > FinalPhase)
668 break;
669
670 // Queue linker inputs.
671 if (Phase == phases::Link) {
672 assert(i + 1 == NumSteps && "linking must be final compilation step.");
673 LinkerInputs.push_back(Current);
674 Current = 0;
675 break;
676 }
677
Daniel Dunbardec14452009-03-24 20:17:30 +0000678 // Some types skip the assembler phase (e.g., llvm-bc), but we
679 // can't encode this in the steps because the intermediate type
680 // depends on arguments. Just special case here.
681 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
682 continue;
683
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000684 // Otherwise construct the appropriate action.
685 Current = ConstructPhaseAction(Args, Phase, Current);
686 if (Current->getType() == types::TY_Nothing)
687 break;
688 }
689
690 // If we ended with something, add to the output list.
691 if (Current)
692 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000693 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000694
695 // Add a link action if necessary.
696 if (!LinkerInputs.empty())
697 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
698}
699
700Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
701 Action *Input) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000702 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000703 // Build the appropriate action.
704 switch (Phase) {
705 case phases::Link: assert(0 && "link action invalid here.");
706 case phases::Preprocess: {
Daniel Dunbar049b7242009-03-30 06:36:42 +0000707 types::ID OutputTy;
708 // -{M, MM} alter the output type.
709 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
710 OutputTy = types::TY_Dependencies;
711 } else {
712 OutputTy = types::getPreprocessedType(Input->getType());
713 assert(OutputTy != types::TY_INVALID &&
714 "Cannot preprocess this input type!");
715 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000716 return new PreprocessJobAction(Input, OutputTy);
717 }
718 case phases::Precompile:
719 return new PrecompileJobAction(Input, types::TY_PCH);
720 case phases::Compile: {
721 if (Args.hasArg(options::OPT_fsyntax_only)) {
722 return new CompileJobAction(Input, types::TY_Nothing);
723 } else if (Args.hasArg(options::OPT__analyze)) {
724 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbardec14452009-03-24 20:17:30 +0000725 } else if (Args.hasArg(options::OPT_emit_llvm) ||
726 Args.hasArg(options::OPT_flto) ||
727 Args.hasArg(options::OPT_O4)) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000728 types::ID Output =
729 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
730 return new CompileJobAction(Input, Output);
731 } else {
732 return new CompileJobAction(Input, types::TY_PP_Asm);
733 }
734 }
735 case phases::Assemble:
736 return new AssembleJobAction(Input, types::TY_Object);
737 }
738
739 assert(0 && "invalid phase in ConstructPhaseAction");
740 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000741}
742
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000743void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000744 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000745 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
746 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000747
748 // FIXME: Pipes are forcibly disabled until we support executing
749 // them.
750 if (!CCCPrintBindings)
751 UsePipes = false;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000752
753 // -save-temps inhibits pipes.
754 if (SaveTemps && UsePipes) {
755 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
756 UsePipes = true;
757 }
758
759 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
760
761 // It is an error to provide a -o option if we are making multiple
762 // output files.
763 if (FinalOutput) {
764 unsigned NumOutputs = 0;
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000765 for (ActionList::const_iterator it = C.getActions().begin(),
766 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000767 if ((*it)->getType() != types::TY_Nothing)
768 ++NumOutputs;
769
770 if (NumOutputs > 1) {
771 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
772 FinalOutput = 0;
773 }
774 }
775
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000776 for (ActionList::const_iterator it = C.getActions().begin(),
777 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000778 Action *A = *it;
779
780 // If we are linking an image for multiple archs then the linker
781 // wants -arch_multiple and -final_output <final image
782 // name>. Unfortunately, this doesn't fit in cleanly because we
783 // have to pass this information down.
784 //
785 // FIXME: This is a hack; find a cleaner way to integrate this
786 // into the process.
787 const char *LinkingOutput = 0;
Daniel Dunbar55e34e32009-03-26 16:12:09 +0000788 if (isa<LipoJobAction>(A)) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000789 if (FinalOutput)
790 LinkingOutput = FinalOutput->getValue(C.getArgs());
791 else
792 LinkingOutput = DefaultImageName.c_str();
793 }
794
795 InputInfo II;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000796 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000797 /*CanAcceptPipe*/ true,
798 /*AtTopLevel*/ true,
799 /*LinkingOutput*/ LinkingOutput,
800 II);
801 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000802
Daniel Dunbar025de202009-04-03 22:09:23 +0000803 // If the user passed -Qunused-arguments or there were errors, don't
804 // warn about any unused arguments.
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000805 if (Diags.getNumErrors() ||
806 C.getArgs().hasArg(options::OPT_Qunused_arguments))
Daniel Dunbar46c70822009-03-18 18:03:46 +0000807 return;
808
Daniel Dunbar1e6f9ff2009-03-29 22:24:54 +0000809 // Claim -### here.
810 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
811
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000812 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
813 it != ie; ++it) {
814 Arg *A = *it;
Daniel Dunbar46c70822009-03-18 18:03:46 +0000815
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000816 // FIXME: It would be nice to be able to send the argument to the
817 // Diagnostic, so that extra values, position, and so on could be
818 // printed.
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000819 if (!A->isClaimed()) {
Daniel Dunbar1cfd8912009-04-07 19:04:18 +0000820 if (A->getOption().hasNoArgumentUnused())
821 continue;
822
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000823 // Suppress the warning automatically if this is just a flag,
824 // and it is an instance of an argument we already claimed.
825 const Option &Opt = A->getOption();
826 if (isa<FlagOption>(Opt)) {
827 bool DuplicateClaimed = false;
828
829 // FIXME: Use iterator.
830 for (ArgList::const_iterator it = C.getArgs().begin(),
831 ie = C.getArgs().end(); it != ie; ++it) {
832 if ((*it)->isClaimed() && (*it)->getOption().matches(Opt.getId())) {
833 DuplicateClaimed = true;
834 break;
835 }
836 }
837
838 if (DuplicateClaimed)
839 continue;
840 }
841
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000842 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar73225932009-03-20 06:14:23 +0000843 << A->getAsString(C.getArgs());
Daniel Dunbar6c6e3f72009-04-04 00:52:26 +0000844 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000845 }
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000846}
847
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000848void Driver::BuildJobsForAction(Compilation &C,
849 const Action *A,
850 const ToolChain *TC,
851 bool CanAcceptPipe,
852 bool AtTopLevel,
853 const char *LinkingOutput,
854 InputInfo &Result) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000855 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000856
857 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
858 // FIXME: Pipes are forcibly disabled until we support executing
859 // them.
860 if (!CCCPrintBindings)
861 UsePipes = false;
862
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000863 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarc3be67b2009-03-19 07:29:38 +0000864 // FIXME: It would be nice to not claim this here; maybe the old
865 // scheme of just using Args was better?
866 const Arg &Input = IA->getInputArg();
867 Input.claim();
868 if (isa<PositionalArg>(Input)) {
869 const char *Name = Input.getValue(C.getArgs());
870 Result = InputInfo(Name, A->getType(), Name);
871 } else
872 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000873 return;
874 }
875
876 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
877 const char *ArchName = BAA->getArchName();
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000878 if (!ArchName)
879 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000880 BuildJobsForAction(C,
881 *BAA->begin(),
882 Host->getToolChain(C.getArgs(), ArchName),
883 CanAcceptPipe,
884 AtTopLevel,
885 LinkingOutput,
886 Result);
887 return;
888 }
889
890 const JobAction *JA = cast<JobAction>(A);
891 const Tool &T = TC->SelectTool(C, *JA);
892
893 // See if we should use an integrated preprocessor. We do so when we
894 // have exactly one input, since this is the only use case we care
895 // about (irrelevant since we don't support combine yet).
896 bool UseIntegratedCPP = false;
897 const ActionList *Inputs = &A->getInputs();
898 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
899 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
900 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
901 !C.getArgs().hasArg(options::OPT_save_temps) &&
902 T.hasIntegratedCPP()) {
903 UseIntegratedCPP = true;
904 Inputs = &(*Inputs)[0]->getInputs();
905 }
906 }
907
908 // Only use pipes when there is exactly one input.
909 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar00fd3792009-03-18 06:00:36 +0000910 InputInfoList InputInfos;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000911 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
912 it != ie; ++it) {
913 InputInfo II;
914 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
915 /*AtTopLevel*/false,
916 LinkingOutput,
917 II);
918 InputInfos.push_back(II);
919 }
920
921 // Determine if we should output to a pipe.
922 bool OutputToPipe = false;
923 if (CanAcceptPipe && T.canPipeOutput()) {
924 // Some actions default to writing to a pipe if they are the top
925 // level phase and there was no user override.
926 //
927 // FIXME: Is there a better way to handle this?
928 if (AtTopLevel) {
929 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
930 OutputToPipe = true;
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000931 } else if (UsePipes)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000932 OutputToPipe = true;
933 }
934
935 // Figure out where to put the job (pipes).
936 Job *Dest = &C.getJobs();
937 if (InputInfos[0].isPipe()) {
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000938 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000939 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
940 Dest = &InputInfos[0].getPipe();
941 }
942
943 // Always use the first input as the base input.
944 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000945
946 // Determine the place to write output to (nothing, pipe, or
947 // filename) and where to put the new job.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000948 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000949 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000950 } else if (OutputToPipe) {
951 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000952 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
953 if (!PJ) {
954 PJ = new PipedJob();
Daniel Dunbar8eeb5a32009-03-20 00:11:04 +0000955 // FIXME: Temporary hack so that -ccc-print-bindings work until
956 // we have pipe support. Please remove later.
957 if (!CCCPrintBindings)
958 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar933ac452009-03-18 07:06:02 +0000959 Dest = PJ;
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000960 }
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000961 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000962 } else {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000963 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
964 A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000965 }
966
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000967 if (CCCPrintBindings) {
Daniel Dunbar049b7242009-03-30 06:36:42 +0000968 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
969 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000970 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
971 llvm::errs() << InputInfos[i].getAsString();
972 if (i + 1 != e)
973 llvm::errs() << ", ";
974 }
975 llvm::errs() << "], output: " << Result.getAsString() << "\n";
976 } else {
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000977 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
978 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000979 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000980}
981
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000982const char *Driver::GetNamedOutputPath(Compilation &C,
983 const JobAction &JA,
984 const char *BaseInput,
985 bool AtTopLevel) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000986 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000987 // Output to a user requested destination?
988 if (AtTopLevel) {
989 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
990 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
991 }
992
993 // Output to a temporary file?
994 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarb6ddc952009-03-18 19:34:39 +0000995 std::string TmpName =
996 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
997 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000998 }
999
1000 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbarfb5e3332009-03-18 02:00:31 +00001001 std::string BaseName(BasePath.getLast());
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001002
1003 // Determine what the derived output name should be.
1004 const char *NamedOutput;
1005 if (JA.getType() == types::TY_Image) {
1006 NamedOutput = DefaultImageName.c_str();
1007 } else {
1008 const char *Suffix = types::getTypeTempSuffix(JA.getType());
1009 assert(Suffix && "All types used for output should have a suffix.");
1010
1011 std::string::size_type End = std::string::npos;
1012 if (!types::appendSuffixForType(JA.getType()))
1013 End = BaseName.rfind('.');
1014 std::string Suffixed(BaseName.substr(0, End));
1015 Suffixed += '.';
1016 Suffixed += Suffix;
1017 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
1018 }
1019
1020 // As an annoying special case, PCH generation doesn't strip the
1021 // pathname.
1022 if (JA.getType() == types::TY_PCH) {
1023 BasePath.eraseComponent();
Daniel Dunbar58a51262009-03-18 09:58:30 +00001024 if (BasePath.isEmpty())
1025 BasePath = NamedOutput;
1026 else
1027 BasePath.appendComponent(NamedOutput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +00001028 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
1029 } else {
1030 return C.addResultFile(NamedOutput);
1031 }
1032}
1033
Daniel Dunbare1cef7d2009-03-16 05:25:36 +00001034llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +00001035 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001036 const ToolChain::path_list &List = TC.getFilePaths();
1037 for (ToolChain::path_list::const_iterator
1038 it = List.begin(), ie = List.end(); it != ie; ++it) {
1039 llvm::sys::Path P(*it);
1040 P.appendComponent(Name);
1041 if (P.exists())
1042 return P;
1043 }
1044
Daniel Dunbarcc006892009-03-13 00:51:18 +00001045 return llvm::sys::Path(Name);
1046}
1047
Daniel Dunbare1cef7d2009-03-16 05:25:36 +00001048llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump32b1bbe2009-03-27 00:40:20 +00001049 const ToolChain &TC,
1050 bool WantFile) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001051 const ToolChain::path_list &List = TC.getProgramPaths();
1052 for (ToolChain::path_list::const_iterator
1053 it = List.begin(), ie = List.end(); it != ie; ++it) {
1054 llvm::sys::Path P(*it);
1055 P.appendComponent(Name);
Mike Stump32b1bbe2009-03-27 00:40:20 +00001056 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbarcc7600c2009-03-18 20:26:19 +00001057 return P;
1058 }
1059
Daniel Dunbar49a35de2009-03-23 16:15:50 +00001060 // If all else failed, search the path.
1061 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +00001062 if (!P.empty())
1063 return P;
1064
Daniel Dunbarcc006892009-03-13 00:51:18 +00001065 return llvm::sys::Path(Name);
1066}
1067
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001068std::string Driver::GetTemporaryPath(const char *Suffix) const {
1069 // FIXME: This is lame; sys::Path should provide this function (in
1070 // particular, it should know how to find the temporary files dir).
1071 std::string Error;
1072 llvm::sys::Path P("/tmp/cc");
1073 if (P.makeUnique(false, &Error)) {
1074 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1075 return "";
1076 }
1077
Daniel Dunbar9083abe2009-03-18 23:08:52 +00001078 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
1079 // PR3837.
1080 P.eraseFromDisk(false, 0);
1081
Daniel Dunbarb6ddc952009-03-18 19:34:39 +00001082 P.appendSuffix(Suffix);
1083 return P.toString();
1084}
1085
Daniel Dunbar08966ca2009-03-17 20:45:45 +00001086const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +00001087 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001088 // Dice into arch, platform, and OS. This matches
1089 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
1090 // and missing fields are left empty.
1091 std::string Arch, Platform, OS;
1092
1093 if (const char *ArchEnd = strchr(Triple, '-')) {
1094 Arch = std::string(Triple, ArchEnd);
1095
1096 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
1097 Platform = std::string(ArchEnd+1, PlatformEnd);
1098 OS = PlatformEnd+1;
1099 } else
1100 Platform = ArchEnd+1;
1101 } else
1102 Arch = Triple;
1103
Daniel Dunbar7424b8a2009-03-17 19:00:50 +00001104 // Normalize Arch a bit.
1105 //
1106 // FIXME: This is very incomplete.
1107 if (Arch == "i686")
1108 Arch = "i386";
1109 else if (Arch == "amd64")
1110 Arch = "x86_64";
Daniel Dunbarb7daa2d2009-04-01 20:33:11 +00001111 else if (Arch == "ppc" || Arch == "Power Macintosh")
1112 Arch = "powerpc";
1113 else if (Arch == "ppc64")
1114 Arch = "powerpc64";
Daniel Dunbar7424b8a2009-03-17 19:00:50 +00001115
Daniel Dunbar44119a12009-03-13 12:23:29 +00001116 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbar08966ca2009-03-17 20:45:45 +00001117 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
1118 OS.c_str());
Daniel Dunbar606f6142009-03-30 21:06:03 +00001119 if (memcmp(&OS[0], "freebsd", 7) == 0)
1120 return createFreeBSDHostInfo(*this, Arch.c_str(), Platform.c_str(),
1121 OS.c_str());
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001122
Daniel Dunbar08966ca2009-03-17 20:45:45 +00001123 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
1124 OS.c_str());
Daniel Dunbard25acaa2009-03-10 23:41:59 +00001125}
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001126
1127bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarb7daa2d2009-04-01 20:33:11 +00001128 const std::string &ArchNameStr) const {
1129 // FIXME: Remove this hack.
1130 const char *ArchName = ArchNameStr.c_str();
1131 if (ArchNameStr == "powerpc")
1132 ArchName = "ppc";
1133 else if (ArchNameStr == "powerpc64")
1134 ArchName = "ppc64";
1135
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001136 // Check if user requested no clang, or clang doesn't understand
1137 // this type (we only handle single inputs for now).
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001138 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001139 !types::isAcceptedByClang((*JA.begin())->getType()))
1140 return false;
1141
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001142 // Otherwise make sure this is an action clang understands.
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001143 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001144 if (!CCCUseClangCPP) {
1145 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001146 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001147 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001148 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1149 return false;
1150
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001151 // Use clang for C++?
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001152 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1153 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001154 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001155 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001156
1157 // Finally, don't use clang if this isn't one of the user specified
1158 // archs to build.
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001159 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1160 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001161 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001162 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001163
1164 return true;
1165}
Daniel Dunbar9cdd4d42009-03-26 15:58:36 +00001166
1167/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1168/// return the grouped values as integers. Numbers which are not
1169/// provided are set to 0.
1170///
1171/// \return True if the entire string was parsed (9.2), or all groups
1172/// were parsed (10.3.5extrastuff).
1173bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1174 unsigned &Minor, unsigned &Micro,
1175 bool &HadExtra) {
1176 HadExtra = false;
1177
1178 Major = Minor = Micro = 0;
1179 if (*Str == '\0')
1180 return true;
1181
1182 char *End;
1183 Major = (unsigned) strtol(Str, &End, 10);
1184 if (*Str != '\0' && *End == '\0')
1185 return true;
1186 if (*End != '.')
1187 return false;
1188
1189 Str = End+1;
1190 Minor = (unsigned) strtol(Str, &End, 10);
1191 if (*Str != '\0' && *End == '\0')
1192 return true;
1193 if (*End != '.')
1194 return false;
1195
1196 Str = End+1;
1197 Micro = (unsigned) strtol(Str, &End, 10);
1198 if (*Str != '\0' && *End == '\0')
1199 return true;
1200 if (Str == End)
1201 return false;
1202 HadExtra = true;
1203 return true;
1204}