blob: 2108c8fbdbfee079e862dbc0ac1376b6452049c4 [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"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000022#include "llvm/ADT/OwningPtr.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000023#include "llvm/Config/config.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000024#include "llvm/Support/ManagedStatic.h"
25#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000026#include "llvm/Support/Regex.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbaraf07f932009-03-31 17:35:15 +000028#include "llvm/System/Host.h"
Daniel Dunbar365c02f2009-03-10 20:52:46 +000029#include "llvm/System/Path.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000030#include "llvm/System/Signals.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000031using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000032using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000033
Rafael Espindola0f4c59c2009-12-04 19:31:58 +000034llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
35 if (!CanonicalPrefixes)
36 return llvm::sys::Path(Argv0);
37
Daniel Dunbar734932c2009-03-18 20:25:53 +000038 // This just needs to be some symbol in the binary; C++ doesn't
39 // allow taking the address of ::main however.
40 void *P = (void*) (intptr_t) GetExecutablePath;
41 return llvm::sys::Path::GetMainExecutable(Argv0, P);
42}
43
Daniel Dunbar237a31b2009-07-17 18:10:27 +000044static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000045 llvm::StringRef S) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +000046 return SavedStrings.insert(S).first->c_str();
47}
48
49/// ApplyQAOverride - Apply a list of edits to the input argument lists.
50///
51/// The input string is a space separate list of edits to perform,
52/// they are applied in order to the input argument lists. Edits
53/// should be one of the following forms:
54///
Daniel Dunbare3d60232009-07-16 21:32:51 +000055/// '#': Silence information about the changes to the command line arguments.
56///
Daniel Dunbarec9587d2009-04-17 01:54:00 +000057/// '^': Add FOO as a new argument at the beginning of the command line.
58///
59/// '+': Add FOO as a new argument at the end of the command line.
60///
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000061/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
62/// line.
Daniel Dunbarec9587d2009-04-17 01:54:00 +000063///
64/// 'xOPTION': Removes all instances of the literal argument OPTION.
65///
66/// 'XOPTION': Removes all instances of the literal argument OPTION,
67/// and the following argument.
68///
69/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
70/// at the end of the command line.
Daniel Dunbare3d60232009-07-16 21:32:51 +000071///
72/// \param OS - The stream to write edit information to.
73/// \param Args - The vector of command line arguments.
74/// \param Edit - The override command to perform.
75/// \param SavedStrings - Set to use for storing string representations.
76void ApplyOneQAOverride(llvm::raw_ostream &OS,
Daniel Dunbar237a31b2009-07-17 18:10:27 +000077 std::vector<const char*> &Args,
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000078 llvm::StringRef Edit,
Daniel Dunbarec9587d2009-04-17 01:54:00 +000079 std::set<std::string> &SavedStrings) {
80 // This does not need to be efficient.
81
Daniel Dunbar237a31b2009-07-17 18:10:27 +000082 if (Edit[0] == '^') {
83 const char *Str =
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000084 SaveStringInSet(SavedStrings, Edit.substr(1));
Daniel Dunbar237a31b2009-07-17 18:10:27 +000085 OS << "### Adding argument " << Str << " at beginning\n";
86 Args.insert(Args.begin() + 1, Str);
87 } else 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 end\n";
91 Args.push_back(Str);
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +000092 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
93 Edit.slice(2, Edit.size()-1).find('/') != llvm::StringRef::npos) {
94 llvm::StringRef MatchPattern = Edit.substr(2).split('/').first;
95 llvm::StringRef ReplPattern = Edit.substr(2).split('/').second;
96 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
97
98 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
99 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
100
101 if (Repl != Args[i]) {
102 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
103 Args[i] = SaveStringInSet(SavedStrings, Repl);
104 }
105 }
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000106 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
107 std::string Option = Edit.substr(1, std::string::npos);
108 for (unsigned i = 1; i < Args.size();) {
109 if (Option == Args[i]) {
110 OS << "### Deleting argument " << Args[i] << '\n';
111 Args.erase(Args.begin() + i);
112 if (Edit[0] == 'X') {
113 if (i < Args.size()) {
114 OS << "### Deleting argument " << Args[i] << '\n';
115 Args.erase(Args.begin() + i);
116 } else
117 OS << "### Invalid X edit, end of command line!\n";
118 }
119 } else
120 ++i;
121 }
122 } else if (Edit[0] == 'O') {
123 for (unsigned i = 1; i < Args.size();) {
124 const char *A = Args[i];
125 if (A[0] == '-' && A[1] == 'O' &&
126 (A[2] == '\0' ||
127 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
128 ('0' <= A[2] && A[2] <= '9'))))) {
129 OS << "### Deleting argument " << Args[i] << '\n';
130 Args.erase(Args.begin() + i);
131 } else
132 ++i;
133 }
134 OS << "### Adding argument " << Edit << " at end\n";
Daniel Dunbar0de9a7b2010-02-17 21:00:34 +0000135 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str()));
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000136 } else {
137 OS << "### Unrecognized edit: " << Edit << "\n";
138 }
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000139}
140
141/// ApplyQAOverride - Apply a comma separate list of edits to the
142/// input argument lists. See ApplyOneQAOverride.
143void ApplyQAOverride(std::vector<const char*> &Args, const char *OverrideStr,
144 std::set<std::string> &SavedStrings) {
Daniel Dunbare3d60232009-07-16 21:32:51 +0000145 llvm::raw_ostream *OS = &llvm::errs();
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000146
Daniel Dunbare3d60232009-07-16 21:32:51 +0000147 if (OverrideStr[0] == '#') {
148 ++OverrideStr;
149 OS = &llvm::nulls();
150 }
151
152 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000153
154 // This does not need to be efficient.
155
156 const char *S = OverrideStr;
157 while (*S) {
158 const char *End = ::strchr(S, ' ');
159 if (!End)
160 End = S + strlen(S);
161 if (End != S)
Daniel Dunbare3d60232009-07-16 21:32:51 +0000162 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000163 S = End;
164 if (*S != '\0')
165 ++S;
166 }
167}
168
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000169extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
Daniel Dunbar545c2812009-11-29 20:58:32 +0000170 const char *Argv0, void *MainAddr);
Daniel Dunbar217acbf2009-11-19 07:37:51 +0000171
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000172int main(int argc, const char **argv) {
173 llvm::sys::PrintStackTraceOnErrorSignal();
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000174 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000175
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000176 // Dispatch to cc1_main if appropriate.
177 if (argc > 1 && llvm::StringRef(argv[1]) == "-cc1")
178 return cc1_main(argv+2, argv+argc, argv[0],
179 (void*) (intptr_t) GetExecutablePath);
180
Rafael Espindola0f4c59c2009-12-04 19:31:58 +0000181 bool CanonicalPrefixes = true;
182 for (int i = 1; i < argc; ++i) {
183 if (llvm::StringRef(argv[i]) == "-no-canonical-prefixes") {
184 CanonicalPrefixes = false;
185 break;
186 }
187 }
188
189 llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes);
190
Daniel Dunbaraf20afb2010-02-25 03:23:43 +0000191 TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions());
192 DiagClient.setPrefix(Path.getBasename());
Daniel Dunbar510d7322009-03-18 02:11:26 +0000193
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000194 Diagnostic Diags(&DiagClient);
Daniel Dunbar510d7322009-03-18 02:11:26 +0000195
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000196#ifdef CLANG_IS_PRODUCTION
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000197 const bool IsProduction = true;
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000198#else
Kovarththanan Rajaratnam240c7342010-03-08 18:33:04 +0000199 const bool IsProduction = false;
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000200#endif
Jeffrey Yasskine3fdca22009-12-08 01:46:24 +0000201 Driver TheDriver(Path.getBasename(), Path.getDirname(),
202 llvm::sys::getHostTriple(),
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000203 "a.out", IsProduction, Diags);
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000204
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000205 // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++
206 // compiler. This matches things like "c++", "clang++", and "clang++-1.1".
207 //
208 // Note that we intentionally want to use argv[0] here, to support "clang++"
209 // being a symlink.
Benjamin Kramerd5dc9f32009-11-20 11:49:06 +0000210 //
211 // We use *argv instead of argv[0] to work around a bogus g++ warning.
212 std::string ProgName(llvm::sys::Path(*argv).getBasename());
Daniel Dunbare26bd902009-11-11 10:10:25 +0000213 if (llvm::StringRef(ProgName).endswith("++") ||
Daniel Dunbar0dea4be2009-12-25 20:21:23 +0000214 llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) {
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000215 TheDriver.CCCIsCXX = true;
Daniel Dunbar0dea4be2009-12-25 20:21:23 +0000216 TheDriver.CCCGenericGCCName = "g++";
217 }
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000218
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000219 llvm::OwningPtr<Compilation> C;
220
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000221 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
222 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
223 if (TheDriver.CCPrintOptions)
224 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
225
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000226 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
227 // command line behind the scenes.
Daniel Dunbara1e2fd92009-04-10 18:32:59 +0000228 std::set<std::string> SavedStrings;
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000229 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
230 // FIXME: Driver shouldn't take extra initial argument.
231 std::vector<const char*> StringPointers(argv, argv + argc);
232
233 ApplyQAOverride(StringPointers, OverrideStr, SavedStrings);
234
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000235 C.reset(TheDriver.BuildCompilation(StringPointers.size(),
Daniel Dunbar0311d472009-05-26 18:00:25 +0000236 &StringPointers[0]));
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000237 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000238 std::vector<const char*> StringPointers;
239
240 // FIXME: Driver shouldn't take extra initial argument.
241 StringPointers.push_back(argv[0]);
242
243 for (;;) {
244 const char *Next = strchr(Cur, ',');
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000245
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000246 if (Next) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000247 StringPointers.push_back(SaveStringInSet(SavedStrings,
248 std::string(Cur, Next)));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000249 Cur = Next + 1;
250 } else {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000251 if (*Cur != '\0')
252 StringPointers.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000253 break;
254 }
255 }
256
257 StringPointers.insert(StringPointers.end(), argv + 1, argv + argc);
258
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000259 C.reset(TheDriver.BuildCompilation(StringPointers.size(),
Daniel Dunbar0311d472009-05-26 18:00:25 +0000260 &StringPointers[0]));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000261 } else
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000262 C.reset(TheDriver.BuildCompilation(argc, argv));
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000263
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000264 int Res = 0;
265 if (C.get())
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000266 Res = TheDriver.ExecuteCompilation(*C);
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000267
268 llvm::llvm_shutdown();
269
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000270 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000271}