1 //===--- Job.h - Commands to Execute ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_DRIVER_JOB_H
11 #define LLVM_CLANG_DRIVER_JOB_H
13 #include "clang/Basic/LLVM.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/iterator.h"
16 #include "llvm/Option/Option.h"
29 // Re-export this as clang::driver::ArgStringList.
30 using llvm::opt::ArgStringList;
32 struct CrashReportInfo {
36 CrashReportInfo(StringRef Filename, StringRef VFSPath)
37 : Filename(Filename), VFSPath(VFSPath) {}
52 Job(JobClass _Kind) : Kind(_Kind) {}
56 JobClass getKind() const { return Kind; }
58 /// Print - Print this Job in -### format.
60 /// \param OS - The stream to print on.
61 /// \param Terminator - A string to print at the end of the line.
62 /// \param Quote - Should separate arguments be quoted.
63 /// \param CrashInfo - Details for inclusion in a crash report.
64 virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
65 CrashReportInfo *CrashInfo = nullptr) const = 0;
68 /// Command - An executable path/name and argument vector to
70 class Command : public Job {
71 /// Source - The action which caused the creation of this job.
74 /// Tool - The tool which caused the creation of this job.
77 /// The executable to run.
78 const char *Executable;
80 /// The list of program arguments (not including the implicit first
81 /// argument, which will be the executable).
82 llvm::opt::ArgStringList Arguments;
84 /// Response file name, if this command is set to use one, or nullptr
86 const char *ResponseFile;
88 /// The input file list in case we need to emit a file list instead of a
89 /// proper response file
90 llvm::opt::ArgStringList InputFileList;
92 /// String storage if we need to create a new argument to specify a response
94 std::string ResponseFileFlag;
96 /// When a response file is needed, we try to put most arguments in an
97 /// exclusive file, while others remains as regular command line arguments.
98 /// This functions fills a vector with the regular command line arguments,
99 /// argv, excluding the ones passed in a response file.
100 void buildArgvForResponseFile(llvm::SmallVectorImpl<const char *> &Out) const;
102 /// Encodes an array of C strings into a single string separated by whitespace.
103 /// This function will also put in quotes arguments that have whitespaces and
104 /// will escape the regular backslashes (used in Windows paths) and quotes.
105 /// The results are the contents of a response file, written into a raw_ostream.
106 void writeResponseFile(raw_ostream &OS) const;
109 Command(const Action &_Source, const Tool &_Creator, const char *_Executable,
110 const llvm::opt::ArgStringList &_Arguments);
112 void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
113 CrashReportInfo *CrashInfo = nullptr) const override;
115 virtual int Execute(const StringRef **Redirects, std::string *ErrMsg,
116 bool *ExecutionFailed) const;
118 /// getSource - Return the Action which caused the creation of this job.
119 const Action &getSource() const { return Source; }
121 /// getCreator - Return the Tool which caused the creation of this job.
122 const Tool &getCreator() const { return Creator; }
124 /// Set to pass arguments via a response file when launching the command
125 void setResponseFile(const char *FileName);
127 /// Set an input file list, necessary if we need to use a response file but
128 /// the tool being called only supports input files lists.
129 void setInputFileList(llvm::opt::ArgStringList List) {
130 InputFileList = std::move(List);
133 const char *getExecutable() const { return Executable; }
135 const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
137 static bool classof(const Job *J) {
138 return J->getKind() == CommandClass ||
139 J->getKind() == FallbackCommandClass;
143 /// Like Command, but with a fallback which is executed in case
144 /// the primary command crashes.
145 class FallbackCommand : public Command {
147 FallbackCommand(const Action &Source_, const Tool &Creator_,
148 const char *Executable_, const ArgStringList &Arguments_,
149 std::unique_ptr<Command> Fallback_);
151 void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
152 CrashReportInfo *CrashInfo = nullptr) const override;
154 int Execute(const StringRef **Redirects, std::string *ErrMsg,
155 bool *ExecutionFailed) const override;
157 static bool classof(const Job *J) {
158 return J->getKind() == FallbackCommandClass;
162 std::unique_ptr<Command> Fallback;
165 /// JobList - A sequence of jobs to perform.
166 class JobList : public Job {
168 typedef SmallVector<std::unique_ptr<Job>, 4> list_type;
169 typedef list_type::size_type size_type;
170 typedef llvm::pointee_iterator<list_type::iterator> iterator;
171 typedef llvm::pointee_iterator<list_type::const_iterator> const_iterator;
178 virtual ~JobList() {}
180 void Print(llvm::raw_ostream &OS, const char *Terminator,
181 bool Quote, CrashReportInfo *CrashInfo = nullptr) const override;
183 /// Add a job to the list (taking ownership).
184 void addJob(std::unique_ptr<Job> J) { Jobs.push_back(std::move(J)); }
186 /// Clear the job list.
189 const list_type &getJobs() const { return Jobs; }
191 size_type size() const { return Jobs.size(); }
192 iterator begin() { return Jobs.begin(); }
193 const_iterator begin() const { return Jobs.begin(); }
194 iterator end() { return Jobs.end(); }
195 const_iterator end() const { return Jobs.end(); }
197 static bool classof(const Job *J) {
198 return J->getKind() == JobListClass;
202 } // end namespace driver
203 } // end namespace clang