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"
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 const ToolChain *CudaHostToolChain;
42 const ToolChain *CudaDeviceToolChain;
44 /// The original (untranslated) input argument list.
45 llvm::opt::InputArgList *Args;
47 /// The driver translated arguments. Note that toolchains may perform their
48 /// own argument translation.
49 llvm::opt::DerivedArgList *TranslatedArgs;
51 /// The list of actions we've created via MakeAction. This is not accessible
52 /// to consumers; it's here just to manage ownership.
53 std::vector<std::unique_ptr<Action>> AllActions;
55 /// The list of actions. This is maintained and modified by consumers, via
59 /// The root list of jobs.
62 /// Cache of translated arguments for a particular tool chain and bound
64 llvm::DenseMap<std::pair<const ToolChain *, const char *>,
65 llvm::opt::DerivedArgList *> TCArgs;
67 /// Temporary files which should be removed on exit.
68 llvm::opt::ArgStringList TempFiles;
70 /// Result files which should be removed on failure.
71 ArgStringMap ResultFiles;
73 /// Result files which are generated correctly on failure, and which should
74 /// only be removed if we crash.
75 ArgStringMap FailureResultFiles;
77 /// Redirection for stdout, stderr, etc.
78 const StringRef **Redirects;
80 /// Whether we're compiling for diagnostic purposes.
84 Compilation(const Driver &D, const ToolChain &DefaultToolChain,
85 llvm::opt::InputArgList *Args,
86 llvm::opt::DerivedArgList *TranslatedArgs);
89 const Driver &getDriver() const { return TheDriver; }
91 const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
92 const ToolChain *getCudaHostToolChain() const { return CudaHostToolChain; }
93 const ToolChain *getCudaDeviceToolChain() const {
94 return CudaDeviceToolChain;
97 void setCudaHostToolChain(const ToolChain *HostToolChain) {
98 CudaHostToolChain = HostToolChain;
100 void setCudaDeviceToolChain(const ToolChain *DeviceToolChain) {
101 CudaDeviceToolChain = DeviceToolChain;
104 const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
106 const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
108 llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
110 ActionList &getActions() { return Actions; }
111 const ActionList &getActions() const { return Actions; }
113 /// Creates a new Action owned by this Compilation.
115 /// The new Action is *not* added to the list returned by getActions().
116 template <typename T, typename... Args> T *MakeAction(Args &&... Arg) {
117 T *RawPtr = new T(std::forward<Args>(Arg)...);
118 AllActions.push_back(std::unique_ptr<Action>(RawPtr));
122 JobList &getJobs() { return Jobs; }
123 const JobList &getJobs() const { return Jobs; }
125 void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
127 const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
129 const ArgStringMap &getResultFiles() const { return ResultFiles; }
131 const ArgStringMap &getFailureResultFiles() const {
132 return FailureResultFiles;
135 /// Returns the sysroot path.
136 StringRef getSysRoot() const;
138 /// getArgsForToolChain - Return the derived argument list for the
139 /// tool chain \p TC (or the default tool chain, if TC is not specified).
141 /// \param BoundArch - The bound architecture name, or 0.
142 const llvm::opt::DerivedArgList &getArgsForToolChain(const ToolChain *TC,
143 const char *BoundArch);
145 /// addTempFile - Add a file to remove on exit, and returns its
147 const char *addTempFile(const char *Name) {
148 TempFiles.push_back(Name);
152 /// addResultFile - Add a file to remove on failure, and returns its
154 const char *addResultFile(const char *Name, const JobAction *JA) {
155 ResultFiles[JA] = Name;
159 /// addFailureResultFile - Add a file to remove if we crash, and returns its
161 const char *addFailureResultFile(const char *Name, const JobAction *JA) {
162 FailureResultFiles[JA] = Name;
166 /// CleanupFile - Delete a given file.
168 /// \param IssueErrors - Report failures as errors.
169 /// \return Whether the file was removed successfully.
170 bool CleanupFile(const char *File, bool IssueErrors = false) const;
172 /// CleanupFileList - Remove the files in the given list.
174 /// \param IssueErrors - Report failures as errors.
175 /// \return Whether all files were removed successfully.
176 bool CleanupFileList(const llvm::opt::ArgStringList &Files,
177 bool IssueErrors = false) const;
179 /// CleanupFileMap - Remove the files in the given map.
181 /// \param JA - If specified, only delete the files associated with this
182 /// JobAction. Otherwise, delete all files in the map.
183 /// \param IssueErrors - Report failures as errors.
184 /// \return Whether all files were removed successfully.
185 bool CleanupFileMap(const ArgStringMap &Files,
187 bool IssueErrors = false) const;
189 /// ExecuteCommand - Execute an actual command.
191 /// \param FailingCommand - For non-zero results, this will be set to the
192 /// Command which failed, if any.
193 /// \return The result code of the subprocess.
194 int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
196 /// ExecuteJob - Execute a single job.
198 /// \param FailingCommands - For non-zero results, this will be a vector of
199 /// failing commands and their associated result code.
202 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
204 /// initCompilationForDiagnostics - Remove stale state and suppress output
205 /// so compilation can be reexecuted to generate additional diagnostic
206 /// information (e.g., preprocessed source(s)).
207 void initCompilationForDiagnostics();
209 /// Return true if we're compiling for diagnostics.
210 bool isForDiagnostics() const { return ForDiagnostics; }
213 } // end namespace driver
214 } // end namespace clang