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 | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 16 | #include "clang/Driver/ToolChain.h" |
| 17 | |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 19 | #include "llvm/System/Program.h" |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
| 21 | #include <errno.h> |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 22 | using namespace clang::driver; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 23 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 24 | Compilation::Compilation(Driver &D, |
| 25 | ToolChain &_DefaultToolChain, |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 26 | ArgList *_Args) |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 27 | : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 30 | Compilation::~Compilation() { |
| 31 | delete Args; |
| 32 | |
| 33 | // Free any derived arg lists. |
| 34 | for (llvm::DenseMap<const ToolChain*, ArgList*>::iterator |
| 35 | it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it) { |
| 36 | ArgList *A = it->second; |
| 37 | if (A != Args) |
| 38 | delete Args; |
| 39 | } |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 40 | |
| 41 | // Free the actions, if built. |
| 42 | for (ActionList::iterator it = Actions.begin(), ie = Actions.end(); |
| 43 | it != ie; ++it) |
| 44 | delete *it; |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | const ArgList &Compilation::getArgsForToolChain(const ToolChain *TC) { |
| 48 | if (!TC) |
| 49 | TC = &DefaultToolChain; |
| 50 | |
Daniel Dunbar | aa3e0d2 | 2009-03-18 05:58:45 +0000 | [diff] [blame] | 51 | ArgList *&Entry = TCArgs[TC]; |
| 52 | if (!Entry) |
| 53 | Entry = TC->TranslateArgs(*Args); |
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 | |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 58 | void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J, |
| 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) { |
| 64 | if (Quote) |
| 65 | OS << " \"" << *it << '"'; |
| 66 | else |
| 67 | OS << ' ' << *it; |
| 68 | } |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 69 | OS << Terminator; |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 70 | } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) { |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 71 | for (PipedJob::const_iterator |
| 72 | it = PJ->begin(), ie = PJ->end(); it != ie; ++it) |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 73 | PrintJob(OS, **it, (it + 1 != PJ->end()) ? " |\n" : "\n", Quote); |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 74 | } else { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 75 | const JobList *Jobs = cast<JobList>(&J); |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 76 | for (JobList::const_iterator |
| 77 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 78 | PrintJob(OS, **it, Terminator, Quote); |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 82 | bool Compilation::CleanupFileList(const ArgStringList &Files, |
| 83 | bool IssueErrors) const { |
| 84 | bool Success = true; |
| 85 | |
| 86 | for (ArgStringList::const_iterator |
| 87 | it = Files.begin(), ie = Files.end(); it != ie; ++it) { |
| 88 | llvm::sys::Path P(*it); |
| 89 | std::string Error; |
| 90 | |
| 91 | if (P.eraseFromDisk(false, &Error)) { |
| 92 | // Failure is only failure if the file doesn't exist. There is a |
| 93 | // race condition here due to the limited interface of |
| 94 | // llvm::sys::Path, we want to know if the removal gave E_NOENT. |
| 95 | |
| 96 | // FIXME: Grumble, P.exists() is broken. PR3837. |
| 97 | struct stat buf; |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 98 | if (::stat(P.c_str(), &buf) == 0 |
| 99 | || errno != ENOENT) { |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 100 | if (IssueErrors) |
| 101 | getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) |
| 102 | << Error; |
| 103 | Success = false; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return Success; |
| 109 | } |
| 110 | |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 111 | int Compilation::ExecuteJob(const Job &J) const { |
| 112 | if (const Command *C = dyn_cast<Command>(&J)) { |
| 113 | llvm::sys::Path Prog(C->getExecutable()); |
| 114 | const char **Argv = new const char*[C->getArguments().size() + 2]; |
| 115 | Argv[0] = C->getExecutable(); |
| 116 | std::copy(C->getArguments().begin(), C->getArguments().end(), Argv+1); |
| 117 | Argv[C->getArguments().size() + 1] = 0; |
| 118 | |
| 119 | if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v)) |
| 120 | PrintJob(llvm::errs(), J, "\n", false); |
| 121 | |
| 122 | std::string Error; |
| 123 | int Res = |
| 124 | llvm::sys::Program::ExecuteAndWait(Prog, Argv, |
| 125 | /*env*/0, /*redirects*/0, |
| 126 | /*secondsToWait*/0, /*memoryLimit*/0, |
| 127 | &Error); |
| 128 | if (!Error.empty()) { |
| 129 | assert(Res && "Error string set with 0 result code!"); |
| 130 | getDriver().Diag(clang::diag::err_drv_command_failure) << Error; |
| 131 | } |
| 132 | |
| 133 | delete[] Argv; |
| 134 | return Res; |
| 135 | } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) { |
| 136 | (void) PJ; |
| 137 | getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe"; |
| 138 | return 1; |
| 139 | } else { |
| 140 | const JobList *Jobs = cast<JobList>(&J); |
| 141 | for (JobList::const_iterator |
| 142 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
| 143 | if (int Res = ExecuteJob(**it)) |
| 144 | return Res; |
| 145 | return 0; |
| 146 | } |
| 147 | } |
| 148 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 149 | int Compilation::Execute() const { |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 150 | // Just print if -### was present. |
| 151 | if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 152 | PrintJob(llvm::errs(), Jobs, "\n", true); |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 155 | |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 156 | int Res = ExecuteJob(Jobs); |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 158 | // Remove temp files. |
| 159 | CleanupFileList(TempFiles); |
| 160 | |
| 161 | // If the compilation failed, remove result files as well. |
| 162 | if (Res != 0) |
| 163 | CleanupFileList(ResultFiles, true); |
| 164 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 165 | return 0; |
| 166 | } |