blob: f5daf3e16bb0a5526bcdaa0eb0b36639ac1f9e36 [file] [log] [blame]
Daniel Dunbar3ede8d02009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000011
Daniel Dunbar53ec5522009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000024
Daniel Dunbar13689542009-03-13 20:33:35 +000025#include "llvm/ADT/StringSet.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000026#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000028#include "llvm/System/Path.h"
Daniel Dunbar632f50e2009-03-18 21:34:08 +000029#include "llvm/System/Program.h"
Daniel Dunbarba102132009-03-13 12:19:02 +000030
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000031#include "InputInfo.h"
32
Daniel Dunbarba102132009-03-13 12:19:02 +000033#include <map>
34
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000035using namespace clang::driver;
36
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000037Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000038 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000039 const char *_DefaultImageName,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000040 Diagnostic &_Diags)
41 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000042 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000043 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000044 Host(0),
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +000045 CCCIsCXX(false), CCCEcho(false), CCCPrintBindings(false),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000046 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false),
47 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000048{
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000049}
50
51Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000052 delete Opts;
Daniel Dunbar7e4534d2009-03-18 01:09:40 +000053 delete Host;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000054}
55
Daniel Dunbar06482622009-03-05 06:38:47 +000056ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000057 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
Daniel Dunbar06482622009-03-05 06:38:47 +000058 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
59
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000060 // FIXME: Handle '@' args (or at least error on them).
61
Daniel Dunbar06482622009-03-05 06:38:47 +000062 unsigned Index = 0, End = ArgEnd - ArgBegin;
63 while (Index < End) {
Daniel Dunbar41393402009-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 Dunbar06482622009-03-05 06:38:47 +000074 unsigned Prev = Index;
75 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000076 if (A) {
77 if (A->getOption().isUnsupported()) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +000078 Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName();
Daniel Dunbar53ec5522009-03-12 07:58:46 +000079 continue;
80 }
81
Daniel Dunbar06482622009-03-05 06:38:47 +000082 Args->append(A);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000083 }
Daniel Dunbar06482622009-03-05 06:38:47 +000084
85 assert(Index > Prev && "Parser failed to consume argument.");
Daniel Dunbar70c16842009-03-17 04:12:06 +000086 (void) Prev;
Daniel Dunbar06482622009-03-05 06:38:47 +000087 }
88
89 return Args;
90}
91
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000092Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbar8f25c792009-03-18 01:38:48 +000093 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
94
Daniel Dunbarcb881672009-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 Dunbar365c02f2009-03-10 20:52:46 +0000103 // FIXME: This stuff needs to go into the Compilation, not the
104 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000105 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000106
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000107 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000108 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-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 Dunbar53ec5522009-03-12 07:58:46 +0000122 CCCPrintActions = true;
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000123 } else if (!strcmp(Opt, "print-bindings")) {
124 CCCPrintBindings = true;
Daniel Dunbar365c02f2009-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 Dunbardd98e2c2009-03-10 23:41:59 +0000152 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000153 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000154 HostTriple = *++Start;
Daniel Dunbar365c02f2009-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 Dunbardd98e2c2009-03-10 23:41:59 +0000162
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000163 ArgList *Args = ParseArgStrings(Start, End);
164
Daniel Dunbare5049522009-03-17 20:45:45 +0000165 Host = GetHostInfo(HostTriple);
Daniel Dunbarcb881672009-03-13 00:51:18 +0000166
Daniel Dunbar586dc232009-03-16 06:42:30 +0000167 // The compilation takes ownership of Args.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000168 Compilation *C = new Compilation(*this, *Host->getToolChain(*Args), Args);
Daniel Dunbar21549232009-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 Dunbar10ffa9a2009-03-18 03:13:20 +0000189 PrintActions(*C);
Daniel Dunbar21549232009-03-18 02:55:38 +0000190 return C;
191 }
192
193 BuildJobs(*C);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000194
195 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000196}
197
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000198void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000199 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000200 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-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 Dunbar53ec5522009-03-12 07:58:46 +0000209 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000210 }
211 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000212 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000213}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000214
Daniel Dunbarcb881672009-03-13 00:51:18 +0000215void Driver::PrintVersion() const {
Mike Stump5d023c32009-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 Stumpe70295b2009-03-18 15:19:35 +0000223 const char *vers = buf+6;
Mike Stump8944c382009-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 Dunbarcb881672009-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 Stumpd227fe72009-03-18 21:19:11 +0000232 llvm::errs() << "clang version 1.0 (" << vers << " " << revision << ")" << "\n";
Mike Stump5d023c32009-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 Dunbarcb881672009-03-13 00:51:18 +0000237}
238
Daniel Dunbar21549232009-03-18 02:55:38 +0000239bool Driver::HandleImmediateArgs(const Compilation &C) {
Daniel Dunbarcb881672009-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 Dunbar21549232009-03-18 02:55:38 +0000243 if (C.getArgs().hasArg(options::OPT_v) ||
244 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000245 PrintVersion();
246 SuppressMissingInputWarning = true;
247 }
248
Daniel Dunbar21549232009-03-18 02:55:38 +0000249 const ToolChain &TC = C.getDefaultToolChain();
Daniel Dunbarcb881672009-03-13 00:51:18 +0000250 // FIXME: The following handlers should use a callback mechanism, we
251 // don't know what the client would like to do.
Daniel Dunbar21549232009-03-18 02:55:38 +0000252 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
253 llvm::outs() << GetFilePath(A->getValue(C.getArgs()), TC).toString()
254 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000255 return false;
256 }
257
Daniel Dunbar21549232009-03-18 02:55:38 +0000258 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
259 llvm::outs() << GetProgramPath(A->getValue(C.getArgs()), TC).toString()
260 << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000261 return false;
262 }
263
Daniel Dunbar21549232009-03-18 02:55:38 +0000264 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
265 llvm::outs() << GetProgramPath("libgcc.a", TC).toString() << "\n";
Daniel Dunbarcb881672009-03-13 00:51:18 +0000266 return false;
267 }
268
269 return true;
270}
271
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000272static unsigned PrintActions1(const Compilation &C,
Daniel Dunbarba102132009-03-13 12:19:02 +0000273 Action *A,
274 std::map<Action*, unsigned> &Ids) {
275 if (Ids.count(A))
276 return Ids[A];
277
278 std::string str;
279 llvm::raw_string_ostream os(str);
280
281 os << Action::getClassName(A->getKind()) << ", ";
282 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000283 os << "\"" << IA->getInputArg().getValue(C.getArgs()) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000284 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000285 os << '"' << (BIA->getArchName() ? BIA->getArchName() :
286 C.getDefaultToolChain().getArchName()) << '"'
287 << ", {" << PrintActions1(C, *BIA->begin(), Ids) << "}";
Daniel Dunbarba102132009-03-13 12:19:02 +0000288 } else {
289 os << "{";
290 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000291 os << PrintActions1(C, *it, Ids);
Daniel Dunbarba102132009-03-13 12:19:02 +0000292 ++it;
293 if (it != ie)
294 os << ", ";
295 }
296 os << "}";
297 }
298
299 unsigned Id = Ids.size();
300 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000301 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000302 << types::getTypeName(A->getType()) << "\n";
303
304 return Id;
305}
306
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000307void Driver::PrintActions(const Compilation &C) const {
Daniel Dunbarba102132009-03-13 12:19:02 +0000308 std::map<Action*, unsigned> Ids;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000309 for (ActionList::const_iterator it = C.getActions().begin(),
310 ie = C.getActions().end(); it != ie; ++it)
311 PrintActions1(C, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000312}
313
Daniel Dunbar21549232009-03-18 02:55:38 +0000314void Driver::BuildUniversalActions(const ArgList &Args,
315 ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000316 llvm::PrettyStackTraceString CrashInfo("Building actions for universal build");
Daniel Dunbar13689542009-03-13 20:33:35 +0000317 // Collect the list of architectures. Duplicates are allowed, but
318 // should only be handled once (in the order seen).
319 llvm::StringSet<> ArchNames;
320 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000321 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
322 it != ie; ++it) {
323 Arg *A = *it;
324
325 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000326 const char *Name = A->getValue(Args);
327
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000328 // FIXME: We need to handle canonicalization of the specified
329 // arch?
330
Daniel Dunbar75877192009-03-19 07:55:12 +0000331 A->claim();
Daniel Dunbar13689542009-03-13 20:33:35 +0000332 if (ArchNames.insert(Name))
333 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000334 }
335 }
336
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000337 // When there is no explicit arch for this platform, make sure we
338 // still bind the architecture (to the default) so that -Xarch_ is
339 // handled correctly.
340 if (!Archs.size())
341 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000342
343 // FIXME: We killed off some others but these aren't yet detected in
344 // a functional manner. If we added information to jobs about which
345 // "auxiliary" files they wrote then we could detect the conflict
346 // these cause downstream.
347 if (Archs.size() > 1) {
348 // No recovery needed, the point of this is just to prevent
349 // overwriting the same files.
350 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
351 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
352 << A->getOption().getName();
353 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
354 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
355 << A->getOption().getName();
356 }
357
358 ActionList SingleActions;
359 BuildActions(Args, SingleActions);
360
361 // Add in arch binding and lipo (if necessary) for every top level
362 // action.
363 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
364 Action *Act = SingleActions[i];
365
366 // Make sure we can lipo this kind of output. If not (and it is an
367 // actual output) then we disallow, since we can't create an
368 // output file with the right name without overwriting it. We
369 // could remove this oddity by just changing the output names to
370 // include the arch, which would also fix
371 // -save-temps. Compatibility wins for now.
372
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000373 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000374 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
375 << types::getTypeName(Act->getType());
376
377 ActionList Inputs;
Daniel Dunbar75877192009-03-19 07:55:12 +0000378 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
Daniel Dunbar13689542009-03-13 20:33:35 +0000379 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000380
381 // Lipo if necessary, We do it this way because we need to set the
382 // arch flag so that -Xarch_ gets overwritten.
383 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
384 Actions.append(Inputs.begin(), Inputs.end());
385 else
386 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
387 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000388}
389
Daniel Dunbar21549232009-03-18 02:55:38 +0000390void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000391 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000392 // Start by constructing the list of inputs and their types.
393
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000394 // Track the current user specified (-x) input. We also explicitly
395 // track the argument used to set the type; we only want to claim
396 // the type when we actually use it, so we warn about unused -x
397 // arguments.
398 types::ID InputType = types::TY_Nothing;
399 Arg *InputTypeArg = 0;
400
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000401 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
402 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
403 it != ie; ++it) {
404 Arg *A = *it;
405
406 if (isa<InputOption>(A->getOption())) {
407 const char *Value = A->getValue(Args);
408 types::ID Ty = types::TY_INVALID;
409
410 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000411 if (InputType == types::TY_Nothing) {
412 // If there was an explicit arg for this, claim it.
413 if (InputTypeArg)
414 InputTypeArg->claim();
415
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000416 // stdin must be handled specially.
417 if (memcmp(Value, "-", 2) == 0) {
418 // If running with -E, treat as a C input (this changes the
419 // builtin macros, for example). This may be overridden by
420 // -ObjC below.
421 //
422 // Otherwise emit an error but still use a valid type to
423 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000424 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000425 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000426 Ty = types::TY_C;
427 } else {
428 // Otherwise lookup by extension, and fallback to ObjectType
429 // if not found.
430 if (const char *Ext = strrchr(Value, '.'))
431 Ty = types::lookupTypeForExtension(Ext + 1);
432 if (Ty == types::TY_INVALID)
433 Ty = types::TY_Object;
434 }
435
436 // -ObjC and -ObjC++ override the default language, but only
437 // -for "source files". We just treat everything that isn't a
438 // -linker input as a source file.
439 //
440 // FIXME: Clean this up if we move the phase sequence into the
441 // type.
442 if (Ty != types::TY_Object) {
443 if (Args.hasArg(options::OPT_ObjC))
444 Ty = types::TY_ObjC;
445 else if (Args.hasArg(options::OPT_ObjCXX))
446 Ty = types::TY_ObjCXX;
447 }
448 } else {
449 assert(InputTypeArg && "InputType set w/o InputTypeArg");
450 InputTypeArg->claim();
451 Ty = InputType;
452 }
453
454 // Check that the file exists. It isn't clear this is worth
455 // doing, since the tool presumably does this anyway, and this
456 // just adds an extra stat to the equation, but this is gcc
457 // compatible.
458 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000459 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000460 else
461 Inputs.push_back(std::make_pair(Ty, A));
462
463 } else if (A->getOption().isLinkerInput()) {
464 // Just treat as object type, we could make a special type for
465 // this if necessary.
466 Inputs.push_back(std::make_pair(types::TY_Object, A));
467
468 } else if (A->getOption().getId() == options::OPT_x) {
469 InputTypeArg = A;
470 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
471
472 // Follow gcc behavior and treat as linker input for invalid -x
473 // options. Its not clear why we shouldn't just revert to
474 // unknown; but this isn't very important, we might as well be
475 // bug comatible.
476 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000477 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000478 InputType = types::TY_Object;
479 }
480 }
481 }
482
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000483 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000484 Diag(clang::diag::err_drv_no_input_files);
485 return;
486 }
487
488 // Determine which compilation mode we are in. We look for options
489 // which affect the phase, starting with the earliest phases, and
490 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000491 Arg *FinalPhaseArg = 0;
492 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000493
494 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000495 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
496 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
497 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
498 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000499
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000500 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
501 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
502 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
503 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_llvm)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000504 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
505 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000506
507 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000508 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
509 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000510
511 // Otherwise do everything.
512 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000513 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000514
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000515 // Reject -Z* at the top level, these options should never have been
516 // exposed by gcc.
517 if (Arg *A = Args.getLastArg(options::OPT_Z))
518 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
519
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000520 // Construct the actions to perform.
521 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000522 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000523 types::ID InputType = Inputs[i].first;
524 const Arg *InputArg = Inputs[i].second;
525
526 unsigned NumSteps = types::getNumCompilationPhases(InputType);
527 assert(NumSteps && "Invalid number of steps!");
528
529 // If the first step comes after the final phase we are doing as
530 // part of this compilation, warn the user about it.
531 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
532 if (InitialPhase > FinalPhase) {
Daniel Dunbar05494a72009-03-19 07:57:08 +0000533 // Claim here to avoid the more general unused warning.
534 InputArg->claim();
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000535 Diag(clang::diag::warn_drv_input_file_unused)
536 << InputArg->getValue(Args)
537 << getPhaseName(InitialPhase)
538 << FinalPhaseArg->getOption().getName();
539 continue;
540 }
541
542 // Build the pipeline for this file.
543 Action *Current = new InputAction(*InputArg, InputType);
544 for (unsigned i = 0; i != NumSteps; ++i) {
545 phases::ID Phase = types::getCompilationPhase(InputType, i);
546
547 // We are done if this step is past what the user requested.
548 if (Phase > FinalPhase)
549 break;
550
551 // Queue linker inputs.
552 if (Phase == phases::Link) {
553 assert(i + 1 == NumSteps && "linking must be final compilation step.");
554 LinkerInputs.push_back(Current);
555 Current = 0;
556 break;
557 }
558
559 // Otherwise construct the appropriate action.
560 Current = ConstructPhaseAction(Args, Phase, Current);
561 if (Current->getType() == types::TY_Nothing)
562 break;
563 }
564
565 // If we ended with something, add to the output list.
566 if (Current)
567 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000568 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000569
570 // Add a link action if necessary.
571 if (!LinkerInputs.empty())
572 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
573}
574
575Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
576 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000577 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000578 // Build the appropriate action.
579 switch (Phase) {
580 case phases::Link: assert(0 && "link action invalid here.");
581 case phases::Preprocess: {
582 types::ID OutputTy = types::getPreprocessedType(Input->getType());
583 assert(OutputTy != types::TY_INVALID &&
584 "Cannot preprocess this input type!");
585 return new PreprocessJobAction(Input, OutputTy);
586 }
587 case phases::Precompile:
588 return new PrecompileJobAction(Input, types::TY_PCH);
589 case phases::Compile: {
590 if (Args.hasArg(options::OPT_fsyntax_only)) {
591 return new CompileJobAction(Input, types::TY_Nothing);
592 } else if (Args.hasArg(options::OPT__analyze)) {
593 return new AnalyzeJobAction(Input, types::TY_Plist);
594 } else if (Args.hasArg(options::OPT_emit_llvm)) {
595 types::ID Output =
596 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
597 return new CompileJobAction(Input, Output);
598 } else {
599 return new CompileJobAction(Input, types::TY_PP_Asm);
600 }
601 }
602 case phases::Assemble:
603 return new AssembleJobAction(Input, types::TY_Object);
604 }
605
606 assert(0 && "invalid phase in ConstructPhaseAction");
607 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000608}
609
Daniel Dunbar21549232009-03-18 02:55:38 +0000610void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000611 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000612 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
613 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000614
615 // FIXME: Pipes are forcibly disabled until we support executing
616 // them.
617 if (!CCCPrintBindings)
618 UsePipes = false;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000619
620 // -save-temps inhibits pipes.
621 if (SaveTemps && UsePipes) {
622 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
623 UsePipes = true;
624 }
625
626 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
627
628 // It is an error to provide a -o option if we are making multiple
629 // output files.
630 if (FinalOutput) {
631 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000632 for (ActionList::const_iterator it = C.getActions().begin(),
633 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000634 if ((*it)->getType() != types::TY_Nothing)
635 ++NumOutputs;
636
637 if (NumOutputs > 1) {
638 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
639 FinalOutput = 0;
640 }
641 }
642
Daniel Dunbar21549232009-03-18 02:55:38 +0000643 for (ActionList::const_iterator it = C.getActions().begin(),
644 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000645 Action *A = *it;
646
647 // If we are linking an image for multiple archs then the linker
648 // wants -arch_multiple and -final_output <final image
649 // name>. Unfortunately, this doesn't fit in cleanly because we
650 // have to pass this information down.
651 //
652 // FIXME: This is a hack; find a cleaner way to integrate this
653 // into the process.
654 const char *LinkingOutput = 0;
655 if (isa<LinkJobAction>(A)) {
656 if (FinalOutput)
657 LinkingOutput = FinalOutput->getValue(C.getArgs());
658 else
659 LinkingOutput = DefaultImageName.c_str();
660 }
661
662 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000663 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000664 /*CanAcceptPipe*/ true,
665 /*AtTopLevel*/ true,
666 /*LinkingOutput*/ LinkingOutput,
667 II);
668 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000669
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000670 // If there were errors, don't warn about any unused arguments.
671 if (Diags.getNumErrors())
672 return;
673
Daniel Dunbar586dc232009-03-16 06:42:30 +0000674 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
675 it != ie; ++it) {
676 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000677
Daniel Dunbar586dc232009-03-16 06:42:30 +0000678 // FIXME: It would be nice to be able to send the argument to the
679 // Diagnostic, so that extra values, position, and so on could be
680 // printed.
681 if (!A->isClaimed())
682 Diag(clang::diag::warn_drv_unused_argument)
683 << A->getOption().getName();
684 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000685}
686
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000687void Driver::BuildJobsForAction(Compilation &C,
688 const Action *A,
689 const ToolChain *TC,
690 bool CanAcceptPipe,
691 bool AtTopLevel,
692 const char *LinkingOutput,
693 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000694 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000695
696 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
697 // FIXME: Pipes are forcibly disabled until we support executing
698 // them.
699 if (!CCCPrintBindings)
700 UsePipes = false;
701
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000702 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar115a7922009-03-19 07:29:38 +0000703 // FIXME: It would be nice to not claim this here; maybe the old
704 // scheme of just using Args was better?
705 const Arg &Input = IA->getInputArg();
706 Input.claim();
707 if (isa<PositionalArg>(Input)) {
708 const char *Name = Input.getValue(C.getArgs());
709 Result = InputInfo(Name, A->getType(), Name);
710 } else
711 Result = InputInfo(&Input, A->getType(), "");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000712 return;
713 }
714
715 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
716 const char *ArchName = BAA->getArchName();
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000717 if (!ArchName)
718 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000719 BuildJobsForAction(C,
720 *BAA->begin(),
721 Host->getToolChain(C.getArgs(), ArchName),
722 CanAcceptPipe,
723 AtTopLevel,
724 LinkingOutput,
725 Result);
726 return;
727 }
728
729 const JobAction *JA = cast<JobAction>(A);
730 const Tool &T = TC->SelectTool(C, *JA);
731
732 // See if we should use an integrated preprocessor. We do so when we
733 // have exactly one input, since this is the only use case we care
734 // about (irrelevant since we don't support combine yet).
735 bool UseIntegratedCPP = false;
736 const ActionList *Inputs = &A->getInputs();
737 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
738 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
739 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
740 !C.getArgs().hasArg(options::OPT_save_temps) &&
741 T.hasIntegratedCPP()) {
742 UseIntegratedCPP = true;
743 Inputs = &(*Inputs)[0]->getInputs();
744 }
745 }
746
747 // Only use pipes when there is exactly one input.
748 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000749 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000750 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
751 it != ie; ++it) {
752 InputInfo II;
753 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
754 /*AtTopLevel*/false,
755 LinkingOutput,
756 II);
757 InputInfos.push_back(II);
758 }
759
760 // Determine if we should output to a pipe.
761 bool OutputToPipe = false;
762 if (CanAcceptPipe && T.canPipeOutput()) {
763 // Some actions default to writing to a pipe if they are the top
764 // level phase and there was no user override.
765 //
766 // FIXME: Is there a better way to handle this?
767 if (AtTopLevel) {
768 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
769 OutputToPipe = true;
Daniel Dunbar60ccc762009-03-18 23:18:19 +0000770 } else if (UsePipes)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000771 OutputToPipe = true;
772 }
773
774 // Figure out where to put the job (pipes).
775 Job *Dest = &C.getJobs();
776 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +0000777 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000778 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
779 Dest = &InputInfos[0].getPipe();
780 }
781
782 // Always use the first input as the base input.
783 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +0000784
785 // Determine the place to write output to (nothing, pipe, or
786 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +0000787 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000788 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000789 } else if (OutputToPipe) {
790 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000791 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
792 if (!PJ) {
793 PJ = new PipedJob();
794 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000795 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +0000796 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000797 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000798 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000799 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
800 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000801 }
802
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000803 if (CCCPrintBindings) {
804 llvm::errs() << "bind - \"" << T.getName() << "\", inputs: [";
805 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
806 llvm::errs() << InputInfos[i].getAsString();
807 if (i + 1 != e)
808 llvm::errs() << ", ";
809 }
810 llvm::errs() << "], output: " << Result.getAsString() << "\n";
811 } else {
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000812 const ArgList &TCArgs = C.getArgsForToolChain(TC);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000813 T.ConstructJob(C, *JA, *Dest, Result, InputInfos, TCArgs, LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000814 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000815}
816
Daniel Dunbar441d0602009-03-17 17:53:55 +0000817const char *Driver::GetNamedOutputPath(Compilation &C,
818 const JobAction &JA,
819 const char *BaseInput,
820 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000821 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +0000822 // Output to a user requested destination?
823 if (AtTopLevel) {
824 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
825 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
826 }
827
828 // Output to a temporary file?
829 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +0000830 std::string TmpName =
831 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
832 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +0000833 }
834
835 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +0000836 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +0000837
838 // Determine what the derived output name should be.
839 const char *NamedOutput;
840 if (JA.getType() == types::TY_Image) {
841 NamedOutput = DefaultImageName.c_str();
842 } else {
843 const char *Suffix = types::getTypeTempSuffix(JA.getType());
844 assert(Suffix && "All types used for output should have a suffix.");
845
846 std::string::size_type End = std::string::npos;
847 if (!types::appendSuffixForType(JA.getType()))
848 End = BaseName.rfind('.');
849 std::string Suffixed(BaseName.substr(0, End));
850 Suffixed += '.';
851 Suffixed += Suffix;
852 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
853 }
854
855 // As an annoying special case, PCH generation doesn't strip the
856 // pathname.
857 if (JA.getType() == types::TY_PCH) {
858 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +0000859 if (BasePath.isEmpty())
860 BasePath = NamedOutput;
861 else
862 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000863 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
864 } else {
865 return C.addResultFile(NamedOutput);
866 }
867}
868
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000869llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000870 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000871 const ToolChain::path_list &List = TC.getFilePaths();
872 for (ToolChain::path_list::const_iterator
873 it = List.begin(), ie = List.end(); it != ie; ++it) {
874 llvm::sys::Path P(*it);
875 P.appendComponent(Name);
876 if (P.exists())
877 return P;
878 }
879
Daniel Dunbarcb881672009-03-13 00:51:18 +0000880 return llvm::sys::Path(Name);
881}
882
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000883llvm::sys::Path Driver::GetProgramPath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000884 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000885 const ToolChain::path_list &List = TC.getProgramPaths();
886 for (ToolChain::path_list::const_iterator
887 it = List.begin(), ie = List.end(); it != ie; ++it) {
888 llvm::sys::Path P(*it);
889 P.appendComponent(Name);
890 if (P.exists())
891 return P;
892 }
893
894 // As a last resort, always search in our directory before pulling
895 // from the path.
896 llvm::sys::Path P(Dir);
897 P.appendComponent(Name);
898 if (P.exists())
899 return P;
900
Daniel Dunbar632f50e2009-03-18 21:34:08 +0000901 // Search path to increase accuracy of logging output.
902 P = llvm::sys::Program::FindProgramByName(Name);
903 if (!P.empty())
904 return P;
905
Daniel Dunbarcb881672009-03-13 00:51:18 +0000906 return llvm::sys::Path(Name);
907}
908
Daniel Dunbar214399e2009-03-18 19:34:39 +0000909std::string Driver::GetTemporaryPath(const char *Suffix) const {
910 // FIXME: This is lame; sys::Path should provide this function (in
911 // particular, it should know how to find the temporary files dir).
912 std::string Error;
913 llvm::sys::Path P("/tmp/cc");
914 if (P.makeUnique(false, &Error)) {
915 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
916 return "";
917 }
918
Daniel Dunbar84603bc2009-03-18 23:08:52 +0000919 // FIXME: Grumble, makeUnique sometimes leaves the file around!?
920 // PR3837.
921 P.eraseFromDisk(false, 0);
922
Daniel Dunbar214399e2009-03-18 19:34:39 +0000923 P.appendSuffix(Suffix);
924 return P.toString();
925}
926
Daniel Dunbare5049522009-03-17 20:45:45 +0000927const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000928 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000929 // Dice into arch, platform, and OS. This matches
930 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
931 // and missing fields are left empty.
932 std::string Arch, Platform, OS;
933
934 if (const char *ArchEnd = strchr(Triple, '-')) {
935 Arch = std::string(Triple, ArchEnd);
936
937 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
938 Platform = std::string(ArchEnd+1, PlatformEnd);
939 OS = PlatformEnd+1;
940 } else
941 Platform = ArchEnd+1;
942 } else
943 Arch = Triple;
944
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000945 // Normalize Arch a bit.
946 //
947 // FIXME: This is very incomplete.
948 if (Arch == "i686")
949 Arch = "i386";
950 else if (Arch == "amd64")
951 Arch = "x86_64";
Daniel Dunbarc811b6c2009-03-18 04:41:46 +0000952 else if (Arch == "powerpc" || Arch == "Power Macintosh")
953 Arch = "ppc";
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000954
Daniel Dunbara88162c2009-03-13 12:23:29 +0000955 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbare5049522009-03-17 20:45:45 +0000956 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
957 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000958
Daniel Dunbare5049522009-03-17 20:45:45 +0000959 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
960 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000961}