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