clang 24.0.0git
Job.h
Go to the documentation of this file.
1//===- Job.h - Commands to Execute ------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_DRIVER_JOB_H
10#define LLVM_CLANG_DRIVER_JOB_H
11
12#include "clang/Basic/LLVM.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/iterator.h"
19#include "llvm/Option/Option.h"
20#include "llvm/Support/Program.h"
21#include <memory>
22#include <optional>
23#include <string>
24#include <utility>
25#include <vector>
26
27namespace clang {
28namespace driver {
29
30class Action;
31class InputInfo;
32class Tool;
33
35 StringRef Filename;
36 StringRef VFSPath;
37
40};
41
42// Encodes the kind of response file supported for a command invocation.
43// Response files are necessary if the command line gets too large, requiring
44// the arguments to be transferred to a file.
47 // Provides full support for response files, which means we can transfer
48 // all tool input arguments to a file.
50 // Input file names can live in a file, but flags can't. This is a special
51 // case for old versions of Apple's ld64.
53 // Does not support response files: all arguments must be passed via
54 // command line.
56 };
57 /// The level of support for response files.
59
60 /// The encoding to use when writing response files on Windows. Ignored on
61 /// other host OSes.
62 ///
63 /// Windows use cases: - GCC and Binutils on mingw only accept ANSI response
64 /// files encoded with the system current code page.
65 /// - MSVC's CL.exe and LINK.exe accept UTF16 on Windows.
66 /// - Clang accepts both UTF8 and UTF16.
67 ///
68 /// FIXME: When GNU tools learn how to parse UTF16 on Windows, we should
69 /// always use UTF16 for Windows, which is the Windows official encoding for
70 /// international characters.
71 llvm::sys::WindowsEncodingMethod ResponseEncoding;
72
73 /// What prefix to use for the command-line argument when passing a response
74 /// file.
75 const char *ResponseFlag;
76
77 /// Returns a ResponseFileSupport indicating that response files are not
78 /// supported.
79 static constexpr ResponseFileSupport None() {
80 return {RF_None, llvm::sys::WEM_UTF8, nullptr};
81 }
82
83 /// Returns a ResponseFileSupport indicating that response files are
84 /// supported, using the @file syntax. On windows, the file is written in the
85 /// UTF8 encoding. On other OSes, no re-encoding occurs.
86 static constexpr ResponseFileSupport AtFileUTF8() {
87 return {RF_Full, llvm::sys::WEM_UTF8, "@"};
88 }
89
90 /// Returns a ResponseFileSupport indicating that response files are
91 /// supported, using the @file syntax. On windows, the file is written in the
92 /// current ANSI code-page encoding. On other OSes, no re-encoding occurs.
93 static constexpr ResponseFileSupport AtFileCurCP() {
94 return {RF_Full, llvm::sys::WEM_CurrentCodePage, "@"};
95 }
96
97 /// Returns a ResponseFileSupport indicating that response files are
98 /// supported, using the @file syntax. On windows, the file is written in the
99 /// UTF-16 encoding. On other OSes, no re-encoding occurs.
100 static constexpr ResponseFileSupport AtFileUTF16() {
101 return {RF_Full, llvm::sys::WEM_UTF16, "@"};
102 }
103};
104
105/// Command - An executable path/name and argument vector to
106/// execute.
107class Command {
108 /// Source - The action which caused the creation of this job.
109 const Action &Source;
110
111 /// Tool - The tool which caused the creation of this job.
112 const Tool &Creator;
113
114 /// Whether and how to generate response files if the arguments are too long.
115 ResponseFileSupport ResponseSupport;
116
117 /// The executable to run.
118 const char *Executable;
119
120 /// Optional argument to prepend.
121 const char *PrependArg;
122
123 /// The list of program arguments (not including the implicit first
124 /// argument, which will be the executable).
125 llvm::opt::ArgStringList Arguments;
126
127 /// The list of program inputs.
128 std::vector<InputInfo> InputInfoList;
129
130 /// The list of program arguments which are outputs. May be empty.
131 std::vector<std::string> OutputFilenames;
132
133 /// Response file name, if this command is set to use one, or nullptr
134 /// otherwise
135 const char *ResponseFile = nullptr;
136
137 /// The input file list in case we need to emit a file list instead of a
138 /// proper response file
139 llvm::opt::ArgStringList InputFileList;
140
141 /// String storage if we need to create a new argument to specify a response
142 /// file
143 std::string ResponseFileFlag;
144
145 /// See Command::setEnvironment
146 std::vector<const char *> Environment;
147
148 /// Optional redirection for stdin, stdout, stderr.
149 std::vector<std::optional<std::string>> RedirectFiles;
150
151 /// Information on executable run provided by OS.
152 mutable std::optional<llvm::sys::ProcessStatistics> ProcStat;
153
154 /// The bound architecture for this command (e.g. "arm64", "gfx90a").
155 std::string BoundArchStr;
156
157 /// Non-empty when this command may run in parallel with adjacent offload
158 /// device commands from the same group.
159 std::string OffloadDeviceParallelJobGroup;
160
161 /// When a response file is needed, we try to put most arguments in an
162 /// exclusive file, while others remains as regular command line arguments.
163 /// This functions fills a vector with the regular command line arguments,
164 /// argv, excluding the ones passed in a response file.
165 void buildArgvForResponseFile(llvm::SmallVectorImpl<const char *> &Out) const;
166
167 /// Encodes an array of C strings into a single string separated by whitespace.
168 /// This function will also put in quotes arguments that have whitespaces and
169 /// will escape the regular backslashes (used in Windows paths) and quotes.
170 /// The results are the contents of a response file, written into a raw_ostream.
171 void writeResponseFile(raw_ostream &OS) const;
172
173public:
174 /// Whether to print the input filenames when executing.
176
177 /// Whether the command will be executed in this process or not.
178 bool InProcess = false;
179
180 Command(const Action &Source, const Tool &Creator,
181 ResponseFileSupport ResponseSupport, const char *Executable,
182 const llvm::opt::ArgStringList &Arguments, ArrayRef<InputInfo> Inputs,
183 ArrayRef<InputInfo> Outputs = {}, const char *PrependArg = nullptr);
184 // FIXME: This really shouldn't be copyable, but is currently copied in some
185 // error handling in Driver::generateCompilationDiagnostics.
186 Command(const Command &) = default;
187 virtual ~Command() = default;
188
189 virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
190 CrashReportInfo *CrashInfo = nullptr) const;
191
192 virtual int Execute(ArrayRef<std::optional<StringRef>> Redirects,
193 std::string *ErrMsg, bool *ExecutionFailed) const;
194
195 /// getSource - Return the Action which caused the creation of this job.
196 const Action &getSource() const { return Source; }
197
198 /// getCreator - Return the Tool which caused the creation of this job.
199 const Tool &getCreator() const { return Creator; }
200
201 /// Return the bound architecture for this command, if any.
202 BoundArch getBoundArch() const { return BoundArch(BoundArchStr); }
203 void setBoundArch(BoundArch BA) { BoundArchStr = BA.ArchName.str(); }
204
206 return OffloadDeviceParallelJobGroup;
207 }
208 void setOffloadDeviceParallelJobGroup(StringRef Group) {
209 OffloadDeviceParallelJobGroup = Group.str();
210 }
211
212 /// Returns the kind of response file supported by the current invocation.
214 return ResponseSupport;
215 }
216
217 /// Set to pass arguments via a response file when launching the command
218 void setResponseFile(const char *FileName);
219
220 /// Set an input file list, necessary if you specified an RF_FileList response
221 /// file support.
222 void setInputFileList(llvm::opt::ArgStringList List) {
223 InputFileList = std::move(List);
224 }
225
226 /// Sets the environment to be used by the new process.
227 /// \param NewEnvironment An array of environment variables.
228 /// \remark If the environment remains unset, then the environment
229 /// from the parent process will be used.
230 virtual void setEnvironment(llvm::ArrayRef<const char *> NewEnvironment);
231
232 void
233 setRedirectFiles(const std::vector<std::optional<std::string>> &Redirects);
234
235 void replaceArguments(llvm::opt::ArgStringList List) {
236 Arguments = std::move(List);
237 }
238
239 void replaceExecutable(const char *Exe) { Executable = Exe; }
240
241 const char *getExecutable() const { return Executable; }
242
243 const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
244
245 const std::vector<InputInfo> &getInputInfos() const { return InputInfoList; }
246
247 const std::vector<std::string> &getOutputFilenames() const {
248 return OutputFilenames;
249 }
250
251 std::optional<llvm::sys::ProcessStatistics> getProcessStatistics() const {
252 return ProcStat;
253 }
254
255protected:
256 /// Optionally print the filenames to be compiled
257 void PrintFileNames() const;
258};
259
260/// Use the CC1 tool callback when available, to avoid creating a new process
261class CC1Command : public Command {
262public:
263 CC1Command(const Action &Source, const Tool &Creator,
264 ResponseFileSupport ResponseSupport, const char *Executable,
265 const llvm::opt::ArgStringList &Arguments,
266 ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs = {},
267 const char *PrependArg = nullptr);
268
269 void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
270 CrashReportInfo *CrashInfo = nullptr) const override;
271
272 int Execute(ArrayRef<std::optional<StringRef>> Redirects, std::string *ErrMsg,
273 bool *ExecutionFailed) const override;
274
275 void setEnvironment(llvm::ArrayRef<const char *> NewEnvironment) override;
276};
277
278/// JobList - A sequence of jobs to perform.
279class JobList {
280public:
282 using size_type = list_type::size_type;
283 using iterator = llvm::pointee_iterator<list_type::iterator>;
284 using const_iterator = llvm::pointee_iterator<list_type::const_iterator>;
285
286private:
287 list_type Jobs;
288
289public:
290 void Print(llvm::raw_ostream &OS, const char *Terminator,
291 bool Quote, CrashReportInfo *CrashInfo = nullptr) const;
292
293 /// Add a job to the list (taking ownership).
294 void addJob(std::unique_ptr<Command> J) { Jobs.push_back(std::move(J)); }
295
296 /// Clear the job list.
297 void clear();
298
299 const list_type &getJobs() const { return Jobs; }
300
301 // Returns and transfers ownership of all jobs, leaving this list empty.
302 list_type takeJobs() { return std::exchange(Jobs, {}); };
303
304 bool empty() const { return Jobs.empty(); }
305 size_type size() const { return Jobs.size(); }
306 iterator begin() { return Jobs.begin(); }
307 const_iterator begin() const { return Jobs.begin(); }
308 iterator end() { return Jobs.end(); }
309 const_iterator end() const { return Jobs.end(); }
310};
311
312} // namespace driver
313} // namespace clang
314
315#endif // LLVM_CLANG_DRIVER_JOB_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Action - Represent an abstract compilation step to perform.
Definition Action.h:48
void setEnvironment(llvm::ArrayRef< const char * > NewEnvironment) override
Sets the environment to be used by the new process.
Definition Job.cpp:449
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const override
Definition Job.cpp:401
int Execute(ArrayRef< std::optional< StringRef > > Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
Definition Job.cpp:408
CC1Command(const Action &Source, const Tool &Creator, ResponseFileSupport ResponseSupport, const char *Executable, const llvm::opt::ArgStringList &Arguments, ArrayRef< InputInfo > Inputs, ArrayRef< InputInfo > Outputs={}, const char *PrependArg=nullptr)
Definition Job.cpp:390
const Action & getSource() const
getSource - Return the Action which caused the creation of this job.
Definition Job.h:196
const std::vector< std::string > & getOutputFilenames() const
Definition Job.h:247
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition Job.h:199
StringRef getOffloadDeviceParallelJobGroup() const
Definition Job.h:205
void setBoundArch(BoundArch BA)
Definition Job.h:203
const llvm::opt::ArgStringList & getArguments() const
Definition Job.h:243
virtual ~Command()=default
void setResponseFile(const char *FileName)
Set to pass arguments via a response file when launching the command.
Definition Job.cpp:300
void setRedirectFiles(const std::vector< std::optional< std::string > > &Redirects)
Definition Job.cpp:312
Command(const Command &)=default
Command(const Action &Source, const Tool &Creator, ResponseFileSupport ResponseSupport, const char *Executable, const llvm::opt::ArgStringList &Arguments, ArrayRef< InputInfo > Inputs, ArrayRef< InputInfo > Outputs={}, const char *PrependArg=nullptr)
Definition Job.cpp:38
void replaceExecutable(const char *Exe)
Definition Job.h:239
void setInputFileList(llvm::opt::ArgStringList List)
Set an input file list, necessary if you specified an RF_FileList response file support.
Definition Job.h:222
bool PrintInputFilenames
Whether to print the input filenames when executing.
Definition Job.h:175
std::optional< llvm::sys::ProcessStatistics > getProcessStatistics() const
Definition Job.h:251
const char * getExecutable() const
Definition Job.h:241
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition Job.cpp:206
const ResponseFileSupport & getResponseFileSupport()
Returns the kind of response file supported by the current invocation.
Definition Job.h:213
bool InProcess
Whether the command will be executed in this process or not.
Definition Job.h:178
void setOffloadDeviceParallelJobGroup(StringRef Group)
Definition Job.h:208
virtual void setEnvironment(llvm::ArrayRef< const char * > NewEnvironment)
Sets the environment to be used by the new process.
Definition Job.cpp:306
void replaceArguments(llvm::opt::ArgStringList List)
Definition Job.h:235
void PrintFileNames() const
Optionally print the filenames to be compiled.
Definition Job.cpp:317
BoundArch getBoundArch() const
Return the bound architecture for this command, if any.
Definition Job.h:202
const std::vector< InputInfo > & getInputInfos() const
Definition Job.h:245
virtual int Execute(ArrayRef< std::optional< StringRef > > Redirects, std::string *ErrMsg, bool *ExecutionFailed) const
Definition Job.cpp:325
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
JobList - A sequence of jobs to perform.
Definition Job.h:279
list_type takeJobs()
Definition Job.h:302
size_type size() const
Definition Job.h:305
bool empty() const
Definition Job.h:304
list_type::size_type size_type
Definition Job.h:282
SmallVector< std::unique_ptr< Command >, 4 > list_type
Definition Job.h:281
void clear()
Clear the job list.
Definition Job.cpp:461
const_iterator begin() const
Definition Job.h:307
const list_type & getJobs() const
Definition Job.h:299
void addJob(std::unique_ptr< Command > J)
Add a job to the list (taking ownership).
Definition Job.h:294
const_iterator end() const
Definition Job.h:309
iterator end()
Definition Job.h:308
llvm::pointee_iterator< list_type::iterator > iterator
Definition Job.h:283
llvm::pointee_iterator< list_type::const_iterator > const_iterator
Definition Job.h:284
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition Job.cpp:455
iterator begin()
Definition Job.h:306
Tool - Information on a specific compilation tool.
Definition Tool.h:32
The JSON file list parser is used to communicate input to InstallAPI.
Represents a bound architecture for offload / multiple architecture compilation.
llvm::StringRef ArchName
CrashReportInfo(StringRef Filename, StringRef VFSPath)
Definition Job.h:38
ResponseFileKind ResponseKind
The level of support for response files.
Definition Job.h:58
llvm::sys::WindowsEncodingMethod ResponseEncoding
The encoding to use when writing response files on Windows.
Definition Job.h:71
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
Definition Job.h:79
static constexpr ResponseFileSupport AtFileUTF8()
Definition Job.h:86
const char * ResponseFlag
What prefix to use for the command-line argument when passing a response file.
Definition Job.h:75
static constexpr ResponseFileSupport AtFileCurCP()
Definition Job.h:93
static constexpr ResponseFileSupport AtFileUTF16()
Definition Job.h:100