blob: a6611a3e8091c75ef36c11cc3b92f5fe8bf5d466 [file] [log] [blame]
Chris Lattner97f752f2003-12-30 07:45:46 +00001//===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Tanya Lattner14baebf2003-08-28 15:22:38 +00009//
Misha Brukman3da94ae2005-04-22 00:00:37 +000010// Builds up (relatively) standard unix archive files (.a) containing LLVM
Gabor Greifa99be512007-07-05 17:07:56 +000011// bitcode or other files.
Tanya Lattner14baebf2003-08-28 15:22:38 +000012//
13//===----------------------------------------------------------------------===//
Brian Gaeke4fa9fd32003-10-10 18:47:08 +000014
Tanya Lattner14baebf2003-08-28 15:22:38 +000015#include "llvm/Module.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000016#include "llvm/Bitcode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000018#include "llvm/Support/ManagedStatic.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000019#include "llvm/System/Signals.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000020#include <iostream>
Reid Spencer3a1582b2004-11-14 22:20:07 +000021#include <algorithm>
22#include <iomanip>
Duraid Madina6927b062005-12-28 06:56:09 +000023#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000024using namespace llvm;
25
Dan Gohman82f209e2007-10-15 21:10:03 +000026// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000027static cl::opt<bool>
28X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000029 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000030
Reid Spencerbede5832004-11-16 06:41:09 +000031// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000032static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000033Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000034
Reid Spencerbede5832004-11-16 06:41:09 +000035// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000036static cl::list<std::string>
37RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000038 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000039
Reid Spencerbede5832004-11-16 06:41:09 +000040// MoreHelp - Provide additional help output explaining the operations and
41// modifiers of llvm-ar. This object instructs the CommandLine library
42// to print the text of the constructor when the --help option is given.
43static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000044 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000045 " d[NsS] - delete file(s) from the archive\n"
46 " m[abiSs] - move file(s) in the archive\n"
47 " p[kN] - print file(s) found in the archive\n"
48 " q[ufsS] - quick append file(s) to the archive\n"
49 " r[abfiuzRsS] - replace or insert file(s) into the archive\n"
50 " t - display contents of archive\n"
51 " x[No] - extract file(s) from the archive\n"
52 "\nMODIFIERS (operation specific):\n"
53 " [a] - put file(s) after [relpos]\n"
54 " [b] - put file(s) before [relpos] (same as [i])\n"
55 " [f] - truncate inserted file names\n"
56 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000057 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000058 " [N] - use instance [count] of name\n"
59 " [o] - preserve original dates\n"
60 " [P] - use full path names when matching\n"
61 " [R] - recurse through directories when inserting\n"
62 " [s] - create an archive index (cf. ranlib)\n"
63 " [S] - do not build a symbol table\n"
64 " [u] - update only files newer than archive contents\n"
65 " [z] - compress files before inserting/extracting\n"
66 "\nMODIFIERS (generic):\n"
67 " [c] - do not warn if the library had to be created\n"
68 " [v] - be verbose about actions taken\n"
69 " [V] - be *really* verbose about actions taken\n"
70);
71
Reid Spencer3a1582b2004-11-14 22:20:07 +000072// This enumeration delineates the kinds of operations on an archive
73// that are permitted.
74enum ArchiveOperation {
75 NoOperation, ///< An operation hasn't been specified
76 Print, ///< Print the contents of the archive
77 Delete, ///< Delete the specified members
78 Move, ///< Move members to end or as given by {a,b,i} modifiers
79 QuickAppend, ///< Quickly append to end of archive
80 ReplaceOrInsert, ///< Replace or Insert members
81 DisplayTable, ///< Display the table of contents
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000082 Extract ///< Extract files back to file system
Tanya Lattner57bd7962003-12-06 23:01:25 +000083};
Tanya Lattner14baebf2003-08-28 15:22:38 +000084
Reid Spencer3a1582b2004-11-14 22:20:07 +000085// Modifiers to follow operation to vary behavior
86bool AddAfter = false; ///< 'a' modifier
87bool AddBefore = false; ///< 'b' modifier
Misha Brukman3da94ae2005-04-22 00:00:37 +000088bool Create = false; ///< 'c' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000089bool TruncateNames = false; ///< 'f' modifier
90bool InsertBefore = false; ///< 'i' modifier
Gabor Greifa99be512007-07-05 17:07:56 +000091bool DontSkipBitcode = false; ///< 'k' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000092bool UseCount = false; ///< 'N' modifier
93bool OriginalDates = false; ///< 'o' modifier
94bool FullPath = false; ///< 'P' modifier
95bool RecurseDirectories = false; ///< 'R' modifier
96bool SymTable = true; ///< 's' & 'S' modifiers
97bool OnlyUpdate = false; ///< 'u' modifier
98bool Verbose = false; ///< 'v' modifier
99bool ReallyVerbose = false; ///< 'V' modifier
100bool Compression = false; ///< 'z' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +0000101
Reid Spencer3a1582b2004-11-14 22:20:07 +0000102// Relative Positional Argument (for insert/move). This variable holds
103// the name of the archive member to which the 'a', 'b' or 'i' modifier
104// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
105// one variable.
106std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000107
Reid Spencer3a1582b2004-11-14 22:20:07 +0000108// Select which of multiple entries in the archive with the same name should be
109// used (specified with -N) for the delete and extract operations.
110int Count = 1;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000111
Reid Spencer3a1582b2004-11-14 22:20:07 +0000112// This variable holds the name of the archive file as given on the
113// command line.
114std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000115
Reid Spencer3a1582b2004-11-14 22:20:07 +0000116// This variable holds the list of member files to proecess, as given
117// on the command line.
118std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000119
Reid Spencer3a1582b2004-11-14 22:20:07 +0000120// This variable holds the (possibly expanded) list of path objects that
121// correspond to files we will
Reid Spencerbede5832004-11-16 06:41:09 +0000122std::set<sys::Path> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000123
Reid Spencer3a1582b2004-11-14 22:20:07 +0000124// The Archive object to which all the editing operations will be sent.
125Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000126
Reid Spencer3a1582b2004-11-14 22:20:07 +0000127// getRelPos - Extract the member filename from the command line for
128// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000129void getRelPos() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000130 if(RestOfArgs.size() > 0) {
131 RelPos = RestOfArgs[0];
132 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000133 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000134 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000135 throw "Expected [relpos] for a, b, or i modifier";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000136}
137
Misha Brukman3da94ae2005-04-22 00:00:37 +0000138// getCount - Extract the [count] argument associated with the N modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000139// from the command line and check its value.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000140void getCount() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000141 if(RestOfArgs.size() > 0) {
142 Count = atoi(RestOfArgs[0].c_str());
143 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000144 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000145 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000146 throw "Expected [count] value with N modifier";
147
148 // Non-positive counts are not allowed
149 if (Count < 1)
150 throw "Invalid [count] value (not a positive integer)";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000151}
152
Reid Spencer3a1582b2004-11-14 22:20:07 +0000153// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000154void getArchive() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000155 if(RestOfArgs.size() > 0) {
156 ArchiveName = RestOfArgs[0];
157 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000158 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000159 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000160 throw "An archive name must be specified.";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000161}
162
Reid Spencer3a1582b2004-11-14 22:20:07 +0000163// getMembers - Copy over remaining items in RestOfArgs to our Members vector
164// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000165void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000166 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000167 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000168}
169
Reid Spencer3a1582b2004-11-14 22:20:07 +0000170// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000171// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000172// constraints on modifier/operation pairs have not been violated.
173ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000174
Reid Spencer3a1582b2004-11-14 22:20:07 +0000175 // Keep track of number of operations. We can only specify one
176 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000177 unsigned NumOperations = 0;
178
Reid Spencer3a1582b2004-11-14 22:20:07 +0000179 // Keep track of the number of positional modifiers (a,b,i). Only
180 // one can be specified.
181 unsigned NumPositional = 0;
182
183 // Keep track of which operation was requested
184 ArchiveOperation Operation = NoOperation;
185
Tanya Lattner57bd7962003-12-06 23:01:25 +0000186 for(unsigned i=0; i<Options.size(); ++i) {
187 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000188 case 'd': ++NumOperations; Operation = Delete; break;
189 case 'm': ++NumOperations; Operation = Move ; break;
190 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000191 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000192 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000193 case 't': ++NumOperations; Operation = DisplayTable; break;
194 case 'x': ++NumOperations; Operation = Extract; break;
195 case 'c': Create = true; break;
196 case 'f': TruncateNames = true; break;
Gabor Greifa99be512007-07-05 17:07:56 +0000197 case 'k': DontSkipBitcode = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000198 case 'l': /* accepted but unused */ break;
199 case 'o': OriginalDates = true; break;
200 case 'P': FullPath = true; break;
201 case 'R': RecurseDirectories = true; break;
202 case 's': SymTable = true; break;
203 case 'S': SymTable = false; break;
204 case 'u': OnlyUpdate = true; break;
205 case 'v': Verbose = true; break;
206 case 'V': Verbose = ReallyVerbose = true; break;
207 case 'z': Compression = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000208 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000209 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000210 AddAfter = true;
211 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000212 break;
213 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000214 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000215 AddBefore = true;
216 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000217 break;
218 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000219 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000220 InsertBefore = true;
221 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000222 break;
223 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000224 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000225 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000226 break;
227 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000228 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000229 }
230 }
231
Misha Brukman3da94ae2005-04-22 00:00:37 +0000232 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000233 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000234 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000235
236 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000237 getMembers();
238
Reid Spencer3a1582b2004-11-14 22:20:07 +0000239 // Perform various checks on the operation/modifier specification
240 // to make sure we are dealing with a legal request.
241 if (NumOperations == 0)
242 throw "You must specify at least one of the operations";
243 if (NumOperations > 1)
244 throw "Only one operation may be specified";
245 if (NumPositional > 1)
246 throw "You may only specify one of a, b, and i modifiers";
247 if (AddAfter || AddBefore || InsertBefore)
248 if (Operation != Move && Operation != ReplaceOrInsert)
249 throw "The 'a', 'b' and 'i' modifiers can only be specified with "
250 "the 'm' or 'r' operations";
251 if (RecurseDirectories && Operation != ReplaceOrInsert)
252 throw "The 'R' modifiers is only applicabe to the 'r' operation";
253 if (OriginalDates && Operation != Extract)
254 throw "The 'o' modifier is only applicable to the 'x' operation";
255 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
256 throw "The 'f' modifier is only applicable to the 'q' and 'r' operations";
257 if (OnlyUpdate && Operation != ReplaceOrInsert)
258 throw "The 'u' modifier is only applicable to the 'r' operation";
259 if (Compression && Operation!=ReplaceOrInsert && Operation!=Extract)
260 throw "The 'z' modifier is only applicable to the 'r' and 'x' operations";
261 if (Count > 1 && Members.size() > 1)
262 throw "Only one member name may be specified with the 'N' modifier";
263
264 // Return the parsed operation to the caller
265 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000266}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000267
Reid Spencer3a1582b2004-11-14 22:20:07 +0000268// recurseDirectories - Implements the "R" modifier. This function scans through
269// the Paths vector (built by buildPaths, below) and replaces any directories it
270// finds with all the files in that directory (recursively). It uses the
271// sys::Path::getDirectoryContent method to perform the actual directory scans.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000272bool
273recurseDirectories(const sys::Path& path,
274 std::set<sys::Path>& result, std::string* ErrMsg) {
275 result.clear();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000276 if (RecurseDirectories) {
Reid Spencerbede5832004-11-16 06:41:09 +0000277 std::set<sys::Path> content;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000278 if (path.getDirectoryContents(content, ErrMsg))
279 return true;
280
Misha Brukman3da94ae2005-04-22 00:00:37 +0000281 for (std::set<sys::Path>::iterator I = content.begin(), E = content.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000282 I != E; ++I) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000283 // Make sure it exists and is a directory
Reid Spencer184e67e2007-04-08 19:59:07 +0000284 sys::PathWithStatus PwS(*I);
285 const sys::FileStatus *Status = PwS.getFileStatus(false, ErrMsg);
Reid Spencer8475ec02007-03-29 19:05:44 +0000286 if (!Status)
287 return true;
288 if (Status->isDir) {
289 std::set<sys::Path> moreResults;
290 if (recurseDirectories(*I, moreResults, ErrMsg))
291 return true;
292 result.insert(moreResults.begin(), moreResults.end());
293 } else {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000294 result.insert(*I);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000295 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000296 }
297 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000298 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000299}
300
301// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000302// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000303// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000304bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000305 for (unsigned i = 0; i < Members.size(); i++) {
306 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000307 if (!aPath.set(Members[i]))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000308 throw std::string("File member name invalid: ") + Members[i];
309 if (checkExistence) {
310 if (!aPath.exists())
311 throw std::string("File does not exist: ") + Members[i];
Chris Lattner252ad032006-07-28 22:03:44 +0000312 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000313 sys::PathWithStatus PwS(aPath);
314 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000315 if (!si)
Chris Lattner252ad032006-07-28 22:03:44 +0000316 throw Err;
Reid Spencer8475ec02007-03-29 19:05:44 +0000317 if (si->isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000318 std::set<sys::Path> dirpaths;
319 if (recurseDirectories(aPath, dirpaths, ErrMsg))
320 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000321 Paths.insert(dirpaths.begin(),dirpaths.end());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000322 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000323 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000324 }
325 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000326 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000327 }
328 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000329 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000330}
331
Reid Spencerbede5832004-11-16 06:41:09 +0000332// printSymbolTable - print out the archive's symbol table.
333void printSymbolTable() {
334 std::cout << "\nArchive Symbol Table:\n";
335 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000336 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000337 I != E; ++I ) {
338 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
339 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
340 }
341}
342
Reid Spencer3a1582b2004-11-14 22:20:07 +0000343// doPrint - Implements the 'p' operation. This function traverses the archive
344// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000345// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000346// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000347bool doPrint(std::string* ErrMsg) {
348 if (buildPaths(false, ErrMsg))
349 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000350 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000351 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000352 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000353 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000354 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
355 if (countDown == 1) {
356 const char* data = reinterpret_cast<const char*>(I->getData());
357
358 // Skip things that don't make sense to print
Misha Brukman3da94ae2005-04-22 00:00:37 +0000359 if (I->isLLVMSymbolTable() || I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000360 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000361 continue;
362
363 if (Verbose)
Reid Spencer1fce0912004-12-11 00:14:15 +0000364 std::cout << "Printing " << I->getPath().toString() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000365
Chris Lattner44dadff2007-05-06 09:29:57 +0000366 unsigned len = I->getSize();
367 std::cout.write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000368 } else {
369 countDown--;
370 }
371 }
372 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000373 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000374}
375
376// putMode - utility function for printing out the file mode when the 't'
377// operation is in verbose mode.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000378void
379printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000380 if (mode & 004)
Reid Spencer3a1582b2004-11-14 22:20:07 +0000381 std::cout << "r";
382 else
383 std::cout << "-";
384 if (mode & 002)
385 std::cout << "w";
386 else
387 std::cout << "-";
388 if (mode & 001)
389 std::cout << "x";
390 else
391 std::cout << "-";
392}
393
394// doDisplayTable - Implement the 't' operation. This function prints out just
395// the file names of each of the members. However, if verbose mode is requested
396// ('v' modifier) then the file type, permission mode, user, group, size, and
397// modification time are also printed.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000398bool
399doDisplayTable(std::string* ErrMsg) {
400 if (buildPaths(false, ErrMsg))
401 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000402 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000403 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000404 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000405 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
406 if (Verbose) {
407 // FIXME: Output should be this format:
408 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000409 if (I->isBitcode())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000410 std::cout << "b";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000411 else if (I->isCompressed())
412 std::cout << "Z";
413 else
414 std::cout << " ";
415 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000416 printMode((mode >> 6) & 007);
417 printMode((mode >> 3) & 007);
418 printMode(mode & 007);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000419 std::cout << " " << std::setw(4) << I->getUser();
420 std::cout << "/" << std::setw(4) << I->getGroup();
421 std::cout << " " << std::setw(8) << I->getSize();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000422 std::cout << " " << std::setw(20) <<
Reid Spencerb55bc062004-11-14 23:17:41 +0000423 I->getModTime().toString().substr(4);
Reid Spencer1fce0912004-12-11 00:14:15 +0000424 std::cout << " " << I->getPath().toString() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000425 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000426 std::cout << I->getPath().toString() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000427 }
428 }
429 }
Reid Spencerbede5832004-11-16 06:41:09 +0000430 if (ReallyVerbose)
431 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000432 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000433}
434
435// doExtract - Implement the 'x' operation. This function extracts files back to
Reid Spencere5c9cb52006-08-23 00:39:35 +0000436// the file system, making sure to uncompress any that were compressed
Reid Spencer142ca8e2006-08-23 06:56:27 +0000437bool
438doExtract(std::string* ErrMsg) {
439 if (buildPaths(false, ErrMsg))
440 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000441 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000442 I != E; ++I ) {
443 if (Paths.empty() ||
444 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
445
446 // Make sure the intervening directories are created
447 if (I->hasPath()) {
448 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000449 dirs.eraseComponent();
Reid Spencere5c9cb52006-08-23 00:39:35 +0000450 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
451 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000452 }
453
454 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000455 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
456 std::ios::binary;
457 std::ofstream file(I->getPath().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000458
459 // Get the data and its length
460 const char* data = reinterpret_cast<const char*>(I->getData());
461 unsigned len = I->getSize();
462
Chris Lattner44dadff2007-05-06 09:29:57 +0000463 // Write the data.
464 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000465 file.close();
466
467 // If we're supposed to retain the original modification times, etc. do so
468 // now.
469 if (OriginalDates)
Chris Lattner252ad032006-07-28 22:03:44 +0000470 I->getPath().setStatusInfoOnDisk(I->getFileStatus());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000471 }
472 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000473 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000474}
475
476// doDelete - Implement the delete operation. This function deletes zero or more
477// members from the archive. Note that if the count is specified, there should
478// be no more than one path in the Paths list or else this algorithm breaks.
479// That check is enforced in parseCommandLine (above).
Reid Spencer142ca8e2006-08-23 06:56:27 +0000480bool
481doDelete(std::string* ErrMsg) {
482 if (buildPaths(false, ErrMsg))
483 return true;
484 if (Paths.empty())
485 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000486 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000487 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000488 I != E; ) {
489 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
490 if (countDown == 1) {
491 Archive::iterator J = I;
492 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000493 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000494 } else
495 countDown--;
496 } else {
497 ++I;
498 }
499 }
500
501 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000502 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000503 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000504 if (ReallyVerbose)
505 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000506 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000507}
508
509// doMore - Implement the move operation. This function re-arranges just the
510// order of the archive members so that when the archive is written the move
511// of the members is accomplished. Note the use of the RelPos variable to
512// determine where the items should be moved to.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000513bool
514doMove(std::string* ErrMsg) {
515 if (buildPaths(false, ErrMsg))
516 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000517
518 // By default and convention the place to move members to is the end of the
519 // archive.
520 Archive::iterator moveto_spot = TheArchive->end();
521
522 // However, if the relative positioning modifiers were used, we need to scan
523 // the archive to find the member in question. If we don't find it, its no
524 // crime, we just move to the end.
525 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000526 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000527 I != E; ++I ) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000528 if (RelPos == I->getPath().toString()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000529 if (AddAfter) {
530 moveto_spot = I;
531 moveto_spot++;
532 } else {
533 moveto_spot = I;
534 }
535 break;
536 }
537 }
538 }
539
540 // Keep a list of the paths remaining to be moved
Reid Spencerbede5832004-11-16 06:41:09 +0000541 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000542
543 // Scan the archive again, this time looking for the members to move to the
544 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000545 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000546 I != E && !remaining.empty(); ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000547 std::set<sys::Path>::iterator found =
Reid Spencer3a1582b2004-11-14 22:20:07 +0000548 std::find(remaining.begin(),remaining.end(),I->getPath());
549 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000550 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000551 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000552 remaining.erase(found);
553 }
554 }
555
556 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000557 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000558 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000559 if (ReallyVerbose)
560 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000561 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000562}
563
564// doQuickAppend - Implements the 'q' operation. This function just
565// indiscriminantly adds the members to the archive and rebuilds it.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000566bool
567doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000568 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000569 if (buildPaths(true, ErrMsg))
570 return true;
571 if (Paths.empty())
572 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000573
574 // Append them quickly.
Reid Spencerbede5832004-11-16 06:41:09 +0000575 for (std::set<sys::Path>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000576 PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000577 if (TheArchive->addFileBefore(*PI,TheArchive->end(),ErrMsg))
578 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000579 }
580
581 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000582 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000583 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000584 if (ReallyVerbose)
585 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000586 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000587}
588
589// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000590// any existing files or insert new ones into the archive.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000591bool
592doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000593
594 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000595 if (buildPaths(true, ErrMsg))
596 return true;
597 if (Paths.empty())
598 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000599
600 // Keep track of the paths that remain to be inserted.
Reid Spencerbede5832004-11-16 06:41:09 +0000601 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000602
603 // Default the insertion spot to the end of the archive
604 Archive::iterator insert_spot = TheArchive->end();
605
606 // Iterate over the archive contents
607 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
608 I != E && !remaining.empty(); ++I ) {
609
610 // Determine if this archive member matches one of the paths we're trying
611 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000612
613 std::set<sys::Path>::iterator found = remaining.end();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000614 for (std::set<sys::Path>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000615 RE = remaining.end(); RI != RE; ++RI ) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000616 std::string compare(RI->toString());
Reid Spencer0de66b52004-12-02 09:21:55 +0000617 if (TruncateNames && compare.length() > 15) {
618 const char* nm = compare.c_str();
619 unsigned len = compare.length();
620 size_t slashpos = compare.rfind('/');
621 if (slashpos != std::string::npos) {
622 nm += slashpos + 1;
623 len -= slashpos +1;
624 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000625 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000626 len = 15;
627 compare.assign(nm,len);
628 }
Reid Spencer1fce0912004-12-11 00:14:15 +0000629 if (compare == I->getPath().toString()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000630 found = RI;
631 break;
632 }
633 }
634
Reid Spencer3a1582b2004-11-14 22:20:07 +0000635 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000636 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000637 sys::PathWithStatus PwS(*found);
638 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000639 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000640 return true;
Reid Spencer8475ec02007-03-29 19:05:44 +0000641 if (si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000642 if (OnlyUpdate) {
643 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000644 if (si->modTime > I->getModTime())
Reid Spencer0ff2d312006-08-24 23:45:08 +0000645 if (I->replaceWith(*found, ErrMsg))
646 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000647 } else {
648 // Replace the item regardless of time stamp
Reid Spencer0ff2d312006-08-24 23:45:08 +0000649 if (I->replaceWith(*found, ErrMsg))
650 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000651 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000652 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000653 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000654 }
655
656 // Remove it from our "to do" list
657 remaining.erase(found);
658 }
659
660 // Determine if this is the place where we should insert
Reid Spencer1fce0912004-12-11 00:14:15 +0000661 if ((AddBefore || InsertBefore) && (RelPos == I->getPath().toString()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000662 insert_spot = I;
Reid Spencer1fce0912004-12-11 00:14:15 +0000663 else if (AddAfter && (RelPos == I->getPath().toString())) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000664 insert_spot = I;
665 insert_spot++;
666 }
667 }
668
669 // If we didn't replace all the members, some will remain and need to be
670 // inserted at the previously computed insert-spot.
671 if (!remaining.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000672 for (std::set<sys::Path>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000673 PE = remaining.end(); PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000674 if (TheArchive->addFileBefore(*PI,insert_spot, ErrMsg))
675 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000676 }
677 }
678
679 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000680 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000681 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000682 if (ReallyVerbose)
683 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000684 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000685}
686
687// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000688int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000689 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000690
Reid Spencer3a1582b2004-11-14 22:20:07 +0000691 // Have the command line options parsed and handle things
692 // like --help and --version.
693 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000694 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000695 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000696 );
697
698 // Print a stack trace if we signal out.
Reid Spencer9de7b332004-08-29 19:28:55 +0000699 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000700
Reid Spencer3a1582b2004-11-14 22:20:07 +0000701 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000702
Reid Spencer3a1582b2004-11-14 22:20:07 +0000703 // Make sure we don't exit with "unhandled exception".
704 try {
705 // Do our own parsing of the command line because the CommandLine utility
706 // can't handle the grouped positional parameters without a dash.
707 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000708
Reid Spencer3a1582b2004-11-14 22:20:07 +0000709 // Check the path name of the archive
710 sys::Path ArchivePath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000711 if (!ArchivePath.set(ArchiveName))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000712 throw std::string("Archive name invalid: ") + ArchiveName;
713
714 // Create or open the archive object.
715 if (!ArchivePath.exists()) {
716 // Produce a warning if we should and we're creating the archive
717 if (!Create)
Reid Spencer1fce0912004-12-11 00:14:15 +0000718 std::cerr << argv[0] << ": creating " << ArchivePath.toString() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000719 TheArchive = Archive::CreateEmpty(ArchivePath);
Andrew Lenharth63b8c1f2008-02-28 22:24:48 +0000720 TheArchive->writeToDisk();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000721 } else {
Chris Lattner67c38212004-12-15 07:44:15 +0000722 std::string Error;
723 TheArchive = Archive::OpenAndLoad(ArchivePath, &Error);
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000724 if (TheArchive == 0) {
Chris Lattner67c38212004-12-15 07:44:15 +0000725 std::cerr << argv[0] << ": error loading '" << ArchivePath << "': "
726 << Error << "!\n";
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000727 return 1;
728 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000729 }
730
731 // Make sure we're not fooling ourselves.
732 assert(TheArchive && "Unable to instantiate the archive");
733
Reid Spencerbede5832004-11-16 06:41:09 +0000734 // Make sure we clean up the archive even on failure.
735 std::auto_ptr<Archive> AutoArchive(TheArchive);
736
Reid Spencer3a1582b2004-11-14 22:20:07 +0000737 // Perform the operation
Reid Spencere5c9cb52006-08-23 00:39:35 +0000738 std::string ErrMsg;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000739 bool haveError = false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000740 switch (Operation) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000741 case Print: haveError = doPrint(&ErrMsg); break;
742 case Delete: haveError = doDelete(&ErrMsg); break;
743 case Move: haveError = doMove(&ErrMsg); break;
744 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
745 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
746 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
747 case Extract: haveError = doExtract(&ErrMsg); break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000748 case NoOperation:
749 std::cerr << argv[0] << ": No operation was selected.\n";
750 break;
751 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000752 if (haveError) {
753 std::cerr << argv[0] << ": " << ErrMsg << "\n";
754 return 1;
755 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000756 } catch (const char*msg) {
757 // These errors are usage errors, thrown only by the various checks in the
758 // code above.
759 std::cerr << argv[0] << ": " << msg << "\n\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000760 cl::PrintHelpMessage();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000761 exitCode = 1;
762 } catch (const std::string& msg) {
763 // These errors are thrown by LLVM libraries (e.g. lib System) and represent
764 // a more serious error so we bump the exitCode and don't print the usage.
765 std::cerr << argv[0] << ": " << msg << "\n";
766 exitCode = 2;
767 } catch (...) {
768 // This really shouldn't happen, but just in case ....
Reid Spencerbede5832004-11-16 06:41:09 +0000769 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000770 exitCode = 3;
771 }
772
773 // Return result code back to operating system.
774 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000775}