blob: a51e4bdd9a7632e96cc3fb972e7286b777cc7791 [file] [log] [blame]
Daniel Dunbar3ede8d02009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000011
Daniel Dunbar53ec5522009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000024
Daniel Dunbar13689542009-03-13 20:33:35 +000025#include "llvm/ADT/StringSet.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000026#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000028#include "llvm/System/Path.h"
Daniel Dunbar632f50e2009-03-18 21:34:08 +000029#include "llvm/System/Program.h"
Daniel Dunbarba102132009-03-13 12:19:02 +000030
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000031#include "InputInfo.h"
32
Daniel Dunbarba102132009-03-13 12:19:02 +000033#include <map>
34
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000035using namespace clang::driver;
Chris Lattner92b36992009-03-26 05:56:24 +000036using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000037
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000038Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000039 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000040 const char *_DefaultImageName,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000041 Diagnostic &_Diags)
42 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000043 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000044 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000045 Host(0),
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +000046 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +000047 CCCUseClang(true), CCCUseClangCXX(false), CCCUseClangCPP(true),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000048 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000049{
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +000050 // Only use clang on i386 and x86_64 by default.
51 CCCClangArchs.insert("i386");
52 CCCClangArchs.insert("x86_64");
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000053}
54
55Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000056 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000057 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000058}
59
Daniel Dunbarf3cad362009-03-25 04:13:45 +000060InputArgList *Driver::ParseArgStrings(const char **ArgBegin,
61 const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000062 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbarf3cad362009-03-25 04:13:45 +000063 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
Daniel Dunbar06482622009-03-05 06:38:47 +000064
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000065 // FIXME: Handle '@' args (or at least error on them).
66
Daniel Dunbar06482622009-03-05 06:38:47 +000067 unsigned Index = 0, End = ArgEnd - ArgBegin;
68 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000069 // gcc's handling of empty arguments doesn't make
70 // sense, but this is not a common use case. :)
71 //
72 // We just ignore them here (note that other things may
73 // still take them as arguments).
74 if (Args->getArgString(Index)[0] == '\0') {
75 ++Index;
76 continue;
77 }
78
Daniel Dunbar06482622009-03-05 06:38:47 +000079 unsigned Prev = Index;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000080 Arg *A = getOpts().ParseOneArg(*Args, Index);
81 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar53ec5522009-03-12 07:58:46 +000082
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000083 // Check for missing argument error.
84 if (!A) {
85 assert(Index >= End && "Unexpected parser error.");
86 Diag(clang::diag::err_drv_missing_argument)
87 << Args->getArgString(Prev)
88 << (Index - Prev - 1);
89 break;
Daniel Dunbar53ec5522009-03-12 07:58:46 +000090 }
Daniel Dunbar06482622009-03-05 06:38:47 +000091
Daniel Dunbarb0c4df52009-03-22 23:26:43 +000092 if (A->getOption().isUnsupported()) {
93 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
94 continue;
95 }
96 Args->append(A);
Daniel Dunbar06482622009-03-05 06:38:47 +000097 }
98
99 return Args;
100}
101
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000102Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000103 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
104
Daniel Dunbarcb881672009-03-13 00:51:18 +0000105 // FIXME: Handle environment options which effect driver behavior,
106 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
107 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
108
109 // FIXME: What are we going to do with -V and -b?
110
111 // FIXME: Handle CCC_ADD_ARGS.
112
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000113 // FIXME: This stuff needs to go into the Compilation, not the
114 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000115 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000116
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000117 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000118 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000119
120 // Read -ccc args.
121 //
122 // FIXME: We need to figure out where this behavior should
123 // live. Most of it should be outside in the client; the parts that
124 // aren't should have proper options, either by introducing new ones
125 // or by overloading gcc ones like -V or -b.
126 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
127 const char *Opt = *Start + 5;
128
129 if (!strcmp(Opt, "print-options")) {
130 CCCPrintOptions = true;
131 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000132 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000133 } else if (!strcmp(Opt, "print-bindings")) {
134 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000135 } else if (!strcmp(Opt, "cxx")) {
136 CCCIsCXX = true;
137 } else if (!strcmp(Opt, "echo")) {
138 CCCEcho = true;
139
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000140 } else if (!strcmp(Opt, "clang-cxx")) {
141 CCCUseClangCXX = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000142 } else if (!strcmp(Opt, "no-clang")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000143 CCCUseClang = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000144 } else if (!strcmp(Opt, "no-clang-cpp")) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000145 CCCUseClangCPP = false;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000146 } else if (!strcmp(Opt, "clang-archs")) {
147 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
148 const char *Cur = *++Start;
149
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000150 CCCClangArchs.clear();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000151 for (;;) {
152 const char *Next = strchr(Cur, ',');
153
154 if (Next) {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000155 if (Cur != Next)
156 CCCClangArchs.insert(std::string(Cur, Next));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000157 Cur = Next + 1;
158 } else {
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +0000159 if (*Cur != '\0')
160 CCCClangArchs.insert(std::string(Cur));
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000161 break;
162 }
163 }
164
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000165 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000166 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000167 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000168
169 } else {
170 // FIXME: Error handling.
171 llvm::errs() << "invalid option: " << *Start << "\n";
172 exit(1);
173 }
174 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000175
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000176 InputArgList *Args = ParseArgStrings(Start, End);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000177
Daniel Dunbare5049522009-03-17 20:45:45 +0000178 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000179
Daniel Dunbar586dc232009-03-16 06:42:30 +0000180 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000181 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-03-18 02:55:38 +0000182
183 // FIXME: This behavior shouldn't be here.
184 if (CCCPrintOptions) {
185 PrintOptions(C->getArgs());
186 return C;
187 }
188
189 if (!HandleImmediateArgs(*C))
190 return C;
191
192 // Construct the list of abstract actions to perform for this
193 // compilation. We avoid passing a Compilation here simply to
194 // enforce the abstraction that pipelining is not host or toolchain
195 // dependent (other than the driver driver test).
196 if (Host->useDriverDriver())
197 BuildUniversalActions(C->getArgs(), C->getActions());
198 else
199 BuildActions(C->getArgs(), C->getActions());
200
201 if (CCCPrintActions) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000202 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000203 return C;
204 }
205
206 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000207
208 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000209}
210
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000211void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000212 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000213 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000214 it != ie; ++it, ++i) {
215 Arg *A = *it;
216 llvm::errs() << "Option " << i << " - "
217 << "Name: \"" << A->getOption().getName() << "\", "
218 << "Values: {";
219 for (unsigned j = 0; j < A->getNumValues(); ++j) {
220 if (j)
221 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000222 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000223 }
224 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000225 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000226}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000227
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000228void Driver::PrintVersion(const Compilation &C) const {
Mike Stump5d023c32009-03-18 14:00:02 +0000229 static char buf[] = "$URL$";
230 char *zap = strstr(buf, "/lib/Driver");
231 if (zap)
232 *zap = 0;
233 zap = strstr(buf, "/clang/tools/clang");
234 if (zap)
235 *zap = 0;
Mike Stumpe70295b2009-03-18 15:19:35 +0000236 const char *vers = buf+6;
Mike Stump8944c382009-03-18 18:45:55 +0000237 // FIXME: Add cmake support and remove #ifdef
238#ifdef SVN_REVISION
239 const char *revision = SVN_REVISION;
240#else
241 const char *revision = "";
242#endif
Daniel Dunbarcb881672009-03-13 00:51:18 +0000243 // FIXME: The following handlers should use a callback mechanism, we
244 // don't know what the client would like to do.
Mike Stumpd227fe72009-03-18 21:19:11 +0000245 llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000246
247 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000248 llvm::errs() << "Target: " << TC.getTripleString() << '\n';
Daniel Dunbarcb881672009-03-13 00:51:18 +0000249}
250
Daniel Dunbar21549232009-03-18 02:55:38 +0000251bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-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 Dunbar21549232009-03-18 02:55:38 +0000255 if (C.getArgs().hasArg(options::OPT_v) ||
256 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000257 PrintVersion(C);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000258 SuppressMissingInputWarning = true;
259 }
260
Daniel Dunbar21549232009-03-18 02:55:38 +0000261 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-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 Dunbarcb881672009-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 Dunbar21549232009-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 Dunbarcb881672009-03-13 00:51:18 +0000286 return false;
287 }
288
Daniel Dunbar21549232009-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 Dunbarcb881672009-03-13 00:51:18 +0000292 return false;
293 }
294
Daniel Dunbar21549232009-03-18 02:55:38 +0000295 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar08c65e02009-03-27 14:26:33 +0000296 llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000297 return false;
298 }
299
300 return true;
301}
302
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000303static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-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 Dunbar10ffa9a2009-03-18 03:13:20 +0000314 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000315 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000316 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
317 C.getDefaultToolChain().getArchName()) << '"'
318 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000319 } else {
320 os << "{";
321 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000322 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-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 Dunbarb269c322009-03-13 17:20:20 +0000332 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000333 << types::getTypeName(A->getType()) << "\n";
334
335 return Id;
336}
337
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000338void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000339 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-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 Dunbar53ec5522009-03-12 07:58:46 +0000343}
344
Daniel Dunbar21549232009-03-18 02:55:38 +0000345void Driver::BuildUniversalActions(const ArgList &Args,
346 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000347 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-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 Dunbar2fe63e62009-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 Dunbar13689542009-03-13 20:33:35 +0000357 const char *Name = A->getValue(Args);
358
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000359 // FIXME: We need to handle canonicalization of the specified
360 // arch?
361
Daniel Dunbar75877192009-03-19 07:55:12 +0000362 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000363 if (ArchNames.insert(Name))
364 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000365 }
366 }
367
Daniel Dunbar10ffa9a2009-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 Dunbar2fe63e62009-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.
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000381 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
382 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000383 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000384 }
385
386 ActionList SingleActions;
387 BuildActions(Args, SingleActions);
388
389 // Add in arch binding and lipo (if necessary) for every top level
390 // action.
391 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
392 Action *Act = SingleActions[i];
393
394 // Make sure we can lipo this kind of output. If not (and it is an
395 // actual output) then we disallow, since we can't create an
396 // output file with the right name without overwriting it. We
397 // could remove this oddity by just changing the output names to
398 // include the arch, which would also fix
399 // -save-temps. Compatibility wins for now.
400
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000401 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000402 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
403 << types::getTypeName(Act->getType());
404
405 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000406 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000407 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000408
409 // Lipo if necessary, We do it this way because we need to set the
410 // arch flag so that -Xarch_ gets overwritten.
411 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
412 Actions.append(Inputs.begin(), Inputs.end());
413 else
414 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
415 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000416}
417
Daniel Dunbar21549232009-03-18 02:55:38 +0000418void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000419 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000420 // Start by constructing the list of inputs and their types.
421
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000422 // Track the current user specified (-x) input. We also explicitly
423 // track the argument used to set the type; we only want to claim
424 // the type when we actually use it, so we warn about unused -x
425 // arguments.
426 types::ID InputType = types::TY_Nothing;
427 Arg *InputTypeArg = 0;
428
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000429 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
430 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
431 it != ie; ++it) {
432 Arg *A = *it;
433
434 if (isa<InputOption>(A->getOption())) {
435 const char *Value = A->getValue(Args);
436 types::ID Ty = types::TY_INVALID;
437
438 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000439 if (InputType == types::TY_Nothing) {
440 // If there was an explicit arg for this, claim it.
441 if (InputTypeArg)
442 InputTypeArg->claim();
443
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000444 // stdin must be handled specially.
445 if (memcmp(Value, "-", 2) == 0) {
446 // If running with -E, treat as a C input (this changes the
447 // builtin macros, for example). This may be overridden by
448 // -ObjC below.
449 //
450 // Otherwise emit an error but still use a valid type to
451 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000452 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000453 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000454 Ty = types::TY_C;
455 } else {
456 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000457 // if not found. We use a host hook here because Darwin at
458 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000459 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000460 Ty = Host->lookupTypeForExtension(Ext + 1);
461
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000462 if (Ty == types::TY_INVALID)
463 Ty = types::TY_Object;
464 }
465
466 // -ObjC and -ObjC++ override the default language, but only
467 // -for "source files". We just treat everything that isn't a
468 // -linker input as a source file.
469 //
470 // FIXME: Clean this up if we move the phase sequence into the
471 // type.
472 if (Ty != types::TY_Object) {
473 if (Args.hasArg(options::OPT_ObjC))
474 Ty = types::TY_ObjC;
475 else if (Args.hasArg(options::OPT_ObjCXX))
476 Ty = types::TY_ObjCXX;
477 }
478 } else {
479 assert(InputTypeArg && "InputType set w/o InputTypeArg");
480 InputTypeArg->claim();
481 Ty = InputType;
482 }
483
484 // Check that the file exists. It isn't clear this is worth
485 // doing, since the tool presumably does this anyway, and this
486 // just adds an extra stat to the equation, but this is gcc
487 // compatible.
488 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000489 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000490 else
491 Inputs.push_back(std::make_pair(Ty, A));
492
493 } else if (A->getOption().isLinkerInput()) {
494 // Just treat as object type, we could make a special type for
495 // this if necessary.
496 Inputs.push_back(std::make_pair(types::TY_Object, A));
497
498 } else if (A->getOption().getId() == options::OPT_x) {
499 InputTypeArg = A;
500 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
501
502 // Follow gcc behavior and treat as linker input for invalid -x
503 // options. Its not clear why we shouldn't just revert to
504 // unknown; but this isn't very important, we might as well be
505 // bug comatible.
506 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000507 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000508 InputType = types::TY_Object;
509 }
510 }
511 }
512
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000513 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000514 Diag(clang::diag::err_drv_no_input_files);
515 return;
516 }
517
518 // Determine which compilation mode we are in. We look for options
519 // which affect the phase, starting with the earliest phases, and
520 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000521 Arg *FinalPhaseArg = 0;
522 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000523
524 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000525 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
526 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
527 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
528 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000529
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000530 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
531 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
532 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000533 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
534 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000535
536 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000537 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
538 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000539
540 // Otherwise do everything.
541 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000542 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000543
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000544 // Reject -Z* at the top level, these options should never have been
545 // exposed by gcc.
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000546 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000547 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000548
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000549 // Construct the actions to perform.
550 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000551 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000552 types::ID InputType = Inputs[i].first;
553 const Arg *InputArg = Inputs[i].second;
554
555 unsigned NumSteps = types::getNumCompilationPhases(InputType);
556 assert(NumSteps && "Invalid number of steps!");
557
558 // If the first step comes after the final phase we are doing as
559 // part of this compilation, warn the user about it.
560 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
561 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000562 // Claim here to avoid the more general unused warning.
563 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000564 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000565 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000566 << getPhaseName(InitialPhase)
567 << FinalPhaseArg->getOption().getName();
568 continue;
569 }
570
571 // Build the pipeline for this file.
572 Action *Current = new InputAction(*InputArg, InputType);
573 for (unsigned i = 0; i != NumSteps; ++i) {
574 phases::ID Phase = types::getCompilationPhase(InputType, i);
575
576 // We are done if this step is past what the user requested.
577 if (Phase > FinalPhase)
578 break;
579
580 // Queue linker inputs.
581 if (Phase == phases::Link) {
582 assert(i + 1 == NumSteps && "linking must be final compilation step.");
583 LinkerInputs.push_back(Current);
584 Current = 0;
585 break;
586 }
587
Daniel Dunbar337a6272009-03-24 20:17:30 +0000588 // Some types skip the assembler phase (e.g., llvm-bc), but we
589 // can't encode this in the steps because the intermediate type
590 // depends on arguments. Just special case here.
591 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
592 continue;
593
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000594 // Otherwise construct the appropriate action.
595 Current = ConstructPhaseAction(Args, Phase, Current);
596 if (Current->getType() == types::TY_Nothing)
597 break;
598 }
599
600 // If we ended with something, add to the output list.
601 if (Current)
602 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000603 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000604
605 // Add a link action if necessary.
606 if (!LinkerInputs.empty())
607 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
608}
609
610Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
611 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000612 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000613 // Build the appropriate action.
614 switch (Phase) {
615 case phases::Link: assert(0 && "link action invalid here.");
616 case phases::Preprocess: {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000617 types::ID OutputTy;
618 // -{M, MM} alter the output type.
619 if (Args.hasArg(options::OPT_M) || Args.hasArg(options::OPT_MM)) {
620 OutputTy = types::TY_Dependencies;
621 } else {
622 OutputTy = types::getPreprocessedType(Input->getType());
623 assert(OutputTy != types::TY_INVALID &&
624 "Cannot preprocess this input type!");
625 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000626 return new PreprocessJobAction(Input, OutputTy);
627 }
628 case phases::Precompile:
629 return new PrecompileJobAction(Input, types::TY_PCH);
630 case phases::Compile: {
631 if (Args.hasArg(options::OPT_fsyntax_only)) {
632 return new CompileJobAction(Input, types::TY_Nothing);
633 } else if (Args.hasArg(options::OPT__analyze)) {
634 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000635 } else if (Args.hasArg(options::OPT_emit_llvm) ||
636 Args.hasArg(options::OPT_flto) ||
637 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000638 types::ID Output =
639 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
640 return new CompileJobAction(Input, Output);
641 } else {
642 return new CompileJobAction(Input, types::TY_PP_Asm);
643 }
644 }
645 case phases::Assemble:
646 return new AssembleJobAction(Input, types::TY_Object);
647 }
648
649 assert(0 && "invalid phase in ConstructPhaseAction");
650 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000651}
652
Daniel Dunbar21549232009-03-18 02:55:38 +0000653void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000654 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000655 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
656 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000657
658 // FIXME: Pipes are forcibly disabled until we support executing
659 // them.
660 if (!CCCPrintBindings)
661 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000662
663 // -save-temps inhibits pipes.
664 if (SaveTemps && UsePipes) {
665 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
666 UsePipes = true;
667 }
668
669 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
670
671 // It is an error to provide a -o option if we are making multiple
672 // output files.
673 if (FinalOutput) {
674 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000675 for (ActionList::const_iterator it = C.getActions().begin(),
676 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000677 if ((*it)->getType() != types::TY_Nothing)
678 ++NumOutputs;
679
680 if (NumOutputs > 1) {
681 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
682 FinalOutput = 0;
683 }
684 }
685
Daniel Dunbar21549232009-03-18 02:55:38 +0000686 for (ActionList::const_iterator it = C.getActions().begin(),
687 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000688 Action *A = *it;
689
690 // If we are linking an image for multiple archs then the linker
691 // wants -arch_multiple and -final_output <final image
692 // name>. Unfortunately, this doesn't fit in cleanly because we
693 // have to pass this information down.
694 //
695 // FIXME: This is a hack; find a cleaner way to integrate this
696 // into the process.
697 const char *LinkingOutput = 0;
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000698 if (isa<LipoJobAction>(A)) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000699 if (FinalOutput)
700 LinkingOutput = FinalOutput->getValue(C.getArgs());
701 else
702 LinkingOutput = DefaultImageName.c_str();
703 }
704
705 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000706 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000707 /*CanAcceptPipe*/ true,
708 /*AtTopLevel*/ true,
709 /*LinkingOutput*/ LinkingOutput,
710 II);
711 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000712
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000713 // If there were errors, don't warn about any unused arguments.
714 if (Diags.getNumErrors())
715 return;
716
Daniel Dunbara2094e72009-03-29 22:24:54 +0000717 // Claim -### here.
718 (void) C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
719
Daniel Dunbar586dc232009-03-16 06:42:30 +0000720 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
721 it != ie; ++it) {
722 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000723
Daniel Dunbar586dc232009-03-16 06:42:30 +0000724 // FIXME: It would be nice to be able to send the argument to the
725 // Diagnostic, so that extra values, position, and so on could be
726 // printed.
727 if (!A->isClaimed())
728 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000729 << A->getAsString(C.getArgs());
Daniel Dunbar586dc232009-03-16 06:42:30 +0000730 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000731}
732
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000733void Driver::BuildJobsForAction(Compilation &C,
734 const Action *A,
735 const ToolChain *TC,
736 bool CanAcceptPipe,
737 bool AtTopLevel,
738 const char *LinkingOutput,
739 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000740 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000741
742 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
743 // FIXME: Pipes are forcibly disabled until we support executing
744 // them.
745 if (!CCCPrintBindings)
746 UsePipes = false;
747
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000748 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +0000749 // FIXME: It would be nice to not claim this here; maybe the old
750 // scheme of just using Args was better?
751 const Arg &Input = IA->getInputArg();
752 Input.claim();
753 if (isa<PositionalArg>(Input)) {
754 const char *Name = Input.getValue(C.getArgs());
755 Result = InputInfo(Name, A->getType(), Name);
756 } else
757 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000758 return;
759 }
760
761 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
762 const char *ArchName = BAA->getArchName();
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000763 if (!ArchName)
764 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000765 BuildJobsForAction(C,
766 *BAA->begin(),
767 Host->getToolChain(C.getArgs(), ArchName),
768 CanAcceptPipe,
769 AtTopLevel,
770 LinkingOutput,
771 Result);
772 return;
773 }
774
775 const JobAction *JA = cast<JobAction>(A);
776 const Tool &T = TC->SelectTool(C, *JA);
777
778 // See if we should use an integrated preprocessor. We do so when we
779 // have exactly one input, since this is the only use case we care
780 // about (irrelevant since we don't support combine yet).
781 bool UseIntegratedCPP = false;
782 const ActionList *Inputs = &A->getInputs();
783 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
784 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
785 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
786 !C.getArgs().hasArg(options::OPT_save_temps) &&
787 T.hasIntegratedCPP()) {
788 UseIntegratedCPP = true;
789 Inputs = &(*Inputs)[0]->getInputs();
790 }
791 }
792
793 // Only use pipes when there is exactly one input.
794 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000795 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000796 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
797 it != ie; ++it) {
798 InputInfo II;
799 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
800 /*AtTopLevel*/false,
801 LinkingOutput,
802 II);
803 InputInfos.push_back(II);
804 }
805
806 // Determine if we should output to a pipe.
807 bool OutputToPipe = false;
808 if (CanAcceptPipe && T.canPipeOutput()) {
809 // Some actions default to writing to a pipe if they are the top
810 // level phase and there was no user override.
811 //
812 // FIXME: Is there a better way to handle this?
813 if (AtTopLevel) {
814 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
815 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000816 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000817 OutputToPipe = true;
818 }
819
820 // Figure out where to put the job (pipes).
821 Job *Dest = &C.getJobs();
822 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +0000823 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000824 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
825 Dest = &InputInfos[0].getPipe();
826 }
827
828 // Always use the first input as the base input.
829 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +0000830
831 // Determine the place to write output to (nothing, pipe, or
832 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +0000833 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000834 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000835 } else if (OutputToPipe) {
836 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000837 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
838 if (!PJ) {
839 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +0000840 // FIXME: Temporary hack so that -ccc-print-bindings work until
841 // we have pipe support. Please remove later.
842 if (!CCCPrintBindings)
843 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000844 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +0000845 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000846 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000847 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000848 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
849 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000850 }
851
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000852 if (CCCPrintBindings) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000853 llvm::errs() << "# \"" << T.getToolChain().getTripleString() << '"'
854 << " - \"" << T.getName() << "\", inputs: [";
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000855 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
856 llvm::errs() << InputInfos[i].getAsString();
857 if (i + 1 != e)
858 llvm::errs() << ", ";
859 }
860 llvm::errs() << "], output: " << Result.getAsString() << "\n";
861 } else {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000862 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
863 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000864 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000865}
866
Daniel Dunbar441d0602009-03-17 17:53:55 +0000867const char *Driver::GetNamedOutputPath(Compilation &C,
868 const JobAction &JA,
869 const char *BaseInput,
870 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000871 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +0000872 // Output to a user requested destination?
873 if (AtTopLevel) {
874 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
875 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
876 }
877
878 // Output to a temporary file?
879 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +0000880 std::string TmpName =
881 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
882 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +0000883 }
884
885 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +0000886 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +0000887
888 // Determine what the derived output name should be.
889 const char *NamedOutput;
890 if (JA.getType() == types::TY_Image) {
891 NamedOutput = DefaultImageName.c_str();
892 } else {
893 const char *Suffix = types::getTypeTempSuffix(JA.getType());
894 assert(Suffix && "All types used for output should have a suffix.");
895
896 std::string::size_type End = std::string::npos;
897 if (!types::appendSuffixForType(JA.getType()))
898 End = BaseName.rfind('.');
899 std::string Suffixed(BaseName.substr(0, End));
900 Suffixed += '.';
901 Suffixed += Suffix;
902 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
903 }
904
905 // As an annoying special case, PCH generation doesn't strip the
906 // pathname.
907 if (JA.getType() == types::TY_PCH) {
908 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +0000909 if (BasePath.isEmpty())
910 BasePath = NamedOutput;
911 else
912 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000913 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
914 } else {
915 return C.addResultFile(NamedOutput);
916 }
917}
918
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000919llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000920 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000921 const ToolChain::path_list &List = TC.getFilePaths();
922 for (ToolChain::path_list::const_iterator
923 it = List.begin(), ie = List.end(); it != ie; ++it) {
924 llvm::sys::Path P(*it);
925 P.appendComponent(Name);
926 if (P.exists())
927 return P;
928 }
929
Daniel Dunbarcb881672009-03-13 00:51:18 +0000930 return llvm::sys::Path(Name);
931}
932
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000933llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump950bedd2009-03-27 00:40:20 +0000934 const ToolChain &TC,
935 bool WantFile) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000936 const ToolChain::path_list &List = TC.getProgramPaths();
937 for (ToolChain::path_list::const_iterator
938 it = List.begin(), ie = List.end(); it != ie; ++it) {
939 llvm::sys::Path P(*it);
940 P.appendComponent(Name);
Mike Stump950bedd2009-03-27 00:40:20 +0000941 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000942 return P;
943 }
944
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000945 // If all else failed, search the path.
946 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +0000947 if (!P.empty())
948 return P;
949
Daniel Dunbarcb881672009-03-13 00:51:18 +0000950 return llvm::sys::Path(Name);
951}
952
Daniel Dunbar214399e2009-03-18 19:34:39 +0000953std::string Driver::GetTemporaryPath(const char *Suffix) const {
954 // FIXME: This is lame; sys::Path should provide this function (in
955 // particular, it should know how to find the temporary files dir).
956 std::string Error;
957 llvm::sys::Path P("/tmp/cc");
958 if (P.makeUnique(false, &Error)) {
959 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
960 return "";
961 }
962
Daniel Dunbar84603bc2009-03-18 23:08:52 +0000963 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
964 // PR3837.
965 P.eraseFromDisk(false, 0);
966
Daniel Dunbar214399e2009-03-18 19:34:39 +0000967 P.appendSuffix(Suffix);
968 return P.toString();
969}
970
Daniel Dunbare5049522009-03-17 20:45:45 +0000971const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000972 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000973 // Dice into arch, platform, and OS. This matches
974 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
975 // and missing fields are left empty.
976 std::string Arch, Platform, OS;
977
978 if (const char *ArchEnd = strchr(Triple, '-')) {
979 Arch = std::string(Triple, ArchEnd);
980
981 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
982 Platform = std::string(ArchEnd+1, PlatformEnd);
983 OS = PlatformEnd+1;
984 } else
985 Platform = ArchEnd+1;
986 } else
987 Arch = Triple;
988
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000989 // Normalize Arch a bit.
990 //
991 // FIXME: This is very incomplete.
992 if (Arch == "i686")
993 Arch = "i386";
994 else if (Arch == "amd64")
995 Arch = "x86_64";
Daniel Dunbarc811b6c2009-03-18 04:41:46 +0000996 else if (Arch == "powerpc" || Arch == "Power Macintosh")
997 Arch = "ppc";
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000998
Daniel Dunbara88162c2009-03-13 12:23:29 +0000999 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbare5049522009-03-17 20:45:45 +00001000 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
1001 OS.c_str());
Daniel Dunbar75358d22009-03-30 21:06:03 +00001002 if (memcmp(&OS[0], "freebsd", 7) == 0)
1003 return createFreeBSDHostInfo(*this, Arch.c_str(), Platform.c_str(),
1004 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001005
Daniel Dunbare5049522009-03-17 20:45:45 +00001006 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
1007 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +00001008}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001009
1010bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
1011 const std::string &ArchName) const {
1012 // Check if user requested no clang, or clang doesn't understand
1013 // this type (we only handle single inputs for now).
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001014 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001015 !types::isAcceptedByClang((*JA.begin())->getType()))
1016 return false;
1017
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001018 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001019 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001020 if (!CCCUseClangCPP) {
1021 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001022 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001023 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001024 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1025 return false;
1026
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001027 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001028 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1029 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001030 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001031 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001032
1033 // Finally, don't use clang if this isn't one of the user specified
1034 // archs to build.
Daniel Dunbar6256d362009-03-24 19:14:56 +00001035 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1036 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001037 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001038 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001039
1040 return true;
1041}
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001042
1043/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1044/// return the grouped values as integers. Numbers which are not
1045/// provided are set to 0.
1046///
1047/// \return True if the entire string was parsed (9.2), or all groups
1048/// were parsed (10.3.5extrastuff).
1049bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1050 unsigned &Minor, unsigned &Micro,
1051 bool &HadExtra) {
1052 HadExtra = false;
1053
1054 Major = Minor = Micro = 0;
1055 if (*Str == '\0')
1056 return true;
1057
1058 char *End;
1059 Major = (unsigned) strtol(Str, &End, 10);
1060 if (*Str != '\0' && *End == '\0')
1061 return true;
1062 if (*End != '.')
1063 return false;
1064
1065 Str = End+1;
1066 Minor = (unsigned) strtol(Str, &End, 10);
1067 if (*Str != '\0' && *End == '\0')
1068 return true;
1069 if (*End != '.')
1070 return false;
1071
1072 Str = End+1;
1073 Micro = (unsigned) strtol(Str, &End, 10);
1074 if (*Str != '\0' && *End == '\0')
1075 return true;
1076 if (Str == End)
1077 return false;
1078 HadExtra = true;
1079 return true;
1080}