clang 23.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", "x86_64").
155 /// Non-empty only for Darwin multi-arch builds.
156 std::string BoundArchStr;
157
158 /// When a response file is needed, we try to put most arguments in an
159 /// exclusive file, while others remains as regular command line arguments.
160 /// This functions fills a vector with the regular command line arguments,
161 /// argv, excluding the ones passed in a response file.
162 void buildArgvForResponseFile(llvm::SmallVectorImpl<const char *> &Out) const;
163
164 /// Encodes an array of C strings into a single string separated by whitespace.
165 /// This function will also put in quotes arguments that have whitespaces and
166 /// will escape the regular backslashes (used in Windows paths) and quotes.
167 /// The results are the contents of a response file, written into a raw_ostream.
168 void writeResponseFile(raw_ostream &OS) const;
169
170public:
171 /// Whether to print the input filenames when executing.
173
174 /// Whether the command will be executed in this process or not.
175 bool InProcess = false;
176
177 Command(const Action &Source, const Tool &Creator,
178 ResponseFileSupport ResponseSupport, const char *Executable,
179 const llvm::opt::ArgStringList &Arguments, ArrayRef<InputInfo> Inputs,
180 ArrayRef<InputInfo> Outputs = {}, const char *PrependArg = nullptr);
181 // FIXME: This really shouldn't be copyable, but is currently copied in some
182 // error handling in Driver::generateCompilationDiagnostics.
183 Command(const Command &) = default;
184 virtual ~Command() = default;
185
186 virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
187 CrashReportInfo *CrashInfo = nullptr) const;
188
189 virtual int Execute(ArrayRef<std::optional<StringRef>> Redirects,
190 std::string *ErrMsg, bool *ExecutionFailed) const;
191
192 /// getSource - Return the Action which caused the creation of this job.
193 const Action &getSource() const { return Source; }
194
195 /// getCreator - Return the Tool which caused the creation of this job.
196 const Tool &getCreator() const { return Creator; }
197
198 /// Return the bound architecture for this command, if any.
199 BoundArch getBoundArch() const { return BoundArch(BoundArchStr); }
200 void setBoundArch(BoundArch BA) { BoundArchStr = BA.ArchName.str(); }
201
202 /// Returns the kind of response file supported by the current invocation.
204 return ResponseSupport;
205 }
206
207 /// Set to pass arguments via a response file when launching the command
208 void setResponseFile(const char *FileName);
209
210 /// Set an input file list, necessary if you specified an RF_FileList response
211 /// file support.
212 void setInputFileList(llvm::opt::ArgStringList List) {
213 InputFileList = std::move(List);
214 }
215
216 /// Sets the environment to be used by the new process.
217 /// \param NewEnvironment An array of environment variables.
218 /// \remark If the environment remains unset, then the environment
219 /// from the parent process will be used.
220 virtual void setEnvironment(llvm::ArrayRef<const char *> NewEnvironment);
221
222 void
223 setRedirectFiles(const std::vector<std::optional<std::string>> &Redirects);
224
225 void replaceArguments(llvm::opt::ArgStringList List) {
226 Arguments = std::move(List);
227 }
228
229 void replaceExecutable(const char *Exe) { Executable = Exe; }
230
231 const char *getExecutable() const { return Executable; }
232
233 const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
234
235 const std::vector<InputInfo> &getInputInfos() const { return InputInfoList; }
236
237 const std::vector<std::string> &getOutputFilenames() const {
238 return OutputFilenames;
239 }
240
241 std::optional<llvm::sys::ProcessStatistics> getProcessStatistics() const {
242 return ProcStat;
243 }
244
245protected:
246 /// Optionally print the filenames to be compiled
247 void PrintFileNames() const;
248};
249
250/// Use the CC1 tool callback when available, to avoid creating a new process
251class CC1Command : public Command {
252public:
253 CC1Command(const Action &Source, const Tool &Creator,
254 ResponseFileSupport ResponseSupport, const char *Executable,
255 const llvm::opt::ArgStringList &Arguments,
256 ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs = {},
257 const char *PrependArg = nullptr);
258
259 void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
260 CrashReportInfo *CrashInfo = nullptr) const override;
261
262 int Execute(ArrayRef<std::optional<StringRef>> Redirects, std::string *ErrMsg,
263 bool *ExecutionFailed) const override;
264
265 void setEnvironment(llvm::ArrayRef<const char *> NewEnvironment) override;
266};
267
268/// JobList - A sequence of jobs to perform.
269class JobList {
270public:
272 using size_type = list_type::size_type;
273 using iterator = llvm::pointee_iterator<list_type::iterator>;
274 using const_iterator = llvm::pointee_iterator<list_type::const_iterator>;
275
276private:
277 list_type Jobs;
278
279public:
280 void Print(llvm::raw_ostream &OS, const char *Terminator,
281 bool Quote, CrashReportInfo *CrashInfo = nullptr) const;
282
283 /// Add a job to the list (taking ownership).
284 void addJob(std::unique_ptr<Command> J) { Jobs.push_back(std::move(J)); }
285
286 /// Clear the job list.
287 void clear();
288
289 const list_type &getJobs() const { return Jobs; }
290
291 // Returns and transfers ownership of all jobs, leaving this list empty.
292 list_type takeJobs() { return std::exchange(Jobs, {}); };
293
294 bool empty() const { return Jobs.empty(); }
295 size_type size() const { return Jobs.size(); }
296 iterator begin() { return Jobs.begin(); }
297 const_iterator begin() const { return Jobs.begin(); }
298 iterator end() { return Jobs.end(); }
299 const_iterator end() const { return Jobs.end(); }
300};
301
302} // namespace driver
303} // namespace clang
304
305#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:193
const std::vector< std::string > & getOutputFilenames() const
Definition Job.h:237
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition Job.h:196
void setBoundArch(BoundArch BA)
Definition Job.h:200
const llvm::opt::ArgStringList & getArguments() const
Definition Job.h:233
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:229
void setInputFileList(llvm::opt::ArgStringList List)
Set an input file list, necessary if you specified an RF_FileList response file support.
Definition Job.h:212
bool PrintInputFilenames
Whether to print the input filenames when executing.
Definition Job.h:172
std::optional< llvm::sys::ProcessStatistics > getProcessStatistics() const
Definition Job.h:241
const char * getExecutable() const
Definition Job.h:231
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:203
bool InProcess
Whether the command will be executed in this process or not.
Definition Job.h:175
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:225
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:199
const std::vector< InputInfo > & getInputInfos() const
Definition Job.h:235
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:269
list_type takeJobs()
Definition Job.h:292
size_type size() const
Definition Job.h:295
bool empty() const
Definition Job.h:294
list_type::size_type size_type
Definition Job.h:272
SmallVector< std::unique_ptr< Command >, 4 > list_type
Definition Job.h:271
void clear()
Clear the job list.
Definition Job.cpp:461
const_iterator begin() const
Definition Job.h:297
const list_type & getJobs() const
Definition Job.h:289
void addJob(std::unique_ptr< Command > J)
Add a job to the list (taking ownership).
Definition Job.h:284
const_iterator end() const
Definition Job.h:299
iterator end()
Definition Job.h:298
llvm::pointee_iterator< list_type::iterator > iterator
Definition Job.h:273
llvm::pointee_iterator< list_type::const_iterator > const_iterator
Definition Job.h:274
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition Job.cpp:455
iterator begin()
Definition Job.h:296
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