blob: 980512285da41fa90339e8023fe1a84299c90b39 [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.");
Daniel Dunbar70c16842009-03-17 04:12:06 +000082 (void) Prev;
Daniel Dunbar06482622009-03-05 06:38:47 +000083 }
84
85 return Args;
86}
87
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000088Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarcb881672009-03-13 00:51:18 +000089 // FIXME: Handle environment options which effect driver behavior,
90 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
91 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
92
93 // FIXME: What are we going to do with -V and -b?
94
95 // FIXME: Handle CCC_ADD_ARGS.
96
Daniel Dunbar365c02f2009-03-10 20:52:46 +000097 // FIXME: This stuff needs to go into the Compilation, not the
98 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +000099 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +0000100
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000101 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000102 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000103
104 // Read -ccc args.
105 //
106 // FIXME: We need to figure out where this behavior should
107 // live. Most of it should be outside in the client; the parts that
108 // aren't should have proper options, either by introducing new ones
109 // or by overloading gcc ones like -V or -b.
110 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
111 const char *Opt = *Start + 5;
112
113 if (!strcmp(Opt, "print-options")) {
114 CCCPrintOptions = true;
115 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000116 CCCPrintActions = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000117 } else if (!strcmp(Opt, "cxx")) {
118 CCCIsCXX = true;
119 } else if (!strcmp(Opt, "echo")) {
120 CCCEcho = true;
121
122 } else if (!strcmp(Opt, "no-clang")) {
123 CCCNoClang = true;
124 } else if (!strcmp(Opt, "no-clang-cxx")) {
125 CCCNoClangCXX = true;
126 } else if (!strcmp(Opt, "no-clang-cpp")) {
127 CCCNoClangCPP = true;
128 } else if (!strcmp(Opt, "clang-archs")) {
129 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
130 const char *Cur = *++Start;
131
132 for (;;) {
133 const char *Next = strchr(Cur, ',');
134
135 if (Next) {
136 CCCClangArchs.insert(std::string(Cur, Next));
137 Cur = Next + 1;
138 } else {
139 CCCClangArchs.insert(std::string(Cur));
140 break;
141 }
142 }
143
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000144 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000145 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000146 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000147
148 } else {
149 // FIXME: Error handling.
150 llvm::errs() << "invalid option: " << *Start << "\n";
151 exit(1);
152 }
153 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000154
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000155 ArgList *Args = ParseArgStrings(Start, End);
156
Daniel Dunbarcb881672009-03-13 00:51:18 +0000157 Host = Driver::GetHostInfo(HostTriple);
158 DefaultToolChain = Host->getToolChain(*Args);
159
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000160 // FIXME: This behavior shouldn't be here.
161 if (CCCPrintOptions) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000162 PrintOptions(*Args);
Daniel Dunbarab835432009-03-13 17:24:34 +0000163 return 0;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000164 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000165
Daniel Dunbarcb881672009-03-13 00:51:18 +0000166 if (!HandleImmediateArgs(*Args))
167 return 0;
168
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000169 // Construct the list of abstract actions to perform for this
170 // compilation.
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000171 ActionList Actions;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000172 if (Host->useDriverDriver())
173 BuildUniversalActions(*Args, Actions);
174 else
175 BuildActions(*Args, Actions);
176
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000177 if (CCCPrintActions) {
Daniel Dunbarba102132009-03-13 12:19:02 +0000178 PrintActions(*Args, Actions);
Daniel Dunbarab835432009-03-13 17:24:34 +0000179 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000180 }
Daniel Dunbarab835432009-03-13 17:24:34 +0000181
Daniel Dunbar586dc232009-03-16 06:42:30 +0000182 // The compilation takes ownership of Args.
183 Compilation *C = new Compilation(*DefaultToolChain, Args);
184 BuildJobs(*C, Actions);
Daniel Dunbar8d2554a2009-03-15 01:38:15 +0000185
186 return C;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000187}
188
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000189void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000190 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000191 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000192 it != ie; ++it, ++i) {
193 Arg *A = *it;
194 llvm::errs() << "Option " << i << " - "
195 << "Name: \"" << A->getOption().getName() << "\", "
196 << "Values: {";
197 for (unsigned j = 0; j < A->getNumValues(); ++j) {
198 if (j)
199 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000200 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000201 }
202 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000203 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000204}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000205
Daniel Dunbarcb881672009-03-13 00:51:18 +0000206void Driver::PrintVersion() const {
207 // FIXME: Get a reasonable version number.
208
209 // FIXME: The following handlers should use a callback mechanism, we
210 // don't know what the client would like to do.
211 llvm::outs() << "ccc version 1.0" << "\n";
212}
213
214bool Driver::HandleImmediateArgs(const ArgList &Args) {
215 // The order these options are handled in in gcc is all over the
216 // place, but we don't expect inconsistencies w.r.t. that to matter
217 // in practice.
218 if (Args.hasArg(options::OPT_v) ||
219 Args.hasArg(options::OPT__HASH_HASH_HASH)) {
220 PrintVersion();
221 SuppressMissingInputWarning = true;
222 }
223
224 // FIXME: The following handlers should use a callback mechanism, we
225 // don't know what the client would like to do.
226 if (Arg *A = Args.getLastArg(options::OPT_print_file_name_EQ)) {
227 llvm::outs() << GetFilePath(A->getValue(Args)).toString() << "\n";
228 return false;
229 }
230
231 if (Arg *A = Args.getLastArg(options::OPT_print_prog_name_EQ)) {
232 llvm::outs() << GetProgramPath(A->getValue(Args)).toString() << "\n";
233 return false;
234 }
235
Daniel Dunbar41393402009-03-13 01:01:44 +0000236 if (Args.hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000237 llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n";
238 return false;
239 }
240
241 return true;
242}
243
Daniel Dunbarba102132009-03-13 12:19:02 +0000244static unsigned PrintActions1(const ArgList &Args,
245 Action *A,
246 std::map<Action*, unsigned> &Ids) {
247 if (Ids.count(A))
248 return Ids[A];
249
250 std::string str;
251 llvm::raw_string_ostream os(str);
252
253 os << Action::getClassName(A->getKind()) << ", ";
254 if (InputAction *IA = dyn_cast<InputAction>(A)) {
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000255 os << "\"" << IA->getInputArg().getValue(Args) << "\"";
Daniel Dunbarba102132009-03-13 12:19:02 +0000256 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
257 os << "\"" << BIA->getArchName() << "\", "
258 << "{" << PrintActions1(Args, *BIA->begin(), Ids) << "}";
259 } else {
260 os << "{";
261 for (Action::iterator it = A->begin(), ie = A->end(); it != ie;) {
262 os << PrintActions1(Args, *it, Ids);
263 ++it;
264 if (it != ie)
265 os << ", ";
266 }
267 os << "}";
268 }
269
270 unsigned Id = Ids.size();
271 Ids[A] = Id;
Daniel Dunbarb269c322009-03-13 17:20:20 +0000272 llvm::errs() << Id << ": " << os.str() << ", "
Daniel Dunbarba102132009-03-13 12:19:02 +0000273 << types::getTypeName(A->getType()) << "\n";
274
275 return Id;
276}
277
278void Driver::PrintActions(const ArgList &Args,
279 const ActionList &Actions) const {
280 std::map<Action*, unsigned> Ids;
281 for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbarb269c322009-03-13 17:20:20 +0000282 it != ie; ++it)
Daniel Dunbarba102132009-03-13 12:19:02 +0000283 PrintActions1(Args, *it, Ids);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000284}
285
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000286void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) const {
Daniel Dunbar13689542009-03-13 20:33:35 +0000287 // Collect the list of architectures. Duplicates are allowed, but
288 // should only be handled once (in the order seen).
289 llvm::StringSet<> ArchNames;
290 llvm::SmallVector<const char *, 4> Archs;
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000291 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
292 it != ie; ++it) {
293 Arg *A = *it;
294
295 if (A->getOption().getId() == options::OPT_arch) {
Daniel Dunbar13689542009-03-13 20:33:35 +0000296 const char *Name = A->getValue(Args);
297
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000298 // FIXME: We need to handle canonicalization of the specified
299 // arch?
300
Daniel Dunbar13689542009-03-13 20:33:35 +0000301 if (ArchNames.insert(Name))
302 Archs.push_back(Name);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000303 }
304 }
305
306 // When there is no explicit arch for this platform, get one from
307 // the host so that -Xarch_ is handled correctly.
308 if (!Archs.size()) {
309 const char *Arch = Host->getArchName().c_str();
Daniel Dunbar13689542009-03-13 20:33:35 +0000310 Archs.push_back(Arch);
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000311 }
312
313 // FIXME: We killed off some others but these aren't yet detected in
314 // a functional manner. If we added information to jobs about which
315 // "auxiliary" files they wrote then we could detect the conflict
316 // these cause downstream.
317 if (Archs.size() > 1) {
318 // No recovery needed, the point of this is just to prevent
319 // overwriting the same files.
320 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
321 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
322 << A->getOption().getName();
323 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
324 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
325 << A->getOption().getName();
326 }
327
328 ActionList SingleActions;
329 BuildActions(Args, SingleActions);
330
331 // Add in arch binding and lipo (if necessary) for every top level
332 // action.
333 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
334 Action *Act = SingleActions[i];
335
336 // Make sure we can lipo this kind of output. If not (and it is an
337 // actual output) then we disallow, since we can't create an
338 // output file with the right name without overwriting it. We
339 // could remove this oddity by just changing the output names to
340 // include the arch, which would also fix
341 // -save-temps. Compatibility wins for now.
342
Daniel Dunbar3dbd6c52009-03-13 17:46:02 +0000343 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000344 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
345 << types::getTypeName(Act->getType());
346
347 ActionList Inputs;
Daniel Dunbar13689542009-03-13 20:33:35 +0000348 for (unsigned i = 0, e = Archs.size(); i != e; ++i )
349 Inputs.push_back(new BindArchAction(Act, Archs[i]));
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000350
351 // Lipo if necessary, We do it this way because we need to set the
352 // arch flag so that -Xarch_ gets overwritten.
353 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
354 Actions.append(Inputs.begin(), Inputs.end());
355 else
356 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
357 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000358}
359
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000360void Driver::BuildActions(ArgList &Args, ActionList &Actions) const {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000361 // Start by constructing the list of inputs and their types.
362
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000363 // Track the current user specified (-x) input. We also explicitly
364 // track the argument used to set the type; we only want to claim
365 // the type when we actually use it, so we warn about unused -x
366 // arguments.
367 types::ID InputType = types::TY_Nothing;
368 Arg *InputTypeArg = 0;
369
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000370 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
371 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
372 it != ie; ++it) {
373 Arg *A = *it;
374
375 if (isa<InputOption>(A->getOption())) {
376 const char *Value = A->getValue(Args);
377 types::ID Ty = types::TY_INVALID;
378
379 // Infer the input type if necessary.
Daniel Dunbar83dd21f2009-03-13 17:57:10 +0000380 if (InputType == types::TY_Nothing) {
381 // If there was an explicit arg for this, claim it.
382 if (InputTypeArg)
383 InputTypeArg->claim();
384
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000385 // stdin must be handled specially.
386 if (memcmp(Value, "-", 2) == 0) {
387 // If running with -E, treat as a C input (this changes the
388 // builtin macros, for example). This may be overridden by
389 // -ObjC below.
390 //
391 // Otherwise emit an error but still use a valid type to
392 // avoid spurious errors (e.g., no inputs).
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000393 if (!Args.hasArg(options::OPT_E, false))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000394 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000395 Ty = types::TY_C;
396 } else {
397 // Otherwise lookup by extension, and fallback to ObjectType
398 // if not found.
399 if (const char *Ext = strrchr(Value, '.'))
400 Ty = types::lookupTypeForExtension(Ext + 1);
401 if (Ty == types::TY_INVALID)
402 Ty = types::TY_Object;
403 }
404
405 // -ObjC and -ObjC++ override the default language, but only
406 // -for "source files". We just treat everything that isn't a
407 // -linker input as a source file.
408 //
409 // FIXME: Clean this up if we move the phase sequence into the
410 // type.
411 if (Ty != types::TY_Object) {
412 if (Args.hasArg(options::OPT_ObjC))
413 Ty = types::TY_ObjC;
414 else if (Args.hasArg(options::OPT_ObjCXX))
415 Ty = types::TY_ObjCXX;
416 }
417 } else {
418 assert(InputTypeArg && "InputType set w/o InputTypeArg");
419 InputTypeArg->claim();
420 Ty = InputType;
421 }
422
423 // Check that the file exists. It isn't clear this is worth
424 // doing, since the tool presumably does this anyway, and this
425 // just adds an extra stat to the equation, but this is gcc
426 // compatible.
Daniel Dunbar411f2e42009-03-15 01:40:22 +0000427 A->claim();
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000428 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000429 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000430 else
431 Inputs.push_back(std::make_pair(Ty, A));
432
433 } else if (A->getOption().isLinkerInput()) {
434 // Just treat as object type, we could make a special type for
435 // this if necessary.
Daniel Dunbar411f2e42009-03-15 01:40:22 +0000436 A->claim();
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000437 Inputs.push_back(std::make_pair(types::TY_Object, A));
438
439 } else if (A->getOption().getId() == options::OPT_x) {
440 InputTypeArg = A;
441 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
442
443 // Follow gcc behavior and treat as linker input for invalid -x
444 // options. Its not clear why we shouldn't just revert to
445 // unknown; but this isn't very important, we might as well be
446 // bug comatible.
447 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000448 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000449 InputType = types::TY_Object;
450 }
451 }
452 }
453
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000454 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000455 Diag(clang::diag::err_drv_no_input_files);
456 return;
457 }
458
459 // Determine which compilation mode we are in. We look for options
460 // which affect the phase, starting with the earliest phases, and
461 // record which option we used to determine the final phase.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000462 Arg *FinalPhaseArg = 0;
463 phases::ID FinalPhase;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000464
465 // -{E,M,MM} only run the preprocessor.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000466 if ((FinalPhaseArg = Args.getLastArg(options::OPT_E)) ||
467 (FinalPhaseArg = Args.getLastArg(options::OPT_M)) ||
468 (FinalPhaseArg = Args.getLastArg(options::OPT_MM))) {
469 FinalPhase = phases::Preprocess;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000470
Daniel Dunbar8022fd42009-03-15 00:48:16 +0000471 // -{fsyntax-only,-analyze,emit-llvm,S} only run up to the compiler.
472 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_fsyntax_only)) ||
473 (FinalPhaseArg = Args.getLastArg(options::OPT__analyze)) ||
474 (FinalPhaseArg = Args.getLastArg(options::OPT_emit_llvm)) ||
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000475 (FinalPhaseArg = Args.getLastArg(options::OPT_S))) {
476 FinalPhase = phases::Compile;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000477
478 // -c only runs up to the assembler.
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000479 } else if ((FinalPhaseArg = Args.getLastArg(options::OPT_c))) {
480 FinalPhase = phases::Assemble;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000481
482 // Otherwise do everything.
483 } else
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000484 FinalPhase = phases::Link;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000485
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000486 // Reject -Z* at the top level, these options should never have been
487 // exposed by gcc.
488 if (Arg *A = Args.getLastArg(options::OPT_Z))
489 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
490
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000491 // Construct the actions to perform.
492 ActionList LinkerInputs;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000493 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000494 types::ID InputType = Inputs[i].first;
495 const Arg *InputArg = Inputs[i].second;
496
497 unsigned NumSteps = types::getNumCompilationPhases(InputType);
498 assert(NumSteps && "Invalid number of steps!");
499
500 // If the first step comes after the final phase we are doing as
501 // part of this compilation, warn the user about it.
502 phases::ID InitialPhase = types::getCompilationPhase(InputType, 0);
503 if (InitialPhase > FinalPhase) {
504 Diag(clang::diag::warn_drv_input_file_unused)
505 << InputArg->getValue(Args)
506 << getPhaseName(InitialPhase)
507 << FinalPhaseArg->getOption().getName();
508 continue;
509 }
510
511 // Build the pipeline for this file.
512 Action *Current = new InputAction(*InputArg, InputType);
513 for (unsigned i = 0; i != NumSteps; ++i) {
514 phases::ID Phase = types::getCompilationPhase(InputType, i);
515
516 // We are done if this step is past what the user requested.
517 if (Phase > FinalPhase)
518 break;
519
520 // Queue linker inputs.
521 if (Phase == phases::Link) {
522 assert(i + 1 == NumSteps && "linking must be final compilation step.");
523 LinkerInputs.push_back(Current);
524 Current = 0;
525 break;
526 }
527
528 // Otherwise construct the appropriate action.
529 Current = ConstructPhaseAction(Args, Phase, Current);
530 if (Current->getType() == types::TY_Nothing)
531 break;
532 }
533
534 // If we ended with something, add to the output list.
535 if (Current)
536 Actions.push_back(Current);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000537 }
Daniel Dunbarad2a9af2009-03-13 11:38:42 +0000538
539 // Add a link action if necessary.
540 if (!LinkerInputs.empty())
541 Actions.push_back(new LinkJobAction(LinkerInputs, types::TY_Image));
542}
543
544Action *Driver::ConstructPhaseAction(const ArgList &Args, phases::ID Phase,
545 Action *Input) const {
546 // Build the appropriate action.
547 switch (Phase) {
548 case phases::Link: assert(0 && "link action invalid here.");
549 case phases::Preprocess: {
550 types::ID OutputTy = types::getPreprocessedType(Input->getType());
551 assert(OutputTy != types::TY_INVALID &&
552 "Cannot preprocess this input type!");
553 return new PreprocessJobAction(Input, OutputTy);
554 }
555 case phases::Precompile:
556 return new PrecompileJobAction(Input, types::TY_PCH);
557 case phases::Compile: {
558 if (Args.hasArg(options::OPT_fsyntax_only)) {
559 return new CompileJobAction(Input, types::TY_Nothing);
560 } else if (Args.hasArg(options::OPT__analyze)) {
561 return new AnalyzeJobAction(Input, types::TY_Plist);
562 } else if (Args.hasArg(options::OPT_emit_llvm)) {
563 types::ID Output =
564 Args.hasArg(options::OPT_S) ? types::TY_LLVMAsm : types::TY_LLVMBC;
565 return new CompileJobAction(Input, Output);
566 } else {
567 return new CompileJobAction(Input, types::TY_PP_Asm);
568 }
569 }
570 case phases::Assemble:
571 return new AssembleJobAction(Input, types::TY_Object);
572 }
573
574 assert(0 && "invalid phase in ConstructPhaseAction");
575 return 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000576}
577
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000578void Driver::BuildJobs(Compilation &C, const ActionList &Actions) const {
579 bool SaveTemps = C.getArgs().hasArg(options::OPT_save_temps);
580 bool UsePipes = C.getArgs().hasArg(options::OPT_pipe);
581
582 // -save-temps inhibits pipes.
583 if (SaveTemps && UsePipes) {
584 Diag(clang::diag::warn_drv_pipe_ignored_with_save_temps);
585 UsePipes = true;
586 }
587
588 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
589
590 // It is an error to provide a -o option if we are making multiple
591 // output files.
592 if (FinalOutput) {
593 unsigned NumOutputs = 0;
594 for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end();
595 it != ie; ++it)
596 if ((*it)->getType() != types::TY_Nothing)
597 ++NumOutputs;
598
599 if (NumOutputs > 1) {
600 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
601 FinalOutput = 0;
602 }
603 }
604
605 for (ActionList::const_iterator it = Actions.begin(), ie = Actions.end();
606 it != ie; ++it) {
607 Action *A = *it;
608
609 // If we are linking an image for multiple archs then the linker
610 // wants -arch_multiple and -final_output <final image
611 // name>. Unfortunately, this doesn't fit in cleanly because we
612 // have to pass this information down.
613 //
614 // FIXME: This is a hack; find a cleaner way to integrate this
615 // into the process.
616 const char *LinkingOutput = 0;
617 if (isa<LinkJobAction>(A)) {
618 if (FinalOutput)
619 LinkingOutput = FinalOutput->getValue(C.getArgs());
620 else
621 LinkingOutput = DefaultImageName.c_str();
622 }
623
624 InputInfo II;
625 BuildJobsForAction(C,
626 A, DefaultToolChain,
627 /*CanAcceptPipe*/ true,
628 /*AtTopLevel*/ true,
629 /*LinkingOutput*/ LinkingOutput,
630 II);
631 }
Daniel Dunbar586dc232009-03-16 06:42:30 +0000632
633 // If there were no errors, warn about any unused arguments.
634 for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
635 it != ie; ++it) {
636 Arg *A = *it;
637
638 // FIXME: It would be nice to be able to send the argument to the
639 // Diagnostic, so that extra values, position, and so on could be
640 // printed.
641 if (!A->isClaimed())
642 Diag(clang::diag::warn_drv_unused_argument)
643 << A->getOption().getName();
644 }
Daniel Dunbar57b704d2009-03-13 22:12:33 +0000645}
646
Daniel Dunbarf353c8c2009-03-16 06:56:51 +0000647void Driver::BuildJobsForAction(Compilation &C,
648 const Action *A,
649 const ToolChain *TC,
650 bool CanAcceptPipe,
651 bool AtTopLevel,
652 const char *LinkingOutput,
653 InputInfo &Result) const {
654 if (const InputAction *IA = dyn_cast<InputAction>(A)) {
655 const char *Name = IA->getInputArg().getValue(C.getArgs());
656 Result = InputInfo(Name, A->getType(), Name);
657 return;
658 }
659
660 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
661 const char *ArchName = BAA->getArchName();
662 BuildJobsForAction(C,
663 *BAA->begin(),
664 Host->getToolChain(C.getArgs(), ArchName),
665 CanAcceptPipe,
666 AtTopLevel,
667 LinkingOutput,
668 Result);
669 return;
670 }
671
672 const JobAction *JA = cast<JobAction>(A);
673 const Tool &T = TC->SelectTool(C, *JA);
674
675 // See if we should use an integrated preprocessor. We do so when we
676 // have exactly one input, since this is the only use case we care
677 // about (irrelevant since we don't support combine yet).
678 bool UseIntegratedCPP = false;
679 const ActionList *Inputs = &A->getInputs();
680 if (Inputs->size() == 1 && isa<PreprocessJobAction>(*Inputs->begin())) {
681 if (!C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
682 !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
683 !C.getArgs().hasArg(options::OPT_save_temps) &&
684 T.hasIntegratedCPP()) {
685 UseIntegratedCPP = true;
686 Inputs = &(*Inputs)[0]->getInputs();
687 }
688 }
689
690 // Only use pipes when there is exactly one input.
691 bool TryToUsePipeInput = Inputs->size() == 1 && T.acceptsPipedInput();
692 llvm::SmallVector<InputInfo, 4> InputInfos;
693 for (ActionList::const_iterator it = Inputs->begin(), ie = Inputs->end();
694 it != ie; ++it) {
695 InputInfo II;
696 BuildJobsForAction(C, *it, TC, TryToUsePipeInput,
697 /*AtTopLevel*/false,
698 LinkingOutput,
699 II);
700 InputInfos.push_back(II);
701 }
702
703 // Determine if we should output to a pipe.
704 bool OutputToPipe = false;
705 if (CanAcceptPipe && T.canPipeOutput()) {
706 // Some actions default to writing to a pipe if they are the top
707 // level phase and there was no user override.
708 //
709 // FIXME: Is there a better way to handle this?
710 if (AtTopLevel) {
711 if (isa<PreprocessJobAction>(A) && !C.getArgs().hasArg(options::OPT_o))
712 OutputToPipe = true;
713 } else if (C.getArgs().hasArg(options::OPT_pipe))
714 OutputToPipe = true;
715 }
716
717 // Figure out where to put the job (pipes).
718 Job *Dest = &C.getJobs();
719 if (InputInfos[0].isPipe()) {
720 assert(InputInfos.size() == 1 && "Unexpected pipe with multiple inputs.");
721 Dest = &InputInfos[0].getPipe();
722 }
723
724 // Always use the first input as the base input.
725 const char *BaseInput = InputInfos[0].getBaseInput();
726 const char *Output = "foo";
727 // FIXME: Make the job.
728
729 Result = InputInfo(Output, A->getType(), BaseInput);
730}
731
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000732llvm::sys::Path Driver::GetFilePath(const char *Name,
733 const ToolChain *TC) const {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000734 // FIXME: Implement.
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000735 if (!TC) TC = DefaultToolChain;
736
Daniel Dunbarcb881672009-03-13 00:51:18 +0000737 return llvm::sys::Path(Name);
738}
739
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000740llvm::sys::Path Driver::GetProgramPath(const char *Name,
741 const ToolChain *TC) const {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000742 // FIXME: Implement.
Daniel Dunbar2ba38ba2009-03-16 05:25:36 +0000743 if (!TC) TC = DefaultToolChain;
744
Daniel Dunbarcb881672009-03-13 00:51:18 +0000745 return llvm::sys::Path(Name);
746}
747
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000748HostInfo *Driver::GetHostInfo(const char *Triple) {
749 // Dice into arch, platform, and OS. This matches
750 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
751 // and missing fields are left empty.
752 std::string Arch, Platform, OS;
753
754 if (const char *ArchEnd = strchr(Triple, '-')) {
755 Arch = std::string(Triple, ArchEnd);
756
757 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
758 Platform = std::string(ArchEnd+1, PlatformEnd);
759 OS = PlatformEnd+1;
760 } else
761 Platform = ArchEnd+1;
762 } else
763 Arch = Triple;
764
Daniel Dunbara88162c2009-03-13 12:23:29 +0000765 if (memcmp(&OS[0], "darwin", 6) == 0)
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000766 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
767
768 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
769}