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