blob: c1172817ed910ce9eaa969c47381334d325bfb2f [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
Daniel Dunbar510d7322009-03-18 02:11:26 +000021#include "llvm/ADT/SmallString.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000022#include "llvm/ADT/SmallVector.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000023#include "llvm/ADT/OwningPtr.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000024#include "llvm/Config/config.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000025#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000026#include "llvm/Support/ManagedStatic.h"
Rafael Espindola8a1af322010-07-19 15:20:12 +000027#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000028#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000029#include "llvm/Support/Regex.h"
Chris Lattner30bc7e82010-03-30 05:39:52 +000030#include "llvm/Support/Timer.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000031#include "llvm/Support/raw_ostream.h"
Daniel Dunbaraf07f932009-03-31 17:35:15 +000032#include "llvm/System/Host.h"
Daniel Dunbar365c02f2009-03-10 20:52:46 +000033#include "llvm/System/Path.h"
Daniel Dunbaredf29b02010-08-01 22:29:51 +000034#include "llvm/System/Program.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000035#include "llvm/System/Signals.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000036using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000037using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000038
Benjamin Krameraeed3da2010-10-30 17:32:40 +000039llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
Rafael Espindola0f4c59c2009-12-04 19:31:58 +000040 if (!CanonicalPrefixes)
41 return llvm::sys::Path(Argv0);
42
Daniel Dunbar734932c2009-03-18 20:25:53 +000043 // This just needs to be some symbol in the binary; C++ doesn't
44 // allow taking the address of ::main however.
45 void *P = (void*) (intptr_t) GetExecutablePath;
46 return llvm::sys::Path::GetMainExecutable(Argv0, P);
47}
48
Daniel Dunbar237a31b2009-07-17 18:10:27 +000049static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000050 llvm::StringRef S) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000051 return SavedStrings.insert(S).first->c_str();
52}
53
54/// ApplyQAOverride - Apply a list of edits to the input argument lists.
55///
56/// The input string is a space separate list of edits to perform,
57/// they are applied in order to the input argument lists. Edits
58/// should be one of the following forms:
59///
Daniel Dunbare3d60232009-07-16 21:32:51 +000060/// '#': Silence information about the changes to the command line arguments.
61///
Daniel Dunbarec9587d2009-04-17 01:54:00 +000062/// '^': Add FOO as a new argument at the beginning of the command line.
63///
64/// '+': Add FOO as a new argument at the end of the command line.
65///
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000066/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
67/// line.
Daniel Dunbarec9587d2009-04-17 01:54:00 +000068///
69/// 'xOPTION': Removes all instances of the literal argument OPTION.
70///
71/// 'XOPTION': Removes all instances of the literal argument OPTION,
72/// and the following argument.
73///
74/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
75/// at the end of the command line.
Daniel Dunbare3d60232009-07-16 21:32:51 +000076///
77/// \param OS - The stream to write edit information to.
78/// \param Args - The vector of command line arguments.
79/// \param Edit - The override command to perform.
80/// \param SavedStrings - Set to use for storing string representations.
Chris Lattner30bc7e82010-03-30 05:39:52 +000081static void ApplyOneQAOverride(llvm::raw_ostream &OS,
Daniel Dunbare5d69672010-07-20 02:47:40 +000082 llvm::SmallVectorImpl<const char*> &Args,
Chris Lattner30bc7e82010-03-30 05:39:52 +000083 llvm::StringRef Edit,
84 std::set<std::string> &SavedStrings) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000085 // This does not need to be efficient.
86
Daniel Dunbar237a31b2009-07-17 18:10:27 +000087 if (Edit[0] == '^') {
88 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000089 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +000090 OS << "### Adding argument " << Str << " at beginning\n";
91 Args.insert(Args.begin() + 1, Str);
92 } else 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 end\n";
96 Args.push_back(Str);
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000097 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
98 Edit.slice(2, Edit.size()-1).find('/') != llvm::StringRef::npos) {
99 llvm::StringRef MatchPattern = Edit.substr(2).split('/').first;
100 llvm::StringRef ReplPattern = Edit.substr(2).split('/').second;
101 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
102
103 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
104 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
105
106 if (Repl != Args[i]) {
107 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
108 Args[i] = SaveStringInSet(SavedStrings, Repl);
109 }
110 }
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000111 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
112 std::string Option = Edit.substr(1, std::string::npos);
113 for (unsigned i = 1; i < Args.size();) {
114 if (Option == Args[i]) {
115 OS << "### Deleting argument " << Args[i] << '\n';
116 Args.erase(Args.begin() + i);
117 if (Edit[0] == 'X') {
118 if (i < Args.size()) {
119 OS << "### Deleting argument " << Args[i] << '\n';
120 Args.erase(Args.begin() + i);
121 } else
122 OS << "### Invalid X edit, end of command line!\n";
123 }
124 } else
125 ++i;
126 }
127 } else if (Edit[0] == 'O') {
128 for (unsigned i = 1; i < Args.size();) {
129 const char *A = Args[i];
130 if (A[0] == '-' && A[1] == 'O' &&
131 (A[2] == '\0' ||
132 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
133 ('0' <= A[2] && A[2] <= '9'))))) {
134 OS << "### Deleting argument " << Args[i] << '\n';
135 Args.erase(Args.begin() + i);
136 } else
137 ++i;
138 }
139 OS << "### Adding argument " << Edit << " at end\n";
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000140 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str()));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000141 } else {
142 OS << "### Unrecognized edit: " << Edit << "\n";
143 }
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000144}
145
146/// ApplyQAOverride - Apply a comma separate list of edits to the
147/// input argument lists. See ApplyOneQAOverride.
Daniel Dunbare5d69672010-07-20 02:47:40 +0000148static void ApplyQAOverride(llvm::SmallVectorImpl<const char*> &Args,
Chris Lattner30bc7e82010-03-30 05:39:52 +0000149 const char *OverrideStr,
150 std::set<std::string> &SavedStrings) {
Daniel Dunbare3d60232009-07-16 21:32:51 +0000151 llvm::raw_ostream *OS = &llvm::errs();
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000152
Daniel Dunbare3d60232009-07-16 21:32:51 +0000153 if (OverrideStr[0] == '#') {
154 ++OverrideStr;
155 OS = &llvm::nulls();
156 }
157
158 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000159
160 // This does not need to be efficient.
161
162 const char *S = OverrideStr;
163 while (*S) {
164 const char *End = ::strchr(S, ' ');
165 if (!End)
166 End = S + strlen(S);
167 if (End != S)
Daniel Dunbare3d60232009-07-16 21:32:51 +0000168 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000169 S = End;
170 if (*S != '\0')
171 ++S;
172 }
173}
174
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000175extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
Daniel Dunbar545c2812009-11-29 20:58:32 +0000176 const char *Argv0, void *MainAddr);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000177extern int cc1as_main(const char **ArgBegin, const char **ArgEnd,
178 const char *Argv0, void *MainAddr);
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000179
Rafael Espindola8a1af322010-07-19 15:20:12 +0000180static void ExpandArgsFromBuf(const char *Arg,
Daniel Dunbare5d69672010-07-20 02:47:40 +0000181 llvm::SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola8a1af322010-07-19 15:20:12 +0000182 std::set<std::string> &SavedStrings) {
183 const char *FName = Arg + 1;
184 llvm::MemoryBuffer *MemBuf = llvm::MemoryBuffer::getFile(FName);
185 if (!MemBuf) {
186 ArgVector.push_back(SaveStringInSet(SavedStrings, Arg));
187 return;
188 }
189
190 const char *Buf = MemBuf->getBufferStart();
191 char InQuote = ' ';
192 std::string CurArg;
193
194 for (const char *P = Buf; ; ++P) {
195 if (*P == '\0' || (isspace(*P) && InQuote == ' ')) {
196 if (!CurArg.empty()) {
197
198 if (CurArg[0] != '@') {
199 ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg));
200 } else {
201 ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings);
202 }
203
204 CurArg = "";
205 }
206 if (*P == '\0')
207 break;
208 else
209 continue;
210 }
211
212 if (isspace(*P)) {
213 if (InQuote != ' ')
214 CurArg.push_back(*P);
215 continue;
216 }
217
218 if (*P == '"' || *P == '\'') {
219 if (InQuote == *P)
220 InQuote = ' ';
221 else if (InQuote == ' ')
222 InQuote = *P;
223 else
224 CurArg.push_back(*P);
225 continue;
226 }
227
228 if (*P == '\\') {
229 ++P;
230 if (*P != '\0')
231 CurArg.push_back(*P);
232 continue;
233 }
234 CurArg.push_back(*P);
235 }
236 delete MemBuf;
237}
238
239static void ExpandArgv(int argc, const char **argv,
Daniel Dunbare5d69672010-07-20 02:47:40 +0000240 llvm::SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola8a1af322010-07-19 15:20:12 +0000241 std::set<std::string> &SavedStrings) {
242 for (int i = 0; i < argc; ++i) {
243 const char *Arg = argv[i];
244 if (Arg[0] != '@') {
245 ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg)));
246 continue;
247 }
248
249 ExpandArgsFromBuf(Arg, ArgVector, SavedStrings);
250 }
251}
252
253int main(int argc_, const char **argv_) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000254 llvm::sys::PrintStackTraceOnErrorSignal();
Rafael Espindola8a1af322010-07-19 15:20:12 +0000255 llvm::PrettyStackTraceProgram X(argc_, argv_);
256
257 std::set<std::string> SavedStrings;
Daniel Dunbare5d69672010-07-20 02:47:40 +0000258 llvm::SmallVector<const char*, 256> argv;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000259
260 ExpandArgv(argc_, argv_, argv, SavedStrings);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000261
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000262 // Handle -cc1 integrated tools.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000263 if (argv.size() > 1 && llvm::StringRef(argv[1]).startswith("-cc1")) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000264 llvm::StringRef Tool = argv[1] + 4;
265
266 if (Tool == "")
Daniel Dunbare5d69672010-07-20 02:47:40 +0000267 return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000268 (void*) (intptr_t) GetExecutablePath);
269 if (Tool == "as")
Daniel Dunbare5d69672010-07-20 02:47:40 +0000270 return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000271 (void*) (intptr_t) GetExecutablePath);
272
273 // Reject unknown tools.
274 llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
275 return 1;
276 }
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000277
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000278 bool CanonicalPrefixes = true;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000279 for (int i = 1, size = argv.size(); i < size; ++i) {
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000280 if (llvm::StringRef(argv[i]) == "-no-canonical-prefixes") {
281 CanonicalPrefixes = false;
282 break;
283 }
284 }
285
286 llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes);
287
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000288 TextDiagnosticPrinter *DiagClient
289 = new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
290 DiagClient->setPrefix(Path.getBasename());
291 Diagnostic Diags(DiagClient);
Daniel Dunbar510d7322009-03-18 02:11:26 +0000292
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000293#ifdef CLANG_IS_PRODUCTION
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000294 const bool IsProduction = true;
Daniel Dunbar5d93ed32010-04-01 18:21:41 +0000295# ifdef CLANGXX_IS_PRODUCTION
296 const bool CXXIsProduction = true;
297# else
298 const bool CXXIsProduction = false;
299# endif
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000300#else
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000301 const bool IsProduction = false;
Daniel Dunbar5d93ed32010-04-01 18:21:41 +0000302 const bool CXXIsProduction = false;
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000303#endif
Daniel Dunbar0bbad512010-07-19 00:44:04 +0000304 Driver TheDriver(Path.str(), llvm::sys::getHostTriple(),
Daniel Dunbar5d93ed32010-04-01 18:21:41 +0000305 "a.out", IsProduction, CXXIsProduction,
306 Diags);
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000307
Daniel Dunbaredf29b02010-08-01 22:29:51 +0000308 // Attempt to find the original path used to invoke the driver, to determine
309 // the installed path. We do this manually, because we want to support that
310 // path being a symlink.
311 llvm::sys::Path InstalledPath(argv[0]);
312
313 // Do a PATH lookup, if there are no directory components.
314 if (InstalledPath.getLast() == InstalledPath.str()) {
315 llvm::sys::Path Tmp =
316 llvm::sys::Program::FindProgramByName(InstalledPath.getLast());
317 if (!Tmp.empty())
318 InstalledPath = Tmp;
319 }
320 InstalledPath.makeAbsolute();
321 InstalledPath.eraseComponent();
322 if (InstalledPath.exists())
323 TheDriver.setInstalledDir(InstalledPath.str());
324
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000325 // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++
326 // compiler. This matches things like "c++", "clang++", and "clang++-1.1".
327 //
328 // Note that we intentionally want to use argv[0] here, to support "clang++"
329 // being a symlink.
Benjamin Kramerd5dc9f32009-11-20 11:49:06 +0000330 //
331 // We use *argv instead of argv[0] to work around a bogus g++ warning.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000332 const char *progname = argv_[0];
333 std::string ProgName(llvm::sys::Path(progname).getBasename());
Daniel Dunbare26bd902009-11-11 10:10:25 +0000334 if (llvm::StringRef(ProgName).endswith("++") ||
Daniel Dunbar0dea4be2009-12-25 20:21:23 +0000335 llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) {
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000336 TheDriver.CCCIsCXX = true;
Daniel Dunbar0dea4be2009-12-25 20:21:23 +0000337 }
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000338
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000339 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
340 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
341 if (TheDriver.CCPrintOptions)
342 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
343
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000344 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
345 // command line behind the scenes.
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000346 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
347 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000348 ApplyQAOverride(argv, OverrideStr, SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000349 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000350 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000351 std::vector<const char*> ExtraArgs;
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000352
353 for (;;) {
354 const char *Next = strchr(Cur, ',');
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000355
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000356 if (Next) {
Rafael Espindola8a1af322010-07-19 15:20:12 +0000357 ExtraArgs.push_back(SaveStringInSet(SavedStrings,
358 std::string(Cur, Next)));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000359 Cur = Next + 1;
360 } else {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000361 if (*Cur != '\0')
Rafael Espindola8a1af322010-07-19 15:20:12 +0000362 ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000363 break;
364 }
365 }
366
Daniel Dunbare5d69672010-07-20 02:47:40 +0000367 argv.insert(&argv[1], ExtraArgs.begin(), ExtraArgs.end());
Rafael Espindola06e35d32010-07-18 22:03:55 +0000368 }
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000369
Daniel Dunbar5633c1e2010-08-01 22:29:47 +0000370 llvm::OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv.size(),
371 &argv[0]));
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000372 int Res = 0;
373 if (C.get())
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000374 Res = TheDriver.ExecuteCompilation(*C);
Chris Lattner30bc7e82010-03-30 05:39:52 +0000375
376 // If any timers were active but haven't been destroyed yet, print their
377 // results now. This happens in -disable-free mode.
378 llvm::TimerGroup::printAll(llvm::errs());
379
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000380 llvm::llvm_shutdown();
381
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000382 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000383}