blob: 34d38b904cc7852510300a0d7229bde2f212335a [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
Hans Wennborgb212b342013-09-12 18:23:34 +000037static int skipArgs(const char *Flag) {
38 // 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)
46 .Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true)
47 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
Justin Bogner61c0e432014-06-22 20:35:10 +000048 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
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
76static bool quoteNextArg(const char *flag) {
77 return llvm::StringSwitch<bool>(flag)
78 .Case("-D", true)
79 .Default(false);
80}
81
82static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) {
83 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
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000100void 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
109 // In regular response files, we send all arguments to the response file
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
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 << ' ';
Reid Kleckner822434d2014-08-05 20:49:12 +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 Bogner25645152014-10-21 17:24:44 +0000166 StringRef MainFilename;
167 // We'll need the argument to -main-file-name to find the input file name.
168 if (CrashInfo)
169 for (size_t I = 0, E = Args.size(); I + 1 < E; ++I)
170 if (StringRef(Args[I]).equals("-main-file-name"))
171 MainFilename = Args[I + 1];
172
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000173 for (size_t i = 0, e = Args.size(); i < e; ++i) {
174 const char *const Arg = Args[i];
Hans Wennborgb212b342013-09-12 18:23:34 +0000175
Justin Bogner25645152014-10-21 17:24:44 +0000176 if (CrashInfo) {
Hans Wennborgb212b342013-09-12 18:23:34 +0000177 if (int Skip = skipArgs(Arg)) {
178 i += Skip - 1;
179 continue;
Justin Bogner25645152014-10-21 17:24:44 +0000180 } else if (llvm::sys::path::filename(Arg) == MainFilename &&
181 (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
182 // Replace the input file name with the crashinfo's file name.
183 OS << ' ';
184 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
185 PrintArg(OS, ShortName.str().c_str(), Quote);
186 continue;
Hans Wennborgb212b342013-09-12 18:23:34 +0000187 }
188 }
189
190 OS << ' ';
191 PrintArg(OS, Arg, Quote);
192
Justin Bogner25645152014-10-21 17:24:44 +0000193 if (CrashInfo && quoteNextArg(Arg) && i + 1 < e) {
Hans Wennborgb212b342013-09-12 18:23:34 +0000194 OS << ' ';
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000195 PrintArg(OS, Args[++i], true);
Hans Wennborgb212b342013-09-12 18:23:34 +0000196 }
197 }
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000198
Justin Bogner25645152014-10-21 17:24:44 +0000199 if (CrashInfo && !CrashInfo->VFSPath.empty()) {
200 OS << ' ';
201 PrintArg(OS, "-ivfsoverlay", Quote);
202 OS << ' ';
203 PrintArg(OS, CrashInfo->VFSPath.str().c_str(), Quote);
204 }
205
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000206 if (ResponseFile != nullptr) {
207 OS << "\n Arguments passed via response file:\n";
208 writeResponseFile(OS);
209 // Avoiding duplicated newline terminator, since FileLists are
210 // newline-separated.
211 if (Creator.getResponseFilesSupport() != Tool::RF_FileList)
212 OS << "\n";
213 OS << " (end of response file)";
214 }
215
Hans Wennborgb212b342013-09-12 18:23:34 +0000216 OS << Terminator;
217}
218
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000219void Command::setResponseFile(const char *FileName) {
220 ResponseFile = FileName;
221 ResponseFileFlag = Creator.getResponseFileFlag();
222 ResponseFileFlag += FileName;
223}
224
Hans Wennborge693e842013-09-18 00:41:15 +0000225int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000226 bool *ExecutionFailed) const {
227 SmallVector<const char*, 128> Argv;
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000228
229 if (ResponseFile == nullptr) {
230 Argv.push_back(Executable);
231 for (size_t i = 0, e = Arguments.size(); i != e; ++i)
232 Argv.push_back(Arguments[i]);
233 Argv.push_back(nullptr);
234
235 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
236 Redirects, /*secondsToWait*/ 0,
237 /*memoryLimit*/ 0, ErrMsg,
238 ExecutionFailed);
239 }
240
241 // We need to put arguments in a response file (command is too large)
242 // Open stream to store the response file contents
243 std::string RespContents;
244 llvm::raw_string_ostream SS(RespContents);
245
246 // Write file contents and build the Argv vector
247 writeResponseFile(SS);
248 buildArgvForResponseFile(Argv);
Craig Topper92fc2df2014-05-17 16:56:41 +0000249 Argv.push_back(nullptr);
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000250 SS.flush();
251
252 // Save the response file in the appropriate encoding
253 if (std::error_code EC = writeFileWithEncoding(
254 ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
255 if (ErrMsg)
256 *ErrMsg = EC.message();
257 if (ExecutionFailed)
258 *ExecutionFailed = true;
259 return -1;
260 }
Hans Wennborge8677ef2013-09-12 18:35:08 +0000261
Craig Topper92fc2df2014-05-17 16:56:41 +0000262 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000263 Redirects, /*secondsToWait*/ 0,
264 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
265}
266
Hans Wennborg87cfa712013-09-19 20:32:16 +0000267FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
268 const char *Executable_,
269 const ArgStringList &Arguments_,
David Blaikiec11bf802014-09-04 16:04:28 +0000270 std::unique_ptr<Command> Fallback_)
271 : Command(Source_, Creator_, Executable_, Arguments_),
272 Fallback(std::move(Fallback_)) {}
Hans Wennborg87cfa712013-09-19 20:32:16 +0000273
274void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
Justin Bogner25645152014-10-21 17:24:44 +0000275 bool Quote, CrashReportInfo *CrashInfo) const {
276 Command::Print(OS, "", Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000277 OS << " ||";
Justin Bogner25645152014-10-21 17:24:44 +0000278 Fallback->Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000279}
280
281static bool ShouldFallback(int ExitCode) {
282 // FIXME: We really just want to fall back for internal errors, such
283 // as when some symbol cannot be mangled, when we should be able to
284 // parse something but can't, etc.
285 return ExitCode != 0;
286}
287
288int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg,
289 bool *ExecutionFailed) const {
290 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
291 if (!ShouldFallback(PrimaryStatus))
292 return PrimaryStatus;
293
294 // Clear ExecutionFailed and ErrMsg before falling back.
295 if (ErrMsg)
296 ErrMsg->clear();
297 if (ExecutionFailed)
298 *ExecutionFailed = false;
299
Hans Wennborgc5afd062014-02-18 21:42:51 +0000300 const Driver &D = getCreator().getToolChain().getDriver();
Hans Wennborg897a69b2014-02-19 02:10:19 +0000301 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
Hans Wennborgc5afd062014-02-18 21:42:51 +0000302
Hans Wennborg87cfa712013-09-19 20:32:16 +0000303 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
304 return SecondaryStatus;
305}
306
Daniel Dunbar313c2912009-03-13 23:36:33 +0000307JobList::JobList() : Job(JobListClass) {}
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +0000308
Hans Wennborgb212b342013-09-12 18:23:34 +0000309void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000310 CrashReportInfo *CrashInfo) const {
Justin Bogneraab97922014-10-03 01:04:53 +0000311 for (const auto &Job : *this)
Justin Bogner25645152014-10-21 17:24:44 +0000312 Job.Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgb212b342013-09-12 18:23:34 +0000313}
314
David Blaikiec11bf802014-09-04 16:04:28 +0000315void JobList::clear() { Jobs.clear(); }