Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 1 | //===--- Compilation.cpp - Compilation Task Implementation --------------*-===// |
| 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 | |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 20 | #include "llvm/System/Program.h" |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <errno.h> |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 23 | using namespace clang::driver; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 24 | |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 25 | Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain, |
| 26 | InputArgList *_Args, DerivedArgList *_TranslatedArgs) |
| 27 | : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args), |
| 28 | TranslatedArgs(_TranslatedArgs) { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | Compilation::~Compilation() { |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 32 | delete TranslatedArgs; |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 33 | delete Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 35 | // Free any derived arg lists. |
Daniel Dunbar | 4954018 | 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 | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 39 | if (it->second != TranslatedArgs) |
| 40 | delete it->second; |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 41 | |
| 42 | // Free the actions, if built. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | for (ActionList::iterator it = Actions.begin(), ie = Actions.end(); |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 44 | it != ie; ++it) |
| 45 | delete *it; |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 48 | const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC, |
| 49 | const char *BoundArch) { |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 50 | if (!TC) |
| 51 | TC = &DefaultToolChain; |
| 52 | |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 53 | DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)]; |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 54 | if (!Entry) { |
| 55 | Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch); |
| 56 | if (!Entry) |
| 57 | Entry = TranslatedArgs; |
| 58 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 59 | |
Daniel Dunbar | aa3e0d2 | 2009-03-18 05:58:45 +0000 | [diff] [blame] | 60 | return *Entry; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J, |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 64 | const char *Terminator, bool Quote) const { |
| 65 | if (const Command *C = dyn_cast<Command>(&J)) { |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 66 | OS << " \"" << C->getExecutable() << '"'; |
| 67 | for (ArgStringList::const_iterator it = C->getArguments().begin(), |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 68 | ie = C->getArguments().end(); it != ie; ++it) { |
Daniel Dunbar | b86c5fc | 2010-03-20 08:01:53 +0000 | [diff] [blame] | 69 | OS << ' '; |
| 70 | if (!Quote) { |
| 71 | OS << *it; |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | // Quote the argument and escape shell special characters; this isn't |
| 76 | // really complete but is good enough. |
| 77 | OS << '"'; |
| 78 | for (const char *s = *it; *s; ++s) { |
| 79 | if (*s == '"' || *s == '\\' || *s == '$') |
| 80 | OS << '\\'; |
| 81 | OS << *s; |
| 82 | } |
| 83 | OS << '"'; |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 84 | } |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 85 | OS << Terminator; |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 86 | } else { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 87 | const JobList *Jobs = cast<JobList>(&J); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | for (JobList::const_iterator |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 89 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 90 | PrintJob(OS, **it, Terminator, Quote); |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | bool Compilation::CleanupFileList(const ArgStringList &Files, |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 95 | bool IssueErrors) const { |
| 96 | bool Success = true; |
| 97 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | for (ArgStringList::const_iterator |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 99 | it = Files.begin(), ie = Files.end(); it != ie; ++it) { |
Edward O'Callaghan | 56eec2b | 2009-11-24 15:23:21 +0000 | [diff] [blame] | 100 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 101 | llvm::sys::Path P(*it); |
| 102 | std::string Error; |
| 103 | |
| 104 | if (P.eraseFromDisk(false, &Error)) { |
Dan Gohman | 978e3a2 | 2010-10-29 23:26:14 +0000 | [diff] [blame] | 105 | // Failure is only failure if the file exists and is "regular". There is |
| 106 | // a race condition here due to the limited interface of |
| 107 | // llvm::sys::Path, we want to know if the removal gave ENOENT. |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 108 | |
| 109 | // FIXME: Grumble, P.exists() is broken. PR3837. |
| 110 | struct stat buf; |
Benjamin Kramer | d99d0e8 | 2010-10-30 08:28:42 +0000 | [diff] [blame] | 111 | 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] | 112 | (errno != ENOENT)) { |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 113 | if (IssueErrors) |
| 114 | getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) |
| 115 | << Error; |
| 116 | Success = false; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return Success; |
| 122 | } |
| 123 | |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 124 | int Compilation::ExecuteCommand(const Command &C, |
| 125 | const Command *&FailingCommand) const { |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 126 | llvm::sys::Path Prog(C.getExecutable()); |
| 127 | const char **Argv = new const char*[C.getArguments().size() + 2]; |
| 128 | Argv[0] = C.getExecutable(); |
| 129 | std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1); |
| 130 | Argv[C.getArguments().size() + 1] = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Daniel Dunbar | 4c00fcd | 2010-03-20 08:01:59 +0000 | [diff] [blame] | 132 | if (getDriver().CCCEcho || getDriver().CCPrintOptions || |
| 133 | getArgs().hasArg(options::OPT_v)) { |
| 134 | llvm::raw_ostream *OS = &llvm::errs(); |
| 135 | |
| 136 | // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the |
| 137 | // output stream. |
| 138 | if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) { |
| 139 | std::string Error; |
| 140 | OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, |
| 141 | Error, |
| 142 | llvm::raw_fd_ostream::F_Append); |
| 143 | if (!Error.empty()) { |
| 144 | getDriver().Diag(clang::diag::err_drv_cc_print_options_failure) |
| 145 | << Error; |
| 146 | FailingCommand = &C; |
| 147 | delete OS; |
| 148 | return 1; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (getDriver().CCPrintOptions) |
| 153 | *OS << "[Logging clang options]"; |
| 154 | |
| 155 | PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions); |
| 156 | |
| 157 | if (OS != &llvm::errs()) |
| 158 | delete OS; |
| 159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 161 | std::string Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | int Res = |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 163 | llvm::sys::Program::ExecuteAndWait(Prog, Argv, |
| 164 | /*env*/0, /*redirects*/0, |
| 165 | /*secondsToWait*/0, /*memoryLimit*/0, |
| 166 | &Error); |
| 167 | if (!Error.empty()) { |
| 168 | assert(Res && "Error string set with 0 result code!"); |
| 169 | getDriver().Diag(clang::diag::err_drv_command_failure) << Error; |
| 170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 172 | if (Res) |
| 173 | FailingCommand = &C; |
| 174 | |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 175 | delete[] Argv; |
| 176 | return Res; |
| 177 | } |
| 178 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | int Compilation::ExecuteJob(const Job &J, |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 180 | const Command *&FailingCommand) const { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 181 | if (const Command *C = dyn_cast<Command>(&J)) { |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 182 | return ExecuteCommand(*C, FailingCommand); |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 183 | } else { |
| 184 | const JobList *Jobs = cast<JobList>(&J); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | for (JobList::const_iterator |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 186 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 187 | if (int Res = ExecuteJob(**it, FailingCommand)) |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 188 | return Res; |
| 189 | return 0; |
| 190 | } |
| 191 | } |