blob: 124ecca4d3bc6543fc63612020b21214788c3a03 [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"
Francois Pichet24fc69e2011-07-23 11:36:43 +000020
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000021using namespace clang::driver;
Francois Pichet24fc69e2011-07-23 11:36:43 +000022using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000023using namespace llvm::opt;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000024
Daniel Dunbar775d4062010-06-11 22:00:26 +000025Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
26 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
Justin Bogner332a5e52014-06-20 22:16:00 +000027 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
28 TranslatedArgs(_TranslatedArgs), Redirects(nullptr),
29 ForDiagnostics(false) {}
Daniel Dunbar544ecd12009-03-02 19:59:07 +000030
Mike Stump11289f42009-09-09 15:08:12 +000031Compilation::~Compilation() {
Daniel Dunbar775d4062010-06-11 22:00:26 +000032 delete TranslatedArgs;
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000033 delete Args;
Mike Stump11289f42009-09-09 15:08:12 +000034
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000035 // Free any derived arg lists.
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000036 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
37 DerivedArgList*>::iterator it = TCArgs.begin(),
38 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbar775d4062010-06-11 22:00:26 +000039 if (it->second != TranslatedArgs)
40 delete it->second;
Daniel Dunbarf0eddb82009-03-18 02:55:38 +000041
42 // Free the actions, if built.
Mike Stump11289f42009-09-09 15:08:12 +000043 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbarf0eddb82009-03-18 02:55:38 +000044 it != ie; ++it)
45 delete *it;
Chad Rosierbe10f982011-08-02 17:58:04 +000046
47 // Free redirections of stdout/stderr.
48 if (Redirects) {
49 delete Redirects[1];
50 delete Redirects[2];
51 delete [] Redirects;
52 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000053}
54
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000055const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
56 const char *BoundArch) {
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000057 if (!TC)
58 TC = &DefaultToolChain;
59
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000060 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar775d4062010-06-11 22:00:26 +000061 if (!Entry) {
62 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
63 if (!Entry)
64 Entry = TranslatedArgs;
65 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000066
Daniel Dunbaraabaac42009-03-18 05:58:45 +000067 return *Entry;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000068}
69
Chad Rosier633dcdc2013-01-24 19:14:47 +000070bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
Rafael Espindola03bc6822013-06-24 17:59:44 +000071 // FIXME: Why are we trying to remove files that we have not created? For
72 // example we should only try to remove a temporary assembly file if
73 // "clang -cc1" succeed in writing it. Was this a workaround for when
74 // clang was writing directly to a .s file and sometimes leaving it behind
75 // during a failure?
76
77 // FIXME: If this is necessary, we can still try to split
78 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
79 // duplicated stat from is_regular_file.
Chad Rosier633dcdc2013-01-24 19:14:47 +000080
81 // Don't try to remove files which we don't have write access to (but may be
82 // able to remove), or non-regular files. Underlying tools may have
83 // intentionally not overwritten them.
Rafael Espindola03bc6822013-06-24 17:59:44 +000084 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
Chad Rosier633dcdc2013-01-24 19:14:47 +000085 return true;
86
Rafael Espindolac0809172014-06-12 14:02:15 +000087 if (std::error_code EC = llvm::sys::fs::remove(File)) {
Rafael Espindola03bc6822013-06-24 17:59:44 +000088 // Failure is only failure if the file exists and is "regular". We checked
89 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
90 // so we don't need to check again.
Reid Kleckner0290c9c2014-09-15 17:45:39 +000091
Rafael Espindola03bc6822013-06-24 17:59:44 +000092 if (IssueErrors)
93 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
94 << EC.message();
95 return false;
Chad Rosier633dcdc2013-01-24 19:14:47 +000096 }
97 return true;
98}
99
Mike Stump11289f42009-09-09 15:08:12 +0000100bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000101 bool IssueErrors) const {
102 bool Success = true;
Mike Stump11289f42009-09-09 15:08:12 +0000103 for (ArgStringList::const_iterator
Chad Rosier633dcdc2013-01-24 19:14:47 +0000104 it = Files.begin(), ie = Files.end(); it != ie; ++it)
105 Success &= CleanupFile(*it, IssueErrors);
106 return Success;
107}
108
109bool Compilation::CleanupFileMap(const ArgStringMap &Files,
110 const JobAction *JA,
111 bool IssueErrors) const {
112 bool Success = true;
113 for (ArgStringMap::const_iterator
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000114 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghandb521ec2009-11-24 15:23:21 +0000115
Chad Rosier633dcdc2013-01-24 19:14:47 +0000116 // If specified, only delete the files associated with the JobAction.
117 // Otherwise, delete all files in the map.
118 if (JA && it->first != JA)
Daniel Dunbar462e7ed2011-04-25 20:43:05 +0000119 continue;
Chad Rosier633dcdc2013-01-24 19:14:47 +0000120 Success &= CleanupFile(it->second, IssueErrors);
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000121 }
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000122 return Success;
123}
124
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000125int Compilation::ExecuteCommand(const Command &C,
126 const Command *&FailingCommand) const {
Rafael Espindola8c424542013-07-23 17:58:53 +0000127 if ((getDriver().CCPrintOptions ||
Chad Rosierbe10f982011-08-02 17:58:04 +0000128 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000129 raw_ostream *OS = &llvm::errs();
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000130
131 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
132 // output stream.
133 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000134 std::error_code EC;
135 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, EC,
Rafael Espindola4fbd3732014-02-24 18:20:21 +0000136 llvm::sys::fs::F_Append |
137 llvm::sys::fs::F_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +0000138 if (EC) {
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000139 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
Rafael Espindoladae941a2014-08-25 18:17:04 +0000140 << EC.message();
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000141 FailingCommand = &C;
142 delete OS;
143 return 1;
144 }
145 }
146
147 if (getDriver().CCPrintOptions)
148 *OS << "[Logging clang options]";
149
Hans Wennborgb212b342013-09-12 18:23:34 +0000150 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000151
152 if (OS != &llvm::errs())
153 delete OS;
154 }
Mike Stump11289f42009-09-09 15:08:12 +0000155
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000156 std::string Error;
Chad Rosierd1475772013-03-26 23:41:30 +0000157 bool ExecutionFailed;
Hans Wennborge8677ef2013-09-12 18:35:08 +0000158 int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000159 if (!Error.empty()) {
160 assert(Res && "Error string set with 0 result code!");
161 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
162 }
Mike Stump11289f42009-09-09 15:08:12 +0000163
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000164 if (Res)
165 FailingCommand = &C;
166
Chad Rosierd1475772013-03-26 23:41:30 +0000167 return ExecutionFailed ? 1 : Res;
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000168}
169
Chad Rosier6a1de5c2013-02-27 18:46:04 +0000170typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
171
172static bool ActionFailed(const Action *A,
173 const FailingCommandList &FailingCommands) {
174
175 if (FailingCommands.empty())
176 return false;
177
178 for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
179 CE = FailingCommands.end(); CI != CE; ++CI)
180 if (A == &(CI->second->getSource()))
181 return true;
182
183 for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
184 if (ActionFailed(*AI, FailingCommands))
185 return true;
186
187 return false;
188}
189
190static bool InputsOk(const Command &C,
191 const FailingCommandList &FailingCommands) {
192 return !ActionFailed(&C.getSource(), FailingCommands);
193}
194
Chad Rosierdd60e092013-01-29 20:15:05 +0000195void Compilation::ExecuteJob(const Job &J,
Chad Rosier6a1de5c2013-02-27 18:46:04 +0000196 FailingCommandList &FailingCommands) const {
Daniel Dunbar64316c42009-03-18 22:44:24 +0000197 if (const Command *C = dyn_cast<Command>(&J)) {
Chad Rosier6a1de5c2013-02-27 18:46:04 +0000198 if (!InputsOk(*C, FailingCommands))
199 return;
Craig Topper92fc2df2014-05-17 16:56:41 +0000200 const Command *FailingCommand = nullptr;
Chad Rosierdd60e092013-01-29 20:15:05 +0000201 if (int Res = ExecuteCommand(*C, FailingCommand))
202 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
Daniel Dunbar64316c42009-03-18 22:44:24 +0000203 } else {
204 const JobList *Jobs = cast<JobList>(&J);
Chad Rosierdd60e092013-01-29 20:15:05 +0000205 for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
206 it != ie; ++it)
207 ExecuteJob(**it, FailingCommands);
Daniel Dunbar64316c42009-03-18 22:44:24 +0000208 }
209}
Chad Rosierbe10f982011-08-02 17:58:04 +0000210
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000211void Compilation::initCompilationForDiagnostics() {
Justin Bogner332a5e52014-06-20 22:16:00 +0000212 ForDiagnostics = true;
213
Chad Rosierbe10f982011-08-02 17:58:04 +0000214 // Free actions and jobs.
215 DeleteContainerPointers(Actions);
216 Jobs.clear();
217
218 // Clear temporary/results file lists.
219 TempFiles.clear();
220 ResultFiles.clear();
Chad Rosierc5103c32013-01-29 23:57:10 +0000221 FailureResultFiles.clear();
Chad Rosierbe10f982011-08-02 17:58:04 +0000222
223 // Remove any user specified output. Claim any unclaimed arguments, so as
224 // to avoid emitting warnings about unused args.
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000225 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
226 options::OPT_MMD };
Chad Rosier45cf50f2012-05-03 21:25:34 +0000227 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000228 if (TranslatedArgs->hasArg(OutputOpts[i]))
229 TranslatedArgs->eraseArg(OutputOpts[i]);
230 }
Chad Rosierbe10f982011-08-02 17:58:04 +0000231 TranslatedArgs->ClaimAllArgs();
232
233 // Redirect stdout/stderr to /dev/null.
Rafael Espindolacb4bb192013-06-13 20:08:52 +0000234 Redirects = new const StringRef*[3]();
Craig Topper92fc2df2014-05-17 16:56:41 +0000235 Redirects[0] = nullptr;
Craig Topperbf3e3272014-08-30 16:55:52 +0000236 Redirects[1] = new StringRef();
237 Redirects[2] = new StringRef();
Chad Rosierbe10f982011-08-02 17:58:04 +0000238}
Sebastian Pop980920a2012-04-16 04:16:43 +0000239
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000240StringRef Compilation::getSysRoot() const {
Sebastian Pop980920a2012-04-16 04:16:43 +0000241 return getDriver().SysRoot;
242}