blob: 2d99b1f22385a4cfcc0718b0e2b1e94fc6df94d1 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- Job.cpp - Command to Execute -------------------------------------===//
Daniel Dunbar789e2202009-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
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080010#include "InputInfo.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070011#include "clang/Driver/Driver.h"
12#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar789e2202009-03-13 23:36:33 +000013#include "clang/Driver/Job.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070014#include "clang/Driver/Tool.h"
15#include "clang/Driver/ToolChain.h"
Stephen Hines176edba2014-12-01 14:53:08 -080016#include "llvm/ADT/ArrayRef.h"
Chad Rosier2b819102011-08-02 17:58:04 +000017#include "llvm/ADT/STLExtras.h"
Hans Wennborgfc338972013-09-12 18:23:34 +000018#include "llvm/ADT/StringRef.h"
Stephen Hines176edba2014-12-01 14:53:08 -080019#include "llvm/ADT/StringSet.h"
Hans Wennborgfc338972013-09-12 18:23:34 +000020#include "llvm/ADT/StringSwitch.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070021#include "llvm/Support/FileSystem.h"
Hans Wennborgaaaa2a12013-09-12 18:35:08 +000022#include "llvm/Support/Program.h"
Hans Wennborgfc338972013-09-12 18:23:34 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbar789e2202009-03-13 23:36:33 +000024#include <cassert>
25using namespace clang::driver;
Hans Wennborgfc338972013-09-12 18:23:34 +000026using llvm::raw_ostream;
27using llvm::StringRef;
Stephen Hines176edba2014-12-01 14:53:08 -080028using llvm::ArrayRef;
Daniel Dunbar789e2202009-03-13 23:36:33 +000029
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080030Command::Command(const Action &Source, const Tool &Creator,
31 const char *Executable, const ArgStringList &Arguments,
32 ArrayRef<InputInfo> Inputs)
33 : Source(Source), Creator(Creator), Executable(Executable),
34 Arguments(Arguments), ResponseFile(nullptr) {
35 for (const auto &II : Inputs)
36 if (II.isFilename())
37 InputFilenames.push_back(II.getFilename());
38}
Daniel Dunbar789e2202009-03-13 23:36:33 +000039
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070040static int skipArgs(const char *Flag, bool HaveCrashVFS) {
Hans Wennborgfc338972013-09-12 18:23:34 +000041 // These flags are all of the form -Flag <Arg> and are treated as two
42 // arguments. Therefore, we need to skip the flag and the next argument.
43 bool Res = llvm::StringSwitch<bool>(Flag)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070044 .Cases("-MF", "-MT", "-MQ", "-serialize-diagnostic-file", true)
Hans Wennborgfc338972013-09-12 18:23:34 +000045 .Cases("-o", "-coverage-file", "-dependency-file", true)
46 .Cases("-fdebug-compilation-dir", "-idirafter", true)
47 .Cases("-include", "-include-pch", "-internal-isystem", true)
48 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070049 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
Stephen Hinesc568f1e2014-07-21 00:47:37 -070050 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080051 .Cases("-header-include-file", "-diagnostic-log-file", true)
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070052 // Some include flags shouldn't be skipped if we have a crash VFS
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070053 .Cases("-isysroot", "-I", "-F", "-resource-dir", !HaveCrashVFS)
Hans Wennborgfc338972013-09-12 18:23:34 +000054 .Default(false);
55
56 // Match found.
57 if (Res)
58 return 2;
59
60 // The remaining flags are treated as a single argument.
61
62 // These flags are all of the form -Flag and have no second argument.
63 Res = llvm::StringSwitch<bool>(Flag)
64 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
65 .Case("-MMD", true)
66 .Default(false);
67
68 // Match found.
69 if (Res)
70 return 1;
71
72 // These flags are treated as a single argument (e.g., -F<Dir>).
73 StringRef FlagRef(Flag);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070074 if ((!HaveCrashVFS &&
75 (FlagRef.startswith("-F") || FlagRef.startswith("-I"))) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -070076 FlagRef.startswith("-fmodules-cache-path="))
Hans Wennborgfc338972013-09-12 18:23:34 +000077 return 1;
78
79 return 0;
80}
81
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080082void Command::printArg(raw_ostream &OS, const char *Arg, bool Quote) {
Hans Wennborgfc338972013-09-12 18:23:34 +000083 const bool Escape = std::strpbrk(Arg, "\"\\$");
84
85 if (!Quote && !Escape) {
86 OS << Arg;
87 return;
88 }
89
90 // Quote and escape. This isn't really complete, but good enough.
91 OS << '"';
92 while (const char c = *Arg++) {
93 if (c == '"' || c == '\\' || c == '$')
94 OS << '\\';
95 OS << c;
96 }
97 OS << '"';
98}
99
Stephen Hines176edba2014-12-01 14:53:08 -0800100void Command::writeResponseFile(raw_ostream &OS) const {
101 // In a file list, we only write the set of inputs to the response file
102 if (Creator.getResponseFilesSupport() == Tool::RF_FileList) {
103 for (const char *Arg : InputFileList) {
104 OS << Arg << '\n';
105 }
106 return;
107 }
108
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800109 // In regular response files, we send all arguments to the response file.
110 // Wrapping all arguments in double quotes ensures that both Unix tools and
111 // Windows tools understand the response file.
Stephen Hines176edba2014-12-01 14:53:08 -0800112 for (const char *Arg : Arguments) {
113 OS << '"';
114
115 for (; *Arg != '\0'; Arg++) {
116 if (*Arg == '\"' || *Arg == '\\') {
117 OS << '\\';
118 }
119 OS << *Arg;
120 }
121
122 OS << "\" ";
123 }
124}
125
126void Command::buildArgvForResponseFile(
127 llvm::SmallVectorImpl<const char *> &Out) const {
128 // When not a file list, all arguments are sent to the response file.
129 // This leaves us to set the argv to a single parameter, requesting the tool
130 // to read the response file.
131 if (Creator.getResponseFilesSupport() != Tool::RF_FileList) {
132 Out.push_back(Executable);
133 Out.push_back(ResponseFileFlag.c_str());
134 return;
135 }
136
137 llvm::StringSet<> Inputs;
138 for (const char *InputName : InputFileList)
139 Inputs.insert(InputName);
140 Out.push_back(Executable);
141 // In a file list, build args vector ignoring parameters that will go in the
142 // response file (elements of the InputFileList vector)
143 bool FirstInput = true;
144 for (const char *Arg : Arguments) {
145 if (Inputs.count(Arg) == 0) {
146 Out.push_back(Arg);
147 } else if (FirstInput) {
148 FirstInput = false;
149 Out.push_back(Creator.getResponseFileFlag());
150 Out.push_back(ResponseFile);
151 }
152 }
153}
154
Hans Wennborgfc338972013-09-12 18:23:34 +0000155void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Stephen Hines176edba2014-12-01 14:53:08 -0800156 CrashReportInfo *CrashInfo) const {
157 // Always quote the exe.
158 OS << ' ';
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800159 printArg(OS, Executable, /*Quote=*/true);
Hans Wennborgfc338972013-09-12 18:23:34 +0000160
Stephen Hines176edba2014-12-01 14:53:08 -0800161 llvm::ArrayRef<const char *> Args = Arguments;
162 llvm::SmallVector<const char *, 128> ArgsRespFile;
163 if (ResponseFile != nullptr) {
164 buildArgvForResponseFile(ArgsRespFile);
165 Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
166 }
Hans Wennborgfc338972013-09-12 18:23:34 +0000167
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700168 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
Stephen Hines176edba2014-12-01 14:53:08 -0800169 for (size_t i = 0, e = Args.size(); i < e; ++i) {
170 const char *const Arg = Args[i];
171
172 if (CrashInfo) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700173 if (int Skip = skipArgs(Arg, HaveCrashVFS)) {
Hans Wennborgfc338972013-09-12 18:23:34 +0000174 i += Skip - 1;
175 continue;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800176 }
177 auto Found = std::find_if(InputFilenames.begin(), InputFilenames.end(),
178 [&Arg](StringRef IF) { return IF == Arg; });
179 if (Found != InputFilenames.end() &&
180 (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
Stephen Hines176edba2014-12-01 14:53:08 -0800181 // Replace the input file name with the crashinfo's file name.
182 OS << ' ';
183 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800184 printArg(OS, ShortName.str().c_str(), Quote);
Stephen Hines176edba2014-12-01 14:53:08 -0800185 continue;
Hans Wennborgfc338972013-09-12 18:23:34 +0000186 }
187 }
188
189 OS << ' ';
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800190 printArg(OS, Arg, Quote);
Hans Wennborgfc338972013-09-12 18:23:34 +0000191 }
Stephen Hines176edba2014-12-01 14:53:08 -0800192
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700193 if (CrashInfo && HaveCrashVFS) {
Stephen Hines176edba2014-12-01 14:53:08 -0800194 OS << ' ';
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800195 printArg(OS, "-ivfsoverlay", Quote);
Stephen Hines176edba2014-12-01 14:53:08 -0800196 OS << ' ';
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800197 printArg(OS, CrashInfo->VFSPath.str().c_str(), Quote);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700198
199 // Insert -fmodules-cache-path and use the relative module directory
200 // <name>.cache/vfs/modules where we already dumped the modules.
201 SmallString<128> RelModCacheDir = llvm::sys::path::parent_path(
202 llvm::sys::path::parent_path(CrashInfo->VFSPath));
203 llvm::sys::path::append(RelModCacheDir, "modules");
204
205 std::string ModCachePath = "-fmodules-cache-path=";
206 ModCachePath.append(RelModCacheDir.c_str());
207
208 OS << ' ';
209 printArg(OS, ModCachePath.c_str(), Quote);
Stephen Hines176edba2014-12-01 14:53:08 -0800210 }
211
212 if (ResponseFile != nullptr) {
213 OS << "\n Arguments passed via response file:\n";
214 writeResponseFile(OS);
215 // Avoiding duplicated newline terminator, since FileLists are
216 // newline-separated.
217 if (Creator.getResponseFilesSupport() != Tool::RF_FileList)
218 OS << "\n";
219 OS << " (end of response file)";
220 }
221
Hans Wennborgfc338972013-09-12 18:23:34 +0000222 OS << Terminator;
223}
224
Stephen Hines176edba2014-12-01 14:53:08 -0800225void Command::setResponseFile(const char *FileName) {
226 ResponseFile = FileName;
227 ResponseFileFlag = Creator.getResponseFileFlag();
228 ResponseFileFlag += FileName;
229}
230
Hans Wennborg64228b62013-09-18 00:41:15 +0000231int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
Hans Wennborgaaaa2a12013-09-12 18:35:08 +0000232 bool *ExecutionFailed) const {
233 SmallVector<const char*, 128> Argv;
Stephen Hines176edba2014-12-01 14:53:08 -0800234
235 if (ResponseFile == nullptr) {
236 Argv.push_back(Executable);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700237 Argv.append(Arguments.begin(), Arguments.end());
Stephen Hines176edba2014-12-01 14:53:08 -0800238 Argv.push_back(nullptr);
239
240 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
241 Redirects, /*secondsToWait*/ 0,
242 /*memoryLimit*/ 0, ErrMsg,
243 ExecutionFailed);
244 }
245
246 // We need to put arguments in a response file (command is too large)
247 // Open stream to store the response file contents
248 std::string RespContents;
249 llvm::raw_string_ostream SS(RespContents);
250
251 // Write file contents and build the Argv vector
252 writeResponseFile(SS);
253 buildArgvForResponseFile(Argv);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700254 Argv.push_back(nullptr);
Stephen Hines176edba2014-12-01 14:53:08 -0800255 SS.flush();
256
257 // Save the response file in the appropriate encoding
258 if (std::error_code EC = writeFileWithEncoding(
259 ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
260 if (ErrMsg)
261 *ErrMsg = EC.message();
262 if (ExecutionFailed)
263 *ExecutionFailed = true;
264 return -1;
265 }
Hans Wennborgaaaa2a12013-09-12 18:35:08 +0000266
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700267 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
Hans Wennborgaaaa2a12013-09-12 18:35:08 +0000268 Redirects, /*secondsToWait*/ 0,
269 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
270}
271
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000272FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
273 const char *Executable_,
274 const ArgStringList &Arguments_,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800275 ArrayRef<InputInfo> Inputs,
Stephen Hines176edba2014-12-01 14:53:08 -0800276 std::unique_ptr<Command> Fallback_)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800277 : Command(Source_, Creator_, Executable_, Arguments_, Inputs),
Stephen Hines176edba2014-12-01 14:53:08 -0800278 Fallback(std::move(Fallback_)) {}
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000279
280void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
Stephen Hines176edba2014-12-01 14:53:08 -0800281 bool Quote, CrashReportInfo *CrashInfo) const {
282 Command::Print(OS, "", Quote, CrashInfo);
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000283 OS << " ||";
Stephen Hines176edba2014-12-01 14:53:08 -0800284 Fallback->Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000285}
286
287static bool ShouldFallback(int ExitCode) {
288 // FIXME: We really just want to fall back for internal errors, such
289 // as when some symbol cannot be mangled, when we should be able to
290 // parse something but can't, etc.
291 return ExitCode != 0;
292}
293
294int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg,
295 bool *ExecutionFailed) const {
296 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
297 if (!ShouldFallback(PrimaryStatus))
298 return PrimaryStatus;
299
300 // Clear ExecutionFailed and ErrMsg before falling back.
301 if (ErrMsg)
302 ErrMsg->clear();
303 if (ExecutionFailed)
304 *ExecutionFailed = false;
305
Stephen Hines651f13c2014-04-23 16:59:28 -0700306 const Driver &D = getCreator().getToolChain().getDriver();
307 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
308
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000309 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
310 return SecondaryStatus;
311}
312
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700313ForceSuccessCommand::ForceSuccessCommand(const Action &Source_,
314 const Tool &Creator_,
315 const char *Executable_,
316 const ArgStringList &Arguments_,
317 ArrayRef<InputInfo> Inputs)
318 : Command(Source_, Creator_, Executable_, Arguments_, Inputs) {}
319
320void ForceSuccessCommand::Print(raw_ostream &OS, const char *Terminator,
321 bool Quote, CrashReportInfo *CrashInfo) const {
322 Command::Print(OS, "", Quote, CrashInfo);
323 OS << " || (exit 0)" << Terminator;
324}
325
326int ForceSuccessCommand::Execute(const StringRef **Redirects,
327 std::string *ErrMsg,
328 bool *ExecutionFailed) const {
329 int Status = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
330 (void)Status;
331 if (ExecutionFailed)
332 *ExecutionFailed = false;
333 return 0;
334}
335
Hans Wennborgfc338972013-09-12 18:23:34 +0000336void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Stephen Hines176edba2014-12-01 14:53:08 -0800337 CrashReportInfo *CrashInfo) const {
338 for (const auto &Job : *this)
339 Job.Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgfc338972013-09-12 18:23:34 +0000340}
341
Stephen Hines176edba2014-12-01 14:53:08 -0800342void JobList::clear() { Jobs.clear(); }