blob: 6a2616f0c2a4d03e3d95269753e55bdd185e4721 [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"
Francois Pichet1c3199a2011-07-23 11:36:43 +000020
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000021using namespace clang::driver;
Francois Pichet1c3199a2011-07-23 11:36:43 +000022using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000023using namespace llvm::opt;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000024
Daniel Dunbar279c1db2010-06-11 22:00:26 +000025Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
26 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070027 : TheDriver(D), DefaultToolChain(_DefaultToolChain), ActiveOffloadMask(0u),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080028 Args(_Args), TranslatedArgs(_TranslatedArgs), Redirects(nullptr),
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070029 ForDiagnostics(false) {
30 // The offloading host toolchain is the default tool chain.
31 OrderedOffloadingToolchains.insert(
32 std::make_pair(Action::OFK_Host, &DefaultToolChain));
33}
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000034
Mike Stump1eb44332009-09-09 15:08:12 +000035Compilation::~Compilation() {
Daniel Dunbar279c1db2010-06-11 22:00:26 +000036 delete TranslatedArgs;
Daniel Dunbar586dc232009-03-16 06:42:30 +000037 delete Args;
Mike Stump1eb44332009-09-09 15:08:12 +000038
Daniel Dunbar586dc232009-03-16 06:42:30 +000039 // Free any derived arg lists.
Daniel Dunbar49540182009-09-09 18:36:01 +000040 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
41 DerivedArgList*>::iterator it = TCArgs.begin(),
42 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbar279c1db2010-06-11 22:00:26 +000043 if (it->second != TranslatedArgs)
44 delete it->second;
Daniel Dunbar21549232009-03-18 02:55:38 +000045
Chad Rosier2b819102011-08-02 17:58:04 +000046 // Free redirections of stdout/stderr.
47 if (Redirects) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070048 delete Redirects[0];
Chad Rosier2b819102011-08-02 17:58:04 +000049 delete Redirects[1];
50 delete Redirects[2];
51 delete [] Redirects;
52 }
Daniel Dunbar586dc232009-03-16 06:42:30 +000053}
54
Daniel Dunbar49540182009-09-09 18:36:01 +000055const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
56 const char *BoundArch) {
Daniel Dunbar586dc232009-03-16 06:42:30 +000057 if (!TC)
58 TC = &DefaultToolChain;
59
Daniel Dunbar49540182009-09-09 18:36:01 +000060 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar279c1db2010-06-11 22:00:26 +000061 if (!Entry) {
62 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
63 if (!Entry)
64 Entry = TranslatedArgs;
65 }
Daniel Dunbar586dc232009-03-16 06:42:30 +000066
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000067 return *Entry;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000068}
69
Chad Rosier9d718632013-01-24 19:14:47 +000070bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
Rafael Espindolade2b5232013-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 Rosier9d718632013-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 Espindolade2b5232013-06-24 17:59:44 +000084 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
Chad Rosier9d718632013-01-24 19:14:47 +000085 return true;
86
Stephen Hinesc568f1e2014-07-21 00:47:37 -070087 if (std::error_code EC = llvm::sys::fs::remove(File)) {
Rafael Espindolade2b5232013-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.
Stephen Hines176edba2014-12-01 14:53:08 -080091
Rafael Espindolade2b5232013-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 Rosier9d718632013-01-24 19:14:47 +000096 }
97 return true;
98}
99
Mike Stump1eb44332009-09-09 15:08:12 +0000100bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +0000101 bool IssueErrors) const {
102 bool Success = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000103 for (ArgStringList::const_iterator
Chad Rosier9d718632013-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 Dunbare530ad42009-03-18 22:16:03 +0000114 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000115
Chad Rosier9d718632013-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 Dunbar8ac127a2011-04-25 20:43:05 +0000119 continue;
Chad Rosier9d718632013-01-24 19:14:47 +0000120 Success &= CleanupFile(it->second, IssueErrors);
Daniel Dunbare530ad42009-03-18 22:16:03 +0000121 }
Daniel Dunbare530ad42009-03-18 22:16:03 +0000122 return Success;
123}
124
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000125int Compilation::ExecuteCommand(const Command &C,
126 const Command *&FailingCommand) const {
Rafael Espindola7d3240d2013-07-23 17:58:53 +0000127 if ((getDriver().CCPrintOptions ||
Chad Rosier2b819102011-08-02 17:58:04 +0000128 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129 raw_ostream *OS = &llvm::errs();
Daniel Dunbar4c00fcd2010-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) {
Stephen Hines176edba2014-12-01 14:53:08 -0800134 std::error_code EC;
135 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, EC,
Stephen Hines651f13c2014-04-23 16:59:28 -0700136 llvm::sys::fs::F_Append |
137 llvm::sys::fs::F_Text);
Stephen Hines176edba2014-12-01 14:53:08 -0800138 if (EC) {
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000139 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
Stephen Hines176edba2014-12-01 14:53:08 -0800140 << EC.message();
Daniel Dunbar4c00fcd2010-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 Wennborgfc338972013-09-12 18:23:34 +0000150 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000151
152 if (OS != &llvm::errs())
153 delete OS;
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000156 std::string Error;
Chad Rosierc48d5752013-03-26 23:41:30 +0000157 bool ExecutionFailed;
Hans Wennborgaaaa2a12013-09-12 18:35:08 +0000158 int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
Daniel Dunbarceafbc82009-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 Stump1eb44332009-09-09 15:08:12 +0000163
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000164 if (Res)
165 FailingCommand = &C;
166
Chad Rosierc48d5752013-03-26 23:41:30 +0000167 return ExecutionFailed ? 1 : Res;
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000168}
169
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700170void Compilation::ExecuteJobs(
171 const JobList &Jobs,
172 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800173 for (const auto &Job : Jobs) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700174 const Command *FailingCommand = nullptr;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700175 if (int Res = ExecuteCommand(Job, FailingCommand)) {
Chad Rosiera16355c2013-01-29 20:15:05 +0000176 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700177 // Bail as soon as one command fails, so we don't output duplicate error
178 // messages if we die on e.g. the same file.
179 return;
180 }
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000181 }
182}
Chad Rosier2b819102011-08-02 17:58:04 +0000183
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +0000184void Compilation::initCompilationForDiagnostics() {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700185 ForDiagnostics = true;
186
Chad Rosier2b819102011-08-02 17:58:04 +0000187 // Free actions and jobs.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700188 Actions.clear();
189 AllActions.clear();
Chad Rosier2b819102011-08-02 17:58:04 +0000190 Jobs.clear();
191
192 // Clear temporary/results file lists.
193 TempFiles.clear();
194 ResultFiles.clear();
Chad Rosier8425a542013-01-29 23:57:10 +0000195 FailureResultFiles.clear();
Chad Rosier2b819102011-08-02 17:58:04 +0000196
197 // Remove any user specified output. Claim any unclaimed arguments, so as
198 // to avoid emitting warnings about unused args.
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000199 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
200 options::OPT_MMD };
Chad Rosierf9e156c2012-05-03 21:25:34 +0000201 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000202 if (TranslatedArgs->hasArg(OutputOpts[i]))
203 TranslatedArgs->eraseArg(OutputOpts[i]);
204 }
Chad Rosier2b819102011-08-02 17:58:04 +0000205 TranslatedArgs->ClaimAllArgs();
206
207 // Redirect stdout/stderr to /dev/null.
Rafael Espindola57a3bbf2013-06-13 20:08:52 +0000208 Redirects = new const StringRef*[3]();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700209 Redirects[0] = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -0800210 Redirects[1] = new StringRef();
211 Redirects[2] = new StringRef();
Chad Rosier2b819102011-08-02 17:58:04 +0000212}
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000213
Dmitri Gribenkoc4a77902012-11-15 14:28:07 +0000214StringRef Compilation::getSysRoot() const {
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000215 return getDriver().SysRoot;
216}
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700217
218void Compilation::Redirect(const StringRef** Redirects) {
219 this->Redirects = Redirects;
220}