blob: 6dc8d6fcebda058194417d4a90a6150dd313ea69 [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 Dunbar3ede8d02009-03-02 19:59:07 +000034#include "llvm/System/Signals.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000035using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000036using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000037
Rafael Espindola0f4c59c2009-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 Dunbar734932c2009-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 Dunbar237a31b2009-07-17 18:10:27 +000048static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000049 llvm::StringRef S) {
Daniel Dunbarec9587d2009-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 Dunbare3d60232009-07-16 21:32:51 +000059/// '#': Silence information about the changes to the command line arguments.
60///
Daniel Dunbarec9587d2009-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 Dunbar0de9a7b2010-02-17 21:00:34 +000065/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
66/// line.
Daniel Dunbarec9587d2009-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 Dunbare3d60232009-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 Lattner30bc7e82010-03-30 05:39:52 +000080static void ApplyOneQAOverride(llvm::raw_ostream &OS,
81 std::vector<const char*> &Args,
82 llvm::StringRef Edit,
83 std::set<std::string> &SavedStrings) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000084 // This does not need to be efficient.
85
Daniel Dunbar237a31b2009-07-17 18:10:27 +000086 if (Edit[0] == '^') {
87 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000088 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-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 Dunbar0de9a7b2010-02-17 21:00:34 +000093 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +000094 OS << "### Adding argument " << Str << " at end\n";
95 Args.push_back(Str);
Daniel Dunbar0de9a7b2010-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 Dunbar237a31b2009-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 Dunbar0de9a7b2010-02-17 21:00:34 +0000139 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str()));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000140 } else {
141 OS << "### Unrecognized edit: " << Edit << "\n";
142 }
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000143}
144
145/// ApplyQAOverride - Apply a comma separate list of edits to the
146/// input argument lists. See ApplyOneQAOverride.
Chris Lattner30bc7e82010-03-30 05:39:52 +0000147static void ApplyQAOverride(std::vector<const char*> &Args,
148 const char *OverrideStr,
149 std::set<std::string> &SavedStrings) {
Daniel Dunbare3d60232009-07-16 21:32:51 +0000150 llvm::raw_ostream *OS = &llvm::errs();
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000151
Daniel Dunbare3d60232009-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 Dunbarec9587d2009-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 Dunbare3d60232009-07-16 21:32:51 +0000167 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000168 S = End;
169 if (*S != '\0')
170 ++S;
171 }
172}
173
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000174extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
Daniel Dunbar545c2812009-11-29 20:58:32 +0000175 const char *Argv0, void *MainAddr);
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000176extern int cc1as_main(const char **ArgBegin, const char **ArgEnd,
177 const char *Argv0, void *MainAddr);
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000178
Rafael Espindola8a1af322010-07-19 15:20:12 +0000179static void ExpandArgsFromBuf(const char *Arg,
180 std::vector<const char*> &ArgVector,
181 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,
239 std::vector<const char*> &ArgVector,
240 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 Dunbar3ede8d02009-03-02 19:59:07 +0000253 llvm::sys::PrintStackTraceOnErrorSignal();
Rafael Espindola8a1af322010-07-19 15:20:12 +0000254 llvm::PrettyStackTraceProgram X(argc_, argv_);
255
256 std::set<std::string> SavedStrings;
257 std::vector<const char*> argv;
258
259 ExpandArgv(argc_, argv_, argv, SavedStrings);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000260
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000261 // Handle -cc1 integrated tools.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000262 if (argv.size() > 1 && llvm::StringRef(argv[1]).startswith("-cc1")) {
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000263 llvm::StringRef Tool = argv[1] + 4;
264
265 if (Tool == "")
Rafael Espindola8a1af322010-07-19 15:20:12 +0000266 return cc1_main(&argv[2], &argv[argv.size()], argv[0],
Daniel Dunbar41b5b172010-05-20 17:49:16 +0000267 (void*) (intptr_t) GetExecutablePath);
268 if (Tool == "as")
Rafael Espindola8a1af322010-07-19 15:20:12 +0000269 return cc1as_main(&argv[2], &argv[argv.size()], argv[0],
Daniel Dunbar41b5b172010-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 Dunbarc88aa792009-11-30 07:18:13 +0000276
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000277 bool CanonicalPrefixes = true;
Rafael Espindola8a1af322010-07-19 15:20:12 +0000278 for (int i = 1, size = argv.size(); i < size; ++i) {
Rafael Espindola0f4c59c2009-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 Dunbaraf20afb2010-02-25 03:23:43 +0000287 TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions());
288 DiagClient.setPrefix(Path.getBasename());
Daniel Dunbar510d7322009-03-18 02:11:26 +0000289
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000290 Diagnostic Diags(&DiagClient);
Daniel Dunbar510d7322009-03-18 02:11:26 +0000291
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000292#ifdef CLANG_IS_PRODUCTION
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000293 const bool IsProduction = true;
Daniel Dunbar5d93ed32010-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 Dunbarf44c5852009-09-22 22:31:13 +0000299#else
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000300 const bool IsProduction = false;
Daniel Dunbar5d93ed32010-04-01 18:21:41 +0000301 const bool CXXIsProduction = false;
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000302#endif
Daniel Dunbar0bbad512010-07-19 00:44:04 +0000303 Driver TheDriver(Path.str(), llvm::sys::getHostTriple(),
Daniel Dunbar5d93ed32010-04-01 18:21:41 +0000304 "a.out", IsProduction, CXXIsProduction,
305 Diags);
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000306
Daniel Dunbar8fa01c82009-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 Kramerd5dc9f32009-11-20 11:49:06 +0000312 //
313 // We use *argv instead of argv[0] to work around a bogus g++ warning.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000314 const char *progname = argv_[0];
315 std::string ProgName(llvm::sys::Path(progname).getBasename());
Daniel Dunbare26bd902009-11-11 10:10:25 +0000316 if (llvm::StringRef(ProgName).endswith("++") ||
Daniel Dunbar0dea4be2009-12-25 20:21:23 +0000317 llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) {
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000318 TheDriver.CCCIsCXX = true;
Daniel Dunbar0dea4be2009-12-25 20:21:23 +0000319 TheDriver.CCCGenericGCCName = "g++";
320 }
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000321
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000322 llvm::OwningPtr<Compilation> C;
323
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000324 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
325 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
326 if (TheDriver.CCPrintOptions)
327 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
328
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000329 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
330 // command line behind the scenes.
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000331 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
332 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000333 ApplyQAOverride(argv, OverrideStr, SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000334 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000335 // FIXME: Driver shouldn't take extra initial argument.
Rafael Espindola8a1af322010-07-19 15:20:12 +0000336 std::vector<const char*> ExtraArgs;
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000337
338 for (;;) {
339 const char *Next = strchr(Cur, ',');
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000340
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000341 if (Next) {
Rafael Espindola8a1af322010-07-19 15:20:12 +0000342 ExtraArgs.push_back(SaveStringInSet(SavedStrings,
343 std::string(Cur, Next)));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000344 Cur = Next + 1;
345 } else {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000346 if (*Cur != '\0')
Rafael Espindola8a1af322010-07-19 15:20:12 +0000347 ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000348 break;
349 }
350 }
351
Rafael Espindola8a1af322010-07-19 15:20:12 +0000352 argv.insert(++argv.begin(), ExtraArgs.begin(), ExtraArgs.end());
Rafael Espindola06e35d32010-07-18 22:03:55 +0000353 }
Rafael Espindola8a1af322010-07-19 15:20:12 +0000354 C.reset(TheDriver.BuildCompilation(argv.size(), &argv[0]));
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000355
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000356 int Res = 0;
357 if (C.get())
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000358 Res = TheDriver.ExecuteCompilation(*C);
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000359
Chris Lattner30bc7e82010-03-30 05:39:52 +0000360
361 // If any timers were active but haven't been destroyed yet, print their
362 // results now. This happens in -disable-free mode.
363 llvm::TimerGroup::printAll(llvm::errs());
364
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000365 llvm::llvm_shutdown();
366
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000367 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000368}