2004-11-14 22:29:21 +00:00
|
|
|
//===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2004-11-14 22:29:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2004-11-14 22:29:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Adds or updates an index (symbol table) for an LLVM archive file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-01 16:58:40 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2004-11-14 22:29:21 +00:00
|
|
|
#include "llvm/Module.h"
|
2007-05-06 09:29:57 +00:00
|
|
|
#include "llvm/Bitcode/Archive.h"
|
2004-11-14 22:29:21 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-03-06 05:34:10 +00:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2009-07-15 16:35:29 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-11-14 22:29:21 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2005-12-28 06:58:12 +00:00
|
|
|
#include <memory>
|
2004-11-14 22:29:21 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// llvm-ar operation code and modifier flags
|
2005-04-22 00:00:37 +00:00
|
|
|
static cl::opt<std::string>
|
2004-12-30 17:51:57 +00:00
|
|
|
ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2004-11-16 06:41:20 +00:00
|
|
|
static cl::opt<bool>
|
2004-11-14 22:29:21 +00:00
|
|
|
Verbose("verbose",cl::Optional,cl::init(false),
|
|
|
|
cl::desc("Print the symbol table"));
|
|
|
|
|
2004-11-16 06:41:20 +00:00
|
|
|
// printSymbolTable - print out the archive's symbol table.
|
|
|
|
void printSymbolTable(Archive* TheArchive) {
|
|
|
|
std::cout << "\nArchive Symbol Table:\n";
|
|
|
|
const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
|
2005-04-22 00:00:37 +00:00
|
|
|
for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
|
2004-11-16 06:41:20 +00:00
|
|
|
I != E; ++I ) {
|
|
|
|
unsigned offset = TheArchive->getFirstFileOffset() + I->second;
|
|
|
|
std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
|
|
|
|
}
|
2004-11-14 22:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 05:34:10 +00:00
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal();
|
|
|
|
llvm::PrettyStackTraceProgram X(argc, argv);
|
2009-07-01 16:58:40 +00:00
|
|
|
|
|
|
|
LLVMContext Context;
|
2009-03-06 05:34:10 +00:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2004-11-14 22:29:21 +00:00
|
|
|
|
|
|
|
// Have the command line options parsed and handle things
|
|
|
|
// like --help and --version.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
2007-10-08 15:45:12 +00:00
|
|
|
"LLVM Archive Index Generator (llvm-ranlib)\n\n"
|
2007-07-05 17:07:56 +00:00
|
|
|
" This program adds or updates an index of bitcode symbols\n"
|
2004-11-14 22:29:21 +00:00
|
|
|
" to an LLVM archive file."
|
|
|
|
);
|
|
|
|
|
|
|
|
int exitCode = 0;
|
|
|
|
|
|
|
|
// Make sure we don't exit with "unhandled exception".
|
|
|
|
try {
|
|
|
|
|
|
|
|
// Check the path name of the archive
|
|
|
|
sys::Path ArchivePath;
|
2005-07-07 23:21:43 +00:00
|
|
|
if (!ArchivePath.set(ArchiveName))
|
2004-11-14 22:29:21 +00:00
|
|
|
throw std::string("Archive name invalid: ") + ArchiveName;
|
|
|
|
|
|
|
|
// Make sure it exists, we don't create empty archives
|
|
|
|
if (!ArchivePath.exists())
|
2005-02-13 07:34:17 +00:00
|
|
|
throw std::string("Archive file does not exist");
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2005-02-13 07:34:17 +00:00
|
|
|
std::string err_msg;
|
2005-04-22 00:00:37 +00:00
|
|
|
std::auto_ptr<Archive>
|
2009-07-01 21:22:36 +00:00
|
|
|
AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg));
|
2004-11-16 06:41:20 +00:00
|
|
|
Archive* TheArchive = AutoArchive.get();
|
2005-02-13 07:34:17 +00:00
|
|
|
if (!TheArchive)
|
|
|
|
throw err_msg;
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2006-08-25 05:29:36 +00:00
|
|
|
if (TheArchive->writeToDisk(true, false, false, &err_msg ))
|
2006-07-07 19:09:14 +00:00
|
|
|
throw err_msg;
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2004-11-16 06:41:20 +00:00
|
|
|
if (Verbose)
|
|
|
|
printSymbolTable(TheArchive);
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2008-12-31 17:41:49 +00:00
|
|
|
} catch (const char* msg) {
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << argv[0] << ": " << msg << "\n\n";
|
2004-11-14 22:29:21 +00:00
|
|
|
exitCode = 1;
|
|
|
|
} catch (const std::string& msg) {
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << argv[0] << ": " << msg << "\n";
|
2004-11-14 22:29:21 +00:00
|
|
|
exitCode = 2;
|
|
|
|
} catch (...) {
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << argv[0] << ": An unexpected unknown exception occurred.\n";
|
2004-11-14 22:29:21 +00:00
|
|
|
exitCode = 3;
|
|
|
|
}
|
|
|
|
return exitCode;
|
|
|
|
}
|