Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 1 | //===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Adds or updates an index (symbol table) for an LLVM archive file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Module.h" |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 15 | #include "llvm/Bitcode/Archive.h" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 18 | #include "llvm/System/Signals.h" |
| 19 | #include <iostream> |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 20 | #include <iomanip> |
Duraid Madina | 4e4fe66 | 2005-12-28 06:58:12 +0000 | [diff] [blame] | 21 | #include <memory> |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | // llvm-ar operation code and modifier flags |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 26 | static cl::opt<std::string> |
Reid Spencer | 3d1cc28 | 2004-12-30 17:51:57 +0000 | [diff] [blame] | 27 | ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>")); |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 28 | |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 29 | static cl::opt<bool> |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 30 | Verbose("verbose",cl::Optional,cl::init(false), |
| 31 | cl::desc("Print the symbol table")); |
| 32 | |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 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(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 37 | for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end(); |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 38 | I != E; ++I ) { |
| 39 | unsigned offset = TheArchive->getFirstFileOffset() + I->second; |
| 40 | std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n"; |
| 41 | } |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | int main(int argc, char **argv) { |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 45 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 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" |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 51 | " This program adds or updates an index of bitcode symbols\n" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 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; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 65 | if (!ArchivePath.set(ArchiveName)) |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 66 | throw std::string("Archive name invalid: ") + ArchiveName; |
| 67 | |
| 68 | // Make sure it exists, we don't create empty archives |
| 69 | if (!ArchivePath.exists()) |
Reid Spencer | ef11c5e | 2005-02-13 07:34:17 +0000 | [diff] [blame] | 70 | throw std::string("Archive file does not exist"); |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 71 | |
Reid Spencer | ef11c5e | 2005-02-13 07:34:17 +0000 | [diff] [blame] | 72 | std::string err_msg; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 73 | std::auto_ptr<Archive> |
Reid Spencer | ef11c5e | 2005-02-13 07:34:17 +0000 | [diff] [blame] | 74 | AutoArchive(Archive::OpenAndLoad(ArchivePath,&err_msg)); |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 75 | Archive* TheArchive = AutoArchive.get(); |
Reid Spencer | ef11c5e | 2005-02-13 07:34:17 +0000 | [diff] [blame] | 76 | if (!TheArchive) |
| 77 | throw err_msg; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 78 | |
Reid Spencer | 30836fc | 2006-08-25 05:29:36 +0000 | [diff] [blame] | 79 | if (TheArchive->writeToDisk(true, false, false, &err_msg )) |
Reid Spencer | 3039b99 | 2006-07-07 19:09:14 +0000 | [diff] [blame] | 80 | throw err_msg; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 81 | |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 82 | if (Verbose) |
| 83 | printSymbolTable(TheArchive); |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 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 (...) { |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 92 | std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n"; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 93 | exitCode = 3; |
| 94 | } |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 95 | return exitCode; |
| 96 | } |