1 //===-- ToolRunner.cpp ----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "ToolRunner.h"
16 #include "llvm/Support/Program.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/FileUtilities.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Config/config.h"   // for HAVE_LINK_R
22 #include <fstream>
23 #include <sstream>
24 using namespace llvm;
25 
26 namespace llvm {
27   cl::opt<bool>
28   SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
29 }
30 
31 namespace {
32   cl::opt<std::string>
33   RemoteClient("remote-client",
34                cl::desc("Remote execution client (rsh/ssh)"));
35 
36   cl::opt<std::string>
37   RemoteHost("remote-host",
38              cl::desc("Remote execution (rsh/ssh) host"));
39 
40   cl::opt<std::string>
41   RemotePort("remote-port",
42              cl::desc("Remote execution (rsh/ssh) port"));
43 
44   cl::opt<std::string>
45   RemoteUser("remote-user",
46              cl::desc("Remote execution (rsh/ssh) user id"));
47 
48   cl::opt<std::string>
49   RemoteExtra("remote-extra-options",
50           cl::desc("Remote execution (rsh/ssh) extra options"));
51 }
52 
53 /// RunProgramWithTimeout - This function provides an alternate interface
54 /// to the sys::Program::ExecuteAndWait interface.
55 /// @see sys::Program::ExecuteAndWait
56 static int RunProgramWithTimeout(const sys::Path &ProgramPath,
57                                  const char **Args,
58                                  const sys::Path &StdInFile,
59                                  const sys::Path &StdOutFile,
60                                  const sys::Path &StdErrFile,
61                                  unsigned NumSeconds = 0,
62                                  unsigned MemoryLimit = 0,
63                                  std::string *ErrMsg = 0) {
64   const sys::Path* redirects[3];
65   redirects[0] = &StdInFile;
66   redirects[1] = &StdOutFile;
67   redirects[2] = &StdErrFile;
68 
69 #if 0 // For debug purposes
70   {
71     errs() << "RUN:";
72     for (unsigned i = 0; Args[i]; ++i)
73       errs() << " " << Args[i];
74     errs() << "\n";
75   }
76 #endif
77 
78   return
79     sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects,
80                                  NumSeconds, MemoryLimit, ErrMsg);
81 }
82 
83 /// RunProgramRemotelyWithTimeout - This function runs the given program
84 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
85 /// Returns the remote program exit code or reports a remote client error if it
86 /// fails. Remote client is required to return 255 if it failed or program exit
87 /// code otherwise.
88 /// @see sys::Program::ExecuteAndWait
89 static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
90                                          const char **Args,
91                                          const sys::Path &StdInFile,
92                                          const sys::Path &StdOutFile,
93                                          const sys::Path &StdErrFile,
94                                          unsigned NumSeconds = 0,
95                                          unsigned MemoryLimit = 0) {
96   const sys::Path* redirects[3];
97   redirects[0] = &StdInFile;
98   redirects[1] = &StdOutFile;
99   redirects[2] = &StdErrFile;
100 
101 #if 0 // For debug purposes
102   {
103     errs() << "RUN:";
104     for (unsigned i = 0; Args[i]; ++i)
105       errs() << " " << Args[i];
106     errs() << "\n";
107   }
108 #endif
109 
110   // Run the program remotely with the remote client
111   int ReturnCode = sys::Program::ExecuteAndWait(RemoteClientPath, Args,
112                                  0, redirects, NumSeconds, MemoryLimit);
113 
114   // Has the remote client fail?
115   if (255 == ReturnCode) {
116     std::ostringstream OS;
117     OS << "\nError running remote client:\n ";
118     for (const char **Arg = Args; *Arg; ++Arg)
119       OS << " " << *Arg;
120     OS << "\n";
121 
122     // The error message is in the output file, let's print it out from there.
123     std::ifstream ErrorFile(StdOutFile.c_str());
124     if (ErrorFile) {
125       std::copy(std::istreambuf_iterator<char>(ErrorFile),
126                 std::istreambuf_iterator<char>(),
127                 std::ostreambuf_iterator<char>(OS));
128       ErrorFile.close();
129     }
130 
131     errs() << OS;
132   }
133 
134   return ReturnCode;
135 }
136 
137 static std::string ProcessFailure(sys::Path ProgPath, const char** Args,
138                                   unsigned Timeout = 0,
139                                   unsigned MemoryLimit = 0) {
140   std::ostringstream OS;
141   OS << "\nError running tool:\n ";
142   for (const char **Arg = Args; *Arg; ++Arg)
143     OS << " " << *Arg;
144   OS << "\n";
145 
146   // Rerun the compiler, capturing any error messages to print them.
147   sys::Path ErrorFilename("bugpoint.program_error_messages");
148   std::string ErrMsg;
149   if (ErrorFilename.makeUnique(true, &ErrMsg)) {
150     errs() << "Error making unique filename: " << ErrMsg << "\n";
151     exit(1);
152   }
153   RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
154                         ErrorFilename, Timeout, MemoryLimit);
155   // FIXME: check return code ?
156 
157   // Print out the error messages generated by GCC if possible...
158   std::ifstream ErrorFile(ErrorFilename.c_str());
159   if (ErrorFile) {
160     std::copy(std::istreambuf_iterator<char>(ErrorFile),
161               std::istreambuf_iterator<char>(),
162               std::ostreambuf_iterator<char>(OS));
163     ErrorFile.close();
164   }
165 
166   ErrorFilename.eraseFromDisk();
167   return OS.str();
168 }
169 
170 //===---------------------------------------------------------------------===//
171 // LLI Implementation of AbstractIntepreter interface
172 //
173 namespace {
174   class LLI : public AbstractInterpreter {
175     std::string LLIPath;          // The path to the LLI executable
176     std::vector<std::string> ToolArgs; // Args to pass to LLI
177   public:
178     LLI(const std::string &Path, const std::vector<std::string> *Args)
179       : LLIPath(Path) {
180       ToolArgs.clear ();
181       if (Args) { ToolArgs = *Args; }
182     }
183 
184     virtual int ExecuteProgram(const std::string &Bitcode,
185                                const std::vector<std::string> &Args,
186                                const std::string &InputFile,
187                                const std::string &OutputFile,
188                                std::string *Error,
189                                const std::vector<std::string> &GCCArgs,
190                                const std::vector<std::string> &SharedLibs =
191                                std::vector<std::string>(),
192                                unsigned Timeout = 0,
193                                unsigned MemoryLimit = 0);
194   };
195 }
196 
197 int LLI::ExecuteProgram(const std::string &Bitcode,
198                         const std::vector<std::string> &Args,
199                         const std::string &InputFile,
200                         const std::string &OutputFile,
201                         std::string *Error,
202                         const std::vector<std::string> &GCCArgs,
203                         const std::vector<std::string> &SharedLibs,
204                         unsigned Timeout,
205                         unsigned MemoryLimit) {
206   std::vector<const char*> LLIArgs;
207   LLIArgs.push_back(LLIPath.c_str());
208   LLIArgs.push_back("-force-interpreter=true");
209 
210   for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
211          e = SharedLibs.end(); i != e; ++i) {
212     LLIArgs.push_back("-load");
213     LLIArgs.push_back((*i).c_str());
214   }
215 
216   // Add any extra LLI args.
217   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
218     LLIArgs.push_back(ToolArgs[i].c_str());
219 
220   LLIArgs.push_back(Bitcode.c_str());
221   // Add optional parameters to the running program from Argv
222   for (unsigned i=0, e = Args.size(); i != e; ++i)
223     LLIArgs.push_back(Args[i].c_str());
224   LLIArgs.push_back(0);
225 
226   outs() << "<lli>"; outs().flush();
227   DEBUG(errs() << "\nAbout to run:\t";
228         for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
229           errs() << " " << LLIArgs[i];
230         errs() << "\n";
231         );
232   return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
233       sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
234       Timeout, MemoryLimit, Error);
235 }
236 
237 void AbstractInterpreter::anchor() { }
238 
239 // LLI create method - Try to find the LLI executable
240 AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
241                                                     std::string &Message,
242                                      const std::vector<std::string> *ToolArgs) {
243   std::string LLIPath =
244     PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI).str();
245   if (!LLIPath.empty()) {
246     Message = "Found lli: " + LLIPath + "\n";
247     return new LLI(LLIPath, ToolArgs);
248   }
249 
250   Message = "Cannot find `lli' in executable directory!\n";
251   return 0;
252 }
253 
254 //===---------------------------------------------------------------------===//
255 // Custom compiler command implementation of AbstractIntepreter interface
256 //
257 // Allows using a custom command for compiling the bitcode, thus allows, for
258 // example, to compile a bitcode fragment without linking or executing, then
259 // using a custom wrapper script to check for compiler errors.
260 namespace {
261   class CustomCompiler : public AbstractInterpreter {
262     std::string CompilerCommand;
263     std::vector<std::string> CompilerArgs;
264   public:
265     CustomCompiler(
266       const std::string &CompilerCmd, std::vector<std::string> CompArgs) :
267       CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {}
268 
269     virtual void compileProgram(const std::string &Bitcode,
270                                 std::string *Error,
271                                 unsigned Timeout = 0,
272                                 unsigned MemoryLimit = 0);
273 
274     virtual int ExecuteProgram(const std::string &Bitcode,
275                                const std::vector<std::string> &Args,
276                                const std::string &InputFile,
277                                const std::string &OutputFile,
278                                std::string *Error,
279                                const std::vector<std::string> &GCCArgs =
280                                std::vector<std::string>(),
281                                const std::vector<std::string> &SharedLibs =
282                                std::vector<std::string>(),
283                                unsigned Timeout = 0,
284                                unsigned MemoryLimit = 0) {
285       *Error = "Execution not supported with -compile-custom";
286       return -1;
287     }
288   };
289 }
290 
291 void CustomCompiler::compileProgram(const std::string &Bitcode,
292                                     std::string *Error,
293                                     unsigned Timeout,
294                                     unsigned MemoryLimit) {
295 
296   std::vector<const char*> ProgramArgs;
297   ProgramArgs.push_back(CompilerCommand.c_str());
298 
299   for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
300     ProgramArgs.push_back(CompilerArgs.at(i).c_str());
301   ProgramArgs.push_back(Bitcode.c_str());
302   ProgramArgs.push_back(0);
303 
304   // Add optional parameters to the running program from Argv
305   for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
306     ProgramArgs.push_back(CompilerArgs[i].c_str());
307 
308   if (RunProgramWithTimeout( sys::Path(CompilerCommand), &ProgramArgs[0],
309                              sys::Path(), sys::Path(), sys::Path(),
310                              Timeout, MemoryLimit, Error))
311     *Error = ProcessFailure(sys::Path(CompilerCommand), &ProgramArgs[0],
312                            Timeout, MemoryLimit);
313 }
314 
315 //===---------------------------------------------------------------------===//
316 // Custom execution command implementation of AbstractIntepreter interface
317 //
318 // Allows using a custom command for executing the bitcode, thus allows,
319 // for example, to invoke a cross compiler for code generation followed by
320 // a simulator that executes the generated binary.
321 namespace {
322   class CustomExecutor : public AbstractInterpreter {
323     std::string ExecutionCommand;
324     std::vector<std::string> ExecutorArgs;
325   public:
326     CustomExecutor(
327       const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
328       ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
329 
330     virtual int ExecuteProgram(const std::string &Bitcode,
331                                const std::vector<std::string> &Args,
332                                const std::string &InputFile,
333                                const std::string &OutputFile,
334                                std::string *Error,
335                                const std::vector<std::string> &GCCArgs,
336                                const std::vector<std::string> &SharedLibs =
337                                  std::vector<std::string>(),
338                                unsigned Timeout = 0,
339                                unsigned MemoryLimit = 0);
340   };
341 }
342 
343 int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
344                         const std::vector<std::string> &Args,
345                         const std::string &InputFile,
346                         const std::string &OutputFile,
347                         std::string *Error,
348                         const std::vector<std::string> &GCCArgs,
349                         const std::vector<std::string> &SharedLibs,
350                         unsigned Timeout,
351                         unsigned MemoryLimit) {
352 
353   std::vector<const char*> ProgramArgs;
354   ProgramArgs.push_back(ExecutionCommand.c_str());
355 
356   for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
357     ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
358   ProgramArgs.push_back(Bitcode.c_str());
359   ProgramArgs.push_back(0);
360 
361   // Add optional parameters to the running program from Argv
362   for (unsigned i = 0, e = Args.size(); i != e; ++i)
363     ProgramArgs.push_back(Args[i].c_str());
364 
365   return RunProgramWithTimeout(
366     sys::Path(ExecutionCommand),
367     &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
368     sys::Path(OutputFile), Timeout, MemoryLimit, Error);
369 }
370 
371 // Tokenize the CommandLine to the command and the args to allow
372 // defining a full command line as the command instead of just the
373 // executed program. We cannot just pass the whole string after the command
374 // as a single argument because then program sees only a single
375 // command line argument (with spaces in it: "foo bar" instead
376 // of "foo" and "bar").
377 //
378 // code borrowed from:
379 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
380 static void lexCommand(std::string &Message, const std::string &CommandLine,
381                        std::string &CmdPath, std::vector<std::string> Args) {
382 
383   std::string Command = "";
384   std::string delimiters = " ";
385 
386   std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
387   std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
388 
389   while (std::string::npos != pos || std::string::npos != lastPos) {
390     std::string token = CommandLine.substr(lastPos, pos - lastPos);
391     if (Command == "")
392        Command = token;
393     else
394        Args.push_back(token);
395     // Skip delimiters.  Note the "not_of"
396     lastPos = CommandLine.find_first_not_of(delimiters, pos);
397     // Find next "non-delimiter"
398     pos = CommandLine.find_first_of(delimiters, lastPos);
399   }
400 
401   CmdPath = sys::Program::FindProgramByName(Command).str();
402   if (CmdPath.empty()) {
403     Message =
404       std::string("Cannot find '") + Command +
405       "' in PATH!\n";
406     return;
407   }
408 
409   Message = "Found command in: " + CmdPath + "\n";
410 }
411 
412 // Custom execution environment create method, takes the execution command
413 // as arguments
414 AbstractInterpreter *AbstractInterpreter::createCustomCompiler(
415                     std::string &Message,
416                     const std::string &CompileCommandLine) {
417 
418   std::string CmdPath;
419   std::vector<std::string> Args;
420   lexCommand(Message, CompileCommandLine, CmdPath, Args);
421   if (CmdPath.empty())
422     return 0;
423 
424   return new CustomCompiler(CmdPath, Args);
425 }
426 
427 // Custom execution environment create method, takes the execution command
428 // as arguments
429 AbstractInterpreter *AbstractInterpreter::createCustomExecutor(
430                     std::string &Message,
431                     const std::string &ExecCommandLine) {
432 
433 
434   std::string CmdPath;
435   std::vector<std::string> Args;
436   lexCommand(Message, ExecCommandLine, CmdPath, Args);
437   if (CmdPath.empty())
438     return 0;
439 
440   return new CustomExecutor(CmdPath, Args);
441 }
442 
443 //===----------------------------------------------------------------------===//
444 // LLC Implementation of AbstractIntepreter interface
445 //
446 GCC::FileType LLC::OutputCode(const std::string &Bitcode,
447                               sys::Path &OutputAsmFile, std::string &Error,
448                               unsigned Timeout, unsigned MemoryLimit) {
449   const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
450   sys::Path uniqueFile(Bitcode + Suffix);
451   std::string ErrMsg;
452   if (uniqueFile.makeUnique(true, &ErrMsg)) {
453     errs() << "Error making unique filename: " << ErrMsg << "\n";
454     exit(1);
455   }
456   OutputAsmFile = uniqueFile;
457   std::vector<const char *> LLCArgs;
458   LLCArgs.push_back(LLCPath.c_str());
459 
460   // Add any extra LLC args.
461   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
462     LLCArgs.push_back(ToolArgs[i].c_str());
463 
464   LLCArgs.push_back("-o");
465   LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
466   LLCArgs.push_back(Bitcode.c_str());      // This is the input bitcode
467 
468   if (UseIntegratedAssembler)
469     LLCArgs.push_back("-filetype=obj");
470 
471   LLCArgs.push_back (0);
472 
473   outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
474   outs().flush();
475   DEBUG(errs() << "\nAbout to run:\t";
476         for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
477           errs() << " " << LLCArgs[i];
478         errs() << "\n";
479         );
480   if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
481                             sys::Path(), sys::Path(), sys::Path(),
482                             Timeout, MemoryLimit))
483     Error = ProcessFailure(sys::Path(LLCPath), &LLCArgs[0],
484                            Timeout, MemoryLimit);
485   return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
486 }
487 
488 void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
489                          unsigned Timeout, unsigned MemoryLimit) {
490   sys::Path OutputAsmFile;
491   OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
492   OutputAsmFile.eraseFromDisk();
493 }
494 
495 int LLC::ExecuteProgram(const std::string &Bitcode,
496                         const std::vector<std::string> &Args,
497                         const std::string &InputFile,
498                         const std::string &OutputFile,
499                         std::string *Error,
500                         const std::vector<std::string> &ArgsForGCC,
501                         const std::vector<std::string> &SharedLibs,
502                         unsigned Timeout,
503                         unsigned MemoryLimit) {
504 
505   sys::Path OutputAsmFile;
506   GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
507                                       MemoryLimit);
508   FileRemover OutFileRemover(OutputAsmFile.str(), !SaveTemps);
509 
510   std::vector<std::string> GCCArgs(ArgsForGCC);
511   GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
512 
513   // Assuming LLC worked, compile the result with GCC and run it.
514   return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind,
515                              InputFile, OutputFile, Error, GCCArgs,
516                              Timeout, MemoryLimit);
517 }
518 
519 /// createLLC - Try to find the LLC executable
520 ///
521 LLC *AbstractInterpreter::createLLC(const char *Argv0,
522                                     std::string &Message,
523                                     const std::string &GCCBinary,
524                                     const std::vector<std::string> *Args,
525                                     const std::vector<std::string> *GCCArgs,
526                                     bool UseIntegratedAssembler) {
527   std::string LLCPath =
528     PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC).str();
529   if (LLCPath.empty()) {
530     Message = "Cannot find `llc' in executable directory!\n";
531     return 0;
532   }
533 
534   Message = "Found llc: " + LLCPath + "\n";
535   GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
536   if (!gcc) {
537     errs() << Message << "\n";
538     exit(1);
539   }
540   return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler);
541 }
542 
543 //===---------------------------------------------------------------------===//
544 // JIT Implementation of AbstractIntepreter interface
545 //
546 namespace {
547   class JIT : public AbstractInterpreter {
548     std::string LLIPath;          // The path to the LLI executable
549     std::vector<std::string> ToolArgs; // Args to pass to LLI
550   public:
551     JIT(const std::string &Path, const std::vector<std::string> *Args)
552       : LLIPath(Path) {
553       ToolArgs.clear ();
554       if (Args) { ToolArgs = *Args; }
555     }
556 
557     virtual int ExecuteProgram(const std::string &Bitcode,
558                                const std::vector<std::string> &Args,
559                                const std::string &InputFile,
560                                const std::string &OutputFile,
561                                std::string *Error,
562                                const std::vector<std::string> &GCCArgs =
563                                  std::vector<std::string>(),
564                                const std::vector<std::string> &SharedLibs =
565                                  std::vector<std::string>(),
566                                unsigned Timeout = 0,
567                                unsigned MemoryLimit = 0);
568   };
569 }
570 
571 int JIT::ExecuteProgram(const std::string &Bitcode,
572                         const std::vector<std::string> &Args,
573                         const std::string &InputFile,
574                         const std::string &OutputFile,
575                         std::string *Error,
576                         const std::vector<std::string> &GCCArgs,
577                         const std::vector<std::string> &SharedLibs,
578                         unsigned Timeout,
579                         unsigned MemoryLimit) {
580   // Construct a vector of parameters, incorporating those from the command-line
581   std::vector<const char*> JITArgs;
582   JITArgs.push_back(LLIPath.c_str());
583   JITArgs.push_back("-force-interpreter=false");
584 
585   // Add any extra LLI args.
586   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
587     JITArgs.push_back(ToolArgs[i].c_str());
588 
589   for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
590     JITArgs.push_back("-load");
591     JITArgs.push_back(SharedLibs[i].c_str());
592   }
593   JITArgs.push_back(Bitcode.c_str());
594   // Add optional parameters to the running program from Argv
595   for (unsigned i=0, e = Args.size(); i != e; ++i)
596     JITArgs.push_back(Args[i].c_str());
597   JITArgs.push_back(0);
598 
599   outs() << "<jit>"; outs().flush();
600   DEBUG(errs() << "\nAbout to run:\t";
601         for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
602           errs() << " " << JITArgs[i];
603         errs() << "\n";
604         );
605   DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
606   return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
607       sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
608       Timeout, MemoryLimit, Error);
609 }
610 
611 /// createJIT - Try to find the LLI executable
612 ///
613 AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
614                    std::string &Message, const std::vector<std::string> *Args) {
615   std::string LLIPath =
616     PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT).str();
617   if (!LLIPath.empty()) {
618     Message = "Found lli: " + LLIPath + "\n";
619     return new JIT(LLIPath, Args);
620   }
621 
622   Message = "Cannot find `lli' in executable directory!\n";
623   return 0;
624 }
625 
626 GCC::FileType CBE::OutputCode(const std::string &Bitcode,
627                               sys::Path &OutputCFile, std::string &Error,
628                               unsigned Timeout, unsigned MemoryLimit) {
629   sys::Path uniqueFile(Bitcode+".cbe.c");
630   std::string ErrMsg;
631   if (uniqueFile.makeUnique(true, &ErrMsg)) {
632     errs() << "Error making unique filename: " << ErrMsg << "\n";
633     exit(1);
634   }
635   OutputCFile = uniqueFile;
636   std::vector<const char *> LLCArgs;
637   LLCArgs.push_back(LLCPath.c_str());
638 
639   // Add any extra LLC args.
640   for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
641     LLCArgs.push_back(ToolArgs[i].c_str());
642 
643   LLCArgs.push_back("-o");
644   LLCArgs.push_back(OutputCFile.c_str());   // Output to the C file
645   LLCArgs.push_back("-march=c");            // Output C language
646   LLCArgs.push_back(Bitcode.c_str());      // This is the input bitcode
647   LLCArgs.push_back(0);
648 
649   outs() << "<cbe>"; outs().flush();
650   DEBUG(errs() << "\nAbout to run:\t";
651         for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
652           errs() << " " << LLCArgs[i];
653         errs() << "\n";
654         );
655   if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
656                             sys::Path(), Timeout, MemoryLimit))
657     Error = ProcessFailure(LLCPath, &LLCArgs[0], Timeout, MemoryLimit);
658   return GCC::CFile;
659 }
660 
661 void CBE::compileProgram(const std::string &Bitcode, std::string *Error,
662                          unsigned Timeout, unsigned MemoryLimit) {
663   sys::Path OutputCFile;
664   OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit);
665   OutputCFile.eraseFromDisk();
666 }
667 
668 int CBE::ExecuteProgram(const std::string &Bitcode,
669                         const std::vector<std::string> &Args,
670                         const std::string &InputFile,
671                         const std::string &OutputFile,
672                         std::string *Error,
673                         const std::vector<std::string> &ArgsForGCC,
674                         const std::vector<std::string> &SharedLibs,
675                         unsigned Timeout,
676                         unsigned MemoryLimit) {
677   sys::Path OutputCFile;
678   OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit);
679 
680   FileRemover CFileRemove(OutputCFile.str(), !SaveTemps);
681 
682   std::vector<std::string> GCCArgs(ArgsForGCC);
683   GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
684 
685   return gcc->ExecuteProgram(OutputCFile.str(), Args, GCC::CFile,
686                              InputFile, OutputFile, Error, GCCArgs,
687                              Timeout, MemoryLimit);
688 }
689 
690 /// createCBE - Try to find the 'llc' executable
691 ///
692 CBE *AbstractInterpreter::createCBE(const char *Argv0,
693                                     std::string &Message,
694                                     const std::string &GCCBinary,
695                                     const std::vector<std::string> *Args,
696                                     const std::vector<std::string> *GCCArgs) {
697   sys::Path LLCPath =
698     PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createCBE);
699   if (LLCPath.isEmpty()) {
700     Message =
701       "Cannot find `llc' in executable directory!\n";
702     return 0;
703   }
704 
705   Message = "Found llc: " + LLCPath.str() + "\n";
706   GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
707   if (!gcc) {
708     errs() << Message << "\n";
709     exit(1);
710   }
711   return new CBE(LLCPath, gcc, Args);
712 }
713 
714 //===---------------------------------------------------------------------===//
715 // GCC abstraction
716 //
717 
718 static bool IsARMArchitecture(std::vector<const char*> Args) {
719   for (std::vector<const char*>::const_iterator
720          I = Args.begin(), E = Args.end(); I != E; ++I) {
721     if (StringRef(*I).equals_lower("-arch")) {
722       ++I;
723       if (I != E && StringRef(*I).substr(0, strlen("arm")).equals_lower("arm"))
724         return true;
725     }
726   }
727 
728   return false;
729 }
730 
731 int GCC::ExecuteProgram(const std::string &ProgramFile,
732                         const std::vector<std::string> &Args,
733                         FileType fileType,
734                         const std::string &InputFile,
735                         const std::string &OutputFile,
736                         std::string *Error,
737                         const std::vector<std::string> &ArgsForGCC,
738                         unsigned Timeout,
739                         unsigned MemoryLimit) {
740   std::vector<const char*> GCCArgs;
741 
742   GCCArgs.push_back(GCCPath.c_str());
743 
744   if (TargetTriple.getArch() == Triple::x86)
745     GCCArgs.push_back("-m32");
746 
747   for (std::vector<std::string>::const_iterator
748          I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
749     GCCArgs.push_back(I->c_str());
750 
751   // Specify -x explicitly in case the extension is wonky
752   if (fileType != ObjectFile) {
753     GCCArgs.push_back("-x");
754     if (fileType == CFile) {
755       GCCArgs.push_back("c");
756       GCCArgs.push_back("-fno-strict-aliasing");
757     } else {
758       GCCArgs.push_back("assembler");
759 
760       // For ARM architectures we don't want this flag. bugpoint isn't
761       // explicitly told what architecture it is working on, so we get
762       // it from gcc flags
763       if (TargetTriple.isOSDarwin() && !IsARMArchitecture(GCCArgs))
764         GCCArgs.push_back("-force_cpusubtype_ALL");
765     }
766   }
767 
768   GCCArgs.push_back(ProgramFile.c_str());  // Specify the input filename.
769 
770   GCCArgs.push_back("-x");
771   GCCArgs.push_back("none");
772   GCCArgs.push_back("-o");
773   sys::Path OutputBinary (ProgramFile+".gcc.exe");
774   std::string ErrMsg;
775   if (OutputBinary.makeUnique(true, &ErrMsg)) {
776     errs() << "Error making unique filename: " << ErrMsg << "\n";
777     exit(1);
778   }
779   GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
780 
781   // Add any arguments intended for GCC. We locate them here because this is
782   // most likely -L and -l options that need to come before other libraries but
783   // after the source. Other options won't be sensitive to placement on the
784   // command line, so this should be safe.
785   for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
786     GCCArgs.push_back(ArgsForGCC[i].c_str());
787 
788   GCCArgs.push_back("-lm");                // Hard-code the math library...
789   GCCArgs.push_back("-O2");                // Optimize the program a bit...
790 #if defined (HAVE_LINK_R)
791   GCCArgs.push_back("-Wl,-R.");            // Search this dir for .so files
792 #endif
793   if (TargetTriple.getArch() == Triple::sparc)
794     GCCArgs.push_back("-mcpu=v9");
795   GCCArgs.push_back(0);                    // NULL terminator
796 
797   outs() << "<gcc>"; outs().flush();
798   DEBUG(errs() << "\nAbout to run:\t";
799         for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
800           errs() << " " << GCCArgs[i];
801         errs() << "\n";
802         );
803   if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
804         sys::Path())) {
805     *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
806     return -1;
807   }
808 
809   std::vector<const char*> ProgramArgs;
810 
811   // Declared here so that the destructor only runs after
812   // ProgramArgs is used.
813   std::string Exec;
814 
815   if (RemoteClientPath.isEmpty())
816     ProgramArgs.push_back(OutputBinary.c_str());
817   else {
818     ProgramArgs.push_back(RemoteClientPath.c_str());
819     ProgramArgs.push_back(RemoteHost.c_str());
820     if (!RemoteUser.empty()) {
821       ProgramArgs.push_back("-l");
822       ProgramArgs.push_back(RemoteUser.c_str());
823     }
824     if (!RemotePort.empty()) {
825       ProgramArgs.push_back("-p");
826       ProgramArgs.push_back(RemotePort.c_str());
827     }
828     if (!RemoteExtra.empty()) {
829       ProgramArgs.push_back(RemoteExtra.c_str());
830     }
831 
832     // Full path to the binary. We need to cd to the exec directory because
833     // there is a dylib there that the exec expects to find in the CWD
834     char* env_pwd = getenv("PWD");
835     Exec = "cd ";
836     Exec += env_pwd;
837     Exec += "; ./";
838     Exec += OutputBinary.c_str();
839     ProgramArgs.push_back(Exec.c_str());
840   }
841 
842   // Add optional parameters to the running program from Argv
843   for (unsigned i = 0, e = Args.size(); i != e; ++i)
844     ProgramArgs.push_back(Args[i].c_str());
845   ProgramArgs.push_back(0);                // NULL terminator
846 
847   // Now that we have a binary, run it!
848   outs() << "<program>"; outs().flush();
849   DEBUG(errs() << "\nAbout to run:\t";
850         for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
851           errs() << " " << ProgramArgs[i];
852         errs() << "\n";
853         );
854 
855   FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
856 
857   if (RemoteClientPath.isEmpty()) {
858     DEBUG(errs() << "<run locally>");
859     int ExitCode = RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
860         sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
861         Timeout, MemoryLimit, Error);
862     // Treat a signal (usually SIGSEGV) or timeout as part of the program output
863     // so that crash-causing miscompilation is handled seamlessly.
864     if (ExitCode < -1) {
865       std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
866       outFile << *Error << '\n';
867       outFile.close();
868       Error->clear();
869     }
870     return ExitCode;
871   } else {
872     outs() << "<run remotely>"; outs().flush();
873     return RunProgramRemotelyWithTimeout(sys::Path(RemoteClientPath),
874         &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
875         sys::Path(OutputFile), Timeout, MemoryLimit);
876   }
877 }
878 
879 int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
880                           std::string &OutputFile,
881                           const std::vector<std::string> &ArgsForGCC,
882                           std::string &Error) {
883   sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
884   std::string ErrMsg;
885   if (uniqueFilename.makeUnique(true, &ErrMsg)) {
886     errs() << "Error making unique filename: " << ErrMsg << "\n";
887     exit(1);
888   }
889   OutputFile = uniqueFilename.str();
890 
891   std::vector<const char*> GCCArgs;
892 
893   GCCArgs.push_back(GCCPath.c_str());
894 
895   if (TargetTriple.getArch() == Triple::x86)
896     GCCArgs.push_back("-m32");
897 
898   for (std::vector<std::string>::const_iterator
899          I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
900     GCCArgs.push_back(I->c_str());
901 
902   // Compile the C/asm file into a shared object
903   if (fileType != ObjectFile) {
904     GCCArgs.push_back("-x");
905     GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
906   }
907   GCCArgs.push_back("-fno-strict-aliasing");
908   GCCArgs.push_back(InputFile.c_str());   // Specify the input filename.
909   GCCArgs.push_back("-x");
910   GCCArgs.push_back("none");
911   if (TargetTriple.getArch() == Triple::sparc)
912     GCCArgs.push_back("-G");       // Compile a shared library, `-G' for Sparc
913   else if (TargetTriple.isOSDarwin()) {
914     // link all source files into a single module in data segment, rather than
915     // generating blocks. dynamic_lookup requires that you set
916     // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.  FIXME: it would be better for
917     // bugpoint to just pass that in the environment of GCC.
918     GCCArgs.push_back("-single_module");
919     GCCArgs.push_back("-dynamiclib");   // `-dynamiclib' for MacOS X/PowerPC
920     GCCArgs.push_back("-undefined");
921     GCCArgs.push_back("dynamic_lookup");
922   } else
923     GCCArgs.push_back("-shared");  // `-shared' for Linux/X86, maybe others
924 
925   if (TargetTriple.getArch() == Triple::x86_64)
926     GCCArgs.push_back("-fPIC");   // Requires shared objs to contain PIC
927 
928   if (TargetTriple.getArch() == Triple::sparc)
929     GCCArgs.push_back("-mcpu=v9");
930 
931   GCCArgs.push_back("-o");
932   GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
933   GCCArgs.push_back("-O2");              // Optimize the program a bit.
934 
935 
936 
937   // Add any arguments intended for GCC. We locate them here because this is
938   // most likely -L and -l options that need to come before other libraries but
939   // after the source. Other options won't be sensitive to placement on the
940   // command line, so this should be safe.
941   for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
942     GCCArgs.push_back(ArgsForGCC[i].c_str());
943   GCCArgs.push_back(0);                    // NULL terminator
944 
945 
946 
947   outs() << "<gcc>"; outs().flush();
948   DEBUG(errs() << "\nAbout to run:\t";
949         for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
950           errs() << " " << GCCArgs[i];
951         errs() << "\n";
952         );
953   if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
954                             sys::Path())) {
955     Error = ProcessFailure(GCCPath, &GCCArgs[0]);
956     return 1;
957   }
958   return 0;
959 }
960 
961 /// create - Try to find the `gcc' executable
962 ///
963 GCC *GCC::create(std::string &Message,
964                  const std::string &GCCBinary,
965                  const std::vector<std::string> *Args) {
966   sys::Path GCCPath = sys::Program::FindProgramByName(GCCBinary);
967   if (GCCPath.isEmpty()) {
968     Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
969     return 0;
970   }
971 
972   sys::Path RemoteClientPath;
973   if (!RemoteClient.empty())
974     RemoteClientPath = sys::Program::FindProgramByName(RemoteClient);
975 
976   Message = "Found gcc: " + GCCPath.str() + "\n";
977   return new GCC(GCCPath, RemoteClientPath, Args);
978 }
979