blob: 47ac1756c81ee6913bb69a956c3dd10c376c98bb [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>
Francois Pichet1c3199a2011-07-23 11:36:43 +000023
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000024using namespace clang::driver;
Francois Pichet1c3199a2011-07-23 11:36:43 +000025using namespace clang;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000026
Daniel Dunbar279c1db2010-06-11 22:00:26 +000027Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
28 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
29 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
Chad Rosierd7a3ba02011-07-20 21:16:17 +000030 TranslatedArgs(_TranslatedArgs) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000031}
32
Mike Stump1eb44332009-09-09 15:08:12 +000033Compilation::~Compilation() {
Daniel Dunbar279c1db2010-06-11 22:00:26 +000034 delete TranslatedArgs;
Daniel Dunbar586dc232009-03-16 06:42:30 +000035 delete Args;
Mike Stump1eb44332009-09-09 15:08:12 +000036
Daniel Dunbar586dc232009-03-16 06:42:30 +000037 // Free any derived arg lists.
Daniel Dunbar49540182009-09-09 18:36:01 +000038 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
39 DerivedArgList*>::iterator it = TCArgs.begin(),
40 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbar279c1db2010-06-11 22:00:26 +000041 if (it->second != TranslatedArgs)
42 delete it->second;
Daniel Dunbar21549232009-03-18 02:55:38 +000043
44 // Free the actions, if built.
Mike Stump1eb44332009-09-09 15:08:12 +000045 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbar21549232009-03-18 02:55:38 +000046 it != ie; ++it)
47 delete *it;
Daniel Dunbar586dc232009-03-16 06:42:30 +000048}
49
Daniel Dunbar49540182009-09-09 18:36:01 +000050const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
51 const char *BoundArch) {
Daniel Dunbar586dc232009-03-16 06:42:30 +000052 if (!TC)
53 TC = &DefaultToolChain;
54
Daniel Dunbar49540182009-09-09 18:36:01 +000055 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar279c1db2010-06-11 22:00:26 +000056 if (!Entry) {
57 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
58 if (!Entry)
59 Entry = TranslatedArgs;
60 }
Daniel Dunbar586dc232009-03-16 06:42:30 +000061
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000062 return *Entry;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000063}
64
Chris Lattner5f9e2722011-07-23 10:55:15 +000065void Compilation::PrintJob(raw_ostream &OS, const Job &J,
Daniel Dunbar49b98e72009-03-18 22:44:24 +000066 const char *Terminator, bool Quote) const {
67 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000068 OS << " \"" << C->getExecutable() << '"';
69 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar49b98e72009-03-18 22:44:24 +000070 ie = C->getArguments().end(); it != ie; ++it) {
Daniel Dunbarb86c5fc2010-03-20 08:01:53 +000071 OS << ' ';
72 if (!Quote) {
73 OS << *it;
74 continue;
75 }
76
77 // Quote the argument and escape shell special characters; this isn't
78 // really complete but is good enough.
79 OS << '"';
80 for (const char *s = *it; *s; ++s) {
81 if (*s == '"' || *s == '\\' || *s == '$')
82 OS << '\\';
83 OS << *s;
84 }
85 OS << '"';
Daniel Dunbar49b98e72009-03-18 22:44:24 +000086 }
Daniel Dunbar24b55602009-03-18 06:49:39 +000087 OS << Terminator;
Daniel Dunbar24b55602009-03-18 06:49:39 +000088 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +000089 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +000090 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000091 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000092 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000093 }
94}
95
Mike Stump1eb44332009-09-09 15:08:12 +000096bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +000097 bool IssueErrors) const {
98 bool Success = true;
99
Mike Stump1eb44332009-09-09 15:08:12 +0000100 for (ArgStringList::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +0000101 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +0000102
Daniel Dunbare530ad42009-03-18 22:16:03 +0000103 llvm::sys::Path P(*it);
104 std::string Error;
105
Daniel Dunbar8ac127a2011-04-25 20:43:05 +0000106 // Don't try to remove files which we don't have write access to (but may be
107 // able to remove). Underlying tools may have intentionally not overwritten
108 // them.
109 if (!P.canWrite())
110 continue;
111
Daniel Dunbare530ad42009-03-18 22:16:03 +0000112 if (P.eraseFromDisk(false, &Error)) {
Dan Gohman978e3a22010-10-29 23:26:14 +0000113 // Failure is only failure if the file exists and is "regular". There is
114 // a race condition here due to the limited interface of
115 // llvm::sys::Path, we want to know if the removal gave ENOENT.
Daniel Dunbare530ad42009-03-18 22:16:03 +0000116
117 // FIXME: Grumble, P.exists() is broken. PR3837.
118 struct stat buf;
Benjamin Kramerd99d0e82010-10-30 08:28:42 +0000119 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
Dan Gohman978e3a22010-10-29 23:26:14 +0000120 (errno != ENOENT)) {
Daniel Dunbare530ad42009-03-18 22:16:03 +0000121 if (IssueErrors)
122 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
123 << Error;
124 Success = false;
125 }
126 }
127 }
128
129 return Success;
130}
131
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000132int Compilation::ExecuteCommand(const Command &C,
133 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000134 llvm::sys::Path Prog(C.getExecutable());
135 const char **Argv = new const char*[C.getArguments().size() + 2];
136 Argv[0] = C.getExecutable();
137 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
138 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Chad Rosierd7a3ba02011-07-20 21:16:17 +0000140 if (getDriver().CCCEcho || getDriver().CCPrintOptions ||
141 getArgs().hasArg(options::OPT_v)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 raw_ostream *OS = &llvm::errs();
Daniel Dunbar4c00fcd2010-03-20 08:01:59 +0000143
144 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
145 // output stream.
146 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
147 std::string Error;
148 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
149 Error,
150 llvm::raw_fd_ostream::F_Append);
151 if (!Error.empty()) {
152 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
153 << Error;
154 FailingCommand = &C;
155 delete OS;
156 return 1;
157 }
158 }
159
160 if (getDriver().CCPrintOptions)
161 *OS << "[Logging clang options]";
162
163 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
164
165 if (OS != &llvm::errs())
166 delete OS;
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000169 std::string Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000170 int Res =
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000171 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
Chad Rosierd7a3ba02011-07-20 21:16:17 +0000172 /*env*/0, /*redirects*/0,
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000173 /*secondsToWait*/0, /*memoryLimit*/0,
174 &Error);
175 if (!Error.empty()) {
176 assert(Res && "Error string set with 0 result code!");
177 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000180 if (Res)
181 FailingCommand = &C;
182
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000183 delete[] Argv;
184 return Res;
185}
186
Mike Stump1eb44332009-09-09 15:08:12 +0000187int Compilation::ExecuteJob(const Job &J,
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000188 const Command *&FailingCommand) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000189 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000190 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000191 } else {
192 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000193 for (JobList::const_iterator
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000194 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000195 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000196 return Res;
197 return 0;
198 }
199}