blob: c61ee726449fde08738bc30fe7bc1248594e00b3 [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 Dunbar2c6f6f32009-03-04 08:33:23 +000018
Daniel Dunbar510d7322009-03-18 02:11:26 +000019#include "llvm/ADT/SmallString.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000020#include "llvm/ADT/OwningPtr.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000021#include "llvm/Config/config.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000022#include "llvm/Support/ManagedStatic.h"
23#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000024#include "llvm/Support/raw_ostream.h"
Daniel Dunbaraf07f932009-03-31 17:35:15 +000025#include "llvm/System/Host.h"
Daniel Dunbar365c02f2009-03-10 20:52:46 +000026#include "llvm/System/Path.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000027#include "llvm/System/Signals.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000028using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000029using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000030
Daniel Dunbar510d7322009-03-18 02:11:26 +000031class DriverDiagnosticPrinter : public DiagnosticClient {
32 std::string ProgName;
33 llvm::raw_ostream &OS;
34
35public:
Daniel Dunbar237a31b2009-07-17 18:10:27 +000036 DriverDiagnosticPrinter(const std::string _ProgName,
Daniel Dunbar510d7322009-03-18 02:11:26 +000037 llvm::raw_ostream &_OS)
38 : ProgName(_ProgName),
39 OS(_OS) {}
40
41 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
42 const DiagnosticInfo &Info);
43};
44
45void DriverDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
46 const DiagnosticInfo &Info) {
47 OS << ProgName << ": ";
48
49 switch (Level) {
50 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
51 case Diagnostic::Note: OS << "note: "; break;
52 case Diagnostic::Warning: OS << "warning: "; break;
53 case Diagnostic::Error: OS << "error: "; break;
54 case Diagnostic::Fatal: OS << "fatal error: "; break;
55 }
Daniel Dunbar237a31b2009-07-17 18:10:27 +000056
Daniel Dunbar510d7322009-03-18 02:11:26 +000057 llvm::SmallString<100> OutStr;
58 Info.FormatDiagnostic(OutStr);
59 OS.write(OutStr.begin(), OutStr.size());
60 OS << '\n';
61}
62
Daniel Dunbar734932c2009-03-18 20:25:53 +000063llvm::sys::Path GetExecutablePath(const char *Argv0) {
64 // This just needs to be some symbol in the binary; C++ doesn't
65 // allow taking the address of ::main however.
66 void *P = (void*) (intptr_t) GetExecutablePath;
67 return llvm::sys::Path::GetMainExecutable(Argv0, P);
68}
69
Daniel Dunbar237a31b2009-07-17 18:10:27 +000070static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Daniel Dunbarec9587d2009-04-17 01:54:00 +000071 const std::string &S) {
72 return SavedStrings.insert(S).first->c_str();
73}
74
75/// ApplyQAOverride - Apply a list of edits to the input argument lists.
76///
77/// The input string is a space separate list of edits to perform,
78/// they are applied in order to the input argument lists. Edits
79/// should be one of the following forms:
80///
Daniel Dunbare3d60232009-07-16 21:32:51 +000081/// '#': Silence information about the changes to the command line arguments.
82///
Daniel Dunbarec9587d2009-04-17 01:54:00 +000083/// '^': Add FOO as a new argument at the beginning of the command line.
84///
85/// '+': Add FOO as a new argument at the end of the command line.
86///
87/// 's/XXX/YYY/': Replace the literal argument XXX by YYY in the
88/// command line.
89///
90/// 'xOPTION': Removes all instances of the literal argument OPTION.
91///
92/// 'XOPTION': Removes all instances of the literal argument OPTION,
93/// and the following argument.
94///
95/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
96/// at the end of the command line.
Daniel Dunbare3d60232009-07-16 21:32:51 +000097///
98/// \param OS - The stream to write edit information to.
99/// \param Args - The vector of command line arguments.
100/// \param Edit - The override command to perform.
101/// \param SavedStrings - Set to use for storing string representations.
102void ApplyOneQAOverride(llvm::raw_ostream &OS,
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000103 std::vector<const char*> &Args,
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000104 const std::string &Edit,
105 std::set<std::string> &SavedStrings) {
106 // This does not need to be efficient.
107
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000108 if (Edit[0] == '^') {
109 const char *Str =
110 SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
111 OS << "### Adding argument " << Str << " at beginning\n";
112 Args.insert(Args.begin() + 1, Str);
113 } else if (Edit[0] == '+') {
114 const char *Str =
115 SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
116 OS << "### Adding argument " << Str << " at end\n";
117 Args.push_back(Str);
118 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
119 std::string Option = Edit.substr(1, std::string::npos);
120 for (unsigned i = 1; i < Args.size();) {
121 if (Option == Args[i]) {
122 OS << "### Deleting argument " << Args[i] << '\n';
123 Args.erase(Args.begin() + i);
124 if (Edit[0] == 'X') {
125 if (i < Args.size()) {
126 OS << "### Deleting argument " << Args[i] << '\n';
127 Args.erase(Args.begin() + i);
128 } else
129 OS << "### Invalid X edit, end of command line!\n";
130 }
131 } else
132 ++i;
133 }
134 } else if (Edit[0] == 'O') {
135 for (unsigned i = 1; i < Args.size();) {
136 const char *A = Args[i];
137 if (A[0] == '-' && A[1] == 'O' &&
138 (A[2] == '\0' ||
139 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
140 ('0' <= A[2] && A[2] <= '9'))))) {
141 OS << "### Deleting argument " << Args[i] << '\n';
142 Args.erase(Args.begin() + i);
143 } else
144 ++i;
145 }
146 OS << "### Adding argument " << Edit << " at end\n";
147 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit));
148 } else {
149 OS << "### Unrecognized edit: " << Edit << "\n";
150 }
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000151}
152
153/// ApplyQAOverride - Apply a comma separate list of edits to the
154/// input argument lists. See ApplyOneQAOverride.
155void ApplyQAOverride(std::vector<const char*> &Args, 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 Dunbar217acbf2009-11-19 07:37:51 +0000183
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000184int main(int argc, const char **argv) {
185 llvm::sys::PrintStackTraceOnErrorSignal();
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000186 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000187
Daniel Dunbarc88aa792009-11-30 07:18:13 +0000188 // Dispatch to cc1_main if appropriate.
189 if (argc > 1 && llvm::StringRef(argv[1]) == "-cc1")
190 return cc1_main(argv+2, argv+argc, argv[0],
191 (void*) (intptr_t) GetExecutablePath);
192
Daniel Dunbar734932c2009-03-18 20:25:53 +0000193 llvm::sys::Path Path = GetExecutablePath(argv[0]);
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000194 DriverDiagnosticPrinter DiagClient(Path.getBasename(), llvm::errs());
Daniel Dunbar510d7322009-03-18 02:11:26 +0000195
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000196 Diagnostic Diags(&DiagClient);
Daniel Dunbar510d7322009-03-18 02:11:26 +0000197
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000198#ifdef CLANG_IS_PRODUCTION
199 bool IsProduction = true;
200#else
201 bool IsProduction = false;
202#endif
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000203 Driver TheDriver(Path.getBasename().c_str(), Path.getDirname().c_str(),
204 llvm::sys::getHostTriple().c_str(),
Daniel Dunbarf44c5852009-09-22 22:31:13 +0000205 "a.out", IsProduction, Diags);
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000206
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000207 // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++
208 // compiler. This matches things like "c++", "clang++", and "clang++-1.1".
209 //
210 // Note that we intentionally want to use argv[0] here, to support "clang++"
211 // being a symlink.
Benjamin Kramerd5dc9f32009-11-20 11:49:06 +0000212 //
213 // We use *argv instead of argv[0] to work around a bogus g++ warning.
214 std::string ProgName(llvm::sys::Path(*argv).getBasename());
Daniel Dunbare26bd902009-11-11 10:10:25 +0000215 if (llvm::StringRef(ProgName).endswith("++") ||
216 llvm::StringRef(ProgName).rsplit('-').first.endswith("++"))
Daniel Dunbar8fa01c82009-11-10 18:47:41 +0000217 TheDriver.CCCIsCXX = true;
218
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000219 llvm::OwningPtr<Compilation> C;
220
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000221 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
222 // command line behind the scenes.
Daniel Dunbara1e2fd92009-04-10 18:32:59 +0000223 std::set<std::string> SavedStrings;
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000224 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
225 // FIXME: Driver shouldn't take extra initial argument.
226 std::vector<const char*> StringPointers(argv, argv + argc);
227
228 ApplyQAOverride(StringPointers, OverrideStr, SavedStrings);
229
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000230 C.reset(TheDriver.BuildCompilation(StringPointers.size(),
Daniel Dunbar0311d472009-05-26 18:00:25 +0000231 &StringPointers[0]));
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000232 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000233 std::vector<const char*> StringPointers;
234
235 // FIXME: Driver shouldn't take extra initial argument.
236 StringPointers.push_back(argv[0]);
237
238 for (;;) {
239 const char *Next = strchr(Cur, ',');
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000240
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000241 if (Next) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000242 StringPointers.push_back(SaveStringInSet(SavedStrings,
243 std::string(Cur, Next)));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000244 Cur = Next + 1;
245 } else {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000246 if (*Cur != '\0')
247 StringPointers.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000248 break;
249 }
250 }
251
252 StringPointers.insert(StringPointers.end(), argv + 1, argv + argc);
253
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000254 C.reset(TheDriver.BuildCompilation(StringPointers.size(),
Daniel Dunbar0311d472009-05-26 18:00:25 +0000255 &StringPointers[0]));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000256 } else
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000257 C.reset(TheDriver.BuildCompilation(argc, argv));
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000258
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000259 int Res = 0;
260 if (C.get())
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000261 Res = TheDriver.ExecuteCompilation(*C);
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000262
263 llvm::llvm_shutdown();
264
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000265 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000266}
Daniel Dunbar7ec3daf2009-03-24 03:07:05 +0000267