blob: e9e751178da0469e65f3df79b81a15c3f3d2aa78 [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 Dunbar13689542009-03-13 20:33:35 +0000331 if (ArchNames.insert(Name))
332 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000333 }
334 }
335
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000336 // When there is no explicit arch for this platform, make sure we
337 // still bind the architecture (to the default) so that -Xarch_ is
338 // handled correctly.
339 if (!Archs.size())
340 Archs.push_back(0);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000341
342 // FIXME: We killed off some others but these aren't yet detected in
343 // a functional manner. If we added information to jobs about which
344 // "auxiliary" files they wrote then we could detect the conflict
345 // these cause downstream.
346 if (Archs.size() > 1) {
347 // No recovery needed, the point of this is just to prevent
348 // overwriting the same files.
349 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
350 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
351 << A->getOption().getName();
352 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
353 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
354 << A->getOption().getName();
355 }
356
357 ActionList SingleActions;
358 BuildActions(Args, SingleActions);
359
360 // Add in arch binding and lipo (if necessary) for every top level
361 // action.
362 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
363 Action *Act = SingleActions[i];
364
365 // Make sure we can lipo this kind of output. If not (and it is an
366 // actual output) then we disallow, since we can't create an
367 // output file with the right name without overwriting it. We
368 // could remove this oddity by just changing the output names to
369 // include the arch, which would also fix
370 // -save-temps. Compatibility wins for now.
371
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000372 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000373 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
374 << types::getTypeName(Act->getType());
375
376 ActionList Inputs;
Daniel Dunbar13689542009-03-13 20:33:35 +0000377 for (unsigned i = 0, e = Archs.size(); i != e; ++i )
378 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000379
380 // Lipo if necessary, We do it this way because we need to set the
381 // arch flag so that -Xarch_ gets overwritten.
382 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
383 Actions.append(Inputs.begin(), Inputs.end());
384 else
385 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
386 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000387}
388
Daniel Dunbar21549232009-03-18 02:55:38 +0000389void Driver::BuildActions(const ArgList &Args, ActionList &Actions) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000390 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000391 // Start by constructing the list of inputs and their types.
392
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000393 // Track the current user specified (-x) input. We also explicitly
394 // track the argument used to set the type; we only want to claim
395 // the type when we actually use it, so we warn about unused -x
396 // arguments.
397 types::ID InputType = types::TY_Nothing;
398 Arg *InputTypeArg = 0;
399
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000400 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
401 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
402 it != ie; ++it) {
403 Arg *A = *it;
404
405 if (isa<InputOption>(A->getOption())) {
406 const char *Value = A->getValue(Args);
407 types::ID Ty = types::TY_INVALID;
408
409 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000410 if (InputType == types::TY_Nothing) {
411 // If there was an explicit arg for this, claim it.
412 if (InputTypeArg)
413 InputTypeArg->claim();
414
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000415 // stdin must be handled specially.
416 if (memcmp(Value, "-", 2) == 0) {
417 // If running with -E, treat as a C input (this changes the
418 // builtin macros, for example). This may be overridden by
419 // -ObjC below.
420 //
421 // Otherwise emit an error but still use a valid type to
422 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000423 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000424 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000425 Ty = types::TY_C;
426 } else {
427 // Otherwise lookup by extension, and fallback to ObjectType
428 // if not found.
429 if (const char *Ext = strrchr(Value, '.'))
430 Ty = types::lookupTypeForExtension(Ext + 1);
431 if (Ty == types::TY_INVALID)
432 Ty = types::TY_Object;
433 }
434
435 // -ObjC and -ObjC++ override the default language, but only
436 // -for "source files". We just treat everything that isn't a
437 // -linker input as a source file.
438 //
439 // FIXME: Clean this up if we move the phase sequence into the
440 // type.
441 if (Ty != types::TY_Object) {
442 if (Args.hasArg(options::OPT_ObjC))
443 Ty = types::TY_ObjC;
444 else if (Args.hasArg(options::OPT_ObjCXX))
445 Ty = types::TY_ObjCXX;
446 }
447 } else {
448 assert(InputTypeArg && "InputType set w/o InputTypeArg");
449 InputTypeArg->claim();
450 Ty = InputType;
451 }
452
453 // Check that the file exists. It isn't clear this is worth
454 // doing, since the tool presumably does this anyway, and this
455 // just adds an extra stat to the equation, but this is gcc
456 // compatible.
457 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000458 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000459 else
460 Inputs.push_back(std::make_pair(Ty, A));
461
462 } else if (A->getOption().isLinkerInput()) {
463 // Just treat as object type, we could make a special type for
464 // this if necessary.
465 Inputs.push_back(std::make_pair(types::TY_Object, A));
466
467 } else if (A->getOption().getId() == options::OPT_x) {
468 InputTypeArg = A;
469 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
470
471 // Follow gcc behavior and treat as linker input for invalid -x
472 // options. Its not clear why we shouldn't just revert to
473 // unknown; but this isn't very important, we might as well be
474 // bug comatible.
475 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000476 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000477 InputType = types::TY_Object;
478 }
479 }
480 }
481
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000482 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000483 Diag(clang::diag::err_drv_no_input_files);
484 return;
485 }
486
487 // Determine which compilation mode we are in. We look for options
488 // which affect the phase, starting with the earliest phases, and
489 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000490 Arg *FinalPhaseArg = 0;
491 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000492
493 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000494 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
495 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
496 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
497 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000498
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000499 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
500 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
501 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
502 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_llvm)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000503 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
504 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000505
506 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000507 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
508 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000509
510 // Otherwise do everything.
511 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000512 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000513
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000514 // Reject -Z* at the top level, these options should never have been
515 // exposed by gcc.
516 if (Arg *A = Args.getLastArg(options::OPT_Z))
517 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
518
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000519 // Construct the actions to perform.
520 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000521 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000522 types::ID InputType = Inputs[i].first;
523 const Arg *InputArg = Inputs[i].second;
524
525 unsigned NumSteps = types::getNumCompilationPhases(InputType);
526 assert(NumSteps && "Invalid number of steps!");
527
528 // If the first step comes after the final phase we are doing as
529 // part of this compilation, warn the user about it.
530 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
531 if (InitialPhase > FinalPhase) {
532 Diag(clang::diag::warn_drv_input_file_unused)
533 << InputArg->getValue(Args)
534 << getPhaseName(InitialPhase)
535 << FinalPhaseArg->getOption().getName();
536 continue;
537 }
538
539 // Build the pipeline for this file.
540 Action *Current = new InputAction(*InputArg, InputType);
541 for (unsigned i = 0; i != NumSteps; ++i) {
542 phases::ID Phase = types::getCompilationPhase(InputType, i);
543
544 // We are done if this step is past what the user requested.
545 if (Phase > FinalPhase)
546 break;
547
548 // Queue linker inputs.
549 if (Phase == phases::Link) {
550 assert(i + 1 == NumSteps && "linking must be final compilation step.");
551 LinkerInputs.push_back(Current);
552 Current = 0;
553 break;
554 }
555
556 // Otherwise construct the appropriate action.
557 Current = ConstructPhaseAction(Args, Phase, Current);
558 if (Current->getType() == types::TY_Nothing)
559 break;
560 }
561
562 // If we ended with something, add to the output list.
563 if (Current)
564 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000565 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000566
567 // Add a link action if necessary.
568 if (!LinkerInputs.empty())
569 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
570}
571
572Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
573 Action *Input) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000574 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000575 // Build the appropriate action.
576 switch (Phase) {
577 case phases::Link: assert(0 && "link action invalid here.");
578 case phases::Preprocess: {
579 types::ID OutputTy = types::getPreprocessedType(Input->getType());
580 assert(OutputTy != types::TY_INVALID &&
581 "Cannot preprocess this input type!");
582 return new PreprocessJobAction(Input, OutputTy);
583 }
584 case phases::Precompile:
585 return new PrecompileJobAction(Input, types::TY_PCH);
586 case phases::Compile: {
587 if (Args.hasArg(options::OPT_fsyntax_only)) {
588 return new CompileJobAction(Input, types::TY_Nothing);
589 } else if (Args.hasArg(options::OPT__analyze)) {
590 return new AnalyzeJobAction(Input, types::TY_Plist);
591 } else if (Args.hasArg(options::OPT_emit_llvm)) {
592 types::ID Output =
593 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
594 return new CompileJobAction(Input, Output);
595 } else {
596 return new CompileJobAction(Input, types::TY_PP_Asm);
597 }
598 }
599 case phases::Assemble:
600 return new AssembleJobAction(Input, types::TY_Object);
601 }
602
603 assert(0 && "invalid phase in ConstructPhaseAction");
604 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000605}
606
Daniel Dunbar21549232009-03-18 02:55:38 +0000607void Driver::BuildJobs(Compilation &C) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000608 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000609 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
610 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
611
612 // -save-temps inhibits pipes.
613 if (SaveTemps && UsePipes) {
614 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
615 UsePipes = true;
616 }
617
618 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
619
620 // It is an error to provide a -o option if we are making multiple
621 // output files.
622 if (FinalOutput) {
623 unsigned NumOutputs = 0;
Daniel Dunbar21549232009-03-18 02:55:38 +0000624 for (ActionList::const_iterator it = C.getActions().begin(),
625 ie = C.getActions().end(); it != ie; ++it)
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000626 if ((*it)->getType() != types::TY_Nothing)
627 ++NumOutputs;
628
629 if (NumOutputs > 1) {
630 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
631 FinalOutput = 0;
632 }
633 }
634
Daniel Dunbar21549232009-03-18 02:55:38 +0000635 for (ActionList::const_iterator it = C.getActions().begin(),
636 ie = C.getActions().end(); it != ie; ++it) {
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000637 Action *A = *it;
638
639 // If we are linking an image for multiple archs then the linker
640 // wants -arch_multiple and -final_output <final image
641 // name>. Unfortunately, this doesn't fit in cleanly because we
642 // have to pass this information down.
643 //
644 // FIXME: This is a hack; find a cleaner way to integrate this
645 // into the process.
646 const char *LinkingOutput = 0;
647 if (isa<LinkJobAction>(A)) {
648 if (FinalOutput)
649 LinkingOutput = FinalOutput->getValue(C.getArgs());
650 else
651 LinkingOutput = DefaultImageName.c_str();
652 }
653
654 InputInfo II;
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000655 BuildJobsForAction(C, A, &C.getDefaultToolChain(),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000656 /*CanAcceptPipe*/ true,
657 /*AtTopLevel*/ true,
658 /*LinkingOutput*/ LinkingOutput,
659 II);
660 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000661
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000662 // If there were errors, don't warn about any unused arguments.
663 if (Diags.getNumErrors())
664 return;
665
Daniel Dunbar586dc232009-03-16 06:42:30 +0000666 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
667 it != ie; ++it) {
668 Arg *A = *it;
Daniel Dunbaraf2e4ba2009-03-18 18:03:46 +0000669
Daniel Dunbar586dc232009-03-16 06:42:30 +0000670 // FIXME: It would be nice to be able to send the argument to the
671 // Diagnostic, so that extra values, position, and so on could be
672 // printed.
673 if (!A->isClaimed())
674 Diag(clang::diag::warn_drv_unused_argument)
675 << A->getOption().getName();
676 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000677}
678
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000679void Driver::BuildJobsForAction(Compilation &C,
680 const Action *A,
681 const ToolChain *TC,
682 bool CanAcceptPipe,
683 bool AtTopLevel,
684 const char *LinkingOutput,
685 InputInfo &Result) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000686 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs for action");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000687 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbard09986a2009-03-18 08:02:40 +0000688 // FIXME: This is broken, linker inputs won't work here.
689 assert(isa<PositionalArg>(IA->getInputArg()) && "FIXME: Linker inputs");
690
Daniel Dunbar5ab483b2009-03-18 06:21:12 +0000691 IA->getInputArg().claim();
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000692 const char *Name = IA->getInputArg().getValue(C.getArgs());
693 Result = InputInfo(Name, A->getType(), Name);
694 return;
695 }
696
697 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
698 const char *ArchName = BAA->getArchName();
Daniel Dunbar10ffa9a2009-03-18 03:13:20 +0000699 if (!ArchName)
700 ArchName = C.getDefaultToolChain().getArchName().c_str();
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000701 BuildJobsForAction(C,
702 *BAA->begin(),
703 Host->getToolChain(C.getArgs(), ArchName),
704 CanAcceptPipe,
705 AtTopLevel,
706 LinkingOutput,
707 Result);
708 return;
709 }
710
711 const JobAction *JA = cast<JobAction>(A);
712 const Tool &T = TC->SelectTool(C, *JA);
713
714 // See if we should use an integrated preprocessor. We do so when we
715 // have exactly one input, since this is the only use case we care
716 // about (irrelevant since we don't support combine yet).
717 bool UseIntegratedCPP = false;
718 const ActionList *Inputs = &A->getInputs();
719 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
720 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
721 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
722 !C.getArgs().hasArg(options::OPT_save_temps) &&
723 T.hasIntegratedCPP()) {
724 UseIntegratedCPP = true;
725 Inputs = &(*Inputs)[0]->getInputs();
726 }
727 }
728
729 // Only use pipes when there is exactly one input.
730 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000731 InputInfoList InputInfos;
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000732 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
733 it != ie; ++it) {
734 InputInfo II;
735 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
736 /*AtTopLevel*/false,
737 LinkingOutput,
738 II);
739 InputInfos.push_back(II);
740 }
741
742 // Determine if we should output to a pipe.
743 bool OutputToPipe = false;
744 if (CanAcceptPipe && T.canPipeOutput()) {
745 // Some actions default to writing to a pipe if they are the top
746 // level phase and there was no user override.
747 //
748 // FIXME: Is there a better way to handle this?
749 if (AtTopLevel) {
750 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
751 OutputToPipe = true;
752 } else if (C.getArgs().hasArg(options::OPT_pipe))
753 OutputToPipe = true;
754 }
755
756 // Figure out where to put the job (pipes).
757 Job *Dest = &C.getJobs();
758 if (InputInfos[0].isPipe()) {
Daniel Dunbar441d0602009-03-17 17:53:55 +0000759 assert(TryToUsePipeInput && "Unrequested pipe!");
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000760 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
761 Dest = &InputInfos[0].getPipe();
762 }
763
764 // Always use the first input as the base input.
765 const char *BaseInput = InputInfos[0].getBaseInput();
Daniel Dunbar441d0602009-03-17 17:53:55 +0000766
767 // Determine the place to write output to (nothing, pipe, or
768 // filename) and where to put the new job.
Daniel Dunbar441d0602009-03-17 17:53:55 +0000769 if (JA->getType() == types::TY_Nothing) {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000770 Result = InputInfo(A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000771 } else if (OutputToPipe) {
772 // Append to current piped job or create a new one as appropriate.
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000773 PipedJob *PJ = dyn_cast<PipedJob>(Dest);
774 if (!PJ) {
775 PJ = new PipedJob();
776 cast<JobList>(Dest)->addJob(PJ);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000777 Dest = PJ;
Daniel Dunbar441d0602009-03-17 17:53:55 +0000778 }
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000779 Result = InputInfo(PJ, A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000780 } else {
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000781 Result = InputInfo(GetNamedOutputPath(C, *JA, BaseInput, AtTopLevel),
782 A->getType(), BaseInput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000783 }
784
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000785 if (CCCPrintBindings) {
786 llvm::errs() << "bind - \"" << T.getName() << "\", inputs: [";
787 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
788 llvm::errs() << InputInfos[i].getAsString();
789 if (i + 1 != e)
790 llvm::errs() << ", ";
791 }
792 llvm::errs() << "], output: " << Result.getAsString() << "\n";
793 } else {
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000794 const ArgList &TCArgs = C.getArgsForToolChain(TC);
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000795 T.ConstructJob(C, *JA, *Dest, Result, InputInfos, TCArgs, LinkingOutput);
Daniel Dunbar5c3c1d72009-03-17 22:47:06 +0000796 }
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000797}
798
Daniel Dunbar441d0602009-03-17 17:53:55 +0000799const char *Driver::GetNamedOutputPath(Compilation &C,
800 const JobAction &JA,
801 const char *BaseInput,
802 bool AtTopLevel) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000803 llvm::PrettyStackTraceString CrashInfo("Computing output path");
Daniel Dunbar441d0602009-03-17 17:53:55 +0000804 // Output to a user requested destination?
805 if (AtTopLevel) {
806 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
807 return C.addResultFile(FinalOutput->getValue(C.getArgs()));
808 }
809
810 // Output to a temporary file?
811 if (!AtTopLevel && !C.getArgs().hasArg(options::OPT_save_temps)) {
Daniel Dunbar214399e2009-03-18 19:34:39 +0000812 std::string TmpName =
813 GetTemporaryPath(types::getTypeTempSuffix(JA.getType()));
814 return C.addTempFile(C.getArgs().MakeArgString(TmpName.c_str()));
Daniel Dunbar441d0602009-03-17 17:53:55 +0000815 }
816
817 llvm::sys::Path BasePath(BaseInput);
Daniel Dunbar5796bf42009-03-18 02:00:31 +0000818 std::string BaseName(BasePath.getLast());
Daniel Dunbar441d0602009-03-17 17:53:55 +0000819
820 // Determine what the derived output name should be.
821 const char *NamedOutput;
822 if (JA.getType() == types::TY_Image) {
823 NamedOutput = DefaultImageName.c_str();
824 } else {
825 const char *Suffix = types::getTypeTempSuffix(JA.getType());
826 assert(Suffix && "All types used for output should have a suffix.");
827
828 std::string::size_type End = std::string::npos;
829 if (!types::appendSuffixForType(JA.getType()))
830 End = BaseName.rfind('.');
831 std::string Suffixed(BaseName.substr(0, End));
832 Suffixed += '.';
833 Suffixed += Suffix;
834 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
835 }
836
837 // As an annoying special case, PCH generation doesn't strip the
838 // pathname.
839 if (JA.getType() == types::TY_PCH) {
840 BasePath.eraseComponent();
Daniel Dunbar56c55942009-03-18 09:58:30 +0000841 if (BasePath.isEmpty())
842 BasePath = NamedOutput;
843 else
844 BasePath.appendComponent(NamedOutput);
Daniel Dunbar441d0602009-03-17 17:53:55 +0000845 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
846 } else {
847 return C.addResultFile(NamedOutput);
848 }
849}
850
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000851llvm::sys::Path Driver::GetFilePath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000852 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000853 const ToolChain::path_list &List = TC.getFilePaths();
854 for (ToolChain::path_list::const_iterator
855 it = List.begin(), ie = List.end(); it != ie; ++it) {
856 llvm::sys::Path P(*it);
857 P.appendComponent(Name);
858 if (P.exists())
859 return P;
860 }
861
Daniel Dunbarcb881672009-03-13 00:51:18 +0000862 return llvm::sys::Path(Name);
863}
864
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000865llvm::sys::Path Driver::GetProgramPath(const char *Name,
Daniel Dunbar21549232009-03-18 02:55:38 +0000866 const ToolChain &TC) const {
Daniel Dunbar0edefeb2009-03-18 20:26:19 +0000867 const ToolChain::path_list &List = TC.getProgramPaths();
868 for (ToolChain::path_list::const_iterator
869 it = List.begin(), ie = List.end(); it != ie; ++it) {
870 llvm::sys::Path P(*it);
871 P.appendComponent(Name);
872 if (P.exists())
873 return P;
874 }
875
876 // As a last resort, always search in our directory before pulling
877 // from the path.
878 llvm::sys::Path P(Dir);
879 P.appendComponent(Name);
880 if (P.exists())
881 return P;
882
Daniel Dunbar632f50e2009-03-18 21:34:08 +0000883 // Search path to increase accuracy of logging output.
884 P = llvm::sys::Program::FindProgramByName(Name);
885 if (!P.empty())
886 return P;
887
Daniel Dunbarcb881672009-03-13 00:51:18 +0000888 return llvm::sys::Path(Name);
889}
890
Daniel Dunbar214399e2009-03-18 19:34:39 +0000891std::string Driver::GetTemporaryPath(const char *Suffix) const {
892 // FIXME: This is lame; sys::Path should provide this function (in
893 // particular, it should know how to find the temporary files dir).
894 std::string Error;
895 llvm::sys::Path P("/tmp/cc");
896 if (P.makeUnique(false, &Error)) {
897 Diag(clang::diag::err_drv_unable_to_make_temp) << Error;
898 return "";
899 }
900
901 P.appendSuffix(Suffix);
902 return P.toString();
903}
904
Daniel Dunbare5049522009-03-17 20:45:45 +0000905const HostInfo *Driver::GetHostInfo(const char *Triple) const {
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000906 llvm::PrettyStackTraceString CrashInfo("Constructing host");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000907 // Dice into arch, platform, and OS. This matches
908 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
909 // and missing fields are left empty.
910 std::string Arch, Platform, OS;
911
912 if (const char *ArchEnd = strchr(Triple, '-')) {
913 Arch = std::string(Triple, ArchEnd);
914
915 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
916 Platform = std::string(ArchEnd+1, PlatformEnd);
917 OS = PlatformEnd+1;
918 } else
919 Platform = ArchEnd+1;
920 } else
921 Arch = Triple;
922
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000923 // Normalize Arch a bit.
924 //
925 // FIXME: This is very incomplete.
926 if (Arch == "i686")
927 Arch = "i386";
928 else if (Arch == "amd64")
929 Arch = "x86_64";
Daniel Dunbarc811b6c2009-03-18 04:41:46 +0000930 else if (Arch == "powerpc" || Arch == "Power Macintosh")
931 Arch = "ppc";
Daniel Dunbar1fd6c4b2009-03-17 19:00:50 +0000932
Daniel Dunbara88162c2009-03-13 12:23:29 +0000933 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbare5049522009-03-17 20:45:45 +0000934 return createDarwinHostInfo(*this, Arch.c_str(), Platform.c_str(),
935 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000936
Daniel Dunbare5049522009-03-17 20:45:45 +0000937 return createUnknownHostInfo(*this, Arch.c_str(), Platform.c_str(),
938 OS.c_str());
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000939}