Regen configure
[oota-llvm.git] / tools / bugpoint / ToolRunner.cpp
index 4baafd127b2f199ee1b43595e5a57ec1546644e5..829f108d8cbeb4dd56d7a609ed4e0f95b6bbd9cd 100644 (file)
@@ -13,7 +13,7 @@
 
 #define DEBUG_TYPE "toolrunner"
 #include "ToolRunner.h"
-#include "llvm/System/Program.h"
+#include "llvm/Support/Program.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/FileUtilities.h"
@@ -59,7 +59,8 @@ static int RunProgramWithTimeout(const sys::Path &ProgramPath,
                                  const sys::Path &StdOutFile,
                                  const sys::Path &StdErrFile,
                                  unsigned NumSeconds = 0,
-                                 unsigned MemoryLimit = 0) {
+                                 unsigned MemoryLimit = 0,
+                                 std::string *ErrMsg = 0) {
   const sys::Path* redirects[3];
   redirects[0] = &StdInFile;
   redirects[1] = &StdOutFile;
@@ -76,7 +77,7 @@ static int RunProgramWithTimeout(const sys::Path &ProgramPath,
 
   return
     sys::Program::ExecuteAndWait(ProgramPath, Args, 0, redirects,
-                                 NumSeconds, MemoryLimit);
+                                 NumSeconds, MemoryLimit, ErrMsg);
 }
 
 /// RunProgramRemotelyWithTimeout - This function runs the given program
@@ -133,13 +134,15 @@ static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
   return ReturnCode;
 }
 
-static std::string ProcessFailure(sys::Path ProgPath, const char** Args) {
+static std::string ProcessFailure(sys::Path ProgPath, const char** Args,
+                                  unsigned Timeout = 0,
+                                  unsigned MemoryLimit = 0) {
   std::ostringstream OS;
   OS << "\nError running tool:\n ";
   for (const char **Arg = Args; *Arg; ++Arg)
     OS << " " << *Arg;
   OS << "\n";
-  
+
   // Rerun the compiler, capturing any error messages to print them.
   sys::Path ErrorFilename("bugpoint.program_error_messages");
   std::string ErrMsg;
@@ -148,7 +151,8 @@ static std::string ProcessFailure(sys::Path ProgPath, const char** Args) {
     exit(1);
   }
   RunProgramWithTimeout(ProgPath, Args, sys::Path(""), ErrorFilename,
-                        ErrorFilename); // FIXME: check return code ?
+                        ErrorFilename, Timeout, MemoryLimit);
+  // FIXME: check return code ?
 
   // Print out the error messages generated by GCC if possible...
   std::ifstream ErrorFile(ErrorFilename.c_str());
@@ -203,7 +207,8 @@ int LLI::ExecuteProgram(const std::string &Bitcode,
   LLIArgs.push_back(LLIPath.c_str());
   LLIArgs.push_back("-force-interpreter=true");
 
-  for (std::vector<std::string>::const_iterator i = SharedLibs.begin(), e = SharedLibs.end(); i != e; ++i) {
+  for (std::vector<std::string>::const_iterator i = SharedLibs.begin(),
+         e = SharedLibs.end(); i != e; ++i) {
     LLIArgs.push_back("-load");
     LLIArgs.push_back((*i).c_str());
   }
@@ -226,7 +231,7 @@ int LLI::ExecuteProgram(const std::string &Bitcode,
         );
   return RunProgramWithTimeout(sys::Path(LLIPath), &LLIArgs[0],
       sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
-      Timeout, MemoryLimit);
+      Timeout, MemoryLimit, Error);
 }
 
 // LLI create method - Try to find the LLI executable
@@ -234,13 +239,13 @@ AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
                                                     std::string &Message,
                                      const std::vector<std::string> *ToolArgs) {
   std::string LLIPath =
-    FindExecutable("lli", Argv0, (void *)(intptr_t)&createLLI).str();
+    PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI).str();
   if (!LLIPath.empty()) {
     Message = "Found lli: " + LLIPath + "\n";
     return new LLI(LLIPath, ToolArgs);
   }
 
-  Message = "Cannot find `lli' in executable directory or PATH!\n";
+  Message = "Cannot find `lli' in executable directory!\n";
   return 0;
 }
 
@@ -248,7 +253,7 @@ AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
 // Custom execution command implementation of AbstractIntepreter interface
 //
 // Allows using a custom command for executing the bitcode, thus allows,
-// for example, to invoke a cross compiler for code generation followed by 
+// for example, to invoke a cross compiler for code generation followed by
 // a simulator that executes the generated binary.
 namespace {
   class CustomExecutor : public AbstractInterpreter {
@@ -296,8 +301,8 @@ int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
 
   return RunProgramWithTimeout(
     sys::Path(ExecutionCommand),
-    &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile), 
-    sys::Path(OutputFile), Timeout, MemoryLimit);
+    &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
+    sys::Path(OutputFile), Timeout, MemoryLimit, Error);
 }
 
 // Custom execution environment create method, takes the execution command
@@ -314,14 +319,14 @@ AbstractInterpreter *AbstractInterpreter::createCustom(
   // defining a full command line as the command instead of just the
   // executed program. We cannot just pass the whole string after the command
   // as a single argument because then program sees only a single
-  // command line argument (with spaces in it: "foo bar" instead 
+  // command line argument (with spaces in it: "foo bar" instead
   // of "foo" and "bar").
 
-  // code borrowed from: 
+  // code borrowed from:
   // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
-  std::string::size_type lastPos = 
+  std::string::size_type lastPos =
     ExecCommandLine.find_first_not_of(delimiters, 0);
-  std::string::size_type pos = 
+  std::string::size_type pos =
     ExecCommandLine.find_first_of(delimiters, lastPos);
 
   while (std::string::npos != pos || std::string::npos != lastPos) {
@@ -338,9 +343,9 @@ AbstractInterpreter *AbstractInterpreter::createCustom(
 
   std::string CmdPath = sys::Program::FindProgramByName(Command).str();
   if (CmdPath.empty()) {
-    Message = 
-      std::string("Cannot find '") + Command + 
-      "' in executable directory or PATH!\n";
+    Message =
+      std::string("Cannot find '") + Command +
+      "' in PATH!\n";
     return 0;
   }
 
@@ -352,8 +357,9 @@ AbstractInterpreter *AbstractInterpreter::createCustom(
 //===----------------------------------------------------------------------===//
 // LLC Implementation of AbstractIntepreter interface
 //
-GCC::FileType LLC::OutputCode(const std::string &Bitcode, 
-                              sys::Path &OutputAsmFile, std::string &Error) {
+GCC::FileType LLC::OutputCode(const std::string &Bitcode,
+                              sys::Path &OutputAsmFile, std::string &Error,
+                              unsigned Timeout, unsigned MemoryLimit) {
   const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
   sys::Path uniqueFile(Bitcode + Suffix);
   std::string ErrMsg;
@@ -372,10 +378,10 @@ GCC::FileType LLC::OutputCode(const std::string &Bitcode,
   LLCArgs.push_back("-o");
   LLCArgs.push_back(OutputAsmFile.c_str()); // Output to the Asm file
   LLCArgs.push_back(Bitcode.c_str());      // This is the input bitcode
-  
+
   if (UseIntegratedAssembler)
     LLCArgs.push_back("-filetype=obj");
-  
+
   LLCArgs.push_back (0);
 
   outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
@@ -386,14 +392,17 @@ GCC::FileType LLC::OutputCode(const std::string &Bitcode,
         errs() << "\n";
         );
   if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
-                            sys::Path(), sys::Path(), sys::Path()))
-    Error = ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
-  return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;  
+                            sys::Path(), sys::Path(), sys::Path(),
+                            Timeout, MemoryLimit))
+    Error = ProcessFailure(sys::Path(LLCPath), &LLCArgs[0],
+                           Timeout, MemoryLimit);
+  return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
 }
 
-void LLC::compileProgram(const std::string &Bitcode, std::string *Error) {
+void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
+                         unsigned Timeout, unsigned MemoryLimit) {
   sys::Path OutputAsmFile;
-  OutputCode(Bitcode, OutputAsmFile, *Error);
+  OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
   OutputAsmFile.eraseFromDisk();
 }
 
@@ -408,7 +417,8 @@ int LLC::ExecuteProgram(const std::string &Bitcode,
                         unsigned MemoryLimit) {
 
   sys::Path OutputAsmFile;
-  GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error);
+  GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
+                                      MemoryLimit);
   FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
 
   std::vector<std::string> GCCArgs(ArgsForGCC);
@@ -429,9 +439,9 @@ LLC *AbstractInterpreter::createLLC(const char *Argv0,
                                     const std::vector<std::string> *GCCArgs,
                                     bool UseIntegratedAssembler) {
   std::string LLCPath =
-    FindExecutable("llc", Argv0, (void *)(intptr_t)&createLLC).str();
+    PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC).str();
   if (LLCPath.empty()) {
-    Message = "Cannot find `llc' in executable directory or PATH!\n";
+    Message = "Cannot find `llc' in executable directory!\n";
     return 0;
   }
 
@@ -466,7 +476,7 @@ namespace {
                                const std::vector<std::string> &GCCArgs =
                                  std::vector<std::string>(),
                                const std::vector<std::string> &SharedLibs =
-                                 std::vector<std::string>(), 
+                                 std::vector<std::string>(),
                                unsigned Timeout = 0,
                                unsigned MemoryLimit = 0);
   };
@@ -509,7 +519,7 @@ int JIT::ExecuteProgram(const std::string &Bitcode,
   DEBUG(errs() << "\nSending output to " << OutputFile << "\n");
   return RunProgramWithTimeout(sys::Path(LLIPath), &JITArgs[0],
       sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
-      Timeout, MemoryLimit);
+      Timeout, MemoryLimit, Error);
 }
 
 /// createJIT - Try to find the LLI executable
@@ -517,18 +527,19 @@ int JIT::ExecuteProgram(const std::string &Bitcode,
 AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
                    std::string &Message, const std::vector<std::string> *Args) {
   std::string LLIPath =
-    FindExecutable("lli", Argv0, (void *)(intptr_t)&createJIT).str();
+    PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT).str();
   if (!LLIPath.empty()) {
     Message = "Found lli: " + LLIPath + "\n";
     return new JIT(LLIPath, Args);
   }
 
-  Message = "Cannot find `lli' in executable directory or PATH!\n";
+  Message = "Cannot find `lli' in executable directory!\n";
   return 0;
 }
 
 GCC::FileType CBE::OutputCode(const std::string &Bitcode,
-                              sys::Path &OutputCFile, std::string &Error) {
+                              sys::Path &OutputCFile, std::string &Error,
+                              unsigned Timeout, unsigned MemoryLimit) {
   sys::Path uniqueFile(Bitcode+".cbe.c");
   std::string ErrMsg;
   if (uniqueFile.makeUnique(true, &ErrMsg)) {
@@ -556,14 +567,15 @@ GCC::FileType CBE::OutputCode(const std::string &Bitcode,
         errs() << "\n";
         );
   if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
-                            sys::Path()))
-    Error = ProcessFailure(LLCPath, &LLCArgs[0]);
+                            sys::Path(), Timeout, MemoryLimit))
+    Error = ProcessFailure(LLCPath, &LLCArgs[0], Timeout, MemoryLimit);
   return GCC::CFile;
 }
 
-void CBE::compileProgram(const std::string &Bitcode, std::string *Error) {
+void CBE::compileProgram(const std::string &Bitcode, std::string *Error,
+                         unsigned Timeout, unsigned MemoryLimit) {
   sys::Path OutputCFile;
-  OutputCode(Bitcode, OutputCFile, *Error);
+  OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit);
   OutputCFile.eraseFromDisk();
 }
 
@@ -577,7 +589,7 @@ int CBE::ExecuteProgram(const std::string &Bitcode,
                         unsigned Timeout,
                         unsigned MemoryLimit) {
   sys::Path OutputCFile;
-  OutputCode(Bitcode, OutputCFile, *Error);
+  OutputCode(Bitcode, OutputCFile, *Error, Timeout, MemoryLimit);
 
   FileRemover CFileRemove(OutputCFile, !SaveTemps);
 
@@ -593,14 +605,14 @@ int CBE::ExecuteProgram(const std::string &Bitcode,
 ///
 CBE *AbstractInterpreter::createCBE(const char *Argv0,
                                     std::string &Message,
-                                    const std::string &GCCBinary, 
+                                    const std::string &GCCBinary,
                                     const std::vector<std::string> *Args,
                                     const std::vector<std::string> *GCCArgs) {
   sys::Path LLCPath =
-    FindExecutable("llc", Argv0, (void *)(intptr_t)&createCBE);
+    PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createCBE);
   if (LLCPath.isEmpty()) {
     Message =
-      "Cannot find `llc' in executable directory or PATH!\n";
+      "Cannot find `llc' in executable directory!\n";
     return 0;
   }
 
@@ -617,8 +629,8 @@ CBE *AbstractInterpreter::createCBE(const char *Argv0,
 // GCC abstraction
 //
 
-static bool IsARMArchitecture(std::vector<std::string> Args) {
-  for (std::vector<std::string>::const_iterator
+static bool IsARMArchitecture(std::vector<const char*> Args) {
+  for (std::vector<const char*>::const_iterator
          I = Args.begin(), E = Args.end(); I != E; ++I) {
     if (StringRef(*I).equals_lower("-arch")) {
       ++I;
@@ -663,13 +675,13 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
       // explicitly told what architecture it is working on, so we get
       // it from gcc flags
       if ((TargetTriple.getOS() == Triple::Darwin) &&
-          !IsARMArchitecture(ArgsForGCC))
+          !IsARMArchitecture(GCCArgs))
         GCCArgs.push_back("-force_cpusubtype_ALL");
     }
   }
-  
+
   GCCArgs.push_back(ProgramFile.c_str());  // Specify the input filename.
-  
+
   GCCArgs.push_back("-x");
   GCCArgs.push_back("none");
   GCCArgs.push_back("-o");
@@ -711,6 +723,10 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
 
   std::vector<const char*> ProgramArgs;
 
+  // Declared here so that the destructor only runs after
+  // ProgramArgs is used.
+  std::string Exec;
+
   if (RemoteClientPath.isEmpty())
     ProgramArgs.push_back(OutputBinary.c_str());
   else {
@@ -731,7 +747,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
     // Full path to the binary. We need to cd to the exec directory because
     // there is a dylib there that the exec expects to find in the CWD
     char* env_pwd = getenv("PWD");
-    std::string Exec = "cd ";
+    Exec = "cd ";
     Exec += env_pwd;
     Exec += "; ./";
     Exec += OutputBinary.c_str();
@@ -757,7 +773,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
     DEBUG(errs() << "<run locally>");
     return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
         sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
-        Timeout, MemoryLimit);
+        Timeout, MemoryLimit, Error);
   } else {
     outs() << "<run remotely>"; outs().flush();
     return RunProgramRemotelyWithTimeout(sys::Path(RemoteClientPath),
@@ -779,7 +795,7 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
   OutputFile = uniqueFilename.str();
 
   std::vector<const char*> GCCArgs;
-  
+
   GCCArgs.push_back(GCCPath.c_str());
 
   if (TargetTriple.getArch() == Triple::x86)
@@ -802,7 +818,7 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
     GCCArgs.push_back("-G");       // Compile a shared library, `-G' for Sparc
   else if (TargetTriple.getOS() == Triple::Darwin) {
     // link all source files into a single module in data segment, rather than
-    // generating blocks. dynamic_lookup requires that you set 
+    // generating blocks. dynamic_lookup requires that you set
     // MACOSX_DEPLOYMENT_TARGET=10.3 in your env.  FIXME: it would be better for
     // bugpoint to just pass that in the environment of GCC.
     GCCArgs.push_back("-single_module");
@@ -823,8 +839,8 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
   GCCArgs.push_back(OutputFile.c_str()); // Output to the right filename.
   GCCArgs.push_back("-O2");              // Optimize the program a bit.
 
-  
-  
+
+
   // Add any arguments intended for GCC. We locate them here because this is
   // most likely -L and -l options that need to come before other libraries but
   // after the source. Other options won't be sensitive to placement on the
@@ -833,7 +849,7 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
     GCCArgs.push_back(ArgsForGCC[i].c_str());
   GCCArgs.push_back(0);                    // NULL terminator
 
-  
+
 
   outs() << "<gcc>"; outs().flush();
   DEBUG(errs() << "\nAbout to run:\t";
@@ -856,7 +872,7 @@ GCC *GCC::create(std::string &Message,
                  const std::vector<std::string> *Args) {
   sys::Path GCCPath = sys::Program::FindProgramByName(GCCBinary);
   if (GCCPath.isEmpty()) {
-    Message = "Cannot find `"+ GCCBinary +"' in executable directory or PATH!\n";
+    Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
     return 0;
   }