blob: 90cf6828e0fd85836de2a15d0a7cf17079a7ee72 [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 Dunbar93468492009-03-12 08:55:43 +000051 Diag(clang::diag::driver_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.
135 llvm::SmallVector<Action*, 2> Actions;
136 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 Dunbardb62cc32009-03-12 07:58:46 +0000152void Driver::PrintOptions(const ArgList &Args) {
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 Dunbardb62cc32009-03-12 07:58:46 +0000169void Driver::PrintActions(const llvm::SmallVector<Action*, 2> &Actions) {
170 llvm::errs() << "FIXME: Print actions.";
171}
172
173void Driver::BuildUniversalActions(const ArgList &Args,
174 llvm::SmallVector<Action*, 2> &Actions) {
175 // FIXME: Implement
176 BuildActions(Args, Actions);
177}
178
179void Driver::BuildActions(const ArgList &Args,
180 llvm::SmallVector<Action*, 2> &Actions) {
181 types::ID InputType = types::TY_INVALID;
182 Arg *InputTypeArg = 0;
183
184 llvm::SmallVector<std::pair<types::ID, const Arg*>, 16> Inputs;
185 for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
186 it != ie; ++it) {
187 Arg *A = *it;
188
189 if (isa<InputOption>(A->getOption())) {
190 const char *Value = A->getValue(Args);
191 types::ID Ty = types::TY_INVALID;
192
193 // Infer the input type if necessary.
Daniel Dunbar93468492009-03-12 08:55:43 +0000194 if (InputType == types::TY_INVALID) {
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000195 // stdin must be handled specially.
196 if (memcmp(Value, "-", 2) == 0) {
197 // If running with -E, treat as a C input (this changes the
198 // builtin macros, for example). This may be overridden by
199 // -ObjC below.
200 //
201 // Otherwise emit an error but still use a valid type to
202 // avoid spurious errors (e.g., no inputs).
203 if (!Args.hasArg(options::OPT_E))
Daniel Dunbar93468492009-03-12 08:55:43 +0000204 Diag(clang::diag::driver_unknown_stdin_type);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000205 Ty = types::TY_C;
206 } else {
207 // Otherwise lookup by extension, and fallback to ObjectType
208 // if not found.
209 if (const char *Ext = strrchr(Value, '.'))
210 Ty = types::lookupTypeForExtension(Ext + 1);
211 if (Ty == types::TY_INVALID)
212 Ty = types::TY_Object;
213 }
214
215 // -ObjC and -ObjC++ override the default language, but only
216 // -for "source files". We just treat everything that isn't a
217 // -linker input as a source file.
218 //
219 // FIXME: Clean this up if we move the phase sequence into the
220 // type.
221 if (Ty != types::TY_Object) {
222 if (Args.hasArg(options::OPT_ObjC))
223 Ty = types::TY_ObjC;
224 else if (Args.hasArg(options::OPT_ObjCXX))
225 Ty = types::TY_ObjCXX;
226 }
227 } else {
228 assert(InputTypeArg && "InputType set w/o InputTypeArg");
229 InputTypeArg->claim();
230 Ty = InputType;
231 }
232
233 // Check that the file exists. It isn't clear this is worth
234 // doing, since the tool presumably does this anyway, and this
235 // just adds an extra stat to the equation, but this is gcc
236 // compatible.
237 if (memcmp(Value, "-", 2) != 0 && !llvm::sys::Path(Value).exists())
Daniel Dunbar93468492009-03-12 08:55:43 +0000238 Diag(clang::diag::driver_no_such_file) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000239 else
240 Inputs.push_back(std::make_pair(Ty, A));
241
242 } else if (A->getOption().isLinkerInput()) {
243 // Just treat as object type, we could make a special type for
244 // this if necessary.
245 Inputs.push_back(std::make_pair(types::TY_Object, A));
246
247 } else if (A->getOption().getId() == options::OPT_x) {
248 InputTypeArg = A;
249 InputType = types::lookupTypeForTypeSpecifier(A->getValue(Args));
250
251 // Follow gcc behavior and treat as linker input for invalid -x
252 // options. Its not clear why we shouldn't just revert to
253 // unknown; but this isn't very important, we might as well be
254 // bug comatible.
255 if (!InputType) {
Daniel Dunbar93468492009-03-12 08:55:43 +0000256 Diag(clang::diag::driver_unknown_language) << A->getValue(Args);
Daniel Dunbardb62cc32009-03-12 07:58:46 +0000257 InputType = types::TY_Object;
258 }
259 }
260 }
261
262 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
263 llvm::errs() << "input " << i << ": "
264 << Inputs[i].second->getValue(Args) << "\n";
265 }
266 exit(0);
267}
268
Daniel Dunbard25acaa2009-03-10 23:41:59 +0000269HostInfo *Driver::GetHostInfo(const char *Triple) {
270 // Dice into arch, platform, and OS. This matches
271 // arch,platform,os = '(.*?)-(.*?)-(.*?)'
272 // and missing fields are left empty.
273 std::string Arch, Platform, OS;
274
275 if (const char *ArchEnd = strchr(Triple, '-')) {
276 Arch = std::string(Triple, ArchEnd);
277
278 if (const char *PlatformEnd = strchr(ArchEnd+1, '-')) {
279 Platform = std::string(ArchEnd+1, PlatformEnd);
280 OS = PlatformEnd+1;
281 } else
282 Platform = ArchEnd+1;
283 } else
284 Arch = Triple;
285
286 if (memcmp(&Platform[0], "darwin", 6) == 0)
287 return new DarwinHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
288
289 return new UnknownHostInfo(Arch.c_str(), Platform.c_str(), OS.c_str());
290}