blob: 282e9fe82e4136b80e86b478d85da457d1a9d34a [file] [log] [blame]
Daniel Dunbar3ede8d02009-03-02 19:59:07 +00001//===--- Compilation.cpp - Compilation Task Implementation --------------*-===//
2//
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"
Daniel Dunbar49b98e72009-03-18 22:44:24 +000020#include "llvm/System/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 Dunbar49b98e72009-03-18 22:44:24 +000086 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Mike Stump1eb44332009-09-09 15:08:12 +000087 for (PipedJob::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000088 it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000089 PrintJob(OS, **it, (it + 1 != PJ->end()) ? " |\n" : "\n", Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000090 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +000091 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +000092 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000093 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000094 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000095 }
96}
97
Mike Stump1eb44332009-09-09 15:08:12 +000098bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +000099 bool IssueErrors) const {
100 bool Success = true;
101
Mike Stump1eb44332009-09-09 15:08:12 +0000102 for (ArgStringList::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +0000103 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000104
Daniel Dunbare530ad42009-03-18 22:16:03 +0000105 llvm::sys::Path P(*it);
106 std::string Error;
107
Edward O'Callaghan98873ec2009-11-25 06:33:27 +0000108 if (!P.isRegularFile()) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000109 // If we have a special file in our list, i.e. /dev/null
110 // then don't call eraseFromDisk() and just continue.
111 continue;
112 }
113
Daniel Dunbare530ad42009-03-18 22:16:03 +0000114 if (P.eraseFromDisk(false, &Error)) {
115 // Failure is only failure if the file doesn't exist. There is a
116 // race condition here due to the limited interface of
117 // llvm::sys::Path, we want to know if the removal gave E_NOENT.
118
119 // FIXME: Grumble, P.exists() is broken. PR3837.
120 struct stat buf;
Mike Stump1eb44332009-09-09 15:08:12 +0000121 if (::stat(P.c_str(), &buf) == 0
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000122 || errno != ENOENT) {
Daniel Dunbare530ad42009-03-18 22:16:03 +0000123 if (IssueErrors)
124 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
125 << Error;
126 Success = false;
127 }
128 }
129 }
130
131 return Success;
132}
133
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000134int Compilation::ExecuteCommand(const Command &C,
135 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000136 llvm::sys::Path Prog(C.getExecutable());
137 const char **Argv = new const char*[C.getArguments().size() + 2];
138 Argv[0] = C.getExecutable();
139 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
140 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000142 if (getDriver().CCCEcho || getDriver().CCPrintOptions ||
143 getArgs().hasArg(options::OPT_v)) {
144 llvm::raw_ostream *OS = &llvm::errs();
145
146 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
147 // output stream.
148 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
149 std::string Error;
150 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
151 Error,
152 llvm::raw_fd_ostream::F_Append);
153 if (!Error.empty()) {
154 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
155 << Error;
156 FailingCommand = &C;
157 delete OS;
158 return 1;
159 }
160 }
161
162 if (getDriver().CCPrintOptions)
163 *OS << "[Logging clang options]";
164
165 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
166
167 if (OS != &llvm::errs())
168 delete OS;
169 }
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000171 std::string Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000172 int Res =
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000173 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
174 /*env*/0, /*redirects*/0,
175 /*secondsToWait*/0, /*memoryLimit*/0,
176 &Error);
177 if (!Error.empty()) {
178 assert(Res && "Error string set with 0 result code!");
179 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000182 if (Res)
183 FailingCommand = &C;
184
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000185 delete[] Argv;
186 return Res;
187}
188
Mike Stump1eb44332009-09-09 15:08:12 +0000189int Compilation::ExecuteJob(const Job &J,
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000190 const Command *&FailingCommand) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000191 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000192 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000193 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000194 // Piped commands with a single job are easy.
195 if (PJ->size() == 1)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000196 return ExecuteCommand(**PJ->begin(), FailingCommand);
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000198 FailingCommand = *PJ->begin();
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000199 getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
200 return 1;
201 } else {
202 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000203 for (JobList::const_iterator
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000204 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000205 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000206 return Res;
207 return 0;
208 }
209}