blob: cd382b3356fe47bd584ded8e79bacdc2cea2cbbb [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- Job.cpp - Command to Execute -------------------------------------===//
Daniel Dunbar313c2912009-03-13 23:36:33 +00002//
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 Bognerd3371d82015-07-17 03:35:54 +000010#include "InputInfo.h"
Hans Wennborgc5afd062014-02-18 21:42:51 +000011#include "clang/Driver/Driver.h"
12#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar313c2912009-03-13 23:36:33 +000013#include "clang/Driver/Job.h"
Hans Wennborgc5afd062014-02-18 21:42:51 +000014#include "clang/Driver/Tool.h"
15#include "clang/Driver/ToolChain.h"
Reid Kleckner0290c9c2014-09-15 17:45:39 +000016#include "llvm/ADT/ArrayRef.h"
Chad Rosierbe10f982011-08-02 17:58:04 +000017#include "llvm/ADT/STLExtras.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000018#include "llvm/ADT/StringRef.h"
Reid Kleckner0290c9c2014-09-15 17:45:39 +000019#include "llvm/ADT/StringSet.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000020#include "llvm/ADT/StringSwitch.h"
Hans Wennborge8677ef2013-09-12 18:35:08 +000021#include "llvm/Support/Program.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000022#include "llvm/Support/raw_ostream.h"
Daniel Dunbar313c2912009-03-13 23:36:33 +000023#include <cassert>
24using namespace clang::driver;
Hans Wennborgb212b342013-09-12 18:23:34 +000025using llvm::raw_ostream;
26using llvm::StringRef;
Reid Kleckner0290c9c2014-09-15 17:45:39 +000027using llvm::ArrayRef;
Daniel Dunbar313c2912009-03-13 23:36:33 +000028
Justin Bogner94817612015-07-02 22:52:04 +000029Command::Command(const Action &Source, const Tool &Creator,
Justin Bognerd3371d82015-07-17 03:35:54 +000030 const char *Executable, const ArgStringList &Arguments,
31 ArrayRef<InputInfo> Inputs)
Justin Bogner0cd92482015-07-02 22:52:08 +000032 : Source(Source), Creator(Creator), Executable(Executable),
Justin Bognerd3371d82015-07-17 03:35:54 +000033 Arguments(Arguments), ResponseFile(nullptr) {
34 for (const auto &II : Inputs)
35 if (II.isFilename())
36 InputFilenames.push_back(II.getFilename());
37}
Daniel Dunbar313c2912009-03-13 23:36:33 +000038
Justin Bogner0cb14752015-03-12 00:52:56 +000039static int skipArgs(const char *Flag, bool HaveCrashVFS) {
Hans Wennborgb212b342013-09-12 18:23:34 +000040 // 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 Bogner0cb14752015-03-12 00:52:56 +000048 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
Hans Wennborgb212b342013-09-12 18:23:34 +000049 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
Justin Bogner61c0e432014-06-22 20:35:10 +000050 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
Justin Bogner0cb14752015-03-12 00:52:56 +000051 // Some include flags shouldn't be skipped if we have a crash VFS
52 .Case("-isysroot", !HaveCrashVFS)
Hans Wennborgb212b342013-09-12 18:23:34 +000053 .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 Bognerf893a652014-04-22 21:30:17 +000073 if (FlagRef.startswith("-F") || FlagRef.startswith("-I") ||
74 FlagRef.startswith("-fmodules-cache-path="))
Hans Wennborgb212b342013-09-12 18:23:34 +000075 return 1;
76
77 return 0;
78}
79
Justin Bognered9cbe02015-07-09 06:58:31 +000080void Command::printArg(raw_ostream &OS, const char *Arg, bool Quote) {
Hans Wennborgb212b342013-09-12 18:23:34 +000081 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 Kleckner0290c9c2014-09-15 17:45:39 +000098void 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 Klecknere2d03442015-07-15 22:42:37 +0000107 // 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 Kleckner0290c9c2014-09-15 17:45:39 +0000110 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
124void 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 Wennborgb212b342013-09-12 18:23:34 +0000153void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000154 CrashReportInfo *CrashInfo) const {
Reid Kleckner822434d2014-08-05 20:49:12 +0000155 // Always quote the exe.
Reid Kleckner4f1fc352014-08-07 00:05:00 +0000156 OS << ' ';
Justin Bognered9cbe02015-07-09 06:58:31 +0000157 printArg(OS, Executable, /*Quote=*/true);
Hans Wennborgb212b342013-09-12 18:23:34 +0000158
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000159 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 Bogner0cb14752015-03-12 00:52:56 +0000166 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000167 for (size_t i = 0, e = Args.size(); i < e; ++i) {
168 const char *const Arg = Args[i];
Hans Wennborgb212b342013-09-12 18:23:34 +0000169
Justin Bogner25645152014-10-21 17:24:44 +0000170 if (CrashInfo) {
Justin Bogner0cb14752015-03-12 00:52:56 +0000171 if (int Skip = skipArgs(Arg, HaveCrashVFS)) {
Hans Wennborgb212b342013-09-12 18:23:34 +0000172 i += Skip - 1;
173 continue;
Justin Bognerd3371d82015-07-17 03:35:54 +0000174 }
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 Bogner25645152014-10-21 17:24:44 +0000179 // Replace the input file name with the crashinfo's file name.
180 OS << ' ';
181 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
Justin Bognered9cbe02015-07-09 06:58:31 +0000182 printArg(OS, ShortName.str().c_str(), Quote);
Justin Bogner25645152014-10-21 17:24:44 +0000183 continue;
Hans Wennborgb212b342013-09-12 18:23:34 +0000184 }
185 }
186
187 OS << ' ';
Justin Bognered9cbe02015-07-09 06:58:31 +0000188 printArg(OS, Arg, Quote);
Hans Wennborgb212b342013-09-12 18:23:34 +0000189 }
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000190
Justin Bogner0cb14752015-03-12 00:52:56 +0000191 if (CrashInfo && HaveCrashVFS) {
Justin Bogner25645152014-10-21 17:24:44 +0000192 OS << ' ';
Justin Bognered9cbe02015-07-09 06:58:31 +0000193 printArg(OS, "-ivfsoverlay", Quote);
Justin Bogner25645152014-10-21 17:24:44 +0000194 OS << ' ';
Justin Bognered9cbe02015-07-09 06:58:31 +0000195 printArg(OS, CrashInfo->VFSPath.str().c_str(), Quote);
Justin Bogner25645152014-10-21 17:24:44 +0000196 }
197
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000198 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 Wennborgb212b342013-09-12 18:23:34 +0000208 OS << Terminator;
209}
210
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000211void Command::setResponseFile(const char *FileName) {
212 ResponseFile = FileName;
213 ResponseFileFlag = Creator.getResponseFileFlag();
214 ResponseFileFlag += FileName;
215}
216
Hans Wennborge693e842013-09-18 00:41:15 +0000217int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000218 bool *ExecutionFailed) const {
219 SmallVector<const char*, 128> Argv;
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000220
221 if (ResponseFile == nullptr) {
222 Argv.push_back(Executable);
Benjamin Kramerf9890422015-02-17 16:48:30 +0000223 Argv.append(Arguments.begin(), Arguments.end());
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000224 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 Topper92fc2df2014-05-17 16:56:41 +0000240 Argv.push_back(nullptr);
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000241 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 Wennborge8677ef2013-09-12 18:35:08 +0000252
Craig Topper92fc2df2014-05-17 16:56:41 +0000253 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000254 Redirects, /*secondsToWait*/ 0,
255 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
256}
257
Hans Wennborg87cfa712013-09-19 20:32:16 +0000258FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
259 const char *Executable_,
260 const ArgStringList &Arguments_,
Justin Bognerd3371d82015-07-17 03:35:54 +0000261 ArrayRef<InputInfo> Inputs,
David Blaikiec11bf802014-09-04 16:04:28 +0000262 std::unique_ptr<Command> Fallback_)
Justin Bognerd3371d82015-07-17 03:35:54 +0000263 : Command(Source_, Creator_, Executable_, Arguments_, Inputs),
David Blaikiec11bf802014-09-04 16:04:28 +0000264 Fallback(std::move(Fallback_)) {}
Hans Wennborg87cfa712013-09-19 20:32:16 +0000265
266void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
Justin Bogner25645152014-10-21 17:24:44 +0000267 bool Quote, CrashReportInfo *CrashInfo) const {
268 Command::Print(OS, "", Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000269 OS << " ||";
Justin Bogner25645152014-10-21 17:24:44 +0000270 Fallback->Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000271}
272
273static 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
280int 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 Wennborgc5afd062014-02-18 21:42:51 +0000292 const Driver &D = getCreator().getToolChain().getDriver();
Hans Wennborg897a69b2014-02-19 02:10:19 +0000293 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
Hans Wennborgc5afd062014-02-18 21:42:51 +0000294
Hans Wennborg87cfa712013-09-19 20:32:16 +0000295 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
296 return SecondaryStatus;
297}
298
Hans Wennborgb212b342013-09-12 18:23:34 +0000299void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000300 CrashReportInfo *CrashInfo) const {
Justin Bogneraab97922014-10-03 01:04:53 +0000301 for (const auto &Job : *this)
Justin Bogner25645152014-10-21 17:24:44 +0000302 Job.Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgb212b342013-09-12 18:23:34 +0000303}
304
David Blaikiec11bf802014-09-04 16:04:28 +0000305void JobList::clear() { Jobs.clear(); }