blob: c962fca0bf9a2f472313ac55c1cb0d2153b98ca3 [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 Dunbar586dc232009-03-16 06:42:30 +000011
Daniel Dunbar21549232009-03-18 02:55:38 +000012#include "clang/Driver/Action.h"
Daniel Dunbar586dc232009-03-16 06:42:30 +000013#include "clang/Driver/ArgList.h"
Daniel Dunbare530ad42009-03-18 22:16:03 +000014#include "clang/Driver/Driver.h"
15#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000016#include "clang/Driver/Options.h"
Daniel Dunbar586dc232009-03-16 06:42:30 +000017#include "clang/Driver/ToolChain.h"
18
Chad Rosier2b819102011-08-02 17:58:04 +000019#include "llvm/ADT/STLExtras.h"
Daniel Dunbar24b55602009-03-18 06:49:39 +000020#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000021#include "llvm/Support/Program.h"
Daniel Dunbare530ad42009-03-18 22:16:03 +000022#include <sys/stat.h>
23#include <errno.h>
Francois Pichet1c3199a2011-07-23 11:36:43 +000024
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000025using namespace clang::driver;
Francois Pichet1c3199a2011-07-23 11:36:43 +000026using namespace clang;
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
Chris Lattner5f9e2722011-07-23 10:55:15 +000073void Compilation::PrintJob(raw_ostream &OS, const Job &J,
Daniel Dunbar49b98e72009-03-18 22:44:24 +000074 const char *Terminator, bool Quote) const {
75 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000076 OS << " \"" << C->getExecutable() << '"';
77 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar49b98e72009-03-18 22:44:24 +000078 ie = C->getArguments().end(); it != ie; ++it) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000079 OS << ' ';
Benjamin Kramer5f226142011-10-06 22:53:35 +000080 if (!Quote && !std::strpbrk(*it, " \"\\$")) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000081 OS << *it;
82 continue;
83 }
84
85 // Quote the argument and escape shell special characters; this isn't
86 // really complete but is good enough.
87 OS << '"';
88 for (const char *s = *it; *s; ++s) {
89 if (*s == '"' || *s == '\\' || *s == '$')
90 OS << '\\';
91 OS << *s;
92 }
93 OS << '"';
Daniel Dunbar49b98e72009-03-18 22:44:24 +000094 }
Daniel Dunbar24b55602009-03-18 06:49:39 +000095 OS << Terminator;
Daniel Dunbar24b55602009-03-18 06:49:39 +000096 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +000097 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +000098 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000099 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000100 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +0000101 }
102}
103
Mike Stump1eb44332009-09-09 15:08:12 +0000104bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +0000105 bool IssueErrors) const {
106 bool Success = true;
107
Mike Stump1eb44332009-09-09 15:08:12 +0000108 for (ArgStringList::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +0000109 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000110
Daniel Dunbare530ad42009-03-18 22:16:03 +0000111 llvm::sys::Path P(*it);
112 std::string Error;
113
Daniel Dunbar8ac127a2011-04-25 20:43:05 +0000114 // Don't try to remove files which we don't have write access to (but may be
115 // able to remove). Underlying tools may have intentionally not overwritten
116 // them.
117 if (!P.canWrite())
118 continue;
119
Daniel Dunbare530ad42009-03-18 22:16:03 +0000120 if (P.eraseFromDisk(false, &Error)) {
Dan Gohman978e3a22010-10-29 23:26:14 +0000121 // Failure is only failure if the file exists and is "regular". There is
122 // a race condition here due to the limited interface of
123 // llvm::sys::Path, we want to know if the removal gave ENOENT.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000124
125 // FIXME: Grumble, P.exists() is broken. PR3837.
126 struct stat buf;
Benjamin Kramerd99d0e82010-10-30 08:28:42 +0000127 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
Dan Gohman978e3a22010-10-29 23:26:14 +0000128 (errno != ENOENT)) {
Daniel Dunbare530ad42009-03-18 22:16:03 +0000129 if (IssueErrors)
130 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
131 << Error;
132 Success = false;
133 }
134 }
135 }
136
137 return Success;
138}
139
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000140int Compilation::ExecuteCommand(const Command &C,
141 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000142 llvm::sys::Path Prog(C.getExecutable());
143 const char **Argv = new const char*[C.getArguments().size() + 2];
144 Argv[0] = C.getExecutable();
145 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
146 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chad Rosier2b819102011-08-02 17:58:04 +0000148 if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
149 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000150 raw_ostream *OS = &llvm::errs();
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000151
152 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
153 // output stream.
154 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
155 std::string Error;
156 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
157 Error,
158 llvm::raw_fd_ostream::F_Append);
159 if (!Error.empty()) {
160 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
161 << Error;
162 FailingCommand = &C;
163 delete OS;
164 return 1;
165 }
166 }
167
168 if (getDriver().CCPrintOptions)
169 *OS << "[Logging clang options]";
170
171 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
172
173 if (OS != &llvm::errs())
174 delete OS;
175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000177 std::string Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000178 int Res =
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000179 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
Chad Rosier2b819102011-08-02 17:58:04 +0000180 /*env*/0, Redirects,
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000181 /*secondsToWait*/0, /*memoryLimit*/0,
182 &Error);
183 if (!Error.empty()) {
184 assert(Res && "Error string set with 0 result code!");
185 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000188 if (Res)
189 FailingCommand = &C;
190
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000191 delete[] Argv;
192 return Res;
193}
194
Mike Stump1eb44332009-09-09 15:08:12 +0000195int Compilation::ExecuteJob(const Job &J,
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000196 const Command *&FailingCommand) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000197 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000198 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000199 } else {
200 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000201 for (JobList::const_iterator
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000202 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000203 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000204 return Res;
205 return 0;
206 }
207}
Chad Rosier2b819102011-08-02 17:58:04 +0000208
209void Compilation::initCompilationForDiagnostics(void) {
210 // Free actions and jobs.
211 DeleteContainerPointers(Actions);
212 Jobs.clear();
213
214 // Clear temporary/results file lists.
215 TempFiles.clear();
216 ResultFiles.clear();
217
218 // Remove any user specified output. Claim any unclaimed arguments, so as
219 // to avoid emitting warnings about unused args.
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000220 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
221 options::OPT_MMD };
Chad Rosierf9e156c2012-05-03 21:25:34 +0000222 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne54db68b2011-11-06 00:40:05 +0000223 if (TranslatedArgs->hasArg(OutputOpts[i]))
224 TranslatedArgs->eraseArg(OutputOpts[i]);
225 }
Chad Rosier2b819102011-08-02 17:58:04 +0000226 TranslatedArgs->ClaimAllArgs();
227
228 // Redirect stdout/stderr to /dev/null.
229 Redirects = new const llvm::sys::Path*[3]();
230 Redirects[1] = new const llvm::sys::Path();
231 Redirects[2] = new const llvm::sys::Path();
232}
Sebastian Pop4762a2d2012-04-16 04:16:43 +0000233
234StringRef Compilation::getSysRoot(void) const {
235 return getDriver().SysRoot;
236}