blob: fe9d3e2954af16b7624c7a874713a01d21c038a5 [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
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000014#include "llvm/IR/LLVMContext.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000015#include "llvm/Bitcode/Archive.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Module.h"
Reid Spencer63ab6692004-11-14 22:29:21 +000017#include "llvm/Support/CommandLine.h"
Michael J. Spencer54453f22011-01-10 02:34:23 +000018#include "llvm/Support/FileSystem.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000019#include "llvm/Support/Format.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000020#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000021#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000022#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000023#include "llvm/Support/raw_ostream.h"
Duraid Madina4e4fe662005-12-28 06:58:12 +000024#include <memory>
Reid Spencer63ab6692004-11-14 22:29:21 +000025using namespace llvm;
26
27// llvm-ar operation code and modifier flags
Misha Brukman3da94ae2005-04-22 00:00:37 +000028static cl::opt<std::string>
Reid Spencer3d1cc282004-12-30 17:51:57 +000029ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
Reid Spencer63ab6692004-11-14 22:29:21 +000030
Reid Spencerbabb45b2004-11-16 06:41:20 +000031static cl::opt<bool>
Reid Spencer63ab6692004-11-14 22:29:21 +000032Verbose("verbose",cl::Optional,cl::init(false),
33 cl::desc("Print the symbol table"));
34
Reid Spencerbabb45b2004-11-16 06:41:20 +000035// printSymbolTable - print out the archive's symbol table.
36void printSymbolTable(Archive* TheArchive) {
Chris Lattner424a04e2010-11-29 23:02:20 +000037 outs() << "\nArchive Symbol Table:\n";
Reid Spencerbabb45b2004-11-16 06:41:20 +000038 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +000039 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbabb45b2004-11-16 06:41:20 +000040 I != E; ++I ) {
41 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
Chris Lattner424a04e2010-11-29 23:02:20 +000042 outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
Reid Spencerbabb45b2004-11-16 06:41:20 +000043 }
Reid Spencer63ab6692004-11-14 22:29:21 +000044}
45
46int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000047 // Print a stack trace if we signal out.
48 llvm::sys::PrintStackTraceOnErrorSignal();
49 llvm::PrettyStackTraceProgram X(argc, argv);
Owen Anderson8b477ed2009-07-01 16:58:40 +000050
Owen Anderson0d7c6952009-07-15 22:16:10 +000051 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +000052 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer63ab6692004-11-14 22:29:21 +000053
54 // Have the command line options parsed and handle things
55 // like --help and --version.
56 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +000057 "LLVM Archive Index Generator (llvm-ranlib)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +000058 " This program adds or updates an index of bitcode symbols\n"
Reid Spencer63ab6692004-11-14 22:29:21 +000059 " to an LLVM archive file."
60 );
61
Reid Spencer63ab6692004-11-14 22:29:21 +000062 int exitCode = 0;
63
Joerg Sonnenberger975bc072012-10-26 10:49:15 +000064 // Check the path name of the archive
65 sys::Path ArchivePath;
66 if (!ArchivePath.set(ArchiveName)) {
67 errs() << argv[0] << ": " << "Archive name invalid: " << ArchiveName <<
68 "\n";
69 return 1;
Reid Spencer63ab6692004-11-14 22:29:21 +000070 }
Joerg Sonnenberger975bc072012-10-26 10:49:15 +000071
72 // Make sure it exists, we don't create empty archives
73 bool Exists;
74 if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) {
75 errs() << argv[0] << ": " << "Archive file does not exist" <<
76 ArchivePath.str() << "\n";
77 return 1;
78 }
79
80 std::string err_msg;
81 std::auto_ptr<Archive>
82 AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg));
83 Archive* TheArchive = AutoArchive.get();
84 if (!TheArchive) {
85 errs() << argv[0] << ": " << err_msg << "\n";
86 return 1;
87 }
88
89 if (TheArchive->writeToDisk(true, false, &err_msg )) {
90 errs() << argv[0] << ": " << err_msg << "\n";
91 return 1;
92 }
93
94 if (Verbose)
95 printSymbolTable(TheArchive);
96
Reid Spencer63ab6692004-11-14 22:29:21 +000097 return exitCode;
98}