blob: 2c324010dac294f89a94816b839c82531975e51c [file] [log] [blame]
Daniel Dunbar63c4da92009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar63c4da92009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar63c4da92009-03-02 19:59:07 +000011
Daniel Dunbardb62cc32009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar93468492009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbard25acaa2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000024
Daniel Dunbarb1873cd2009-03-13 20:33:35 +000025#include "llvm/ADT/StringSet.h"
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000026#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000028#include "llvm/System/Path.h"
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +000029#include "llvm/System/Program.h"
Daniel Dunbar494646b2009-03-13 12:19:02 +000030
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000031#include "InputInfo.h"
32
Daniel Dunbar494646b2009-03-13 12:19:02 +000033#include <map>
34
Daniel Dunbard6f0e372009-03-04 20:49:20 +000035using namespace clang::driver;
36
Daniel Dunbard25acaa2009-03-10 23:41:59 +000037Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000038 const char *_DefaultHostTriple,
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000039 const char *_DefaultImageName,
Daniel Dunbar93468492009-03-12 08:55:43 +000040 Diagnostic &_Diags)
41 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000042 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +000043 DefaultImageName(_DefaultImageName),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000044 Host(0),
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +000045 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +000046 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false),
47 SuppressMissingInputWarning(false)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000048{
Daniel Dunbar63c4da92009-03-02 19:59:07 +000049}
50
51Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000052 delete Opts;
Daniel Dunbar2f8b37e2009-03-18 01:09:40 +000053 delete Host;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000054}
55
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000056ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000057 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000058 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
59
Daniel Dunbar85cb3592009-03-13 11:38:42 +000060 // FIXME: Handle '@' args (or at least error on them).
61
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000062 unsigned Index = 0, End = ArgEnd - ArgBegin;
63 while (Index < End) {
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000064 // gcc's handling of empty arguments doesn't make
65 // sense, but this is not a common use case. :)
66 //
67 // We just ignore them here (note that other things may
68 // still take them as arguments).
69 if (Args->getArgString(Index)[0] == '\0') {
70 ++Index;
71 continue;
72 }
73
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000074 unsigned Prev = Index;
75 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000076 if (A) {
77 if (A->getOption().isUnsupported()) {
Daniel Dunbar73225932009-03-20 06:14:23 +000078 Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000079 continue;
80 }
81
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000082 Args->append(A);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000083 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000084
85 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbarbb087552009-03-17 04:12:06 +000086 (void) Prev;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000087 }
88
89 return Args;
90}
91
Daniel Dunbar63c4da92009-03-02 19:59:07 +000092Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +000093 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
94
Daniel Dunbarcc006892009-03-13 00:51:18 +000095 // FIXME: Handle environment options which effect driver behavior,
96 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
97 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
98
99 // FIXME: What are we going to do with -V and -b?
100
101 // FIXME: Handle CCC_ADD_ARGS.
102
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000103 // FIXME: This stuff needs to go into the Compilation, not the
104 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000105 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000106
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000107 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000108 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000109
110 // Read -ccc args.
111 //
112 // FIXME: We need to figure out where this behavior should
113 // live. Most of it should be outside in the client; the parts that
114 // aren't should have proper options, either by introducing new ones
115 // or by overloading gcc ones like -V or -b.
116 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
117 const char *Opt = *Start + 5;
118
119 if (!strcmp(Opt, "print-options")) {
120 CCCPrintOptions = true;
121 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000122 CCCPrintActions = true;
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000123 } else if (!strcmp(Opt, "print-bindings")) {
124 CCCPrintBindings = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000125 } else if (!strcmp(Opt, "cxx")) {
126 CCCIsCXX = true;
127 } else if (!strcmp(Opt, "echo")) {
128 CCCEcho = true;
129
130 } else if (!strcmp(Opt, "no-clang")) {
131 CCCNoClang = true;
132 } else if (!strcmp(Opt, "no-clang-cxx")) {
133 CCCNoClangCXX = true;
134 } else if (!strcmp(Opt, "no-clang-cpp")) {
135 CCCNoClangCPP = true;
136 } else if (!strcmp(Opt, "clang-archs")) {
137 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
138 const char *Cur = *++Start;
139
140 for (;;) {
141 const char *Next = strchr(Cur, ',');
142
143 if (Next) {
144 CCCClangArchs.insert(std::string(Cur, Next));
145 Cur = Next + 1;
146 } else {
147 CCCClangArchs.insert(std::string(Cur));
148 break;
149 }
150 }
151
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000152 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000153 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000154 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000155
156 } else {
157 // FIXME: Error handling.
158 llvm::errs() << "invalid option: " << *Start << "\n";
159 exit(1);
160 }
161 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000162
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000163 ArgList *Args = ParseArgStrings(Start, End);
164
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000165 Host = GetHostInfo(HostTriple);
Daniel Dunbarcc006892009-03-13 00:51:18 +0000166
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000167 // The compilation takes ownership of Args.
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000168 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000169
170 // FIXME: This behavior shouldn't be here.
171 if (CCCPrintOptions) {
172 PrintOptions(C->getArgs());
173 return C;
174 }
175
176 if (!HandleImmediateArgs(*C))
177 return C;
178
179 // Construct the list of abstract actions to perform for this
180 // compilation. We avoid passing a Compilation here simply to
181 // enforce the abstraction that pipelining is not host or toolchain
182 // dependent (other than the driver driver test).
183 if (Host->useDriverDriver())
184 BuildUniversalActions(C->getArgs(), C->getActions());
185 else
186 BuildActions(C->getArgs(), C->getActions());
187
188 if (CCCPrintActions) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000189 PrintActions(*C);
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000190 return C;
191 }
192
193 BuildJobs(*C);
Daniel Dunbarc413f822009-03-15 01:38:15 +0000194
195 return C;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000196}
197
Daniel Dunbara790d372009-03-12 18:24:49 +0000198void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000199 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000200 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000201 it != ie; ++it, ++i) {
202 Arg *A = *it;
203 llvm::errs() << "Option " << i << " - "
204 << "Name: \"" << A->getOption().getName() << "\", "
205 << "Values: {";
206 for (unsigned j = 0; j < A->getNumValues(); ++j) {
207 if (j)
208 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000209 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000210 }
211 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000212 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000213}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000214
Daniel Dunbarcc006892009-03-13 00:51:18 +0000215void Driver::PrintVersion() const {
Mike Stump74d94472009-03-18 14:00:02 +0000216 static char buf[] = "$URL$";
217 char *zap = strstr(buf, "/lib/Driver");
218 if (zap)
219 *zap = 0;
220 zap = strstr(buf, "/clang/tools/clang");
221 if (zap)
222 *zap = 0;
Mike Stumpbc927292009-03-18 15:19:35 +0000223 const char *vers = buf+6;
Mike Stump3fc58f02009-03-18 18:45:55 +0000224 // FIXME: Add cmake support and remove #ifdef
225#ifdef SVN_REVISION
226 const char *revision = SVN_REVISION;
227#else
228 const char *revision = "";
229#endif
Daniel Dunbarcc006892009-03-13 00:51:18 +0000230 // FIXME: The following handlers should use a callback mechanism, we
231 // don't know what the client would like to do.
Mike Stump1207fae2009-03-18 21:19:11 +0000232 llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
Mike Stump74d94472009-03-18 14:00:02 +0000233 // FIXME: Add cmake support and remove #ifdef
234#ifdef TARGET_TRIPLE
235 llvm::errs() << "Target: " << TARGET_TRIPLE << "\n";
236#endif
Daniel Dunbarcc006892009-03-13 00:51:18 +0000237}
238
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000239bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000240 // The order these options are handled in in gcc is all over the
241 // place, but we don't expect inconsistencies w.r.t. that to matter
242 // in practice.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000243 if (C.getArgs().hasArg(options::OPT_v) ||
244 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000245 PrintVersion();
246 SuppressMissingInputWarning = true;
247 }
248
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000249 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbar91b9e202009-03-20 04:37:21 +0000250 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
251 llvm::outs() << "programs: =";
252 for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
253 ie = TC.getProgramPaths().end(); it != ie; ++it) {
254 if (it != TC.getProgramPaths().begin())
255 llvm::outs() << ':';
256 llvm::outs() << *it;
257 }
258 llvm::outs() << "\n";
259 llvm::outs() << "libraries: =";
260 for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
261 ie = TC.getFilePaths().end(); it != ie; ++it) {
262 if (it != TC.getFilePaths().begin())
263 llvm::outs() << ':';
264 llvm::outs() << *it;
265 }
266 llvm::outs() << "\n";
267 }
268
Daniel Dunbarcc006892009-03-13 00:51:18 +0000269 // FIXME: The following handlers should use a callback mechanism, we
270 // don't know what the client would like to do.
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000271 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
272 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
273 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000274 return false;
275 }
276
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000277 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
278 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
279 << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000280 return false;
281 }
282
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000283 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
284 llvm::outs() << GetProgramPath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcc006892009-03-13 00:51:18 +0000285 return false;
286 }
287
288 return true;
289}
290
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000291static unsigned PrintActions1(const Compilation &C,
Daniel Dunbar494646b2009-03-13 12:19:02 +0000292 Action *A,
293 std::map<Action*, unsigned> &Ids) {
294 if (Ids.count(A))
295 return Ids[A];
296
297 std::string str;
298 llvm::raw_string_ostream os(str);
299
300 os << Action::getClassName(A->getKind()) << ", ";
301 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000302 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000303 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000304 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
305 C.getDefaultToolChain().getArchName()) << '"'
306 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000307 } else {
308 os << "{";
309 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000310 os << PrintActions1(C, *it, Ids);
Daniel Dunbar494646b2009-03-13 12:19:02 +0000311 ++it;
312 if (it != ie)
313 os << ", ";
314 }
315 os << "}";
316 }
317
318 unsigned Id = Ids.size();
319 Ids[A] = Id;
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000320 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000321 << types::getTypeName(A->getType()) << "\n";
322
323 return Id;
324}
325
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000326void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbar494646b2009-03-13 12:19:02 +0000327 std::map<Action*, unsigned> Ids;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000328 for (ActionList::const_iterator it = C.getActions().begin(),
329 ie = C.getActions().end(); it != ie; ++it)
330 PrintActions1(C, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000331}
332
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000333void Driver::BuildUniversalActions(const ArgList &Args,
334 ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000335 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000336 // Collect the list of architectures. Duplicates are allowed, but
337 // should only be handled once (in the order seen).
338 llvm::StringSet<> ArchNames;
339 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000340 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
341 it != ie; ++it) {
342 Arg *A = *it;
343
344 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000345 const char *Name = A->getValue(Args);
346
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000347 // FIXME: We need to handle canonicalization of the specified
348 // arch?
349
Daniel Dunbara345a442009-03-19 07:55:12 +0000350 A->claim();
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000351 if (ArchNames.insert(Name))
352 Archs.push_back(Name);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000353 }
354 }
355
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000356 // When there is no explicit arch for this platform, make sure we
357 // still bind the architecture (to the default) so that -Xarch_ is
358 // handled correctly.
359 if (!Archs.size())
360 Archs.push_back(0);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000361
362 // FIXME: We killed off some others but these aren't yet detected in
363 // a functional manner. If we added information to jobs about which
364 // "auxiliary" files they wrote then we could detect the conflict
365 // these cause downstream.
366 if (Archs.size() > 1) {
367 // No recovery needed, the point of this is just to prevent
368 // overwriting the same files.
369 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
370 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000371 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000372 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
373 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
Daniel Dunbar73225932009-03-20 06:14:23 +0000374 << A->getAsString(Args);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000375 }
376
377 ActionList SingleActions;
378 BuildActions(Args, SingleActions);
379
380 // Add in arch binding and lipo (if necessary) for every top level
381 // action.
382 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
383 Action *Act = SingleActions[i];
384
385 // Make sure we can lipo this kind of output. If not (and it is an
386 // actual output) then we disallow, since we can't create an
387 // output file with the right name without overwriting it. We
388 // could remove this oddity by just changing the output names to
389 // include the arch, which would also fix
390 // -save-temps. Compatibility wins for now.
391
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000392 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000393 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
394 << types::getTypeName(Act->getType());
395
396 ActionList Inputs;
Daniel Dunbara345a442009-03-19 07:55:12 +0000397 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000398 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000399
400 // Lipo if necessary, We do it this way because we need to set the
401 // arch flag so that -Xarch_ gets overwritten.
402 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
403 Actions.append(Inputs.begin(), Inputs.end());
404 else
405 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
406 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000407}
408
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000409void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000410 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000411 // Start by constructing the list of inputs and their types.
412
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000413 // Track the current user specified (-x) input. We also explicitly
414 // track the argument used to set the type; we only want to claim
415 // the type when we actually use it, so we warn about unused -x
416 // arguments.
417 types::ID InputType = types::TY_Nothing;
418 Arg *InputTypeArg = 0;
419
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000420 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
421 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
422 it != ie; ++it) {
423 Arg *A = *it;
424
425 if (isa<InputOption>(A->getOption())) {
426 const char *Value = A->getValue(Args);
427 types::ID Ty = types::TY_INVALID;
428
429 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000430 if (InputType == types::TY_Nothing) {
431 // If there was an explicit arg for this, claim it.
432 if (InputTypeArg)
433 InputTypeArg->claim();
434
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000435 // stdin must be handled specially.
436 if (memcmp(Value, "-", 2) == 0) {
437 // If running with -E, treat as a C input (this changes the
438 // builtin macros, for example). This may be overridden by
439 // -ObjC below.
440 //
441 // Otherwise emit an error but still use a valid type to
442 // avoid spurious errors (e.g., no inputs).
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000443 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbard724e332009-03-12 09:13:48 +0000444 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000445 Ty = types::TY_C;
446 } else {
447 // Otherwise lookup by extension, and fallback to ObjectType
448 // if not found.
449 if (const char *Ext = strrchr(Value, '.'))
450 Ty = types::lookupTypeForExtension(Ext + 1);
451 if (Ty == types::TY_INVALID)
452 Ty = types::TY_Object;
453 }
454
455 // -ObjC and -ObjC++ override the default language, but only
456 // -for "source files". We just treat everything that isn't a
457 // -linker input as a source file.
458 //
459 // FIXME: Clean this up if we move the phase sequence into the
460 // type.
461 if (Ty != types::TY_Object) {
462 if (Args.hasArg(options::OPT_ObjC))
463 Ty = types::TY_ObjC;
464 else if (Args.hasArg(options::OPT_ObjCXX))
465 Ty = types::TY_ObjCXX;
466 }
467 } else {
468 assert(InputTypeArg && "InputType set w/o InputTypeArg");
469 InputTypeArg->claim();
470 Ty = InputType;
471 }
472
473 // Check that the file exists. It isn't clear this is worth
474 // doing, since the tool presumably does this anyway, and this
475 // just adds an extra stat to the equation, but this is gcc
476 // compatible.
477 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000478 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000479 else
480 Inputs.push_back(std::make_pair(Ty, A));
481
482 } else if (A->getOption().isLinkerInput()) {
483 // Just treat as object type, we could make a special type for
484 // this if necessary.
485 Inputs.push_back(std::make_pair(types::TY_Object, A));
486
487 } else if (A->getOption().getId() == options::OPT_x) {
488 InputTypeArg = A;
489 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
490
491 // Follow gcc behavior and treat as linker input for invalid -x
492 // options. Its not clear why we shouldn't just revert to
493 // unknown; but this isn't very important, we might as well be
494 // bug comatible.
495 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000496 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000497 InputType = types::TY_Object;
498 }
499 }
500 }
501
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000502 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000503 Diag(clang::diag::err_drv_no_input_files);
504 return;
505 }
506
507 // Determine which compilation mode we are in. We look for options
508 // which affect the phase, starting with the earliest phases, and
509 // record which option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000510 Arg *FinalPhaseArg = 0;
511 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000512
513 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000514 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
515 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
516 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
517 FinalPhase = phases::Preprocess;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000518
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000519 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
520 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
521 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
522 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_llvm)) ||
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000523 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
524 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000525
526 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000527 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
528 FinalPhase = phases::Assemble;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000529
530 // Otherwise do everything.
531 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000532 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000533
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000534 // Reject -Z* at the top level, these options should never have been
535 // exposed by gcc.
536 if (Arg *A = Args.getLastArg(options::OPT_Z))
Daniel Dunbar73225932009-03-20 06:14:23 +0000537 Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args);
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000538
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000539 // Construct the actions to perform.
540 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000541 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000542 types::ID InputType = Inputs[i].first;
543 const Arg *InputArg = Inputs[i].second;
544
545 unsigned NumSteps = types::getNumCompilationPhases(InputType);
546 assert(NumSteps && "Invalid number of steps!");
547
548 // If the first step comes after the final phase we are doing as
549 // part of this compilation, warn the user about it.
550 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
551 if (InitialPhase > FinalPhase) {
Daniel Dunbar923e7032009-03-19 07:57:08 +0000552 // Claim here to avoid the more general unused warning.
553 InputArg->claim();
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000554 Diag(clang::diag::warn_drv_input_file_unused)
Daniel Dunbar73225932009-03-20 06:14:23 +0000555 << InputArg->getAsString(Args)
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000556 << getPhaseName(InitialPhase)
557 << FinalPhaseArg->getOption().getName();
558 continue;
559 }
560
561 // Build the pipeline for this file.
562 Action *Current = new InputAction(*InputArg, InputType);
563 for (unsigned i = 0; i != NumSteps; ++i) {
564 phases::ID Phase = types::getCompilationPhase(InputType, i);
565
566 // We are done if this step is past what the user requested.
567 if (Phase > FinalPhase)
568 break;
569
570 // Queue linker inputs.
571 if (Phase == phases::Link) {
572 assert(i + 1 == NumSteps && "linking must be final compilation step.");
573 LinkerInputs.push_back(Current);
574 Current = 0;
575 break;
576 }
577
578 // Otherwise construct the appropriate action.
579 Current = ConstructPhaseAction(Args, Phase, Current);
580 if (Current->getType() == types::TY_Nothing)
581 break;
582 }
583
584 // If we ended with something, add to the output list.
585 if (Current)
586 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000587 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000588
589 // Add a link action if necessary.
590 if (!LinkerInputs.empty())
591 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
592}
593
594Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
595 Action *Input) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000596 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000597 // Build the appropriate action.
598 switch (Phase) {
599 case phases::Link: assert(0 && "link action invalid here.");
600 case phases::Preprocess: {
601 types::ID OutputTy = types::getPreprocessedType(Input->getType());
602 assert(OutputTy != types::TY_INVALID &&
603 "Cannot preprocess this input type!");
604 return new PreprocessJobAction(Input, OutputTy);
605 }
606 case phases::Precompile:
607 return new PrecompileJobAction(Input, types::TY_PCH);
608 case phases::Compile: {
609 if (Args.hasArg(options::OPT_fsyntax_only)) {
610 return new CompileJobAction(Input, types::TY_Nothing);
611 } else if (Args.hasArg(options::OPT__analyze)) {
612 return new AnalyzeJobAction(Input, types::TY_Plist);
613 } else if (Args.hasArg(options::OPT_emit_llvm)) {
614 types::ID Output =
615 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
616 return new CompileJobAction(Input, Output);
617 } else {
618 return new CompileJobAction(Input, types::TY_PP_Asm);
619 }
620 }
621 case phases::Assemble:
622 return new AssembleJobAction(Input, types::TY_Object);
623 }
624
625 assert(0 && "invalid phase in ConstructPhaseAction");
626 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000627}
628
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000629void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000630 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000631 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
632 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000633
634 // FIXME: Pipes are forcibly disabled until we support executing
635 // them.
636 if (!CCCPrintBindings)
637 UsePipes = false;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000638
639 // -save-temps inhibits pipes.
640 if (SaveTemps && UsePipes) {
641 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
642 UsePipes = true;
643 }
644
645 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
646
647 // It is an error to provide a -o option if we are making multiple
648 // output files.
649 if (FinalOutput) {
650 unsigned NumOutputs = 0;
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000651 for (ActionList::const_iterator it = C.getActions().begin(),
652 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000653 if ((*it)->getType() != types::TY_Nothing)
654 ++NumOutputs;
655
656 if (NumOutputs > 1) {
657 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
658 FinalOutput = 0;
659 }
660 }
661
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000662 for (ActionList::const_iterator it = C.getActions().begin(),
663 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000664 Action *A = *it;
665
666 // If we are linking an image for multiple archs then the linker
667 // wants -arch_multiple and -final_output <final image
668 // name>. Unfortunately, this doesn't fit in cleanly because we
669 // have to pass this information down.
670 //
671 // FIXME: This is a hack; find a cleaner way to integrate this
672 // into the process.
673 const char *LinkingOutput = 0;
674 if (isa<LinkJobAction>(A)) {
675 if (FinalOutput)
676 LinkingOutput = FinalOutput->getValue(C.getArgs());
677 else
678 LinkingOutput = DefaultImageName.c_str();
679 }
680
681 InputInfo II;
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000682 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000683 /*CanAcceptPipe*/ true,
684 /*AtTopLevel*/ true,
685 /*LinkingOutput*/ LinkingOutput,
686 II);
687 }
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000688
Daniel Dunbar46c70822009-03-18 18:03:46 +0000689 // If there were errors, don't warn about any unused arguments.
690 if (Diags.getNumErrors())
691 return;
692
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000693 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
694 it != ie; ++it) {
695 Arg *A = *it;
Daniel Dunbar46c70822009-03-18 18:03:46 +0000696
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000697 // FIXME: It would be nice to be able to send the argument to the
698 // Diagnostic, so that extra values, position, and so on could be
699 // printed.
700 if (!A->isClaimed())
701 Diag(clang::diag::warn_drv_unused_argument)
Daniel Dunbar73225932009-03-20 06:14:23 +0000702 << A->getAsString(C.getArgs());
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000703 }
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000704}
705
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000706void Driver::BuildJobsForAction(Compilation &C,
707 const Action *A,
708 const ToolChain *TC,
709 bool CanAcceptPipe,
710 bool AtTopLevel,
711 const char *LinkingOutput,
712 InputInfo &Result) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000713 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000714
715 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
716 // FIXME: Pipes are forcibly disabled until we support executing
717 // them.
718 if (!CCCPrintBindings)
719 UsePipes = false;
720
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000721 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbarc3be67b2009-03-19 07:29:38 +0000722 // FIXME: It would be nice to not claim this here; maybe the old
723 // scheme of just using Args was better?
724 const Arg &Input = IA->getInputArg();
725 Input.claim();
726 if (isa<PositionalArg>(Input)) {
727 const char *Name = Input.getValue(C.getArgs());
728 Result = InputInfo(Name, A->getType(), Name);
729 } else
730 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000731 return;
732 }
733
734 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
735 const char *ArchName = BAA->getArchName();
Daniel Dunbar2b856ce2009-03-18 03:13:20 +0000736 if (!ArchName)
737 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000738 BuildJobsForAction(C,
739 *BAA->begin(),
740 Host->getToolChain(C.getArgs(), ArchName),
741 CanAcceptPipe,
742 AtTopLevel,
743 LinkingOutput,
744 Result);
745 return;
746 }
747
748 const JobAction *JA = cast<JobAction>(A);
749 const Tool &T = TC->SelectTool(C, *JA);
750
751 // See if we should use an integrated preprocessor. We do so when we
752 // have exactly one input, since this is the only use case we care
753 // about (irrelevant since we don't support combine yet).
754 bool UseIntegratedCPP = false;
755 const ActionList *Inputs = &A->getInputs();
756 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
757 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
758 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
759 !C.getArgs().hasArg(options::OPT_save_temps) &&
760 T.hasIntegratedCPP()) {
761 UseIntegratedCPP = true;
762 Inputs = &(*Inputs)[0]->getInputs();
763 }
764 }
765
766 // Only use pipes when there is exactly one input.
767 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar00fd3792009-03-18 06:00:36 +0000768 InputInfoList InputInfos;
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000769 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
770 it != ie; ++it) {
771 InputInfo II;
772 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
773 /*AtTopLevel*/false,
774 LinkingOutput,
775 II);
776 InputInfos.push_back(II);
777 }
778
779 // Determine if we should output to a pipe.
780 bool OutputToPipe = false;
781 if (CanAcceptPipe && T.canPipeOutput()) {
782 // Some actions default to writing to a pipe if they are the top
783 // level phase and there was no user override.
784 //
785 // FIXME: Is there a better way to handle this?
786 if (AtTopLevel) {
787 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
788 OutputToPipe = true;
Daniel Dunbarbac6b642009-03-18 23:18:19 +0000789 } else if (UsePipes)
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000790 OutputToPipe = true;
791 }
792
793 // Figure out where to put the job (pipes).
794 Job *Dest = &C.getJobs();
795 if (InputInfos[0].isPipe()) {
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000796 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000797 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
798 Dest = &InputInfos[0].getPipe();
799 }
800
801 // Always use the first input as the base input.
802 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000803
804 // Determine the place to write output to (nothing, pipe, or
805 // filename) and where to put the new job.
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000806 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000807 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000808 } else if (OutputToPipe) {
809 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000810 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
811 if (!PJ) {
812 PJ = new PipedJob();
Daniel Dunbar8eeb5a32009-03-20 00:11:04 +0000813 // FIXME: Temporary hack so that -ccc-print-bindings work until
814 // we have pipe support. Please remove later.
815 if (!CCCPrintBindings)
816 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar933ac452009-03-18 07:06:02 +0000817 Dest = PJ;
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000818 }
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000819 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000820 } else {
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000821 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
822 A->getType(), BaseInput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000823 }
824
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000825 if (CCCPrintBindings) {
826 llvm::errs() << "bind - \"" << T.getName() << "\", inputs: [";
827 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
828 llvm::errs() << InputInfos[i].getAsString();
829 if (i + 1 != e)
830 llvm::errs() << ", ";
831 }
832 llvm::errs() << "], output: " << Result.getAsString() << "\n";
833 } else {
Daniel Dunbar00fd3792009-03-18 06:00:36 +0000834 const ArgList &TCArgs = C.getArgsForToolChain(TC);
Daniel Dunbar933ac452009-03-18 07:06:02 +0000835 T.ConstructJob(C, *JA, *Dest, Result, InputInfos, TCArgs, LinkingOutput);
Daniel Dunbar07c9a1d2009-03-17 22:47:06 +0000836 }
Daniel Dunbar7ce6add2009-03-16 06:56:51 +0000837}
838
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000839const char *Driver::GetNamedOutputPath(Compilation &C,
840 const JobAction &JA,
841 const char *BaseInput,
842 bool AtTopLevel) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000843 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000844 // Output to a user requested destination?
845 if (AtTopLevel) {
846 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
847 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
848 }
849
850 // Output to a temporary file?
851 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbarb6ddc952009-03-18 19:34:39 +0000852 std::string TmpName =
853 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
854 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000855 }
856
857 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbarfb5e3332009-03-18 02:00:31 +0000858 std::string BaseName(BasePath.getLast());
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000859
860 // Determine what the derived output name should be.
861 const char *NamedOutput;
862 if (JA.getType() == types::TY_Image) {
863 NamedOutput = DefaultImageName.c_str();
864 } else {
865 const char *Suffix = types::getTypeTempSuffix(JA.getType());
866 assert(Suffix && "All types used for output should have a suffix.");
867
868 std::string::size_type End = std::string::npos;
869 if (!types::appendSuffixForType(JA.getType()))
870 End = BaseName.rfind('.');
871 std::string Suffixed(BaseName.substr(0, End));
872 Suffixed += '.';
873 Suffixed += Suffix;
874 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
875 }
876
877 // As an annoying special case, PCH generation doesn't strip the
878 // pathname.
879 if (JA.getType() == types::TY_PCH) {
880 BasePath.eraseComponent();
Daniel Dunbar58a51262009-03-18 09:58:30 +0000881 if (BasePath.isEmpty())
882 BasePath = NamedOutput;
883 else
884 BasePath.appendComponent(NamedOutput);
Daniel Dunbar01fb26a2009-03-17 17:53:55 +0000885 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
886 } else {
887 return C.addResultFile(NamedOutput);
888 }
889}
890
Daniel Dunbare1cef7d2009-03-16 05:25:36 +0000891llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000892 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +0000893 const ToolChain::path_list &List = TC.getFilePaths();
894 for (ToolChain::path_list::const_iterator
895 it = List.begin(), ie = List.end(); it != ie; ++it) {
896 llvm::sys::Path P(*it);
897 P.appendComponent(Name);
898 if (P.exists())
899 return P;
900 }
901
Daniel Dunbarcc006892009-03-13 00:51:18 +0000902 return llvm::sys::Path(Name);
903}
904
Daniel Dunbare1cef7d2009-03-16 05:25:36 +0000905llvm::sys::Path Driver::GetProgramPath(const char *Name,
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +0000906 const ToolChain &TC) const {
Daniel Dunbarcc7600c2009-03-18 20:26:19 +0000907 const ToolChain::path_list &List = TC.getProgramPaths();
908 for (ToolChain::path_list::const_iterator
909 it = List.begin(), ie = List.end(); it != ie; ++it) {
910 llvm::sys::Path P(*it);
911 P.appendComponent(Name);
912 if (P.exists())
913 return P;
914 }
915
916 // As a last resort, always search in our directory before pulling
917 // from the path.
918 llvm::sys::Path P(Dir);
919 P.appendComponent(Name);
920 if (P.exists())
921 return P;
922
Daniel Dunbarcb84b9a2009-03-18 21:34:08 +0000923 // Search path to increase accuracy of logging output.
924 P = llvm::sys::Program::FindProgramByName(Name);
925 if (!P.empty())
926 return P;
927
Daniel Dunbarcc006892009-03-13 00:51:18 +0000928 return llvm::sys::Path(Name);
929}
930
Daniel Dunbarb6ddc952009-03-18 19:34:39 +0000931std::string Driver::GetTemporaryPath(const char *Suffix) const {
932 // FIXME: This is lame; sys::Path should provide this function (in
933 // particular, it should know how to find the temporary files dir).
934 std::string Error;
935 llvm::sys::Path P("/tmp/cc");
936 if (P.makeUnique(false, &Error)) {
937 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
938 return "";
939 }
940
Daniel Dunbar9083abe2009-03-18 23:08:52 +0000941 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
942 // PR3837.
943 P.eraseFromDisk(false, 0);
944
Daniel Dunbarb6ddc952009-03-18 19:34:39 +0000945 P.appendSuffix(Suffix);
946 return P.toString();
947}
948
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000949const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar16e04ff2009-03-18 01:38:48 +0000950 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000951 // Dice into arch, platform, and OS. This matches
952 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
953 // and missing fields are left empty.
954 std::string Arch, Platform, OS;
955
956 if (const char *ArchEnd = strchr(Triple, '-')) {
957 Arch = std::string(Triple, ArchEnd);
958
959 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
960 Platform = std::string(ArchEnd+1, PlatformEnd);
961 OS = PlatformEnd+1;
962 } else
963 Platform = ArchEnd+1;
964 } else
965 Arch = Triple;
966
Daniel Dunbar7424b8a2009-03-17 19:00:50 +0000967 // Normalize Arch a bit.
968 //
969 // FIXME: This is very incomplete.
970 if (Arch == "i686")
971 Arch = "i386";
972 else if (Arch == "amd64")
973 Arch = "x86_64";
Daniel Dunbard78126f2009-03-18 04:41:46 +0000974 else if (Arch == "powerpc" || Arch == "Power Macintosh")
975 Arch = "ppc";
Daniel Dunbar7424b8a2009-03-17 19:00:50 +0000976
Daniel Dunbar44119a12009-03-13 12:23:29 +0000977 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000978 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
979 OS.c_str());
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000980
Daniel Dunbar08966ca2009-03-17 20:45:45 +0000981 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
982 OS.c_str());
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000983}