blob: 34d3327c5ff85059d05538a918311edeae44ecba [file] [log] [blame]
Daniel Dunbar544ecd12009-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 Dunbarb2da9332009-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 Dunbar544ecd12009-03-02 19:59:07 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Driver/Compilation.h"
16#include "clang/Driver/Driver.h"
Daniel Dunbar56372082009-03-04 08:33:23 +000017#include "clang/Driver/Option.h"
Daniel Dunbar12ee3802010-02-25 03:23:43 +000018#include "clang/Frontend/DiagnosticOptions.h"
19#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Dunbar56372082009-03-04 08:33:23 +000020
Daniel Dunbard9b80c22009-03-18 02:11:26 +000021#include "llvm/ADT/SmallString.h"
Rafael Espindola77a067a2010-07-19 15:20:12 +000022#include "llvm/ADT/SmallVector.h"
Daniel Dunbar544ecd12009-03-02 19:59:07 +000023#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar4dff6a42009-03-10 23:41:59 +000024#include "llvm/Config/config.h"
Rafael Espindola77a067a2010-07-19 15:20:12 +000025#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar2608c542009-03-18 01:38:48 +000026#include "llvm/Support/ManagedStatic.h"
Rafael Espindola77a067a2010-07-19 15:20:12 +000027#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2608c542009-03-18 01:38:48 +000028#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbard2b20c12010-02-17 21:00:34 +000029#include "llvm/Support/Regex.h"
Chris Lattner09f8cc82010-03-30 05:39:52 +000030#include "llvm/Support/Timer.h"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000031#include "llvm/Support/raw_ostream.h"
Daniel Dunbard640be22009-03-31 17:35:15 +000032#include "llvm/System/Host.h"
Daniel Dunbaree66cf22009-03-10 20:52:46 +000033#include "llvm/System/Path.h"
Daniel Dunbar544ecd12009-03-02 19:59:07 +000034#include "llvm/System/Signals.h"
Daniel Dunbarc0b3e952009-03-12 08:55:43 +000035using namespace clang;
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000036using namespace clang::driver;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000037
Rafael Espindola73d46372009-12-04 19:31:58 +000038llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
39 if (!CanonicalPrefixes)
40 return llvm::sys::Path(Argv0);
41
Daniel Dunbarda382a82009-03-18 20:25:53 +000042 // This just needs to be some symbol in the binary; C++ doesn't
43 // allow taking the address of ::main however.
44 void *P = (void*) (intptr_t) GetExecutablePath;
45 return llvm::sys::Path::GetMainExecutable(Argv0, P);
46}
47
Daniel Dunbarc3ccd182009-07-17 18:10:27 +000048static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Daniel Dunbard2b20c12010-02-17 21:00:34 +000049 llvm::StringRef S) {
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +000050 return SavedStrings.insert(S).first->c_str();
51}
52
53/// ApplyQAOverride - Apply a list of edits to the input argument lists.
54///
55/// The input string is a space separate list of edits to perform,
56/// they are applied in order to the input argument lists. Edits
57/// should be one of the following forms:
58///
Daniel Dunbar54091b82009-07-16 21:32:51 +000059/// '#': Silence information about the changes to the command line arguments.
60///
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +000061/// '^': Add FOO as a new argument at the beginning of the command line.
62///
63/// '+': Add FOO as a new argument at the end of the command line.
64///
Daniel Dunbard2b20c12010-02-17 21:00:34 +000065/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
66/// line.
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +000067///
68/// 'xOPTION': Removes all instances of the literal argument OPTION.
69///
70/// 'XOPTION': Removes all instances of the literal argument OPTION,
71/// and the following argument.
72///
73/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
74/// at the end of the command line.
Daniel Dunbar54091b82009-07-16 21:32:51 +000075///
76/// \param OS - The stream to write edit information to.
77/// \param Args - The vector of command line arguments.
78/// \param Edit - The override command to perform.
79/// \param SavedStrings - Set to use for storing string representations.
Chris Lattner09f8cc82010-03-30 05:39:52 +000080static void ApplyOneQAOverride(llvm::raw_ostream &OS,
Daniel Dunbar9d8416b2010-07-20 02:47:40 +000081 llvm::SmallVectorImpl<const char*> &Args,
Chris Lattner09f8cc82010-03-30 05:39:52 +000082 llvm::StringRef Edit,
83 std::set<std::string> &SavedStrings) {
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +000084 // This does not need to be efficient.
85
Daniel Dunbarc3ccd182009-07-17 18:10:27 +000086 if (Edit[0] == '^') {
87 const char *Str =
Daniel Dunbard2b20c12010-02-17 21:00:34 +000088 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbarc3ccd182009-07-17 18:10:27 +000089 OS << "### Adding argument " << Str << " at beginning\n";
90 Args.insert(Args.begin() + 1, Str);
91 } else if (Edit[0] == '+') {
92 const char *Str =
Daniel Dunbard2b20c12010-02-17 21:00:34 +000093 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbarc3ccd182009-07-17 18:10:27 +000094 OS << "### Adding argument " << Str << " at end\n";
95 Args.push_back(Str);
Daniel Dunbard2b20c12010-02-17 21:00:34 +000096 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
97 Edit.slice(2, Edit.size()-1).find('/') != llvm::StringRef::npos) {
98 llvm::StringRef MatchPattern = Edit.substr(2).split('/').first;
99 llvm::StringRef ReplPattern = Edit.substr(2).split('/').second;
100 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
101
102 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
103 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
104
105 if (Repl != Args[i]) {
106 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
107 Args[i] = SaveStringInSet(SavedStrings, Repl);
108 }
109 }
Daniel Dunbarc3ccd182009-07-17 18:10:27 +0000110 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
111 std::string Option = Edit.substr(1, std::string::npos);
112 for (unsigned i = 1; i < Args.size();) {
113 if (Option == Args[i]) {
114 OS << "### Deleting argument " << Args[i] << '\n';
115 Args.erase(Args.begin() + i);
116 if (Edit[0] == 'X') {
117 if (i < Args.size()) {
118 OS << "### Deleting argument " << Args[i] << '\n';
119 Args.erase(Args.begin() + i);
120 } else
121 OS << "### Invalid X edit, end of command line!\n";
122 }
123 } else
124 ++i;
125 }
126 } else if (Edit[0] == 'O') {
127 for (unsigned i = 1; i < Args.size();) {
128 const char *A = Args[i];
129 if (A[0] == '-' && A[1] == 'O' &&
130 (A[2] == '\0' ||
131 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
132 ('0' <= A[2] && A[2] <= '9'))))) {
133 OS << "### Deleting argument " << Args[i] << '\n';
134 Args.erase(Args.begin() + i);
135 } else
136 ++i;
137 }
138 OS << "### Adding argument " << Edit << " at end\n";
Daniel Dunbard2b20c12010-02-17 21:00:34 +0000139 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str()));
Daniel Dunbarc3ccd182009-07-17 18:10:27 +0000140 } else {
141 OS << "### Unrecognized edit: " << Edit << "\n";
142 }
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +0000143}
144
145/// ApplyQAOverride - Apply a comma separate list of edits to the
146/// input argument lists. See ApplyOneQAOverride.
Daniel Dunbar9d8416b2010-07-20 02:47:40 +0000147static void ApplyQAOverride(llvm::SmallVectorImpl<const char*> &Args,
Chris Lattner09f8cc82010-03-30 05:39:52 +0000148 const char *OverrideStr,
149 std::set<std::string> &SavedStrings) {
Daniel Dunbar54091b82009-07-16 21:32:51 +0000150 llvm::raw_ostream *OS = &llvm::errs();
Daniel Dunbarc3ccd182009-07-17 18:10:27 +0000151
Daniel Dunbar54091b82009-07-16 21:32:51 +0000152 if (OverrideStr[0] == '#') {
153 ++OverrideStr;
154 OS = &llvm::nulls();
155 }
156
157 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +0000158
159 // This does not need to be efficient.
160
161 const char *S = OverrideStr;
162 while (*S) {
163 const char *End = ::strchr(S, ' ');
164 if (!End)
165 End = S + strlen(S);
166 if (End != S)
Daniel Dunbar54091b82009-07-16 21:32:51 +0000167 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +0000168 S = End;
169 if (*S != '\0')
170 ++S;
171 }
172}
173
Daniel Dunbar1c39f3c2009-11-30 07:18:13 +0000174extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
Daniel Dunbar65ca1c62009-11-29 20:58:32 +0000175 const char *Argv0, void *MainAddr);
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000176extern int cc1as_main(const char **ArgBegin, const char **ArgEnd,
177 const char *Argv0, void *MainAddr);
Daniel Dunbar51cd8f02009-11-19 07:37:51 +0000178
Rafael Espindola77a067a2010-07-19 15:20:12 +0000179static void ExpandArgsFromBuf(const char *Arg,
Daniel Dunbar9d8416b2010-07-20 02:47:40 +0000180 llvm::SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola77a067a2010-07-19 15:20:12 +0000181 std::set<std::string> &SavedStrings) {
182 const char *FName = Arg + 1;
183 llvm::MemoryBuffer *MemBuf = llvm::MemoryBuffer::getFile(FName);
184 if (!MemBuf) {
185 ArgVector.push_back(SaveStringInSet(SavedStrings, Arg));
186 return;
187 }
188
189 const char *Buf = MemBuf->getBufferStart();
190 char InQuote = ' ';
191 std::string CurArg;
192
193 for (const char *P = Buf; ; ++P) {
194 if (*P == '\0' || (isspace(*P) && InQuote == ' ')) {
195 if (!CurArg.empty()) {
196
197 if (CurArg[0] != '@') {
198 ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg));
199 } else {
200 ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings);
201 }
202
203 CurArg = "";
204 }
205 if (*P == '\0')
206 break;
207 else
208 continue;
209 }
210
211 if (isspace(*P)) {
212 if (InQuote != ' ')
213 CurArg.push_back(*P);
214 continue;
215 }
216
217 if (*P == '"' || *P == '\'') {
218 if (InQuote == *P)
219 InQuote = ' ';
220 else if (InQuote == ' ')
221 InQuote = *P;
222 else
223 CurArg.push_back(*P);
224 continue;
225 }
226
227 if (*P == '\\') {
228 ++P;
229 if (*P != '\0')
230 CurArg.push_back(*P);
231 continue;
232 }
233 CurArg.push_back(*P);
234 }
235 delete MemBuf;
236}
237
238static void ExpandArgv(int argc, const char **argv,
Daniel Dunbar9d8416b2010-07-20 02:47:40 +0000239 llvm::SmallVectorImpl<const char*> &ArgVector,
Rafael Espindola77a067a2010-07-19 15:20:12 +0000240 std::set<std::string> &SavedStrings) {
241 for (int i = 0; i < argc; ++i) {
242 const char *Arg = argv[i];
243 if (Arg[0] != '@') {
244 ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg)));
245 continue;
246 }
247
248 ExpandArgsFromBuf(Arg, ArgVector, SavedStrings);
249 }
250}
251
252int main(int argc_, const char **argv_) {
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000253 llvm::sys::PrintStackTraceOnErrorSignal();
Rafael Espindola77a067a2010-07-19 15:20:12 +0000254 llvm::PrettyStackTraceProgram X(argc_, argv_);
255
256 std::set<std::string> SavedStrings;
Daniel Dunbar9d8416b2010-07-20 02:47:40 +0000257 llvm::SmallVector<const char*, 256> argv;
Rafael Espindola77a067a2010-07-19 15:20:12 +0000258
259 ExpandArgv(argc_, argv_, argv, SavedStrings);
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000260
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000261 // Handle -cc1 integrated tools.
Rafael Espindola77a067a2010-07-19 15:20:12 +0000262 if (argv.size() > 1 && llvm::StringRef(argv[1]).startswith("-cc1")) {
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000263 llvm::StringRef Tool = argv[1] + 4;
264
265 if (Tool == "")
Daniel Dunbar9d8416b2010-07-20 02:47:40 +0000266 return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000267 (void*) (intptr_t) GetExecutablePath);
268 if (Tool == "as")
Daniel Dunbar9d8416b2010-07-20 02:47:40 +0000269 return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0],
Daniel Dunbar2fcaa542010-05-20 17:49:16 +0000270 (void*) (intptr_t) GetExecutablePath);
271
272 // Reject unknown tools.
273 llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
274 return 1;
275 }
Daniel Dunbar1c39f3c2009-11-30 07:18:13 +0000276
Rafael Espindola73d46372009-12-04 19:31:58 +0000277 bool CanonicalPrefixes = true;
Rafael Espindola77a067a2010-07-19 15:20:12 +0000278 for (int i = 1, size = argv.size(); i < size; ++i) {
Rafael Espindola73d46372009-12-04 19:31:58 +0000279 if (llvm::StringRef(argv[i]) == "-no-canonical-prefixes") {
280 CanonicalPrefixes = false;
281 break;
282 }
283 }
284
285 llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes);
286
Daniel Dunbar12ee3802010-02-25 03:23:43 +0000287 TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions());
288 DiagClient.setPrefix(Path.getBasename());
Daniel Dunbard9b80c22009-03-18 02:11:26 +0000289
Daniel Dunbarf7323f32009-05-26 16:15:44 +0000290 Diagnostic Diags(&DiagClient);
Daniel Dunbard9b80c22009-03-18 02:11:26 +0000291
Daniel Dunbar5564ba72009-09-22 22:31:13 +0000292#ifdef CLANG_IS_PRODUCTION
Kovarththanan Rajaratnam4bc61182010-03-08 18:33:04 +0000293 const bool IsProduction = true;
Daniel Dunbare43887b2010-04-01 18:21:41 +0000294# ifdef CLANGXX_IS_PRODUCTION
295 const bool CXXIsProduction = true;
296# else
297 const bool CXXIsProduction = false;
298# endif
Daniel Dunbar5564ba72009-09-22 22:31:13 +0000299#else
Kovarththanan Rajaratnam4bc61182010-03-08 18:33:04 +0000300 const bool IsProduction = false;
Daniel Dunbare43887b2010-04-01 18:21:41 +0000301 const bool CXXIsProduction = false;
Daniel Dunbar5564ba72009-09-22 22:31:13 +0000302#endif
Daniel Dunbare38764c2010-07-19 00:44:04 +0000303 Driver TheDriver(Path.str(), llvm::sys::getHostTriple(),
Daniel Dunbare43887b2010-04-01 18:21:41 +0000304 "a.out", IsProduction, CXXIsProduction,
305 Diags);
Daniel Dunbardae1d342009-04-01 19:08:46 +0000306
Daniel Dunbarf76c9d22009-11-10 18:47:41 +0000307 // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++
308 // compiler. This matches things like "c++", "clang++", and "clang++-1.1".
309 //
310 // Note that we intentionally want to use argv[0] here, to support "clang++"
311 // being a symlink.
Benjamin Kramerdf5280d2009-11-20 11:49:06 +0000312 //
313 // We use *argv instead of argv[0] to work around a bogus g++ warning.
Rafael Espindola77a067a2010-07-19 15:20:12 +0000314 const char *progname = argv_[0];
315 std::string ProgName(llvm::sys::Path(progname).getBasename());
Daniel Dunbar559f7a52009-11-11 10:10:25 +0000316 if (llvm::StringRef(ProgName).endswith("++") ||
Daniel Dunbarb2138e52009-12-25 20:21:23 +0000317 llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) {
Daniel Dunbarf76c9d22009-11-10 18:47:41 +0000318 TheDriver.CCCIsCXX = true;
Daniel Dunbarb2138e52009-12-25 20:21:23 +0000319 TheDriver.CCCGenericGCCName = "g++";
320 }
Daniel Dunbarf76c9d22009-11-10 18:47:41 +0000321
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000322 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
323 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
324 if (TheDriver.CCPrintOptions)
325 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
326
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +0000327 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
328 // command line behind the scenes.
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +0000329 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
330 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola77a067a2010-07-19 15:20:12 +0000331 ApplyQAOverride(argv, OverrideStr, SavedStrings);
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +0000332 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbardae1d342009-04-01 19:08:46 +0000333 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola77a067a2010-07-19 15:20:12 +0000334 std::vector<const char*> ExtraArgs;
Daniel Dunbardae1d342009-04-01 19:08:46 +0000335
336 for (;;) {
337 const char *Next = strchr(Cur, ',');
Daniel Dunbarc3ccd182009-07-17 18:10:27 +0000338
Daniel Dunbardae1d342009-04-01 19:08:46 +0000339 if (Next) {
Rafael Espindola77a067a2010-07-19 15:20:12 +0000340 ExtraArgs.push_back(SaveStringInSet(SavedStrings,
341 std::string(Cur, Next)));
Daniel Dunbardae1d342009-04-01 19:08:46 +0000342 Cur = Next + 1;
343 } else {
Daniel Dunbar3f4a2c22009-04-17 01:54:00 +0000344 if (*Cur != '\0')
Rafael Espindola77a067a2010-07-19 15:20:12 +0000345 ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbardae1d342009-04-01 19:08:46 +0000346 break;
347 }
348 }
349
Daniel Dunbar9d8416b2010-07-20 02:47:40 +0000350 argv.insert(&argv[1], ExtraArgs.begin(), ExtraArgs.end());
Rafael Espindola76614362010-07-18 22:03:55 +0000351 }
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000352
Daniel Dunbar5863fa52010-08-01 22:29:47 +0000353 llvm::OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv.size(),
354 &argv[0]));
Daniel Dunbar1acb0eb2009-03-21 00:40:53 +0000355 int Res = 0;
356 if (C.get())
Daniel Dunbar38bfda62009-07-01 20:03:04 +0000357 Res = TheDriver.ExecuteCompilation(*C);
Chris Lattner09f8cc82010-03-30 05:39:52 +0000358
359 // If any timers were active but haven't been destroyed yet, print their
360 // results now. This happens in -disable-free mode.
361 llvm::TimerGroup::printAll(llvm::errs());
362
Daniel Dunbar2608c542009-03-18 01:38:48 +0000363 llvm::llvm_shutdown();
364
Daniel Dunbar1acb0eb2009-03-21 00:40:53 +0000365 return Res;
Daniel Dunbar544ecd12009-03-02 19:59:07 +0000366}