1 //===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Adds or updates an index (symbol table) for an LLVM archive file.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/Bytecode/Archive.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/System/Signals.h"
24 // llvm-ar operation code and modifier flags
25 static cl::opt<std::string>
26 ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
29 Verbose("verbose",cl::Optional,cl::init(false),
30 cl::desc("Print the symbol table"));
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();
38 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
39 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
43 int main(int argc, char **argv) {
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."
53 // Print a stack trace if we signal out.
54 sys::PrintStackTraceOnErrorSignal();
58 // Make sure we don't exit with "unhandled exception".
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;
66 // Make sure it exists, we don't create empty archives
67 if (!ArchivePath.exists())
68 throw std::string("Archive file does not exist");
71 std::auto_ptr<Archive>
72 AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
73 Archive* TheArchive = AutoArchive.get();
77 if (!TheArchive->writeToDisk(true, false, false, &err_msg ))
81 printSymbolTable(TheArchive);
83 } catch (const char*msg) {
84 std::cerr << argv[0] << ": " << msg << "\n\n";
86 } catch (const std::string& msg) {
87 std::cerr << argv[0] << ": " << msg << "\n";
90 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";