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