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