blob: 66e5eaf894f41a42ca2782789439924a6c3dad20 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- Compilation.cpp - Compilation Task Implementation ----------------===//
Daniel Dunbar544ecd12009-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 Dunbarf0eddb82009-03-18 02:55:38 +000011#include "clang/Driver/Action.h"
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +000012#include "clang/Driver/Driver.h"
13#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000014#include "clang/Driver/Options.h"
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000015#include "clang/Driver/ToolChain.h"
Chad Rosierbe10f982011-08-02 17:58:04 +000016#include "llvm/ADT/STLExtras.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000017#include "llvm/Option/ArgList.h"
Rafael Espindolafc92e842013-06-18 20:58:25 +000018#include "llvm/Support/FileSystem.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +000020#include <errno.h>
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include <sys/stat.h>
Francois Pichet24fc69e2011-07-23 11:36:43 +000022
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000023using namespace clang::driver;
Francois Pichet24fc69e2011-07-23 11:36:43 +000024using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000025using namespace llvm::opt;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000026
Daniel Dunbar775d4062010-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 Rosierbe10f982011-08-02 17:58:04 +000030 TranslatedArgs(_TranslatedArgs), Redirects(0) {
Daniel Dunbar544ecd12009-03-02 19:59:07 +000031}
32
Mike Stump11289f42009-09-09 15:08:12 +000033Compilation::~Compilation() {
Daniel Dunbar775d4062010-06-11 22:00:26 +000034 delete TranslatedArgs;
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000035 delete Args;
Mike Stump11289f42009-09-09 15:08:12 +000036
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000037 // Free any derived arg lists.
Daniel Dunbarb5d86bb2009-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 Dunbar775d4062010-06-11 22:00:26 +000041 if (it->second != TranslatedArgs)
42 delete it->second;
Daniel Dunbarf0eddb82009-03-18 02:55:38 +000043
44 // Free the actions, if built.
Mike Stump11289f42009-09-09 15:08:12 +000045 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbarf0eddb82009-03-18 02:55:38 +000046 it != ie; ++it)
47 delete *it;
Chad Rosierbe10f982011-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 Dunbar3ce436d2009-03-16 06:42:30 +000055}
56
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000057const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
58 const char *BoundArch) {
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000059 if (!TC)
60 TC = &DefaultToolChain;
61
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000062 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar775d4062010-06-11 22:00:26 +000063 if (!Entry) {
64 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
65 if (!Entry)
66 Entry = TranslatedArgs;
67 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000068
Daniel Dunbaraabaac42009-03-18 05:58:45 +000069 return *Entry;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000070}
71
Chad Rosier633dcdc2013-01-24 19:14:47 +000072bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
Rafael Espindola03bc6822013-06-24 17:59:44 +000073 // FIXME: Why are we trying to remove files that we have not created? For
74 // example we should only try to remove a temporary assembly file if
75 // "clang -cc1" succeed in writing it. Was this a workaround for when
76 // clang was writing directly to a .s file and sometimes leaving it behind
77 // during a failure?
78
79 // FIXME: If this is necessary, we can still try to split
80 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
81 // duplicated stat from is_regular_file.
Chad Rosier633dcdc2013-01-24 19:14:47 +000082
83 // Don't try to remove files which we don't have write access to (but may be
84 // able to remove), or non-regular files. Underlying tools may have
85 // intentionally not overwritten them.
Rafael Espindola03bc6822013-06-24 17:59:44 +000086 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
Chad Rosier633dcdc2013-01-24 19:14:47 +000087 return true;
88
Rafael Espindola03bc6822013-06-24 17:59:44 +000089 if (llvm::error_code EC = llvm::sys::fs::remove(File)) {
90 // Failure is only failure if the file exists and is "regular". We checked
91 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
92 // so we don't need to check again.
Chad Rosier633dcdc2013-01-24 19:14:47 +000093
Rafael Espindola03bc6822013-06-24 17:59:44 +000094 if (IssueErrors)
95 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
96 << EC.message();
97 return false;
Chad Rosier633dcdc2013-01-24 19:14:47 +000098 }
99 return true;
100}
101
Mike Stump11289f42009-09-09 15:08:12 +0000102bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000103 bool IssueErrors) const {
104 bool Success = true;
Mike Stump11289f42009-09-09 15:08:12 +0000105 for (ArgStringList::const_iterator
Chad Rosier633dcdc2013-01-24 19:14:47 +0000106 it = Files.begin(), ie = Files.end(); it != ie; ++it)
107 Success &= CleanupFile(*it, IssueErrors);
108 return Success;
109}
110
111bool Compilation::CleanupFileMap(const ArgStringMap &Files,
112 const JobAction *JA,
113 bool IssueErrors) const {
114 bool Success = true;
115 for (ArgStringMap::const_iterator
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000116 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghandb521ec2009-11-24 15:23:21 +0000117
Chad Rosier633dcdc2013-01-24 19:14:47 +0000118 // If specified, only delete the files associated with the JobAction.
119 // Otherwise, delete all files in the map.
120 if (JA && it->first != JA)
Daniel Dunbar462e7ed2011-04-25 20:43:05 +0000121 continue;
Chad Rosier633dcdc2013-01-24 19:14:47 +0000122 Success &= CleanupFile(it->second, IssueErrors);
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000123 }
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000124 return Success;
125}
126
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000127int Compilation::ExecuteCommand(const Command &C,
128 const Command *&FailingCommand) const {
Rafael Espindola8c424542013-07-23 17:58:53 +0000129 if ((getDriver().CCPrintOptions ||
Chad Rosierbe10f982011-08-02 17:58:04 +0000130 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000131 raw_ostream *OS = &llvm::errs();
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000132
133 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
134 // output stream.
135 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
136 std::string Error;
Rafael Espindola16125fb2013-07-16 19:44:23 +0000137 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, Error,
138 llvm::sys::fs::F_Append);
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000139 if (!Error.empty()) {
140 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
141 << Error;
142 FailingCommand = &C;
143 delete OS;
144 return 1;
145 }
146 }
147
148 if (getDriver().CCPrintOptions)
149 *OS << "[Logging clang options]";
150
Hans Wennborgb212b342013-09-12 18:23:34 +0000151 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000152
153 if (OS != &llvm::errs())
154 delete OS;
155 }
Mike Stump11289f42009-09-09 15:08:12 +0000156
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000157 std::string Error;
Chad Rosierd1475772013-03-26 23:41:30 +0000158 bool ExecutionFailed;
Hans Wennborge8677ef2013-09-12 18:35:08 +0000159 int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000160 if (!Error.empty()) {
161 assert(Res && "Error string set with 0 result code!");
162 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
163 }
Mike Stump11289f42009-09-09 15:08:12 +0000164
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000165 if (Res)
166 FailingCommand = &C;
167
Chad Rosierd1475772013-03-26 23:41:30 +0000168 return ExecutionFailed ? 1 : Res;
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000169}
170
Chad Rosier6a1de5c2013-02-27 18:46:04 +0000171typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
172
173static bool ActionFailed(const Action *A,
174 const FailingCommandList &FailingCommands) {
175
176 if (FailingCommands.empty())
177 return false;
178
179 for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
180 CE = FailingCommands.end(); CI != CE; ++CI)
181 if (A == &(CI->second->getSource()))
182 return true;
183
184 for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
185 if (ActionFailed(*AI, FailingCommands))
186 return true;
187
188 return false;
189}
190
191static bool InputsOk(const Command &C,
192 const FailingCommandList &FailingCommands) {
193 return !ActionFailed(&C.getSource(), FailingCommands);
194}
195
Chad Rosierdd60e092013-01-29 20:15:05 +0000196void Compilation::ExecuteJob(const Job &J,
Chad Rosier6a1de5c2013-02-27 18:46:04 +0000197 FailingCommandList &FailingCommands) const {
Daniel Dunbar64316c42009-03-18 22:44:24 +0000198 if (const Command *C = dyn_cast<Command>(&J)) {
Chad Rosier6a1de5c2013-02-27 18:46:04 +0000199 if (!InputsOk(*C, FailingCommands))
200 return;
Chad Rosierdd60e092013-01-29 20:15:05 +0000201 const Command *FailingCommand = 0;
202 if (int Res = ExecuteCommand(*C, FailingCommand))
203 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
Daniel Dunbar64316c42009-03-18 22:44:24 +0000204 } else {
205 const JobList *Jobs = cast<JobList>(&J);
Chad Rosierdd60e092013-01-29 20:15:05 +0000206 for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
207 it != ie; ++it)
208 ExecuteJob(**it, FailingCommands);
Daniel Dunbar64316c42009-03-18 22:44:24 +0000209 }
210}
Chad Rosierbe10f982011-08-02 17:58:04 +0000211
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000212void Compilation::initCompilationForDiagnostics() {
Chad Rosierbe10f982011-08-02 17:58:04 +0000213 // Free actions and jobs.
214 DeleteContainerPointers(Actions);
215 Jobs.clear();
216
217 // Clear temporary/results file lists.
218 TempFiles.clear();
219 ResultFiles.clear();
Chad Rosierc5103c32013-01-29 23:57:10 +0000220 FailureResultFiles.clear();
Chad Rosierbe10f982011-08-02 17:58:04 +0000221
222 // Remove any user specified output. Claim any unclaimed arguments, so as
223 // to avoid emitting warnings about unused args.
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000224 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
225 options::OPT_MMD };
Chad Rosier45cf50f2012-05-03 21:25:34 +0000226 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000227 if (TranslatedArgs->hasArg(OutputOpts[i]))
228 TranslatedArgs->eraseArg(OutputOpts[i]);
229 }
Chad Rosierbe10f982011-08-02 17:58:04 +0000230 TranslatedArgs->ClaimAllArgs();
231
232 // Redirect stdout/stderr to /dev/null.
Rafael Espindolacb4bb192013-06-13 20:08:52 +0000233 Redirects = new const StringRef*[3]();
234 Redirects[0] = 0;
235 Redirects[1] = new const StringRef();
236 Redirects[2] = new const StringRef();
Chad Rosierbe10f982011-08-02 17:58:04 +0000237}
Sebastian Pop980920a2012-04-16 04:16:43 +0000238
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000239StringRef Compilation::getSysRoot() const {
Sebastian Pop980920a2012-04-16 04:16:43 +0000240 return getDriver().SysRoot;
241}