1 //===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Adds or updates an index (symbol table) for an LLVM archive file.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/Bitcode/Archive.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/System/Signals.h"
25 // llvm-ar operation code and modifier flags
26 static cl::opt<std::string>
27 ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
30 Verbose("verbose",cl::Optional,cl::init(false),
31 cl::desc("Print the symbol table"));
33 // printSymbolTable - print out the archive's symbol table.
34 void printSymbolTable(Archive* TheArchive) {
35 std::cout << "\nArchive Symbol Table:\n";
36 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
37 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
39 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
40 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
44 int main(int argc, char **argv) {
45 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
47 // Have the command line options parsed and handle things
48 // like --help and --version.
49 cl::ParseCommandLineOptions(argc, argv,
50 "LLVM Archive Index Generator (llvm-ranlib)\n\n"
51 " This program adds or updates an index of bitcode symbols\n"
52 " to an LLVM archive file."
55 // Print a stack trace if we signal out.
56 sys::PrintStackTraceOnErrorSignal();
60 // Make sure we don't exit with "unhandled exception".
63 // Check the path name of the archive
64 sys::Path ArchivePath;
65 if (!ArchivePath.set(ArchiveName))
66 throw std::string("Archive name invalid: ") + ArchiveName;
68 // Make sure it exists, we don't create empty archives
69 if (!ArchivePath.exists())
70 throw std::string("Archive file does not exist");
73 std::auto_ptr<Archive>
74 AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
75 Archive* TheArchive = AutoArchive.get();
79 if (TheArchive->writeToDisk(true, false, false, &err_msg ))
83 printSymbolTable(TheArchive);
85 } catch (const char*msg) {
86 std::cerr << argv[0] << ": " << msg << "\n\n";
88 } catch (const std::string& msg) {
89 std::cerr << argv[0] << ": " << msg << "\n";
92 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";