blob: 2657faa0d3a786e198443c565e62f2c2e3913a1d [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),
28 TranslatedArgs(_TranslatedArgs) {
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;
Daniel Dunbar586dc232009-03-16 06:42:30 +000046}
47
Daniel Dunbar49540182009-09-09 18:36:01 +000048const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
49 const char *BoundArch) {
Daniel Dunbar586dc232009-03-16 06:42:30 +000050 if (!TC)
51 TC = &DefaultToolChain;
52
Daniel Dunbar49540182009-09-09 18:36:01 +000053 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar279c1db2010-06-11 22:00:26 +000054 if (!Entry) {
55 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
56 if (!Entry)
57 Entry = TranslatedArgs;
58 }
Daniel Dunbar586dc232009-03-16 06:42:30 +000059
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000060 return *Entry;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000061}
62
Mike Stump1eb44332009-09-09 15:08:12 +000063void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J,
Daniel Dunbar49b98e72009-03-18 22:44:24 +000064 const char *Terminator, bool Quote) const {
65 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000066 OS << " \"" << C->getExecutable() << '"';
67 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar49b98e72009-03-18 22:44:24 +000068 ie = C->getArguments().end(); it != ie; ++it) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000069 OS << ' ';
70 if (!Quote) {
71 OS << *it;
72 continue;
73 }
74
75 // Quote the argument and escape shell special characters; this isn't
76 // really complete but is good enough.
77 OS << '"';
78 for (const char *s = *it; *s; ++s) {
79 if (*s == '"' || *s == '\\' || *s == '$')
80 OS << '\\';
81 OS << *s;
82 }
83 OS << '"';
Daniel Dunbar49b98e72009-03-18 22:44:24 +000084 }
Daniel Dunbar24b55602009-03-18 06:49:39 +000085 OS << Terminator;
Daniel Dunbar24b55602009-03-18 06:49:39 +000086 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +000087 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +000088 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000089 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000090 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000091 }
92}
93
Mike Stump1eb44332009-09-09 15:08:12 +000094bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +000095 bool IssueErrors) const {
96 bool Success = true;
97
Mike Stump1eb44332009-09-09 15:08:12 +000098 for (ArgStringList::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +000099 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000100
Daniel Dunbare530ad42009-03-18 22:16:03 +0000101 llvm::sys::Path P(*it);
102 std::string Error;
103
Daniel Dunbar8ac127a2011-04-25 20:43:05 +0000104 // Don't try to remove files which we don't have write access to (but may be
105 // able to remove). Underlying tools may have intentionally not overwritten
106 // them.
107 if (!P.canWrite())
108 continue;
109
Daniel Dunbare530ad42009-03-18 22:16:03 +0000110 if (P.eraseFromDisk(false, &Error)) {
Dan Gohman978e3a22010-10-29 23:26:14 +0000111 // Failure is only failure if the file exists and is "regular". There is
112 // a race condition here due to the limited interface of
113 // llvm::sys::Path, we want to know if the removal gave ENOENT.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000114
115 // FIXME: Grumble, P.exists() is broken. PR3837.
116 struct stat buf;
Benjamin Kramerd99d0e82010-10-30 08:28:42 +0000117 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
Dan Gohman978e3a22010-10-29 23:26:14 +0000118 (errno != ENOENT)) {
Daniel Dunbare530ad42009-03-18 22:16:03 +0000119 if (IssueErrors)
120 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
121 << Error;
122 Success = false;
123 }
124 }
125 }
126
127 return Success;
128}
129
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000130int Compilation::ExecuteCommand(const Command &C,
131 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000132 llvm::sys::Path Prog(C.getExecutable());
133 const char **Argv = new const char*[C.getArguments().size() + 2];
134 Argv[0] = C.getExecutable();
135 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
136 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000138 if (getDriver().CCCEcho || getDriver().CCPrintOptions ||
139 getArgs().hasArg(options::OPT_v)) {
140 llvm::raw_ostream *OS = &llvm::errs();
141
142 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
143 // output stream.
144 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
145 std::string Error;
146 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
147 Error,
148 llvm::raw_fd_ostream::F_Append);
149 if (!Error.empty()) {
150 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
151 << Error;
152 FailingCommand = &C;
153 delete OS;
154 return 1;
155 }
156 }
157
158 if (getDriver().CCPrintOptions)
159 *OS << "[Logging clang options]";
160
161 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
162
163 if (OS != &llvm::errs())
164 delete OS;
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000167 std::string Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000168 int Res =
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000169 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
170 /*env*/0, /*redirects*/0,
171 /*secondsToWait*/0, /*memoryLimit*/0,
172 &Error);
173 if (!Error.empty()) {
174 assert(Res && "Error string set with 0 result code!");
175 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000178 if (Res)
179 FailingCommand = &C;
180
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000181 delete[] Argv;
182 return Res;
183}
184
Mike Stump1eb44332009-09-09 15:08:12 +0000185int Compilation::ExecuteJob(const Job &J,
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000186 const Command *&FailingCommand) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000187 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000188 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000189 } else {
190 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000191 for (JobList::const_iterator
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000192 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000193 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000194 return Res;
195 return 0;
196 }
197}