Nick Lewycky | 6da9077 | 2010-12-31 17:31:54 +0000 | [diff] [blame] | 1 | //===--- Job.cpp - Command to Execute -------------------------------------===// |
Daniel Dunbar | 313c291 | 2009-03-13 23:36:33 +0000 | [diff] [blame] | 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 | |
Hans Wennborg | c5afd06 | 2014-02-18 21:42:51 +0000 | [diff] [blame] | 10 | #include "clang/Driver/Driver.h" |
| 11 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | 313c291 | 2009-03-13 23:36:33 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Job.h" |
Hans Wennborg | c5afd06 | 2014-02-18 21:42:51 +0000 | [diff] [blame] | 13 | #include "clang/Driver/Tool.h" |
| 14 | #include "clang/Driver/ToolChain.h" |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ArrayRef.h" |
Chad Rosier | be10f98 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSet.h" |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringSwitch.h" |
Hans Wennborg | e8677ef | 2013-09-12 18:35:08 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Program.h" |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 313c291 | 2009-03-13 23:36:33 +0000 | [diff] [blame] | 22 | #include <cassert> |
| 23 | using namespace clang::driver; |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 24 | using llvm::raw_ostream; |
| 25 | using llvm::StringRef; |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 26 | using llvm::ArrayRef; |
Daniel Dunbar | 313c291 | 2009-03-13 23:36:33 +0000 | [diff] [blame] | 27 | |
Justin Bogner | 9481761 | 2015-07-02 22:52:04 +0000 | [diff] [blame] | 28 | Command::Command(const Action &Source, const Tool &Creator, |
| 29 | const char *Executable, const ArgStringList &Arguments) |
Justin Bogner | 0cd9248 | 2015-07-02 22:52:08 +0000 | [diff] [blame^] | 30 | : Source(Source), Creator(Creator), Executable(Executable), |
| 31 | Arguments(Arguments), ResponseFile(nullptr) {} |
Daniel Dunbar | 313c291 | 2009-03-13 23:36:33 +0000 | [diff] [blame] | 32 | |
Justin Bogner | 0cb1475 | 2015-03-12 00:52:56 +0000 | [diff] [blame] | 33 | static int skipArgs(const char *Flag, bool HaveCrashVFS) { |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 34 | // These flags are all of the form -Flag <Arg> and are treated as two |
| 35 | // arguments. Therefore, we need to skip the flag and the next argument. |
| 36 | bool Res = llvm::StringSwitch<bool>(Flag) |
| 37 | .Cases("-I", "-MF", "-MT", "-MQ", true) |
| 38 | .Cases("-o", "-coverage-file", "-dependency-file", true) |
| 39 | .Cases("-fdebug-compilation-dir", "-idirafter", true) |
| 40 | .Cases("-include", "-include-pch", "-internal-isystem", true) |
| 41 | .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true) |
Justin Bogner | 0cb1475 | 2015-03-12 00:52:56 +0000 | [diff] [blame] | 42 | .Cases("-iwithprefixbefore", "-isystem", "-iquote", true) |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 43 | .Cases("-resource-dir", "-serialize-diagnostic-file", true) |
Justin Bogner | 61c0e43 | 2014-06-22 20:35:10 +0000 | [diff] [blame] | 44 | .Cases("-dwarf-debug-flags", "-ivfsoverlay", true) |
Justin Bogner | 0cb1475 | 2015-03-12 00:52:56 +0000 | [diff] [blame] | 45 | // Some include flags shouldn't be skipped if we have a crash VFS |
| 46 | .Case("-isysroot", !HaveCrashVFS) |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 47 | .Default(false); |
| 48 | |
| 49 | // Match found. |
| 50 | if (Res) |
| 51 | return 2; |
| 52 | |
| 53 | // The remaining flags are treated as a single argument. |
| 54 | |
| 55 | // These flags are all of the form -Flag and have no second argument. |
| 56 | Res = llvm::StringSwitch<bool>(Flag) |
| 57 | .Cases("-M", "-MM", "-MG", "-MP", "-MD", true) |
| 58 | .Case("-MMD", true) |
| 59 | .Default(false); |
| 60 | |
| 61 | // Match found. |
| 62 | if (Res) |
| 63 | return 1; |
| 64 | |
| 65 | // These flags are treated as a single argument (e.g., -F<Dir>). |
| 66 | StringRef FlagRef(Flag); |
Justin Bogner | f893a65 | 2014-04-22 21:30:17 +0000 | [diff] [blame] | 67 | if (FlagRef.startswith("-F") || FlagRef.startswith("-I") || |
| 68 | FlagRef.startswith("-fmodules-cache-path=")) |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 69 | return 1; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 74 | static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) { |
| 75 | const bool Escape = std::strpbrk(Arg, "\"\\$"); |
| 76 | |
| 77 | if (!Quote && !Escape) { |
| 78 | OS << Arg; |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Quote and escape. This isn't really complete, but good enough. |
| 83 | OS << '"'; |
| 84 | while (const char c = *Arg++) { |
| 85 | if (c == '"' || c == '\\' || c == '$') |
| 86 | OS << '\\'; |
| 87 | OS << c; |
| 88 | } |
| 89 | OS << '"'; |
| 90 | } |
| 91 | |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 92 | void Command::writeResponseFile(raw_ostream &OS) const { |
| 93 | // In a file list, we only write the set of inputs to the response file |
| 94 | if (Creator.getResponseFilesSupport() == Tool::RF_FileList) { |
| 95 | for (const char *Arg : InputFileList) { |
| 96 | OS << Arg << '\n'; |
| 97 | } |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // In regular response files, we send all arguments to the response file |
| 102 | for (const char *Arg : Arguments) { |
| 103 | OS << '"'; |
| 104 | |
| 105 | for (; *Arg != '\0'; Arg++) { |
| 106 | if (*Arg == '\"' || *Arg == '\\') { |
| 107 | OS << '\\'; |
| 108 | } |
| 109 | OS << *Arg; |
| 110 | } |
| 111 | |
| 112 | OS << "\" "; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void Command::buildArgvForResponseFile( |
| 117 | llvm::SmallVectorImpl<const char *> &Out) const { |
| 118 | // When not a file list, all arguments are sent to the response file. |
| 119 | // This leaves us to set the argv to a single parameter, requesting the tool |
| 120 | // to read the response file. |
| 121 | if (Creator.getResponseFilesSupport() != Tool::RF_FileList) { |
| 122 | Out.push_back(Executable); |
| 123 | Out.push_back(ResponseFileFlag.c_str()); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | llvm::StringSet<> Inputs; |
| 128 | for (const char *InputName : InputFileList) |
| 129 | Inputs.insert(InputName); |
| 130 | Out.push_back(Executable); |
| 131 | // In a file list, build args vector ignoring parameters that will go in the |
| 132 | // response file (elements of the InputFileList vector) |
| 133 | bool FirstInput = true; |
| 134 | for (const char *Arg : Arguments) { |
| 135 | if (Inputs.count(Arg) == 0) { |
| 136 | Out.push_back(Arg); |
| 137 | } else if (FirstInput) { |
| 138 | FirstInput = false; |
| 139 | Out.push_back(Creator.getResponseFileFlag()); |
| 140 | Out.push_back(ResponseFile); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 145 | void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote, |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 146 | CrashReportInfo *CrashInfo) const { |
Reid Kleckner | 822434d | 2014-08-05 20:49:12 +0000 | [diff] [blame] | 147 | // Always quote the exe. |
Reid Kleckner | 4f1fc35 | 2014-08-07 00:05:00 +0000 | [diff] [blame] | 148 | OS << ' '; |
Reid Kleckner | 822434d | 2014-08-05 20:49:12 +0000 | [diff] [blame] | 149 | PrintArg(OS, Executable, /*Quote=*/true); |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 150 | |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 151 | llvm::ArrayRef<const char *> Args = Arguments; |
| 152 | llvm::SmallVector<const char *, 128> ArgsRespFile; |
| 153 | if (ResponseFile != nullptr) { |
| 154 | buildArgvForResponseFile(ArgsRespFile); |
| 155 | Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name |
| 156 | } |
| 157 | |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 158 | StringRef MainFilename; |
| 159 | // We'll need the argument to -main-file-name to find the input file name. |
| 160 | if (CrashInfo) |
| 161 | for (size_t I = 0, E = Args.size(); I + 1 < E; ++I) |
| 162 | if (StringRef(Args[I]).equals("-main-file-name")) |
| 163 | MainFilename = Args[I + 1]; |
| 164 | |
Justin Bogner | 0cb1475 | 2015-03-12 00:52:56 +0000 | [diff] [blame] | 165 | bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty(); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 166 | for (size_t i = 0, e = Args.size(); i < e; ++i) { |
| 167 | const char *const Arg = Args[i]; |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 168 | |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 169 | if (CrashInfo) { |
Justin Bogner | 0cb1475 | 2015-03-12 00:52:56 +0000 | [diff] [blame] | 170 | if (int Skip = skipArgs(Arg, HaveCrashVFS)) { |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 171 | i += Skip - 1; |
| 172 | continue; |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 173 | } else if (llvm::sys::path::filename(Arg) == MainFilename && |
| 174 | (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) { |
| 175 | // Replace the input file name with the crashinfo's file name. |
| 176 | OS << ' '; |
| 177 | StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename); |
| 178 | PrintArg(OS, ShortName.str().c_str(), Quote); |
| 179 | continue; |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | OS << ' '; |
| 184 | PrintArg(OS, Arg, Quote); |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 185 | } |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 186 | |
Justin Bogner | 0cb1475 | 2015-03-12 00:52:56 +0000 | [diff] [blame] | 187 | if (CrashInfo && HaveCrashVFS) { |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 188 | OS << ' '; |
| 189 | PrintArg(OS, "-ivfsoverlay", Quote); |
| 190 | OS << ' '; |
| 191 | PrintArg(OS, CrashInfo->VFSPath.str().c_str(), Quote); |
| 192 | } |
| 193 | |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 194 | if (ResponseFile != nullptr) { |
| 195 | OS << "\n Arguments passed via response file:\n"; |
| 196 | writeResponseFile(OS); |
| 197 | // Avoiding duplicated newline terminator, since FileLists are |
| 198 | // newline-separated. |
| 199 | if (Creator.getResponseFilesSupport() != Tool::RF_FileList) |
| 200 | OS << "\n"; |
| 201 | OS << " (end of response file)"; |
| 202 | } |
| 203 | |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 204 | OS << Terminator; |
| 205 | } |
| 206 | |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 207 | void Command::setResponseFile(const char *FileName) { |
| 208 | ResponseFile = FileName; |
| 209 | ResponseFileFlag = Creator.getResponseFileFlag(); |
| 210 | ResponseFileFlag += FileName; |
| 211 | } |
| 212 | |
Hans Wennborg | e693e84 | 2013-09-18 00:41:15 +0000 | [diff] [blame] | 213 | int Command::Execute(const StringRef **Redirects, std::string *ErrMsg, |
Hans Wennborg | e8677ef | 2013-09-12 18:35:08 +0000 | [diff] [blame] | 214 | bool *ExecutionFailed) const { |
| 215 | SmallVector<const char*, 128> Argv; |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 216 | |
| 217 | if (ResponseFile == nullptr) { |
| 218 | Argv.push_back(Executable); |
Benjamin Kramer | f989042 | 2015-02-17 16:48:30 +0000 | [diff] [blame] | 219 | Argv.append(Arguments.begin(), Arguments.end()); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 220 | Argv.push_back(nullptr); |
| 221 | |
| 222 | return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr, |
| 223 | Redirects, /*secondsToWait*/ 0, |
| 224 | /*memoryLimit*/ 0, ErrMsg, |
| 225 | ExecutionFailed); |
| 226 | } |
| 227 | |
| 228 | // We need to put arguments in a response file (command is too large) |
| 229 | // Open stream to store the response file contents |
| 230 | std::string RespContents; |
| 231 | llvm::raw_string_ostream SS(RespContents); |
| 232 | |
| 233 | // Write file contents and build the Argv vector |
| 234 | writeResponseFile(SS); |
| 235 | buildArgvForResponseFile(Argv); |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 236 | Argv.push_back(nullptr); |
Reid Kleckner | 0290c9c | 2014-09-15 17:45:39 +0000 | [diff] [blame] | 237 | SS.flush(); |
| 238 | |
| 239 | // Save the response file in the appropriate encoding |
| 240 | if (std::error_code EC = writeFileWithEncoding( |
| 241 | ResponseFile, RespContents, Creator.getResponseFileEncoding())) { |
| 242 | if (ErrMsg) |
| 243 | *ErrMsg = EC.message(); |
| 244 | if (ExecutionFailed) |
| 245 | *ExecutionFailed = true; |
| 246 | return -1; |
| 247 | } |
Hans Wennborg | e8677ef | 2013-09-12 18:35:08 +0000 | [diff] [blame] | 248 | |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 249 | return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr, |
Hans Wennborg | e8677ef | 2013-09-12 18:35:08 +0000 | [diff] [blame] | 250 | Redirects, /*secondsToWait*/ 0, |
| 251 | /*memoryLimit*/ 0, ErrMsg, ExecutionFailed); |
| 252 | } |
| 253 | |
Hans Wennborg | 87cfa71 | 2013-09-19 20:32:16 +0000 | [diff] [blame] | 254 | FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_, |
| 255 | const char *Executable_, |
| 256 | const ArgStringList &Arguments_, |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 257 | std::unique_ptr<Command> Fallback_) |
| 258 | : Command(Source_, Creator_, Executable_, Arguments_), |
| 259 | Fallback(std::move(Fallback_)) {} |
Hans Wennborg | 87cfa71 | 2013-09-19 20:32:16 +0000 | [diff] [blame] | 260 | |
| 261 | void FallbackCommand::Print(raw_ostream &OS, const char *Terminator, |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 262 | bool Quote, CrashReportInfo *CrashInfo) const { |
| 263 | Command::Print(OS, "", Quote, CrashInfo); |
Hans Wennborg | 87cfa71 | 2013-09-19 20:32:16 +0000 | [diff] [blame] | 264 | OS << " ||"; |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 265 | Fallback->Print(OS, Terminator, Quote, CrashInfo); |
Hans Wennborg | 87cfa71 | 2013-09-19 20:32:16 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static bool ShouldFallback(int ExitCode) { |
| 269 | // FIXME: We really just want to fall back for internal errors, such |
| 270 | // as when some symbol cannot be mangled, when we should be able to |
| 271 | // parse something but can't, etc. |
| 272 | return ExitCode != 0; |
| 273 | } |
| 274 | |
| 275 | int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg, |
| 276 | bool *ExecutionFailed) const { |
| 277 | int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed); |
| 278 | if (!ShouldFallback(PrimaryStatus)) |
| 279 | return PrimaryStatus; |
| 280 | |
| 281 | // Clear ExecutionFailed and ErrMsg before falling back. |
| 282 | if (ErrMsg) |
| 283 | ErrMsg->clear(); |
| 284 | if (ExecutionFailed) |
| 285 | *ExecutionFailed = false; |
| 286 | |
Hans Wennborg | c5afd06 | 2014-02-18 21:42:51 +0000 | [diff] [blame] | 287 | const Driver &D = getCreator().getToolChain().getDriver(); |
Hans Wennborg | 897a69b | 2014-02-19 02:10:19 +0000 | [diff] [blame] | 288 | D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable(); |
Hans Wennborg | c5afd06 | 2014-02-18 21:42:51 +0000 | [diff] [blame] | 289 | |
Hans Wennborg | 87cfa71 | 2013-09-19 20:32:16 +0000 | [diff] [blame] | 290 | int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed); |
| 291 | return SecondaryStatus; |
| 292 | } |
| 293 | |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 294 | void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote, |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 295 | CrashReportInfo *CrashInfo) const { |
Justin Bogner | aab9792 | 2014-10-03 01:04:53 +0000 | [diff] [blame] | 296 | for (const auto &Job : *this) |
Justin Bogner | 2564515 | 2014-10-21 17:24:44 +0000 | [diff] [blame] | 297 | Job.Print(OS, Terminator, Quote, CrashInfo); |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 298 | } |
| 299 | |
David Blaikie | c11bf80 | 2014-09-04 16:04:28 +0000 | [diff] [blame] | 300 | void JobList::clear() { Jobs.clear(); } |