blob: f077fd68cc4fb26b9c0074bd834c5483fca85d0e [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"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000017#include "llvm/Option/ArgList.h"
Rafael Espindolada1f9cb2013-06-18 20:58:25 +000018#include "llvm/Support/FileSystem.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbare530ad42009-03-18 22:16:03 +000020#include <errno.h>
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include <sys/stat.h>
Francois Pichet1c3199a2011-07-23 11:36:43 +000022
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000023using namespace clang::driver;
Francois Pichet1c3199a2011-07-23 11:36:43 +000024using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000025using namespace llvm::opt;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000026
Daniel Dunbar279c1db2010-06-11 22:00:26 +000027Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
28 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
29 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
Chad Rosier2b819102011-08-02 17:58:04 +000030 TranslatedArgs(_TranslatedArgs), Redirects(0) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000031}
32
Mike Stump1eb44332009-09-09 15:08:12 +000033Compilation::~Compilation() {
Daniel Dunbar279c1db2010-06-11 22:00:26 +000034 delete TranslatedArgs;
Daniel Dunbar586dc232009-03-16 06:42:30 +000035 delete Args;
Mike Stump1eb44332009-09-09 15:08:12 +000036
Daniel Dunbar586dc232009-03-16 06:42:30 +000037 // Free any derived arg lists.
Daniel Dunbar49540182009-09-09 18:36:01 +000038 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
39 DerivedArgList*>::iterator it = TCArgs.begin(),
40 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbar279c1db2010-06-11 22:00:26 +000041 if (it->second != TranslatedArgs)
42 delete it->second;
Daniel Dunbar21549232009-03-18 02:55:38 +000043
44 // Free the actions, if built.
Mike Stump1eb44332009-09-09 15:08:12 +000045 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbar21549232009-03-18 02:55:38 +000046 it != ie; ++it)
47 delete *it;
Chad Rosier2b819102011-08-02 17:58:04 +000048
49 // Free redirections of stdout/stderr.
50 if (Redirects) {
51 delete Redirects[1];
52 delete Redirects[2];
53 delete [] Redirects;
54 }
Daniel Dunbar586dc232009-03-16 06:42:30 +000055}
56
Daniel Dunbar49540182009-09-09 18:36:01 +000057const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
58 const char *BoundArch) {
Daniel Dunbar586dc232009-03-16 06:42:30 +000059 if (!TC)
60 TC = &DefaultToolChain;
61
Daniel Dunbar49540182009-09-09 18:36:01 +000062 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar279c1db2010-06-11 22:00:26 +000063 if (!Entry) {
64 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
65 if (!Entry)
66 Entry = TranslatedArgs;
67 }
Daniel Dunbar586dc232009-03-16 06:42:30 +000068
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000069 return *Entry;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000070}
71
Chad Rosier9d718632013-01-24 19:14:47 +000072bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
Rafael Espindolade2b5232013-06-24 17:59:44 +000073 std::string P(File);
74
75 // FIXME: Why are we trying to remove files that we have not created? For
76 // example we should only try to remove a temporary assembly file if
77 // "clang -cc1" succeed in writing it. Was this a workaround for when
78 // clang was writing directly to a .s file and sometimes leaving it behind
79 // during a failure?
80
81 // FIXME: If this is necessary, we can still try to split
82 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
83 // duplicated stat from is_regular_file.
Chad Rosier9d718632013-01-24 19:14:47 +000084
85 // Don't try to remove files which we don't have write access to (but may be
86 // able to remove), or non-regular files. Underlying tools may have
87 // intentionally not overwritten them.
Rafael Espindolade2b5232013-06-24 17:59:44 +000088 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
Chad Rosier9d718632013-01-24 19:14:47 +000089 return true;
90
Rafael Espindolade2b5232013-06-24 17:59:44 +000091 if (llvm::error_code EC = llvm::sys::fs::remove(File)) {
92 // Failure is only failure if the file exists and is "regular". We checked
93 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
94 // so we don't need to check again.
Chad Rosier9d718632013-01-24 19:14:47 +000095
Rafael Espindolade2b5232013-06-24 17:59:44 +000096 if (IssueErrors)
97 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
98 << EC.message();
99 return false;
Chad Rosier9d718632013-01-24 19:14:47 +0000100 }
101 return true;
102}
103
Mike Stump1eb44332009-09-09 15:08:12 +0000104bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +0000105 bool IssueErrors) const {
106 bool Success = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000107 for (ArgStringList::const_iterator
Chad Rosier9d718632013-01-24 19:14:47 +0000108 it = Files.begin(), ie = Files.end(); it != ie; ++it)
109 Success &= CleanupFile(*it, IssueErrors);
110 return Success;
111}
112
113bool Compilation::CleanupFileMap(const ArgStringMap &Files,
114 const JobAction *JA,
115 bool IssueErrors) const {
116 bool Success = true;
117 for (ArgStringMap::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +0000118 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000119
Chad Rosier9d718632013-01-24 19:14:47 +0000120 // If specified, only delete the files associated with the JobAction.
121 // Otherwise, delete all files in the map.
122 if (JA && it->first != JA)
Daniel Dunbar8ac127a2011-04-25 20:43:05 +0000123 continue;
Chad Rosier9d718632013-01-24 19:14:47 +0000124 Success &= CleanupFile(it->second, IssueErrors);
Daniel Dunbare530ad42009-03-18 22:16:03 +0000125 }
Daniel Dunbare530ad42009-03-18 22:16:03 +0000126 return Success;
127}
128
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000129int Compilation::ExecuteCommand(const Command &C,
130 const Command *&FailingCommand) const {
Rafael Espindola7d3240d2013-07-23 17:58:53 +0000131 if ((getDriver().CCPrintOptions ||
Chad Rosier2b819102011-08-02 17:58:04 +0000132 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000133 raw_ostream *OS = &llvm::errs();
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000134
135 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
136 // output stream.
137 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
138 std::string Error;
Rafael Espindolad965f952013-07-16 19:44:23 +0000139 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, Error,
140 llvm::sys::fs::F_Append);
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000141 if (!Error.empty()) {
142 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
143 << Error;
144 FailingCommand = &C;
145 delete OS;
146 return 1;
147 }
148 }
149
150 if (getDriver().CCPrintOptions)
151 *OS << "[Logging clang options]";
152
Hans Wennborgfc338972013-09-12 18:23:34 +0000153 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000154
155 if (OS != &llvm::errs())
156 delete OS;
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000159 std::string Error;
Chad Rosierc48d5752013-03-26 23:41:30 +0000160 bool ExecutionFailed;
Hans Wennborgaaaa2a12013-09-12 18:35:08 +0000161 int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000162 if (!Error.empty()) {
163 assert(Res && "Error string set with 0 result code!");
164 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000167 if (Res)
168 FailingCommand = &C;
169
Chad Rosierc48d5752013-03-26 23:41:30 +0000170 return ExecutionFailed ? 1 : Res;
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000171}
172
Chad Rosier4c4df452013-02-27 18:46:04 +0000173typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
174
175static bool ActionFailed(const Action *A,
176 const FailingCommandList &FailingCommands) {
177
178 if (FailingCommands.empty())
179 return false;
180
181 for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
182 CE = FailingCommands.end(); CI != CE; ++CI)
183 if (A == &(CI->second->getSource()))
184 return true;
185
186 for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
187 if (ActionFailed(*AI, FailingCommands))
188 return true;
189
190 return false;
191}
192
193static bool InputsOk(const Command &C,
194 const FailingCommandList &FailingCommands) {
195 return !ActionFailed(&C.getSource(), FailingCommands);
196}
197
Chad Rosiera16355c2013-01-29 20:15:05 +0000198void Compilation::ExecuteJob(const Job &J,
Chad Rosier4c4df452013-02-27 18:46:04 +0000199 FailingCommandList &FailingCommands) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000200 if (const Command *C = dyn_cast<Command>(&J)) {
Chad Rosier4c4df452013-02-27 18:46:04 +0000201 if (!InputsOk(*C, FailingCommands))
202 return;
Chad Rosiera16355c2013-01-29 20:15:05 +0000203 const Command *FailingCommand = 0;
204 if (int Res = ExecuteCommand(*C, FailingCommand))
205 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000206 } else {
207 const JobList *Jobs = cast<JobList>(&J);
Chad Rosiera16355c2013-01-29 20:15:05 +0000208 for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
209 it != ie; ++it)
210 ExecuteJob(**it, FailingCommands);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000211 }
212}
Chad Rosier2b819102011-08-02 17:58:04 +0000213
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +0000214void Compilation::initCompilationForDiagnostics() {
Chad Rosier2b819102011-08-02 17:58:04 +0000215 // Free actions and jobs.
216 DeleteContainerPointers(Actions);
217 Jobs.clear();
218
219 // Clear temporary/results file lists.
220 TempFiles.clear();
221 ResultFiles.clear();
Chad Rosier8425a542013-01-29 23:57:10 +0000222 FailureResultFiles.clear();
Chad Rosier2b819102011-08-02 17:58:04 +0000223
224 // Remove any user specified output. Claim any unclaimed arguments, so as
225 // to avoid emitting warnings about unused args.
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000226 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
227 options::OPT_MMD };
Chad Rosierf9e156c2012-05-03 21:25:34 +0000228 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000229 if (TranslatedArgs->hasArg(OutputOpts[i]))
230 TranslatedArgs->eraseArg(OutputOpts[i]);
231 }
Chad Rosier2b819102011-08-02 17:58:04 +0000232 TranslatedArgs->ClaimAllArgs();
233
234 // Redirect stdout/stderr to /dev/null.
Rafael Espindola57a3bbf2013-06-13 20:08:52 +0000235 Redirects = new const StringRef*[3]();
236 Redirects[0] = 0;
237 Redirects[1] = new const StringRef();
238 Redirects[2] = new const StringRef();
Chad Rosier2b819102011-08-02 17:58:04 +0000239}
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000240
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +0000241StringRef Compilation::getSysRoot() const {
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000242 return getDriver().SysRoot;
243}