blob: c1c8b2474e7944a1adb6c3d685cb8c14b1e9006b [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"
Michael J. Spencer54453f22011-01-10 02:34:23 +000019#include "llvm/Support/FileSystem.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000020#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000021#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner424a04e2010-11-29 23:02:20 +000022#include "llvm/Support/Format.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000023#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000024#include "llvm/Support/Signals.h"
Reid Spencer3a1582b2004-11-14 22:20:07 +000025#include <algorithm>
Duraid Madina6927b062005-12-28 06:56:09 +000026#include <memory>
Chris Lattner8da76362009-08-24 04:14:03 +000027#include <fstream>
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Dan Gohman82f209e2007-10-15 21:10:03 +000030// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000031static cl::opt<bool>
32X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000033 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000034
Reid Spencerbede5832004-11-16 06:41:09 +000035// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000036static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000037Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000038
Reid Spencerbede5832004-11-16 06:41:09 +000039// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000040static cl::list<std::string>
41RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000042 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000043
Reid Spencerbede5832004-11-16 06:41:09 +000044// MoreHelp - Provide additional help output explaining the operations and
45// modifiers of llvm-ar. This object instructs the CommandLine library
46// to print the text of the constructor when the --help option is given.
47static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000048 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000049 " d[NsS] - delete file(s) from the archive\n"
50 " m[abiSs] - move file(s) in the archive\n"
51 " p[kN] - print file(s) found in the archive\n"
52 " q[ufsS] - quick append file(s) to the archive\n"
53 " r[abfiuzRsS] - replace or insert file(s) into the archive\n"
54 " t - display contents of archive\n"
55 " x[No] - extract file(s) from the archive\n"
56 "\nMODIFIERS (operation specific):\n"
57 " [a] - put file(s) after [relpos]\n"
58 " [b] - put file(s) before [relpos] (same as [i])\n"
59 " [f] - truncate inserted file names\n"
60 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000061 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000062 " [N] - use instance [count] of name\n"
63 " [o] - preserve original dates\n"
64 " [P] - use full path names when matching\n"
65 " [R] - recurse through directories when inserting\n"
66 " [s] - create an archive index (cf. ranlib)\n"
67 " [S] - do not build a symbol table\n"
68 " [u] - update only files newer than archive contents\n"
69 " [z] - compress files before inserting/extracting\n"
70 "\nMODIFIERS (generic):\n"
71 " [c] - do not warn if the library had to be created\n"
72 " [v] - be verbose about actions taken\n"
73 " [V] - be *really* verbose about actions taken\n"
74);
75
Reid Spencer3a1582b2004-11-14 22:20:07 +000076// This enumeration delineates the kinds of operations on an archive
77// that are permitted.
78enum ArchiveOperation {
79 NoOperation, ///< An operation hasn't been specified
80 Print, ///< Print the contents of the archive
81 Delete, ///< Delete the specified members
82 Move, ///< Move members to end or as given by {a,b,i} modifiers
83 QuickAppend, ///< Quickly append to end of archive
84 ReplaceOrInsert, ///< Replace or Insert members
85 DisplayTable, ///< Display the table of contents
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000086 Extract ///< Extract files back to file system
Tanya Lattner57bd7962003-12-06 23:01:25 +000087};
Tanya Lattner14baebf2003-08-28 15:22:38 +000088
Reid Spencer3a1582b2004-11-14 22:20:07 +000089// Modifiers to follow operation to vary behavior
90bool AddAfter = false; ///< 'a' modifier
91bool AddBefore = false; ///< 'b' modifier
Misha Brukman3da94ae2005-04-22 00:00:37 +000092bool Create = false; ///< 'c' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000093bool TruncateNames = false; ///< 'f' modifier
94bool InsertBefore = false; ///< 'i' modifier
Gabor Greifa99be512007-07-05 17:07:56 +000095bool DontSkipBitcode = false; ///< 'k' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000096bool UseCount = false; ///< 'N' modifier
97bool OriginalDates = false; ///< 'o' modifier
98bool FullPath = false; ///< 'P' modifier
99bool RecurseDirectories = false; ///< 'R' modifier
100bool SymTable = true; ///< 's' & 'S' modifiers
101bool OnlyUpdate = false; ///< 'u' modifier
102bool Verbose = false; ///< 'v' modifier
103bool ReallyVerbose = false; ///< 'V' modifier
104bool Compression = false; ///< 'z' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +0000105
Reid Spencer3a1582b2004-11-14 22:20:07 +0000106// Relative Positional Argument (for insert/move). This variable holds
107// the name of the archive member to which the 'a', 'b' or 'i' modifier
108// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
109// one variable.
110std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000111
Reid Spencer3a1582b2004-11-14 22:20:07 +0000112// Select which of multiple entries in the archive with the same name should be
113// used (specified with -N) for the delete and extract operations.
114int Count = 1;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000115
Reid Spencer3a1582b2004-11-14 22:20:07 +0000116// This variable holds the name of the archive file as given on the
117// command line.
118std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000119
Reid Spencer3a1582b2004-11-14 22:20:07 +0000120// This variable holds the list of member files to proecess, as given
121// on the command line.
122std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000123
Reid Spencer3a1582b2004-11-14 22:20:07 +0000124// This variable holds the (possibly expanded) list of path objects that
125// correspond to files we will
Reid Spencerbede5832004-11-16 06:41:09 +0000126std::set<sys::Path> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000127
Reid Spencer3a1582b2004-11-14 22:20:07 +0000128// The Archive object to which all the editing operations will be sent.
129Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000130
Reid Spencer3a1582b2004-11-14 22:20:07 +0000131// getRelPos - Extract the member filename from the command line for
132// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000133void getRelPos() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000134 if(RestOfArgs.size() > 0) {
135 RelPos = RestOfArgs[0];
136 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000137 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000138 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000139 throw "Expected [relpos] for a, b, or i modifier";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000140}
141
Misha Brukman3da94ae2005-04-22 00:00:37 +0000142// getCount - Extract the [count] argument associated with the N modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000143// from the command line and check its value.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000144void getCount() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000145 if(RestOfArgs.size() > 0) {
146 Count = atoi(RestOfArgs[0].c_str());
147 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000148 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000149 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000150 throw "Expected [count] value with N modifier";
151
152 // Non-positive counts are not allowed
153 if (Count < 1)
154 throw "Invalid [count] value (not a positive integer)";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000155}
156
Reid Spencer3a1582b2004-11-14 22:20:07 +0000157// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000158void getArchive() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000159 if(RestOfArgs.size() > 0) {
160 ArchiveName = RestOfArgs[0];
161 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000162 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000163 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000164 throw "An archive name must be specified.";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000165}
166
Reid Spencer3a1582b2004-11-14 22:20:07 +0000167// getMembers - Copy over remaining items in RestOfArgs to our Members vector
168// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000169void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000170 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000171 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000172}
173
Reid Spencer3a1582b2004-11-14 22:20:07 +0000174// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000175// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000176// constraints on modifier/operation pairs have not been violated.
177ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000178
Reid Spencer3a1582b2004-11-14 22:20:07 +0000179 // Keep track of number of operations. We can only specify one
180 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000181 unsigned NumOperations = 0;
182
Reid Spencer3a1582b2004-11-14 22:20:07 +0000183 // Keep track of the number of positional modifiers (a,b,i). Only
184 // one can be specified.
185 unsigned NumPositional = 0;
186
187 // Keep track of which operation was requested
188 ArchiveOperation Operation = NoOperation;
189
Tanya Lattner57bd7962003-12-06 23:01:25 +0000190 for(unsigned i=0; i<Options.size(); ++i) {
191 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000192 case 'd': ++NumOperations; Operation = Delete; break;
193 case 'm': ++NumOperations; Operation = Move ; break;
194 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000195 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000196 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000197 case 't': ++NumOperations; Operation = DisplayTable; break;
198 case 'x': ++NumOperations; Operation = Extract; break;
199 case 'c': Create = true; break;
200 case 'f': TruncateNames = true; break;
Gabor Greifa99be512007-07-05 17:07:56 +0000201 case 'k': DontSkipBitcode = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000202 case 'l': /* accepted but unused */ break;
203 case 'o': OriginalDates = true; break;
204 case 'P': FullPath = true; break;
205 case 'R': RecurseDirectories = true; break;
206 case 's': SymTable = true; break;
207 case 'S': SymTable = false; break;
208 case 'u': OnlyUpdate = true; break;
209 case 'v': Verbose = true; break;
210 case 'V': Verbose = ReallyVerbose = true; break;
211 case 'z': Compression = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000212 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000213 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000214 AddAfter = true;
215 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000216 break;
217 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000218 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000219 AddBefore = true;
220 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000221 break;
222 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000223 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000224 InsertBefore = true;
225 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000226 break;
227 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000228 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000229 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000230 break;
231 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000232 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000233 }
234 }
235
Misha Brukman3da94ae2005-04-22 00:00:37 +0000236 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000237 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000238 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000239
240 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000241 getMembers();
242
Reid Spencer3a1582b2004-11-14 22:20:07 +0000243 // Perform various checks on the operation/modifier specification
244 // to make sure we are dealing with a legal request.
245 if (NumOperations == 0)
246 throw "You must specify at least one of the operations";
247 if (NumOperations > 1)
248 throw "Only one operation may be specified";
249 if (NumPositional > 1)
250 throw "You may only specify one of a, b, and i modifiers";
251 if (AddAfter || AddBefore || InsertBefore)
252 if (Operation != Move && Operation != ReplaceOrInsert)
253 throw "The 'a', 'b' and 'i' modifiers can only be specified with "
254 "the 'm' or 'r' operations";
255 if (RecurseDirectories && Operation != ReplaceOrInsert)
256 throw "The 'R' modifiers is only applicabe to the 'r' operation";
257 if (OriginalDates && Operation != Extract)
258 throw "The 'o' modifier is only applicable to the 'x' operation";
259 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
260 throw "The 'f' modifier is only applicable to the 'q' and 'r' operations";
261 if (OnlyUpdate && Operation != ReplaceOrInsert)
262 throw "The 'u' modifier is only applicable to the 'r' operation";
263 if (Compression && Operation!=ReplaceOrInsert && Operation!=Extract)
264 throw "The 'z' modifier is only applicable to the 'r' and 'x' operations";
265 if (Count > 1 && Members.size() > 1)
266 throw "Only one member name may be specified with the 'N' modifier";
267
268 // Return the parsed operation to the caller
269 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000270}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000271
Reid Spencer3a1582b2004-11-14 22:20:07 +0000272// recurseDirectories - Implements the "R" modifier. This function scans through
273// the Paths vector (built by buildPaths, below) and replaces any directories it
274// finds with all the files in that directory (recursively). It uses the
275// sys::Path::getDirectoryContent method to perform the actual directory scans.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000276bool
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000277recurseDirectories(const sys::Path& path,
Reid Spencer142ca8e2006-08-23 06:56:27 +0000278 std::set<sys::Path>& result, std::string* ErrMsg) {
279 result.clear();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000280 if (RecurseDirectories) {
Reid Spencerbede5832004-11-16 06:41:09 +0000281 std::set<sys::Path> content;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000282 if (path.getDirectoryContents(content, ErrMsg))
283 return true;
284
Misha Brukman3da94ae2005-04-22 00:00:37 +0000285 for (std::set<sys::Path>::iterator I = content.begin(), E = content.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000286 I != E; ++I) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000287 // Make sure it exists and is a directory
Reid Spencer184e67e2007-04-08 19:59:07 +0000288 sys::PathWithStatus PwS(*I);
289 const sys::FileStatus *Status = PwS.getFileStatus(false, ErrMsg);
Reid Spencer8475ec02007-03-29 19:05:44 +0000290 if (!Status)
291 return true;
292 if (Status->isDir) {
293 std::set<sys::Path> moreResults;
294 if (recurseDirectories(*I, moreResults, ErrMsg))
295 return true;
296 result.insert(moreResults.begin(), moreResults.end());
297 } else {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000298 result.insert(*I);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000299 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000300 }
301 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000302 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000303}
304
305// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000306// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000307// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000308bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000309 for (unsigned i = 0; i < Members.size(); i++) {
310 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000311 if (!aPath.set(Members[i]))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000312 throw std::string("File member name invalid: ") + Members[i];
313 if (checkExistence) {
Michael J. Spencer54453f22011-01-10 02:34:23 +0000314 bool Exists;
315 if (sys::fs::exists(aPath.str(), Exists) || !Exists)
Reid Spencer3a1582b2004-11-14 22:20:07 +0000316 throw std::string("File does not exist: ") + Members[i];
Chris Lattner252ad032006-07-28 22:03:44 +0000317 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000318 sys::PathWithStatus PwS(aPath);
319 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000320 if (!si)
Chris Lattner252ad032006-07-28 22:03:44 +0000321 throw Err;
Reid Spencer8475ec02007-03-29 19:05:44 +0000322 if (si->isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000323 std::set<sys::Path> dirpaths;
324 if (recurseDirectories(aPath, dirpaths, ErrMsg))
325 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000326 Paths.insert(dirpaths.begin(),dirpaths.end());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000327 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000328 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000329 }
330 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000331 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000332 }
333 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000334 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000335}
336
Reid Spencerbede5832004-11-16 06:41:09 +0000337// printSymbolTable - print out the archive's symbol table.
338void printSymbolTable() {
Chris Lattner424a04e2010-11-29 23:02:20 +0000339 outs() << "\nArchive Symbol Table:\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000340 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000341 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000342 I != E; ++I ) {
343 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
Chris Lattner424a04e2010-11-29 23:02:20 +0000344 outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000345 }
346}
347
Reid Spencer3a1582b2004-11-14 22:20:07 +0000348// doPrint - Implements the 'p' operation. This function traverses the archive
349// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000350// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000351// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000352bool doPrint(std::string* ErrMsg) {
353 if (buildPaths(false, ErrMsg))
354 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000355 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000356 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000357 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000358 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000359 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
360 if (countDown == 1) {
361 const char* data = reinterpret_cast<const char*>(I->getData());
362
363 // Skip things that don't make sense to print
Misha Brukman3da94ae2005-04-22 00:00:37 +0000364 if (I->isLLVMSymbolTable() || I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000365 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000366 continue;
367
368 if (Verbose)
Chris Lattner424a04e2010-11-29 23:02:20 +0000369 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000370
Chris Lattner44dadff2007-05-06 09:29:57 +0000371 unsigned len = I->getSize();
Chris Lattner424a04e2010-11-29 23:02:20 +0000372 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000373 } else {
374 countDown--;
375 }
376 }
377 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000378 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000379}
380
381// putMode - utility function for printing out the file mode when the 't'
382// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000383void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000384printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000385 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000386 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000387 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000388 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000389 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000390 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000391 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000392 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000393 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000394 outs() << "x";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000395 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000396 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000397}
398
399// doDisplayTable - Implement the 't' operation. This function prints out just
400// the file names of each of the members. However, if verbose mode is requested
401// ('v' modifier) then the file type, permission mode, user, group, size, and
402// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000403bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000404doDisplayTable(std::string* ErrMsg) {
405 if (buildPaths(false, ErrMsg))
406 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000407 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000408 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000409 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000410 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
411 if (Verbose) {
412 // FIXME: Output should be this format:
413 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000414 if (I->isBitcode())
Chris Lattner424a04e2010-11-29 23:02:20 +0000415 outs() << "b";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000416 else if (I->isCompressed())
Chris Lattner424a04e2010-11-29 23:02:20 +0000417 outs() << "Z";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000418 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000419 outs() << " ";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000420 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000421 printMode((mode >> 6) & 007);
422 printMode((mode >> 3) & 007);
423 printMode(mode & 007);
Chris Lattner424a04e2010-11-29 23:02:20 +0000424 outs() << " " << format("%4u", I->getUser());
425 outs() << "/" << format("%4u", I->getGroup());
426 outs() << " " << format("%8u", I->getSize());
427 outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str());
428 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000429 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000430 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000431 }
432 }
433 }
Reid Spencerbede5832004-11-16 06:41:09 +0000434 if (ReallyVerbose)
435 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000436 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000437}
438
439// doExtract - Implement the 'x' operation. This function extracts files back to
Reid Spencere5c9cb52006-08-23 00:39:35 +0000440// the file system, making sure to uncompress any that were compressed
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000441bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000442doExtract(std::string* ErrMsg) {
443 if (buildPaths(false, ErrMsg))
444 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000445 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000446 I != E; ++I ) {
447 if (Paths.empty() ||
448 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
449
450 // Make sure the intervening directories are created
451 if (I->hasPath()) {
452 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000453 dirs.eraseComponent();
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000454 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
Reid Spencere5c9cb52006-08-23 00:39:35 +0000455 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000456 }
457
458 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000459 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
460 std::ios::binary;
461 std::ofstream file(I->getPath().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000462
463 // Get the data and its length
464 const char* data = reinterpret_cast<const char*>(I->getData());
465 unsigned len = I->getSize();
466
Chris Lattner44dadff2007-05-06 09:29:57 +0000467 // Write the data.
468 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000469 file.close();
470
471 // If we're supposed to retain the original modification times, etc. do so
472 // now.
473 if (OriginalDates)
Chris Lattner252ad032006-07-28 22:03:44 +0000474 I->getPath().setStatusInfoOnDisk(I->getFileStatus());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000475 }
476 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000477 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000478}
479
480// doDelete - Implement the delete operation. This function deletes zero or more
481// members from the archive. Note that if the count is specified, there should
482// be no more than one path in the Paths list or else this algorithm breaks.
483// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000484bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000485doDelete(std::string* ErrMsg) {
486 if (buildPaths(false, ErrMsg))
487 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000488 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000489 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000490 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000491 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000492 I != E; ) {
493 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
494 if (countDown == 1) {
495 Archive::iterator J = I;
496 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000497 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000498 } else
499 countDown--;
500 } else {
501 ++I;
502 }
503 }
504
505 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000506 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000507 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000508 if (ReallyVerbose)
509 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000510 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000511}
512
513// doMore - Implement the move operation. This function re-arranges just the
514// order of the archive members so that when the archive is written the move
515// of the members is accomplished. Note the use of the RelPos variable to
516// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000517bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000518doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000519 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000520 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000521
522 // By default and convention the place to move members to is the end of the
523 // archive.
524 Archive::iterator moveto_spot = TheArchive->end();
525
526 // However, if the relative positioning modifiers were used, we need to scan
527 // the archive to find the member in question. If we don't find it, its no
528 // crime, we just move to the end.
529 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000530 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000531 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000532 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000533 if (AddAfter) {
534 moveto_spot = I;
535 moveto_spot++;
536 } else {
537 moveto_spot = I;
538 }
539 break;
540 }
541 }
542 }
543
544 // Keep a list of the paths remaining to be moved
Reid Spencerbede5832004-11-16 06:41:09 +0000545 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000546
547 // Scan the archive again, this time looking for the members to move to the
548 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000549 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000550 I != E && !remaining.empty(); ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000551 std::set<sys::Path>::iterator found =
Reid Spencer3a1582b2004-11-14 22:20:07 +0000552 std::find(remaining.begin(),remaining.end(),I->getPath());
553 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000554 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000555 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000556 remaining.erase(found);
557 }
558 }
559
560 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000561 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000562 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000563 if (ReallyVerbose)
564 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000565 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000566}
567
568// doQuickAppend - Implements the 'q' operation. This function just
569// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000570bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000571doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000572 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000573 if (buildPaths(true, ErrMsg))
574 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000575 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000576 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000577
578 // Append them quickly.
Reid Spencerbede5832004-11-16 06:41:09 +0000579 for (std::set<sys::Path>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000580 PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000581 if (TheArchive->addFileBefore(*PI,TheArchive->end(),ErrMsg))
582 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000583 }
584
585 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000586 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000587 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000588 if (ReallyVerbose)
589 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000590 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000591}
592
593// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000594// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000595bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000596doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000597
598 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000599 if (buildPaths(true, ErrMsg))
600 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000601 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000602 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000603
604 // Keep track of the paths that remain to be inserted.
Reid Spencerbede5832004-11-16 06:41:09 +0000605 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000606
607 // Default the insertion spot to the end of the archive
608 Archive::iterator insert_spot = TheArchive->end();
609
610 // Iterate over the archive contents
611 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
612 I != E && !remaining.empty(); ++I ) {
613
614 // Determine if this archive member matches one of the paths we're trying
615 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000616
617 std::set<sys::Path>::iterator found = remaining.end();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000618 for (std::set<sys::Path>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000619 RE = remaining.end(); RI != RE; ++RI ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000620 std::string compare(RI->str());
Reid Spencer0de66b52004-12-02 09:21:55 +0000621 if (TruncateNames && compare.length() > 15) {
622 const char* nm = compare.c_str();
623 unsigned len = compare.length();
624 size_t slashpos = compare.rfind('/');
625 if (slashpos != std::string::npos) {
626 nm += slashpos + 1;
627 len -= slashpos +1;
628 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000629 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000630 len = 15;
631 compare.assign(nm,len);
632 }
Chris Lattner74382b72009-08-23 22:45:37 +0000633 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000634 found = RI;
635 break;
636 }
637 }
638
Reid Spencer3a1582b2004-11-14 22:20:07 +0000639 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000640 std::string Err;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000641 sys::PathWithStatus PwS(*found);
Reid Spencer184e67e2007-04-08 19:59:07 +0000642 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000643 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000644 return true;
Chris Lattner7d8f9b62009-02-05 17:58:39 +0000645 if (!si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000646 if (OnlyUpdate) {
647 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000648 if (si->modTime > I->getModTime())
Reid Spencer0ff2d312006-08-24 23:45:08 +0000649 if (I->replaceWith(*found, ErrMsg))
650 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000651 } else {
652 // Replace the item regardless of time stamp
Reid Spencer0ff2d312006-08-24 23:45:08 +0000653 if (I->replaceWith(*found, ErrMsg))
654 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000655 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000656 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000657 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000658 }
659
660 // Remove it from our "to do" list
661 remaining.erase(found);
662 }
663
664 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000665 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000666 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000667 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000668 insert_spot = I;
669 insert_spot++;
670 }
671 }
672
673 // If we didn't replace all the members, some will remain and need to be
674 // inserted at the previously computed insert-spot.
675 if (!remaining.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000676 for (std::set<sys::Path>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000677 PE = remaining.end(); PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000678 if (TheArchive->addFileBefore(*PI,insert_spot, ErrMsg))
679 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000680 }
681 }
682
683 // We're done editting, reconstruct the archive.
Reid Spencer09069b32006-08-25 05:28:17 +0000684 if (TheArchive->writeToDisk(SymTable,TruncateNames,Compression,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000685 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000686 if (ReallyVerbose)
687 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000688 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000689}
690
691// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000692int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000693 // Print a stack trace if we signal out.
694 sys::PrintStackTraceOnErrorSignal();
695 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000696 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000697 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000698
Reid Spencer3a1582b2004-11-14 22:20:07 +0000699 // Have the command line options parsed and handle things
700 // like --help and --version.
701 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000702 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000703 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000704 );
705
Reid Spencer3a1582b2004-11-14 22:20:07 +0000706 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000707
Reid Spencer3a1582b2004-11-14 22:20:07 +0000708 // Make sure we don't exit with "unhandled exception".
709 try {
710 // Do our own parsing of the command line because the CommandLine utility
711 // can't handle the grouped positional parameters without a dash.
712 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000713
Reid Spencer3a1582b2004-11-14 22:20:07 +0000714 // Check the path name of the archive
715 sys::Path ArchivePath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000716 if (!ArchivePath.set(ArchiveName))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000717 throw std::string("Archive name invalid: ") + ArchiveName;
718
719 // Create or open the archive object.
Michael J. Spencer54453f22011-01-10 02:34:23 +0000720 bool Exists;
721 if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000722 // Produce a warning if we should and we're creating the archive
723 if (!Create)
Chris Lattner74382b72009-08-23 22:45:37 +0000724 errs() << argv[0] << ": creating " << ArchivePath.str() << "\n";
Owen Anderson31895e72009-07-01 21:22:36 +0000725 TheArchive = Archive::CreateEmpty(ArchivePath, Context);
Andrew Lenharth63b8c1f2008-02-28 22:24:48 +0000726 TheArchive->writeToDisk();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000727 } else {
Chris Lattner67c38212004-12-15 07:44:15 +0000728 std::string Error;
Owen Anderson31895e72009-07-01 21:22:36 +0000729 TheArchive = Archive::OpenAndLoad(ArchivePath, Context, &Error);
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000730 if (TheArchive == 0) {
Chris Lattner74382b72009-08-23 22:45:37 +0000731 errs() << argv[0] << ": error loading '" << ArchivePath.str() << "': "
Dan Gohman65f57c22009-07-15 16:35:29 +0000732 << Error << "!\n";
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000733 return 1;
734 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000735 }
736
737 // Make sure we're not fooling ourselves.
738 assert(TheArchive && "Unable to instantiate the archive");
739
Reid Spencerbede5832004-11-16 06:41:09 +0000740 // Make sure we clean up the archive even on failure.
741 std::auto_ptr<Archive> AutoArchive(TheArchive);
742
Reid Spencer3a1582b2004-11-14 22:20:07 +0000743 // Perform the operation
Reid Spencere5c9cb52006-08-23 00:39:35 +0000744 std::string ErrMsg;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000745 bool haveError = false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000746 switch (Operation) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000747 case Print: haveError = doPrint(&ErrMsg); break;
748 case Delete: haveError = doDelete(&ErrMsg); break;
749 case Move: haveError = doMove(&ErrMsg); break;
750 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
751 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
752 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
753 case Extract: haveError = doExtract(&ErrMsg); break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000754 case NoOperation:
Dan Gohman65f57c22009-07-15 16:35:29 +0000755 errs() << argv[0] << ": No operation was selected.\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000756 break;
757 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000758 if (haveError) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000759 errs() << argv[0] << ": " << ErrMsg << "\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000760 return 1;
761 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000762 } catch (const char*msg) {
763 // These errors are usage errors, thrown only by the various checks in the
764 // code above.
Dan Gohman65f57c22009-07-15 16:35:29 +0000765 errs() << argv[0] << ": " << msg << "\n\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000766 cl::PrintHelpMessage();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000767 exitCode = 1;
768 } catch (const std::string& msg) {
769 // These errors are thrown by LLVM libraries (e.g. lib System) and represent
770 // a more serious error so we bump the exitCode and don't print the usage.
Dan Gohman65f57c22009-07-15 16:35:29 +0000771 errs() << argv[0] << ": " << msg << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000772 exitCode = 2;
773 } catch (...) {
774 // This really shouldn't happen, but just in case ....
Dan Gohman65f57c22009-07-15 16:35:29 +0000775 errs() << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000776 exitCode = 3;
777 }
778
779 // Return result code back to operating system.
780 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000781}