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 "ToolRunner.h"
16 #include "llvm/Config/config.h" // for HAVE_LINK_R
17 #include "llvm/System/Program.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/FileUtilities.h"
25 ToolExecutionError::~ToolExecutionError() throw() { }
27 /// RunProgramWithTimeout - This function provides an alternate interface to the
28 /// sys::Program::ExecuteAndWait interface.
29 /// @see sys:Program::ExecuteAndWait
30 static int RunProgramWithTimeout(const sys::Path &ProgramPath,
32 const sys::Path &StdInFile,
33 const sys::Path &StdOutFile,
34 const sys::Path &StdErrFile,
35 unsigned NumSeconds = 0) {
36 const sys::Path* redirects[3];
37 redirects[0] = &StdInFile;
38 redirects[1] = &StdOutFile;
39 redirects[2] = &StdErrFile;
42 sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds);
47 static void ProcessFailure(sys::Path ProgPath, const char** Args) {
48 std::ostringstream OS;
49 OS << "\nError running tool:\n ";
50 for (const char **Arg = Args; *Arg; ++Arg)
54 // Rerun the compiler, capturing any error messages to print them.
55 sys::Path ErrorFilename("error_messages");
57 if (ErrorFilename.makeUnique(true, &ErrMsg)) {
58 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
61 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
62 ErrorFilename); // FIXME: check return code ?
64 // Print out the error messages generated by GCC if possible...
65 std::ifstream ErrorFile(ErrorFilename.c_str());
67 std::copy(std::istreambuf_iterator<char>(ErrorFile),
68 std::istreambuf_iterator<char>(),
69 std::ostreambuf_iterator<char>(OS));
73 ErrorFilename.eraseFromDisk();
74 throw ToolExecutionError(OS.str());
77 //===---------------------------------------------------------------------===//
78 // LLI Implementation of AbstractIntepreter interface
81 class LLI : public AbstractInterpreter {
82 std::string LLIPath; // The path to the LLI executable
83 std::vector<std::string> ToolArgs; // Args to pass to LLI
85 LLI(const std::string &Path, const std::vector<std::string> *Args)
88 if (Args) { ToolArgs = *Args; }
91 virtual int ExecuteProgram(const std::string &Bytecode,
92 const std::vector<std::string> &Args,
93 const std::string &InputFile,
94 const std::string &OutputFile,
95 const std::vector<std::string> &GCCArgs,
96 const std::vector<std::string> &SharedLibs =
97 std::vector<std::string>(),
98 unsigned Timeout = 0);
102 int LLI::ExecuteProgram(const std::string &Bytecode,
103 const std::vector<std::string> &Args,
104 const std::string &InputFile,
105 const std::string &OutputFile,
106 const std::vector<std::string> &GCCArgs,
107 const std::vector<std::string> &SharedLibs,
109 if (!SharedLibs.empty())
110 throw ToolExecutionError("LLI currently does not support "
111 "loading shared libraries.");
113 if (!GCCArgs.empty())
114 throw ToolExecutionError("LLI currently does not support "
116 std::vector<const char*> LLIArgs;
117 LLIArgs.push_back(LLIPath.c_str());
118 LLIArgs.push_back("-force-interpreter=true");
120 // Add any extra LLI args.
121 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
122 LLIArgs.push_back(ToolArgs[i].c_str());
124 LLIArgs.push_back(Bytecode.c_str());
125 // Add optional parameters to the running program from Argv
126 for (unsigned i=0, e = Args.size(); i != e; ++i)
127 LLIArgs.push_back(Args[i].c_str());
128 LLIArgs.push_back(0);
130 std::cout << "<lli>" << std::flush;
131 DEBUG(std::cerr << "\nAbout to run:\t";
132 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
133 std::cerr << " " << LLIArgs[i];
136 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
137 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
141 // LLI create method - Try to find the LLI executable
142 AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
143 std::string &Message,
144 const std::vector<std::string> *ToolArgs) {
145 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
146 if (!LLIPath.empty()) {
147 Message = "Found lli: " + LLIPath + "\n";
148 return new LLI(LLIPath, ToolArgs);
151 Message = "Cannot find `lli' in executable directory or PATH!\n";
155 //===----------------------------------------------------------------------===//
156 // LLC Implementation of AbstractIntepreter interface
158 void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
159 sys::Path uniqueFile(Bytecode+".llc.s");
161 if (uniqueFile.makeUnique(true, &ErrMsg)) {
162 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
165 OutputAsmFile = uniqueFile;
166 std::vector<const char *> LLCArgs;
167 LLCArgs.push_back (LLCPath.c_str());
169 // Add any extra LLC args.
170 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
171 LLCArgs.push_back(ToolArgs[i].c_str());
173 LLCArgs.push_back ("-o");
174 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
175 LLCArgs.push_back ("-f"); // Overwrite as necessary...
176 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
177 LLCArgs.push_back (0);
179 std::cout << "<llc>" << std::flush;
180 DEBUG(std::cerr << "\nAbout to run:\t";
181 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
182 std::cerr << " " << LLCArgs[i];
185 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
186 sys::Path(), sys::Path(), sys::Path()))
187 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
190 void LLC::compileProgram(const std::string &Bytecode) {
191 sys::Path OutputAsmFile;
192 OutputAsm(Bytecode, OutputAsmFile);
193 OutputAsmFile.eraseFromDisk();
196 int LLC::ExecuteProgram(const std::string &Bytecode,
197 const std::vector<std::string> &Args,
198 const std::string &InputFile,
199 const std::string &OutputFile,
200 const std::vector<std::string> &ArgsForGCC,
201 const std::vector<std::string> &SharedLibs,
204 sys::Path OutputAsmFile;
205 OutputAsm(Bytecode, OutputAsmFile);
206 FileRemover OutFileRemover(OutputAsmFile);
208 std::vector<std::string> GCCArgs(ArgsForGCC);
209 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
211 // Assuming LLC worked, compile the result with GCC and run it.
212 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
213 InputFile, OutputFile, GCCArgs, Timeout);
216 /// createLLC - Try to find the LLC executable
218 LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
219 std::string &Message,
220 const std::vector<std::string> *Args) {
221 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
222 if (LLCPath.empty()) {
223 Message = "Cannot find `llc' in executable directory or PATH!\n";
227 Message = "Found llc: " + LLCPath + "\n";
228 GCC *gcc = GCC::create(ProgramPath, Message);
230 std::cerr << Message << "\n";
233 return new LLC(LLCPath, gcc, Args);
236 //===---------------------------------------------------------------------===//
237 // JIT Implementation of AbstractIntepreter interface
240 class JIT : public AbstractInterpreter {
241 std::string LLIPath; // The path to the LLI executable
242 std::vector<std::string> ToolArgs; // Args to pass to LLI
244 JIT(const std::string &Path, const std::vector<std::string> *Args)
247 if (Args) { ToolArgs = *Args; }
250 virtual int ExecuteProgram(const std::string &Bytecode,
251 const std::vector<std::string> &Args,
252 const std::string &InputFile,
253 const std::string &OutputFile,
254 const std::vector<std::string> &GCCArgs =
255 std::vector<std::string>(),
256 const std::vector<std::string> &SharedLibs =
257 std::vector<std::string>(),
258 unsigned Timeout =0 );
262 int JIT::ExecuteProgram(const std::string &Bytecode,
263 const std::vector<std::string> &Args,
264 const std::string &InputFile,
265 const std::string &OutputFile,
266 const std::vector<std::string> &GCCArgs,
267 const std::vector<std::string> &SharedLibs,
269 if (!GCCArgs.empty())
270 throw ToolExecutionError("JIT does not support GCC Arguments.");
271 // Construct a vector of parameters, incorporating those from the command-line
272 std::vector<const char*> JITArgs;
273 JITArgs.push_back(LLIPath.c_str());
274 JITArgs.push_back("-force-interpreter=false");
276 // Add any extra LLI args.
277 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
278 JITArgs.push_back(ToolArgs[i].c_str());
280 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
281 JITArgs.push_back("-load");
282 JITArgs.push_back(SharedLibs[i].c_str());
284 JITArgs.push_back(Bytecode.c_str());
285 // Add optional parameters to the running program from Argv
286 for (unsigned i=0, e = Args.size(); i != e; ++i)
287 JITArgs.push_back(Args[i].c_str());
288 JITArgs.push_back(0);
290 std::cout << "<jit>" << std::flush;
291 DEBUG(std::cerr << "\nAbout to run:\t";
292 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
293 std::cerr << " " << JITArgs[i];
296 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
297 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
298 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
302 /// createJIT - Try to find the LLI executable
304 AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
305 std::string &Message, const std::vector<std::string> *Args) {
306 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
307 if (!LLIPath.empty()) {
308 Message = "Found lli: " + LLIPath + "\n";
309 return new JIT(LLIPath, Args);
312 Message = "Cannot find `lli' in executable directory or PATH!\n";
316 void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
317 sys::Path uniqueFile(Bytecode+".cbe.c");
319 if (uniqueFile.makeUnique(true, &ErrMsg)) {
320 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
323 OutputCFile = uniqueFile;
324 std::vector<const char *> LLCArgs;
325 LLCArgs.push_back (LLCPath.c_str());
327 // Add any extra LLC args.
328 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
329 LLCArgs.push_back(ToolArgs[i].c_str());
331 LLCArgs.push_back ("-o");
332 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
333 LLCArgs.push_back ("-march=c"); // Output C language
334 LLCArgs.push_back ("-f"); // Overwrite as necessary...
335 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
336 LLCArgs.push_back (0);
338 std::cout << "<cbe>" << std::flush;
339 DEBUG(std::cerr << "\nAbout to run:\t";
340 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
341 std::cerr << " " << LLCArgs[i];
344 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
346 ProcessFailure(LLCPath, &LLCArgs[0]);
349 void CBE::compileProgram(const std::string &Bytecode) {
350 sys::Path OutputCFile;
351 OutputC(Bytecode, OutputCFile);
352 OutputCFile.eraseFromDisk();
355 int CBE::ExecuteProgram(const std::string &Bytecode,
356 const std::vector<std::string> &Args,
357 const std::string &InputFile,
358 const std::string &OutputFile,
359 const std::vector<std::string> &ArgsForGCC,
360 const std::vector<std::string> &SharedLibs,
362 sys::Path OutputCFile;
363 OutputC(Bytecode, OutputCFile);
365 FileRemover CFileRemove(OutputCFile);
367 std::vector<std::string> GCCArgs(ArgsForGCC);
368 GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
369 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
370 InputFile, OutputFile, GCCArgs, Timeout);
373 /// createCBE - Try to find the 'llc' executable
375 CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
376 std::string &Message,
377 const std::vector<std::string> *Args) {
378 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
379 if (LLCPath.isEmpty()) {
381 "Cannot find `llc' in executable directory or PATH!\n";
385 Message = "Found llc: " + LLCPath.toString() + "\n";
386 GCC *gcc = GCC::create(ProgramPath, Message);
388 std::cerr << Message << "\n";
391 return new CBE(LLCPath, gcc, Args);
394 //===---------------------------------------------------------------------===//
397 int GCC::ExecuteProgram(const std::string &ProgramFile,
398 const std::vector<std::string> &Args,
400 const std::string &InputFile,
401 const std::string &OutputFile,
402 const std::vector<std::string> &ArgsForGCC,
404 std::vector<const char*> GCCArgs;
406 GCCArgs.push_back(GCCPath.c_str());
408 // Specify -x explicitly in case the extension is wonky
409 GCCArgs.push_back("-x");
410 if (fileType == CFile) {
411 GCCArgs.push_back("c");
412 GCCArgs.push_back("-fno-strict-aliasing");
414 GCCArgs.push_back("assembler");
416 GCCArgs.push_back("-force_cpusubtype_ALL");
419 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
420 GCCArgs.push_back("-x");
421 GCCArgs.push_back("none");
422 GCCArgs.push_back("-o");
423 sys::Path OutputBinary (ProgramFile+".gcc.exe");
425 if (OutputBinary.makeUnique(true, &ErrMsg)) {
426 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
429 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
431 // Add any arguments intended for GCC. We locate them here because this is
432 // most likely -L and -l options that need to come before other libraries but
433 // after the source. Other options won't be sensitive to placement on the
434 // command line, so this should be safe.
435 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
436 GCCArgs.push_back(ArgsForGCC[i].c_str());
438 GCCArgs.push_back("-lm"); // Hard-code the math library...
439 GCCArgs.push_back("-O2"); // Optimize the program a bit...
440 #if defined (HAVE_LINK_R)
441 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
444 GCCArgs.push_back("-mcpu=v9");
446 GCCArgs.push_back(0); // NULL terminator
448 std::cout << "<gcc>" << std::flush;
449 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
451 ProcessFailure(GCCPath, &GCCArgs[0]);
455 std::vector<const char*> ProgramArgs;
457 ProgramArgs.push_back(OutputBinary.c_str());
458 // Add optional parameters to the running program from Argv
459 for (unsigned i=0, e = Args.size(); i != e; ++i)
460 ProgramArgs.push_back(Args[i].c_str());
461 ProgramArgs.push_back(0); // NULL terminator
463 // Now that we have a binary, run it!
464 std::cout << "<program>" << std::flush;
465 DEBUG(std::cerr << "\nAbout to run:\t";
466 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
467 std::cerr << " " << ProgramArgs[i];
471 FileRemover OutputBinaryRemover(OutputBinary);
472 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
473 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
477 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
478 std::string &OutputFile,
479 const std::vector<std::string> &ArgsForGCC) {
480 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
482 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
483 std::cerr << "Error making unique filename: " << ErrMsg << "\n";
486 OutputFile = uniqueFilename.toString();
488 std::vector<const char*> GCCArgs;
490 GCCArgs.push_back(GCCPath.c_str());
493 // Compile the C/asm file into a shared object
494 GCCArgs.push_back("-x");
495 GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
496 GCCArgs.push_back("-fno-strict-aliasing");
497 GCCArgs.push_back(InputFile.c_str()); // Specify the input filename.
498 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
499 GCCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
500 #elif defined(__APPLE__)
501 // link all source files into a single module in data segment, rather than
502 // generating blocks. dynamic_lookup requires that you set
503 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
504 // bugpoint to just pass that in the environment of GCC.
505 GCCArgs.push_back("-single_module");
506 GCCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
507 GCCArgs.push_back("-undefined");
508 GCCArgs.push_back("dynamic_lookup");
510 GCCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
513 #if defined(__ia64__) || defined(__alpha__)
514 GCCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
517 GCCArgs.push_back("-mcpu=v9");
519 GCCArgs.push_back("-o");
520 GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
521 GCCArgs.push_back("-O2"); // Optimize the program a bit.
525 // Add any arguments intended for GCC. We locate them here because this is
526 // most likely -L and -l options that need to come before other libraries but
527 // after the source. Other options won't be sensitive to placement on the
528 // command line, so this should be safe.
529 for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
530 GCCArgs.push_back(ArgsForGCC[i].c_str());
531 GCCArgs.push_back(0); // NULL terminator
535 std::cout << "<gcc>" << std::flush;
536 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
538 ProcessFailure(GCCPath, &GCCArgs[0]);
544 /// create - Try to find the `gcc' executable
546 GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
547 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
548 if (GCCPath.isEmpty()) {
549 Message = "Cannot find `gcc' in executable directory or PATH!\n";
553 Message = "Found gcc: " + GCCPath.toString() + "\n";
554 return new GCC(GCCPath);