blob: 50725fc678d98df6a34db0270286bb46e63d0dc0 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- Compilation.cpp - Compilation Task Implementation ----------------===//
Daniel Dunbar3ede8d02009-03-02 19:59:07 +00002//
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 Dunbar21549232009-03-18 02:55:38 +000011#include "clang/Driver/Action.h"
Daniel Dunbare530ad42009-03-18 22:16:03 +000012#include "clang/Driver/Driver.h"
13#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000014#include "clang/Driver/Options.h"
Daniel Dunbar586dc232009-03-16 06:42:30 +000015#include "clang/Driver/ToolChain.h"
Chad Rosier2b819102011-08-02 17:58:04 +000016#include "llvm/ADT/STLExtras.h"
Chad Rosierc91b41a2012-10-31 18:31:33 +000017#include "llvm/ADT/StringSwitch.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000018#include "llvm/Option/ArgList.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000019#include "llvm/Support/Program.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "llvm/Support/raw_ostream.h"
Daniel Dunbare530ad42009-03-18 22:16:03 +000021#include <errno.h>
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include <sys/stat.h>
Francois Pichet1c3199a2011-07-23 11:36:43 +000023
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000024using namespace clang::driver;
Francois Pichet1c3199a2011-07-23 11:36:43 +000025using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000026using namespace llvm::opt;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000027
Daniel Dunbar279c1db2010-06-11 22:00:26 +000028Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
29 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
30 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
Chad Rosier2b819102011-08-02 17:58:04 +000031 TranslatedArgs(_TranslatedArgs), Redirects(0) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000032}
33
Mike Stump1eb44332009-09-09 15:08:12 +000034Compilation::~Compilation() {
Daniel Dunbar279c1db2010-06-11 22:00:26 +000035 delete TranslatedArgs;
Daniel Dunbar586dc232009-03-16 06:42:30 +000036 delete Args;
Mike Stump1eb44332009-09-09 15:08:12 +000037
Daniel Dunbar586dc232009-03-16 06:42:30 +000038 // Free any derived arg lists.
Daniel Dunbar49540182009-09-09 18:36:01 +000039 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
40 DerivedArgList*>::iterator it = TCArgs.begin(),
41 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbar279c1db2010-06-11 22:00:26 +000042 if (it->second != TranslatedArgs)
43 delete it->second;
Daniel Dunbar21549232009-03-18 02:55:38 +000044
45 // Free the actions, if built.
Mike Stump1eb44332009-09-09 15:08:12 +000046 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbar21549232009-03-18 02:55:38 +000047 it != ie; ++it)
48 delete *it;
Chad Rosier2b819102011-08-02 17:58:04 +000049
50 // Free redirections of stdout/stderr.
51 if (Redirects) {
52 delete Redirects[1];
53 delete Redirects[2];
54 delete [] Redirects;
55 }
Daniel Dunbar586dc232009-03-16 06:42:30 +000056}
57
Daniel Dunbar49540182009-09-09 18:36:01 +000058const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
59 const char *BoundArch) {
Daniel Dunbar586dc232009-03-16 06:42:30 +000060 if (!TC)
61 TC = &DefaultToolChain;
62
Daniel Dunbar49540182009-09-09 18:36:01 +000063 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar279c1db2010-06-11 22:00:26 +000064 if (!Entry) {
65 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
66 if (!Entry)
67 Entry = TranslatedArgs;
68 }
Daniel Dunbar586dc232009-03-16 06:42:30 +000069
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000070 return *Entry;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000071}
72
Chris Lattner5f9e2722011-07-23 10:55:15 +000073void Compilation::PrintJob(raw_ostream &OS, const Job &J,
Daniel Dunbar49b98e72009-03-18 22:44:24 +000074 const char *Terminator, bool Quote) const {
75 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000076 OS << " \"" << C->getExecutable() << '"';
77 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar49b98e72009-03-18 22:44:24 +000078 ie = C->getArguments().end(); it != ie; ++it) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000079 OS << ' ';
Benjamin Kramer5f226142011-10-06 22:53:35 +000080 if (!Quote && !std::strpbrk(*it, " \"\\$")) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000081 OS << *it;
82 continue;
83 }
84
85 // Quote the argument and escape shell special characters; this isn't
86 // really complete but is good enough.
87 OS << '"';
88 for (const char *s = *it; *s; ++s) {
89 if (*s == '"' || *s == '\\' || *s == '$')
90 OS << '\\';
91 OS << *s;
92 }
93 OS << '"';
Daniel Dunbar49b98e72009-03-18 22:44:24 +000094 }
Daniel Dunbar24b55602009-03-18 06:49:39 +000095 OS << Terminator;
Daniel Dunbar24b55602009-03-18 06:49:39 +000096 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +000097 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +000098 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000099 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000100 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +0000101 }
102}
103
Chad Rosierc91b41a2012-10-31 18:31:33 +0000104static bool skipArg(const char *Flag, bool &SkipNextArg) {
105 StringRef FlagRef(Flag);
106
107 // Assume we're going to see -Flag <Arg>.
108 SkipNextArg = true;
109
110 // These flags are all of the form -Flag <Arg> and are treated as two
111 // arguments. Therefore, we need to skip the flag and the next argument.
112 bool Res = llvm::StringSwitch<bool>(Flag)
113 .Cases("-I", "-MF", "-MT", "-MQ", true)
114 .Cases("-o", "-coverage-file", "-dependency-file", true)
Douglas Gregor250172a2013-02-07 22:59:12 +0000115 .Cases("-fdebug-compilation-dir", "-idirafter", true)
Chad Rosierc91b41a2012-10-31 18:31:33 +0000116 .Cases("-include", "-include-pch", "-internal-isystem", true)
Chad Rosier1cd46de2012-10-31 21:08:30 +0000117 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
Chad Rosierc91b41a2012-10-31 18:31:33 +0000118 .Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true)
119 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
120 .Case("-dwarf-debug-flags", true)
121 .Default(false);
122
123 // Match found.
124 if (Res)
125 return Res;
126
127 // The remaining flags are treated as a single argument.
128 SkipNextArg = false;
129
130 // These flags are all of the form -Flag and have no second argument.
131 Res = llvm::StringSwitch<bool>(Flag)
132 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
133 .Case("-MMD", true)
134 .Default(false);
135
136 // Match found.
137 if (Res)
138 return Res;
139
140 // These flags are treated as a single argument (e.g., -F<Dir>).
141 if (FlagRef.startswith("-F") || FlagRef.startswith("-I"))
142 return true;
143
144 return false;
145}
146
147static bool quoteNextArg(const char *flag) {
148 return llvm::StringSwitch<bool>(flag)
149 .Case("-D", true)
150 .Default(false);
151}
152
153void Compilation::PrintDiagnosticJob(raw_ostream &OS, const Job &J) const {
154 if (const Command *C = dyn_cast<Command>(&J)) {
155 OS << C->getExecutable();
156 unsigned QuoteNextArg = 0;
157 for (ArgStringList::const_iterator it = C->getArguments().begin(),
158 ie = C->getArguments().end(); it != ie; ++it) {
159
160 bool SkipNext;
161 if (skipArg(*it, SkipNext)) {
162 if (SkipNext) ++it;
163 continue;
164 }
165
166 if (!QuoteNextArg)
167 QuoteNextArg = quoteNextArg(*it) ? 2 : 0;
168
169 OS << ' ';
170
171 if (QuoteNextArg == 1)
172 OS << '"';
173
174 if (!std::strpbrk(*it, " \"\\$")) {
175 OS << *it;
176 } else {
177 // Quote the argument and escape shell special characters; this isn't
178 // really complete but is good enough.
179 OS << '"';
180 for (const char *s = *it; *s; ++s) {
181 if (*s == '"' || *s == '\\' || *s == '$')
182 OS << '\\';
183 OS << *s;
184 }
185 OS << '"';
186 }
187
188 if (QuoteNextArg) {
189 if (QuoteNextArg == 1)
190 OS << '"';
191 --QuoteNextArg;
192 }
193 }
194 OS << '\n';
195 } else {
196 const JobList *Jobs = cast<JobList>(&J);
197 for (JobList::const_iterator
198 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
199 PrintDiagnosticJob(OS, **it);
200 }
201}
202
Chad Rosier9d718632013-01-24 19:14:47 +0000203bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
204 llvm::sys::Path P(File);
205 std::string Error;
206
207 // Don't try to remove files which we don't have write access to (but may be
208 // able to remove), or non-regular files. Underlying tools may have
209 // intentionally not overwritten them.
210 if (!P.canWrite() || !P.isRegularFile())
211 return true;
212
213 if (P.eraseFromDisk(false, &Error)) {
214 // Failure is only failure if the file exists and is "regular". There is
215 // a race condition here due to the limited interface of
216 // llvm::sys::Path, we want to know if the removal gave ENOENT.
217
218 // FIXME: Grumble, P.exists() is broken. PR3837.
219 struct stat buf;
220 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
221 (errno != ENOENT)) {
222 if (IssueErrors)
223 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
224 << Error;
225 return false;
226 }
227 }
228 return true;
229}
230
Mike Stump1eb44332009-09-09 15:08:12 +0000231bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +0000232 bool IssueErrors) const {
233 bool Success = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000234 for (ArgStringList::const_iterator
Chad Rosier9d718632013-01-24 19:14:47 +0000235 it = Files.begin(), ie = Files.end(); it != ie; ++it)
236 Success &= CleanupFile(*it, IssueErrors);
237 return Success;
238}
239
240bool Compilation::CleanupFileMap(const ArgStringMap &Files,
241 const JobAction *JA,
242 bool IssueErrors) const {
243 bool Success = true;
244 for (ArgStringMap::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +0000245 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000246
Chad Rosier9d718632013-01-24 19:14:47 +0000247 // If specified, only delete the files associated with the JobAction.
248 // Otherwise, delete all files in the map.
249 if (JA && it->first != JA)
Daniel Dunbar8ac127a2011-04-25 20:43:05 +0000250 continue;
Chad Rosier9d718632013-01-24 19:14:47 +0000251 Success &= CleanupFile(it->second, IssueErrors);
Daniel Dunbare530ad42009-03-18 22:16:03 +0000252 }
Daniel Dunbare530ad42009-03-18 22:16:03 +0000253 return Success;
254}
255
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000256int Compilation::ExecuteCommand(const Command &C,
257 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000258 llvm::sys::Path Prog(C.getExecutable());
259 const char **Argv = new const char*[C.getArguments().size() + 2];
260 Argv[0] = C.getExecutable();
261 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
262 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chad Rosier2b819102011-08-02 17:58:04 +0000264 if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
265 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266 raw_ostream *OS = &llvm::errs();
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000267
268 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
269 // output stream.
270 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
271 std::string Error;
272 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
273 Error,
274 llvm::raw_fd_ostream::F_Append);
275 if (!Error.empty()) {
276 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
277 << Error;
278 FailingCommand = &C;
279 delete OS;
280 return 1;
281 }
282 }
283
284 if (getDriver().CCPrintOptions)
285 *OS << "[Logging clang options]";
286
287 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
288
289 if (OS != &llvm::errs())
290 delete OS;
291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000293 std::string Error;
Chad Rosierc48d5752013-03-26 23:41:30 +0000294 bool ExecutionFailed;
Rafael Espindola57a3bbf2013-06-13 20:08:52 +0000295 int Res = llvm::sys::ExecuteAndWait(Prog.str(), Argv, /*env*/ 0, Redirects,
Rafael Espindolaa6035692013-06-12 20:44:26 +0000296 /*secondsToWait*/ 0, /*memoryLimit*/ 0,
297 &Error, &ExecutionFailed);
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000298 if (!Error.empty()) {
299 assert(Res && "Error string set with 0 result code!");
300 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000303 if (Res)
304 FailingCommand = &C;
305
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000306 delete[] Argv;
Chad Rosierc48d5752013-03-26 23:41:30 +0000307 return ExecutionFailed ? 1 : Res;
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000308}
309
Chad Rosier4c4df452013-02-27 18:46:04 +0000310typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
311
312static bool ActionFailed(const Action *A,
313 const FailingCommandList &FailingCommands) {
314
315 if (FailingCommands.empty())
316 return false;
317
318 for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
319 CE = FailingCommands.end(); CI != CE; ++CI)
320 if (A == &(CI->second->getSource()))
321 return true;
322
323 for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
324 if (ActionFailed(*AI, FailingCommands))
325 return true;
326
327 return false;
328}
329
330static bool InputsOk(const Command &C,
331 const FailingCommandList &FailingCommands) {
332 return !ActionFailed(&C.getSource(), FailingCommands);
333}
334
Chad Rosiera16355c2013-01-29 20:15:05 +0000335void Compilation::ExecuteJob(const Job &J,
Chad Rosier4c4df452013-02-27 18:46:04 +0000336 FailingCommandList &FailingCommands) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000337 if (const Command *C = dyn_cast<Command>(&J)) {
Chad Rosier4c4df452013-02-27 18:46:04 +0000338 if (!InputsOk(*C, FailingCommands))
339 return;
Chad Rosiera16355c2013-01-29 20:15:05 +0000340 const Command *FailingCommand = 0;
341 if (int Res = ExecuteCommand(*C, FailingCommand))
342 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000343 } else {
344 const JobList *Jobs = cast<JobList>(&J);
Chad Rosiera16355c2013-01-29 20:15:05 +0000345 for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
346 it != ie; ++it)
347 ExecuteJob(**it, FailingCommands);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000348 }
349}
Chad Rosier2b819102011-08-02 17:58:04 +0000350
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +0000351void Compilation::initCompilationForDiagnostics() {
Chad Rosier2b819102011-08-02 17:58:04 +0000352 // Free actions and jobs.
353 DeleteContainerPointers(Actions);
354 Jobs.clear();
355
356 // Clear temporary/results file lists.
357 TempFiles.clear();
358 ResultFiles.clear();
Chad Rosier8425a542013-01-29 23:57:10 +0000359 FailureResultFiles.clear();
Chad Rosier2b819102011-08-02 17:58:04 +0000360
361 // Remove any user specified output. Claim any unclaimed arguments, so as
362 // to avoid emitting warnings about unused args.
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000363 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
364 options::OPT_MMD };
Chad Rosierf9e156c2012-05-03 21:25:34 +0000365 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000366 if (TranslatedArgs->hasArg(OutputOpts[i]))
367 TranslatedArgs->eraseArg(OutputOpts[i]);
368 }
Chad Rosier2b819102011-08-02 17:58:04 +0000369 TranslatedArgs->ClaimAllArgs();
370
371 // Redirect stdout/stderr to /dev/null.
Rafael Espindola57a3bbf2013-06-13 20:08:52 +0000372 Redirects = new const StringRef*[3]();
373 Redirects[0] = 0;
374 Redirects[1] = new const StringRef();
375 Redirects[2] = new const StringRef();
Chad Rosier2b819102011-08-02 17:58:04 +0000376}
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000377
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +0000378StringRef Compilation::getSysRoot() const {
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000379 return getDriver().SysRoot;
380}