blob: b24d38161aa7b3c571ec5b4d9767a941c05c786e [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)
Artem Belevich5e2a3ec2015-11-17 22:28:40 +000027 : TheDriver(D), DefaultToolChain(_DefaultToolChain),
28 CudaHostToolChain(&DefaultToolChain), CudaDeviceToolChain(nullptr),
29 Args(_Args), TranslatedArgs(_TranslatedArgs), Redirects(nullptr),
Justin Bogner332a5e52014-06-20 22:16:00 +000030 ForDiagnostics(false) {}
Daniel Dunbar544ecd12009-03-02 19:59:07 +000031
Mike Stump11289f42009-09-09 15:08:12 +000032Compilation::~Compilation() {
Daniel Dunbar775d4062010-06-11 22:00:26 +000033 delete TranslatedArgs;
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000034 delete Args;
Mike Stump11289f42009-09-09 15:08:12 +000035
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000036 // Free any derived arg lists.
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000037 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
38 DerivedArgList*>::iterator it = TCArgs.begin(),
39 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbar775d4062010-06-11 22:00:26 +000040 if (it->second != TranslatedArgs)
41 delete it->second;
Daniel Dunbarf0eddb82009-03-18 02:55:38 +000042
Chad Rosierbe10f982011-08-02 17:58:04 +000043 // Free redirections of stdout/stderr.
44 if (Redirects) {
45 delete Redirects[1];
46 delete Redirects[2];
47 delete [] Redirects;
48 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000049}
50
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000051const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
52 const char *BoundArch) {
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000053 if (!TC)
54 TC = &DefaultToolChain;
55
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000056 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar775d4062010-06-11 22:00:26 +000057 if (!Entry) {
58 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
59 if (!Entry)
60 Entry = TranslatedArgs;
61 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000062
Daniel Dunbaraabaac42009-03-18 05:58:45 +000063 return *Entry;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000064}
65
Chad Rosier633dcdc2013-01-24 19:14:47 +000066bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
Rafael Espindola03bc6822013-06-24 17:59:44 +000067 // FIXME: Why are we trying to remove files that we have not created? For
68 // example we should only try to remove a temporary assembly file if
69 // "clang -cc1" succeed in writing it. Was this a workaround for when
70 // clang was writing directly to a .s file and sometimes leaving it behind
71 // during a failure?
72
73 // FIXME: If this is necessary, we can still try to split
74 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
75 // duplicated stat from is_regular_file.
Chad Rosier633dcdc2013-01-24 19:14:47 +000076
77 // Don't try to remove files which we don't have write access to (but may be
78 // able to remove), or non-regular files. Underlying tools may have
79 // intentionally not overwritten them.
Rafael Espindola03bc6822013-06-24 17:59:44 +000080 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
Chad Rosier633dcdc2013-01-24 19:14:47 +000081 return true;
82
Rafael Espindolac0809172014-06-12 14:02:15 +000083 if (std::error_code EC = llvm::sys::fs::remove(File)) {
Rafael Espindola03bc6822013-06-24 17:59:44 +000084 // Failure is only failure if the file exists and is "regular". We checked
85 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
86 // so we don't need to check again.
Reid Kleckner0290c9c2014-09-15 17:45:39 +000087
Rafael Espindola03bc6822013-06-24 17:59:44 +000088 if (IssueErrors)
89 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
90 << EC.message();
91 return false;
Chad Rosier633dcdc2013-01-24 19:14:47 +000092 }
93 return true;
94}
95
Mike Stump11289f42009-09-09 15:08:12 +000096bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +000097 bool IssueErrors) const {
98 bool Success = true;
Mike Stump11289f42009-09-09 15:08:12 +000099 for (ArgStringList::const_iterator
Chad Rosier633dcdc2013-01-24 19:14:47 +0000100 it = Files.begin(), ie = Files.end(); it != ie; ++it)
101 Success &= CleanupFile(*it, IssueErrors);
102 return Success;
103}
104
105bool Compilation::CleanupFileMap(const ArgStringMap &Files,
106 const JobAction *JA,
107 bool IssueErrors) const {
108 bool Success = true;
109 for (ArgStringMap::const_iterator
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000110 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghandb521ec2009-11-24 15:23:21 +0000111
Chad Rosier633dcdc2013-01-24 19:14:47 +0000112 // If specified, only delete the files associated with the JobAction.
113 // Otherwise, delete all files in the map.
114 if (JA && it->first != JA)
Daniel Dunbar462e7ed2011-04-25 20:43:05 +0000115 continue;
Chad Rosier633dcdc2013-01-24 19:14:47 +0000116 Success &= CleanupFile(it->second, IssueErrors);
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000117 }
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000118 return Success;
119}
120
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000121int Compilation::ExecuteCommand(const Command &C,
122 const Command *&FailingCommand) const {
Rafael Espindola8c424542013-07-23 17:58:53 +0000123 if ((getDriver().CCPrintOptions ||
Chad Rosierbe10f982011-08-02 17:58:04 +0000124 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000125 raw_ostream *OS = &llvm::errs();
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000126
127 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
128 // output stream.
129 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000130 std::error_code EC;
131 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, EC,
Rafael Espindola4fbd3732014-02-24 18:20:21 +0000132 llvm::sys::fs::F_Append |
133 llvm::sys::fs::F_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +0000134 if (EC) {
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000135 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
Rafael Espindoladae941a2014-08-25 18:17:04 +0000136 << EC.message();
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000137 FailingCommand = &C;
138 delete OS;
139 return 1;
140 }
141 }
142
143 if (getDriver().CCPrintOptions)
144 *OS << "[Logging clang options]";
145
Hans Wennborgb212b342013-09-12 18:23:34 +0000146 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000147
148 if (OS != &llvm::errs())
149 delete OS;
150 }
Mike Stump11289f42009-09-09 15:08:12 +0000151
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000152 std::string Error;
Chad Rosierd1475772013-03-26 23:41:30 +0000153 bool ExecutionFailed;
Hans Wennborge8677ef2013-09-12 18:35:08 +0000154 int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000155 if (!Error.empty()) {
156 assert(Res && "Error string set with 0 result code!");
157 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
158 }
Mike Stump11289f42009-09-09 15:08:12 +0000159
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000160 if (Res)
161 FailingCommand = &C;
162
Chad Rosierd1475772013-03-26 23:41:30 +0000163 return ExecutionFailed ? 1 : Res;
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000164}
165
Justin Lebar8671c442016-02-24 21:49:28 +0000166void Compilation::ExecuteJobs(
167 const JobList &Jobs,
168 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const {
Justin Bogner0cd92482015-07-02 22:52:08 +0000169 for (const auto &Job : Jobs) {
Craig Topper92fc2df2014-05-17 16:56:41 +0000170 const Command *FailingCommand = nullptr;
Justin Lebar8671c442016-02-24 21:49:28 +0000171 if (int Res = ExecuteCommand(Job, FailingCommand)) {
Chad Rosierdd60e092013-01-29 20:15:05 +0000172 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
Justin Lebar8671c442016-02-24 21:49:28 +0000173 // Bail as soon as one command fails, so we don't output duplicate error
174 // messages if we die on e.g. the same file.
175 return;
176 }
Daniel Dunbar64316c42009-03-18 22:44:24 +0000177 }
178}
Chad Rosierbe10f982011-08-02 17:58:04 +0000179
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000180void Compilation::initCompilationForDiagnostics() {
Justin Bogner332a5e52014-06-20 22:16:00 +0000181 ForDiagnostics = true;
182
Chad Rosierbe10f982011-08-02 17:58:04 +0000183 // Free actions and jobs.
Justin Lebar41094612016-01-11 23:07:27 +0000184 Actions.clear();
185 AllActions.clear();
Chad Rosierbe10f982011-08-02 17:58:04 +0000186 Jobs.clear();
187
188 // Clear temporary/results file lists.
189 TempFiles.clear();
190 ResultFiles.clear();
Chad Rosierc5103c32013-01-29 23:57:10 +0000191 FailureResultFiles.clear();
Chad Rosierbe10f982011-08-02 17:58:04 +0000192
193 // Remove any user specified output. Claim any unclaimed arguments, so as
194 // to avoid emitting warnings about unused args.
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000195 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
196 options::OPT_MMD };
Chad Rosier45cf50f2012-05-03 21:25:34 +0000197 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000198 if (TranslatedArgs->hasArg(OutputOpts[i]))
199 TranslatedArgs->eraseArg(OutputOpts[i]);
200 }
Chad Rosierbe10f982011-08-02 17:58:04 +0000201 TranslatedArgs->ClaimAllArgs();
202
203 // Redirect stdout/stderr to /dev/null.
Rafael Espindolacb4bb192013-06-13 20:08:52 +0000204 Redirects = new const StringRef*[3]();
Craig Topper92fc2df2014-05-17 16:56:41 +0000205 Redirects[0] = nullptr;
Craig Topperbf3e3272014-08-30 16:55:52 +0000206 Redirects[1] = new StringRef();
207 Redirects[2] = new StringRef();
Chad Rosierbe10f982011-08-02 17:58:04 +0000208}
Sebastian Pop980920a2012-04-16 04:16:43 +0000209
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000210StringRef Compilation::getSysRoot() const {
Sebastian Pop980920a2012-04-16 04:16:43 +0000211 return getDriver().SysRoot;
212}