blob: ee8a649d1a501aa86fa3afd4972b80d106d965ce [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 Dunbar7dc2a042009-03-05 06:38:47 +000018#include "clang/Driver/Option.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000019#include "clang/Driver/Options.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000020#include "clang/Driver/Types.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000021
Daniel Dunbarb1873cd2009-03-13 20:33:35 +000022#include "llvm/ADT/StringSet.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000024#include "llvm/System/Path.h"
Daniel Dunbar494646b2009-03-13 12:19:02 +000025
26#include <map>
27
Daniel Dunbard6f0e372009-03-04 20:49:20 +000028using namespace clang::driver;
29
Daniel Dunbard25acaa2009-03-10 23:41:59 +000030Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000031 const char *_DefaultHostTriple,
32 Diagnostic &_Diags)
33 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000034 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
35 Host(0),
Daniel Dunbarb282ced2009-03-10 20:52:46 +000036 CCCIsCXX(false), CCCEcho(false),
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +000037 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false),
38 SuppressMissingInputWarning(false)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000039{
Daniel Dunbar63c4da92009-03-02 19:59:07 +000040}
41
42Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000043 delete Opts;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000044}
45
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000046ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
47 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
48
Daniel Dunbar85cb3592009-03-13 11:38:42 +000049 // FIXME: Handle '@' args (or at least error on them).
50
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000051 unsigned Index = 0, End = ArgEnd - ArgBegin;
52 while (Index < End) {
Daniel Dunbarb043ebd2009-03-13 01:01:44 +000053 // gcc's handling of empty arguments doesn't make
54 // sense, but this is not a common use case. :)
55 //
56 // We just ignore them here (note that other things may
57 // still take them as arguments).
58 if (Args->getArgString(Index)[0] == '\0') {
59 ++Index;
60 continue;
61 }
62
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000063 unsigned Prev = Index;
64 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000065 if (A) {
66 if (A->getOption().isUnsupported()) {
Daniel Dunbard724e332009-03-12 09:13:48 +000067 Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName();
Daniel Dunbardb62cc32009-03-12 07:58:46 +000068 continue;
69 }
70
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000071 Args->append(A);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000072 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000073
74 assert(Index > Prev && "Parser failed to consume argument.");
75 }
76
77 return Args;
78}
79
Daniel Dunbar63c4da92009-03-02 19:59:07 +000080Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarcc006892009-03-13 00:51:18 +000081 // FIXME: Handle environment options which effect driver behavior,
82 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
83 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
84
85 // FIXME: What are we going to do with -V and -b?
86
87 // FIXME: Handle CCC_ADD_ARGS.
88
Daniel Dunbarb282ced2009-03-10 20:52:46 +000089 // FIXME: This stuff needs to go into the Compilation, not the
90 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +000091 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000092
Daniel Dunbarb282ced2009-03-10 20:52:46 +000093 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +000094 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +000095
96 // Read -ccc args.
97 //
98 // FIXME: We need to figure out where this behavior should
99 // live. Most of it should be outside in the client; the parts that
100 // aren't should have proper options, either by introducing new ones
101 // or by overloading gcc ones like -V or -b.
102 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
103 const char *Opt = *Start + 5;
104
105 if (!strcmp(Opt, "print-options")) {
106 CCCPrintOptions = true;
107 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000108 CCCPrintActions = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000109 } else if (!strcmp(Opt, "cxx")) {
110 CCCIsCXX = true;
111 } else if (!strcmp(Opt, "echo")) {
112 CCCEcho = true;
113
114 } else if (!strcmp(Opt, "no-clang")) {
115 CCCNoClang = true;
116 } else if (!strcmp(Opt, "no-clang-cxx")) {
117 CCCNoClangCXX = true;
118 } else if (!strcmp(Opt, "no-clang-cpp")) {
119 CCCNoClangCPP = true;
120 } else if (!strcmp(Opt, "clang-archs")) {
121 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
122 const char *Cur = *++Start;
123
124 for (;;) {
125 const char *Next = strchr(Cur, ',');
126
127 if (Next) {
128 CCCClangArchs.insert(std::string(Cur, Next));
129 Cur = Next + 1;
130 } else {
131 CCCClangArchs.insert(std::string(Cur));
132 break;
133 }
134 }
135
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000136 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000137 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000138 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000139
140 } else {
141 // FIXME: Error handling.
142 llvm::errs() << "invalid option: " << *Start << "\n";
143 exit(1);
144 }
145 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000146
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000147 ArgList *Args = ParseArgStrings(Start, End);
148
Daniel Dunbarcc006892009-03-13 00:51:18 +0000149 Host = Driver::GetHostInfo(HostTriple);
150 DefaultToolChain = Host->getToolChain(*Args);
151
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000152 // FIXME: This behavior shouldn't be here.
153 if (CCCPrintOptions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000154 PrintOptions(*Args);
Daniel Dunbar88c9eae2009-03-13 17:24:34 +0000155 return 0;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000156 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000157
Daniel Dunbarcc006892009-03-13 00:51:18 +0000158 if (!HandleImmediateArgs(*Args))
159 return 0;
160
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000161 // Construct the list of abstract actions to perform for this
162 // compilation.
Daniel Dunbara790d372009-03-12 18:24:49 +0000163 ActionList Actions;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000164 if (Host->useDriverDriver())
165 BuildUniversalActions(*Args, Actions);
166 else
167 BuildActions(*Args, Actions);
168
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000169 if (CCCPrintActions) {
Daniel Dunbar494646b2009-03-13 12:19:02 +0000170 PrintActions(*Args, Actions);
Daniel Dunbar88c9eae2009-03-13 17:24:34 +0000171 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000172 }
Daniel Dunbar88c9eae2009-03-13 17:24:34 +0000173
174
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000175 assert(0 && "FIXME: Implement");
176
177 return new Compilation();
178}
179
Daniel Dunbara790d372009-03-12 18:24:49 +0000180void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000181 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000182 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000183 it != ie; ++it, ++i) {
184 Arg *A = *it;
185 llvm::errs() << "Option " << i << " - "
186 << "Name: \"" << A->getOption().getName() << "\", "
187 << "Values: {";
188 for (unsigned j = 0; j < A->getNumValues(); ++j) {
189 if (j)
190 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000191 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000192 }
193 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000194 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000195}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000196
Daniel Dunbarcc006892009-03-13 00:51:18 +0000197void Driver::PrintVersion() const {
198 // FIXME: Get a reasonable version number.
199
200 // FIXME: The following handlers should use a callback mechanism, we
201 // don't know what the client would like to do.
202 llvm::outs() << "ccc version 1.0" << "\n";
203}
204
205bool Driver::HandleImmediateArgs(const ArgList &Args) {
206 // The order these options are handled in in gcc is all over the
207 // place, but we don't expect inconsistencies w.r.t. that to matter
208 // in practice.
209 if (Args.hasArg(options::OPT_v) ||
210 Args.hasArg(options::OPT__HASH_HASH_HASH)) {
211 PrintVersion();
212 SuppressMissingInputWarning = true;
213 }
214
215 // FIXME: The following handlers should use a callback mechanism, we
216 // don't know what the client would like to do.
217 if (Arg *A = Args.getLastArg(options::OPT_print_file_name_EQ)) {
218 llvm::outs() << GetFilePath(A->getValue(Args)).toString() << "\n";
219 return false;
220 }
221
222 if (Arg *A = Args.getLastArg(options::OPT_print_prog_name_EQ)) {
223 llvm::outs() << GetProgramPath(A->getValue(Args)).toString() << "\n";
224 return false;
225 }
226
Daniel Dunbarb043ebd2009-03-13 01:01:44 +0000227 if (Args.hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000228 llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n";
229 return false;
230 }
231
232 return true;
233}
234
Daniel Dunbar494646b2009-03-13 12:19:02 +0000235static unsigned PrintActions1(const ArgList &Args,
236 Action *A,
237 std::map<Action*, unsigned> &Ids) {
238 if (Ids.count(A))
239 return Ids[A];
240
241 std::string str;
242 llvm::raw_string_ostream os(str);
243
244 os << Action::getClassName(A->getKind()) << ", ";
245 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000246 os << "\"" << IA->getInputArg().getValue(Args) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000247 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
248 os << "\"" << BIA->getArchName() << "\", "
249 << "{" << PrintActions1(Args, *BIA->begin(), Ids) << "}";
250 } else {
251 os << "{";
252 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
253 os << PrintActions1(Args, *it, Ids);
254 ++it;
255 if (it != ie)
256 os << ", ";
257 }
258 os << "}";
259 }
260
261 unsigned Id = Ids.size();
262 Ids[A] = Id;
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000263 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000264 << types::getTypeName(A->getType()) << "\n";
265
266 return Id;
267}
268
269void Driver::PrintActions(const ArgList &Args,
270 const ActionList &Actions) const {
271 std::map<Action*, unsigned> Ids;
272 for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000273 it != ie; ++it)
Daniel Dunbar494646b2009-03-13 12:19:02 +0000274 PrintActions1(Args, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000275}
276
Daniel Dunbara790d372009-03-12 18:24:49 +0000277void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000278 // Collect the list of architectures. Duplicates are allowed, but
279 // should only be handled once (in the order seen).
280 llvm::StringSet<> ArchNames;
281 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000282 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
283 it != ie; ++it) {
284 Arg *A = *it;
285
286 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000287 const char *Name = A->getValue(Args);
288
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000289 // FIXME: We need to handle canonicalization of the specified
290 // arch?
291
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000292 if (ArchNames.insert(Name))
293 Archs.push_back(Name);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000294 }
295 }
296
297 // When there is no explicit arch for this platform, get one from
298 // the host so that -Xarch_ is handled correctly.
299 if (!Archs.size()) {
300 const char *Arch = Host->getArchName().c_str();
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000301 Archs.push_back(Arch);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000302 }
303
304 // FIXME: We killed off some others but these aren't yet detected in
305 // a functional manner. If we added information to jobs about which
306 // "auxiliary" files they wrote then we could detect the conflict
307 // these cause downstream.
308 if (Archs.size() > 1) {
309 // No recovery needed, the point of this is just to prevent
310 // overwriting the same files.
311 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
312 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
313 << A->getOption().getName();
314 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
315 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
316 << A->getOption().getName();
317 }
318
319 ActionList SingleActions;
320 BuildActions(Args, SingleActions);
321
322 // Add in arch binding and lipo (if necessary) for every top level
323 // action.
324 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
325 Action *Act = SingleActions[i];
326
327 // Make sure we can lipo this kind of output. If not (and it is an
328 // actual output) then we disallow, since we can't create an
329 // output file with the right name without overwriting it. We
330 // could remove this oddity by just changing the output names to
331 // include the arch, which would also fix
332 // -save-temps. Compatibility wins for now.
333
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000334 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000335 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
336 << types::getTypeName(Act->getType());
337
338 ActionList Inputs;
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000339 for (unsigned i = 0, e = Archs.size(); i != e; ++i )
340 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000341
342 // Lipo if necessary, We do it this way because we need to set the
343 // arch flag so that -Xarch_ gets overwritten.
344 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
345 Actions.append(Inputs.begin(), Inputs.end());
346 else
347 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
348 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000349}
350
Daniel Dunbara790d372009-03-12 18:24:49 +0000351void Driver::BuildActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000352 // Start by constructing the list of inputs and their types.
353
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000354 // Track the current user specified (-x) input. We also explicitly
355 // track the argument used to set the type; we only want to claim
356 // the type when we actually use it, so we warn about unused -x
357 // arguments.
358 types::ID InputType = types::TY_Nothing;
359 Arg *InputTypeArg = 0;
360
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000361 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
362 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
363 it != ie; ++it) {
364 Arg *A = *it;
365
366 if (isa<InputOption>(A->getOption())) {
367 const char *Value = A->getValue(Args);
368 types::ID Ty = types::TY_INVALID;
369
370 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000371 if (InputType == types::TY_Nothing) {
372 // If there was an explicit arg for this, claim it.
373 if (InputTypeArg)
374 InputTypeArg->claim();
375
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000376 // stdin must be handled specially.
377 if (memcmp(Value, "-", 2) == 0) {
378 // If running with -E, treat as a C input (this changes the
379 // builtin macros, for example). This may be overridden by
380 // -ObjC below.
381 //
382 // Otherwise emit an error but still use a valid type to
383 // avoid spurious errors (e.g., no inputs).
384 if (!Args.hasArg(options::OPT_E))
Daniel Dunbard724e332009-03-12 09:13:48 +0000385 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000386 Ty = types::TY_C;
387 } else {
388 // Otherwise lookup by extension, and fallback to ObjectType
389 // if not found.
390 if (const char *Ext = strrchr(Value, '.'))
391 Ty = types::lookupTypeForExtension(Ext + 1);
392 if (Ty == types::TY_INVALID)
393 Ty = types::TY_Object;
394 }
395
396 // -ObjC and -ObjC++ override the default language, but only
397 // -for "source files". We just treat everything that isn't a
398 // -linker input as a source file.
399 //
400 // FIXME: Clean this up if we move the phase sequence into the
401 // type.
402 if (Ty != types::TY_Object) {
403 if (Args.hasArg(options::OPT_ObjC))
404 Ty = types::TY_ObjC;
405 else if (Args.hasArg(options::OPT_ObjCXX))
406 Ty = types::TY_ObjCXX;
407 }
408 } else {
409 assert(InputTypeArg && "InputType set w/o InputTypeArg");
410 InputTypeArg->claim();
411 Ty = InputType;
412 }
413
414 // Check that the file exists. It isn't clear this is worth
415 // doing, since the tool presumably does this anyway, and this
416 // just adds an extra stat to the equation, but this is gcc
417 // compatible.
418 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000419 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000420 else
421 Inputs.push_back(std::make_pair(Ty, A));
422
423 } else if (A->getOption().isLinkerInput()) {
424 // Just treat as object type, we could make a special type for
425 // this if necessary.
426 Inputs.push_back(std::make_pair(types::TY_Object, A));
427
428 } else if (A->getOption().getId() == options::OPT_x) {
429 InputTypeArg = A;
430 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
431
432 // Follow gcc behavior and treat as linker input for invalid -x
433 // options. Its not clear why we shouldn't just revert to
434 // unknown; but this isn't very important, we might as well be
435 // bug comatible.
436 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000437 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000438 InputType = types::TY_Object;
439 }
440 }
441 }
442
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000443 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000444 Diag(clang::diag::err_drv_no_input_files);
445 return;
446 }
447
448 // Determine which compilation mode we are in. We look for options
449 // which affect the phase, starting with the earliest phases, and
450 // record which option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000451 Arg *FinalPhaseArg = 0;
452 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000453
454 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000455 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
456 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
457 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
458 FinalPhase = phases::Preprocess;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000459
460 // -{-analyze,fsyntax-only,S} only run up to the compiler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000461 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
462 (FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
463 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
464 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000465
466 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000467 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
468 FinalPhase = phases::Assemble;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000469
470 // Otherwise do everything.
471 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000472 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000473
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000474 if (FinalPhaseArg)
475 FinalPhaseArg->claim();
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000476
477 // Reject -Z* at the top level, these options should never have been
478 // exposed by gcc.
479 if (Arg *A = Args.getLastArg(options::OPT_Z))
480 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
481
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000482 // Construct the actions to perform.
483 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000484 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000485 types::ID InputType = Inputs[i].first;
486 const Arg *InputArg = Inputs[i].second;
487
488 unsigned NumSteps = types::getNumCompilationPhases(InputType);
489 assert(NumSteps && "Invalid number of steps!");
490
491 // If the first step comes after the final phase we are doing as
492 // part of this compilation, warn the user about it.
493 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
494 if (InitialPhase > FinalPhase) {
495 Diag(clang::diag::warn_drv_input_file_unused)
496 << InputArg->getValue(Args)
497 << getPhaseName(InitialPhase)
498 << FinalPhaseArg->getOption().getName();
499 continue;
500 }
501
502 // Build the pipeline for this file.
503 Action *Current = new InputAction(*InputArg, InputType);
504 for (unsigned i = 0; i != NumSteps; ++i) {
505 phases::ID Phase = types::getCompilationPhase(InputType, i);
506
507 // We are done if this step is past what the user requested.
508 if (Phase > FinalPhase)
509 break;
510
511 // Queue linker inputs.
512 if (Phase == phases::Link) {
513 assert(i + 1 == NumSteps && "linking must be final compilation step.");
514 LinkerInputs.push_back(Current);
515 Current = 0;
516 break;
517 }
518
519 // Otherwise construct the appropriate action.
520 Current = ConstructPhaseAction(Args, Phase, Current);
521 if (Current->getType() == types::TY_Nothing)
522 break;
523 }
524
525 // If we ended with something, add to the output list.
526 if (Current)
527 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000528 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000529
530 // Add a link action if necessary.
531 if (!LinkerInputs.empty())
532 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
533}
534
535Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
536 Action *Input) const {
537 // Build the appropriate action.
538 switch (Phase) {
539 case phases::Link: assert(0 && "link action invalid here.");
540 case phases::Preprocess: {
541 types::ID OutputTy = types::getPreprocessedType(Input->getType());
542 assert(OutputTy != types::TY_INVALID &&
543 "Cannot preprocess this input type!");
544 return new PreprocessJobAction(Input, OutputTy);
545 }
546 case phases::Precompile:
547 return new PrecompileJobAction(Input, types::TY_PCH);
548 case phases::Compile: {
549 if (Args.hasArg(options::OPT_fsyntax_only)) {
550 return new CompileJobAction(Input, types::TY_Nothing);
551 } else if (Args.hasArg(options::OPT__analyze)) {
552 return new AnalyzeJobAction(Input, types::TY_Plist);
553 } else if (Args.hasArg(options::OPT_emit_llvm)) {
554 types::ID Output =
555 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
556 return new CompileJobAction(Input, Output);
557 } else {
558 return new CompileJobAction(Input, types::TY_PP_Asm);
559 }
560 }
561 case phases::Assemble:
562 return new AssembleJobAction(Input, types::TY_Object);
563 }
564
565 assert(0 && "invalid phase in ConstructPhaseAction");
566 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000567}
568
Daniel Dunbarcc006892009-03-13 00:51:18 +0000569llvm::sys::Path Driver::GetFilePath(const char *Name) const {
570 // FIXME: Implement.
571 return llvm::sys::Path(Name);
572}
573
574llvm::sys::Path Driver::GetProgramPath(const char *Name) const {
575 // FIXME: Implement.
576 return llvm::sys::Path(Name);
577}
578
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000579HostInfo *Driver::GetHostInfo(const char *Triple) {
580 // Dice into arch, platform, and OS. This matches
581 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
582 // and missing fields are left empty.
583 std::string Arch, Platform, OS;
584
585 if (const char *ArchEnd = strchr(Triple, '-')) {
586 Arch = std::string(Triple, ArchEnd);
587
588 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
589 Platform = std::string(ArchEnd+1, PlatformEnd);
590 OS = PlatformEnd+1;
591 } else
592 Platform = ArchEnd+1;
593 } else
594 Arch = Triple;
595
Daniel Dunbar44119a12009-03-13 12:23:29 +0000596 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000597 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
598
599 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
600}