blob: c12f5aa88195a5038840752b970a919842285f10 [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 Dunbar586dc232009-03-16 06:42:30 +000016#include "clang/Driver/ToolChain.h"
17
Daniel Dunbar24b55602009-03-18 06:49:39 +000018#include "llvm/Support/raw_ostream.h"
Daniel Dunbar49b98e72009-03-18 22:44:24 +000019#include "llvm/System/Program.h"
Daniel Dunbare530ad42009-03-18 22:16:03 +000020#include <sys/stat.h>
21#include <errno.h>
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000022using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000023
Daniel Dunbardf35d7f2009-07-01 20:30:52 +000024Compilation::Compilation(const Driver &D,
25 const ToolChain &_DefaultToolChain,
Mike Stump1eb44332009-09-09 15:08:12 +000026 InputArgList *_Args)
Daniel Dunbare530ad42009-03-18 22:16:03 +000027 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000028}
29
Mike Stump1eb44332009-09-09 15:08:12 +000030Compilation::~Compilation() {
Daniel Dunbar586dc232009-03-16 06:42:30 +000031 delete Args;
Mike Stump1eb44332009-09-09 15:08:12 +000032
Daniel Dunbar586dc232009-03-16 06:42:30 +000033 // Free any derived arg lists.
Daniel Dunbar49540182009-09-09 18:36:01 +000034 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
35 DerivedArgList*>::iterator it = TCArgs.begin(),
36 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbarf3cad362009-03-25 04:13:45 +000037 delete it->second;
Daniel Dunbar21549232009-03-18 02:55:38 +000038
39 // Free the actions, if built.
Mike Stump1eb44332009-09-09 15:08:12 +000040 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbar21549232009-03-18 02:55:38 +000041 it != ie; ++it)
42 delete *it;
Daniel Dunbar586dc232009-03-16 06:42:30 +000043}
44
Daniel Dunbar49540182009-09-09 18:36:01 +000045const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
46 const char *BoundArch) {
Daniel Dunbar586dc232009-03-16 06:42:30 +000047 if (!TC)
48 TC = &DefaultToolChain;
49
Daniel Dunbar49540182009-09-09 18:36:01 +000050 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000051 if (!Entry)
Daniel Dunbar0dcb9a32009-09-09 18:36:12 +000052 Entry = TC->TranslateArgs(*Args, BoundArch);
Daniel Dunbar586dc232009-03-16 06:42:30 +000053
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000054 return *Entry;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000055}
56
Mike Stump1eb44332009-09-09 15:08:12 +000057void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J,
Daniel Dunbar49b98e72009-03-18 22:44:24 +000058 const char *Terminator, bool Quote) const {
59 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000060 OS << " \"" << C->getExecutable() << '"';
61 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar49b98e72009-03-18 22:44:24 +000062 ie = C->getArguments().end(); it != ie; ++it) {
63 if (Quote)
64 OS << " \"" << *it << '"';
65 else
66 OS << ' ' << *it;
67 }
Daniel Dunbar24b55602009-03-18 06:49:39 +000068 OS << Terminator;
Daniel Dunbar49b98e72009-03-18 22:44:24 +000069 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Mike Stump1eb44332009-09-09 15:08:12 +000070 for (PipedJob::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000071 it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000072 PrintJob(OS, **it, (it + 1 != PJ->end()) ? " |\n" : "\n", Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000073 } else {
Daniel Dunbar49b98e72009-03-18 22:44:24 +000074 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +000075 for (JobList::const_iterator
Daniel Dunbar24b55602009-03-18 06:49:39 +000076 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar49b98e72009-03-18 22:44:24 +000077 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar24b55602009-03-18 06:49:39 +000078 }
79}
80
Mike Stump1eb44332009-09-09 15:08:12 +000081bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbare530ad42009-03-18 22:16:03 +000082 bool IssueErrors) const {
83 bool Success = true;
84
Mike Stump1eb44332009-09-09 15:08:12 +000085 for (ArgStringList::const_iterator
Daniel Dunbare530ad42009-03-18 22:16:03 +000086 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
87 llvm::sys::Path P(*it);
88 std::string Error;
89
90 if (P.eraseFromDisk(false, &Error)) {
91 // Failure is only failure if the file doesn't exist. There is a
92 // race condition here due to the limited interface of
93 // llvm::sys::Path, we want to know if the removal gave E_NOENT.
94
95 // FIXME: Grumble, P.exists() is broken. PR3837.
96 struct stat buf;
Mike Stump1eb44332009-09-09 15:08:12 +000097 if (::stat(P.c_str(), &buf) == 0
Daniel Dunbar49b98e72009-03-18 22:44:24 +000098 || errno != ENOENT) {
Daniel Dunbare530ad42009-03-18 22:16:03 +000099 if (IssueErrors)
100 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
101 << Error;
102 Success = false;
103 }
104 }
105 }
106
107 return Success;
108}
109
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000110int Compilation::ExecuteCommand(const Command &C,
111 const Command *&FailingCommand) const {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000112 llvm::sys::Path Prog(C.getExecutable());
113 const char **Argv = new const char*[C.getArguments().size() + 2];
114 Argv[0] = C.getExecutable();
115 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
116 Argv[C.getArguments().size() + 1] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000118 if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v))
119 PrintJob(llvm::errs(), C, "\n", false);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000121 std::string Error;
Mike Stump1eb44332009-09-09 15:08:12 +0000122 int Res =
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000123 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
124 /*env*/0, /*redirects*/0,
125 /*secondsToWait*/0, /*memoryLimit*/0,
126 &Error);
127 if (!Error.empty()) {
128 assert(Res && "Error string set with 0 result code!");
129 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
130 }
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000132 if (Res)
133 FailingCommand = &C;
134
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000135 delete[] Argv;
136 return Res;
137}
138
Mike Stump1eb44332009-09-09 15:08:12 +0000139int Compilation::ExecuteJob(const Job &J,
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000140 const Command *&FailingCommand) const {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000141 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000142 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000143 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000144 // Piped commands with a single job are easy.
145 if (PJ->size() == 1)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000146 return ExecuteCommand(**PJ->begin(), FailingCommand);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000148 FailingCommand = *PJ->begin();
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000149 getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
150 return 1;
151 } else {
152 const JobList *Jobs = cast<JobList>(&J);
Mike Stump1eb44332009-09-09 15:08:12 +0000153 for (JobList::const_iterator
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000154 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar31c11eb2009-07-01 19:14:39 +0000155 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000156 return Res;
157 return 0;
158 }
159}