blob: b59768d1d0cf030286ff7548d8e2b5ecfe8a778c [file] [log] [blame]
Daniel Dunbar3ede8d02009-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 Dunbar3ede8d02009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000011
Daniel Dunbar53ec5522009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000024
Daniel Dunbar13689542009-03-13 20:33:35 +000025#include "llvm/ADT/StringSet.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000026#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000028#include "llvm/System/Path.h"
Daniel Dunbar632f50e2009-03-18 21:34:08 +000029#include "llvm/System/Program.h"
Daniel Dunbarba102132009-03-13 12:19:02 +000030
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000031#include "InputInfo.h"
32
Daniel Dunbarba102132009-03-13 12:19:02 +000033#include <map>
34
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000035using namespace clang::driver;
Chris Lattner92b36992009-03-26 05:56:24 +000036using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000037
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000038Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000039 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000040 const char *_DefaultImageName,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000041 Diagnostic &_Diags)
42 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000043 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000044 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000045 Host(0),
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +000046 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +000047 CCCUseClang(true), CCCUseClangCXX(false), CCCUseClangCPP(true),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000048 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000049{
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +000050 // Only use clang on i386 and x86_64 by default.
51 CCCClangArchs.insert("i386");
52 CCCClangArchs.insert("x86_64");
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000053}
54
55Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000056 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000057 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000058}
59
Daniel Dunbarf3cad362009-03-25 04:13:45 +000060InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
61 const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000062 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbarf3cad362009-03-25 04:13:45 +000063 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar06482622009-03-05 06:38:47 +000064
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000065 // FIXME: Handle '@' args (or at least error on them).
66
Daniel Dunbar06482622009-03-05 06:38:47 +000067 unsigned Index = 0, End = ArgEnd - ArgBegin;
68 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000069 // gcc's handling of empty arguments doesn't make
70 // sense, but this is not a common use case. :)
71 //
72 // We just ignore them here (note that other things may
73 // still take them as arguments).
74 if (Args->getArgString(Index)[0] == '\0') {
75 ++Index;
76 continue;
77 }
78
Daniel Dunbar06482622009-03-05 06:38:47 +000079 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000080 Arg *A = getOpts().ParseOneArg(*Args, Index);
81 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000082
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000083 // Check for missing argument error.
84 if (!A) {
85 assert(Index >= End && "Unexpected parser error.");
86 Diag(clang::diag::err_drv_missing_argument)
87 << Args->getArgString(Prev)
88 << (Index - Prev - 1);
89 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +000090 }
Daniel Dunbar06482622009-03-05 06:38:47 +000091
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000092 if (A->getOption().isUnsupported()) {
93 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
94 continue;
95 }
96 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +000097 }
98
99 return Args;
100}
101
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000102Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000103 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
104
Daniel Dunbarcb881672009-03-13 00:51:18 +0000105 // FIXME: Handle environment options which effect driver behavior,
106 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
107 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
108
109 // FIXME: What are we going to do with -V and -b?
110
111 // FIXME: Handle CCC_ADD_ARGS.
112
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000113 // FIXME: This stuff needs to go into the Compilation, not the
114 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000115 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000116
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000117 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000118 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000119
120 // Read -ccc args.
121 //
122 // FIXME: We need to figure out where this behavior should
123 // live. Most of it should be outside in the client; the parts that
124 // aren't should have proper options, either by introducing new ones
125 // or by overloading gcc ones like -V or -b.
126 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
127 const char *Opt = *Start + 5;
128
129 if (!strcmp(Opt, "print-options")) {
130 CCCPrintOptions = true;
131 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000132 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000133 } else if (!strcmp(Opt, "print-bindings")) {
134 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000135 } else if (!strcmp(Opt, "cxx")) {
136 CCCIsCXX = true;
137 } else if (!strcmp(Opt, "echo")) {
138 CCCEcho = true;
139
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000140 } else if (!strcmp(Opt, "clang-cxx")) {
141 CCCUseClangCXX = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000142 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000143 CCCUseClang = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000144 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000145 CCCUseClangCPP = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000146 } else if (!strcmp(Opt, "clang-archs")) {
147 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
148 const char *Cur = *++Start;
149
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000150 CCCClangArchs.clear();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000151 for (;;) {
152 const char *Next = strchr(Cur, ',');
153
154 if (Next) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000155 if (Cur != Next)
156 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000157 Cur = Next + 1;
158 } else {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000159 if (*Cur != '\0')
160 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000161 break;
162 }
163 }
164
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000165 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000166 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000167 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000168
169 } else {
170 // FIXME: Error handling.
171 llvm::errs() << "invalid option: " << *Start << "\n";
172 exit(1);
173 }
174 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000175
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000176 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000177
Daniel Dunbare5049522009-03-17 20:45:45 +0000178 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000179
Daniel Dunbar586dc232009-03-16 06:42:30 +0000180 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000181 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000182
183 // FIXME: This behavior shouldn't be here.
184 if (CCCPrintOptions) {
185 PrintOptions(C->getArgs());
186 return C;
187 }
188
189 if (!HandleImmediateArgs(*C))
190 return C;
191
192 // Construct the list of abstract actions to perform for this
193 // compilation. We avoid passing a Compilation here simply to
194 // enforce the abstraction that pipelining is not host or toolchain
195 // dependent (other than the driver driver test).
196 if (Host->useDriverDriver())
197 BuildUniversalActions(C->getArgs(), C->getActions());
198 else
199 BuildActions(C->getArgs(), C->getActions());
200
201 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000202 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000203 return C;
204 }
205
206 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000207
208 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000209}
210
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000211void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000212 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000213 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000214 it != ie; ++it, ++i) {
215 Arg *A = *it;
216 llvm::errs() << "Option " << i << " - "
217 << "Name: \"" << A->getOption().getName() << "\", "
218 << "Values: {";
219 for (unsigned j = 0; j < A->getNumValues(); ++j) {
220 if (j)
221 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000222 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000223 }
224 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000225 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000226}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000227
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000228static std::string getOptionHelpName(const OptTable &Opts, options::ID Id) {
229 std::string Name = Opts.getOptionName(Id);
230
231 // Add metavar, if used.
232 switch (Opts.getOptionKind(Id)) {
233 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
234 assert(0 && "Invalid option with help text.");
235
236 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
237 assert(0 && "Cannot print metavar for this kind of option.");
238
239 case Option::FlagClass:
240 break;
241
242 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
243 Name += ' ';
244 // FALLTHROUGH
245 case Option::JoinedClass: case Option::CommaJoinedClass:
246 Name += Opts.getOptionMetaVar(Id);
247 break;
248 }
249
250 return Name;
251}
252
253void Driver::PrintHelp() const {
254 llvm::raw_ostream &OS = llvm::outs();
255
256 OS << "OVERVIEW: clang \"gcc-compatible\" driver\n";
257 OS << '\n';
258 OS << "USAGE: " << Name << " [options] <input files>\n";
259 OS << '\n';
260 OS << "OPTIONS:\n";
261
262 // Render help text into (option, help) pairs.
263 std::vector< std::pair<std::string, const char*> > OptionHelp;
264
265 for (unsigned i = options::OPT_INPUT, e = options::LastOption; i != e; ++i) {
266 options::ID Id = (options::ID) i;
267 if (const char *Text = getOpts().getOptionHelpText(Id))
268 OptionHelp.push_back(std::make_pair(getOptionHelpName(getOpts(), Id),
269 Text));
270 }
271
272 // Find the maximum option length.
273 unsigned OptionFieldWidth = 0;
274 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
275 // Limit the amount of padding we are willing to give up for
276 // alignment.
277 unsigned Length = OptionHelp[i].first.size();
278 if (Length <= 23)
279 OptionFieldWidth = std::max(OptionFieldWidth, Length);
280 }
281
282 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
283 const std::string &Option = OptionHelp[i].first;
284 OS << " " << Option;
285 for (int j = Option.length(), e = OptionFieldWidth; j < e; ++j)
286 OS << ' ';
287 OS << ' ' << OptionHelp[i].second << '\n';
288 }
289
290 OS.flush();
291}
292
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000293void Driver::PrintVersion(const Compilation &C) const {
Mike Stump5d023c32009-03-18 14:00:02 +0000294 static char buf[] = "$URL$";
295 char *zap = strstr(buf, "/lib/Driver");
296 if (zap)
297 *zap = 0;
298 zap = strstr(buf, "/clang/tools/clang");
299 if (zap)
300 *zap = 0;
Mike Stumpe70295b2009-03-18 15:19:35 +0000301 const char *vers = buf+6;
Mike Stump8944c382009-03-18 18:45:55 +0000302 // FIXME: Add cmake support and remove #ifdef
303#ifdef SVN_REVISION
304 const char *revision = SVN_REVISION;
305#else
306 const char *revision = "";
307#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000308 // FIXME: The following handlers should use a callback mechanism, we
309 // don't know what the client would like to do.
Mike Stumpd227fe72009-03-18 21:19:11 +0000310 llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000311
312 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000313 llvm::errs() << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbarcb881672009-03-13 00:51:18 +0000314}
315
Daniel Dunbar21549232009-03-18 02:55:38 +0000316bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000317 // The order these options are handled in in gcc is all over the
318 // place, but we don't expect inconsistencies w.r.t. that to matter
319 // in practice.
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000320
321 if (C.getArgs().hasArg(options::OPT__help)) {
322 PrintHelp();
323 return false;
324 }
325
Daniel Dunbar21549232009-03-18 02:55:38 +0000326 if (C.getArgs().hasArg(options::OPT_v) ||
327 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000328 PrintVersion(C);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000329 SuppressMissingInputWarning = true;
330 }
331
Daniel Dunbar21549232009-03-18 02:55:38 +0000332 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000333 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
334 llvm::outs() << "programs: =";
335 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
336 ie = TC.getProgramPaths().end(); it != ie; ++it) {
337 if (it != TC.getProgramPaths().begin())
338 llvm::outs() << ':';
339 llvm::outs() << *it;
340 }
341 llvm::outs() << "\n";
342 llvm::outs() << "libraries: =";
343 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
344 ie = TC.getFilePaths().end(); it != ie; ++it) {
345 if (it != TC.getFilePaths().begin())
346 llvm::outs() << ':';
347 llvm::outs() << *it;
348 }
349 llvm::outs() << "\n";
Daniel Dunbar91e28af2009-03-31 21:38:17 +0000350 return false;
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000351 }
352
Daniel Dunbarcb881672009-03-13 00:51:18 +0000353 // FIXME: The following handlers should use a callback mechanism, we
354 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000355 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
356 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
357 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000358 return false;
359 }
360
Daniel Dunbar21549232009-03-18 02:55:38 +0000361 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
362 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
363 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000364 return false;
365 }
366
Daniel Dunbar21549232009-03-18 02:55:38 +0000367 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar08c65e02009-03-27 14:26:33 +0000368 llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000369 return false;
370 }
371
372 return true;
373}
374
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000375static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000376 Action *A,
377 std::map<Action*, unsigned> &Ids) {
378 if (Ids.count(A))
379 return Ids[A];
380
381 std::string str;
382 llvm::raw_string_ostream os(str);
383
384 os << Action::getClassName(A->getKind()) << ", ";
385 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000386 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000387 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000388 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
389 C.getDefaultToolChain().getArchName()) << '"'
390 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000391 } else {
392 os << "{";
393 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000394 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000395 ++it;
396 if (it != ie)
397 os << ", ";
398 }
399 os << "}";
400 }
401
402 unsigned Id = Ids.size();
403 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000404 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000405 << types::getTypeName(A->getType()) << "\n";
406
407 return Id;
408}
409
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000410void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000411 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000412 for (ActionList::const_iterator it = C.getActions().begin(),
413 ie = C.getActions().end(); it != ie; ++it)
414 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000415}
416
Daniel Dunbar21549232009-03-18 02:55:38 +0000417void Driver::BuildUniversalActions(const ArgList &Args,
418 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000419 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000420 // Collect the list of architectures. Duplicates are allowed, but
421 // should only be handled once (in the order seen).
422 llvm::StringSet<> ArchNames;
423 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000424 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
425 it != ie; ++it) {
426 Arg *A = *it;
427
428 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000429 const char *Name = A->getValue(Args);
430
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000431 // FIXME: We need to handle canonicalization of the specified
432 // arch?
433
Daniel Dunbar75877192009-03-19 07:55:12 +0000434 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000435 if (ArchNames.insert(Name))
436 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000437 }
438 }
439
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000440 // When there is no explicit arch for this platform, make sure we
441 // still bind the architecture (to the default) so that -Xarch_ is
442 // handled correctly.
443 if (!Archs.size())
444 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000445
446 // FIXME: We killed off some others but these aren't yet detected in
447 // a functional manner. If we added information to jobs about which
448 // "auxiliary" files they wrote then we could detect the conflict
449 // these cause downstream.
450 if (Archs.size() > 1) {
451 // No recovery needed, the point of this is just to prevent
452 // overwriting the same files.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000453 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
454 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000455 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000456 }
457
458 ActionList SingleActions;
459 BuildActions(Args, SingleActions);
460
461 // Add in arch binding and lipo (if necessary) for every top level
462 // action.
463 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
464 Action *Act = SingleActions[i];
465
466 // Make sure we can lipo this kind of output. If not (and it is an
467 // actual output) then we disallow, since we can't create an
468 // output file with the right name without overwriting it. We
469 // could remove this oddity by just changing the output names to
470 // include the arch, which would also fix
471 // -save-temps. Compatibility wins for now.
472
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000473 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000474 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
475 << types::getTypeName(Act->getType());
476
477 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000478 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000479 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000480
481 // Lipo if necessary, We do it this way because we need to set the
482 // arch flag so that -Xarch_ gets overwritten.
483 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
484 Actions.append(Inputs.begin(), Inputs.end());
485 else
486 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
487 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000488}
489
Daniel Dunbar21549232009-03-18 02:55:38 +0000490void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000491 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000492 // Start by constructing the list of inputs and their types.
493
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000494 // Track the current user specified (-x) input. We also explicitly
495 // track the argument used to set the type; we only want to claim
496 // the type when we actually use it, so we warn about unused -x
497 // arguments.
498 types::ID InputType = types::TY_Nothing;
499 Arg *InputTypeArg = 0;
500
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000501 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
502 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
503 it != ie; ++it) {
504 Arg *A = *it;
505
506 if (isa<InputOption>(A->getOption())) {
507 const char *Value = A->getValue(Args);
508 types::ID Ty = types::TY_INVALID;
509
510 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000511 if (InputType == types::TY_Nothing) {
512 // If there was an explicit arg for this, claim it.
513 if (InputTypeArg)
514 InputTypeArg->claim();
515
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000516 // stdin must be handled specially.
517 if (memcmp(Value, "-", 2) == 0) {
518 // If running with -E, treat as a C input (this changes the
519 // builtin macros, for example). This may be overridden by
520 // -ObjC below.
521 //
522 // Otherwise emit an error but still use a valid type to
523 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000524 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000525 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000526 Ty = types::TY_C;
527 } else {
528 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000529 // if not found. We use a host hook here because Darwin at
530 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000531 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000532 Ty = Host->lookupTypeForExtension(Ext + 1);
533
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000534 if (Ty == types::TY_INVALID)
535 Ty = types::TY_Object;
536 }
537
538 // -ObjC and -ObjC++ override the default language, but only
539 // -for "source files". We just treat everything that isn't a
540 // -linker input as a source file.
541 //
542 // FIXME: Clean this up if we move the phase sequence into the
543 // type.
544 if (Ty != types::TY_Object) {
545 if (Args.hasArg(options::OPT_ObjC))
546 Ty = types::TY_ObjC;
547 else if (Args.hasArg(options::OPT_ObjCXX))
548 Ty = types::TY_ObjCXX;
549 }
550 } else {
551 assert(InputTypeArg && "InputType set w/o InputTypeArg");
552 InputTypeArg->claim();
553 Ty = InputType;
554 }
555
556 // Check that the file exists. It isn't clear this is worth
557 // doing, since the tool presumably does this anyway, and this
558 // just adds an extra stat to the equation, but this is gcc
559 // compatible.
560 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000561 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000562 else
563 Inputs.push_back(std::make_pair(Ty, A));
564
565 } else if (A->getOption().isLinkerInput()) {
566 // Just treat as object type, we could make a special type for
567 // this if necessary.
568 Inputs.push_back(std::make_pair(types::TY_Object, A));
569
570 } else if (A->getOption().getId() == options::OPT_x) {
571 InputTypeArg = A;
572 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
573
574 // Follow gcc behavior and treat as linker input for invalid -x
575 // options. Its not clear why we shouldn't just revert to
576 // unknown; but this isn't very important, we might as well be
577 // bug comatible.
578 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000579 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000580 InputType = types::TY_Object;
581 }
582 }
583 }
584
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000585 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000586 Diag(clang::diag::err_drv_no_input_files);
587 return;
588 }
589
590 // Determine which compilation mode we are in. We look for options
591 // which affect the phase, starting with the earliest phases, and
592 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000593 Arg *FinalPhaseArg = 0;
594 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000595
596 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000597 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
598 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
599 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
600 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000601
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000602 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
603 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
604 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000605 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
606 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000607
608 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000609 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
610 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000611
612 // Otherwise do everything.
613 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000614 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000615
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000616 // Reject -Z* at the top level, these options should never have been
617 // exposed by gcc.
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000618 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000619 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000620
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000621 // Construct the actions to perform.
622 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000623 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000624 types::ID InputType = Inputs[i].first;
625 const Arg *InputArg = Inputs[i].second;
626
627 unsigned NumSteps = types::getNumCompilationPhases(InputType);
628 assert(NumSteps && "Invalid number of steps!");
629
630 // If the first step comes after the final phase we are doing as
631 // part of this compilation, warn the user about it.
632 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
633 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000634 // Claim here to avoid the more general unused warning.
635 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000636 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000637 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000638 << getPhaseName(InitialPhase)
639 << FinalPhaseArg->getOption().getName();
640 continue;
641 }
642
643 // Build the pipeline for this file.
644 Action *Current = new InputAction(*InputArg, InputType);
645 for (unsigned i = 0; i != NumSteps; ++i) {
646 phases::ID Phase = types::getCompilationPhase(InputType, i);
647
648 // We are done if this step is past what the user requested.
649 if (Phase > FinalPhase)
650 break;
651
652 // Queue linker inputs.
653 if (Phase == phases::Link) {
654 assert(i + 1 == NumSteps && "linking must be final compilation step.");
655 LinkerInputs.push_back(Current);
656 Current = 0;
657 break;
658 }
659
Daniel Dunbar337a6272009-03-24 20:17:30 +0000660 // Some types skip the assembler phase (e.g., llvm-bc), but we
661 // can't encode this in the steps because the intermediate type
662 // depends on arguments. Just special case here.
663 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
664 continue;
665
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000666 // Otherwise construct the appropriate action.
667 Current = ConstructPhaseAction(Args, Phase, Current);
668 if (Current->getType() == types::TY_Nothing)
669 break;
670 }
671
672 // If we ended with something, add to the output list.
673 if (Current)
674 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000675 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000676
677 // Add a link action if necessary.
678 if (!LinkerInputs.empty())
679 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
680}
681
682Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
683 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000684 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000685 // Build the appropriate action.
686 switch (Phase) {
687 case phases::Link: assert(0 && "link action invalid here.");
688 case phases::Preprocess: {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000689 types::ID OutputTy;
690 // -{M, MM} alter the output type.
691 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
692 OutputTy = types::TY_Dependencies;
693 } else {
694 OutputTy = types::getPreprocessedType(Input->getType());
695 assert(OutputTy != types::TY_INVALID &&
696 "Cannot preprocess this input type!");
697 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000698 return new PreprocessJobAction(Input, OutputTy);
699 }
700 case phases::Precompile:
701 return new PrecompileJobAction(Input, types::TY_PCH);
702 case phases::Compile: {
703 if (Args.hasArg(options::OPT_fsyntax_only)) {
704 return new CompileJobAction(Input, types::TY_Nothing);
705 } else if (Args.hasArg(options::OPT__analyze)) {
706 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000707 } else if (Args.hasArg(options::OPT_emit_llvm) ||
708 Args.hasArg(options::OPT_flto) ||
709 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000710 types::ID Output =
711 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
712 return new CompileJobAction(Input, Output);
713 } else {
714 return new CompileJobAction(Input, types::TY_PP_Asm);
715 }
716 }
717 case phases::Assemble:
718 return new AssembleJobAction(Input, types::TY_Object);
719 }
720
721 assert(0 && "invalid phase in ConstructPhaseAction");
722 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000723}
724
Daniel Dunbar21549232009-03-18 02:55:38 +0000725void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000726 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000727 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
728 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000729
730 // FIXME: Pipes are forcibly disabled until we support executing
731 // them.
732 if (!CCCPrintBindings)
733 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000734
735 // -save-temps inhibits pipes.
736 if (SaveTemps && UsePipes) {
737 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
738 UsePipes = true;
739 }
740
741 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
742
743 // It is an error to provide a -o option if we are making multiple
744 // output files.
745 if (FinalOutput) {
746 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000747 for (ActionList::const_iterator it = C.getActions().begin(),
748 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000749 if ((*it)->getType() != types::TY_Nothing)
750 ++NumOutputs;
751
752 if (NumOutputs > 1) {
753 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
754 FinalOutput = 0;
755 }
756 }
757
Daniel Dunbar21549232009-03-18 02:55:38 +0000758 for (ActionList::const_iterator it = C.getActions().begin(),
759 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000760 Action *A = *it;
761
762 // If we are linking an image for multiple archs then the linker
763 // wants -arch_multiple and -final_output <final image
764 // name>. Unfortunately, this doesn't fit in cleanly because we
765 // have to pass this information down.
766 //
767 // FIXME: This is a hack; find a cleaner way to integrate this
768 // into the process.
769 const char *LinkingOutput = 0;
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000770 if (isa<LipoJobAction>(A)) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000771 if (FinalOutput)
772 LinkingOutput = FinalOutput->getValue(C.getArgs());
773 else
774 LinkingOutput = DefaultImageName.c_str();
775 }
776
777 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000778 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000779 /*CanAcceptPipe*/ true,
780 /*AtTopLevel*/ true,
781 /*LinkingOutput*/ LinkingOutput,
782 II);
783 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000784
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000785 // If there were errors, don't warn about any unused arguments.
786 if (Diags.getNumErrors())
787 return;
788
Daniel Dunbara2094e72009-03-29 22:24:54 +0000789 // Claim -### here.
790 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
791
Daniel Dunbar586dc232009-03-16 06:42:30 +0000792 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
793 it != ie; ++it) {
794 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000795
Daniel Dunbar586dc232009-03-16 06:42:30 +0000796 // FIXME: It would be nice to be able to send the argument to the
797 // Diagnostic, so that extra values, position, and so on could be
798 // printed.
799 if (!A->isClaimed())
800 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000801 << A->getAsString(C.getArgs());
Daniel Dunbar586dc232009-03-16 06:42:30 +0000802 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000803}
804
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000805void Driver::BuildJobsForAction(Compilation &C,
806 const Action *A,
807 const ToolChain *TC,
808 bool CanAcceptPipe,
809 bool AtTopLevel,
810 const char *LinkingOutput,
811 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000812 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000813
814 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
815 // FIXME: Pipes are forcibly disabled until we support executing
816 // them.
817 if (!CCCPrintBindings)
818 UsePipes = false;
819
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000820 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +0000821 // FIXME: It would be nice to not claim this here; maybe the old
822 // scheme of just using Args was better?
823 const Arg &Input = IA->getInputArg();
824 Input.claim();
825 if (isa<PositionalArg>(Input)) {
826 const char *Name = Input.getValue(C.getArgs());
827 Result = InputInfo(Name, A->getType(), Name);
828 } else
829 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000830 return;
831 }
832
833 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
834 const char *ArchName = BAA->getArchName();
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000835 if (!ArchName)
836 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000837 BuildJobsForAction(C,
838 *BAA->begin(),
839 Host->getToolChain(C.getArgs(), ArchName),
840 CanAcceptPipe,
841 AtTopLevel,
842 LinkingOutput,
843 Result);
844 return;
845 }
846
847 const JobAction *JA = cast<JobAction>(A);
848 const Tool &T = TC->SelectTool(C, *JA);
849
850 // See if we should use an integrated preprocessor. We do so when we
851 // have exactly one input, since this is the only use case we care
852 // about (irrelevant since we don't support combine yet).
853 bool UseIntegratedCPP = false;
854 const ActionList *Inputs = &A->getInputs();
855 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
856 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
857 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
858 !C.getArgs().hasArg(options::OPT_save_temps) &&
859 T.hasIntegratedCPP()) {
860 UseIntegratedCPP = true;
861 Inputs = &(*Inputs)[0]->getInputs();
862 }
863 }
864
865 // Only use pipes when there is exactly one input.
866 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000867 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000868 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
869 it != ie; ++it) {
870 InputInfo II;
871 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
872 /*AtTopLevel*/false,
873 LinkingOutput,
874 II);
875 InputInfos.push_back(II);
876 }
877
878 // Determine if we should output to a pipe.
879 bool OutputToPipe = false;
880 if (CanAcceptPipe && T.canPipeOutput()) {
881 // Some actions default to writing to a pipe if they are the top
882 // level phase and there was no user override.
883 //
884 // FIXME: Is there a better way to handle this?
885 if (AtTopLevel) {
886 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
887 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000888 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000889 OutputToPipe = true;
890 }
891
892 // Figure out where to put the job (pipes).
893 Job *Dest = &C.getJobs();
894 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +0000895 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000896 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
897 Dest = &InputInfos[0].getPipe();
898 }
899
900 // Always use the first input as the base input.
901 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +0000902
903 // Determine the place to write output to (nothing, pipe, or
904 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +0000905 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000906 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000907 } else if (OutputToPipe) {
908 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000909 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
910 if (!PJ) {
911 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +0000912 // FIXME: Temporary hack so that -ccc-print-bindings work until
913 // we have pipe support. Please remove later.
914 if (!CCCPrintBindings)
915 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000916 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +0000917 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000918 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000919 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000920 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
921 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000922 }
923
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000924 if (CCCPrintBindings) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000925 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
926 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000927 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
928 llvm::errs() << InputInfos[i].getAsString();
929 if (i + 1 != e)
930 llvm::errs() << ", ";
931 }
932 llvm::errs() << "], output: " << Result.getAsString() << "\n";
933 } else {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000934 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
935 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000936 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000937}
938
Daniel Dunbar441d0602009-03-17 17:53:55 +0000939const char *Driver::GetNamedOutputPath(Compilation &C,
940 const JobAction &JA,
941 const char *BaseInput,
942 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000943 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +0000944 // Output to a user requested destination?
945 if (AtTopLevel) {
946 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
947 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
948 }
949
950 // Output to a temporary file?
951 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +0000952 std::string TmpName =
953 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
954 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +0000955 }
956
957 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +0000958 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +0000959
960 // Determine what the derived output name should be.
961 const char *NamedOutput;
962 if (JA.getType() == types::TY_Image) {
963 NamedOutput = DefaultImageName.c_str();
964 } else {
965 const char *Suffix = types::getTypeTempSuffix(JA.getType());
966 assert(Suffix && "All types used for output should have a suffix.");
967
968 std::string::size_type End = std::string::npos;
969 if (!types::appendSuffixForType(JA.getType()))
970 End = BaseName.rfind('.');
971 std::string Suffixed(BaseName.substr(0, End));
972 Suffixed += '.';
973 Suffixed += Suffix;
974 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
975 }
976
977 // As an annoying special case, PCH generation doesn't strip the
978 // pathname.
979 if (JA.getType() == types::TY_PCH) {
980 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +0000981 if (BasePath.isEmpty())
982 BasePath = NamedOutput;
983 else
984 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000985 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
986 } else {
987 return C.addResultFile(NamedOutput);
988 }
989}
990
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000991llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000992 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000993 const ToolChain::path_list &List = TC.getFilePaths();
994 for (ToolChain::path_list::const_iterator
995 it = List.begin(), ie = List.end(); it != ie; ++it) {
996 llvm::sys::Path P(*it);
997 P.appendComponent(Name);
998 if (P.exists())
999 return P;
1000 }
1001
Daniel Dunbarcb881672009-03-13 00:51:18 +00001002 return llvm::sys::Path(Name);
1003}
1004
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +00001005llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump950bedd2009-03-27 00:40:20 +00001006 const ToolChain &TC,
1007 bool WantFile) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001008 const ToolChain::path_list &List = TC.getProgramPaths();
1009 for (ToolChain::path_list::const_iterator
1010 it = List.begin(), ie = List.end(); it != ie; ++it) {
1011 llvm::sys::Path P(*it);
1012 P.appendComponent(Name);
Mike Stump950bedd2009-03-27 00:40:20 +00001013 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar0edefeb2009-03-18 20:26:19 +00001014 return P;
1015 }
1016
Daniel Dunbarc50b00d2009-03-23 16:15:50 +00001017 // If all else failed, search the path.
1018 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +00001019 if (!P.empty())
1020 return P;
1021
Daniel Dunbarcb881672009-03-13 00:51:18 +00001022 return llvm::sys::Path(Name);
1023}
1024
Daniel Dunbar214399e2009-03-18 19:34:39 +00001025std::string Driver::GetTemporaryPath(const char *Suffix) const {
1026 // FIXME: This is lame; sys::Path should provide this function (in
1027 // particular, it should know how to find the temporary files dir).
1028 std::string Error;
1029 llvm::sys::Path P("/tmp/cc");
1030 if (P.makeUnique(false, &Error)) {
1031 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
1032 return "";
1033 }
1034
Daniel Dunbar84603bc2009-03-18 23:08:52 +00001035 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
1036 // PR3837.
1037 P.eraseFromDisk(false, 0);
1038
Daniel Dunbar214399e2009-03-18 19:34:39 +00001039 P.appendSuffix(Suffix);
1040 return P.toString();
1041}
1042
Daniel Dunbare5049522009-03-17 20:45:45 +00001043const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +00001044 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001045 // Dice into arch, platform, and OS. This matches
1046 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
1047 // and missing fields are left empty.
1048 std::string Arch, Platform, OS;
1049
1050 if (const char *ArchEnd = strchr(Triple, '-')) {
1051 Arch = std::string(Triple, ArchEnd);
1052
1053 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
1054 Platform = std::string(ArchEnd+1, PlatformEnd);
1055 OS = PlatformEnd+1;
1056 } else
1057 Platform = ArchEnd+1;
1058 } else
1059 Arch = Triple;
1060
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +00001061 // Normalize Arch a bit.
1062 //
1063 // FIXME: This is very incomplete.
1064 if (Arch == "i686")
1065 Arch = "i386";
1066 else if (Arch == "amd64")
1067 Arch = "x86_64";
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001068 else if (Arch == "ppc" || Arch == "Power Macintosh")
1069 Arch = "powerpc";
1070 else if (Arch == "ppc64")
1071 Arch = "powerpc64";
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +00001072
Daniel Dunbara88162c2009-03-13 12:23:29 +00001073 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbare5049522009-03-17 20:45:45 +00001074 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
1075 OS.c_str());
Daniel Dunbar75358d22009-03-30 21:06:03 +00001076 if (memcmp(&OS[0], "freebsd", 7) == 0)
1077 return createFreeBSDHostInfo(*this, Arch.c_str(), Platform.c_str(),
1078 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001079
Daniel Dunbare5049522009-03-17 20:45:45 +00001080 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
1081 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001082}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001083
1084bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001085 const std::string &ArchNameStr) const {
1086 // FIXME: Remove this hack.
1087 const char *ArchName = ArchNameStr.c_str();
1088 if (ArchNameStr == "powerpc")
1089 ArchName = "ppc";
1090 else if (ArchNameStr == "powerpc64")
1091 ArchName = "ppc64";
1092
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001093 // Check if user requested no clang, or clang doesn't understand
1094 // this type (we only handle single inputs for now).
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001095 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001096 !types::isAcceptedByClang((*JA.begin())->getType()))
1097 return false;
1098
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001099 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001100 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001101 if (!CCCUseClangCPP) {
1102 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001103 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001104 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001105 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1106 return false;
1107
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001108 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001109 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1110 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001111 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001112 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001113
1114 // Finally, don't use clang if this isn't one of the user specified
1115 // archs to build.
Daniel Dunbar6256d362009-03-24 19:14:56 +00001116 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1117 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001118 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001119 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001120
1121 return true;
1122}
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001123
1124/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1125/// return the grouped values as integers. Numbers which are not
1126/// provided are set to 0.
1127///
1128/// \return True if the entire string was parsed (9.2), or all groups
1129/// were parsed (10.3.5extrastuff).
1130bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1131 unsigned &Minor, unsigned &Micro,
1132 bool &HadExtra) {
1133 HadExtra = false;
1134
1135 Major = Minor = Micro = 0;
1136 if (*Str == '\0')
1137 return true;
1138
1139 char *End;
1140 Major = (unsigned) strtol(Str, &End, 10);
1141 if (*Str != '\0' && *End == '\0')
1142 return true;
1143 if (*End != '.')
1144 return false;
1145
1146 Str = End+1;
1147 Minor = (unsigned) strtol(Str, &End, 10);
1148 if (*Str != '\0' && *End == '\0')
1149 return true;
1150 if (*End != '.')
1151 return false;
1152
1153 Str = End+1;
1154 Micro = (unsigned) strtol(Str, &End, 10);
1155 if (*Str != '\0' && *End == '\0')
1156 return true;
1157 if (Str == End)
1158 return false;
1159 HadExtra = true;
1160 return true;
1161}