1 //===- Job.cpp - Command to Execute ---------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Driver/Job.h"
10 #include "clang/Basic/LLVM.h"
11 #include "clang/Driver/Driver.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/InputInfo.h"
14 #include "clang/Driver/Tool.h"
15 #include "clang/Driver/ToolChain.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/StringSet.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/CrashRecoveryContext.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/Program.h"
27 #include "llvm/Support/raw_ostream.h"
32 #include <system_error>
35 using namespace clang
;
36 using namespace driver
;
38 Command::Command(const Action
&Source
, const Tool
&Creator
,
39 ResponseFileSupport ResponseSupport
, const char *Executable
,
40 const llvm::opt::ArgStringList
&Arguments
,
41 ArrayRef
<InputInfo
> Inputs
, ArrayRef
<InputInfo
> Outputs
)
42 : Source(Source
), Creator(Creator
), ResponseSupport(ResponseSupport
),
43 Executable(Executable
), Arguments(Arguments
) {
44 for (const auto &II
: Inputs
)
46 InputInfoList
.push_back(II
);
47 for (const auto &II
: Outputs
)
49 OutputFilenames
.push_back(II
.getFilename());
52 /// Check if the compiler flag in question should be skipped when
53 /// emitting a reproducer. Also track how many arguments it has and if the
54 /// option is some kind of include path.
55 static bool skipArgs(const char *Flag
, bool HaveCrashVFS
, int &SkipNum
,
58 // These flags are all of the form -Flag <Arg> and are treated as two
59 // arguments. Therefore, we need to skip the flag and the next argument.
60 bool ShouldSkip
= llvm::StringSwitch
<bool>(Flag
)
61 .Cases("-MF", "-MT", "-MQ", "-serialize-diagnostic-file", true)
62 .Cases("-o", "-dependency-file", true)
63 .Cases("-fdebug-compilation-dir", "-diagnostic-log-file", true)
64 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
69 // Some include flags shouldn't be skipped if we have a crash VFS
70 IsInclude
= llvm::StringSwitch
<bool>(Flag
)
71 .Cases("-include", "-header-include-file", true)
72 .Cases("-idirafter", "-internal-isystem", "-iwithprefix", true)
73 .Cases("-internal-externc-isystem", "-iprefix", true)
74 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
75 .Cases("-isysroot", "-I", "-F", "-resource-dir", true)
76 .Cases("-iframework", "-include-pch", true)
81 // The remaining flags are treated as a single argument.
83 // These flags are all of the form -Flag and have no second argument.
84 ShouldSkip
= llvm::StringSwitch
<bool>(Flag
)
85 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
94 // These flags are treated as a single argument (e.g., -F<Dir>).
95 StringRef
FlagRef(Flag
);
96 IsInclude
= FlagRef
.startswith("-F") || FlagRef
.startswith("-I");
99 if (FlagRef
.startswith("-fmodules-cache-path="))
106 void Command::writeResponseFile(raw_ostream
&OS
) const {
107 // In a file list, we only write the set of inputs to the response file
108 if (ResponseSupport
.ResponseKind
== ResponseFileSupport::RF_FileList
) {
109 for (const auto *Arg
: InputFileList
) {
115 // In regular response files, we send all arguments to the response file.
116 // Wrapping all arguments in double quotes ensures that both Unix tools and
117 // Windows tools understand the response file.
118 for (const auto *Arg
: Arguments
) {
121 for (; *Arg
!= '\0'; Arg
++) {
122 if (*Arg
== '\"' || *Arg
== '\\') {
132 void Command::buildArgvForResponseFile(
133 llvm::SmallVectorImpl
<const char *> &Out
) const {
134 // When not a file list, all arguments are sent to the response file.
135 // This leaves us to set the argv to a single parameter, requesting the tool
136 // to read the response file.
137 if (ResponseSupport
.ResponseKind
!= ResponseFileSupport::RF_FileList
) {
138 Out
.push_back(Executable
);
139 Out
.push_back(ResponseFileFlag
.c_str());
143 llvm::StringSet
<> Inputs
;
144 for (const auto *InputName
: InputFileList
)
145 Inputs
.insert(InputName
);
146 Out
.push_back(Executable
);
147 // In a file list, build args vector ignoring parameters that will go in the
148 // response file (elements of the InputFileList vector)
149 bool FirstInput
= true;
150 for (const auto *Arg
: Arguments
) {
151 if (Inputs
.count(Arg
) == 0) {
153 } else if (FirstInput
) {
155 Out
.push_back(ResponseSupport
.ResponseFlag
);
156 Out
.push_back(ResponseFile
);
161 /// Rewrite relative include-like flag paths to absolute ones.
163 rewriteIncludes(const llvm::ArrayRef
<const char *> &Args
, size_t Idx
,
165 llvm::SmallVectorImpl
<llvm::SmallString
<128>> &IncFlags
) {
166 using namespace llvm
;
169 auto getAbsPath
= [](StringRef InInc
, SmallVectorImpl
<char> &OutInc
) -> bool {
170 if (path::is_absolute(InInc
)) // Nothing to do here...
172 std::error_code EC
= fs::current_path(OutInc
);
175 path::append(OutInc
, InInc
);
179 SmallString
<128> NewInc
;
181 StringRef
FlagRef(Args
[Idx
+ NumArgs
- 1]);
182 assert((FlagRef
.startswith("-F") || FlagRef
.startswith("-I")) &&
183 "Expecting -I or -F");
184 StringRef Inc
= FlagRef
.slice(2, StringRef::npos
);
185 if (getAbsPath(Inc
, NewInc
)) {
186 SmallString
<128> NewArg(FlagRef
.slice(0, 2));
188 IncFlags
.push_back(std::move(NewArg
));
193 assert(NumArgs
== 2 && "Not expecting more than two arguments");
194 StringRef
Inc(Args
[Idx
+ NumArgs
- 1]);
195 if (!getAbsPath(Inc
, NewInc
))
197 IncFlags
.push_back(SmallString
<128>(Args
[Idx
]));
198 IncFlags
.push_back(std::move(NewInc
));
201 void Command::Print(raw_ostream
&OS
, const char *Terminator
, bool Quote
,
202 CrashReportInfo
*CrashInfo
) const {
203 // Always quote the exe.
205 llvm::sys::printArg(OS
, Executable
, /*Quote=*/true);
207 ArrayRef
<const char *> Args
= Arguments
;
208 SmallVector
<const char *, 128> ArgsRespFile
;
209 if (ResponseFile
!= nullptr) {
210 buildArgvForResponseFile(ArgsRespFile
);
211 Args
= ArrayRef
<const char *>(ArgsRespFile
).slice(1); // no executable name
214 bool HaveCrashVFS
= CrashInfo
&& !CrashInfo
->VFSPath
.empty();
215 for (size_t i
= 0, e
= Args
.size(); i
< e
; ++i
) {
216 const char *const Arg
= Args
[i
];
220 bool IsInclude
= false;
221 if (skipArgs(Arg
, HaveCrashVFS
, NumArgs
, IsInclude
)) {
226 // Relative includes need to be expanded to absolute paths.
227 if (HaveCrashVFS
&& IsInclude
) {
228 SmallVector
<SmallString
<128>, 2> NewIncFlags
;
229 rewriteIncludes(Args
, i
, NumArgs
, NewIncFlags
);
230 if (!NewIncFlags
.empty()) {
231 for (auto &F
: NewIncFlags
) {
233 llvm::sys::printArg(OS
, F
.c_str(), Quote
);
240 auto Found
= llvm::find_if(InputInfoList
, [&Arg
](const InputInfo
&II
) {
241 return II
.getFilename() == Arg
;
243 if (Found
!= InputInfoList
.end() &&
244 (i
== 0 || StringRef(Args
[i
- 1]) != "-main-file-name")) {
245 // Replace the input file name with the crashinfo's file name.
247 StringRef ShortName
= llvm::sys::path::filename(CrashInfo
->Filename
);
248 llvm::sys::printArg(OS
, ShortName
.str(), Quote
);
254 llvm::sys::printArg(OS
, Arg
, Quote
);
257 if (CrashInfo
&& HaveCrashVFS
) {
259 llvm::sys::printArg(OS
, "-ivfsoverlay", Quote
);
261 llvm::sys::printArg(OS
, CrashInfo
->VFSPath
.str(), Quote
);
263 // The leftover modules from the crash are stored in
264 // <name>.cache/vfs/modules
265 // Leave it untouched for pcm inspection and provide a clean/empty dir
266 // path to contain the future generated module cache:
267 // <name>.cache/vfs/repro-modules
268 SmallString
<128> RelModCacheDir
= llvm::sys::path::parent_path(
269 llvm::sys::path::parent_path(CrashInfo
->VFSPath
));
270 llvm::sys::path::append(RelModCacheDir
, "repro-modules");
272 std::string ModCachePath
= "-fmodules-cache-path=";
273 ModCachePath
.append(RelModCacheDir
.c_str());
276 llvm::sys::printArg(OS
, ModCachePath
, Quote
);
279 if (ResponseFile
!= nullptr) {
280 OS
<< "\n Arguments passed via response file:\n";
281 writeResponseFile(OS
);
282 // Avoiding duplicated newline terminator, since FileLists are
283 // newline-separated.
284 if (ResponseSupport
.ResponseKind
!= ResponseFileSupport::RF_FileList
)
286 OS
<< " (end of response file)";
292 void Command::setResponseFile(const char *FileName
) {
293 ResponseFile
= FileName
;
294 ResponseFileFlag
= ResponseSupport
.ResponseFlag
;
295 ResponseFileFlag
+= FileName
;
298 void Command::setEnvironment(llvm::ArrayRef
<const char *> NewEnvironment
) {
299 Environment
.reserve(NewEnvironment
.size() + 1);
300 Environment
.assign(NewEnvironment
.begin(), NewEnvironment
.end());
301 Environment
.push_back(nullptr);
304 void Command::setRedirectFiles(
305 const std::vector
<Optional
<std::string
>> &Redirects
) {
306 RedirectFiles
= Redirects
;
309 void Command::PrintFileNames() const {
310 if (PrintInputFilenames
) {
311 for (const auto &Arg
: InputInfoList
)
312 llvm::outs() << llvm::sys::path::filename(Arg
.getFilename()) << "\n";
313 llvm::outs().flush();
317 int Command::Execute(ArrayRef
<llvm::Optional
<StringRef
>> Redirects
,
318 std::string
*ErrMsg
, bool *ExecutionFailed
) const {
321 SmallVector
<const char *, 128> Argv
;
322 if (ResponseFile
== nullptr) {
323 Argv
.push_back(Executable
);
324 Argv
.append(Arguments
.begin(), Arguments
.end());
325 Argv
.push_back(nullptr);
327 // If the command is too large, we need to put arguments in a response file.
328 std::string RespContents
;
329 llvm::raw_string_ostream
SS(RespContents
);
331 // Write file contents and build the Argv vector
332 writeResponseFile(SS
);
333 buildArgvForResponseFile(Argv
);
334 Argv
.push_back(nullptr);
337 // Save the response file in the appropriate encoding
338 if (std::error_code EC
= writeFileWithEncoding(
339 ResponseFile
, RespContents
, ResponseSupport
.ResponseEncoding
)) {
341 *ErrMsg
= EC
.message();
343 *ExecutionFailed
= true;
344 // Return -1 by convention (see llvm/include/llvm/Support/Program.h) to
345 // indicate the requested executable cannot be started.
350 Optional
<ArrayRef
<StringRef
>> Env
;
351 std::vector
<StringRef
> ArgvVectorStorage
;
352 if (!Environment
.empty()) {
353 assert(Environment
.back() == nullptr &&
354 "Environment vector should be null-terminated by now");
355 ArgvVectorStorage
= llvm::toStringRefArray(Environment
.data());
356 Env
= makeArrayRef(ArgvVectorStorage
);
359 auto Args
= llvm::toStringRefArray(Argv
.data());
361 // Use Job-specific redirect files if they are present.
362 if (!RedirectFiles
.empty()) {
363 std::vector
<Optional
<StringRef
>> RedirectFilesOptional
;
364 for (const auto &Ele
: RedirectFiles
)
366 RedirectFilesOptional
.push_back(Optional
<StringRef
>(*Ele
));
368 RedirectFilesOptional
.push_back(None
);
370 return llvm::sys::ExecuteAndWait(Executable
, Args
, Env
,
371 makeArrayRef(RedirectFilesOptional
),
372 /*secondsToWait=*/0, /*memoryLimit=*/0,
373 ErrMsg
, ExecutionFailed
, &ProcStat
);
376 return llvm::sys::ExecuteAndWait(Executable
, Args
, Env
, Redirects
,
377 /*secondsToWait*/ 0, /*memoryLimit*/ 0,
378 ErrMsg
, ExecutionFailed
, &ProcStat
);
381 CC1Command::CC1Command(const Action
&Source
, const Tool
&Creator
,
382 ResponseFileSupport ResponseSupport
,
383 const char *Executable
,
384 const llvm::opt::ArgStringList
&Arguments
,
385 ArrayRef
<InputInfo
> Inputs
, ArrayRef
<InputInfo
> Outputs
)
386 : Command(Source
, Creator
, ResponseSupport
, Executable
, Arguments
, Inputs
,
391 void CC1Command::Print(raw_ostream
&OS
, const char *Terminator
, bool Quote
,
392 CrashReportInfo
*CrashInfo
) const {
394 OS
<< " (in-process)\n";
395 Command::Print(OS
, Terminator
, Quote
, CrashInfo
);
398 int CC1Command::Execute(ArrayRef
<llvm::Optional
<StringRef
>> Redirects
,
399 std::string
*ErrMsg
, bool *ExecutionFailed
) const {
400 // FIXME: Currently, if there're more than one job, we disable
401 // -fintegrate-cc1. If we're no longer a integrated-cc1 job, fallback to
402 // out-of-process execution. See discussion in https://reviews.llvm.org/D74447
404 return Command::Execute(Redirects
, ErrMsg
, ExecutionFailed
);
408 SmallVector
<const char *, 128> Argv
;
409 Argv
.push_back(getExecutable());
410 Argv
.append(getArguments().begin(), getArguments().end());
411 Argv
.push_back(nullptr);
412 Argv
.pop_back(); // The terminating null element shall not be part of the
413 // slice (main() behavior).
415 // This flag simply indicates that the program couldn't start, which isn't
418 *ExecutionFailed
= false;
420 llvm::CrashRecoveryContext CRC
;
421 CRC
.DumpStackAndCleanupOnFailure
= true;
423 const void *PrettyState
= llvm::SavePrettyStackState();
424 const Driver
&D
= getCreator().getToolChain().getDriver();
427 // Enter ExecuteCC1Tool() instead of starting up a new process
428 if (!CRC
.RunSafely([&]() { R
= D
.CC1Main(Argv
); })) {
429 llvm::RestorePrettyStackState(PrettyState
);
435 void CC1Command::setEnvironment(llvm::ArrayRef
<const char *> NewEnvironment
) {
436 // We don't support set a new environment when calling into ExecuteCC1Tool()
438 "The CC1Command doesn't support changing the environment vars!");
441 ForceSuccessCommand::ForceSuccessCommand(
442 const Action
&Source_
, const Tool
&Creator_
,
443 ResponseFileSupport ResponseSupport
, const char *Executable_
,
444 const llvm::opt::ArgStringList
&Arguments_
, ArrayRef
<InputInfo
> Inputs
,
445 ArrayRef
<InputInfo
> Outputs
)
446 : Command(Source_
, Creator_
, ResponseSupport
, Executable_
, Arguments_
,
449 void ForceSuccessCommand::Print(raw_ostream
&OS
, const char *Terminator
,
450 bool Quote
, CrashReportInfo
*CrashInfo
) const {
451 Command::Print(OS
, "", Quote
, CrashInfo
);
452 OS
<< " || (exit 0)" << Terminator
;
455 int ForceSuccessCommand::Execute(ArrayRef
<llvm::Optional
<StringRef
>> Redirects
,
457 bool *ExecutionFailed
) const {
458 int Status
= Command::Execute(Redirects
, ErrMsg
, ExecutionFailed
);
461 *ExecutionFailed
= false;
465 void JobList::Print(raw_ostream
&OS
, const char *Terminator
, bool Quote
,
466 CrashReportInfo
*CrashInfo
) const {
467 for (const auto &Job
: *this)
468 Job
.Print(OS
, Terminator
, Quote
, CrashInfo
);
471 void JobList::clear() { Jobs
.clear(); }