Nick Lewycky | 3fdcc6f | 2010-12-31 17:31:54 +0000 | [diff] [blame] | 1 | //===--- Compilation.cpp - Compilation Task Implementation ----------------===// |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 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 | #include "clang/Driver/Compilation.h" |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 11 | #include "clang/Driver/Action.h" |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Driver.h" |
| 13 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | 265e9ef | 2009-11-19 04:25:22 +0000 | [diff] [blame] | 14 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 15 | #include "clang/Driver/ToolChain.h" |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 17 | #include "llvm/Option/ArgList.h" |
Rafael Espindola | da1f9cb | 2013-06-18 20:58:25 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
Francois Pichet | 1c3199a | 2011-07-23 11:36:43 +0000 | [diff] [blame] | 22 | |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 23 | using namespace clang::driver; |
Francois Pichet | 1c3199a | 2011-07-23 11:36:43 +0000 | [diff] [blame] | 24 | using namespace clang; |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 25 | using namespace llvm::opt; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 26 | |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 27 | Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain, |
| 28 | InputArgList *_Args, DerivedArgList *_TranslatedArgs) |
| 29 | : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args), |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 30 | TranslatedArgs(_TranslatedArgs), Redirects(0) { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | Compilation::~Compilation() { |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 34 | delete TranslatedArgs; |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 35 | delete Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 37 | // Free any derived arg lists. |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 38 | for (llvm::DenseMap<std::pair<const ToolChain*, const char*>, |
| 39 | DerivedArgList*>::iterator it = TCArgs.begin(), |
| 40 | ie = TCArgs.end(); it != ie; ++it) |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 41 | if (it->second != TranslatedArgs) |
| 42 | delete it->second; |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 43 | |
| 44 | // Free the actions, if built. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | for (ActionList::iterator it = Actions.begin(), ie = Actions.end(); |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 46 | it != ie; ++it) |
| 47 | delete *it; |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 48 | |
| 49 | // Free redirections of stdout/stderr. |
| 50 | if (Redirects) { |
| 51 | delete Redirects[1]; |
| 52 | delete Redirects[2]; |
| 53 | delete [] Redirects; |
| 54 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 57 | const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC, |
| 58 | const char *BoundArch) { |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 59 | if (!TC) |
| 60 | TC = &DefaultToolChain; |
| 61 | |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 62 | DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)]; |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 63 | if (!Entry) { |
| 64 | Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch); |
| 65 | if (!Entry) |
| 66 | Entry = TranslatedArgs; |
| 67 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | aa3e0d2 | 2009-03-18 05:58:45 +0000 | [diff] [blame] | 69 | return *Entry; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 72 | bool Compilation::CleanupFile(const char *File, bool IssueErrors) const { |
Rafael Espindola | de2b523 | 2013-06-24 17:59:44 +0000 | [diff] [blame] | 73 | std::string P(File); |
| 74 | |
| 75 | // FIXME: Why are we trying to remove files that we have not created? For |
| 76 | // example we should only try to remove a temporary assembly file if |
| 77 | // "clang -cc1" succeed in writing it. Was this a workaround for when |
| 78 | // clang was writing directly to a .s file and sometimes leaving it behind |
| 79 | // during a failure? |
| 80 | |
| 81 | // FIXME: If this is necessary, we can still try to split |
| 82 | // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the |
| 83 | // duplicated stat from is_regular_file. |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 84 | |
| 85 | // Don't try to remove files which we don't have write access to (but may be |
| 86 | // able to remove), or non-regular files. Underlying tools may have |
| 87 | // intentionally not overwritten them. |
Rafael Espindola | de2b523 | 2013-06-24 17:59:44 +0000 | [diff] [blame] | 88 | if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File)) |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 89 | return true; |
| 90 | |
Rafael Espindola | de2b523 | 2013-06-24 17:59:44 +0000 | [diff] [blame] | 91 | if (llvm::error_code EC = llvm::sys::fs::remove(File)) { |
| 92 | // Failure is only failure if the file exists and is "regular". We checked |
| 93 | // for it being regular before, and llvm::sys::fs::remove ignores ENOENT, |
| 94 | // so we don't need to check again. |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 95 | |
Rafael Espindola | de2b523 | 2013-06-24 17:59:44 +0000 | [diff] [blame] | 96 | if (IssueErrors) |
| 97 | getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) |
| 98 | << EC.message(); |
| 99 | return false; |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | bool Compilation::CleanupFileList(const ArgStringList &Files, |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 105 | bool IssueErrors) const { |
| 106 | bool Success = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | for (ArgStringList::const_iterator |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 108 | it = Files.begin(), ie = Files.end(); it != ie; ++it) |
| 109 | Success &= CleanupFile(*it, IssueErrors); |
| 110 | return Success; |
| 111 | } |
| 112 | |
| 113 | bool Compilation::CleanupFileMap(const ArgStringMap &Files, |
| 114 | const JobAction *JA, |
| 115 | bool IssueErrors) const { |
| 116 | bool Success = true; |
| 117 | for (ArgStringMap::const_iterator |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 118 | it = Files.begin(), ie = Files.end(); it != ie; ++it) { |
Edward O'Callaghan | 56eec2b | 2009-11-24 15:23:21 +0000 | [diff] [blame] | 119 | |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 120 | // If specified, only delete the files associated with the JobAction. |
| 121 | // Otherwise, delete all files in the map. |
| 122 | if (JA && it->first != JA) |
Daniel Dunbar | 8ac127a | 2011-04-25 20:43:05 +0000 | [diff] [blame] | 123 | continue; |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 124 | Success &= CleanupFile(it->second, IssueErrors); |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 125 | } |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 126 | return Success; |
| 127 | } |
| 128 | |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 129 | int Compilation::ExecuteCommand(const Command &C, |
| 130 | const Command *&FailingCommand) const { |
Rafael Espindola | 7d3240d | 2013-07-23 17:58:53 +0000 | [diff] [blame] | 131 | if ((getDriver().CCPrintOptions || |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 132 | getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | raw_ostream *OS = &llvm::errs(); |
Daniel Dunbar | 4c00fcd | 2010-03-20 08:01:59 +0000 | [diff] [blame] | 134 | |
| 135 | // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the |
| 136 | // output stream. |
| 137 | if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) { |
| 138 | std::string Error; |
Rafael Espindola | d965f95 | 2013-07-16 19:44:23 +0000 | [diff] [blame] | 139 | OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, Error, |
| 140 | llvm::sys::fs::F_Append); |
Daniel Dunbar | 4c00fcd | 2010-03-20 08:01:59 +0000 | [diff] [blame] | 141 | if (!Error.empty()) { |
| 142 | getDriver().Diag(clang::diag::err_drv_cc_print_options_failure) |
| 143 | << Error; |
| 144 | FailingCommand = &C; |
| 145 | delete OS; |
| 146 | return 1; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (getDriver().CCPrintOptions) |
| 151 | *OS << "[Logging clang options]"; |
| 152 | |
Hans Wennborg | fc33897 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 153 | C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions); |
Daniel Dunbar | 4c00fcd | 2010-03-20 08:01:59 +0000 | [diff] [blame] | 154 | |
| 155 | if (OS != &llvm::errs()) |
| 156 | delete OS; |
| 157 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 159 | std::string Error; |
Chad Rosier | c48d575 | 2013-03-26 23:41:30 +0000 | [diff] [blame] | 160 | bool ExecutionFailed; |
Hans Wennborg | aaaa2a1 | 2013-09-12 18:35:08 +0000 | [diff] [blame] | 161 | int Res = C.Execute(Redirects, &Error, &ExecutionFailed); |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 162 | if (!Error.empty()) { |
| 163 | assert(Res && "Error string set with 0 result code!"); |
| 164 | getDriver().Diag(clang::diag::err_drv_command_failure) << Error; |
| 165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 167 | if (Res) |
| 168 | FailingCommand = &C; |
| 169 | |
Chad Rosier | c48d575 | 2013-03-26 23:41:30 +0000 | [diff] [blame] | 170 | return ExecutionFailed ? 1 : Res; |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chad Rosier | 4c4df45 | 2013-02-27 18:46:04 +0000 | [diff] [blame] | 173 | typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList; |
| 174 | |
| 175 | static bool ActionFailed(const Action *A, |
| 176 | const FailingCommandList &FailingCommands) { |
| 177 | |
| 178 | if (FailingCommands.empty()) |
| 179 | return false; |
| 180 | |
| 181 | for (FailingCommandList::const_iterator CI = FailingCommands.begin(), |
| 182 | CE = FailingCommands.end(); CI != CE; ++CI) |
| 183 | if (A == &(CI->second->getSource())) |
| 184 | return true; |
| 185 | |
| 186 | for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI) |
| 187 | if (ActionFailed(*AI, FailingCommands)) |
| 188 | return true; |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | static bool InputsOk(const Command &C, |
| 194 | const FailingCommandList &FailingCommands) { |
| 195 | return !ActionFailed(&C.getSource(), FailingCommands); |
| 196 | } |
| 197 | |
Chad Rosier | a16355c | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 198 | void Compilation::ExecuteJob(const Job &J, |
Chad Rosier | 4c4df45 | 2013-02-27 18:46:04 +0000 | [diff] [blame] | 199 | FailingCommandList &FailingCommands) const { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 200 | if (const Command *C = dyn_cast<Command>(&J)) { |
Chad Rosier | 4c4df45 | 2013-02-27 18:46:04 +0000 | [diff] [blame] | 201 | if (!InputsOk(*C, FailingCommands)) |
| 202 | return; |
Chad Rosier | a16355c | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 203 | const Command *FailingCommand = 0; |
| 204 | if (int Res = ExecuteCommand(*C, FailingCommand)) |
| 205 | FailingCommands.push_back(std::make_pair(Res, FailingCommand)); |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 206 | } else { |
| 207 | const JobList *Jobs = cast<JobList>(&J); |
Chad Rosier | a16355c | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 208 | for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end(); |
| 209 | it != ie; ++it) |
| 210 | ExecuteJob(**it, FailingCommands); |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 213 | |
Dmitri Gribenko | c4a7790 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 214 | void Compilation::initCompilationForDiagnostics() { |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 215 | // Free actions and jobs. |
| 216 | DeleteContainerPointers(Actions); |
| 217 | Jobs.clear(); |
| 218 | |
| 219 | // Clear temporary/results file lists. |
| 220 | TempFiles.clear(); |
| 221 | ResultFiles.clear(); |
Chad Rosier | 8425a54 | 2013-01-29 23:57:10 +0000 | [diff] [blame] | 222 | FailureResultFiles.clear(); |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 223 | |
| 224 | // Remove any user specified output. Claim any unclaimed arguments, so as |
| 225 | // to avoid emitting warnings about unused args. |
Peter Collingbourne | 54db68b | 2011-11-06 00:40:05 +0000 | [diff] [blame] | 226 | OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD, |
| 227 | options::OPT_MMD }; |
Chad Rosier | f9e156c | 2012-05-03 21:25:34 +0000 | [diff] [blame] | 228 | for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) { |
Peter Collingbourne | 54db68b | 2011-11-06 00:40:05 +0000 | [diff] [blame] | 229 | if (TranslatedArgs->hasArg(OutputOpts[i])) |
| 230 | TranslatedArgs->eraseArg(OutputOpts[i]); |
| 231 | } |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 232 | TranslatedArgs->ClaimAllArgs(); |
| 233 | |
| 234 | // Redirect stdout/stderr to /dev/null. |
Rafael Espindola | 57a3bbf | 2013-06-13 20:08:52 +0000 | [diff] [blame] | 235 | Redirects = new const StringRef*[3](); |
| 236 | Redirects[0] = 0; |
| 237 | Redirects[1] = new const StringRef(); |
| 238 | Redirects[2] = new const StringRef(); |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 239 | } |
Sebastian Pop | 4762a2d | 2012-04-16 04:16:43 +0000 | [diff] [blame] | 240 | |
Dmitri Gribenko | c4a7790 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 241 | StringRef Compilation::getSysRoot() const { |
Sebastian Pop | 4762a2d | 2012-04-16 04:16:43 +0000 | [diff] [blame] | 242 | return getDriver().SysRoot; |
| 243 | } |