1 //===--- Compilation.h - Compilation Task Data Structure --------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef CLANG_DRIVER_COMPILATION_H_ 11 #define CLANG_DRIVER_COMPILATION_H_ 12 13 #include "clang/Driver/Job.h" 14 #include "clang/Driver/Util.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/Support/Path.h" 17 18 namespace clang { 19 namespace driver { 20 class DerivedArgList; 21 class Driver; 22 class InputArgList; 23 class JobAction; 24 class JobList; 25 class ToolChain; 26 27 /// Compilation - A set of tasks to perform for a single driver 28 /// invocation. 29 class Compilation { 30 /// The driver we were created by. 31 const Driver &TheDriver; 32 33 /// The default tool chain. 34 const ToolChain &DefaultToolChain; 35 36 /// The original (untranslated) input argument list. 37 InputArgList *Args; 38 39 /// The driver translated arguments. Note that toolchains may perform their 40 /// own argument translation. 41 DerivedArgList *TranslatedArgs; 42 43 /// The list of actions. 44 ActionList Actions; 45 46 /// The root list of jobs. 47 JobList Jobs; 48 49 /// Cache of translated arguments for a particular tool chain and bound 50 /// architecture. 51 llvm::DenseMap<std::pair<const ToolChain*, const char*>, 52 DerivedArgList*> TCArgs; 53 54 /// Temporary files which should be removed on exit. 55 ArgStringList TempFiles; 56 57 /// Result files which should be removed on failure. 58 ArgStringMap ResultFiles; 59 60 /// Result files which are generated correctly on failure, and which should 61 /// only be removed if we crash. 62 ArgStringMap FailureResultFiles; 63 64 /// Redirection for stdout, stderr, etc. 65 const llvm::sys::Path **Redirects; 66 67 public: 68 Compilation(const Driver &D, const ToolChain &DefaultToolChain, 69 InputArgList *Args, DerivedArgList *TranslatedArgs); 70 ~Compilation(); 71 72 const Driver &getDriver() const { return TheDriver; } 73 74 const ToolChain &getDefaultToolChain() const { return DefaultToolChain; } 75 76 const InputArgList &getInputArgs() const { return *Args; } 77 78 const DerivedArgList &getArgs() const { return *TranslatedArgs; } 79 80 DerivedArgList &getArgs() { return *TranslatedArgs; } 81 82 ActionList &getActions() { return Actions; } 83 const ActionList &getActions() const { return Actions; } 84 85 JobList &getJobs() { return Jobs; } 86 const JobList &getJobs() const { return Jobs; } 87 88 void addCommand(Command *C) { Jobs.addJob(C); } 89 90 const ArgStringList &getTempFiles() const { return TempFiles; } 91 92 const ArgStringMap &getResultFiles() const { return ResultFiles; } 93 94 const ArgStringMap &getFailureResultFiles() const { 95 return FailureResultFiles; 96 } 97 98 /// Returns the sysroot path. 99 StringRef getSysRoot() const; 100 101 /// getArgsForToolChain - Return the derived argument list for the 102 /// tool chain \p TC (or the default tool chain, if TC is not specified). 103 /// 104 /// \param BoundArch - The bound architecture name, or 0. 105 const DerivedArgList &getArgsForToolChain(const ToolChain *TC, 106 const char *BoundArch); 107 108 /// addTempFile - Add a file to remove on exit, and returns its 109 /// argument. 110 const char *addTempFile(const char *Name) { 111 TempFiles.push_back(Name); 112 return Name; 113 } 114 115 /// addResultFile - Add a file to remove on failure, and returns its 116 /// argument. 117 const char *addResultFile(const char *Name, const JobAction *JA) { 118 ResultFiles[JA] = Name; 119 return Name; 120 } 121 122 /// addFailureResultFile - Add a file to remove if we crash, and returns its 123 /// argument. 124 const char *addFailureResultFile(const char *Name, const JobAction *JA) { 125 FailureResultFiles[JA] = Name; 126 return Name; 127 } 128 129 /// CleanupFile - Delete a given file. 130 /// 131 /// \param IssueErrors - Report failures as errors. 132 /// \return Whether the file was removed successfully. 133 bool CleanupFile(const char *File, bool IssueErrors = false) const; 134 135 /// CleanupFileList - Remove the files in the given list. 136 /// 137 /// \param IssueErrors - Report failures as errors. 138 /// \return Whether all files were removed successfully. 139 bool CleanupFileList(const ArgStringList &Files, 140 bool IssueErrors = false) const; 141 142 /// CleanupFileMap - Remove the files in the given map. 143 /// 144 /// \param JA - If specified, only delete the files associated with this 145 /// JobAction. Otherwise, delete all files in the map. 146 /// \param IssueErrors - Report failures as errors. 147 /// \return Whether all files were removed successfully. 148 bool CleanupFileMap(const ArgStringMap &Files, 149 const JobAction *JA, 150 bool IssueErrors = false) const; 151 152 /// PrintJob - Print one job in -### format. 153 /// 154 /// \param OS - The stream to print on. 155 /// \param J - The job to print. 156 /// \param Terminator - A string to print at the end of the line. 157 /// \param Quote - Should separate arguments be quoted. 158 void PrintJob(raw_ostream &OS, const Job &J, 159 const char *Terminator, bool Quote) const; 160 161 /// PrintDiagnosticJob - Print one job in -### format, but with the 162 /// superfluous options removed, which are not necessary for 163 /// reproducing the crash. 164 /// 165 /// \param OS - The stream to print on. 166 /// \param J - The job to print. 167 void PrintDiagnosticJob(raw_ostream &OS, const Job &J) const; 168 169 /// ExecuteCommand - Execute an actual command. 170 /// 171 /// \param FailingCommand - For non-zero results, this will be set to the 172 /// Command which failed, if any. 173 /// \return The result code of the subprocess. 174 int ExecuteCommand(const Command &C, const Command *&FailingCommand) const; 175 176 /// ExecuteJob - Execute a single job. 177 /// 178 /// \param FailingCommands - For non-zero results, this will be a vector of 179 /// failing commands and their associated result code. 180 void ExecuteJob(const Job &J, 181 SmallVectorImpl< std::pair<int, const Command *> > &FailingCommands) const; 182 183 /// initCompilationForDiagnostics - Remove stale state and suppress output 184 /// so compilation can be reexecuted to generate additional diagnostic 185 /// information (e.g., preprocessed source(s)). 186 void initCompilationForDiagnostics(); 187 }; 188 189 } // end namespace driver 190 } // end namespace clang 191 192 #endif 193