blob: 2165bb74018a650451c7adb4274c7a0032fe3582 [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 Dunbare530ad42009-03-18 22:16:03 +000024Compilation::Compilation(Driver &D,
25 ToolChain &_DefaultToolChain,
Daniel Dunbar586dc232009-03-16 06:42:30 +000026 ArgList *_Args)
Daniel Dunbare530ad42009-03-18 22:16:03 +000027 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000028}
29
Daniel Dunbar586dc232009-03-16 06:42:30 +000030Compilation::~Compilation() {
31 delete Args;
32
33 // Free any derived arg lists.
34 for (llvm::DenseMap<const ToolChain*, ArgList*>::iterator
35 it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it) {
36 ArgList *A = it->second;
37 if (A != Args)
38 delete Args;
39 }
Daniel Dunbar21549232009-03-18 02:55:38 +000040
41 // Free the actions, if built.
42 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
43 it != ie; ++it)
44 delete *it;
Daniel Dunbar586dc232009-03-16 06:42:30 +000045}
46
47const ArgList &Compilation::getArgsForToolChain(const ToolChain *TC) {
48 if (!TC)
49 TC = &DefaultToolChain;
50
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000051 ArgList *&Entry = TCArgs[TC];
52 if (!Entry)
53 Entry = TC->TranslateArgs(*Args);
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
Daniel Dunbar49b98e72009-03-18 22:44:24 +000058void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J,
59 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)) {
Daniel Dunbar24b55602009-03-18 06:49:39 +000071 for (PipedJob::const_iterator
72 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);
Daniel Dunbar24b55602009-03-18 06:49:39 +000076 for (JobList::const_iterator
77 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
Daniel Dunbare530ad42009-03-18 22:16:03 +000082bool Compilation::CleanupFileList(const ArgStringList &Files,
83 bool IssueErrors) const {
84 bool Success = true;
85
86 for (ArgStringList::const_iterator
87 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
88 llvm::sys::Path P(*it);
89 std::string Error;
90
91 if (P.eraseFromDisk(false, &Error)) {
92 // Failure is only failure if the file doesn't exist. There is a
93 // race condition here due to the limited interface of
94 // llvm::sys::Path, we want to know if the removal gave E_NOENT.
95
96 // FIXME: Grumble, P.exists() is broken. PR3837.
97 struct stat buf;
Daniel Dunbar49b98e72009-03-18 22:44:24 +000098 if (::stat(P.c_str(), &buf) == 0
99 || errno != ENOENT) {
Daniel Dunbare530ad42009-03-18 22:16:03 +0000100 if (IssueErrors)
101 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
102 << Error;
103 Success = false;
104 }
105 }
106 }
107
108 return Success;
109}
110
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000111int Compilation::ExecuteCommand(const Command &C) const {
112 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;
117
118 if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v))
119 PrintJob(llvm::errs(), C, "\n", false);
120
121 std::string Error;
122 int Res =
123 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 }
131
132 delete[] Argv;
133 return Res;
134}
135
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000136int Compilation::ExecuteJob(const Job &J) const {
137 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000138 return ExecuteCommand(*C);
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000139 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
Daniel Dunbarceafbc82009-03-19 08:01:45 +0000140 // Piped commands with a single job are easy.
141 if (PJ->size() == 1)
142 return ExecuteCommand(**PJ->begin());
143
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000144 getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
145 return 1;
146 } else {
147 const JobList *Jobs = cast<JobList>(&J);
148 for (JobList::const_iterator
149 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
150 if (int Res = ExecuteJob(**it))
151 return Res;
152 return 0;
153 }
154}
155
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000156int Compilation::Execute() const {
Daniel Dunbar24b55602009-03-18 06:49:39 +0000157 // Just print if -### was present.
158 if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000159 PrintJob(llvm::errs(), Jobs, "\n", true);
Daniel Dunbar24b55602009-03-18 06:49:39 +0000160 return 0;
161 }
Daniel Dunbare530ad42009-03-18 22:16:03 +0000162
Daniel Dunbaraf96def2009-03-21 00:40:53 +0000163 // If there were errors building the compilation, quit now.
164 if (getDriver().getDiags().getNumErrors())
165 return 1;
166
Daniel Dunbar49b98e72009-03-18 22:44:24 +0000167 int Res = ExecuteJob(Jobs);
Daniel Dunbar24b55602009-03-18 06:49:39 +0000168
Daniel Dunbare530ad42009-03-18 22:16:03 +0000169 // Remove temp files.
170 CleanupFileList(TempFiles);
171
172 // If the compilation failed, remove result files as well.
Daniel Dunbar9e3a2242009-03-24 17:49:01 +0000173 if (Res != 0 && !getArgs().hasArg(options::OPT_save_temps))
Daniel Dunbare530ad42009-03-18 22:16:03 +0000174 CleanupFileList(ResultFiles, true);
175
Daniel Dunbar56b10ab2009-03-19 08:03:45 +0000176 return Res;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000177}