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 | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame^] | 19 | #include <sys/stat.h> |
| 20 | #include <errno.h> |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 21 | using namespace clang::driver; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 22 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame^] | 23 | Compilation::Compilation(Driver &D, |
| 24 | ToolChain &_DefaultToolChain, |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 25 | ArgList *_Args) |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame^] | 26 | : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 29 | Compilation::~Compilation() { |
| 30 | delete Args; |
| 31 | |
| 32 | // Free any derived arg lists. |
| 33 | for (llvm::DenseMap<const ToolChain*, ArgList*>::iterator |
| 34 | it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it) { |
| 35 | ArgList *A = it->second; |
| 36 | if (A != Args) |
| 37 | delete Args; |
| 38 | } |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 39 | |
| 40 | // Free the actions, if built. |
| 41 | for (ActionList::iterator it = Actions.begin(), ie = Actions.end(); |
| 42 | it != ie; ++it) |
| 43 | delete *it; |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | const ArgList &Compilation::getArgsForToolChain(const ToolChain *TC) { |
| 47 | if (!TC) |
| 48 | TC = &DefaultToolChain; |
| 49 | |
Daniel Dunbar | aa3e0d2 | 2009-03-18 05:58:45 +0000 | [diff] [blame] | 50 | ArgList *&Entry = TCArgs[TC]; |
| 51 | if (!Entry) |
| 52 | Entry = TC->TranslateArgs(*Args); |
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 | |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 57 | void Compilation::PrintJob(llvm::raw_ostream &OS, const Job *J, |
| 58 | const char *Terminator) const { |
| 59 | if (const Command *C = dyn_cast<Command>(J)) { |
| 60 | OS << " \"" << C->getExecutable() << '"'; |
| 61 | for (ArgStringList::const_iterator it = C->getArguments().begin(), |
| 62 | ie = C->getArguments().end(); it != ie; ++it) |
| 63 | OS << " \"" << *it << '"'; |
| 64 | OS << Terminator; |
| 65 | } else if (const PipedJob *PJ = dyn_cast<PipedJob>(J)) { |
| 66 | for (PipedJob::const_iterator |
| 67 | it = PJ->begin(), ie = PJ->end(); it != ie; ++it) |
| 68 | PrintJob(OS, *it, (it + 1 != PJ->end()) ? " |\n" : "\n"); |
| 69 | } else { |
| 70 | const JobList *Jobs = cast<JobList>(J); |
| 71 | for (JobList::const_iterator |
| 72 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
| 73 | PrintJob(OS, *it, Terminator); |
| 74 | } |
| 75 | } |
| 76 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame^] | 77 | bool Compilation::CleanupFileList(const ArgStringList &Files, |
| 78 | bool IssueErrors) const { |
| 79 | bool Success = true; |
| 80 | |
| 81 | for (ArgStringList::const_iterator |
| 82 | it = Files.begin(), ie = Files.end(); it != ie; ++it) { |
| 83 | llvm::sys::Path P(*it); |
| 84 | std::string Error; |
| 85 | |
| 86 | if (P.eraseFromDisk(false, &Error)) { |
| 87 | // Failure is only failure if the file doesn't exist. There is a |
| 88 | // race condition here due to the limited interface of |
| 89 | // llvm::sys::Path, we want to know if the removal gave E_NOENT. |
| 90 | |
| 91 | // FIXME: Grumble, P.exists() is broken. PR3837. |
| 92 | struct stat buf; |
| 93 | if (::stat(P.c_str(), &buf) || errno != ENOENT) { |
| 94 | if (IssueErrors) |
| 95 | getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) |
| 96 | << Error; |
| 97 | Success = false; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return Success; |
| 103 | } |
| 104 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 105 | int Compilation::Execute() const { |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 106 | // Just print if -### was present. |
| 107 | if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
| 108 | PrintJob(llvm::errs(), &Jobs, "\n"); |
| 109 | return 0; |
| 110 | } |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame^] | 111 | |
| 112 | // FIXME: Execute. |
| 113 | |
| 114 | int Res = 0; |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame^] | 116 | // Remove temp files. |
| 117 | CleanupFileList(TempFiles); |
| 118 | |
| 119 | // If the compilation failed, remove result files as well. |
| 120 | if (Res != 0) |
| 121 | CleanupFileList(ResultFiles, true); |
| 122 | |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 123 | return 0; |
| 124 | } |