1 //===--- Compilation.cpp - Compilation Task Implementation --------------*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/Compilation.h"
12 #include "clang/Driver/Action.h"
13 #include "clang/Driver/ArgList.h"
14 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/DriverDiagnostic.h"
16 #include "clang/Driver/ToolChain.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/System/Program.h"
22 using namespace clang::driver;
24 Compilation::Compilation(Driver &D,
25 ToolChain &_DefaultToolChain,
27 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) {
30 Compilation::~Compilation() {
33 // Free any derived arg lists.
34 for (llvm::DenseMap<const ToolChain*, DerivedArgList*>::iterator
35 it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it)
38 // Free the actions, if built.
39 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
44 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC) {
46 TC = &DefaultToolChain;
48 DerivedArgList *&Entry = TCArgs[TC];
50 Entry = TC->TranslateArgs(*Args);
55 void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J,
56 const char *Terminator, bool Quote) const {
57 if (const Command *C = dyn_cast<Command>(&J)) {
58 OS << " \"" << C->getExecutable() << '"';
59 for (ArgStringList::const_iterator it = C->getArguments().begin(),
60 ie = C->getArguments().end(); it != ie; ++it) {
62 OS << " \"" << *it << '"';
67 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
68 for (PipedJob::const_iterator
69 it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
70 PrintJob(OS, **it, (it + 1 != PJ->end()) ? " |\n" : "\n", Quote);
72 const JobList *Jobs = cast<JobList>(&J);
73 for (JobList::const_iterator
74 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
75 PrintJob(OS, **it, Terminator, Quote);
79 bool Compilation::CleanupFileList(const ArgStringList &Files,
80 bool IssueErrors) const {
83 for (ArgStringList::const_iterator
84 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
85 llvm::sys::Path P(*it);
88 if (P.eraseFromDisk(false, &Error)) {
89 // Failure is only failure if the file doesn't exist. There is a
90 // race condition here due to the limited interface of
91 // llvm::sys::Path, we want to know if the removal gave E_NOENT.
93 // FIXME: Grumble, P.exists() is broken. PR3837.
95 if (::stat(P.c_str(), &buf) == 0
98 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
108 int Compilation::ExecuteCommand(const Command &C) const {
109 llvm::sys::Path Prog(C.getExecutable());
110 const char **Argv = new const char*[C.getArguments().size() + 2];
111 Argv[0] = C.getExecutable();
112 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
113 Argv[C.getArguments().size() + 1] = 0;
115 if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v))
116 PrintJob(llvm::errs(), C, "\n", false);
120 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
121 /*env*/0, /*redirects*/0,
122 /*secondsToWait*/0, /*memoryLimit*/0,
124 if (!Error.empty()) {
125 assert(Res && "Error string set with 0 result code!");
126 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
133 int Compilation::ExecuteJob(const Job &J) const {
134 if (const Command *C = dyn_cast<Command>(&J)) {
135 return ExecuteCommand(*C);
136 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
137 // Piped commands with a single job are easy.
139 return ExecuteCommand(**PJ->begin());
141 getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
144 const JobList *Jobs = cast<JobList>(&J);
145 for (JobList::const_iterator
146 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
147 if (int Res = ExecuteJob(**it))
153 int Compilation::Execute() const {
154 // Just print if -### was present.
155 if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
156 PrintJob(llvm::errs(), Jobs, "\n", true);
160 // If there were errors building the compilation, quit now.
161 if (getDriver().getDiags().getNumErrors())
164 int Res = ExecuteJob(Jobs);
166 // Remove temp files.
167 CleanupFileList(TempFiles);
169 // If the compilation failed, remove result files as well.
170 if (Res != 0 && !getArgs().hasArg(options::OPT_save_temps))
171 CleanupFileList(ResultFiles, true);