A tool for adding a symbol table to LLVM Archives
[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 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"
17 #include "llvm/Support/FileUtilities.h"
18 #include "llvm/System/Signals.h"
19 #include <iostream>
20 #include <algorithm>
21 #include <iomanip>
22
23 using namespace llvm;
24
25 // llvm-ar operation code and modifier flags
26 cl::opt<std::string> 
27 ArchiveName(cl::Positional, cl::Optional, cl::desc("<archive-file>..."));
28
29 cl::opt<bool>
30 Verbose("verbose",cl::Optional,cl::init(false),
31         cl::desc("Print the symbol table"));
32
33 sys::Path TmpArchive;
34
35 void cleanup() {
36   if (TmpArchive.exists())
37     TmpArchive.destroyFile();
38 }
39
40 int main(int argc, char **argv) {
41
42   // Have the command line options parsed and handle things
43   // like --help and --version.
44   cl::ParseCommandLineOptions(argc, argv,
45     " LLVM Archive Index Generator (llvm-ranlib)\n\n"
46     "  This program adds or updates an index of bytecode symbols\n"
47     "  to an LLVM archive file."
48   );
49
50   // Print a stack trace if we signal out.
51   sys::PrintStackTraceOnErrorSignal();
52
53   int exitCode = 0;
54
55   // Make sure we don't exit with "unhandled exception".
56   try {
57
58     // Check the path name of the archive
59     sys::Path ArchivePath;
60     if (!ArchivePath.setFile(ArchiveName))
61       throw std::string("Archive name invalid: ") + ArchiveName;
62
63     // Make sure it exists, we don't create empty archives
64     if (!ArchivePath.exists())
65       throw "Archive file does not exist";
66
67     // Archive* TheArchive = Archive::OpenAndLoad(ArchivePath);
68     Archive* TheArchive = Archive::OpenAndLoad(ArchivePath);
69
70     assert(TheArchive && "Unable to instantiate the archive");
71
72     TheArchive->writeToDisk(true,false,false,Verbose);
73
74     delete TheArchive;
75
76   } catch (const char*msg) {
77     std::cerr << argv[0] << ": " << msg << "\n\n";
78     exitCode = 1;
79   } catch (const std::string& msg) {
80     std::cerr << argv[0] << ": " << msg << "\n";
81     exitCode = 2;
82   } catch (...) {
83     std::cerr << argv[0] << ": An nexpected unknown exception occurred.\n";
84     exitCode = 3;
85   }
86   cleanup();
87   return exitCode;
88 }