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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/LLVMContext.h" |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 15 | #include "llvm/Bitcode/Archive.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Module.h" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Michael J. Spencer | 54453f2 | 2011-01-10 02:34:23 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/PrettyStackTrace.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Signals.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
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 | using namespace llvm; |
| 26 | |
| 27 | // llvm-ar operation code and modifier flags |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 28 | static cl::opt<std::string> |
Reid Spencer | 3d1cc28 | 2004-12-30 17:51:57 +0000 | [diff] [blame] | 29 | ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>")); |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 30 | |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 31 | static cl::opt<bool> |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 32 | Verbose("verbose",cl::Optional,cl::init(false), |
| 33 | cl::desc("Print the symbol table")); |
| 34 | |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 35 | // printSymbolTable - print out the archive's symbol table. |
| 36 | void printSymbolTable(Archive* TheArchive) { |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 37 | outs() << "\nArchive Symbol Table:\n"; |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 38 | const Archive::SymTabType& symtab = TheArchive->getSymbolTable(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 39 | for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end(); |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 40 | I != E; ++I ) { |
| 41 | unsigned offset = TheArchive->getFirstFileOffset() + I->second; |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 42 | outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n"; |
Reid Spencer | babb45b | 2004-11-16 06:41:20 +0000 | [diff] [blame] | 43 | } |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 47 | // Print a stack trace if we signal out. |
| 48 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 49 | llvm::PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 50 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 51 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 52 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 53 | |
| 54 | // Have the command line options parsed and handle things |
| 55 | // like --help and --version. |
| 56 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 57 | "LLVM Archive Index Generator (llvm-ranlib)\n\n" |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 58 | " This program adds or updates an index of bitcode symbols\n" |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 59 | " to an LLVM archive file." |
| 60 | ); |
| 61 | |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 62 | int exitCode = 0; |
| 63 | |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 64 | // Check the path name of the archive |
| 65 | sys::Path ArchivePath; |
| 66 | if (!ArchivePath.set(ArchiveName)) { |
| 67 | errs() << argv[0] << ": " << "Archive name invalid: " << ArchiveName << |
| 68 | "\n"; |
| 69 | return 1; |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 70 | } |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 71 | |
| 72 | // Make sure it exists, we don't create empty archives |
| 73 | bool Exists; |
| 74 | if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) { |
| 75 | errs() << argv[0] << ": " << "Archive file does not exist" << |
| 76 | ArchivePath.str() << "\n"; |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | std::string err_msg; |
| 81 | std::auto_ptr<Archive> |
| 82 | AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg)); |
| 83 | Archive* TheArchive = AutoArchive.get(); |
| 84 | if (!TheArchive) { |
| 85 | errs() << argv[0] << ": " << err_msg << "\n"; |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | if (TheArchive->writeToDisk(true, false, &err_msg )) { |
| 90 | errs() << argv[0] << ": " << err_msg << "\n"; |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | if (Verbose) |
| 95 | printSymbolTable(TheArchive); |
| 96 | |
Reid Spencer | 63ab669 | 2004-11-14 22:29:21 +0000 | [diff] [blame] | 97 | return exitCode; |
| 98 | } |