blob: c8750f7449bd57da002d21641b038f8df231cf32 [file] [log] [blame]
Daniel Dunbar63c4da92009-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 Dunbar63c4da92009-03-02 19:59:07 +000010#include "clang/Driver/Driver.h"
Daniel Dunbar63c4da92009-03-02 19:59:07 +000011
Daniel Dunbardb62cc32009-03-12 07:58:46 +000012#include "clang/Driver/Action.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000013#include "clang/Driver/Arg.h"
14#include "clang/Driver/ArgList.h"
15#include "clang/Driver/Compilation.h"
Daniel Dunbar93468492009-03-12 08:55:43 +000016#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbard25acaa2009-03-10 23:41:59 +000017#include "clang/Driver/HostInfo.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000018#include "clang/Driver/Option.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000019#include "clang/Driver/Options.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000020#include "clang/Driver/Types.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000021
Daniel Dunbarfba157b2009-03-12 18:40:18 +000022#include "llvm/ADT/StringMap.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000024#include "llvm/System/Path.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000025using namespace clang::driver;
26
Daniel Dunbard25acaa2009-03-10 23:41:59 +000027Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000028 const char *_DefaultHostTriple,
29 Diagnostic &_Diags)
30 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000031 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
32 Host(0),
Daniel Dunbarb282ced2009-03-10 20:52:46 +000033 CCCIsCXX(false), CCCEcho(false),
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +000034 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false),
35 SuppressMissingInputWarning(false)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000036{
Daniel Dunbar63c4da92009-03-02 19:59:07 +000037}
38
39Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000040 delete Opts;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000041}
42
Daniel Dunbar7dc2a042009-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) {
48 unsigned Prev = Index;
49 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000050 if (A) {
51 if (A->getOption().isUnsupported()) {
Daniel Dunbard724e332009-03-12 09:13:48 +000052 Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName();
Daniel Dunbardb62cc32009-03-12 07:58:46 +000053 continue;
54 }
55
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000056 Args->append(A);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000057 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000058
59 assert(Index > Prev && "Parser failed to consume argument.");
60 }
61
62 return Args;
63}
64
Daniel Dunbar63c4da92009-03-02 19:59:07 +000065Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarcc006892009-03-13 00:51:18 +000066 // FIXME: Handle environment options which effect driver behavior,
67 // somewhere (client?). GCC_EXEC_PREFIX, COMPILER_PATH,
68 // LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, QA_OVERRIDE_GCC3_OPTIONS.
69
70 // FIXME: What are we going to do with -V and -b?
71
72 // FIXME: Handle CCC_ADD_ARGS.
73
Daniel Dunbarb282ced2009-03-10 20:52:46 +000074 // FIXME: This stuff needs to go into the Compilation, not the
75 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +000076 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000077
Daniel Dunbarb282ced2009-03-10 20:52:46 +000078 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +000079 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +000080
81 // Read -ccc args.
82 //
83 // FIXME: We need to figure out where this behavior should
84 // live. Most of it should be outside in the client; the parts that
85 // aren't should have proper options, either by introducing new ones
86 // or by overloading gcc ones like -V or -b.
87 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
88 const char *Opt = *Start + 5;
89
90 if (!strcmp(Opt, "print-options")) {
91 CCCPrintOptions = true;
92 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +000093 CCCPrintActions = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +000094 } else if (!strcmp(Opt, "cxx")) {
95 CCCIsCXX = true;
96 } else if (!strcmp(Opt, "echo")) {
97 CCCEcho = true;
98
99 } else if (!strcmp(Opt, "no-clang")) {
100 CCCNoClang = true;
101 } else if (!strcmp(Opt, "no-clang-cxx")) {
102 CCCNoClangCXX = true;
103 } else if (!strcmp(Opt, "no-clang-cpp")) {
104 CCCNoClangCPP = true;
105 } else if (!strcmp(Opt, "clang-archs")) {
106 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
107 const char *Cur = *++Start;
108
109 for (;;) {
110 const char *Next = strchr(Cur, ',');
111
112 if (Next) {
113 CCCClangArchs.insert(std::string(Cur, Next));
114 Cur = Next + 1;
115 } else {
116 CCCClangArchs.insert(std::string(Cur));
117 break;
118 }
119 }
120
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000121 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000122 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000123 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000124
125 } else {
126 // FIXME: Error handling.
127 llvm::errs() << "invalid option: " << *Start << "\n";
128 exit(1);
129 }
130 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000131
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000132 ArgList *Args = ParseArgStrings(Start, End);
133
Daniel Dunbarcc006892009-03-13 00:51:18 +0000134 Host = Driver::GetHostInfo(HostTriple);
135 DefaultToolChain = Host->getToolChain(*Args);
136
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000137 // FIXME: This behavior shouldn't be here.
138 if (CCCPrintOptions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000139 PrintOptions(*Args);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000140 exit(0);
141 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000142
Daniel Dunbarcc006892009-03-13 00:51:18 +0000143 if (!HandleImmediateArgs(*Args))
144 return 0;
145
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000146 // Construct the list of abstract actions to perform for this
147 // compilation.
Daniel Dunbara790d372009-03-12 18:24:49 +0000148 ActionList Actions;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000149 if (Host->useDriverDriver())
150 BuildUniversalActions(*Args, Actions);
151 else
152 BuildActions(*Args, Actions);
153
154 // FIXME: This behavior shouldn't be here.
155 if (CCCPrintActions) {
156 PrintActions(Actions);
157 exit(0);
158 }
159
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000160 assert(0 && "FIXME: Implement");
161
162 return new Compilation();
163}
164
Daniel Dunbara790d372009-03-12 18:24:49 +0000165void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000166 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000167 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000168 it != ie; ++it, ++i) {
169 Arg *A = *it;
170 llvm::errs() << "Option " << i << " - "
171 << "Name: \"" << A->getOption().getName() << "\", "
172 << "Values: {";
173 for (unsigned j = 0; j < A->getNumValues(); ++j) {
174 if (j)
175 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000176 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000177 }
178 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000179 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000180}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000181
Daniel Dunbarcc006892009-03-13 00:51:18 +0000182void Driver::PrintVersion() const {
183 // FIXME: Get a reasonable version number.
184
185 // FIXME: The following handlers should use a callback mechanism, we
186 // don't know what the client would like to do.
187 llvm::outs() << "ccc version 1.0" << "\n";
188}
189
190bool Driver::HandleImmediateArgs(const ArgList &Args) {
191 // The order these options are handled in in gcc is all over the
192 // place, but we don't expect inconsistencies w.r.t. that to matter
193 // in practice.
194 if (Args.hasArg(options::OPT_v) ||
195 Args.hasArg(options::OPT__HASH_HASH_HASH)) {
196 PrintVersion();
197 SuppressMissingInputWarning = true;
198 }
199
200 // FIXME: The following handlers should use a callback mechanism, we
201 // don't know what the client would like to do.
202 if (Arg *A = Args.getLastArg(options::OPT_print_file_name_EQ)) {
203 llvm::outs() << GetFilePath(A->getValue(Args)).toString() << "\n";
204 return false;
205 }
206
207 if (Arg *A = Args.getLastArg(options::OPT_print_prog_name_EQ)) {
208 llvm::outs() << GetProgramPath(A->getValue(Args)).toString() << "\n";
209 return false;
210 }
211
212 if (Arg *A = Args.getLastArg(options::OPT_print_libgcc_file_name)) {
213 llvm::outs() << GetProgramPath("libgcc.a").toString() << "\n";
214 return false;
215 }
216
217 return true;
218}
219
Daniel Dunbara790d372009-03-12 18:24:49 +0000220void Driver::PrintActions(const ActionList &Actions) const {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000221 llvm::errs() << "FIXME: Print actions.";
222}
223
Daniel Dunbara790d372009-03-12 18:24:49 +0000224void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000225 llvm::StringMap<Arg *> Archs;
226 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
227 it != ie; ++it) {
228 Arg *A = *it;
229
230 if (A->getOption().getId() == options::OPT_arch) {
231 // FIXME: We need to handle canonicalization of the specified
232 // arch?
233
234 Archs[A->getValue(Args)] = A;
235 }
236 }
237
238 // When there is no explicit arch for this platform, get one from
239 // the host so that -Xarch_ is handled correctly.
240 if (!Archs.size()) {
241 const char *Arch = Host->getArchName().c_str();
242 Archs[Arch] = Args.MakeSeparateArg(getOpts().getOption(options::OPT_arch),
243 Arch);
244 }
245
246 // FIXME: We killed off some others but these aren't yet detected in
247 // a functional manner. If we added information to jobs about which
248 // "auxiliary" files they wrote then we could detect the conflict
249 // these cause downstream.
250 if (Archs.size() > 1) {
251 // No recovery needed, the point of this is just to prevent
252 // overwriting the same files.
253 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
254 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
255 << A->getOption().getName();
256 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
257 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
258 << A->getOption().getName();
259 }
260
261 ActionList SingleActions;
262 BuildActions(Args, SingleActions);
263
264 // Add in arch binding and lipo (if necessary) for every top level
265 // action.
266 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
267 Action *Act = SingleActions[i];
268
269 // Make sure we can lipo this kind of output. If not (and it is an
270 // actual output) then we disallow, since we can't create an
271 // output file with the right name without overwriting it. We
272 // could remove this oddity by just changing the output names to
273 // include the arch, which would also fix
274 // -save-temps. Compatibility wins for now.
275
276 if (Archs.size() > 1 && types::canLipoType(Act->getType()))
277 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
278 << types::getTypeName(Act->getType());
279
280 ActionList Inputs;
281 for (llvm::StringMap<Arg*>::iterator it = Archs.begin(), ie = Archs.end();
282 it != ie; ++it)
283 Inputs.push_back(new BindArchAction(Act, it->second->getValue(Args)));
284
285 // Lipo if necessary, We do it this way because we need to set the
286 // arch flag so that -Xarch_ gets overwritten.
287 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
288 Actions.append(Inputs.begin(), Inputs.end());
289 else
290 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
291 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000292}
293
Daniel Dunbara790d372009-03-12 18:24:49 +0000294void Driver::BuildActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000295 types::ID InputType = types::TY_INVALID;
296 Arg *InputTypeArg = 0;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000297
298 // Start by constructing the list of inputs and their types.
299
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000300 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
301 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
302 it != ie; ++it) {
303 Arg *A = *it;
304
305 if (isa<InputOption>(A->getOption())) {
306 const char *Value = A->getValue(Args);
307 types::ID Ty = types::TY_INVALID;
308
309 // Infer the input type if necessary.
Daniel Dunbar93468492009-03-12 08:55:43 +0000310 if (InputType == types::TY_INVALID) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000311 // stdin must be handled specially.
312 if (memcmp(Value, "-", 2) == 0) {
313 // If running with -E, treat as a C input (this changes the
314 // builtin macros, for example). This may be overridden by
315 // -ObjC below.
316 //
317 // Otherwise emit an error but still use a valid type to
318 // avoid spurious errors (e.g., no inputs).
319 if (!Args.hasArg(options::OPT_E))
Daniel Dunbard724e332009-03-12 09:13:48 +0000320 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000321 Ty = types::TY_C;
322 } else {
323 // Otherwise lookup by extension, and fallback to ObjectType
324 // if not found.
325 if (const char *Ext = strrchr(Value, '.'))
326 Ty = types::lookupTypeForExtension(Ext + 1);
327 if (Ty == types::TY_INVALID)
328 Ty = types::TY_Object;
329 }
330
331 // -ObjC and -ObjC++ override the default language, but only
332 // -for "source files". We just treat everything that isn't a
333 // -linker input as a source file.
334 //
335 // FIXME: Clean this up if we move the phase sequence into the
336 // type.
337 if (Ty != types::TY_Object) {
338 if (Args.hasArg(options::OPT_ObjC))
339 Ty = types::TY_ObjC;
340 else if (Args.hasArg(options::OPT_ObjCXX))
341 Ty = types::TY_ObjCXX;
342 }
343 } else {
344 assert(InputTypeArg && "InputType set w/o InputTypeArg");
345 InputTypeArg->claim();
346 Ty = InputType;
347 }
348
349 // Check that the file exists. It isn't clear this is worth
350 // doing, since the tool presumably does this anyway, and this
351 // just adds an extra stat to the equation, but this is gcc
352 // compatible.
353 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000354 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000355 else
356 Inputs.push_back(std::make_pair(Ty, A));
357
358 } else if (A->getOption().isLinkerInput()) {
359 // Just treat as object type, we could make a special type for
360 // this if necessary.
361 Inputs.push_back(std::make_pair(types::TY_Object, A));
362
363 } else if (A->getOption().getId() == options::OPT_x) {
364 InputTypeArg = A;
365 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
366
367 // Follow gcc behavior and treat as linker input for invalid -x
368 // options. Its not clear why we shouldn't just revert to
369 // unknown; but this isn't very important, we might as well be
370 // bug comatible.
371 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000372 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000373 InputType = types::TY_Object;
374 }
375 }
376 }
377
Daniel Dunbar5a5ec5c2009-03-13 00:17:48 +0000378 if (!SuppressMissingInputWarning && Inputs.empty()) {
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000379 Diag(clang::diag::err_drv_no_input_files);
380 return;
381 }
382
383 // Determine which compilation mode we are in. We look for options
384 // which affect the phase, starting with the earliest phases, and
385 // record which option we used to determine the final phase.
386 Arg *FinalPhaseOpt = 0;
387 PhaseOrder FinalPhase;
388
389 // -{E,M,MM} only run the preprocessor.
390 if ((FinalPhaseOpt = Args.getLastArg(options::OPT_E)) ||
391 (FinalPhaseOpt = Args.getLastArg(options::OPT_M)) ||
392 (FinalPhaseOpt = Args.getLastArg(options::OPT_MM))) {
393 FinalPhase = PreprocessPhaseOrder;
394
395 // -{-analyze,fsyntax-only,S} only run up to the compiler.
396 } else if ((FinalPhaseOpt = Args.getLastArg(options::OPT__analyze)) ||
397 (FinalPhaseOpt = Args.getLastArg(options::OPT_fsyntax_only)) ||
398 (FinalPhaseOpt = Args.getLastArg(options::OPT_S))) {
399 FinalPhase = CompilePhaseOrder;
400
401 // -c only runs up to the assembler.
402 } else if ((FinalPhaseOpt = Args.getLastArg(options::OPT_c))) {
403 FinalPhase = AssemblePhaseOrder;
404
405 // Otherwise do everything.
406 } else
407 FinalPhase = PostAssemblePhaseOrder;
408
409 if (FinalPhaseOpt)
410 FinalPhaseOpt->claim();
411
412 // Reject -Z* at the top level, these options should never have been
413 // exposed by gcc.
414 if (Arg *A = Args.getLastArg(options::OPT_Z))
415 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
416
417 // FIXME: This is just debugging code.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000418 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
419 llvm::errs() << "input " << i << ": "
420 << Inputs[i].second->getValue(Args) << "\n";
421 }
422 exit(0);
423}
424
Daniel Dunbarcc006892009-03-13 00:51:18 +0000425llvm::sys::Path Driver::GetFilePath(const char *Name) const {
426 // FIXME: Implement.
427 return llvm::sys::Path(Name);
428}
429
430llvm::sys::Path Driver::GetProgramPath(const char *Name) const {
431 // FIXME: Implement.
432 return llvm::sys::Path(Name);
433}
434
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000435HostInfo *Driver::GetHostInfo(const char *Triple) {
436 // Dice into arch, platform, and OS. This matches
437 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
438 // and missing fields are left empty.
439 std::string Arch, Platform, OS;
440
441 if (const char *ArchEnd = strchr(Triple, '-')) {
442 Arch = std::string(Triple, ArchEnd);
443
444 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
445 Platform = std::string(ArchEnd+1, PlatformEnd);
446 OS = PlatformEnd+1;
447 } else
448 Platform = ArchEnd+1;
449 } else
450 Arch = Triple;
451
452 if (memcmp(&Platform[0], "darwin", 6) == 0)
453 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
454
455 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
456}