blob: 0bd573856ea1a0ca53da75b7daf61987993cb743 [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 Dunbare530ad42009-03-18 22:16:03 +000019#include <sys/stat.h>
20#include <errno.h>
Daniel Dunbar1b3bb6e2009-03-04 20:49:20 +000021using namespace clang::driver;
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000022
Daniel Dunbare530ad42009-03-18 22:16:03 +000023Compilation::Compilation(Driver &D,
24 ToolChain &_DefaultToolChain,
Daniel Dunbar586dc232009-03-16 06:42:30 +000025 ArgList *_Args)
Daniel Dunbare530ad42009-03-18 22:16:03 +000026 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) {
Daniel Dunbar3ede8d02009-03-02 19:59:07 +000027}
28
Daniel Dunbar586dc232009-03-16 06:42:30 +000029Compilation::~Compilation() {
30 delete Args;
31
32 // Free any derived arg lists.
33 for (llvm::DenseMap<const ToolChain*, ArgList*>::iterator
34 it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it) {
35 ArgList *A = it->second;
36 if (A != Args)
37 delete Args;
38 }
Daniel Dunbar21549232009-03-18 02:55:38 +000039
40 // Free the actions, if built.
41 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
42 it != ie; ++it)
43 delete *it;
Daniel Dunbar586dc232009-03-16 06:42:30 +000044}
45
46const ArgList &Compilation::getArgsForToolChain(const ToolChain *TC) {
47 if (!TC)
48 TC = &DefaultToolChain;
49
Daniel Dunbaraa3e0d22009-03-18 05:58:45 +000050 ArgList *&Entry = TCArgs[TC];
51 if (!Entry)
52 Entry = TC->TranslateArgs(*Args);
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
Daniel Dunbar24b55602009-03-18 06:49:39 +000057void Compilation::PrintJob(llvm::raw_ostream &OS, const Job *J,
58 const char *Terminator) const {
59 if (const Command *C = dyn_cast<Command>(J)) {
60 OS << " \"" << C->getExecutable() << '"';
61 for (ArgStringList::const_iterator it = C->getArguments().begin(),
62 ie = C->getArguments().end(); it != ie; ++it)
63 OS << " \"" << *it << '"';
64 OS << Terminator;
65 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(J)) {
66 for (PipedJob::const_iterator
67 it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
68 PrintJob(OS, *it, (it + 1 != PJ->end()) ? " |\n" : "\n");
69 } else {
70 const JobList *Jobs = cast<JobList>(J);
71 for (JobList::const_iterator
72 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
73 PrintJob(OS, *it, Terminator);
74 }
75}
76
Daniel Dunbare530ad42009-03-18 22:16:03 +000077bool Compilation::CleanupFileList(const ArgStringList &Files,
78 bool IssueErrors) const {
79 bool Success = true;
80
81 for (ArgStringList::const_iterator
82 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
83 llvm::sys::Path P(*it);
84 std::string Error;
85
86 if (P.eraseFromDisk(false, &Error)) {
87 // Failure is only failure if the file doesn't exist. There is a
88 // race condition here due to the limited interface of
89 // llvm::sys::Path, we want to know if the removal gave E_NOENT.
90
91 // FIXME: Grumble, P.exists() is broken. PR3837.
92 struct stat buf;
93 if (::stat(P.c_str(), &buf) || errno != ENOENT) {
94 if (IssueErrors)
95 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
96 << Error;
97 Success = false;
98 }
99 }
100 }
101
102 return Success;
103}
104
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000105int Compilation::Execute() const {
Daniel Dunbar24b55602009-03-18 06:49:39 +0000106 // Just print if -### was present.
107 if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
108 PrintJob(llvm::errs(), &Jobs, "\n");
109 return 0;
110 }
Daniel Dunbare530ad42009-03-18 22:16:03 +0000111
112 // FIXME: Execute.
113
114 int Res = 0;
Daniel Dunbar24b55602009-03-18 06:49:39 +0000115
Daniel Dunbare530ad42009-03-18 22:16:03 +0000116 // Remove temp files.
117 CleanupFileList(TempFiles);
118
119 // If the compilation failed, remove result files as well.
120 if (Res != 0)
121 CleanupFileList(ResultFiles, true);
122
Daniel Dunbar3ede8d02009-03-02 19:59:07 +0000123 return 0;
124}