blob: cba2c9f7c634055915925fd6f495acb44ea3587b [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
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000174 // The compilation takes ownership of Args.
175 Compilation *C = new Compilation(*DefaultToolChain, Args);
176 BuildJobs(*C, Actions);
Daniel Dunbarc413f822009-03-15 01:38:15 +0000177
178 return C;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000179}
180
Daniel Dunbara790d372009-03-12 18:24:49 +0000181void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000182 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000183 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000184 it != ie; ++it, ++i) {
185 Arg *A = *it;
186 llvm::errs() << "Option " << i << " - "
187 << "Name: \"" << A->getOption().getName() << "\", "
188 << "Values: {";
189 for (unsigned j = 0; j < A->getNumValues(); ++j) {
190 if (j)
191 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000192 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000193 }
194 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000195 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000196}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000197
Daniel Dunbarcc006892009-03-13 00:51:18 +0000198void Driver::PrintVersion() const {
199 // FIXME: Get a reasonable version number.
200
201 // FIXME: The following handlers should use a callback mechanism, we
202 // don't know what the client would like to do.
203 llvm::outs() << "ccc version 1.0" << "\n";
204}
205
206bool Driver::HandleImmediateArgs(const ArgList &Args) {
207 // The order these options are handled in in gcc is all over the
208 // place, but we don't expect inconsistencies w.r.t. that to matter
209 // in practice.
210 if (Args.hasArg(options::OPT_v) ||
211 Args.hasArg(options::OPT__HASH_HASH_HASH)) {
212 PrintVersion();
213 SuppressMissingInputWarning = true;
214 }
215
216 // FIXME: The following handlers should use a callback mechanism, we
217 // don't know what the client would like to do.
218 if (Arg *A = Args.getLastArg(options::OPT_print_file_name_EQ)) {
219 llvm::outs() << GetFilePath(A->getValue(Args)).toString() << "\n";
220 return false;
221 }
222
223 if (Arg *A = Args.getLastArg(options::OPT_print_prog_name_EQ)) {
224 llvm::outs() << GetProgramPath(A->getValue(Args)).toString() << "\n";
225 return false;
226 }
227
Daniel Dunbarb043ebd2009-03-13 01:01:44 +0000228 if (Args.hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000229 llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n";
230 return false;
231 }
232
233 return true;
234}
235
Daniel Dunbar494646b2009-03-13 12:19:02 +0000236static unsigned PrintActions1(const ArgList &Args,
237 Action *A,
238 std::map<Action*, unsigned> &Ids) {
239 if (Ids.count(A))
240 return Ids[A];
241
242 std::string str;
243 llvm::raw_string_ostream os(str);
244
245 os << Action::getClassName(A->getKind()) << ", ";
246 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000247 os << "\"" << IA->getInputArg().getValue(Args) << "\"";
Daniel Dunbar494646b2009-03-13 12:19:02 +0000248 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
249 os << "\"" << BIA->getArchName() << "\", "
250 << "{" << PrintActions1(Args, *BIA->begin(), Ids) << "}";
251 } else {
252 os << "{";
253 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
254 os << PrintActions1(Args, *it, Ids);
255 ++it;
256 if (it != ie)
257 os << ", ";
258 }
259 os << "}";
260 }
261
262 unsigned Id = Ids.size();
263 Ids[A] = Id;
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000264 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbar494646b2009-03-13 12:19:02 +0000265 << types::getTypeName(A->getType()) << "\n";
266
267 return Id;
268}
269
270void Driver::PrintActions(const ArgList &Args,
271 const ActionList &Actions) const {
272 std::map<Action*, unsigned> Ids;
273 for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbar9dc28b82009-03-13 17:20:20 +0000274 it != ie; ++it)
Daniel Dunbar494646b2009-03-13 12:19:02 +0000275 PrintActions1(Args, *it, Ids);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000276}
277
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000278void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) const {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000279 // Collect the list of architectures. Duplicates are allowed, but
280 // should only be handled once (in the order seen).
281 llvm::StringSet<> ArchNames;
282 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000283 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
284 it != ie; ++it) {
285 Arg *A = *it;
286
287 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000288 const char *Name = A->getValue(Args);
289
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000290 // FIXME: We need to handle canonicalization of the specified
291 // arch?
292
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000293 if (ArchNames.insert(Name))
294 Archs.push_back(Name);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000295 }
296 }
297
298 // When there is no explicit arch for this platform, get one from
299 // the host so that -Xarch_ is handled correctly.
300 if (!Archs.size()) {
301 const char *Arch = Host->getArchName().c_str();
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000302 Archs.push_back(Arch);
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000303 }
304
305 // FIXME: We killed off some others but these aren't yet detected in
306 // a functional manner. If we added information to jobs about which
307 // "auxiliary" files they wrote then we could detect the conflict
308 // these cause downstream.
309 if (Archs.size() > 1) {
310 // No recovery needed, the point of this is just to prevent
311 // overwriting the same files.
312 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
313 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
314 << A->getOption().getName();
315 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
316 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
317 << A->getOption().getName();
318 }
319
320 ActionList SingleActions;
321 BuildActions(Args, SingleActions);
322
323 // Add in arch binding and lipo (if necessary) for every top level
324 // action.
325 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
326 Action *Act = SingleActions[i];
327
328 // Make sure we can lipo this kind of output. If not (and it is an
329 // actual output) then we disallow, since we can't create an
330 // output file with the right name without overwriting it. We
331 // could remove this oddity by just changing the output names to
332 // include the arch, which would also fix
333 // -save-temps. Compatibility wins for now.
334
Daniel Dunbardd863aa2009-03-13 17:46:02 +0000335 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000336 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
337 << types::getTypeName(Act->getType());
338
339 ActionList Inputs;
Daniel Dunbarb1873cd2009-03-13 20:33:35 +0000340 for (unsigned i = 0, e = Archs.size(); i != e; ++i )
341 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000342
343 // Lipo if necessary, We do it this way because we need to set the
344 // arch flag so that -Xarch_ gets overwritten.
345 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
346 Actions.append(Inputs.begin(), Inputs.end());
347 else
348 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
349 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000350}
351
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000352void Driver::BuildActions(ArgList &Args, ActionList &Actions) const {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000353 // Start by constructing the list of inputs and their types.
354
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000355 // Track the current user specified (-x) input. We also explicitly
356 // track the argument used to set the type; we only want to claim
357 // the type when we actually use it, so we warn about unused -x
358 // arguments.
359 types::ID InputType = types::TY_Nothing;
360 Arg *InputTypeArg = 0;
361
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000362 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
363 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
364 it != ie; ++it) {
365 Arg *A = *it;
366
367 if (isa<InputOption>(A->getOption())) {
368 const char *Value = A->getValue(Args);
369 types::ID Ty = types::TY_INVALID;
370
371 // Infer the input type if necessary.
Daniel Dunbar5cb75d62009-03-13 17:57:10 +0000372 if (InputType == types::TY_Nothing) {
373 // If there was an explicit arg for this, claim it.
374 if (InputTypeArg)
375 InputTypeArg->claim();
376
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000377 // stdin must be handled specially.
378 if (memcmp(Value, "-", 2) == 0) {
379 // If running with -E, treat as a C input (this changes the
380 // builtin macros, for example). This may be overridden by
381 // -ObjC below.
382 //
383 // Otherwise emit an error but still use a valid type to
384 // avoid spurious errors (e.g., no inputs).
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000385 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbard724e332009-03-12 09:13:48 +0000386 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000387 Ty = types::TY_C;
388 } else {
389 // Otherwise lookup by extension, and fallback to ObjectType
390 // if not found.
391 if (const char *Ext = strrchr(Value, '.'))
392 Ty = types::lookupTypeForExtension(Ext + 1);
393 if (Ty == types::TY_INVALID)
394 Ty = types::TY_Object;
395 }
396
397 // -ObjC and -ObjC++ override the default language, but only
398 // -for "source files". We just treat everything that isn't a
399 // -linker input as a source file.
400 //
401 // FIXME: Clean this up if we move the phase sequence into the
402 // type.
403 if (Ty != types::TY_Object) {
404 if (Args.hasArg(options::OPT_ObjC))
405 Ty = types::TY_ObjC;
406 else if (Args.hasArg(options::OPT_ObjCXX))
407 Ty = types::TY_ObjCXX;
408 }
409 } else {
410 assert(InputTypeArg && "InputType set w/o InputTypeArg");
411 InputTypeArg->claim();
412 Ty = InputType;
413 }
414
415 // Check that the file exists. It isn't clear this is worth
416 // doing, since the tool presumably does this anyway, and this
417 // just adds an extra stat to the equation, but this is gcc
418 // compatible.
Daniel Dunbar321c12d2009-03-15 01:40:22 +0000419 A->claim();
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000420 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000421 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000422 else
423 Inputs.push_back(std::make_pair(Ty, A));
424
425 } else if (A->getOption().isLinkerInput()) {
426 // Just treat as object type, we could make a special type for
427 // this if necessary.
Daniel Dunbar321c12d2009-03-15 01:40:22 +0000428 A->claim();
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000429 Inputs.push_back(std::make_pair(types::TY_Object, A));
430
431 } else if (A->getOption().getId() == options::OPT_x) {
432 InputTypeArg = A;
433 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
434
435 // Follow gcc behavior and treat as linker input for invalid -x
436 // options. Its not clear why we shouldn't just revert to
437 // unknown; but this isn't very important, we might as well be
438 // bug comatible.
439 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000440 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000441 InputType = types::TY_Object;
442 }
443 }
444 }
445
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000446 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000447 Diag(clang::diag::err_drv_no_input_files);
448 return;
449 }
450
451 // Determine which compilation mode we are in. We look for options
452 // which affect the phase, starting with the earliest phases, and
453 // record which option we used to determine the final phase.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000454 Arg *FinalPhaseArg = 0;
455 phases::ID FinalPhase;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000456
457 // -{E,M,MM} only run the preprocessor.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000458 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
459 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
460 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
461 FinalPhase = phases::Preprocess;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000462
Daniel Dunbare9c70fa2009-03-15 00:48:16 +0000463 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
464 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
465 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
466 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_llvm)) ||
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000467 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
468 FinalPhase = phases::Compile;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000469
470 // -c only runs up to the assembler.
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000471 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
472 FinalPhase = phases::Assemble;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000473
474 // Otherwise do everything.
475 } else
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000476 FinalPhase = phases::Link;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000477
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000478 // Reject -Z* at the top level, these options should never have been
479 // exposed by gcc.
480 if (Arg *A = Args.getLastArg(options::OPT_Z))
481 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
482
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000483 // Construct the actions to perform.
484 ActionList LinkerInputs;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000485 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000486 types::ID InputType = Inputs[i].first;
487 const Arg *InputArg = Inputs[i].second;
488
489 unsigned NumSteps = types::getNumCompilationPhases(InputType);
490 assert(NumSteps && "Invalid number of steps!");
491
492 // If the first step comes after the final phase we are doing as
493 // part of this compilation, warn the user about it.
494 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
495 if (InitialPhase > FinalPhase) {
496 Diag(clang::diag::warn_drv_input_file_unused)
497 << InputArg->getValue(Args)
498 << getPhaseName(InitialPhase)
499 << FinalPhaseArg->getOption().getName();
500 continue;
501 }
502
503 // Build the pipeline for this file.
504 Action *Current = new InputAction(*InputArg, InputType);
505 for (unsigned i = 0; i != NumSteps; ++i) {
506 phases::ID Phase = types::getCompilationPhase(InputType, i);
507
508 // We are done if this step is past what the user requested.
509 if (Phase > FinalPhase)
510 break;
511
512 // Queue linker inputs.
513 if (Phase == phases::Link) {
514 assert(i + 1 == NumSteps && "linking must be final compilation step.");
515 LinkerInputs.push_back(Current);
516 Current = 0;
517 break;
518 }
519
520 // Otherwise construct the appropriate action.
521 Current = ConstructPhaseAction(Args, Phase, Current);
522 if (Current->getType() == types::TY_Nothing)
523 break;
524 }
525
526 // If we ended with something, add to the output list.
527 if (Current)
528 Actions.push_back(Current);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000529 }
Daniel Dunbar85cb3592009-03-13 11:38:42 +0000530
531 // Add a link action if necessary.
532 if (!LinkerInputs.empty())
533 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
534}
535
536Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
537 Action *Input) const {
538 // Build the appropriate action.
539 switch (Phase) {
540 case phases::Link: assert(0 && "link action invalid here.");
541 case phases::Preprocess: {
542 types::ID OutputTy = types::getPreprocessedType(Input->getType());
543 assert(OutputTy != types::TY_INVALID &&
544 "Cannot preprocess this input type!");
545 return new PreprocessJobAction(Input, OutputTy);
546 }
547 case phases::Precompile:
548 return new PrecompileJobAction(Input, types::TY_PCH);
549 case phases::Compile: {
550 if (Args.hasArg(options::OPT_fsyntax_only)) {
551 return new CompileJobAction(Input, types::TY_Nothing);
552 } else if (Args.hasArg(options::OPT__analyze)) {
553 return new AnalyzeJobAction(Input, types::TY_Plist);
554 } else if (Args.hasArg(options::OPT_emit_llvm)) {
555 types::ID Output =
556 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
557 return new CompileJobAction(Input, Output);
558 } else {
559 return new CompileJobAction(Input, types::TY_PP_Asm);
560 }
561 }
562 case phases::Assemble:
563 return new AssembleJobAction(Input, types::TY_Object);
564 }
565
566 assert(0 && "invalid phase in ConstructPhaseAction");
567 return 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000568}
569
Daniel Dunbar9d625e12009-03-16 06:42:30 +0000570void Driver::BuildJobs(Compilation &C,
571 const ActionList &Actions) const {
572
573 // If there were no errors, warn about any unused arguments.
574 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
575 it != ie; ++it) {
576 Arg *A = *it;
577
578 // FIXME: It would be nice to be able to send the argument to the
579 // Diagnostic, so that extra values, position, and so on could be
580 // printed.
581 if (!A->isClaimed())
582 Diag(clang::diag::warn_drv_unused_argument)
583 << A->getOption().getName();
584 }
Daniel Dunbar47d762e2009-03-13 22:12:33 +0000585}
586
Daniel Dunbare1cef7d2009-03-16 05:25:36 +0000587llvm::sys::Path Driver::GetFilePath(const char *Name,
588 const ToolChain *TC) const {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000589 // FIXME: Implement.
Daniel Dunbare1cef7d2009-03-16 05:25:36 +0000590 if (!TC) TC = DefaultToolChain;
591
Daniel Dunbarcc006892009-03-13 00:51:18 +0000592 return llvm::sys::Path(Name);
593}
594
Daniel Dunbare1cef7d2009-03-16 05:25:36 +0000595llvm::sys::Path Driver::GetProgramPath(const char *Name,
596 const ToolChain *TC) const {
Daniel Dunbarcc006892009-03-13 00:51:18 +0000597 // FIXME: Implement.
Daniel Dunbare1cef7d2009-03-16 05:25:36 +0000598 if (!TC) TC = DefaultToolChain;
599
Daniel Dunbarcc006892009-03-13 00:51:18 +0000600 return llvm::sys::Path(Name);
601}
602
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000603HostInfo *Driver::GetHostInfo(const char *Triple) {
604 // Dice into arch, platform, and OS. This matches
605 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
606 // and missing fields are left empty.
607 std::string Arch, Platform, OS;
608
609 if (const char *ArchEnd = strchr(Triple, '-')) {
610 Arch = std::string(Triple, ArchEnd);
611
612 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
613 Platform = std::string(ArchEnd+1, PlatformEnd);
614 OS = PlatformEnd+1;
615 } else
616 Platform = ArchEnd+1;
617 } else
618 Arch = Triple;
619
Daniel Dunbar44119a12009-03-13 12:23:29 +0000620 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000621 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
622
623 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
624}