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