Add support for programs with a null argv[0]
[oota-llvm.git] / tools / bugpoint / ToolRunner.cpp
1 //===-- ToolRunner.cpp ----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the interfaces described in the ToolRunner.h file.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
20 #include <fstream>
21 #include <sstream>
22 using namespace llvm;
23
24 ToolExecutionError::~ToolExecutionError() throw() { }
25
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,
30                                  const char **Args,
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;
39
40   return
41     sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects, NumSeconds);
42 }
43
44
45
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)
50     OS << " " << *Arg;
51   OS << "\n";
52
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,
57                         ErrorFilename);
58
59   // Print out the error messages generated by GCC if possible...
60   std::ifstream ErrorFile(ErrorFilename.c_str());
61   if (ErrorFile) {
62     std::copy(std::istreambuf_iterator<char>(ErrorFile),
63               std::istreambuf_iterator<char>(),
64               std::ostreambuf_iterator<char>(OS));
65     ErrorFile.close();
66   }
67
68   ErrorFilename.eraseFromDisk();
69   throw ToolExecutionError(OS.str());
70 }
71
72 //===---------------------------------------------------------------------===//
73 // LLI Implementation of AbstractIntepreter interface
74 //
75 namespace {
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
79   public:
80     LLI(const std::string &Path, const std::vector<std::string> *Args)
81       : LLIPath(Path) {
82       ToolArgs.clear ();
83       if (Args) { ToolArgs = *Args; }
84     }
85
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);
93   };
94 }
95
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,
101                         unsigned Timeout) {
102   if (!SharedLibs.empty())
103     throw ToolExecutionError("LLI currently does not support "
104                              "loading shared libraries.");
105
106   std::vector<const char*> LLIArgs;
107   LLIArgs.push_back(LLIPath.c_str());
108   LLIArgs.push_back("-force-interpreter=true");
109
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());
113
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);
119
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];
124         std::cerr << "\n";
125         );
126   return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
127       sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
128       Timeout);
129 }
130
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);
139   }
140
141   Message = "Cannot find `lli' in executable directory or PATH!\n";
142   return 0;
143 }
144
145 //===----------------------------------------------------------------------===//
146 // LLC Implementation of AbstractIntepreter interface
147 //
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());
154
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());
158
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);
164
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];
169         std::cerr << "\n";
170         );
171   if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
172                             sys::Path(), sys::Path(), sys::Path()))
173     ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
174 }
175
176 void LLC::compileProgram(const std::string &Bytecode) {
177   sys::Path OutputAsmFile;
178   OutputAsm(Bytecode, OutputAsmFile);
179   OutputAsmFile.eraseFromDisk();
180 }
181
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,
187                         unsigned Timeout) {
188
189   sys::Path OutputAsmFile;
190   OutputAsm(Bytecode, OutputAsmFile);
191   FileRemover OutFileRemover(OutputAsmFile);
192
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);
196 }
197
198 /// createLLC - Try to find the LLC executable
199 ///
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";
206     return 0;
207   }
208
209   Message = "Found llc: " + LLCPath + "\n";
210   GCC *gcc = GCC::create(ProgramPath, Message);
211   if (!gcc) {
212     std::cerr << Message << "\n";
213     exit(1);
214   }
215   return new LLC(LLCPath, gcc, Args);
216 }
217
218 //===---------------------------------------------------------------------===//
219 // JIT Implementation of AbstractIntepreter interface
220 //
221 namespace {
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
225   public:
226     JIT(const std::string &Path, const std::vector<std::string> *Args)
227       : LLIPath(Path) {
228       ToolArgs.clear ();
229       if (Args) { ToolArgs = *Args; }
230     }
231
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);
238   };
239 }
240
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,
246                         unsigned Timeout) {
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");
251
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());
255
256   for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
257     JITArgs.push_back("-load");
258     JITArgs.push_back(SharedLibs[i].c_str());
259   }
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);
265
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];
270         std::cerr << "\n";
271         );
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),
275       Timeout);
276 }
277
278 /// createJIT - Try to find the LLI executable
279 ///
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);
286   }
287
288   Message = "Cannot find `lli' in executable directory or PATH!\n";
289   return 0;
290 }
291
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());
298
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());
302
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);
309
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];
314         std::cerr << "\n";
315         );
316   if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
317                             sys::Path()))
318     ProcessFailure(LLCPath, &LLCArgs[0]);
319 }
320
321 void CBE::compileProgram(const std::string &Bytecode) {
322   sys::Path OutputCFile;
323   OutputC(Bytecode, OutputCFile);
324   OutputCFile.eraseFromDisk();
325 }
326
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,
332                         unsigned Timeout) {
333   sys::Path OutputCFile;
334   OutputC(Bytecode, OutputCFile);
335
336   FileRemover CFileRemove(OutputCFile);
337
338   return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
339                              InputFile, OutputFile, SharedLibs, Timeout);
340 }
341
342 /// createCBE - Try to find the 'llc' executable
343 ///
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()) {
349     Message =
350       "Cannot find `llc' in executable directory or PATH!\n";
351     return 0;
352   }
353
354   Message = "Found llc: " + LLCPath.toString() + "\n";
355   GCC *gcc = GCC::create(ProgramPath, Message);
356   if (!gcc) {
357     std::cerr << Message << "\n";
358     exit(1);
359   }
360   return new CBE(LLCPath, gcc, Args);
361 }
362
363 //===---------------------------------------------------------------------===//
364 // GCC abstraction
365 //
366 int GCC::ExecuteProgram(const std::string &ProgramFile,
367                         const std::vector<std::string> &Args,
368                         FileType fileType,
369                         const std::string &InputFile,
370                         const std::string &OutputFile,
371                         const std::vector<std::string> &SharedLibs,
372                         unsigned Timeout) {
373   std::vector<const char*> GCCArgs;
374
375   GCCArgs.push_back(GCCPath.c_str());
376
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());
380
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");
386   } else {
387     GCCArgs.push_back("assembler");
388 #ifdef __APPLE__
389     GCCArgs.push_back("-force_cpusubtype_ALL");
390 #endif
391   }
392   GCCArgs.push_back(ProgramFile.c_str());  // Specify the input filename...
393   GCCArgs.push_back("-o");
394   sys::Path OutputBinary (ProgramFile+".gcc.exe");
395   OutputBinary.makeUnique();
396   GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
397   GCCArgs.push_back("-lz");
398   GCCArgs.push_back("-lm");                // Hard-code the math library...
399   GCCArgs.push_back("-x");
400   GCCArgs.push_back("none");
401   GCCArgs.push_back("/usr/local/lib/NAGWare/quickfit.o");
402   GCCArgs.push_back("-Xlinker");
403   GCCArgs.push_back("-flat_namespace");
404   GCCArgs.push_back("/usr/local/lib/NAGWare/libf97.dylib");
405   GCCArgs.push_back("/usr/local/lib/NAGWare/libf96.a");
406   GCCArgs.push_back("-O2");                // Optimize the program a bit...
407 #if defined (HAVE_LINK_R)
408   GCCArgs.push_back("-Wl,-R.");            // Search this dir for .so files
409 #endif
410   GCCArgs.push_back(0);                    // NULL terminator
411
412   std::cout << "<gcc>" << std::flush;
413   if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
414         sys::Path())) {
415     ProcessFailure(GCCPath, &GCCArgs[0]);
416     exit(1);
417   }
418
419   std::vector<const char*> ProgramArgs;
420
421   ProgramArgs.push_back(OutputBinary.c_str());
422   // Add optional parameters to the running program from Argv
423   for (unsigned i=0, e = Args.size(); i != e; ++i)
424     ProgramArgs.push_back(Args[i].c_str());
425   ProgramArgs.push_back(0);                // NULL terminator
426
427   // Now that we have a binary, run it!
428   std::cout << "<program>" << std::flush;
429   DEBUG(std::cerr << "\nAbout to run:\t";
430         for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
431           std::cerr << " " << ProgramArgs[i];
432         std::cerr << "\n";
433         );
434
435   FileRemover OutputBinaryRemover(OutputBinary);
436   return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
437       sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
438       Timeout);
439 }
440
441 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
442                           std::string &OutputFile) {
443   sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
444   uniqueFilename.makeUnique();
445   OutputFile = uniqueFilename.toString();
446
447   // Compile the C/asm file into a shared object
448   const char* GCCArgs[] = {
449     GCCPath.c_str(),
450     "-x", (fileType == AsmFile) ? "assembler" : "c",
451     "-fno-strict-aliasing",
452     InputFile.c_str(),           // Specify the input filename...
453 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
454     "-G",                        // Compile a shared library, `-G' for Sparc
455 #elif defined(__APPLE__)
456     "-single_module",            // link all source files into a single module
457     "-dynamiclib",               // `-dynamiclib' for MacOS X/PowerPC
458     "-undefined",                // in data segment, rather than generating
459     "dynamic_lookup",            // blocks. dynamic_lookup requires that you set
460                                  // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.
461 #else
462     "-shared",                   // `-shared' for Linux/X86, maybe others
463 #endif
464
465 #if defined(__ia64__) || defined(__alpha__)
466     "-fPIC",                     // IA64 requires shared objs to contain PIC
467 #endif
468     "-o", OutputFile.c_str(),    // Output to the right filename...
469     "-O2",                       // Optimize the program a bit...
470     0
471   };
472
473   std::cout << "<gcc>" << std::flush;
474   if (RunProgramWithTimeout(GCCPath, GCCArgs, sys::Path(), sys::Path(),
475                             sys::Path())) {
476     ProcessFailure(GCCPath, GCCArgs);
477     return 1;
478   }
479   return 0;
480 }
481
482 /// create - Try to find the `gcc' executable
483 ///
484 GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
485   sys::Path GCCPath = FindExecutable("gcc", ProgramPath);
486   if (GCCPath.isEmpty()) {
487     Message = "Cannot find `gcc' in executable directory or PATH!\n";
488     return 0;
489   }
490
491   Message = "Found gcc: " + GCCPath.toString() + "\n";
492   return new GCC(GCCPath);
493 }