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