blob: d9ed37b7de489cf126a1290459f7c4bb53fb8192 [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 Dunbard25acaa2009-03-10 23:41:59 +000016#include "clang/Driver/HostInfo.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000017#include "clang/Driver/Option.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000018#include "clang/Driver/Options.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000019#include "clang/Driver/Types.h"
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000020
21#include "llvm/Support/raw_ostream.h"
Daniel Dunbardb62cc32009-03-12 07:58:46 +000022#include "llvm/System/Path.h"
Daniel Dunbard6f0e372009-03-04 20:49:20 +000023using namespace clang::driver;
24
Daniel Dunbard25acaa2009-03-10 23:41:59 +000025Driver::Driver(const char *_Name, const char *_Dir,
26 const char *_DefaultHostTriple)
Daniel Dunbarb282ced2009-03-10 20:52:46 +000027 : Opts(new OptTable()),
Daniel Dunbard25acaa2009-03-10 23:41:59 +000028 Name(_Name), Dir(_Dir), DefaultHostTriple(_DefaultHostTriple),
29 Host(0),
Daniel Dunbarb282ced2009-03-10 20:52:46 +000030 CCCIsCXX(false), CCCEcho(false),
31 CCCNoClang(false), CCCNoClangCXX(false), CCCNoClangCPP(false)
32{
Daniel Dunbard6f0e372009-03-04 20:49:20 +000033
Daniel Dunbar63c4da92009-03-02 19:59:07 +000034}
35
36Driver::~Driver() {
Daniel Dunbard6f0e372009-03-04 20:49:20 +000037 delete Opts;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000038}
39
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000040ArgList *Driver::ParseArgStrings(const char **ArgBegin, const char **ArgEnd) {
41 ArgList *Args = new ArgList(ArgBegin, ArgEnd);
42
43 unsigned Index = 0, End = ArgEnd - ArgBegin;
44 while (Index < End) {
45 unsigned Prev = Index;
46 Arg *A = getOpts().ParseOneArg(*Args, Index, End);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000047 if (A) {
48 if (A->getOption().isUnsupported()) {
49 Diag("unsupported option: ") << A->getOption().getName() << "\n";
50 continue;
51 }
52
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000053 Args->append(A);
Daniel Dunbardb62cc32009-03-12 07:58:46 +000054 }
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000055
56 assert(Index > Prev && "Parser failed to consume argument.");
57 }
58
59 return Args;
60}
61
Daniel Dunbar63c4da92009-03-02 19:59:07 +000062Compilation *Driver::BuildCompilation(int argc, const char **argv) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +000063 // FIXME: This stuff needs to go into the Compilation, not the
64 // driver.
Daniel Dunbardb62cc32009-03-12 07:58:46 +000065 bool CCCPrintOptions = false, CCCPrintActions = false;
Daniel Dunbar7dc2a042009-03-05 06:38:47 +000066
Daniel Dunbarb282ced2009-03-10 20:52:46 +000067 const char **Start = argv + 1, **End = argv + argc;
Daniel Dunbard25acaa2009-03-10 23:41:59 +000068 const char *HostTriple = DefaultHostTriple.c_str();
Daniel Dunbarb282ced2009-03-10 20:52:46 +000069
70 // Read -ccc args.
71 //
72 // FIXME: We need to figure out where this behavior should
73 // live. Most of it should be outside in the client; the parts that
74 // aren't should have proper options, either by introducing new ones
75 // or by overloading gcc ones like -V or -b.
76 for (; Start != End && memcmp(*Start, "-ccc-", 5) == 0; ++Start) {
77 const char *Opt = *Start + 5;
78
79 if (!strcmp(Opt, "print-options")) {
80 CCCPrintOptions = true;
81 } else if (!strcmp(Opt, "print-phases")) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +000082 CCCPrintActions = true;
Daniel Dunbarb282ced2009-03-10 20:52:46 +000083 } else if (!strcmp(Opt, "cxx")) {
84 CCCIsCXX = true;
85 } else if (!strcmp(Opt, "echo")) {
86 CCCEcho = true;
87
88 } else if (!strcmp(Opt, "no-clang")) {
89 CCCNoClang = true;
90 } else if (!strcmp(Opt, "no-clang-cxx")) {
91 CCCNoClangCXX = true;
92 } else if (!strcmp(Opt, "no-clang-cpp")) {
93 CCCNoClangCPP = true;
94 } else if (!strcmp(Opt, "clang-archs")) {
95 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
96 const char *Cur = *++Start;
97
98 for (;;) {
99 const char *Next = strchr(Cur, ',');
100
101 if (Next) {
102 CCCClangArchs.insert(std::string(Cur, Next));
103 Cur = Next + 1;
104 } else {
105 CCCClangArchs.insert(std::string(Cur));
106 break;
107 }
108 }
109
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000110 } else if (!strcmp(Opt, "host-triple")) {
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000111 assert(Start+1 < End && "FIXME: -ccc- argument handling.");
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000112 HostTriple = *++Start;
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000113
114 } else {
115 // FIXME: Error handling.
116 llvm::errs() << "invalid option: " << *Start << "\n";
117 exit(1);
118 }
119 }
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000120
121 Host = Driver::GetHostInfo(HostTriple);
122
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000123 ArgList *Args = ParseArgStrings(Start, End);
124
125 // FIXME: This behavior shouldn't be here.
126 if (CCCPrintOptions) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000127 PrintOptions(*Args);
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000128 exit(0);
129 }
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000130
131 // Construct the list of abstract actions to perform for this
132 // compilation.
133 llvm::SmallVector<Action*, 2> Actions;
134 if (Host->useDriverDriver())
135 BuildUniversalActions(*Args, Actions);
136 else
137 BuildActions(*Args, Actions);
138
139 // FIXME: This behavior shouldn't be here.
140 if (CCCPrintActions) {
141 PrintActions(Actions);
142 exit(0);
143 }
144
Daniel Dunbarb282ced2009-03-10 20:52:46 +0000145 assert(0 && "FIXME: Implement");
146
147 return new Compilation();
148}
149
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000150void Driver::PrintOptions(const ArgList &Args) {
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000151 unsigned i = 0;
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000152 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000153 it != ie; ++it, ++i) {
154 Arg *A = *it;
155 llvm::errs() << "Option " << i << " - "
156 << "Name: \"" << A->getOption().getName() << "\", "
157 << "Values: {";
158 for (unsigned j = 0; j < A->getNumValues(); ++j) {
159 if (j)
160 llvm::errs() << ", ";
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000161 llvm::errs() << '"' << A->getValue(Args, j) << '"';
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000162 }
163 llvm::errs() << "}\n";
Daniel Dunbar7dc2a042009-03-05 06:38:47 +0000164 }
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000165}
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000166
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000167void Driver::PrintActions(const llvm::SmallVector<Action*, 2> &Actions) {
168 llvm::errs() << "FIXME: Print actions.";
169}
170
171void Driver::BuildUniversalActions(const ArgList &Args,
172 llvm::SmallVector<Action*, 2> &Actions) {
173 // FIXME: Implement
174 BuildActions(Args, Actions);
175}
176
177void Driver::BuildActions(const ArgList &Args,
178 llvm::SmallVector<Action*, 2> &Actions) {
179 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.
192 if (!InputType) {
193 // 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))
202 Diag("-E or -x required when input is from standard input");
203 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())
236 Diag("no such file or directory: ") << A->getValue(Args) << "\n";
237 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) {
254 Diag("language not recognized: ") << A->getValue(Args) << "\n";
255 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}
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000289
290// FIXME: Migrate to a normal diagnostics client.
291llvm::raw_ostream &Driver::Diag(const char *Message) const {
292 return (llvm::errs() << Message);
293}