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"
|
2007-05-06 09:29:57 +00:00
|
|
|
#include "llvm/Bitcode/Archive.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-11-14 22:29:21 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2011-01-10 02:34:23 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/Format.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"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/Signals.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
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) {
|
2010-11-29 23:02:20 +00:00
|
|
|
outs() << "\nArchive Symbol Table:\n";
|
2004-11-16 06:41:20 +00:00
|
|
|
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;
|
2010-11-29 23:02:20 +00:00
|
|
|
outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
|
2004-11-16 06:41:20 +00:00
|
|
|
}
|
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
|
|
|
|
2009-07-15 22:16:10 +00:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
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;
|
|
|
|
|
2012-10-26 10:49:15 +00:00
|
|
|
// Check the path name of the archive
|
|
|
|
sys::Path ArchivePath;
|
|
|
|
if (!ArchivePath.set(ArchiveName)) {
|
|
|
|
errs() << argv[0] << ": " << "Archive name invalid: " << ArchiveName <<
|
|
|
|
"\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2012-10-26 10:49:15 +00:00
|
|
|
// Make sure it exists, we don't create empty archives
|
|
|
|
bool Exists;
|
|
|
|
if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) {
|
|
|
|
errs() << argv[0] << ": " << "Archive file does not exist" <<
|
|
|
|
ArchivePath.str() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2012-10-26 10:49:15 +00:00
|
|
|
std::string err_msg;
|
|
|
|
std::auto_ptr<Archive>
|
|
|
|
AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg));
|
|
|
|
Archive* TheArchive = AutoArchive.get();
|
|
|
|
if (!TheArchive) {
|
|
|
|
errs() << argv[0] << ": " << err_msg << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2012-10-26 10:49:15 +00:00
|
|
|
if (TheArchive->writeToDisk(true, false, &err_msg )) {
|
|
|
|
errs() << argv[0] << ": " << err_msg << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2004-11-14 22:29:21 +00:00
|
|
|
|
2012-10-26 10:49:15 +00:00
|
|
|
if (Verbose)
|
|
|
|
printSymbolTable(TheArchive);
|
2004-11-14 22:29:21 +00:00
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
}
|