Fix Path::GetMainExecutable on cygwin, patch by Sam Bishop.
[oota-llvm.git] / tools / llvmc / CompilerDriver.cpp
index 46dbd89fc95cb00f2c9c51af90d15ba53f51a475..65684d65cd903a8338b7fae79799535e15515a08 100644 (file)
@@ -3,8 +3,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Reid Spencer and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -15,7 +15,9 @@
 #include "CompilerDriver.h"
 #include "ConfigLexer.h"
 #include "llvm/Module.h"
-#include "llvm/Bytecode/Reader.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Timer.h"
 #include "llvm/System/Signals.h"
 #include "llvm/ADT/SetVector.h"
@@ -24,6 +26,7 @@
 #include <iostream>
 using namespace llvm;
 
+
 namespace {
 
 void WriteAction(CompilerDriver::Action* action ) {
@@ -62,12 +65,23 @@ void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
   DumpAction(&cd->Linker);
 }
 
-/// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
-/// which should reduce the volume of code and make compilation
-/// faster. This is also safe on any llvm module.
-static const char* DefaultFastCompileOptimizations[] = {
-  "-simplifycfg", "-mem2reg", "-instcombine"
-};
+static bool GetBitcodeDependentLibraries(const std::string &fname,
+                                         Module::LibraryListType& deplibs,
+                                         std::string* ErrMsg) {
+  ModuleProvider *MP = 0;
+  if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(fname)) {
+    MP = getBitcodeModuleProvider(Buffer);
+    if (MP == 0) delete Buffer;
+  }
+  if (!MP) {
+    deplibs.clear();
+    return true;
+  }
+  deplibs = MP->getModule()->getLibraries();
+  delete MP;
+  return false;
+}
+
 
 class CompilerDriverImpl : public CompilerDriver {
 /// @name Constructors
@@ -185,8 +199,8 @@ private:
 
   void cleanup() {
     if (!isSet(KEEP_TEMPS_FLAG)) {
-      sys::FileStatus Status;
-      if (!TempDir.getFileStatus(Status) && Status.isDir)
+      const sys::FileStatus *Status = TempDir.getFileStatus();
+      if (Status && Status->isDir)
         TempDir.eraseFromDisk(/*remove_contents=*/true);
     } else {
       std::cout << "Temporary files are in " << TempDir << "\n";
@@ -245,43 +259,43 @@ private:
     if (programName[0] == '%' && programName.length() >2) {
       switch(programName[1]){
       case 'b':
-       if (programName.substr(0,8) == "%bindir%") {
-         std::string tmp(LLVM_BINDIR);
-         tmp.append(programName.substr(8));
-         pat->program.set(tmp);
-       }
-       break;
+        if (programName.substr(0,8) == "%bindir%") {
+          std::string tmp(LLVM_BINDIR);
+          tmp.append(programName.substr(8));
+          pat->program.set(tmp);
+        }
+        break;
       case 'l':
-       if (programName.substr(0,12) == "%llvmgccdir%"){
-         std::string tmp(LLVMGCCDIR);
-         tmp.append(programName.substr(12));
-         pat->program.set(tmp);
-       }else if (programName.substr(0,13) == "%llvmgccarch%"){
-         std::string tmp(LLVMGCCARCH);
-         tmp.append(programName.substr(13));
-         pat->program.set(tmp);
-       }else if (programName.substr(0,9) == "%llvmgcc%"){
-         std::string tmp(LLVMGCC);
-         tmp.append(programName.substr(9));
-         pat->program.set(tmp);
-       }else if (programName.substr(0,9) == "%llvmgxx%"){
-         std::string tmp(LLVMGXX);
-         tmp.append(programName.substr(9));
-         pat->program.set(tmp);
-       }else if (programName.substr(0,9) == "%llvmcc1%"){
-         std::string tmp(LLVMCC1);
-         tmp.append(programName.substr(9));
-         pat->program.set(tmp);
-       }else if (programName.substr(0,13) == "%llvmcc1plus%"){
-         std::string tmp(LLVMCC1PLUS);
-         tmp.append(programName.substr(13));
-         pat->program.set(tmp);
-       }else if (programName.substr(0,8) == "%libdir%") {
-         std::string tmp(LLVM_LIBDIR);
-         tmp.append(programName.substr(8));
-         pat->program.set(tmp);
-       }
-         break;
+        if (programName.substr(0,12) == "%llvmgccdir%"){
+          std::string tmp(LLVMGCCDIR);
+          tmp.append(programName.substr(12));
+          pat->program.set(tmp);
+        }else if (programName.substr(0,13) == "%llvmgccarch%"){
+          std::string tmp(LLVMGCCARCH);
+          tmp.append(programName.substr(13));
+          pat->program.set(tmp);
+        }else if (programName.substr(0,9) == "%llvmgcc%"){
+          std::string tmp(LLVMGCC);
+          tmp.append(programName.substr(9));
+          pat->program.set(tmp);
+        }else if (programName.substr(0,9) == "%llvmgxx%"){
+          std::string tmp(LLVMGXX);
+          tmp.append(programName.substr(9));
+          pat->program.set(tmp);
+        }else if (programName.substr(0,9) == "%llvmcc1%"){
+          std::string tmp(LLVMCC1);
+          tmp.append(programName.substr(9));
+          pat->program.set(tmp);
+        }else if (programName.substr(0,13) == "%llvmcc1plus%"){
+          std::string tmp(LLVMCC1PLUS);
+          tmp.append(programName.substr(13));
+          pat->program.set(tmp);
+        }else if (programName.substr(0,8) == "%libdir%") {
+          std::string tmp(LLVM_LIBDIR);
+          tmp.append(programName.substr(8));
+          pat->program.set(tmp);
+        }
+        break;
       }
     }
     action->program = pat->program;
@@ -489,14 +503,14 @@ private:
         Timer timer(action->program.toString());
         timer.startTimer();
         int resultCode = 
-          sys::Program::ExecuteAndWait(action->program, Args,0,0,0,&ErrMsg);
+          sys::Program::ExecuteAndWait(action->program, Args,0,0,0,0, &ErrMsg);
         timer.stopTimer();
         timer.print(timer,std::cerr);
         return resultCode;
       }
       else
         return 
-          sys::Program::ExecuteAndWait(action->program, Args, 0,0,0, &ErrMsg);
+          sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
     }
     return 0;
   }
@@ -544,8 +558,8 @@ private:
   }
 
   /// This method processes a linkage item. The item could be a
-  /// Bytecode file needing translation to native code and that is
-  /// dependent on other bytecode libraries, or a native code
+  /// Bitcode file needing translation to native code and that is
+  /// dependent on other bitcode libraries, or a native code
   /// library that should just be linked into the program.
   bool ProcessLinkageItem(const llvm::sys::Path& link_item,
                           SetVector<sys::Path>& set,
@@ -572,11 +586,11 @@ private:
     // If we got here fullpath is the path to the file, and its readable.
     set.insert(fullpath);
 
-    // If its an LLVM bytecode file ...
-    if (fullpath.isBytecodeFile()) {
+    // If its an LLVM bitcode file ...
+    if (fullpath.isBitcodeFile()) {
       // Process the dependent libraries recursively
       Module::LibraryListType modlibs;
-      if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,&err)) {
+      if (GetBitcodeDependentLibraries(fullpath.toString(),modlibs, &err)) {
         // Traverse the dependent libraries list
         Module::lib_iterator LI = modlibs.begin();
         Module::lib_iterator LE = modlibs.end();
@@ -661,7 +675,7 @@ public:
         // Get the suffix of the file name
         const std::string& ftype = I->second;
 
-        // If its a library, bytecode file, or object file, save
+        // If its a library, bitcode file, or object file, save
         // it for linking below and short circuit the
         // pre-processing/translation/assembly phases
         if (ftype.empty() ||  ftype == "o" || ftype == "bc" || ftype=="a") {
@@ -757,7 +771,7 @@ public:
             // ll -> bc Helper
             if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
               /// The output of the translator is an LLVM Assembly program
-              /// We need to translate it to bytecode
+              /// We need to translate it to bitcode
               Action* action = new Action();
               action->program.set("llvm-as");
               action->args.push_back(InFile.toString());
@@ -802,7 +816,7 @@ public:
               // ll -> bc Helper
               if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
                 /// The output of the optimizer is an LLVM Assembly program
-                /// We need to translate it to bytecode with llvm-as
+                /// We need to translate it to bitcode with llvm-as
                 Action* action = new Action();
                 action->program.set("llvm-as");
                 action->args.push_back(InFile.toString());
@@ -984,7 +998,7 @@ private:
   PathVector IncludePaths;      ///< -I options
   PathVector ToolPaths;         ///< -B options
   StringVector Defines;         ///< -D options
-  sys::Path TempDir;            ///< Name of the temporary directory.
+  sys::PathWithStatus TempDir;  ///< Name of the temporary directory.
   StringTable AdditionalArgs;   ///< The -Txyz options
   StringVector fOptions;        ///< -f options
   StringVector MOptions;        ///< -M options