blob: d9bcc48eb13ed1bcce7db5d3a76868610c3a8d65 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Adds or updates an index (symbol table) for an LLVM archive file.
11//
12//===----------------------------------------------------------------------===//
13
Owen Anderson25209b42009-07-01 16:58:40 +000014#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/Module.h"
16#include "llvm/Bitcode/Archive.h"
17#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/ManagedStatic.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000019#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/System/Signals.h"
21#include <iostream>
22#include <iomanip>
23#include <memory>
24
25using namespace llvm;
26
27// llvm-ar operation code and modifier flags
28static cl::opt<std::string>
29ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
30
31static cl::opt<bool>
32Verbose("verbose",cl::Optional,cl::init(false),
33 cl::desc("Print the symbol table"));
34
35// printSymbolTable - print out the archive's symbol table.
36void printSymbolTable(Archive* TheArchive) {
37 std::cout << "\nArchive Symbol Table:\n";
38 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
39 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
40 I != E; ++I ) {
41 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
42 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
43 }
44}
45
46int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000047 // Print a stack trace if we signal out.
48 llvm::sys::PrintStackTraceOnErrorSignal();
49 llvm::PrettyStackTraceProgram X(argc, argv);
Owen Anderson25209b42009-07-01 16:58:40 +000050
51 LLVMContext Context;
Chris Lattnere6012df2009-03-06 05:34:10 +000052 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 // Have the command line options parsed and handle things
55 // like --help and --version.
56 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +000057 "LLVM Archive Index Generator (llvm-ranlib)\n\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 " This program adds or updates an index of bitcode symbols\n"
59 " to an LLVM archive file."
60 );
61
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 int exitCode = 0;
63
64 // Make sure we don't exit with "unhandled exception".
65 try {
66
67 // Check the path name of the archive
68 sys::Path ArchivePath;
69 if (!ArchivePath.set(ArchiveName))
70 throw std::string("Archive name invalid: ") + ArchiveName;
71
72 // Make sure it exists, we don't create empty archives
73 if (!ArchivePath.exists())
74 throw std::string("Archive file does not exist");
75
76 std::string err_msg;
77 std::auto_ptr<Archive>
Owen Andersona148fdd2009-07-01 21:22:36 +000078 AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 Archive* TheArchive = AutoArchive.get();
80 if (!TheArchive)
81 throw err_msg;
82
83 if (TheArchive->writeToDisk(true, false, false, &err_msg ))
84 throw err_msg;
85
86 if (Verbose)
87 printSymbolTable(TheArchive);
88
Misha Brukman7d63d352008-12-31 17:41:49 +000089 } catch (const char* msg) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 std::cerr << argv[0] << ": " << msg << "\n\n";
91 exitCode = 1;
92 } catch (const std::string& msg) {
93 std::cerr << argv[0] << ": " << msg << "\n";
94 exitCode = 2;
95 } catch (...) {
96 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
97 exitCode = 3;
98 }
99 return exitCode;
100}