1f4a2713aSLionel Sambuc //===--- Compilation.cpp - Compilation Task Implementation ----------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "clang/Driver/Compilation.h"
11f4a2713aSLionel Sambuc #include "clang/Driver/Action.h"
12f4a2713aSLionel Sambuc #include "clang/Driver/Driver.h"
13f4a2713aSLionel Sambuc #include "clang/Driver/DriverDiagnostic.h"
14f4a2713aSLionel Sambuc #include "clang/Driver/Options.h"
15f4a2713aSLionel Sambuc #include "clang/Driver/ToolChain.h"
16f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
17f4a2713aSLionel Sambuc #include "llvm/Option/ArgList.h"
18f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
19f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
20f4a2713aSLionel Sambuc
21f4a2713aSLionel Sambuc using namespace clang::driver;
22f4a2713aSLionel Sambuc using namespace clang;
23f4a2713aSLionel Sambuc using namespace llvm::opt;
24f4a2713aSLionel Sambuc
Compilation(const Driver & D,const ToolChain & _DefaultToolChain,InputArgList * _Args,DerivedArgList * _TranslatedArgs)25f4a2713aSLionel Sambuc Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
26f4a2713aSLionel Sambuc InputArgList *_Args, DerivedArgList *_TranslatedArgs)
27f4a2713aSLionel Sambuc : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
28*0a6a1f1dSLionel Sambuc TranslatedArgs(_TranslatedArgs), Redirects(nullptr),
29*0a6a1f1dSLionel Sambuc ForDiagnostics(false) {}
30f4a2713aSLionel Sambuc
~Compilation()31f4a2713aSLionel Sambuc Compilation::~Compilation() {
32f4a2713aSLionel Sambuc delete TranslatedArgs;
33f4a2713aSLionel Sambuc delete Args;
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc // Free any derived arg lists.
36f4a2713aSLionel Sambuc for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
37f4a2713aSLionel Sambuc DerivedArgList*>::iterator it = TCArgs.begin(),
38f4a2713aSLionel Sambuc ie = TCArgs.end(); it != ie; ++it)
39f4a2713aSLionel Sambuc if (it->second != TranslatedArgs)
40f4a2713aSLionel Sambuc delete it->second;
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc // Free the actions, if built.
43f4a2713aSLionel Sambuc for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
44f4a2713aSLionel Sambuc it != ie; ++it)
45f4a2713aSLionel Sambuc delete *it;
46f4a2713aSLionel Sambuc
47f4a2713aSLionel Sambuc // Free redirections of stdout/stderr.
48f4a2713aSLionel Sambuc if (Redirects) {
49f4a2713aSLionel Sambuc delete Redirects[1];
50f4a2713aSLionel Sambuc delete Redirects[2];
51f4a2713aSLionel Sambuc delete [] Redirects;
52f4a2713aSLionel Sambuc }
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
getArgsForToolChain(const ToolChain * TC,const char * BoundArch)55f4a2713aSLionel Sambuc const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
56f4a2713aSLionel Sambuc const char *BoundArch) {
57f4a2713aSLionel Sambuc if (!TC)
58f4a2713aSLionel Sambuc TC = &DefaultToolChain;
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
61f4a2713aSLionel Sambuc if (!Entry) {
62f4a2713aSLionel Sambuc Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
63f4a2713aSLionel Sambuc if (!Entry)
64f4a2713aSLionel Sambuc Entry = TranslatedArgs;
65f4a2713aSLionel Sambuc }
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc return *Entry;
68f4a2713aSLionel Sambuc }
69f4a2713aSLionel Sambuc
CleanupFile(const char * File,bool IssueErrors) const70f4a2713aSLionel Sambuc bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
71f4a2713aSLionel Sambuc // FIXME: Why are we trying to remove files that we have not created? For
72f4a2713aSLionel Sambuc // example we should only try to remove a temporary assembly file if
73f4a2713aSLionel Sambuc // "clang -cc1" succeed in writing it. Was this a workaround for when
74f4a2713aSLionel Sambuc // clang was writing directly to a .s file and sometimes leaving it behind
75f4a2713aSLionel Sambuc // during a failure?
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc // FIXME: If this is necessary, we can still try to split
78f4a2713aSLionel Sambuc // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
79f4a2713aSLionel Sambuc // duplicated stat from is_regular_file.
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc // Don't try to remove files which we don't have write access to (but may be
82f4a2713aSLionel Sambuc // able to remove), or non-regular files. Underlying tools may have
83f4a2713aSLionel Sambuc // intentionally not overwritten them.
84f4a2713aSLionel Sambuc if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
85f4a2713aSLionel Sambuc return true;
86f4a2713aSLionel Sambuc
87*0a6a1f1dSLionel Sambuc if (std::error_code EC = llvm::sys::fs::remove(File)) {
88f4a2713aSLionel Sambuc // Failure is only failure if the file exists and is "regular". We checked
89f4a2713aSLionel Sambuc // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
90f4a2713aSLionel Sambuc // so we don't need to check again.
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc if (IssueErrors)
93f4a2713aSLionel Sambuc getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
94f4a2713aSLionel Sambuc << EC.message();
95f4a2713aSLionel Sambuc return false;
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc return true;
98f4a2713aSLionel Sambuc }
99f4a2713aSLionel Sambuc
CleanupFileList(const ArgStringList & Files,bool IssueErrors) const100f4a2713aSLionel Sambuc bool Compilation::CleanupFileList(const ArgStringList &Files,
101f4a2713aSLionel Sambuc bool IssueErrors) const {
102f4a2713aSLionel Sambuc bool Success = true;
103f4a2713aSLionel Sambuc for (ArgStringList::const_iterator
104f4a2713aSLionel Sambuc it = Files.begin(), ie = Files.end(); it != ie; ++it)
105f4a2713aSLionel Sambuc Success &= CleanupFile(*it, IssueErrors);
106f4a2713aSLionel Sambuc return Success;
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc
CleanupFileMap(const ArgStringMap & Files,const JobAction * JA,bool IssueErrors) const109f4a2713aSLionel Sambuc bool Compilation::CleanupFileMap(const ArgStringMap &Files,
110f4a2713aSLionel Sambuc const JobAction *JA,
111f4a2713aSLionel Sambuc bool IssueErrors) const {
112f4a2713aSLionel Sambuc bool Success = true;
113f4a2713aSLionel Sambuc for (ArgStringMap::const_iterator
114f4a2713aSLionel Sambuc it = Files.begin(), ie = Files.end(); it != ie; ++it) {
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc // If specified, only delete the files associated with the JobAction.
117f4a2713aSLionel Sambuc // Otherwise, delete all files in the map.
118f4a2713aSLionel Sambuc if (JA && it->first != JA)
119f4a2713aSLionel Sambuc continue;
120f4a2713aSLionel Sambuc Success &= CleanupFile(it->second, IssueErrors);
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc return Success;
123f4a2713aSLionel Sambuc }
124f4a2713aSLionel Sambuc
ExecuteCommand(const Command & C,const Command * & FailingCommand) const125f4a2713aSLionel Sambuc int Compilation::ExecuteCommand(const Command &C,
126f4a2713aSLionel Sambuc const Command *&FailingCommand) const {
127f4a2713aSLionel Sambuc if ((getDriver().CCPrintOptions ||
128f4a2713aSLionel Sambuc getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
129f4a2713aSLionel Sambuc raw_ostream *OS = &llvm::errs();
130f4a2713aSLionel Sambuc
131f4a2713aSLionel Sambuc // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
132f4a2713aSLionel Sambuc // output stream.
133f4a2713aSLionel Sambuc if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
134*0a6a1f1dSLionel Sambuc std::error_code EC;
135*0a6a1f1dSLionel Sambuc OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, EC,
136*0a6a1f1dSLionel Sambuc llvm::sys::fs::F_Append |
137*0a6a1f1dSLionel Sambuc llvm::sys::fs::F_Text);
138*0a6a1f1dSLionel Sambuc if (EC) {
139f4a2713aSLionel Sambuc getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
140*0a6a1f1dSLionel Sambuc << EC.message();
141f4a2713aSLionel Sambuc FailingCommand = &C;
142f4a2713aSLionel Sambuc delete OS;
143f4a2713aSLionel Sambuc return 1;
144f4a2713aSLionel Sambuc }
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc if (getDriver().CCPrintOptions)
148f4a2713aSLionel Sambuc *OS << "[Logging clang options]";
149f4a2713aSLionel Sambuc
150f4a2713aSLionel Sambuc C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
151f4a2713aSLionel Sambuc
152f4a2713aSLionel Sambuc if (OS != &llvm::errs())
153f4a2713aSLionel Sambuc delete OS;
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc std::string Error;
157f4a2713aSLionel Sambuc bool ExecutionFailed;
158f4a2713aSLionel Sambuc int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
159f4a2713aSLionel Sambuc if (!Error.empty()) {
160f4a2713aSLionel Sambuc assert(Res && "Error string set with 0 result code!");
161f4a2713aSLionel Sambuc getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
162f4a2713aSLionel Sambuc }
163f4a2713aSLionel Sambuc
164f4a2713aSLionel Sambuc if (Res)
165f4a2713aSLionel Sambuc FailingCommand = &C;
166f4a2713aSLionel Sambuc
167f4a2713aSLionel Sambuc return ExecutionFailed ? 1 : Res;
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc
170f4a2713aSLionel Sambuc typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
171f4a2713aSLionel Sambuc
ActionFailed(const Action * A,const FailingCommandList & FailingCommands)172f4a2713aSLionel Sambuc static bool ActionFailed(const Action *A,
173f4a2713aSLionel Sambuc const FailingCommandList &FailingCommands) {
174f4a2713aSLionel Sambuc
175f4a2713aSLionel Sambuc if (FailingCommands.empty())
176f4a2713aSLionel Sambuc return false;
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
179f4a2713aSLionel Sambuc CE = FailingCommands.end(); CI != CE; ++CI)
180f4a2713aSLionel Sambuc if (A == &(CI->second->getSource()))
181f4a2713aSLionel Sambuc return true;
182f4a2713aSLionel Sambuc
183f4a2713aSLionel Sambuc for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
184f4a2713aSLionel Sambuc if (ActionFailed(*AI, FailingCommands))
185f4a2713aSLionel Sambuc return true;
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc return false;
188f4a2713aSLionel Sambuc }
189f4a2713aSLionel Sambuc
InputsOk(const Command & C,const FailingCommandList & FailingCommands)190f4a2713aSLionel Sambuc static bool InputsOk(const Command &C,
191f4a2713aSLionel Sambuc const FailingCommandList &FailingCommands) {
192f4a2713aSLionel Sambuc return !ActionFailed(&C.getSource(), FailingCommands);
193f4a2713aSLionel Sambuc }
194f4a2713aSLionel Sambuc
ExecuteJob(const Job & J,FailingCommandList & FailingCommands) const195f4a2713aSLionel Sambuc void Compilation::ExecuteJob(const Job &J,
196f4a2713aSLionel Sambuc FailingCommandList &FailingCommands) const {
197f4a2713aSLionel Sambuc if (const Command *C = dyn_cast<Command>(&J)) {
198f4a2713aSLionel Sambuc if (!InputsOk(*C, FailingCommands))
199f4a2713aSLionel Sambuc return;
200*0a6a1f1dSLionel Sambuc const Command *FailingCommand = nullptr;
201f4a2713aSLionel Sambuc if (int Res = ExecuteCommand(*C, FailingCommand))
202f4a2713aSLionel Sambuc FailingCommands.push_back(std::make_pair(Res, FailingCommand));
203f4a2713aSLionel Sambuc } else {
204f4a2713aSLionel Sambuc const JobList *Jobs = cast<JobList>(&J);
205*0a6a1f1dSLionel Sambuc for (const auto &Job : *Jobs)
206*0a6a1f1dSLionel Sambuc ExecuteJob(Job, FailingCommands);
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc }
209f4a2713aSLionel Sambuc
initCompilationForDiagnostics()210f4a2713aSLionel Sambuc void Compilation::initCompilationForDiagnostics() {
211*0a6a1f1dSLionel Sambuc ForDiagnostics = true;
212*0a6a1f1dSLionel Sambuc
213f4a2713aSLionel Sambuc // Free actions and jobs.
214f4a2713aSLionel Sambuc DeleteContainerPointers(Actions);
215f4a2713aSLionel Sambuc Jobs.clear();
216f4a2713aSLionel Sambuc
217f4a2713aSLionel Sambuc // Clear temporary/results file lists.
218f4a2713aSLionel Sambuc TempFiles.clear();
219f4a2713aSLionel Sambuc ResultFiles.clear();
220f4a2713aSLionel Sambuc FailureResultFiles.clear();
221f4a2713aSLionel Sambuc
222f4a2713aSLionel Sambuc // Remove any user specified output. Claim any unclaimed arguments, so as
223f4a2713aSLionel Sambuc // to avoid emitting warnings about unused args.
224f4a2713aSLionel Sambuc OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
225f4a2713aSLionel Sambuc options::OPT_MMD };
226f4a2713aSLionel Sambuc for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
227f4a2713aSLionel Sambuc if (TranslatedArgs->hasArg(OutputOpts[i]))
228f4a2713aSLionel Sambuc TranslatedArgs->eraseArg(OutputOpts[i]);
229f4a2713aSLionel Sambuc }
230f4a2713aSLionel Sambuc TranslatedArgs->ClaimAllArgs();
231f4a2713aSLionel Sambuc
232f4a2713aSLionel Sambuc // Redirect stdout/stderr to /dev/null.
233f4a2713aSLionel Sambuc Redirects = new const StringRef*[3]();
234*0a6a1f1dSLionel Sambuc Redirects[0] = nullptr;
235*0a6a1f1dSLionel Sambuc Redirects[1] = new StringRef();
236*0a6a1f1dSLionel Sambuc Redirects[2] = new StringRef();
237f4a2713aSLionel Sambuc }
238f4a2713aSLionel Sambuc
getSysRoot() const239f4a2713aSLionel Sambuc StringRef Compilation::getSysRoot() const {
240f4a2713aSLionel Sambuc return getDriver().SysRoot;
241f4a2713aSLionel Sambuc }
242