blob: 91b48f15484b51e555827a87b7e815d4e2776b10 [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"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000025#include "llvm/Config/config.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000026#include "llvm/Support/ErrorHandling.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000027#include "llvm/Support/FileSystem.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000028#include "llvm/Support/ManagedStatic.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000029#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000030#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000031#include "llvm/Support/Regex.h"
Chris Lattner30bc7e82010-03-30 05:39:52 +000032#include "llvm/Support/Timer.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000033#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000034#include "llvm/Support/Host.h"
35#include "llvm/Support/Path.h"
36#include "llvm/Support/Program.h"
37#include "llvm/Support/Signals.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000038#include "llvm/Support/system_error.h"
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +000039#include "llvm/Target/TargetRegistry.h"
40#include "llvm/Target/TargetSelect.h"
Douglas Gregor43d013d2011-01-17 19:15:43 +000041#include <cctype>
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000042using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000043using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000044
Benjamin Krameraeed3da2010-10-30 17:32:40 +000045llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
Rafael Espindola0f4c59c2009-12-04 19:31:58 +000046 if (!CanonicalPrefixes)
47 return llvm::sys::Path(Argv0);
48
Daniel Dunbar734932c2009-03-18 20:25:53 +000049 // This just needs to be some symbol in the binary; C++ doesn't
50 // allow taking the address of ::main however.
51 void *P = (void*) (intptr_t) GetExecutablePath;
52 return llvm::sys::Path::GetMainExecutable(Argv0, P);
53}
54
Daniel Dunbar237a31b2009-07-17 18:10:27 +000055static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000056 llvm::StringRef S) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000057 return SavedStrings.insert(S).first->c_str();
58}
59
60/// ApplyQAOverride - Apply a list of edits to the input argument lists.
61///
62/// The input string is a space separate list of edits to perform,
63/// they are applied in order to the input argument lists. Edits
64/// should be one of the following forms:
65///
Daniel Dunbare3d60232009-07-16 21:32:51 +000066/// '#': Silence information about the changes to the command line arguments.
67///
Daniel Dunbarec9587d2009-04-17 01:54:00 +000068/// '^': Add FOO as a new argument at the beginning of the command line.
69///
70/// '+': Add FOO as a new argument at the end of the command line.
71///
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000072/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
73/// line.
Daniel Dunbarec9587d2009-04-17 01:54:00 +000074///
75/// 'xOPTION': Removes all instances of the literal argument OPTION.
76///
77/// 'XOPTION': Removes all instances of the literal argument OPTION,
78/// and the following argument.
79///
80/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
81/// at the end of the command line.
Daniel Dunbare3d60232009-07-16 21:32:51 +000082///
83/// \param OS - The stream to write edit information to.
84/// \param Args - The vector of command line arguments.
85/// \param Edit - The override command to perform.
86/// \param SavedStrings - Set to use for storing string representations.
Chris Lattner30bc7e82010-03-30 05:39:52 +000087static void ApplyOneQAOverride(llvm::raw_ostream &OS,
Daniel Dunbare5d69672010-07-20 02:47:40 +000088 llvm::SmallVectorImpl<const char*> &Args,
Chris Lattner30bc7e82010-03-30 05:39:52 +000089 llvm::StringRef Edit,
90 std::set<std::string> &SavedStrings) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000091 // This does not need to be efficient.
92
Daniel Dunbar237a31b2009-07-17 18:10:27 +000093 if (Edit[0] == '^') {
94 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000095 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +000096 OS << "### Adding argument " << Str << " at beginning\n";
97 Args.insert(Args.begin() + 1, Str);
98 } else if (Edit[0] == '+') {
99 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000100 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000101 OS << "### Adding argument " << Str << " at end\n";
102 Args.push_back(Str);
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000103 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
104 Edit.slice(2, Edit.size()-1).find('/') != llvm::StringRef::npos) {
105 llvm::StringRef MatchPattern = Edit.substr(2).split('/').first;
106 llvm::StringRef ReplPattern = Edit.substr(2).split('/').second;
107 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
108
109 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
110 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
111
112 if (Repl != Args[i]) {
113 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
114 Args[i] = SaveStringInSet(SavedStrings, Repl);
115 }
116 }
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000117 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
118 std::string Option = Edit.substr(1, std::string::npos);
119 for (unsigned i = 1; i < Args.size();) {
120 if (Option == Args[i]) {
121 OS << "### Deleting argument " << Args[i] << '\n';
122 Args.erase(Args.begin() + i);
123 if (Edit[0] == 'X') {
124 if (i < Args.size()) {
125 OS << "### Deleting argument " << Args[i] << '\n';
126 Args.erase(Args.begin() + i);
127 } else
128 OS << "### Invalid X edit, end of command line!\n";
129 }
130 } else
131 ++i;
132 }
133 } else if (Edit[0] == 'O') {
134 for (unsigned i = 1; i < Args.size();) {
135 const char *A = Args[i];
136 if (A[0] == '-' && A[1] == 'O' &&
137 (A[2] == '\0' ||
138 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
139 ('0' <= A[2] && A[2] <= '9'))))) {
140 OS << "### Deleting argument " << Args[i] << '\n';
141 Args.erase(Args.begin() + i);
142 } else
143 ++i;
144 }
145 OS << "### Adding argument " << Edit << " at end\n";
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000146 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str()));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000147 } else {
148 OS << "### Unrecognized edit: " << Edit << "\n";
149 }
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000150}
151
152/// ApplyQAOverride - Apply a comma separate list of edits to the
153/// input argument lists. See ApplyOneQAOverride.
Daniel Dunbare5d69672010-07-20 02:47:40 +0000154static void ApplyQAOverride(llvm::SmallVectorImpl<const char*> &Args,
Chris Lattner30bc7e82010-03-30 05:39:52 +0000155 const char *OverrideStr,
156 std::set<std::string> &SavedStrings) {
Daniel Dunbare3d60232009-07-16 21:32:51 +0000157 llvm::raw_ostream *OS = &llvm::errs();
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000158
Daniel Dunbare3d60232009-07-16 21:32:51 +0000159 if (OverrideStr[0] == '#') {
160 ++OverrideStr;
161 OS = &llvm::nulls();
162 }
163
164 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000165
166 // This does not need to be efficient.
167
168 const char *S = OverrideStr;
169 while (*S) {
170 const char *End = ::strchr(S, ' ');
171 if (!End)
172 End = S + strlen(S);
173 if (End != S)
Daniel Dunbare3d60232009-07-16 21:32:51 +0000174 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000175 S = End;
176 if (*S != '\0')
177 ++S;
178 }
179}
180
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000181extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
Daniel Dunbar545c2812009-11-29 20:58:32 +0000182 const char *Argv0, void *MainAddr);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000183extern int cc1as_main(const char **ArgBegin, const char **ArgEnd,
184 const char *Argv0, void *MainAddr);
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000185
Rafael Espindola8a1af322010-07-19 15:20:12 +0000186static void ExpandArgsFromBuf(const char *Arg,
Daniel Dunbare5d69672010-07-20 02:47:40 +0000187 llvm::SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola8a1af322010-07-19 15:20:12 +0000188 std::set<std::string> &SavedStrings) {
189 const char *FName = Arg + 1;
Michael J. Spencer4eeebc42010-12-16 03:28:14 +0000190 llvm::OwningPtr<llvm::MemoryBuffer> MemBuf;
191 if (llvm::MemoryBuffer::getFile(FName, MemBuf)) {
Rafael Espindola8a1af322010-07-19 15:20:12 +0000192 ArgVector.push_back(SaveStringInSet(SavedStrings, Arg));
193 return;
194 }
195
196 const char *Buf = MemBuf->getBufferStart();
197 char InQuote = ' ';
198 std::string CurArg;
199
200 for (const char *P = Buf; ; ++P) {
201 if (*P == '\0' || (isspace(*P) && InQuote == ' ')) {
202 if (!CurArg.empty()) {
203
204 if (CurArg[0] != '@') {
205 ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg));
206 } else {
207 ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings);
208 }
209
210 CurArg = "";
211 }
212 if (*P == '\0')
213 break;
214 else
215 continue;
216 }
217
218 if (isspace(*P)) {
219 if (InQuote != ' ')
220 CurArg.push_back(*P);
221 continue;
222 }
223
224 if (*P == '"' || *P == '\'') {
225 if (InQuote == *P)
226 InQuote = ' ';
227 else if (InQuote == ' ')
228 InQuote = *P;
229 else
230 CurArg.push_back(*P);
231 continue;
232 }
233
234 if (*P == '\\') {
235 ++P;
236 if (*P != '\0')
237 CurArg.push_back(*P);
238 continue;
239 }
240 CurArg.push_back(*P);
241 }
Rafael Espindola8a1af322010-07-19 15:20:12 +0000242}
243
244static void ExpandArgv(int argc, const char **argv,
Daniel Dunbare5d69672010-07-20 02:47:40 +0000245 llvm::SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola8a1af322010-07-19 15:20:12 +0000246 std::set<std::string> &SavedStrings) {
247 for (int i = 0; i < argc; ++i) {
248 const char *Arg = argv[i];
249 if (Arg[0] != '@') {
250 ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg)));
251 continue;
252 }
253
254 ExpandArgsFromBuf(Arg, ArgVector, SavedStrings);
255 }
256}
257
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000258static void ParseProgName(llvm::SmallVectorImpl<const char *> &ArgVector,
259 std::set<std::string> &SavedStrings,
260 Driver &TheDriver)
261{
262 // Try to infer frontend type and default target from the program name.
263
264 // suffixes[] contains the list of known driver suffixes.
265 // Suffixes are compared against the program name in order.
266 // If there is a match, the frontend type is updated as necessary (CPP/C++).
267 // If there is no match, a second round is done after stripping the last
268 // hyphen and everything following it. This allows using something like
269 // "clang++-2.9".
270
271 // If there is a match in either the first or second round,
272 // the function tries to identify a target as prefix. E.g.
273 // "x86_64-linux-clang" as interpreted as suffix "clang" with
274 // target prefix "x86_64-linux". If such a target prefix is found,
275 // is gets added via -ccc-host-triple as implicit first argument.
276 static const struct {
277 const char *Suffix;
278 bool IsCXX;
279 bool IsCPP;
280 } suffixes [] = {
281 { "clang", false, false },
282 { "clang++", true, false },
283 { "clang-c++", true, false },
284 { "clang-cc", false, false },
285 { "clang-cpp", false, true },
286 { "clang-g++", true, false },
287 { "clang-gcc", false, false },
288 { "cc", false, false },
289 { "cpp", false, true },
290 { "++", true, false },
291 };
292 std::string ProgName(llvm::sys::path::stem(ArgVector[0]));
293 llvm::StringRef ProgNameRef(ProgName);
294 llvm::StringRef Prefix;
295
296 for (int Components = 2; Components; --Components) {
297 bool FoundMatch = false;
298 size_t i;
299
300 for (i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) {
301 if (ProgNameRef.endswith(suffixes[i].Suffix)) {
302 FoundMatch = true;
303 if (suffixes[i].IsCXX)
304 TheDriver.CCCIsCXX = true;
305 if (suffixes[i].IsCPP)
306 TheDriver.CCCIsCPP = true;
307 break;
308 }
309 }
310
311 if (FoundMatch) {
312 llvm::StringRef::size_type LastComponent = ProgNameRef.rfind('-',
313 ProgNameRef.size() - strlen(suffixes[i].Suffix));
314 if (LastComponent != llvm::StringRef::npos)
315 Prefix = ProgNameRef.slice(0, LastComponent);
316 break;
317 }
318
319 llvm::StringRef::size_type LastComponent = ProgNameRef.rfind('-');
320 if (LastComponent == llvm::StringRef::npos)
321 break;
322 ProgNameRef = ProgNameRef.slice(0, LastComponent);
323 }
324
325 if (Prefix.empty())
326 return;
327
328 std::string IgnoredError;
329 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
330 ArgVector.insert(&ArgVector[1],
331 SaveStringInSet(SavedStrings, Prefix));
332 ArgVector.insert(&ArgVector[1],
333 SaveStringInSet(SavedStrings, std::string("-ccc-host-triple")));
334 }
335}
336
Rafael Espindola8a1af322010-07-19 15:20:12 +0000337int main(int argc_, const char **argv_) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000338 llvm::sys::PrintStackTraceOnErrorSignal();
Rafael Espindola8a1af322010-07-19 15:20:12 +0000339 llvm::PrettyStackTraceProgram X(argc_, argv_);
340
341 std::set<std::string> SavedStrings;
Daniel Dunbare5d69672010-07-20 02:47:40 +0000342 llvm::SmallVector<const char*, 256> argv;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000343
344 ExpandArgv(argc_, argv_, argv, SavedStrings);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000345
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000346 // Handle -cc1 integrated tools.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000347 if (argv.size() > 1 && llvm::StringRef(argv[1]).startswith("-cc1")) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000348 llvm::StringRef Tool = argv[1] + 4;
349
350 if (Tool == "")
Daniel Dunbare5d69672010-07-20 02:47:40 +0000351 return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000352 (void*) (intptr_t) GetExecutablePath);
353 if (Tool == "as")
Daniel Dunbare5d69672010-07-20 02:47:40 +0000354 return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000355 (void*) (intptr_t) GetExecutablePath);
356
357 // Reject unknown tools.
358 llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
359 return 1;
360 }
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000361
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000362 bool CanonicalPrefixes = true;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000363 for (int i = 1, size = argv.size(); i < size; ++i) {
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000364 if (llvm::StringRef(argv[i]) == "-no-canonical-prefixes") {
365 CanonicalPrefixes = false;
366 break;
367 }
368 }
369
370 llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes);
371
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000372 TextDiagnosticPrinter *DiagClient
373 = new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
Michael J. Spencerd5b08be2010-12-18 04:13:32 +0000374 DiagClient->setPrefix(llvm::sys::path::stem(Path.str()));
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000375 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
376 Diagnostic Diags(DiagID, DiagClient);
Daniel Dunbar510d7322009-03-18 02:11:26 +0000377
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000378#ifdef CLANG_IS_PRODUCTION
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000379 const bool IsProduction = true;
Daniel Dunbar5d93ed32010-04-01 18:21:41 +0000380# ifdef CLANGXX_IS_PRODUCTION
381 const bool CXXIsProduction = true;
382# else
383 const bool CXXIsProduction = false;
384# endif
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000385#else
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000386 const bool IsProduction = false;
Daniel Dunbar5d93ed32010-04-01 18:21:41 +0000387 const bool CXXIsProduction = false;
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000388#endif
Daniel Dunbar0bbad512010-07-19 00:44:04 +0000389 Driver TheDriver(Path.str(), llvm::sys::getHostTriple(),
Daniel Dunbar5d93ed32010-04-01 18:21:41 +0000390 "a.out", IsProduction, CXXIsProduction,
391 Diags);
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000392
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000393 // Attempt to find the original path used to invoke the driver, to determine
394 // the installed path. We do this manually, because we want to support that
395 // path being a symlink.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000396 {
397 llvm::SmallString<128> InstalledPath(argv[0]);
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000398
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000399 // Do a PATH lookup, if there are no directory components.
400 if (llvm::sys::path::filename(InstalledPath) == InstalledPath) {
401 llvm::sys::Path Tmp = llvm::sys::Program::FindProgramByName(
402 llvm::sys::path::filename(InstalledPath.str()));
403 if (!Tmp.empty())
404 InstalledPath = Tmp.str();
405 }
406 llvm::sys::fs::make_absolute(InstalledPath);
407 InstalledPath = llvm::sys::path::parent_path(InstalledPath);
408 bool exists;
409 if (!llvm::sys::fs::exists(InstalledPath.str(), exists) && exists)
410 TheDriver.setInstalledDir(InstalledPath);
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000411 }
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000412
Joerg Sonnenberger0ce89c62011-03-16 20:15:43 +0000413 llvm::InitializeAllTargets();
414 ParseProgName(argv, SavedStrings, TheDriver);
Joerg Sonnenberger9ade4ae2011-03-06 23:31:01 +0000415
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000416 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
417 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
418 if (TheDriver.CCPrintOptions)
419 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
420
Daniel Dunbar322c29f2011-02-02 21:11:35 +0000421 // Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE.
422 TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS");
423 if (TheDriver.CCPrintHeaders)
424 TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE");
425
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000426 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
427 // command line behind the scenes.
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000428 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
429 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000430 ApplyQAOverride(argv, OverrideStr, SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000431 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000432 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000433 std::vector<const char*> ExtraArgs;
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000434
435 for (;;) {
436 const char *Next = strchr(Cur, ',');
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000437
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000438 if (Next) {
Rafael Espindola8a1af322010-07-19 15:20:12 +0000439 ExtraArgs.push_back(SaveStringInSet(SavedStrings,
440 std::string(Cur, Next)));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000441 Cur = Next + 1;
442 } else {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000443 if (*Cur != '\0')
Rafael Espindola8a1af322010-07-19 15:20:12 +0000444 ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000445 break;
446 }
447 }
448
Daniel Dunbare5d69672010-07-20 02:47:40 +0000449 argv.insert(&argv[1], ExtraArgs.begin(), ExtraArgs.end());
Rafael Espindola06e35d32010-07-18 22:03:55 +0000450 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000451
Chris Lattner7f9fc3f2011-03-23 04:04:01 +0000452 llvm::OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv));
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000453 int Res = 0;
454 if (C.get())
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000455 Res = TheDriver.ExecuteCompilation(*C);
Chris Lattner30bc7e82010-03-30 05:39:52 +0000456
457 // If any timers were active but haven't been destroyed yet, print their
458 // results now. This happens in -disable-free mode.
459 llvm::TimerGroup::printAll(llvm::errs());
460
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000461 llvm::llvm_shutdown();
462
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000463 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000464}