blob: b0e46ff5aeeb9cdb644815bc93a24b699d7deeb8 [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
Daniel Dunbar24b55602009-03-18 06:49:39 +000019#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000020#include "llvm/Support/Program.h"
Daniel Dunbare530ad42009-03-18 22:16:03 +000021#include <sys/stat.h>
22#include <errno.h>
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000023using namespace clang::driver;
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)
27 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
Chad Rosier2da13b12011-07-20 20:26:32 +000028 TranslatedArgs(_TranslatedArgs), Redirects(0) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000029}
30
Mike Stump1eb44332009-09-09 15:08:12 +000031Compilation::~Compilation() {
Daniel Dunbar279c1db2010-06-11 22:00:26 +000032 delete TranslatedArgs;
Daniel Dunbar586dc232009-03-16 06:42:30 +000033 delete Args;
Mike Stump1eb44332009-09-09 15:08:12 +000034
Daniel Dunbar586dc232009-03-16 06:42:30 +000035 // Free any derived arg lists.
Daniel Dunbar49540182009-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 Dunbar279c1db2010-06-11 22:00:26 +000039 if (it->second != TranslatedArgs)
40 delete it->second;
Daniel Dunbar21549232009-03-18 02:55:38 +000041
42 // Free the actions, if built.
Mike Stump1eb44332009-09-09 15:08:12 +000043 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbar21549232009-03-18 02:55:38 +000044 it != ie; ++it)
45 delete *it;
Chad Rosier2da13b12011-07-20 20:26:32 +000046
47 // Free redirections of stdout/stderr.
48 if (Redirects) {
49 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
Mike Stump1eb44332009-09-09 15:08:12 +000070void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J,
Daniel Dunbar49b98e72009-03-18 22:44:24 +000071 const char *Terminator, bool Quote) const {
72 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000073 OS << " \"" << C->getExecutable() << '"';
74 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar49b98e72009-03-18 22:44:24 +000075 ie = C->getArguments().end(); it != ie; ++it) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000076 OS << ' ';
77 if (!Quote) {
78 OS << *it;
79 continue;
80 }
81
82 // Quote the argument and escape shell special characters; this isn't
83 // really complete but is good enough.
84 OS << '"';
85 for (const char *s = *it; *s; ++s) {
86 if (*s == '"' || *s == '\\' || *s == '$')
87 OS << '\\';
88 OS << *s;
89 }
90 OS << '"';
Daniel Dunbar49b98e72009-03-18 22:44:24 +000091 }
Daniel Dunbar24b55602009-03-18 06:49:39 +000092 OS << Terminator;
Daniel Dunbar24b55602009-03-18 06:49:39 +000093 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +000094 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +000095 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000096 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000097 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000098 }
99}
100
Mike Stump1eb44332009-09-09 15:08:12 +0000101bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +0000102 bool IssueErrors) const {
103 bool Success = true;
104
Mike Stump1eb44332009-09-09 15:08:12 +0000105 for (ArgStringList::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +0000106 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000107
Daniel Dunbare530ad42009-03-18 22:16:03 +0000108 llvm::sys::Path P(*it);
109 std::string Error;
110
Daniel Dunbar8ac127a2011-04-25 20:43:05 +0000111 // Don't try to remove files which we don't have write access to (but may be
112 // able to remove). Underlying tools may have intentionally not overwritten
113 // them.
114 if (!P.canWrite())
115 continue;
116
Daniel Dunbare530ad42009-03-18 22:16:03 +0000117 if (P.eraseFromDisk(false, &Error)) {
Dan Gohman978e3a22010-10-29 23:26:14 +0000118 // Failure is only failure if the file exists and is "regular". There is
119 // a race condition here due to the limited interface of
120 // llvm::sys::Path, we want to know if the removal gave ENOENT.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000121
122 // FIXME: Grumble, P.exists() is broken. PR3837.
123 struct stat buf;
Benjamin Kramerd99d0e82010-10-30 08:28:42 +0000124 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
Dan Gohman978e3a22010-10-29 23:26:14 +0000125 (errno != ENOENT)) {
Daniel Dunbare530ad42009-03-18 22:16:03 +0000126 if (IssueErrors)
127 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
128 << Error;
129 Success = false;
130 }
131 }
132 }
133
134 return Success;
135}
136
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000137int Compilation::ExecuteCommand(const Command &C,
138 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000139 llvm::sys::Path Prog(C.getExecutable());
140 const char **Argv = new const char*[C.getArguments().size() + 2];
141 Argv[0] = C.getExecutable();
142 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
143 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Chad Rosier2da13b12011-07-20 20:26:32 +0000145 if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
146 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000147 llvm::raw_ostream *OS = &llvm::errs();
148
149 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
150 // output stream.
151 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
152 std::string Error;
153 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
154 Error,
155 llvm::raw_fd_ostream::F_Append);
156 if (!Error.empty()) {
157 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
158 << Error;
159 FailingCommand = &C;
160 delete OS;
161 return 1;
162 }
163 }
164
165 if (getDriver().CCPrintOptions)
166 *OS << "[Logging clang options]";
167
168 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
169
170 if (OS != &llvm::errs())
171 delete OS;
172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000174 std::string Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000175 int Res =
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000176 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
Chad Rosier2da13b12011-07-20 20:26:32 +0000177 /*env*/0, Redirects,
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000178 /*secondsToWait*/0, /*memoryLimit*/0,
179 &Error);
180 if (!Error.empty()) {
181 assert(Res && "Error string set with 0 result code!");
182 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000185 if (Res)
186 FailingCommand = &C;
187
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000188 delete[] Argv;
189 return Res;
190}
191
Mike Stump1eb44332009-09-09 15:08:12 +0000192int Compilation::ExecuteJob(const Job &J,
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000193 const Command *&FailingCommand) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000194 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000195 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000196 } else {
197 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000198 for (JobList::const_iterator
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000199 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000200 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000201 return Res;
202 return 0;
203 }
204}
Chad Rosier2da13b12011-07-20 20:26:32 +0000205
206void Compilation::initCompilationForDiagnostics(void) {
207 // Free actions and jobs, if built.
208 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
209 it != ie; ++it)
210 delete *it;
211 Actions.clear();
212 Jobs.clear();
213
214 // Clear temporary and 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.
220 if (TranslatedArgs->hasArg(options::OPT_o))
221 TranslatedArgs->eraseArg(options::OPT_o);
222 TranslatedArgs->ClaimAllArgs();
223
224 // Redirect stdout/stderr to /dev/null.
225 Redirects = new const llvm::sys::Path*[3]();
226 Redirects[1] = new const llvm::sys::Path();
227 Redirects[2] = new const llvm::sys::Path();
228}