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-16 14:42:30 +08:00
|
|
|
|
2009-03-18 10:55:38 +08:00
|
|
|
#include "clang/Driver/Action.h"
|
2009-03-16 14:42:30 +08:00
|
|
|
#include "clang/Driver/ArgList.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"
|
2009-03-18 14:49:39 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2009-03-19 06:16:03 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.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;
|
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),
|
2011-08-03 01:58:04 +08:00
|
|
|
TranslatedArgs(_TranslatedArgs), Redirects(0) {
|
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
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void Compilation::PrintJob(raw_ostream &OS, const Job &J,
|
2009-03-19 06:44:24 +08:00
|
|
|
const char *Terminator, bool Quote) const {
|
|
|
|
if (const Command *C = dyn_cast<Command>(&J)) {
|
2009-03-18 14:49:39 +08:00
|
|
|
OS << " \"" << C->getExecutable() << '"';
|
|
|
|
for (ArgStringList::const_iterator it = C->getArguments().begin(),
|
2009-03-19 06:44:24 +08:00
|
|
|
ie = C->getArguments().end(); it != ie; ++it) {
|
2010-03-20 16:01:53 +08:00
|
|
|
OS << ' ';
|
2011-10-07 06:53:35 +08:00
|
|
|
if (!Quote && !std::strpbrk(*it, " \"\\$")) {
|
2010-03-20 16:01:53 +08:00
|
|
|
OS << *it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quote the argument and escape shell special characters; this isn't
|
|
|
|
// really complete but is good enough.
|
|
|
|
OS << '"';
|
|
|
|
for (const char *s = *it; *s; ++s) {
|
|
|
|
if (*s == '"' || *s == '\\' || *s == '$')
|
|
|
|
OS << '\\';
|
|
|
|
OS << *s;
|
|
|
|
}
|
|
|
|
OS << '"';
|
2009-03-19 06:44:24 +08:00
|
|
|
}
|
2009-03-18 14:49:39 +08:00
|
|
|
OS << Terminator;
|
|
|
|
} else {
|
2009-03-19 06:44:24 +08:00
|
|
|
const JobList *Jobs = cast<JobList>(&J);
|
2009-09-09 23:08:12 +08:00
|
|
|
for (JobList::const_iterator
|
2009-03-18 14:49:39 +08:00
|
|
|
it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
|
2009-03-19 06:44:24 +08:00
|
|
|
PrintJob(OS, **it, Terminator, Quote);
|
2009-03-18 14:49:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2009-03-19 06:16:03 +08:00
|
|
|
it = Files.begin(), ie = Files.end(); it != ie; ++it) {
|
2009-11-24 23:23:21 +08:00
|
|
|
|
2009-03-19 06:16:03 +08:00
|
|
|
llvm::sys::Path P(*it);
|
|
|
|
std::string Error;
|
|
|
|
|
2011-04-26 04:43:05 +08:00
|
|
|
// Don't try to remove files which we don't have write access to (but may be
|
|
|
|
// able to remove). Underlying tools may have intentionally not overwritten
|
|
|
|
// them.
|
|
|
|
if (!P.canWrite())
|
|
|
|
continue;
|
|
|
|
|
2009-03-19 06:16:03 +08:00
|
|
|
if (P.eraseFromDisk(false, &Error)) {
|
2010-10-30 07:26:14 +08:00
|
|
|
// Failure is only failure if the file exists and is "regular". There is
|
|
|
|
// a race condition here due to the limited interface of
|
|
|
|
// llvm::sys::Path, we want to know if the removal gave ENOENT.
|
2009-03-19 06:16:03 +08:00
|
|
|
|
|
|
|
// FIXME: Grumble, P.exists() is broken. PR3837.
|
|
|
|
struct stat buf;
|
2010-10-30 16:28:42 +08:00
|
|
|
if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
|
2010-10-30 07:26:14 +08:00
|
|
|
(errno != ENOENT)) {
|
2009-03-19 06:16:03 +08:00
|
|
|
if (IssueErrors)
|
|
|
|
getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
|
|
|
|
<< Error;
|
|
|
|
Success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2009-07-02 03:14:39 +08:00
|
|
|
int Compilation::ExecuteCommand(const Command &C,
|
|
|
|
const Command *&FailingCommand) const {
|
2009-03-19 16:01:45 +08:00
|
|
|
llvm::sys::Path Prog(C.getExecutable());
|
|
|
|
const char **Argv = new const char*[C.getArguments().size() + 2];
|
|
|
|
Argv[0] = C.getExecutable();
|
|
|
|
std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
|
|
|
|
Argv[C.getArguments().size() + 1] = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-03 01:58:04 +08:00
|
|
|
if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
|
|
|
|
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;
|
|
|
|
OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
|
|
|
|
Error,
|
|
|
|
llvm::raw_fd_ostream::F_Append);
|
|
|
|
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]";
|
|
|
|
|
|
|
|
PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
|
|
|
|
|
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
int Res =
|
2009-03-19 16:01:45 +08:00
|
|
|
llvm::sys::Program::ExecuteAndWait(Prog, Argv,
|
2011-08-03 01:58:04 +08:00
|
|
|
/*env*/0, Redirects,
|
2009-03-19 16:01:45 +08:00
|
|
|
/*secondsToWait*/0, /*memoryLimit*/0,
|
|
|
|
&Error);
|
|
|
|
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;
|
|
|
|
|
2009-03-19 16:01:45 +08:00
|
|
|
delete[] Argv;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
int Compilation::ExecuteJob(const Job &J,
|
2009-07-02 03:14:39 +08:00
|
|
|
const Command *&FailingCommand) const {
|
2009-03-19 06:44:24 +08:00
|
|
|
if (const Command *C = dyn_cast<Command>(&J)) {
|
2009-07-02 03:14:39 +08:00
|
|
|
return ExecuteCommand(*C, FailingCommand);
|
2009-03-19 06:44:24 +08:00
|
|
|
} else {
|
|
|
|
const JobList *Jobs = cast<JobList>(&J);
|
2009-09-09 23:08:12 +08:00
|
|
|
for (JobList::const_iterator
|
2009-03-19 06:44:24 +08:00
|
|
|
it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
|
2009-07-02 03:14:39 +08:00
|
|
|
if (int Res = ExecuteJob(**it, FailingCommand))
|
2009-03-19 06:44:24 +08:00
|
|
|
return Res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-08-03 01:58:04 +08:00
|
|
|
|
|
|
|
void Compilation::initCompilationForDiagnostics(void) {
|
|
|
|
// Free actions and jobs.
|
|
|
|
DeleteContainerPointers(Actions);
|
|
|
|
Jobs.clear();
|
|
|
|
|
|
|
|
// Clear temporary/results file lists.
|
|
|
|
TempFiles.clear();
|
|
|
|
ResultFiles.clear();
|
|
|
|
|
|
|
|
// 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 };
|
|
|
|
for (unsigned i = 0; i != sizeof(OutputOpts)/sizeof(OutputOpts[0]); ++i) {
|
|
|
|
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.
|
|
|
|
Redirects = new const llvm::sys::Path*[3]();
|
|
|
|
Redirects[1] = new const llvm::sys::Path();
|
|
|
|
Redirects[2] = new const llvm::sys::Path();
|
|
|
|
}
|