1 //===-- ToolRunner.cpp ----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the interfaces described in the ToolRunner.h file.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "toolrunner"
15 #include "llvm/Support/ToolRunner.h"
16 #include "Config/config.h" // for HAVE_LINK_R
17 #include "Support/Debug.h"
18 #include "Support/FileUtilities.h"
23 ToolExecutionError::~ToolExecutionError() throw() { }
25 static void ProcessFailure(std::string ProgPath, const char** Args) {
26 std::ostringstream OS;
27 OS << "\nError running tool:\n ";
28 for (const char **Arg = Args; *Arg; ++Arg)
32 // Rerun the compiler, capturing any error messages to print them.
33 std::string ErrorFilename = getUniqueFilename("error_messages");
34 RunProgramWithTimeout(ProgPath, Args, "/dev/null", ErrorFilename.c_str(),
35 ErrorFilename.c_str());
37 // Print out the error messages generated by GCC if possible...
38 std::ifstream ErrorFile(ErrorFilename.c_str());
40 std::copy(std::istreambuf_iterator<char>(ErrorFile),
41 std::istreambuf_iterator<char>(),
42 std::ostreambuf_iterator<char>(OS));
46 removeFile(ErrorFilename);
47 throw ToolExecutionError(OS.str());
50 //===---------------------------------------------------------------------===//
51 // LLI Implementation of AbstractIntepreter interface
54 class LLI : public AbstractInterpreter {
55 std::string LLIPath; // The path to the LLI executable
56 std::vector<std::string> ToolArgs; // Args to pass to LLI
58 LLI(const std::string &Path, const std::vector<std::string> *Args)
61 if (Args) { ToolArgs = *Args; }
64 virtual int ExecuteProgram(const std::string &Bytecode,
65 const std::vector<std::string> &Args,
66 const std::string &InputFile,
67 const std::string &OutputFile,
68 const std::vector<std::string> &SharedLibs =
69 std::vector<std::string>(),
70 unsigned Timeout = 0);
74 int LLI::ExecuteProgram(const std::string &Bytecode,
75 const std::vector<std::string> &Args,
76 const std::string &InputFile,
77 const std::string &OutputFile,
78 const std::vector<std::string> &SharedLibs,
80 if (!SharedLibs.empty())
81 throw ToolExecutionError("LLI currently does not support "
82 "loading shared libraries.");
84 std::vector<const char*> LLIArgs;
85 LLIArgs.push_back(LLIPath.c_str());
86 LLIArgs.push_back("-force-interpreter=true");
88 // Add any extra LLI args.
89 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
90 LLIArgs.push_back(ToolArgs[i].c_str());
92 LLIArgs.push_back(Bytecode.c_str());
93 // Add optional parameters to the running program from Argv
94 for (unsigned i=0, e = Args.size(); i != e; ++i)
95 LLIArgs.push_back(Args[i].c_str());
98 std::cout << "<lli>" << std::flush;
99 DEBUG(std::cerr << "\nAbout to run:\t";
100 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
101 std::cerr << " " << LLIArgs[i];
104 return RunProgramWithTimeout(LLIPath, &LLIArgs[0],
105 InputFile, OutputFile, OutputFile, Timeout);
108 // LLI create method - Try to find the LLI executable
109 AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
110 std::string &Message,
111 const std::vector<std::string> *ToolArgs) {
112 std::string LLIPath = FindExecutable("lli", ProgPath);
113 if (!LLIPath.empty()) {
114 Message = "Found lli: " + LLIPath + "\n";
115 return new LLI(LLIPath, ToolArgs);
118 Message = "Cannot find `lli' in executable directory or PATH!\n";
122 //===----------------------------------------------------------------------===//
123 // LLC Implementation of AbstractIntepreter interface
125 void LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
126 OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
127 std::vector<const char *> LLCArgs;
128 LLCArgs.push_back (LLCPath.c_str());
130 // Add any extra LLC args.
131 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
132 LLCArgs.push_back(ToolArgs[i].c_str());
134 LLCArgs.push_back ("-o");
135 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
136 LLCArgs.push_back ("-f"); // Overwrite as necessary...
137 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
138 LLCArgs.push_back (0);
140 std::cout << "<llc>" << std::flush;
141 DEBUG(std::cerr << "\nAbout to run:\t";
142 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
143 std::cerr << " " << LLCArgs[i];
146 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
148 ProcessFailure(LLCPath, &LLCArgs[0]);
151 void LLC::compileProgram(const std::string &Bytecode) {
152 std::string OutputAsmFile;
153 OutputAsm(Bytecode, OutputAsmFile);
154 removeFile(OutputAsmFile);
157 int LLC::ExecuteProgram(const std::string &Bytecode,
158 const std::vector<std::string> &Args,
159 const std::string &InputFile,
160 const std::string &OutputFile,
161 const std::vector<std::string> &SharedLibs,
164 std::string OutputAsmFile;
165 OutputAsm(Bytecode, OutputAsmFile);
166 FileRemover OutFileRemover(OutputAsmFile);
168 // Assuming LLC worked, compile the result with GCC and run it.
169 return gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
170 InputFile, OutputFile, SharedLibs, Timeout);
173 /// createLLC - Try to find the LLC executable
175 LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
176 std::string &Message,
177 const std::vector<std::string> *Args) {
178 std::string LLCPath = FindExecutable("llc", ProgramPath);
179 if (LLCPath.empty()) {
180 Message = "Cannot find `llc' in executable directory or PATH!\n";
184 Message = "Found llc: " + LLCPath + "\n";
185 GCC *gcc = GCC::create(ProgramPath, Message);
187 std::cerr << Message << "\n";
190 return new LLC(LLCPath, gcc, Args);
193 //===---------------------------------------------------------------------===//
194 // JIT Implementation of AbstractIntepreter interface
197 class JIT : public AbstractInterpreter {
198 std::string LLIPath; // The path to the LLI executable
199 std::vector<std::string> ToolArgs; // Args to pass to LLI
201 JIT(const std::string &Path, const std::vector<std::string> *Args)
204 if (Args) { ToolArgs = *Args; }
207 virtual int ExecuteProgram(const std::string &Bytecode,
208 const std::vector<std::string> &Args,
209 const std::string &InputFile,
210 const std::string &OutputFile,
211 const std::vector<std::string> &SharedLibs =
212 std::vector<std::string>(), unsigned Timeout =0);
216 int JIT::ExecuteProgram(const std::string &Bytecode,
217 const std::vector<std::string> &Args,
218 const std::string &InputFile,
219 const std::string &OutputFile,
220 const std::vector<std::string> &SharedLibs,
222 // Construct a vector of parameters, incorporating those from the command-line
223 std::vector<const char*> JITArgs;
224 JITArgs.push_back(LLIPath.c_str());
225 JITArgs.push_back("-force-interpreter=false");
227 // Add any extra LLI args.
228 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
229 JITArgs.push_back(ToolArgs[i].c_str());
231 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
232 JITArgs.push_back("-load");
233 JITArgs.push_back(SharedLibs[i].c_str());
235 JITArgs.push_back(Bytecode.c_str());
236 // Add optional parameters to the running program from Argv
237 for (unsigned i=0, e = Args.size(); i != e; ++i)
238 JITArgs.push_back(Args[i].c_str());
239 JITArgs.push_back(0);
241 std::cout << "<jit>" << std::flush;
242 DEBUG(std::cerr << "\nAbout to run:\t";
243 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
244 std::cerr << " " << JITArgs[i];
247 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
248 return RunProgramWithTimeout(LLIPath, &JITArgs[0],
249 InputFile, OutputFile, OutputFile, Timeout);
252 /// createJIT - Try to find the LLI executable
254 AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
255 std::string &Message, const std::vector<std::string> *Args) {
256 std::string LLIPath = FindExecutable("lli", ProgPath);
257 if (!LLIPath.empty()) {
258 Message = "Found lli: " + LLIPath + "\n";
259 return new JIT(LLIPath, Args);
262 Message = "Cannot find `lli' in executable directory or PATH!\n";
266 void CBE::OutputC(const std::string &Bytecode,
267 std::string &OutputCFile) {
268 OutputCFile = getUniqueFilename(Bytecode+".cbe.c");
269 std::vector<const char *> LLCArgs;
270 LLCArgs.push_back (LLCPath.c_str());
272 // Add any extra LLC args.
273 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
274 LLCArgs.push_back(ToolArgs[i].c_str());
276 LLCArgs.push_back ("-o");
277 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
278 LLCArgs.push_back ("-march=c"); // Output C language
279 LLCArgs.push_back ("-f"); // Overwrite as necessary...
280 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
281 LLCArgs.push_back (0);
283 std::cout << "<cbe>" << std::flush;
284 DEBUG(std::cerr << "\nAbout to run:\t";
285 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
286 std::cerr << " " << LLCArgs[i];
289 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "/dev/null", "/dev/null",
291 ProcessFailure(LLCPath, &LLCArgs[0]);
294 void CBE::compileProgram(const std::string &Bytecode) {
295 std::string OutputCFile;
296 OutputC(Bytecode, OutputCFile);
297 removeFile(OutputCFile);
300 int CBE::ExecuteProgram(const std::string &Bytecode,
301 const std::vector<std::string> &Args,
302 const std::string &InputFile,
303 const std::string &OutputFile,
304 const std::vector<std::string> &SharedLibs,
306 std::string OutputCFile;
307 OutputC(Bytecode, OutputCFile);
309 FileRemover CFileRemove(OutputCFile);
311 return gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile,
312 InputFile, OutputFile, SharedLibs, Timeout);
315 /// createCBE - Try to find the 'llc' executable
317 CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
318 std::string &Message,
319 const std::vector<std::string> *Args) {
320 std::string LLCPath = FindExecutable("llc", ProgramPath);
321 if (LLCPath.empty()) {
323 "Cannot find `llc' in executable directory or PATH!\n";
327 Message = "Found llc: " + LLCPath + "\n";
328 GCC *gcc = GCC::create(ProgramPath, Message);
330 std::cerr << Message << "\n";
333 return new CBE(LLCPath, gcc, Args);
336 //===---------------------------------------------------------------------===//
339 int GCC::ExecuteProgram(const std::string &ProgramFile,
340 const std::vector<std::string> &Args,
342 const std::string &InputFile,
343 const std::string &OutputFile,
344 const std::vector<std::string> &SharedLibs,
346 std::vector<const char*> GCCArgs;
348 GCCArgs.push_back(GCCPath.c_str());
350 // Specify the shared libraries to link in...
351 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
352 GCCArgs.push_back(SharedLibs[i].c_str());
354 // Specify -x explicitly in case the extension is wonky
355 GCCArgs.push_back("-x");
356 if (fileType == CFile) {
357 GCCArgs.push_back("c");
358 GCCArgs.push_back("-fno-strict-aliasing");
360 GCCArgs.push_back("assembler");
362 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
363 GCCArgs.push_back("-o");
364 std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
365 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
366 GCCArgs.push_back("-lm"); // Hard-code the math library...
367 GCCArgs.push_back("-O2"); // Optimize the program a bit...
368 #if defined (HAVE_LINK_R)
369 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
371 GCCArgs.push_back(0); // NULL terminator
373 std::cout << "<gcc>" << std::flush;
374 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "/dev/null", "/dev/null",
376 ProcessFailure(GCCPath, &GCCArgs[0]);
380 std::vector<const char*> ProgramArgs;
381 ProgramArgs.push_back(OutputBinary.c_str());
382 // Add optional parameters to the running program from Argv
383 for (unsigned i=0, e = Args.size(); i != e; ++i)
384 ProgramArgs.push_back(Args[i].c_str());
385 ProgramArgs.push_back(0); // NULL terminator
387 // Now that we have a binary, run it!
388 std::cout << "<program>" << std::flush;
389 DEBUG(std::cerr << "\nAbout to run:\t";
390 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
391 std::cerr << " " << ProgramArgs[i];
395 FileRemover OutputBinaryRemover(OutputBinary);
396 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
397 InputFile, OutputFile, OutputFile, Timeout);
400 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
401 std::string &OutputFile) {
402 OutputFile = getUniqueFilename(InputFile+SHLIBEXT);
403 // Compile the C/asm file into a shared object
404 const char* GCCArgs[] = {
406 "-x", (fileType == AsmFile) ? "assembler" : "c",
407 "-fno-strict-aliasing",
408 InputFile.c_str(), // Specify the input filename...
409 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
410 "-G", // Compile a shared library, `-G' for Sparc
411 #elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
412 "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
413 "-fno-common", // allow global vars w/o initializers to live
414 "-undefined", // in data segment, rather than generating
415 "dynamic_lookup", // blocks. dynamic_lookup requires that you set
416 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
418 "-shared", // `-shared' for Linux/X86, maybe others
420 "-o", OutputFile.c_str(), // Output to the right filename...
421 "-O2", // Optimize the program a bit...
425 std::cout << "<gcc>" << std::flush;
426 if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
428 ProcessFailure(GCCPath, GCCArgs);
434 /// create - Try to find the `gcc' executable
436 GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
437 std::string GCCPath = FindExecutable("gcc", ProgramPath);
438 if (GCCPath.empty()) {
439 Message = "Cannot find `gcc' in executable directory or PATH!\n";
443 Message = "Found gcc: " + GCCPath + "\n";
444 return new GCC(GCCPath);