blob: 9085b7ed45c948e6ed0f736345b63668a45bc330 [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"
Chris Lattner44dadff2007-05-06 09:29:57 +000015#include "llvm/Bitcode/Archive.h"
Reid Spencer63ab6692004-11-14 22:29:21 +000016#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000017#include "llvm/Support/ManagedStatic.h"
Reid Spencer63ab6692004-11-14 22:29:21 +000018#include "llvm/System/Signals.h"
19#include <iostream>
Reid Spencer63ab6692004-11-14 22:29:21 +000020#include <iomanip>
Duraid Madina4e4fe662005-12-28 06:58:12 +000021#include <memory>
Reid Spencer63ab6692004-11-14 22:29:21 +000022
23using namespace llvm;
24
25// llvm-ar operation code and modifier flags
Misha Brukman3da94ae2005-04-22 00:00:37 +000026static cl::opt<std::string>
Reid Spencer3d1cc282004-12-30 17:51:57 +000027ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
Reid Spencer63ab6692004-11-14 22:29:21 +000028
Reid Spencerbabb45b2004-11-16 06:41:20 +000029static cl::opt<bool>
Reid Spencer63ab6692004-11-14 22:29:21 +000030Verbose("verbose",cl::Optional,cl::init(false),
31 cl::desc("Print the symbol table"));
32
Reid Spencerbabb45b2004-11-16 06:41:20 +000033// printSymbolTable - print out the archive's symbol table.
34void printSymbolTable(Archive* TheArchive) {
35 std::cout << "\nArchive Symbol Table:\n";
36 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +000037 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbabb45b2004-11-16 06:41:20 +000038 I != E; ++I ) {
39 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
40 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
41 }
Reid Spencer63ab6692004-11-14 22:29:21 +000042}
43
44int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000045 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer63ab6692004-11-14 22:29:21 +000046
47 // Have the command line options parsed and handle things
48 // like --help and --version.
49 cl::ParseCommandLineOptions(argc, argv,
50 " LLVM Archive Index Generator (llvm-ranlib)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +000051 " This program adds or updates an index of bitcode symbols\n"
Reid Spencer63ab6692004-11-14 22:29:21 +000052 " to an LLVM archive file."
53 );
54
55 // Print a stack trace if we signal out.
56 sys::PrintStackTraceOnErrorSignal();
57
58 int exitCode = 0;
59
60 // Make sure we don't exit with "unhandled exception".
61 try {
62
63 // Check the path name of the archive
64 sys::Path ArchivePath;
Reid Spencerdd04df02005-07-07 23:21:43 +000065 if (!ArchivePath.set(ArchiveName))
Reid Spencer63ab6692004-11-14 22:29:21 +000066 throw std::string("Archive name invalid: ") + ArchiveName;
67
68 // Make sure it exists, we don't create empty archives
69 if (!ArchivePath.exists())
Reid Spenceref11c5e2005-02-13 07:34:17 +000070 throw std::string("Archive file does not exist");
Reid Spencer63ab6692004-11-14 22:29:21 +000071
Reid Spenceref11c5e2005-02-13 07:34:17 +000072 std::string err_msg;
Misha Brukman3da94ae2005-04-22 00:00:37 +000073 std::auto_ptr<Archive>
Reid Spenceref11c5e2005-02-13 07:34:17 +000074 AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
Reid Spencerbabb45b2004-11-16 06:41:20 +000075 Archive* TheArchive = AutoArchive.get();
Reid Spenceref11c5e2005-02-13 07:34:17 +000076 if (!TheArchive)
77 throw err_msg;
Reid Spencer63ab6692004-11-14 22:29:21 +000078
Reid Spencer30836fc2006-08-25 05:29:36 +000079 if (TheArchive->writeToDisk(true, false, false, &err_msg ))
Reid Spencer3039b992006-07-07 19:09:14 +000080 throw err_msg;
Reid Spencer63ab6692004-11-14 22:29:21 +000081
Reid Spencerbabb45b2004-11-16 06:41:20 +000082 if (Verbose)
83 printSymbolTable(TheArchive);
Reid Spencer63ab6692004-11-14 22:29:21 +000084
85 } catch (const char*msg) {
86 std::cerr << argv[0] << ": " << msg << "\n\n";
87 exitCode = 1;
88 } catch (const std::string& msg) {
89 std::cerr << argv[0] << ": " << msg << "\n";
90 exitCode = 2;
91 } catch (...) {
Reid Spencerbabb45b2004-11-16 06:41:20 +000092 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer63ab6692004-11-14 22:29:21 +000093 exitCode = 3;
94 }
Reid Spencer63ab6692004-11-14 22:29:21 +000095 return exitCode;
96}