2003-10-14 21:34:11 +00:00
|
|
|
//===-- ToolRunner.cpp ----------------------------------------------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-14 21:34:11 +00:00
|
|
|
//
|
|
|
|
// This file implements the interfaces described in the ToolRunner.h file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-10-19 02:27:40 +00:00
|
|
|
#define DEBUG_TYPE "toolrunner"
|
2003-10-06 18:37:24 +00:00
|
|
|
#include "llvm/Support/ToolRunner.h"
|
2003-11-18 06:31:17 +00:00
|
|
|
#include "Config/config.h" // for HAVE_LINK_R
|
2003-09-29 22:39:25 +00:00
|
|
|
#include "Support/Debug.h"
|
|
|
|
#include "Support/FileUtilities.h"
|
2003-10-14 21:34:11 +00:00
|
|
|
#include <fstream>
|
2004-02-18 20:38:00 +00:00
|
|
|
#include <sstream>
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-02-19 07:39:26 +00:00
|
|
|
ToolExecutionError::~ToolExecutionError() throw() { }
|
|
|
|
|
2004-02-18 20:38:00 +00:00
|
|
|
static void ProcessFailure(std::string ProgPath, const char** Args) {
|
|
|
|
std::ostringstream OS;
|
2004-02-18 20:58:00 +00:00
|
|
|
OS << "\nError running tool:\n ";
|
2004-02-18 20:38:00 +00:00
|
|
|
for (const char **Arg = Args; *Arg; ++Arg)
|
|
|
|
OS << " " << *Arg;
|
|
|
|
OS << "\n";
|
|
|
|
|
|
|
|
// Rerun the compiler, capturing any error messages to print them.
|
|
|
|
std::string ErrorFilename = getUniqueFilename("error_messages");
|
|
|
|
RunProgramWithTimeout(ProgPath, Args, "/dev/null", ErrorFilename.c_str(),
|
|
|
|
ErrorFilename.c_str());
|
|
|
|
|
|
|
|
// Print out the error messages generated by GCC if possible...
|
|
|
|
std::ifstream ErrorFile(ErrorFilename.c_str());
|
|
|
|
if (ErrorFile) {
|
|
|
|
std::copy(std::istreambuf_iterator<char>(ErrorFile),
|
|
|
|
std::istreambuf_iterator<char>(),
|
|
|
|
std::ostreambuf_iterator<char>(OS));
|
|
|
|
ErrorFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeFile(ErrorFilename);
|
|
|
|
throw ToolExecutionError(OS.str());
|
|
|
|
}
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// LLI Implementation of AbstractIntepreter interface
|
|
|
|
//
|
2003-12-14 21:35:53 +00:00
|
|
|
namespace {
|
|
|
|
class LLI : public AbstractInterpreter {
|
|
|
|
std::string LLIPath; // The path to the LLI executable
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<std::string> ToolArgs; // Args to pass to LLI
|
2003-12-14 21:35:53 +00:00
|
|
|
public:
|
2004-05-04 21:09:01 +00:00
|
|
|
LLI(const std::string &Path, const std::vector<std::string> *Args)
|
|
|
|
: LLIPath(Path) {
|
|
|
|
ToolArgs.clear ();
|
2004-05-04 22:02:41 +00:00
|
|
|
if (Args) { ToolArgs = *Args; }
|
2004-05-04 21:09:01 +00:00
|
|
|
}
|
2003-12-14 21:35:53 +00:00
|
|
|
|
|
|
|
virtual int ExecuteProgram(const std::string &Bytecode,
|
|
|
|
const std::vector<std::string> &Args,
|
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
|
|
|
const std::vector<std::string> &SharedLibs =
|
2003-10-14 21:52:52 +00:00
|
|
|
std::vector<std::string>());
|
2003-12-14 21:35:53 +00:00
|
|
|
};
|
|
|
|
}
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
int LLI::ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs) {
|
2004-02-18 20:21:57 +00:00
|
|
|
if (!SharedLibs.empty())
|
|
|
|
throw ToolExecutionError("LLI currently does not support "
|
|
|
|
"loading shared libraries.");
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
std::vector<const char*> LLIArgs;
|
|
|
|
LLIArgs.push_back(LLIPath.c_str());
|
|
|
|
LLIArgs.push_back("-force-interpreter=true");
|
2004-05-04 21:09:01 +00:00
|
|
|
|
|
|
|
// Add any extra LLI args.
|
|
|
|
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
|
|
|
LLIArgs.push_back(ToolArgs[i].c_str());
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
LLIArgs.push_back(Bytecode.c_str());
|
|
|
|
// Add optional parameters to the running program from Argv
|
|
|
|
for (unsigned i=0, e = Args.size(); i != e; ++i)
|
|
|
|
LLIArgs.push_back(Args[i].c_str());
|
|
|
|
LLIArgs.push_back(0);
|
|
|
|
|
|
|
|
std::cout << "<lli>" << std::flush;
|
2003-10-19 02:27:40 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
2003-10-19 02:14:58 +00:00
|
|
|
for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
|
2003-09-29 22:39:25 +00:00
|
|
|
std::cerr << " " << LLIArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
|
|
|
return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
|
|
|
|
InputFile, OutputFile, OutputFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// LLI create method - Try to find the LLI executable
|
2003-10-14 21:34:11 +00:00
|
|
|
AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message,
|
|
|
|
const std::vector<std::string> *ToolArgs) {
|
2003-10-14 21:34:11 +00:00
|
|
|
std::string LLIPath = FindExecutable("lli", ProgPath);
|
2003-09-29 22:39:25 +00:00
|
|
|
if (!LLIPath.empty()) {
|
|
|
|
Message = "Found lli: " + LLIPath + "\n";
|
2004-05-04 21:09:01 +00:00
|
|
|
return new LLI(LLIPath, ToolArgs);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Message = "Cannot find `lli' in executable directory or PATH!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LLC Implementation of AbstractIntepreter interface
|
|
|
|
//
|
2004-02-18 20:21:57 +00:00
|
|
|
void LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
|
2003-09-29 22:39:25 +00:00
|
|
|
OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<const char *> LLCArgs;
|
|
|
|
LLCArgs.push_back (LLCPath.c_str());
|
|
|
|
|
|
|
|
// Add any extra LLC args.
|
|
|
|
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
|
|
|
LLCArgs.push_back(ToolArgs[i].c_str());
|
|
|
|
|
|
|
|
LLCArgs.push_back ("-o");
|
|
|
|
LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
|
|
|
|
LLCArgs.push_back ("-f"); // Overwrite as necessary...
|
|
|
|
LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
|
|
|
|
LLCArgs.push_back (0);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
std::cout << "<llc>" << std::flush;
|
2004-05-04 21:09:01 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
|
|
|
for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
|
|
|
|
std::cerr << " " << LLCArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
|
|
|
if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
|
2004-02-18 20:21:57 +00:00
|
|
|
"/dev/null"))
|
2004-05-04 21:09:01 +00:00
|
|
|
ProcessFailure(LLCPath, &LLCArgs[0]);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2004-02-18 23:24:41 +00:00
|
|
|
void LLC::compileProgram(const std::string &Bytecode) {
|
|
|
|
std::string OutputAsmFile;
|
|
|
|
OutputAsm(Bytecode, OutputAsmFile);
|
|
|
|
removeFile(OutputAsmFile);
|
|
|
|
}
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
int LLC::ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs) {
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
std::string OutputAsmFile;
|
2004-02-18 20:21:57 +00:00
|
|
|
OutputAsm(Bytecode, OutputAsmFile);
|
|
|
|
FileRemover OutFileRemover(OutputAsmFile);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
// Assuming LLC worked, compile the result with GCC and run it.
|
2004-02-18 20:21:57 +00:00
|
|
|
return gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
|
|
|
|
InputFile, OutputFile, SharedLibs);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
/// createLLC - Try to find the LLC executable
|
2003-09-29 22:39:25 +00:00
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message,
|
|
|
|
const std::vector<std::string> *Args) {
|
2003-09-29 22:39:25 +00:00
|
|
|
std::string LLCPath = FindExecutable("llc", ProgramPath);
|
|
|
|
if (LLCPath.empty()) {
|
|
|
|
Message = "Cannot find `llc' in executable directory or PATH!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Message = "Found llc: " + LLCPath + "\n";
|
2003-10-14 21:34:11 +00:00
|
|
|
GCC *gcc = GCC::create(ProgramPath, Message);
|
2003-09-29 22:39:25 +00:00
|
|
|
if (!gcc) {
|
|
|
|
std::cerr << Message << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-05-04 21:09:01 +00:00
|
|
|
return new LLC(LLCPath, gcc, Args);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// JIT Implementation of AbstractIntepreter interface
|
|
|
|
//
|
2003-12-14 21:35:53 +00:00
|
|
|
namespace {
|
|
|
|
class JIT : public AbstractInterpreter {
|
|
|
|
std::string LLIPath; // The path to the LLI executable
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<std::string> ToolArgs; // Args to pass to LLI
|
2003-12-14 21:35:53 +00:00
|
|
|
public:
|
2004-05-04 21:09:01 +00:00
|
|
|
JIT(const std::string &Path, const std::vector<std::string> *Args)
|
|
|
|
: LLIPath(Path) {
|
|
|
|
ToolArgs.clear ();
|
2004-05-04 22:02:41 +00:00
|
|
|
if (Args) { ToolArgs = *Args; }
|
2004-05-04 21:09:01 +00:00
|
|
|
}
|
2003-12-14 21:35:53 +00:00
|
|
|
|
|
|
|
virtual int ExecuteProgram(const std::string &Bytecode,
|
|
|
|
const std::vector<std::string> &Args,
|
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
|
|
|
const std::vector<std::string> &SharedLibs =
|
2003-10-14 21:52:52 +00:00
|
|
|
std::vector<std::string>());
|
2003-12-14 21:35:53 +00:00
|
|
|
};
|
|
|
|
}
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
int JIT::ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs) {
|
2003-09-29 22:39:25 +00:00
|
|
|
// Construct a vector of parameters, incorporating those from the command-line
|
|
|
|
std::vector<const char*> JITArgs;
|
|
|
|
JITArgs.push_back(LLIPath.c_str());
|
|
|
|
JITArgs.push_back("-force-interpreter=false");
|
2003-10-14 21:52:52 +00:00
|
|
|
|
2004-05-04 21:09:01 +00:00
|
|
|
// Add any extra LLI args.
|
|
|
|
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
|
|
|
JITArgs.push_back(ToolArgs[i].c_str());
|
|
|
|
|
2003-10-14 21:52:52 +00:00
|
|
|
for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
|
2003-09-29 22:39:25 +00:00
|
|
|
JITArgs.push_back("-load");
|
2003-10-14 21:52:52 +00:00
|
|
|
JITArgs.push_back(SharedLibs[i].c_str());
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
JITArgs.push_back(Bytecode.c_str());
|
|
|
|
// Add optional parameters to the running program from Argv
|
|
|
|
for (unsigned i=0, e = Args.size(); i != e; ++i)
|
|
|
|
JITArgs.push_back(Args[i].c_str());
|
|
|
|
JITArgs.push_back(0);
|
|
|
|
|
|
|
|
std::cout << "<jit>" << std::flush;
|
2003-10-19 02:27:40 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
2003-10-19 02:14:58 +00:00
|
|
|
for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
|
2003-09-29 22:39:25 +00:00
|
|
|
std::cerr << " " << JITArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
|
|
|
DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
|
|
|
|
return RunProgramWithTimeout(LLIPath, &JITArgs[0],
|
|
|
|
InputFile, OutputFile, OutputFile);
|
|
|
|
}
|
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
/// createJIT - Try to find the LLI executable
|
2003-09-29 22:39:25 +00:00
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message, const std::vector<std::string> *Args) {
|
2003-10-14 21:34:11 +00:00
|
|
|
std::string LLIPath = FindExecutable("lli", ProgPath);
|
2003-09-29 22:39:25 +00:00
|
|
|
if (!LLIPath.empty()) {
|
|
|
|
Message = "Found lli: " + LLIPath + "\n";
|
2004-05-04 21:09:01 +00:00
|
|
|
return new JIT(LLIPath, Args);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Message = "Cannot find `lli' in executable directory or PATH!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-02-18 20:21:57 +00:00
|
|
|
void CBE::OutputC(const std::string &Bytecode,
|
2003-09-29 22:39:25 +00:00
|
|
|
std::string &OutputCFile) {
|
|
|
|
OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
|
2004-05-04 21:09:01 +00:00
|
|
|
std::vector<const char *> LLCArgs;
|
|
|
|
LLCArgs.push_back (LLCPath.c_str());
|
|
|
|
|
|
|
|
// Add any extra LLC args.
|
|
|
|
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
|
|
|
|
LLCArgs.push_back(ToolArgs[i].c_str());
|
|
|
|
|
|
|
|
LLCArgs.push_back ("-o");
|
|
|
|
LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
|
|
|
|
LLCArgs.push_back ("-march=c"); // Output C language
|
|
|
|
LLCArgs.push_back ("-f"); // Overwrite as necessary...
|
|
|
|
LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
|
|
|
|
LLCArgs.push_back (0);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
|
|
|
std::cout << "<cbe>" << std::flush;
|
2004-05-04 21:09:01 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
|
|
|
for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
|
|
|
|
std::cerr << " " << LLCArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
|
|
|
if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
|
2004-02-18 20:21:57 +00:00
|
|
|
"/dev/null"))
|
2004-05-04 21:09:01 +00:00
|
|
|
ProcessFailure(LLCPath, &LLCArgs[0]);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2004-02-18 23:24:41 +00:00
|
|
|
void CBE::compileProgram(const std::string &Bytecode) {
|
|
|
|
std::string OutputCFile;
|
|
|
|
OutputC(Bytecode, OutputCFile);
|
|
|
|
removeFile(OutputCFile);
|
|
|
|
}
|
|
|
|
|
2003-09-29 22:39:25 +00:00
|
|
|
int CBE::ExecuteProgram(const std::string &Bytecode,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs) {
|
2003-09-29 22:39:25 +00:00
|
|
|
std::string OutputCFile;
|
2004-02-18 20:21:57 +00:00
|
|
|
OutputC(Bytecode, OutputCFile);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
2004-02-18 20:21:57 +00:00
|
|
|
FileRemover CFileRemove(OutputCFile);
|
2003-09-29 22:39:25 +00:00
|
|
|
|
2004-02-18 20:21:57 +00:00
|
|
|
return gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
|
|
|
|
InputFile, OutputFile, SharedLibs);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2004-02-17 06:40:06 +00:00
|
|
|
/// createCBE - Try to find the 'llc' executable
|
2003-09-29 22:39:25 +00:00
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
|
2004-05-04 21:09:01 +00:00
|
|
|
std::string &Message,
|
|
|
|
const std::vector<std::string> *Args) {
|
2004-02-17 06:40:06 +00:00
|
|
|
std::string LLCPath = FindExecutable("llc", ProgramPath);
|
|
|
|
if (LLCPath.empty()) {
|
2003-09-29 22:39:25 +00:00
|
|
|
Message =
|
2004-02-17 06:40:06 +00:00
|
|
|
"Cannot find `llc' in executable directory or PATH!\n";
|
2003-09-29 22:39:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-02-17 06:40:06 +00:00
|
|
|
Message = "Found llc: " + LLCPath + "\n";
|
2003-10-14 21:34:11 +00:00
|
|
|
GCC *gcc = GCC::create(ProgramPath, Message);
|
2003-09-29 22:39:25 +00:00
|
|
|
if (!gcc) {
|
|
|
|
std::cerr << Message << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-05-04 21:09:01 +00:00
|
|
|
return new CBE(LLCPath, gcc, Args);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// GCC abstraction
|
|
|
|
//
|
|
|
|
int GCC::ExecuteProgram(const std::string &ProgramFile,
|
2003-10-14 21:34:11 +00:00
|
|
|
const std::vector<std::string> &Args,
|
2003-09-29 22:39:25 +00:00
|
|
|
FileType fileType,
|
|
|
|
const std::string &InputFile,
|
|
|
|
const std::string &OutputFile,
|
2003-10-14 21:52:52 +00:00
|
|
|
const std::vector<std::string> &SharedLibs) {
|
2003-09-29 22:39:25 +00:00
|
|
|
std::vector<const char*> GCCArgs;
|
|
|
|
|
|
|
|
GCCArgs.push_back(GCCPath.c_str());
|
2003-10-14 21:52:52 +00:00
|
|
|
|
|
|
|
// Specify the shared libraries to link in...
|
|
|
|
for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
|
|
|
|
GCCArgs.push_back(SharedLibs[i].c_str());
|
|
|
|
|
|
|
|
// Specify -x explicitly in case the extension is wonky
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back("-x");
|
|
|
|
if (fileType == CFile) {
|
|
|
|
GCCArgs.push_back("c");
|
|
|
|
GCCArgs.push_back("-fno-strict-aliasing");
|
|
|
|
} else {
|
|
|
|
GCCArgs.push_back("assembler");
|
|
|
|
}
|
|
|
|
GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
|
|
|
|
GCCArgs.push_back("-o");
|
2003-10-14 21:52:52 +00:00
|
|
|
std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
|
|
|
|
GCCArgs.push_back("-lm"); // Hard-code the math library...
|
|
|
|
GCCArgs.push_back("-O2"); // Optimize the program a bit...
|
2003-11-18 06:31:17 +00:00
|
|
|
#if defined (HAVE_LINK_R)
|
2003-10-18 21:54:47 +00:00
|
|
|
GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
|
2003-11-18 06:31:17 +00:00
|
|
|
#endif
|
2003-09-29 22:39:25 +00:00
|
|
|
GCCArgs.push_back(0); // NULL terminator
|
|
|
|
|
|
|
|
std::cout << "<gcc>" << std::flush;
|
|
|
|
if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
|
|
|
|
"/dev/null")) {
|
2004-02-18 20:38:00 +00:00
|
|
|
ProcessFailure(GCCPath, &GCCArgs[0]);
|
2003-09-29 22:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const char*> ProgramArgs;
|
|
|
|
ProgramArgs.push_back(OutputBinary.c_str());
|
|
|
|
// Add optional parameters to the running program from Argv
|
|
|
|
for (unsigned i=0, e = Args.size(); i != e; ++i)
|
|
|
|
ProgramArgs.push_back(Args[i].c_str());
|
|
|
|
ProgramArgs.push_back(0); // NULL terminator
|
|
|
|
|
|
|
|
// Now that we have a binary, run it!
|
|
|
|
std::cout << "<program>" << std::flush;
|
2003-10-19 02:27:40 +00:00
|
|
|
DEBUG(std::cerr << "\nAbout to run:\t";
|
2003-10-19 02:14:58 +00:00
|
|
|
for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
|
2003-09-29 22:39:25 +00:00
|
|
|
std::cerr << " " << ProgramArgs[i];
|
|
|
|
std::cerr << "\n";
|
|
|
|
);
|
2004-02-18 20:21:57 +00:00
|
|
|
|
|
|
|
FileRemover OutputBinaryRemover(OutputBinary);
|
|
|
|
return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
|
|
|
|
InputFile, OutputFile, OutputFile);
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-14 21:07:25 +00:00
|
|
|
int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
|
2003-09-29 22:39:25 +00:00
|
|
|
std::string &OutputFile) {
|
2004-01-26 20:59:41 +00:00
|
|
|
OutputFile = getUniqueFilename(InputFile+SHLIBEXT);
|
2003-09-29 22:39:25 +00:00
|
|
|
// Compile the C/asm file into a shared object
|
|
|
|
const char* GCCArgs[] = {
|
|
|
|
GCCPath.c_str(),
|
|
|
|
"-x", (fileType == AsmFile) ? "assembler" : "c",
|
|
|
|
"-fno-strict-aliasing",
|
|
|
|
InputFile.c_str(), // Specify the input filename...
|
|
|
|
#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
|
|
|
|
"-G", // Compile a shared library, `-G' for Sparc
|
2004-07-16 19:45:45 +00:00
|
|
|
#elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
|
|
|
|
"-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
|
|
|
|
"-fno-common", // allow global vars w/o initializers to live
|
2004-07-19 06:03:51 +00:00
|
|
|
"-undefined", // in data segment, rather than generating
|
2004-07-19 06:00:17 +00:00
|
|
|
"dynamic_lookup", // blocks. dynamic_lookup requires that you set
|
|
|
|
// MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
|
2004-07-16 19:45:45 +00:00
|
|
|
#else
|
2003-09-29 22:39:25 +00:00
|
|
|
"-shared", // `-shared' for Linux/X86, maybe others
|
|
|
|
#endif
|
|
|
|
"-o", OutputFile.c_str(), // Output to the right filename...
|
|
|
|
"-O2", // Optimize the program a bit...
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
std::cout << "<gcc>" << std::flush;
|
2003-10-14 21:07:25 +00:00
|
|
|
if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
|
|
|
|
"/dev/null")) {
|
2004-02-18 20:38:00 +00:00
|
|
|
ProcessFailure(GCCPath, GCCArgs);
|
2003-10-14 21:07:25 +00:00
|
|
|
return 1;
|
2003-09-29 22:39:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-10-14 21:34:11 +00:00
|
|
|
/// create - Try to find the `gcc' executable
|
2003-09-29 22:39:25 +00:00
|
|
|
///
|
2003-10-14 21:34:11 +00:00
|
|
|
GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
|
2003-09-29 22:39:25 +00:00
|
|
|
std::string GCCPath = FindExecutable("gcc", ProgramPath);
|
|
|
|
if (GCCPath.empty()) {
|
|
|
|
Message = "Cannot find `gcc' in executable directory or PATH!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Message = "Found gcc: " + GCCPath + "\n";
|
|
|
|
return new GCC(GCCPath);
|
|
|
|
}
|