blob: 72106104c0067f22f8b93ab5e6b0334db212aab6 [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
14#include "llvm/Module.h"
15#include "llvm/Bitcode/Archive.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/ManagedStatic.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000018#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/System/Signals.h"
20#include <iostream>
21#include <iomanip>
22#include <memory>
23
24using namespace llvm;
25
26// llvm-ar operation code and modifier flags
27static cl::opt<std::string>
28ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
29
30static cl::opt<bool>
31Verbose("verbose",cl::Optional,cl::init(false),
32 cl::desc("Print the symbol table"));
33
34// printSymbolTable - print out the archive's symbol table.
35void printSymbolTable(Archive* TheArchive) {
36 std::cout << "\nArchive Symbol Table:\n";
37 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
38 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
39 I != E; ++I ) {
40 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
41 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
42 }
43}
44
45int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000046 // Print a stack trace if we signal out.
47 llvm::sys::PrintStackTraceOnErrorSignal();
48 llvm::PrettyStackTraceProgram X(argc, argv);
49
50 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52 // Have the command line options parsed and handle things
53 // like --help and --version.
54 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +000055 "LLVM Archive Index Generator (llvm-ranlib)\n\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 " This program adds or updates an index of bitcode symbols\n"
57 " to an LLVM archive file."
58 );
59
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 int exitCode = 0;
61
62 // Make sure we don't exit with "unhandled exception".
63 try {
64
65 // Check the path name of the archive
66 sys::Path ArchivePath;
67 if (!ArchivePath.set(ArchiveName))
68 throw std::string("Archive name invalid: ") + ArchiveName;
69
70 // Make sure it exists, we don't create empty archives
71 if (!ArchivePath.exists())
72 throw std::string("Archive file does not exist");
73
74 std::string err_msg;
75 std::auto_ptr<Archive>
76 AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
77 Archive* TheArchive = AutoArchive.get();
78 if (!TheArchive)
79 throw err_msg;
80
81 if (TheArchive->writeToDisk(true, false, false, &err_msg ))
82 throw err_msg;
83
84 if (Verbose)
85 printSymbolTable(TheArchive);
86
Misha Brukman7d63d352008-12-31 17:41:49 +000087 } catch (const char* msg) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 std::cerr << argv[0] << ": " << msg << "\n\n";
89 exitCode = 1;
90 } catch (const std::string& msg) {
91 std::cerr << argv[0] << ": " << msg << "\n";
92 exitCode = 2;
93 } catch (...) {
94 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
95 exitCode = 3;
96 }
97 return exitCode;
98}