2011-01-01 01:31:54 +08:00
|
|
|
//===--- Compilation.cpp - Compilation Task Implementation ----------------===//
|
2009-03-03 03:59:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Driver/Compilation.h"
|
2009-03-18 10:55:38 +08:00
|
|
|
#include "clang/Driver/Action.h"
|
2009-03-19 06:16:03 +08:00
|
|
|
#include "clang/Driver/Driver.h"
|
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
2009-11-19 12:25:22 +08:00
|
|
|
#include "clang/Driver/Options.h"
|
2009-03-16 14:42:30 +08:00
|
|
|
#include "clang/Driver/ToolChain.h"
|
2011-08-03 01:58:04 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "llvm/Option/ArgList.h"
|
2013-06-19 04:58:25 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-03-19 06:16:03 +08:00
|
|
|
#include <errno.h>
|
2012-12-04 17:13:33 +08:00
|
|
|
#include <sys/stat.h>
|
2011-07-23 19:36:43 +08:00
|
|
|
|
2009-03-05 04:49:20 +08:00
|
|
|
using namespace clang::driver;
|
2011-07-23 19:36:43 +08:00
|
|
|
using namespace clang;
|
2013-06-15 01:17:23 +08:00
|
|
|
using namespace llvm::opt;
|
2009-03-03 03:59:07 +08:00
|
|
|
|
2010-06-12 06:00:26 +08:00
|
|
|
Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
|
|
|
|
InputArgList *_Args, DerivedArgList *_TranslatedArgs)
|
|
|
|
: TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
|
2014-05-18 00:56:41 +08:00
|
|
|
TranslatedArgs(_TranslatedArgs), Redirects(nullptr) {
|
2009-03-16 14:42:30 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Compilation::~Compilation() {
|
2010-06-12 06:00:26 +08:00
|
|
|
delete TranslatedArgs;
|
2009-03-16 14:42:30 +08:00
|
|
|
delete Args;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-16 14:42:30 +08:00
|
|
|
// Free any derived arg lists.
|
2009-09-10 02:36:01 +08:00
|
|
|
for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
|
|
|
|
DerivedArgList*>::iterator it = TCArgs.begin(),
|
|
|
|
ie = TCArgs.end(); it != ie; ++it)
|
2010-06-12 06:00:26 +08:00
|
|
|
if (it->second != TranslatedArgs)
|
|
|
|
delete it->second;
|
2009-03-18 10:55:38 +08:00
|
|
|
|
|
|
|
// Free the actions, if built.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
|
2009-03-18 10:55:38 +08:00
|
|
|
it != ie; ++it)
|
|
|
|
delete *it;
|
2011-08-03 01:58:04 +08:00
|
|
|
|
|
|
|
// Free redirections of stdout/stderr.
|
|
|
|
if (Redirects) {
|
|
|
|
delete Redirects[1];
|
|
|
|
delete Redirects[2];
|
|
|
|
delete [] Redirects;
|
|
|
|
}
|
2009-03-03 03:59:07 +08:00
|
|
|
}
|
|
|
|
|
2009-09-10 02:36:01 +08:00
|
|
|
const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
|
|
|
|
const char *BoundArch) {
|
2009-03-16 14:42:30 +08:00
|
|
|
if (!TC)
|
|
|
|
TC = &DefaultToolChain;
|
|
|
|
|
2009-09-10 02:36:01 +08:00
|
|
|
DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
|
2010-06-12 06:00:26 +08:00
|
|
|
if (!Entry) {
|
|
|
|
Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
|
|
|
|
if (!Entry)
|
|
|
|
Entry = TranslatedArgs;
|
|
|
|
}
|
2009-03-16 14:42:30 +08:00
|
|
|
|
2009-03-18 13:58:45 +08:00
|
|
|
return *Entry;
|
2009-03-03 03:59:07 +08:00
|
|
|
}
|
|
|
|
|
2013-01-25 03:14:47 +08:00
|
|
|
bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
|
2013-06-25 01:59:44 +08:00
|
|
|
// FIXME: Why are we trying to remove files that we have not created? For
|
|
|
|
// example we should only try to remove a temporary assembly file if
|
|
|
|
// "clang -cc1" succeed in writing it. Was this a workaround for when
|
|
|
|
// clang was writing directly to a .s file and sometimes leaving it behind
|
|
|
|
// during a failure?
|
|
|
|
|
|
|
|
// FIXME: If this is necessary, we can still try to split
|
|
|
|
// llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
|
|
|
|
// duplicated stat from is_regular_file.
|
2013-01-25 03:14:47 +08:00
|
|
|
|
|
|
|
// Don't try to remove files which we don't have write access to (but may be
|
|
|
|
// able to remove), or non-regular files. Underlying tools may have
|
|
|
|
// intentionally not overwritten them.
|
2013-06-25 01:59:44 +08:00
|
|
|
if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
|
2013-01-25 03:14:47 +08:00
|
|
|
return true;
|
|
|
|
|
2013-06-25 01:59:44 +08:00
|
|
|
if (llvm::error_code EC = llvm::sys::fs::remove(File)) {
|
|
|
|
// Failure is only failure if the file exists and is "regular". We checked
|
|
|
|
// for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
|
|
|
|
// so we don't need to check again.
|
2013-01-25 03:14:47 +08:00
|
|
|
|
2013-06-25 01:59:44 +08:00
|
|
|
if (IssueErrors)
|
|
|
|
getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
|
|
|
|
<< EC.message();
|
|
|
|
return false;
|
2013-01-25 03:14:47 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Compilation::CleanupFileList(const ArgStringList &Files,
|
2009-03-19 06:16:03 +08:00
|
|
|
bool IssueErrors) const {
|
|
|
|
bool Success = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ArgStringList::const_iterator
|
2013-01-25 03:14:47 +08:00
|
|
|
it = Files.begin(), ie = Files.end(); it != ie; ++it)
|
|
|
|
Success &= CleanupFile(*it, IssueErrors);
|
|
|
|
return Success;
|
|
|
|
}
|
2009-11-24 23:23:21 +08:00
|
|
|
|
2013-01-25 03:14:47 +08:00
|
|
|
bool Compilation::CleanupFileMap(const ArgStringMap &Files,
|
|
|
|
const JobAction *JA,
|
|
|
|
bool IssueErrors) const {
|
|
|
|
bool Success = true;
|
|
|
|
for (ArgStringMap::const_iterator
|
|
|
|
it = Files.begin(), ie = Files.end(); it != ie; ++it) {
|
2009-03-19 06:16:03 +08:00
|
|
|
|
2013-01-25 03:14:47 +08:00
|
|
|
// If specified, only delete the files associated with the JobAction.
|
|
|
|
// Otherwise, delete all files in the map.
|
|
|
|
if (JA && it->first != JA)
|
2011-04-26 04:43:05 +08:00
|
|
|
continue;
|
2013-01-25 03:14:47 +08:00
|
|
|
Success &= CleanupFile(it->second, IssueErrors);
|
2009-03-19 06:16:03 +08:00
|
|
|
}
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2009-07-02 03:14:39 +08:00
|
|
|
int Compilation::ExecuteCommand(const Command &C,
|
|
|
|
const Command *&FailingCommand) const {
|
2013-07-24 01:58:53 +08:00
|
|
|
if ((getDriver().CCPrintOptions ||
|
2011-08-03 01:58:04 +08:00
|
|
|
getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream *OS = &llvm::errs();
|
2010-03-20 16:01:59 +08:00
|
|
|
|
|
|
|
// Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
|
|
|
|
// output stream.
|
|
|
|
if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
|
|
|
|
std::string Error;
|
2013-07-17 03:44:23 +08:00
|
|
|
OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, Error,
|
2014-02-25 02:20:21 +08:00
|
|
|
llvm::sys::fs::F_Append |
|
|
|
|
llvm::sys::fs::F_Text);
|
2010-03-20 16:01:59 +08:00
|
|
|
if (!Error.empty()) {
|
|
|
|
getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
|
|
|
|
<< Error;
|
|
|
|
FailingCommand = &C;
|
|
|
|
delete OS;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getDriver().CCPrintOptions)
|
|
|
|
*OS << "[Logging clang options]";
|
|
|
|
|
2013-09-13 02:23:34 +08:00
|
|
|
C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
|
2010-03-20 16:01:59 +08:00
|
|
|
|
|
|
|
if (OS != &llvm::errs())
|
|
|
|
delete OS;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-19 16:01:45 +08:00
|
|
|
std::string Error;
|
2013-03-27 07:41:30 +08:00
|
|
|
bool ExecutionFailed;
|
2013-09-13 02:35:08 +08:00
|
|
|
int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
|
2009-03-19 16:01:45 +08:00
|
|
|
if (!Error.empty()) {
|
|
|
|
assert(Res && "Error string set with 0 result code!");
|
|
|
|
getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-02 03:14:39 +08:00
|
|
|
if (Res)
|
|
|
|
FailingCommand = &C;
|
|
|
|
|
2013-03-27 07:41:30 +08:00
|
|
|
return ExecutionFailed ? 1 : Res;
|
2009-03-19 16:01:45 +08:00
|
|
|
}
|
|
|
|
|
2013-02-28 02:46:04 +08:00
|
|
|
typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
|
|
|
|
|
|
|
|
static bool ActionFailed(const Action *A,
|
|
|
|
const FailingCommandList &FailingCommands) {
|
|
|
|
|
|
|
|
if (FailingCommands.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
|
|
|
|
CE = FailingCommands.end(); CI != CE; ++CI)
|
|
|
|
if (A == &(CI->second->getSource()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
|
|
|
|
if (ActionFailed(*AI, FailingCommands))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool InputsOk(const Command &C,
|
|
|
|
const FailingCommandList &FailingCommands) {
|
|
|
|
return !ActionFailed(&C.getSource(), FailingCommands);
|
|
|
|
}
|
|
|
|
|
2013-01-30 04:15:05 +08:00
|
|
|
void Compilation::ExecuteJob(const Job &J,
|
2013-02-28 02:46:04 +08:00
|
|
|
FailingCommandList &FailingCommands) const {
|
2009-03-19 06:44:24 +08:00
|
|
|
if (const Command *C = dyn_cast<Command>(&J)) {
|
2013-02-28 02:46:04 +08:00
|
|
|
if (!InputsOk(*C, FailingCommands))
|
|
|
|
return;
|
2014-05-18 00:56:41 +08:00
|
|
|
const Command *FailingCommand = nullptr;
|
2013-01-30 04:15:05 +08:00
|
|
|
if (int Res = ExecuteCommand(*C, FailingCommand))
|
|
|
|
FailingCommands.push_back(std::make_pair(Res, FailingCommand));
|
2009-03-19 06:44:24 +08:00
|
|
|
} else {
|
|
|
|
const JobList *Jobs = cast<JobList>(&J);
|
2013-01-30 04:15:05 +08:00
|
|
|
for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
|
|
|
|
it != ie; ++it)
|
|
|
|
ExecuteJob(**it, FailingCommands);
|
2009-03-19 06:44:24 +08:00
|
|
|
}
|
|
|
|
}
|
2011-08-03 01:58:04 +08:00
|
|
|
|
2012-11-15 22:28:07 +08:00
|
|
|
void Compilation::initCompilationForDiagnostics() {
|
2011-08-03 01:58:04 +08:00
|
|
|
// Free actions and jobs.
|
|
|
|
DeleteContainerPointers(Actions);
|
|
|
|
Jobs.clear();
|
|
|
|
|
|
|
|
// Clear temporary/results file lists.
|
|
|
|
TempFiles.clear();
|
|
|
|
ResultFiles.clear();
|
2013-01-30 07:57:10 +08:00
|
|
|
FailureResultFiles.clear();
|
2011-08-03 01:58:04 +08:00
|
|
|
|
|
|
|
// Remove any user specified output. Claim any unclaimed arguments, so as
|
|
|
|
// to avoid emitting warnings about unused args.
|
2011-11-06 08:40:05 +08:00
|
|
|
OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
|
|
|
|
options::OPT_MMD };
|
2012-05-04 05:25:34 +08:00
|
|
|
for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
|
2011-11-06 08:40:05 +08:00
|
|
|
if (TranslatedArgs->hasArg(OutputOpts[i]))
|
|
|
|
TranslatedArgs->eraseArg(OutputOpts[i]);
|
|
|
|
}
|
2011-08-03 01:58:04 +08:00
|
|
|
TranslatedArgs->ClaimAllArgs();
|
|
|
|
|
|
|
|
// Redirect stdout/stderr to /dev/null.
|
2013-06-14 04:08:52 +08:00
|
|
|
Redirects = new const StringRef*[3]();
|
2014-05-18 00:56:41 +08:00
|
|
|
Redirects[0] = nullptr;
|
2013-06-14 04:08:52 +08:00
|
|
|
Redirects[1] = new const StringRef();
|
|
|
|
Redirects[2] = new const StringRef();
|
2011-08-03 01:58:04 +08:00
|
|
|
}
|
2012-04-16 12:16:43 +08:00
|
|
|
|
2012-11-15 22:28:07 +08:00
|
|
|
StringRef Compilation::getSysRoot() const {
|
2012-04-16 12:16:43 +08:00
|
|
|
return getDriver().SysRoot;
|
|
|
|
}
|