blob: bb89207466286b78471f1537f23e1f31b4fa1c3f [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();
248 llvm::errs() << "Target: " << TC.getArchName() << '-'
249 << TC.getPlatform() << '-' << TC.getOS() << '\n';
Daniel Dunbarcb881672009-03-13 00:51:18 +0000250}
251
Daniel Dunbar21549232009-03-18 02:55:38 +0000252bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000253 // The order these options are handled in in gcc is all over the
254 // place, but we don't expect inconsistencies w.r.t. that to matter
255 // in practice.
Daniel Dunbar21549232009-03-18 02:55:38 +0000256 if (C.getArgs().hasArg(options::OPT_v) ||
257 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar70c8db12009-03-26 16:09:13 +0000258 PrintVersion(C);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000259 SuppressMissingInputWarning = true;
260 }
261
Daniel Dunbar21549232009-03-18 02:55:38 +0000262 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarca3459e2009-03-20 04:37:21 +0000263 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
264 llvm::outs() << "programs: =";
265 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
266 ie = TC.getProgramPaths().end(); it != ie; ++it) {
267 if (it != TC.getProgramPaths().begin())
268 llvm::outs() << ':';
269 llvm::outs() << *it;
270 }
271 llvm::outs() << "\n";
272 llvm::outs() << "libraries: =";
273 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
274 ie = TC.getFilePaths().end(); it != ie; ++it) {
275 if (it != TC.getFilePaths().begin())
276 llvm::outs() << ':';
277 llvm::outs() << *it;
278 }
279 llvm::outs() << "\n";
280 }
281
Daniel Dunbarcb881672009-03-13 00:51:18 +0000282 // FIXME: The following handlers should use a callback mechanism, we
283 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000284 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
285 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
286 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000287 return false;
288 }
289
Daniel Dunbar21549232009-03-18 02:55:38 +0000290 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
291 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
292 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000293 return false;
294 }
295
Daniel Dunbar21549232009-03-18 02:55:38 +0000296 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbar08c65e02009-03-27 14:26:33 +0000297 llvm::outs() << GetFilePath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000298 return false;
299 }
300
301 return true;
302}
303
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000304static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000305 Action *A,
306 std::map<Action*, unsigned> &Ids) {
307 if (Ids.count(A))
308 return Ids[A];
309
310 std::string str;
311 llvm::raw_string_ostream os(str);
312
313 os << Action::getClassName(A->getKind()) << ", ";
314 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000315 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000316 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000317 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
318 C.getDefaultToolChain().getArchName()) << '"'
319 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000320 } else {
321 os << "{";
322 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000323 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000324 ++it;
325 if (it != ie)
326 os << ", ";
327 }
328 os << "}";
329 }
330
331 unsigned Id = Ids.size();
332 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000333 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000334 << types::getTypeName(A->getType()) << "\n";
335
336 return Id;
337}
338
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000339void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000340 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000341 for (ActionList::const_iterator it = C.getActions().begin(),
342 ie = C.getActions().end(); it != ie; ++it)
343 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000344}
345
Daniel Dunbar21549232009-03-18 02:55:38 +0000346void Driver::BuildUniversalActions(const ArgList &Args,
347 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000348 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000349 // Collect the list of architectures. Duplicates are allowed, but
350 // should only be handled once (in the order seen).
351 llvm::StringSet<> ArchNames;
352 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000353 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
354 it != ie; ++it) {
355 Arg *A = *it;
356
357 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000358 const char *Name = A->getValue(Args);
359
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000360 // FIXME: We need to handle canonicalization of the specified
361 // arch?
362
Daniel Dunbar75877192009-03-19 07:55:12 +0000363 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000364 if (ArchNames.insert(Name))
365 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000366 }
367 }
368
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000369 // When there is no explicit arch for this platform, make sure we
370 // still bind the architecture (to the default) so that -Xarch_ is
371 // handled correctly.
372 if (!Archs.size())
373 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000374
375 // FIXME: We killed off some others but these aren't yet detected in
376 // a functional manner. If we added information to jobs about which
377 // "auxiliary" files they wrote then we could detect the conflict
378 // these cause downstream.
379 if (Archs.size() > 1) {
380 // No recovery needed, the point of this is just to prevent
381 // overwriting the same files.
382 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
383 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000384 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000385 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
386 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000387 << A->getAsString(Args);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000388 }
389
390 ActionList SingleActions;
391 BuildActions(Args, SingleActions);
392
393 // Add in arch binding and lipo (if necessary) for every top level
394 // action.
395 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
396 Action *Act = SingleActions[i];
397
398 // Make sure we can lipo this kind of output. If not (and it is an
399 // actual output) then we disallow, since we can't create an
400 // output file with the right name without overwriting it. We
401 // could remove this oddity by just changing the output names to
402 // include the arch, which would also fix
403 // -save-temps. Compatibility wins for now.
404
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000405 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000406 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
407 << types::getTypeName(Act->getType());
408
409 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000410 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000411 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000412
413 // Lipo if necessary, We do it this way because we need to set the
414 // arch flag so that -Xarch_ gets overwritten.
415 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
416 Actions.append(Inputs.begin(), Inputs.end());
417 else
418 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
419 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000420}
421
Daniel Dunbar21549232009-03-18 02:55:38 +0000422void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000423 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000424 // Start by constructing the list of inputs and their types.
425
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000426 // Track the current user specified (-x) input. We also explicitly
427 // track the argument used to set the type; we only want to claim
428 // the type when we actually use it, so we warn about unused -x
429 // arguments.
430 types::ID InputType = types::TY_Nothing;
431 Arg *InputTypeArg = 0;
432
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000433 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
434 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
435 it != ie; ++it) {
436 Arg *A = *it;
437
438 if (isa<InputOption>(A->getOption())) {
439 const char *Value = A->getValue(Args);
440 types::ID Ty = types::TY_INVALID;
441
442 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000443 if (InputType == types::TY_Nothing) {
444 // If there was an explicit arg for this, claim it.
445 if (InputTypeArg)
446 InputTypeArg->claim();
447
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000448 // stdin must be handled specially.
449 if (memcmp(Value, "-", 2) == 0) {
450 // If running with -E, treat as a C input (this changes the
451 // builtin macros, for example). This may be overridden by
452 // -ObjC below.
453 //
454 // Otherwise emit an error but still use a valid type to
455 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000456 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000457 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000458 Ty = types::TY_C;
459 } else {
460 // Otherwise lookup by extension, and fallback to ObjectType
Daniel Dunbare33bea42009-03-20 23:39:23 +0000461 // if not found. We use a host hook here because Darwin at
462 // least has its own idea of what .s is.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000463 if (const char *Ext = strrchr(Value, '.'))
Daniel Dunbare33bea42009-03-20 23:39:23 +0000464 Ty = Host->lookupTypeForExtension(Ext + 1);
465
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000466 if (Ty == types::TY_INVALID)
467 Ty = types::TY_Object;
468 }
469
470 // -ObjC and -ObjC++ override the default language, but only
471 // -for "source files". We just treat everything that isn't a
472 // -linker input as a source file.
473 //
474 // FIXME: Clean this up if we move the phase sequence into the
475 // type.
476 if (Ty != types::TY_Object) {
477 if (Args.hasArg(options::OPT_ObjC))
478 Ty = types::TY_ObjC;
479 else if (Args.hasArg(options::OPT_ObjCXX))
480 Ty = types::TY_ObjCXX;
481 }
482 } else {
483 assert(InputTypeArg && "InputType set w/o InputTypeArg");
484 InputTypeArg->claim();
485 Ty = InputType;
486 }
487
488 // Check that the file exists. It isn't clear this is worth
489 // doing, since the tool presumably does this anyway, and this
490 // just adds an extra stat to the equation, but this is gcc
491 // compatible.
492 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000493 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000494 else
495 Inputs.push_back(std::make_pair(Ty, A));
496
497 } else if (A->getOption().isLinkerInput()) {
498 // Just treat as object type, we could make a special type for
499 // this if necessary.
500 Inputs.push_back(std::make_pair(types::TY_Object, A));
501
502 } else if (A->getOption().getId() == options::OPT_x) {
503 InputTypeArg = A;
504 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
505
506 // Follow gcc behavior and treat as linker input for invalid -x
507 // options. Its not clear why we shouldn't just revert to
508 // unknown; but this isn't very important, we might as well be
509 // bug comatible.
510 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000511 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000512 InputType = types::TY_Object;
513 }
514 }
515 }
516
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000517 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000518 Diag(clang::diag::err_drv_no_input_files);
519 return;
520 }
521
522 // Determine which compilation mode we are in. We look for options
523 // which affect the phase, starting with the earliest phases, and
524 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000525 Arg *FinalPhaseArg = 0;
526 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000527
528 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000529 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
530 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
531 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
532 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000533
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000534 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
535 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
536 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000537 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
538 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000539
540 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000541 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
542 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000543
544 // Otherwise do everything.
545 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000546 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000547
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000548 // Reject -Z* at the top level, these options should never have been
549 // exposed by gcc.
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000550 if (Arg *A = Args.getLastArg(options::OPT_Z_Joined))
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000551 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000552
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000553 // Construct the actions to perform.
554 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000555 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000556 types::ID InputType = Inputs[i].first;
557 const Arg *InputArg = Inputs[i].second;
558
559 unsigned NumSteps = types::getNumCompilationPhases(InputType);
560 assert(NumSteps && "Invalid number of steps!");
561
562 // If the first step comes after the final phase we are doing as
563 // part of this compilation, warn the user about it.
564 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
565 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000566 // Claim here to avoid the more general unused warning.
567 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000568 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000569 << InputArg->getAsString(Args)
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000570 << getPhaseName(InitialPhase)
571 << FinalPhaseArg->getOption().getName();
572 continue;
573 }
574
575 // Build the pipeline for this file.
576 Action *Current = new InputAction(*InputArg, InputType);
577 for (unsigned i = 0; i != NumSteps; ++i) {
578 phases::ID Phase = types::getCompilationPhase(InputType, i);
579
580 // We are done if this step is past what the user requested.
581 if (Phase > FinalPhase)
582 break;
583
584 // Queue linker inputs.
585 if (Phase == phases::Link) {
586 assert(i + 1 == NumSteps && "linking must be final compilation step.");
587 LinkerInputs.push_back(Current);
588 Current = 0;
589 break;
590 }
591
Daniel Dunbar337a6272009-03-24 20:17:30 +0000592 // Some types skip the assembler phase (e.g., llvm-bc), but we
593 // can't encode this in the steps because the intermediate type
594 // depends on arguments. Just special case here.
595 if (Phase == phases::Assemble && Current->getType() != types::TY_PP_Asm)
596 continue;
597
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000598 // Otherwise construct the appropriate action.
599 Current = ConstructPhaseAction(Args, Phase, Current);
600 if (Current->getType() == types::TY_Nothing)
601 break;
602 }
603
604 // If we ended with something, add to the output list.
605 if (Current)
606 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000607 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000608
609 // Add a link action if necessary.
610 if (!LinkerInputs.empty())
611 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
612}
613
614Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
615 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000616 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000617 // Build the appropriate action.
618 switch (Phase) {
619 case phases::Link: assert(0 && "link action invalid here.");
620 case phases::Preprocess: {
621 types::ID OutputTy = types::getPreprocessedType(Input->getType());
622 assert(OutputTy != types::TY_INVALID &&
623 "Cannot preprocess this input type!");
624 return new PreprocessJobAction(Input, OutputTy);
625 }
626 case phases::Precompile:
627 return new PrecompileJobAction(Input, types::TY_PCH);
628 case phases::Compile: {
629 if (Args.hasArg(options::OPT_fsyntax_only)) {
630 return new CompileJobAction(Input, types::TY_Nothing);
631 } else if (Args.hasArg(options::OPT__analyze)) {
632 return new AnalyzeJobAction(Input, types::TY_Plist);
Daniel Dunbar337a6272009-03-24 20:17:30 +0000633 } else if (Args.hasArg(options::OPT_emit_llvm) ||
634 Args.hasArg(options::OPT_flto) ||
635 Args.hasArg(options::OPT_O4)) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000636 types::ID Output =
637 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
638 return new CompileJobAction(Input, Output);
639 } else {
640 return new CompileJobAction(Input, types::TY_PP_Asm);
641 }
642 }
643 case phases::Assemble:
644 return new AssembleJobAction(Input, types::TY_Object);
645 }
646
647 assert(0 && "invalid phase in ConstructPhaseAction");
648 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000649}
650
Daniel Dunbar21549232009-03-18 02:55:38 +0000651void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000652 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000653 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
654 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000655
656 // FIXME: Pipes are forcibly disabled until we support executing
657 // them.
658 if (!CCCPrintBindings)
659 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000660
661 // -save-temps inhibits pipes.
662 if (SaveTemps && UsePipes) {
663 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
664 UsePipes = true;
665 }
666
667 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
668
669 // It is an error to provide a -o option if we are making multiple
670 // output files.
671 if (FinalOutput) {
672 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000673 for (ActionList::const_iterator it = C.getActions().begin(),
674 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000675 if ((*it)->getType() != types::TY_Nothing)
676 ++NumOutputs;
677
678 if (NumOutputs > 1) {
679 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
680 FinalOutput = 0;
681 }
682 }
683
Daniel Dunbar21549232009-03-18 02:55:38 +0000684 for (ActionList::const_iterator it = C.getActions().begin(),
685 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000686 Action *A = *it;
687
688 // If we are linking an image for multiple archs then the linker
689 // wants -arch_multiple and -final_output <final image
690 // name>. Unfortunately, this doesn't fit in cleanly because we
691 // have to pass this information down.
692 //
693 // FIXME: This is a hack; find a cleaner way to integrate this
694 // into the process.
695 const char *LinkingOutput = 0;
Daniel Dunbard7b88c22009-03-26 16:12:09 +0000696 if (isa<LipoJobAction>(A)) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000697 if (FinalOutput)
698 LinkingOutput = FinalOutput->getValue(C.getArgs());
699 else
700 LinkingOutput = DefaultImageName.c_str();
701 }
702
703 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000704 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000705 /*CanAcceptPipe*/ true,
706 /*AtTopLevel*/ true,
707 /*LinkingOutput*/ LinkingOutput,
708 II);
709 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000710
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000711 // If there were errors, don't warn about any unused arguments.
712 if (Diags.getNumErrors())
713 return;
714
Daniel Dunbar586dc232009-03-16 06:42:30 +0000715 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
716 it != ie; ++it) {
717 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000718
Daniel Dunbar586dc232009-03-16 06:42:30 +0000719 // FIXME: It would be nice to be able to send the argument to the
720 // Diagnostic, so that extra values, position, and so on could be
721 // printed.
722 if (!A->isClaimed())
723 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar38dd3d52009-03-20 06:14:23 +0000724 << A->getAsString(C.getArgs());
Daniel Dunbar586dc232009-03-16 06:42:30 +0000725 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000726}
727
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000728void Driver::BuildJobsForAction(Compilation &C,
729 const Action *A,
730 const ToolChain *TC,
731 bool CanAcceptPipe,
732 bool AtTopLevel,
733 const char *LinkingOutput,
734 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000735 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000736
737 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
738 // FIXME: Pipes are forcibly disabled until we support executing
739 // them.
740 if (!CCCPrintBindings)
741 UsePipes = false;
742
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000743 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +0000744 // FIXME: It would be nice to not claim this here; maybe the old
745 // scheme of just using Args was better?
746 const Arg &Input = IA->getInputArg();
747 Input.claim();
748 if (isa<PositionalArg>(Input)) {
749 const char *Name = Input.getValue(C.getArgs());
750 Result = InputInfo(Name, A->getType(), Name);
751 } else
752 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000753 return;
754 }
755
756 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
757 const char *ArchName = BAA->getArchName();
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000758 if (!ArchName)
759 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000760 BuildJobsForAction(C,
761 *BAA->begin(),
762 Host->getToolChain(C.getArgs(), ArchName),
763 CanAcceptPipe,
764 AtTopLevel,
765 LinkingOutput,
766 Result);
767 return;
768 }
769
770 const JobAction *JA = cast<JobAction>(A);
771 const Tool &T = TC->SelectTool(C, *JA);
772
773 // See if we should use an integrated preprocessor. We do so when we
774 // have exactly one input, since this is the only use case we care
775 // about (irrelevant since we don't support combine yet).
776 bool UseIntegratedCPP = false;
777 const ActionList *Inputs = &A->getInputs();
778 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
779 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
780 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
781 !C.getArgs().hasArg(options::OPT_save_temps) &&
782 T.hasIntegratedCPP()) {
783 UseIntegratedCPP = true;
784 Inputs = &(*Inputs)[0]->getInputs();
785 }
786 }
787
788 // Only use pipes when there is exactly one input.
789 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000790 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000791 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
792 it != ie; ++it) {
793 InputInfo II;
794 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
795 /*AtTopLevel*/false,
796 LinkingOutput,
797 II);
798 InputInfos.push_back(II);
799 }
800
801 // Determine if we should output to a pipe.
802 bool OutputToPipe = false;
803 if (CanAcceptPipe && T.canPipeOutput()) {
804 // Some actions default to writing to a pipe if they are the top
805 // level phase and there was no user override.
806 //
807 // FIXME: Is there a better way to handle this?
808 if (AtTopLevel) {
809 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
810 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000811 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000812 OutputToPipe = true;
813 }
814
815 // Figure out where to put the job (pipes).
816 Job *Dest = &C.getJobs();
817 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +0000818 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000819 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
820 Dest = &InputInfos[0].getPipe();
821 }
822
823 // Always use the first input as the base input.
824 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +0000825
826 // Determine the place to write output to (nothing, pipe, or
827 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +0000828 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000829 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000830 } else if (OutputToPipe) {
831 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000832 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
833 if (!PJ) {
834 PJ = new PipedJob();
Daniel Dunbarb7b61b22009-03-20 00:11:04 +0000835 // FIXME: Temporary hack so that -ccc-print-bindings work until
836 // we have pipe support. Please remove later.
837 if (!CCCPrintBindings)
838 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000839 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +0000840 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000841 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000842 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000843 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
844 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000845 }
846
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000847 if (CCCPrintBindings) {
848 llvm::errs() << "bind - \"" << T.getName() << "\", inputs: [";
849 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
850 llvm::errs() << InputInfos[i].getAsString();
851 if (i + 1 != e)
852 llvm::errs() << ", ";
853 }
854 llvm::errs() << "], output: " << Result.getAsString() << "\n";
855 } else {
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000856 T.ConstructJob(C, *JA, *Dest, Result, InputInfos,
857 C.getArgsForToolChain(TC), LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000858 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000859}
860
Daniel Dunbar441d0602009-03-17 17:53:55 +0000861const char *Driver::GetNamedOutputPath(Compilation &C,
862 const JobAction &JA,
863 const char *BaseInput,
864 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000865 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +0000866 // Output to a user requested destination?
867 if (AtTopLevel) {
868 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
869 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
870 }
871
872 // Output to a temporary file?
873 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +0000874 std::string TmpName =
875 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
876 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +0000877 }
878
879 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +0000880 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +0000881
882 // Determine what the derived output name should be.
883 const char *NamedOutput;
884 if (JA.getType() == types::TY_Image) {
885 NamedOutput = DefaultImageName.c_str();
886 } else {
887 const char *Suffix = types::getTypeTempSuffix(JA.getType());
888 assert(Suffix && "All types used for output should have a suffix.");
889
890 std::string::size_type End = std::string::npos;
891 if (!types::appendSuffixForType(JA.getType()))
892 End = BaseName.rfind('.');
893 std::string Suffixed(BaseName.substr(0, End));
894 Suffixed += '.';
895 Suffixed += Suffix;
896 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
897 }
898
899 // As an annoying special case, PCH generation doesn't strip the
900 // pathname.
901 if (JA.getType() == types::TY_PCH) {
902 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +0000903 if (BasePath.isEmpty())
904 BasePath = NamedOutput;
905 else
906 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000907 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
908 } else {
909 return C.addResultFile(NamedOutput);
910 }
911}
912
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000913llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000914 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000915 const ToolChain::path_list &List = TC.getFilePaths();
916 for (ToolChain::path_list::const_iterator
917 it = List.begin(), ie = List.end(); it != ie; ++it) {
918 llvm::sys::Path P(*it);
919 P.appendComponent(Name);
920 if (P.exists())
921 return P;
922 }
923
Daniel Dunbarcb881672009-03-13 00:51:18 +0000924 return llvm::sys::Path(Name);
925}
926
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000927llvm::sys::Path Driver::GetProgramPath(const char *Name,
Mike Stump950bedd2009-03-27 00:40:20 +0000928 const ToolChain &TC,
929 bool WantFile) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000930 const ToolChain::path_list &List = TC.getProgramPaths();
931 for (ToolChain::path_list::const_iterator
932 it = List.begin(), ie = List.end(); it != ie; ++it) {
933 llvm::sys::Path P(*it);
934 P.appendComponent(Name);
Mike Stump950bedd2009-03-27 00:40:20 +0000935 if (WantFile ? P.exists() : P.canExecute())
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000936 return P;
937 }
938
Daniel Dunbarc50b00d2009-03-23 16:15:50 +0000939 // If all else failed, search the path.
940 llvm::sys::Path P(llvm::sys::Program::FindProgramByName(Name));
Daniel Dunbar632f50e2009-03-18 21:34:08 +0000941 if (!P.empty())
942 return P;
943
Daniel Dunbarcb881672009-03-13 00:51:18 +0000944 return llvm::sys::Path(Name);
945}
946
Daniel Dunbar214399e2009-03-18 19:34:39 +0000947std::string Driver::GetTemporaryPath(const char *Suffix) const {
948 // FIXME: This is lame; sys::Path should provide this function (in
949 // particular, it should know how to find the temporary files dir).
950 std::string Error;
951 llvm::sys::Path P("/tmp/cc");
952 if (P.makeUnique(false, &Error)) {
953 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
954 return "";
955 }
956
Daniel Dunbar84603bc2009-03-18 23:08:52 +0000957 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
958 // PR3837.
959 P.eraseFromDisk(false, 0);
960
Daniel Dunbar214399e2009-03-18 19:34:39 +0000961 P.appendSuffix(Suffix);
962 return P.toString();
963}
964
Daniel Dunbare5049522009-03-17 20:45:45 +0000965const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000966 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000967 // Dice into arch, platform, and OS. This matches
968 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
969 // and missing fields are left empty.
970 std::string Arch, Platform, OS;
971
972 if (const char *ArchEnd = strchr(Triple, '-')) {
973 Arch = std::string(Triple, ArchEnd);
974
975 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
976 Platform = std::string(ArchEnd+1, PlatformEnd);
977 OS = PlatformEnd+1;
978 } else
979 Platform = ArchEnd+1;
980 } else
981 Arch = Triple;
982
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000983 // Normalize Arch a bit.
984 //
985 // FIXME: This is very incomplete.
986 if (Arch == "i686")
987 Arch = "i386";
988 else if (Arch == "amd64")
989 Arch = "x86_64";
Daniel Dunbarc811b6c2009-03-18 04:41:46 +0000990 else if (Arch == "powerpc" || Arch == "Power Macintosh")
991 Arch = "ppc";
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000992
Daniel Dunbara88162c2009-03-13 12:23:29 +0000993 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbare5049522009-03-17 20:45:45 +0000994 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
995 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000996
Daniel Dunbare5049522009-03-17 20:45:45 +0000997 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
998 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000999}
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001000
1001bool Driver::ShouldUseClangCompiler(const Compilation &C, const JobAction &JA,
1002 const std::string &ArchName) const {
1003 // Check if user requested no clang, or clang doesn't understand
1004 // this type (we only handle single inputs for now).
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001005 if (!CCCUseClang || JA.size() != 1 ||
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001006 !types::isAcceptedByClang((*JA.begin())->getType()))
1007 return false;
1008
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001009 // Otherwise make sure this is an action clang understands.
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001010 if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbar6256d362009-03-24 19:14:56 +00001011 if (!CCCUseClangCPP) {
1012 Diag(clang::diag::warn_drv_not_using_clang_cpp);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001013 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001014 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001015 } else if (!isa<PrecompileJobAction>(JA) && !isa<CompileJobAction>(JA))
1016 return false;
1017
Daniel Dunbar0f99d2e2009-03-24 19:02:31 +00001018 // Use clang for C++?
Daniel Dunbar6256d362009-03-24 19:14:56 +00001019 if (!CCCUseClangCXX && types::isCXX((*JA.begin())->getType())) {
1020 Diag(clang::diag::warn_drv_not_using_clang_cxx);
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001021 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001022 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001023
1024 // Finally, don't use clang if this isn't one of the user specified
1025 // archs to build.
Daniel Dunbar6256d362009-03-24 19:14:56 +00001026 if (!CCCClangArchs.empty() && !CCCClangArchs.count(ArchName)) {
1027 Diag(clang::diag::warn_drv_not_using_clang_arch) << ArchName;
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001028 return false;
Daniel Dunbar6256d362009-03-24 19:14:56 +00001029 }
Daniel Dunbaraf80e1f2009-03-24 18:57:02 +00001030
1031 return true;
1032}
Daniel Dunbard73fe9b2009-03-26 15:58:36 +00001033
1034/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and
1035/// return the grouped values as integers. Numbers which are not
1036/// provided are set to 0.
1037///
1038/// \return True if the entire string was parsed (9.2), or all groups
1039/// were parsed (10.3.5extrastuff).
1040bool Driver::GetReleaseVersion(const char *Str, unsigned &Major,
1041 unsigned &Minor, unsigned &Micro,
1042 bool &HadExtra) {
1043 HadExtra = false;
1044
1045 Major = Minor = Micro = 0;
1046 if (*Str == '\0')
1047 return true;
1048
1049 char *End;
1050 Major = (unsigned) strtol(Str, &End, 10);
1051 if (*Str != '\0' && *End == '\0')
1052 return true;
1053 if (*End != '.')
1054 return false;
1055
1056 Str = End+1;
1057 Minor = (unsigned) strtol(Str, &End, 10);
1058 if (*Str != '\0' && *End == '\0')
1059 return true;
1060 if (*End != '.')
1061 return false;
1062
1063 Str = End+1;
1064 Micro = (unsigned) strtol(Str, &End, 10);
1065 if (*Str != '\0' && *End == '\0')
1066 return true;
1067 if (Str == End)
1068 return false;
1069 HadExtra = true;
1070 return true;
1071}