blob: 2ffd64191014c6919114e4ac41f6e642fb254674 [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;
36
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000037Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000038 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000039 const char *_DefaultImageName,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000040 Diagnostic &_Diags)
41 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000042 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000043 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000044 Host(0),
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +000045 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +000046 CCCUseClang(true), CCCUseClangCXX(false), CCCUseClangCPP(true),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000047 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000048{
Daniel Dunbar0f99d2e2009-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 Dunbar3ede8d02009-03-02 19:59:07 +000052}
53
54Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000055 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000056 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000057}
58
Daniel Dunbar06482622009-03-05 06:38:47 +000059ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000060 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbar06482622009-03-05 06:38:47 +000061 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
62
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000063 // FIXME: Handle '@' args (or at least error on them).
64
Daniel Dunbar06482622009-03-05 06:38:47 +000065 unsigned Index = 0, End = ArgEnd - ArgBegin;
66 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000067 // gcc's handling of empty arguments doesn't make
68 // sense, but this is not a common use case. :)
69 //
70 // We just ignore them here (note that other things may
71 // still take them as arguments).
72 if (Args->getArgString(Index)[0] == '\0') {
73 ++Index;
74 continue;
75 }
76
Daniel Dunbar06482622009-03-05 06:38:47 +000077 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000078 Arg *A = getOpts().ParseOneArg(*Args, Index);
79 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000080
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000081 // Check for missing argument error.
82 if (!A) {
83 assert(Index >= End && "Unexpected parser error.");
84 Diag(clang::diag::err_drv_missing_argument)
85 << Args->getArgString(Prev)
86 << (Index - Prev - 1);
87 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +000088 }
Daniel Dunbar06482622009-03-05 06:38:47 +000089
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000090 if (A->getOption().isUnsupported()) {
91 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
92 continue;
93 }
94 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +000095 }
96
97 return Args;
98}
99
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000100Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000101 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
102
Daniel Dunbarcb881672009-03-13 00:51:18 +0000103 // FIXME: Handle environment options which effect driver behavior,
104 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
105 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
106
107 // FIXME: What are we going to do with -V and -b?
108
109 // FIXME: Handle CCC_ADD_ARGS.
110
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000111 // FIXME: This stuff needs to go into the Compilation, not the
112 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000113 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000114
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000115 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000116 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000117
118 // Read -ccc args.
119 //
120 // FIXME: We need to figure out where this behavior should
121 // live. Most of it should be outside in the client; the parts that
122 // aren't should have proper options, either by introducing new ones
123 // or by overloading gcc ones like -V or -b.
124 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
125 const char *Opt = *Start + 5;
126
127 if (!strcmp(Opt, "print-options")) {
128 CCCPrintOptions = true;
129 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000130 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000131 } else if (!strcmp(Opt, "print-bindings")) {
132 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000133 } else if (!strcmp(Opt, "cxx")) {
134 CCCIsCXX = true;
135 } else if (!strcmp(Opt, "echo")) {
136 CCCEcho = true;
137
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000138 } else if (!strcmp(Opt, "clang-cxx")) {
139 CCCUseClangCXX = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000140 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000141 CCCUseClang = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000142 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000143 CCCUseClangCPP = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000144 } else if (!strcmp(Opt, "clang-archs")) {
145 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
146 const char *Cur = *++Start;
147
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000148 CCCClangArchs.clear();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000149 for (;;) {
150 const char *Next = strchr(Cur, ',');
151
152 if (Next) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000153 if (Cur != Next)
154 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000155 Cur = Next + 1;
156 } else {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000157 if (*Cur != '\0')
158 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000159 break;
160 }
161 }
162
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000163 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000164 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000165 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000166
167 } else {
168 // FIXME: Error handling.
169 llvm::errs() << "invalid option: " << *Start << "\n";
170 exit(1);
171 }
172 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000173
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000174 ArgList *Args = ParseArgStrings(Start, End);
175
Daniel Dunbare5049522009-03-17 20:45:45 +0000176 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000177
Daniel Dunbar586dc232009-03-16 06:42:30 +0000178 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000179 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000180
181 // FIXME: This behavior shouldn't be here.
182 if (CCCPrintOptions) {
183 PrintOptions(C->getArgs());
184 return C;
185 }
186
187 if (!HandleImmediateArgs(*C))
188 return C;
189
190 // Construct the list of abstract actions to perform for this
191 // compilation. We avoid passing a Compilation here simply to
192 // enforce the abstraction that pipelining is not host or toolchain
193 // dependent (other than the driver driver test).
194 if (Host->useDriverDriver())
195 BuildUniversalActions(C->getArgs(), C->getActions());
196 else
197 BuildActions(C->getArgs(), C->getActions());
198
199 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000200 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000201 return C;
202 }
203
204 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000205
206 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000207}
208
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000209void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000210 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000211 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000212 it != ie; ++it, ++i) {
213 Arg *A = *it;
214 llvm::errs() << "Option " << i << " - "
215 << "Name: \"" << A->getOption().getName() << "\", "
216 << "Values: {";
217 for (unsigned j = 0; j < A->getNumValues(); ++j) {
218 if (j)
219 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000220 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000221 }
222 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000223 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000224}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000225
Daniel Dunbarcb881672009-03-13 00:51:18 +0000226void Driver::PrintVersion() const {
Mike Stump5d023c32009-03-18 14:00:02 +0000227 static char buf[] = "$URL$";
228 char *zap = strstr(buf, "/lib/Driver");
229 if (zap)
230 *zap = 0;
231 zap = strstr(buf, "/clang/tools/clang");
232 if (zap)
233 *zap = 0;
Mike Stumpe70295b2009-03-18 15:19:35 +0000234 const char *vers = buf+6;
Mike Stump8944c382009-03-18 18:45:55 +0000235 // FIXME: Add cmake support and remove #ifdef
236#ifdef SVN_REVISION
237 const char *revision = SVN_REVISION;
238#else
239 const char *revision = "";
240#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000241 // FIXME: The following handlers should use a callback mechanism, we
242 // don't know what the client would like to do.
Mike Stumpd227fe72009-03-18 21:19:11 +0000243 llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
Mike Stump5d023c32009-03-18 14:00:02 +0000244 // FIXME: Add cmake support and remove #ifdef
245#ifdef TARGET_TRIPLE
246 llvm::errs() << "Target: " << TARGET_TRIPLE << "\n";
247#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000248}
249
Daniel Dunbar21549232009-03-18 02:55:38 +0000250bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000251 // The order these options are handled in in gcc is all over the
252 // place, but we don't expect inconsistencies w.r.t. that to matter
253 // in practice.
Daniel Dunbar21549232009-03-18 02:55:38 +0000254 if (C.getArgs().hasArg(options::OPT_v) ||
255 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000256 PrintVersion();
257 SuppressMissingInputWarning = true;
258 }
259
Daniel Dunbar21549232009-03-18 02:55:38 +0000260 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000261 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
262 llvm::outs() << "programs: =";
263 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
264 ie = TC.getProgramPaths().end(); it != ie; ++it) {
265 if (it != TC.getProgramPaths().begin())
266 llvm::outs() << ':';
267 llvm::outs() << *it;
268 }
269 llvm::outs() << "\n";
270 llvm::outs() << "libraries: =";
271 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
272 ie = TC.getFilePaths().end(); it != ie; ++it) {
273 if (it != TC.getFilePaths().begin())
274 llvm::outs() << ':';
275 llvm::outs() << *it;
276 }
277 llvm::outs() << "\n";
278 }
279
Daniel Dunbarcb881672009-03-13 00:51:18 +0000280 // FIXME: The following handlers should use a callback mechanism, we
281 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000282 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
283 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
284 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000285 return false;
286 }
287
Daniel Dunbar21549232009-03-18 02:55:38 +0000288 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
289 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
290 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000291 return false;
292 }
293
Daniel Dunbar21549232009-03-18 02:55:38 +0000294 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
295 llvm::outs() << GetProgramPath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000296 return false;
297 }
298
299 return true;
300}
301
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000302static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000303 Action *A,
304 std::map<Action*, unsigned> &Ids) {
305 if (Ids.count(A))
306 return Ids[A];
307
308 std::string str;
309 llvm::raw_string_ostream os(str);
310
311 os << Action::getClassName(A->getKind()) << ", ";
312 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000313 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000314 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000315 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
316 C.getDefaultToolChain().getArchName()) << '"'
317 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000318 } else {
319 os << "{";
320 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000321 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000322 ++it;
323 if (it != ie)
324 os << ", ";
325 }
326 os << "}";
327 }
328
329 unsigned Id = Ids.size();
330 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000331 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000332 << types::getTypeName(A->getType()) << "\n";
333
334 return Id;
335}
336
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000337void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000338 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000339 for (ActionList::const_iterator it = C.getActions().begin(),
340 ie = C.getActions().end(); it != ie; ++it)
341 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000342}
343
Daniel Dunbar21549232009-03-18 02:55:38 +0000344void Driver::BuildUniversalActions(const ArgList &Args,
345 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000346 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000347 // Collect the list of architectures. Duplicates are allowed, but
348 // should only be handled once (in the order seen).
349 llvm::StringSet<> ArchNames;
350 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000351 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
352 it != ie; ++it) {
353 Arg *A = *it;
354
355 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000356 const char *Name = A->getValue(Args);
357
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000358 // FIXME: We need to handle canonicalization of the specified
359 // arch?
360
Daniel Dunbar75877192009-03-19 07:55:12 +0000361 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000362 if (ArchNames.insert(Name))
363 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000364 }
365 }
366
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000367 // When there is no explicit arch for this platform, make sure we
368 // still bind the architecture (to the default) so that -Xarch_ is
369 // handled correctly.
370 if (!Archs.size())
371 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000372
373 // FIXME: We killed off some others but these aren't yet detected in
374 // a functional manner. If we added information to jobs about which
375 // "auxiliary" files they wrote then we could detect the conflict
376 // these cause downstream.
377 if (Archs.size() > 1) {
378 // No recovery needed, the point of this is just to prevent
379 // overwriting the same files.
380 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
381 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000382 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000383 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
384 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000385 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000386 }
387
388 ActionList SingleActions;
389 BuildActions(Args, SingleActions);
390
391 // Add in arch binding and lipo (if necessary) for every top level
392 // action.
393 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
394 Action *Act = SingleActions[i];
395
396 // Make sure we can lipo this kind of output. If not (and it is an
397 // actual output) then we disallow, since we can't create an
398 // output file with the right name without overwriting it. We
399 // could remove this oddity by just changing the output names to
400 // include the arch, which would also fix
401 // -save-temps. Compatibility wins for now.
402
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000403 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000404 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
405 << types::getTypeName(Act->getType());
406
407 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000408 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000409 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000410
411 // Lipo if necessary, We do it this way because we need to set the
412 // arch flag so that -Xarch_ gets overwritten.
413 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
414 Actions.append(Inputs.begin(), Inputs.end());
415 else
416 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
417 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000418}
419
Daniel Dunbar21549232009-03-18 02:55:38 +0000420void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000421 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000422 // Start by constructing the list of inputs and their types.
423
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000424 // Track the current user specified (-x) input. We also explicitly
425 // track the argument used to set the type; we only want to claim
426 // the type when we actually use it, so we warn about unused -x
427 // arguments.
428 types::ID InputType = types::TY_Nothing;
429 Arg *InputTypeArg = 0;
430
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000431 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
432 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
433 it != ie; ++it) {
434 Arg *A = *it;
435
436 if (isa<InputOption>(A->getOption())) {
437 const char *Value = A->getValue(Args);
438 types::ID Ty = types::TY_INVALID;
439
440 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000441 if (InputType == types::TY_Nothing) {
442 // If there was an explicit arg for this, claim it.
443 if (InputTypeArg)
444 InputTypeArg->claim();
445
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000446 // stdin must be handled specially.
447 if (memcmp(Value, "-", 2) == 0) {
448 // If running with -E, treat as a C input (this changes the
449 // builtin macros, for example). This may be overridden by
450 // -ObjC below.
451 //
452 // Otherwise emit an error but still use a valid type to
453 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000454 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000455 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000456 Ty = types::TY_C;
457 } else {
458 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000459 // if not found. We use a host hook here because Darwin at
460 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000461 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000462 Ty = Host->lookupTypeForExtension(Ext + 1);
463
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000464 if (Ty == types::TY_INVALID)
465 Ty = types::TY_Object;
466 }
467
468 // -ObjC and -ObjC++ override the default language, but only
469 // -for "source files". We just treat everything that isn't a
470 // -linker input as a source file.
471 //
472 // FIXME: Clean this up if we move the phase sequence into the
473 // type.
474 if (Ty != types::TY_Object) {
475 if (Args.hasArg(options::OPT_ObjC))
476 Ty = types::TY_ObjC;
477 else if (Args.hasArg(options::OPT_ObjCXX))
478 Ty = types::TY_ObjCXX;
479 }
480 } else {
481 assert(InputTypeArg && "InputType set w/o InputTypeArg");
482 InputTypeArg->claim();
483 Ty = InputType;
484 }
485
486 // Check that the file exists. It isn't clear this is worth
487 // doing, since the tool presumably does this anyway, and this
488 // just adds an extra stat to the equation, but this is gcc
489 // compatible.
490 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000491 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000492 else
493 Inputs.push_back(std::make_pair(Ty, A));
494
495 } else if (A->getOption().isLinkerInput()) {
496 // Just treat as object type, we could make a special type for
497 // this if necessary.
498 Inputs.push_back(std::make_pair(types::TY_Object, A));
499
500 } else if (A->getOption().getId() == options::OPT_x) {
501 InputTypeArg = A;
502 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
503
504 // Follow gcc behavior and treat as linker input for invalid -x
505 // options. Its not clear why we shouldn't just revert to
506 // unknown; but this isn't very important, we might as well be
507 // bug comatible.
508 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000509 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000510 InputType = types::TY_Object;
511 }
512 }
513 }
514
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000515 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000516 Diag(clang::diag::err_drv_no_input_files);
517 return;
518 }
519
520 // Determine which compilation mode we are in. We look for options
521 // which affect the phase, starting with the earliest phases, and
522 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000523 Arg *FinalPhaseArg = 0;
524 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000525
526 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000527 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
528 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
529 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
530 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000531
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000532 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
533 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
534 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000535 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
536 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000537
538 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000539 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
540 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000541
542 // Otherwise do everything.
543 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000544 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000545
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000546 // Reject -Z* at the top level, these options should never have been
547 // exposed by gcc.
548 if (Arg *A = Args.getLastArg(options::OPT_Z))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000549 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000550
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000551 // Construct the actions to perform.
552 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000553 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000554 types::ID InputType = Inputs[i].first;
555 const Arg *InputArg = Inputs[i].second;
556
557 unsigned NumSteps = types::getNumCompilationPhases(InputType);
558 assert(NumSteps && "Invalid number of steps!");
559
560 // If the first step comes after the final phase we are doing as
561 // part of this compilation, warn the user about it.
562 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
563 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000564 // Claim here to avoid the more general unused warning.
565 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000566 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000567 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000568 << getPhaseName(InitialPhase)
569 << FinalPhaseArg->getOption().getName();
570 continue;
571 }
572
573 // Build the pipeline for this file.
574 Action *Current = new InputAction(*InputArg, InputType);
575 for (unsigned i = 0; i != NumSteps; ++i) {
576 phases::ID Phase = types::getCompilationPhase(InputType, i);
577
578 // We are done if this step is past what the user requested.
579 if (Phase > FinalPhase)
580 break;
581
582 // Queue linker inputs.
583 if (Phase == phases::Link) {
584 assert(i + 1 == NumSteps && "linking must be final compilation step.");
585 LinkerInputs.push_back(Current);
586 Current = 0;
587 break;
588 }
589
Daniel Dunbar337a6272009-03-24 20:17:30 +0000590 // Some types skip the assembler phase (e.g., llvm-bc), but we
591 // can't encode this in the steps because the intermediate type
592 // depends on arguments. Just special case here.
593 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
594 continue;
595
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000596 // Otherwise construct the appropriate action.
597 Current = ConstructPhaseAction(Args, Phase, Current);
598 if (Current->getType() == types::TY_Nothing)
599 break;
600 }
601
602 // If we ended with something, add to the output list.
603 if (Current)
604 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000605 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000606
607 // Add a link action if necessary.
608 if (!LinkerInputs.empty())
609 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
610}
611
612Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
613 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000614 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000615 // Build the appropriate action.
616 switch (Phase) {
617 case phases::Link: assert(0 && "link action invalid here.");
618 case phases::Preprocess: {
619 types::ID OutputTy = types::getPreprocessedType(Input->getType());
620 assert(OutputTy != types::TY_INVALID &&
621 "Cannot preprocess this input type!");
622 return new PreprocessJobAction(Input, OutputTy);
623 }
624 case phases::Precompile:
625 return new PrecompileJobAction(Input, types::TY_PCH);
626 case phases::Compile: {
627 if (Args.hasArg(options::OPT_fsyntax_only)) {
628 return new CompileJobAction(Input, types::TY_Nothing);
629 } else if (Args.hasArg(options::OPT__analyze)) {
630 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000631 } else if (Args.hasArg(options::OPT_emit_llvm) ||
632 Args.hasArg(options::OPT_flto) ||
633 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000634 types::ID Output =
635 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
636 return new CompileJobAction(Input, Output);
637 } else {
638 return new CompileJobAction(Input, types::TY_PP_Asm);
639 }
640 }
641 case phases::Assemble:
642 return new AssembleJobAction(Input, types::TY_Object);
643 }
644
645 assert(0 && "invalid phase in ConstructPhaseAction");
646 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000647}
648
Daniel Dunbar21549232009-03-18 02:55:38 +0000649void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000650 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000651 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
652 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000653
654 // FIXME: Pipes are forcibly disabled until we support executing
655 // them.
656 if (!CCCPrintBindings)
657 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000658
659 // -save-temps inhibits pipes.
660 if (SaveTemps && UsePipes) {
661 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
662 UsePipes = true;
663 }
664
665 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
666
667 // It is an error to provide a -o option if we are making multiple
668 // output files.
669 if (FinalOutput) {
670 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000671 for (ActionList::const_iterator it = C.getActions().begin(),
672 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000673 if ((*it)->getType() != types::TY_Nothing)
674 ++NumOutputs;
675
676 if (NumOutputs > 1) {
677 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
678 FinalOutput = 0;
679 }
680 }
681
Daniel Dunbar21549232009-03-18 02:55:38 +0000682 for (ActionList::const_iterator it = C.getActions().begin(),
683 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000684 Action *A = *it;
685
686 // If we are linking an image for multiple archs then the linker
687 // wants -arch_multiple and -final_output <final image
688 // name>. Unfortunately, this doesn't fit in cleanly because we
689 // have to pass this information down.
690 //
691 // FIXME: This is a hack; find a cleaner way to integrate this
692 // into the process.
693 const char *LinkingOutput = 0;
694 if (isa<LinkJobAction>(A)) {
695 if (FinalOutput)
696 LinkingOutput = FinalOutput->getValue(C.getArgs());
697 else
698 LinkingOutput = DefaultImageName.c_str();
699 }
700
701 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000702 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000703 /*CanAcceptPipe*/ true,
704 /*AtTopLevel*/ true,
705 /*LinkingOutput*/ LinkingOutput,
706 II);
707 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000708
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000709 // If there were errors, don't warn about any unused arguments.
710 if (Diags.getNumErrors())
711 return;
712
Daniel Dunbar586dc232009-03-16 06:42:30 +0000713 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
714 it != ie; ++it) {
715 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000716
Daniel Dunbar586dc232009-03-16 06:42:30 +0000717 // FIXME: It would be nice to be able to send the argument to the
718 // Diagnostic, so that extra values, position, and so on could be
719 // printed.
720 if (!A->isClaimed())
721 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000722 << A->getAsString(C.getArgs());
Daniel Dunbar586dc232009-03-16 06:42:30 +0000723 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000724}
725
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000726void Driver::BuildJobsForAction(Compilation &C,
727 const Action *A,
728 const ToolChain *TC,
729 bool CanAcceptPipe,
730 bool AtTopLevel,
731 const char *LinkingOutput,
732 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000733 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000734
735 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
736 // FIXME: Pipes are forcibly disabled until we support executing
737 // them.
738 if (!CCCPrintBindings)
739 UsePipes = false;
740
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000741 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +0000742 // FIXME: It would be nice to not claim this here; maybe the old
743 // scheme of just using Args was better?
744 const Arg &Input = IA->getInputArg();
745 Input.claim();
746 if (isa<PositionalArg>(Input)) {
747 const char *Name = Input.getValue(C.getArgs());
748 Result = InputInfo(Name, A->getType(), Name);
749 } else
750 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000751 return;
752 }
753
754 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
755 const char *ArchName = BAA->getArchName();
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000756 if (!ArchName)
757 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000758 BuildJobsForAction(C,
759 *BAA->begin(),
760 Host->getToolChain(C.getArgs(), ArchName),
761 CanAcceptPipe,
762 AtTopLevel,
763 LinkingOutput,
764 Result);
765 return;
766 }
767
768 const JobAction *JA = cast<JobAction>(A);
769 const Tool &T = TC->SelectTool(C, *JA);
770
771 // See if we should use an integrated preprocessor. We do so when we
772 // have exactly one input, since this is the only use case we care
773 // about (irrelevant since we don't support combine yet).
774 bool UseIntegratedCPP = false;
775 const ActionList *Inputs = &A->getInputs();
776 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
777 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
778 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
779 !C.getArgs().hasArg(options::OPT_save_temps) &&
780 T.hasIntegratedCPP()) {
781 UseIntegratedCPP = true;
782 Inputs = &(*Inputs)[0]->getInputs();
783 }
784 }
785
786 // Only use pipes when there is exactly one input.
787 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000788 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000789 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
790 it != ie; ++it) {
791 InputInfo II;
792 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
793 /*AtTopLevel*/false,
794 LinkingOutput,
795 II);
796 InputInfos.push_back(II);
797 }
798
799 // Determine if we should output to a pipe.
800 bool OutputToPipe = false;
801 if (CanAcceptPipe && T.canPipeOutput()) {
802 // Some actions default to writing to a pipe if they are the top
803 // level phase and there was no user override.
804 //
805 // FIXME: Is there a better way to handle this?
806 if (AtTopLevel) {
807 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
808 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000809 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000810 OutputToPipe = true;
811 }
812
813 // Figure out where to put the job (pipes).
814 Job *Dest = &C.getJobs();
815 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +0000816 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000817 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
818 Dest = &InputInfos[0].getPipe();
819 }
820
821 // Always use the first input as the base input.
822 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +0000823
824 // Determine the place to write output to (nothing, pipe, or
825 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +0000826 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000827 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000828 } else if (OutputToPipe) {
829 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000830 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
831 if (!PJ) {
832 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +0000833 // FIXME: Temporary hack so that -ccc-print-bindings work until
834 // we have pipe support. Please remove later.
835 if (!CCCPrintBindings)
836 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000837 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +0000838 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000839 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000840 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000841 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
842 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000843 }
844
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000845 if (CCCPrintBindings) {
846 llvm::errs() << "bind - \"" << T.getName() << "\", inputs: [";
847 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
848 llvm::errs() << InputInfos[i].getAsString();
849 if (i + 1 != e)
850 llvm::errs() << ", ";
851 }
852 llvm::errs() << "], output: " << Result.getAsString() << "\n";
853 } else {
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000854 const ArgList &TCArgs = C.getArgsForToolChain(TC);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000855 T.ConstructJob(C, *JA, *Dest, Result, InputInfos, TCArgs, LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000856 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000857}
858
Daniel Dunbar441d0602009-03-17 17:53:55 +0000859const char *Driver::GetNamedOutputPath(Compilation &C,
860 const JobAction &JA,
861 const char *BaseInput,
862 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000863 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +0000864 // Output to a user requested destination?
865 if (AtTopLevel) {
866 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
867 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
868 }
869
870 // Output to a temporary file?
871 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +0000872 std::string TmpName =
873 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
874 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +0000875 }
876
877 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +0000878 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +0000879
880 // Determine what the derived output name should be.
881 const char *NamedOutput;
882 if (JA.getType() == types::TY_Image) {
883 NamedOutput = DefaultImageName.c_str();
884 } else {
885 const char *Suffix = types::getTypeTempSuffix(JA.getType());
886 assert(Suffix && "All types used for output should have a suffix.");
887
888 std::string::size_type End = std::string::npos;
889 if (!types::appendSuffixForType(JA.getType()))
890 End = BaseName.rfind('.');
891 std::string Suffixed(BaseName.substr(0, End));
892 Suffixed += '.';
893 Suffixed += Suffix;
894 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
895 }
896
897 // As an annoying special case, PCH generation doesn't strip the
898 // pathname.
899 if (JA.getType() == types::TY_PCH) {
900 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +0000901 if (BasePath.isEmpty())
902 BasePath = NamedOutput;
903 else
904 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000905 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
906 } else {
907 return C.addResultFile(NamedOutput);
908 }
909}
910
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000911llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000912 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000913 const ToolChain::path_list &List = TC.getFilePaths();
914 for (ToolChain::path_list::const_iterator
915 it = List.begin(), ie = List.end(); it != ie; ++it) {
916 llvm::sys::Path P(*it);
917 P.appendComponent(Name);
918 if (P.exists())
919 return P;
920 }
921
Daniel Dunbarcb881672009-03-13 00:51:18 +0000922 return llvm::sys::Path(Name);
923}
924
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000925llvm::sys::Path Driver::GetProgramPath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000926 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000927 const ToolChain::path_list &List = TC.getProgramPaths();
928 for (ToolChain::path_list::const_iterator
929 it = List.begin(), ie = List.end(); it != ie; ++it) {
930 llvm::sys::Path P(*it);
931 P.appendComponent(Name);
932 if (P.exists())
933 return P;
934 }
935
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000936 // If all else failed, search the path.
937 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +0000938 if (!P.empty())
939 return P;
940
Daniel Dunbarcb881672009-03-13 00:51:18 +0000941 return llvm::sys::Path(Name);
942}
943
Daniel Dunbar214399e2009-03-18 19:34:39 +0000944std::string Driver::GetTemporaryPath(const char *Suffix) const {
945 // FIXME: This is lame; sys::Path should provide this function (in
946 // particular, it should know how to find the temporary files dir).
947 std::string Error;
948 llvm::sys::Path P("/tmp/cc");
949 if (P.makeUnique(false, &Error)) {
950 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
951 return "";
952 }
953
Daniel Dunbar84603bc2009-03-18 23:08:52 +0000954 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
955 // PR3837.
956 P.eraseFromDisk(false, 0);
957
Daniel Dunbar214399e2009-03-18 19:34:39 +0000958 P.appendSuffix(Suffix);
959 return P.toString();
960}
961
Daniel Dunbare5049522009-03-17 20:45:45 +0000962const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000963 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000964 // Dice into arch, platform, and OS. This matches
965 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
966 // and missing fields are left empty.
967 std::string Arch, Platform, OS;
968
969 if (const char *ArchEnd = strchr(Triple, '-')) {
970 Arch = std::string(Triple, ArchEnd);
971
972 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
973 Platform = std::string(ArchEnd+1, PlatformEnd);
974 OS = PlatformEnd+1;
975 } else
976 Platform = ArchEnd+1;
977 } else
978 Arch = Triple;
979
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000980 // Normalize Arch a bit.
981 //
982 // FIXME: This is very incomplete.
983 if (Arch == "i686")
984 Arch = "i386";
985 else if (Arch == "amd64")
986 Arch = "x86_64";
Daniel Dunbarc811b6c2009-03-18 04:41:46 +0000987 else if (Arch == "powerpc" || Arch == "Power Macintosh")
988 Arch = "ppc";
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000989
Daniel Dunbara88162c2009-03-13 12:23:29 +0000990 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbare5049522009-03-17 20:45:45 +0000991 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
992 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000993
Daniel Dunbare5049522009-03-17 20:45:45 +0000994 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
995 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000996}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +0000997
998bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
999 const std::string &ArchName) const {
1000 // Check if user requested no clang, or clang doesn't understand
1001 // this type (we only handle single inputs for now).
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001002 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001003 !types::isAcceptedByClang((*JA.begin())->getType()))
1004 return false;
1005
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001006 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001007 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001008 if (!CCCUseClangCPP) {
1009 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001010 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001011 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001012 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1013 return false;
1014
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001015 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001016 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1017 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001018 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001019 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001020
1021 // Finally, don't use clang if this isn't one of the user specified
1022 // archs to build.
Daniel Dunbar6256d362009-03-24 19:14:56 +00001023 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1024 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001025 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001026 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001027
1028 return true;
1029}