Move ToolRunner.(cpp|h) into the bugpoint directory
[oota-llvm.git] / tools / bugpoint / ToolRunner.cpp
index 9ef14c56773a19f93db5996623184b6739b68663..37f080ab9c18081c29695828e8b2b5ebee30b0c6 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #define DEBUG_TYPE "toolrunner"
-#include "llvm/Support/ToolRunner.h"
+#include "ToolRunner.h"
 #include "llvm/Config/config.h"   // for HAVE_LINK_R
 #include "llvm/System/Program.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/FileUtilities.h"
 #include <fstream>
 #include <sstream>
+#include <iostream>
 using namespace llvm;
 
 ToolExecutionError::~ToolExecutionError() throw() { }
@@ -65,7 +66,7 @@ static void ProcessFailure(sys::Path ProgPath, const char** Args) {
     ErrorFile.close();
   }
 
-  ErrorFilename.destroyFile();
+  ErrorFilename.eraseFromDisk();
   throw ToolExecutionError(OS.str());
 }
 
@@ -87,6 +88,7 @@ namespace {
                                const std::vector<std::string> &Args,
                                const std::string &InputFile,
                                const std::string &OutputFile,
+                               const std::vector<std::string> &GCCArgs,
                                const std::vector<std::string> &SharedLibs =
                                std::vector<std::string>(),
                                unsigned Timeout = 0);
@@ -97,12 +99,16 @@ int LLI::ExecuteProgram(const std::string &Bytecode,
                         const std::vector<std::string> &Args,
                         const std::string &InputFile,
                         const std::string &OutputFile,
+                        const std::vector<std::string> &GCCArgs,
                         const std::vector<std::string> &SharedLibs,
                         unsigned Timeout) {
   if (!SharedLibs.empty())
     throw ToolExecutionError("LLI currently does not support "
                              "loading shared libraries.");
 
+  if (!GCCArgs.empty())
+    throw ToolExecutionError("LLI currently does not support "
+                             "GCC Arguments.");
   std::vector<const char*> LLIArgs;
   LLIArgs.push_back(LLIPath.c_str());
   LLIArgs.push_back("-force-interpreter=true");
@@ -176,13 +182,14 @@ void LLC::OutputAsm(const std::string &Bytecode, sys::Path &OutputAsmFile) {
 void LLC::compileProgram(const std::string &Bytecode) {
   sys::Path OutputAsmFile;
   OutputAsm(Bytecode, OutputAsmFile);
-  OutputAsmFile.destroyFile();
+  OutputAsmFile.eraseFromDisk();
 }
 
 int LLC::ExecuteProgram(const std::string &Bytecode,
                         const std::vector<std::string> &Args,
                         const std::string &InputFile,
                         const std::string &OutputFile,
+                        const std::vector<std::string> &ArgsForGCC,
                         const std::vector<std::string> &SharedLibs,
                         unsigned Timeout) {
 
@@ -190,9 +197,12 @@ int LLC::ExecuteProgram(const std::string &Bytecode,
   OutputAsm(Bytecode, OutputAsmFile);
   FileRemover OutFileRemover(OutputAsmFile);
 
+  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.toString(), Args, GCC::AsmFile,
-                             InputFile, OutputFile, SharedLibs, Timeout);
+                             InputFile, OutputFile, GCCArgs, Timeout);
 }
 
 /// createLLC - Try to find the LLC executable
@@ -233,8 +243,11 @@ namespace {
                                const std::vector<std::string> &Args,
                                const std::string &InputFile,
                                const std::string &OutputFile,
+                               const std::vector<std::string> &GCCArgs =
+                                 std::vector<std::string>(),
                                const std::vector<std::string> &SharedLibs =
-                               std::vector<std::string>(), unsigned Timeout =0);
+                                 std::vector<std::string>(), 
+                               unsigned Timeout =0 );
   };
 }
 
@@ -242,8 +255,11 @@ int JIT::ExecuteProgram(const std::string &Bytecode,
                         const std::vector<std::string> &Args,
                         const std::string &InputFile,
                         const std::string &OutputFile,
+                        const std::vector<std::string> &GCCArgs,
                         const std::vector<std::string> &SharedLibs,
                         unsigned Timeout) {
+  if (!GCCArgs.empty())
+    throw ToolExecutionError("JIT does not support GCC Arguments.");
   // Construct a vector of parameters, incorporating those from the command-line
   std::vector<const char*> JITArgs;
   JITArgs.push_back(LLIPath.c_str());
@@ -321,13 +337,14 @@ void CBE::OutputC(const std::string &Bytecode, sys::Path& OutputCFile) {
 void CBE::compileProgram(const std::string &Bytecode) {
   sys::Path OutputCFile;
   OutputC(Bytecode, OutputCFile);
-  OutputCFile.destroyFile();
+  OutputCFile.eraseFromDisk();
 }
 
 int CBE::ExecuteProgram(const std::string &Bytecode,
                         const std::vector<std::string> &Args,
                         const std::string &InputFile,
                         const std::string &OutputFile,
+                        const std::vector<std::string> &ArgsForGCC,
                         const std::vector<std::string> &SharedLibs,
                         unsigned Timeout) {
   sys::Path OutputCFile;
@@ -335,8 +352,10 @@ int CBE::ExecuteProgram(const std::string &Bytecode,
 
   FileRemover CFileRemove(OutputCFile);
 
+  std::vector<std::string> GCCArgs(ArgsForGCC);
+  GCCArgs.insert(GCCArgs.end(),SharedLibs.begin(),SharedLibs.end());
   return gcc->ExecuteProgram(OutputCFile.toString(), Args, GCC::CFile,
-                             InputFile, OutputFile, SharedLibs, Timeout);
+                             InputFile, OutputFile, GCCArgs, Timeout);
 }
 
 /// createCBE - Try to find the 'llc' executable
@@ -368,16 +387,12 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
                         FileType fileType,
                         const std::string &InputFile,
                         const std::string &OutputFile,
-                        const std::vector<std::string> &SharedLibs,
-                        unsigned Timeout) {
+                        const std::vector<std::string> &ArgsForGCC,
+                        unsigned Timeout ) {
   std::vector<const char*> GCCArgs;
 
   GCCArgs.push_back(GCCPath.c_str());
 
-  // Specify the shared libraries to link in...
-  for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i)
-    GCCArgs.push_back(SharedLibs[i].c_str());
-
   // Specify -x explicitly in case the extension is wonky
   GCCArgs.push_back("-x");
   if (fileType == CFile) {
@@ -385,16 +400,30 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
     GCCArgs.push_back("-fno-strict-aliasing");
   } else {
     GCCArgs.push_back("assembler");
+#ifdef __APPLE__
+    GCCArgs.push_back("-force_cpusubtype_ALL");
+#endif
   }
   GCCArgs.push_back(ProgramFile.c_str());  // Specify the input filename...
   GCCArgs.push_back("-o");
   sys::Path OutputBinary (ProgramFile+".gcc.exe");
   OutputBinary.makeUnique();
   GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
+
+  // 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
+  // command line, so this should be safe.
+  for (unsigned i = 0, e = ArgsForGCC.size(); i != e; ++i)
+    GCCArgs.push_back(ArgsForGCC[i].c_str());
+
   GCCArgs.push_back("-lm");                // Hard-code the math library...
   GCCArgs.push_back("-O2");                // Optimize the program a bit...
 #if defined (HAVE_LINK_R)
   GCCArgs.push_back("-Wl,-R.");            // Search this dir for .so files
+#endif
+#ifdef __sparc__
+  GCCArgs.push_back("-mcpu=v9");
 #endif
   GCCArgs.push_back(0);                    // NULL terminator
 
@@ -441,7 +470,7 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
     InputFile.c_str(),           // Specify the input filename...
 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
     "-G",                        // Compile a shared library, `-G' for Sparc
-#elif (defined(__POWERPC__) || defined(__ppc__)) && defined(__APPLE__)
+#elif defined(__APPLE__)
     "-single_module",            // link all source files into a single module
     "-dynamiclib",               // `-dynamiclib' for MacOS X/PowerPC
     "-undefined",                // in data segment, rather than generating
@@ -453,6 +482,9 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
 
 #if defined(__ia64__) || defined(__alpha__)
     "-fPIC",                     // IA64 requires shared objs to contain PIC
+#endif
+#ifdef __sparc__
+    "-mcpu=v9",
 #endif
     "-o", OutputFile.c_str(),    // Output to the right filename...
     "-O2",                       // Optimize the program a bit...