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 | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 11 | |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Action.h" |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 13 | #include "clang/Driver/ArgList.h" |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 14 | #include "clang/Driver/Driver.h" |
| 15 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | 265e9ef | 2009-11-19 04:25:22 +0000 | [diff] [blame] | 16 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 17 | #include "clang/Driver/ToolChain.h" |
| 18 | |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Program.h" |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <errno.h> |
Francois Pichet | 1c3199a | 2011-07-23 11:36:43 +0000 | [diff] [blame] | 24 | |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 25 | using namespace clang::driver; |
Francois Pichet | 1c3199a | 2011-07-23 11:36:43 +0000 | [diff] [blame] | 26 | using namespace clang; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 27 | |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 28 | Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain, |
| 29 | InputArgList *_Args, DerivedArgList *_TranslatedArgs) |
| 30 | : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args), |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 31 | TranslatedArgs(_TranslatedArgs), Redirects(0) { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | Compilation::~Compilation() { |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 35 | delete TranslatedArgs; |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 36 | delete Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 38 | // Free any derived arg lists. |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 39 | for (llvm::DenseMap<std::pair<const ToolChain*, const char*>, |
| 40 | DerivedArgList*>::iterator it = TCArgs.begin(), |
| 41 | ie = TCArgs.end(); it != ie; ++it) |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 42 | if (it->second != TranslatedArgs) |
| 43 | delete it->second; |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 44 | |
| 45 | // Free the actions, if built. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | for (ActionList::iterator it = Actions.begin(), ie = Actions.end(); |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 47 | it != ie; ++it) |
| 48 | delete *it; |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 49 | |
| 50 | // Free redirections of stdout/stderr. |
| 51 | if (Redirects) { |
| 52 | delete Redirects[1]; |
| 53 | delete Redirects[2]; |
| 54 | delete [] Redirects; |
| 55 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 58 | const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC, |
| 59 | const char *BoundArch) { |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 60 | if (!TC) |
| 61 | TC = &DefaultToolChain; |
| 62 | |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 63 | DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)]; |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 64 | if (!Entry) { |
| 65 | Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch); |
| 66 | if (!Entry) |
| 67 | Entry = TranslatedArgs; |
| 68 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 69 | |
Daniel Dunbar | aa3e0d2 | 2009-03-18 05:58:45 +0000 | [diff] [blame] | 70 | return *Entry; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 73 | void Compilation::PrintJob(raw_ostream &OS, const Job &J, |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 74 | const char *Terminator, bool Quote) const { |
| 75 | if (const Command *C = dyn_cast<Command>(&J)) { |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 76 | OS << " \"" << C->getExecutable() << '"'; |
| 77 | for (ArgStringList::const_iterator it = C->getArguments().begin(), |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 78 | ie = C->getArguments().end(); it != ie; ++it) { |
Daniel Dunbar | b86c5fc | 2010-03-20 08:01:53 +0000 | [diff] [blame] | 79 | OS << ' '; |
Benjamin Kramer | 5f22614 | 2011-10-06 22:53:35 +0000 | [diff] [blame] | 80 | if (!Quote && !std::strpbrk(*it, " \"\\$")) { |
Daniel Dunbar | b86c5fc | 2010-03-20 08:01:53 +0000 | [diff] [blame] | 81 | OS << *it; |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | // Quote the argument and escape shell special characters; this isn't |
| 86 | // really complete but is good enough. |
| 87 | OS << '"'; |
| 88 | for (const char *s = *it; *s; ++s) { |
| 89 | if (*s == '"' || *s == '\\' || *s == '$') |
| 90 | OS << '\\'; |
| 91 | OS << *s; |
| 92 | } |
| 93 | OS << '"'; |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 94 | } |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 95 | OS << Terminator; |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 96 | } else { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 97 | const JobList *Jobs = cast<JobList>(&J); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | for (JobList::const_iterator |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 99 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 100 | PrintJob(OS, **it, Terminator, Quote); |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 101 | } |
| 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; |
| 107 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | for (ArgStringList::const_iterator |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 109 | it = Files.begin(), ie = Files.end(); it != ie; ++it) { |
Edward O'Callaghan | 56eec2b | 2009-11-24 15:23:21 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 111 | llvm::sys::Path P(*it); |
| 112 | std::string Error; |
| 113 | |
Daniel Dunbar | 8ac127a | 2011-04-25 20:43:05 +0000 | [diff] [blame] | 114 | // Don't try to remove files which we don't have write access to (but may be |
| 115 | // able to remove). Underlying tools may have intentionally not overwritten |
| 116 | // them. |
| 117 | if (!P.canWrite()) |
| 118 | continue; |
| 119 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 120 | if (P.eraseFromDisk(false, &Error)) { |
Dan Gohman | 978e3a2 | 2010-10-29 23:26:14 +0000 | [diff] [blame] | 121 | // Failure is only failure if the file exists and is "regular". There is |
| 122 | // a race condition here due to the limited interface of |
| 123 | // llvm::sys::Path, we want to know if the removal gave ENOENT. |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 124 | |
| 125 | // FIXME: Grumble, P.exists() is broken. PR3837. |
| 126 | struct stat buf; |
Benjamin Kramer | d99d0e8 | 2010-10-30 08:28:42 +0000 | [diff] [blame] | 127 | if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG : |
Dan Gohman | 978e3a2 | 2010-10-29 23:26:14 +0000 | [diff] [blame] | 128 | (errno != ENOENT)) { |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 129 | if (IssueErrors) |
| 130 | getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) |
| 131 | << Error; |
| 132 | Success = false; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return Success; |
| 138 | } |
| 139 | |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 140 | int Compilation::ExecuteCommand(const Command &C, |
| 141 | const Command *&FailingCommand) const { |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 142 | llvm::sys::Path Prog(C.getExecutable()); |
| 143 | const char **Argv = new const char*[C.getArguments().size() + 2]; |
| 144 | Argv[0] = C.getExecutable(); |
| 145 | std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1); |
| 146 | Argv[C.getArguments().size() + 1] = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 148 | if ((getDriver().CCCEcho || getDriver().CCPrintOptions || |
| 149 | getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 150 | raw_ostream *OS = &llvm::errs(); |
Daniel Dunbar | 4c00fcd | 2010-03-20 08:01:59 +0000 | [diff] [blame] | 151 | |
| 152 | // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the |
| 153 | // output stream. |
| 154 | if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) { |
| 155 | std::string Error; |
| 156 | OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, |
| 157 | Error, |
| 158 | llvm::raw_fd_ostream::F_Append); |
| 159 | if (!Error.empty()) { |
| 160 | getDriver().Diag(clang::diag::err_drv_cc_print_options_failure) |
| 161 | << Error; |
| 162 | FailingCommand = &C; |
| 163 | delete OS; |
| 164 | return 1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (getDriver().CCPrintOptions) |
| 169 | *OS << "[Logging clang options]"; |
| 170 | |
| 171 | PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions); |
| 172 | |
| 173 | if (OS != &llvm::errs()) |
| 174 | delete OS; |
| 175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 177 | std::string Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | int Res = |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 179 | llvm::sys::Program::ExecuteAndWait(Prog, Argv, |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 180 | /*env*/0, Redirects, |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 181 | /*secondsToWait*/0, /*memoryLimit*/0, |
| 182 | &Error); |
| 183 | if (!Error.empty()) { |
| 184 | assert(Res && "Error string set with 0 result code!"); |
| 185 | getDriver().Diag(clang::diag::err_drv_command_failure) << Error; |
| 186 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 188 | if (Res) |
| 189 | FailingCommand = &C; |
| 190 | |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 191 | delete[] Argv; |
| 192 | return Res; |
| 193 | } |
| 194 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | int Compilation::ExecuteJob(const Job &J, |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 196 | const Command *&FailingCommand) const { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 197 | if (const Command *C = dyn_cast<Command>(&J)) { |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 198 | return ExecuteCommand(*C, FailingCommand); |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 199 | } else { |
| 200 | const JobList *Jobs = cast<JobList>(&J); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | for (JobList::const_iterator |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 202 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 203 | if (int Res = ExecuteJob(**it, FailingCommand)) |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 204 | return Res; |
| 205 | return 0; |
| 206 | } |
| 207 | } |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 208 | |
| 209 | void Compilation::initCompilationForDiagnostics(void) { |
| 210 | // Free actions and jobs. |
| 211 | DeleteContainerPointers(Actions); |
| 212 | Jobs.clear(); |
| 213 | |
| 214 | // Clear temporary/results file lists. |
| 215 | TempFiles.clear(); |
| 216 | ResultFiles.clear(); |
| 217 | |
| 218 | // Remove any user specified output. Claim any unclaimed arguments, so as |
| 219 | // to avoid emitting warnings about unused args. |
Peter Collingbourne | 54db68b | 2011-11-06 00:40:05 +0000 | [diff] [blame] | 220 | OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD, |
| 221 | options::OPT_MMD }; |
| 222 | for (unsigned i = 0; i != sizeof(OutputOpts)/sizeof(OutputOpts[0]); ++i) { |
| 223 | if (TranslatedArgs->hasArg(OutputOpts[i])) |
| 224 | TranslatedArgs->eraseArg(OutputOpts[i]); |
| 225 | } |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 226 | TranslatedArgs->ClaimAllArgs(); |
| 227 | |
| 228 | // Redirect stdout/stderr to /dev/null. |
| 229 | Redirects = new const llvm::sys::Path*[3](); |
| 230 | Redirects[1] = new const llvm::sys::Path(); |
| 231 | Redirects[2] = new const llvm::sys::Path(); |
| 232 | } |