ToolRunner.cpp revision 231919
1167598Srrs//===-- ToolRunner.cpp ----------------------------------------------------===//
2169382Srrs//
3235828Stuexen//                     The LLVM Compiler Infrastructure
4235828Stuexen//
5167598Srrs// This file is distributed under the University of Illinois Open Source
6167598Srrs// License. See LICENSE.TXT for details.
7167598Srrs//
8167598Srrs//===----------------------------------------------------------------------===//
9167598Srrs//
10228653Stuexen// This file implements the interfaces described in the ToolRunner.h file.
11167598Srrs//
12167598Srrs//===----------------------------------------------------------------------===//
13167598Srrs
14228653Stuexen#define DEBUG_TYPE "toolrunner"
15167598Srrs#include "ToolRunner.h"
16167598Srrs#include "llvm/Support/Program.h"
17167598Srrs#include "llvm/Support/CommandLine.h"
18167598Srrs#include "llvm/Support/Debug.h"
19167598Srrs#include "llvm/Support/FileUtilities.h"
20167598Srrs#include "llvm/Support/raw_ostream.h"
21167598Srrs#include "llvm/Config/config.h"   // for HAVE_LINK_R
22167598Srrs#include <fstream>
23167598Srrs#include <sstream>
24167598Srrsusing namespace llvm;
25167598Srrs
26167598Srrsnamespace llvm {
27167598Srrs  cl::opt<bool>
28167598Srrs  SaveTemps("save-temps", cl::init(false), cl::desc("Save temporary files"));
29167598Srrs}
30167598Srrs
31167598Srrsnamespace {
32167598Srrs  cl::opt<std::string>
33167598Srrs  RemoteClient("remote-client",
34167598Srrs               cl::desc("Remote execution client (rsh/ssh)"));
35167598Srrs
36235828Stuexen  cl::opt<std::string>
37235828Stuexen  RemoteHost("remote-host",
38167598Srrs             cl::desc("Remote execution (rsh/ssh) host"));
39167598Srrs
40167598Srrs  cl::opt<std::string>
41167598Srrs  RemotePort("remote-port",
42179783Srrs             cl::desc("Remote execution (rsh/ssh) port"));
43179783Srrs
44179783Srrs  cl::opt<std::string>
45179783Srrs  RemoteUser("remote-user",
46179783Srrs             cl::desc("Remote execution (rsh/ssh) user id"));
47179783Srrs
48218186Srrs  cl::opt<std::string>
49179783Srrs  RemoteExtra("remote-extra-options",
50211969Stuexen          cl::desc("Remote execution (rsh/ssh) extra options"));
51179783Srrs}
52211969Stuexen
53179783Srrs/// RunProgramWithTimeout - This function provides an alternate interface
54179783Srrs/// to the sys::Program::ExecuteAndWait interface.
55179783Srrs/// @see sys::Program::ExecuteAndWait
56179783Srrsstatic int RunProgramWithTimeout(const sys::Path &ProgramPath,
57179783Srrs                                 const char **Args,
58179783Srrs                                 const sys::Path &StdInFile,
59179783Srrs                                 const sys::Path &StdOutFile,
60179783Srrs                                 const sys::Path &StdErrFile,
61179783Srrs                                 unsigned NumSeconds = 0,
62179783Srrs                                 unsigned MemoryLimit = 0,
63179783Srrs                                 std::string *ErrMsg = 0) {
64179783Srrs  const sys::Path* redirects[3];
65179783Srrs  redirects[0] = &StdInFile;
66179783Srrs  redirects[1] = &StdOutFile;
67179783Srrs  redirects[2] = &StdErrFile;
68179783Srrs
69179783Srrs#if 0 // For debug purposes
70179783Srrs  {
71179783Srrs    errs() << "RUN:";
72179783Srrs    for (unsigned i = 0; Args[i]; ++i)
73179783Srrs      errs() << " " << Args[i];
74179783Srrs    errs() << "\n";
75179783Srrs  }
76224641Stuexen#endif
77179783Srrs
78179783Srrs  return
79179783Srrs    sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects,
80179783Srrs                                 NumSeconds, MemoryLimit, ErrMsg);
81190689Srrs}
82185694Srrs
83179783Srrs/// RunProgramRemotelyWithTimeout - This function runs the given program
84179783Srrs/// remotely using the given remote client and the sys::Program::ExecuteAndWait.
85179783Srrs/// Returns the remote program exit code or reports a remote client error if it
86179783Srrs/// fails. Remote client is required to return 255 if it failed or program exit
87179783Srrs/// code otherwise.
88179783Srrs/// @see sys::Program::ExecuteAndWait
89179783Srrsstatic int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
90179783Srrs                                         const char **Args,
91179783Srrs                                         const sys::Path &StdInFile,
92179783Srrs                                         const sys::Path &StdOutFile,
93179783Srrs                                         const sys::Path &StdErrFile,
94179783Srrs                                         unsigned NumSeconds = 0,
95179783Srrs                                         unsigned MemoryLimit = 0) {
96179783Srrs  const sys::Path* redirects[3];
97179783Srrs  redirects[0] = &StdInFile;
98217760Stuexen  redirects[1] = &StdOutFile;
99217760Stuexen  redirects[2] = &StdErrFile;
100179783Srrs
101179783Srrs#if 0 // For debug purposes
102179783Srrs  {
103185694Srrs    errs() << "RUN:";
104219057Srrs    for (unsigned i = 0; Args[i]; ++i)
105219057Srrs      errs() << " " << Args[i];
106219057Srrs    errs() << "\n";
107219397Srrs  }
108219397Srrs#endif
109179783Srrs
110179783Srrs  // Run the program remotely with the remote client
111179783Srrs  int ReturnCode = sys::Program::ExecuteAndWait(RemoteClientPath, Args,
112179783Srrs                                 0, redirects, NumSeconds, MemoryLimit);
113179783Srrs
114193090Srrs  // Has the remote client fail?
115212799Stuexen  if (255 == ReturnCode) {
116212800Stuexen    std::ostringstream OS;
117229805Stuexen    OS << "\nError running remote client:\n ";
118179783Srrs    for (const char **Arg = Args; *Arg; ++Arg)
119179783Srrs      OS << " " << *Arg;
120179783Srrs    OS << "\n";
121179783Srrs
122179783Srrs    // The error message is in the output file, let's print it out from there.
123179783Srrs    std::ifstream ErrorFile(StdOutFile.c_str());
124179783Srrs    if (ErrorFile) {
125179783Srrs      std::copy(std::istreambuf_iterator<char>(ErrorFile),
126167598Srrs                std::istreambuf_iterator<char>(),
127167598Srrs                std::ostreambuf_iterator<char>(OS));
128167598Srrs      ErrorFile.close();
129167598Srrs    }
130167598Srrs
131167598Srrs    errs() << OS;
132167598Srrs  }
133167598Srrs
134167598Srrs  return ReturnCode;
135167598Srrs}
136167598Srrs
137167598Srrsstatic std::string ProcessFailure(sys::Path ProgPath, const char** Args,
138167598Srrs                                  unsigned Timeout = 0,
139167598Srrs                                  unsigned MemoryLimit = 0) {
140167598Srrs  std::ostringstream OS;
141167598Srrs  OS << "\nError running tool:\n ";
142167598Srrs  for (const char **Arg = Args; *Arg; ++Arg)
143167598Srrs    OS << " " << *Arg;
144167598Srrs  OS << "\n";
145167598Srrs
146167598Srrs  // Rerun the compiler, capturing any error messages to print them.
147179157Srrs  sys::Path ErrorFilename("bugpoint.program_error_messages");
148179157Srrs  std::string ErrMsg;
149179157Srrs  if (ErrorFilename.makeUnique(true, &ErrMsg)) {
150179157Srrs    errs() << "Error making unique filename: " << ErrMsg << "\n";
151179157Srrs    exit(1);
152179157Srrs  }
153167598Srrs  RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
154167598Srrs                        ErrorFilename, Timeout, MemoryLimit);
155167598Srrs  // FIXME: check return code ?
156167598Srrs
157167598Srrs  // Print out the error messages generated by GCC if possible...
158167598Srrs  std::ifstream ErrorFile(ErrorFilename.c_str());
159167598Srrs  if (ErrorFile) {
160167598Srrs    std::copy(std::istreambuf_iterator<char>(ErrorFile),
161167598Srrs              std::istreambuf_iterator<char>(),
162167598Srrs              std::ostreambuf_iterator<char>(OS));
163182367Srrs    ErrorFile.close();
164167598Srrs  }
165167598Srrs
166167598Srrs  ErrorFilename.eraseFromDisk();
167167598Srrs  return OS.str();
168167598Srrs}
169167598Srrs
170167598Srrs//===---------------------------------------------------------------------===//
171167598Srrs// LLI Implementation of AbstractIntepreter interface
172167598Srrs//
173167598Srrsnamespace {
174167598Srrs  class LLI : public AbstractInterpreter {
175170642Srrs    std::string LLIPath;          // The path to the LLI executable
176167598Srrs    std::vector<std::string> ToolArgs; // Args to pass to LLI
177167598Srrs  public:
178167598Srrs    LLI(const std::string &Path, const std::vector<std::string> *Args)
179217894Stuexen      : LLIPath(Path) {
180167598Srrs      ToolArgs.clear ();
181167598Srrs      if (Args) { ToolArgs = *Args; }
182167598Srrs    }
183218186Srrs
184218186Srrs    virtual int ExecuteProgram(const std::string &Bitcode,
185218186Srrs                               const std::vector<std::string> &Args,
186218186Srrs                               const std::string &InputFile,
187218186Srrs                               const std::string &OutputFile,
188218186Srrs                               std::string *Error,
189218186Srrs                               const std::vector<std::string> &GCCArgs,
190167598Srrs                               const std::vector<std::string> &SharedLibs =
191167598Srrs                               std::vector<std::string>(),
192167598Srrs                               unsigned Timeout = 0,
193167598Srrs                               unsigned MemoryLimit = 0);
194167598Srrs  };
195167598Srrs}
196218264Sbrucec
197167598Srrsint LLI::ExecuteProgram(const std::string &Bitcode,
198167598Srrs                        const std::vector<std::string> &Args,
199167598Srrs                        const std::string &InputFile,
200167598Srrs                        const std::string &OutputFile,
201167598Srrs                        std::string *Error,
202218264Sbrucec                        const std::vector<std::string> &GCCArgs,
203167598Srrs                        const std::vector<std::string> &SharedLibs,
204167598Srrs                        unsigned Timeout,
205167598Srrs                        unsigned MemoryLimit) {
206167598Srrs  std::vector<const char*> LLIArgs;
207167598Srrs  LLIArgs.push_back(LLIPath.c_str());
208167598Srrs  LLIArgs.push_back("-force-interpreter=true");
209167598Srrs
210167598Srrs  for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
211167598Srrs         e = SharedLibs.end(); i != e; ++i) {
212167598Srrs    LLIArgs.push_back("-load");
213167598Srrs    LLIArgs.push_back((*i).c_str());
214218264Sbrucec  }
215218264Sbrucec
216167598Srrs  // Add any extra LLI args.
217167598Srrs  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
218167598Srrs    LLIArgs.push_back(ToolArgs[i].c_str());
219167598Srrs
220219397Srrs  LLIArgs.push_back(Bitcode.c_str());
221219397Srrs  // Add optional parameters to the running program from Argv
222167598Srrs  for (unsigned i=0, e = Args.size(); i != e; ++i)
223167598Srrs    LLIArgs.push_back(Args[i].c_str());
224167598Srrs  LLIArgs.push_back(0);
225167598Srrs
226167598Srrs  outs() << "<lli>"; outs().flush();
227167598Srrs  DEBUG(errs() << "\nAbout to run:\t";
228167598Srrs        for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
229167598Srrs          errs() << " " << LLIArgs[i];
230167598Srrs        errs() << "\n";
231167598Srrs        );
232167598Srrs  return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
233167598Srrs      sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
234167598Srrs      Timeout, MemoryLimit, Error);
235167598Srrs}
236167598Srrs
237167598Srrs// LLI create method - Try to find the LLI executable
238167598SrrsAbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
239167598Srrs                                                    std::string &Message,
240167598Srrs                                     const std::vector<std::string> *ToolArgs) {
241167598Srrs  std::string LLIPath =
242167598Srrs    PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI).str();
243167598Srrs  if (!LLIPath.empty()) {
244219397Srrs    Message = "Found lli: " + LLIPath + "\n";
245219397Srrs    return new LLI(LLIPath, ToolArgs);
246167598Srrs  }
247167598Srrs
248167598Srrs  Message = "Cannot find `lli' in executable directory!\n";
249167598Srrs  return 0;
250219397Srrs}
251219397Srrs
252167598Srrs//===---------------------------------------------------------------------===//
253167598Srrs// Custom compiler command implementation of AbstractIntepreter interface
254167598Srrs//
255167598Srrs// Allows using a custom command for compiling the bitcode, thus allows, for
256219397Srrs// example, to compile a bitcode fragment without linking or executing, then
257219397Srrs// using a custom wrapper script to check for compiler errors.
258167598Srrsnamespace {
259167598Srrs  class CustomCompiler : public AbstractInterpreter {
260167598Srrs    std::string CompilerCommand;
261167598Srrs    std::vector<std::string> CompilerArgs;
262219397Srrs  public:
263219397Srrs    CustomCompiler(
264167598Srrs      const std::string &CompilerCmd, std::vector<std::string> CompArgs) :
265167598Srrs      CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {}
266167598Srrs
267167598Srrs    virtual void compileProgram(const std::string &Bitcode,
268219397Srrs                                std::string *Error,
269219397Srrs                                unsigned Timeout = 0,
270167598Srrs                                unsigned MemoryLimit = 0);
271167598Srrs
272167598Srrs    virtual int ExecuteProgram(const std::string &Bitcode,
273167598Srrs                               const std::vector<std::string> &Args,
274219397Srrs                               const std::string &InputFile,
275219397Srrs                               const std::string &OutputFile,
276167598Srrs                               std::string *Error,
277167598Srrs                               const std::vector<std::string> &GCCArgs =
278167598Srrs                               std::vector<std::string>(),
279167598Srrs                               const std::vector<std::string> &SharedLibs =
280219397Srrs                               std::vector<std::string>(),
281219397Srrs                               unsigned Timeout = 0,
282167598Srrs                               unsigned MemoryLimit = 0) {
283167598Srrs      *Error = "Execution not supported with -compile-custom";
284167598Srrs      return -1;
285167598Srrs    }
286219397Srrs  };
287219397Srrs}
288167598Srrs
289167598Srrsvoid CustomCompiler::compileProgram(const std::string &Bitcode,
290167598Srrs                                    std::string *Error,
291167598Srrs                                    unsigned Timeout,
292167598Srrs                                    unsigned MemoryLimit) {
293219397Srrs
294167598Srrs  std::vector<const char*> ProgramArgs;
295167598Srrs  ProgramArgs.push_back(CompilerCommand.c_str());
296167598Srrs
297167598Srrs  for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
298167598Srrs    ProgramArgs.push_back(CompilerArgs.at(i).c_str());
299167598Srrs  ProgramArgs.push_back(Bitcode.c_str());
300167598Srrs  ProgramArgs.push_back(0);
301167598Srrs
302167598Srrs  // Add optional parameters to the running program from Argv
303167598Srrs  for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
304167598Srrs    ProgramArgs.push_back(CompilerArgs[i].c_str());
305167598Srrs
306167598Srrs  if (RunProgramWithTimeout( sys::Path(CompilerCommand), &ProgramArgs[0],
307167598Srrs                             sys::Path(), sys::Path(), sys::Path(),
308167598Srrs                             Timeout, MemoryLimit, Error))
309167598Srrs    *Error = ProcessFailure(sys::Path(CompilerCommand), &ProgramArgs[0],
310167598Srrs                           Timeout, MemoryLimit);
311167598Srrs}
312167598Srrs
313167598Srrs//===---------------------------------------------------------------------===//
314167598Srrs// Custom execution command implementation of AbstractIntepreter interface
315167598Srrs//
316224641Stuexen// Allows using a custom command for executing the bitcode, thus allows,
317224641Stuexen// for example, to invoke a cross compiler for code generation followed by
318224641Stuexen// a simulator that executes the generated binary.
319224641Stuexennamespace {
320224641Stuexen  class CustomExecutor : public AbstractInterpreter {
321224641Stuexen    std::string ExecutionCommand;
322219397Srrs    std::vector<std::string> ExecutorArgs;
323219397Srrs  public:
324167598Srrs    CustomExecutor(
325167598Srrs      const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
326167598Srrs      ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
327167598Srrs
328167598Srrs    virtual int ExecuteProgram(const std::string &Bitcode,
329167598Srrs                               const std::vector<std::string> &Args,
330167598Srrs                               const std::string &InputFile,
331167598Srrs                               const std::string &OutputFile,
332167598Srrs                               std::string *Error,
333167598Srrs                               const std::vector<std::string> &GCCArgs,
334167598Srrs                               const std::vector<std::string> &SharedLibs =
335216669Stuexen                                 std::vector<std::string>(),
336221460Stuexen                               unsigned Timeout = 0,
337221460Stuexen                               unsigned MemoryLimit = 0);
338221460Stuexen  };
339167598Srrs}
340185694Srrs
341185694Srrsint CustomExecutor::ExecuteProgram(const std::string &Bitcode,
342185694Srrs                        const std::vector<std::string> &Args,
343185694Srrs                        const std::string &InputFile,
344185694Srrs                        const std::string &OutputFile,
345185694Srrs                        std::string *Error,
346172091Srrs                        const std::vector<std::string> &GCCArgs,
347172091Srrs                        const std::vector<std::string> &SharedLibs,
348172091Srrs                        unsigned Timeout,
349172091Srrs                        unsigned MemoryLimit) {
350172091Srrs
351172091Srrs  std::vector<const char*> ProgramArgs;
352167598Srrs  ProgramArgs.push_back(ExecutionCommand.c_str());
353167598Srrs
354167598Srrs  for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
355167598Srrs    ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
356167598Srrs  ProgramArgs.push_back(Bitcode.c_str());
357167598Srrs  ProgramArgs.push_back(0);
358167598Srrs
359167598Srrs  // Add optional parameters to the running program from Argv
360167598Srrs  for (unsigned i = 0, e = Args.size(); i != e; ++i)
361167598Srrs    ProgramArgs.push_back(Args[i].c_str());
362167598Srrs
363167598Srrs  return RunProgramWithTimeout(
364167598Srrs    sys::Path(ExecutionCommand),
365167598Srrs    &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
366167598Srrs    sys::Path(OutputFile), Timeout, MemoryLimit, Error);
367167598Srrs}
368167598Srrs
369167598Srrs// Tokenize the CommandLine to the command and the args to allow
370167598Srrs// defining a full command line as the command instead of just the
371167598Srrs// executed program. We cannot just pass the whole string after the command
372167598Srrs// as a single argument because then program sees only a single
373167598Srrs// command line argument (with spaces in it: "foo bar" instead
374167598Srrs// of "foo" and "bar").
375167598Srrs//
376167598Srrs// code borrowed from:
377167598Srrs// http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
378167598Srrsstatic void lexCommand(std::string &Message, const std::string &CommandLine,
379167598Srrs                       std::string &CmdPath, std::vector<std::string> Args) {
380235557Stuexen
381167598Srrs  std::string Command = "";
382167598Srrs  std::string delimiters = " ";
383167598Srrs
384167598Srrs  std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
385167598Srrs  std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
386167598Srrs
387167598Srrs  while (std::string::npos != pos || std::string::npos != lastPos) {
388167598Srrs    std::string token = CommandLine.substr(lastPos, pos - lastPos);
389167598Srrs    if (Command == "")
390167598Srrs       Command = token;
391167598Srrs    else
392167598Srrs       Args.push_back(token);
393167598Srrs    // Skip delimiters.  Note the "not_of"
394167598Srrs    lastPos = CommandLine.find_first_not_of(delimiters, pos);
395172091Srrs    // Find next "non-delimiter"
396167598Srrs    pos = CommandLine.find_first_of(delimiters, lastPos);
397167598Srrs  }
398218186Srrs
399167598Srrs  CmdPath = sys::Program::FindProgramByName(Command).str();
400167598Srrs  if (CmdPath.empty()) {
401167598Srrs    Message =
402167598Srrs      std::string("Cannot find '") + Command +
403167598Srrs      "' in PATH!\n";
404167598Srrs    return;
405167598Srrs  }
406167598Srrs
407167598Srrs  Message = "Found command in: " + CmdPath + "\n";
408167598Srrs}
409167598Srrs
410167598Srrs// Custom execution environment create method, takes the execution command
411167598Srrs// as arguments
412168299SrrsAbstractInterpreter *AbstractInterpreter::createCustomCompiler(
413168124Srrs                    std::string &Message,
414168124Srrs                    const std::string &CompileCommandLine) {
415168124Srrs
416168124Srrs  std::string CmdPath;
417168124Srrs  std::vector<std::string> Args;
418168299Srrs  lexCommand(Message, CompileCommandLine, CmdPath, Args);
419168299Srrs  if (CmdPath.empty())
420168299Srrs    return 0;
421168299Srrs
422168299Srrs  return new CustomCompiler(CmdPath, Args);
423168124Srrs}
424170744Srrs
425170744Srrs// Custom execution environment create method, takes the execution command
426170744Srrs// as arguments
427170814SrrsAbstractInterpreter *AbstractInterpreter::createCustomExecutor(
428170744Srrs                    std::string &Message,
429168124Srrs                    const std::string &ExecCommandLine) {
430171440Srrs
431171440Srrs
432171440Srrs  std::string CmdPath;
433171440Srrs  std::vector<std::string> Args;
434171440Srrs  lexCommand(Message, ExecCommandLine, CmdPath, Args);
435171440Srrs  if (CmdPath.empty())
436217760Stuexen    return 0;
437217760Stuexen
438217760Stuexen  return new CustomExecutor(CmdPath, Args);
439217760Stuexen}
440217760Stuexen
441217760Stuexen//===----------------------------------------------------------------------===//
442171943Srrs// LLC Implementation of AbstractIntepreter interface
443171943Srrs//
444171943SrrsGCC::FileType LLC::OutputCode(const std::string &Bitcode,
445171943Srrs                              sys::Path &OutputAsmFile, std::string &Error,
446171943Srrs                              unsigned Timeout, unsigned MemoryLimit) {
447171943Srrs  const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
448171990Srrs  sys::Path uniqueFile(Bitcode + Suffix);
449171990Srrs  std::string ErrMsg;
450171990Srrs  if (uniqueFile.makeUnique(true, &ErrMsg)) {
451171990Srrs    errs() << "Error making unique filename: " << ErrMsg << "\n";
452171990Srrs    exit(1);
453171943Srrs  }
454171990Srrs  OutputAsmFile = uniqueFile;
455171990Srrs  std::vector<const char *> LLCArgs;
456171990Srrs  LLCArgs.push_back(LLCPath.c_str());
457171990Srrs
458171990Srrs  // Add any extra LLC args.
459171990Srrs  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
460179157Srrs    LLCArgs.push_back(ToolArgs[i].c_str());
461179157Srrs
462179157Srrs  LLCArgs.push_back("-o");
463179157Srrs  LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
464179157Srrs  LLCArgs.push_back(Bitcode.c_str());      // This is the input bitcode
465179157Srrs
466179783Srrs  if (UseIntegratedAssembler)
467179783Srrs    LLCArgs.push_back("-filetype=obj");
468179783Srrs
469179783Srrs  LLCArgs.push_back (0);
470179783Srrs
471179783Srrs  outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
472212799Stuexen  outs().flush();
473195919Stuexen  DEBUG(errs() << "\nAbout to run:\t";
474185694Srrs        for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
475185694Srrs          errs() << " " << LLCArgs[i];
476185694Srrs        errs() << "\n";
477185694Srrs        );
478212799Stuexen  if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
479212799Stuexen                            sys::Path(), sys::Path(), sys::Path(),
480193090Srrs                            Timeout, MemoryLimit))
481193090Srrs    Error = ProcessFailure(sys::Path(LLCPath), &LLCArgs[0],
482193090Srrs                           Timeout, MemoryLimit);
483193090Srrs  return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
484212799Stuexen}
485212799Stuexen
486212799Stuexenvoid LLC::compileProgram(const std::string &Bitcode, std::string *Error,
487212799Stuexen                         unsigned Timeout, unsigned MemoryLimit) {
488212799Stuexen  sys::Path OutputAsmFile;
489193090Srrs  OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
490212800Stuexen  OutputAsmFile.eraseFromDisk();
491212851Stuexen}
492216672Stuexen
493212800Stuexenint LLC::ExecuteProgram(const std::string &Bitcode,
494212800Stuexen                        const std::vector<std::string> &Args,
495212800Stuexen                        const std::string &InputFile,
496219057Srrs                        const std::string &OutputFile,
497219397Srrs                        std::string *Error,
498219057Srrs                        const std::vector<std::string> &ArgsForGCC,
499219057Srrs                        const std::vector<std::string> &SharedLibs,
500219057Srrs                        unsigned Timeout,
501219057Srrs                        unsigned MemoryLimit) {
502219057Srrs
503219397Srrs  sys::Path OutputAsmFile;
504219057Srrs  GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
505219057Srrs                                      MemoryLimit);
506219057Srrs  FileRemover OutFileRemover(OutputAsmFile.str(), !SaveTemps);
507219057Srrs
508219397Srrs  std::vector<std::string> GCCArgs(ArgsForGCC);
509219057Srrs  GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
510219057Srrs
511219057Srrs  // Assuming LLC worked, compile the result with GCC and run it.
512219057Srrs  return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind,
513219397Srrs                             InputFile, OutputFile, Error, GCCArgs,
514219397Srrs                             Timeout, MemoryLimit);
515219397Srrs}
516219397Srrs
517219397Srrs/// createLLC - Try to find the LLC executable
518219397Srrs///
519219397SrrsLLC *AbstractInterpreter::createLLC(const char *Argv0,
520219397Srrs                                    std::string &Message,
521219397Srrs                                    const std::string &GCCBinary,
522219397Srrs                                    const std::vector<std::string> *Args,
523229805Stuexen                                    const std::vector<std::string> *GCCArgs,
524229805Stuexen                                    bool UseIntegratedAssembler) {
525229805Stuexen  std::string LLCPath =
526229805Stuexen    PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC).str();
527229805Stuexen  if (LLCPath.empty()) {
528172091Srrs    Message = "Cannot find `llc' in executable directory!\n";
529167598Srrs    return 0;
530167598Srrs  }
531167598Srrs
532167598Srrs  Message = "Found llc: " + LLCPath + "\n";
533167598Srrs  GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
534167598Srrs  if (!gcc) {
535167598Srrs    errs() << Message << "\n";
536167598Srrs    exit(1);
537179783Srrs  }
538179783Srrs  return new LLC(LLCPath, gcc, Args, UseIntegratedAssembler);
539179783Srrs}
540179783Srrs
541179783Srrs//===---------------------------------------------------------------------===//
542172157Srrs// JIT Implementation of AbstractIntepreter interface
543179157Srrs//
544167598Srrsnamespace {
545180387Srrs  class JIT : public AbstractInterpreter {
546172091Srrs    std::string LLIPath;          // The path to the LLI executable
547167598Srrs    std::vector<std::string> ToolArgs; // Args to pass to LLI
548167598Srrs  public:
549167598Srrs    JIT(const std::string &Path, const std::vector<std::string> *Args)
550179783Srrs      : LLIPath(Path) {
551179783Srrs      ToolArgs.clear ();
552167598Srrs      if (Args) { ToolArgs = *Args; }
553167598Srrs    }
554
555    virtual int ExecuteProgram(const std::string &Bitcode,
556                               const std::vector<std::string> &Args,
557                               const std::string &InputFile,
558                               const std::string &OutputFile,
559                               std::string *Error,
560                               const std::vector<std::string> &GCCArgs =
561                                 std::vector<std::string>(),
562                               const std::vector<std::string> &SharedLibs =
563                                 std::vector<std::string>(),
564                               unsigned Timeout = 0,
565                               unsigned MemoryLimit = 0);
566  };
567}
568
569int JIT::ExecuteProgram(const std::string &Bitcode,
570                        const std::vector<std::string> &Args,
571                        const std::string &InputFile,
572                        const std::string &OutputFile,
573                        std::string *Error,
574                        const std::vector<std::string> &GCCArgs,
575                        const std::vector<std::string> &SharedLibs,
576                        unsigned Timeout,
577                        unsigned MemoryLimit) {
578  // Construct a vector of parameters, incorporating those from the command-line
579  std::vector<const char*> JITArgs;
580  JITArgs.push_back(LLIPath.c_str());
581  JITArgs.push_back("-force-interpreter=false");
582
583  // Add any extra LLI args.
584  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
585    JITArgs.push_back(ToolArgs[i].c_str());
586
587  for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
588    JITArgs.push_back("-load");
589    JITArgs.push_back(SharedLibs[i].c_str());
590  }
591  JITArgs.push_back(Bitcode.c_str());
592  // Add optional parameters to the running program from Argv
593  for (unsigned i=0, e = Args.size(); i != e; ++i)
594    JITArgs.push_back(Args[i].c_str());
595  JITArgs.push_back(0);
596
597  outs() << "<jit>"; outs().flush();
598  DEBUG(errs() << "\nAbout to run:\t";
599        for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
600          errs() << " " << JITArgs[i];
601        errs() << "\n";
602        );
603  DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
604  return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
605      sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
606      Timeout, MemoryLimit, Error);
607}
608
609/// createJIT - Try to find the LLI executable
610///
611AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
612                   std::string &Message, const std::vector<std::string> *Args) {
613  std::string LLIPath =
614    PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT).str();
615  if (!LLIPath.empty()) {
616    Message = "Found lli: " + LLIPath + "\n";
617    return new JIT(LLIPath, Args);
618  }
619
620  Message = "Cannot find `lli' in executable directory!\n";
621  return 0;
622}
623
624GCC::FileType CBE::OutputCode(const std::string &Bitcode,
625                              sys::Path &OutputCFile, std::string &Error,
626                              unsigned Timeout, unsigned MemoryLimit) {
627  sys::Path uniqueFile(Bitcode+".cbe.c");
628  std::string ErrMsg;
629  if (uniqueFile.makeUnique(true, &ErrMsg)) {
630    errs() << "Error making unique filename: " << ErrMsg << "\n";
631    exit(1);
632  }
633  OutputCFile = uniqueFile;
634  std::vector<const char *> LLCArgs;
635  LLCArgs.push_back(LLCPath.c_str());
636
637  // Add any extra LLC args.
638  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
639    LLCArgs.push_back(ToolArgs[i].c_str());
640
641  LLCArgs.push_back("-o");
642  LLCArgs.push_back(OutputCFile.c_str());   // Output to the C file
643  LLCArgs.push_back("-march=c");            // Output C language
644  LLCArgs.push_back(Bitcode.c_str());      // This is the input bitcode
645  LLCArgs.push_back(0);
646
647  outs() << "<cbe>"; outs().flush();
648  DEBUG(errs() << "\nAbout to run:\t";
649        for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
650          errs() << " " << LLCArgs[i];
651        errs() << "\n";
652        );
653  if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
654                            sys::Path(), Timeout, MemoryLimit))
655    Error = ProcessFailure(LLCPath, &LLCArgs[0], Timeout, MemoryLimit);
656  return GCC::CFile;
657}
658
659void CBE::compileProgram(const std::string &Bitcode, std::string *Error,
660                         unsigned Timeout, unsigned MemoryLimit) {
661  sys::Path OutputCFile;
662  OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit);
663  OutputCFile.eraseFromDisk();
664}
665
666int CBE::ExecuteProgram(const std::string &Bitcode,
667                        const std::vector<std::string> &Args,
668                        const std::string &InputFile,
669                        const std::string &OutputFile,
670                        std::string *Error,
671                        const std::vector<std::string> &ArgsForGCC,
672                        const std::vector<std::string> &SharedLibs,
673                        unsigned Timeout,
674                        unsigned MemoryLimit) {
675  sys::Path OutputCFile;
676  OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit);
677
678  FileRemover CFileRemove(OutputCFile.str(), !SaveTemps);
679
680  std::vector<std::string> GCCArgs(ArgsForGCC);
681  GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
682
683  return gcc->ExecuteProgram(OutputCFile.str(), Args, GCC::CFile,
684                             InputFile, OutputFile, Error, GCCArgs,
685                             Timeout, MemoryLimit);
686}
687
688/// createCBE - Try to find the 'llc' executable
689///
690CBE *AbstractInterpreter::createCBE(const char *Argv0,
691                                    std::string &Message,
692                                    const std::string &GCCBinary,
693                                    const std::vector<std::string> *Args,
694                                    const std::vector<std::string> *GCCArgs) {
695  sys::Path LLCPath =
696    PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createCBE);
697  if (LLCPath.isEmpty()) {
698    Message =
699      "Cannot find `llc' in executable directory!\n";
700    return 0;
701  }
702
703  Message = "Found llc: " + LLCPath.str() + "\n";
704  GCC *gcc = GCC::create(Message, GCCBinary, GCCArgs);
705  if (!gcc) {
706    errs() << Message << "\n";
707    exit(1);
708  }
709  return new CBE(LLCPath, gcc, Args);
710}
711
712//===---------------------------------------------------------------------===//
713// GCC abstraction
714//
715
716static bool IsARMArchitecture(std::vector<const char*> Args) {
717  for (std::vector<const char*>::const_iterator
718         I = Args.begin(), E = Args.end(); I != E; ++I) {
719    if (StringRef(*I).equals_lower("-arch")) {
720      ++I;
721      if (I != E && StringRef(*I).substr(0, strlen("arm")).equals_lower("arm"))
722        return true;
723    }
724  }
725
726  return false;
727}
728
729int GCC::ExecuteProgram(const std::string &ProgramFile,
730                        const std::vector<std::string> &Args,
731                        FileType fileType,
732                        const std::string &InputFile,
733                        const std::string &OutputFile,
734                        std::string *Error,
735                        const std::vector<std::string> &ArgsForGCC,
736                        unsigned Timeout,
737                        unsigned MemoryLimit) {
738  std::vector<const char*> GCCArgs;
739
740  GCCArgs.push_back(GCCPath.c_str());
741
742  if (TargetTriple.getArch() == Triple::x86)
743    GCCArgs.push_back("-m32");
744
745  for (std::vector<std::string>::const_iterator
746         I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
747    GCCArgs.push_back(I->c_str());
748
749  // Specify -x explicitly in case the extension is wonky
750  if (fileType != ObjectFile) {
751    GCCArgs.push_back("-x");
752    if (fileType == CFile) {
753      GCCArgs.push_back("c");
754      GCCArgs.push_back("-fno-strict-aliasing");
755    } else {
756      GCCArgs.push_back("assembler");
757
758      // For ARM architectures we don't want this flag. bugpoint isn't
759      // explicitly told what architecture it is working on, so we get
760      // it from gcc flags
761      if (TargetTriple.isOSDarwin() && !IsARMArchitecture(GCCArgs))
762        GCCArgs.push_back("-force_cpusubtype_ALL");
763    }
764  }
765
766  GCCArgs.push_back(ProgramFile.c_str());  // Specify the input filename.
767
768  GCCArgs.push_back("-x");
769  GCCArgs.push_back("none");
770  GCCArgs.push_back("-o");
771  sys::Path OutputBinary (ProgramFile+".gcc.exe");
772  std::string ErrMsg;
773  if (OutputBinary.makeUnique(true, &ErrMsg)) {
774    errs() << "Error making unique filename: " << ErrMsg << "\n";
775    exit(1);
776  }
777  GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
778
779  // Add any arguments intended for GCC. We locate them here because this is
780  // most likely -L and -l options that need to come before other libraries but
781  // after the source. Other options won't be sensitive to placement on the
782  // command line, so this should be safe.
783  for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
784    GCCArgs.push_back(ArgsForGCC[i].c_str());
785
786  GCCArgs.push_back("-lm");                // Hard-code the math library...
787  GCCArgs.push_back("-O2");                // Optimize the program a bit...
788#if defined (HAVE_LINK_R)
789  GCCArgs.push_back("-Wl,-R.");            // Search this dir for .so files
790#endif
791  if (TargetTriple.getArch() == Triple::sparc)
792    GCCArgs.push_back("-mcpu=v9");
793  GCCArgs.push_back(0);                    // NULL terminator
794
795  outs() << "<gcc>"; outs().flush();
796  DEBUG(errs() << "\nAbout to run:\t";
797        for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
798          errs() << " " << GCCArgs[i];
799        errs() << "\n";
800        );
801  if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
802        sys::Path())) {
803    *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
804    return -1;
805  }
806
807  std::vector<const char*> ProgramArgs;
808
809  // Declared here so that the destructor only runs after
810  // ProgramArgs is used.
811  std::string Exec;
812
813  if (RemoteClientPath.isEmpty())
814    ProgramArgs.push_back(OutputBinary.c_str());
815  else {
816    ProgramArgs.push_back(RemoteClientPath.c_str());
817    ProgramArgs.push_back(RemoteHost.c_str());
818    if (!RemoteUser.empty()) {
819      ProgramArgs.push_back("-l");
820      ProgramArgs.push_back(RemoteUser.c_str());
821    }
822    if (!RemotePort.empty()) {
823      ProgramArgs.push_back("-p");
824      ProgramArgs.push_back(RemotePort.c_str());
825    }
826    if (!RemoteExtra.empty()) {
827      ProgramArgs.push_back(RemoteExtra.c_str());
828    }
829
830    // Full path to the binary. We need to cd to the exec directory because
831    // there is a dylib there that the exec expects to find in the CWD
832    char* env_pwd = getenv("PWD");
833    Exec = "cd ";
834    Exec += env_pwd;
835    Exec += "; ./";
836    Exec += OutputBinary.c_str();
837    ProgramArgs.push_back(Exec.c_str());
838  }
839
840  // Add optional parameters to the running program from Argv
841  for (unsigned i = 0, e = Args.size(); i != e; ++i)
842    ProgramArgs.push_back(Args[i].c_str());
843  ProgramArgs.push_back(0);                // NULL terminator
844
845  // Now that we have a binary, run it!
846  outs() << "<program>"; outs().flush();
847  DEBUG(errs() << "\nAbout to run:\t";
848        for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
849          errs() << " " << ProgramArgs[i];
850        errs() << "\n";
851        );
852
853  FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
854
855  if (RemoteClientPath.isEmpty()) {
856    DEBUG(errs() << "<run locally>");
857    int ExitCode = RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
858        sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
859        Timeout, MemoryLimit, Error);
860    // Treat a signal (usually SIGSEGV) or timeout as part of the program output
861    // so that crash-causing miscompilation is handled seamlessly.
862    if (ExitCode < -1) {
863      std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
864      outFile << *Error << '\n';
865      outFile.close();
866      Error->clear();
867    }
868    return ExitCode;
869  } else {
870    outs() << "<run remotely>"; outs().flush();
871    return RunProgramRemotelyWithTimeout(sys::Path(RemoteClientPath),
872        &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
873        sys::Path(OutputFile), Timeout, MemoryLimit);
874  }
875}
876
877int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
878                          std::string &OutputFile,
879                          const std::vector<std::string> &ArgsForGCC,
880                          std::string &Error) {
881  sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
882  std::string ErrMsg;
883  if (uniqueFilename.makeUnique(true, &ErrMsg)) {
884    errs() << "Error making unique filename: " << ErrMsg << "\n";
885    exit(1);
886  }
887  OutputFile = uniqueFilename.str();
888
889  std::vector<const char*> GCCArgs;
890
891  GCCArgs.push_back(GCCPath.c_str());
892
893  if (TargetTriple.getArch() == Triple::x86)
894    GCCArgs.push_back("-m32");
895
896  for (std::vector<std::string>::const_iterator
897         I = gccArgs.begin(), E = gccArgs.end(); I != E; ++I)
898    GCCArgs.push_back(I->c_str());
899
900  // Compile the C/asm file into a shared object
901  if (fileType != ObjectFile) {
902    GCCArgs.push_back("-x");
903    GCCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
904  }
905  GCCArgs.push_back("-fno-strict-aliasing");
906  GCCArgs.push_back(InputFile.c_str());   // Specify the input filename.
907  GCCArgs.push_back("-x");
908  GCCArgs.push_back("none");
909  if (TargetTriple.getArch() == Triple::sparc)
910    GCCArgs.push_back("-G");       // Compile a shared library, `-G' for Sparc
911  else if (TargetTriple.isOSDarwin()) {
912    // link all source files into a single module in data segment, rather than
913    // generating blocks. dynamic_lookup requires that you set
914    // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.  FIXME: it would be better for
915    // bugpoint to just pass that in the environment of GCC.
916    GCCArgs.push_back("-single_module");
917    GCCArgs.push_back("-dynamiclib");   // `-dynamiclib' for MacOS X/PowerPC
918    GCCArgs.push_back("-undefined");
919    GCCArgs.push_back("dynamic_lookup");
920  } else
921    GCCArgs.push_back("-shared");  // `-shared' for Linux/X86, maybe others
922
923  if ((TargetTriple.getArch() == Triple::alpha) ||
924      (TargetTriple.getArch() == Triple::x86_64))
925    GCCArgs.push_back("-fPIC");   // Requires shared objs to contain PIC
926
927  if (TargetTriple.getArch() == Triple::sparc)
928    GCCArgs.push_back("-mcpu=v9");
929
930  GCCArgs.push_back("-o");
931  GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
932  GCCArgs.push_back("-O2");              // Optimize the program a bit.
933
934
935
936  // Add any arguments intended for GCC. We locate them here because this is
937  // most likely -L and -l options that need to come before other libraries but
938  // after the source. Other options won't be sensitive to placement on the
939  // command line, so this should be safe.
940  for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
941    GCCArgs.push_back(ArgsForGCC[i].c_str());
942  GCCArgs.push_back(0);                    // NULL terminator
943
944
945
946  outs() << "<gcc>"; outs().flush();
947  DEBUG(errs() << "\nAbout to run:\t";
948        for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
949          errs() << " " << GCCArgs[i];
950        errs() << "\n";
951        );
952  if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
953                            sys::Path())) {
954    Error = ProcessFailure(GCCPath, &GCCArgs[0]);
955    return 1;
956  }
957  return 0;
958}
959
960/// create - Try to find the `gcc' executable
961///
962GCC *GCC::create(std::string &Message,
963                 const std::string &GCCBinary,
964                 const std::vector<std::string> *Args) {
965  sys::Path GCCPath = sys::Program::FindProgramByName(GCCBinary);
966  if (GCCPath.isEmpty()) {
967    Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
968    return 0;
969  }
970
971  sys::Path RemoteClientPath;
972  if (!RemoteClient.empty())
973    RemoteClientPath = sys::Program::FindProgramByName(RemoteClient);
974
975  Message = "Found gcc: " + GCCPath.str() + "\n";
976  return new GCC(GCCPath, RemoteClientPath, Args);
977}
978