blob: 204436abba9aab26fe1e04f93b6ca9dad7c032c4 [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),
34 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false)
35{
Daniel Dunbard6f0e372009-03-04 20:49:20 +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 Dunbarb282ced2009-03-10 20:52:46 +000066 // FIXME: This stuff needs to go into the Compilation, not the
67 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +000068 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000069
Daniel Dunbarb282ced2009-03-10 20:52:46 +000070 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +000071 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +000072
73 // Read -ccc args.
74 //
75 // FIXME: We need to figure out where this behavior should
76 // live. Most of it should be outside in the client; the parts that
77 // aren't should have proper options, either by introducing new ones
78 // or by overloading gcc ones like -V or -b.
79 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
80 const char *Opt = *Start + 5;
81
82 if (!strcmp(Opt, "print-options")) {
83 CCCPrintOptions = true;
84 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +000085 CCCPrintActions = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +000086 } else if (!strcmp(Opt, "cxx")) {
87 CCCIsCXX = true;
88 } else if (!strcmp(Opt, "echo")) {
89 CCCEcho = true;
90
91 } else if (!strcmp(Opt, "no-clang")) {
92 CCCNoClang = true;
93 } else if (!strcmp(Opt, "no-clang-cxx")) {
94 CCCNoClangCXX = true;
95 } else if (!strcmp(Opt, "no-clang-cpp")) {
96 CCCNoClangCPP = true;
97 } else if (!strcmp(Opt, "clang-archs")) {
98 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
99 const char *Cur = *++Start;
100
101 for (;;) {
102 const char *Next = strchr(Cur, ',');
103
104 if (Next) {
105 CCCClangArchs.insert(std::string(Cur, Next));
106 Cur = Next + 1;
107 } else {
108 CCCClangArchs.insert(std::string(Cur));
109 break;
110 }
111 }
112
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000113 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000114 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000115 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000116
117 } else {
118 // FIXME: Error handling.
119 llvm::errs() << "invalid option: " << *Start << "\n";
120 exit(1);
121 }
122 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000123
124 Host = Driver::GetHostInfo(HostTriple);
125
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000126 ArgList *Args = ParseArgStrings(Start, End);
127
128 // FIXME: This behavior shouldn't be here.
129 if (CCCPrintOptions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000130 PrintOptions(*Args);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000131 exit(0);
132 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000133
134 // Construct the list of abstract actions to perform for this
135 // compilation.
Daniel Dunbara790d372009-03-12 18:24:49 +0000136 ActionList Actions;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000137 if (Host->useDriverDriver())
138 BuildUniversalActions(*Args, Actions);
139 else
140 BuildActions(*Args, Actions);
141
142 // FIXME: This behavior shouldn't be here.
143 if (CCCPrintActions) {
144 PrintActions(Actions);
145 exit(0);
146 }
147
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000148 assert(0 && "FIXME: Implement");
149
150 return new Compilation();
151}
152
Daniel Dunbara790d372009-03-12 18:24:49 +0000153void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000154 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000155 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000156 it != ie; ++it, ++i) {
157 Arg *A = *it;
158 llvm::errs() << "Option " << i << " - "
159 << "Name: \"" << A->getOption().getName() << "\", "
160 << "Values: {";
161 for (unsigned j = 0; j < A->getNumValues(); ++j) {
162 if (j)
163 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000164 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000165 }
166 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000167 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000168}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000169
Daniel Dunbara790d372009-03-12 18:24:49 +0000170void Driver::PrintActions(const ActionList &Actions) const {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000171 llvm::errs() << "FIXME: Print actions.";
172}
173
Daniel Dunbara790d372009-03-12 18:24:49 +0000174void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbarfba157b2009-03-12 18:40:18 +0000175 llvm::StringMap<Arg *> Archs;
176 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
177 it != ie; ++it) {
178 Arg *A = *it;
179
180 if (A->getOption().getId() == options::OPT_arch) {
181 // FIXME: We need to handle canonicalization of the specified
182 // arch?
183
184 Archs[A->getValue(Args)] = A;
185 }
186 }
187
188 // When there is no explicit arch for this platform, get one from
189 // the host so that -Xarch_ is handled correctly.
190 if (!Archs.size()) {
191 const char *Arch = Host->getArchName().c_str();
192 Archs[Arch] = Args.MakeSeparateArg(getOpts().getOption(options::OPT_arch),
193 Arch);
194 }
195
196 // FIXME: We killed off some others but these aren't yet detected in
197 // a functional manner. If we added information to jobs about which
198 // "auxiliary" files they wrote then we could detect the conflict
199 // these cause downstream.
200 if (Archs.size() > 1) {
201 // No recovery needed, the point of this is just to prevent
202 // overwriting the same files.
203 if (const Arg *A = Args.getLastArg(options::OPT_M_Group))
204 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
205 << A->getOption().getName();
206 if (const Arg *A = Args.getLastArg(options::OPT_save_temps))
207 Diag(clang::diag::err_drv_invalid_opt_with_multiple_archs)
208 << A->getOption().getName();
209 }
210
211 ActionList SingleActions;
212 BuildActions(Args, SingleActions);
213
214 // Add in arch binding and lipo (if necessary) for every top level
215 // action.
216 for (unsigned i = 0, e = SingleActions.size(); i != e; ++i) {
217 Action *Act = SingleActions[i];
218
219 // Make sure we can lipo this kind of output. If not (and it is an
220 // actual output) then we disallow, since we can't create an
221 // output file with the right name without overwriting it. We
222 // could remove this oddity by just changing the output names to
223 // include the arch, which would also fix
224 // -save-temps. Compatibility wins for now.
225
226 if (Archs.size() > 1 && types::canLipoType(Act->getType()))
227 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
228 << types::getTypeName(Act->getType());
229
230 ActionList Inputs;
231 for (llvm::StringMap<Arg*>::iterator it = Archs.begin(), ie = Archs.end();
232 it != ie; ++it)
233 Inputs.push_back(new BindArchAction(Act, it->second->getValue(Args)));
234
235 // Lipo if necessary, We do it this way because we need to set the
236 // arch flag so that -Xarch_ gets overwritten.
237 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
238 Actions.append(Inputs.begin(), Inputs.end());
239 else
240 Actions.push_back(new LipoJobAction(Inputs, Act->getType()));
241 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000242}
243
Daniel Dunbara790d372009-03-12 18:24:49 +0000244void Driver::BuildActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000245 types::ID InputType = types::TY_INVALID;
246 Arg *InputTypeArg = 0;
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000247
248 // Start by constructing the list of inputs and their types.
249
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000250 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
251 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
252 it != ie; ++it) {
253 Arg *A = *it;
254
255 if (isa<InputOption>(A->getOption())) {
256 const char *Value = A->getValue(Args);
257 types::ID Ty = types::TY_INVALID;
258
259 // Infer the input type if necessary.
Daniel Dunbar93468492009-03-12 08:55:43 +0000260 if (InputType == types::TY_INVALID) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000261 // stdin must be handled specially.
262 if (memcmp(Value, "-", 2) == 0) {
263 // If running with -E, treat as a C input (this changes the
264 // builtin macros, for example). This may be overridden by
265 // -ObjC below.
266 //
267 // Otherwise emit an error but still use a valid type to
268 // avoid spurious errors (e.g., no inputs).
269 if (!Args.hasArg(options::OPT_E))
Daniel Dunbard724e332009-03-12 09:13:48 +0000270 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000271 Ty = types::TY_C;
272 } else {
273 // Otherwise lookup by extension, and fallback to ObjectType
274 // if not found.
275 if (const char *Ext = strrchr(Value, '.'))
276 Ty = types::lookupTypeForExtension(Ext + 1);
277 if (Ty == types::TY_INVALID)
278 Ty = types::TY_Object;
279 }
280
281 // -ObjC and -ObjC++ override the default language, but only
282 // -for "source files". We just treat everything that isn't a
283 // -linker input as a source file.
284 //
285 // FIXME: Clean this up if we move the phase sequence into the
286 // type.
287 if (Ty != types::TY_Object) {
288 if (Args.hasArg(options::OPT_ObjC))
289 Ty = types::TY_ObjC;
290 else if (Args.hasArg(options::OPT_ObjCXX))
291 Ty = types::TY_ObjCXX;
292 }
293 } else {
294 assert(InputTypeArg && "InputType set w/o InputTypeArg");
295 InputTypeArg->claim();
296 Ty = InputType;
297 }
298
299 // Check that the file exists. It isn't clear this is worth
300 // doing, since the tool presumably does this anyway, and this
301 // just adds an extra stat to the equation, but this is gcc
302 // compatible.
303 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000304 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000305 else
306 Inputs.push_back(std::make_pair(Ty, A));
307
308 } else if (A->getOption().isLinkerInput()) {
309 // Just treat as object type, we could make a special type for
310 // this if necessary.
311 Inputs.push_back(std::make_pair(types::TY_Object, A));
312
313 } else if (A->getOption().getId() == options::OPT_x) {
314 InputTypeArg = A;
315 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
316
317 // Follow gcc behavior and treat as linker input for invalid -x
318 // options. Its not clear why we shouldn't just revert to
319 // unknown; but this isn't very important, we might as well be
320 // bug comatible.
321 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000322 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000323 InputType = types::TY_Object;
324 }
325 }
326 }
327
Daniel Dunbar207a56d2009-03-12 23:55:14 +0000328 if (Inputs.empty()) {
329 Diag(clang::diag::err_drv_no_input_files);
330 return;
331 }
332
333 // Determine which compilation mode we are in. We look for options
334 // which affect the phase, starting with the earliest phases, and
335 // record which option we used to determine the final phase.
336 Arg *FinalPhaseOpt = 0;
337 PhaseOrder FinalPhase;
338
339 // -{E,M,MM} only run the preprocessor.
340 if ((FinalPhaseOpt = Args.getLastArg(options::OPT_E)) ||
341 (FinalPhaseOpt = Args.getLastArg(options::OPT_M)) ||
342 (FinalPhaseOpt = Args.getLastArg(options::OPT_MM))) {
343 FinalPhase = PreprocessPhaseOrder;
344
345 // -{-analyze,fsyntax-only,S} only run up to the compiler.
346 } else if ((FinalPhaseOpt = Args.getLastArg(options::OPT__analyze)) ||
347 (FinalPhaseOpt = Args.getLastArg(options::OPT_fsyntax_only)) ||
348 (FinalPhaseOpt = Args.getLastArg(options::OPT_S))) {
349 FinalPhase = CompilePhaseOrder;
350
351 // -c only runs up to the assembler.
352 } else if ((FinalPhaseOpt = Args.getLastArg(options::OPT_c))) {
353 FinalPhase = AssemblePhaseOrder;
354
355 // Otherwise do everything.
356 } else
357 FinalPhase = PostAssemblePhaseOrder;
358
359 if (FinalPhaseOpt)
360 FinalPhaseOpt->claim();
361
362 // Reject -Z* at the top level, these options should never have been
363 // exposed by gcc.
364 if (Arg *A = Args.getLastArg(options::OPT_Z))
365 Diag(clang::diag::err_drv_use_of_Z_option) << A->getValue(Args);
366
367 // FIXME: This is just debugging code.
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000368 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
369 llvm::errs() << "input " << i << ": "
370 << Inputs[i].second->getValue(Args) << "\n";
371 }
372 exit(0);
373}
374
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000375HostInfo *Driver::GetHostInfo(const char *Triple) {
376 // Dice into arch, platform, and OS. This matches
377 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
378 // and missing fields are left empty.
379 std::string Arch, Platform, OS;
380
381 if (const char *ArchEnd = strchr(Triple, '-')) {
382 Arch = std::string(Triple, ArchEnd);
383
384 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
385 Platform = std::string(ArchEnd+1, PlatformEnd);
386 OS = PlatformEnd+1;
387 } else
388 Platform = ArchEnd+1;
389 } else
390 Arch = Triple;
391
392 if (memcmp(&Platform[0], "darwin", 6) == 0)
393 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
394
395 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
396}