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 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 14 | #include "llvm/LLVMContext.h" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/Archive.h" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/PrettyStackTrace.h" |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 21 | #include "llvm/System/Signals.h" |
| 22 | #include <iostream> |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 23 | #include <iomanip> |
Duraid Madina | 4e4fe66 | 2005-12-28 06:58:12 +0000 | [diff] [blame] | 24 | #include <memory> |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | // llvm-ar operation code and modifier flags |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 29 | static cl::opt<std::string> |
Reid Spencer | 3d1cc28 | 2004-12-30 17:51:57 +0000 | [diff] [blame] | 30 | ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>")); |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 31 | |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 32 | static cl::opt<bool> |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 33 | Verbose("verbose",cl::Optional,cl::init(false), |
| 34 | cl::desc("Print the symbol table")); |
| 35 | |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 36 | // printSymbolTable - print out the archive's symbol table. |
| 37 | void printSymbolTable(Archive* TheArchive) { |
| 38 | std::cout << "\nArchive Symbol Table:\n"; |
| 39 | const Archive::SymTabType& symtab = TheArchive->getSymbolTable(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 40 | for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end(); |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 41 | I != E; ++I ) { |
| 42 | unsigned offset = TheArchive->getFirstFileOffset() + I->second; |
| 43 | std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n"; |
| 44 | } |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 48 | // Print a stack trace if we signal out. |
| 49 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 50 | llvm::PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 51 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame^] | 52 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 53 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 54 | |
| 55 | // Have the command line options parsed and handle things |
| 56 | // like --help and --version. |
| 57 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 58 | "LLVM Archive Index Generator (llvm-ranlib)\n\n" |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 59 | " This program adds or updates an index of bitcode symbols\n" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 60 | " to an LLVM archive file." |
| 61 | ); |
| 62 | |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 63 | int exitCode = 0; |
| 64 | |
| 65 | // Make sure we don't exit with "unhandled exception". |
| 66 | try { |
| 67 | |
| 68 | // Check the path name of the archive |
| 69 | sys::Path ArchivePath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 70 | if (!ArchivePath.set(ArchiveName)) |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 71 | throw std::string("Archive name invalid: ") + ArchiveName; |
| 72 | |
| 73 | // Make sure it exists, we don't create empty archives |
| 74 | if (!ArchivePath.exists()) |
Reid Spencer | ef11c5e | 2005-02-13 07:34:17 +0000 | [diff] [blame] | 75 | throw std::string("Archive file does not exist"); |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 76 | |
Reid Spencer | ef11c5e | 2005-02-13 07:34:17 +0000 | [diff] [blame] | 77 | std::string err_msg; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 78 | std::auto_ptr<Archive> |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 79 | AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg)); |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 80 | Archive* TheArchive = AutoArchive.get(); |
Reid Spencer | ef11c5e | 2005-02-13 07:34:17 +0000 | [diff] [blame] | 81 | if (!TheArchive) |
| 82 | throw err_msg; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 83 | |
Reid Spencer | 30836fc | 2006-08-25 05:29:36 +0000 | [diff] [blame] | 84 | if (TheArchive->writeToDisk(true, false, false, &err_msg )) |
Reid Spencer | 3039b99 | 2006-07-07 19:09:14 +0000 | [diff] [blame] | 85 | throw err_msg; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 86 | |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 87 | if (Verbose) |
| 88 | printSymbolTable(TheArchive); |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 89 | |
Misha Brukman | f2f8d79 | 2008-12-31 17:41:49 +0000 | [diff] [blame] | 90 | } catch (const char* msg) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 91 | errs() << argv[0] << ": " << msg << "\n\n"; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 92 | exitCode = 1; |
| 93 | } catch (const std::string& msg) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 94 | errs() << argv[0] << ": " << msg << "\n"; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 95 | exitCode = 2; |
| 96 | } catch (...) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 97 | errs() << argv[0] << ": An unexpected unknown exception occurred.\n"; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 98 | exitCode = 3; |
| 99 | } |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 100 | return exitCode; |
| 101 | } |