blob: a311c528027a274711322beaacd5dbbef74faf6c [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 Gohmanf8b81bf2009-07-15 16:35:29 +000020#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/System/Signals.h"
22#include <iostream>
23#include <iomanip>
24#include <memory>
25
26using namespace llvm;
27
28// llvm-ar operation code and modifier flags
29static cl::opt<std::string>
30ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
31
32static cl::opt<bool>
33Verbose("verbose",cl::Optional,cl::init(false),
34 cl::desc("Print the symbol table"));
35
36// printSymbolTable - print out the archive's symbol table.
37void 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();
41 I != E; ++I ) {
42 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
43 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
44 }
45}
46
47int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000048 // Print a stack trace if we signal out.
49 llvm::sys::PrintStackTraceOnErrorSignal();
50 llvm::PrettyStackTraceProgram X(argc, argv);
Owen Anderson25209b42009-07-01 16:58:40 +000051
52 LLVMContext Context;
Chris Lattnere6012df2009-03-06 05:34:10 +000053 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 // Have the command line options parsed and handle things
56 // like --help and --version.
57 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +000058 "LLVM Archive Index Generator (llvm-ranlib)\n\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 " This program adds or updates an index of bitcode symbols\n"
60 " to an LLVM archive file."
61 );
62
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 int exitCode = 0;
64
65 // Make sure we don't exit with "unhandled exception".
66 try {
67
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;
72
73 // Make sure it exists, we don't create empty archives
74 if (!ArchivePath.exists())
75 throw std::string("Archive file does not exist");
76
77 std::string err_msg;
78 std::auto_ptr<Archive>
Owen Andersona148fdd2009-07-01 21:22:36 +000079 AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 Archive* TheArchive = AutoArchive.get();
81 if (!TheArchive)
82 throw err_msg;
83
84 if (TheArchive->writeToDisk(true, false, false, &err_msg ))
85 throw err_msg;
86
87 if (Verbose)
88 printSymbolTable(TheArchive);
89
Misha Brukman7d63d352008-12-31 17:41:49 +000090 } catch (const char* msg) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000091 errs() << argv[0] << ": " << msg << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 exitCode = 1;
93 } catch (const std::string& msg) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000094 errs() << argv[0] << ": " << msg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 exitCode = 2;
96 } catch (...) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000097 errs() << argv[0] << ": An unexpected unknown exception occurred.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 exitCode = 3;
99 }
100 return exitCode;
101}