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/Options.h"
17 #include "clang/Driver/ToolChain.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/Program.h"
23 using namespace clang::driver;
25 Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
26 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
27 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
28 TranslatedArgs(_TranslatedArgs) {
31 Compilation::~Compilation() {
32 delete TranslatedArgs;
35 // Free any derived arg lists.
36 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
37 DerivedArgList*>::iterator it = TCArgs.begin(),
38 ie = TCArgs.end(); it != ie; ++it)
39 if (it->second != TranslatedArgs)
42 // Free the actions, if built.
43 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
48 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
49 const char *BoundArch) {
51 TC = &DefaultToolChain;
53 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
55 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
57 Entry = TranslatedArgs;
63 void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J,
64 const char *Terminator, bool Quote) const {
65 if (const Command *C = dyn_cast<Command>(&J)) {
66 OS << " \"" << C->getExecutable() << '"';
67 for (ArgStringList::const_iterator it = C->getArguments().begin(),
68 ie = C->getArguments().end(); it != ie; ++it) {
75 // Quote the argument and escape shell special characters; this isn't
76 // really complete but is good enough.
78 for (const char *s = *it; *s; ++s) {
79 if (*s == '"' || *s == '\\' || *s == '$')
87 const JobList *Jobs = cast<JobList>(&J);
88 for (JobList::const_iterator
89 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
90 PrintJob(OS, **it, Terminator, Quote);
94 bool Compilation::CleanupFileList(const ArgStringList &Files,
95 bool IssueErrors) const {
98 for (ArgStringList::const_iterator
99 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
101 llvm::sys::Path P(*it);
104 // Don't try to remove files which we don't have write access to (but may be
105 // able to remove). Underlying tools may have intentionally not overwritten
110 if (P.eraseFromDisk(false, &Error)) {
111 // Failure is only failure if the file exists and is "regular". There is
112 // a race condition here due to the limited interface of
113 // llvm::sys::Path, we want to know if the removal gave ENOENT.
115 // FIXME: Grumble, P.exists() is broken. PR3837.
117 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
120 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
130 int Compilation::ExecuteCommand(const Command &C,
131 const Command *&FailingCommand) const {
132 llvm::sys::Path Prog(C.getExecutable());
133 const char **Argv = new const char*[C.getArguments().size() + 2];
134 Argv[0] = C.getExecutable();
135 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
136 Argv[C.getArguments().size() + 1] = 0;
138 if (getDriver().CCCEcho || getDriver().CCPrintOptions ||
139 getArgs().hasArg(options::OPT_v)) {
140 llvm::raw_ostream *OS = &llvm::errs();
142 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
144 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
146 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
148 llvm::raw_fd_ostream::F_Append);
149 if (!Error.empty()) {
150 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
158 if (getDriver().CCPrintOptions)
159 *OS << "[Logging clang options]";
161 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
163 if (OS != &llvm::errs())
169 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
170 /*env*/0, /*redirects*/0,
171 /*secondsToWait*/0, /*memoryLimit*/0,
173 if (!Error.empty()) {
174 assert(Res && "Error string set with 0 result code!");
175 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
185 int Compilation::ExecuteJob(const Job &J,
186 const Command *&FailingCommand) const {
187 if (const Command *C = dyn_cast<Command>(&J)) {
188 return ExecuteCommand(*C, FailingCommand);
190 const JobList *Jobs = cast<JobList>(&J);
191 for (JobList::const_iterator
192 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
193 if (int Res = ExecuteJob(**it, FailingCommand))