blob: e2fbf7d55d53fca93b9e898270a633fb638e3dd7 [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>
20
21using namespace llvm;
22
23// llvm-ar operation code and modifier flags
Misha Brukman3da94ae2005-04-22 00:00:37 +000024static cl::opt<std::string>
Reid Spencer3d1cc282004-12-30 17:51:57 +000025ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
Reid Spencer63ab6692004-11-14 22:29:21 +000026
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();
Misha Brukman3da94ae2005-04-22 00:00:37 +000035 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbabb45b2004-11-16 06:41:20 +000036 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())
Reid Spenceref11c5e2005-02-13 07:34:17 +000067 throw std::string("Archive file does not exist");
Reid Spencer63ab6692004-11-14 22:29:21 +000068
Reid Spenceref11c5e2005-02-13 07:34:17 +000069 std::string err_msg;
Misha Brukman3da94ae2005-04-22 00:00:37 +000070 std::auto_ptr<Archive>
Reid Spenceref11c5e2005-02-13 07:34:17 +000071 AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
Reid Spencerbabb45b2004-11-16 06:41:20 +000072 Archive* TheArchive = AutoArchive.get();
Reid Spenceref11c5e2005-02-13 07:34:17 +000073 if (!TheArchive)
74 throw err_msg;
Reid Spencer63ab6692004-11-14 22:29:21 +000075
Reid Spencerbabb45b2004-11-16 06:41:20 +000076 TheArchive->writeToDisk(true, false, false );
Reid Spencer63ab6692004-11-14 22:29:21 +000077
Reid Spencerbabb45b2004-11-16 06:41:20 +000078 if (Verbose)
79 printSymbolTable(TheArchive);
Reid Spencer63ab6692004-11-14 22:29:21 +000080
81 } catch (const char*msg) {
82 std::cerr << argv[0] << ": " << msg << "\n\n";
83 exitCode = 1;
84 } catch (const std::string& msg) {
85 std::cerr << argv[0] << ": " << msg << "\n";
86 exitCode = 2;
87 } catch (...) {
Reid Spencerbabb45b2004-11-16 06:41:20 +000088 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer63ab6692004-11-14 22:29:21 +000089 exitCode = 3;
90 }
Reid Spencer63ab6692004-11-14 22:29:21 +000091 return exitCode;
92}