For PR351:
[oota-llvm.git] / tools / llvm-ranlib / llvm-ranlib.cpp
1 //===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source 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/Module.h"
15 #include "llvm/Bytecode/Archive.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/System/Signals.h"
18 #include <iostream>
19 #include <iomanip>
20
21 using namespace llvm;
22
23 // llvm-ar operation code and modifier flags
24 static cl::opt<std::string> 
25 ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>..."));
26
27 static cl::opt<bool>
28 Verbose("verbose",cl::Optional,cl::init(false),
29         cl::desc("Print the symbol table"));
30
31 // printSymbolTable - print out the archive's symbol table.
32 void printSymbolTable(Archive* TheArchive) {
33   std::cout << "\nArchive Symbol Table:\n";
34   const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
35   for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end(); 
36        I != E; ++I ) {
37     unsigned offset = TheArchive->getFirstFileOffset() + I->second;
38     std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
39   }
40 }
41
42 int main(int argc, char **argv) {
43
44   // Have the command line options parsed and handle things
45   // like --help and --version.
46   cl::ParseCommandLineOptions(argc, argv,
47     " LLVM Archive Index Generator (llvm-ranlib)\n\n"
48     "  This program adds or updates an index of bytecode symbols\n"
49     "  to an LLVM archive file."
50   );
51
52   // Print a stack trace if we signal out.
53   sys::PrintStackTraceOnErrorSignal();
54
55   int exitCode = 0;
56
57   // Make sure we don't exit with "unhandled exception".
58   try {
59
60     // Check the path name of the archive
61     sys::Path ArchivePath;
62     if (!ArchivePath.setFile(ArchiveName))
63       throw std::string("Archive name invalid: ") + ArchiveName;
64
65     // Make sure it exists, we don't create empty archives
66     if (!ArchivePath.exists())
67       throw "Archive file does not exist";
68
69     std::auto_ptr<Archive> AutoArchive(Archive::OpenAndLoad(ArchivePath));
70     Archive* TheArchive = AutoArchive.get();
71
72     assert(TheArchive && "Unable to instantiate the archive");
73
74     TheArchive->writeToDisk(true, false, false );
75
76     if (Verbose)
77       printSymbolTable(TheArchive);
78
79   } catch (const char*msg) {
80     std::cerr << argv[0] << ": " << msg << "\n\n";
81     exitCode = 1;
82   } catch (const std::string& msg) {
83     std::cerr << argv[0] << ": " << msg << "\n";
84     exitCode = 2;
85   } catch (...) {
86     std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
87     exitCode = 3;
88   }
89   return exitCode;
90 }