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