blob: 4fc03ce76593d57c0086ff82f22c61221efa2d2b [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
46 unsigned Index = 0, End = ArgEnd - ArgBegin;
47 while (Index < End) {
Daniel Dunbar41393402009-03-13 01:01:44 +000048 // gcc's handling of empty arguments doesn't make
49 // sense, but this is not a common use case. :)
50 //
51 // We just ignore them here (note that other things may
52 // still take them as arguments).
53 if (Args->getArgString(Index)[0] == '\0') {
54 ++Index;
55 continue;
56 }
57
Daniel Dunbar06482622009-03-05 06:38:47 +000058 unsigned Prev = Index;
59 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000060 if (A) {
61 if (A->getOption().isUnsupported()) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +000062 Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName();
Daniel Dunbar53ec5522009-03-12 07:58:46 +000063 continue;
64 }
65
Daniel Dunbar06482622009-03-05 06:38:47 +000066 Args->append(A);
Daniel Dunbar53ec5522009-03-12 07:58:46 +000067 }
Daniel Dunbar06482622009-03-05 06:38:47 +000068
69 assert(Index > Prev && "Parser failed to consume argument.");
70 }
71
72 return Args;
73}
74
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000075Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarcb881672009-03-13 00:51:18 +000076 // FIXME: Handle environment options which effect driver behavior,
77 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
78 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
79
80 // FIXME: What are we going to do with -V and -b?
81
82 // FIXME: Handle CCC_ADD_ARGS.
83
Daniel Dunbar365c02f2009-03-10 20:52:46 +000084 // FIXME: This stuff needs to go into the Compilation, not the
85 // driver.
Daniel Dunbar53ec5522009-03-12 07:58:46 +000086 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar06482622009-03-05 06:38:47 +000087
Daniel Dunbar365c02f2009-03-10 20:52:46 +000088 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000089 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbar365c02f2009-03-10 20:52:46 +000090
91 // Read -ccc args.
92 //
93 // FIXME: We need to figure out where this behavior should
94 // live. Most of it should be outside in the client; the parts that
95 // aren't should have proper options, either by introducing new ones
96 // or by overloading gcc ones like -V or -b.
97 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
98 const char *Opt = *Start + 5;
99
100 if (!strcmp(Opt, "print-options")) {
101 CCCPrintOptions = true;
102 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000103 CCCPrintActions = true;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000104 } else if (!strcmp(Opt, "cxx")) {
105 CCCIsCXX = true;
106 } else if (!strcmp(Opt, "echo")) {
107 CCCEcho = true;
108
109 } else if (!strcmp(Opt, "no-clang")) {
110 CCCNoClang = true;
111 } else if (!strcmp(Opt, "no-clang-cxx")) {
112 CCCNoClangCXX = true;
113 } else if (!strcmp(Opt, "no-clang-cpp")) {
114 CCCNoClangCPP = true;
115 } else if (!strcmp(Opt, "clang-archs")) {
116 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
117 const char *Cur = *++Start;
118
119 for (;;) {
120 const char *Next = strchr(Cur, ',');
121
122 if (Next) {
123 CCCClangArchs.insert(std::string(Cur, Next));
124 Cur = Next + 1;
125 } else {
126 CCCClangArchs.insert(std::string(Cur));
127 break;
128 }
129 }
130
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000131 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000132 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000133 HostTriple = *++Start;
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000134
135 } else {
136 // FIXME: Error handling.
137 llvm::errs() << "invalid option: " << *Start << "\n";
138 exit(1);
139 }
140 }
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000141
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000142 ArgList *Args = ParseArgStrings(Start, End);
143
Daniel Dunbarcb881672009-03-13 00:51:18 +0000144 Host = Driver::GetHostInfo(HostTriple);
145 DefaultToolChain = Host->getToolChain(*Args);
146
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000147 // FIXME: This behavior shouldn't be here.
148 if (CCCPrintOptions) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000149 PrintOptions(*Args);
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000150 exit(0);
151 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000152
Daniel Dunbarcb881672009-03-13 00:51:18 +0000153 if (!HandleImmediateArgs(*Args))
154 return 0;
155
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000156 // Construct the list of abstract actions to perform for this
157 // compilation.
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000158 ActionList Actions;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000159 if (Host->useDriverDriver())
160 BuildUniversalActions(*Args, Actions);
161 else
162 BuildActions(*Args, Actions);
163
164 // FIXME: This behavior shouldn't be here.
165 if (CCCPrintActions) {
166 PrintActions(Actions);
167 exit(0);
168 }
169
Daniel Dunbar365c02f2009-03-10 20:52:46 +0000170 assert(0 && "FIXME: Implement");
171
172 return new Compilation();
173}
174
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000175void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar06482622009-03-05 06:38:47 +0000176 unsigned i = 0;
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000177 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar06482622009-03-05 06:38:47 +0000178 it != ie; ++it, ++i) {
179 Arg *A = *it;
180 llvm::errs() << "Option " << i << " - "
181 << "Name: \"" << A->getOption().getName() << "\", "
182 << "Values: {";
183 for (unsigned j = 0; j < A->getNumValues(); ++j) {
184 if (j)
185 llvm::errs() << ", ";
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000186 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar06482622009-03-05 06:38:47 +0000187 }
188 llvm::errs() << "}\n";
Daniel Dunbar06482622009-03-05 06:38:47 +0000189 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000190}
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000191
Daniel Dunbarcb881672009-03-13 00:51:18 +0000192void Driver::PrintVersion() const {
193 // FIXME: Get a reasonable version number.
194
195 // FIXME: The following handlers should use a callback mechanism, we
196 // don't know what the client would like to do.
197 llvm::outs() << "ccc version 1.0" << "\n";
198}
199
200bool Driver::HandleImmediateArgs(const ArgList &Args) {
201 // The order these options are handled in in gcc is all over the
202 // place, but we don't expect inconsistencies w.r.t. that to matter
203 // in practice.
204 if (Args.hasArg(options::OPT_v) ||
205 Args.hasArg(options::OPT__HASH_HASH_HASH)) {
206 PrintVersion();
207 SuppressMissingInputWarning = true;
208 }
209
210 // FIXME: The following handlers should use a callback mechanism, we
211 // don't know what the client would like to do.
212 if (Arg *A = Args.getLastArg(options::OPT_print_file_name_EQ)) {
213 llvm::outs() << GetFilePath(A->getValue(Args)).toString() << "\n";
214 return false;
215 }
216
217 if (Arg *A = Args.getLastArg(options::OPT_print_prog_name_EQ)) {
218 llvm::outs() << GetProgramPath(A->getValue(Args)).toString() << "\n";
219 return false;
220 }
221
Daniel Dunbar41393402009-03-13 01:01:44 +0000222 if (Args.hasArg(options::OPT_print_libgcc_file_name)) {
Daniel Dunbarcb881672009-03-13 00:51:18 +0000223 llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n";
224 return false;
225 }
226
227 return true;
228}
229
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000230void Driver::PrintActions(const ActionList &Actions) const {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000231 llvm::errs() << "FIXME: Print actions.";
232}
233
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000234void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbar2fe63e62009-03-12 18:40:18 +0000235 llvm::StringMap<Arg *> Archs;
236 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
237 it != ie; ++it) {
238 Arg *A = *it;
239
240 if (A->getOption().getId() == options::OPT_arch) {
241 // FIXME: We need to handle canonicalization of the specified
242 // arch?
243
244 Archs[A->getValue(Args)] = A;
245 }
246 }
247
248 // When there is no explicit arch for this platform, get one from
249 // the host so that -Xarch_ is handled correctly.
250 if (!Archs.size()) {
251 const char *Arch = Host->getArchName().c_str();
252 Archs[Arch] = Args.MakeSeparateArg(getOpts().getOption(options::OPT_arch),
253 Arch);
254 }
255
256 // FIXME: We killed off some others but these aren't yet detected in
257 // a functional manner. If we added information to jobs about which
258 // "auxiliary" files they wrote then we could detect the conflict
259 // these cause downstream.
260 if (Archs.size() > 1) {
261 // No recovery needed, the point of this is just to prevent
262 // overwriting the same files.
263 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
264 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
265 << A->getOption().getName();
266 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
267 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
268 << A->getOption().getName();
269 }
270
271 ActionList SingleActions;
272 BuildActions(Args, SingleActions);
273
274 // Add in arch binding and lipo (if necessary) for every top level
275 // action.
276 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
277 Action *Act = SingleActions[i];
278
279 // Make sure we can lipo this kind of output. If not (and it is an
280 // actual output) then we disallow, since we can't create an
281 // output file with the right name without overwriting it. We
282 // could remove this oddity by just changing the output names to
283 // include the arch, which would also fix
284 // -save-temps. Compatibility wins for now.
285
286 if (Archs.size() > 1 && types::canLipoType(Act->getType()))
287 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
288 << types::getTypeName(Act->getType());
289
290 ActionList Inputs;
291 for (llvm::StringMap<Arg*>::iterator it = Archs.begin(), ie = Archs.end();
292 it != ie; ++it)
293 Inputs.push_back(new BindArchAction(Act, it->second->getValue(Args)));
294
295 // Lipo if necessary, We do it this way because we need to set the
296 // arch flag so that -Xarch_ gets overwritten.
297 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
298 Actions.append(Inputs.begin(), Inputs.end());
299 else
300 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
301 }
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000302}
303
Daniel Dunbard65bddc2009-03-12 18:24:49 +0000304void Driver::BuildActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000305 types::ID InputType = types::TY_INVALID;
306 Arg *InputTypeArg = 0;
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000307
308 // Start by constructing the list of inputs and their types.
309
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000310 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
311 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
312 it != ie; ++it) {
313 Arg *A = *it;
314
315 if (isa<InputOption>(A->getOption())) {
316 const char *Value = A->getValue(Args);
317 types::ID Ty = types::TY_INVALID;
318
319 // Infer the input type if necessary.
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +0000320 if (InputType == types::TY_INVALID) {
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000321 // stdin must be handled specially.
322 if (memcmp(Value, "-", 2) == 0) {
323 // If running with -E, treat as a C input (this changes the
324 // builtin macros, for example). This may be overridden by
325 // -ObjC below.
326 //
327 // Otherwise emit an error but still use a valid type to
328 // avoid spurious errors (e.g., no inputs).
329 if (!Args.hasArg(options::OPT_E))
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000330 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000331 Ty = types::TY_C;
332 } else {
333 // Otherwise lookup by extension, and fallback to ObjectType
334 // if not found.
335 if (const char *Ext = strrchr(Value, '.'))
336 Ty = types::lookupTypeForExtension(Ext + 1);
337 if (Ty == types::TY_INVALID)
338 Ty = types::TY_Object;
339 }
340
341 // -ObjC and -ObjC++ override the default language, but only
342 // -for "source files". We just treat everything that isn't a
343 // -linker input as a source file.
344 //
345 // FIXME: Clean this up if we move the phase sequence into the
346 // type.
347 if (Ty != types::TY_Object) {
348 if (Args.hasArg(options::OPT_ObjC))
349 Ty = types::TY_ObjC;
350 else if (Args.hasArg(options::OPT_ObjCXX))
351 Ty = types::TY_ObjCXX;
352 }
353 } else {
354 assert(InputTypeArg && "InputType set w/o InputTypeArg");
355 InputTypeArg->claim();
356 Ty = InputType;
357 }
358
359 // Check that the file exists. It isn't clear this is worth
360 // doing, since the tool presumably does this anyway, and this
361 // just adds an extra stat to the equation, but this is gcc
362 // compatible.
363 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000364 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000365 else
366 Inputs.push_back(std::make_pair(Ty, A));
367
368 } else if (A->getOption().isLinkerInput()) {
369 // Just treat as object type, we could make a special type for
370 // this if necessary.
371 Inputs.push_back(std::make_pair(types::TY_Object, A));
372
373 } else if (A->getOption().getId() == options::OPT_x) {
374 InputTypeArg = A;
375 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
376
377 // Follow gcc behavior and treat as linker input for invalid -x
378 // options. Its not clear why we shouldn't just revert to
379 // unknown; but this isn't very important, we might as well be
380 // bug comatible.
381 if (!InputType) {
Daniel Dunbarb897f5d2009-03-12 09:13:48 +0000382 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000383 InputType = types::TY_Object;
384 }
385 }
386 }
387
Daniel Dunbar8b1604e2009-03-13 00:17:48 +0000388 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbaraf61c712009-03-12 23:55:14 +0000389 Diag(clang::diag::err_drv_no_input_files);
390 return;
391 }
392
393 // Determine which compilation mode we are in. We look for options
394 // which affect the phase, starting with the earliest phases, and
395 // record which option we used to determine the final phase.
396 Arg *FinalPhaseOpt = 0;
397 PhaseOrder FinalPhase;
398
399 // -{E,M,MM} only run the preprocessor.
400 if ((FinalPhaseOpt = Args.getLastArg(options::OPT_E)) ||
401 (FinalPhaseOpt = Args.getLastArg(options::OPT_M)) ||
402 (FinalPhaseOpt = Args.getLastArg(options::OPT_MM))) {
403 FinalPhase = PreprocessPhaseOrder;
404
405 // -{-analyze,fsyntax-only,S} only run up to the compiler.
406 } else if ((FinalPhaseOpt = Args.getLastArg(options::OPT__analyze)) ||
407 (FinalPhaseOpt = Args.getLastArg(options::OPT_fsyntax_only)) ||
408 (FinalPhaseOpt = Args.getLastArg(options::OPT_S))) {
409 FinalPhase = CompilePhaseOrder;
410
411 // -c only runs up to the assembler.
412 } else if ((FinalPhaseOpt = Args.getLastArg(options::OPT_c))) {
413 FinalPhase = AssemblePhaseOrder;
414
415 // Otherwise do everything.
416 } else
417 FinalPhase = PostAssemblePhaseOrder;
418
419 if (FinalPhaseOpt)
420 FinalPhaseOpt->claim();
421
422 // Reject -Z* at the top level, these options should never have been
423 // exposed by gcc.
424 if (Arg *A = Args.getLastArg(options::OPT_Z))
425 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
426
427 // FIXME: This is just debugging code.
Daniel Dunbar53ec5522009-03-12 07:58:46 +0000428 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
429 llvm::errs() << "input " << i << ": "
430 << Inputs[i].second->getValue(Args) << "\n";
431 }
432 exit(0);
433}
434
Daniel Dunbarcb881672009-03-13 00:51:18 +0000435llvm::sys::Path Driver::GetFilePath(const char *Name) const {
436 // FIXME: Implement.
437 return llvm::sys::Path(Name);
438}
439
440llvm::sys::Path Driver::GetProgramPath(const char *Name) const {
441 // FIXME: Implement.
442 return llvm::sys::Path(Name);
443}
444
Daniel Dunbardd98e2c2009-03-10 23:41:59 +0000445HostInfo *Driver::GetHostInfo(const char *Triple) {
446 // Dice into arch, platform, and OS. This matches
447 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
448 // and missing fields are left empty.
449 std::string Arch, Platform, OS;
450
451 if (const char *ArchEnd = strchr(Triple, '-')) {
452 Arch = std::string(Triple, ArchEnd);
453
454 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
455 Platform = std::string(ArchEnd+1, PlatformEnd);
456 OS = PlatformEnd+1;
457 } else
458 Platform = ArchEnd+1;
459 } else
460 Arch = Triple;
461
462 if (memcmp(&Platform[0], "darwin", 6) == 0)
463 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
464
465 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
466}