blob: 5d81fc7cf3886d8bfdfb485f4e0e6c405994d812 [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 Lattnercc14d252009-03-06 05:34:10 +000019#include "llvm/Support/PrettyStackTrace.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000020#include "llvm/System/Signals.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000021#include <iostream>
Reid Spencer3a1582b2004-11-14 22:20:07 +000022#include <algorithm>
23#include <iomanip>
Duraid Madina6927b062005-12-28 06:56:09 +000024#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000025using namespace llvm;
26
Dan Gohman82f209e2007-10-15 21:10:03 +000027// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000028static cl::opt<bool>
29X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000030 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000031
Reid Spencerbede5832004-11-16 06:41:09 +000032// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000033static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000034Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000035
Reid Spencerbede5832004-11-16 06:41:09 +000036// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000037static cl::list<std::string>
38RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000039 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000040
Reid Spencerbede5832004-11-16 06:41:09 +000041// MoreHelp - Provide additional help output explaining the operations and
42// modifiers of llvm-ar. This object instructs the CommandLine library
43// to print the text of the constructor when the --help option is given.
44static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000045 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000046 " d[NsS] - delete file(s) from the archive\n"
47 " m[abiSs] - move file(s) in the archive\n"
48 " p[kN] - print file(s) found in the archive\n"
49 " q[ufsS] - quick append file(s) to the archive\n"
50 " r[abfiuzRsS] - replace or insert file(s) into the archive\n"
51 " t - display contents of archive\n"
52 " x[No] - extract file(s) from the archive\n"
53 "\nMODIFIERS (operation specific):\n"
54 " [a] - put file(s) after [relpos]\n"
55 " [b] - put file(s) before [relpos] (same as [i])\n"
56 " [f] - truncate inserted file names\n"
57 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000058 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000059 " [N] - use instance [count] of name\n"
60 " [o] - preserve original dates\n"
61 " [P] - use full path names when matching\n"
62 " [R] - recurse through directories when inserting\n"
63 " [s] - create an archive index (cf. ranlib)\n"
64 " [S] - do not build a symbol table\n"
65 " [u] - update only files newer than archive contents\n"
66 " [z] - compress files before inserting/extracting\n"
67 "\nMODIFIERS (generic):\n"
68 " [c] - do not warn if the library had to be created\n"
69 " [v] - be verbose about actions taken\n"
70 " [V] - be *really* verbose about actions taken\n"
71);
72
Reid Spencer3a1582b2004-11-14 22:20:07 +000073// This enumeration delineates the kinds of operations on an archive
74// that are permitted.
75enum ArchiveOperation {
76 NoOperation, ///< An operation hasn't been specified
77 Print, ///< Print the contents of the archive
78 Delete, ///< Delete the specified members
79 Move, ///< Move members to end or as given by {a,b,i} modifiers
80 QuickAppend, ///< Quickly append to end of archive
81 ReplaceOrInsert, ///< Replace or Insert members
82 DisplayTable, ///< Display the table of contents
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000083 Extract ///< Extract files back to file system
Tanya Lattner57bd7962003-12-06 23:01:25 +000084};
Tanya Lattner14baebf2003-08-28 15:22:38 +000085
Reid Spencer3a1582b2004-11-14 22:20:07 +000086// Modifiers to follow operation to vary behavior
87bool AddAfter = false; ///< 'a' modifier
88bool AddBefore = false; ///< 'b' modifier
Misha Brukman3da94ae2005-04-22 00:00:37 +000089bool Create = false; ///< 'c' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000090bool TruncateNames = false; ///< 'f' modifier
91bool InsertBefore = false; ///< 'i' modifier
Gabor Greifa99be512007-07-05 17:07:56 +000092bool DontSkipBitcode = false; ///< 'k' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000093bool UseCount = false; ///< 'N' modifier
94bool OriginalDates = false; ///< 'o' modifier
95bool FullPath = false; ///< 'P' modifier
96bool RecurseDirectories = false; ///< 'R' modifier
97bool SymTable = true; ///< 's' & 'S' modifiers
98bool OnlyUpdate = false; ///< 'u' modifier
99bool Verbose = false; ///< 'v' modifier
100bool ReallyVerbose = false; ///< 'V' modifier
101bool Compression = false; ///< 'z' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +0000102
Reid Spencer3a1582b2004-11-14 22:20:07 +0000103// Relative Positional Argument (for insert/move). This variable holds
104// the name of the archive member to which the 'a', 'b' or 'i' modifier
105// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
106// one variable.
107std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000108
Reid Spencer3a1582b2004-11-14 22:20:07 +0000109// Select which of multiple entries in the archive with the same name should be
110// used (specified with -N) for the delete and extract operations.
111int Count = 1;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000112
Reid Spencer3a1582b2004-11-14 22:20:07 +0000113// This variable holds the name of the archive file as given on the
114// command line.
115std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000116
Reid Spencer3a1582b2004-11-14 22:20:07 +0000117// This variable holds the list of member files to proecess, as given
118// on the command line.
119std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000120
Reid Spencer3a1582b2004-11-14 22:20:07 +0000121// This variable holds the (possibly expanded) list of path objects that
122// correspond to files we will
Reid Spencerbede5832004-11-16 06:41:09 +0000123std::set<sys::Path> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000124
Reid Spencer3a1582b2004-11-14 22:20:07 +0000125// The Archive object to which all the editing operations will be sent.
126Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000127
Reid Spencer3a1582b2004-11-14 22:20:07 +0000128// getRelPos - Extract the member filename from the command line for
129// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000130void getRelPos() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000131 if(RestOfArgs.size() > 0) {
132 RelPos = RestOfArgs[0];
133 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000134 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000135 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000136 throw "Expected [relpos] for a, b, or i modifier";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000137}
138
Misha Brukman3da94ae2005-04-22 00:00:37 +0000139// getCount - Extract the [count] argument associated with the N modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000140// from the command line and check its value.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000141void getCount() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000142 if(RestOfArgs.size() > 0) {
143 Count = atoi(RestOfArgs[0].c_str());
144 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000145 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000146 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000147 throw "Expected [count] value with N modifier";
148
149 // Non-positive counts are not allowed
150 if (Count < 1)
151 throw "Invalid [count] value (not a positive integer)";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000152}
153
Reid Spencer3a1582b2004-11-14 22:20:07 +0000154// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000155void getArchive() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000156 if(RestOfArgs.size() > 0) {
157 ArchiveName = RestOfArgs[0];
158 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000159 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000160 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000161 throw "An archive name must be specified.";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000162}
163
Reid Spencer3a1582b2004-11-14 22:20:07 +0000164// getMembers - Copy over remaining items in RestOfArgs to our Members vector
165// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000166void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000167 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000168 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000169}
170
Reid Spencer3a1582b2004-11-14 22:20:07 +0000171// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000172// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000173// constraints on modifier/operation pairs have not been violated.
174ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000175
Reid Spencer3a1582b2004-11-14 22:20:07 +0000176 // Keep track of number of operations. We can only specify one
177 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000178 unsigned NumOperations = 0;
179
Reid Spencer3a1582b2004-11-14 22:20:07 +0000180 // Keep track of the number of positional modifiers (a,b,i). Only
181 // one can be specified.
182 unsigned NumPositional = 0;
183
184 // Keep track of which operation was requested
185 ArchiveOperation Operation = NoOperation;
186
Tanya Lattner57bd7962003-12-06 23:01:25 +0000187 for(unsigned i=0; i<Options.size(); ++i) {
188 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000189 case 'd': ++NumOperations; Operation = Delete; break;
190 case 'm': ++NumOperations; Operation = Move ; break;
191 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000192 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000193 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000194 case 't': ++NumOperations; Operation = DisplayTable; break;
195 case 'x': ++NumOperations; Operation = Extract; break;
196 case 'c': Create = true; break;
197 case 'f': TruncateNames = true; break;
Gabor Greifa99be512007-07-05 17:07:56 +0000198 case 'k': DontSkipBitcode = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000199 case 'l': /* accepted but unused */ break;
200 case 'o': OriginalDates = true; break;
201 case 'P': FullPath = true; break;
202 case 'R': RecurseDirectories = true; break;
203 case 's': SymTable = true; break;
204 case 'S': SymTable = false; break;
205 case 'u': OnlyUpdate = true; break;
206 case 'v': Verbose = true; break;
207 case 'V': Verbose = ReallyVerbose = true; break;
208 case 'z': Compression = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000209 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000210 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000211 AddAfter = true;
212 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000213 break;
214 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000215 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000216 AddBefore = true;
217 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000218 break;
219 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000220 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000221 InsertBefore = true;
222 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000223 break;
224 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000225 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000226 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000227 break;
228 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000229 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000230 }
231 }
232
Misha Brukman3da94ae2005-04-22 00:00:37 +0000233 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000234 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000235 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000236
237 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000238 getMembers();
239
Reid Spencer3a1582b2004-11-14 22:20:07 +0000240 // Perform various checks on the operation/modifier specification
241 // to make sure we are dealing with a legal request.
242 if (NumOperations == 0)
243 throw "You must specify at least one of the operations";
244 if (NumOperations > 1)
245 throw "Only one operation may be specified";
246 if (NumPositional > 1)
247 throw "You may only specify one of a, b, and i modifiers";
248 if (AddAfter || AddBefore || InsertBefore)
249 if (Operation != Move && Operation != ReplaceOrInsert)
250 throw "The 'a', 'b' and 'i' modifiers can only be specified with "
251 "the 'm' or 'r' operations";
252 if (RecurseDirectories && Operation != ReplaceOrInsert)
253 throw "The 'R' modifiers is only applicabe to the 'r' operation";
254 if (OriginalDates && Operation != Extract)
255 throw "The 'o' modifier is only applicable to the 'x' operation";
256 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
257 throw "The 'f' modifier is only applicable to the 'q' and 'r' operations";
258 if (OnlyUpdate && Operation != ReplaceOrInsert)
259 throw "The 'u' modifier is only applicable to the 'r' operation";
260 if (Compression && Operation!=ReplaceOrInsert && Operation!=Extract)
261 throw "The 'z' modifier is only applicable to the 'r' and 'x' operations";
262 if (Count > 1 && Members.size() > 1)
263 throw "Only one member name may be specified with the 'N' modifier";
264
265 // Return the parsed operation to the caller
266 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000267}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000268
Reid Spencer3a1582b2004-11-14 22:20:07 +0000269// recurseDirectories - Implements the "R" modifier. This function scans through
270// the Paths vector (built by buildPaths, below) and replaces any directories it
271// finds with all the files in that directory (recursively). It uses the
272// sys::Path::getDirectoryContent method to perform the actual directory scans.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000273bool
274recurseDirectories(const sys::Path& path,
275 std::set<sys::Path>& result, std::string* ErrMsg) {
276 result.clear();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000277 if (RecurseDirectories) {
Reid Spencerbede5832004-11-16 06:41:09 +0000278 std::set<sys::Path> content;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000279 if (path.getDirectoryContents(content, ErrMsg))
280 return true;
281
Misha Brukman3da94ae2005-04-22 00:00:37 +0000282 for (std::set<sys::Path>::iterator I = content.begin(), E = content.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000283 I != E; ++I) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000284 // Make sure it exists and is a directory
Reid Spencer184e67e2007-04-08 19:59:07 +0000285 sys::PathWithStatus PwS(*I);
286 const sys::FileStatus *Status = PwS.getFileStatus(false, ErrMsg);
Reid Spencer8475ec02007-03-29 19:05:44 +0000287 if (!Status)
288 return true;
289 if (Status->isDir) {
290 std::set<sys::Path> moreResults;
291 if (recurseDirectories(*I, moreResults, ErrMsg))
292 return true;
293 result.insert(moreResults.begin(), moreResults.end());
294 } else {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000295 result.insert(*I);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000296 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000297 }
298 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000299 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000300}
301
302// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000303// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000304// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000305bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000306 for (unsigned i = 0; i < Members.size(); i++) {
307 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000308 if (!aPath.set(Members[i]))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000309 throw std::string("File member name invalid: ") + Members[i];
310 if (checkExistence) {
311 if (!aPath.exists())
312 throw std::string("File does not exist: ") + Members[i];
Chris Lattner252ad032006-07-28 22:03:44 +0000313 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000314 sys::PathWithStatus PwS(aPath);
315 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000316 if (!si)
Chris Lattner252ad032006-07-28 22:03:44 +0000317 throw Err;
Reid Spencer8475ec02007-03-29 19:05:44 +0000318 if (si->isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000319 std::set<sys::Path> dirpaths;
320 if (recurseDirectories(aPath, dirpaths, ErrMsg))
321 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000322 Paths.insert(dirpaths.begin(),dirpaths.end());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000323 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000324 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000325 }
326 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000327 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000328 }
329 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000330 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000331}
332
Reid Spencerbede5832004-11-16 06:41:09 +0000333// printSymbolTable - print out the archive's symbol table.
334void printSymbolTable() {
335 std::cout << "\nArchive Symbol Table:\n";
336 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000337 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000338 I != E; ++I ) {
339 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
340 std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n";
341 }
342}
343
Reid Spencer3a1582b2004-11-14 22:20:07 +0000344// doPrint - Implements the 'p' operation. This function traverses the archive
345// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000346// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000347// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000348bool doPrint(std::string* ErrMsg) {
349 if (buildPaths(false, ErrMsg))
350 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000351 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000352 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000353 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000354 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000355 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
356 if (countDown == 1) {
357 const char* data = reinterpret_cast<const char*>(I->getData());
358
359 // Skip things that don't make sense to print
Misha Brukman3da94ae2005-04-22 00:00:37 +0000360 if (I->isLLVMSymbolTable() || I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000361 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000362 continue;
363
364 if (Verbose)
Reid Spencer1fce0912004-12-11 00:14:15 +0000365 std::cout << "Printing " << I->getPath().toString() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000366
Chris Lattner44dadff2007-05-06 09:29:57 +0000367 unsigned len = I->getSize();
368 std::cout.write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000369 } else {
370 countDown--;
371 }
372 }
373 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000374 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000375}
376
377// putMode - utility function for printing out the file mode when the 't'
378// operation is in verbose mode.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000379void
380printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000381 if (mode & 004)
Reid Spencer3a1582b2004-11-14 22:20:07 +0000382 std::cout << "r";
383 else
384 std::cout << "-";
385 if (mode & 002)
386 std::cout << "w";
387 else
388 std::cout << "-";
389 if (mode & 001)
390 std::cout << "x";
391 else
392 std::cout << "-";
393}
394
395// doDisplayTable - Implement the 't' operation. This function prints out just
396// the file names of each of the members. However, if verbose mode is requested
397// ('v' modifier) then the file type, permission mode, user, group, size, and
398// modification time are also printed.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000399bool
400doDisplayTable(std::string* ErrMsg) {
401 if (buildPaths(false, ErrMsg))
402 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000403 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000404 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000405 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000406 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
407 if (Verbose) {
408 // FIXME: Output should be this format:
409 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000410 if (I->isBitcode())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000411 std::cout << "b";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000412 else if (I->isCompressed())
413 std::cout << "Z";
414 else
415 std::cout << " ";
416 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000417 printMode((mode >> 6) & 007);
418 printMode((mode >> 3) & 007);
419 printMode(mode & 007);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000420 std::cout << " " << std::setw(4) << I->getUser();
421 std::cout << "/" << std::setw(4) << I->getGroup();
422 std::cout << " " << std::setw(8) << I->getSize();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000423 std::cout << " " << std::setw(20) <<
Reid Spencerb55bc062004-11-14 23:17:41 +0000424 I->getModTime().toString().substr(4);
Reid Spencer1fce0912004-12-11 00:14:15 +0000425 std::cout << " " << I->getPath().toString() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000426 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000427 std::cout << I->getPath().toString() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000428 }
429 }
430 }
Reid Spencerbede5832004-11-16 06:41:09 +0000431 if (ReallyVerbose)
432 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000433 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000434}
435
436// doExtract - Implement the 'x' operation. This function extracts files back to
Reid Spencere5c9cb52006-08-23 00:39:35 +0000437// the file system, making sure to uncompress any that were compressed
Reid Spencer142ca8e2006-08-23 06:56:27 +0000438bool
439doExtract(std::string* ErrMsg) {
440 if (buildPaths(false, ErrMsg))
441 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000442 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000443 I != E; ++I ) {
444 if (Paths.empty() ||
445 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
446
447 // Make sure the intervening directories are created
448 if (I->hasPath()) {
449 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000450 dirs.eraseComponent();
Reid Spencere5c9cb52006-08-23 00:39:35 +0000451 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
452 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000453 }
454
455 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000456 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
457 std::ios::binary;
458 std::ofstream file(I->getPath().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000459
460 // Get the data and its length
461 const char* data = reinterpret_cast<const char*>(I->getData());
462 unsigned len = I->getSize();
463
Chris Lattner44dadff2007-05-06 09:29:57 +0000464 // Write the data.
465 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000466 file.close();
467
468 // If we're supposed to retain the original modification times, etc. do so
469 // now.
470 if (OriginalDates)
Chris Lattner252ad032006-07-28 22:03:44 +0000471 I->getPath().setStatusInfoOnDisk(I->getFileStatus());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000472 }
473 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000474 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000475}
476
477// doDelete - Implement the delete operation. This function deletes zero or more
478// members from the archive. Note that if the count is specified, there should
479// be no more than one path in the Paths list or else this algorithm breaks.
480// That check is enforced in parseCommandLine (above).
Reid Spencer142ca8e2006-08-23 06:56:27 +0000481bool
482doDelete(std::string* ErrMsg) {
483 if (buildPaths(false, ErrMsg))
484 return true;
485 if (Paths.empty())
486 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000487 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000488 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000489 I != E; ) {
490 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
491 if (countDown == 1) {
492 Archive::iterator J = I;
493 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000494 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000495 } else
496 countDown--;
497 } else {
498 ++I;
499 }
500 }
501
502 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000503 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000504 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000505 if (ReallyVerbose)
506 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000507 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000508}
509
510// doMore - Implement the move operation. This function re-arranges just the
511// order of the archive members so that when the archive is written the move
512// of the members is accomplished. Note the use of the RelPos variable to
513// determine where the items should be moved to.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000514bool
515doMove(std::string* ErrMsg) {
516 if (buildPaths(false, ErrMsg))
517 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000518
519 // By default and convention the place to move members to is the end of the
520 // archive.
521 Archive::iterator moveto_spot = TheArchive->end();
522
523 // However, if the relative positioning modifiers were used, we need to scan
524 // the archive to find the member in question. If we don't find it, its no
525 // crime, we just move to the end.
526 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000527 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000528 I != E; ++I ) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000529 if (RelPos == I->getPath().toString()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000530 if (AddAfter) {
531 moveto_spot = I;
532 moveto_spot++;
533 } else {
534 moveto_spot = I;
535 }
536 break;
537 }
538 }
539 }
540
541 // Keep a list of the paths remaining to be moved
Reid Spencerbede5832004-11-16 06:41:09 +0000542 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000543
544 // Scan the archive again, this time looking for the members to move to the
545 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000546 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000547 I != E && !remaining.empty(); ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000548 std::set<sys::Path>::iterator found =
Reid Spencer3a1582b2004-11-14 22:20:07 +0000549 std::find(remaining.begin(),remaining.end(),I->getPath());
550 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000551 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000552 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000553 remaining.erase(found);
554 }
555 }
556
557 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000558 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000559 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000560 if (ReallyVerbose)
561 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000562 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000563}
564
565// doQuickAppend - Implements the 'q' operation. This function just
566// indiscriminantly adds the members to the archive and rebuilds it.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000567bool
568doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000569 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000570 if (buildPaths(true, ErrMsg))
571 return true;
572 if (Paths.empty())
573 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000574
575 // Append them quickly.
Reid Spencerbede5832004-11-16 06:41:09 +0000576 for (std::set<sys::Path>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000577 PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000578 if (TheArchive->addFileBefore(*PI,TheArchive->end(),ErrMsg))
579 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000580 }
581
582 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000583 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000584 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000585 if (ReallyVerbose)
586 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000587 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000588}
589
590// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000591// any existing files or insert new ones into the archive.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000592bool
593doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000594
595 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000596 if (buildPaths(true, ErrMsg))
597 return true;
598 if (Paths.empty())
599 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000600
601 // Keep track of the paths that remain to be inserted.
Reid Spencerbede5832004-11-16 06:41:09 +0000602 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000603
604 // Default the insertion spot to the end of the archive
605 Archive::iterator insert_spot = TheArchive->end();
606
607 // Iterate over the archive contents
608 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
609 I != E && !remaining.empty(); ++I ) {
610
611 // Determine if this archive member matches one of the paths we're trying
612 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000613
614 std::set<sys::Path>::iterator found = remaining.end();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000615 for (std::set<sys::Path>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000616 RE = remaining.end(); RI != RE; ++RI ) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000617 std::string compare(RI->toString());
Reid Spencer0de66b52004-12-02 09:21:55 +0000618 if (TruncateNames && compare.length() > 15) {
619 const char* nm = compare.c_str();
620 unsigned len = compare.length();
621 size_t slashpos = compare.rfind('/');
622 if (slashpos != std::string::npos) {
623 nm += slashpos + 1;
624 len -= slashpos +1;
625 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000626 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000627 len = 15;
628 compare.assign(nm,len);
629 }
Reid Spencer1fce0912004-12-11 00:14:15 +0000630 if (compare == I->getPath().toString()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000631 found = RI;
632 break;
633 }
634 }
635
Reid Spencer3a1582b2004-11-14 22:20:07 +0000636 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000637 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000638 sys::PathWithStatus PwS(*found);
639 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000640 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000641 return true;
Chris Lattner7d8f9b62009-02-05 17:58:39 +0000642 if (!si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000643 if (OnlyUpdate) {
644 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000645 if (si->modTime > I->getModTime())
Reid Spencer0ff2d312006-08-24 23:45:08 +0000646 if (I->replaceWith(*found, ErrMsg))
647 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000648 } else {
649 // Replace the item regardless of time stamp
Reid Spencer0ff2d312006-08-24 23:45:08 +0000650 if (I->replaceWith(*found, ErrMsg))
651 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000652 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000653 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000654 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000655 }
656
657 // Remove it from our "to do" list
658 remaining.erase(found);
659 }
660
661 // Determine if this is the place where we should insert
Reid Spencer1fce0912004-12-11 00:14:15 +0000662 if ((AddBefore || InsertBefore) && (RelPos == I->getPath().toString()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000663 insert_spot = I;
Reid Spencer1fce0912004-12-11 00:14:15 +0000664 else if (AddAfter && (RelPos == I->getPath().toString())) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000665 insert_spot = I;
666 insert_spot++;
667 }
668 }
669
670 // If we didn't replace all the members, some will remain and need to be
671 // inserted at the previously computed insert-spot.
672 if (!remaining.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000673 for (std::set<sys::Path>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000674 PE = remaining.end(); PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000675 if (TheArchive->addFileBefore(*PI,insert_spot, ErrMsg))
676 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000677 }
678 }
679
680 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000681 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000682 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000683 if (ReallyVerbose)
684 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000685 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000686}
687
688// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000689int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000690 // Print a stack trace if we signal out.
691 sys::PrintStackTraceOnErrorSignal();
692 PrettyStackTraceProgram X(argc, argv);
693 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000694
Reid Spencer3a1582b2004-11-14 22:20:07 +0000695 // Have the command line options parsed and handle things
696 // like --help and --version.
697 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000698 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000699 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000700 );
701
Reid Spencer3a1582b2004-11-14 22:20:07 +0000702 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000703
Reid Spencer3a1582b2004-11-14 22:20:07 +0000704 // Make sure we don't exit with "unhandled exception".
705 try {
706 // Do our own parsing of the command line because the CommandLine utility
707 // can't handle the grouped positional parameters without a dash.
708 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000709
Reid Spencer3a1582b2004-11-14 22:20:07 +0000710 // Check the path name of the archive
711 sys::Path ArchivePath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000712 if (!ArchivePath.set(ArchiveName))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000713 throw std::string("Archive name invalid: ") + ArchiveName;
714
715 // Create or open the archive object.
716 if (!ArchivePath.exists()) {
717 // Produce a warning if we should and we're creating the archive
718 if (!Create)
Reid Spencer1fce0912004-12-11 00:14:15 +0000719 std::cerr << argv[0] << ": creating " << ArchivePath.toString() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000720 TheArchive = Archive::CreateEmpty(ArchivePath);
Andrew Lenharth63b8c1f2008-02-28 22:24:48 +0000721 TheArchive->writeToDisk();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000722 } else {
Chris Lattner67c38212004-12-15 07:44:15 +0000723 std::string Error;
724 TheArchive = Archive::OpenAndLoad(ArchivePath, &Error);
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000725 if (TheArchive == 0) {
Chris Lattner67c38212004-12-15 07:44:15 +0000726 std::cerr << argv[0] << ": error loading '" << ArchivePath << "': "
727 << Error << "!\n";
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000728 return 1;
729 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000730 }
731
732 // Make sure we're not fooling ourselves.
733 assert(TheArchive && "Unable to instantiate the archive");
734
Reid Spencerbede5832004-11-16 06:41:09 +0000735 // Make sure we clean up the archive even on failure.
736 std::auto_ptr<Archive> AutoArchive(TheArchive);
737
Reid Spencer3a1582b2004-11-14 22:20:07 +0000738 // Perform the operation
Reid Spencere5c9cb52006-08-23 00:39:35 +0000739 std::string ErrMsg;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000740 bool haveError = false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000741 switch (Operation) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000742 case Print: haveError = doPrint(&ErrMsg); break;
743 case Delete: haveError = doDelete(&ErrMsg); break;
744 case Move: haveError = doMove(&ErrMsg); break;
745 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
746 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
747 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
748 case Extract: haveError = doExtract(&ErrMsg); break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000749 case NoOperation:
750 std::cerr << argv[0] << ": No operation was selected.\n";
751 break;
752 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000753 if (haveError) {
754 std::cerr << argv[0] << ": " << ErrMsg << "\n";
755 return 1;
756 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000757 } catch (const char*msg) {
758 // These errors are usage errors, thrown only by the various checks in the
759 // code above.
760 std::cerr << argv[0] << ": " << msg << "\n\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000761 cl::PrintHelpMessage();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000762 exitCode = 1;
763 } catch (const std::string& msg) {
764 // These errors are thrown by LLVM libraries (e.g. lib System) and represent
765 // a more serious error so we bump the exitCode and don't print the usage.
766 std::cerr << argv[0] << ": " << msg << "\n";
767 exitCode = 2;
768 } catch (...) {
769 // This really shouldn't happen, but just in case ....
Reid Spencerbede5832004-11-16 06:41:09 +0000770 std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000771 exitCode = 3;
772 }
773
774 // Return result code back to operating system.
775 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000776}