blob: b819cda89c09bcd62227764a760063e85b32da51 [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 Dunbardf35d7f2009-07-01 20:30:52 +000025Compilation::Compilation(const Driver &D,
26 const ToolChain &_DefaultToolChain,
Mike Stump1eb44332009-09-09 15:08:12 +000027 InputArgList *_Args)
Daniel Dunbare530ad42009-03-18 22:16:03 +000028 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000029}
30
Mike Stump1eb44332009-09-09 15:08:12 +000031Compilation::~Compilation() {
Daniel Dunbar586dc232009-03-16 06:42:30 +000032 delete Args;
Mike Stump1eb44332009-09-09 15:08:12 +000033
Daniel Dunbar586dc232009-03-16 06:42:30 +000034 // Free any derived arg lists.
Daniel Dunbar49540182009-09-09 18:36:01 +000035 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
36 DerivedArgList*>::iterator it = TCArgs.begin(),
37 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbarf3cad362009-03-25 04:13:45 +000038 delete it->second;
Daniel Dunbar21549232009-03-18 02:55:38 +000039
40 // Free the actions, if built.
Mike Stump1eb44332009-09-09 15:08:12 +000041 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbar21549232009-03-18 02:55:38 +000042 it != ie; ++it)
43 delete *it;
Daniel Dunbar586dc232009-03-16 06:42:30 +000044}
45
Daniel Dunbar49540182009-09-09 18:36:01 +000046const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
47 const char *BoundArch) {
Daniel Dunbar586dc232009-03-16 06:42:30 +000048 if (!TC)
49 TC = &DefaultToolChain;
50
Daniel Dunbar49540182009-09-09 18:36:01 +000051 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000052 if (!Entry)
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +000053 Entry = TC->TranslateArgs(*Args, BoundArch);
Daniel Dunbar586dc232009-03-16 06:42:30 +000054
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000055 return *Entry;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000056}
57
Mike Stump1eb44332009-09-09 15:08:12 +000058void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J,
Daniel Dunbar49b98e72009-03-18 22:44:24 +000059 const char *Terminator, bool Quote) const {
60 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000061 OS << " \"" << C->getExecutable() << '"';
62 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar49b98e72009-03-18 22:44:24 +000063 ie = C->getArguments().end(); it != ie; ++it) {
64 if (Quote)
65 OS << " \"" << *it << '"';
66 else
67 OS << ' ' << *it;
68 }
Daniel Dunbar24b55602009-03-18 06:49:39 +000069 OS << Terminator;
Daniel Dunbar49b98e72009-03-18 22:44:24 +000070 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Mike Stump1eb44332009-09-09 15:08:12 +000071 for (PipedJob::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000072 it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000073 PrintJob(OS, **it, (it + 1 != PJ->end()) ? " |\n" : "\n", Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000074 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +000075 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +000076 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000077 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000078 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000079 }
80}
81
Mike Stump1eb44332009-09-09 15:08:12 +000082bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +000083 bool IssueErrors) const {
84 bool Success = true;
85
Mike Stump1eb44332009-09-09 15:08:12 +000086 for (ArgStringList::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +000087 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +000088
Daniel Dunbare530ad42009-03-18 22:16:03 +000089 llvm::sys::Path P(*it);
90 std::string Error;
91
Edward O'Callaghan98873ec2009-11-25 06:33:27 +000092 if (!P.isRegularFile()) {
Edward O'Callaghan56eec2b2009-11-24 15:23:21 +000093 // If we have a special file in our list, i.e. /dev/null
94 // then don't call eraseFromDisk() and just continue.
95 continue;
96 }
97
Daniel Dunbare530ad42009-03-18 22:16:03 +000098 if (P.eraseFromDisk(false, &Error)) {
99 // Failure is only failure if the file doesn't exist. There is a
100 // race condition here due to the limited interface of
101 // llvm::sys::Path, we want to know if the removal gave E_NOENT.
102
103 // FIXME: Grumble, P.exists() is broken. PR3837.
104 struct stat buf;
Mike Stump1eb44332009-09-09 15:08:12 +0000105 if (::stat(P.c_str(), &buf) == 0
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000106 || errno != ENOENT) {
Daniel Dunbare530ad42009-03-18 22:16:03 +0000107 if (IssueErrors)
108 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
109 << Error;
110 Success = false;
111 }
112 }
113 }
114
115 return Success;
116}
117
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000118int Compilation::ExecuteCommand(const Command &C,
119 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000120 llvm::sys::Path Prog(C.getExecutable());
121 const char **Argv = new const char*[C.getArguments().size() + 2];
122 Argv[0] = C.getExecutable();
123 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
124 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000126 if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v))
127 PrintJob(llvm::errs(), C, "\n", false);
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000129 std::string Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000130 int Res =
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000131 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
132 /*env*/0, /*redirects*/0,
133 /*secondsToWait*/0, /*memoryLimit*/0,
134 &Error);
135 if (!Error.empty()) {
136 assert(Res && "Error string set with 0 result code!");
137 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000140 if (Res)
141 FailingCommand = &C;
142
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000143 delete[] Argv;
144 return Res;
145}
146
Mike Stump1eb44332009-09-09 15:08:12 +0000147int Compilation::ExecuteJob(const Job &J,
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000148 const Command *&FailingCommand) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000149 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000150 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000151 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000152 // Piped commands with a single job are easy.
153 if (PJ->size() == 1)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000154 return ExecuteCommand(**PJ->begin(), FailingCommand);
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000156 FailingCommand = *PJ->begin();
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000157 getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
158 return 1;
159 } else {
160 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000161 for (JobList::const_iterator
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000162 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000163 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000164 return Res;
165 return 0;
166 }
167}