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"
30 // Re-export this as clang::driver::ArgStringList.
31 using llvm::opt::ArgStringList;
33 struct CrashReportInfo {
37 CrashReportInfo(StringRef Filename, StringRef VFSPath)
38 : Filename(Filename), VFSPath(VFSPath) {}
41 /// Command - An executable path/name and argument vector to
44 /// Source - The action which caused the creation of this job.
47 /// Tool - The tool which caused the creation of this job.
50 /// The executable to run.
51 const char *Executable;
53 /// The list of program arguments (not including the implicit first
54 /// argument, which will be the executable).
55 llvm::opt::ArgStringList Arguments;
57 /// The list of program arguments which are inputs.
58 llvm::opt::ArgStringList InputFilenames;
60 /// Response file name, if this command is set to use one, or nullptr
62 const char *ResponseFile;
64 /// The input file list in case we need to emit a file list instead of a
65 /// proper response file
66 llvm::opt::ArgStringList InputFileList;
68 /// String storage if we need to create a new argument to specify a response
70 std::string ResponseFileFlag;
72 /// When a response file is needed, we try to put most arguments in an
73 /// exclusive file, while others remains as regular command line arguments.
74 /// This functions fills a vector with the regular command line arguments,
75 /// argv, excluding the ones passed in a response file.
76 void buildArgvForResponseFile(llvm::SmallVectorImpl<const char *> &Out) const;
78 /// Encodes an array of C strings into a single string separated by whitespace.
79 /// This function will also put in quotes arguments that have whitespaces and
80 /// will escape the regular backslashes (used in Windows paths) and quotes.
81 /// The results are the contents of a response file, written into a raw_ostream.
82 void writeResponseFile(raw_ostream &OS) const;
85 Command(const Action &Source, const Tool &Creator, const char *Executable,
86 const llvm::opt::ArgStringList &Arguments,
87 ArrayRef<InputInfo> Inputs);
88 // FIXME: This really shouldn't be copyable, but is currently copied in some
89 // error handling in Driver::generateCompilationDiagnostics.
90 Command(const Command &) = default;
93 virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
94 CrashReportInfo *CrashInfo = nullptr) const;
96 virtual int Execute(const StringRef **Redirects, std::string *ErrMsg,
97 bool *ExecutionFailed) const;
99 /// getSource - Return the Action which caused the creation of this job.
100 const Action &getSource() const { return Source; }
102 /// getCreator - Return the Tool which caused the creation of this job.
103 const Tool &getCreator() const { return Creator; }
105 /// Set to pass arguments via a response file when launching the command
106 void setResponseFile(const char *FileName);
108 /// Set an input file list, necessary if we need to use a response file but
109 /// the tool being called only supports input files lists.
110 void setInputFileList(llvm::opt::ArgStringList List) {
111 InputFileList = std::move(List);
114 const char *getExecutable() const { return Executable; }
116 const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
118 /// Print a command argument, and optionally quote it.
119 static void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
122 /// Like Command, but with a fallback which is executed in case
123 /// the primary command crashes.
124 class FallbackCommand : public Command {
126 FallbackCommand(const Action &Source_, const Tool &Creator_,
127 const char *Executable_, const ArgStringList &Arguments_,
128 ArrayRef<InputInfo> Inputs,
129 std::unique_ptr<Command> Fallback_);
131 void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
132 CrashReportInfo *CrashInfo = nullptr) const override;
134 int Execute(const StringRef **Redirects, std::string *ErrMsg,
135 bool *ExecutionFailed) const override;
138 std::unique_ptr<Command> Fallback;
141 /// Like Command, but always pretends that the wrapped command succeeded.
142 class ForceSuccessCommand : public Command {
144 ForceSuccessCommand(const Action &Source_, const Tool &Creator_,
145 const char *Executable_, const ArgStringList &Arguments_,
146 ArrayRef<InputInfo> Inputs);
148 void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
149 CrashReportInfo *CrashInfo = nullptr) const override;
151 int Execute(const StringRef **Redirects, std::string *ErrMsg,
152 bool *ExecutionFailed) const override;
155 /// JobList - A sequence of jobs to perform.
158 typedef SmallVector<std::unique_ptr<Command>, 4> list_type;
159 typedef list_type::size_type size_type;
160 typedef llvm::pointee_iterator<list_type::iterator> iterator;
161 typedef llvm::pointee_iterator<list_type::const_iterator> const_iterator;
167 void Print(llvm::raw_ostream &OS, const char *Terminator,
168 bool Quote, CrashReportInfo *CrashInfo = nullptr) const;
170 /// Add a job to the list (taking ownership).
171 void addJob(std::unique_ptr<Command> J) { Jobs.push_back(std::move(J)); }
173 /// Clear the job list.
176 const list_type &getJobs() const { return Jobs; }
178 bool empty() const { return Jobs.empty(); }
179 size_type size() const { return Jobs.size(); }
180 iterator begin() { return Jobs.begin(); }
181 const_iterator begin() const { return Jobs.begin(); }
182 iterator end() { return Jobs.end(); }
183 const_iterator end() const { return Jobs.end(); }
186 } // end namespace driver
187 } // end namespace clang