//===-- ToolRunner.cpp ----------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the interfaces described in the ToolRunner.h file.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "toolrunner"
#include "ToolRunner.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Config/config.h" // for HAVE_LINK_R
#include <fstream>
#include <sstream>
using namespace llvm;
namespace llvm {
cl::opt<bool>
SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
}
namespace {
cl::opt<std::string>
RemoteClient("remote-client",
cl::desc("Remote execution client (rsh/ssh)"));
cl::opt<std::string>
RemoteHost("remote-host",
cl::desc("Remote execution (rsh/ssh) host"));
cl::opt<std::string>
RemotePort("remote-port",
cl::desc("Remote execution (rsh/ssh) port"));
cl::opt<std::string>
RemoteUser("remote-user",
cl::desc("Remote execution (rsh/ssh) user id"));
cl::opt<std::string>
RemoteExtra("remote-extra-options",
cl::desc("Remote execution (rsh/ssh) extra options"));
}
/// RunProgramWithTimeout - This function provides an alternate interface
/// to the sys::Program::ExecuteAndWait interface.
/// @see sys::Program::ExecuteAndWait
static int RunProgramWithTimeout(const sys::Path &ProgramPath,
const char **Args,
const sys::Path &StdInFile,
const sys::Path &StdOutFile,
const sys::Path &StdErrFile,
unsigned NumSeconds = 0,
unsigned MemoryLimit = 0,
std::string *ErrMsg = 0) {
const sys::Path* redirects[3];
redirects[0] = &StdInFile;
redirects[1] = &StdOutFile;
redirects[2] = &StdErrFile;
#if 0 // For debug purposes
{
errs() << "RUN:";
for (unsigned i = 0; Args[i]; ++i)
errs() << " " << Args[i];
errs() << "\n";
}
#endif
return
sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects,
NumSeconds, MemoryLimit, ErrMsg);
}
/// RunProgramRemotelyWithTimeout - This function runs the given program
/// remotely using the given remote client and the sys::Program::ExecuteAndWait.
/// Returns the remote program exit code or reports a remote client error if it
/// fails. Remote client is required to return 255 if it failed or program exit
/// code otherwise.
/// @see sys::Program::ExecuteAndWait
static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
const char **Args,
const sys::Path &StdInFile,
const sys::Path &StdOutFile,
const sys::Path &StdErrFile,
unsigned NumSeconds = 0,
unsigned MemoryLimit = 0) {
const sys::Path* redirects[3];
redirects[0] = &StdInFile;
redirects[1] = &StdOutFile;
redirects[2] = &StdErrFile;
#if 0 // For debug purposes
{
errs() << "RUN:";
for (unsigned i = 0; Args[i]; ++i)
errs() << " " << Args[i];
errs() << "\n";
}
#endif
// Run the program remotely with the remote client
int ReturnCode = sys::Program::ExecuteAndWait(RemoteClientPath, Args,
0, redirects, NumSeconds, MemoryLimit);
// Has the remote client fail?
if (255 == ReturnCode) {
std::ostringstream OS;
OS << "\nError running remote client:\n ";
for (const char **Arg = Args; *Arg; ++Arg)
OS << " " << *Arg;
OS << "\n";
// The error message is in the output file, let's print it out from there.
std::ifstream ErrorFile(StdOutFile.c_str());
if (ErrorFile) {
std::copy(std::istreambuf_iterator<char>(ErrorFile),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(OS));
ErrorFile.close();
}
errs() << OS;
}
return ReturnCode;
}
static std::string ProcessFailure(sys::Path ProgPath, const char** Args,
unsigned Timeout = 0,
unsigned MemoryLimit = 0) {
std::ostringstream OS;
OS << "\nError running tool:\n ";
for (const char **Arg = Args; *Arg;