1 //===--- Compilation.h - Compilation Task Data Structure --------*- 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_COMPILATION_H
11 #define LLVM_CLANG_DRIVER_COMPILATION_H
13 #include "clang/Driver/Action.h"
14 #include "clang/Driver/Job.h"
15 #include "clang/Driver/Util.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/Support/Path.h"
33 /// Compilation - A set of tasks to perform for a single driver
36 /// The driver we were created by.
37 const Driver &TheDriver;
39 /// The default tool chain.
40 const ToolChain &DefaultToolChain;
42 /// A mask of all the programming models the host has to support in the
43 /// current compilation.
44 unsigned ActiveOffloadMask;
46 /// Array with the toolchains of offloading host and devices in the order they
47 /// were requested by the user. We are preserving that order in case the code
48 /// generation needs to derive a programming-model-specific semantic out of
50 std::multimap<Action::OffloadKind, const ToolChain *>
51 OrderedOffloadingToolchains;
53 /// The original (untranslated) input argument list.
54 llvm::opt::InputArgList *Args;
56 /// The driver translated arguments. Note that toolchains may perform their
57 /// own argument translation.
58 llvm::opt::DerivedArgList *TranslatedArgs;
60 /// The list of actions we've created via MakeAction. This is not accessible
61 /// to consumers; it's here just to manage ownership.
62 std::vector<std::unique_ptr<Action>> AllActions;
64 /// The list of actions. This is maintained and modified by consumers, via
68 /// The root list of jobs.
71 /// Cache of translated arguments for a particular tool chain and bound
73 llvm::DenseMap<std::pair<const ToolChain *, const char *>,
74 llvm::opt::DerivedArgList *> TCArgs;
76 /// Temporary files which should be removed on exit.
77 llvm::opt::ArgStringList TempFiles;
79 /// Result files which should be removed on failure.
80 ArgStringMap ResultFiles;
82 /// Result files which are generated correctly on failure, and which should
83 /// only be removed if we crash.
84 ArgStringMap FailureResultFiles;
86 /// Redirection for stdout, stderr, etc.
87 const StringRef **Redirects;
89 /// Whether we're compiling for diagnostic purposes.
93 Compilation(const Driver &D, const ToolChain &DefaultToolChain,
94 llvm::opt::InputArgList *Args,
95 llvm::opt::DerivedArgList *TranslatedArgs);
98 const Driver &getDriver() const { return TheDriver; }
100 const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
102 unsigned isOffloadingHostKind(Action::OffloadKind Kind) const {
103 return ActiveOffloadMask & Kind;
106 /// Iterator that visits device toolchains of a given kind.
107 typedef const std::multimap<Action::OffloadKind,
108 const ToolChain *>::const_iterator
109 const_offload_toolchains_iterator;
110 typedef std::pair<const_offload_toolchains_iterator,
111 const_offload_toolchains_iterator>
112 const_offload_toolchains_range;
114 template <Action::OffloadKind Kind>
115 const_offload_toolchains_range getOffloadToolChains() const {
116 return OrderedOffloadingToolchains.equal_range(Kind);
119 /// Return an offload toolchain of the provided kind. Only one is expected to
121 template <Action::OffloadKind Kind>
122 const ToolChain *getSingleOffloadToolChain() const {
123 auto TCs = getOffloadToolChains<Kind>();
125 assert(TCs.first != TCs.second &&
126 "No tool chains of the selected kind exist!");
127 assert(std::next(TCs.first) == TCs.second &&
128 "More than one tool chain of the this kind exist.");
129 return TCs.first->second;
132 void addOffloadDeviceToolChain(const ToolChain *DeviceToolChain,
133 Action::OffloadKind OffloadKind) {
134 assert(OffloadKind != Action::OFK_Host && OffloadKind != Action::OFK_None &&
135 "This is not a device tool chain!");
137 // Update the host offload kind to also contain this kind.
138 ActiveOffloadMask |= OffloadKind;
139 OrderedOffloadingToolchains.insert(
140 std::make_pair(OffloadKind, DeviceToolChain));
143 const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
145 const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
147 llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
149 ActionList &getActions() { return Actions; }
150 const ActionList &getActions() const { return Actions; }
152 /// Creates a new Action owned by this Compilation.
154 /// The new Action is *not* added to the list returned by getActions().
155 template <typename T, typename... Args> T *MakeAction(Args &&... Arg) {
156 T *RawPtr = new T(std::forward<Args>(Arg)...);
157 AllActions.push_back(std::unique_ptr<Action>(RawPtr));
161 JobList &getJobs() { return Jobs; }
162 const JobList &getJobs() const { return Jobs; }
164 void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
166 const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
168 const ArgStringMap &getResultFiles() const { return ResultFiles; }
170 const ArgStringMap &getFailureResultFiles() const {
171 return FailureResultFiles;
174 /// Returns the sysroot path.
175 StringRef getSysRoot() const;
177 /// getArgsForToolChain - Return the derived argument list for the
178 /// tool chain \p TC (or the default tool chain, if TC is not specified).
180 /// \param BoundArch - The bound architecture name, or 0.
181 const llvm::opt::DerivedArgList &getArgsForToolChain(const ToolChain *TC,
182 const char *BoundArch);
184 /// addTempFile - Add a file to remove on exit, and returns its
186 const char *addTempFile(const char *Name) {
187 TempFiles.push_back(Name);
191 /// addResultFile - Add a file to remove on failure, and returns its
193 const char *addResultFile(const char *Name, const JobAction *JA) {
194 ResultFiles[JA] = Name;
198 /// addFailureResultFile - Add a file to remove if we crash, and returns its
200 const char *addFailureResultFile(const char *Name, const JobAction *JA) {
201 FailureResultFiles[JA] = Name;
205 /// CleanupFile - Delete a given file.
207 /// \param IssueErrors - Report failures as errors.
208 /// \return Whether the file was removed successfully.
209 bool CleanupFile(const char *File, bool IssueErrors = false) const;
211 /// CleanupFileList - Remove the files in the given list.
213 /// \param IssueErrors - Report failures as errors.
214 /// \return Whether all files were removed successfully.
215 bool CleanupFileList(const llvm::opt::ArgStringList &Files,
216 bool IssueErrors = false) const;
218 /// CleanupFileMap - Remove the files in the given map.
220 /// \param JA - If specified, only delete the files associated with this
221 /// JobAction. Otherwise, delete all files in the map.
222 /// \param IssueErrors - Report failures as errors.
223 /// \return Whether all files were removed successfully.
224 bool CleanupFileMap(const ArgStringMap &Files,
226 bool IssueErrors = false) const;
228 /// ExecuteCommand - Execute an actual command.
230 /// \param FailingCommand - For non-zero results, this will be set to the
231 /// Command which failed, if any.
232 /// \return The result code of the subprocess.
233 int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
235 /// ExecuteJob - Execute a single job.
237 /// \param FailingCommands - For non-zero results, this will be a vector of
238 /// failing commands and their associated result code.
241 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
243 /// initCompilationForDiagnostics - Remove stale state and suppress output
244 /// so compilation can be reexecuted to generate additional diagnostic
245 /// information (e.g., preprocessed source(s)).
246 void initCompilationForDiagnostics();
248 /// Return true if we're compiling for diagnostics.
249 bool isForDiagnostics() const { return ForDiagnostics; }
251 /// Redirect - Redirect output of this compilation. Can only be done once.
253 /// \param Redirects - array of pointers to paths. The array
254 /// should have a size of three. The inferior process's
255 /// stdin(0), stdout(1), and stderr(2) will be redirected to the
256 /// corresponding paths. This compilation instance becomes
257 /// the owner of Redirects and will delete the array and StringRef's.
258 void Redirect(const StringRef** Redirects);
261 } // end namespace driver
262 } // end namespace clang