blob: f499754ced3ce46eeccc890fb2598fe1c40fdad7 [file] [log] [blame]
Reid Spencer63ab6692004-11-14 22:29:21 +00001//===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencer63ab6692004-11-14 22:29:21 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman3da94ae2005-04-22 00:00:37 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer63ab6692004-11-14 22:29:21 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencer63ab6692004-11-14 22:29:21 +00008//===----------------------------------------------------------------------===//
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>
Duraid Madina4e4fe662005-12-28 06:58:12 +000020#include <memory>
Reid Spencer63ab6692004-11-14 22:29:21 +000021
22using namespace llvm;
23
24// llvm-ar operation code and modifier flags
Misha Brukman3da94ae2005-04-22 00:00:37 +000025static cl::opt<std::string>
Reid Spencer3d1cc282004-12-30 17:51:57 +000026ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
Reid Spencer63ab6692004-11-14 22:29:21 +000027
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();
Misha Brukman3da94ae2005-04-22 00:00:37 +000036 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbabb45b2004-11-16 06:41:20 +000037 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;
Reid Spencerdd04df02005-07-07 23:21:43 +000063 if (!ArchivePath.set(ArchiveName))
Reid Spencer63ab6692004-11-14 22:29:21 +000064 throw std::string("Archive name invalid: ") + ArchiveName;
65
66 // Make sure it exists, we don't create empty archives
67 if (!ArchivePath.exists())
Reid Spenceref11c5e2005-02-13 07:34:17 +000068 throw std::string("Archive file does not exist");
Reid Spencer63ab6692004-11-14 22:29:21 +000069
Reid Spenceref11c5e2005-02-13 07:34:17 +000070 std::string err_msg;
Misha Brukman3da94ae2005-04-22 00:00:37 +000071 std::auto_ptr<Archive>
Reid Spenceref11c5e2005-02-13 07:34:17 +000072 AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
Reid Spencerbabb45b2004-11-16 06:41:20 +000073 Archive* TheArchive = AutoArchive.get();
Reid Spenceref11c5e2005-02-13 07:34:17 +000074 if (!TheArchive)
75 throw err_msg;
Reid Spencer63ab6692004-11-14 22:29:21 +000076
Reid Spencer30836fc2006-08-25 05:29:36 +000077 if (TheArchive->writeToDisk(true, false, false, &err_msg ))
Reid Spencer3039b992006-07-07 19:09:14 +000078 throw err_msg;
Reid Spencer63ab6692004-11-14 22:29:21 +000079
Reid Spencerbabb45b2004-11-16 06:41:20 +000080 if (Verbose)
81 printSymbolTable(TheArchive);
Reid Spencer63ab6692004-11-14 22:29:21 +000082
83 } catch (const char*msg) {
84 std::cerr << argv[0] << ": " << msg << "\n\n";
85 exitCode = 1;
86 } catch (const std::string& msg) {
87 std::cerr << argv[0] << ": " << msg << "\n";
88 exitCode = 2;
89 } catch (...) {
Reid Spencerbabb45b2004-11-16 06:41:20 +000090 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer63ab6692004-11-14 22:29:21 +000091 exitCode = 3;
92 }
Reid Spencer63ab6692004-11-14 22:29:21 +000093 return exitCode;
94}