blob: baaba19ed72b32de5f1d0659b13b7502f2121950 [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
Chad Rosiera2dd7d02011-10-05 19:51:41 +000073static bool needsQuote(const char *s) {
74 for (const char *c = s; *c; ++c)
Chad Rosier22807862011-10-05 20:09:11 +000075 if (*c == ' ' || *c == '"' || *c == '\\' || *c == '$')
Chad Rosiera2dd7d02011-10-05 19:51:41 +000076 return true;
77 return false;
78}
79
Chris Lattner5f9e2722011-07-23 10:55:15 +000080void Compilation::PrintJob(raw_ostream &OS, const Job &J,
Daniel Dunbar49b98e72009-03-18 22:44:24 +000081 const char *Terminator, bool Quote) const {
82 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000083 OS << " \"" << C->getExecutable() << '"';
84 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar49b98e72009-03-18 22:44:24 +000085 ie = C->getArguments().end(); it != ie; ++it) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000086 OS << ' ';
Chad Rosiera2dd7d02011-10-05 19:51:41 +000087 if (!Quote && !needsQuote(*it)) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000088 OS << *it;
89 continue;
90 }
91
92 // Quote the argument and escape shell special characters; this isn't
93 // really complete but is good enough.
94 OS << '"';
95 for (const char *s = *it; *s; ++s) {
96 if (*s == '"' || *s == '\\' || *s == '$')
97 OS << '\\';
98 OS << *s;
99 }
100 OS << '"';
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000101 }
Daniel Dunbar24b55602009-03-18 06:49:39 +0000102 OS << Terminator;
Daniel Dunbar24b55602009-03-18 06:49:39 +0000103 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000104 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000105 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +0000106 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000107 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +0000108 }
109}
110
Mike Stump1eb44332009-09-09 15:08:12 +0000111bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +0000112 bool IssueErrors) const {
113 bool Success = true;
114
Mike Stump1eb44332009-09-09 15:08:12 +0000115 for (ArgStringList::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +0000116 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000117
Daniel Dunbare530ad42009-03-18 22:16:03 +0000118 llvm::sys::Path P(*it);
119 std::string Error;
120
Daniel Dunbar8ac127a2011-04-25 20:43:05 +0000121 // Don't try to remove files which we don't have write access to (but may be
122 // able to remove). Underlying tools may have intentionally not overwritten
123 // them.
124 if (!P.canWrite())
125 continue;
126
Daniel Dunbare530ad42009-03-18 22:16:03 +0000127 if (P.eraseFromDisk(false, &Error)) {
Dan Gohman978e3a22010-10-29 23:26:14 +0000128 // Failure is only failure if the file exists and is "regular". There is
129 // a race condition here due to the limited interface of
130 // llvm::sys::Path, we want to know if the removal gave ENOENT.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000131
132 // FIXME: Grumble, P.exists() is broken. PR3837.
133 struct stat buf;
Benjamin Kramerd99d0e82010-10-30 08:28:42 +0000134 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
Dan Gohman978e3a22010-10-29 23:26:14 +0000135 (errno != ENOENT)) {
Daniel Dunbare530ad42009-03-18 22:16:03 +0000136 if (IssueErrors)
137 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
138 << Error;
139 Success = false;
140 }
141 }
142 }
143
144 return Success;
145}
146
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000147int Compilation::ExecuteCommand(const Command &C,
148 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000149 llvm::sys::Path Prog(C.getExecutable());
150 const char **Argv = new const char*[C.getArguments().size() + 2];
151 Argv[0] = C.getExecutable();
152 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
153 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Chad Rosier2b819102011-08-02 17:58:04 +0000155 if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
156 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000157 raw_ostream *OS = &llvm::errs();
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000158
159 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
160 // output stream.
161 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
162 std::string Error;
163 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
164 Error,
165 llvm::raw_fd_ostream::F_Append);
166 if (!Error.empty()) {
167 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
168 << Error;
169 FailingCommand = &C;
170 delete OS;
171 return 1;
172 }
173 }
174
175 if (getDriver().CCPrintOptions)
176 *OS << "[Logging clang options]";
177
178 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
179
180 if (OS != &llvm::errs())
181 delete OS;
182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000184 std::string Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000185 int Res =
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000186 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
Chad Rosier2b819102011-08-02 17:58:04 +0000187 /*env*/0, Redirects,
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000188 /*secondsToWait*/0, /*memoryLimit*/0,
189 &Error);
190 if (!Error.empty()) {
191 assert(Res && "Error string set with 0 result code!");
192 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000195 if (Res)
196 FailingCommand = &C;
197
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000198 delete[] Argv;
199 return Res;
200}
201
Mike Stump1eb44332009-09-09 15:08:12 +0000202int Compilation::ExecuteJob(const Job &J,
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000203 const Command *&FailingCommand) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000204 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000205 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000206 } else {
207 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000208 for (JobList::const_iterator
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000209 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000210 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000211 return Res;
212 return 0;
213 }
214}
Chad Rosier2b819102011-08-02 17:58:04 +0000215
216void Compilation::initCompilationForDiagnostics(void) {
217 // Free actions and jobs.
218 DeleteContainerPointers(Actions);
219 Jobs.clear();
220
221 // Clear temporary/results file lists.
222 TempFiles.clear();
223 ResultFiles.clear();
224
225 // Remove any user specified output. Claim any unclaimed arguments, so as
226 // to avoid emitting warnings about unused args.
227 if (TranslatedArgs->hasArg(options::OPT_o))
228 TranslatedArgs->eraseArg(options::OPT_o);
229 TranslatedArgs->ClaimAllArgs();
230
231 // Redirect stdout/stderr to /dev/null.
232 Redirects = new const llvm::sys::Path*[3]();
233 Redirects[1] = new const llvm::sys::Path();
234 Redirects[2] = new const llvm::sys::Path();
235}