Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 1 | //===-- 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 Dunbar | 1eb4e64 | 2009-03-03 05:55:11 +0000 | [diff] [blame] | 10 | // This is the entry point to the clang driver; it is a thin wrapper |
| 11 | // for functionality in the Driver clang library. |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Driver/Compilation.h" |
| 16 | #include "clang/Driver/Driver.h" |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Option.h" |
Daniel Dunbar | af20afb | 2010-02-25 03:23:43 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/DiagnosticOptions.h" |
| 19 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 20 | |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | dd98e2c | 2009-03-10 23:41:59 +0000 | [diff] [blame] | 24 | #include "llvm/Config/config.h" |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ManagedStatic.h" |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Regex.h" |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame^] | 32 | #include "llvm/Support/Host.h" |
| 33 | #include "llvm/Support/Path.h" |
| 34 | #include "llvm/Support/Program.h" |
| 35 | #include "llvm/Support/Signals.h" |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 36 | using namespace clang; |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 37 | using namespace clang::driver; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 38 | |
Benjamin Kramer | aeed3da | 2010-10-30 17:32:40 +0000 | [diff] [blame] | 39 | llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) { |
Rafael Espindola | 0f4c59c | 2009-12-04 19:31:58 +0000 | [diff] [blame] | 40 | if (!CanonicalPrefixes) |
| 41 | return llvm::sys::Path(Argv0); |
| 42 | |
Daniel Dunbar | 734932c | 2009-03-18 20:25:53 +0000 | [diff] [blame] | 43 | // This just needs to be some symbol in the binary; C++ doesn't |
| 44 | // allow taking the address of ::main however. |
| 45 | void *P = (void*) (intptr_t) GetExecutablePath; |
| 46 | return llvm::sys::Path::GetMainExecutable(Argv0, P); |
| 47 | } |
| 48 | |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 49 | static const char *SaveStringInSet(std::set<std::string> &SavedStrings, |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 50 | llvm::StringRef S) { |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 51 | return SavedStrings.insert(S).first->c_str(); |
| 52 | } |
| 53 | |
| 54 | /// ApplyQAOverride - Apply a list of edits to the input argument lists. |
| 55 | /// |
| 56 | /// The input string is a space separate list of edits to perform, |
| 57 | /// they are applied in order to the input argument lists. Edits |
| 58 | /// should be one of the following forms: |
| 59 | /// |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 60 | /// '#': Silence information about the changes to the command line arguments. |
| 61 | /// |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 62 | /// '^': Add FOO as a new argument at the beginning of the command line. |
| 63 | /// |
| 64 | /// '+': Add FOO as a new argument at the end of the command line. |
| 65 | /// |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 66 | /// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command |
| 67 | /// line. |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 68 | /// |
| 69 | /// 'xOPTION': Removes all instances of the literal argument OPTION. |
| 70 | /// |
| 71 | /// 'XOPTION': Removes all instances of the literal argument OPTION, |
| 72 | /// and the following argument. |
| 73 | /// |
| 74 | /// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox' |
| 75 | /// at the end of the command line. |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 76 | /// |
| 77 | /// \param OS - The stream to write edit information to. |
| 78 | /// \param Args - The vector of command line arguments. |
| 79 | /// \param Edit - The override command to perform. |
| 80 | /// \param SavedStrings - Set to use for storing string representations. |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 81 | static void ApplyOneQAOverride(llvm::raw_ostream &OS, |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 82 | llvm::SmallVectorImpl<const char*> &Args, |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 83 | llvm::StringRef Edit, |
| 84 | std::set<std::string> &SavedStrings) { |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 85 | // This does not need to be efficient. |
| 86 | |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 87 | if (Edit[0] == '^') { |
| 88 | const char *Str = |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 89 | SaveStringInSet(SavedStrings, Edit.substr(1)); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 90 | OS << "### Adding argument " << Str << " at beginning\n"; |
| 91 | Args.insert(Args.begin() + 1, Str); |
| 92 | } else if (Edit[0] == '+') { |
| 93 | const char *Str = |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 94 | SaveStringInSet(SavedStrings, Edit.substr(1)); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 95 | OS << "### Adding argument " << Str << " at end\n"; |
| 96 | Args.push_back(Str); |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 97 | } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") && |
| 98 | Edit.slice(2, Edit.size()-1).find('/') != llvm::StringRef::npos) { |
| 99 | llvm::StringRef MatchPattern = Edit.substr(2).split('/').first; |
| 100 | llvm::StringRef ReplPattern = Edit.substr(2).split('/').second; |
| 101 | ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1); |
| 102 | |
| 103 | for (unsigned i = 1, e = Args.size(); i != e; ++i) { |
| 104 | std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]); |
| 105 | |
| 106 | if (Repl != Args[i]) { |
| 107 | OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n"; |
| 108 | Args[i] = SaveStringInSet(SavedStrings, Repl); |
| 109 | } |
| 110 | } |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 111 | } else if (Edit[0] == 'x' || Edit[0] == 'X') { |
| 112 | std::string Option = Edit.substr(1, std::string::npos); |
| 113 | for (unsigned i = 1; i < Args.size();) { |
| 114 | if (Option == Args[i]) { |
| 115 | OS << "### Deleting argument " << Args[i] << '\n'; |
| 116 | Args.erase(Args.begin() + i); |
| 117 | if (Edit[0] == 'X') { |
| 118 | if (i < Args.size()) { |
| 119 | OS << "### Deleting argument " << Args[i] << '\n'; |
| 120 | Args.erase(Args.begin() + i); |
| 121 | } else |
| 122 | OS << "### Invalid X edit, end of command line!\n"; |
| 123 | } |
| 124 | } else |
| 125 | ++i; |
| 126 | } |
| 127 | } else if (Edit[0] == 'O') { |
| 128 | for (unsigned i = 1; i < Args.size();) { |
| 129 | const char *A = Args[i]; |
| 130 | if (A[0] == '-' && A[1] == 'O' && |
| 131 | (A[2] == '\0' || |
| 132 | (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' || |
| 133 | ('0' <= A[2] && A[2] <= '9'))))) { |
| 134 | OS << "### Deleting argument " << Args[i] << '\n'; |
| 135 | Args.erase(Args.begin() + i); |
| 136 | } else |
| 137 | ++i; |
| 138 | } |
| 139 | OS << "### Adding argument " << Edit << " at end\n"; |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 140 | Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str())); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 141 | } else { |
| 142 | OS << "### Unrecognized edit: " << Edit << "\n"; |
| 143 | } |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /// ApplyQAOverride - Apply a comma separate list of edits to the |
| 147 | /// input argument lists. See ApplyOneQAOverride. |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 148 | static void ApplyQAOverride(llvm::SmallVectorImpl<const char*> &Args, |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 149 | const char *OverrideStr, |
| 150 | std::set<std::string> &SavedStrings) { |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 151 | llvm::raw_ostream *OS = &llvm::errs(); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 152 | |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 153 | if (OverrideStr[0] == '#') { |
| 154 | ++OverrideStr; |
| 155 | OS = &llvm::nulls(); |
| 156 | } |
| 157 | |
| 158 | *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n"; |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 159 | |
| 160 | // This does not need to be efficient. |
| 161 | |
| 162 | const char *S = OverrideStr; |
| 163 | while (*S) { |
| 164 | const char *End = ::strchr(S, ' '); |
| 165 | if (!End) |
| 166 | End = S + strlen(S); |
| 167 | if (End != S) |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 168 | ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings); |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 169 | S = End; |
| 170 | if (*S != '\0') |
| 171 | ++S; |
| 172 | } |
| 173 | } |
| 174 | |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 175 | extern int cc1_main(const char **ArgBegin, const char **ArgEnd, |
Daniel Dunbar | 545c281 | 2009-11-29 20:58:32 +0000 | [diff] [blame] | 176 | const char *Argv0, void *MainAddr); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 177 | extern int cc1as_main(const char **ArgBegin, const char **ArgEnd, |
| 178 | const char *Argv0, void *MainAddr); |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 179 | |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 180 | static void ExpandArgsFromBuf(const char *Arg, |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 181 | llvm::SmallVectorImpl<const char*> &ArgVector, |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 182 | std::set<std::string> &SavedStrings) { |
| 183 | const char *FName = Arg + 1; |
| 184 | llvm::MemoryBuffer *MemBuf = llvm::MemoryBuffer::getFile(FName); |
| 185 | if (!MemBuf) { |
| 186 | ArgVector.push_back(SaveStringInSet(SavedStrings, Arg)); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | const char *Buf = MemBuf->getBufferStart(); |
| 191 | char InQuote = ' '; |
| 192 | std::string CurArg; |
| 193 | |
| 194 | for (const char *P = Buf; ; ++P) { |
| 195 | if (*P == '\0' || (isspace(*P) && InQuote == ' ')) { |
| 196 | if (!CurArg.empty()) { |
| 197 | |
| 198 | if (CurArg[0] != '@') { |
| 199 | ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg)); |
| 200 | } else { |
| 201 | ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings); |
| 202 | } |
| 203 | |
| 204 | CurArg = ""; |
| 205 | } |
| 206 | if (*P == '\0') |
| 207 | break; |
| 208 | else |
| 209 | continue; |
| 210 | } |
| 211 | |
| 212 | if (isspace(*P)) { |
| 213 | if (InQuote != ' ') |
| 214 | CurArg.push_back(*P); |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | if (*P == '"' || *P == '\'') { |
| 219 | if (InQuote == *P) |
| 220 | InQuote = ' '; |
| 221 | else if (InQuote == ' ') |
| 222 | InQuote = *P; |
| 223 | else |
| 224 | CurArg.push_back(*P); |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | if (*P == '\\') { |
| 229 | ++P; |
| 230 | if (*P != '\0') |
| 231 | CurArg.push_back(*P); |
| 232 | continue; |
| 233 | } |
| 234 | CurArg.push_back(*P); |
| 235 | } |
| 236 | delete MemBuf; |
| 237 | } |
| 238 | |
| 239 | static void ExpandArgv(int argc, const char **argv, |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 240 | llvm::SmallVectorImpl<const char*> &ArgVector, |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 241 | std::set<std::string> &SavedStrings) { |
| 242 | for (int i = 0; i < argc; ++i) { |
| 243 | const char *Arg = argv[i]; |
| 244 | if (Arg[0] != '@') { |
| 245 | ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg))); |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | ExpandArgsFromBuf(Arg, ArgVector, SavedStrings); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | int main(int argc_, const char **argv_) { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 254 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 255 | llvm::PrettyStackTraceProgram X(argc_, argv_); |
| 256 | |
| 257 | std::set<std::string> SavedStrings; |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 258 | llvm::SmallVector<const char*, 256> argv; |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 259 | |
| 260 | ExpandArgv(argc_, argv_, argv, SavedStrings); |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 261 | |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 262 | // Handle -cc1 integrated tools. |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 263 | if (argv.size() > 1 && llvm::StringRef(argv[1]).startswith("-cc1")) { |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 264 | llvm::StringRef Tool = argv[1] + 4; |
| 265 | |
| 266 | if (Tool == "") |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 267 | return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0], |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 268 | (void*) (intptr_t) GetExecutablePath); |
| 269 | if (Tool == "as") |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 270 | return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0], |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 271 | (void*) (intptr_t) GetExecutablePath); |
| 272 | |
| 273 | // Reject unknown tools. |
| 274 | llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n"; |
| 275 | return 1; |
| 276 | } |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 277 | |
Rafael Espindola | 0f4c59c | 2009-12-04 19:31:58 +0000 | [diff] [blame] | 278 | bool CanonicalPrefixes = true; |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 279 | for (int i = 1, size = argv.size(); i < size; ++i) { |
Rafael Espindola | 0f4c59c | 2009-12-04 19:31:58 +0000 | [diff] [blame] | 280 | if (llvm::StringRef(argv[i]) == "-no-canonical-prefixes") { |
| 281 | CanonicalPrefixes = false; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes); |
| 287 | |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 288 | TextDiagnosticPrinter *DiagClient |
| 289 | = new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions()); |
| 290 | DiagClient->setPrefix(Path.getBasename()); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 291 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 292 | Diagnostic Diags(DiagID, DiagClient); |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 293 | |
Daniel Dunbar | f44c585 | 2009-09-22 22:31:13 +0000 | [diff] [blame] | 294 | #ifdef CLANG_IS_PRODUCTION |
Kovarththanan Rajaratnam | 240c734 | 2010-03-08 18:33:04 +0000 | [diff] [blame] | 295 | const bool IsProduction = true; |
Daniel Dunbar | 5d93ed3 | 2010-04-01 18:21:41 +0000 | [diff] [blame] | 296 | # ifdef CLANGXX_IS_PRODUCTION |
| 297 | const bool CXXIsProduction = true; |
| 298 | # else |
| 299 | const bool CXXIsProduction = false; |
| 300 | # endif |
Daniel Dunbar | f44c585 | 2009-09-22 22:31:13 +0000 | [diff] [blame] | 301 | #else |
Kovarththanan Rajaratnam | 240c734 | 2010-03-08 18:33:04 +0000 | [diff] [blame] | 302 | const bool IsProduction = false; |
Daniel Dunbar | 5d93ed3 | 2010-04-01 18:21:41 +0000 | [diff] [blame] | 303 | const bool CXXIsProduction = false; |
Daniel Dunbar | f44c585 | 2009-09-22 22:31:13 +0000 | [diff] [blame] | 304 | #endif |
Daniel Dunbar | 0bbad51 | 2010-07-19 00:44:04 +0000 | [diff] [blame] | 305 | Driver TheDriver(Path.str(), llvm::sys::getHostTriple(), |
Daniel Dunbar | 5d93ed3 | 2010-04-01 18:21:41 +0000 | [diff] [blame] | 306 | "a.out", IsProduction, CXXIsProduction, |
| 307 | Diags); |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 308 | |
Daniel Dunbar | edf29b0 | 2010-08-01 22:29:51 +0000 | [diff] [blame] | 309 | // Attempt to find the original path used to invoke the driver, to determine |
| 310 | // the installed path. We do this manually, because we want to support that |
| 311 | // path being a symlink. |
| 312 | llvm::sys::Path InstalledPath(argv[0]); |
| 313 | |
| 314 | // Do a PATH lookup, if there are no directory components. |
| 315 | if (InstalledPath.getLast() == InstalledPath.str()) { |
| 316 | llvm::sys::Path Tmp = |
| 317 | llvm::sys::Program::FindProgramByName(InstalledPath.getLast()); |
| 318 | if (!Tmp.empty()) |
| 319 | InstalledPath = Tmp; |
| 320 | } |
| 321 | InstalledPath.makeAbsolute(); |
| 322 | InstalledPath.eraseComponent(); |
| 323 | if (InstalledPath.exists()) |
| 324 | TheDriver.setInstalledDir(InstalledPath.str()); |
| 325 | |
Daniel Dunbar | 8fa01c8 | 2009-11-10 18:47:41 +0000 | [diff] [blame] | 326 | // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++ |
| 327 | // compiler. This matches things like "c++", "clang++", and "clang++-1.1". |
| 328 | // |
| 329 | // Note that we intentionally want to use argv[0] here, to support "clang++" |
| 330 | // being a symlink. |
Benjamin Kramer | d5dc9f3 | 2009-11-20 11:49:06 +0000 | [diff] [blame] | 331 | // |
| 332 | // We use *argv instead of argv[0] to work around a bogus g++ warning. |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 333 | const char *progname = argv_[0]; |
| 334 | std::string ProgName(llvm::sys::Path(progname).getBasename()); |
Daniel Dunbar | e26bd90 | 2009-11-11 10:10:25 +0000 | [diff] [blame] | 335 | if (llvm::StringRef(ProgName).endswith("++") || |
Daniel Dunbar | 0dea4be | 2009-12-25 20:21:23 +0000 | [diff] [blame] | 336 | llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) { |
Daniel Dunbar | 8fa01c8 | 2009-11-10 18:47:41 +0000 | [diff] [blame] | 337 | TheDriver.CCCIsCXX = true; |
Daniel Dunbar | 0dea4be | 2009-12-25 20:21:23 +0000 | [diff] [blame] | 338 | } |
Daniel Dunbar | 8fa01c8 | 2009-11-10 18:47:41 +0000 | [diff] [blame] | 339 | |
Daniel Dunbar | 4c00fcd | 2010-03-20 08:01:59 +0000 | [diff] [blame] | 340 | // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE. |
| 341 | TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS"); |
| 342 | if (TheDriver.CCPrintOptions) |
| 343 | TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE"); |
| 344 | |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 345 | // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a |
| 346 | // command line behind the scenes. |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 347 | if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) { |
| 348 | // FIXME: Driver shouldn't take extra initial argument. |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 349 | ApplyQAOverride(argv, OverrideStr, SavedStrings); |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 350 | } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) { |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 351 | // FIXME: Driver shouldn't take extra initial argument. |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 352 | std::vector<const char*> ExtraArgs; |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 353 | |
| 354 | for (;;) { |
| 355 | const char *Next = strchr(Cur, ','); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 356 | |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 357 | if (Next) { |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 358 | ExtraArgs.push_back(SaveStringInSet(SavedStrings, |
| 359 | std::string(Cur, Next))); |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 360 | Cur = Next + 1; |
| 361 | } else { |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 362 | if (*Cur != '\0') |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 363 | ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur)); |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 368 | argv.insert(&argv[1], ExtraArgs.begin(), ExtraArgs.end()); |
Rafael Espindola | 06e35d3 | 2010-07-18 22:03:55 +0000 | [diff] [blame] | 369 | } |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 370 | |
Daniel Dunbar | 5633c1e | 2010-08-01 22:29:47 +0000 | [diff] [blame] | 371 | llvm::OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv.size(), |
| 372 | &argv[0])); |
Daniel Dunbar | af96def | 2009-03-21 00:40:53 +0000 | [diff] [blame] | 373 | int Res = 0; |
| 374 | if (C.get()) |
Daniel Dunbar | c88a88f | 2009-07-01 20:03:04 +0000 | [diff] [blame] | 375 | Res = TheDriver.ExecuteCompilation(*C); |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 376 | |
| 377 | // If any timers were active but haven't been destroyed yet, print their |
| 378 | // results now. This happens in -disable-free mode. |
| 379 | llvm::TimerGroup::printAll(llvm::errs()); |
| 380 | |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 381 | llvm::llvm_shutdown(); |
| 382 | |
Daniel Dunbar | af96def | 2009-03-21 00:40:53 +0000 | [diff] [blame] | 383 | return Res; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 384 | } |