blob: bf9447bccf94e89dd65cbee9078b8f093ffd06d7 [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
Rafael Espindola8496fae2013-06-17 15:47:20 +000015#include "Archive.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/LLVMContext.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Module.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"
Chandler Carruthf010c462012-12-04 10:44:52 +000020#include "llvm/Support/Format.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000021#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000022#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000023#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000024#include "llvm/Support/raw_ostream.h"
Reid Spencer3a1582b2004-11-14 22:20:07 +000025#include <algorithm>
Joerg Sonnenberger975bc072012-10-26 10:49:15 +000026#include <cstdlib>
Rafael Espindola11ca2e52013-06-20 20:56:14 +000027#include <fcntl.h>
Chandler Carruthf010c462012-12-04 10:44:52 +000028#include <memory>
Rafael Espindola11ca2e52013-06-20 20:56:14 +000029
30#if !defined(_MSC_VER) && !defined(__MINGW32__)
31#include <unistd.h>
32#else
33#include <io.h>
34#endif
35
Brian Gaeked0fde302003-11-11 22:41:34 +000036using namespace llvm;
37
Dan Gohman82f209e2007-10-15 21:10:03 +000038// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000039static cl::opt<bool>
40X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000041 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000042
Reid Spencerbede5832004-11-16 06:41:09 +000043// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000044static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000045Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000046
Reid Spencerbede5832004-11-16 06:41:09 +000047// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000048static cl::list<std::string>
49RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000050 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000051
Reid Spencerbede5832004-11-16 06:41:09 +000052// MoreHelp - Provide additional help output explaining the operations and
53// modifiers of llvm-ar. This object instructs the CommandLine library
54// to print the text of the constructor when the --help option is given.
55static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000056 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000057 " d[NsS] - delete file(s) from the archive\n"
58 " m[abiSs] - move file(s) in the archive\n"
59 " p[kN] - print file(s) found in the archive\n"
60 " q[ufsS] - quick append file(s) to the archive\n"
Rafael Espindola94bc2462012-08-10 01:57:52 +000061 " r[abfiuRsS] - replace or insert file(s) into the archive\n"
Reid Spencerbede5832004-11-16 06:41:09 +000062 " t - display contents of archive\n"
63 " x[No] - extract file(s) from the archive\n"
64 "\nMODIFIERS (operation specific):\n"
65 " [a] - put file(s) after [relpos]\n"
66 " [b] - put file(s) before [relpos] (same as [i])\n"
67 " [f] - truncate inserted file names\n"
68 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000069 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000070 " [N] - use instance [count] of name\n"
71 " [o] - preserve original dates\n"
72 " [P] - use full path names when matching\n"
73 " [R] - recurse through directories when inserting\n"
74 " [s] - create an archive index (cf. ranlib)\n"
75 " [S] - do not build a symbol table\n"
76 " [u] - update only files newer than archive contents\n"
Reid Spencerbede5832004-11-16 06:41:09 +000077 "\nMODIFIERS (generic):\n"
78 " [c] - do not warn if the library had to be created\n"
79 " [v] - be verbose about actions taken\n"
80 " [V] - be *really* verbose about actions taken\n"
81);
82
Reid Spencer3a1582b2004-11-14 22:20:07 +000083// This enumeration delineates the kinds of operations on an archive
84// that are permitted.
85enum ArchiveOperation {
Reid Spencer3a1582b2004-11-14 22:20:07 +000086 Print, ///< Print the contents of the archive
87 Delete, ///< Delete the specified members
88 Move, ///< Move members to end or as given by {a,b,i} modifiers
89 QuickAppend, ///< Quickly append to end of archive
90 ReplaceOrInsert, ///< Replace or Insert members
91 DisplayTable, ///< Display the table of contents
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000092 Extract ///< Extract files back to file system
Tanya Lattner57bd7962003-12-06 23:01:25 +000093};
Tanya Lattner14baebf2003-08-28 15:22:38 +000094
Reid Spencer3a1582b2004-11-14 22:20:07 +000095// Modifiers to follow operation to vary behavior
96bool AddAfter = false; ///< 'a' modifier
97bool AddBefore = false; ///< 'b' modifier
Misha Brukman3da94ae2005-04-22 00:00:37 +000098bool Create = false; ///< 'c' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000099bool TruncateNames = false; ///< 'f' modifier
100bool InsertBefore = false; ///< 'i' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000101bool UseCount = false; ///< 'N' modifier
102bool OriginalDates = false; ///< 'o' modifier
103bool FullPath = false; ///< 'P' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000104bool SymTable = true; ///< 's' & 'S' modifiers
105bool OnlyUpdate = false; ///< 'u' modifier
106bool Verbose = false; ///< 'v' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +0000107
Reid Spencer3a1582b2004-11-14 22:20:07 +0000108// Relative Positional Argument (for insert/move). This variable holds
109// the name of the archive member to which the 'a', 'b' or 'i' modifier
110// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
111// one variable.
112std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000113
Reid Spencer3a1582b2004-11-14 22:20:07 +0000114// Select which of multiple entries in the archive with the same name should be
115// used (specified with -N) for the delete and extract operations.
116int Count = 1;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000117
Reid Spencer3a1582b2004-11-14 22:20:07 +0000118// This variable holds the name of the archive file as given on the
119// command line.
120std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000121
Reid Spencer3a1582b2004-11-14 22:20:07 +0000122// This variable holds the list of member files to proecess, as given
123// on the command line.
124std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000125
Reid Spencer3a1582b2004-11-14 22:20:07 +0000126// This variable holds the (possibly expanded) list of path objects that
127// correspond to files we will
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000128std::set<std::string> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000129
Reid Spencer3a1582b2004-11-14 22:20:07 +0000130// The Archive object to which all the editing operations will be sent.
131Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000132
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000133// The name this program was invoked as.
134static const char *program_name;
135
136// show_help - Show the error message, the help message and exit.
137LLVM_ATTRIBUTE_NORETURN static void
138show_help(const std::string &msg) {
139 errs() << program_name << ": " << msg << "\n\n";
140 cl::PrintHelpMessage();
141 if (TheArchive)
142 delete TheArchive;
143 std::exit(1);
144}
145
146// fail - Show the error message and exit.
147LLVM_ATTRIBUTE_NORETURN static void
148fail(const std::string &msg) {
149 errs() << program_name << ": " << msg << "\n\n";
150 if (TheArchive)
151 delete TheArchive;
152 std::exit(1);
153}
154
Reid Spencer3a1582b2004-11-14 22:20:07 +0000155// getRelPos - Extract the member filename from the command line for
156// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000157void getRelPos() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000158 if(RestOfArgs.size() == 0)
159 show_help("Expected [relpos] for a, b, or i modifier");
160 RelPos = RestOfArgs[0];
161 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000162}
163
Misha Brukman3da94ae2005-04-22 00:00:37 +0000164// getCount - Extract the [count] argument associated with the N modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000165// from the command line and check its value.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000166void getCount() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000167 if(RestOfArgs.size() == 0)
168 show_help("Expected [count] value with N modifier");
169
170 Count = atoi(RestOfArgs[0].c_str());
171 RestOfArgs.erase(RestOfArgs.begin());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000172
173 // Non-positive counts are not allowed
174 if (Count < 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000175 show_help("Invalid [count] value (not a positive integer)");
Tanya Lattner57bd7962003-12-06 23:01:25 +0000176}
177
Reid Spencer3a1582b2004-11-14 22:20:07 +0000178// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000179void getArchive() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000180 if(RestOfArgs.size() == 0)
181 show_help("An archive name must be specified");
182 ArchiveName = RestOfArgs[0];
183 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000184}
185
Reid Spencer3a1582b2004-11-14 22:20:07 +0000186// getMembers - Copy over remaining items in RestOfArgs to our Members vector
187// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000188void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000189 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000190 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000191}
192
Reid Spencer3a1582b2004-11-14 22:20:07 +0000193// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000194// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000195// constraints on modifier/operation pairs have not been violated.
196ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000197
Reid Spencer3a1582b2004-11-14 22:20:07 +0000198 // Keep track of number of operations. We can only specify one
199 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000200 unsigned NumOperations = 0;
201
Reid Spencer3a1582b2004-11-14 22:20:07 +0000202 // Keep track of the number of positional modifiers (a,b,i). Only
203 // one can be specified.
204 unsigned NumPositional = 0;
205
206 // Keep track of which operation was requested
Rafael Espindola87b8a7f92013-07-05 12:12:43 +0000207 ArchiveOperation Operation;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000208
Tanya Lattner57bd7962003-12-06 23:01:25 +0000209 for(unsigned i=0; i<Options.size(); ++i) {
210 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000211 case 'd': ++NumOperations; Operation = Delete; break;
212 case 'm': ++NumOperations; Operation = Move ; break;
213 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000214 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000215 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000216 case 't': ++NumOperations; Operation = DisplayTable; break;
217 case 'x': ++NumOperations; Operation = Extract; break;
218 case 'c': Create = true; break;
219 case 'f': TruncateNames = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000220 case 'l': /* accepted but unused */ break;
221 case 'o': OriginalDates = true; break;
Rafael Espindola6f2c88a2013-06-20 13:00:30 +0000222 case 's': break; // Ignore for now.
223 case 'S': break; // Ignore for now.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000224 case 'P': FullPath = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000225 case 'u': OnlyUpdate = true; break;
226 case 'v': Verbose = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000227 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000228 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000229 AddAfter = true;
230 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000231 break;
232 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000233 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000234 AddBefore = true;
235 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000236 break;
237 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000238 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000239 InsertBefore = true;
240 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000241 break;
242 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000243 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000244 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000245 break;
246 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000247 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000248 }
249 }
250
Misha Brukman3da94ae2005-04-22 00:00:37 +0000251 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000252 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000253 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000254
255 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000256 getMembers();
257
Reid Spencer3a1582b2004-11-14 22:20:07 +0000258 // Perform various checks on the operation/modifier specification
259 // to make sure we are dealing with a legal request.
260 if (NumOperations == 0)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000261 show_help("You must specify at least one of the operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000262 if (NumOperations > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000263 show_help("Only one operation may be specified");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000264 if (NumPositional > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000265 show_help("You may only specify one of a, b, and i modifiers");
266 if (AddAfter || AddBefore || InsertBefore) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000267 if (Operation != Move && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000268 show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
269 "the 'm' or 'r' operations");
270 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000271 if (OriginalDates && Operation != Extract)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000272 show_help("The 'o' modifier is only applicable to the 'x' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000273 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000274 show_help("The 'f' modifier is only applicable to the 'q' and 'r' "
275 "operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000276 if (OnlyUpdate && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000277 show_help("The 'u' modifier is only applicable to the 'r' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000278 if (Count > 1 && Members.size() > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000279 show_help("Only one member name may be specified with the 'N' modifier");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000280
281 // Return the parsed operation to the caller
282 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000283}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000284
Reid Spencer3a1582b2004-11-14 22:20:07 +0000285// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000286// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000287// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000288bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000289 for (unsigned i = 0; i < Members.size(); i++) {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000290 std::string aPath = Members[i];
Reid Spencer3a1582b2004-11-14 22:20:07 +0000291 if (checkExistence) {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000292 bool IsDirectory;
293 error_code EC = sys::fs::is_directory(aPath, IsDirectory);
294 if (EC)
295 fail(aPath + ": " + EC.message());
296 if (IsDirectory)
297 fail(aPath + " Is a directory");
Rafael Espindola8fe960e2013-06-19 14:58:16 +0000298
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000299 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000300 } else {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000301 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000302 }
303 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000304 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000305}
306
307// doPrint - Implements the 'p' operation. This function traverses the archive
308// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000309// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000310// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000311bool doPrint(std::string* ErrMsg) {
312 if (buildPaths(false, ErrMsg))
313 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000314 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000315 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000316 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000317 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000318 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
319 if (countDown == 1) {
320 const char* data = reinterpret_cast<const char*>(I->getData());
321
322 // Skip things that don't make sense to print
Rafael Espindola24663f62013-07-05 04:19:32 +0000323 if (I->isSVR4SymbolTable() || I->isBSD4SymbolTable())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000324 continue;
325
326 if (Verbose)
Chris Lattner424a04e2010-11-29 23:02:20 +0000327 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000328
Chris Lattner44dadff2007-05-06 09:29:57 +0000329 unsigned len = I->getSize();
Chris Lattner424a04e2010-11-29 23:02:20 +0000330 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000331 } else {
332 countDown--;
333 }
334 }
335 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000336 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000337}
338
339// putMode - utility function for printing out the file mode when the 't'
340// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000341void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000342printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000343 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000344 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000345 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000346 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000347 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000348 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000349 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000350 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000351 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000352 outs() << "x";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000353 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000354 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000355}
356
357// doDisplayTable - Implement the 't' operation. This function prints out just
358// the file names of each of the members. However, if verbose mode is requested
359// ('v' modifier) then the file type, permission mode, user, group, size, and
360// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000361bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000362doDisplayTable(std::string* ErrMsg) {
363 if (buildPaths(false, ErrMsg))
364 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000365 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000366 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000367 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000368 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
369 if (Verbose) {
370 // FIXME: Output should be this format:
371 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Rafael Espindola24663f62013-07-05 04:19:32 +0000372 outs() << " ";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000373 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000374 printMode((mode >> 6) & 007);
375 printMode((mode >> 3) & 007);
376 printMode(mode & 007);
Chris Lattner424a04e2010-11-29 23:02:20 +0000377 outs() << " " << format("%4u", I->getUser());
378 outs() << "/" << format("%4u", I->getGroup());
379 outs() << " " << format("%8u", I->getSize());
380 outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str());
381 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000382 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000383 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000384 }
385 }
386 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000387 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000388}
389
390// doExtract - Implement the 'x' operation. This function extracts files back to
Rafael Espindola94bc2462012-08-10 01:57:52 +0000391// the file system.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000392bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000393doExtract(std::string* ErrMsg) {
394 if (buildPaths(false, ErrMsg))
395 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000396 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000397 I != E; ++I ) {
398 if (Paths.empty() ||
399 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
400
Reid Spencer3a1582b2004-11-14 22:20:07 +0000401 // Open up a file stream for writing
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000402 int OpenFlags = O_TRUNC | O_WRONLY | O_CREAT;
403#ifdef O_BINARY
404 OpenFlags |= O_BINARY;
405#endif
Reid Spencer3a1582b2004-11-14 22:20:07 +0000406
Rafael Espindola35637fc2013-07-08 16:16:51 +0000407 // Retain the original mode.
408 sys::fs::perms Mode = sys::fs::perms(I->getMode());
409
410 int FD = open(I->getPath().str().c_str(), OpenFlags, Mode);
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000411 if (FD < 0)
412 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000413
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000414 {
415 raw_fd_ostream file(FD, false);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000416
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000417 // Get the data and its length
418 const char* data = reinterpret_cast<const char*>(I->getData());
419 unsigned len = I->getSize();
420
421 // Write the data.
422 file.write(data, len);
423 }
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000424
Reid Spencer3a1582b2004-11-14 22:20:07 +0000425 // If we're supposed to retain the original modification times, etc. do so
426 // now.
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000427 if (OriginalDates) {
Rafael Espindola35637fc2013-07-08 16:16:51 +0000428 error_code EC =
429 sys::fs::setLastModificationAndAccessTime(FD, I->getModTime());
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000430 if (EC)
431 fail(EC.message());
432 }
433 if (close(FD))
434 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000435 }
436 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000437 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000438}
439
440// doDelete - Implement the delete operation. This function deletes zero or more
441// members from the archive. Note that if the count is specified, there should
442// be no more than one path in the Paths list or else this algorithm breaks.
443// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000444bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000445doDelete(std::string* ErrMsg) {
446 if (buildPaths(false, ErrMsg))
447 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000448 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000449 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000450 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000451 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000452 I != E; ) {
453 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
454 if (countDown == 1) {
455 Archive::iterator J = I;
456 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000457 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000458 } else
459 countDown--;
460 } else {
461 ++I;
462 }
463 }
464
465 // We're done editting, reconstruct the archive.
Rafael Espindola6f2c88a2013-06-20 13:00:30 +0000466 if (TheArchive->writeToDisk(TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000467 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000468 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000469}
470
471// doMore - Implement the move operation. This function re-arranges just the
472// order of the archive members so that when the archive is written the move
473// of the members is accomplished. Note the use of the RelPos variable to
474// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000475bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000476doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000477 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000478 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000479
480 // By default and convention the place to move members to is the end of the
481 // archive.
482 Archive::iterator moveto_spot = TheArchive->end();
483
484 // However, if the relative positioning modifiers were used, we need to scan
485 // the archive to find the member in question. If we don't find it, its no
486 // crime, we just move to the end.
487 if (AddBefore || InsertBefore || AddAfter) {
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; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000490 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000491 if (AddAfter) {
492 moveto_spot = I;
493 moveto_spot++;
494 } else {
495 moveto_spot = I;
496 }
497 break;
498 }
499 }
500 }
501
502 // Keep a list of the paths remaining to be moved
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000503 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000504
505 // Scan the archive again, this time looking for the members to move to the
506 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000507 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000508 I != E && !remaining.empty(); ++I ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000509 std::set<std::string>::iterator found =
510 std::find(remaining.begin(),remaining.end(), I->getPath());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000511 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000512 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000513 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000514 remaining.erase(found);
515 }
516 }
517
518 // We're done editting, reconstruct the archive.
Rafael Espindola6f2c88a2013-06-20 13:00:30 +0000519 if (TheArchive->writeToDisk(TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000520 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000521 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000522}
523
524// doQuickAppend - Implements the 'q' operation. This function just
525// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000526bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000527doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000528 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000529 if (buildPaths(true, ErrMsg))
530 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000531 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000532 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000533
534 // Append them quickly.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000535 for (std::set<std::string>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000536 PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000537 if (TheArchive->addFileBefore(*PI, TheArchive->end(), ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000538 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000539 }
540
541 // We're done editting, reconstruct the archive.
Rafael Espindola6f2c88a2013-06-20 13:00:30 +0000542 if (TheArchive->writeToDisk(TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000543 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000544 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000545}
546
547// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000548// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000549bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000550doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000551
552 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000553 if (buildPaths(true, ErrMsg))
554 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000555 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000556 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000557
558 // Keep track of the paths that remain to be inserted.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000559 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000560
561 // Default the insertion spot to the end of the archive
562 Archive::iterator insert_spot = TheArchive->end();
563
564 // Iterate over the archive contents
565 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
566 I != E && !remaining.empty(); ++I ) {
567
568 // Determine if this archive member matches one of the paths we're trying
569 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000570
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000571 std::set<std::string>::iterator found = remaining.end();
572 for (std::set<std::string>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000573 RE = remaining.end(); RI != RE; ++RI ) {
Rafael Espindolaeb729e02013-06-20 18:30:37 +0000574 std::string compare(sys::path::filename(*RI));
Reid Spencer0de66b52004-12-02 09:21:55 +0000575 if (TruncateNames && compare.length() > 15) {
576 const char* nm = compare.c_str();
577 unsigned len = compare.length();
578 size_t slashpos = compare.rfind('/');
579 if (slashpos != std::string::npos) {
580 nm += slashpos + 1;
581 len -= slashpos +1;
582 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000583 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000584 len = 15;
585 compare.assign(nm,len);
586 }
Chris Lattner74382b72009-08-23 22:45:37 +0000587 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000588 found = RI;
589 break;
590 }
591 }
592
Reid Spencer3a1582b2004-11-14 22:20:07 +0000593 if (found != remaining.end()) {
Rafael Espindola29c17db2013-06-20 18:42:04 +0000594 sys::fs::file_status Status;
595 error_code EC = sys::fs::status(*found, Status);
596 if (EC)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000597 return true;
Rafael Espindola29c17db2013-06-20 18:42:04 +0000598 if (!sys::fs::is_directory(Status)) {
Reid Spencerbede5832004-11-16 06:41:09 +0000599 if (OnlyUpdate) {
600 // Replace the item only if it is newer.
Rafael Espindola29c17db2013-06-20 18:42:04 +0000601 if (Status.getLastModificationTime() > I->getModTime())
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000602 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000603 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000604 } else {
605 // Replace the item regardless of time stamp
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000606 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000607 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000608 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000609 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000610 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000611 }
612
613 // Remove it from our "to do" list
614 remaining.erase(found);
615 }
616
617 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000618 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000619 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000620 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000621 insert_spot = I;
622 insert_spot++;
623 }
624 }
625
626 // If we didn't replace all the members, some will remain and need to be
627 // inserted at the previously computed insert-spot.
628 if (!remaining.empty()) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000629 for (std::set<std::string>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000630 PE = remaining.end(); PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000631 if (TheArchive->addFileBefore(*PI, insert_spot, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000632 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000633 }
634 }
635
636 // We're done editting, reconstruct the archive.
Rafael Espindola6f2c88a2013-06-20 13:00:30 +0000637 if (TheArchive->writeToDisk(TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000638 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000639 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000640}
641
Rafael Espindola61de1422013-07-05 13:03:07 +0000642bool shouldCreateArchive(ArchiveOperation Op) {
643 switch (Op) {
644 case Print:
645 case Delete:
646 case Move:
647 case DisplayTable:
648 case Extract:
649 return false;
650
651 case QuickAppend:
652 case ReplaceOrInsert:
653 return true;
654 }
Michael Gottesman2be430d2013-07-06 02:39:51 +0000655
656 llvm_unreachable("Missing entry in covered switch.");
Rafael Espindola61de1422013-07-05 13:03:07 +0000657}
658
Reid Spencer3a1582b2004-11-14 22:20:07 +0000659// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000660int main(int argc, char **argv) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000661 program_name = argv[0];
Chris Lattnercc14d252009-03-06 05:34:10 +0000662 // Print a stack trace if we signal out.
663 sys::PrintStackTraceOnErrorSignal();
664 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000665 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000666 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000667
Reid Spencer3a1582b2004-11-14 22:20:07 +0000668 // Have the command line options parsed and handle things
669 // like --help and --version.
670 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000671 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000672 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000673 );
674
Reid Spencer3a1582b2004-11-14 22:20:07 +0000675 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000676
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000677 // Do our own parsing of the command line because the CommandLine utility
678 // can't handle the grouped positional parameters without a dash.
679 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000680
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000681 // Create or open the archive object.
Rafael Espindola61de1422013-07-05 13:03:07 +0000682 if (shouldCreateArchive(Operation) && !llvm::sys::fs::exists(ArchiveName)) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000683 // Produce a warning if we should and we're creating the archive
684 if (!Create)
Rafael Espindola201c66c2013-06-20 18:55:44 +0000685 errs() << argv[0] << ": creating " << ArchiveName << "\n";
686 TheArchive = Archive::CreateEmpty(ArchiveName, Context);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000687 TheArchive->writeToDisk();
Rafael Espindola61de1422013-07-05 13:03:07 +0000688 }
689
690 if (!TheArchive) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000691 std::string Error;
Rafael Espindola201c66c2013-06-20 18:55:44 +0000692 TheArchive = Archive::OpenAndLoad(ArchiveName, Context, &Error);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000693 if (TheArchive == 0) {
Rafael Espindola201c66c2013-06-20 18:55:44 +0000694 errs() << argv[0] << ": error loading '" << ArchiveName << "': "
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000695 << Error << "!\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000696 return 1;
697 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000698 }
699
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000700 // Make sure we're not fooling ourselves.
701 assert(TheArchive && "Unable to instantiate the archive");
702
703 // Perform the operation
704 std::string ErrMsg;
705 bool haveError = false;
706 switch (Operation) {
707 case Print: haveError = doPrint(&ErrMsg); break;
708 case Delete: haveError = doDelete(&ErrMsg); break;
709 case Move: haveError = doMove(&ErrMsg); break;
710 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
711 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
712 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
713 case Extract: haveError = doExtract(&ErrMsg); break;
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000714 }
715 if (haveError) {
716 errs() << argv[0] << ": " << ErrMsg << "\n";
717 return 1;
718 }
719
720 delete TheArchive;
721 TheArchive = 0;
722
Reid Spencer3a1582b2004-11-14 22:20:07 +0000723 // Return result code back to operating system.
724 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000725}