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"
32 /// Compilation - A set of tasks to perform for a single driver
35 /// The driver we were created by.
36 const Driver &TheDriver;
38 /// The default tool chain.
39 const ToolChain &DefaultToolChain;
41 /// A mask of all the programming models the host has to support in the
42 /// current compilation.
43 unsigned ActiveOffloadMask;
45 /// Array with the toolchains of offloading host and devices in the order they
46 /// were requested by the user. We are preserving that order in case the code
47 /// generation needs to derive a programming-model-specific semantic out of
49 std::multimap<Action::OffloadKind, const ToolChain *>
50 OrderedOffloadingToolchains;
52 /// The original (untranslated) input argument list.
53 llvm::opt::InputArgList *Args;
55 /// The driver translated arguments. Note that toolchains may perform their
56 /// own argument translation.
57 llvm::opt::DerivedArgList *TranslatedArgs;
59 /// The list of actions we've created via MakeAction. This is not accessible
60 /// to consumers; it's here just to manage ownership.
61 std::vector<std::unique_ptr<Action>> AllActions;
63 /// The list of actions. This is maintained and modified by consumers, via
67 /// The root list of jobs.
70 /// Cache of translated arguments for a particular tool chain, bound
71 /// architecture, and device offload kind.
72 struct TCArgsKey final {
73 const ToolChain *TC = nullptr;
75 Action::OffloadKind DeviceOffloadKind = Action::OFK_None;
76 bool operator<(const TCArgsKey &K) const {
79 else if (TC == K.TC && BoundArch < K.BoundArch)
81 else if (TC == K.TC && BoundArch == K.BoundArch &&
82 DeviceOffloadKind < K.DeviceOffloadKind)
86 TCArgsKey(const ToolChain *TC, StringRef BoundArch,
87 Action::OffloadKind DeviceOffloadKind)
88 : TC(TC), BoundArch(BoundArch), DeviceOffloadKind(DeviceOffloadKind) {}
90 std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
92 /// Temporary files which should be removed on exit.
93 llvm::opt::ArgStringList TempFiles;
95 /// Result files which should be removed on failure.
96 ArgStringMap ResultFiles;
98 /// Result files which are generated correctly on failure, and which should
99 /// only be removed if we crash.
100 ArgStringMap FailureResultFiles;
102 /// Redirection for stdout, stderr, etc.
103 const StringRef **Redirects;
105 /// Whether we're compiling for diagnostic purposes.
108 /// Whether an error during the parsing of the input args.
112 Compilation(const Driver &D, const ToolChain &DefaultToolChain,
113 llvm::opt::InputArgList *Args,
114 llvm::opt::DerivedArgList *TranslatedArgs, bool ContainsError);
117 const Driver &getDriver() const { return TheDriver; }
119 const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
121 unsigned isOffloadingHostKind(Action::OffloadKind Kind) const {
122 return ActiveOffloadMask & Kind;
125 /// Iterator that visits device toolchains of a given kind.
126 typedef const std::multimap<Action::OffloadKind,
127 const ToolChain *>::const_iterator
128 const_offload_toolchains_iterator;
129 typedef std::pair<const_offload_toolchains_iterator,
130 const_offload_toolchains_iterator>
131 const_offload_toolchains_range;
133 template <Action::OffloadKind Kind>
134 const_offload_toolchains_range getOffloadToolChains() const {
135 return OrderedOffloadingToolchains.equal_range(Kind);
138 /// Return true if an offloading tool chain of a given kind exists.
139 template <Action::OffloadKind Kind> bool hasOffloadToolChain() const {
140 return OrderedOffloadingToolchains.find(Kind) !=
141 OrderedOffloadingToolchains.end();
144 /// Return an offload toolchain of the provided kind. Only one is expected to
146 template <Action::OffloadKind Kind>
147 const ToolChain *getSingleOffloadToolChain() const {
148 auto TCs = getOffloadToolChains<Kind>();
150 assert(TCs.first != TCs.second &&
151 "No tool chains of the selected kind exist!");
152 assert(std::next(TCs.first) == TCs.second &&
153 "More than one tool chain of the this kind exist.");
154 return TCs.first->second;
157 void addOffloadDeviceToolChain(const ToolChain *DeviceToolChain,
158 Action::OffloadKind OffloadKind) {
159 assert(OffloadKind != Action::OFK_Host && OffloadKind != Action::OFK_None &&
160 "This is not a device tool chain!");
162 // Update the host offload kind to also contain this kind.
163 ActiveOffloadMask |= OffloadKind;
164 OrderedOffloadingToolchains.insert(
165 std::make_pair(OffloadKind, DeviceToolChain));
168 const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
170 const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
172 llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
174 ActionList &getActions() { return Actions; }
175 const ActionList &getActions() const { return Actions; }
177 /// Creates a new Action owned by this Compilation.
179 /// The new Action is *not* added to the list returned by getActions().
180 template <typename T, typename... Args> T *MakeAction(Args &&... Arg) {
181 T *RawPtr = new T(std::forward<Args>(Arg)...);
182 AllActions.push_back(std::unique_ptr<Action>(RawPtr));
186 JobList &getJobs() { return Jobs; }
187 const JobList &getJobs() const { return Jobs; }
189 void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
191 const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
193 const ArgStringMap &getResultFiles() const { return ResultFiles; }
195 const ArgStringMap &getFailureResultFiles() const {
196 return FailureResultFiles;
199 /// Returns the sysroot path.
200 StringRef getSysRoot() const;
202 /// getArgsForToolChain - Return the derived argument list for the
203 /// tool chain \p TC (or the default tool chain, if TC is not specified).
204 /// If a device offloading kind is specified, a translation specific for that
205 /// kind is performed, if any.
207 /// \param BoundArch - The bound architecture name, or 0.
208 /// \param DeviceOffloadKind - The offload device kind that should be used in
209 /// the translation, if any.
210 const llvm::opt::DerivedArgList &
211 getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
212 Action::OffloadKind DeviceOffloadKind);
214 /// addTempFile - Add a file to remove on exit, and returns its
216 const char *addTempFile(const char *Name) {
217 TempFiles.push_back(Name);
221 /// addResultFile - Add a file to remove on failure, and returns its
223 const char *addResultFile(const char *Name, const JobAction *JA) {
224 ResultFiles[JA] = Name;
228 /// addFailureResultFile - Add a file to remove if we crash, and returns its
230 const char *addFailureResultFile(const char *Name, const JobAction *JA) {
231 FailureResultFiles[JA] = Name;
235 /// CleanupFile - Delete a given file.
237 /// \param IssueErrors - Report failures as errors.
238 /// \return Whether the file was removed successfully.
239 bool CleanupFile(const char *File, bool IssueErrors = false) const;
241 /// CleanupFileList - Remove the files in the given list.
243 /// \param IssueErrors - Report failures as errors.
244 /// \return Whether all files were removed successfully.
245 bool CleanupFileList(const llvm::opt::ArgStringList &Files,
246 bool IssueErrors = false) const;
248 /// CleanupFileMap - Remove the files in the given map.
250 /// \param JA - If specified, only delete the files associated with this
251 /// JobAction. Otherwise, delete all files in the map.
252 /// \param IssueErrors - Report failures as errors.
253 /// \return Whether all files were removed successfully.
254 bool CleanupFileMap(const ArgStringMap &Files,
256 bool IssueErrors = false) const;
258 /// ExecuteCommand - Execute an actual command.
260 /// \param FailingCommand - For non-zero results, this will be set to the
261 /// Command which failed, if any.
262 /// \return The result code of the subprocess.
263 int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
265 /// ExecuteJob - Execute a single job.
267 /// \param FailingCommands - For non-zero results, this will be a vector of
268 /// failing commands and their associated result code.
271 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
273 /// initCompilationForDiagnostics - Remove stale state and suppress output
274 /// so compilation can be reexecuted to generate additional diagnostic
275 /// information (e.g., preprocessed source(s)).
276 void initCompilationForDiagnostics();
278 /// Return true if we're compiling for diagnostics.
279 bool isForDiagnostics() const { return ForDiagnostics; }
281 /// Return whether an error during the parsing of the input args.
282 bool containsError() const { return ContainsError; }
284 /// Redirect - Redirect output of this compilation. Can only be done once.
286 /// \param Redirects - array of pointers to paths. The array
287 /// should have a size of three. The inferior process's
288 /// stdin(0), stdout(1), and stderr(2) will be redirected to the
289 /// corresponding paths. This compilation instance becomes
290 /// the owner of Redirects and will delete the array and StringRef's.
291 void Redirect(const StringRef** Redirects);
294 } // end namespace driver
295 } // end namespace clang