Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame^] | 1 | //===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 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/Bitcode/Archive.h" |
| 16 | #include "llvm/Support/CommandLine.h" |
| 17 | #include "llvm/Support/ManagedStatic.h" |
| 18 | #include "llvm/System/Signals.h" |
| 19 | #include <iostream> |
| 20 | #include <iomanip> |
| 21 | #include <memory> |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | // llvm-ar operation code and modifier flags |
| 26 | static cl::opt<std::string> |
| 27 | ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>")); |
| 28 | |
| 29 | static cl::opt<bool> |
| 30 | Verbose("verbose",cl::Optional,cl::init(false), |
| 31 | cl::desc("Print the symbol table")); |
| 32 | |
| 33 | // printSymbolTable - print out the archive's symbol table. |
| 34 | void printSymbolTable(Archive* TheArchive) { |
| 35 | std::cout << "\nArchive Symbol Table:\n"; |
| 36 | const Archive::SymTabType& symtab = TheArchive->getSymbolTable(); |
| 37 | for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end(); |
| 38 | I != E; ++I ) { |
| 39 | unsigned offset = TheArchive->getFirstFileOffset() + I->second; |
| 40 | std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n"; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | int main(int argc, char **argv) { |
| 45 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
| 46 | |
| 47 | // Have the command line options parsed and handle things |
| 48 | // like --help and --version. |
| 49 | cl::ParseCommandLineOptions(argc, argv, |
| 50 | " LLVM Archive Index Generator (llvm-ranlib)\n\n" |
| 51 | " This program adds or updates an index of bitcode symbols\n" |
| 52 | " to an LLVM archive file." |
| 53 | ); |
| 54 | |
| 55 | // Print a stack trace if we signal out. |
| 56 | sys::PrintStackTraceOnErrorSignal(); |
| 57 | |
| 58 | int exitCode = 0; |
| 59 | |
| 60 | // Make sure we don't exit with "unhandled exception". |
| 61 | try { |
| 62 | |
| 63 | // Check the path name of the archive |
| 64 | sys::Path ArchivePath; |
| 65 | if (!ArchivePath.set(ArchiveName)) |
| 66 | throw std::string("Archive name invalid: ") + ArchiveName; |
| 67 | |
| 68 | // Make sure it exists, we don't create empty archives |
| 69 | if (!ArchivePath.exists()) |
| 70 | throw std::string("Archive file does not exist"); |
| 71 | |
| 72 | std::string err_msg; |
| 73 | std::auto_ptr<Archive> |
| 74 | AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg)); |
| 75 | Archive* TheArchive = AutoArchive.get(); |
| 76 | if (!TheArchive) |
| 77 | throw err_msg; |
| 78 | |
| 79 | if (TheArchive->writeToDisk(true, false, false, &err_msg )) |
| 80 | throw err_msg; |
| 81 | |
| 82 | if (Verbose) |
| 83 | printSymbolTable(TheArchive); |
| 84 | |
| 85 | } catch (const char*msg) { |
| 86 | std::cerr << argv[0] << ": " << msg << "\n\n"; |
| 87 | exitCode = 1; |
| 88 | } catch (const std::string& msg) { |
| 89 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 90 | exitCode = 2; |
| 91 | } catch (...) { |
| 92 | std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n"; |
| 93 | exitCode = 3; |
| 94 | } |
| 95 | return exitCode; |
| 96 | } |