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