blob: 6d18a41cad0879d8f7daed5871e5d77081c240fa [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
Hans Wennborgc5afd062014-02-18 21:42:51 +000010#include "clang/Driver/Driver.h"
11#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar313c2912009-03-13 23:36:33 +000012#include "clang/Driver/Job.h"
Hans Wennborgc5afd062014-02-18 21:42:51 +000013#include "clang/Driver/Tool.h"
14#include "clang/Driver/ToolChain.h"
Reid Kleckner0290c9c2014-09-15 17:45:39 +000015#include "llvm/ADT/ArrayRef.h"
Chad Rosierbe10f982011-08-02 17:58:04 +000016#include "llvm/ADT/STLExtras.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000017#include "llvm/ADT/StringRef.h"
Reid Kleckner0290c9c2014-09-15 17:45:39 +000018#include "llvm/ADT/StringSet.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000019#include "llvm/ADT/StringSwitch.h"
Hans Wennborge8677ef2013-09-12 18:35:08 +000020#include "llvm/Support/Program.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000021#include "llvm/Support/raw_ostream.h"
Daniel Dunbar313c2912009-03-13 23:36:33 +000022#include <cassert>
23using namespace clang::driver;
Hans Wennborgb212b342013-09-12 18:23:34 +000024using llvm::raw_ostream;
25using llvm::StringRef;
Reid Kleckner0290c9c2014-09-15 17:45:39 +000026using llvm::ArrayRef;
Daniel Dunbar313c2912009-03-13 23:36:33 +000027
28Job::~Job() {}
29
Daniel Dunbar120c77e2009-12-02 03:23:25 +000030Command::Command(const Action &_Source, const Tool &_Creator,
Hans Wennborg0daa12252013-09-18 00:21:51 +000031 const char *_Executable,
Hans Wennborge693e842013-09-18 00:41:15 +000032 const ArgStringList &_Arguments)
Reid Kleckner898229a2013-06-14 17:17:23 +000033 : Job(CommandClass), Source(_Source), Creator(_Creator),
Reid Kleckner0290c9c2014-09-15 17:45:39 +000034 Executable(_Executable), Arguments(_Arguments),
35 ResponseFile(nullptr) {}
Daniel Dunbar313c2912009-03-13 23:36:33 +000036
Justin Bogner0cb14752015-03-12 00:52:56 +000037static int skipArgs(const char *Flag, bool HaveCrashVFS) {
Hans Wennborgb212b342013-09-12 18:23:34 +000038 // These flags are all of the form -Flag <Arg> and are treated as two
39 // arguments. Therefore, we need to skip the flag and the next argument.
40 bool Res = llvm::StringSwitch<bool>(Flag)
41 .Cases("-I", "-MF", "-MT", "-MQ", true)
42 .Cases("-o", "-coverage-file", "-dependency-file", true)
43 .Cases("-fdebug-compilation-dir", "-idirafter", true)
44 .Cases("-include", "-include-pch", "-internal-isystem", true)
45 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
Justin Bogner0cb14752015-03-12 00:52:56 +000046 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
Hans Wennborgb212b342013-09-12 18:23:34 +000047 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
Justin Bogner61c0e432014-06-22 20:35:10 +000048 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
Justin Bogner0cb14752015-03-12 00:52:56 +000049 // Some include flags shouldn't be skipped if we have a crash VFS
50 .Case("-isysroot", !HaveCrashVFS)
Hans Wennborgb212b342013-09-12 18:23:34 +000051 .Default(false);
52
53 // Match found.
54 if (Res)
55 return 2;
56
57 // The remaining flags are treated as a single argument.
58
59 // These flags are all of the form -Flag and have no second argument.
60 Res = llvm::StringSwitch<bool>(Flag)
61 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
62 .Case("-MMD", true)
63 .Default(false);
64
65 // Match found.
66 if (Res)
67 return 1;
68
69 // These flags are treated as a single argument (e.g., -F<Dir>).
70 StringRef FlagRef(Flag);
Justin Bognerf893a652014-04-22 21:30:17 +000071 if (FlagRef.startswith("-F") || FlagRef.startswith("-I") ||
72 FlagRef.startswith("-fmodules-cache-path="))
Hans Wennborgb212b342013-09-12 18:23:34 +000073 return 1;
74
75 return 0;
76}
77
Hans Wennborgb212b342013-09-12 18:23:34 +000078static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) {
79 const bool Escape = std::strpbrk(Arg, "\"\\$");
80
81 if (!Quote && !Escape) {
82 OS << Arg;
83 return;
84 }
85
86 // Quote and escape. This isn't really complete, but good enough.
87 OS << '"';
88 while (const char c = *Arg++) {
89 if (c == '"' || c == '\\' || c == '$')
90 OS << '\\';
91 OS << c;
92 }
93 OS << '"';
94}
95
Reid Kleckner0290c9c2014-09-15 17:45:39 +000096void Command::writeResponseFile(raw_ostream &OS) const {
97 // In a file list, we only write the set of inputs to the response file
98 if (Creator.getResponseFilesSupport() == Tool::RF_FileList) {
99 for (const char *Arg : InputFileList) {
100 OS << Arg << '\n';
101 }
102 return;
103 }
104
105 // In regular response files, we send all arguments to the response file
106 for (const char *Arg : Arguments) {
107 OS << '"';
108
109 for (; *Arg != '\0'; Arg++) {
110 if (*Arg == '\"' || *Arg == '\\') {
111 OS << '\\';
112 }
113 OS << *Arg;
114 }
115
116 OS << "\" ";
117 }
118}
119
120void Command::buildArgvForResponseFile(
121 llvm::SmallVectorImpl<const char *> &Out) const {
122 // When not a file list, all arguments are sent to the response file.
123 // This leaves us to set the argv to a single parameter, requesting the tool
124 // to read the response file.
125 if (Creator.getResponseFilesSupport() != Tool::RF_FileList) {
126 Out.push_back(Executable);
127 Out.push_back(ResponseFileFlag.c_str());
128 return;
129 }
130
131 llvm::StringSet<> Inputs;
132 for (const char *InputName : InputFileList)
133 Inputs.insert(InputName);
134 Out.push_back(Executable);
135 // In a file list, build args vector ignoring parameters that will go in the
136 // response file (elements of the InputFileList vector)
137 bool FirstInput = true;
138 for (const char *Arg : Arguments) {
139 if (Inputs.count(Arg) == 0) {
140 Out.push_back(Arg);
141 } else if (FirstInput) {
142 FirstInput = false;
143 Out.push_back(Creator.getResponseFileFlag());
144 Out.push_back(ResponseFile);
145 }
146 }
147}
148
Hans Wennborgb212b342013-09-12 18:23:34 +0000149void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000150 CrashReportInfo *CrashInfo) const {
Reid Kleckner822434d2014-08-05 20:49:12 +0000151 // Always quote the exe.
Reid Kleckner4f1fc352014-08-07 00:05:00 +0000152 OS << ' ';
Reid Kleckner822434d2014-08-05 20:49:12 +0000153 PrintArg(OS, Executable, /*Quote=*/true);
Hans Wennborgb212b342013-09-12 18:23:34 +0000154
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000155 llvm::ArrayRef<const char *> Args = Arguments;
156 llvm::SmallVector<const char *, 128> ArgsRespFile;
157 if (ResponseFile != nullptr) {
158 buildArgvForResponseFile(ArgsRespFile);
159 Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
160 }
161
Justin Bogner25645152014-10-21 17:24:44 +0000162 StringRef MainFilename;
163 // We'll need the argument to -main-file-name to find the input file name.
164 if (CrashInfo)
165 for (size_t I = 0, E = Args.size(); I + 1 < E; ++I)
166 if (StringRef(Args[I]).equals("-main-file-name"))
167 MainFilename = Args[I + 1];
168
Justin Bogner0cb14752015-03-12 00:52:56 +0000169 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000170 for (size_t i = 0, e = Args.size(); i < e; ++i) {
171 const char *const Arg = Args[i];
Hans Wennborgb212b342013-09-12 18:23:34 +0000172
Justin Bogner25645152014-10-21 17:24:44 +0000173 if (CrashInfo) {
Justin Bogner0cb14752015-03-12 00:52:56 +0000174 if (int Skip = skipArgs(Arg, HaveCrashVFS)) {
Hans Wennborgb212b342013-09-12 18:23:34 +0000175 i += Skip - 1;
176 continue;
Justin Bogner25645152014-10-21 17:24:44 +0000177 } else if (llvm::sys::path::filename(Arg) == MainFilename &&
178 (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
179 // Replace the input file name with the crashinfo's file name.
180 OS << ' ';
181 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
182 PrintArg(OS, ShortName.str().c_str(), Quote);
183 continue;
Hans Wennborgb212b342013-09-12 18:23:34 +0000184 }
185 }
186
187 OS << ' ';
188 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 << ' ';
193 PrintArg(OS, "-ivfsoverlay", Quote);
194 OS << ' ';
195 PrintArg(OS, CrashInfo->VFSPath.str().c_str(), Quote);
196 }
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_,
David Blaikiec11bf802014-09-04 16:04:28 +0000261 std::unique_ptr<Command> Fallback_)
262 : Command(Source_, Creator_, Executable_, Arguments_),
263 Fallback(std::move(Fallback_)) {}
Hans Wennborg87cfa712013-09-19 20:32:16 +0000264
265void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
Justin Bogner25645152014-10-21 17:24:44 +0000266 bool Quote, CrashReportInfo *CrashInfo) const {
267 Command::Print(OS, "", Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000268 OS << " ||";
Justin Bogner25645152014-10-21 17:24:44 +0000269 Fallback->Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000270}
271
272static bool ShouldFallback(int ExitCode) {
273 // FIXME: We really just want to fall back for internal errors, such
274 // as when some symbol cannot be mangled, when we should be able to
275 // parse something but can't, etc.
276 return ExitCode != 0;
277}
278
279int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg,
280 bool *ExecutionFailed) const {
281 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
282 if (!ShouldFallback(PrimaryStatus))
283 return PrimaryStatus;
284
285 // Clear ExecutionFailed and ErrMsg before falling back.
286 if (ErrMsg)
287 ErrMsg->clear();
288 if (ExecutionFailed)
289 *ExecutionFailed = false;
290
Hans Wennborgc5afd062014-02-18 21:42:51 +0000291 const Driver &D = getCreator().getToolChain().getDriver();
Hans Wennborg897a69b2014-02-19 02:10:19 +0000292 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
Hans Wennborgc5afd062014-02-18 21:42:51 +0000293
Hans Wennborg87cfa712013-09-19 20:32:16 +0000294 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
295 return SecondaryStatus;
296}
297
Daniel Dunbar313c2912009-03-13 23:36:33 +0000298JobList::JobList() : Job(JobListClass) {}
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +0000299
Hans Wennborgb212b342013-09-12 18:23:34 +0000300void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000301 CrashReportInfo *CrashInfo) const {
Justin Bogneraab97922014-10-03 01:04:53 +0000302 for (const auto &Job : *this)
Justin Bogner25645152014-10-21 17:24:44 +0000303 Job.Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgb212b342013-09-12 18:23:34 +0000304}
305
David Blaikiec11bf802014-09-04 16:04:28 +0000306void JobList::clear() { Jobs.clear(); }