blob: fdc60daf0bdbd8497bda3f0c53a36693250e4001 [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 Dunbar06482622009-03-05 06:38:47 +000018#include "clang/Driver/Option.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000019#include "clang/Driver/Options.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000020#include "clang/Driver/Types.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000021
Daniel Dunbar2fe63e62009-03-12 18:40:18 +000022#include "llvm/ADT/StringMap.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000024#include "llvm/System/Path.h"
Daniel Dunbarba102132009-03-13 12:19:02 +000025
26#include <map>
27
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000028using namespace clang::driver;
29
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000030Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000031 const char *_DefaultHostTriple,
32 Diagnostic &_Diags)
33 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000034 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
35 Host(0),
Daniel Dunbar365c02f2009-03-10 20:52:46 +000036 CCCIsCXX(false), CCCEcho(false),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000037 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false),
38 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000039{
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000040}
41
42Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000043 delete Opts;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000044}
45
Daniel Dunbar06482622009-03-05 06:38:47 +000046ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
47 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
48
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000049 // FIXME: Handle '@' args (or at least error on them).
50
Daniel Dunbar06482622009-03-05 06:38:47 +000051 unsigned Index = 0, End = ArgEnd - ArgBegin;
52 while (Index < End) {
Daniel Dunbar41393402009-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 Dunbar06482622009-03-05 06:38:47 +000063 unsigned Prev = Index;
64 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000065 if (A) {
66 if (A->getOption().isUnsupported()) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +000067 Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName();
Daniel Dunbar53ec5522009-03-12 07:58:46 +000068 continue;
69 }
70
Daniel Dunbar06482622009-03-05 06:38:47 +000071 Args->append(A);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000072 }
Daniel Dunbar06482622009-03-05 06:38:47 +000073
74 assert(Index > Prev && "Parser failed to consume argument.");
75 }
76
77 return Args;
78}
79
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000080Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarcb881672009-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 Dunbar365c02f2009-03-10 20:52:46 +000089 // FIXME: This stuff needs to go into the Compilation, not the
90 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +000091 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +000092
Daniel Dunbar365c02f2009-03-10 20:52:46 +000093 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000094 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-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 Dunbar53ec5522009-03-12 07:58:46 +0000108 CCCPrintActions = true;
Daniel Dunbar365c02f2009-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 Dunbardd98e2c2009-03-10 23:41:59 +0000136 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000137 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000138 HostTriple = *++Start;
Daniel Dunbar365c02f2009-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 Dunbardd98e2c2009-03-10 23:41:59 +0000146
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000147 ArgList *Args = ParseArgStrings(Start, End);
148
Daniel Dunbarcb881672009-03-13 00:51:18 +0000149 Host = Driver::GetHostInfo(HostTriple);
150 DefaultToolChain = Host->getToolChain(*Args);
151
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000152 // FIXME: This behavior shouldn't be here.
153 if (CCCPrintOptions) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000154 PrintOptions(*Args);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000155 exit(0);
156 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000157
Daniel Dunbarcb881672009-03-13 00:51:18 +0000158 if (!HandleImmediateArgs(*Args))
159 return 0;
160
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000161 // Construct the list of abstract actions to perform for this
162 // compilation.
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000163 ActionList Actions;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000164 if (Host->useDriverDriver())
165 BuildUniversalActions(*Args, Actions);
166 else
167 BuildActions(*Args, Actions);
168
169 // FIXME: This behavior shouldn't be here.
170 if (CCCPrintActions) {
Daniel Dunbarba102132009-03-13 12:19:02 +0000171 PrintActions(*Args, Actions);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000172 exit(0);
173 }
174
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000175 assert(0 && "FIXME: Implement");
176
177 return new Compilation();
178}
179
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000180void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000181 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000182 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-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 Dunbar53ec5522009-03-12 07:58:46 +0000191 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000192 }
193 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000194 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000195}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000196
Daniel Dunbarcb881672009-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 Dunbar41393402009-03-13 01:01:44 +0000227 if (Args.hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000228 llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n";
229 return false;
230 }
231
232 return true;
233}
234
Daniel Dunbarba102132009-03-13 12:19:02 +0000235// FIXME: This shouldn't be here?
236static 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)) {
247 os << IA->getInputArg().getValue(Args) << "\"";
248 } 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;
264 llvm::outs() << Id << ": " << os.str() << ", "
265 << 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();
274 it != ie; ++it) {
275 PrintActions1(Args, *it, Ids);
276 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000277}
278
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000279void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000280 llvm::StringMap<Arg *> Archs;
281 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
282 it != ie; ++it) {
283 Arg *A = *it;
284
285 if (A->getOption().getId() == options::OPT_arch) {
286 // FIXME: We need to handle canonicalization of the specified
287 // arch?
288
289 Archs[A->getValue(Args)] = A;
290 }
291 }
292
293 // When there is no explicit arch for this platform, get one from
294 // the host so that -Xarch_ is handled correctly.
295 if (!Archs.size()) {
296 const char *Arch = Host->getArchName().c_str();
297 Archs[Arch] = Args.MakeSeparateArg(getOpts().getOption(options::OPT_arch),
298 Arch);
299 }
300
301 // FIXME: We killed off some others but these aren't yet detected in
302 // a functional manner. If we added information to jobs about which
303 // "auxiliary" files they wrote then we could detect the conflict
304 // these cause downstream.
305 if (Archs.size() > 1) {
306 // No recovery needed, the point of this is just to prevent
307 // overwriting the same files.
308 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
309 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
310 << A->getOption().getName();
311 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
312 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
313 << A->getOption().getName();
314 }
315
316 ActionList SingleActions;
317 BuildActions(Args, SingleActions);
318
319 // Add in arch binding and lipo (if necessary) for every top level
320 // action.
321 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
322 Action *Act = SingleActions[i];
323
324 // Make sure we can lipo this kind of output. If not (and it is an
325 // actual output) then we disallow, since we can't create an
326 // output file with the right name without overwriting it. We
327 // could remove this oddity by just changing the output names to
328 // include the arch, which would also fix
329 // -save-temps. Compatibility wins for now.
330
331 if (Archs.size() > 1 && types::canLipoType(Act->getType()))
332 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
333 << types::getTypeName(Act->getType());
334
335 ActionList Inputs;
336 for (llvm::StringMap<Arg*>::iterator it = Archs.begin(), ie = Archs.end();
337 it != ie; ++it)
338 Inputs.push_back(new BindArchAction(Act, it->second->getValue(Args)));
339
340 // Lipo if necessary, We do it this way because we need to set the
341 // arch flag so that -Xarch_ gets overwritten.
342 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
343 Actions.append(Inputs.begin(), Inputs.end());
344 else
345 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
346 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000347}
348
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000349void Driver::BuildActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000350 types::ID InputType = types::TY_INVALID;
351 Arg *InputTypeArg = 0;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000352
353 // Start by constructing the list of inputs and their types.
354
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000355 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
356 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
357 it != ie; ++it) {
358 Arg *A = *it;
359
360 if (isa<InputOption>(A->getOption())) {
361 const char *Value = A->getValue(Args);
362 types::ID Ty = types::TY_INVALID;
363
364 // Infer the input type if necessary.
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000365 if (InputType == types::TY_INVALID) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000366 // stdin must be handled specially.
367 if (memcmp(Value, "-", 2) == 0) {
368 // If running with -E, treat as a C input (this changes the
369 // builtin macros, for example). This may be overridden by
370 // -ObjC below.
371 //
372 // Otherwise emit an error but still use a valid type to
373 // avoid spurious errors (e.g., no inputs).
374 if (!Args.hasArg(options::OPT_E))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000375 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000376 Ty = types::TY_C;
377 } else {
378 // Otherwise lookup by extension, and fallback to ObjectType
379 // if not found.
380 if (const char *Ext = strrchr(Value, '.'))
381 Ty = types::lookupTypeForExtension(Ext + 1);
382 if (Ty == types::TY_INVALID)
383 Ty = types::TY_Object;
384 }
385
386 // -ObjC and -ObjC++ override the default language, but only
387 // -for "source files". We just treat everything that isn't a
388 // -linker input as a source file.
389 //
390 // FIXME: Clean this up if we move the phase sequence into the
391 // type.
392 if (Ty != types::TY_Object) {
393 if (Args.hasArg(options::OPT_ObjC))
394 Ty = types::TY_ObjC;
395 else if (Args.hasArg(options::OPT_ObjCXX))
396 Ty = types::TY_ObjCXX;
397 }
398 } else {
399 assert(InputTypeArg && "InputType set w/o InputTypeArg");
400 InputTypeArg->claim();
401 Ty = InputType;
402 }
403
404 // Check that the file exists. It isn't clear this is worth
405 // doing, since the tool presumably does this anyway, and this
406 // just adds an extra stat to the equation, but this is gcc
407 // compatible.
408 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000409 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000410 else
411 Inputs.push_back(std::make_pair(Ty, A));
412
413 } else if (A->getOption().isLinkerInput()) {
414 // Just treat as object type, we could make a special type for
415 // this if necessary.
416 Inputs.push_back(std::make_pair(types::TY_Object, A));
417
418 } else if (A->getOption().getId() == options::OPT_x) {
419 InputTypeArg = A;
420 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
421
422 // Follow gcc behavior and treat as linker input for invalid -x
423 // options. Its not clear why we shouldn't just revert to
424 // unknown; but this isn't very important, we might as well be
425 // bug comatible.
426 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000427 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000428 InputType = types::TY_Object;
429 }
430 }
431 }
432
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000433 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000434 Diag(clang::diag::err_drv_no_input_files);
435 return;
436 }
437
438 // Determine which compilation mode we are in. We look for options
439 // which affect the phase, starting with the earliest phases, and
440 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000441 Arg *FinalPhaseArg = 0;
442 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000443
444 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000445 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
446 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
447 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
448 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000449
450 // -{-analyze,fsyntax-only,S} only run up to the compiler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000451 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
452 (FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
453 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
454 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000455
456 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000457 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
458 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000459
460 // Otherwise do everything.
461 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000462 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000463
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000464 if (FinalPhaseArg)
465 FinalPhaseArg->claim();
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000466
467 // Reject -Z* at the top level, these options should never have been
468 // exposed by gcc.
469 if (Arg *A = Args.getLastArg(options::OPT_Z))
470 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
471
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000472 // Construct the actions to perform.
473 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000474 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000475 types::ID InputType = Inputs[i].first;
476 const Arg *InputArg = Inputs[i].second;
477
478 unsigned NumSteps = types::getNumCompilationPhases(InputType);
479 assert(NumSteps && "Invalid number of steps!");
480
481 // If the first step comes after the final phase we are doing as
482 // part of this compilation, warn the user about it.
483 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
484 if (InitialPhase > FinalPhase) {
485 Diag(clang::diag::warn_drv_input_file_unused)
486 << InputArg->getValue(Args)
487 << getPhaseName(InitialPhase)
488 << FinalPhaseArg->getOption().getName();
489 continue;
490 }
491
492 // Build the pipeline for this file.
493 Action *Current = new InputAction(*InputArg, InputType);
494 for (unsigned i = 0; i != NumSteps; ++i) {
495 phases::ID Phase = types::getCompilationPhase(InputType, i);
496
497 // We are done if this step is past what the user requested.
498 if (Phase > FinalPhase)
499 break;
500
501 // Queue linker inputs.
502 if (Phase == phases::Link) {
503 assert(i + 1 == NumSteps && "linking must be final compilation step.");
504 LinkerInputs.push_back(Current);
505 Current = 0;
506 break;
507 }
508
509 // Otherwise construct the appropriate action.
510 Current = ConstructPhaseAction(Args, Phase, Current);
511 if (Current->getType() == types::TY_Nothing)
512 break;
513 }
514
515 // If we ended with something, add to the output list.
516 if (Current)
517 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000518 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000519
520 // Add a link action if necessary.
521 if (!LinkerInputs.empty())
522 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
523}
524
525Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
526 Action *Input) const {
527 // Build the appropriate action.
528 switch (Phase) {
529 case phases::Link: assert(0 && "link action invalid here.");
530 case phases::Preprocess: {
531 types::ID OutputTy = types::getPreprocessedType(Input->getType());
532 assert(OutputTy != types::TY_INVALID &&
533 "Cannot preprocess this input type!");
534 return new PreprocessJobAction(Input, OutputTy);
535 }
536 case phases::Precompile:
537 return new PrecompileJobAction(Input, types::TY_PCH);
538 case phases::Compile: {
539 if (Args.hasArg(options::OPT_fsyntax_only)) {
540 return new CompileJobAction(Input, types::TY_Nothing);
541 } else if (Args.hasArg(options::OPT__analyze)) {
542 return new AnalyzeJobAction(Input, types::TY_Plist);
543 } else if (Args.hasArg(options::OPT_emit_llvm)) {
544 types::ID Output =
545 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
546 return new CompileJobAction(Input, Output);
547 } else {
548 return new CompileJobAction(Input, types::TY_PP_Asm);
549 }
550 }
551 case phases::Assemble:
552 return new AssembleJobAction(Input, types::TY_Object);
553 }
554
555 assert(0 && "invalid phase in ConstructPhaseAction");
556 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000557}
558
Daniel Dunbarcb881672009-03-13 00:51:18 +0000559llvm::sys::Path Driver::GetFilePath(const char *Name) const {
560 // FIXME: Implement.
561 return llvm::sys::Path(Name);
562}
563
564llvm::sys::Path Driver::GetProgramPath(const char *Name) const {
565 // FIXME: Implement.
566 return llvm::sys::Path(Name);
567}
568
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000569HostInfo *Driver::GetHostInfo(const char *Triple) {
570 // Dice into arch, platform, and OS. This matches
571 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
572 // and missing fields are left empty.
573 std::string Arch, Platform, OS;
574
575 if (const char *ArchEnd = strchr(Triple, '-')) {
576 Arch = std::string(Triple, ArchEnd);
577
578 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
579 Platform = std::string(ArchEnd+1, PlatformEnd);
580 OS = PlatformEnd+1;
581 } else
582 Platform = ArchEnd+1;
583 } else
584 Arch = Triple;
585
Daniel Dunbara88162c2009-03-13 12:23:29 +0000586 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000587 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
588
589 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
590}