blob: 03bb36b116ef4397cd92d728cf5576817c99eb97 [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>
Chris Lattner8da76362009-08-24 04:14:03 +000027#include <fstream>
Chandler Carruthf010c462012-12-04 10:44:52 +000028#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Dan Gohman82f209e2007-10-15 21:10:03 +000031// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000032static cl::opt<bool>
33X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000034 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000035
Reid Spencerbede5832004-11-16 06:41:09 +000036// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000037static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000038Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000039
Reid Spencerbede5832004-11-16 06:41:09 +000040// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000041static cl::list<std::string>
42RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000043 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000044
Reid Spencerbede5832004-11-16 06:41:09 +000045// MoreHelp - Provide additional help output explaining the operations and
46// modifiers of llvm-ar. This object instructs the CommandLine library
47// to print the text of the constructor when the --help option is given.
48static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000049 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000050 " d[NsS] - delete file(s) from the archive\n"
51 " m[abiSs] - move file(s) in the archive\n"
52 " p[kN] - print file(s) found in the archive\n"
53 " q[ufsS] - quick append file(s) to the archive\n"
Rafael Espindola94bc2462012-08-10 01:57:52 +000054 " r[abfiuRsS] - replace or insert file(s) into the archive\n"
Reid Spencerbede5832004-11-16 06:41:09 +000055 " t - display contents of archive\n"
56 " x[No] - extract file(s) from the archive\n"
57 "\nMODIFIERS (operation specific):\n"
58 " [a] - put file(s) after [relpos]\n"
59 " [b] - put file(s) before [relpos] (same as [i])\n"
60 " [f] - truncate inserted file names\n"
61 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000062 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000063 " [N] - use instance [count] of name\n"
64 " [o] - preserve original dates\n"
65 " [P] - use full path names when matching\n"
66 " [R] - recurse through directories when inserting\n"
67 " [s] - create an archive index (cf. ranlib)\n"
68 " [S] - do not build a symbol table\n"
69 " [u] - update only files newer than archive contents\n"
Reid Spencerbede5832004-11-16 06:41:09 +000070 "\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
Reid Spencer3a1582b2004-11-14 22:20:07 +000099bool SymTable = true; ///< 's' & 'S' modifiers
100bool OnlyUpdate = false; ///< 'u' modifier
101bool Verbose = false; ///< 'v' modifier
102bool ReallyVerbose = false; ///< 'V' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +0000103
Reid Spencer3a1582b2004-11-14 22:20:07 +0000104// Relative Positional Argument (for insert/move). This variable holds
105// the name of the archive member to which the 'a', 'b' or 'i' modifier
106// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
107// one variable.
108std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000109
Reid Spencer3a1582b2004-11-14 22:20:07 +0000110// Select which of multiple entries in the archive with the same name should be
111// used (specified with -N) for the delete and extract operations.
112int Count = 1;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000113
Reid Spencer3a1582b2004-11-14 22:20:07 +0000114// This variable holds the name of the archive file as given on the
115// command line.
116std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000117
Reid Spencer3a1582b2004-11-14 22:20:07 +0000118// This variable holds the list of member files to proecess, as given
119// on the command line.
120std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000121
Reid Spencer3a1582b2004-11-14 22:20:07 +0000122// This variable holds the (possibly expanded) list of path objects that
123// correspond to files we will
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000124std::set<std::string> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000125
Reid Spencer3a1582b2004-11-14 22:20:07 +0000126// The Archive object to which all the editing operations will be sent.
127Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000128
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000129// The name this program was invoked as.
130static const char *program_name;
131
132// show_help - Show the error message, the help message and exit.
133LLVM_ATTRIBUTE_NORETURN static void
134show_help(const std::string &msg) {
135 errs() << program_name << ": " << msg << "\n\n";
136 cl::PrintHelpMessage();
137 if (TheArchive)
138 delete TheArchive;
139 std::exit(1);
140}
141
142// fail - Show the error message and exit.
143LLVM_ATTRIBUTE_NORETURN static void
144fail(const std::string &msg) {
145 errs() << program_name << ": " << msg << "\n\n";
146 if (TheArchive)
147 delete TheArchive;
148 std::exit(1);
149}
150
Reid Spencer3a1582b2004-11-14 22:20:07 +0000151// getRelPos - Extract the member filename from the command line for
152// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000153void getRelPos() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000154 if(RestOfArgs.size() == 0)
155 show_help("Expected [relpos] for a, b, or i modifier");
156 RelPos = RestOfArgs[0];
157 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000158}
159
Misha Brukman3da94ae2005-04-22 00:00:37 +0000160// getCount - Extract the [count] argument associated with the N modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000161// from the command line and check its value.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000162void getCount() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000163 if(RestOfArgs.size() == 0)
164 show_help("Expected [count] value with N modifier");
165
166 Count = atoi(RestOfArgs[0].c_str());
167 RestOfArgs.erase(RestOfArgs.begin());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000168
169 // Non-positive counts are not allowed
170 if (Count < 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000171 show_help("Invalid [count] value (not a positive integer)");
Tanya Lattner57bd7962003-12-06 23:01:25 +0000172}
173
Reid Spencer3a1582b2004-11-14 22:20:07 +0000174// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000175void getArchive() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000176 if(RestOfArgs.size() == 0)
177 show_help("An archive name must be specified");
178 ArchiveName = RestOfArgs[0];
179 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000180}
181
Reid Spencer3a1582b2004-11-14 22:20:07 +0000182// getMembers - Copy over remaining items in RestOfArgs to our Members vector
183// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000184void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000185 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000186 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000187}
188
Reid Spencer3a1582b2004-11-14 22:20:07 +0000189// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000190// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000191// constraints on modifier/operation pairs have not been violated.
192ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000193
Reid Spencer3a1582b2004-11-14 22:20:07 +0000194 // Keep track of number of operations. We can only specify one
195 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000196 unsigned NumOperations = 0;
197
Reid Spencer3a1582b2004-11-14 22:20:07 +0000198 // Keep track of the number of positional modifiers (a,b,i). Only
199 // one can be specified.
200 unsigned NumPositional = 0;
201
202 // Keep track of which operation was requested
203 ArchiveOperation Operation = NoOperation;
204
Tanya Lattner57bd7962003-12-06 23:01:25 +0000205 for(unsigned i=0; i<Options.size(); ++i) {
206 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000207 case 'd': ++NumOperations; Operation = Delete; break;
208 case 'm': ++NumOperations; Operation = Move ; break;
209 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000210 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000211 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000212 case 't': ++NumOperations; Operation = DisplayTable; break;
213 case 'x': ++NumOperations; Operation = Extract; break;
214 case 'c': Create = true; break;
215 case 'f': TruncateNames = true; break;
Gabor Greifa99be512007-07-05 17:07:56 +0000216 case 'k': DontSkipBitcode = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000217 case 'l': /* accepted but unused */ break;
218 case 'o': OriginalDates = true; break;
219 case 'P': FullPath = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000220 case 's': SymTable = true; break;
221 case 'S': SymTable = false; break;
222 case 'u': OnlyUpdate = true; break;
223 case 'v': Verbose = true; break;
224 case 'V': Verbose = ReallyVerbose = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000225 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000226 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000227 AddAfter = true;
228 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000229 break;
230 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000231 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000232 AddBefore = true;
233 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000234 break;
235 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000236 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000237 InsertBefore = true;
238 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000239 break;
240 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000241 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000242 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000243 break;
244 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000245 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000246 }
247 }
248
Misha Brukman3da94ae2005-04-22 00:00:37 +0000249 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000250 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000251 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000252
253 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000254 getMembers();
255
Reid Spencer3a1582b2004-11-14 22:20:07 +0000256 // Perform various checks on the operation/modifier specification
257 // to make sure we are dealing with a legal request.
258 if (NumOperations == 0)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000259 show_help("You must specify at least one of the operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000260 if (NumOperations > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000261 show_help("Only one operation may be specified");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000262 if (NumPositional > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000263 show_help("You may only specify one of a, b, and i modifiers");
264 if (AddAfter || AddBefore || InsertBefore) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000265 if (Operation != Move && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000266 show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
267 "the 'm' or 'r' operations");
268 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000269 if (OriginalDates && Operation != Extract)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000270 show_help("The 'o' modifier is only applicable to the 'x' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000271 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000272 show_help("The 'f' modifier is only applicable to the 'q' and 'r' "
273 "operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000274 if (OnlyUpdate && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000275 show_help("The 'u' modifier is only applicable to the 'r' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000276 if (Count > 1 && Members.size() > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000277 show_help("Only one member name may be specified with the 'N' modifier");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000278
279 // Return the parsed operation to the caller
280 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000281}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000282
Reid Spencer3a1582b2004-11-14 22:20:07 +0000283// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000284// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000285// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000286bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000287 for (unsigned i = 0; i < Members.size(); i++) {
288 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000289 if (!aPath.set(Members[i]))
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000290 fail(std::string("File member name invalid: ") + Members[i]);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000291 if (checkExistence) {
Michael J. Spencer54453f22011-01-10 02:34:23 +0000292 bool Exists;
293 if (sys::fs::exists(aPath.str(), Exists) || !Exists)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000294 fail(std::string("File does not exist: ") + Members[i]);
Chris Lattner252ad032006-07-28 22:03:44 +0000295 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000296 sys::PathWithStatus PwS(aPath);
297 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000298 if (!si)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000299 fail(Err);
Rafael Espindola8fe960e2013-06-19 14:58:16 +0000300 if (si->isDir)
301 fail(aPath.str() + " Is a directory");
302
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000303 Paths.insert(aPath.str());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000304 } else {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000305 Paths.insert(aPath.str());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000306 }
307 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000308 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000309}
310
Reid Spencerbede5832004-11-16 06:41:09 +0000311// printSymbolTable - print out the archive's symbol table.
312void printSymbolTable() {
Chris Lattner424a04e2010-11-29 23:02:20 +0000313 outs() << "\nArchive Symbol Table:\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000314 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000315 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000316 I != E; ++I ) {
317 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
Chris Lattner424a04e2010-11-29 23:02:20 +0000318 outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000319 }
320}
321
Reid Spencer3a1582b2004-11-14 22:20:07 +0000322// doPrint - Implements the 'p' operation. This function traverses the archive
323// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000324// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000325// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000326bool doPrint(std::string* ErrMsg) {
327 if (buildPaths(false, ErrMsg))
328 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000329 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000330 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000331 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000332 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000333 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
334 if (countDown == 1) {
335 const char* data = reinterpret_cast<const char*>(I->getData());
336
337 // Skip things that don't make sense to print
Rafael Espindola250bfb12013-06-14 23:25:53 +0000338 if (I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000339 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000340 continue;
341
342 if (Verbose)
Chris Lattner424a04e2010-11-29 23:02:20 +0000343 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000344
Chris Lattner44dadff2007-05-06 09:29:57 +0000345 unsigned len = I->getSize();
Chris Lattner424a04e2010-11-29 23:02:20 +0000346 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000347 } else {
348 countDown--;
349 }
350 }
351 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000352 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000353}
354
355// putMode - utility function for printing out the file mode when the 't'
356// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000357void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000358printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000359 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000360 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000361 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000362 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000363 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000364 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000365 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000366 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000367 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000368 outs() << "x";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000369 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000370 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000371}
372
373// doDisplayTable - Implement the 't' operation. This function prints out just
374// the file names of each of the members. However, if verbose mode is requested
375// ('v' modifier) then the file type, permission mode, user, group, size, and
376// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000377bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000378doDisplayTable(std::string* ErrMsg) {
379 if (buildPaths(false, ErrMsg))
380 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000381 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000382 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000383 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000384 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
385 if (Verbose) {
386 // FIXME: Output should be this format:
387 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000388 if (I->isBitcode())
Chris Lattner424a04e2010-11-29 23:02:20 +0000389 outs() << "b";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000390 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000391 outs() << " ";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000392 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000393 printMode((mode >> 6) & 007);
394 printMode((mode >> 3) & 007);
395 printMode(mode & 007);
Chris Lattner424a04e2010-11-29 23:02:20 +0000396 outs() << " " << format("%4u", I->getUser());
397 outs() << "/" << format("%4u", I->getGroup());
398 outs() << " " << format("%8u", I->getSize());
399 outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str());
400 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000401 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000402 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000403 }
404 }
405 }
Reid Spencerbede5832004-11-16 06:41:09 +0000406 if (ReallyVerbose)
407 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000408 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000409}
410
411// doExtract - Implement the 'x' operation. This function extracts files back to
Rafael Espindola94bc2462012-08-10 01:57:52 +0000412// the file system.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000413bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000414doExtract(std::string* ErrMsg) {
415 if (buildPaths(false, ErrMsg))
416 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000417 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000418 I != E; ++I ) {
419 if (Paths.empty() ||
420 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
421
422 // Make sure the intervening directories are created
423 if (I->hasPath()) {
424 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000425 dirs.eraseComponent();
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000426 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
Reid Spencere5c9cb52006-08-23 00:39:35 +0000427 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000428 }
429
430 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000431 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
432 std::ios::binary;
Rafael Espindola3baf01b2013-06-19 16:16:13 +0000433 std::ofstream file(I->getPath().str().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000434
435 // Get the data and its length
436 const char* data = reinterpret_cast<const char*>(I->getData());
437 unsigned len = I->getSize();
438
Chris Lattner44dadff2007-05-06 09:29:57 +0000439 // Write the data.
440 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000441 file.close();
442
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000443 sys::PathWithStatus PWS(I->getPath());
444 sys::FileStatus Status = *PWS.getFileStatus();
445
446 // Retain the original mode.
447 Status.mode = I->getMode();
448
Reid Spencer3a1582b2004-11-14 22:20:07 +0000449 // If we're supposed to retain the original modification times, etc. do so
450 // now.
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000451 if (OriginalDates)
452 Status.modTime = I->getModTime();
453
454 PWS.setStatusInfoOnDisk(Status);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000455 }
456 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000457 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000458}
459
460// doDelete - Implement the delete operation. This function deletes zero or more
461// members from the archive. Note that if the count is specified, there should
462// be no more than one path in the Paths list or else this algorithm breaks.
463// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000464bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000465doDelete(std::string* ErrMsg) {
466 if (buildPaths(false, ErrMsg))
467 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000468 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000469 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000470 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000471 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000472 I != E; ) {
473 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
474 if (countDown == 1) {
475 Archive::iterator J = I;
476 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000477 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000478 } else
479 countDown--;
480 } else {
481 ++I;
482 }
483 }
484
485 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000486 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000487 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000488 if (ReallyVerbose)
489 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000490 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000491}
492
493// doMore - Implement the move operation. This function re-arranges just the
494// order of the archive members so that when the archive is written the move
495// of the members is accomplished. Note the use of the RelPos variable to
496// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000497bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000498doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000499 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000500 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000501
502 // By default and convention the place to move members to is the end of the
503 // archive.
504 Archive::iterator moveto_spot = TheArchive->end();
505
506 // However, if the relative positioning modifiers were used, we need to scan
507 // the archive to find the member in question. If we don't find it, its no
508 // crime, we just move to the end.
509 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000510 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000511 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000512 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000513 if (AddAfter) {
514 moveto_spot = I;
515 moveto_spot++;
516 } else {
517 moveto_spot = I;
518 }
519 break;
520 }
521 }
522 }
523
524 // Keep a list of the paths remaining to be moved
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000525 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000526
527 // Scan the archive again, this time looking for the members to move to the
528 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000529 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000530 I != E && !remaining.empty(); ++I ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000531 std::set<std::string>::iterator found =
532 std::find(remaining.begin(),remaining.end(), I->getPath());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000533 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000534 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000535 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000536 remaining.erase(found);
537 }
538 }
539
540 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000541 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000542 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000543 if (ReallyVerbose)
544 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000545 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000546}
547
548// doQuickAppend - Implements the 'q' operation. This function just
549// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000550bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000551doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000552 // Get the list of paths to append.
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 // Append them quickly.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000559 for (std::set<std::string>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000560 PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000561 if (TheArchive->addFileBefore(*PI, TheArchive->end(), ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000562 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000563 }
564
565 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000566 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000567 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000568 if (ReallyVerbose)
569 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000570 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000571}
572
573// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000574// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000575bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000576doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000577
578 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000579 if (buildPaths(true, ErrMsg))
580 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000581 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000582 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000583
584 // Keep track of the paths that remain to be inserted.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000585 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000586
587 // Default the insertion spot to the end of the archive
588 Archive::iterator insert_spot = TheArchive->end();
589
590 // Iterate over the archive contents
591 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
592 I != E && !remaining.empty(); ++I ) {
593
594 // Determine if this archive member matches one of the paths we're trying
595 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000596
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000597 std::set<std::string>::iterator found = remaining.end();
598 for (std::set<std::string>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000599 RE = remaining.end(); RI != RE; ++RI ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000600 std::string compare(*RI);
Reid Spencer0de66b52004-12-02 09:21:55 +0000601 if (TruncateNames && compare.length() > 15) {
602 const char* nm = compare.c_str();
603 unsigned len = compare.length();
604 size_t slashpos = compare.rfind('/');
605 if (slashpos != std::string::npos) {
606 nm += slashpos + 1;
607 len -= slashpos +1;
608 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000609 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000610 len = 15;
611 compare.assign(nm,len);
612 }
Chris Lattner74382b72009-08-23 22:45:37 +0000613 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000614 found = RI;
615 break;
616 }
617 }
618
Reid Spencer3a1582b2004-11-14 22:20:07 +0000619 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000620 std::string Err;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000621 sys::PathWithStatus PwS(*found);
Reid Spencer184e67e2007-04-08 19:59:07 +0000622 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000623 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000624 return true;
Chris Lattner7d8f9b62009-02-05 17:58:39 +0000625 if (!si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000626 if (OnlyUpdate) {
627 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000628 if (si->modTime > I->getModTime())
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000629 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000630 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000631 } else {
632 // Replace the item regardless of time stamp
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000633 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000634 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000635 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000636 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000637 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000638 }
639
640 // Remove it from our "to do" list
641 remaining.erase(found);
642 }
643
644 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000645 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000646 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000647 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000648 insert_spot = I;
649 insert_spot++;
650 }
651 }
652
653 // If we didn't replace all the members, some will remain and need to be
654 // inserted at the previously computed insert-spot.
655 if (!remaining.empty()) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000656 for (std::set<std::string>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000657 PE = remaining.end(); PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000658 if (TheArchive->addFileBefore(*PI, insert_spot, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000659 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000660 }
661 }
662
663 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000664 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000665 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000666 if (ReallyVerbose)
667 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000668 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000669}
670
671// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000672int main(int argc, char **argv) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000673 program_name = argv[0];
Chris Lattnercc14d252009-03-06 05:34:10 +0000674 // Print a stack trace if we signal out.
675 sys::PrintStackTraceOnErrorSignal();
676 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000677 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000678 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000679
Reid Spencer3a1582b2004-11-14 22:20:07 +0000680 // Have the command line options parsed and handle things
681 // like --help and --version.
682 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000683 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000684 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000685 );
686
Reid Spencer3a1582b2004-11-14 22:20:07 +0000687 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000688
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000689 // Do our own parsing of the command line because the CommandLine utility
690 // can't handle the grouped positional parameters without a dash.
691 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000692
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000693 // Check the path name of the archive
694 sys::Path ArchivePath;
695 if (!ArchivePath.set(ArchiveName)) {
696 errs() << argv[0] << ": Archive name invalid: " << ArchiveName << "\n";
697 return 1;
698 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000699
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000700 // Create or open the archive object.
701 bool Exists;
702 if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) {
703 // Produce a warning if we should and we're creating the archive
704 if (!Create)
705 errs() << argv[0] << ": creating " << ArchivePath.str() << "\n";
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000706 TheArchive = Archive::CreateEmpty(ArchivePath.str(), Context);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000707 TheArchive->writeToDisk();
708 } else {
709 std::string Error;
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000710 TheArchive = Archive::OpenAndLoad(ArchivePath.str(), Context, &Error);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000711 if (TheArchive == 0) {
712 errs() << argv[0] << ": error loading '" << ArchivePath.str() << "': "
713 << Error << "!\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000714 return 1;
715 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000716 }
717
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000718 // Make sure we're not fooling ourselves.
719 assert(TheArchive && "Unable to instantiate the archive");
720
721 // Perform the operation
722 std::string ErrMsg;
723 bool haveError = false;
724 switch (Operation) {
725 case Print: haveError = doPrint(&ErrMsg); break;
726 case Delete: haveError = doDelete(&ErrMsg); break;
727 case Move: haveError = doMove(&ErrMsg); break;
728 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
729 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
730 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
731 case Extract: haveError = doExtract(&ErrMsg); break;
732 case NoOperation:
733 errs() << argv[0] << ": No operation was selected.\n";
734 break;
735 }
736 if (haveError) {
737 errs() << argv[0] << ": " << ErrMsg << "\n";
738 return 1;
739 }
740
741 delete TheArchive;
742 TheArchive = 0;
743
Reid Spencer3a1582b2004-11-14 22:20:07 +0000744 // Return result code back to operating system.
745 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000746}