Home | History | Annotate | Download | only in llvm-ranlib
      1 //===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Adds or updates an index (symbol table) for an LLVM archive file.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/LLVMContext.h"
     15 #include "llvm/Module.h"
     16 #include "llvm/Bitcode/Archive.h"
     17 #include "llvm/Support/CommandLine.h"
     18 #include "llvm/Support/FileSystem.h"
     19 #include "llvm/Support/ManagedStatic.h"
     20 #include "llvm/Support/PrettyStackTrace.h"
     21 #include "llvm/Support/Format.h"
     22 #include "llvm/Support/raw_ostream.h"
     23 #include "llvm/Support/Signals.h"
     24 #include <memory>
     25 using namespace llvm;
     26 
     27 // llvm-ar operation code and modifier flags
     28 static cl::opt<std::string>
     29 ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
     30 
     31 static cl::opt<bool>
     32 Verbose("verbose",cl::Optional,cl::init(false),
     33         cl::desc("Print the symbol table"));
     34 
     35 // printSymbolTable - print out the archive's symbol table.
     36 void printSymbolTable(Archive* TheArchive) {
     37   outs() << "\nArchive Symbol Table:\n";
     38   const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
     39   for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
     40        I != E; ++I ) {
     41     unsigned offset = TheArchive->getFirstFileOffset() + I->second;
     42     outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
     43   }
     44 }
     45 
     46 int main(int argc, char **argv) {
     47   // Print a stack trace if we signal out.
     48   llvm::sys::PrintStackTraceOnErrorSignal();
     49   llvm::PrettyStackTraceProgram X(argc, argv);
     50 
     51   LLVMContext &Context = getGlobalContext();
     52   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
     53 
     54   // Have the command line options parsed and handle things
     55   // like --help and --version.
     56   cl::ParseCommandLineOptions(argc, argv,
     57     "LLVM Archive Index Generator (llvm-ranlib)\n\n"
     58     "  This program adds or updates an index of bitcode symbols\n"
     59     "  to an LLVM archive file."
     60   );
     61 
     62   int exitCode = 0;
     63 
     64   // Make sure we don't exit with "unhandled exception".
     65   try {
     66 
     67     // Check the path name of the archive
     68     sys::Path ArchivePath;
     69     if (!ArchivePath.set(ArchiveName))
     70       throw std::string("Archive name invalid: ") + ArchiveName;
     71 
     72     // Make sure it exists, we don't create empty archives
     73     bool Exists;
     74     if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists)
     75       throw std::string("Archive file does not exist");
     76 
     77     std::string err_msg;
     78     std::auto_ptr<Archive>
     79       AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg));
     80     Archive* TheArchive = AutoArchive.get();
     81     if (!TheArchive)
     82       throw err_msg;
     83 
     84     if (TheArchive->writeToDisk(true, false, false, &err_msg ))
     85       throw err_msg;
     86 
     87     if (Verbose)
     88       printSymbolTable(TheArchive);
     89 
     90   } catch (const char* msg) {
     91     errs() << argv[0] << ": " << msg << "\n\n";
     92     exitCode = 1;
     93   } catch (const std::string& msg) {
     94     errs() << argv[0] << ": " << msg << "\n";
     95     exitCode = 2;
     96   } catch (...) {
     97     errs() << argv[0] << ": An unexpected unknown exception occurred.\n";
     98     exitCode = 3;
     99   }
    100   return exitCode;
    101 }
    102