1e5dd7070Spatrick //===- Job.cpp - Command to Execute ---------------------------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick
9e5dd7070Spatrick #include "clang/Driver/Job.h"
10e5dd7070Spatrick #include "clang/Basic/LLVM.h"
11e5dd7070Spatrick #include "clang/Driver/Driver.h"
12e5dd7070Spatrick #include "clang/Driver/DriverDiagnostic.h"
13a9ac8606Spatrick #include "clang/Driver/InputInfo.h"
14e5dd7070Spatrick #include "clang/Driver/Tool.h"
15e5dd7070Spatrick #include "clang/Driver/ToolChain.h"
16e5dd7070Spatrick #include "llvm/ADT/ArrayRef.h"
17e5dd7070Spatrick #include "llvm/ADT/SmallString.h"
18e5dd7070Spatrick #include "llvm/ADT/SmallVector.h"
19e5dd7070Spatrick #include "llvm/ADT/StringRef.h"
20e5dd7070Spatrick #include "llvm/ADT/StringSet.h"
21e5dd7070Spatrick #include "llvm/ADT/StringSwitch.h"
22e5dd7070Spatrick #include "llvm/Support/CrashRecoveryContext.h"
23e5dd7070Spatrick #include "llvm/Support/FileSystem.h"
24e5dd7070Spatrick #include "llvm/Support/Path.h"
25e5dd7070Spatrick #include "llvm/Support/PrettyStackTrace.h"
26e5dd7070Spatrick #include "llvm/Support/Program.h"
27e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
28e5dd7070Spatrick #include <algorithm>
29e5dd7070Spatrick #include <cassert>
30e5dd7070Spatrick #include <cstddef>
31e5dd7070Spatrick #include <string>
32e5dd7070Spatrick #include <system_error>
33e5dd7070Spatrick #include <utility>
34e5dd7070Spatrick
35e5dd7070Spatrick using namespace clang;
36e5dd7070Spatrick using namespace driver;
37e5dd7070Spatrick
Command(const Action & Source,const Tool & Creator,ResponseFileSupport ResponseSupport,const char * Executable,const llvm::opt::ArgStringList & Arguments,ArrayRef<InputInfo> Inputs,ArrayRef<InputInfo> Outputs)38e5dd7070Spatrick Command::Command(const Action &Source, const Tool &Creator,
39ec727ea7Spatrick ResponseFileSupport ResponseSupport, const char *Executable,
40e5dd7070Spatrick const llvm::opt::ArgStringList &Arguments,
41a9ac8606Spatrick ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs)
42ec727ea7Spatrick : Source(Source), Creator(Creator), ResponseSupport(ResponseSupport),
43ec727ea7Spatrick Executable(Executable), Arguments(Arguments) {
44e5dd7070Spatrick for (const auto &II : Inputs)
45e5dd7070Spatrick if (II.isFilename())
46a9ac8606Spatrick InputInfoList.push_back(II);
47a9ac8606Spatrick for (const auto &II : Outputs)
48a9ac8606Spatrick if (II.isFilename())
49a9ac8606Spatrick OutputFilenames.push_back(II.getFilename());
50e5dd7070Spatrick }
51e5dd7070Spatrick
52e5dd7070Spatrick /// Check if the compiler flag in question should be skipped when
53e5dd7070Spatrick /// emitting a reproducer. Also track how many arguments it has and if the
54e5dd7070Spatrick /// option is some kind of include path.
skipArgs(const char * Flag,bool HaveCrashVFS,int & SkipNum,bool & IsInclude)55e5dd7070Spatrick static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum,
56e5dd7070Spatrick bool &IsInclude) {
57e5dd7070Spatrick SkipNum = 2;
58e5dd7070Spatrick // These flags are all of the form -Flag <Arg> and are treated as two
59e5dd7070Spatrick // arguments. Therefore, we need to skip the flag and the next argument.
60e5dd7070Spatrick bool ShouldSkip = llvm::StringSwitch<bool>(Flag)
61e5dd7070Spatrick .Cases("-MF", "-MT", "-MQ", "-serialize-diagnostic-file", true)
62e5dd7070Spatrick .Cases("-o", "-dependency-file", true)
63e5dd7070Spatrick .Cases("-fdebug-compilation-dir", "-diagnostic-log-file", true)
64e5dd7070Spatrick .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
65e5dd7070Spatrick .Default(false);
66e5dd7070Spatrick if (ShouldSkip)
67e5dd7070Spatrick return true;
68e5dd7070Spatrick
69e5dd7070Spatrick // Some include flags shouldn't be skipped if we have a crash VFS
70e5dd7070Spatrick IsInclude = llvm::StringSwitch<bool>(Flag)
71e5dd7070Spatrick .Cases("-include", "-header-include-file", true)
72e5dd7070Spatrick .Cases("-idirafter", "-internal-isystem", "-iwithprefix", true)
73e5dd7070Spatrick .Cases("-internal-externc-isystem", "-iprefix", true)
74e5dd7070Spatrick .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
75e5dd7070Spatrick .Cases("-isysroot", "-I", "-F", "-resource-dir", true)
76e5dd7070Spatrick .Cases("-iframework", "-include-pch", true)
77e5dd7070Spatrick .Default(false);
78e5dd7070Spatrick if (IsInclude)
79e5dd7070Spatrick return !HaveCrashVFS;
80e5dd7070Spatrick
81e5dd7070Spatrick // The remaining flags are treated as a single argument.
82e5dd7070Spatrick
83e5dd7070Spatrick // These flags are all of the form -Flag and have no second argument.
84e5dd7070Spatrick ShouldSkip = llvm::StringSwitch<bool>(Flag)
85e5dd7070Spatrick .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
86e5dd7070Spatrick .Case("-MMD", true)
87e5dd7070Spatrick .Default(false);
88e5dd7070Spatrick
89e5dd7070Spatrick // Match found.
90e5dd7070Spatrick SkipNum = 1;
91e5dd7070Spatrick if (ShouldSkip)
92e5dd7070Spatrick return true;
93e5dd7070Spatrick
94e5dd7070Spatrick // These flags are treated as a single argument (e.g., -F<Dir>).
95e5dd7070Spatrick StringRef FlagRef(Flag);
96e5dd7070Spatrick IsInclude = FlagRef.startswith("-F") || FlagRef.startswith("-I");
97e5dd7070Spatrick if (IsInclude)
98e5dd7070Spatrick return !HaveCrashVFS;
99e5dd7070Spatrick if (FlagRef.startswith("-fmodules-cache-path="))
100e5dd7070Spatrick return true;
101e5dd7070Spatrick
102e5dd7070Spatrick SkipNum = 0;
103e5dd7070Spatrick return false;
104e5dd7070Spatrick }
105e5dd7070Spatrick
writeResponseFile(raw_ostream & OS) const106e5dd7070Spatrick void Command::writeResponseFile(raw_ostream &OS) const {
107e5dd7070Spatrick // In a file list, we only write the set of inputs to the response file
108ec727ea7Spatrick if (ResponseSupport.ResponseKind == ResponseFileSupport::RF_FileList) {
109e5dd7070Spatrick for (const auto *Arg : InputFileList) {
110e5dd7070Spatrick OS << Arg << '\n';
111e5dd7070Spatrick }
112e5dd7070Spatrick return;
113e5dd7070Spatrick }
114e5dd7070Spatrick
115e5dd7070Spatrick // In regular response files, we send all arguments to the response file.
116e5dd7070Spatrick // Wrapping all arguments in double quotes ensures that both Unix tools and
117e5dd7070Spatrick // Windows tools understand the response file.
118e5dd7070Spatrick for (const auto *Arg : Arguments) {
119e5dd7070Spatrick OS << '"';
120e5dd7070Spatrick
121e5dd7070Spatrick for (; *Arg != '\0'; Arg++) {
122e5dd7070Spatrick if (*Arg == '\"' || *Arg == '\\') {
123e5dd7070Spatrick OS << '\\';
124e5dd7070Spatrick }
125e5dd7070Spatrick OS << *Arg;
126e5dd7070Spatrick }
127e5dd7070Spatrick
128e5dd7070Spatrick OS << "\" ";
129e5dd7070Spatrick }
130e5dd7070Spatrick }
131e5dd7070Spatrick
buildArgvForResponseFile(llvm::SmallVectorImpl<const char * > & Out) const132e5dd7070Spatrick void Command::buildArgvForResponseFile(
133e5dd7070Spatrick llvm::SmallVectorImpl<const char *> &Out) const {
134e5dd7070Spatrick // When not a file list, all arguments are sent to the response file.
135e5dd7070Spatrick // This leaves us to set the argv to a single parameter, requesting the tool
136e5dd7070Spatrick // to read the response file.
137ec727ea7Spatrick if (ResponseSupport.ResponseKind != ResponseFileSupport::RF_FileList) {
138e5dd7070Spatrick Out.push_back(Executable);
139e5dd7070Spatrick Out.push_back(ResponseFileFlag.c_str());
140e5dd7070Spatrick return;
141e5dd7070Spatrick }
142e5dd7070Spatrick
143e5dd7070Spatrick llvm::StringSet<> Inputs;
144e5dd7070Spatrick for (const auto *InputName : InputFileList)
145e5dd7070Spatrick Inputs.insert(InputName);
146e5dd7070Spatrick Out.push_back(Executable);
147e5dd7070Spatrick // In a file list, build args vector ignoring parameters that will go in the
148e5dd7070Spatrick // response file (elements of the InputFileList vector)
149e5dd7070Spatrick bool FirstInput = true;
150e5dd7070Spatrick for (const auto *Arg : Arguments) {
151e5dd7070Spatrick if (Inputs.count(Arg) == 0) {
152e5dd7070Spatrick Out.push_back(Arg);
153e5dd7070Spatrick } else if (FirstInput) {
154e5dd7070Spatrick FirstInput = false;
155ec727ea7Spatrick Out.push_back(ResponseSupport.ResponseFlag);
156e5dd7070Spatrick Out.push_back(ResponseFile);
157e5dd7070Spatrick }
158e5dd7070Spatrick }
159e5dd7070Spatrick }
160e5dd7070Spatrick
161e5dd7070Spatrick /// Rewrite relative include-like flag paths to absolute ones.
162e5dd7070Spatrick static void
rewriteIncludes(const llvm::ArrayRef<const char * > & Args,size_t Idx,size_t NumArgs,llvm::SmallVectorImpl<llvm::SmallString<128>> & IncFlags)163e5dd7070Spatrick rewriteIncludes(const llvm::ArrayRef<const char *> &Args, size_t Idx,
164e5dd7070Spatrick size_t NumArgs,
165e5dd7070Spatrick llvm::SmallVectorImpl<llvm::SmallString<128>> &IncFlags) {
166e5dd7070Spatrick using namespace llvm;
167e5dd7070Spatrick using namespace sys;
168e5dd7070Spatrick
169e5dd7070Spatrick auto getAbsPath = [](StringRef InInc, SmallVectorImpl<char> &OutInc) -> bool {
170e5dd7070Spatrick if (path::is_absolute(InInc)) // Nothing to do here...
171e5dd7070Spatrick return false;
172e5dd7070Spatrick std::error_code EC = fs::current_path(OutInc);
173e5dd7070Spatrick if (EC)
174e5dd7070Spatrick return false;
175e5dd7070Spatrick path::append(OutInc, InInc);
176e5dd7070Spatrick return true;
177e5dd7070Spatrick };
178e5dd7070Spatrick
179e5dd7070Spatrick SmallString<128> NewInc;
180e5dd7070Spatrick if (NumArgs == 1) {
181e5dd7070Spatrick StringRef FlagRef(Args[Idx + NumArgs - 1]);
182e5dd7070Spatrick assert((FlagRef.startswith("-F") || FlagRef.startswith("-I")) &&
183e5dd7070Spatrick "Expecting -I or -F");
184e5dd7070Spatrick StringRef Inc = FlagRef.slice(2, StringRef::npos);
185e5dd7070Spatrick if (getAbsPath(Inc, NewInc)) {
186e5dd7070Spatrick SmallString<128> NewArg(FlagRef.slice(0, 2));
187e5dd7070Spatrick NewArg += NewInc;
188e5dd7070Spatrick IncFlags.push_back(std::move(NewArg));
189e5dd7070Spatrick }
190e5dd7070Spatrick return;
191e5dd7070Spatrick }
192e5dd7070Spatrick
193e5dd7070Spatrick assert(NumArgs == 2 && "Not expecting more than two arguments");
194e5dd7070Spatrick StringRef Inc(Args[Idx + NumArgs - 1]);
195e5dd7070Spatrick if (!getAbsPath(Inc, NewInc))
196e5dd7070Spatrick return;
197e5dd7070Spatrick IncFlags.push_back(SmallString<128>(Args[Idx]));
198e5dd7070Spatrick IncFlags.push_back(std::move(NewInc));
199e5dd7070Spatrick }
200e5dd7070Spatrick
Print(raw_ostream & OS,const char * Terminator,bool Quote,CrashReportInfo * CrashInfo) const201e5dd7070Spatrick void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
202e5dd7070Spatrick CrashReportInfo *CrashInfo) const {
203e5dd7070Spatrick // Always quote the exe.
204e5dd7070Spatrick OS << ' ';
205ec727ea7Spatrick llvm::sys::printArg(OS, Executable, /*Quote=*/true);
206e5dd7070Spatrick
207e5dd7070Spatrick ArrayRef<const char *> Args = Arguments;
208e5dd7070Spatrick SmallVector<const char *, 128> ArgsRespFile;
209e5dd7070Spatrick if (ResponseFile != nullptr) {
210e5dd7070Spatrick buildArgvForResponseFile(ArgsRespFile);
211e5dd7070Spatrick Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
212e5dd7070Spatrick }
213e5dd7070Spatrick
214e5dd7070Spatrick bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
215e5dd7070Spatrick for (size_t i = 0, e = Args.size(); i < e; ++i) {
216e5dd7070Spatrick const char *const Arg = Args[i];
217e5dd7070Spatrick
218e5dd7070Spatrick if (CrashInfo) {
219e5dd7070Spatrick int NumArgs = 0;
220e5dd7070Spatrick bool IsInclude = false;
221e5dd7070Spatrick if (skipArgs(Arg, HaveCrashVFS, NumArgs, IsInclude)) {
222e5dd7070Spatrick i += NumArgs - 1;
223e5dd7070Spatrick continue;
224e5dd7070Spatrick }
225e5dd7070Spatrick
226e5dd7070Spatrick // Relative includes need to be expanded to absolute paths.
227e5dd7070Spatrick if (HaveCrashVFS && IsInclude) {
228e5dd7070Spatrick SmallVector<SmallString<128>, 2> NewIncFlags;
229e5dd7070Spatrick rewriteIncludes(Args, i, NumArgs, NewIncFlags);
230e5dd7070Spatrick if (!NewIncFlags.empty()) {
231e5dd7070Spatrick for (auto &F : NewIncFlags) {
232e5dd7070Spatrick OS << ' ';
233ec727ea7Spatrick llvm::sys::printArg(OS, F.c_str(), Quote);
234e5dd7070Spatrick }
235e5dd7070Spatrick i += NumArgs - 1;
236e5dd7070Spatrick continue;
237e5dd7070Spatrick }
238e5dd7070Spatrick }
239e5dd7070Spatrick
240a9ac8606Spatrick auto Found = llvm::find_if(InputInfoList, [&Arg](const InputInfo &II) {
241a9ac8606Spatrick return II.getFilename() == Arg;
242a9ac8606Spatrick });
243a9ac8606Spatrick if (Found != InputInfoList.end() &&
244e5dd7070Spatrick (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
245e5dd7070Spatrick // Replace the input file name with the crashinfo's file name.
246e5dd7070Spatrick OS << ' ';
247e5dd7070Spatrick StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
248ec727ea7Spatrick llvm::sys::printArg(OS, ShortName.str(), Quote);
249e5dd7070Spatrick continue;
250e5dd7070Spatrick }
251e5dd7070Spatrick }
252e5dd7070Spatrick
253e5dd7070Spatrick OS << ' ';
254ec727ea7Spatrick llvm::sys::printArg(OS, Arg, Quote);
255e5dd7070Spatrick }
256e5dd7070Spatrick
257e5dd7070Spatrick if (CrashInfo && HaveCrashVFS) {
258e5dd7070Spatrick OS << ' ';
259ec727ea7Spatrick llvm::sys::printArg(OS, "-ivfsoverlay", Quote);
260e5dd7070Spatrick OS << ' ';
261ec727ea7Spatrick llvm::sys::printArg(OS, CrashInfo->VFSPath.str(), Quote);
262e5dd7070Spatrick
263e5dd7070Spatrick // The leftover modules from the crash are stored in
264e5dd7070Spatrick // <name>.cache/vfs/modules
265e5dd7070Spatrick // Leave it untouched for pcm inspection and provide a clean/empty dir
266e5dd7070Spatrick // path to contain the future generated module cache:
267e5dd7070Spatrick // <name>.cache/vfs/repro-modules
268e5dd7070Spatrick SmallString<128> RelModCacheDir = llvm::sys::path::parent_path(
269e5dd7070Spatrick llvm::sys::path::parent_path(CrashInfo->VFSPath));
270e5dd7070Spatrick llvm::sys::path::append(RelModCacheDir, "repro-modules");
271e5dd7070Spatrick
272e5dd7070Spatrick std::string ModCachePath = "-fmodules-cache-path=";
273e5dd7070Spatrick ModCachePath.append(RelModCacheDir.c_str());
274e5dd7070Spatrick
275e5dd7070Spatrick OS << ' ';
276ec727ea7Spatrick llvm::sys::printArg(OS, ModCachePath, Quote);
277e5dd7070Spatrick }
278e5dd7070Spatrick
279e5dd7070Spatrick if (ResponseFile != nullptr) {
280e5dd7070Spatrick OS << "\n Arguments passed via response file:\n";
281e5dd7070Spatrick writeResponseFile(OS);
282e5dd7070Spatrick // Avoiding duplicated newline terminator, since FileLists are
283e5dd7070Spatrick // newline-separated.
284ec727ea7Spatrick if (ResponseSupport.ResponseKind != ResponseFileSupport::RF_FileList)
285e5dd7070Spatrick OS << "\n";
286e5dd7070Spatrick OS << " (end of response file)";
287e5dd7070Spatrick }
288e5dd7070Spatrick
289e5dd7070Spatrick OS << Terminator;
290e5dd7070Spatrick }
291e5dd7070Spatrick
setResponseFile(const char * FileName)292e5dd7070Spatrick void Command::setResponseFile(const char *FileName) {
293e5dd7070Spatrick ResponseFile = FileName;
294ec727ea7Spatrick ResponseFileFlag = ResponseSupport.ResponseFlag;
295e5dd7070Spatrick ResponseFileFlag += FileName;
296e5dd7070Spatrick }
297e5dd7070Spatrick
setEnvironment(llvm::ArrayRef<const char * > NewEnvironment)298e5dd7070Spatrick void Command::setEnvironment(llvm::ArrayRef<const char *> NewEnvironment) {
299e5dd7070Spatrick Environment.reserve(NewEnvironment.size() + 1);
300e5dd7070Spatrick Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
301e5dd7070Spatrick Environment.push_back(nullptr);
302e5dd7070Spatrick }
303e5dd7070Spatrick
setRedirectFiles(const std::vector<std::optional<std::string>> & Redirects)304*12c85518Srobert void Command::setRedirectFiles(
305*12c85518Srobert const std::vector<std::optional<std::string>> &Redirects) {
306*12c85518Srobert RedirectFiles = Redirects;
307*12c85518Srobert }
308*12c85518Srobert
PrintFileNames() const309e5dd7070Spatrick void Command::PrintFileNames() const {
310e5dd7070Spatrick if (PrintInputFilenames) {
311a9ac8606Spatrick for (const auto &Arg : InputInfoList)
312a9ac8606Spatrick llvm::outs() << llvm::sys::path::filename(Arg.getFilename()) << "\n";
313e5dd7070Spatrick llvm::outs().flush();
314e5dd7070Spatrick }
315e5dd7070Spatrick }
316e5dd7070Spatrick
Execute(ArrayRef<std::optional<StringRef>> Redirects,std::string * ErrMsg,bool * ExecutionFailed) const317*12c85518Srobert int Command::Execute(ArrayRef<std::optional<StringRef>> Redirects,
318e5dd7070Spatrick std::string *ErrMsg, bool *ExecutionFailed) const {
319e5dd7070Spatrick PrintFileNames();
320e5dd7070Spatrick
321e5dd7070Spatrick SmallVector<const char *, 128> Argv;
322e5dd7070Spatrick if (ResponseFile == nullptr) {
323e5dd7070Spatrick Argv.push_back(Executable);
324e5dd7070Spatrick Argv.append(Arguments.begin(), Arguments.end());
325e5dd7070Spatrick Argv.push_back(nullptr);
326e5dd7070Spatrick } else {
327e5dd7070Spatrick // If the command is too large, we need to put arguments in a response file.
328e5dd7070Spatrick std::string RespContents;
329e5dd7070Spatrick llvm::raw_string_ostream SS(RespContents);
330e5dd7070Spatrick
331e5dd7070Spatrick // Write file contents and build the Argv vector
332e5dd7070Spatrick writeResponseFile(SS);
333e5dd7070Spatrick buildArgvForResponseFile(Argv);
334e5dd7070Spatrick Argv.push_back(nullptr);
335e5dd7070Spatrick SS.flush();
336e5dd7070Spatrick
337e5dd7070Spatrick // Save the response file in the appropriate encoding
338e5dd7070Spatrick if (std::error_code EC = writeFileWithEncoding(
339ec727ea7Spatrick ResponseFile, RespContents, ResponseSupport.ResponseEncoding)) {
340e5dd7070Spatrick if (ErrMsg)
341e5dd7070Spatrick *ErrMsg = EC.message();
342e5dd7070Spatrick if (ExecutionFailed)
343e5dd7070Spatrick *ExecutionFailed = true;
344e5dd7070Spatrick // Return -1 by convention (see llvm/include/llvm/Support/Program.h) to
345e5dd7070Spatrick // indicate the requested executable cannot be started.
346e5dd7070Spatrick return -1;
347e5dd7070Spatrick }
348e5dd7070Spatrick }
349e5dd7070Spatrick
350*12c85518Srobert std::optional<ArrayRef<StringRef>> Env;
351e5dd7070Spatrick std::vector<StringRef> ArgvVectorStorage;
352e5dd7070Spatrick if (!Environment.empty()) {
353e5dd7070Spatrick assert(Environment.back() == nullptr &&
354e5dd7070Spatrick "Environment vector should be null-terminated by now");
355e5dd7070Spatrick ArgvVectorStorage = llvm::toStringRefArray(Environment.data());
356*12c85518Srobert Env = ArrayRef(ArgvVectorStorage);
357e5dd7070Spatrick }
358e5dd7070Spatrick
359e5dd7070Spatrick auto Args = llvm::toStringRefArray(Argv.data());
360*12c85518Srobert
361*12c85518Srobert // Use Job-specific redirect files if they are present.
362*12c85518Srobert if (!RedirectFiles.empty()) {
363*12c85518Srobert std::vector<std::optional<StringRef>> RedirectFilesOptional;
364*12c85518Srobert for (const auto &Ele : RedirectFiles)
365*12c85518Srobert if (Ele)
366*12c85518Srobert RedirectFilesOptional.push_back(std::optional<StringRef>(*Ele));
367*12c85518Srobert else
368*12c85518Srobert RedirectFilesOptional.push_back(std::nullopt);
369*12c85518Srobert
370*12c85518Srobert return llvm::sys::ExecuteAndWait(Executable, Args, Env,
371*12c85518Srobert ArrayRef(RedirectFilesOptional),
372*12c85518Srobert /*secondsToWait=*/0, /*memoryLimit=*/0,
373*12c85518Srobert ErrMsg, ExecutionFailed, &ProcStat);
374*12c85518Srobert }
375*12c85518Srobert
376e5dd7070Spatrick return llvm::sys::ExecuteAndWait(Executable, Args, Env, Redirects,
377a9ac8606Spatrick /*secondsToWait*/ 0, /*memoryLimit*/ 0,
378a9ac8606Spatrick ErrMsg, ExecutionFailed, &ProcStat);
379e5dd7070Spatrick }
380e5dd7070Spatrick
CC1Command(const Action & Source,const Tool & Creator,ResponseFileSupport ResponseSupport,const char * Executable,const llvm::opt::ArgStringList & Arguments,ArrayRef<InputInfo> Inputs,ArrayRef<InputInfo> Outputs)381e5dd7070Spatrick CC1Command::CC1Command(const Action &Source, const Tool &Creator,
382ec727ea7Spatrick ResponseFileSupport ResponseSupport,
383e5dd7070Spatrick const char *Executable,
384e5dd7070Spatrick const llvm::opt::ArgStringList &Arguments,
385a9ac8606Spatrick ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs)
386a9ac8606Spatrick : Command(Source, Creator, ResponseSupport, Executable, Arguments, Inputs,
387a9ac8606Spatrick Outputs) {
388e5dd7070Spatrick InProcess = true;
389e5dd7070Spatrick }
390e5dd7070Spatrick
Print(raw_ostream & OS,const char * Terminator,bool Quote,CrashReportInfo * CrashInfo) const391e5dd7070Spatrick void CC1Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
392e5dd7070Spatrick CrashReportInfo *CrashInfo) const {
393e5dd7070Spatrick if (InProcess)
394e5dd7070Spatrick OS << " (in-process)\n";
395e5dd7070Spatrick Command::Print(OS, Terminator, Quote, CrashInfo);
396e5dd7070Spatrick }
397e5dd7070Spatrick
Execute(ArrayRef<std::optional<StringRef>> Redirects,std::string * ErrMsg,bool * ExecutionFailed) const398*12c85518Srobert int CC1Command::Execute(ArrayRef<std::optional<StringRef>> Redirects,
399e5dd7070Spatrick std::string *ErrMsg, bool *ExecutionFailed) const {
400e5dd7070Spatrick // FIXME: Currently, if there're more than one job, we disable
401e5dd7070Spatrick // -fintegrate-cc1. If we're no longer a integrated-cc1 job, fallback to
402e5dd7070Spatrick // out-of-process execution. See discussion in https://reviews.llvm.org/D74447
403e5dd7070Spatrick if (!InProcess)
404e5dd7070Spatrick return Command::Execute(Redirects, ErrMsg, ExecutionFailed);
405e5dd7070Spatrick
406e5dd7070Spatrick PrintFileNames();
407e5dd7070Spatrick
408e5dd7070Spatrick SmallVector<const char *, 128> Argv;
409e5dd7070Spatrick Argv.push_back(getExecutable());
410e5dd7070Spatrick Argv.append(getArguments().begin(), getArguments().end());
411e5dd7070Spatrick Argv.push_back(nullptr);
412*12c85518Srobert Argv.pop_back(); // The terminating null element shall not be part of the
413*12c85518Srobert // slice (main() behavior).
414e5dd7070Spatrick
415e5dd7070Spatrick // This flag simply indicates that the program couldn't start, which isn't
416e5dd7070Spatrick // applicable here.
417e5dd7070Spatrick if (ExecutionFailed)
418e5dd7070Spatrick *ExecutionFailed = false;
419e5dd7070Spatrick
420e5dd7070Spatrick llvm::CrashRecoveryContext CRC;
421e5dd7070Spatrick CRC.DumpStackAndCleanupOnFailure = true;
422e5dd7070Spatrick
423e5dd7070Spatrick const void *PrettyState = llvm::SavePrettyStackState();
424e5dd7070Spatrick const Driver &D = getCreator().getToolChain().getDriver();
425e5dd7070Spatrick
426e5dd7070Spatrick int R = 0;
427e5dd7070Spatrick // Enter ExecuteCC1Tool() instead of starting up a new process
428e5dd7070Spatrick if (!CRC.RunSafely([&]() { R = D.CC1Main(Argv); })) {
429e5dd7070Spatrick llvm::RestorePrettyStackState(PrettyState);
430e5dd7070Spatrick return CRC.RetCode;
431e5dd7070Spatrick }
432e5dd7070Spatrick return R;
433e5dd7070Spatrick }
434e5dd7070Spatrick
setEnvironment(llvm::ArrayRef<const char * > NewEnvironment)435e5dd7070Spatrick void CC1Command::setEnvironment(llvm::ArrayRef<const char *> NewEnvironment) {
436e5dd7070Spatrick // We don't support set a new environment when calling into ExecuteCC1Tool()
437e5dd7070Spatrick llvm_unreachable(
438e5dd7070Spatrick "The CC1Command doesn't support changing the environment vars!");
439e5dd7070Spatrick }
440e5dd7070Spatrick
ForceSuccessCommand(const Action & Source_,const Tool & Creator_,ResponseFileSupport ResponseSupport,const char * Executable_,const llvm::opt::ArgStringList & Arguments_,ArrayRef<InputInfo> Inputs,ArrayRef<InputInfo> Outputs)441e5dd7070Spatrick ForceSuccessCommand::ForceSuccessCommand(
442ec727ea7Spatrick const Action &Source_, const Tool &Creator_,
443ec727ea7Spatrick ResponseFileSupport ResponseSupport, const char *Executable_,
444a9ac8606Spatrick const llvm::opt::ArgStringList &Arguments_, ArrayRef<InputInfo> Inputs,
445a9ac8606Spatrick ArrayRef<InputInfo> Outputs)
446ec727ea7Spatrick : Command(Source_, Creator_, ResponseSupport, Executable_, Arguments_,
447a9ac8606Spatrick Inputs, Outputs) {}
448e5dd7070Spatrick
Print(raw_ostream & OS,const char * Terminator,bool Quote,CrashReportInfo * CrashInfo) const449e5dd7070Spatrick void ForceSuccessCommand::Print(raw_ostream &OS, const char *Terminator,
450e5dd7070Spatrick bool Quote, CrashReportInfo *CrashInfo) const {
451e5dd7070Spatrick Command::Print(OS, "", Quote, CrashInfo);
452e5dd7070Spatrick OS << " || (exit 0)" << Terminator;
453e5dd7070Spatrick }
454e5dd7070Spatrick
Execute(ArrayRef<std::optional<StringRef>> Redirects,std::string * ErrMsg,bool * ExecutionFailed) const455*12c85518Srobert int ForceSuccessCommand::Execute(ArrayRef<std::optional<StringRef>> Redirects,
456e5dd7070Spatrick std::string *ErrMsg,
457e5dd7070Spatrick bool *ExecutionFailed) const {
458e5dd7070Spatrick int Status = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
459e5dd7070Spatrick (void)Status;
460e5dd7070Spatrick if (ExecutionFailed)
461e5dd7070Spatrick *ExecutionFailed = false;
462e5dd7070Spatrick return 0;
463e5dd7070Spatrick }
464e5dd7070Spatrick
Print(raw_ostream & OS,const char * Terminator,bool Quote,CrashReportInfo * CrashInfo) const465e5dd7070Spatrick void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
466e5dd7070Spatrick CrashReportInfo *CrashInfo) const {
467e5dd7070Spatrick for (const auto &Job : *this)
468e5dd7070Spatrick Job.Print(OS, Terminator, Quote, CrashInfo);
469e5dd7070Spatrick }
470e5dd7070Spatrick
clear()471e5dd7070Spatrick void JobList::clear() { Jobs.clear(); }
472