blob: 95957df53feec33b501f70dd1aee9275a7fe49e3 [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
Owen Anderson8b477ed2009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Tanya Lattner14baebf2003-08-28 15:22:38 +000016#include "llvm/Module.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000017#include "llvm/Bitcode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000019#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000020#include "llvm/Support/PrettyStackTrace.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000022#include "llvm/System/Signals.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000023#include <iostream>
Reid Spencer3a1582b2004-11-14 22:20:07 +000024#include <algorithm>
25#include <iomanip>
Duraid Madina6927b062005-12-28 06:56:09 +000026#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000027using namespace llvm;
28
Dan Gohman82f209e2007-10-15 21:10:03 +000029// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000030static cl::opt<bool>
31X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000032 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000033
Reid Spencerbede5832004-11-16 06:41:09 +000034// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000035static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000036Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000037
Reid Spencerbede5832004-11-16 06:41:09 +000038// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000039static cl::list<std::string>
40RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000041 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000042
Reid Spencerbede5832004-11-16 06:41:09 +000043// MoreHelp - Provide additional help output explaining the operations and
44// modifiers of llvm-ar. This object instructs the CommandLine library
45// to print the text of the constructor when the --help option is given.
46static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000047 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000048 " d[NsS] - delete file(s) from the archive\n"
49 " m[abiSs] - move file(s) in the archive\n"
50 " p[kN] - print file(s) found in the archive\n"
51 " q[ufsS] - quick append file(s) to the archive\n"
52 " r[abfiuzRsS] - replace or insert file(s) into the archive\n"
53 " t - display contents of archive\n"
54 " x[No] - extract file(s) from the archive\n"
55 "\nMODIFIERS (operation specific):\n"
56 " [a] - put file(s) after [relpos]\n"
57 " [b] - put file(s) before [relpos] (same as [i])\n"
58 " [f] - truncate inserted file names\n"
59 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000060 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000061 " [N] - use instance [count] of name\n"
62 " [o] - preserve original dates\n"
63 " [P] - use full path names when matching\n"
64 " [R] - recurse through directories when inserting\n"
65 " [s] - create an archive index (cf. ranlib)\n"
66 " [S] - do not build a symbol table\n"
67 " [u] - update only files newer than archive contents\n"
68 " [z] - compress files before inserting/extracting\n"
69 "\nMODIFIERS (generic):\n"
70 " [c] - do not warn if the library had to be created\n"
71 " [v] - be verbose about actions taken\n"
72 " [V] - be *really* verbose about actions taken\n"
73);
74
Reid Spencer3a1582b2004-11-14 22:20:07 +000075// This enumeration delineates the kinds of operations on an archive
76// that are permitted.
77enum ArchiveOperation {
78 NoOperation, ///< An operation hasn't been specified
79 Print, ///< Print the contents of the archive
80 Delete, ///< Delete the specified members
81 Move, ///< Move members to end or as given by {a,b,i} modifiers
82 QuickAppend, ///< Quickly append to end of archive
83 ReplaceOrInsert, ///< Replace or Insert members
84 DisplayTable, ///< Display the table of contents
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000085 Extract ///< Extract files back to file system
Tanya Lattner57bd7962003-12-06 23:01:25 +000086};
Tanya Lattner14baebf2003-08-28 15:22:38 +000087
Reid Spencer3a1582b2004-11-14 22:20:07 +000088// Modifiers to follow operation to vary behavior
89bool AddAfter = false; ///< 'a' modifier
90bool AddBefore = false; ///< 'b' modifier
Misha Brukman3da94ae2005-04-22 00:00:37 +000091bool Create = false; ///< 'c' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000092bool TruncateNames = false; ///< 'f' modifier
93bool InsertBefore = false; ///< 'i' modifier
Gabor Greifa99be512007-07-05 17:07:56 +000094bool DontSkipBitcode = false; ///< 'k' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000095bool UseCount = false; ///< 'N' modifier
96bool OriginalDates = false; ///< 'o' modifier
97bool FullPath = false; ///< 'P' modifier
98bool RecurseDirectories = false; ///< 'R' modifier
99bool SymTable = true; ///< 's' & 'S' modifiers
100bool OnlyUpdate = false; ///< 'u' modifier
101bool Verbose = false; ///< 'v' modifier
102bool ReallyVerbose = false; ///< 'V' modifier
103bool Compression = false; ///< 'z' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +0000104
Reid Spencer3a1582b2004-11-14 22:20:07 +0000105// Relative Positional Argument (for insert/move). This variable holds
106// the name of the archive member to which the 'a', 'b' or 'i' modifier
107// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
108// one variable.
109std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000110
Reid Spencer3a1582b2004-11-14 22:20:07 +0000111// Select which of multiple entries in the archive with the same name should be
112// used (specified with -N) for the delete and extract operations.
113int Count = 1;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000114
Reid Spencer3a1582b2004-11-14 22:20:07 +0000115// This variable holds the name of the archive file as given on the
116// command line.
117std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000118
Reid Spencer3a1582b2004-11-14 22:20:07 +0000119// This variable holds the list of member files to proecess, as given
120// on the command line.
121std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000122
Reid Spencer3a1582b2004-11-14 22:20:07 +0000123// This variable holds the (possibly expanded) list of path objects that
124// correspond to files we will
Reid Spencerbede5832004-11-16 06:41:09 +0000125std::set<sys::Path> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000126
Reid Spencer3a1582b2004-11-14 22:20:07 +0000127// The Archive object to which all the editing operations will be sent.
128Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000129
Reid Spencer3a1582b2004-11-14 22:20:07 +0000130// getRelPos - Extract the member filename from the command line for
131// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000132void getRelPos() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000133 if(RestOfArgs.size() > 0) {
134 RelPos = RestOfArgs[0];
135 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000136 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000137 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000138 throw "Expected [relpos] for a, b, or i modifier";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000139}
140
Misha Brukman3da94ae2005-04-22 00:00:37 +0000141// getCount - Extract the [count] argument associated with the N modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000142// from the command line and check its value.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000143void getCount() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000144 if(RestOfArgs.size() > 0) {
145 Count = atoi(RestOfArgs[0].c_str());
146 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000147 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000148 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000149 throw "Expected [count] value with N modifier";
150
151 // Non-positive counts are not allowed
152 if (Count < 1)
153 throw "Invalid [count] value (not a positive integer)";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000154}
155
Reid Spencer3a1582b2004-11-14 22:20:07 +0000156// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000157void getArchive() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000158 if(RestOfArgs.size() > 0) {
159 ArchiveName = RestOfArgs[0];
160 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000161 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000162 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000163 throw "An archive name must be specified.";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000164}
165
Reid Spencer3a1582b2004-11-14 22:20:07 +0000166// getMembers - Copy over remaining items in RestOfArgs to our Members vector
167// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000168void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000169 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000170 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000171}
172
Reid Spencer3a1582b2004-11-14 22:20:07 +0000173// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000174// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000175// constraints on modifier/operation pairs have not been violated.
176ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000177
Reid Spencer3a1582b2004-11-14 22:20:07 +0000178 // Keep track of number of operations. We can only specify one
179 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000180 unsigned NumOperations = 0;
181
Reid Spencer3a1582b2004-11-14 22:20:07 +0000182 // Keep track of the number of positional modifiers (a,b,i). Only
183 // one can be specified.
184 unsigned NumPositional = 0;
185
186 // Keep track of which operation was requested
187 ArchiveOperation Operation = NoOperation;
188
Tanya Lattner57bd7962003-12-06 23:01:25 +0000189 for(unsigned i=0; i<Options.size(); ++i) {
190 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000191 case 'd': ++NumOperations; Operation = Delete; break;
192 case 'm': ++NumOperations; Operation = Move ; break;
193 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000194 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000195 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000196 case 't': ++NumOperations; Operation = DisplayTable; break;
197 case 'x': ++NumOperations; Operation = Extract; break;
198 case 'c': Create = true; break;
199 case 'f': TruncateNames = true; break;
Gabor Greifa99be512007-07-05 17:07:56 +0000200 case 'k': DontSkipBitcode = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000201 case 'l': /* accepted but unused */ break;
202 case 'o': OriginalDates = true; break;
203 case 'P': FullPath = true; break;
204 case 'R': RecurseDirectories = true; break;
205 case 's': SymTable = true; break;
206 case 'S': SymTable = false; break;
207 case 'u': OnlyUpdate = true; break;
208 case 'v': Verbose = true; break;
209 case 'V': Verbose = ReallyVerbose = true; break;
210 case 'z': Compression = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000211 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000212 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000213 AddAfter = true;
214 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000215 break;
216 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000217 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000218 AddBefore = true;
219 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000220 break;
221 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000222 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000223 InsertBefore = true;
224 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000225 break;
226 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000227 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000228 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000229 break;
230 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000231 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000232 }
233 }
234
Misha Brukman3da94ae2005-04-22 00:00:37 +0000235 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000236 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000237 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000238
239 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000240 getMembers();
241
Reid Spencer3a1582b2004-11-14 22:20:07 +0000242 // Perform various checks on the operation/modifier specification
243 // to make sure we are dealing with a legal request.
244 if (NumOperations == 0)
245 throw "You must specify at least one of the operations";
246 if (NumOperations > 1)
247 throw "Only one operation may be specified";
248 if (NumPositional > 1)
249 throw "You may only specify one of a, b, and i modifiers";
250 if (AddAfter || AddBefore || InsertBefore)
251 if (Operation != Move && Operation != ReplaceOrInsert)
252 throw "The 'a', 'b' and 'i' modifiers can only be specified with "
253 "the 'm' or 'r' operations";
254 if (RecurseDirectories && Operation != ReplaceOrInsert)
255 throw "The 'R' modifiers is only applicabe to the 'r' operation";
256 if (OriginalDates && Operation != Extract)
257 throw "The 'o' modifier is only applicable to the 'x' operation";
258 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
259 throw "The 'f' modifier is only applicable to the 'q' and 'r' operations";
260 if (OnlyUpdate && Operation != ReplaceOrInsert)
261 throw "The 'u' modifier is only applicable to the 'r' operation";
262 if (Compression && Operation!=ReplaceOrInsert && Operation!=Extract)
263 throw "The 'z' modifier is only applicable to the 'r' and 'x' operations";
264 if (Count > 1 && Members.size() > 1)
265 throw "Only one member name may be specified with the 'N' modifier";
266
267 // Return the parsed operation to the caller
268 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000269}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000270
Reid Spencer3a1582b2004-11-14 22:20:07 +0000271// recurseDirectories - Implements the "R" modifier. This function scans through
272// the Paths vector (built by buildPaths, below) and replaces any directories it
273// finds with all the files in that directory (recursively). It uses the
274// sys::Path::getDirectoryContent method to perform the actual directory scans.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000275bool
276recurseDirectories(const sys::Path& path,
277 std::set<sys::Path>& result, std::string* ErrMsg) {
278 result.clear();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000279 if (RecurseDirectories) {
Reid Spencerbede5832004-11-16 06:41:09 +0000280 std::set<sys::Path> content;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000281 if (path.getDirectoryContents(content, ErrMsg))
282 return true;
283
Misha Brukman3da94ae2005-04-22 00:00:37 +0000284 for (std::set<sys::Path>::iterator I = content.begin(), E = content.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000285 I != E; ++I) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000286 // Make sure it exists and is a directory
Reid Spencer184e67e2007-04-08 19:59:07 +0000287 sys::PathWithStatus PwS(*I);
288 const sys::FileStatus *Status = PwS.getFileStatus(false, ErrMsg);
Reid Spencer8475ec02007-03-29 19:05:44 +0000289 if (!Status)
290 return true;
291 if (Status->isDir) {
292 std::set<sys::Path> moreResults;
293 if (recurseDirectories(*I, moreResults, ErrMsg))
294 return true;
295 result.insert(moreResults.begin(), moreResults.end());
296 } else {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000297 result.insert(*I);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000298 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000299 }
300 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000301 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000302}
303
304// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000305// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000306// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000307bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000308 for (unsigned i = 0; i < Members.size(); i++) {
309 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000310 if (!aPath.set(Members[i]))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000311 throw std::string("File member name invalid: ") + Members[i];
312 if (checkExistence) {
313 if (!aPath.exists())
314 throw std::string("File does not exist: ") + Members[i];
Chris Lattner252ad032006-07-28 22:03:44 +0000315 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000316 sys::PathWithStatus PwS(aPath);
317 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000318 if (!si)
Chris Lattner252ad032006-07-28 22:03:44 +0000319 throw Err;
Reid Spencer8475ec02007-03-29 19:05:44 +0000320 if (si->isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000321 std::set<sys::Path> dirpaths;
322 if (recurseDirectories(aPath, dirpaths, ErrMsg))
323 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000324 Paths.insert(dirpaths.begin(),dirpaths.end());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000325 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000326 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000327 }
328 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000329 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000330 }
331 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000332 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000333}
334
Reid Spencerbede5832004-11-16 06:41:09 +0000335// printSymbolTable - print out the archive's symbol table.
336void printSymbolTable() {
337 std::cout << "\nArchive Symbol Table:\n";
338 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000339 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000340 I != E; ++I ) {
341 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
342 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
343 }
344}
345
Reid Spencer3a1582b2004-11-14 22:20:07 +0000346// doPrint - Implements the 'p' operation. This function traverses the archive
347// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000348// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000349// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000350bool doPrint(std::string* ErrMsg) {
351 if (buildPaths(false, ErrMsg))
352 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000353 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000354 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000355 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000356 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000357 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
358 if (countDown == 1) {
359 const char* data = reinterpret_cast<const char*>(I->getData());
360
361 // Skip things that don't make sense to print
Misha Brukman3da94ae2005-04-22 00:00:37 +0000362 if (I->isLLVMSymbolTable() || I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000363 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000364 continue;
365
366 if (Verbose)
Chris Lattner74382b72009-08-23 22:45:37 +0000367 std::cout << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000368
Chris Lattner44dadff2007-05-06 09:29:57 +0000369 unsigned len = I->getSize();
370 std::cout.write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000371 } else {
372 countDown--;
373 }
374 }
375 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000376 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000377}
378
379// putMode - utility function for printing out the file mode when the 't'
380// operation is in verbose mode.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000381void
382printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000383 if (mode & 004)
Reid Spencer3a1582b2004-11-14 22:20:07 +0000384 std::cout << "r";
385 else
386 std::cout << "-";
387 if (mode & 002)
388 std::cout << "w";
389 else
390 std::cout << "-";
391 if (mode & 001)
392 std::cout << "x";
393 else
394 std::cout << "-";
395}
396
397// doDisplayTable - Implement the 't' operation. This function prints out just
398// the file names of each of the members. However, if verbose mode is requested
399// ('v' modifier) then the file type, permission mode, user, group, size, and
400// modification time are also printed.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000401bool
402doDisplayTable(std::string* ErrMsg) {
403 if (buildPaths(false, ErrMsg))
404 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000405 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000406 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000407 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000408 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
409 if (Verbose) {
410 // FIXME: Output should be this format:
411 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000412 if (I->isBitcode())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000413 std::cout << "b";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000414 else if (I->isCompressed())
415 std::cout << "Z";
416 else
417 std::cout << " ";
418 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000419 printMode((mode >> 6) & 007);
420 printMode((mode >> 3) & 007);
421 printMode(mode & 007);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000422 std::cout << " " << std::setw(4) << I->getUser();
423 std::cout << "/" << std::setw(4) << I->getGroup();
424 std::cout << " " << std::setw(8) << I->getSize();
Chris Lattner74382b72009-08-23 22:45:37 +0000425 std::cout << " " << std::setw(20) << I->getModTime().str().substr(4);
426 std::cout << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000427 } else {
Chris Lattner74382b72009-08-23 22:45:37 +0000428 std::cout << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000429 }
430 }
431 }
Reid Spencerbede5832004-11-16 06:41:09 +0000432 if (ReallyVerbose)
433 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000434 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000435}
436
437// doExtract - Implement the 'x' operation. This function extracts files back to
Reid Spencere5c9cb52006-08-23 00:39:35 +0000438// the file system, making sure to uncompress any that were compressed
Reid Spencer142ca8e2006-08-23 06:56:27 +0000439bool
440doExtract(std::string* ErrMsg) {
441 if (buildPaths(false, ErrMsg))
442 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000443 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000444 I != E; ++I ) {
445 if (Paths.empty() ||
446 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
447
448 // Make sure the intervening directories are created
449 if (I->hasPath()) {
450 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000451 dirs.eraseComponent();
Reid Spencere5c9cb52006-08-23 00:39:35 +0000452 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
453 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000454 }
455
456 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000457 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
458 std::ios::binary;
459 std::ofstream file(I->getPath().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000460
461 // Get the data and its length
462 const char* data = reinterpret_cast<const char*>(I->getData());
463 unsigned len = I->getSize();
464
Chris Lattner44dadff2007-05-06 09:29:57 +0000465 // Write the data.
466 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000467 file.close();
468
469 // If we're supposed to retain the original modification times, etc. do so
470 // now.
471 if (OriginalDates)
Chris Lattner252ad032006-07-28 22:03:44 +0000472 I->getPath().setStatusInfoOnDisk(I->getFileStatus());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000473 }
474 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000475 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000476}
477
478// doDelete - Implement the delete operation. This function deletes zero or more
479// members from the archive. Note that if the count is specified, there should
480// be no more than one path in the Paths list or else this algorithm breaks.
481// That check is enforced in parseCommandLine (above).
Reid Spencer142ca8e2006-08-23 06:56:27 +0000482bool
483doDelete(std::string* ErrMsg) {
484 if (buildPaths(false, ErrMsg))
485 return true;
486 if (Paths.empty())
487 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000488 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000489 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000490 I != E; ) {
491 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
492 if (countDown == 1) {
493 Archive::iterator J = I;
494 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000495 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000496 } else
497 countDown--;
498 } else {
499 ++I;
500 }
501 }
502
503 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000504 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000505 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000506 if (ReallyVerbose)
507 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000508 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000509}
510
511// doMore - Implement the move operation. This function re-arranges just the
512// order of the archive members so that when the archive is written the move
513// of the members is accomplished. Note the use of the RelPos variable to
514// determine where the items should be moved to.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000515bool
516doMove(std::string* ErrMsg) {
517 if (buildPaths(false, ErrMsg))
518 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000519
520 // By default and convention the place to move members to is the end of the
521 // archive.
522 Archive::iterator moveto_spot = TheArchive->end();
523
524 // However, if the relative positioning modifiers were used, we need to scan
525 // the archive to find the member in question. If we don't find it, its no
526 // crime, we just move to the end.
527 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000528 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000529 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000530 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000531 if (AddAfter) {
532 moveto_spot = I;
533 moveto_spot++;
534 } else {
535 moveto_spot = I;
536 }
537 break;
538 }
539 }
540 }
541
542 // Keep a list of the paths remaining to be moved
Reid Spencerbede5832004-11-16 06:41:09 +0000543 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000544
545 // Scan the archive again, this time looking for the members to move to the
546 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000547 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000548 I != E && !remaining.empty(); ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000549 std::set<sys::Path>::iterator found =
Reid Spencer3a1582b2004-11-14 22:20:07 +0000550 std::find(remaining.begin(),remaining.end(),I->getPath());
551 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000552 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000553 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000554 remaining.erase(found);
555 }
556 }
557
558 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000559 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000560 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000561 if (ReallyVerbose)
562 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000563 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000564}
565
566// doQuickAppend - Implements the 'q' operation. This function just
567// indiscriminantly adds the members to the archive and rebuilds it.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000568bool
569doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000570 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000571 if (buildPaths(true, ErrMsg))
572 return true;
573 if (Paths.empty())
574 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000575
576 // Append them quickly.
Reid Spencerbede5832004-11-16 06:41:09 +0000577 for (std::set<sys::Path>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000578 PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000579 if (TheArchive->addFileBefore(*PI,TheArchive->end(),ErrMsg))
580 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000581 }
582
583 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000584 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000585 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000586 if (ReallyVerbose)
587 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000588 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000589}
590
591// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000592// any existing files or insert new ones into the archive.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000593bool
594doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000595
596 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000597 if (buildPaths(true, ErrMsg))
598 return true;
599 if (Paths.empty())
600 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000601
602 // Keep track of the paths that remain to be inserted.
Reid Spencerbede5832004-11-16 06:41:09 +0000603 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000604
605 // Default the insertion spot to the end of the archive
606 Archive::iterator insert_spot = TheArchive->end();
607
608 // Iterate over the archive contents
609 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
610 I != E && !remaining.empty(); ++I ) {
611
612 // Determine if this archive member matches one of the paths we're trying
613 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000614
615 std::set<sys::Path>::iterator found = remaining.end();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000616 for (std::set<sys::Path>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000617 RE = remaining.end(); RI != RE; ++RI ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000618 std::string compare(RI->str());
Reid Spencer0de66b52004-12-02 09:21:55 +0000619 if (TruncateNames && compare.length() > 15) {
620 const char* nm = compare.c_str();
621 unsigned len = compare.length();
622 size_t slashpos = compare.rfind('/');
623 if (slashpos != std::string::npos) {
624 nm += slashpos + 1;
625 len -= slashpos +1;
626 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000627 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000628 len = 15;
629 compare.assign(nm,len);
630 }
Chris Lattner74382b72009-08-23 22:45:37 +0000631 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000632 found = RI;
633 break;
634 }
635 }
636
Reid Spencer3a1582b2004-11-14 22:20:07 +0000637 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000638 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000639 sys::PathWithStatus PwS(*found);
640 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000641 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000642 return true;
Chris Lattner7d8f9b62009-02-05 17:58:39 +0000643 if (!si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000644 if (OnlyUpdate) {
645 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000646 if (si->modTime > I->getModTime())
Reid Spencer0ff2d312006-08-24 23:45:08 +0000647 if (I->replaceWith(*found, ErrMsg))
648 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000649 } else {
650 // Replace the item regardless of time stamp
Reid Spencer0ff2d312006-08-24 23:45:08 +0000651 if (I->replaceWith(*found, ErrMsg))
652 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000653 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000654 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000655 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000656 }
657
658 // Remove it from our "to do" list
659 remaining.erase(found);
660 }
661
662 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000663 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000664 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000665 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000666 insert_spot = I;
667 insert_spot++;
668 }
669 }
670
671 // If we didn't replace all the members, some will remain and need to be
672 // inserted at the previously computed insert-spot.
673 if (!remaining.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000674 for (std::set<sys::Path>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000675 PE = remaining.end(); PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000676 if (TheArchive->addFileBefore(*PI,insert_spot, ErrMsg))
677 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000678 }
679 }
680
681 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000682 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000683 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000684 if (ReallyVerbose)
685 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000686 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000687}
688
689// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000690int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000691 // Print a stack trace if we signal out.
692 sys::PrintStackTraceOnErrorSignal();
693 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000694 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000695 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000696
Reid Spencer3a1582b2004-11-14 22:20:07 +0000697 // Have the command line options parsed and handle things
698 // like --help and --version.
699 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000700 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000701 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000702 );
703
Reid Spencer3a1582b2004-11-14 22:20:07 +0000704 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000705
Reid Spencer3a1582b2004-11-14 22:20:07 +0000706 // Make sure we don't exit with "unhandled exception".
707 try {
708 // Do our own parsing of the command line because the CommandLine utility
709 // can't handle the grouped positional parameters without a dash.
710 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000711
Reid Spencer3a1582b2004-11-14 22:20:07 +0000712 // Check the path name of the archive
713 sys::Path ArchivePath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000714 if (!ArchivePath.set(ArchiveName))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000715 throw std::string("Archive name invalid: ") + ArchiveName;
716
717 // Create or open the archive object.
718 if (!ArchivePath.exists()) {
719 // Produce a warning if we should and we're creating the archive
720 if (!Create)
Chris Lattner74382b72009-08-23 22:45:37 +0000721 errs() << argv[0] << ": creating " << ArchivePath.str() << "\n";
Owen Anderson31895e72009-07-01 21:22:36 +0000722 TheArchive = Archive::CreateEmpty(ArchivePath, Context);
Andrew Lenharth63b8c1f2008-02-28 22:24:48 +0000723 TheArchive->writeToDisk();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000724 } else {
Chris Lattner67c38212004-12-15 07:44:15 +0000725 std::string Error;
Owen Anderson31895e72009-07-01 21:22:36 +0000726 TheArchive = Archive::OpenAndLoad(ArchivePath, Context, &Error);
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000727 if (TheArchive == 0) {
Chris Lattner74382b72009-08-23 22:45:37 +0000728 errs() << argv[0] << ": error loading '" << ArchivePath.str() << "': "
Dan Gohman65f57c22009-07-15 16:35:29 +0000729 << Error << "!\n";
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000730 return 1;
731 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000732 }
733
734 // Make sure we're not fooling ourselves.
735 assert(TheArchive && "Unable to instantiate the archive");
736
Reid Spencerbede5832004-11-16 06:41:09 +0000737 // Make sure we clean up the archive even on failure.
738 std::auto_ptr<Archive> AutoArchive(TheArchive);
739
Reid Spencer3a1582b2004-11-14 22:20:07 +0000740 // Perform the operation
Reid Spencere5c9cb52006-08-23 00:39:35 +0000741 std::string ErrMsg;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000742 bool haveError = false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000743 switch (Operation) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000744 case Print: haveError = doPrint(&ErrMsg); break;
745 case Delete: haveError = doDelete(&ErrMsg); break;
746 case Move: haveError = doMove(&ErrMsg); break;
747 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
748 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
749 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
750 case Extract: haveError = doExtract(&ErrMsg); break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000751 case NoOperation:
Dan Gohman65f57c22009-07-15 16:35:29 +0000752 errs() << argv[0] << ": No operation was selected.\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000753 break;
754 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000755 if (haveError) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000756 errs() << argv[0] << ": " << ErrMsg << "\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000757 return 1;
758 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000759 } catch (const char*msg) {
760 // These errors are usage errors, thrown only by the various checks in the
761 // code above.
Dan Gohman65f57c22009-07-15 16:35:29 +0000762 errs() << argv[0] << ": " << msg << "\n\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000763 cl::PrintHelpMessage();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000764 exitCode = 1;
765 } catch (const std::string& msg) {
766 // These errors are thrown by LLVM libraries (e.g. lib System) and represent
767 // a more serious error so we bump the exitCode and don't print the usage.
Dan Gohman65f57c22009-07-15 16:35:29 +0000768 errs() << argv[0] << ": " << msg << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000769 exitCode = 2;
770 } catch (...) {
771 // This really shouldn't happen, but just in case ....
Dan Gohman65f57c22009-07-15 16:35:29 +0000772 errs() << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000773 exitCode = 3;
774 }
775
776 // Return result code back to operating system.
777 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000778}