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