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.
54 /// The root list of jobs.
57 /// Cache of translated arguments for a particular tool chain and bound
59 llvm::DenseMap<std::pair<const ToolChain *, const char *>,
60 llvm::opt::DerivedArgList *> TCArgs;
62 /// Temporary files which should be removed on exit.
63 llvm::opt::ArgStringList TempFiles;
65 /// Result files which should be removed on failure.
66 ArgStringMap ResultFiles;
68 /// Result files which are generated correctly on failure, and which should
69 /// only be removed if we crash.
70 ArgStringMap FailureResultFiles;
72 /// Redirection for stdout, stderr, etc.
73 const StringRef **Redirects;
75 /// Whether we're compiling for diagnostic purposes.
79 Compilation(const Driver &D, const ToolChain &DefaultToolChain,
80 llvm::opt::InputArgList *Args,
81 llvm::opt::DerivedArgList *TranslatedArgs);
84 const Driver &getDriver() const { return TheDriver; }
86 const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
87 const ToolChain *getCudaHostToolChain() const { return CudaHostToolChain; }
88 const ToolChain *getCudaDeviceToolChain() const {
89 return CudaDeviceToolChain;
92 void setCudaHostToolChain(const ToolChain *HostToolChain) {
93 CudaHostToolChain = HostToolChain;
95 void setCudaDeviceToolChain(const ToolChain *DeviceToolChain) {
96 CudaDeviceToolChain = DeviceToolChain;
99 const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
101 const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
103 llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
105 ActionList &getActions() { return Actions; }
106 const ActionList &getActions() const { return Actions; }
108 JobList &getJobs() { return Jobs; }
109 const JobList &getJobs() const { return Jobs; }
111 void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
113 const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
115 const ArgStringMap &getResultFiles() const { return ResultFiles; }
117 const ArgStringMap &getFailureResultFiles() const {
118 return FailureResultFiles;
121 /// Returns the sysroot path.
122 StringRef getSysRoot() const;
124 /// getArgsForToolChain - Return the derived argument list for the
125 /// tool chain \p TC (or the default tool chain, if TC is not specified).
127 /// \param BoundArch - The bound architecture name, or 0.
128 const llvm::opt::DerivedArgList &getArgsForToolChain(const ToolChain *TC,
129 const char *BoundArch);
131 /// addTempFile - Add a file to remove on exit, and returns its
133 const char *addTempFile(const char *Name) {
134 TempFiles.push_back(Name);
138 /// addResultFile - Add a file to remove on failure, and returns its
140 const char *addResultFile(const char *Name, const JobAction *JA) {
141 ResultFiles[JA] = Name;
145 /// addFailureResultFile - Add a file to remove if we crash, and returns its
147 const char *addFailureResultFile(const char *Name, const JobAction *JA) {
148 FailureResultFiles[JA] = Name;
152 /// CleanupFile - Delete a given file.
154 /// \param IssueErrors - Report failures as errors.
155 /// \return Whether the file was removed successfully.
156 bool CleanupFile(const char *File, bool IssueErrors = false) const;
158 /// CleanupFileList - Remove the files in the given list.
160 /// \param IssueErrors - Report failures as errors.
161 /// \return Whether all files were removed successfully.
162 bool CleanupFileList(const llvm::opt::ArgStringList &Files,
163 bool IssueErrors = false) const;
165 /// CleanupFileMap - Remove the files in the given map.
167 /// \param JA - If specified, only delete the files associated with this
168 /// JobAction. Otherwise, delete all files in the map.
169 /// \param IssueErrors - Report failures as errors.
170 /// \return Whether all files were removed successfully.
171 bool CleanupFileMap(const ArgStringMap &Files,
173 bool IssueErrors = false) const;
175 /// ExecuteCommand - Execute an actual command.
177 /// \param FailingCommand - For non-zero results, this will be set to the
178 /// Command which failed, if any.
179 /// \return The result code of the subprocess.
180 int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
182 /// ExecuteJob - Execute a single job.
184 /// \param FailingCommands - For non-zero results, this will be a vector of
185 /// failing commands and their associated result code.
188 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
190 /// initCompilationForDiagnostics - Remove stale state and suppress output
191 /// so compilation can be reexecuted to generate additional diagnostic
192 /// information (e.g., preprocessed source(s)).
193 void initCompilationForDiagnostics();
195 /// Return true if we're compiling for diagnostics.
196 bool isForDiagnostics() const { return ForDiagnostics; }
199 } // end namespace driver
200 } // end namespace clang