d7d471498086af382f25aa7ef9ee0ab82bee6ad7
[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.set(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 std::string("Archive file does not exist");
68
69     std::string err_msg;
70     std::auto_ptr<Archive>
71       AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
72     Archive* TheArchive = AutoArchive.get();
73     if (!TheArchive)
74       throw err_msg;
75
76     TheArchive->writeToDisk(true, false, false );
77
78     if (Verbose)
79       printSymbolTable(TheArchive);
80
81   } catch (const char*msg) {
82     std::cerr << argv[0] << ": " << msg << "\n\n";
83     exitCode = 1;
84   } catch (const std::string& msg) {
85     std::cerr << argv[0] << ": " << msg << "\n";
86     exitCode = 2;
87   } catch (...) {
88     std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
89     exitCode = 3;
90   }
91   return exitCode;
92 }