blob: 75e9df25e05105bf33cf0ddd4d1ac093e4e26419 [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 Dunbar1b3bb6e2009-03-04 20:49:20 +000025using namespace clang::driver;
26
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000027Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000028 const char *_DefaultHostTriple,
29 Diagnostic &_Diags)
30 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000031 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
32 Host(0),
Daniel Dunbar365c02f2009-03-10 20:52:46 +000033 CCCIsCXX(false), CCCEcho(false),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000034 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false),
35 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000036{
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000037}
38
39Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000040 delete Opts;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000041}
42
Daniel Dunbar06482622009-03-05 06:38:47 +000043ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
44 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
45
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000046 // FIXME: Handle '@' args (or at least error on them).
47
Daniel Dunbar06482622009-03-05 06:38:47 +000048 unsigned Index = 0, End = ArgEnd - ArgBegin;
49 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000050 // gcc's handling of empty arguments doesn't make
51 // sense, but this is not a common use case. :)
52 //
53 // We just ignore them here (note that other things may
54 // still take them as arguments).
55 if (Args->getArgString(Index)[0] == '\0') {
56 ++Index;
57 continue;
58 }
59
Daniel Dunbar06482622009-03-05 06:38:47 +000060 unsigned Prev = Index;
61 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000062 if (A) {
63 if (A->getOption().isUnsupported()) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +000064 Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName();
Daniel Dunbar53ec5522009-03-12 07:58:46 +000065 continue;
66 }
67
Daniel Dunbar06482622009-03-05 06:38:47 +000068 Args->append(A);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000069 }
Daniel Dunbar06482622009-03-05 06:38:47 +000070
71 assert(Index > Prev && "Parser failed to consume argument.");
72 }
73
74 return Args;
75}
76
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000077Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarcb881672009-03-13 00:51:18 +000078 // FIXME: Handle environment options which effect driver behavior,
79 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
80 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
81
82 // FIXME: What are we going to do with -V and -b?
83
84 // FIXME: Handle CCC_ADD_ARGS.
85
Daniel Dunbar365c02f2009-03-10 20:52:46 +000086 // FIXME: This stuff needs to go into the Compilation, not the
87 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +000088 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +000089
Daniel Dunbar365c02f2009-03-10 20:52:46 +000090 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000091 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +000092
93 // Read -ccc args.
94 //
95 // FIXME: We need to figure out where this behavior should
96 // live. Most of it should be outside in the client; the parts that
97 // aren't should have proper options, either by introducing new ones
98 // or by overloading gcc ones like -V or -b.
99 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
100 const char *Opt = *Start + 5;
101
102 if (!strcmp(Opt, "print-options")) {
103 CCCPrintOptions = true;
104 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000105 CCCPrintActions = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000106 } else if (!strcmp(Opt, "cxx")) {
107 CCCIsCXX = true;
108 } else if (!strcmp(Opt, "echo")) {
109 CCCEcho = true;
110
111 } else if (!strcmp(Opt, "no-clang")) {
112 CCCNoClang = true;
113 } else if (!strcmp(Opt, "no-clang-cxx")) {
114 CCCNoClangCXX = true;
115 } else if (!strcmp(Opt, "no-clang-cpp")) {
116 CCCNoClangCPP = true;
117 } else if (!strcmp(Opt, "clang-archs")) {
118 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
119 const char *Cur = *++Start;
120
121 for (;;) {
122 const char *Next = strchr(Cur, ',');
123
124 if (Next) {
125 CCCClangArchs.insert(std::string(Cur, Next));
126 Cur = Next + 1;
127 } else {
128 CCCClangArchs.insert(std::string(Cur));
129 break;
130 }
131 }
132
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000133 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000134 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000135 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000136
137 } else {
138 // FIXME: Error handling.
139 llvm::errs() << "invalid option: " << *Start << "\n";
140 exit(1);
141 }
142 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000143
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000144 ArgList *Args = ParseArgStrings(Start, End);
145
Daniel Dunbarcb881672009-03-13 00:51:18 +0000146 Host = Driver::GetHostInfo(HostTriple);
147 DefaultToolChain = Host->getToolChain(*Args);
148
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000149 // FIXME: This behavior shouldn't be here.
150 if (CCCPrintOptions) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000151 PrintOptions(*Args);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000152 exit(0);
153 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000154
Daniel Dunbarcb881672009-03-13 00:51:18 +0000155 if (!HandleImmediateArgs(*Args))
156 return 0;
157
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000158 // Construct the list of abstract actions to perform for this
159 // compilation.
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000160 ActionList Actions;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000161 if (Host->useDriverDriver())
162 BuildUniversalActions(*Args, Actions);
163 else
164 BuildActions(*Args, Actions);
165
166 // FIXME: This behavior shouldn't be here.
167 if (CCCPrintActions) {
168 PrintActions(Actions);
169 exit(0);
170 }
171
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000172 assert(0 && "FIXME: Implement");
173
174 return new Compilation();
175}
176
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000177void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000178 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000179 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000180 it != ie; ++it, ++i) {
181 Arg *A = *it;
182 llvm::errs() << "Option " << i << " - "
183 << "Name: \"" << A->getOption().getName() << "\", "
184 << "Values: {";
185 for (unsigned j = 0; j < A->getNumValues(); ++j) {
186 if (j)
187 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000188 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000189 }
190 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000191 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000192}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000193
Daniel Dunbarcb881672009-03-13 00:51:18 +0000194void Driver::PrintVersion() const {
195 // FIXME: Get a reasonable version number.
196
197 // FIXME: The following handlers should use a callback mechanism, we
198 // don't know what the client would like to do.
199 llvm::outs() << "ccc version 1.0" << "\n";
200}
201
202bool Driver::HandleImmediateArgs(const ArgList &Args) {
203 // The order these options are handled in in gcc is all over the
204 // place, but we don't expect inconsistencies w.r.t. that to matter
205 // in practice.
206 if (Args.hasArg(options::OPT_v) ||
207 Args.hasArg(options::OPT__HASH_HASH_HASH)) {
208 PrintVersion();
209 SuppressMissingInputWarning = true;
210 }
211
212 // FIXME: The following handlers should use a callback mechanism, we
213 // don't know what the client would like to do.
214 if (Arg *A = Args.getLastArg(options::OPT_print_file_name_EQ)) {
215 llvm::outs() << GetFilePath(A->getValue(Args)).toString() << "\n";
216 return false;
217 }
218
219 if (Arg *A = Args.getLastArg(options::OPT_print_prog_name_EQ)) {
220 llvm::outs() << GetProgramPath(A->getValue(Args)).toString() << "\n";
221 return false;
222 }
223
Daniel Dunbar41393402009-03-13 01:01:44 +0000224 if (Args.hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000225 llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n";
226 return false;
227 }
228
229 return true;
230}
231
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000232void Driver::PrintActions(const ActionList &Actions) const {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000233 llvm::errs() << "FIXME: Print actions.";
234}
235
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000236void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000237 llvm::StringMap<Arg *> Archs;
238 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
239 it != ie; ++it) {
240 Arg *A = *it;
241
242 if (A->getOption().getId() == options::OPT_arch) {
243 // FIXME: We need to handle canonicalization of the specified
244 // arch?
245
246 Archs[A->getValue(Args)] = A;
247 }
248 }
249
250 // When there is no explicit arch for this platform, get one from
251 // the host so that -Xarch_ is handled correctly.
252 if (!Archs.size()) {
253 const char *Arch = Host->getArchName().c_str();
254 Archs[Arch] = Args.MakeSeparateArg(getOpts().getOption(options::OPT_arch),
255 Arch);
256 }
257
258 // FIXME: We killed off some others but these aren't yet detected in
259 // a functional manner. If we added information to jobs about which
260 // "auxiliary" files they wrote then we could detect the conflict
261 // these cause downstream.
262 if (Archs.size() > 1) {
263 // No recovery needed, the point of this is just to prevent
264 // overwriting the same files.
265 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
266 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
267 << A->getOption().getName();
268 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
269 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
270 << A->getOption().getName();
271 }
272
273 ActionList SingleActions;
274 BuildActions(Args, SingleActions);
275
276 // Add in arch binding and lipo (if necessary) for every top level
277 // action.
278 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
279 Action *Act = SingleActions[i];
280
281 // Make sure we can lipo this kind of output. If not (and it is an
282 // actual output) then we disallow, since we can't create an
283 // output file with the right name without overwriting it. We
284 // could remove this oddity by just changing the output names to
285 // include the arch, which would also fix
286 // -save-temps. Compatibility wins for now.
287
288 if (Archs.size() > 1 && types::canLipoType(Act->getType()))
289 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
290 << types::getTypeName(Act->getType());
291
292 ActionList Inputs;
293 for (llvm::StringMap<Arg*>::iterator it = Archs.begin(), ie = Archs.end();
294 it != ie; ++it)
295 Inputs.push_back(new BindArchAction(Act, it->second->getValue(Args)));
296
297 // Lipo if necessary, We do it this way because we need to set the
298 // arch flag so that -Xarch_ gets overwritten.
299 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
300 Actions.append(Inputs.begin(), Inputs.end());
301 else
302 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
303 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000304}
305
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000306void Driver::BuildActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000307 types::ID InputType = types::TY_INVALID;
308 Arg *InputTypeArg = 0;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000309
310 // Start by constructing the list of inputs and their types.
311
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000312 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
313 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
314 it != ie; ++it) {
315 Arg *A = *it;
316
317 if (isa<InputOption>(A->getOption())) {
318 const char *Value = A->getValue(Args);
319 types::ID Ty = types::TY_INVALID;
320
321 // Infer the input type if necessary.
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000322 if (InputType == types::TY_INVALID) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000323 // stdin must be handled specially.
324 if (memcmp(Value, "-", 2) == 0) {
325 // If running with -E, treat as a C input (this changes the
326 // builtin macros, for example). This may be overridden by
327 // -ObjC below.
328 //
329 // Otherwise emit an error but still use a valid type to
330 // avoid spurious errors (e.g., no inputs).
331 if (!Args.hasArg(options::OPT_E))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000332 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000333 Ty = types::TY_C;
334 } else {
335 // Otherwise lookup by extension, and fallback to ObjectType
336 // if not found.
337 if (const char *Ext = strrchr(Value, '.'))
338 Ty = types::lookupTypeForExtension(Ext + 1);
339 if (Ty == types::TY_INVALID)
340 Ty = types::TY_Object;
341 }
342
343 // -ObjC and -ObjC++ override the default language, but only
344 // -for "source files". We just treat everything that isn't a
345 // -linker input as a source file.
346 //
347 // FIXME: Clean this up if we move the phase sequence into the
348 // type.
349 if (Ty != types::TY_Object) {
350 if (Args.hasArg(options::OPT_ObjC))
351 Ty = types::TY_ObjC;
352 else if (Args.hasArg(options::OPT_ObjCXX))
353 Ty = types::TY_ObjCXX;
354 }
355 } else {
356 assert(InputTypeArg && "InputType set w/o InputTypeArg");
357 InputTypeArg->claim();
358 Ty = InputType;
359 }
360
361 // Check that the file exists. It isn't clear this is worth
362 // doing, since the tool presumably does this anyway, and this
363 // just adds an extra stat to the equation, but this is gcc
364 // compatible.
365 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000366 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000367 else
368 Inputs.push_back(std::make_pair(Ty, A));
369
370 } else if (A->getOption().isLinkerInput()) {
371 // Just treat as object type, we could make a special type for
372 // this if necessary.
373 Inputs.push_back(std::make_pair(types::TY_Object, A));
374
375 } else if (A->getOption().getId() == options::OPT_x) {
376 InputTypeArg = A;
377 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
378
379 // Follow gcc behavior and treat as linker input for invalid -x
380 // options. Its not clear why we shouldn't just revert to
381 // unknown; but this isn't very important, we might as well be
382 // bug comatible.
383 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000384 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000385 InputType = types::TY_Object;
386 }
387 }
388 }
389
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000390 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000391 Diag(clang::diag::err_drv_no_input_files);
392 return;
393 }
394
395 // Determine which compilation mode we are in. We look for options
396 // which affect the phase, starting with the earliest phases, and
397 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000398 Arg *FinalPhaseArg = 0;
399 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000400
401 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000402 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
403 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
404 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
405 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000406
407 // -{-analyze,fsyntax-only,S} only run up to the compiler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000408 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
409 (FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
410 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
411 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000412
413 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000414 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
415 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000416
417 // Otherwise do everything.
418 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000419 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000420
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000421 if (FinalPhaseArg)
422 FinalPhaseArg->claim();
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000423
424 // Reject -Z* at the top level, these options should never have been
425 // exposed by gcc.
426 if (Arg *A = Args.getLastArg(options::OPT_Z))
427 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
428
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000429 // Construct the actions to perform.
430 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000431 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000432 types::ID InputType = Inputs[i].first;
433 const Arg *InputArg = Inputs[i].second;
434
435 unsigned NumSteps = types::getNumCompilationPhases(InputType);
436 assert(NumSteps && "Invalid number of steps!");
437
438 // If the first step comes after the final phase we are doing as
439 // part of this compilation, warn the user about it.
440 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
441 if (InitialPhase > FinalPhase) {
442 Diag(clang::diag::warn_drv_input_file_unused)
443 << InputArg->getValue(Args)
444 << getPhaseName(InitialPhase)
445 << FinalPhaseArg->getOption().getName();
446 continue;
447 }
448
449 // Build the pipeline for this file.
450 Action *Current = new InputAction(*InputArg, InputType);
451 for (unsigned i = 0; i != NumSteps; ++i) {
452 phases::ID Phase = types::getCompilationPhase(InputType, i);
453
454 // We are done if this step is past what the user requested.
455 if (Phase > FinalPhase)
456 break;
457
458 // Queue linker inputs.
459 if (Phase == phases::Link) {
460 assert(i + 1 == NumSteps && "linking must be final compilation step.");
461 LinkerInputs.push_back(Current);
462 Current = 0;
463 break;
464 }
465
466 // Otherwise construct the appropriate action.
467 Current = ConstructPhaseAction(Args, Phase, Current);
468 if (Current->getType() == types::TY_Nothing)
469 break;
470 }
471
472 // If we ended with something, add to the output list.
473 if (Current)
474 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000475 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000476
477 // Add a link action if necessary.
478 if (!LinkerInputs.empty())
479 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
480}
481
482Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
483 Action *Input) const {
484 // Build the appropriate action.
485 switch (Phase) {
486 case phases::Link: assert(0 && "link action invalid here.");
487 case phases::Preprocess: {
488 types::ID OutputTy = types::getPreprocessedType(Input->getType());
489 assert(OutputTy != types::TY_INVALID &&
490 "Cannot preprocess this input type!");
491 return new PreprocessJobAction(Input, OutputTy);
492 }
493 case phases::Precompile:
494 return new PrecompileJobAction(Input, types::TY_PCH);
495 case phases::Compile: {
496 if (Args.hasArg(options::OPT_fsyntax_only)) {
497 return new CompileJobAction(Input, types::TY_Nothing);
498 } else if (Args.hasArg(options::OPT__analyze)) {
499 return new AnalyzeJobAction(Input, types::TY_Plist);
500 } else if (Args.hasArg(options::OPT_emit_llvm)) {
501 types::ID Output =
502 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
503 return new CompileJobAction(Input, Output);
504 } else {
505 return new CompileJobAction(Input, types::TY_PP_Asm);
506 }
507 }
508 case phases::Assemble:
509 return new AssembleJobAction(Input, types::TY_Object);
510 }
511
512 assert(0 && "invalid phase in ConstructPhaseAction");
513 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000514}
515
Daniel Dunbarcb881672009-03-13 00:51:18 +0000516llvm::sys::Path Driver::GetFilePath(const char *Name) const {
517 // FIXME: Implement.
518 return llvm::sys::Path(Name);
519}
520
521llvm::sys::Path Driver::GetProgramPath(const char *Name) const {
522 // FIXME: Implement.
523 return llvm::sys::Path(Name);
524}
525
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000526HostInfo *Driver::GetHostInfo(const char *Triple) {
527 // Dice into arch, platform, and OS. This matches
528 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
529 // and missing fields are left empty.
530 std::string Arch, Platform, OS;
531
532 if (const char *ArchEnd = strchr(Triple, '-')) {
533 Arch = std::string(Triple, ArchEnd);
534
535 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
536 Platform = std::string(ArchEnd+1, PlatformEnd);
537 OS = PlatformEnd+1;
538 } else
539 Platform = ArchEnd+1;
540 } else
541 Arch = Triple;
542
543 if (memcmp(&Platform[0], "darwin", 6) == 0)
544 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
545
546 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
547}