blob: 5e7babef0da61e7fda3b8796f35b080cc60b1f22 [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"
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
Chad Rosier9d718632013-01-24 19:14:47 +000073bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
Rafael Espindolade2b5232013-06-24 17:59:44 +000074 std::string P(File);
75
76 // FIXME: Why are we trying to remove files that we have not created? For
77 // example we should only try to remove a temporary assembly file if
78 // "clang -cc1" succeed in writing it. Was this a workaround for when
79 // clang was writing directly to a .s file and sometimes leaving it behind
80 // during a failure?
81
82 // FIXME: If this is necessary, we can still try to split
83 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
84 // duplicated stat from is_regular_file.
Chad Rosier9d718632013-01-24 19:14:47 +000085
86 // Don't try to remove files which we don't have write access to (but may be
87 // able to remove), or non-regular files. Underlying tools may have
88 // intentionally not overwritten them.
Rafael Espindolade2b5232013-06-24 17:59:44 +000089 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
Chad Rosier9d718632013-01-24 19:14:47 +000090 return true;
91
Rafael Espindolade2b5232013-06-24 17:59:44 +000092 if (llvm::error_code EC = llvm::sys::fs::remove(File)) {
93 // Failure is only failure if the file exists and is "regular". We checked
94 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
95 // so we don't need to check again.
Chad Rosier9d718632013-01-24 19:14:47 +000096
Rafael Espindolade2b5232013-06-24 17:59:44 +000097 if (IssueErrors)
98 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
99 << EC.message();
100 return false;
Chad Rosier9d718632013-01-24 19:14:47 +0000101 }
102 return true;
103}
104
Mike Stump1eb44332009-09-09 15:08:12 +0000105bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +0000106 bool IssueErrors) const {
107 bool Success = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000108 for (ArgStringList::const_iterator
Chad Rosier9d718632013-01-24 19:14:47 +0000109 it = Files.begin(), ie = Files.end(); it != ie; ++it)
110 Success &= CleanupFile(*it, IssueErrors);
111 return Success;
112}
113
114bool Compilation::CleanupFileMap(const ArgStringMap &Files,
115 const JobAction *JA,
116 bool IssueErrors) const {
117 bool Success = true;
118 for (ArgStringMap::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +0000119 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000120
Chad Rosier9d718632013-01-24 19:14:47 +0000121 // If specified, only delete the files associated with the JobAction.
122 // Otherwise, delete all files in the map.
123 if (JA && it->first != JA)
Daniel Dunbar8ac127a2011-04-25 20:43:05 +0000124 continue;
Chad Rosier9d718632013-01-24 19:14:47 +0000125 Success &= CleanupFile(it->second, IssueErrors);
Daniel Dunbare530ad42009-03-18 22:16:03 +0000126 }
Daniel Dunbare530ad42009-03-18 22:16:03 +0000127 return Success;
128}
129
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000130int Compilation::ExecuteCommand(const Command &C,
131 const Command *&FailingCommand) const {
Rafael Espindola8ce90542013-06-24 16:46:15 +0000132 std::string Prog(C.getExecutable());
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000133 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Rafael Espindola7d3240d2013-07-23 17:58:53 +0000138 if ((getDriver().CCPrintOptions ||
Chad Rosier2b819102011-08-02 17:58:04 +0000139 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140 raw_ostream *OS = &llvm::errs();
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000141
142 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
143 // output stream.
144 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
145 std::string Error;
Rafael Espindolad965f952013-07-16 19:44:23 +0000146 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, Error,
147 llvm::sys::fs::F_Append);
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000148 if (!Error.empty()) {
149 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
150 << Error;
151 FailingCommand = &C;
152 delete OS;
153 return 1;
154 }
155 }
156
157 if (getDriver().CCPrintOptions)
158 *OS << "[Logging clang options]";
159
Hans Wennborgfc338972013-09-12 18:23:34 +0000160 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000161
162 if (OS != &llvm::errs())
163 delete OS;
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000166 std::string Error;
Chad Rosierc48d5752013-03-26 23:41:30 +0000167 bool ExecutionFailed;
Rafael Espindola8ce90542013-06-24 16:46:15 +0000168 int Res = llvm::sys::ExecuteAndWait(Prog, Argv, /*env*/ 0, Redirects,
Rafael Espindolaa6035692013-06-12 20:44:26 +0000169 /*secondsToWait*/ 0, /*memoryLimit*/ 0,
170 &Error, &ExecutionFailed);
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000171 if (!Error.empty()) {
172 assert(Res && "Error string set with 0 result code!");
173 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
174 }
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000176 if (Res)
177 FailingCommand = &C;
178
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000179 delete[] Argv;
Chad Rosierc48d5752013-03-26 23:41:30 +0000180 return ExecutionFailed ? 1 : Res;
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000181}
182
Chad Rosier4c4df452013-02-27 18:46:04 +0000183typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
184
185static bool ActionFailed(const Action *A,
186 const FailingCommandList &FailingCommands) {
187
188 if (FailingCommands.empty())
189 return false;
190
191 for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
192 CE = FailingCommands.end(); CI != CE; ++CI)
193 if (A == &(CI->second->getSource()))
194 return true;
195
196 for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
197 if (ActionFailed(*AI, FailingCommands))
198 return true;
199
200 return false;
201}
202
203static bool InputsOk(const Command &C,
204 const FailingCommandList &FailingCommands) {
205 return !ActionFailed(&C.getSource(), FailingCommands);
206}
207
Chad Rosiera16355c2013-01-29 20:15:05 +0000208void Compilation::ExecuteJob(const Job &J,
Chad Rosier4c4df452013-02-27 18:46:04 +0000209 FailingCommandList &FailingCommands) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000210 if (const Command *C = dyn_cast<Command>(&J)) {
Chad Rosier4c4df452013-02-27 18:46:04 +0000211 if (!InputsOk(*C, FailingCommands))
212 return;
Chad Rosiera16355c2013-01-29 20:15:05 +0000213 const Command *FailingCommand = 0;
214 if (int Res = ExecuteCommand(*C, FailingCommand))
215 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000216 } else {
217 const JobList *Jobs = cast<JobList>(&J);
Chad Rosiera16355c2013-01-29 20:15:05 +0000218 for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
219 it != ie; ++it)
220 ExecuteJob(**it, FailingCommands);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000221 }
222}
Chad Rosier2b819102011-08-02 17:58:04 +0000223
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +0000224void Compilation::initCompilationForDiagnostics() {
Chad Rosier2b819102011-08-02 17:58:04 +0000225 // Free actions and jobs.
226 DeleteContainerPointers(Actions);
227 Jobs.clear();
228
229 // Clear temporary/results file lists.
230 TempFiles.clear();
231 ResultFiles.clear();
Chad Rosier8425a542013-01-29 23:57:10 +0000232 FailureResultFiles.clear();
Chad Rosier2b819102011-08-02 17:58:04 +0000233
234 // Remove any user specified output. Claim any unclaimed arguments, so as
235 // to avoid emitting warnings about unused args.
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000236 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
237 options::OPT_MMD };
Chad Rosierf9e156c2012-05-03 21:25:34 +0000238 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000239 if (TranslatedArgs->hasArg(OutputOpts[i]))
240 TranslatedArgs->eraseArg(OutputOpts[i]);
241 }
Chad Rosier2b819102011-08-02 17:58:04 +0000242 TranslatedArgs->ClaimAllArgs();
243
244 // Redirect stdout/stderr to /dev/null.
Rafael Espindola57a3bbf2013-06-13 20:08:52 +0000245 Redirects = new const StringRef*[3]();
246 Redirects[0] = 0;
247 Redirects[1] = new const StringRef();
248 Redirects[2] = new const StringRef();
Chad Rosier2b819102011-08-02 17:58:04 +0000249}
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000250
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +0000251StringRef Compilation::getSysRoot() const {
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000252 return getDriver().SysRoot;
253}