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 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 15 | #include "clang/Basic/DiagnosticOptions.h" |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 16 | #include "clang/Driver/ArgList.h" |
James Molloy | bfd7a52 | 2012-05-01 14:57:16 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Compilation.h" |
| 19 | #include "clang/Driver/Driver.h" |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Option.h" |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 21 | #include "clang/Driver/OptTable.h" |
| 22 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | af20afb | 2010-02-25 03:23:43 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/Utils.h" |
Daniel Dunbar | 2c6f6f3 | 2009-03-04 08:33:23 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 7f9fc3f | 2011-03-23 04:04:01 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/ArrayRef.h" |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/OwningPtr.h" |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | fbfd180 | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileSystem.h" |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ManagedStatic.h" |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 34 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Regex.h" |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Host.h" |
| 39 | #include "llvm/Support/Path.h" |
| 40 | #include "llvm/Support/Program.h" |
| 41 | #include "llvm/Support/Signals.h" |
Evan Cheng | a6b4045 | 2011-08-24 18:09:14 +0000 | [diff] [blame] | 42 | #include "llvm/Support/TargetRegistry.h" |
| 43 | #include "llvm/Support/TargetSelect.h" |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 44 | #include "llvm/Support/system_error.h" |
Douglas Gregor | 43d013d | 2011-01-17 19:15:43 +0000 | [diff] [blame] | 45 | #include <cctype> |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 46 | using namespace clang; |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 47 | using namespace clang::driver; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 48 | |
Benjamin Kramer | aeed3da | 2010-10-30 17:32:40 +0000 | [diff] [blame] | 49 | llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) { |
Rafael Espindola | 0f4c59c | 2009-12-04 19:31:58 +0000 | [diff] [blame] | 50 | if (!CanonicalPrefixes) |
| 51 | return llvm::sys::Path(Argv0); |
| 52 | |
Daniel Dunbar | 734932c | 2009-03-18 20:25:53 +0000 | [diff] [blame] | 53 | // This just needs to be some symbol in the binary; C++ doesn't |
| 54 | // allow taking the address of ::main however. |
| 55 | void *P = (void*) (intptr_t) GetExecutablePath; |
| 56 | return llvm::sys::Path::GetMainExecutable(Argv0, P); |
| 57 | } |
| 58 | |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 59 | static const char *SaveStringInSet(std::set<std::string> &SavedStrings, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 60 | StringRef S) { |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 61 | return SavedStrings.insert(S).first->c_str(); |
| 62 | } |
| 63 | |
| 64 | /// ApplyQAOverride - Apply a list of edits to the input argument lists. |
| 65 | /// |
| 66 | /// The input string is a space separate list of edits to perform, |
| 67 | /// they are applied in order to the input argument lists. Edits |
| 68 | /// should be one of the following forms: |
| 69 | /// |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 70 | /// '#': Silence information about the changes to the command line arguments. |
| 71 | /// |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 72 | /// '^': Add FOO as a new argument at the beginning of the command line. |
| 73 | /// |
| 74 | /// '+': Add FOO as a new argument at the end of the command line. |
| 75 | /// |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 76 | /// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command |
| 77 | /// line. |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 78 | /// |
| 79 | /// 'xOPTION': Removes all instances of the literal argument OPTION. |
| 80 | /// |
| 81 | /// 'XOPTION': Removes all instances of the literal argument OPTION, |
| 82 | /// and the following argument. |
| 83 | /// |
| 84 | /// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox' |
| 85 | /// at the end of the command line. |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 86 | /// |
| 87 | /// \param OS - The stream to write edit information to. |
| 88 | /// \param Args - The vector of command line arguments. |
| 89 | /// \param Edit - The override command to perform. |
| 90 | /// \param SavedStrings - Set to use for storing string representations. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 91 | static void ApplyOneQAOverride(raw_ostream &OS, |
| 92 | SmallVectorImpl<const char*> &Args, |
| 93 | StringRef Edit, |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 94 | std::set<std::string> &SavedStrings) { |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 95 | // This does not need to be efficient. |
| 96 | |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 97 | if (Edit[0] == '^') { |
| 98 | const char *Str = |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 99 | SaveStringInSet(SavedStrings, Edit.substr(1)); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 100 | OS << "### Adding argument " << Str << " at beginning\n"; |
| 101 | Args.insert(Args.begin() + 1, Str); |
| 102 | } else if (Edit[0] == '+') { |
| 103 | const char *Str = |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 104 | SaveStringInSet(SavedStrings, Edit.substr(1)); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 105 | OS << "### Adding argument " << Str << " at end\n"; |
| 106 | Args.push_back(Str); |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 107 | } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") && |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 108 | Edit.slice(2, Edit.size()-1).find('/') != StringRef::npos) { |
| 109 | StringRef MatchPattern = Edit.substr(2).split('/').first; |
| 110 | StringRef ReplPattern = Edit.substr(2).split('/').second; |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 111 | ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1); |
| 112 | |
| 113 | for (unsigned i = 1, e = Args.size(); i != e; ++i) { |
| 114 | std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]); |
| 115 | |
| 116 | if (Repl != Args[i]) { |
| 117 | OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n"; |
| 118 | Args[i] = SaveStringInSet(SavedStrings, Repl); |
| 119 | } |
| 120 | } |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 121 | } else if (Edit[0] == 'x' || Edit[0] == 'X') { |
| 122 | std::string Option = Edit.substr(1, std::string::npos); |
| 123 | for (unsigned i = 1; i < Args.size();) { |
| 124 | if (Option == Args[i]) { |
| 125 | OS << "### Deleting argument " << Args[i] << '\n'; |
| 126 | Args.erase(Args.begin() + i); |
| 127 | if (Edit[0] == 'X') { |
| 128 | if (i < Args.size()) { |
| 129 | OS << "### Deleting argument " << Args[i] << '\n'; |
| 130 | Args.erase(Args.begin() + i); |
| 131 | } else |
| 132 | OS << "### Invalid X edit, end of command line!\n"; |
| 133 | } |
| 134 | } else |
| 135 | ++i; |
| 136 | } |
| 137 | } else if (Edit[0] == 'O') { |
| 138 | for (unsigned i = 1; i < Args.size();) { |
| 139 | const char *A = Args[i]; |
| 140 | if (A[0] == '-' && A[1] == 'O' && |
| 141 | (A[2] == '\0' || |
| 142 | (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' || |
| 143 | ('0' <= A[2] && A[2] <= '9'))))) { |
| 144 | OS << "### Deleting argument " << Args[i] << '\n'; |
| 145 | Args.erase(Args.begin() + i); |
| 146 | } else |
| 147 | ++i; |
| 148 | } |
| 149 | OS << "### Adding argument " << Edit << " at end\n"; |
Daniel Dunbar | 0de9a7b | 2010-02-17 21:00:34 +0000 | [diff] [blame] | 150 | Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str())); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 151 | } else { |
| 152 | OS << "### Unrecognized edit: " << Edit << "\n"; |
| 153 | } |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /// ApplyQAOverride - Apply a comma separate list of edits to the |
| 157 | /// input argument lists. See ApplyOneQAOverride. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 158 | static void ApplyQAOverride(SmallVectorImpl<const char*> &Args, |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 159 | const char *OverrideStr, |
| 160 | std::set<std::string> &SavedStrings) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 161 | raw_ostream *OS = &llvm::errs(); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 162 | |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 163 | if (OverrideStr[0] == '#') { |
| 164 | ++OverrideStr; |
| 165 | OS = &llvm::nulls(); |
| 166 | } |
| 167 | |
| 168 | *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n"; |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 169 | |
| 170 | // This does not need to be efficient. |
| 171 | |
| 172 | const char *S = OverrideStr; |
| 173 | while (*S) { |
| 174 | const char *End = ::strchr(S, ' '); |
| 175 | if (!End) |
| 176 | End = S + strlen(S); |
| 177 | if (End != S) |
Daniel Dunbar | e3d6023 | 2009-07-16 21:32:51 +0000 | [diff] [blame] | 178 | ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings); |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 179 | S = End; |
| 180 | if (*S != '\0') |
| 181 | ++S; |
| 182 | } |
| 183 | } |
| 184 | |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 185 | extern int cc1_main(const char **ArgBegin, const char **ArgEnd, |
Daniel Dunbar | 545c281 | 2009-11-29 20:58:32 +0000 | [diff] [blame] | 186 | const char *Argv0, void *MainAddr); |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 187 | extern int cc1as_main(const char **ArgBegin, const char **ArgEnd, |
| 188 | const char *Argv0, void *MainAddr); |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 189 | |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 190 | static void ExpandArgsFromBuf(const char *Arg, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 191 | SmallVectorImpl<const char*> &ArgVector, |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 192 | std::set<std::string> &SavedStrings) { |
| 193 | const char *FName = Arg + 1; |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 194 | OwningPtr<llvm::MemoryBuffer> MemBuf; |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 195 | if (llvm::MemoryBuffer::getFile(FName, MemBuf)) { |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 196 | ArgVector.push_back(SaveStringInSet(SavedStrings, Arg)); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | const char *Buf = MemBuf->getBufferStart(); |
| 201 | char InQuote = ' '; |
| 202 | std::string CurArg; |
| 203 | |
| 204 | for (const char *P = Buf; ; ++P) { |
| 205 | if (*P == '\0' || (isspace(*P) && InQuote == ' ')) { |
| 206 | if (!CurArg.empty()) { |
| 207 | |
| 208 | if (CurArg[0] != '@') { |
| 209 | ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg)); |
| 210 | } else { |
| 211 | ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings); |
| 212 | } |
| 213 | |
| 214 | CurArg = ""; |
| 215 | } |
| 216 | if (*P == '\0') |
| 217 | break; |
| 218 | else |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | if (isspace(*P)) { |
| 223 | if (InQuote != ' ') |
| 224 | CurArg.push_back(*P); |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | if (*P == '"' || *P == '\'') { |
| 229 | if (InQuote == *P) |
| 230 | InQuote = ' '; |
| 231 | else if (InQuote == ' ') |
| 232 | InQuote = *P; |
| 233 | else |
| 234 | CurArg.push_back(*P); |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | if (*P == '\\') { |
| 239 | ++P; |
| 240 | if (*P != '\0') |
| 241 | CurArg.push_back(*P); |
| 242 | continue; |
| 243 | } |
| 244 | CurArg.push_back(*P); |
| 245 | } |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static void ExpandArgv(int argc, const char **argv, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 249 | SmallVectorImpl<const char*> &ArgVector, |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 250 | std::set<std::string> &SavedStrings) { |
| 251 | for (int i = 0; i < argc; ++i) { |
| 252 | const char *Arg = argv[i]; |
| 253 | if (Arg[0] != '@') { |
| 254 | ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg))); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | ExpandArgsFromBuf(Arg, ArgVector, SavedStrings); |
| 259 | } |
| 260 | } |
| 261 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 262 | static void ParseProgName(SmallVectorImpl<const char *> &ArgVector, |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 263 | std::set<std::string> &SavedStrings, |
| 264 | Driver &TheDriver) |
| 265 | { |
| 266 | // Try to infer frontend type and default target from the program name. |
| 267 | |
| 268 | // suffixes[] contains the list of known driver suffixes. |
| 269 | // Suffixes are compared against the program name in order. |
| 270 | // If there is a match, the frontend type is updated as necessary (CPP/C++). |
| 271 | // If there is no match, a second round is done after stripping the last |
| 272 | // hyphen and everything following it. This allows using something like |
| 273 | // "clang++-2.9". |
| 274 | |
| 275 | // If there is a match in either the first or second round, |
| 276 | // the function tries to identify a target as prefix. E.g. |
| 277 | // "x86_64-linux-clang" as interpreted as suffix "clang" with |
| 278 | // target prefix "x86_64-linux". If such a target prefix is found, |
Sebastian Pop | 9ec60df | 2012-01-20 22:01:23 +0000 | [diff] [blame] | 279 | // is gets added via -target as implicit first argument. |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 280 | static const struct { |
| 281 | const char *Suffix; |
| 282 | bool IsCXX; |
| 283 | bool IsCPP; |
| 284 | } suffixes [] = { |
Chandler Carruth | a74e4af | 2012-09-11 10:40:21 +0000 | [diff] [blame] | 285 | { "clang", false, false }, |
| 286 | { "clang++", true, false }, |
| 287 | { "clang-c++", true, false }, |
| 288 | { "clang-cc", false, false }, |
| 289 | { "clang-cpp", false, true }, |
| 290 | { "clang-g++", true, false }, |
| 291 | { "clang-gcc", false, false }, |
| 292 | { "cc", false, false }, |
| 293 | { "cpp", false, true }, |
| 294 | { "++", true, false }, |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 295 | }; |
| 296 | std::string ProgName(llvm::sys::path::stem(ArgVector[0])); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 297 | StringRef ProgNameRef(ProgName); |
| 298 | StringRef Prefix; |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 299 | |
| 300 | for (int Components = 2; Components; --Components) { |
| 301 | bool FoundMatch = false; |
| 302 | size_t i; |
| 303 | |
| 304 | for (i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) { |
| 305 | if (ProgNameRef.endswith(suffixes[i].Suffix)) { |
| 306 | FoundMatch = true; |
Chandler Carruth | a74e4af | 2012-09-11 10:40:21 +0000 | [diff] [blame] | 307 | if (suffixes[i].IsCXX) |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 308 | TheDriver.CCCIsCXX = true; |
| 309 | if (suffixes[i].IsCPP) |
| 310 | TheDriver.CCCIsCPP = true; |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (FoundMatch) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 316 | StringRef::size_type LastComponent = ProgNameRef.rfind('-', |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 317 | ProgNameRef.size() - strlen(suffixes[i].Suffix)); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 318 | if (LastComponent != StringRef::npos) |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 319 | Prefix = ProgNameRef.slice(0, LastComponent); |
| 320 | break; |
| 321 | } |
| 322 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 323 | StringRef::size_type LastComponent = ProgNameRef.rfind('-'); |
| 324 | if (LastComponent == StringRef::npos) |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 325 | break; |
| 326 | ProgNameRef = ProgNameRef.slice(0, LastComponent); |
| 327 | } |
| 328 | |
| 329 | if (Prefix.empty()) |
| 330 | return; |
| 331 | |
| 332 | std::string IgnoredError; |
| 333 | if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 334 | SmallVectorImpl<const char *>::iterator it = ArgVector.begin(); |
Joerg Sonnenberger | ca02643 | 2011-07-07 16:57:26 +0000 | [diff] [blame] | 335 | if (it != ArgVector.end()) |
| 336 | ++it; |
| 337 | ArgVector.insert(it, SaveStringInSet(SavedStrings, Prefix)); |
| 338 | ArgVector.insert(it, |
Sebastian Pop | 9ec60df | 2012-01-20 22:01:23 +0000 | [diff] [blame] | 339 | SaveStringInSet(SavedStrings, std::string("-target"))); |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 343 | int main(int argc_, const char **argv_) { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 344 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 345 | llvm::PrettyStackTraceProgram X(argc_, argv_); |
| 346 | |
| 347 | std::set<std::string> SavedStrings; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 348 | SmallVector<const char*, 256> argv; |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 349 | |
| 350 | ExpandArgv(argc_, argv_, argv, SavedStrings); |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 351 | |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 352 | // Handle -cc1 integrated tools. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 353 | if (argv.size() > 1 && StringRef(argv[1]).startswith("-cc1")) { |
| 354 | StringRef Tool = argv[1] + 4; |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 355 | |
| 356 | if (Tool == "") |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 357 | return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0], |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 358 | (void*) (intptr_t) GetExecutablePath); |
| 359 | if (Tool == "as") |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 360 | return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0], |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 361 | (void*) (intptr_t) GetExecutablePath); |
| 362 | |
| 363 | // Reject unknown tools. |
| 364 | llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n"; |
| 365 | return 1; |
| 366 | } |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 367 | |
Rafael Espindola | 0f4c59c | 2009-12-04 19:31:58 +0000 | [diff] [blame] | 368 | bool CanonicalPrefixes = true; |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 369 | for (int i = 1, size = argv.size(); i < size; ++i) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 370 | if (StringRef(argv[i]) == "-no-canonical-prefixes") { |
Rafael Espindola | 0f4c59c | 2009-12-04 19:31:58 +0000 | [diff] [blame] | 371 | CanonicalPrefixes = false; |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes); |
| 377 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 378 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions; |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 379 | { |
| 380 | // Note that ParseDiagnosticArgs() uses the cc1 option table. |
James Molloy | bfd7a52 | 2012-05-01 14:57:16 +0000 | [diff] [blame] | 381 | OwningPtr<OptTable> CC1Opts(createDriverOptTable()); |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 382 | unsigned MissingArgIndex, MissingArgCount; |
| 383 | OwningPtr<InputArgList> Args(CC1Opts->ParseArgs(argv.begin()+1, argv.end(), |
| 384 | MissingArgIndex, MissingArgCount)); |
| 385 | // We ignore MissingArgCount and the return value of ParseDiagnosticArgs. |
| 386 | // Any errors that would be diagnosed here will also be diagnosed later, |
| 387 | // when the DiagnosticsEngine actually exists. |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 388 | (void) ParseDiagnosticArgs(*DiagOpts, *Args); |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 389 | } |
| 390 | // Now we can create the DiagnosticsEngine with a properly-filled-out |
| 391 | // DiagnosticOptions instance. |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 392 | TextDiagnosticPrinter *DiagClient |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 393 | = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); |
Michael J. Spencer | d5b08be | 2010-12-18 04:13:32 +0000 | [diff] [blame] | 394 | DiagClient->setPrefix(llvm::sys::path::stem(Path.str())); |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 395 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
Chad Rosier | 2dec85b | 2012-03-13 20:09:56 +0000 | [diff] [blame] | 396 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 397 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient); |
| 398 | ProcessWarningOptions(Diags, *DiagOpts); |
Daniel Dunbar | 510d732 | 2009-03-18 02:11:26 +0000 | [diff] [blame] | 399 | |
Daniel Dunbar | f44c585 | 2009-09-22 22:31:13 +0000 | [diff] [blame] | 400 | #ifdef CLANG_IS_PRODUCTION |
Kovarththanan Rajaratnam | 240c734 | 2010-03-08 18:33:04 +0000 | [diff] [blame] | 401 | const bool IsProduction = true; |
Daniel Dunbar | f44c585 | 2009-09-22 22:31:13 +0000 | [diff] [blame] | 402 | #else |
Kovarththanan Rajaratnam | 240c734 | 2010-03-08 18:33:04 +0000 | [diff] [blame] | 403 | const bool IsProduction = false; |
Daniel Dunbar | f44c585 | 2009-09-22 22:31:13 +0000 | [diff] [blame] | 404 | #endif |
Sebastian Pop | 5d8b954 | 2011-11-01 21:33:06 +0000 | [diff] [blame] | 405 | Driver TheDriver(Path.str(), llvm::sys::getDefaultTargetTriple(), |
Bob Wilson | 10a82cd | 2011-10-04 05:34:14 +0000 | [diff] [blame] | 406 | "a.out", IsProduction, Diags); |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | edf29b0 | 2010-08-01 22:29:51 +0000 | [diff] [blame] | 408 | // Attempt to find the original path used to invoke the driver, to determine |
| 409 | // the installed path. We do this manually, because we want to support that |
| 410 | // path being a symlink. |
Michael J. Spencer | fbfd180 | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 411 | { |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 412 | SmallString<128> InstalledPath(argv[0]); |
Daniel Dunbar | edf29b0 | 2010-08-01 22:29:51 +0000 | [diff] [blame] | 413 | |
Michael J. Spencer | fbfd180 | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 414 | // Do a PATH lookup, if there are no directory components. |
| 415 | if (llvm::sys::path::filename(InstalledPath) == InstalledPath) { |
| 416 | llvm::sys::Path Tmp = llvm::sys::Program::FindProgramByName( |
| 417 | llvm::sys::path::filename(InstalledPath.str())); |
| 418 | if (!Tmp.empty()) |
| 419 | InstalledPath = Tmp.str(); |
| 420 | } |
| 421 | llvm::sys::fs::make_absolute(InstalledPath); |
| 422 | InstalledPath = llvm::sys::path::parent_path(InstalledPath); |
| 423 | bool exists; |
| 424 | if (!llvm::sys::fs::exists(InstalledPath.str(), exists) && exists) |
| 425 | TheDriver.setInstalledDir(InstalledPath); |
Daniel Dunbar | edf29b0 | 2010-08-01 22:29:51 +0000 | [diff] [blame] | 426 | } |
Daniel Dunbar | edf29b0 | 2010-08-01 22:29:51 +0000 | [diff] [blame] | 427 | |
Joerg Sonnenberger | 0ce89c6 | 2011-03-16 20:15:43 +0000 | [diff] [blame] | 428 | llvm::InitializeAllTargets(); |
| 429 | ParseProgName(argv, SavedStrings, TheDriver); |
Joerg Sonnenberger | 9ade4ae | 2011-03-06 23:31:01 +0000 | [diff] [blame] | 430 | |
Daniel Dunbar | 4c00fcd | 2010-03-20 08:01:59 +0000 | [diff] [blame] | 431 | // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE. |
| 432 | TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS"); |
| 433 | if (TheDriver.CCPrintOptions) |
| 434 | TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE"); |
| 435 | |
Daniel Dunbar | 322c29f | 2011-02-02 21:11:35 +0000 | [diff] [blame] | 436 | // Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE. |
| 437 | TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS"); |
| 438 | if (TheDriver.CCPrintHeaders) |
| 439 | TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE"); |
| 440 | |
Daniel Dunbar | c8a22b0 | 2011-04-07 18:01:20 +0000 | [diff] [blame] | 441 | // Handle CC_LOG_DIAGNOSTICS and CC_LOG_DIAGNOSTICS_FILE. |
| 442 | TheDriver.CCLogDiagnostics = !!::getenv("CC_LOG_DIAGNOSTICS"); |
| 443 | if (TheDriver.CCLogDiagnostics) |
| 444 | TheDriver.CCLogDiagnosticsFilename = ::getenv("CC_LOG_DIAGNOSTICS_FILE"); |
| 445 | |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 446 | // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a |
| 447 | // command line behind the scenes. |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 448 | if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) { |
| 449 | // FIXME: Driver shouldn't take extra initial argument. |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 450 | ApplyQAOverride(argv, OverrideStr, SavedStrings); |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 451 | } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) { |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 452 | // FIXME: Driver shouldn't take extra initial argument. |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 453 | std::vector<const char*> ExtraArgs; |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 454 | |
| 455 | for (;;) { |
| 456 | const char *Next = strchr(Cur, ','); |
Daniel Dunbar | 237a31b | 2009-07-17 18:10:27 +0000 | [diff] [blame] | 457 | |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 458 | if (Next) { |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 459 | ExtraArgs.push_back(SaveStringInSet(SavedStrings, |
| 460 | std::string(Cur, Next))); |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 461 | Cur = Next + 1; |
| 462 | } else { |
Daniel Dunbar | ec9587d | 2009-04-17 01:54:00 +0000 | [diff] [blame] | 463 | if (*Cur != '\0') |
Rafael Espindola | 8a1af32 | 2010-07-19 15:20:12 +0000 | [diff] [blame] | 464 | ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur)); |
Daniel Dunbar | e5be6da | 2009-04-01 19:08:46 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
Daniel Dunbar | e5d6967 | 2010-07-20 02:47:40 +0000 | [diff] [blame] | 469 | argv.insert(&argv[1], ExtraArgs.begin(), ExtraArgs.end()); |
Rafael Espindola | 06e35d3 | 2010-07-18 22:03:55 +0000 | [diff] [blame] | 470 | } |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 471 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 472 | OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv)); |
Daniel Dunbar | af96def | 2009-03-21 00:40:53 +0000 | [diff] [blame] | 473 | int Res = 0; |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 474 | const Command *FailingCommand = 0; |
Daniel Dunbar | af96def | 2009-03-21 00:40:53 +0000 | [diff] [blame] | 475 | if (C.get()) |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 476 | Res = TheDriver.ExecuteCompilation(*C, FailingCommand); |
| 477 | |
Chad Rosier | 80ec127 | 2012-04-20 17:08:59 +0000 | [diff] [blame] | 478 | // Force a crash to test the diagnostics. |
| 479 | if(::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")) |
| 480 | Res = -1; |
| 481 | |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 482 | // If result status is < 0, then the driver command signalled an error. |
| 483 | // In this case, generate additional diagnostic information if possible. |
| 484 | if (Res < 0) |
| 485 | TheDriver.generateCompilationDiagnostics(*C, FailingCommand); |
| 486 | |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 487 | // If any timers were active but haven't been destroyed yet, print their |
| 488 | // results now. This happens in -disable-free mode. |
| 489 | llvm::TimerGroup::printAll(llvm::errs()); |
| 490 | |
Daniel Dunbar | 8f25c79 | 2009-03-18 01:38:48 +0000 | [diff] [blame] | 491 | llvm::llvm_shutdown(); |
| 492 | |
NAKAMURA Takumi | df9b933 | 2012-07-17 05:09:29 +0000 | [diff] [blame] | 493 | #ifdef _WIN32 |
| 494 | // Exit status should not be negative on Win32, unless abnormal termination. |
| 495 | // Once abnormal termiation was caught, negative status should not be |
| 496 | // propagated. |
| 497 | if (Res < 0) |
| 498 | Res = 1; |
| 499 | #endif |
| 500 | |
Daniel Dunbar | af96def | 2009-03-21 00:40:53 +0000 | [diff] [blame] | 501 | return Res; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 502 | } |