blob: 72106104c0067f22f8b93ab5e6b0334db212aab6 [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//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Chris Lattnercc14d252009-03-06 05:34:10 +000018#include "llvm/Support/PrettyStackTrace.h"
Reid Spencer63ab6692004-11-14 22:29:21 +000019#include "llvm/System/Signals.h"
20#include <iostream>
Reid Spencer63ab6692004-11-14 22:29:21 +000021#include <iomanip>
Duraid Madina4e4fe662005-12-28 06:58:12 +000022#include <memory>
Reid Spencer63ab6692004-11-14 22:29:21 +000023
24using namespace llvm;
25
26// llvm-ar operation code and modifier flags
Misha Brukman3da94ae2005-04-22 00:00:37 +000027static cl::opt<std::string>
Reid Spencer3d1cc282004-12-30 17:51:57 +000028ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
Reid Spencer63ab6692004-11-14 22:29:21 +000029
Reid Spencerbabb45b2004-11-16 06:41:20 +000030static cl::opt<bool>
Reid Spencer63ab6692004-11-14 22:29:21 +000031Verbose("verbose",cl::Optional,cl::init(false),
32 cl::desc("Print the symbol table"));
33
Reid Spencerbabb45b2004-11-16 06:41:20 +000034// printSymbolTable - print out the archive's symbol table.
35void printSymbolTable(Archive* TheArchive) {
36 std::cout << "\nArchive Symbol Table:\n";
37 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +000038 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbabb45b2004-11-16 06:41:20 +000039 I != E; ++I ) {
40 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
41 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
42 }
Reid Spencer63ab6692004-11-14 22:29:21 +000043}
44
45int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000046 // Print a stack trace if we signal out.
47 llvm::sys::PrintStackTraceOnErrorSignal();
48 llvm::PrettyStackTraceProgram X(argc, argv);
49
50 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer63ab6692004-11-14 22:29:21 +000051
52 // Have the command line options parsed and handle things
53 // like --help and --version.
54 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +000055 "LLVM Archive Index Generator (llvm-ranlib)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +000056 " This program adds or updates an index of bitcode symbols\n"
Reid Spencer63ab6692004-11-14 22:29:21 +000057 " to an LLVM archive file."
58 );
59
Reid Spencer63ab6692004-11-14 22:29:21 +000060 int exitCode = 0;
61
62 // Make sure we don't exit with "unhandled exception".
63 try {
64
65 // Check the path name of the archive
66 sys::Path ArchivePath;
Reid Spencerdd04df02005-07-07 23:21:43 +000067 if (!ArchivePath.set(ArchiveName))
Reid Spencer63ab6692004-11-14 22:29:21 +000068 throw std::string("Archive name invalid: ") + ArchiveName;
69
70 // Make sure it exists, we don't create empty archives
71 if (!ArchivePath.exists())
Reid Spenceref11c5e2005-02-13 07:34:17 +000072 throw std::string("Archive file does not exist");
Reid Spencer63ab6692004-11-14 22:29:21 +000073
Reid Spenceref11c5e2005-02-13 07:34:17 +000074 std::string err_msg;
Misha Brukman3da94ae2005-04-22 00:00:37 +000075 std::auto_ptr<Archive>
Reid Spenceref11c5e2005-02-13 07:34:17 +000076 AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg));
Reid Spencerbabb45b2004-11-16 06:41:20 +000077 Archive* TheArchive = AutoArchive.get();
Reid Spenceref11c5e2005-02-13 07:34:17 +000078 if (!TheArchive)
79 throw err_msg;
Reid Spencer63ab6692004-11-14 22:29:21 +000080
Reid Spencer30836fc2006-08-25 05:29:36 +000081 if (TheArchive->writeToDisk(true, false, false, &err_msg ))
Reid Spencer3039b992006-07-07 19:09:14 +000082 throw err_msg;
Reid Spencer63ab6692004-11-14 22:29:21 +000083
Reid Spencerbabb45b2004-11-16 06:41:20 +000084 if (Verbose)
85 printSymbolTable(TheArchive);
Reid Spencer63ab6692004-11-14 22:29:21 +000086
Misha Brukmanf2f8d792008-12-31 17:41:49 +000087 } catch (const char* msg) {
Reid Spencer63ab6692004-11-14 22:29:21 +000088 std::cerr << argv[0] << ": " << msg << "\n\n";
89 exitCode = 1;
90 } catch (const std::string& msg) {
91 std::cerr << argv[0] << ": " << msg << "\n";
92 exitCode = 2;
93 } catch (...) {
Reid Spencerbabb45b2004-11-16 06:41:20 +000094 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer63ab6692004-11-14 22:29:21 +000095 exitCode = 3;
96 }
Reid Spencer63ab6692004-11-14 22:29:21 +000097 return exitCode;
98}