blob: 4b5b839e524d4c60c1511047231e142f9b68a401 [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
Justin Bogner94817612015-07-02 22:52:04 +000030Command::Command(const Action &Source, const Tool &Creator,
31 const char *Executable, const ArgStringList &Arguments)
32 : Job(CommandClass), Source(Source), Creator(Creator),
33 Executable(Executable), Arguments(Arguments), ResponseFile(nullptr) {}
Daniel Dunbar313c2912009-03-13 23:36:33 +000034
Justin Bogner0cb14752015-03-12 00:52:56 +000035static int skipArgs(const char *Flag, bool HaveCrashVFS) {
Hans Wennborgb212b342013-09-12 18:23:34 +000036 // These flags are all of the form -Flag <Arg> and are treated as two
37 // arguments. Therefore, we need to skip the flag and the next argument.
38 bool Res = llvm::StringSwitch<bool>(Flag)
39 .Cases("-I", "-MF", "-MT", "-MQ", true)
40 .Cases("-o", "-coverage-file", "-dependency-file", true)
41 .Cases("-fdebug-compilation-dir", "-idirafter", true)
42 .Cases("-include", "-include-pch", "-internal-isystem", true)
43 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
Justin Bogner0cb14752015-03-12 00:52:56 +000044 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
Hans Wennborgb212b342013-09-12 18:23:34 +000045 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
Justin Bogner61c0e432014-06-22 20:35:10 +000046 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
Justin Bogner0cb14752015-03-12 00:52:56 +000047 // Some include flags shouldn't be skipped if we have a crash VFS
48 .Case("-isysroot", !HaveCrashVFS)
Hans Wennborgb212b342013-09-12 18:23:34 +000049 .Default(false);
50
51 // Match found.
52 if (Res)
53 return 2;
54
55 // The remaining flags are treated as a single argument.
56
57 // These flags are all of the form -Flag and have no second argument.
58 Res = llvm::StringSwitch<bool>(Flag)
59 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
60 .Case("-MMD", true)
61 .Default(false);
62
63 // Match found.
64 if (Res)
65 return 1;
66
67 // These flags are treated as a single argument (e.g., -F<Dir>).
68 StringRef FlagRef(Flag);
Justin Bognerf893a652014-04-22 21:30:17 +000069 if (FlagRef.startswith("-F") || FlagRef.startswith("-I") ||
70 FlagRef.startswith("-fmodules-cache-path="))
Hans Wennborgb212b342013-09-12 18:23:34 +000071 return 1;
72
73 return 0;
74}
75
Hans Wennborgb212b342013-09-12 18:23:34 +000076static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) {
77 const bool Escape = std::strpbrk(Arg, "\"\\$");
78
79 if (!Quote && !Escape) {
80 OS << Arg;
81 return;
82 }
83
84 // Quote and escape. This isn't really complete, but good enough.
85 OS << '"';
86 while (const char c = *Arg++) {
87 if (c == '"' || c == '\\' || c == '$')
88 OS << '\\';
89 OS << c;
90 }
91 OS << '"';
92}
93
Reid Kleckner0290c9c2014-09-15 17:45:39 +000094void Command::writeResponseFile(raw_ostream &OS) const {
95 // In a file list, we only write the set of inputs to the response file
96 if (Creator.getResponseFilesSupport() == Tool::RF_FileList) {
97 for (const char *Arg : InputFileList) {
98 OS << Arg << '\n';
99 }
100 return;
101 }
102
103 // In regular response files, we send all arguments to the response file
104 for (const char *Arg : Arguments) {
105 OS << '"';
106
107 for (; *Arg != '\0'; Arg++) {
108 if (*Arg == '\"' || *Arg == '\\') {
109 OS << '\\';
110 }
111 OS << *Arg;
112 }
113
114 OS << "\" ";
115 }
116}
117
118void Command::buildArgvForResponseFile(
119 llvm::SmallVectorImpl<const char *> &Out) const {
120 // When not a file list, all arguments are sent to the response file.
121 // This leaves us to set the argv to a single parameter, requesting the tool
122 // to read the response file.
123 if (Creator.getResponseFilesSupport() != Tool::RF_FileList) {
124 Out.push_back(Executable);
125 Out.push_back(ResponseFileFlag.c_str());
126 return;
127 }
128
129 llvm::StringSet<> Inputs;
130 for (const char *InputName : InputFileList)
131 Inputs.insert(InputName);
132 Out.push_back(Executable);
133 // In a file list, build args vector ignoring parameters that will go in the
134 // response file (elements of the InputFileList vector)
135 bool FirstInput = true;
136 for (const char *Arg : Arguments) {
137 if (Inputs.count(Arg) == 0) {
138 Out.push_back(Arg);
139 } else if (FirstInput) {
140 FirstInput = false;
141 Out.push_back(Creator.getResponseFileFlag());
142 Out.push_back(ResponseFile);
143 }
144 }
145}
146
Hans Wennborgb212b342013-09-12 18:23:34 +0000147void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000148 CrashReportInfo *CrashInfo) const {
Reid Kleckner822434d2014-08-05 20:49:12 +0000149 // Always quote the exe.
Reid Kleckner4f1fc352014-08-07 00:05:00 +0000150 OS << ' ';
Reid Kleckner822434d2014-08-05 20:49:12 +0000151 PrintArg(OS, Executable, /*Quote=*/true);
Hans Wennborgb212b342013-09-12 18:23:34 +0000152
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000153 llvm::ArrayRef<const char *> Args = Arguments;
154 llvm::SmallVector<const char *, 128> ArgsRespFile;
155 if (ResponseFile != nullptr) {
156 buildArgvForResponseFile(ArgsRespFile);
157 Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
158 }
159
Justin Bogner25645152014-10-21 17:24:44 +0000160 StringRef MainFilename;
161 // We'll need the argument to -main-file-name to find the input file name.
162 if (CrashInfo)
163 for (size_t I = 0, E = Args.size(); I + 1 < E; ++I)
164 if (StringRef(Args[I]).equals("-main-file-name"))
165 MainFilename = Args[I + 1];
166
Justin Bogner0cb14752015-03-12 00:52:56 +0000167 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000168 for (size_t i = 0, e = Args.size(); i < e; ++i) {
169 const char *const Arg = Args[i];
Hans Wennborgb212b342013-09-12 18:23:34 +0000170
Justin Bogner25645152014-10-21 17:24:44 +0000171 if (CrashInfo) {
Justin Bogner0cb14752015-03-12 00:52:56 +0000172 if (int Skip = skipArgs(Arg, HaveCrashVFS)) {
Hans Wennborgb212b342013-09-12 18:23:34 +0000173 i += Skip - 1;
174 continue;
Justin Bogner25645152014-10-21 17:24:44 +0000175 } else if (llvm::sys::path::filename(Arg) == MainFilename &&
176 (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
177 // Replace the input file name with the crashinfo's file name.
178 OS << ' ';
179 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
180 PrintArg(OS, ShortName.str().c_str(), Quote);
181 continue;
Hans Wennborgb212b342013-09-12 18:23:34 +0000182 }
183 }
184
185 OS << ' ';
186 PrintArg(OS, Arg, Quote);
Hans Wennborgb212b342013-09-12 18:23:34 +0000187 }
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000188
Justin Bogner0cb14752015-03-12 00:52:56 +0000189 if (CrashInfo && HaveCrashVFS) {
Justin Bogner25645152014-10-21 17:24:44 +0000190 OS << ' ';
191 PrintArg(OS, "-ivfsoverlay", Quote);
192 OS << ' ';
193 PrintArg(OS, CrashInfo->VFSPath.str().c_str(), Quote);
194 }
195
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000196 if (ResponseFile != nullptr) {
197 OS << "\n Arguments passed via response file:\n";
198 writeResponseFile(OS);
199 // Avoiding duplicated newline terminator, since FileLists are
200 // newline-separated.
201 if (Creator.getResponseFilesSupport() != Tool::RF_FileList)
202 OS << "\n";
203 OS << " (end of response file)";
204 }
205
Hans Wennborgb212b342013-09-12 18:23:34 +0000206 OS << Terminator;
207}
208
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000209void Command::setResponseFile(const char *FileName) {
210 ResponseFile = FileName;
211 ResponseFileFlag = Creator.getResponseFileFlag();
212 ResponseFileFlag += FileName;
213}
214
Hans Wennborge693e842013-09-18 00:41:15 +0000215int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000216 bool *ExecutionFailed) const {
217 SmallVector<const char*, 128> Argv;
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000218
219 if (ResponseFile == nullptr) {
220 Argv.push_back(Executable);
Benjamin Kramerf9890422015-02-17 16:48:30 +0000221 Argv.append(Arguments.begin(), Arguments.end());
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000222 Argv.push_back(nullptr);
223
224 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
225 Redirects, /*secondsToWait*/ 0,
226 /*memoryLimit*/ 0, ErrMsg,
227 ExecutionFailed);
228 }
229
230 // We need to put arguments in a response file (command is too large)
231 // Open stream to store the response file contents
232 std::string RespContents;
233 llvm::raw_string_ostream SS(RespContents);
234
235 // Write file contents and build the Argv vector
236 writeResponseFile(SS);
237 buildArgvForResponseFile(Argv);
Craig Topper92fc2df2014-05-17 16:56:41 +0000238 Argv.push_back(nullptr);
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000239 SS.flush();
240
241 // Save the response file in the appropriate encoding
242 if (std::error_code EC = writeFileWithEncoding(
243 ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
244 if (ErrMsg)
245 *ErrMsg = EC.message();
246 if (ExecutionFailed)
247 *ExecutionFailed = true;
248 return -1;
249 }
Hans Wennborge8677ef2013-09-12 18:35:08 +0000250
Craig Topper92fc2df2014-05-17 16:56:41 +0000251 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000252 Redirects, /*secondsToWait*/ 0,
253 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
254}
255
Hans Wennborg87cfa712013-09-19 20:32:16 +0000256FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
257 const char *Executable_,
258 const ArgStringList &Arguments_,
David Blaikiec11bf802014-09-04 16:04:28 +0000259 std::unique_ptr<Command> Fallback_)
260 : Command(Source_, Creator_, Executable_, Arguments_),
261 Fallback(std::move(Fallback_)) {}
Hans Wennborg87cfa712013-09-19 20:32:16 +0000262
263void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
Justin Bogner25645152014-10-21 17:24:44 +0000264 bool Quote, CrashReportInfo *CrashInfo) const {
265 Command::Print(OS, "", Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000266 OS << " ||";
Justin Bogner25645152014-10-21 17:24:44 +0000267 Fallback->Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000268}
269
270static bool ShouldFallback(int ExitCode) {
271 // FIXME: We really just want to fall back for internal errors, such
272 // as when some symbol cannot be mangled, when we should be able to
273 // parse something but can't, etc.
274 return ExitCode != 0;
275}
276
277int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg,
278 bool *ExecutionFailed) const {
279 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
280 if (!ShouldFallback(PrimaryStatus))
281 return PrimaryStatus;
282
283 // Clear ExecutionFailed and ErrMsg before falling back.
284 if (ErrMsg)
285 ErrMsg->clear();
286 if (ExecutionFailed)
287 *ExecutionFailed = false;
288
Hans Wennborgc5afd062014-02-18 21:42:51 +0000289 const Driver &D = getCreator().getToolChain().getDriver();
Hans Wennborg897a69b2014-02-19 02:10:19 +0000290 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
Hans Wennborgc5afd062014-02-18 21:42:51 +0000291
Hans Wennborg87cfa712013-09-19 20:32:16 +0000292 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
293 return SecondaryStatus;
294}
295
Daniel Dunbar313c2912009-03-13 23:36:33 +0000296JobList::JobList() : Job(JobListClass) {}
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +0000297
Hans Wennborgb212b342013-09-12 18:23:34 +0000298void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000299 CrashReportInfo *CrashInfo) const {
Justin Bogneraab97922014-10-03 01:04:53 +0000300 for (const auto &Job : *this)
Justin Bogner25645152014-10-21 17:24:44 +0000301 Job.Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgb212b342013-09-12 18:23:34 +0000302}
303
David Blaikiec11bf802014-09-04 16:04:28 +0000304void JobList::clear() { Jobs.clear(); }