blob: 6d18a41cad0879d8f7daed5871e5d77081c240fa [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
Stephen Hines651f13c2014-04-23 16:59:28 -070010#include "clang/Driver/Driver.h"
11#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar789e2202009-03-13 23:36:33 +000012#include "clang/Driver/Job.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070013#include "clang/Driver/Tool.h"
14#include "clang/Driver/ToolChain.h"
Stephen Hines176edba2014-12-01 14:53:08 -080015#include "llvm/ADT/ArrayRef.h"
Chad Rosier2b819102011-08-02 17:58:04 +000016#include "llvm/ADT/STLExtras.h"
Hans Wennborgfc338972013-09-12 18:23:34 +000017#include "llvm/ADT/StringRef.h"
Stephen Hines176edba2014-12-01 14:53:08 -080018#include "llvm/ADT/StringSet.h"
Hans Wennborgfc338972013-09-12 18:23:34 +000019#include "llvm/ADT/StringSwitch.h"
Hans Wennborgaaaa2a12013-09-12 18:35:08 +000020#include "llvm/Support/Program.h"
Hans Wennborgfc338972013-09-12 18:23:34 +000021#include "llvm/Support/raw_ostream.h"
Daniel Dunbar789e2202009-03-13 23:36:33 +000022#include <cassert>
23using namespace clang::driver;
Hans Wennborgfc338972013-09-12 18:23:34 +000024using llvm::raw_ostream;
25using llvm::StringRef;
Stephen Hines176edba2014-12-01 14:53:08 -080026using llvm::ArrayRef;
Daniel Dunbar789e2202009-03-13 23:36:33 +000027
28Job::~Job() {}
29
Daniel Dunbardaab7b12009-12-02 03:23:25 +000030Command::Command(const Action &_Source, const Tool &_Creator,
Hans Wennborg5c6ecf52013-09-18 00:21:51 +000031 const char *_Executable,
Hans Wennborg64228b62013-09-18 00:41:15 +000032 const ArgStringList &_Arguments)
Reid Klecknerb1e25a12013-06-14 17:17:23 +000033 : Job(CommandClass), Source(_Source), Creator(_Creator),
Stephen Hines176edba2014-12-01 14:53:08 -080034 Executable(_Executable), Arguments(_Arguments),
35 ResponseFile(nullptr) {}
Daniel Dunbar789e2202009-03-13 23:36:33 +000036
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070037static int skipArgs(const char *Flag, bool HaveCrashVFS) {
Hans Wennborgfc338972013-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)
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070046 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
Hans Wennborgfc338972013-09-12 18:23:34 +000047 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
Stephen Hinesc568f1e2014-07-21 00:47:37 -070048 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070049 // Some include flags shouldn't be skipped if we have a crash VFS
50 .Case("-isysroot", !HaveCrashVFS)
Hans Wennborgfc338972013-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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -070071 if (FlagRef.startswith("-F") || FlagRef.startswith("-I") ||
72 FlagRef.startswith("-fmodules-cache-path="))
Hans Wennborgfc338972013-09-12 18:23:34 +000073 return 1;
74
75 return 0;
76}
77
Hans Wennborgfc338972013-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
Stephen Hines176edba2014-12-01 14:53:08 -080096void 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 Wennborgfc338972013-09-12 18:23:34 +0000149void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Stephen Hines176edba2014-12-01 14:53:08 -0800150 CrashReportInfo *CrashInfo) const {
151 // Always quote the exe.
152 OS << ' ';
153 PrintArg(OS, Executable, /*Quote=*/true);
Hans Wennborgfc338972013-09-12 18:23:34 +0000154
Stephen Hines176edba2014-12-01 14:53:08 -0800155 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 }
Hans Wennborgfc338972013-09-12 18:23:34 +0000161
Stephen Hines176edba2014-12-01 14:53:08 -0800162 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
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700169 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
Stephen Hines176edba2014-12-01 14:53:08 -0800170 for (size_t i = 0, e = Args.size(); i < e; ++i) {
171 const char *const Arg = Args[i];
172
173 if (CrashInfo) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700174 if (int Skip = skipArgs(Arg, HaveCrashVFS)) {
Hans Wennborgfc338972013-09-12 18:23:34 +0000175 i += Skip - 1;
176 continue;
Stephen Hines176edba2014-12-01 14:53:08 -0800177 } 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 Wennborgfc338972013-09-12 18:23:34 +0000184 }
185 }
186
187 OS << ' ';
188 PrintArg(OS, Arg, Quote);
Hans Wennborgfc338972013-09-12 18:23:34 +0000189 }
Stephen Hines176edba2014-12-01 14:53:08 -0800190
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700191 if (CrashInfo && HaveCrashVFS) {
Stephen Hines176edba2014-12-01 14:53:08 -0800192 OS << ' ';
193 PrintArg(OS, "-ivfsoverlay", Quote);
194 OS << ' ';
195 PrintArg(OS, CrashInfo->VFSPath.str().c_str(), Quote);
196 }
197
198 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 Wennborgfc338972013-09-12 18:23:34 +0000208 OS << Terminator;
209}
210
Stephen Hines176edba2014-12-01 14:53:08 -0800211void Command::setResponseFile(const char *FileName) {
212 ResponseFile = FileName;
213 ResponseFileFlag = Creator.getResponseFileFlag();
214 ResponseFileFlag += FileName;
215}
216
Hans Wennborg64228b62013-09-18 00:41:15 +0000217int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
Hans Wennborgaaaa2a12013-09-12 18:35:08 +0000218 bool *ExecutionFailed) const {
219 SmallVector<const char*, 128> Argv;
Stephen Hines176edba2014-12-01 14:53:08 -0800220
221 if (ResponseFile == nullptr) {
222 Argv.push_back(Executable);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700223 Argv.append(Arguments.begin(), Arguments.end());
Stephen Hines176edba2014-12-01 14:53:08 -0800224 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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700240 Argv.push_back(nullptr);
Stephen Hines176edba2014-12-01 14:53:08 -0800241 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 Wennborgaaaa2a12013-09-12 18:35:08 +0000252
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700253 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
Hans Wennborgaaaa2a12013-09-12 18:35:08 +0000254 Redirects, /*secondsToWait*/ 0,
255 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
256}
257
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000258FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
259 const char *Executable_,
260 const ArgStringList &Arguments_,
Stephen Hines176edba2014-12-01 14:53:08 -0800261 std::unique_ptr<Command> Fallback_)
262 : Command(Source_, Creator_, Executable_, Arguments_),
263 Fallback(std::move(Fallback_)) {}
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000264
265void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
Stephen Hines176edba2014-12-01 14:53:08 -0800266 bool Quote, CrashReportInfo *CrashInfo) const {
267 Command::Print(OS, "", Quote, CrashInfo);
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000268 OS << " ||";
Stephen Hines176edba2014-12-01 14:53:08 -0800269 Fallback->Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgc8ba0a02013-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
Stephen Hines651f13c2014-04-23 16:59:28 -0700291 const Driver &D = getCreator().getToolChain().getDriver();
292 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
293
Hans Wennborgc8ba0a02013-09-19 20:32:16 +0000294 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
295 return SecondaryStatus;
296}
297
Daniel Dunbar789e2202009-03-13 23:36:33 +0000298JobList::JobList() : Job(JobListClass) {}
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000299
Hans Wennborgfc338972013-09-12 18:23:34 +0000300void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Stephen Hines176edba2014-12-01 14:53:08 -0800301 CrashReportInfo *CrashInfo) const {
302 for (const auto &Job : *this)
303 Job.Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgfc338972013-09-12 18:23:34 +0000304}
305
Stephen Hines176edba2014-12-01 14:53:08 -0800306void JobList::clear() { Jobs.clear(); }