blob: bdb39c7fecc6c7bc5100dc6d42b92ad9646ef76a [file] [log] [blame]
Daniel Dunbar63c4da92009-03-02 19:59:07 +00001//===--- 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 Dunbar9d625e12009-03-16 06:42:30 +000011
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +000012#include "clang/Driver/Action.h"
Daniel Dunbar9d625e12009-03-16 06:42:30 +000013#include "clang/Driver/ArgList.h"
Daniel Dunbar31a76e32009-03-18 22:16:03 +000014#include "clang/Driver/Driver.h"
15#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar9d625e12009-03-16 06:42:30 +000016#include "clang/Driver/ToolChain.h"
17
Daniel Dunbar1b265972009-03-18 06:49:39 +000018#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1fef88d2009-03-18 22:44:24 +000019#include "llvm/System/Program.h"
Daniel Dunbar31a76e32009-03-18 22:16:03 +000020#include <sys/stat.h>
21#include <errno.h>
Daniel Dunbard6f0e372009-03-04 20:49:20 +000022using namespace clang::driver;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000023
Daniel Dunbar31a76e32009-03-18 22:16:03 +000024Compilation::Compilation(Driver &D,
25 ToolChain &_DefaultToolChain,
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000026 InputArgList *_Args)
Daniel Dunbar31a76e32009-03-18 22:16:03 +000027 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) {
Daniel Dunbar63c4da92009-03-02 19:59:07 +000028}
29
Daniel Dunbar9d625e12009-03-16 06:42:30 +000030Compilation::~Compilation() {
31 delete Args;
32
33 // Free any derived arg lists.
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000034 for (llvm::DenseMap<const ToolChain*, DerivedArgList*>::iterator
35 it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it)
36 delete it->second;
Daniel Dunbar73f7b8c2009-03-18 02:55:38 +000037
38 // Free the actions, if built.
39 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
40 it != ie; ++it)
41 delete *it;
Daniel Dunbar9d625e12009-03-16 06:42:30 +000042}
43
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000044const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC) {
Daniel Dunbar9d625e12009-03-16 06:42:30 +000045 if (!TC)
46 TC = &DefaultToolChain;
47
Daniel Dunbara16e4fe2009-03-25 04:13:45 +000048 DerivedArgList *&Entry = TCArgs[TC];
Daniel Dunbare5640282009-03-18 05:58:45 +000049 if (!Entry)
50 Entry = TC->TranslateArgs(*Args);
Daniel Dunbar9d625e12009-03-16 06:42:30 +000051
Daniel Dunbare5640282009-03-18 05:58:45 +000052 return *Entry;
Daniel Dunbar63c4da92009-03-02 19:59:07 +000053}
54
Daniel Dunbar1fef88d2009-03-18 22:44:24 +000055void 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)) {
Daniel Dunbar1b265972009-03-18 06:49:39 +000058 OS << " \"" << C->getExecutable() << '"';
59 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar1fef88d2009-03-18 22:44:24 +000060 ie = C->getArguments().end(); it != ie; ++it) {
61 if (Quote)
62 OS << " \"" << *it << '"';
63 else
64 OS << ' ' << *it;
65 }
Daniel Dunbar1b265972009-03-18 06:49:39 +000066 OS << Terminator;
Daniel Dunbar1fef88d2009-03-18 22:44:24 +000067 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Daniel Dunbar1b265972009-03-18 06:49:39 +000068 for (PipedJob::const_iterator
69 it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
Daniel Dunbar1fef88d2009-03-18 22:44:24 +000070 PrintJob(OS, **it, (it + 1 != PJ->end()) ? " |\n" : "\n", Quote);
Daniel Dunbar1b265972009-03-18 06:49:39 +000071 } else {
Daniel Dunbar1fef88d2009-03-18 22:44:24 +000072 const JobList *Jobs = cast<JobList>(&J);
Daniel Dunbar1b265972009-03-18 06:49:39 +000073 for (JobList::const_iterator
74 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar1fef88d2009-03-18 22:44:24 +000075 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar1b265972009-03-18 06:49:39 +000076 }
77}
78
Daniel Dunbar31a76e32009-03-18 22:16:03 +000079bool Compilation::CleanupFileList(const ArgStringList &Files,
80 bool IssueErrors) const {
81 bool Success = true;
82
83 for (ArgStringList::const_iterator
84 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
85 llvm::sys::Path P(*it);
86 std::string Error;
87
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.
92
93 // FIXME: Grumble, P.exists() is broken. PR3837.
94 struct stat buf;
Daniel Dunbar1fef88d2009-03-18 22:44:24 +000095 if (::stat(P.c_str(), &buf) == 0
96 || errno != ENOENT) {
Daniel Dunbar31a76e32009-03-18 22:16:03 +000097 if (IssueErrors)
98 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
99 << Error;
100 Success = false;
101 }
102 }
103 }
104
105 return Success;
106}
107
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000108int Compilation::ExecuteCommand(const Command &C,
109 const Command *&FailingCommand) const {
Daniel Dunbar3c27dbc2009-03-19 08:01:45 +0000110 llvm::sys::Path Prog(C.getExecutable());
111 const char **Argv = new const char*[C.getArguments().size() + 2];
112 Argv[0] = C.getExecutable();
113 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
114 Argv[C.getArguments().size() + 1] = 0;
115
116 if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v))
117 PrintJob(llvm::errs(), C, "\n", false);
118
119 std::string Error;
120 int Res =
121 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
122 /*env*/0, /*redirects*/0,
123 /*secondsToWait*/0, /*memoryLimit*/0,
124 &Error);
125 if (!Error.empty()) {
126 assert(Res && "Error string set with 0 result code!");
127 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
128 }
129
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000130 if (Res)
131 FailingCommand = &C;
132
Daniel Dunbar3c27dbc2009-03-19 08:01:45 +0000133 delete[] Argv;
134 return Res;
135}
136
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000137int Compilation::ExecuteJob(const Job &J,
138 const Command *&FailingCommand) const {
Daniel Dunbar1fef88d2009-03-18 22:44:24 +0000139 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000140 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar1fef88d2009-03-18 22:44:24 +0000141 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Daniel Dunbar3c27dbc2009-03-19 08:01:45 +0000142 // Piped commands with a single job are easy.
143 if (PJ->size() == 1)
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000144 return ExecuteCommand(**PJ->begin(), FailingCommand);
Daniel Dunbar3c27dbc2009-03-19 08:01:45 +0000145
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000146 FailingCommand = *PJ->begin();
Daniel Dunbar1fef88d2009-03-18 22:44:24 +0000147 getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
148 return 1;
149 } else {
150 const JobList *Jobs = cast<JobList>(&J);
151 for (JobList::const_iterator
152 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000153 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar1fef88d2009-03-18 22:44:24 +0000154 return Res;
155 return 0;
156 }
157}
158
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000159int Compilation::Execute() const {
Daniel Dunbar1b265972009-03-18 06:49:39 +0000160 // Just print if -### was present.
161 if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar1fef88d2009-03-18 22:44:24 +0000162 PrintJob(llvm::errs(), Jobs, "\n", true);
Daniel Dunbar1b265972009-03-18 06:49:39 +0000163 return 0;
164 }
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000165
Daniel Dunbar37dda8b2009-03-21 00:40:53 +0000166 // If there were errors building the compilation, quit now.
167 if (getDriver().getDiags().getNumErrors())
168 return 1;
169
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000170 const Command *FailingCommand = 0;
171 int Res = ExecuteJob(Jobs, FailingCommand);
Daniel Dunbar1b265972009-03-18 06:49:39 +0000172
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000173 // Remove temp files.
174 CleanupFileList(TempFiles);
175
176 // If the compilation failed, remove result files as well.
Daniel Dunbar217f5a02009-03-24 17:49:01 +0000177 if (Res != 0 && !getArgs().hasArg(options::OPT_save_temps))
Daniel Dunbar31a76e32009-03-18 22:16:03 +0000178 CleanupFileList(ResultFiles, true);
179
Daniel Dunbarc78c5f62009-07-01 19:14:39 +0000180 // Print extra information about abnormal failures, if possible.
181 if (Res) {
182 // This is ad-hoc, but we don't want to be excessively noisy. If the result
183 // status was 1, assume the command failed normally. In particular, if it
184 // was the compiler then assume it gave a reasonable error code. Failures in
185 // other tools are less common, and they generally have worse diagnostics,
186 // so always print the diagnostic there.
187 const Action &Source = FailingCommand->getSource();
188 bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
189 isa<PrecompileJobAction>(Source) ||
190 isa<AnalyzeJobAction>(Source) ||
191 isa<CompileJobAction>(Source));
192
193 if (!IsFriendlyTool || Res != 1) {
194 // FIXME: See FIXME above regarding result code interpretation.
195 if (Res < 0)
196 getDriver().Diag(clang::diag::err_drv_command_signalled)
197 << Source.getClassName() << -Res;
198 else
199 getDriver().Diag(clang::diag::err_drv_command_failed)
200 << Source.getClassName() << Res;
201 }
202 }
203
Daniel Dunbar0ef9a232009-03-19 08:03:45 +0000204 return Res;
Daniel Dunbar63c4da92009-03-02 19:59:07 +0000205}