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 "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"
24 ToolExecutionError::~ToolExecutionError() throw() { }
26 /// RunProgramWithTimeout - This function provides an alternate interface to the
27 /// sys::Program::ExecuteAndWait interface.
28 /// @see sys:Program::ExecuteAndWait
29 static int RunProgramWithTimeout(const sys::Path &ProgramPath,
31 const sys::Path &StdInFile,
32 const sys::Path &StdOutFile,
33 const sys::Path &StdErrFile,
34 unsigned NumSeconds = 0) {
35 const sys::Path* redirects[3];
36 redirects[0] = &StdInFile;
37 redirects[1] = &StdOutFile;
38 redirects[2] = &StdErrFile;
41 sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds);
46 static void ProcessFailure(sys::Path ProgPath, const char** Args) {
47 std::ostringstream OS;
48 OS << "\nError running tool:\n ";
49 for (const char **Arg = Args; *Arg; ++Arg)
53 // Rerun the compiler, capturing any error messages to print them.
54 sys::Path ErrorFilename("error_messages");
55 ErrorFilename.makeUnique();
56 RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
59 // Print out the error messages generated by GCC if possible...
60 std::ifstream ErrorFile(ErrorFilename.c_str());
62 std::copy(std::istreambuf_iterator<char>(ErrorFile),
63 std::istreambuf_iterator<char>(),
64 std::ostreambuf_iterator<char>(OS));
68 ErrorFilename.destroyFile();
69 throw ToolExecutionError(OS.str());
72 //===---------------------------------------------------------------------===//
73 // LLI Implementation of AbstractIntepreter interface
76 class LLI : public AbstractInterpreter {
77 std::string LLIPath; // The path to the LLI executable
78 std::vector<std::string> ToolArgs; // Args to pass to LLI
80 LLI(const std::string &Path, const std::vector<std::string> *Args)
83 if (Args) { ToolArgs = *Args; }
86 virtual int ExecuteProgram(const std::string &Bytecode,
87 const std::vector<std::string> &Args,
88 const std::string &InputFile,
89 const std::string &OutputFile,
90 const std::vector<std::string> &SharedLibs =
91 std::vector<std::string>(),
92 unsigned Timeout = 0);
96 int LLI::ExecuteProgram(const std::string &Bytecode,
97 const std::vector<std::string> &Args,
98 const std::string &InputFile,
99 const std::string &OutputFile,
100 const std::vector<std::string> &SharedLibs,
102 if (!SharedLibs.empty())
103 throw ToolExecutionError("LLI currently does not support "
104 "loading shared libraries.");
106 std::vector<const char*> LLIArgs;
107 LLIArgs.push_back(LLIPath.c_str());
108 LLIArgs.push_back("-force-interpreter=true");
110 // Add any extra LLI args.
111 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
112 LLIArgs.push_back(ToolArgs[i].c_str());
114 LLIArgs.push_back(Bytecode.c_str());
115 // Add optional parameters to the running program from Argv
116 for (unsigned i=0, e = Args.size(); i != e; ++i)
117 LLIArgs.push_back(Args[i].c_str());
118 LLIArgs.push_back(0);
120 std::cout << "<lli>" << std::flush;
121 DEBUG(std::cerr << "\nAbout to run:\t";
122 for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
123 std::cerr << " " << LLIArgs[i];
126 return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
127 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
131 // LLI create method - Try to find the LLI executable
132 AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
133 std::string &Message,
134 const std::vector<std::string> *ToolArgs) {
135 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
136 if (!LLIPath.empty()) {
137 Message = "Found lli: " + LLIPath + "\n";
138 return new LLI(LLIPath, ToolArgs);
141 Message = "Cannot find `lli' in executable directory or PATH!\n";
145 //===----------------------------------------------------------------------===//
146 // LLC Implementation of AbstractIntepreter interface
148 void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
149 sys::Path uniqueFile(Bytecode+".llc.s");
150 uniqueFile.makeUnique();
151 OutputAsmFile = uniqueFile;
152 std::vector<const char *> LLCArgs;
153 LLCArgs.push_back (LLCPath.c_str());
155 // Add any extra LLC args.
156 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
157 LLCArgs.push_back(ToolArgs[i].c_str());
159 LLCArgs.push_back ("-o");
160 LLCArgs.push_back (OutputAsmFile.c_str()); // Output to the Asm file
161 LLCArgs.push_back ("-f"); // Overwrite as necessary...
162 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
163 LLCArgs.push_back (0);
165 std::cout << "<llc>" << std::flush;
166 DEBUG(std::cerr << "\nAbout to run:\t";
167 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
168 std::cerr << " " << LLCArgs[i];
171 if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
172 sys::Path(), sys::Path(), sys::Path()))
173 ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
176 void LLC::compileProgram(const std::string &Bytecode) {
177 sys::Path OutputAsmFile;
178 OutputAsm(Bytecode, OutputAsmFile);
179 OutputAsmFile.destroyFile();
182 int LLC::ExecuteProgram(const std::string &Bytecode,
183 const std::vector<std::string> &Args,
184 const std::string &InputFile,
185 const std::string &OutputFile,
186 const std::vector<std::string> &SharedLibs,
189 sys::Path OutputAsmFile;
190 OutputAsm(Bytecode, OutputAsmFile);
191 FileRemover OutFileRemover(OutputAsmFile);
193 // Assuming LLC worked, compile the result with GCC and run it.
194 return gcc->ExecuteProgram(OutputAsmFile.toString(), Args, GCC::AsmFile,
195 InputFile, OutputFile, SharedLibs, Timeout);
198 /// createLLC - Try to find the LLC executable
200 LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
201 std::string &Message,
202 const std::vector<std::string> *Args) {
203 std::string LLCPath = FindExecutable("llc", ProgramPath).toString();
204 if (LLCPath.empty()) {
205 Message = "Cannot find `llc' in executable directory or PATH!\n";
209 Message = "Found llc: " + LLCPath + "\n";
210 GCC *gcc = GCC::create(ProgramPath, Message);
212 std::cerr << Message << "\n";
215 return new LLC(LLCPath, gcc, Args);
218 //===---------------------------------------------------------------------===//
219 // JIT Implementation of AbstractIntepreter interface
222 class JIT : public AbstractInterpreter {
223 std::string LLIPath; // The path to the LLI executable
224 std::vector<std::string> ToolArgs; // Args to pass to LLI
226 JIT(const std::string &Path, const std::vector<std::string> *Args)
229 if (Args) { ToolArgs = *Args; }
232 virtual int ExecuteProgram(const std::string &Bytecode,
233 const std::vector<std::string> &Args,
234 const std::string &InputFile,
235 const std::string &OutputFile,
236 const std::vector<std::string> &SharedLibs =
237 std::vector<std::string>(), unsigned Timeout =0);
241 int JIT::ExecuteProgram(const std::string &Bytecode,
242 const std::vector<std::string> &Args,
243 const std::string &InputFile,
244 const std::string &OutputFile,
245 const std::vector<std::string> &SharedLibs,
247 // Construct a vector of parameters, incorporating those from the command-line
248 std::vector<const char*> JITArgs;
249 JITArgs.push_back(LLIPath.c_str());
250 JITArgs.push_back("-force-interpreter=false");
252 // Add any extra LLI args.
253 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
254 JITArgs.push_back(ToolArgs[i].c_str());
256 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
257 JITArgs.push_back("-load");
258 JITArgs.push_back(SharedLibs[i].c_str());
260 JITArgs.push_back(Bytecode.c_str());
261 // Add optional parameters to the running program from Argv
262 for (unsigned i=0, e = Args.size(); i != e; ++i)
263 JITArgs.push_back(Args[i].c_str());
264 JITArgs.push_back(0);
266 std::cout << "<jit>" << std::flush;
267 DEBUG(std::cerr << "\nAbout to run:\t";
268 for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
269 std::cerr << " " << JITArgs[i];
272 DEBUG(std::cerr << "\nSending output to " << OutputFile << "\n");
273 return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
274 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
278 /// createJIT - Try to find the LLI executable
280 AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
281 std::string &Message, const std::vector<std::string> *Args) {
282 std::string LLIPath = FindExecutable("lli", ProgPath).toString();
283 if (!LLIPath.empty()) {
284 Message = "Found lli: " + LLIPath + "\n";
285 return new JIT(LLIPath, Args);
288 Message = "Cannot find `lli' in executable directory or PATH!\n";
292 void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
293 sys::Path uniqueFile(Bytecode+".cbe.c");
294 uniqueFile.makeUnique();
295 OutputCFile = uniqueFile;
296 std::vector<const char *> LLCArgs;
297 LLCArgs.push_back (LLCPath.c_str());
299 // Add any extra LLC args.
300 for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
301 LLCArgs.push_back(ToolArgs[i].c_str());
303 LLCArgs.push_back ("-o");
304 LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file
305 LLCArgs.push_back ("-march=c"); // Output C language
306 LLCArgs.push_back ("-f"); // Overwrite as necessary...
307 LLCArgs.push_back (Bytecode.c_str()); // This is the input bytecode
308 LLCArgs.push_back (0);
310 std::cout << "<cbe>" << std::flush;
311 DEBUG(std::cerr << "\nAbout to run:\t";
312 for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
313 std::cerr << " " << LLCArgs[i];
316 if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
318 ProcessFailure(LLCPath, &LLCArgs[0]);
321 void CBE::compileProgram(const std::string &Bytecode) {
322 sys::Path OutputCFile;
323 OutputC(Bytecode, OutputCFile);
324 OutputCFile.destroyFile();
327 int CBE::ExecuteProgram(const std::string &Bytecode,
328 const std::vector<std::string> &Args,
329 const std::string &InputFile,
330 const std::string &OutputFile,
331 const std::vector<std::string> &SharedLibs,
333 sys::Path OutputCFile;
334 OutputC(Bytecode, OutputCFile);
336 FileRemover CFileRemove(OutputCFile);
338 return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
339 InputFile, OutputFile, SharedLibs, Timeout);
342 /// createCBE - Try to find the 'llc' executable
344 CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
345 std::string &Message,
346 const std::vector<std::string> *Args) {
347 sys::Path LLCPath = FindExecutable("llc", ProgramPath);
348 if (LLCPath.isEmpty()) {
350 "Cannot find `llc' in executable directory or PATH!\n";
354 Message = "Found llc: " + LLCPath.toString() + "\n";
355 GCC *gcc = GCC::create(ProgramPath, Message);
357 std::cerr << Message << "\n";
360 return new CBE(LLCPath, gcc, Args);
363 //===---------------------------------------------------------------------===//
366 int GCC::ExecuteProgram(const std::string &ProgramFile,
367 const std::vector<std::string> &Args,
369 const std::string &InputFile,
370 const std::string &OutputFile,
371 const std::vector<std::string> &SharedLibs,
373 std::vector<const char*> GCCArgs;
375 GCCArgs.push_back(GCCPath.c_str());
377 // Specify the shared libraries to link in...
378 for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
379 GCCArgs.push_back(SharedLibs[i].c_str());
381 // Specify -x explicitly in case the extension is wonky
382 GCCArgs.push_back("-x");
383 if (fileType == CFile) {
384 GCCArgs.push_back("c");
385 GCCArgs.push_back("-fno-strict-aliasing");
387 GCCArgs.push_back("assembler");
389 GCCArgs.push_back(ProgramFile.c_str()); // Specify the input filename...
390 GCCArgs.push_back("-o");
391 sys::Path OutputBinary (ProgramFile+".gcc.exe");
392 OutputBinary.makeUnique();
393 GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
394 GCCArgs.push_back("-L/home/vadve/shared/localtools/x86/lib");
395 GCCArgs.push_back("-lf2c"); // Hard-code the math library...
396 GCCArgs.push_back("-lpng"); // Hard-code the math library...
397 GCCArgs.push_back("-lz"); // Hard-code the math library...
398 GCCArgs.push_back("-lm"); // Hard-code the math library...
399 GCCArgs.push_back("-O2"); // Optimize the program a bit...
400 #if defined (HAVE_LINK_R)
401 GCCArgs.push_back("-Wl,-R."); // Search this dir for .so files
403 GCCArgs.push_back(0); // NULL terminator
405 std::cout << "<gcc>" << std::flush;
406 if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
408 ProcessFailure(GCCPath, &GCCArgs[0]);
412 std::vector<const char*> ProgramArgs;
413 //ProgramArgs.push_back("valgrind");
414 //ProgramArgs.push_back("--tool=memcheck");
416 ProgramArgs.push_back(OutputBinary.c_str());
417 // Add optional parameters to the running program from Argv
418 for (unsigned i=0, e = Args.size(); i != e; ++i)
419 ProgramArgs.push_back(Args[i].c_str());
420 ProgramArgs.push_back(0); // NULL terminator
422 // Now that we have a binary, run it!
423 std::cout << "<program>" << std::flush;
424 DEBUG(std::cerr << "\nAbout to run:\t";
425 for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
426 std::cerr << " " << ProgramArgs[i];
430 FileRemover OutputBinaryRemover(OutputBinary);
431 return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
432 sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
436 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
437 std::string &OutputFile) {
438 sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
439 uniqueFilename.makeUnique();
440 OutputFile = uniqueFilename.toString();
442 // Compile the C/asm file into a shared object
443 const char* GCCArgs[] = {
445 "-x", (fileType == AsmFile) ? "assembler" : "c",
446 "-fno-strict-aliasing",
447 InputFile.c_str(), // Specify the input filename...
448 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
449 "-G", // Compile a shared library, `-G' for Sparc
450 #elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
451 "-single_module", // link all source files into a single module
452 "-dynamiclib", // `-dynamiclib' for MacOS X/PowerPC
453 "-undefined", // in data segment, rather than generating
454 "dynamic_lookup", // blocks. dynamic_lookup requires that you set
455 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
457 "-shared", // `-shared' for Linux/X86, maybe others
459 "-o", OutputFile.c_str(), // Output to the right filename...
460 "-O2", // Optimize the program a bit...
464 std::cout << "<gcc>" << std::flush;
465 if (RunProgramWithTimeout(GCCPath, GCCArgs, sys::Path(), sys::Path(),
467 ProcessFailure(GCCPath, GCCArgs);
473 /// create - Try to find the `gcc' executable
475 GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
476 sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
477 if (GCCPath.isEmpty()) {
478 Message = "Cannot find `gcc' in executable directory or PATH!\n";
482 Message = "Found gcc: " + GCCPath.toString() + "\n";
483 return new GCC(GCCPath);