xref: /aosp_15_r20/external/clang/include/clang/Driver/Job.h (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===--- Job.h - Commands to Execute ----------------------------*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li 
10*67e74705SXin Li #ifndef LLVM_CLANG_DRIVER_JOB_H
11*67e74705SXin Li #define LLVM_CLANG_DRIVER_JOB_H
12*67e74705SXin Li 
13*67e74705SXin Li #include "clang/Basic/LLVM.h"
14*67e74705SXin Li #include "llvm/ADT/SmallVector.h"
15*67e74705SXin Li #include "llvm/ADT/iterator.h"
16*67e74705SXin Li #include "llvm/Option/Option.h"
17*67e74705SXin Li #include <memory>
18*67e74705SXin Li 
19*67e74705SXin Li namespace llvm {
20*67e74705SXin Li   class raw_ostream;
21*67e74705SXin Li }
22*67e74705SXin Li 
23*67e74705SXin Li namespace clang {
24*67e74705SXin Li namespace driver {
25*67e74705SXin Li class Action;
26*67e74705SXin Li class Command;
27*67e74705SXin Li class Tool;
28*67e74705SXin Li class InputInfo;
29*67e74705SXin Li 
30*67e74705SXin Li // Re-export this as clang::driver::ArgStringList.
31*67e74705SXin Li using llvm::opt::ArgStringList;
32*67e74705SXin Li 
33*67e74705SXin Li struct CrashReportInfo {
34*67e74705SXin Li   StringRef Filename;
35*67e74705SXin Li   StringRef VFSPath;
36*67e74705SXin Li 
CrashReportInfoCrashReportInfo37*67e74705SXin Li   CrashReportInfo(StringRef Filename, StringRef VFSPath)
38*67e74705SXin Li       : Filename(Filename), VFSPath(VFSPath) {}
39*67e74705SXin Li };
40*67e74705SXin Li 
41*67e74705SXin Li /// Command - An executable path/name and argument vector to
42*67e74705SXin Li /// execute.
43*67e74705SXin Li class Command {
44*67e74705SXin Li   /// Source - The action which caused the creation of this job.
45*67e74705SXin Li   const Action &Source;
46*67e74705SXin Li 
47*67e74705SXin Li   /// Tool - The tool which caused the creation of this job.
48*67e74705SXin Li   const Tool &Creator;
49*67e74705SXin Li 
50*67e74705SXin Li   /// The executable to run.
51*67e74705SXin Li   const char *Executable;
52*67e74705SXin Li 
53*67e74705SXin Li   /// The list of program arguments (not including the implicit first
54*67e74705SXin Li   /// argument, which will be the executable).
55*67e74705SXin Li   llvm::opt::ArgStringList Arguments;
56*67e74705SXin Li 
57*67e74705SXin Li   /// The list of program arguments which are inputs.
58*67e74705SXin Li   llvm::opt::ArgStringList InputFilenames;
59*67e74705SXin Li 
60*67e74705SXin Li   /// Response file name, if this command is set to use one, or nullptr
61*67e74705SXin Li   /// otherwise
62*67e74705SXin Li   const char *ResponseFile;
63*67e74705SXin Li 
64*67e74705SXin Li   /// The input file list in case we need to emit a file list instead of a
65*67e74705SXin Li   /// proper response file
66*67e74705SXin Li   llvm::opt::ArgStringList InputFileList;
67*67e74705SXin Li 
68*67e74705SXin Li   /// String storage if we need to create a new argument to specify a response
69*67e74705SXin Li   /// file
70*67e74705SXin Li   std::string ResponseFileFlag;
71*67e74705SXin Li 
72*67e74705SXin Li   /// When a response file is needed, we try to put most arguments in an
73*67e74705SXin Li   /// exclusive file, while others remains as regular command line arguments.
74*67e74705SXin Li   /// This functions fills a vector with the regular command line arguments,
75*67e74705SXin Li   /// argv, excluding the ones passed in a response file.
76*67e74705SXin Li   void buildArgvForResponseFile(llvm::SmallVectorImpl<const char *> &Out) const;
77*67e74705SXin Li 
78*67e74705SXin Li   /// Encodes an array of C strings into a single string separated by whitespace.
79*67e74705SXin Li   /// This function will also put in quotes arguments that have whitespaces and
80*67e74705SXin Li   /// will escape the regular backslashes (used in Windows paths) and quotes.
81*67e74705SXin Li   /// The results are the contents of a response file, written into a raw_ostream.
82*67e74705SXin Li   void writeResponseFile(raw_ostream &OS) const;
83*67e74705SXin Li 
84*67e74705SXin Li public:
85*67e74705SXin Li   Command(const Action &Source, const Tool &Creator, const char *Executable,
86*67e74705SXin Li           const llvm::opt::ArgStringList &Arguments,
87*67e74705SXin Li           ArrayRef<InputInfo> Inputs);
88*67e74705SXin Li   // FIXME: This really shouldn't be copyable, but is currently copied in some
89*67e74705SXin Li   // error handling in Driver::generateCompilationDiagnostics.
90*67e74705SXin Li   Command(const Command &) = default;
~Command()91*67e74705SXin Li   virtual ~Command() {}
92*67e74705SXin Li 
93*67e74705SXin Li   virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
94*67e74705SXin Li                      CrashReportInfo *CrashInfo = nullptr) const;
95*67e74705SXin Li 
96*67e74705SXin Li   virtual int Execute(const StringRef **Redirects, std::string *ErrMsg,
97*67e74705SXin Li                       bool *ExecutionFailed) const;
98*67e74705SXin Li 
99*67e74705SXin Li   /// getSource - Return the Action which caused the creation of this job.
getSource()100*67e74705SXin Li   const Action &getSource() const { return Source; }
101*67e74705SXin Li 
102*67e74705SXin Li   /// getCreator - Return the Tool which caused the creation of this job.
getCreator()103*67e74705SXin Li   const Tool &getCreator() const { return Creator; }
104*67e74705SXin Li 
105*67e74705SXin Li   /// Set to pass arguments via a response file when launching the command
106*67e74705SXin Li   void setResponseFile(const char *FileName);
107*67e74705SXin Li 
108*67e74705SXin Li   /// Set an input file list, necessary if we need to use a response file but
109*67e74705SXin Li   /// the tool being called only supports input files lists.
setInputFileList(llvm::opt::ArgStringList List)110*67e74705SXin Li   void setInputFileList(llvm::opt::ArgStringList List) {
111*67e74705SXin Li     InputFileList = std::move(List);
112*67e74705SXin Li   }
113*67e74705SXin Li 
getExecutable()114*67e74705SXin Li   const char *getExecutable() const { return Executable; }
115*67e74705SXin Li 
getArguments()116*67e74705SXin Li   const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
117*67e74705SXin Li 
118*67e74705SXin Li   /// Print a command argument, and optionally quote it.
119*67e74705SXin Li   static void printArg(llvm::raw_ostream &OS, const char *Arg, bool Quote);
120*67e74705SXin Li };
121*67e74705SXin Li 
122*67e74705SXin Li /// Like Command, but with a fallback which is executed in case
123*67e74705SXin Li /// the primary command crashes.
124*67e74705SXin Li class FallbackCommand : public Command {
125*67e74705SXin Li public:
126*67e74705SXin Li   FallbackCommand(const Action &Source_, const Tool &Creator_,
127*67e74705SXin Li                   const char *Executable_, const ArgStringList &Arguments_,
128*67e74705SXin Li                   ArrayRef<InputInfo> Inputs,
129*67e74705SXin Li                   std::unique_ptr<Command> Fallback_);
130*67e74705SXin Li 
131*67e74705SXin Li   void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
132*67e74705SXin Li              CrashReportInfo *CrashInfo = nullptr) const override;
133*67e74705SXin Li 
134*67e74705SXin Li   int Execute(const StringRef **Redirects, std::string *ErrMsg,
135*67e74705SXin Li               bool *ExecutionFailed) const override;
136*67e74705SXin Li 
137*67e74705SXin Li private:
138*67e74705SXin Li   std::unique_ptr<Command> Fallback;
139*67e74705SXin Li };
140*67e74705SXin Li 
141*67e74705SXin Li /// Like Command, but always pretends that the wrapped command succeeded.
142*67e74705SXin Li class ForceSuccessCommand : public Command {
143*67e74705SXin Li public:
144*67e74705SXin Li   ForceSuccessCommand(const Action &Source_, const Tool &Creator_,
145*67e74705SXin Li                       const char *Executable_, const ArgStringList &Arguments_,
146*67e74705SXin Li                       ArrayRef<InputInfo> Inputs);
147*67e74705SXin Li 
148*67e74705SXin Li   void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
149*67e74705SXin Li              CrashReportInfo *CrashInfo = nullptr) const override;
150*67e74705SXin Li 
151*67e74705SXin Li   int Execute(const StringRef **Redirects, std::string *ErrMsg,
152*67e74705SXin Li               bool *ExecutionFailed) const override;
153*67e74705SXin Li };
154*67e74705SXin Li 
155*67e74705SXin Li /// JobList - A sequence of jobs to perform.
156*67e74705SXin Li class JobList {
157*67e74705SXin Li public:
158*67e74705SXin Li   typedef SmallVector<std::unique_ptr<Command>, 4> list_type;
159*67e74705SXin Li   typedef list_type::size_type size_type;
160*67e74705SXin Li   typedef llvm::pointee_iterator<list_type::iterator> iterator;
161*67e74705SXin Li   typedef llvm::pointee_iterator<list_type::const_iterator> const_iterator;
162*67e74705SXin Li 
163*67e74705SXin Li private:
164*67e74705SXin Li   list_type Jobs;
165*67e74705SXin Li 
166*67e74705SXin Li public:
167*67e74705SXin Li   void Print(llvm::raw_ostream &OS, const char *Terminator,
168*67e74705SXin Li              bool Quote, CrashReportInfo *CrashInfo = nullptr) const;
169*67e74705SXin Li 
170*67e74705SXin Li   /// Add a job to the list (taking ownership).
addJob(std::unique_ptr<Command> J)171*67e74705SXin Li   void addJob(std::unique_ptr<Command> J) { Jobs.push_back(std::move(J)); }
172*67e74705SXin Li 
173*67e74705SXin Li   /// Clear the job list.
174*67e74705SXin Li   void clear();
175*67e74705SXin Li 
getJobs()176*67e74705SXin Li   const list_type &getJobs() const { return Jobs; }
177*67e74705SXin Li 
size()178*67e74705SXin Li   size_type size() const { return Jobs.size(); }
begin()179*67e74705SXin Li   iterator begin() { return Jobs.begin(); }
begin()180*67e74705SXin Li   const_iterator begin() const { return Jobs.begin(); }
end()181*67e74705SXin Li   iterator end() { return Jobs.end(); }
end()182*67e74705SXin Li   const_iterator end() const { return Jobs.end(); }
183*67e74705SXin Li };
184*67e74705SXin Li 
185*67e74705SXin Li } // end namespace driver
186*67e74705SXin Li } // end namespace clang
187*67e74705SXin Li 
188*67e74705SXin Li #endif
189