Added LLVM copyright notice to Makefiles.
[oota-llvm.git] / lib / Support / ToolRunner.cpp
index 4538bb2d570703f8b2765d233c11c3313a522f91..a66b868c22cdc5f064c888b2816b8f6810ea0e7a 100644 (file)
@@ -1,6 +1,22 @@
+//===-- ToolRunner.cpp ----------------------------------------------------===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
+//
+// This file implements the interfaces described in the ToolRunner.h file.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "toolrunner"
 #include "llvm/Support/ToolRunner.h"
 #include "Support/Debug.h"
 #include "Support/FileUtilities.h"
+#include <iostream>
+#include <fstream>
 
 //===---------------------------------------------------------------------===//
 // LLI Implementation of AbstractIntepreter interface
@@ -12,18 +28,19 @@ public:
 
 
   virtual int ExecuteProgram(const std::string &Bytecode,
-                             const cl::list<std::string> &Args,
+                             const std::vector<std::string> &Args,
                              const std::string &InputFile,
                              const std::string &OutputFile,
-                             const std::string &SharedLib = "");
+                             const std::vector<std::string> &SharedLibs = 
+                               std::vector<std::string>());
 };
 
 int LLI::ExecuteProgram(const std::string &Bytecode,
-                        const cl::list<std::string> &Args,
+                        const std::vector<std::string> &Args,
                         const std::string &InputFile,
                         const std::string &OutputFile,
-                        const std::string &SharedLib) {
-  if (!SharedLib.empty()) {
+                        const std::vector<std::string> &SharedLibs) {
+  if (!SharedLibs.empty()) {
     std::cerr << "LLI currently does not support loading shared libraries.\n"
               << "Exiting.\n";
     exit(1);
@@ -40,8 +57,8 @@ int LLI::ExecuteProgram(const std::string &Bytecode,
   LLIArgs.push_back(0);
 
   std::cout << "<lli>" << std::flush;
-  DEBUG(std::cerr << "\nAbout to run:\n\t";
-        for (unsigned i=0, e = LLIArgs.size(); i != e; ++i)
+  DEBUG(std::cerr << "\nAbout to run:\t";
+        for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
           std::cerr << " " << LLIArgs[i];
         std::cerr << "\n";
         );
@@ -50,9 +67,9 @@ int LLI::ExecuteProgram(const std::string &Bytecode,
 }
 
 // LLI create method - Try to find the LLI executable
-AbstractInterpreter *createLLItool(const std::string &ProgramPath, 
-                                   std::string &Message) {
-  std::string LLIPath = FindExecutable("lli", ProgramPath);
+AbstractInterpreter *AbstractInterpreter::createLLI(const std::string &ProgPath,
+                                                    std::string &Message) {
+  std::string LLIPath = FindExecutable("lli", ProgPath);
   if (!LLIPath.empty()) {
     Message = "Found lli: " + LLIPath + "\n";
     return new LLI(LLIPath);
@@ -65,8 +82,7 @@ AbstractInterpreter *createLLItool(const std::string &ProgramPath,
 //===----------------------------------------------------------------------===//
 // LLC Implementation of AbstractIntepreter interface
 //
-int LLC::OutputAsm(const std::string &Bytecode,
-                   std::string &OutputAsmFile) {
+int LLC::OutputAsm(const std::string &Bytecode, std::string &OutputAsmFile) {
   OutputAsmFile = getUniqueFilename(Bytecode+".llc.s");
   const char *LLCArgs[] = {
     LLCPath.c_str(),
@@ -89,10 +105,10 @@ int LLC::OutputAsm(const std::string &Bytecode,
 }
 
 int LLC::ExecuteProgram(const std::string &Bytecode,
-                        const cl::list<std::string> &Args,
+                        const std::vector<std::string> &Args,
                         const std::string &InputFile,
                         const std::string &OutputFile,
-                        const std::string &SharedLib) {
+                        const std::vector<std::string> &SharedLibs) {
 
   std::string OutputAsmFile;
   if (OutputAsm(Bytecode, OutputAsmFile)) {
@@ -101,16 +117,16 @@ int LLC::ExecuteProgram(const std::string &Bytecode,
   }
 
   // Assuming LLC worked, compile the result with GCC and run it.
-  int Result = gcc->ExecuteProgram(OutputAsmFile, Args, AsmFile,
-                                   InputFile, OutputFile, SharedLib);
+  int Result = gcc->ExecuteProgram(OutputAsmFile, Args, GCC::AsmFile,
+                                   InputFile, OutputFile, SharedLibs);
   removeFile(OutputAsmFile);
   return Result;
 }
 
-/// createLLCtool - Try to find the LLC executable
+/// createLLC - Try to find the LLC executable
 ///
-LLC *createLLCtool(const std::string &ProgramPath, std::string &Message)
-{
+LLC *AbstractInterpreter::createLLC(const std::string &ProgramPath,
+                                    std::string &Message) {
   std::string LLCPath = FindExecutable("llc", ProgramPath);
   if (LLCPath.empty()) {
     Message = "Cannot find `llc' in executable directory or PATH!\n";
@@ -118,7 +134,7 @@ LLC *createLLCtool(const std::string &ProgramPath, std::string &Message)
   }
 
   Message = "Found llc: " + LLCPath + "\n";
-  GCC *gcc = createGCCtool(ProgramPath, Message);
+  GCC *gcc = GCC::create(ProgramPath, Message);
   if (!gcc) {
     std::cerr << Message << "\n";
     exit(1);
@@ -136,25 +152,27 @@ public:
 
 
   virtual int ExecuteProgram(const std::string &Bytecode,
-                             const cl::list<std::string> &Args,
+                             const std::vector<std::string> &Args,
                              const std::string &InputFile,
                              const std::string &OutputFile,
-                             const std::string &SharedLib = "");
+                             const std::vector<std::string> &SharedLibs = 
+                               std::vector<std::string>());
 };
 
 int JIT::ExecuteProgram(const std::string &Bytecode,
-                        const cl::list<std::string> &Args,
+                        const std::vector<std::string> &Args,
                         const std::string &InputFile,
                         const std::string &OutputFile,
-                        const std::string &SharedLib) {
+                        const std::vector<std::string> &SharedLibs) {
   // Construct a vector of parameters, incorporating those from the command-line
   std::vector<const char*> JITArgs;
   JITArgs.push_back(LLIPath.c_str());
   JITArgs.push_back("-quiet");
   JITArgs.push_back("-force-interpreter=false");
-  if (!SharedLib.empty()) {
+
+  for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
     JITArgs.push_back("-load");
-    JITArgs.push_back(SharedLib.c_str());
+    JITArgs.push_back(SharedLibs[i].c_str());
   }
   JITArgs.push_back(Bytecode.c_str());
   // Add optional parameters to the running program from Argv
@@ -163,8 +181,8 @@ int JIT::ExecuteProgram(const std::string &Bytecode,
   JITArgs.push_back(0);
 
   std::cout << "<jit>" << std::flush;
-  DEBUG(std::cerr << "\nAbout to run:\n\t";
-        for (unsigned i=0, e = JITArgs.size(); i != e; ++i)
+  DEBUG(std::cerr << "\nAbout to run:\t";
+        for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
           std::cerr << " " << JITArgs[i];
         std::cerr << "\n";
         );
@@ -173,11 +191,11 @@ int JIT::ExecuteProgram(const std::string &Bytecode,
                                InputFile, OutputFile, OutputFile);
 }
 
-/// createJITtool - Try to find the LLI executable
+/// createJIT - Try to find the LLI executable
 ///
-AbstractInterpreter *createJITtool(const std::string &ProgramPath, 
-                                   std::string &Message) {
-  std::string LLIPath = FindExecutable("lli", ProgramPath);
+AbstractInterpreter *AbstractInterpreter::createJIT(const std::string &ProgPath,
+                                                    std::string &Message) {
+  std::string LLIPath = FindExecutable("lli", ProgPath);
   if (!LLIPath.empty()) {
     Message = "Found lli: " + LLIPath + "\n";
     return new JIT(LLIPath);
@@ -211,26 +229,27 @@ int CBE::OutputC(const std::string &Bytecode,
 }
 
 int CBE::ExecuteProgram(const std::string &Bytecode,
-                        const cl::list<std::string> &Args,
+                        const std::vector<std::string> &Args,
                         const std::string &InputFile,
                         const std::string &OutputFile,
-                        const std::string &SharedLib) {
+                        const std::vector<std::string> &SharedLibs) {
   std::string OutputCFile;
   if (OutputC(Bytecode, OutputCFile)) {
     std::cerr << "Could not generate C code with `llvm-dis', exiting.\n";
     exit(1);
   }
 
-  int Result = gcc->ExecuteProgram(OutputCFile, Args, CFile, 
-                                   InputFile, OutputFile, SharedLib);
+  int Result = gcc->ExecuteProgram(OutputCFile, Args, GCC::CFile, 
+                                   InputFile, OutputFile, SharedLibs);
   removeFile(OutputCFile);
 
   return Result;
 }
 
-/// createCBEtool - Try to find the 'dis' executable
+/// createCBE - Try to find the 'llvm-dis' executable
 ///
-CBE *createCBEtool(const std::string &ProgramPath, std::string &Message) {
+CBE *AbstractInterpreter::createCBE(const std::string &ProgramPath,
+                                    std::string &Message) {
   std::string DISPath = FindExecutable("llvm-dis", ProgramPath);
   if (DISPath.empty()) {
     Message = 
@@ -239,7 +258,7 @@ CBE *createCBEtool(const std::string &ProgramPath, std::string &Message) {
   }
 
   Message = "Found llvm-dis: " + DISPath + "\n";
-  GCC *gcc = createGCCtool(ProgramPath, Message);
+  GCC *gcc = GCC::create(ProgramPath, Message);
   if (!gcc) {
     std::cerr << Message << "\n";
     exit(1);
@@ -250,21 +269,21 @@ CBE *createCBEtool(const std::string &ProgramPath, std::string &Message) {
 //===---------------------------------------------------------------------===//
 // GCC abstraction
 //
-// This is not a *real* AbstractInterpreter as it does not accept bytecode
-// files, but only input acceptable to GCC, i.e. C, C++, and assembly files
-//
 int GCC::ExecuteProgram(const std::string &ProgramFile,
-                        const cl::list<std::string> &Args,
+                        const std::vector<std::string> &Args,
                         FileType fileType,
                         const std::string &InputFile,
                         const std::string &OutputFile,
-                        const std::string &SharedLib) {
-  std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
+                        const std::vector<std::string> &SharedLibs) {
   std::vector<const char*> GCCArgs;
 
   GCCArgs.push_back(GCCPath.c_str());
-  if (!SharedLib.empty()) // Specify the shared library to link in...
-    GCCArgs.push_back(SharedLib.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) {
     GCCArgs.push_back("c");
@@ -274,9 +293,11 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
   }
   GCCArgs.push_back(ProgramFile.c_str());  // Specify the input filename...
   GCCArgs.push_back("-o");
+  std::string OutputBinary = getUniqueFilename(ProgramFile+".gcc.exe");
   GCCArgs.push_back(OutputBinary.c_str()); // Output to the right file...
   GCCArgs.push_back("-lm");                // Hard-code the math library...
   GCCArgs.push_back("-O2");                // Optimize the program a bit...
+  GCCArgs.push_back("-Wl,-R.");            // Search this dir for .so files
   GCCArgs.push_back(0);                    // NULL terminator
 
   std::cout << "<gcc>" << std::flush;
@@ -295,8 +316,8 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
 
   // Now that we have a binary, run it!
   std::cout << "<program>" << std::flush;
-  DEBUG(std::cerr << "\nAbout to run:\n\t";
-        for (unsigned i=0, e = ProgramArgs.size(); i != e; ++i)
+  DEBUG(std::cerr << "\nAbout to run:\t";
+        for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
           std::cerr << " " << ProgramArgs[i];
         std::cerr << "\n";
         );
@@ -306,8 +327,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
   return ProgramResult;
 }
 
-int GCC::MakeSharedObject(const std::string &InputFile,
-                          FileType fileType,
+int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
                           std::string &OutputFile) {
   OutputFile = getUniqueFilename(InputFile+".so");
   // Compile the C/asm file into a shared object
@@ -327,10 +347,10 @@ int GCC::MakeSharedObject(const std::string &InputFile,
   };
   
   std::cout << "<gcc>" << std::flush;
-  if(RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
-                           "/dev/null")) {
+  if (RunProgramWithTimeout(GCCPath, GCCArgs, "/dev/null", "/dev/null",
+                            "/dev/null")) {
     ProcessFailure(GCCArgs);
-    exit(1);
+    return 1;
   }
   return 0;
 }
@@ -359,9 +379,9 @@ void GCC::ProcessFailure(const char** GCCArgs) {
   removeFile(ErrorFilename);
 }
 
-/// createGCCtool - Try to find the `gcc' executable
+/// create - Try to find the `gcc' executable
 ///
-GCC *createGCCtool(const std::string &ProgramPath, std::string &Message) {
+GCC *GCC::create(const std::string &ProgramPath, std::string &Message) {
   std::string GCCPath = FindExecutable("gcc", ProgramPath);
   if (GCCPath.empty()) {
     Message = "Cannot find `gcc' in executable directory or PATH!\n";