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