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/Basic/LLVM.h"
14 #include "clang/Driver/Action.h"
15 #include "clang/Driver/Job.h"
16 #include "clang/Driver/Util.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Option/Option.h"
44 /// Compilation - A set of tasks to perform for a single driver
47 /// The driver we were created by.
48 const Driver &TheDriver;
50 /// The default tool chain.
51 const ToolChain &DefaultToolChain;
53 /// A mask of all the programming models the host has to support in the
54 /// current compilation.
55 unsigned ActiveOffloadMask = 0;
57 /// Array with the toolchains of offloading host and devices in the order they
58 /// were requested by the user. We are preserving that order in case the code
59 /// generation needs to derive a programming-model-specific semantic out of
61 std::multimap<Action::OffloadKind, const ToolChain *>
62 OrderedOffloadingToolchains;
64 /// The original (untranslated) input argument list.
65 llvm::opt::InputArgList *Args;
67 /// The driver translated arguments. Note that toolchains may perform their
68 /// own argument translation.
69 llvm::opt::DerivedArgList *TranslatedArgs;
71 /// The list of actions we've created via MakeAction. This is not accessible
72 /// to consumers; it's here just to manage ownership.
73 std::vector<std::unique_ptr<Action>> AllActions;
75 /// The list of actions. This is maintained and modified by consumers, via
79 /// The root list of jobs.
82 /// Cache of translated arguments for a particular tool chain, bound
83 /// architecture, and device offload kind.
84 struct TCArgsKey final {
85 const ToolChain *TC = nullptr;
87 Action::OffloadKind DeviceOffloadKind = Action::OFK_None;
89 TCArgsKey(const ToolChain *TC, StringRef BoundArch,
90 Action::OffloadKind DeviceOffloadKind)
91 : TC(TC), BoundArch(BoundArch), DeviceOffloadKind(DeviceOffloadKind) {}
93 bool operator<(const TCArgsKey &K) const {
96 else if (TC == K.TC && BoundArch < K.BoundArch)
98 else if (TC == K.TC && BoundArch == K.BoundArch &&
99 DeviceOffloadKind < K.DeviceOffloadKind)
104 std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
106 /// Temporary files which should be removed on exit.
107 llvm::opt::ArgStringList TempFiles;
109 /// Result files which should be removed on failure.
110 ArgStringMap ResultFiles;
112 /// Result files which are generated correctly on failure, and which should
113 /// only be removed if we crash.
114 ArgStringMap FailureResultFiles;
116 /// Optional redirection for stdin, stdout, stderr.
117 std::vector<Optional<StringRef>> Redirects;
119 /// Whether we're compiling for diagnostic purposes.
120 bool ForDiagnostics = false;
122 /// Whether an error during the parsing of the input args.
125 /// Whether to keep temporary files regardless of -save-temps.
126 bool ForceKeepTempFiles = false;
129 Compilation(const Driver &D, const ToolChain &DefaultToolChain,
130 llvm::opt::InputArgList *Args,
131 llvm::opt::DerivedArgList *TranslatedArgs, bool ContainsError);
134 const Driver &getDriver() const { return TheDriver; }
136 const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
138 unsigned isOffloadingHostKind(Action::OffloadKind Kind) const {
139 return ActiveOffloadMask & Kind;
142 /// Iterator that visits device toolchains of a given kind.
143 using const_offload_toolchains_iterator =
144 const std::multimap<Action::OffloadKind,
145 const ToolChain *>::const_iterator;
146 using const_offload_toolchains_range =
147 std::pair<const_offload_toolchains_iterator,
148 const_offload_toolchains_iterator>;
150 template <Action::OffloadKind Kind>
151 const_offload_toolchains_range getOffloadToolChains() const {
152 return OrderedOffloadingToolchains.equal_range(Kind);
155 /// Return true if an offloading tool chain of a given kind exists.
156 template <Action::OffloadKind Kind> bool hasOffloadToolChain() const {
157 return OrderedOffloadingToolchains.find(Kind) !=
158 OrderedOffloadingToolchains.end();
161 /// Return an offload toolchain of the provided kind. Only one is expected to
163 template <Action::OffloadKind Kind>
164 const ToolChain *getSingleOffloadToolChain() const {
165 auto TCs = getOffloadToolChains<Kind>();
167 assert(TCs.first != TCs.second &&
168 "No tool chains of the selected kind exist!");
169 assert(std::next(TCs.first) == TCs.second &&
170 "More than one tool chain of the this kind exist.");
171 return TCs.first->second;
174 void addOffloadDeviceToolChain(const ToolChain *DeviceToolChain,
175 Action::OffloadKind OffloadKind) {
176 assert(OffloadKind != Action::OFK_Host && OffloadKind != Action::OFK_None &&
177 "This is not a device tool chain!");
179 // Update the host offload kind to also contain this kind.
180 ActiveOffloadMask |= OffloadKind;
181 OrderedOffloadingToolchains.insert(
182 std::make_pair(OffloadKind, DeviceToolChain));
185 const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
187 const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
189 llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
191 ActionList &getActions() { return Actions; }
192 const ActionList &getActions() const { return Actions; }
194 /// Creates a new Action owned by this Compilation.
196 /// The new Action is *not* added to the list returned by getActions().
197 template <typename T, typename... Args> T *MakeAction(Args &&... Arg) {
198 T *RawPtr = new T(std::forward<Args>(Arg)...);
199 AllActions.push_back(std::unique_ptr<Action>(RawPtr));
203 JobList &getJobs() { return Jobs; }
204 const JobList &getJobs() const { return Jobs; }
206 void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
208 const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
210 const ArgStringMap &getResultFiles() const { return ResultFiles; }
212 const ArgStringMap &getFailureResultFiles() const {
213 return FailureResultFiles;
216 /// Returns the sysroot path.
217 StringRef getSysRoot() const;
219 /// getArgsForToolChain - Return the derived argument list for the
220 /// tool chain \p TC (or the default tool chain, if TC is not specified).
221 /// If a device offloading kind is specified, a translation specific for that
222 /// kind is performed, if any.
224 /// \param BoundArch - The bound architecture name, or 0.
225 /// \param DeviceOffloadKind - The offload device kind that should be used in
226 /// the translation, if any.
227 const llvm::opt::DerivedArgList &
228 getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
229 Action::OffloadKind DeviceOffloadKind);
231 /// addTempFile - Add a file to remove on exit, and returns its
233 const char *addTempFile(const char *Name) {
234 TempFiles.push_back(Name);
238 /// addResultFile - Add a file to remove on failure, and returns its
240 const char *addResultFile(const char *Name, const JobAction *JA) {
241 ResultFiles[JA] = Name;
245 /// addFailureResultFile - Add a file to remove if we crash, and returns its
247 const char *addFailureResultFile(const char *Name, const JobAction *JA) {
248 FailureResultFiles[JA] = Name;
252 /// CleanupFile - Delete a given file.
254 /// \param IssueErrors - Report failures as errors.
255 /// \return Whether the file was removed successfully.
256 bool CleanupFile(const char *File, bool IssueErrors = false) const;
258 /// CleanupFileList - Remove the files in the given list.
260 /// \param IssueErrors - Report failures as errors.
261 /// \return Whether all files were removed successfully.
262 bool CleanupFileList(const llvm::opt::ArgStringList &Files,
263 bool IssueErrors = false) const;
265 /// CleanupFileMap - Remove the files in the given map.
267 /// \param JA - If specified, only delete the files associated with this
268 /// JobAction. Otherwise, delete all files in the map.
269 /// \param IssueErrors - Report failures as errors.
270 /// \return Whether all files were removed successfully.
271 bool CleanupFileMap(const ArgStringMap &Files,
273 bool IssueErrors = false) const;
275 /// ExecuteCommand - Execute an actual command.
277 /// \param FailingCommand - For non-zero results, this will be set to the
278 /// Command which failed, if any.
279 /// \return The result code of the subprocess.
280 int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
282 /// ExecuteJob - Execute a single job.
284 /// \param FailingCommands - For non-zero results, this will be a vector of
285 /// failing commands and their associated result code.
288 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
290 /// initCompilationForDiagnostics - Remove stale state and suppress output
291 /// so compilation can be reexecuted to generate additional diagnostic
292 /// information (e.g., preprocessed source(s)).
293 void initCompilationForDiagnostics();
295 /// Return true if we're compiling for diagnostics.
296 bool isForDiagnostics() const { return ForDiagnostics; }
298 /// Return whether an error during the parsing of the input args.
299 bool containsError() const { return ContainsError; }
301 /// Redirect - Redirect output of this compilation. Can only be done once.
303 /// \param Redirects - array of optional paths. The array should have a size
304 /// of three. The inferior process's stdin(0), stdout(1), and stderr(2) will
305 /// be redirected to the corresponding paths, if provided (not llvm::None).
306 void Redirect(ArrayRef<Optional<StringRef>> Redirects);
309 } // namespace driver
312 #endif // LLVM_CLANG_DRIVER_COMPILATION_H