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) {}
40 /// Command - An executable path/name and argument vector to
43 /// Source - The action which caused the creation of this job.
46 /// Tool - The tool which caused the creation of this job.
49 /// The executable to run.
50 const char *Executable;
52 /// The list of program arguments (not including the implicit first
53 /// argument, which will be the executable).
54 llvm::opt::ArgStringList Arguments;
56 /// Response file name, if this command is set to use one, or nullptr
58 const char *ResponseFile;
60 /// The input file list in case we need to emit a file list instead of a
61 /// proper response file
62 llvm::opt::ArgStringList InputFileList;
64 /// String storage if we need to create a new argument to specify a response
66 std::string ResponseFileFlag;
68 /// When a response file is needed, we try to put most arguments in an
69 /// exclusive file, while others remains as regular command line arguments.
70 /// This functions fills a vector with the regular command line arguments,
71 /// argv, excluding the ones passed in a response file.
72 void buildArgvForResponseFile(llvm::SmallVectorImpl<const char *> &Out) const;
74 /// Encodes an array of C strings into a single string separated by whitespace.
75 /// This function will also put in quotes arguments that have whitespaces and
76 /// will escape the regular backslashes (used in Windows paths) and quotes.
77 /// The results are the contents of a response file, written into a raw_ostream.
78 void writeResponseFile(raw_ostream &OS) const;
81 Command(const Action &Source, const Tool &Creator, const char *Executable,
82 const llvm::opt::ArgStringList &Arguments);
85 virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
86 CrashReportInfo *CrashInfo = nullptr) const;
88 virtual int Execute(const StringRef **Redirects, std::string *ErrMsg,
89 bool *ExecutionFailed) const;
91 /// getSource - Return the Action which caused the creation of this job.
92 const Action &getSource() const { return Source; }
94 /// getCreator - Return the Tool which caused the creation of this job.
95 const Tool &getCreator() const { return Creator; }
97 /// Set to pass arguments via a response file when launching the command
98 void setResponseFile(const char *FileName);
100 /// Set an input file list, necessary if we need to use a response file but
101 /// the tool being called only supports input files lists.
102 void setInputFileList(llvm::opt::ArgStringList List) {
103 InputFileList = std::move(List);
106 const char *getExecutable() const { return Executable; }
108 const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
110 /// Print a command argument, and optionally quote it.
111 static void printArg(llvm::raw_ostream &OS, const char *Arg, bool Quote);
114 /// Like Command, but with a fallback which is executed in case
115 /// the primary command crashes.
116 class FallbackCommand : public Command {
118 FallbackCommand(const Action &Source_, const Tool &Creator_,
119 const char *Executable_, const ArgStringList &Arguments_,
120 std::unique_ptr<Command> Fallback_);
122 void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
123 CrashReportInfo *CrashInfo = nullptr) const override;
125 int Execute(const StringRef **Redirects, std::string *ErrMsg,
126 bool *ExecutionFailed) const override;
129 std::unique_ptr<Command> Fallback;
132 /// JobList - A sequence of jobs to perform.
135 typedef SmallVector<std::unique_ptr<Command>, 4> list_type;
136 typedef list_type::size_type size_type;
137 typedef llvm::pointee_iterator<list_type::iterator> iterator;
138 typedef llvm::pointee_iterator<list_type::const_iterator> const_iterator;
144 void Print(llvm::raw_ostream &OS, const char *Terminator,
145 bool Quote, CrashReportInfo *CrashInfo = nullptr) const;
147 /// Add a job to the list (taking ownership).
148 void addJob(std::unique_ptr<Command> J) { Jobs.push_back(std::move(J)); }
150 /// Clear the job list.
153 const list_type &getJobs() const { return Jobs; }
155 size_type size() const { return Jobs.size(); }
156 iterator begin() { return Jobs.begin(); }
157 const_iterator begin() const { return Jobs.begin(); }
158 iterator end() { return Jobs.end(); }
159 const_iterator end() const { return Jobs.end(); }
162 } // end namespace driver
163 } // end namespace clang