blob: e02c283b7c5f8a5414370d046aa252a4031e90a9 [file] [log] [blame]
Daniel Dunbar3ede8d02009-03-02 19:59:07 +00001//===--- Driver.cpp - Clang GCC Compatible Driver -----------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000011
Daniel Dunbar53ec5522009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000018#include "clang/Driver/Job.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000019#include "clang/Driver/Option.h"
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000020#include "clang/Driver/Options.h"
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000021#include "clang/Driver/Tool.h"
22#include "clang/Driver/ToolChain.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000023#include "clang/Driver/Types.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000024
Daniel Dunbar13689542009-03-13 20:33:35 +000025#include "llvm/ADT/StringSet.h"
Daniel Dunbar06482622009-03-05 06:38:47 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbar53ec5522009-03-12 07:58:46 +000027#include "llvm/System/Path.h"
Daniel Dunbarba102132009-03-13 12:19:02 +000028
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000029#include "InputInfo.h"
30
Daniel Dunbarba102132009-03-13 12:19:02 +000031#include <map>
32
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000033using namespace clang::driver;
34
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000035Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000036 const char *_DefaultHostTriple,
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000037 const char *_DefaultImageName,
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000038 Diagnostic &_Diags)
39 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000040 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
Daniel Dunbarf353c8c2009-03-16 06:56:51 +000041 DefaultImageName(_DefaultImageName),
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000042 Host(0),
Daniel Dunbar365c02f2009-03-10 20:52:46 +000043 CCCIsCXX(false), CCCEcho(false),
Daniel Dunbar8b1604e2009-03-13 00:17:48 +000044 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false),
45 SuppressMissingInputWarning(false)
Daniel Dunbar365c02f2009-03-10 20:52:46 +000046{
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000047}
48
49Driver::~Driver() {
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000050 delete Opts;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000051}
52
Daniel Dunbar06482622009-03-05 06:38:47 +000053ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
54 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
55
Daniel Dunbarad2a9af2009-03-13 11:38:42 +000056 // FIXME: Handle '@' args (or at least error on them).
57
Daniel Dunbar06482622009-03-05 06:38:47 +000058 unsigned Index = 0, End = ArgEnd - ArgBegin;
59 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000060 // gcc's handling of empty arguments doesn't make
61 // sense, but this is not a common use case. :)
62 //
63 // We just ignore them here (note that other things may
64 // still take them as arguments).
65 if (Args->getArgString(Index)[0] == '\0') {
66 ++Index;
67 continue;
68 }
69
Daniel Dunbar06482622009-03-05 06:38:47 +000070 unsigned Prev = Index;
71 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000072 if (A) {
73 if (A->getOption().isUnsupported()) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +000074 Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName();
Daniel Dunbar53ec5522009-03-12 07:58:46 +000075 continue;
76 }
77
Daniel Dunbar06482622009-03-05 06:38:47 +000078 Args->append(A);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000079 }
Daniel Dunbar06482622009-03-05 06:38:47 +000080
81 assert(Index > Prev && "Parser failed to consume argument.");
82 }
83
84 return Args;
85}
86
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000087Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarcb881672009-03-13 00:51:18 +000088 // FIXME: Handle environment options which effect driver behavior,
89 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
90 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
91
92 // FIXME: What are we going to do with -V and -b?
93
94 // FIXME: Handle CCC_ADD_ARGS.
95
Daniel Dunbar365c02f2009-03-10 20:52:46 +000096 // FIXME: This stuff needs to go into the Compilation, not the
97 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +000098 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +000099
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000100 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000101 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000102
103 // Read -ccc args.
104 //
105 // FIXME: We need to figure out where this behavior should
106 // live. Most of it should be outside in the client; the parts that
107 // aren't should have proper options, either by introducing new ones
108 // or by overloading gcc ones like -V or -b.
109 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
110 const char *Opt = *Start + 5;
111
112 if (!strcmp(Opt, "print-options")) {
113 CCCPrintOptions = true;
114 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000115 CCCPrintActions = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000116 } else if (!strcmp(Opt, "cxx")) {
117 CCCIsCXX = true;
118 } else if (!strcmp(Opt, "echo")) {
119 CCCEcho = true;
120
121 } else if (!strcmp(Opt, "no-clang")) {
122 CCCNoClang = true;
123 } else if (!strcmp(Opt, "no-clang-cxx")) {
124 CCCNoClangCXX = true;
125 } else if (!strcmp(Opt, "no-clang-cpp")) {
126 CCCNoClangCPP = true;
127 } else if (!strcmp(Opt, "clang-archs")) {
128 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
129 const char *Cur = *++Start;
130
131 for (;;) {
132 const char *Next = strchr(Cur, ',');
133
134 if (Next) {
135 CCCClangArchs.insert(std::string(Cur, Next));
136 Cur = Next + 1;
137 } else {
138 CCCClangArchs.insert(std::string(Cur));
139 break;
140 }
141 }
142
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000143 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000144 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000145 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000146
147 } else {
148 // FIXME: Error handling.
149 llvm::errs() << "invalid option: " << *Start << "\n";
150 exit(1);
151 }
152 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000153
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000154 ArgList *Args = ParseArgStrings(Start, End);
155
Daniel Dunbarcb881672009-03-13 00:51:18 +0000156 Host = Driver::GetHostInfo(HostTriple);
157 DefaultToolChain = Host->getToolChain(*Args);
158
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000159 // FIXME: This behavior shouldn't be here.
160 if (CCCPrintOptions) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000161 PrintOptions(*Args);
Daniel Dunbarab835432009-03-13 17:24:34 +0000162 return 0;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000163 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000164
Daniel Dunbarcb881672009-03-13 00:51:18 +0000165 if (!HandleImmediateArgs(*Args))
166 return 0;
167
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000168 // Construct the list of abstract actions to perform for this
169 // compilation.
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000170 ActionList Actions;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000171 if (Host->useDriverDriver())
172 BuildUniversalActions(*Args, Actions);
173 else
174 BuildActions(*Args, Actions);
175
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000176 if (CCCPrintActions) {
Daniel Dunbarba102132009-03-13 12:19:02 +0000177 PrintActions(*Args, Actions);
Daniel Dunbarab835432009-03-13 17:24:34 +0000178 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000179 }
Daniel Dunbarab835432009-03-13 17:24:34 +0000180
Daniel Dunbar586dc232009-03-16 06:42:30 +0000181 // The compilation takes ownership of Args.
182 Compilation *C = new Compilation(*DefaultToolChain, Args);
183 BuildJobs(*C, Actions);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000184
185 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000186}
187
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000188void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000189 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000190 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000191 it != ie; ++it, ++i) {
192 Arg *A = *it;
193 llvm::errs() << "Option " << i << " - "
194 << "Name: \"" << A->getOption().getName() << "\", "
195 << "Values: {";
196 for (unsigned j = 0; j < A->getNumValues(); ++j) {
197 if (j)
198 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000199 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000200 }
201 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000202 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000203}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000204
Daniel Dunbarcb881672009-03-13 00:51:18 +0000205void Driver::PrintVersion() const {
206 // FIXME: Get a reasonable version number.
207
208 // FIXME: The following handlers should use a callback mechanism, we
209 // don't know what the client would like to do.
210 llvm::outs() << "ccc version 1.0" << "\n";
211}
212
213bool Driver::HandleImmediateArgs(const ArgList &Args) {
214 // The order these options are handled in in gcc is all over the
215 // place, but we don't expect inconsistencies w.r.t. that to matter
216 // in practice.
217 if (Args.hasArg(options::OPT_v) ||
218 Args.hasArg(options::OPT__HASH_HASH_HASH)) {
219 PrintVersion();
220 SuppressMissingInputWarning = true;
221 }
222
223 // FIXME: The following handlers should use a callback mechanism, we
224 // don't know what the client would like to do.
225 if (Arg *A = Args.getLastArg(options::OPT_print_file_name_EQ)) {
226 llvm::outs() << GetFilePath(A->getValue(Args)).toString() << "\n";
227 return false;
228 }
229
230 if (Arg *A = Args.getLastArg(options::OPT_print_prog_name_EQ)) {
231 llvm::outs() << GetProgramPath(A->getValue(Args)).toString() << "\n";
232 return false;
233 }
234
Daniel Dunbar41393402009-03-13 01:01:44 +0000235 if (Args.hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000236 llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n";
237 return false;
238 }
239
240 return true;
241}
242
Daniel Dunbarba102132009-03-13 12:19:02 +0000243static unsigned PrintActions1(const ArgList &Args,
244 Action *A,
245 std::map<Action*, unsigned> &Ids) {
246 if (Ids.count(A))
247 return Ids[A];
248
249 std::string str;
250 llvm::raw_string_ostream os(str);
251
252 os << Action::getClassName(A->getKind()) << ", ";
253 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000254 os << "\"" << IA->getInputArg().getValue(Args) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000255 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
256 os << "\"" << BIA->getArchName() << "\", "
257 << "{" << PrintActions1(Args, *BIA->begin(), Ids) << "}";
258 } else {
259 os << "{";
260 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
261 os << PrintActions1(Args, *it, Ids);
262 ++it;
263 if (it != ie)
264 os << ", ";
265 }
266 os << "}";
267 }
268
269 unsigned Id = Ids.size();
270 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000271 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000272 << types::getTypeName(A->getType()) << "\n";
273
274 return Id;
275}
276
277void Driver::PrintActions(const ArgList &Args,
278 const ActionList &Actions) const {
279 std::map<Action*, unsigned> Ids;
280 for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbarb269c322009-03-13 17:20:20 +0000281 it != ie; ++it)
Daniel Dunbarba102132009-03-13 12:19:02 +0000282 PrintActions1(Args, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000283}
284
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000285void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) const {
Daniel Dunbar13689542009-03-13 20:33:35 +0000286 // Collect the list of architectures. Duplicates are allowed, but
287 // should only be handled once (in the order seen).
288 llvm::StringSet<> ArchNames;
289 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000290 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
291 it != ie; ++it) {
292 Arg *A = *it;
293
294 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000295 const char *Name = A->getValue(Args);
296
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000297 // FIXME: We need to handle canonicalization of the specified
298 // arch?
299
Daniel Dunbar13689542009-03-13 20:33:35 +0000300 if (ArchNames.insert(Name))
301 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000302 }
303 }
304
305 // When there is no explicit arch for this platform, get one from
306 // the host so that -Xarch_ is handled correctly.
307 if (!Archs.size()) {
308 const char *Arch = Host->getArchName().c_str();
Daniel Dunbar13689542009-03-13 20:33:35 +0000309 Archs.push_back(Arch);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000310 }
311
312 // FIXME: We killed off some others but these aren't yet detected in
313 // a functional manner. If we added information to jobs about which
314 // "auxiliary" files they wrote then we could detect the conflict
315 // these cause downstream.
316 if (Archs.size() > 1) {
317 // No recovery needed, the point of this is just to prevent
318 // overwriting the same files.
319 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
320 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
321 << A->getOption().getName();
322 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
323 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
324 << A->getOption().getName();
325 }
326
327 ActionList SingleActions;
328 BuildActions(Args, SingleActions);
329
330 // Add in arch binding and lipo (if necessary) for every top level
331 // action.
332 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
333 Action *Act = SingleActions[i];
334
335 // Make sure we can lipo this kind of output. If not (and it is an
336 // actual output) then we disallow, since we can't create an
337 // output file with the right name without overwriting it. We
338 // could remove this oddity by just changing the output names to
339 // include the arch, which would also fix
340 // -save-temps. Compatibility wins for now.
341
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000342 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000343 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
344 << types::getTypeName(Act->getType());
345
346 ActionList Inputs;
Daniel Dunbar13689542009-03-13 20:33:35 +0000347 for (unsigned i = 0, e = Archs.size(); i != e; ++i )
348 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000349
350 // Lipo if necessary, We do it this way because we need to set the
351 // arch flag so that -Xarch_ gets overwritten.
352 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
353 Actions.append(Inputs.begin(), Inputs.end());
354 else
355 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
356 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000357}
358
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000359void Driver::BuildActions(ArgList &Args, ActionList &Actions) const {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000360 // Start by constructing the list of inputs and their types.
361
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000362 // Track the current user specified (-x) input. We also explicitly
363 // track the argument used to set the type; we only want to claim
364 // the type when we actually use it, so we warn about unused -x
365 // arguments.
366 types::ID InputType = types::TY_Nothing;
367 Arg *InputTypeArg = 0;
368
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000369 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
370 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
371 it != ie; ++it) {
372 Arg *A = *it;
373
374 if (isa<InputOption>(A->getOption())) {
375 const char *Value = A->getValue(Args);
376 types::ID Ty = types::TY_INVALID;
377
378 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000379 if (InputType == types::TY_Nothing) {
380 // If there was an explicit arg for this, claim it.
381 if (InputTypeArg)
382 InputTypeArg->claim();
383
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000384 // stdin must be handled specially.
385 if (memcmp(Value, "-", 2) == 0) {
386 // If running with -E, treat as a C input (this changes the
387 // builtin macros, for example). This may be overridden by
388 // -ObjC below.
389 //
390 // Otherwise emit an error but still use a valid type to
391 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000392 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000393 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000394 Ty = types::TY_C;
395 } else {
396 // Otherwise lookup by extension, and fallback to ObjectType
397 // if not found.
398 if (const char *Ext = strrchr(Value, '.'))
399 Ty = types::lookupTypeForExtension(Ext + 1);
400 if (Ty == types::TY_INVALID)
401 Ty = types::TY_Object;
402 }
403
404 // -ObjC and -ObjC++ override the default language, but only
405 // -for "source files". We just treat everything that isn't a
406 // -linker input as a source file.
407 //
408 // FIXME: Clean this up if we move the phase sequence into the
409 // type.
410 if (Ty != types::TY_Object) {
411 if (Args.hasArg(options::OPT_ObjC))
412 Ty = types::TY_ObjC;
413 else if (Args.hasArg(options::OPT_ObjCXX))
414 Ty = types::TY_ObjCXX;
415 }
416 } else {
417 assert(InputTypeArg && "InputType set w/o InputTypeArg");
418 InputTypeArg->claim();
419 Ty = InputType;
420 }
421
422 // Check that the file exists. It isn't clear this is worth
423 // doing, since the tool presumably does this anyway, and this
424 // just adds an extra stat to the equation, but this is gcc
425 // compatible.
Daniel Dunbar411f2e42009-03-15 01:40:22 +0000426 A->claim();
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000427 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000428 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000429 else
430 Inputs.push_back(std::make_pair(Ty, A));
431
432 } else if (A->getOption().isLinkerInput()) {
433 // Just treat as object type, we could make a special type for
434 // this if necessary.
Daniel Dunbar411f2e42009-03-15 01:40:22 +0000435 A->claim();
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000436 Inputs.push_back(std::make_pair(types::TY_Object, A));
437
438 } else if (A->getOption().getId() == options::OPT_x) {
439 InputTypeArg = A;
440 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
441
442 // Follow gcc behavior and treat as linker input for invalid -x
443 // options. Its not clear why we shouldn't just revert to
444 // unknown; but this isn't very important, we might as well be
445 // bug comatible.
446 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000447 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000448 InputType = types::TY_Object;
449 }
450 }
451 }
452
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000453 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000454 Diag(clang::diag::err_drv_no_input_files);
455 return;
456 }
457
458 // Determine which compilation mode we are in. We look for options
459 // which affect the phase, starting with the earliest phases, and
460 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000461 Arg *FinalPhaseArg = 0;
462 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000463
464 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000465 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
466 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
467 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
468 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000469
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000470 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
471 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
472 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
473 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_llvm)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000474 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
475 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000476
477 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000478 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
479 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000480
481 // Otherwise do everything.
482 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000483 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000484
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000485 // Reject -Z* at the top level, these options should never have been
486 // exposed by gcc.
487 if (Arg *A = Args.getLastArg(options::OPT_Z))
488 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
489
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000490 // Construct the actions to perform.
491 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000492 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000493 types::ID InputType = Inputs[i].first;
494 const Arg *InputArg = Inputs[i].second;
495
496 unsigned NumSteps = types::getNumCompilationPhases(InputType);
497 assert(NumSteps && "Invalid number of steps!");
498
499 // If the first step comes after the final phase we are doing as
500 // part of this compilation, warn the user about it.
501 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
502 if (InitialPhase > FinalPhase) {
503 Diag(clang::diag::warn_drv_input_file_unused)
504 << InputArg->getValue(Args)
505 << getPhaseName(InitialPhase)
506 << FinalPhaseArg->getOption().getName();
507 continue;
508 }
509
510 // Build the pipeline for this file.
511 Action *Current = new InputAction(*InputArg, InputType);
512 for (unsigned i = 0; i != NumSteps; ++i) {
513 phases::ID Phase = types::getCompilationPhase(InputType, i);
514
515 // We are done if this step is past what the user requested.
516 if (Phase > FinalPhase)
517 break;
518
519 // Queue linker inputs.
520 if (Phase == phases::Link) {
521 assert(i + 1 == NumSteps && "linking must be final compilation step.");
522 LinkerInputs.push_back(Current);
523 Current = 0;
524 break;
525 }
526
527 // Otherwise construct the appropriate action.
528 Current = ConstructPhaseAction(Args, Phase, Current);
529 if (Current->getType() == types::TY_Nothing)
530 break;
531 }
532
533 // If we ended with something, add to the output list.
534 if (Current)
535 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000536 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000537
538 // Add a link action if necessary.
539 if (!LinkerInputs.empty())
540 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
541}
542
543Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
544 Action *Input) const {
545 // Build the appropriate action.
546 switch (Phase) {
547 case phases::Link: assert(0 && "link action invalid here.");
548 case phases::Preprocess: {
549 types::ID OutputTy = types::getPreprocessedType(Input->getType());
550 assert(OutputTy != types::TY_INVALID &&
551 "Cannot preprocess this input type!");
552 return new PreprocessJobAction(Input, OutputTy);
553 }
554 case phases::Precompile:
555 return new PrecompileJobAction(Input, types::TY_PCH);
556 case phases::Compile: {
557 if (Args.hasArg(options::OPT_fsyntax_only)) {
558 return new CompileJobAction(Input, types::TY_Nothing);
559 } else if (Args.hasArg(options::OPT__analyze)) {
560 return new AnalyzeJobAction(Input, types::TY_Plist);
561 } else if (Args.hasArg(options::OPT_emit_llvm)) {
562 types::ID Output =
563 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
564 return new CompileJobAction(Input, Output);
565 } else {
566 return new CompileJobAction(Input, types::TY_PP_Asm);
567 }
568 }
569 case phases::Assemble:
570 return new AssembleJobAction(Input, types::TY_Object);
571 }
572
573 assert(0 && "invalid phase in ConstructPhaseAction");
574 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000575}
576
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000577void Driver::BuildJobs(Compilation &C, const ActionList &Actions) const {
578 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
579 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
580
581 // -save-temps inhibits pipes.
582 if (SaveTemps && UsePipes) {
583 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
584 UsePipes = true;
585 }
586
587 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
588
589 // It is an error to provide a -o option if we are making multiple
590 // output files.
591 if (FinalOutput) {
592 unsigned NumOutputs = 0;
593 for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end();
594 it != ie; ++it)
595 if ((*it)->getType() != types::TY_Nothing)
596 ++NumOutputs;
597
598 if (NumOutputs > 1) {
599 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
600 FinalOutput = 0;
601 }
602 }
603
604 for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end();
605 it != ie; ++it) {
606 Action *A = *it;
607
608 // If we are linking an image for multiple archs then the linker
609 // wants -arch_multiple and -final_output <final image
610 // name>. Unfortunately, this doesn't fit in cleanly because we
611 // have to pass this information down.
612 //
613 // FIXME: This is a hack; find a cleaner way to integrate this
614 // into the process.
615 const char *LinkingOutput = 0;
616 if (isa<LinkJobAction>(A)) {
617 if (FinalOutput)
618 LinkingOutput = FinalOutput->getValue(C.getArgs());
619 else
620 LinkingOutput = DefaultImageName.c_str();
621 }
622
623 InputInfo II;
624 BuildJobsForAction(C,
625 A, DefaultToolChain,
626 /*CanAcceptPipe*/ true,
627 /*AtTopLevel*/ true,
628 /*LinkingOutput*/ LinkingOutput,
629 II);
630 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000631
632 // If there were no errors, warn about any unused arguments.
633 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
634 it != ie; ++it) {
635 Arg *A = *it;
636
637 // FIXME: It would be nice to be able to send the argument to the
638 // Diagnostic, so that extra values, position, and so on could be
639 // printed.
640 if (!A->isClaimed())
641 Diag(clang::diag::warn_drv_unused_argument)
642 << A->getOption().getName();
643 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000644}
645
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000646void Driver::BuildJobsForAction(Compilation &C,
647 const Action *A,
648 const ToolChain *TC,
649 bool CanAcceptPipe,
650 bool AtTopLevel,
651 const char *LinkingOutput,
652 InputInfo &Result) const {
653 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
654 const char *Name = IA->getInputArg().getValue(C.getArgs());
655 Result = InputInfo(Name, A->getType(), Name);
656 return;
657 }
658
659 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
660 const char *ArchName = BAA->getArchName();
661 BuildJobsForAction(C,
662 *BAA->begin(),
663 Host->getToolChain(C.getArgs(), ArchName),
664 CanAcceptPipe,
665 AtTopLevel,
666 LinkingOutput,
667 Result);
668 return;
669 }
670
671 const JobAction *JA = cast<JobAction>(A);
672 const Tool &T = TC->SelectTool(C, *JA);
673
674 // See if we should use an integrated preprocessor. We do so when we
675 // have exactly one input, since this is the only use case we care
676 // about (irrelevant since we don't support combine yet).
677 bool UseIntegratedCPP = false;
678 const ActionList *Inputs = &A->getInputs();
679 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
680 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
681 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
682 !C.getArgs().hasArg(options::OPT_save_temps) &&
683 T.hasIntegratedCPP()) {
684 UseIntegratedCPP = true;
685 Inputs = &(*Inputs)[0]->getInputs();
686 }
687 }
688
689 // Only use pipes when there is exactly one input.
690 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
691 llvm::SmallVector<InputInfo, 4> InputInfos;
692 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
693 it != ie; ++it) {
694 InputInfo II;
695 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
696 /*AtTopLevel*/false,
697 LinkingOutput,
698 II);
699 InputInfos.push_back(II);
700 }
701
702 // Determine if we should output to a pipe.
703 bool OutputToPipe = false;
704 if (CanAcceptPipe && T.canPipeOutput()) {
705 // Some actions default to writing to a pipe if they are the top
706 // level phase and there was no user override.
707 //
708 // FIXME: Is there a better way to handle this?
709 if (AtTopLevel) {
710 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
711 OutputToPipe = true;
712 } else if (C.getArgs().hasArg(options::OPT_pipe))
713 OutputToPipe = true;
714 }
715
716 // Figure out where to put the job (pipes).
717 Job *Dest = &C.getJobs();
718 if (InputInfos[0].isPipe()) {
719 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
720 Dest = &InputInfos[0].getPipe();
721 }
722
723 // Always use the first input as the base input.
724 const char *BaseInput = InputInfos[0].getBaseInput();
725 const char *Output = "foo";
726 // FIXME: Make the job.
727
728 Result = InputInfo(Output, A->getType(), BaseInput);
729}
730
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000731llvm::sys::Path Driver::GetFilePath(const char *Name,
732 const ToolChain *TC) const {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000733 // FIXME: Implement.
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000734 if (!TC) TC = DefaultToolChain;
735
Daniel Dunbarcb881672009-03-13 00:51:18 +0000736 return llvm::sys::Path(Name);
737}
738
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000739llvm::sys::Path Driver::GetProgramPath(const char *Name,
740 const ToolChain *TC) const {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000741 // FIXME: Implement.
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000742 if (!TC) TC = DefaultToolChain;
743
Daniel Dunbarcb881672009-03-13 00:51:18 +0000744 return llvm::sys::Path(Name);
745}
746
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000747HostInfo *Driver::GetHostInfo(const char *Triple) {
748 // Dice into arch, platform, and OS. This matches
749 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
750 // and missing fields are left empty.
751 std::string Arch, Platform, OS;
752
753 if (const char *ArchEnd = strchr(Triple, '-')) {
754 Arch = std::string(Triple, ArchEnd);
755
756 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
757 Platform = std::string(ArchEnd+1, PlatformEnd);
758 OS = PlatformEnd+1;
759 } else
760 Platform = ArchEnd+1;
761 } else
762 Arch = Triple;
763
Daniel Dunbara88162c2009-03-13 12:23:29 +0000764 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000765 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
766
767 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
768}