blob: e846b091c51a0c5ce5b4764bfd92bc10983646db [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
15#include "clang/Driver/Compilation.h"
16#include "clang/Driver/Driver.h"
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000017#include "clang/Driver/Option.h"
Daniel Dunbaraf20afb2010-02-25 03:23:43 +000018#include "clang/Frontend/DiagnosticOptions.h"
19#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000020
Chris Lattner7f9fc3f2011-03-23 04:04:01 +000021#include "llvm/ADT/ArrayRef.h"
Daniel Dunbar510d7322009-03-18 02:11:26 +000022#include "llvm/ADT/SmallString.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000023#include "llvm/ADT/SmallVector.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000024#include "llvm/ADT/OwningPtr.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000025#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000026#include "llvm/Support/FileSystem.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000027#include "llvm/Support/ManagedStatic.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000028#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000029#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000030#include "llvm/Support/Regex.h"
Chris Lattner30bc7e82010-03-30 05:39:52 +000031#include "llvm/Support/Timer.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000032#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000033#include "llvm/Support/Host.h"
34#include "llvm/Support/Path.h"
35#include "llvm/Support/Program.h"
36#include "llvm/Support/Signals.h"
Evan Chenga6b40452011-08-24 18:09:14 +000037#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000039#include "llvm/Support/system_error.h"
Douglas Gregor43d013d2011-01-17 19:15:43 +000040#include <cctype>
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000041using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000042using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000043
Benjamin Krameraeed3da2010-10-30 17:32:40 +000044llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
Rafael Espindola0f4c59c2009-12-04 19:31:58 +000045 if (!CanonicalPrefixes)
46 return llvm::sys::Path(Argv0);
47
Daniel Dunbar734932c2009-03-18 20:25:53 +000048 // This just needs to be some symbol in the binary; C++ doesn't
49 // allow taking the address of ::main however.
50 void *P = (void*) (intptr_t) GetExecutablePath;
51 return llvm::sys::Path::GetMainExecutable(Argv0, P);
52}
53
Daniel Dunbar237a31b2009-07-17 18:10:27 +000054static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Chris Lattner5f9e2722011-07-23 10:55:15 +000055 StringRef S) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000056 return SavedStrings.insert(S).first->c_str();
57}
58
59/// ApplyQAOverride - Apply a list of edits to the input argument lists.
60///
61/// The input string is a space separate list of edits to perform,
62/// they are applied in order to the input argument lists. Edits
63/// should be one of the following forms:
64///
Daniel Dunbare3d60232009-07-16 21:32:51 +000065/// '#': Silence information about the changes to the command line arguments.
66///
Daniel Dunbarec9587d2009-04-17 01:54:00 +000067/// '^': Add FOO as a new argument at the beginning of the command line.
68///
69/// '+': Add FOO as a new argument at the end of the command line.
70///
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000071/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
72/// line.
Daniel Dunbarec9587d2009-04-17 01:54:00 +000073///
74/// 'xOPTION': Removes all instances of the literal argument OPTION.
75///
76/// 'XOPTION': Removes all instances of the literal argument OPTION,
77/// and the following argument.
78///
79/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
80/// at the end of the command line.
Daniel Dunbare3d60232009-07-16 21:32:51 +000081///
82/// \param OS - The stream to write edit information to.
83/// \param Args - The vector of command line arguments.
84/// \param Edit - The override command to perform.
85/// \param SavedStrings - Set to use for storing string representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +000086static void ApplyOneQAOverride(raw_ostream &OS,
87 SmallVectorImpl<const char*> &Args,
88 StringRef Edit,
Chris Lattner30bc7e82010-03-30 05:39:52 +000089 std::set<std::string> &SavedStrings) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000090 // This does not need to be efficient.
91
Daniel Dunbar237a31b2009-07-17 18:10:27 +000092 if (Edit[0] == '^') {
93 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000094 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +000095 OS << "### Adding argument " << Str << " at beginning\n";
96 Args.insert(Args.begin() + 1, Str);
97 } else if (Edit[0] == '+') {
98 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000099 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000100 OS << "### Adding argument " << Str << " at end\n";
101 Args.push_back(Str);
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000102 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103 Edit.slice(2, Edit.size()-1).find('/') != StringRef::npos) {
104 StringRef MatchPattern = Edit.substr(2).split('/').first;
105 StringRef ReplPattern = Edit.substr(2).split('/').second;
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000106 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
107
108 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
109 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
110
111 if (Repl != Args[i]) {
112 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
113 Args[i] = SaveStringInSet(SavedStrings, Repl);
114 }
115 }
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000116 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
117 std::string Option = Edit.substr(1, std::string::npos);
118 for (unsigned i = 1; i < Args.size();) {
119 if (Option == Args[i]) {
120 OS << "### Deleting argument " << Args[i] << '\n';
121 Args.erase(Args.begin() + i);
122 if (Edit[0] == 'X') {
123 if (i < Args.size()) {
124 OS << "### Deleting argument " << Args[i] << '\n';
125 Args.erase(Args.begin() + i);
126 } else
127 OS << "### Invalid X edit, end of command line!\n";
128 }
129 } else
130 ++i;
131 }
132 } else if (Edit[0] == 'O') {
133 for (unsigned i = 1; i < Args.size();) {
134 const char *A = Args[i];
135 if (A[0] == '-' && A[1] == 'O' &&
136 (A[2] == '\0' ||
137 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
138 ('0' <= A[2] && A[2] <= '9'))))) {
139 OS << "### Deleting argument " << Args[i] << '\n';
140 Args.erase(Args.begin() + i);
141 } else
142 ++i;
143 }
144 OS << "### Adding argument " << Edit << " at end\n";
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000145 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str()));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000146 } else {
147 OS << "### Unrecognized edit: " << Edit << "\n";
148 }
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000149}
150
151/// ApplyQAOverride - Apply a comma separate list of edits to the
152/// input argument lists. See ApplyOneQAOverride.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000153static void ApplyQAOverride(SmallVectorImpl<const char*> &Args,
Chris Lattner30bc7e82010-03-30 05:39:52 +0000154 const char *OverrideStr,
155 std::set<std::string> &SavedStrings) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000156 raw_ostream *OS = &llvm::errs();
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000157
Daniel Dunbare3d60232009-07-16 21:32:51 +0000158 if (OverrideStr[0] == '#') {
159 ++OverrideStr;
160 OS = &llvm::nulls();
161 }
162
163 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000164
165 // This does not need to be efficient.
166
167 const char *S = OverrideStr;
168 while (*S) {
169 const char *End = ::strchr(S, ' ');
170 if (!End)
171 End = S + strlen(S);
172 if (End != S)
Daniel Dunbare3d60232009-07-16 21:32:51 +0000173 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000174 S = End;
175 if (*S != '\0')
176 ++S;
177 }
178}
179
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000180extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
Daniel Dunbar545c2812009-11-29 20:58:32 +0000181 const char *Argv0, void *MainAddr);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000182extern int cc1as_main(const char **ArgBegin, const char **ArgEnd,
183 const char *Argv0, void *MainAddr);
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000184
Rafael Espindola8a1af322010-07-19 15:20:12 +0000185static void ExpandArgsFromBuf(const char *Arg,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000186 SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola8a1af322010-07-19 15:20:12 +0000187 std::set<std::string> &SavedStrings) {
188 const char *FName = Arg + 1;
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +0000189 OwningPtr<llvm::MemoryBuffer> MemBuf;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000190 if (llvm::MemoryBuffer::getFile(FName, MemBuf)) {
Rafael Espindola8a1af322010-07-19 15:20:12 +0000191 ArgVector.push_back(SaveStringInSet(SavedStrings, Arg));
192 return;
193 }
194
195 const char *Buf = MemBuf->getBufferStart();
196 char InQuote = ' ';
197 std::string CurArg;
198
199 for (const char *P = Buf; ; ++P) {
200 if (*P == '\0' || (isspace(*P) && InQuote == ' ')) {
201 if (!CurArg.empty()) {
202
203 if (CurArg[0] != '@') {
204 ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg));
205 } else {
206 ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings);
207 }
208
209 CurArg = "";
210 }
211 if (*P == '\0')
212 break;
213 else
214 continue;
215 }
216
217 if (isspace(*P)) {
218 if (InQuote != ' ')
219 CurArg.push_back(*P);
220 continue;
221 }
222
223 if (*P == '"' || *P == '\'') {
224 if (InQuote == *P)
225 InQuote = ' ';
226 else if (InQuote == ' ')
227 InQuote = *P;
228 else
229 CurArg.push_back(*P);
230 continue;
231 }
232
233 if (*P == '\\') {
234 ++P;
235 if (*P != '\0')
236 CurArg.push_back(*P);
237 continue;
238 }
239 CurArg.push_back(*P);
240 }
Rafael Espindola8a1af322010-07-19 15:20:12 +0000241}
242
243static void ExpandArgv(int argc, const char **argv,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000244 SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola8a1af322010-07-19 15:20:12 +0000245 std::set<std::string> &SavedStrings) {
246 for (int i = 0; i < argc; ++i) {
247 const char *Arg = argv[i];
248 if (Arg[0] != '@') {
249 ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg)));
250 continue;
251 }
252
253 ExpandArgsFromBuf(Arg, ArgVector, SavedStrings);
254 }
255}
256
Chris Lattner5f9e2722011-07-23 10:55:15 +0000257static void ParseProgName(SmallVectorImpl<const char *> &ArgVector,
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000258 std::set<std::string> &SavedStrings,
259 Driver &TheDriver)
260{
261 // Try to infer frontend type and default target from the program name.
262
263 // suffixes[] contains the list of known driver suffixes.
264 // Suffixes are compared against the program name in order.
265 // If there is a match, the frontend type is updated as necessary (CPP/C++).
266 // If there is no match, a second round is done after stripping the last
267 // hyphen and everything following it. This allows using something like
268 // "clang++-2.9".
269
270 // If there is a match in either the first or second round,
271 // the function tries to identify a target as prefix. E.g.
272 // "x86_64-linux-clang" as interpreted as suffix "clang" with
273 // target prefix "x86_64-linux". If such a target prefix is found,
Sebastian Pop9ec60df2012-01-20 22:01:23 +0000274 // is gets added via -target as implicit first argument.
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000275 static const struct {
276 const char *Suffix;
277 bool IsCXX;
278 bool IsCPP;
279 } suffixes [] = {
280 { "clang", false, false },
281 { "clang++", true, false },
282 { "clang-c++", true, false },
283 { "clang-cc", false, false },
284 { "clang-cpp", false, true },
285 { "clang-g++", true, false },
286 { "clang-gcc", false, false },
287 { "cc", false, false },
288 { "cpp", false, true },
289 { "++", true, false },
290 };
291 std::string ProgName(llvm::sys::path::stem(ArgVector[0]));
Chris Lattner5f9e2722011-07-23 10:55:15 +0000292 StringRef ProgNameRef(ProgName);
293 StringRef Prefix;
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000294
295 for (int Components = 2; Components; --Components) {
296 bool FoundMatch = false;
297 size_t i;
298
299 for (i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) {
300 if (ProgNameRef.endswith(suffixes[i].Suffix)) {
301 FoundMatch = true;
302 if (suffixes[i].IsCXX)
303 TheDriver.CCCIsCXX = true;
304 if (suffixes[i].IsCPP)
305 TheDriver.CCCIsCPP = true;
306 break;
307 }
308 }
309
310 if (FoundMatch) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 StringRef::size_type LastComponent = ProgNameRef.rfind('-',
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000312 ProgNameRef.size() - strlen(suffixes[i].Suffix));
Chris Lattner5f9e2722011-07-23 10:55:15 +0000313 if (LastComponent != StringRef::npos)
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000314 Prefix = ProgNameRef.slice(0, LastComponent);
315 break;
316 }
317
Chris Lattner5f9e2722011-07-23 10:55:15 +0000318 StringRef::size_type LastComponent = ProgNameRef.rfind('-');
319 if (LastComponent == StringRef::npos)
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000320 break;
321 ProgNameRef = ProgNameRef.slice(0, LastComponent);
322 }
323
324 if (Prefix.empty())
325 return;
326
327 std::string IgnoredError;
328 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000329 SmallVectorImpl<const char *>::iterator it = ArgVector.begin();
Joerg Sonnenbergerca026432011-07-07 16:57:26 +0000330 if (it != ArgVector.end())
331 ++it;
332 ArgVector.insert(it, SaveStringInSet(SavedStrings, Prefix));
333 ArgVector.insert(it,
Sebastian Pop9ec60df2012-01-20 22:01:23 +0000334 SaveStringInSet(SavedStrings, std::string("-target")));
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000335 }
336}
337
Rafael Espindola8a1af322010-07-19 15:20:12 +0000338int main(int argc_, const char **argv_) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000339 llvm::sys::PrintStackTraceOnErrorSignal();
Rafael Espindola8a1af322010-07-19 15:20:12 +0000340 llvm::PrettyStackTraceProgram X(argc_, argv_);
341
342 std::set<std::string> SavedStrings;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000343 SmallVector<const char*, 256> argv;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000344
345 ExpandArgv(argc_, argv_, argv, SavedStrings);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000346
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000347 // Handle -cc1 integrated tools.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000348 if (argv.size() > 1 && StringRef(argv[1]).startswith("-cc1")) {
349 StringRef Tool = argv[1] + 4;
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000350
351 if (Tool == "")
Daniel Dunbare5d69672010-07-20 02:47:40 +0000352 return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000353 (void*) (intptr_t) GetExecutablePath);
354 if (Tool == "as")
Daniel Dunbare5d69672010-07-20 02:47:40 +0000355 return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000356 (void*) (intptr_t) GetExecutablePath);
357
358 // Reject unknown tools.
359 llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
360 return 1;
361 }
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000362
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000363 bool CanonicalPrefixes = true;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000364 for (int i = 1, size = argv.size(); i < size; ++i) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000365 if (StringRef(argv[i]) == "-no-canonical-prefixes") {
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000366 CanonicalPrefixes = false;
367 break;
368 }
369 }
370
371 llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes);
372
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000373 TextDiagnosticPrinter *DiagClient
374 = new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
Michael J. Spencerd5b08be2010-12-18 04:13:32 +0000375 DiagClient->setPrefix(llvm::sys::path::stem(Path.str()));
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000376 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
David Blaikied6471f72011-09-25 23:23:43 +0000377 DiagnosticsEngine Diags(DiagID, DiagClient);
Daniel Dunbar510d7322009-03-18 02:11:26 +0000378
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000379#ifdef CLANG_IS_PRODUCTION
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000380 const bool IsProduction = true;
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000381#else
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000382 const bool IsProduction = false;
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000383#endif
Sebastian Pop5d8b9542011-11-01 21:33:06 +0000384 Driver TheDriver(Path.str(), llvm::sys::getDefaultTargetTriple(),
Bob Wilson10a82cd2011-10-04 05:34:14 +0000385 "a.out", IsProduction, Diags);
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000386
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000387 // Attempt to find the original path used to invoke the driver, to determine
388 // the installed path. We do this manually, because we want to support that
389 // path being a symlink.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000390 {
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000391 SmallString<128> InstalledPath(argv[0]);
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000392
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000393 // Do a PATH lookup, if there are no directory components.
394 if (llvm::sys::path::filename(InstalledPath) == InstalledPath) {
395 llvm::sys::Path Tmp = llvm::sys::Program::FindProgramByName(
396 llvm::sys::path::filename(InstalledPath.str()));
397 if (!Tmp.empty())
398 InstalledPath = Tmp.str();
399 }
400 llvm::sys::fs::make_absolute(InstalledPath);
401 InstalledPath = llvm::sys::path::parent_path(InstalledPath);
402 bool exists;
403 if (!llvm::sys::fs::exists(InstalledPath.str(), exists) && exists)
404 TheDriver.setInstalledDir(InstalledPath);
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000405 }
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000406
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000407 llvm::InitializeAllTargets();
408 ParseProgName(argv, SavedStrings, TheDriver);
Joerg Sonnenberger9ade4ae2011-03-06 23:31:01 +0000409
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000410 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
411 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
412 if (TheDriver.CCPrintOptions)
413 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
414
Daniel Dunbar322c29f2011-02-02 21:11:35 +0000415 // Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE.
416 TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS");
417 if (TheDriver.CCPrintHeaders)
418 TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE");
419
Daniel Dunbarc8a22b02011-04-07 18:01:20 +0000420 // Handle CC_LOG_DIAGNOSTICS and CC_LOG_DIAGNOSTICS_FILE.
421 TheDriver.CCLogDiagnostics = !!::getenv("CC_LOG_DIAGNOSTICS");
422 if (TheDriver.CCLogDiagnostics)
423 TheDriver.CCLogDiagnosticsFilename = ::getenv("CC_LOG_DIAGNOSTICS_FILE");
424
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000425 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
426 // command line behind the scenes.
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000427 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
428 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000429 ApplyQAOverride(argv, OverrideStr, SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000430 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000431 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000432 std::vector<const char*> ExtraArgs;
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000433
434 for (;;) {
435 const char *Next = strchr(Cur, ',');
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000436
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000437 if (Next) {
Rafael Espindola8a1af322010-07-19 15:20:12 +0000438 ExtraArgs.push_back(SaveStringInSet(SavedStrings,
439 std::string(Cur, Next)));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000440 Cur = Next + 1;
441 } else {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000442 if (*Cur != '\0')
Rafael Espindola8a1af322010-07-19 15:20:12 +0000443 ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000444 break;
445 }
446 }
447
Daniel Dunbare5d69672010-07-20 02:47:40 +0000448 argv.insert(&argv[1], ExtraArgs.begin(), ExtraArgs.end());
Rafael Espindola06e35d32010-07-18 22:03:55 +0000449 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000450
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +0000451 OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv));
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000452 int Res = 0;
Chad Rosier2b819102011-08-02 17:58:04 +0000453 const Command *FailingCommand = 0;
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000454 if (C.get())
Chad Rosier2b819102011-08-02 17:58:04 +0000455 Res = TheDriver.ExecuteCompilation(*C, FailingCommand);
456
457 // If result status is < 0, then the driver command signalled an error.
458 // In this case, generate additional diagnostic information if possible.
459 if (Res < 0)
460 TheDriver.generateCompilationDiagnostics(*C, FailingCommand);
461
Chris Lattner30bc7e82010-03-30 05:39:52 +0000462 // If any timers were active but haven't been destroyed yet, print their
463 // results now. This happens in -disable-free mode.
464 llvm::TimerGroup::printAll(llvm::errs());
465
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000466 llvm::llvm_shutdown();
467
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000468 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000469}