ToolRunner.cpp revision 314564
1193323Sed//===-- ToolRunner.cpp ----------------------------------------------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements the interfaces described in the ToolRunner.h file.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#include "ToolRunner.h"
15314564Sdim#include "llvm/Config/config.h"
16193323Sed#include "llvm/Support/CommandLine.h"
17193323Sed#include "llvm/Support/Debug.h"
18261991Sdim#include "llvm/Support/FileSystem.h"
19193323Sed#include "llvm/Support/FileUtilities.h"
20249423Sdim#include "llvm/Support/Program.h"
21198090Srdivacky#include "llvm/Support/raw_ostream.h"
22193323Sed#include <fstream>
23193323Sed#include <sstream>
24309124Sdim#include <utility>
25193323Sedusing namespace llvm;
26193323Sed
27276479Sdim#define DEBUG_TYPE "toolrunner"
28276479Sdim
29198090Srdivackynamespace llvm {
30314564Sdimcl::opt<bool> SaveTemps("save-temps", cl::init(false),
31314564Sdim                        cl::desc("Save temporary files"));
32198090Srdivacky}
33198090Srdivacky
34193323Sednamespace {
35314564Sdimcl::opt<std::string>
36314564Sdim    RemoteClient("remote-client",
37314564Sdim                 cl::desc("Remote execution client (rsh/ssh)"));
38193323Sed
39314564Sdimcl::opt<std::string> RemoteHost("remote-host",
40314564Sdim                                cl::desc("Remote execution (rsh/ssh) host"));
41193323Sed
42314564Sdimcl::opt<std::string> RemotePort("remote-port",
43314564Sdim                                cl::desc("Remote execution (rsh/ssh) port"));
44198090Srdivacky
45314564Sdimcl::opt<std::string> RemoteUser("remote-user",
46314564Sdim                                cl::desc("Remote execution (rsh/ssh) user id"));
47193323Sed
48314564Sdimcl::opt<std::string>
49314564Sdim    RemoteExtra("remote-extra-options",
50314564Sdim                cl::desc("Remote execution (rsh/ssh) extra options"));
51193323Sed}
52193323Sed
53198090Srdivacky/// RunProgramWithTimeout - This function provides an alternate interface
54198090Srdivacky/// to the sys::Program::ExecuteAndWait interface.
55207618Srdivacky/// @see sys::Program::ExecuteAndWait
56314564Sdimstatic int RunProgramWithTimeout(StringRef ProgramPath, const char **Args,
57314564Sdim                                 StringRef StdInFile, StringRef StdOutFile,
58314564Sdim                                 StringRef StdErrFile, unsigned NumSeconds = 0,
59218885Sdim                                 unsigned MemoryLimit = 0,
60276479Sdim                                 std::string *ErrMsg = nullptr) {
61314564Sdim  const StringRef *Redirects[3] = {&StdInFile, &StdOutFile, &StdErrFile};
62314564Sdim  return sys::ExecuteAndWait(ProgramPath, Args, nullptr, Redirects, NumSeconds,
63314564Sdim                             MemoryLimit, ErrMsg);
64193323Sed}
65193323Sed
66198090Srdivacky/// RunProgramRemotelyWithTimeout - This function runs the given program
67198090Srdivacky/// remotely using the given remote client and the sys::Program::ExecuteAndWait.
68198090Srdivacky/// Returns the remote program exit code or reports a remote client error if it
69198090Srdivacky/// fails. Remote client is required to return 255 if it failed or program exit
70198090Srdivacky/// code otherwise.
71207618Srdivacky/// @see sys::Program::ExecuteAndWait
72261991Sdimstatic int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
73314564Sdim                                         const char **Args, StringRef StdInFile,
74261991Sdim                                         StringRef StdOutFile,
75261991Sdim                                         StringRef StdErrFile,
76198090Srdivacky                                         unsigned NumSeconds = 0,
77198090Srdivacky                                         unsigned MemoryLimit = 0) {
78314564Sdim  const StringRef *Redirects[3] = {&StdInFile, &StdOutFile, &StdErrFile};
79193323Sed
80198090Srdivacky  // Run the program remotely with the remote client
81276479Sdim  int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, nullptr,
82261991Sdim                                       Redirects, NumSeconds, MemoryLimit);
83198090Srdivacky
84198090Srdivacky  // Has the remote client fail?
85198090Srdivacky  if (255 == ReturnCode) {
86198090Srdivacky    std::ostringstream OS;
87198090Srdivacky    OS << "\nError running remote client:\n ";
88198090Srdivacky    for (const char **Arg = Args; *Arg; ++Arg)
89198090Srdivacky      OS << " " << *Arg;
90198090Srdivacky    OS << "\n";
91198090Srdivacky
92198090Srdivacky    // The error message is in the output file, let's print it out from there.
93261991Sdim    std::string StdOutFileName = StdOutFile.str();
94261991Sdim    std::ifstream ErrorFile(StdOutFileName.c_str());
95198090Srdivacky    if (ErrorFile) {
96198090Srdivacky      std::copy(std::istreambuf_iterator<char>(ErrorFile),
97198090Srdivacky                std::istreambuf_iterator<char>(),
98198090Srdivacky                std::ostreambuf_iterator<char>(OS));
99198090Srdivacky      ErrorFile.close();
100198090Srdivacky    }
101198090Srdivacky
102236386Sdim    errs() << OS.str();
103198090Srdivacky  }
104198090Srdivacky
105198090Srdivacky  return ReturnCode;
106198090Srdivacky}
107198090Srdivacky
108314564Sdimstatic Error ProcessFailure(StringRef ProgPath, const char **Args,
109314564Sdim                            unsigned Timeout = 0, unsigned MemoryLimit = 0) {
110193323Sed  std::ostringstream OS;
111193323Sed  OS << "\nError running tool:\n ";
112193323Sed  for (const char **Arg = Args; *Arg; ++Arg)
113193323Sed    OS << " " << *Arg;
114193323Sed  OS << "\n";
115218885Sdim
116193323Sed  // Rerun the compiler, capturing any error messages to print them.
117261991Sdim  SmallString<128> ErrorFilename;
118276479Sdim  std::error_code EC = sys::fs::createTemporaryFile(
119280031Sdim      "bugpoint.program_error_messages", "", ErrorFilename);
120261991Sdim  if (EC) {
121261991Sdim    errs() << "Error making unique filename: " << EC.message() << "\n";
122193323Sed    exit(1);
123193323Sed  }
124276479Sdim
125261991Sdim  RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
126261991Sdim                        ErrorFilename.str(), Timeout, MemoryLimit);
127208599Srdivacky  // FIXME: check return code ?
128193323Sed
129296417Sdim  // Print out the error messages generated by CC if possible...
130193323Sed  std::ifstream ErrorFile(ErrorFilename.c_str());
131193323Sed  if (ErrorFile) {
132193323Sed    std::copy(std::istreambuf_iterator<char>(ErrorFile),
133193323Sed              std::istreambuf_iterator<char>(),
134193323Sed              std::ostreambuf_iterator<char>(OS));
135193323Sed    ErrorFile.close();
136193323Sed  }
137193323Sed
138261991Sdim  sys::fs::remove(ErrorFilename.c_str());
139314564Sdim  return make_error<StringError>(OS.str(), inconvertibleErrorCode());
140193323Sed}
141193323Sed
142193323Sed//===---------------------------------------------------------------------===//
143193323Sed// LLI Implementation of AbstractIntepreter interface
144193323Sed//
145193323Sednamespace {
146314564Sdimclass LLI : public AbstractInterpreter {
147314564Sdim  std::string LLIPath;               // The path to the LLI executable
148314564Sdim  std::vector<std::string> ToolArgs; // Args to pass to LLI
149314564Sdimpublic:
150314564Sdim  LLI(const std::string &Path, const std::vector<std::string> *Args)
151193323Sed      : LLIPath(Path) {
152314564Sdim    ToolArgs.clear();
153314564Sdim    if (Args) {
154314564Sdim      ToolArgs = *Args;
155193323Sed    }
156314564Sdim  }
157193323Sed
158314564Sdim  Expected<int> ExecuteProgram(
159314564Sdim      const std::string &Bitcode, const std::vector<std::string> &Args,
160314564Sdim      const std::string &InputFile, const std::string &OutputFile,
161314564Sdim      const std::vector<std::string> &CCArgs,
162314564Sdim      const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
163314564Sdim      unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
164314564Sdim};
165193323Sed}
166193323Sed
167314564SdimExpected<int> LLI::ExecuteProgram(const std::string &Bitcode,
168314564Sdim                                  const std::vector<std::string> &Args,
169314564Sdim                                  const std::string &InputFile,
170314564Sdim                                  const std::string &OutputFile,
171314564Sdim                                  const std::vector<std::string> &CCArgs,
172314564Sdim                                  const std::vector<std::string> &SharedLibs,
173314564Sdim                                  unsigned Timeout, unsigned MemoryLimit) {
174314564Sdim  std::vector<const char *> LLIArgs;
175193323Sed  LLIArgs.push_back(LLIPath.c_str());
176193323Sed  LLIArgs.push_back("-force-interpreter=true");
177193323Sed
178218885Sdim  for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
179314564Sdim                                                e = SharedLibs.end();
180314564Sdim       i != e; ++i) {
181199481Srdivacky    LLIArgs.push_back("-load");
182199481Srdivacky    LLIArgs.push_back((*i).c_str());
183199481Srdivacky  }
184199481Srdivacky
185193323Sed  // Add any extra LLI args.
186193323Sed  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
187193323Sed    LLIArgs.push_back(ToolArgs[i].c_str());
188193323Sed
189193323Sed  LLIArgs.push_back(Bitcode.c_str());
190193323Sed  // Add optional parameters to the running program from Argv
191314564Sdim  for (unsigned i = 0, e = Args.size(); i != e; ++i)
192193323Sed    LLIArgs.push_back(Args[i].c_str());
193276479Sdim  LLIArgs.push_back(nullptr);
194193323Sed
195314564Sdim  outs() << "<lli>";
196314564Sdim  outs().flush();
197198090Srdivacky  DEBUG(errs() << "\nAbout to run:\t";
198314564Sdim        for (unsigned i = 0, e = LLIArgs.size() - 1; i != e; ++i) errs()
199314564Sdim        << " " << LLIArgs[i];
200314564Sdim        errs() << "\n";);
201314564Sdim  return RunProgramWithTimeout(LLIPath, &LLIArgs[0], InputFile, OutputFile,
202314564Sdim                               OutputFile, Timeout, MemoryLimit);
203193323Sed}
204193323Sed
205314564Sdimvoid AbstractInterpreter::anchor() {}
206234353Sdim
207261991Sdim#if defined(LLVM_ON_UNIX)
208261991Sdimconst char EXESuffix[] = "";
209314564Sdim#elif defined(LLVM_ON_WIN32)
210261991Sdimconst char EXESuffix[] = "exe";
211261991Sdim#endif
212261991Sdim
213261991Sdim/// Prepend the path to the program being executed
214261991Sdim/// to \p ExeName, given the value of argv[0] and the address of main()
215261991Sdim/// itself. This allows us to find another LLVM tool if it is built in the same
216261991Sdim/// directory. An empty string is returned on error; note that this function
217261991Sdim/// just mainpulates the path and doesn't check for executability.
218261991Sdim/// @brief Find a named executable.
219261991Sdimstatic std::string PrependMainExecutablePath(const std::string &ExeName,
220261991Sdim                                             const char *Argv0,
221261991Sdim                                             void *MainAddr) {
222261991Sdim  // Check the directory that the calling program is in.  We can do
223261991Sdim  // this if ProgramPath contains at least one / character, indicating that it
224261991Sdim  // is a relative path to the executable itself.
225261991Sdim  std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
226261991Sdim  StringRef Result = sys::path::parent_path(Main);
227261991Sdim
228261991Sdim  if (!Result.empty()) {
229261991Sdim    SmallString<128> Storage = Result;
230261991Sdim    sys::path::append(Storage, ExeName);
231261991Sdim    sys::path::replace_extension(Storage, EXESuffix);
232261991Sdim    return Storage.str();
233261991Sdim  }
234261991Sdim
235261991Sdim  return Result.str();
236261991Sdim}
237261991Sdim
238193323Sed// LLI create method - Try to find the LLI executable
239314564SdimAbstractInterpreter *
240314564SdimAbstractInterpreter::createLLI(const char *Argv0, std::string &Message,
241314564Sdim                               const std::vector<std::string> *ToolArgs) {
242198090Srdivacky  std::string LLIPath =
243314564Sdim      PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI);
244193323Sed  if (!LLIPath.empty()) {
245193323Sed    Message = "Found lli: " + LLIPath + "\n";
246193323Sed    return new LLI(LLIPath, ToolArgs);
247193323Sed  }
248193323Sed
249218885Sdim  Message = "Cannot find `lli' in executable directory!\n";
250276479Sdim  return nullptr;
251193323Sed}
252193323Sed
253193323Sed//===---------------------------------------------------------------------===//
254218885Sdim// Custom compiler command implementation of AbstractIntepreter interface
255218885Sdim//
256218885Sdim// Allows using a custom command for compiling the bitcode, thus allows, for
257218885Sdim// example, to compile a bitcode fragment without linking or executing, then
258218885Sdim// using a custom wrapper script to check for compiler errors.
259218885Sdimnamespace {
260314564Sdimclass CustomCompiler : public AbstractInterpreter {
261314564Sdim  std::string CompilerCommand;
262314564Sdim  std::vector<std::string> CompilerArgs;
263218885Sdim
264314564Sdimpublic:
265314564Sdim  CustomCompiler(const std::string &CompilerCmd,
266314564Sdim                 std::vector<std::string> CompArgs)
267314564Sdim      : CompilerCommand(CompilerCmd), CompilerArgs(std::move(CompArgs)) {}
268218885Sdim
269314564Sdim  Error compileProgram(const std::string &Bitcode, unsigned Timeout = 0,
270314564Sdim                       unsigned MemoryLimit = 0) override;
271314564Sdim
272314564Sdim  Expected<int> ExecuteProgram(
273314564Sdim      const std::string &Bitcode, const std::vector<std::string> &Args,
274314564Sdim      const std::string &InputFile, const std::string &OutputFile,
275314564Sdim      const std::vector<std::string> &CCArgs = std::vector<std::string>(),
276314564Sdim      const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
277314564Sdim      unsigned Timeout = 0, unsigned MemoryLimit = 0) override {
278314564Sdim    return make_error<StringError>(
279314564Sdim        "Execution not supported with -compile-custom",
280314564Sdim        inconvertibleErrorCode());
281314564Sdim  }
282314564Sdim};
283218885Sdim}
284218885Sdim
285314564SdimError CustomCompiler::compileProgram(const std::string &Bitcode,
286314564Sdim                                     unsigned Timeout, unsigned MemoryLimit) {
287218885Sdim
288314564Sdim  std::vector<const char *> ProgramArgs;
289218885Sdim  ProgramArgs.push_back(CompilerCommand.c_str());
290218885Sdim
291218885Sdim  for (std::size_t i = 0; i < CompilerArgs.size(); ++i)
292218885Sdim    ProgramArgs.push_back(CompilerArgs.at(i).c_str());
293218885Sdim  ProgramArgs.push_back(Bitcode.c_str());
294276479Sdim  ProgramArgs.push_back(nullptr);
295218885Sdim
296218885Sdim  // Add optional parameters to the running program from Argv
297218885Sdim  for (unsigned i = 0, e = CompilerArgs.size(); i != e; ++i)
298218885Sdim    ProgramArgs.push_back(CompilerArgs[i].c_str());
299218885Sdim
300314564Sdim  if (RunProgramWithTimeout(CompilerCommand, &ProgramArgs[0], "", "", "",
301314564Sdim                            Timeout, MemoryLimit))
302314564Sdim    return ProcessFailure(CompilerCommand, &ProgramArgs[0], Timeout,
303314564Sdim                          MemoryLimit);
304314564Sdim  return Error::success();
305218885Sdim}
306218885Sdim
307218885Sdim//===---------------------------------------------------------------------===//
308193323Sed// Custom execution command implementation of AbstractIntepreter interface
309193323Sed//
310193323Sed// Allows using a custom command for executing the bitcode, thus allows,
311218885Sdim// for example, to invoke a cross compiler for code generation followed by
312193323Sed// a simulator that executes the generated binary.
313193323Sednamespace {
314314564Sdimclass CustomExecutor : public AbstractInterpreter {
315314564Sdim  std::string ExecutionCommand;
316314564Sdim  std::vector<std::string> ExecutorArgs;
317193323Sed
318314564Sdimpublic:
319314564Sdim  CustomExecutor(const std::string &ExecutionCmd,
320314564Sdim                 std::vector<std::string> ExecArgs)
321314564Sdim      : ExecutionCommand(ExecutionCmd), ExecutorArgs(std::move(ExecArgs)) {}
322314564Sdim
323314564Sdim  Expected<int> ExecuteProgram(
324314564Sdim      const std::string &Bitcode, const std::vector<std::string> &Args,
325314564Sdim      const std::string &InputFile, const std::string &OutputFile,
326314564Sdim      const std::vector<std::string> &CCArgs,
327314564Sdim      const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
328314564Sdim      unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
329314564Sdim};
330193323Sed}
331193323Sed
332314564SdimExpected<int> CustomExecutor::ExecuteProgram(
333314564Sdim    const std::string &Bitcode, const std::vector<std::string> &Args,
334314564Sdim    const std::string &InputFile, const std::string &OutputFile,
335314564Sdim    const std::vector<std::string> &CCArgs,
336314564Sdim    const std::vector<std::string> &SharedLibs, unsigned Timeout,
337314564Sdim    unsigned MemoryLimit) {
338193323Sed
339314564Sdim  std::vector<const char *> ProgramArgs;
340193323Sed  ProgramArgs.push_back(ExecutionCommand.c_str());
341193323Sed
342193323Sed  for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
343193323Sed    ProgramArgs.push_back(ExecutorArgs.at(i).c_str());
344193323Sed  ProgramArgs.push_back(Bitcode.c_str());
345276479Sdim  ProgramArgs.push_back(nullptr);
346193323Sed
347193323Sed  // Add optional parameters to the running program from Argv
348207618Srdivacky  for (unsigned i = 0, e = Args.size(); i != e; ++i)
349193323Sed    ProgramArgs.push_back(Args[i].c_str());
350193323Sed
351314564Sdim  return RunProgramWithTimeout(ExecutionCommand, &ProgramArgs[0], InputFile,
352314564Sdim                               OutputFile, OutputFile, Timeout, MemoryLimit);
353193323Sed}
354193323Sed
355218885Sdim// Tokenize the CommandLine to the command and the args to allow
356218885Sdim// defining a full command line as the command instead of just the
357218885Sdim// executed program. We cannot just pass the whole string after the command
358218885Sdim// as a single argument because then program sees only a single
359218885Sdim// command line argument (with spaces in it: "foo bar" instead
360218885Sdim// of "foo" and "bar").
361218885Sdim//
362218885Sdim// code borrowed from:
363218885Sdim// http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
364218885Sdimstatic void lexCommand(std::string &Message, const std::string &CommandLine,
365276479Sdim                       std::string &CmdPath, std::vector<std::string> &Args) {
366193323Sed
367193323Sed  std::string Command = "";
368193323Sed  std::string delimiters = " ";
369193323Sed
370218885Sdim  std::string::size_type lastPos = CommandLine.find_first_not_of(delimiters, 0);
371218885Sdim  std::string::size_type pos = CommandLine.find_first_of(delimiters, lastPos);
372193323Sed
373193323Sed  while (std::string::npos != pos || std::string::npos != lastPos) {
374218885Sdim    std::string token = CommandLine.substr(lastPos, pos - lastPos);
375193323Sed    if (Command == "")
376314564Sdim      Command = token;
377193323Sed    else
378314564Sdim      Args.push_back(token);
379193323Sed    // Skip delimiters.  Note the "not_of"
380218885Sdim    lastPos = CommandLine.find_first_not_of(delimiters, pos);
381193323Sed    // Find next "non-delimiter"
382218885Sdim    pos = CommandLine.find_first_of(delimiters, lastPos);
383193323Sed  }
384193323Sed
385280031Sdim  auto Path = sys::findProgramByName(Command);
386280031Sdim  if (!Path) {
387314564Sdim    Message = std::string("Cannot find '") + Command + "' in PATH: " +
388314564Sdim              Path.getError().message() + "\n";
389218885Sdim    return;
390193323Sed  }
391280031Sdim  CmdPath = *Path;
392193323Sed
393193323Sed  Message = "Found command in: " + CmdPath + "\n";
394218885Sdim}
395193323Sed
396218885Sdim// Custom execution environment create method, takes the execution command
397218885Sdim// as arguments
398218885SdimAbstractInterpreter *AbstractInterpreter::createCustomCompiler(
399314564Sdim    std::string &Message, const std::string &CompileCommandLine) {
400218885Sdim
401218885Sdim  std::string CmdPath;
402218885Sdim  std::vector<std::string> Args;
403218885Sdim  lexCommand(Message, CompileCommandLine, CmdPath, Args);
404218885Sdim  if (CmdPath.empty())
405276479Sdim    return nullptr;
406218885Sdim
407218885Sdim  return new CustomCompiler(CmdPath, Args);
408218885Sdim}
409218885Sdim
410218885Sdim// Custom execution environment create method, takes the execution command
411218885Sdim// as arguments
412314564SdimAbstractInterpreter *
413314564SdimAbstractInterpreter::createCustomExecutor(std::string &Message,
414314564Sdim                                          const std::string &ExecCommandLine) {
415218885Sdim
416218885Sdim  std::string CmdPath;
417218885Sdim  std::vector<std::string> Args;
418218885Sdim  lexCommand(Message, ExecCommandLine, CmdPath, Args);
419218885Sdim  if (CmdPath.empty())
420276479Sdim    return nullptr;
421218885Sdim
422193323Sed  return new CustomExecutor(CmdPath, Args);
423193323Sed}
424193323Sed
425193323Sed//===----------------------------------------------------------------------===//
426193323Sed// LLC Implementation of AbstractIntepreter interface
427193323Sed//
428314564SdimExpected<CC::FileType> LLC::OutputCode(const std::string &Bitcode,
429314564Sdim                                       std::string &OutputAsmFile,
430314564Sdim                                       unsigned Timeout, unsigned MemoryLimit) {
431205218Srdivacky  const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
432261991Sdim
433261991Sdim  SmallString<128> UniqueFile;
434276479Sdim  std::error_code EC =
435261991Sdim      sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
436261991Sdim  if (EC) {
437261991Sdim    errs() << "Error making unique filename: " << EC.message() << "\n";
438193323Sed    exit(1);
439193323Sed  }
440261991Sdim  OutputAsmFile = UniqueFile.str();
441193323Sed  std::vector<const char *> LLCArgs;
442205218Srdivacky  LLCArgs.push_back(LLCPath.c_str());
443193323Sed
444193323Sed  // Add any extra LLC args.
445193323Sed  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
446193323Sed    LLCArgs.push_back(ToolArgs[i].c_str());
447193323Sed
448205218Srdivacky  LLCArgs.push_back("-o");
449205218Srdivacky  LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
450314564Sdim  LLCArgs.push_back(Bitcode.c_str());       // This is the input bitcode
451218885Sdim
452205218Srdivacky  if (UseIntegratedAssembler)
453205218Srdivacky    LLCArgs.push_back("-filetype=obj");
454218885Sdim
455314564Sdim  LLCArgs.push_back(nullptr);
456193323Sed
457205218Srdivacky  outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
458205218Srdivacky  outs().flush();
459198090Srdivacky  DEBUG(errs() << "\nAbout to run:\t";
460314564Sdim        for (unsigned i = 0, e = LLCArgs.size() - 1; i != e; ++i) errs()
461314564Sdim        << " " << LLCArgs[i];
462314564Sdim        errs() << "\n";);
463314564Sdim  if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], "", "", "", Timeout,
464314564Sdim                            MemoryLimit))
465314564Sdim    return ProcessFailure(LLCPath, &LLCArgs[0], Timeout, MemoryLimit);
466296417Sdim  return UseIntegratedAssembler ? CC::ObjectFile : CC::AsmFile;
467193323Sed}
468193323Sed
469314564SdimError LLC::compileProgram(const std::string &Bitcode, unsigned Timeout,
470314564Sdim                          unsigned MemoryLimit) {
471261991Sdim  std::string OutputAsmFile;
472314564Sdim  Expected<CC::FileType> Result =
473314564Sdim      OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
474261991Sdim  sys::fs::remove(OutputAsmFile);
475314564Sdim  if (Error E = Result.takeError())
476314564Sdim    return E;
477314564Sdim  return Error::success();
478193323Sed}
479193323Sed
480314564SdimExpected<int> LLC::ExecuteProgram(const std::string &Bitcode,
481314564Sdim                                  const std::vector<std::string> &Args,
482314564Sdim                                  const std::string &InputFile,
483314564Sdim                                  const std::string &OutputFile,
484314564Sdim                                  const std::vector<std::string> &ArgsForCC,
485314564Sdim                                  const std::vector<std::string> &SharedLibs,
486314564Sdim                                  unsigned Timeout, unsigned MemoryLimit) {
487193323Sed
488261991Sdim  std::string OutputAsmFile;
489314564Sdim  Expected<CC::FileType> FileKind =
490314564Sdim      OutputCode(Bitcode, OutputAsmFile, Timeout, MemoryLimit);
491261991Sdim  FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
492314564Sdim  if (Error E = FileKind.takeError())
493314564Sdim    return std::move(E);
494193323Sed
495296417Sdim  std::vector<std::string> CCArgs(ArgsForCC);
496296417Sdim  CCArgs.insert(CCArgs.end(), SharedLibs.begin(), SharedLibs.end());
497193323Sed
498296417Sdim  // Assuming LLC worked, compile the result with CC and run it.
499314564Sdim  return cc->ExecuteProgram(OutputAsmFile, Args, *FileKind, InputFile,
500314564Sdim                            OutputFile, CCArgs, Timeout, MemoryLimit);
501193323Sed}
502193323Sed
503193323Sed/// createLLC - Try to find the LLC executable
504193323Sed///
505314564SdimLLC *AbstractInterpreter::createLLC(const char *Argv0, std::string &Message,
506296417Sdim                                    const std::string &CCBinary,
507193323Sed                                    const std::vector<std::string> *Args,
508296417Sdim                                    const std::vector<std::string> *CCArgs,
509205218Srdivacky                                    bool UseIntegratedAssembler) {
510198090Srdivacky  std::string LLCPath =
511314564Sdim      PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC);
512193323Sed  if (LLCPath.empty()) {
513218885Sdim    Message = "Cannot find `llc' in executable directory!\n";
514276479Sdim    return nullptr;
515193323Sed  }
516193323Sed
517296417Sdim  CC *cc = CC::create(Message, CCBinary, CCArgs);
518296417Sdim  if (!cc) {
519198090Srdivacky    errs() << Message << "\n";
520193323Sed    exit(1);
521193323Sed  }
522249423Sdim  Message = "Found llc: " + LLCPath + "\n";
523296417Sdim  return new LLC(LLCPath, cc, Args, UseIntegratedAssembler);
524193323Sed}
525193323Sed
526193323Sed//===---------------------------------------------------------------------===//
527193323Sed// JIT Implementation of AbstractIntepreter interface
528193323Sed//
529193323Sednamespace {
530314564Sdimclass JIT : public AbstractInterpreter {
531314564Sdim  std::string LLIPath;               // The path to the LLI executable
532314564Sdim  std::vector<std::string> ToolArgs; // Args to pass to LLI
533314564Sdimpublic:
534314564Sdim  JIT(const std::string &Path, const std::vector<std::string> *Args)
535193323Sed      : LLIPath(Path) {
536314564Sdim    ToolArgs.clear();
537314564Sdim    if (Args) {
538314564Sdim      ToolArgs = *Args;
539193323Sed    }
540314564Sdim  }
541193323Sed
542314564Sdim  Expected<int> ExecuteProgram(
543314564Sdim      const std::string &Bitcode, const std::vector<std::string> &Args,
544314564Sdim      const std::string &InputFile, const std::string &OutputFile,
545314564Sdim      const std::vector<std::string> &CCArgs = std::vector<std::string>(),
546314564Sdim      const std::vector<std::string> &SharedLibs = std::vector<std::string>(),
547314564Sdim      unsigned Timeout = 0, unsigned MemoryLimit = 0) override;
548314564Sdim};
549193323Sed}
550193323Sed
551314564SdimExpected<int> JIT::ExecuteProgram(const std::string &Bitcode,
552314564Sdim                                  const std::vector<std::string> &Args,
553314564Sdim                                  const std::string &InputFile,
554314564Sdim                                  const std::string &OutputFile,
555314564Sdim                                  const std::vector<std::string> &CCArgs,
556314564Sdim                                  const std::vector<std::string> &SharedLibs,
557314564Sdim                                  unsigned Timeout, unsigned MemoryLimit) {
558193323Sed  // Construct a vector of parameters, incorporating those from the command-line
559314564Sdim  std::vector<const char *> JITArgs;
560193323Sed  JITArgs.push_back(LLIPath.c_str());
561193323Sed  JITArgs.push_back("-force-interpreter=false");
562193323Sed
563193323Sed  // Add any extra LLI args.
564193323Sed  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
565193323Sed    JITArgs.push_back(ToolArgs[i].c_str());
566193323Sed
567193323Sed  for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
568193323Sed    JITArgs.push_back("-load");
569193323Sed    JITArgs.push_back(SharedLibs[i].c_str());
570193323Sed  }
571193323Sed  JITArgs.push_back(Bitcode.c_str());
572193323Sed  // Add optional parameters to the running program from Argv
573314564Sdim  for (unsigned i = 0, e = Args.size(); i != e; ++i)
574193323Sed    JITArgs.push_back(Args[i].c_str());
575276479Sdim  JITArgs.push_back(nullptr);
576193323Sed
577314564Sdim  outs() << "<jit>";
578314564Sdim  outs().flush();
579198090Srdivacky  DEBUG(errs() << "\nAbout to run:\t";
580314564Sdim        for (unsigned i = 0, e = JITArgs.size() - 1; i != e; ++i) errs()
581314564Sdim        << " " << JITArgs[i];
582314564Sdim        errs() << "\n";);
583198090Srdivacky  DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
584314564Sdim  return RunProgramWithTimeout(LLIPath, &JITArgs[0], InputFile, OutputFile,
585314564Sdim                               OutputFile, Timeout, MemoryLimit);
586193323Sed}
587193323Sed
588193323Sed/// createJIT - Try to find the LLI executable
589193323Sed///
590314564SdimAbstractInterpreter *
591314564SdimAbstractInterpreter::createJIT(const char *Argv0, std::string &Message,
592314564Sdim                               const std::vector<std::string> *Args) {
593198090Srdivacky  std::string LLIPath =
594314564Sdim      PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT);
595193323Sed  if (!LLIPath.empty()) {
596193323Sed    Message = "Found lli: " + LLIPath + "\n";
597193323Sed    return new JIT(LLIPath, Args);
598193323Sed  }
599193323Sed
600218885Sdim  Message = "Cannot find `lli' in executable directory!\n";
601276479Sdim  return nullptr;
602193323Sed}
603193323Sed
604193323Sed//===---------------------------------------------------------------------===//
605296417Sdim// CC abstraction
606193323Sed//
607198090Srdivacky
608314564Sdimstatic bool IsARMArchitecture(std::vector<const char *> Args) {
609314564Sdim  for (std::vector<const char *>::const_iterator I = Args.begin(),
610314564Sdim                                                 E = Args.end();
611314564Sdim       I != E; ++I) {
612208599Srdivacky    if (StringRef(*I).equals_lower("-arch")) {
613198090Srdivacky      ++I;
614261991Sdim      if (I != E && StringRef(*I).startswith_lower("arm"))
615198090Srdivacky        return true;
616198090Srdivacky    }
617198090Srdivacky  }
618198090Srdivacky
619198090Srdivacky  return false;
620198090Srdivacky}
621198090Srdivacky
622314564SdimExpected<int> CC::ExecuteProgram(const std::string &ProgramFile,
623314564Sdim                                 const std::vector<std::string> &Args,
624314564Sdim                                 FileType fileType,
625314564Sdim                                 const std::string &InputFile,
626314564Sdim                                 const std::string &OutputFile,
627314564Sdim                                 const std::vector<std::string> &ArgsForCC,
628314564Sdim                                 unsigned Timeout, unsigned MemoryLimit) {
629314564Sdim  std::vector<const char *> CCArgs;
630193323Sed
631296417Sdim  CCArgs.push_back(CCPath.c_str());
632193323Sed
633205218Srdivacky  if (TargetTriple.getArch() == Triple::x86)
634296417Sdim    CCArgs.push_back("-m32");
635205218Srdivacky
636314564Sdim  for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
637314564Sdim                                                E = ccArgs.end();
638314564Sdim       I != E; ++I)
639296417Sdim    CCArgs.push_back(I->c_str());
640193323Sed
641193323Sed  // Specify -x explicitly in case the extension is wonky
642205218Srdivacky  if (fileType != ObjectFile) {
643296417Sdim    CCArgs.push_back("-x");
644205218Srdivacky    if (fileType == CFile) {
645296417Sdim      CCArgs.push_back("c");
646296417Sdim      CCArgs.push_back("-fno-strict-aliasing");
647205218Srdivacky    } else {
648296417Sdim      CCArgs.push_back("assembler");
649198090Srdivacky
650205218Srdivacky      // For ARM architectures we don't want this flag. bugpoint isn't
651205218Srdivacky      // explicitly told what architecture it is working on, so we get
652296417Sdim      // it from cc flags
653296417Sdim      if (TargetTriple.isOSDarwin() && !IsARMArchitecture(CCArgs))
654296417Sdim        CCArgs.push_back("-force_cpusubtype_ALL");
655205218Srdivacky    }
656193323Sed  }
657218885Sdim
658314564Sdim  CCArgs.push_back(ProgramFile.c_str()); // Specify the input filename.
659218885Sdim
660296417Sdim  CCArgs.push_back("-x");
661296417Sdim  CCArgs.push_back("none");
662296417Sdim  CCArgs.push_back("-o");
663261991Sdim
664261991Sdim  SmallString<128> OutputBinary;
665276479Sdim  std::error_code EC =
666296417Sdim      sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.cc.exe", OutputBinary);
667261991Sdim  if (EC) {
668261991Sdim    errs() << "Error making unique filename: " << EC.message() << "\n";
669193323Sed    exit(1);
670193323Sed  }
671296417Sdim  CCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
672193323Sed
673296417Sdim  // Add any arguments intended for CC. We locate them here because this is
674193323Sed  // most likely -L and -l options that need to come before other libraries but
675193323Sed  // after the source. Other options won't be sensitive to placement on the
676193323Sed  // command line, so this should be safe.
677296417Sdim  for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
678296417Sdim    CCArgs.push_back(ArgsForCC[i].c_str());
679193323Sed
680314564Sdim  CCArgs.push_back("-lm"); // Hard-code the math library...
681314564Sdim  CCArgs.push_back("-O2"); // Optimize the program a bit...
682198090Srdivacky  if (TargetTriple.getArch() == Triple::sparc)
683296417Sdim    CCArgs.push_back("-mcpu=v9");
684314564Sdim  CCArgs.push_back(nullptr); // NULL terminator
685193323Sed
686314564Sdim  outs() << "<CC>";
687314564Sdim  outs().flush();
688198090Srdivacky  DEBUG(errs() << "\nAbout to run:\t";
689314564Sdim        for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
690314564Sdim        << " " << CCArgs[i];
691314564Sdim        errs() << "\n";);
692314564Sdim  if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", ""))
693314564Sdim    return ProcessFailure(CCPath, &CCArgs[0]);
694193323Sed
695314564Sdim  std::vector<const char *> ProgramArgs;
696193323Sed
697212793Sdim  // Declared here so that the destructor only runs after
698212793Sdim  // ProgramArgs is used.
699212793Sdim  std::string Exec;
700212793Sdim
701261991Sdim  if (RemoteClientPath.empty())
702193323Sed    ProgramArgs.push_back(OutputBinary.c_str());
703193323Sed  else {
704193323Sed    ProgramArgs.push_back(RemoteClientPath.c_str());
705193323Sed    ProgramArgs.push_back(RemoteHost.c_str());
706198090Srdivacky    if (!RemoteUser.empty()) {
707198090Srdivacky      ProgramArgs.push_back("-l");
708198090Srdivacky      ProgramArgs.push_back(RemoteUser.c_str());
709198090Srdivacky    }
710198090Srdivacky    if (!RemotePort.empty()) {
711198090Srdivacky      ProgramArgs.push_back("-p");
712198090Srdivacky      ProgramArgs.push_back(RemotePort.c_str());
713198090Srdivacky    }
714193323Sed    if (!RemoteExtra.empty()) {
715193323Sed      ProgramArgs.push_back(RemoteExtra.c_str());
716193323Sed    }
717193323Sed
718198090Srdivacky    // Full path to the binary. We need to cd to the exec directory because
719198090Srdivacky    // there is a dylib there that the exec expects to find in the CWD
720314564Sdim    char *env_pwd = getenv("PWD");
721212793Sdim    Exec = "cd ";
722193323Sed    Exec += env_pwd;
723193323Sed    Exec += "; ./";
724193323Sed    Exec += OutputBinary.c_str();
725193323Sed    ProgramArgs.push_back(Exec.c_str());
726193323Sed  }
727193323Sed
728193323Sed  // Add optional parameters to the running program from Argv
729207618Srdivacky  for (unsigned i = 0, e = Args.size(); i != e; ++i)
730193323Sed    ProgramArgs.push_back(Args[i].c_str());
731314564Sdim  ProgramArgs.push_back(nullptr); // NULL terminator
732193323Sed
733193323Sed  // Now that we have a binary, run it!
734314564Sdim  outs() << "<program>";
735314564Sdim  outs().flush();
736198090Srdivacky  DEBUG(errs() << "\nAbout to run:\t";
737314564Sdim        for (unsigned i = 0, e = ProgramArgs.size() - 1; i != e; ++i) errs()
738314564Sdim        << " " << ProgramArgs[i];
739314564Sdim        errs() << "\n";);
740193323Sed
741221337Sdim  FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
742193323Sed
743261991Sdim  if (RemoteClientPath.empty()) {
744207618Srdivacky    DEBUG(errs() << "<run locally>");
745314564Sdim    std::string Error;
746261991Sdim    int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
747261991Sdim                                         InputFile, OutputFile, OutputFile,
748314564Sdim                                         Timeout, MemoryLimit, &Error);
749223013Sdim    // Treat a signal (usually SIGSEGV) or timeout as part of the program output
750223013Sdim    // so that crash-causing miscompilation is handled seamlessly.
751223013Sdim    if (ExitCode < -1) {
752223013Sdim      std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
753314564Sdim      outFile << Error << '\n';
754223013Sdim      outFile.close();
755223013Sdim    }
756223013Sdim    return ExitCode;
757198090Srdivacky  } else {
758314564Sdim    outs() << "<run remotely>";
759314564Sdim    outs().flush();
760314564Sdim    return RunProgramRemotelyWithTimeout(RemoteClientPath, &ProgramArgs[0],
761314564Sdim                                         InputFile, OutputFile, OutputFile,
762314564Sdim                                         Timeout, MemoryLimit);
763198090Srdivacky  }
764193323Sed}
765193323Sed
766314564SdimError CC::MakeSharedObject(const std::string &InputFile, FileType fileType,
767314564Sdim                           std::string &OutputFile,
768314564Sdim                           const std::vector<std::string> &ArgsForCC) {
769261991Sdim  SmallString<128> UniqueFilename;
770276479Sdim  std::error_code EC = sys::fs::createUniqueFile(
771261991Sdim      InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
772261991Sdim  if (EC) {
773261991Sdim    errs() << "Error making unique filename: " << EC.message() << "\n";
774193323Sed    exit(1);
775193323Sed  }
776261991Sdim  OutputFile = UniqueFilename.str();
777193323Sed
778314564Sdim  std::vector<const char *> CCArgs;
779218885Sdim
780296417Sdim  CCArgs.push_back(CCPath.c_str());
781193323Sed
782205218Srdivacky  if (TargetTriple.getArch() == Triple::x86)
783296417Sdim    CCArgs.push_back("-m32");
784205218Srdivacky
785314564Sdim  for (std::vector<std::string>::const_iterator I = ccArgs.begin(),
786314564Sdim                                                E = ccArgs.end();
787314564Sdim       I != E; ++I)
788296417Sdim    CCArgs.push_back(I->c_str());
789193323Sed
790193323Sed  // Compile the C/asm file into a shared object
791205218Srdivacky  if (fileType != ObjectFile) {
792296417Sdim    CCArgs.push_back("-x");
793296417Sdim    CCArgs.push_back(fileType == AsmFile ? "assembler" : "c");
794205218Srdivacky  }
795296417Sdim  CCArgs.push_back("-fno-strict-aliasing");
796314564Sdim  CCArgs.push_back(InputFile.c_str()); // Specify the input filename.
797296417Sdim  CCArgs.push_back("-x");
798296417Sdim  CCArgs.push_back("none");
799198090Srdivacky  if (TargetTriple.getArch() == Triple::sparc)
800314564Sdim    CCArgs.push_back("-G"); // Compile a shared library, `-G' for Sparc
801221337Sdim  else if (TargetTriple.isOSDarwin()) {
802198090Srdivacky    // link all source files into a single module in data segment, rather than
803218885Sdim    // generating blocks. dynamic_lookup requires that you set
804198090Srdivacky    // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.  FIXME: it would be better for
805296417Sdim    // bugpoint to just pass that in the environment of CC.
806296417Sdim    CCArgs.push_back("-single_module");
807314564Sdim    CCArgs.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
808296417Sdim    CCArgs.push_back("-undefined");
809296417Sdim    CCArgs.push_back("dynamic_lookup");
810198090Srdivacky  } else
811314564Sdim    CCArgs.push_back("-shared"); // `-shared' for Linux/X86, maybe others
812193323Sed
813234353Sdim  if (TargetTriple.getArch() == Triple::x86_64)
814314564Sdim    CCArgs.push_back("-fPIC"); // Requires shared objs to contain PIC
815198090Srdivacky
816198090Srdivacky  if (TargetTriple.getArch() == Triple::sparc)
817296417Sdim    CCArgs.push_back("-mcpu=v9");
818198090Srdivacky
819296417Sdim  CCArgs.push_back("-o");
820296417Sdim  CCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
821296417Sdim  CCArgs.push_back("-O2");              // Optimize the program a bit.
822193323Sed
823296417Sdim  // Add any arguments intended for CC. We locate them here because this is
824193323Sed  // most likely -L and -l options that need to come before other libraries but
825193323Sed  // after the source. Other options won't be sensitive to placement on the
826193323Sed  // command line, so this should be safe.
827296417Sdim  for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
828296417Sdim    CCArgs.push_back(ArgsForCC[i].c_str());
829314564Sdim  CCArgs.push_back(nullptr); // NULL terminator
830193323Sed
831314564Sdim  outs() << "<CC>";
832314564Sdim  outs().flush();
833198090Srdivacky  DEBUG(errs() << "\nAbout to run:\t";
834314564Sdim        for (unsigned i = 0, e = CCArgs.size() - 1; i != e; ++i) errs()
835314564Sdim        << " " << CCArgs[i];
836314564Sdim        errs() << "\n";);
837314564Sdim  if (RunProgramWithTimeout(CCPath, &CCArgs[0], "", "", ""))
838314564Sdim    return ProcessFailure(CCPath, &CCArgs[0]);
839314564Sdim  return Error::success();;
840193323Sed}
841193323Sed
842296417Sdim/// create - Try to find the CC executable
843193323Sed///
844314564SdimCC *CC::create(std::string &Message, const std::string &CCBinary,
845314564Sdim               const std::vector<std::string> *Args) {
846296417Sdim  auto CCPath = sys::findProgramByName(CCBinary);
847296417Sdim  if (!CCPath) {
848296417Sdim    Message = "Cannot find `" + CCBinary + "' in PATH: " +
849296417Sdim              CCPath.getError().message() + "\n";
850276479Sdim    return nullptr;
851193323Sed  }
852193323Sed
853261991Sdim  std::string RemoteClientPath;
854280031Sdim  if (!RemoteClient.empty()) {
855280031Sdim    auto Path = sys::findProgramByName(RemoteClient);
856280031Sdim    if (!Path) {
857280031Sdim      Message = "Cannot find `" + RemoteClient + "' in PATH: " +
858280031Sdim                Path.getError().message() + "\n";
859280031Sdim      return nullptr;
860280031Sdim    }
861280031Sdim    RemoteClientPath = *Path;
862280031Sdim  }
863193323Sed
864296417Sdim  Message = "Found CC: " + *CCPath + "\n";
865296417Sdim  return new CC(*CCPath, RemoteClientPath, Args);
866193323Sed}
867