blob: 85d76c8c20f8910a5ab464e5890fdb44c32449f3 [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;
36
Daniel Dunbard25acaa2009-03-10 23:41:59 +000037Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000038 const char *_DefaultHostTriple,
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000039 const char *_DefaultImageName,
Daniel Dunbar93468492009-03-12 08:55:43 +000040 Diagnostic &_Diags)
41 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000042 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000043 DefaultImageName(_DefaultImageName),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000044 Host(0),
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +000045 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar0ea85f72009-03-24 19:02:31 +000046 CCCUseClang(true), CCCUseClangCXX(false), CCCUseClangCPP(true),
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +000047 SuppressMissingInputWarning(false)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000048{
Daniel Dunbar0ea85f72009-03-24 19:02:31 +000049 // Only use clang on i386 and x86_64 by default.
50 CCCClangArchs.insert("i386");
51 CCCClangArchs.insert("x86_64");
Daniel Dunbar63c4da92009-03-02 19:59:07 +000052}
53
54Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000055 delete Opts;
Daniel Dunbar2f8b37e2009-03-18 01:09:40 +000056 delete Host;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000057}
58
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000059InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
60 const char **ArgEnd) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000061 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000062 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000063
Daniel Dunbar85cb3592009-03-13 11:38:42 +000064 // FIXME: Handle '@' args (or at least error on them).
65
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000066 unsigned Index = 0, End = ArgEnd - ArgBegin;
67 while (Index < End) {
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000068 // gcc's handling of empty arguments doesn't make
69 // sense, but this is not a common use case. :)
70 //
71 // We just ignore them here (note that other things may
72 // still take them as arguments).
73 if (Args->getArgString(Index)[0] == '\0') {
74 ++Index;
75 continue;
76 }
77
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000078 unsigned Prev = Index;
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000079 Arg *A = getOpts().ParseOneArg(*Args, Index);
80 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbardb62cc32009-03-12 07:58:46 +000081
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000082 // Check for missing argument error.
83 if (!A) {
84 assert(Index >= End && "Unexpected parser error.");
85 Diag(clang::diag::err_drv_missing_argument)
86 << Args->getArgString(Prev)
87 << (Index - Prev - 1);
88 break;
Daniel Dunbardb62cc32009-03-12 07:58:46 +000089 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000090
Daniel Dunbarfb88ce02009-03-22 23:26:43 +000091 if (A->getOption().isUnsupported()) {
92 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
93 continue;
94 }
95 Args->append(A);
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000096 }
97
98 return Args;
99}
100
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000101Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000102 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
103
Daniel Dunbarcc006892009-03-13 00:51:18 +0000104 // FIXME: Handle environment options which effect driver behavior,
105 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
106 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
107
108 // FIXME: What are we going to do with -V and -b?
109
110 // FIXME: Handle CCC_ADD_ARGS.
111
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000112 // FIXME: This stuff needs to go into the Compilation, not the
113 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000114 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000115
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000116 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000117 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000118
119 // Read -ccc args.
120 //
121 // FIXME: We need to figure out where this behavior should
122 // live. Most of it should be outside in the client; the parts that
123 // aren't should have proper options, either by introducing new ones
124 // or by overloading gcc ones like -V or -b.
125 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
126 const char *Opt = *Start + 5;
127
128 if (!strcmp(Opt, "print-options")) {
129 CCCPrintOptions = true;
130 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000131 CCCPrintActions = true;
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000132 } else if (!strcmp(Opt, "print-bindings")) {
133 CCCPrintBindings = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000134 } else if (!strcmp(Opt, "cxx")) {
135 CCCIsCXX = true;
136 } else if (!strcmp(Opt, "echo")) {
137 CCCEcho = true;
138
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000139 } else if (!strcmp(Opt, "clang-cxx")) {
140 CCCUseClangCXX = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000141 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000142 CCCUseClang = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000143 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000144 CCCUseClangCPP = false;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000145 } else if (!strcmp(Opt, "clang-archs")) {
146 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
147 const char *Cur = *++Start;
148
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000149 CCCClangArchs.clear();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000150 for (;;) {
151 const char *Next = strchr(Cur, ',');
152
153 if (Next) {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000154 if (Cur != Next)
155 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000156 Cur = Next + 1;
157 } else {
Daniel Dunbar0ea85f72009-03-24 19:02:31 +0000158 if (*Cur != '\0')
159 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000160 break;
161 }
162 }
163
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000164 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000165 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000166 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000167
168 } else {
169 // FIXME: Error handling.
170 llvm::errs() << "invalid option: " << *Start << "\n";
171 exit(1);
172 }
173 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000174
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000175 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000176
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000177 Host = GetHostInfo(HostTriple);
Daniel Dunbarcc006892009-03-13 00:51:18 +0000178
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000179 // The compilation takes ownership of Args.
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000180 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000181
182 // FIXME: This behavior shouldn't be here.
183 if (CCCPrintOptions) {
184 PrintOptions(C->getArgs());
185 return C;
186 }
187
188 if (!HandleImmediateArgs(*C))
189 return C;
190
191 // Construct the list of abstract actions to perform for this
192 // compilation. We avoid passing a Compilation here simply to
193 // enforce the abstraction that pipelining is not host or toolchain
194 // dependent (other than the driver driver test).
195 if (Host->useDriverDriver())
196 BuildUniversalActions(C->getArgs(), C->getActions());
197 else
198 BuildActions(C->getArgs(), C->getActions());
199
200 if (CCCPrintActions) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000201 PrintActions(*C);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000202 return C;
203 }
204
205 BuildJobs(*C);
Daniel Dunbarc413f822009-03-15 01:38:15 +0000206
207 return C;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000208}
209
Daniel Dunbara790d372009-03-12 18:24:49 +0000210void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000211 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000212 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000213 it != ie; ++it, ++i) {
214 Arg *A = *it;
215 llvm::errs() << "Option " << i << " - "
216 << "Name: \"" << A->getOption().getName() << "\", "
217 << "Values: {";
218 for (unsigned j = 0; j < A->getNumValues(); ++j) {
219 if (j)
220 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000221 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000222 }
223 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000224 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000225}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000226
Daniel Dunbarcc006892009-03-13 00:51:18 +0000227void Driver::PrintVersion() const {
Mike Stump74d94472009-03-18 14:00:02 +0000228 static char buf[] = "$URL$";
229 char *zap = strstr(buf, "/lib/Driver");
230 if (zap)
231 *zap = 0;
232 zap = strstr(buf, "/clang/tools/clang");
233 if (zap)
234 *zap = 0;
Mike Stumpbc927292009-03-18 15:19:35 +0000235 const char *vers = buf+6;
Mike Stump3fc58f02009-03-18 18:45:55 +0000236 // FIXME: Add cmake support and remove #ifdef
237#ifdef SVN_REVISION
238 const char *revision = SVN_REVISION;
239#else
240 const char *revision = "";
241#endif
Daniel Dunbarcc006892009-03-13 00:51:18 +0000242 // FIXME: The following handlers should use a callback mechanism, we
243 // don't know what the client would like to do.
Mike Stump1207fae2009-03-18 21:19:11 +0000244 llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
Mike Stump74d94472009-03-18 14:00:02 +0000245 // FIXME: Add cmake support and remove #ifdef
246#ifdef TARGET_TRIPLE
247 llvm::errs() << "Target: " << TARGET_TRIPLE << "\n";
248#endif
Daniel Dunbarcc006892009-03-13 00:51:18 +0000249}
250
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000251bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000252 // The order these options are handled in in gcc is all over the
253 // place, but we don't expect inconsistencies w.r.t. that to matter
254 // in practice.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000255 if (C.getArgs().hasArg(options::OPT_v) ||
256 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000257 PrintVersion();
258 SuppressMissingInputWarning = true;
259 }
260
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000261 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000262 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
263 llvm::outs() << "programs: =";
264 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
265 ie = TC.getProgramPaths().end(); it != ie; ++it) {
266 if (it != TC.getProgramPaths().begin())
267 llvm::outs() << ':';
268 llvm::outs() << *it;
269 }
270 llvm::outs() << "\n";
271 llvm::outs() << "libraries: =";
272 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
273 ie = TC.getFilePaths().end(); it != ie; ++it) {
274 if (it != TC.getFilePaths().begin())
275 llvm::outs() << ':';
276 llvm::outs() << *it;
277 }
278 llvm::outs() << "\n";
279 }
280
Daniel Dunbarcc006892009-03-13 00:51:18 +0000281 // FIXME: The following handlers should use a callback mechanism, we
282 // don't know what the client would like to do.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000283 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
284 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
285 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000286 return false;
287 }
288
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000289 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
290 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
291 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000292 return false;
293 }
294
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000295 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
296 llvm::outs() << GetProgramPath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000297 return false;
298 }
299
300 return true;
301}
302
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000303static unsigned PrintActions1(const Compilation &C,
Daniel Dunbar494646b2009-03-13 12:19:02 +0000304 Action *A,
305 std::map<Action*, unsigned> &Ids) {
306 if (Ids.count(A))
307 return Ids[A];
308
309 std::string str;
310 llvm::raw_string_ostream os(str);
311
312 os << Action::getClassName(A->getKind()) << ", ";
313 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000314 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000315 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000316 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
317 C.getDefaultToolChain().getArchName()) << '"'
318 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000319 } else {
320 os << "{";
321 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000322 os << PrintActions1(C, *it, Ids);
Daniel Dunbar494646b2009-03-13 12:19:02 +0000323 ++it;
324 if (it != ie)
325 os << ", ";
326 }
327 os << "}";
328 }
329
330 unsigned Id = Ids.size();
331 Ids[A] = Id;
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000332 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000333 << types::getTypeName(A->getType()) << "\n";
334
335 return Id;
336}
337
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000338void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbar494646b2009-03-13 12:19:02 +0000339 std::map<Action*, unsigned> Ids;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000340 for (ActionList::const_iterator it = C.getActions().begin(),
341 ie = C.getActions().end(); it != ie; ++it)
342 PrintActions1(C, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000343}
344
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000345void Driver::BuildUniversalActions(const ArgList &Args,
346 ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000347 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000348 // Collect the list of architectures. Duplicates are allowed, but
349 // should only be handled once (in the order seen).
350 llvm::StringSet<> ArchNames;
351 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000352 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
353 it != ie; ++it) {
354 Arg *A = *it;
355
356 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000357 const char *Name = A->getValue(Args);
358
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000359 // FIXME: We need to handle canonicalization of the specified
360 // arch?
361
Daniel Dunbara345a442009-03-19 07:55:12 +0000362 A->claim();
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000363 if (ArchNames.insert(Name))
364 Archs.push_back(Name);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000365 }
366 }
367
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000368 // When there is no explicit arch for this platform, make sure we
369 // still bind the architecture (to the default) so that -Xarch_ is
370 // handled correctly.
371 if (!Archs.size())
372 Archs.push_back(0);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000373
374 // FIXME: We killed off some others but these aren't yet detected in
375 // a functional manner. If we added information to jobs about which
376 // "auxiliary" files they wrote then we could detect the conflict
377 // these cause downstream.
378 if (Archs.size() > 1) {
379 // No recovery needed, the point of this is just to prevent
380 // overwriting the same files.
381 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
382 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000383 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000384 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
385 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000386 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000387 }
388
389 ActionList SingleActions;
390 BuildActions(Args, SingleActions);
391
392 // Add in arch binding and lipo (if necessary) for every top level
393 // action.
394 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
395 Action *Act = SingleActions[i];
396
397 // Make sure we can lipo this kind of output. If not (and it is an
398 // actual output) then we disallow, since we can't create an
399 // output file with the right name without overwriting it. We
400 // could remove this oddity by just changing the output names to
401 // include the arch, which would also fix
402 // -save-temps. Compatibility wins for now.
403
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000404 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000405 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
406 << types::getTypeName(Act->getType());
407
408 ActionList Inputs;
Daniel Dunbara345a442009-03-19 07:55:12 +0000409 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000410 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000411
412 // Lipo if necessary, We do it this way because we need to set the
413 // arch flag so that -Xarch_ gets overwritten.
414 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
415 Actions.append(Inputs.begin(), Inputs.end());
416 else
417 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
418 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000419}
420
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000421void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000422 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000423 // Start by constructing the list of inputs and their types.
424
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000425 // Track the current user specified (-x) input. We also explicitly
426 // track the argument used to set the type; we only want to claim
427 // the type when we actually use it, so we warn about unused -x
428 // arguments.
429 types::ID InputType = types::TY_Nothing;
430 Arg *InputTypeArg = 0;
431
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000432 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
433 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
434 it != ie; ++it) {
435 Arg *A = *it;
436
437 if (isa<InputOption>(A->getOption())) {
438 const char *Value = A->getValue(Args);
439 types::ID Ty = types::TY_INVALID;
440
441 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000442 if (InputType == types::TY_Nothing) {
443 // If there was an explicit arg for this, claim it.
444 if (InputTypeArg)
445 InputTypeArg->claim();
446
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000447 // stdin must be handled specially.
448 if (memcmp(Value, "-", 2) == 0) {
449 // If running with -E, treat as a C input (this changes the
450 // builtin macros, for example). This may be overridden by
451 // -ObjC below.
452 //
453 // Otherwise emit an error but still use a valid type to
454 // avoid spurious errors (e.g., no inputs).
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000455 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbard724e332009-03-12 09:13:48 +0000456 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000457 Ty = types::TY_C;
458 } else {
459 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000460 // if not found. We use a host hook here because Darwin at
461 // least has its own idea of what .s is.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000462 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbar7b6dfbd2009-03-20 23:39:23 +0000463 Ty = Host->lookupTypeForExtension(Ext + 1);
464
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000465 if (Ty == types::TY_INVALID)
466 Ty = types::TY_Object;
467 }
468
469 // -ObjC and -ObjC++ override the default language, but only
470 // -for "source files". We just treat everything that isn't a
471 // -linker input as a source file.
472 //
473 // FIXME: Clean this up if we move the phase sequence into the
474 // type.
475 if (Ty != types::TY_Object) {
476 if (Args.hasArg(options::OPT_ObjC))
477 Ty = types::TY_ObjC;
478 else if (Args.hasArg(options::OPT_ObjCXX))
479 Ty = types::TY_ObjCXX;
480 }
481 } else {
482 assert(InputTypeArg && "InputType set w/o InputTypeArg");
483 InputTypeArg->claim();
484 Ty = InputType;
485 }
486
487 // Check that the file exists. It isn't clear this is worth
488 // doing, since the tool presumably does this anyway, and this
489 // just adds an extra stat to the equation, but this is gcc
490 // compatible.
491 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000492 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000493 else
494 Inputs.push_back(std::make_pair(Ty, A));
495
496 } else if (A->getOption().isLinkerInput()) {
497 // Just treat as object type, we could make a special type for
498 // this if necessary.
499 Inputs.push_back(std::make_pair(types::TY_Object, A));
500
501 } else if (A->getOption().getId() == options::OPT_x) {
502 InputTypeArg = A;
503 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
504
505 // Follow gcc behavior and treat as linker input for invalid -x
506 // options. Its not clear why we shouldn't just revert to
507 // unknown; but this isn't very important, we might as well be
508 // bug comatible.
509 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000510 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000511 InputType = types::TY_Object;
512 }
513 }
514 }
515
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000516 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000517 Diag(clang::diag::err_drv_no_input_files);
518 return;
519 }
520
521 // Determine which compilation mode we are in. We look for options
522 // which affect the phase, starting with the earliest phases, and
523 // record which option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000524 Arg *FinalPhaseArg = 0;
525 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000526
527 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000528 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
529 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
530 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
531 FinalPhase = phases::Preprocess;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000532
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000533 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
534 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
535 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000536 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
537 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000538
539 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000540 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
541 FinalPhase = phases::Assemble;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000542
543 // Otherwise do everything.
544 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000545 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000546
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000547 // Reject -Z* at the top level, these options should never have been
548 // exposed by gcc.
549 if (Arg *A = Args.getLastArg(options::OPT_Z))
Daniel Dunbar73225932009-03-20 06:14:23 +0000550 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000551
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000552 // Construct the actions to perform.
553 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000554 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000555 types::ID InputType = Inputs[i].first;
556 const Arg *InputArg = Inputs[i].second;
557
558 unsigned NumSteps = types::getNumCompilationPhases(InputType);
559 assert(NumSteps && "Invalid number of steps!");
560
561 // If the first step comes after the final phase we are doing as
562 // part of this compilation, warn the user about it.
563 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
564 if (InitialPhase > FinalPhase) {
Daniel Dunbar923e7032009-03-19 07:57:08 +0000565 // Claim here to avoid the more general unused warning.
566 InputArg->claim();
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000567 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar73225932009-03-20 06:14:23 +0000568 << InputArg->getAsString(Args)
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000569 << getPhaseName(InitialPhase)
570 << FinalPhaseArg->getOption().getName();
571 continue;
572 }
573
574 // Build the pipeline for this file.
575 Action *Current = new InputAction(*InputArg, InputType);
576 for (unsigned i = 0; i != NumSteps; ++i) {
577 phases::ID Phase = types::getCompilationPhase(InputType, i);
578
579 // We are done if this step is past what the user requested.
580 if (Phase > FinalPhase)
581 break;
582
583 // Queue linker inputs.
584 if (Phase == phases::Link) {
585 assert(i + 1 == NumSteps && "linking must be final compilation step.");
586 LinkerInputs.push_back(Current);
587 Current = 0;
588 break;
589 }
590
Daniel Dunbardec14452009-03-24 20:17:30 +0000591 // Some types skip the assembler phase (e.g., llvm-bc), but we
592 // can't encode this in the steps because the intermediate type
593 // depends on arguments. Just special case here.
594 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
595 continue;
596
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000597 // Otherwise construct the appropriate action.
598 Current = ConstructPhaseAction(Args, Phase, Current);
599 if (Current->getType() == types::TY_Nothing)
600 break;
601 }
602
603 // If we ended with something, add to the output list.
604 if (Current)
605 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000606 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000607
608 // Add a link action if necessary.
609 if (!LinkerInputs.empty())
610 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
611}
612
613Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
614 Action *Input) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000615 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000616 // Build the appropriate action.
617 switch (Phase) {
618 case phases::Link: assert(0 && "link action invalid here.");
619 case phases::Preprocess: {
620 types::ID OutputTy = types::getPreprocessedType(Input->getType());
621 assert(OutputTy != types::TY_INVALID &&
622 "Cannot preprocess this input type!");
623 return new PreprocessJobAction(Input, OutputTy);
624 }
625 case phases::Precompile:
626 return new PrecompileJobAction(Input, types::TY_PCH);
627 case phases::Compile: {
628 if (Args.hasArg(options::OPT_fsyntax_only)) {
629 return new CompileJobAction(Input, types::TY_Nothing);
630 } else if (Args.hasArg(options::OPT__analyze)) {
631 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbardec14452009-03-24 20:17:30 +0000632 } else if (Args.hasArg(options::OPT_emit_llvm) ||
633 Args.hasArg(options::OPT_flto) ||
634 Args.hasArg(options::OPT_O4)) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000635 types::ID Output =
636 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
637 return new CompileJobAction(Input, Output);
638 } else {
639 return new CompileJobAction(Input, types::TY_PP_Asm);
640 }
641 }
642 case phases::Assemble:
643 return new AssembleJobAction(Input, types::TY_Object);
644 }
645
646 assert(0 && "invalid phase in ConstructPhaseAction");
647 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000648}
649
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000650void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000651 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000652 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
653 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000654
655 // FIXME: Pipes are forcibly disabled until we support executing
656 // them.
657 if (!CCCPrintBindings)
658 UsePipes = false;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000659
660 // -save-temps inhibits pipes.
661 if (SaveTemps && UsePipes) {
662 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
663 UsePipes = true;
664 }
665
666 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
667
668 // It is an error to provide a -o option if we are making multiple
669 // output files.
670 if (FinalOutput) {
671 unsigned NumOutputs = 0;
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000672 for (ActionList::const_iterator it = C.getActions().begin(),
673 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000674 if ((*it)->getType() != types::TY_Nothing)
675 ++NumOutputs;
676
677 if (NumOutputs > 1) {
678 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
679 FinalOutput = 0;
680 }
681 }
682
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000683 for (ActionList::const_iterator it = C.getActions().begin(),
684 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000685 Action *A = *it;
686
687 // If we are linking an image for multiple archs then the linker
688 // wants -arch_multiple and -final_output <final image
689 // name>. Unfortunately, this doesn't fit in cleanly because we
690 // have to pass this information down.
691 //
692 // FIXME: This is a hack; find a cleaner way to integrate this
693 // into the process.
694 const char *LinkingOutput = 0;
695 if (isa<LinkJobAction>(A)) {
696 if (FinalOutput)
697 LinkingOutput = FinalOutput->getValue(C.getArgs());
698 else
699 LinkingOutput = DefaultImageName.c_str();
700 }
701
702 InputInfo II;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000703 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000704 /*CanAcceptPipe*/ true,
705 /*AtTopLevel*/ true,
706 /*LinkingOutput*/ LinkingOutput,
707 II);
708 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000709
Daniel Dunbar46c70822009-03-18 18:03:46 +0000710 // If there were errors, don't warn about any unused arguments.
711 if (Diags.getNumErrors())
712 return;
713
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000714 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
715 it != ie; ++it) {
716 Arg *A = *it;
Daniel Dunbar46c70822009-03-18 18:03:46 +0000717
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000718 // FIXME: It would be nice to be able to send the argument to the
719 // Diagnostic, so that extra values, position, and so on could be
720 // printed.
721 if (!A->isClaimed())
722 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar73225932009-03-20 06:14:23 +0000723 << A->getAsString(C.getArgs());
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000724 }
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000725}
726
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000727void Driver::BuildJobsForAction(Compilation &C,
728 const Action *A,
729 const ToolChain *TC,
730 bool CanAcceptPipe,
731 bool AtTopLevel,
732 const char *LinkingOutput,
733 InputInfo &Result) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000734 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000735
736 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
737 // FIXME: Pipes are forcibly disabled until we support executing
738 // them.
739 if (!CCCPrintBindings)
740 UsePipes = false;
741
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000742 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarc3be67b2009-03-19 07:29:38 +0000743 // FIXME: It would be nice to not claim this here; maybe the old
744 // scheme of just using Args was better?
745 const Arg &Input = IA->getInputArg();
746 Input.claim();
747 if (isa<PositionalArg>(Input)) {
748 const char *Name = Input.getValue(C.getArgs());
749 Result = InputInfo(Name, A->getType(), Name);
750 } else
751 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000752 return;
753 }
754
755 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
756 const char *ArchName = BAA->getArchName();
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000757 if (!ArchName)
758 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000759 BuildJobsForAction(C,
760 *BAA->begin(),
761 Host->getToolChain(C.getArgs(), ArchName),
762 CanAcceptPipe,
763 AtTopLevel,
764 LinkingOutput,
765 Result);
766 return;
767 }
768
769 const JobAction *JA = cast<JobAction>(A);
770 const Tool &T = TC->SelectTool(C, *JA);
771
772 // See if we should use an integrated preprocessor. We do so when we
773 // have exactly one input, since this is the only use case we care
774 // about (irrelevant since we don't support combine yet).
775 bool UseIntegratedCPP = false;
776 const ActionList *Inputs = &A->getInputs();
777 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
778 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
779 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
780 !C.getArgs().hasArg(options::OPT_save_temps) &&
781 T.hasIntegratedCPP()) {
782 UseIntegratedCPP = true;
783 Inputs = &(*Inputs)[0]->getInputs();
784 }
785 }
786
787 // Only use pipes when there is exactly one input.
788 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar00fd3792009-03-18 06:00:36 +0000789 InputInfoList InputInfos;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000790 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
791 it != ie; ++it) {
792 InputInfo II;
793 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
794 /*AtTopLevel*/false,
795 LinkingOutput,
796 II);
797 InputInfos.push_back(II);
798 }
799
800 // Determine if we should output to a pipe.
801 bool OutputToPipe = false;
802 if (CanAcceptPipe && T.canPipeOutput()) {
803 // Some actions default to writing to a pipe if they are the top
804 // level phase and there was no user override.
805 //
806 // FIXME: Is there a better way to handle this?
807 if (AtTopLevel) {
808 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
809 OutputToPipe = true;
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000810 } else if (UsePipes)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000811 OutputToPipe = true;
812 }
813
814 // Figure out where to put the job (pipes).
815 Job *Dest = &C.getJobs();
816 if (InputInfos[0].isPipe()) {
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000817 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000818 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
819 Dest = &InputInfos[0].getPipe();
820 }
821
822 // Always use the first input as the base input.
823 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000824
825 // Determine the place to write output to (nothing, pipe, or
826 // filename) and where to put the new job.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000827 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000828 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000829 } else if (OutputToPipe) {
830 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000831 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
832 if (!PJ) {
833 PJ = new PipedJob();
Daniel Dunbar8eeb5a32009-03-20 00:11:04 +0000834 // FIXME: Temporary hack so that -ccc-print-bindings work until
835 // we have pipe support. Please remove later.
836 if (!CCCPrintBindings)
837 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar933ac452009-03-18 07:06:02 +0000838 Dest = PJ;
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000839 }
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000840 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000841 } else {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000842 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
843 A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000844 }
845
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000846 if (CCCPrintBindings) {
847 llvm::errs() << "bind - \"" << T.getName() << "\", inputs: [";
848 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
849 llvm::errs() << InputInfos[i].getAsString();
850 if (i + 1 != e)
851 llvm::errs() << ", ";
852 }
853 llvm::errs() << "], output: " << Result.getAsString() << "\n";
854 } else {
Daniel Dunbara16e4fe2009-03-25 04:13:45 +0000855 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
856 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000857 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000858}
859
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000860const char *Driver::GetNamedOutputPath(Compilation &C,
861 const JobAction &JA,
862 const char *BaseInput,
863 bool AtTopLevel) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000864 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000865 // Output to a user requested destination?
866 if (AtTopLevel) {
867 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
868 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
869 }
870
871 // Output to a temporary file?
872 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarb6ddc952009-03-18 19:34:39 +0000873 std::string TmpName =
874 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
875 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000876 }
877
878 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbarfb5e3332009-03-18 02:00:31 +0000879 std::string BaseName(BasePath.getLast());
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000880
881 // Determine what the derived output name should be.
882 const char *NamedOutput;
883 if (JA.getType() == types::TY_Image) {
884 NamedOutput = DefaultImageName.c_str();
885 } else {
886 const char *Suffix = types::getTypeTempSuffix(JA.getType());
887 assert(Suffix && "All types used for output should have a suffix.");
888
889 std::string::size_type End = std::string::npos;
890 if (!types::appendSuffixForType(JA.getType()))
891 End = BaseName.rfind('.');
892 std::string Suffixed(BaseName.substr(0, End));
893 Suffixed += '.';
894 Suffixed += Suffix;
895 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
896 }
897
898 // As an annoying special case, PCH generation doesn't strip the
899 // pathname.
900 if (JA.getType() == types::TY_PCH) {
901 BasePath.eraseComponent();
Daniel Dunbar58a51262009-03-18 09:58:30 +0000902 if (BasePath.isEmpty())
903 BasePath = NamedOutput;
904 else
905 BasePath.appendComponent(NamedOutput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000906 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
907 } else {
908 return C.addResultFile(NamedOutput);
909 }
910}
911
Daniel Dunbare1cef7d2009-03-16 05:25:36 +0000912llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000913 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +0000914 const ToolChain::path_list &List = TC.getFilePaths();
915 for (ToolChain::path_list::const_iterator
916 it = List.begin(), ie = List.end(); it != ie; ++it) {
917 llvm::sys::Path P(*it);
918 P.appendComponent(Name);
919 if (P.exists())
920 return P;
921 }
922
Daniel Dunbarcc006892009-03-13 00:51:18 +0000923 return llvm::sys::Path(Name);
924}
925
Daniel Dunbare1cef7d2009-03-16 05:25:36 +0000926llvm::sys::Path Driver::GetProgramPath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000927 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +0000928 const ToolChain::path_list &List = TC.getProgramPaths();
929 for (ToolChain::path_list::const_iterator
930 it = List.begin(), ie = List.end(); it != ie; ++it) {
931 llvm::sys::Path P(*it);
932 P.appendComponent(Name);
933 if (P.exists())
934 return P;
935 }
936
Daniel Dunbar49a35de2009-03-23 16:15:50 +0000937 // If all else failed, search the path.
938 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +0000939 if (!P.empty())
940 return P;
941
Daniel Dunbarcc006892009-03-13 00:51:18 +0000942 return llvm::sys::Path(Name);
943}
944
Daniel Dunbarb6ddc952009-03-18 19:34:39 +0000945std::string Driver::GetTemporaryPath(const char *Suffix) const {
946 // FIXME: This is lame; sys::Path should provide this function (in
947 // particular, it should know how to find the temporary files dir).
948 std::string Error;
949 llvm::sys::Path P("/tmp/cc");
950 if (P.makeUnique(false, &Error)) {
951 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
952 return "";
953 }
954
Daniel Dunbar9083abe2009-03-18 23:08:52 +0000955 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
956 // PR3837.
957 P.eraseFromDisk(false, 0);
958
Daniel Dunbarb6ddc952009-03-18 19:34:39 +0000959 P.appendSuffix(Suffix);
960 return P.toString();
961}
962
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000963const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000964 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000965 // Dice into arch, platform, and OS. This matches
966 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
967 // and missing fields are left empty.
968 std::string Arch, Platform, OS;
969
970 if (const char *ArchEnd = strchr(Triple, '-')) {
971 Arch = std::string(Triple, ArchEnd);
972
973 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
974 Platform = std::string(ArchEnd+1, PlatformEnd);
975 OS = PlatformEnd+1;
976 } else
977 Platform = ArchEnd+1;
978 } else
979 Arch = Triple;
980
Daniel Dunbar7424b8a2009-03-17 19:00:50 +0000981 // Normalize Arch a bit.
982 //
983 // FIXME: This is very incomplete.
984 if (Arch == "i686")
985 Arch = "i386";
986 else if (Arch == "amd64")
987 Arch = "x86_64";
Daniel Dunbard78126f2009-03-18 04:41:46 +0000988 else if (Arch == "powerpc" || Arch == "Power Macintosh")
989 Arch = "ppc";
Daniel Dunbar7424b8a2009-03-17 19:00:50 +0000990
Daniel Dunbar44119a12009-03-13 12:23:29 +0000991 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000992 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
993 OS.c_str());
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000994
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000995 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
996 OS.c_str());
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000997}
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +0000998
999bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
1000 const std::string &ArchName) const {
1001 // Check if user requested no clang, or clang doesn't understand
1002 // this type (we only handle single inputs for now).
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001003 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001004 !types::isAcceptedByClang((*JA.begin())->getType()))
1005 return false;
1006
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001007 // Otherwise make sure this is an action clang understands.
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001008 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001009 if (!CCCUseClangCPP) {
1010 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001011 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001012 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001013 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1014 return false;
1015
Daniel Dunbar0ea85f72009-03-24 19:02:31 +00001016 // Use clang for C++?
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001017 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1018 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001019 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001020 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001021
1022 // Finally, don't use clang if this isn't one of the user specified
1023 // archs to build.
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001024 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1025 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001026 return false;
Daniel Dunbarb1ae14d2009-03-24 19:14:56 +00001027 }
Daniel Dunbar1a9c8ca2009-03-24 18:57:02 +00001028
1029 return true;
1030}