blob: 353d06fb14e4cb5c9ee64926651d346d9bce1589 [file] [log] [blame]
Daniel Dunbar3ede8d02009-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 Dunbar1eb4e642009-03-03 05:55:11 +000010// This is the entry point to the clang driver; it is a thin wrapper
11// for functionality in the Driver clang library.
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000012//
13//===----------------------------------------------------------------------===//
14
Douglas Gregor02c23eb2012-10-23 22:26:28 +000015#include "clang/Basic/DiagnosticOptions.h"
Chad Rosier2dec85b2012-03-13 20:09:56 +000016#include "clang/Driver/ArgList.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000017#include "clang/Driver/Compilation.h"
18#include "clang/Driver/Driver.h"
Chad Rosier2dec85b2012-03-13 20:09:56 +000019#include "clang/Driver/OptTable.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000020#include "clang/Driver/Option.h"
21#include "clang/Driver/Options.h"
Chad Rosier2dec85b2012-03-13 20:09:56 +000022#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbaraf20afb2010-02-25 03:23:43 +000023#include "clang/Frontend/TextDiagnosticPrinter.h"
Chad Rosier2dec85b2012-03-13 20:09:56 +000024#include "clang/Frontend/Utils.h"
Chris Lattner7f9fc3f2011-03-23 04:04:01 +000025#include "llvm/ADT/ArrayRef.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000026#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar510d7322009-03-18 02:11:26 +000027#include "llvm/ADT/SmallString.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000028#include "llvm/ADT/SmallVector.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000029#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000030#include "llvm/Support/FileSystem.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000031#include "llvm/Support/Host.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000032#include "llvm/Support/ManagedStatic.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000033#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000034#include "llvm/Support/Path.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000035#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000036#include "llvm/Support/Program.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000037#include "llvm/Support/Regex.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000038#include "llvm/Support/Signals.h"
Evan Chenga6b40452011-08-24 18:09:14 +000039#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/TargetSelect.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000041#include "llvm/Support/Timer.h"
42#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000043#include "llvm/Support/system_error.h"
Douglas Gregor43d013d2011-01-17 19:15:43 +000044#include <cctype>
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000045using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000046using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000047
Benjamin Krameraeed3da2010-10-30 17:32:40 +000048llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
Rafael Espindola0f4c59c2009-12-04 19:31:58 +000049 if (!CanonicalPrefixes)
50 return llvm::sys::Path(Argv0);
51
Daniel Dunbar734932c2009-03-18 20:25:53 +000052 // This just needs to be some symbol in the binary; C++ doesn't
53 // allow taking the address of ::main however.
54 void *P = (void*) (intptr_t) GetExecutablePath;
55 return llvm::sys::Path::GetMainExecutable(Argv0, P);
56}
57
Daniel Dunbar237a31b2009-07-17 18:10:27 +000058static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Chris Lattner5f9e2722011-07-23 10:55:15 +000059 StringRef S) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000060 return SavedStrings.insert(S).first->c_str();
61}
62
63/// ApplyQAOverride - Apply a list of edits to the input argument lists.
64///
65/// The input string is a space separate list of edits to perform,
66/// they are applied in order to the input argument lists. Edits
67/// should be one of the following forms:
68///
Daniel Dunbare3d60232009-07-16 21:32:51 +000069/// '#': Silence information about the changes to the command line arguments.
70///
Daniel Dunbarec9587d2009-04-17 01:54:00 +000071/// '^': Add FOO as a new argument at the beginning of the command line.
72///
73/// '+': Add FOO as a new argument at the end of the command line.
74///
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000075/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
76/// line.
Daniel Dunbarec9587d2009-04-17 01:54:00 +000077///
78/// 'xOPTION': Removes all instances of the literal argument OPTION.
79///
80/// 'XOPTION': Removes all instances of the literal argument OPTION,
81/// and the following argument.
82///
83/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
84/// at the end of the command line.
Daniel Dunbare3d60232009-07-16 21:32:51 +000085///
86/// \param OS - The stream to write edit information to.
87/// \param Args - The vector of command line arguments.
88/// \param Edit - The override command to perform.
89/// \param SavedStrings - Set to use for storing string representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +000090static void ApplyOneQAOverride(raw_ostream &OS,
91 SmallVectorImpl<const char*> &Args,
92 StringRef Edit,
Chris Lattner30bc7e82010-03-30 05:39:52 +000093 std::set<std::string> &SavedStrings) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000094 // This does not need to be efficient.
95
Daniel Dunbar237a31b2009-07-17 18:10:27 +000096 if (Edit[0] == '^') {
97 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000098 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +000099 OS << "### Adding argument " << Str << " at beginning\n";
100 Args.insert(Args.begin() + 1, Str);
101 } else if (Edit[0] == '+') {
102 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000103 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000104 OS << "### Adding argument " << Str << " at end\n";
105 Args.push_back(Str);
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000106 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +0000107 Edit.slice(2, Edit.size()-1).find('/') != StringRef::npos) {
108 StringRef MatchPattern = Edit.substr(2).split('/').first;
109 StringRef ReplPattern = Edit.substr(2).split('/').second;
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000110 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
111
112 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
113 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
114
115 if (Repl != Args[i]) {
116 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
117 Args[i] = SaveStringInSet(SavedStrings, Repl);
118 }
119 }
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000120 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
121 std::string Option = Edit.substr(1, std::string::npos);
122 for (unsigned i = 1; i < Args.size();) {
123 if (Option == Args[i]) {
124 OS << "### Deleting argument " << Args[i] << '\n';
125 Args.erase(Args.begin() + i);
126 if (Edit[0] == 'X') {
127 if (i < Args.size()) {
128 OS << "### Deleting argument " << Args[i] << '\n';
129 Args.erase(Args.begin() + i);
130 } else
131 OS << "### Invalid X edit, end of command line!\n";
132 }
133 } else
134 ++i;
135 }
136 } else if (Edit[0] == 'O') {
137 for (unsigned i = 1; i < Args.size();) {
138 const char *A = Args[i];
139 if (A[0] == '-' && A[1] == 'O' &&
140 (A[2] == '\0' ||
141 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
142 ('0' <= A[2] && A[2] <= '9'))))) {
143 OS << "### Deleting argument " << Args[i] << '\n';
144 Args.erase(Args.begin() + i);
145 } else
146 ++i;
147 }
148 OS << "### Adding argument " << Edit << " at end\n";
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000149 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str()));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000150 } else {
151 OS << "### Unrecognized edit: " << Edit << "\n";
152 }
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000153}
154
155/// ApplyQAOverride - Apply a comma separate list of edits to the
156/// input argument lists. See ApplyOneQAOverride.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000157static void ApplyQAOverride(SmallVectorImpl<const char*> &Args,
Chris Lattner30bc7e82010-03-30 05:39:52 +0000158 const char *OverrideStr,
159 std::set<std::string> &SavedStrings) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000160 raw_ostream *OS = &llvm::errs();
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000161
Daniel Dunbare3d60232009-07-16 21:32:51 +0000162 if (OverrideStr[0] == '#') {
163 ++OverrideStr;
164 OS = &llvm::nulls();
165 }
166
167 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000168
169 // This does not need to be efficient.
170
171 const char *S = OverrideStr;
172 while (*S) {
173 const char *End = ::strchr(S, ' ');
174 if (!End)
175 End = S + strlen(S);
176 if (End != S)
Daniel Dunbare3d60232009-07-16 21:32:51 +0000177 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000178 S = End;
179 if (*S != '\0')
180 ++S;
181 }
182}
183
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000184extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
Daniel Dunbar545c2812009-11-29 20:58:32 +0000185 const char *Argv0, void *MainAddr);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000186extern int cc1as_main(const char **ArgBegin, const char **ArgEnd,
187 const char *Argv0, void *MainAddr);
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000188
Rafael Espindola8a1af322010-07-19 15:20:12 +0000189static void ExpandArgsFromBuf(const char *Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190 SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola8a1af322010-07-19 15:20:12 +0000191 std::set<std::string> &SavedStrings) {
192 const char *FName = Arg + 1;
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +0000193 OwningPtr<llvm::MemoryBuffer> MemBuf;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000194 if (llvm::MemoryBuffer::getFile(FName, MemBuf)) {
Rafael Espindola8a1af322010-07-19 15:20:12 +0000195 ArgVector.push_back(SaveStringInSet(SavedStrings, Arg));
196 return;
197 }
198
199 const char *Buf = MemBuf->getBufferStart();
200 char InQuote = ' ';
201 std::string CurArg;
202
203 for (const char *P = Buf; ; ++P) {
204 if (*P == '\0' || (isspace(*P) && InQuote == ' ')) {
205 if (!CurArg.empty()) {
206
207 if (CurArg[0] != '@') {
208 ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg));
209 } else {
210 ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings);
211 }
212
213 CurArg = "";
214 }
215 if (*P == '\0')
216 break;
217 else
218 continue;
219 }
220
221 if (isspace(*P)) {
222 if (InQuote != ' ')
223 CurArg.push_back(*P);
224 continue;
225 }
226
227 if (*P == '"' || *P == '\'') {
228 if (InQuote == *P)
229 InQuote = ' ';
230 else if (InQuote == ' ')
231 InQuote = *P;
232 else
233 CurArg.push_back(*P);
234 continue;
235 }
236
237 if (*P == '\\') {
238 ++P;
239 if (*P != '\0')
240 CurArg.push_back(*P);
241 continue;
242 }
243 CurArg.push_back(*P);
244 }
Rafael Espindola8a1af322010-07-19 15:20:12 +0000245}
246
247static void ExpandArgv(int argc, const char **argv,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000248 SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola8a1af322010-07-19 15:20:12 +0000249 std::set<std::string> &SavedStrings) {
250 for (int i = 0; i < argc; ++i) {
251 const char *Arg = argv[i];
252 if (Arg[0] != '@') {
253 ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg)));
254 continue;
255 }
256
257 ExpandArgsFromBuf(Arg, ArgVector, SavedStrings);
258 }
259}
260
Chris Lattner5f9e2722011-07-23 10:55:15 +0000261static void ParseProgName(SmallVectorImpl<const char *> &ArgVector,
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000262 std::set<std::string> &SavedStrings,
263 Driver &TheDriver)
264{
265 // Try to infer frontend type and default target from the program name.
266
267 // suffixes[] contains the list of known driver suffixes.
268 // Suffixes are compared against the program name in order.
269 // If there is a match, the frontend type is updated as necessary (CPP/C++).
270 // If there is no match, a second round is done after stripping the last
271 // hyphen and everything following it. This allows using something like
272 // "clang++-2.9".
273
274 // If there is a match in either the first or second round,
275 // the function tries to identify a target as prefix. E.g.
276 // "x86_64-linux-clang" as interpreted as suffix "clang" with
277 // target prefix "x86_64-linux". If such a target prefix is found,
Sebastian Pop9ec60df2012-01-20 22:01:23 +0000278 // is gets added via -target as implicit first argument.
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000279 static const struct {
280 const char *Suffix;
281 bool IsCXX;
282 bool IsCPP;
283 } suffixes [] = {
Chandler Carrutha74e4af2012-09-11 10:40:21 +0000284 { "clang", false, false },
285 { "clang++", true, false },
286 { "clang-c++", true, false },
287 { "clang-cc", false, false },
288 { "clang-cpp", false, true },
289 { "clang-g++", true, false },
290 { "clang-gcc", false, false },
291 { "cc", false, false },
292 { "cpp", false, true },
293 { "++", true, false },
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000294 };
295 std::string ProgName(llvm::sys::path::stem(ArgVector[0]));
Chris Lattner5f9e2722011-07-23 10:55:15 +0000296 StringRef ProgNameRef(ProgName);
297 StringRef Prefix;
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000298
299 for (int Components = 2; Components; --Components) {
300 bool FoundMatch = false;
301 size_t i;
302
303 for (i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) {
304 if (ProgNameRef.endswith(suffixes[i].Suffix)) {
305 FoundMatch = true;
Chandler Carrutha74e4af2012-09-11 10:40:21 +0000306 if (suffixes[i].IsCXX)
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000307 TheDriver.CCCIsCXX = true;
308 if (suffixes[i].IsCPP)
309 TheDriver.CCCIsCPP = true;
310 break;
311 }
312 }
313
314 if (FoundMatch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000315 StringRef::size_type LastComponent = ProgNameRef.rfind('-',
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000316 ProgNameRef.size() - strlen(suffixes[i].Suffix));
Chris Lattner5f9e2722011-07-23 10:55:15 +0000317 if (LastComponent != StringRef::npos)
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000318 Prefix = ProgNameRef.slice(0, LastComponent);
319 break;
320 }
321
Chris Lattner5f9e2722011-07-23 10:55:15 +0000322 StringRef::size_type LastComponent = ProgNameRef.rfind('-');
323 if (LastComponent == StringRef::npos)
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000324 break;
325 ProgNameRef = ProgNameRef.slice(0, LastComponent);
326 }
327
328 if (Prefix.empty())
329 return;
330
331 std::string IgnoredError;
332 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000333 SmallVectorImpl<const char *>::iterator it = ArgVector.begin();
Joerg Sonnenbergerca026432011-07-07 16:57:26 +0000334 if (it != ArgVector.end())
335 ++it;
336 ArgVector.insert(it, SaveStringInSet(SavedStrings, Prefix));
337 ArgVector.insert(it,
Sebastian Pop9ec60df2012-01-20 22:01:23 +0000338 SaveStringInSet(SavedStrings, std::string("-target")));
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000339 }
340}
341
Rafael Espindola8a1af322010-07-19 15:20:12 +0000342int main(int argc_, const char **argv_) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000343 llvm::sys::PrintStackTraceOnErrorSignal();
Rafael Espindola8a1af322010-07-19 15:20:12 +0000344 llvm::PrettyStackTraceProgram X(argc_, argv_);
345
346 std::set<std::string> SavedStrings;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000347 SmallVector<const char*, 256> argv;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000348
349 ExpandArgv(argc_, argv_, argv, SavedStrings);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000350
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000351 // Handle -cc1 integrated tools.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352 if (argv.size() > 1 && StringRef(argv[1]).startswith("-cc1")) {
353 StringRef Tool = argv[1] + 4;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000354
355 if (Tool == "")
Daniel Dunbare5d69672010-07-20 02:47:40 +0000356 return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000357 (void*) (intptr_t) GetExecutablePath);
358 if (Tool == "as")
Daniel Dunbare5d69672010-07-20 02:47:40 +0000359 return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000360 (void*) (intptr_t) GetExecutablePath);
361
362 // Reject unknown tools.
363 llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
364 return 1;
365 }
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000366
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000367 bool CanonicalPrefixes = true;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000368 for (int i = 1, size = argv.size(); i < size; ++i) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000369 if (StringRef(argv[i]) == "-no-canonical-prefixes") {
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000370 CanonicalPrefixes = false;
371 break;
372 }
373 }
374
375 llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes);
376
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000377 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions;
Chad Rosier2dec85b2012-03-13 20:09:56 +0000378 {
379 // Note that ParseDiagnosticArgs() uses the cc1 option table.
James Molloybfd7a522012-05-01 14:57:16 +0000380 OwningPtr<OptTable> CC1Opts(createDriverOptTable());
Chad Rosier2dec85b2012-03-13 20:09:56 +0000381 unsigned MissingArgIndex, MissingArgCount;
382 OwningPtr<InputArgList> Args(CC1Opts->ParseArgs(argv.begin()+1, argv.end(),
383 MissingArgIndex, MissingArgCount));
384 // We ignore MissingArgCount and the return value of ParseDiagnosticArgs.
385 // Any errors that would be diagnosed here will also be diagnosed later,
386 // when the DiagnosticsEngine actually exists.
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000387 (void) ParseDiagnosticArgs(*DiagOpts, *Args);
Chad Rosier2dec85b2012-03-13 20:09:56 +0000388 }
389 // Now we can create the DiagnosticsEngine with a properly-filled-out
390 // DiagnosticOptions instance.
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000391 TextDiagnosticPrinter *DiagClient
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000392 = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
Michael J. Spencerd5b08be2010-12-18 04:13:32 +0000393 DiagClient->setPrefix(llvm::sys::path::stem(Path.str()));
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000394 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Chad Rosier2dec85b2012-03-13 20:09:56 +0000395
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000396 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
397 ProcessWarningOptions(Diags, *DiagOpts);
Daniel Dunbar510d7322009-03-18 02:11:26 +0000398
Sebastian Pop5d8b9542011-11-01 21:33:06 +0000399 Driver TheDriver(Path.str(), llvm::sys::getDefaultTargetTriple(),
Rafael Espindola17c874a2012-11-27 16:10:37 +0000400 "a.out", Diags);
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000401
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000402 // Attempt to find the original path used to invoke the driver, to determine
403 // the installed path. We do this manually, because we want to support that
404 // path being a symlink.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000405 {
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000406 SmallString<128> InstalledPath(argv[0]);
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000407
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000408 // Do a PATH lookup, if there are no directory components.
409 if (llvm::sys::path::filename(InstalledPath) == InstalledPath) {
410 llvm::sys::Path Tmp = llvm::sys::Program::FindProgramByName(
411 llvm::sys::path::filename(InstalledPath.str()));
412 if (!Tmp.empty())
413 InstalledPath = Tmp.str();
414 }
415 llvm::sys::fs::make_absolute(InstalledPath);
416 InstalledPath = llvm::sys::path::parent_path(InstalledPath);
417 bool exists;
418 if (!llvm::sys::fs::exists(InstalledPath.str(), exists) && exists)
419 TheDriver.setInstalledDir(InstalledPath);
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000420 }
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000421
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000422 llvm::InitializeAllTargets();
423 ParseProgName(argv, SavedStrings, TheDriver);
Joerg Sonnenberger9ade4ae2011-03-06 23:31:01 +0000424
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000425 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
426 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
427 if (TheDriver.CCPrintOptions)
428 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
429
Daniel Dunbar322c29f2011-02-02 21:11:35 +0000430 // Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE.
431 TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS");
432 if (TheDriver.CCPrintHeaders)
433 TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE");
434
Daniel Dunbarc8a22b02011-04-07 18:01:20 +0000435 // Handle CC_LOG_DIAGNOSTICS and CC_LOG_DIAGNOSTICS_FILE.
436 TheDriver.CCLogDiagnostics = !!::getenv("CC_LOG_DIAGNOSTICS");
437 if (TheDriver.CCLogDiagnostics)
438 TheDriver.CCLogDiagnosticsFilename = ::getenv("CC_LOG_DIAGNOSTICS_FILE");
439
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000440 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
441 // command line behind the scenes.
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000442 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
443 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000444 ApplyQAOverride(argv, OverrideStr, SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000445 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000446 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000447 std::vector<const char*> ExtraArgs;
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000448
449 for (;;) {
450 const char *Next = strchr(Cur, ',');
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000451
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000452 if (Next) {
Rafael Espindola8a1af322010-07-19 15:20:12 +0000453 ExtraArgs.push_back(SaveStringInSet(SavedStrings,
454 std::string(Cur, Next)));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000455 Cur = Next + 1;
456 } else {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000457 if (*Cur != '\0')
Rafael Espindola8a1af322010-07-19 15:20:12 +0000458 ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000459 break;
460 }
461 }
462
Daniel Dunbare5d69672010-07-20 02:47:40 +0000463 argv.insert(&argv[1], ExtraArgs.begin(), ExtraArgs.end());
Rafael Espindola06e35d32010-07-18 22:03:55 +0000464 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000465
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +0000466 OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv));
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000467 int Res = 0;
Chad Rosier2b819102011-08-02 17:58:04 +0000468 const Command *FailingCommand = 0;
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000469 if (C.get())
Chad Rosier2b819102011-08-02 17:58:04 +0000470 Res = TheDriver.ExecuteCompilation(*C, FailingCommand);
471
Chad Rosier80ec1272012-04-20 17:08:59 +0000472 // Force a crash to test the diagnostics.
473 if(::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH"))
474 Res = -1;
475
Chad Rosier2b819102011-08-02 17:58:04 +0000476 // If result status is < 0, then the driver command signalled an error.
Chad Rosier5af8de82012-11-12 21:32:24 +0000477 // If result status is 70, then the driver command reported a fatal error.
478 // In these cases, generate additional diagnostic information if possible.
479 if (Res < 0 || Res == 70)
Chad Rosier2b819102011-08-02 17:58:04 +0000480 TheDriver.generateCompilationDiagnostics(*C, FailingCommand);
481
Chris Lattner30bc7e82010-03-30 05:39:52 +0000482 // If any timers were active but haven't been destroyed yet, print their
483 // results now. This happens in -disable-free mode.
484 llvm::TimerGroup::printAll(llvm::errs());
485
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000486 llvm::llvm_shutdown();
487
NAKAMURA Takumidf9b9332012-07-17 05:09:29 +0000488#ifdef _WIN32
489 // Exit status should not be negative on Win32, unless abnormal termination.
490 // Once abnormal termiation was caught, negative status should not be
491 // propagated.
492 if (Res < 0)
493 Res = 1;
494#endif
495
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000496 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000497}