blob: 38633867c9c1d07c60f33f037c7257239599f620 [file] [log] [blame]
Reid Spencer63ab6692004-11-14 22:29:21 +00001//===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
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/Bytecode/Archive.h"
16#include "llvm/Support/CommandLine.h"
Reid Spencer63ab6692004-11-14 22:29:21 +000017#include "llvm/System/Signals.h"
18#include <iostream>
Reid Spencer63ab6692004-11-14 22:29:21 +000019#include <iomanip>
20
21using namespace llvm;
22
23// llvm-ar operation code and modifier flags
Reid Spencerbabb45b2004-11-16 06:41:20 +000024static cl::opt<std::string>
Reid Spencer63ab6692004-11-14 22:29:21 +000025ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>..."));
26
Reid Spencerbabb45b2004-11-16 06:41:20 +000027static cl::opt<bool>
Reid Spencer63ab6692004-11-14 22:29:21 +000028Verbose("verbose",cl::Optional,cl::init(false),
29 cl::desc("Print the symbol table"));
30
Reid Spencerbabb45b2004-11-16 06:41:20 +000031// printSymbolTable - print out the archive's symbol table.
32void printSymbolTable(Archive* TheArchive) {
33 std::cout << "\nArchive Symbol Table:\n";
34 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
35 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
36 I != E; ++I ) {
37 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
38 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
39 }
Reid Spencer63ab6692004-11-14 22:29:21 +000040}
41
42int main(int argc, char **argv) {
43
44 // Have the command line options parsed and handle things
45 // like --help and --version.
46 cl::ParseCommandLineOptions(argc, argv,
47 " LLVM Archive Index Generator (llvm-ranlib)\n\n"
48 " This program adds or updates an index of bytecode symbols\n"
49 " to an LLVM archive file."
50 );
51
52 // Print a stack trace if we signal out.
53 sys::PrintStackTraceOnErrorSignal();
54
55 int exitCode = 0;
56
57 // Make sure we don't exit with "unhandled exception".
58 try {
59
60 // Check the path name of the archive
61 sys::Path ArchivePath;
62 if (!ArchivePath.setFile(ArchiveName))
63 throw std::string("Archive name invalid: ") + ArchiveName;
64
65 // Make sure it exists, we don't create empty archives
66 if (!ArchivePath.exists())
67 throw "Archive file does not exist";
68
Reid Spencerbabb45b2004-11-16 06:41:20 +000069 std::auto_ptr<Archive> AutoArchive(Archive::OpenAndLoad(ArchivePath));
70 Archive* TheArchive = AutoArchive.get();
Reid Spencer63ab6692004-11-14 22:29:21 +000071
72 assert(TheArchive && "Unable to instantiate the archive");
73
Reid Spencerbabb45b2004-11-16 06:41:20 +000074 TheArchive->writeToDisk(true, false, false );
Reid Spencer63ab6692004-11-14 22:29:21 +000075
Reid Spencerbabb45b2004-11-16 06:41:20 +000076 if (Verbose)
77 printSymbolTable(TheArchive);
Reid Spencer63ab6692004-11-14 22:29:21 +000078
79 } catch (const char*msg) {
80 std::cerr << argv[0] << ": " << msg << "\n\n";
81 exitCode = 1;
82 } catch (const std::string& msg) {
83 std::cerr << argv[0] << ": " << msg << "\n";
84 exitCode = 2;
85 } catch (...) {
Reid Spencerbabb45b2004-11-16 06:41:20 +000086 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer63ab6692004-11-14 22:29:21 +000087 exitCode = 3;
88 }
Reid Spencer63ab6692004-11-14 22:29:21 +000089 return exitCode;
90}