[docs] Fix some links
[oota-llvm.git] / tools / bugpoint / ToolRunner.cpp
index 92250418d1bf7ffadd7f43de76fcc8c783420e91..f0fb4bf6f53b2aa924735d29e4ed4f1c11bb8d94 100644 (file)
@@ -16,6 +16,7 @@
 #include "llvm/Config/config.h"   // for HAVE_LINK_R
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FileUtilities.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/raw_ostream.h"
@@ -61,11 +62,7 @@ static int RunProgramWithTimeout(StringRef ProgramPath,
                                  unsigned NumSeconds = 0,
                                  unsigned MemoryLimit = 0,
                                  std::string *ErrMsg = 0) {
-  const sys::Path P[3] = { sys::Path(StdInFile), sys::Path(StdOutFile),
-                           sys::Path(StdErrFile) };
-  const sys::Path* redirects[3];
-  for (int I = 0; I < 3; ++I)
-    redirects[I] = &P[I];
+  const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
 
 #if 0 // For debug purposes
   {
@@ -76,7 +73,7 @@ static int RunProgramWithTimeout(StringRef ProgramPath,
   }
 #endif
 
-  return sys::ExecuteAndWait(sys::Path(ProgramPath), Args, 0, redirects,
+  return sys::ExecuteAndWait(ProgramPath, Args, 0, Redirects,
                              NumSeconds, MemoryLimit, ErrMsg);
 }
 
@@ -93,11 +90,7 @@ static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
                                          StringRef StdErrFile,
                                          unsigned NumSeconds = 0,
                                          unsigned MemoryLimit = 0) {
-  const sys::Path P[3] = { sys::Path(StdInFile), sys::Path(StdOutFile),
-                           sys::Path(StdErrFile) };
-  const sys::Path* redirects[3];
-  for (int I = 0; I < 3; ++I)
-    redirects[I] = &P[I];
+  const StringRef *Redirects[3] = { &StdInFile, &StdOutFile, &StdErrFile };
 
 #if 0 // For debug purposes
   {
@@ -109,8 +102,8 @@ static int RunProgramRemotelyWithTimeout(StringRef RemoteClientPath,
 #endif
 
   // Run the program remotely with the remote client
-  int ReturnCode = sys::ExecuteAndWait(sys::Path(RemoteClientPath), Args, 0,
-                                       redirects, NumSeconds, MemoryLimit);
+  int ReturnCode = sys::ExecuteAndWait(RemoteClientPath, Args, 0,
+                                       Redirects, NumSeconds, MemoryLimit);
 
   // Has the remote client fail?
   if (255 == ReturnCode) {
@@ -146,10 +139,12 @@ static std::string ProcessFailure(StringRef ProgPath, const char** Args,
   OS << "\n";
 
   // Rerun the compiler, capturing any error messages to print them.
-  sys::Path ErrorFilename("bugpoint.program_error_messages");
-  std::string ErrMsg;
-  if (ErrorFilename.makeUnique(true, &ErrMsg)) {
-    errs() << "Error making unique filename: " << ErrMsg << "\n";
+  SmallString<128> ErrorFilename;
+  int ErrorFD;
+  error_code EC = sys::fs::createTemporaryFile(
+      "bugpoint.program_error_messages", "", ErrorFD, ErrorFilename);
+  if (EC) {
+    errs() << "Error making unique filename: " << EC.message() << "\n";
     exit(1);
   }
   RunProgramWithTimeout(ProgPath, Args, "", ErrorFilename.str(),
@@ -165,7 +160,7 @@ static std::string ProcessFailure(StringRef ProgPath, const char** Args,
     ErrorFile.close();
   }
 
-  ErrorFilename.eraseFromDisk();
+  sys::fs::remove(ErrorFilename.c_str());
   return OS.str();
 }
 
@@ -183,16 +178,16 @@ namespace {
       if (Args) { ToolArgs = *Args; }
     }
 
-    virtual int ExecuteProgram(const std::string &Bitcode,
-                               const std::vector<std::string> &Args,
-                               const std::string &InputFile,
-                               const std::string &OutputFile,
-                               std::string *Error,
-                               const std::vector<std::string> &GCCArgs,
-                               const std::vector<std::string> &SharedLibs =
-                               std::vector<std::string>(),
-                               unsigned Timeout = 0,
-                               unsigned MemoryLimit = 0);
+    int ExecuteProgram(const std::string &Bitcode,
+                       const std::vector<std::string> &Args,
+                       const std::string &InputFile,
+                       const std::string &OutputFile,
+                       std::string *Error,
+                       const std::vector<std::string> &GCCArgs,
+                       const std::vector<std::string> &SharedLibs =
+                       std::vector<std::string>(),
+                       unsigned Timeout = 0,
+                       unsigned MemoryLimit = 0) override;
   };
 }
 
@@ -238,12 +233,43 @@ int LLI::ExecuteProgram(const std::string &Bitcode,
 
 void AbstractInterpreter::anchor() { }
 
+#if defined(LLVM_ON_UNIX)
+const char EXESuffix[] = "";
+#elif defined (LLVM_ON_WIN32)
+const char EXESuffix[] = "exe";
+#endif
+
+/// Prepend the path to the program being executed
+/// to \p ExeName, given the value of argv[0] and the address of main()
+/// itself. This allows us to find another LLVM tool if it is built in the same
+/// directory. An empty string is returned on error; note that this function
+/// just mainpulates the path and doesn't check for executability.
+/// @brief Find a named executable.
+static std::string PrependMainExecutablePath(const std::string &ExeName,
+                                             const char *Argv0,
+                                             void *MainAddr) {
+  // Check the directory that the calling program is in.  We can do
+  // this if ProgramPath contains at least one / character, indicating that it
+  // is a relative path to the executable itself.
+  std::string Main = sys::fs::getMainExecutable(Argv0, MainAddr);
+  StringRef Result = sys::path::parent_path(Main);
+
+  if (!Result.empty()) {
+    SmallString<128> Storage = Result;
+    sys::path::append(Storage, ExeName);
+    sys::path::replace_extension(Storage, EXESuffix);
+    return Storage.str();
+  }
+
+  return Result.str();
+}
+
 // LLI create method - Try to find the LLI executable
 AbstractInterpreter *AbstractInterpreter::createLLI(const char *Argv0,
                                                     std::string &Message,
                                      const std::vector<std::string> *ToolArgs) {
   std::string LLIPath =
-    PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createLLI).str();
+      PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createLLI);
   if (!LLIPath.empty()) {
     Message = "Found lli: " + LLIPath + "\n";
     return new LLI(LLIPath, ToolArgs);
@@ -268,22 +294,22 @@ namespace {
       const std::string &CompilerCmd, std::vector<std::string> CompArgs) :
       CompilerCommand(CompilerCmd), CompilerArgs(CompArgs) {}
 
-    virtual void compileProgram(const std::string &Bitcode,
-                                std::string *Error,
-                                unsigned Timeout = 0,
-                                unsigned MemoryLimit = 0);
-
-    virtual int ExecuteProgram(const std::string &Bitcode,
-                               const std::vector<std::string> &Args,
-                               const std::string &InputFile,
-                               const std::string &OutputFile,
-                               std::string *Error,
-                               const std::vector<std::string> &GCCArgs =
-                               std::vector<std::string>(),
-                               const std::vector<std::string> &SharedLibs =
-                               std::vector<std::string>(),
-                               unsigned Timeout = 0,
-                               unsigned MemoryLimit = 0) {
+    void compileProgram(const std::string &Bitcode,
+                        std::string *Error,
+                        unsigned Timeout = 0,
+                        unsigned MemoryLimit = 0) override;
+
+    int ExecuteProgram(const std::string &Bitcode,
+                       const std::vector<std::string> &Args,
+                       const std::string &InputFile,
+                       const std::string &OutputFile,
+                       std::string *Error,
+                       const std::vector<std::string> &GCCArgs =
+                       std::vector<std::string>(),
+                       const std::vector<std::string> &SharedLibs =
+                       std::vector<std::string>(),
+                       unsigned Timeout = 0,
+                       unsigned MemoryLimit = 0) override {
       *Error = "Execution not supported with -compile-custom";
       return -1;
     }
@@ -329,16 +355,16 @@ namespace {
       const std::string &ExecutionCmd, std::vector<std::string> ExecArgs) :
       ExecutionCommand(ExecutionCmd), ExecutorArgs(ExecArgs) {}
 
-    virtual int ExecuteProgram(const std::string &Bitcode,
-                               const std::vector<std::string> &Args,
-                               const std::string &InputFile,
-                               const std::string &OutputFile,
-                               std::string *Error,
-                               const std::vector<std::string> &GCCArgs,
-                               const std::vector<std::string> &SharedLibs =
-                                 std::vector<std::string>(),
-                               unsigned Timeout = 0,
-                               unsigned MemoryLimit = 0);
+    int ExecuteProgram(const std::string &Bitcode,
+                       const std::vector<std::string> &Args,
+                       const std::string &InputFile,
+                       const std::string &OutputFile,
+                       std::string *Error,
+                       const std::vector<std::string> &GCCArgs,
+                       const std::vector<std::string> &SharedLibs =
+                         std::vector<std::string>(),
+                       unsigned Timeout = 0,
+                       unsigned MemoryLimit = 0) override;
   };
 }
 
@@ -380,7 +406,7 @@ int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
 // code borrowed from:
 // http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
 static void lexCommand(std::string &Message, const std::string &CommandLine,
-                       std::string &CmdPath, std::vector<std::string> Args) {
+                       std::string &CmdPath, std::vector<std::string> &Args) {
 
   std::string Command = "";
   std::string delimiters = " ";
@@ -400,7 +426,7 @@ static void lexCommand(std::string &Message, const std::string &CommandLine,
     pos = CommandLine.find_first_of(delimiters, lastPos);
   }
 
-  CmdPath = sys::FindProgramByName(Command).str();
+  CmdPath = sys::FindProgramByName(Command);
   if (CmdPath.empty()) {
     Message =
       std::string("Cannot find '") + Command +
@@ -446,16 +472,18 @@ AbstractInterpreter *AbstractInterpreter::createCustomExecutor(
 // LLC Implementation of AbstractIntepreter interface
 //
 GCC::FileType LLC::OutputCode(const std::string &Bitcode,
-                              sys::Path &OutputAsmFile, std::string &Error,
+                              std::string &OutputAsmFile, std::string &Error,
                               unsigned Timeout, unsigned MemoryLimit) {
   const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
-  sys::Path uniqueFile(Bitcode + Suffix);
-  std::string ErrMsg;
-  if (uniqueFile.makeUnique(true, &ErrMsg)) {
-    errs() << "Error making unique filename: " << ErrMsg << "\n";
+
+  SmallString<128> UniqueFile;
+  error_code EC =
+      sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
+  if (EC) {
+    errs() << "Error making unique filename: " << EC.message() << "\n";
     exit(1);
   }
-  OutputAsmFile = uniqueFile;
+  OutputAsmFile = UniqueFile.str();
   std::vector<const char *> LLCArgs;
   LLCArgs.push_back(LLCPath.c_str());
 
@@ -489,9 +517,9 @@ GCC::FileType LLC::OutputCode(const std::string &Bitcode,
 
 void LLC::compileProgram(const std::string &Bitcode, std::string *Error,
                          unsigned Timeout, unsigned MemoryLimit) {
-  sys::Path OutputAsmFile;
+  std::string OutputAsmFile;
   OutputCode(Bitcode, OutputAsmFile, *Error, Timeout, MemoryLimit);
-  OutputAsmFile.eraseFromDisk();
+  sys::fs::remove(OutputAsmFile);
 }
 
 int LLC::ExecuteProgram(const std::string &Bitcode,
@@ -504,16 +532,16 @@ int LLC::ExecuteProgram(const std::string &Bitcode,
                         unsigned Timeout,
                         unsigned MemoryLimit) {
 
-  sys::Path OutputAsmFile;
+  std::string OutputAsmFile;
   GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error, Timeout,
                                       MemoryLimit);
-  FileRemover OutFileRemover(OutputAsmFile.str(), !SaveTemps);
+  FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
 
   std::vector<std::string> GCCArgs(ArgsForGCC);
   GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
 
   // Assuming LLC worked, compile the result with GCC and run it.
-  return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind,
+  return gcc->ExecuteProgram(OutputAsmFile, Args, FileKind,
                              InputFile, OutputFile, Error, GCCArgs,
                              Timeout, MemoryLimit);
 }
@@ -527,7 +555,7 @@ LLC *AbstractInterpreter::createLLC(const char *Argv0,
                                     const std::vector<std::string> *GCCArgs,
                                     bool UseIntegratedAssembler) {
   std::string LLCPath =
-    PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t)&createLLC).str();
+      PrependMainExecutablePath("llc", Argv0, (void *)(intptr_t) & createLLC);
   if (LLCPath.empty()) {
     Message = "Cannot find `llc' in executable directory!\n";
     return 0;
@@ -556,17 +584,17 @@ namespace {
       if (Args) { ToolArgs = *Args; }
     }
 
-    virtual int ExecuteProgram(const std::string &Bitcode,
-                               const std::vector<std::string> &Args,
-                               const std::string &InputFile,
-                               const std::string &OutputFile,
-                               std::string *Error,
-                               const std::vector<std::string> &GCCArgs =
-                                 std::vector<std::string>(),
-                               const std::vector<std::string> &SharedLibs =
-                                 std::vector<std::string>(),
-                               unsigned Timeout = 0,
-                               unsigned MemoryLimit = 0);
+    int ExecuteProgram(const std::string &Bitcode,
+                       const std::vector<std::string> &Args,
+                       const std::string &InputFile,
+                       const std::string &OutputFile,
+                       std::string *Error,
+                       const std::vector<std::string> &GCCArgs =
+                         std::vector<std::string>(),
+                       const std::vector<std::string> &SharedLibs =
+                         std::vector<std::string>(),
+                       unsigned Timeout = 0,
+                       unsigned MemoryLimit = 0) override;
   };
 }
 
@@ -615,7 +643,7 @@ 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 =
-    PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t)&createJIT).str();
+      PrependMainExecutablePath("lli", Argv0, (void *)(intptr_t) & createJIT);
   if (!LLIPath.empty()) {
     Message = "Found lli: " + LLIPath + "\n";
     return new JIT(LLIPath, Args);
@@ -634,7 +662,7 @@ static bool IsARMArchitecture(std::vector<const char*> Args) {
          I = Args.begin(), E = Args.end(); I != E; ++I) {
     if (StringRef(*I).equals_lower("-arch")) {
       ++I;
-      if (I != E && StringRef(*I).substr(0, strlen("arm")).equals_lower("arm"))
+      if (I != E && StringRef(*I).startswith_lower("arm"))
         return true;
     }
   }
@@ -684,10 +712,12 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
   GCCArgs.push_back("-x");
   GCCArgs.push_back("none");
   GCCArgs.push_back("-o");
-  sys::Path OutputBinary (ProgramFile+".gcc.exe");
-  std::string ErrMsg;
-  if (OutputBinary.makeUnique(true, &ErrMsg)) {
-    errs() << "Error making unique filename: " << ErrMsg << "\n";
+
+  SmallString<128> OutputBinary;
+  error_code EC =
+      sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.gcc.exe", OutputBinary);
+  if (EC) {
+    errs() << "Error making unique filename: " << EC.message() << "\n";
     exit(1);
   }
   GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
@@ -714,8 +744,8 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
           errs() << " " << GCCArgs[i];
         errs() << "\n";
         );
-  if (RunProgramWithTimeout(GCCPath.str(), &GCCArgs[0], "", "", "")) {
-    *Error = ProcessFailure(GCCPath.str(), &GCCArgs[0]);
+  if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
+    *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
     return -1;
   }
 
@@ -725,7 +755,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
   // ProgramArgs is used.
   std::string Exec;
 
-  if (RemoteClientPath.isEmpty())
+  if (RemoteClientPath.empty())
     ProgramArgs.push_back(OutputBinary.c_str());
   else {
     ProgramArgs.push_back(RemoteClientPath.c_str());
@@ -767,7 +797,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
 
   FileRemover OutputBinaryRemover(OutputBinary.str(), !SaveTemps);
 
-  if (RemoteClientPath.isEmpty()) {
+  if (RemoteClientPath.empty()) {
     DEBUG(errs() << "<run locally>");
     int ExitCode = RunProgramWithTimeout(OutputBinary.str(), &ProgramArgs[0],
                                          InputFile, OutputFile, OutputFile,
@@ -783,7 +813,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
     return ExitCode;
   } else {
     outs() << "<run remotely>"; outs().flush();
-    return RunProgramRemotelyWithTimeout(RemoteClientPath.str(),
+    return RunProgramRemotelyWithTimeout(RemoteClientPath,
         &ProgramArgs[0], InputFile, OutputFile,
         OutputFile, Timeout, MemoryLimit);
   }
@@ -793,13 +823,14 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
                           std::string &OutputFile,
                           const std::vector<std::string> &ArgsForGCC,
                           std::string &Error) {
-  sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
-  std::string ErrMsg;
-  if (uniqueFilename.makeUnique(true, &ErrMsg)) {
-    errs() << "Error making unique filename: " << ErrMsg << "\n";
+  SmallString<128> UniqueFilename;
+  error_code EC = sys::fs::createUniqueFile(
+      InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
+  if (EC) {
+    errs() << "Error making unique filename: " << EC.message() << "\n";
     exit(1);
   }
-  OutputFile = uniqueFilename.str();
+  OutputFile = UniqueFilename.str();
 
   std::vector<const char*> GCCArgs;
 
@@ -863,8 +894,8 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
           errs() << " " << GCCArgs[i];
         errs() << "\n";
         );
-  if (RunProgramWithTimeout(GCCPath.str(), &GCCArgs[0], "", "", "")) {
-    Error = ProcessFailure(GCCPath.str(), &GCCArgs[0]);
+  if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], "", "", "")) {
+    Error = ProcessFailure(GCCPath, &GCCArgs[0]);
     return 1;
   }
   return 0;
@@ -875,16 +906,16 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
 GCC *GCC::create(std::string &Message,
                  const std::string &GCCBinary,
                  const std::vector<std::string> *Args) {
-  sys::Path GCCPath = sys::FindProgramByName(GCCBinary);
-  if (GCCPath.isEmpty()) {
+  std::string GCCPath = sys::FindProgramByName(GCCBinary);
+  if (GCCPath.empty()) {
     Message = "Cannot find `"+ GCCBinary +"' in PATH!\n";
     return 0;
   }
 
-  sys::Path RemoteClientPath;
+  std::string RemoteClientPath;
   if (!RemoteClient.empty())
     RemoteClientPath = sys::FindProgramByName(RemoteClient);
 
-  Message = "Found gcc: " + GCCPath.str() + "\n";
+  Message = "Found gcc: " + GCCPath + "\n";
   return new GCC(GCCPath, RemoteClientPath, Args);
 }