blob: a1585009b09e821e812f298ed6ac8cf2408da9d7 [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"
18#include "clang/Driver/Options.h"
19
Daniel Dunbar510d7322009-03-18 02:11:26 +000020#include "llvm/ADT/SmallString.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000021#include "llvm/ADT/OwningPtr.h"
Daniel Dunbardd98e2c2009-03-10 23:41:59 +000022#include "llvm/Config/config.h"
Daniel Dunbar8f25c792009-03-18 01:38:48 +000023#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbaraf07f932009-03-31 17:35:15 +000026#include "llvm/System/Host.h"
Daniel Dunbar365c02f2009-03-10 20:52:46 +000027#include "llvm/System/Path.h"
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000028#include "llvm/System/Signals.h"
Daniel Dunbar4ad4b3e2009-03-12 08:55:43 +000029using namespace clang;
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000030using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000031
Daniel Dunbar510d7322009-03-18 02:11:26 +000032class DriverDiagnosticPrinter : public DiagnosticClient {
33 std::string ProgName;
34 llvm::raw_ostream &OS;
35
36public:
Daniel Dunbar237a31b2009-07-17 18:10:27 +000037 DriverDiagnosticPrinter(const std::string _ProgName,
Daniel Dunbar510d7322009-03-18 02:11:26 +000038 llvm::raw_ostream &_OS)
39 : ProgName(_ProgName),
40 OS(_OS) {}
41
42 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
43 const DiagnosticInfo &Info);
44};
45
46void DriverDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
47 const DiagnosticInfo &Info) {
48 OS << ProgName << ": ";
49
50 switch (Level) {
51 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
52 case Diagnostic::Note: OS << "note: "; break;
53 case Diagnostic::Warning: OS << "warning: "; break;
54 case Diagnostic::Error: OS << "error: "; break;
55 case Diagnostic::Fatal: OS << "fatal error: "; break;
56 }
Daniel Dunbar237a31b2009-07-17 18:10:27 +000057
Daniel Dunbar510d7322009-03-18 02:11:26 +000058 llvm::SmallString<100> OutStr;
59 Info.FormatDiagnostic(OutStr);
60 OS.write(OutStr.begin(), OutStr.size());
61 OS << '\n';
62}
63
Daniel Dunbar734932c2009-03-18 20:25:53 +000064llvm::sys::Path GetExecutablePath(const char *Argv0) {
65 // This just needs to be some symbol in the binary; C++ doesn't
66 // allow taking the address of ::main however.
67 void *P = (void*) (intptr_t) GetExecutablePath;
68 return llvm::sys::Path::GetMainExecutable(Argv0, P);
69}
70
Daniel Dunbar237a31b2009-07-17 18:10:27 +000071static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
Daniel Dunbarec9587d2009-04-17 01:54:00 +000072 const std::string &S) {
73 return SavedStrings.insert(S).first->c_str();
74}
75
76/// ApplyQAOverride - Apply a list of edits to the input argument lists.
77///
78/// The input string is a space separate list of edits to perform,
79/// they are applied in order to the input argument lists. Edits
80/// should be one of the following forms:
81///
Daniel Dunbare3d60232009-07-16 21:32:51 +000082/// '#': Silence information about the changes to the command line arguments.
83///
Daniel Dunbarec9587d2009-04-17 01:54:00 +000084/// '^': Add FOO as a new argument at the beginning of the command line.
85///
86/// '+': Add FOO as a new argument at the end of the command line.
87///
88/// 's/XXX/YYY/': Replace the literal argument XXX by YYY in the
89/// command line.
90///
91/// 'xOPTION': Removes all instances of the literal argument OPTION.
92///
93/// 'XOPTION': Removes all instances of the literal argument OPTION,
94/// and the following argument.
95///
96/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
97/// at the end of the command line.
Daniel Dunbare3d60232009-07-16 21:32:51 +000098///
99/// \param OS - The stream to write edit information to.
100/// \param Args - The vector of command line arguments.
101/// \param Edit - The override command to perform.
102/// \param SavedStrings - Set to use for storing string representations.
103void ApplyOneQAOverride(llvm::raw_ostream &OS,
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000104 std::vector<const char*> &Args,
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000105 const std::string &Edit,
106 std::set<std::string> &SavedStrings) {
107 // This does not need to be efficient.
108
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000109 if (Edit[0] == '^') {
110 const char *Str =
111 SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
112 OS << "### Adding argument " << Str << " at beginning\n";
113 Args.insert(Args.begin() + 1, Str);
114 } else if (Edit[0] == '+') {
115 const char *Str =
116 SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
117 OS << "### Adding argument " << Str << " at end\n";
118 Args.push_back(Str);
119 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
120 std::string Option = Edit.substr(1, std::string::npos);
121 for (unsigned i = 1; i < Args.size();) {
122 if (Option == Args[i]) {
123 OS << "### Deleting argument " << Args[i] << '\n';
124 Args.erase(Args.begin() + i);
125 if (Edit[0] == 'X') {
126 if (i < Args.size()) {
127 OS << "### Deleting argument " << Args[i] << '\n';
128 Args.erase(Args.begin() + i);
129 } else
130 OS << "### Invalid X edit, end of command line!\n";
131 }
132 } else
133 ++i;
134 }
135 } else if (Edit[0] == 'O') {
136 for (unsigned i = 1; i < Args.size();) {
137 const char *A = Args[i];
138 if (A[0] == '-' && A[1] == 'O' &&
139 (A[2] == '\0' ||
140 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
141 ('0' <= A[2] && A[2] <= '9'))))) {
142 OS << "### Deleting argument " << Args[i] << '\n';
143 Args.erase(Args.begin() + i);
144 } else
145 ++i;
146 }
147 OS << "### Adding argument " << Edit << " at end\n";
148 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit));
149 } else {
150 OS << "### Unrecognized edit: " << Edit << "\n";
151 }
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000152}
153
154/// ApplyQAOverride - Apply a comma separate list of edits to the
155/// input argument lists. See ApplyOneQAOverride.
156void ApplyQAOverride(std::vector<const char*> &Args, const char *OverrideStr,
157 std::set<std::string> &SavedStrings) {
Daniel Dunbare3d60232009-07-16 21:32:51 +0000158 llvm::raw_ostream *OS = &llvm::errs();
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000159
Daniel Dunbare3d60232009-07-16 21:32:51 +0000160 if (OverrideStr[0] == '#') {
161 ++OverrideStr;
162 OS = &llvm::nulls();
163 }
164
165 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000166
167 // This does not need to be efficient.
168
169 const char *S = OverrideStr;
170 while (*S) {
171 const char *End = ::strchr(S, ' ');
172 if (!End)
173 End = S + strlen(S);
174 if (End != S)
Daniel Dunbare3d60232009-07-16 21:32:51 +0000175 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000176 S = End;
177 if (*S != '\0')
178 ++S;
179 }
180}
181
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000182int main(int argc, const char **argv) {
183 llvm::sys::PrintStackTraceOnErrorSignal();
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000184 llvm::PrettyStackTraceProgram X(argc, argv);
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000185
Daniel Dunbar734932c2009-03-18 20:25:53 +0000186 llvm::sys::Path Path = GetExecutablePath(argv[0]);
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000187 DriverDiagnosticPrinter DiagClient(Path.getBasename(), llvm::errs());
Daniel Dunbar510d7322009-03-18 02:11:26 +0000188
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000189 Diagnostic Diags(&DiagClient);
Daniel Dunbar510d7322009-03-18 02:11:26 +0000190
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000191 Driver TheDriver(Path.getBasename().c_str(), Path.getDirname().c_str(),
192 llvm::sys::getHostTriple().c_str(),
193 "a.out", Diags);
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000194
195 llvm::OwningPtr<Compilation> C;
196
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000197 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
198 // command line behind the scenes.
Daniel Dunbara1e2fd92009-04-10 18:32:59 +0000199 std::set<std::string> SavedStrings;
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000200 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
201 // FIXME: Driver shouldn't take extra initial argument.
202 std::vector<const char*> StringPointers(argv, argv + argc);
203
204 ApplyQAOverride(StringPointers, OverrideStr, SavedStrings);
205
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000206 C.reset(TheDriver.BuildCompilation(StringPointers.size(),
Daniel Dunbar0311d472009-05-26 18:00:25 +0000207 &StringPointers[0]));
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000208 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000209 std::vector<const char*> StringPointers;
210
211 // FIXME: Driver shouldn't take extra initial argument.
212 StringPointers.push_back(argv[0]);
213
214 for (;;) {
215 const char *Next = strchr(Cur, ',');
Daniel Dunbar237a31b2009-07-17 18:10:27 +0000216
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000217 if (Next) {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000218 StringPointers.push_back(SaveStringInSet(SavedStrings,
219 std::string(Cur, Next)));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000220 Cur = Next + 1;
221 } else {
Daniel Dunbarec9587d2009-04-17 01:54:00 +0000222 if (*Cur != '\0')
223 StringPointers.push_back(SaveStringInSet(SavedStrings, Cur));
Daniel Dunbare5be6da2009-04-01 19:08:46 +0000224 break;
225 }
226 }
227
228 StringPointers.insert(StringPointers.end(), argv + 1, argv + argc);
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 Dunbare5be6da2009-04-01 19:08:46 +0000232 } else
Daniel Dunbar8e6e7092009-05-26 16:15:44 +0000233 C.reset(TheDriver.BuildCompilation(argc, argv));
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000234
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000235 int Res = 0;
236 if (C.get())
Daniel Dunbarc88a88f2009-07-01 20:03:04 +0000237 Res = TheDriver.ExecuteCompilation(*C);
Daniel Dunbar8f25c792009-03-18 01:38:48 +0000238
239 llvm::llvm_shutdown();
240
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000241 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000242}
Daniel Dunbar7ec3daf2009-03-24 03:07:05 +0000243