Simplify the handling of .size expressions.
[oota-llvm.git] / tools / llvm-ranlib / llvm-ranlib.cpp
1 //===-- llvm-ranlib.cpp - LLVM archive index generator --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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/LLVMContext.h"
15 #include "llvm/Module.h"
16 #include "llvm/Bitcode/Archive.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/Signals.h"
23 #include <memory>
24 using namespace llvm;
25
26 // llvm-ar operation code and modifier flags
27 static cl::opt<std::string>
28 ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>"));
29
30 static cl::opt<bool>
31 Verbose("verbose",cl::Optional,cl::init(false),
32         cl::desc("Print the symbol table"));
33
34 // printSymbolTable - print out the archive's symbol table.
35 void printSymbolTable(Archive* TheArchive) {
36   outs() << "\nArchive Symbol Table:\n";
37   const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
38   for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
39        I != E; ++I ) {
40     unsigned offset = TheArchive->getFirstFileOffset() + I->second;
41     outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
42   }
43 }
44
45 int main(int argc, char **argv) {
46   // Print a stack trace if we signal out.
47   llvm::sys::PrintStackTraceOnErrorSignal();
48   llvm::PrettyStackTraceProgram X(argc, argv);
49
50   LLVMContext &Context = getGlobalContext();
51   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
52
53   // Have the command line options parsed and handle things
54   // like --help and --version.
55   cl::ParseCommandLineOptions(argc, argv,
56     "LLVM Archive Index Generator (llvm-ranlib)\n\n"
57     "  This program adds or updates an index of bitcode symbols\n"
58     "  to an LLVM archive file."
59   );
60
61   int exitCode = 0;
62
63   // Make sure we don't exit with "unhandled exception".
64   try {
65
66     // Check the path name of the archive
67     sys::Path ArchivePath;
68     if (!ArchivePath.set(ArchiveName))
69       throw std::string("Archive name invalid: ") + ArchiveName;
70
71     // Make sure it exists, we don't create empty archives
72     if (!ArchivePath.exists())
73       throw std::string("Archive file does not exist");
74
75     std::string err_msg;
76     std::auto_ptr<Archive>
77       AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg));
78     Archive* TheArchive = AutoArchive.get();
79     if (!TheArchive)
80       throw err_msg;
81
82     if (TheArchive->writeToDisk(true, false, false, &err_msg ))
83       throw err_msg;
84
85     if (Verbose)
86       printSymbolTable(TheArchive);
87
88   } catch (const char* msg) {
89     errs() << argv[0] << ": " << msg << "\n\n";
90     exitCode = 1;
91   } catch (const std::string& msg) {
92     errs() << argv[0] << ": " << msg << "\n";
93     exitCode = 2;
94   } catch (...) {
95     errs() << argv[0] << ": An unexpected unknown exception occurred.\n";
96     exitCode = 3;
97   }
98   return exitCode;
99 }