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/LLVMContext.h"
15 #include "llvm/Module.h"
16 #include "llvm/Bitcode/Archive.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/System/Signals.h"
28 // llvm-ar operation code and modifier flags
29 static cl::opt
<std::string
>
30 ArchiveName(cl::Positional
, cl::Optional
, cl::desc("<archive-file>"));
33 Verbose("verbose",cl::Optional
,cl::init(false),
34 cl::desc("Print the symbol table"));
36 // printSymbolTable - print out the archive's symbol table.
37 void printSymbolTable(Archive
* TheArchive
) {
38 std::cout
<< "\nArchive Symbol Table:\n";
39 const Archive::SymTabType
& symtab
= TheArchive
->getSymbolTable();
40 for (Archive::SymTabType::const_iterator I
=symtab
.begin(), E
=symtab
.end();
42 unsigned offset
= TheArchive
->getFirstFileOffset() + I
->second
;
43 std::cout
<< " " << std::setw(9) << offset
<< "\t" << I
->first
<<"\n";
47 int main(int argc
, char **argv
) {
48 // Print a stack trace if we signal out.
49 llvm::sys::PrintStackTraceOnErrorSignal();
50 llvm::PrettyStackTraceProgram
X(argc
, argv
);
52 LLVMContext
&Context
= getGlobalContext();
53 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
55 // Have the command line options parsed and handle things
56 // like --help and --version.
57 cl::ParseCommandLineOptions(argc
, argv
,
58 "LLVM Archive Index Generator (llvm-ranlib)\n\n"
59 " This program adds or updates an index of bitcode symbols\n"
60 " to an LLVM archive file."
65 // Make sure we don't exit with "unhandled exception".
68 // Check the path name of the archive
69 sys::Path ArchivePath
;
70 if (!ArchivePath
.set(ArchiveName
))
71 throw std::string("Archive name invalid: ") + ArchiveName
;
73 // Make sure it exists, we don't create empty archives
74 if (!ArchivePath
.exists())
75 throw std::string("Archive file does not exist");
78 std::auto_ptr
<Archive
>
79 AutoArchive(Archive::OpenAndLoad(ArchivePath
, Context
, &err_msg
));
80 Archive
* TheArchive
= AutoArchive
.get();
84 if (TheArchive
->writeToDisk(true, false, false, &err_msg
))
88 printSymbolTable(TheArchive
);
90 } catch (const char* msg
) {
91 errs() << argv
[0] << ": " << msg
<< "\n\n";
93 } catch (const std::string
& msg
) {
94 errs() << argv
[0] << ": " << msg
<< "\n";
97 errs() << argv
[0] << ": An unexpected unknown exception occurred.\n";