blob: 2b763c6ae57274b2bc602f648cfff1733b6fae17 [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
22#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000023#include "llvm/System/Path.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000024using namespace clang::driver;
25
Daniel Dunbard25acaa2009-03-10 23:41:59 +000026Driver::Driver(const char *_Name, const char *_Dir,
Daniel Dunbar93468492009-03-12 08:55:43 +000027 const char *_DefaultHostTriple,
28 Diagnostic &_Diags)
29 : Opts(new OptTable()), Diags(_Diags),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000030 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
31 Host(0),
Daniel Dunbarb282ced2009-03-10 20:52:46 +000032 CCCIsCXX(false), CCCEcho(false),
33 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false)
34{
Daniel Dunbard6f0e372009-03-04 20:49:20 +000035
Daniel Dunbar63c4da92009-03-02 19:59:07 +000036}
37
38Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000039 delete Opts;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000040}
41
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000042ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
43 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
44
45 unsigned Index = 0, End = ArgEnd - ArgBegin;
46 while (Index < End) {
47 unsigned Prev = Index;
48 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000049 if (A) {
50 if (A->getOption().isUnsupported()) {
Daniel Dunbard724e332009-03-12 09:13:48 +000051 Diag(clang::diag::err_drv_unsupported_opt) << A->getOption().getName();
Daniel Dunbardb62cc32009-03-12 07:58:46 +000052 continue;
53 }
54
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000055 Args->append(A);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000056 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000057
58 assert(Index > Prev && "Parser failed to consume argument.");
59 }
60
61 return Args;
62}
63
Daniel Dunbar63c4da92009-03-02 19:59:07 +000064Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +000065 // FIXME: This stuff needs to go into the Compilation, not the
66 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +000067 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000068
Daniel Dunbarb282ced2009-03-10 20:52:46 +000069 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +000070 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +000071
72 // Read -ccc args.
73 //
74 // FIXME: We need to figure out where this behavior should
75 // live. Most of it should be outside in the client; the parts that
76 // aren't should have proper options, either by introducing new ones
77 // or by overloading gcc ones like -V or -b.
78 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
79 const char *Opt = *Start + 5;
80
81 if (!strcmp(Opt, "print-options")) {
82 CCCPrintOptions = true;
83 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +000084 CCCPrintActions = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +000085 } else if (!strcmp(Opt, "cxx")) {
86 CCCIsCXX = true;
87 } else if (!strcmp(Opt, "echo")) {
88 CCCEcho = true;
89
90 } else if (!strcmp(Opt, "no-clang")) {
91 CCCNoClang = true;
92 } else if (!strcmp(Opt, "no-clang-cxx")) {
93 CCCNoClangCXX = true;
94 } else if (!strcmp(Opt, "no-clang-cpp")) {
95 CCCNoClangCPP = true;
96 } else if (!strcmp(Opt, "clang-archs")) {
97 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
98 const char *Cur = *++Start;
99
100 for (;;) {
101 const char *Next = strchr(Cur, ',');
102
103 if (Next) {
104 CCCClangArchs.insert(std::string(Cur, Next));
105 Cur = Next + 1;
106 } else {
107 CCCClangArchs.insert(std::string(Cur));
108 break;
109 }
110 }
111
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000112 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000113 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000114 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000115
116 } else {
117 // FIXME: Error handling.
118 llvm::errs() << "invalid option: " << *Start << "\n";
119 exit(1);
120 }
121 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000122
123 Host = Driver::GetHostInfo(HostTriple);
124
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000125 ArgList *Args = ParseArgStrings(Start, End);
126
127 // FIXME: This behavior shouldn't be here.
128 if (CCCPrintOptions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000129 PrintOptions(*Args);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000130 exit(0);
131 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000132
133 // Construct the list of abstract actions to perform for this
134 // compilation.
Daniel Dunbara790d372009-03-12 18:24:49 +0000135 ActionList Actions;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000136 if (Host->useDriverDriver())
137 BuildUniversalActions(*Args, Actions);
138 else
139 BuildActions(*Args, Actions);
140
141 // FIXME: This behavior shouldn't be here.
142 if (CCCPrintActions) {
143 PrintActions(Actions);
144 exit(0);
145 }
146
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000147 assert(0 && "FIXME: Implement");
148
149 return new Compilation();
150}
151
Daniel Dunbara790d372009-03-12 18:24:49 +0000152void Driver::PrintOptions(const ArgList &Args) const {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000153 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000154 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000155 it != ie; ++it, ++i) {
156 Arg *A = *it;
157 llvm::errs() << "Option " << i << " - "
158 << "Name: \"" << A->getOption().getName() << "\", "
159 << "Values: {";
160 for (unsigned j = 0; j < A->getNumValues(); ++j) {
161 if (j)
162 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000163 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000164 }
165 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000166 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000167}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000168
Daniel Dunbara790d372009-03-12 18:24:49 +0000169void Driver::PrintActions(const ActionList &Actions) const {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000170 llvm::errs() << "FIXME: Print actions.";
171}
172
Daniel Dunbara790d372009-03-12 18:24:49 +0000173void Driver::BuildUniversalActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000174 // FIXME: Implement
175 BuildActions(Args, Actions);
176}
177
Daniel Dunbara790d372009-03-12 18:24:49 +0000178void Driver::BuildActions(ArgList &Args, ActionList &Actions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000179 types::ID InputType = types::TY_INVALID;
180 Arg *InputTypeArg = 0;
181
182 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
183 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
184 it != ie; ++it) {
185 Arg *A = *it;
186
187 if (isa<InputOption>(A->getOption())) {
188 const char *Value = A->getValue(Args);
189 types::ID Ty = types::TY_INVALID;
190
191 // Infer the input type if necessary.
Daniel Dunbar93468492009-03-12 08:55:43 +0000192 if (InputType == types::TY_INVALID) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000193 // stdin must be handled specially.
194 if (memcmp(Value, "-", 2) == 0) {
195 // If running with -E, treat as a C input (this changes the
196 // builtin macros, for example). This may be overridden by
197 // -ObjC below.
198 //
199 // Otherwise emit an error but still use a valid type to
200 // avoid spurious errors (e.g., no inputs).
201 if (!Args.hasArg(options::OPT_E))
Daniel Dunbard724e332009-03-12 09:13:48 +0000202 Diag(clang::diag::err_drv_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000203 Ty = types::TY_C;
204 } else {
205 // Otherwise lookup by extension, and fallback to ObjectType
206 // if not found.
207 if (const char *Ext = strrchr(Value, '.'))
208 Ty = types::lookupTypeForExtension(Ext + 1);
209 if (Ty == types::TY_INVALID)
210 Ty = types::TY_Object;
211 }
212
213 // -ObjC and -ObjC++ override the default language, but only
214 // -for "source files". We just treat everything that isn't a
215 // -linker input as a source file.
216 //
217 // FIXME: Clean this up if we move the phase sequence into the
218 // type.
219 if (Ty != types::TY_Object) {
220 if (Args.hasArg(options::OPT_ObjC))
221 Ty = types::TY_ObjC;
222 else if (Args.hasArg(options::OPT_ObjCXX))
223 Ty = types::TY_ObjCXX;
224 }
225 } else {
226 assert(InputTypeArg && "InputType set w/o InputTypeArg");
227 InputTypeArg->claim();
228 Ty = InputType;
229 }
230
231 // Check that the file exists. It isn't clear this is worth
232 // doing, since the tool presumably does this anyway, and this
233 // just adds an extra stat to the equation, but this is gcc
234 // compatible.
235 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbard724e332009-03-12 09:13:48 +0000236 Diag(clang::diag::err_drv_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000237 else
238 Inputs.push_back(std::make_pair(Ty, A));
239
240 } else if (A->getOption().isLinkerInput()) {
241 // Just treat as object type, we could make a special type for
242 // this if necessary.
243 Inputs.push_back(std::make_pair(types::TY_Object, A));
244
245 } else if (A->getOption().getId() == options::OPT_x) {
246 InputTypeArg = A;
247 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
248
249 // Follow gcc behavior and treat as linker input for invalid -x
250 // options. Its not clear why we shouldn't just revert to
251 // unknown; but this isn't very important, we might as well be
252 // bug comatible.
253 if (!InputType) {
Daniel Dunbard724e332009-03-12 09:13:48 +0000254 Diag(clang::diag::err_drv_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000255 InputType = types::TY_Object;
256 }
257 }
258 }
259
260 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
261 llvm::errs() << "input " << i << ": "
262 << Inputs[i].second->getValue(Args) << "\n";
263 }
264 exit(0);
265}
266
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000267HostInfo *Driver::GetHostInfo(const char *Triple) {
268 // Dice into arch, platform, and OS. This matches
269 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
270 // and missing fields are left empty.
271 std::string Arch, Platform, OS;
272
273 if (const char *ArchEnd = strchr(Triple, '-')) {
274 Arch = std::string(Triple, ArchEnd);
275
276 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
277 Platform = std::string(ArchEnd+1, PlatformEnd);
278 OS = PlatformEnd+1;
279 } else
280 Platform = ArchEnd+1;
281 } else
282 Arch = Triple;
283
284 if (memcmp(&Platform[0], "darwin", 6) == 0)
285 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
286
287 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
288}