blob: 85f3517b58ad7f416c85d329a1c961bde0ebe66b [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"
Rafael Espindolab4900b12013-06-19 21:13:59 +000022#include "llvm/Support/PathV1.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000023#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000024#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000025#include "llvm/Support/raw_ostream.h"
Reid Spencer3a1582b2004-11-14 22:20:07 +000026#include <algorithm>
Joerg Sonnenberger975bc072012-10-26 10:49:15 +000027#include <cstdlib>
Chris Lattner8da76362009-08-24 04:14:03 +000028#include <fstream>
Chandler Carruthf010c462012-12-04 10:44:52 +000029#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000030using namespace llvm;
31
Dan Gohman82f209e2007-10-15 21:10:03 +000032// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000033static cl::opt<bool>
34X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000035 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000036
Reid Spencerbede5832004-11-16 06:41:09 +000037// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000038static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000039Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000040
Reid Spencerbede5832004-11-16 06:41:09 +000041// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000042static cl::list<std::string>
43RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000044 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000045
Reid Spencerbede5832004-11-16 06:41:09 +000046// MoreHelp - Provide additional help output explaining the operations and
47// modifiers of llvm-ar. This object instructs the CommandLine library
48// to print the text of the constructor when the --help option is given.
49static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000050 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000051 " d[NsS] - delete file(s) from the archive\n"
52 " m[abiSs] - move file(s) in the archive\n"
53 " p[kN] - print file(s) found in the archive\n"
54 " q[ufsS] - quick append file(s) to the archive\n"
Rafael Espindola94bc2462012-08-10 01:57:52 +000055 " r[abfiuRsS] - replace or insert file(s) into the archive\n"
Reid Spencerbede5832004-11-16 06:41:09 +000056 " t - display contents of archive\n"
57 " x[No] - extract file(s) from the archive\n"
58 "\nMODIFIERS (operation specific):\n"
59 " [a] - put file(s) after [relpos]\n"
60 " [b] - put file(s) before [relpos] (same as [i])\n"
61 " [f] - truncate inserted file names\n"
62 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000063 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000064 " [N] - use instance [count] of name\n"
65 " [o] - preserve original dates\n"
66 " [P] - use full path names when matching\n"
67 " [R] - recurse through directories when inserting\n"
68 " [s] - create an archive index (cf. ranlib)\n"
69 " [S] - do not build a symbol table\n"
70 " [u] - update only files newer than archive contents\n"
Reid Spencerbede5832004-11-16 06:41:09 +000071 "\nMODIFIERS (generic):\n"
72 " [c] - do not warn if the library had to be created\n"
73 " [v] - be verbose about actions taken\n"
74 " [V] - be *really* verbose about actions taken\n"
75);
76
Reid Spencer3a1582b2004-11-14 22:20:07 +000077// This enumeration delineates the kinds of operations on an archive
78// that are permitted.
79enum ArchiveOperation {
80 NoOperation, ///< An operation hasn't been specified
81 Print, ///< Print the contents of the archive
82 Delete, ///< Delete the specified members
83 Move, ///< Move members to end or as given by {a,b,i} modifiers
84 QuickAppend, ///< Quickly append to end of archive
85 ReplaceOrInsert, ///< Replace or Insert members
86 DisplayTable, ///< Display the table of contents
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000087 Extract ///< Extract files back to file system
Tanya Lattner57bd7962003-12-06 23:01:25 +000088};
Tanya Lattner14baebf2003-08-28 15:22:38 +000089
Reid Spencer3a1582b2004-11-14 22:20:07 +000090// Modifiers to follow operation to vary behavior
91bool AddAfter = false; ///< 'a' modifier
92bool AddBefore = false; ///< 'b' modifier
Misha Brukman3da94ae2005-04-22 00:00:37 +000093bool Create = false; ///< 'c' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000094bool TruncateNames = false; ///< 'f' modifier
95bool InsertBefore = false; ///< 'i' modifier
Gabor Greifa99be512007-07-05 17:07:56 +000096bool DontSkipBitcode = false; ///< 'k' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000097bool UseCount = false; ///< 'N' modifier
98bool OriginalDates = false; ///< 'o' modifier
99bool FullPath = false; ///< 'P' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000100bool SymTable = true; ///< 's' & 'S' modifiers
101bool OnlyUpdate = false; ///< 'u' modifier
102bool Verbose = false; ///< 'v' modifier
103bool ReallyVerbose = false; ///< 'V' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +0000104
Reid Spencer3a1582b2004-11-14 22:20:07 +0000105// Relative Positional Argument (for insert/move). This variable holds
106// the name of the archive member to which the 'a', 'b' or 'i' modifier
107// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
108// one variable.
109std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000110
Reid Spencer3a1582b2004-11-14 22:20:07 +0000111// Select which of multiple entries in the archive with the same name should be
112// used (specified with -N) for the delete and extract operations.
113int Count = 1;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000114
Reid Spencer3a1582b2004-11-14 22:20:07 +0000115// This variable holds the name of the archive file as given on the
116// command line.
117std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000118
Reid Spencer3a1582b2004-11-14 22:20:07 +0000119// This variable holds the list of member files to proecess, as given
120// on the command line.
121std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000122
Reid Spencer3a1582b2004-11-14 22:20:07 +0000123// This variable holds the (possibly expanded) list of path objects that
124// correspond to files we will
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000125std::set<std::string> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000126
Reid Spencer3a1582b2004-11-14 22:20:07 +0000127// The Archive object to which all the editing operations will be sent.
128Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000129
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000130// The name this program was invoked as.
131static const char *program_name;
132
133// show_help - Show the error message, the help message and exit.
134LLVM_ATTRIBUTE_NORETURN static void
135show_help(const std::string &msg) {
136 errs() << program_name << ": " << msg << "\n\n";
137 cl::PrintHelpMessage();
138 if (TheArchive)
139 delete TheArchive;
140 std::exit(1);
141}
142
143// fail - Show the error message and exit.
144LLVM_ATTRIBUTE_NORETURN static void
145fail(const std::string &msg) {
146 errs() << program_name << ": " << msg << "\n\n";
147 if (TheArchive)
148 delete TheArchive;
149 std::exit(1);
150}
151
Reid Spencer3a1582b2004-11-14 22:20:07 +0000152// getRelPos - Extract the member filename from the command line for
153// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000154void getRelPos() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000155 if(RestOfArgs.size() == 0)
156 show_help("Expected [relpos] for a, b, or i modifier");
157 RelPos = RestOfArgs[0];
158 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000159}
160
Misha Brukman3da94ae2005-04-22 00:00:37 +0000161// getCount - Extract the [count] argument associated with the N modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000162// from the command line and check its value.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000163void getCount() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000164 if(RestOfArgs.size() == 0)
165 show_help("Expected [count] value with N modifier");
166
167 Count = atoi(RestOfArgs[0].c_str());
168 RestOfArgs.erase(RestOfArgs.begin());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000169
170 // Non-positive counts are not allowed
171 if (Count < 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000172 show_help("Invalid [count] value (not a positive integer)");
Tanya Lattner57bd7962003-12-06 23:01:25 +0000173}
174
Reid Spencer3a1582b2004-11-14 22:20:07 +0000175// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000176void getArchive() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000177 if(RestOfArgs.size() == 0)
178 show_help("An archive name must be specified");
179 ArchiveName = RestOfArgs[0];
180 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000181}
182
Reid Spencer3a1582b2004-11-14 22:20:07 +0000183// getMembers - Copy over remaining items in RestOfArgs to our Members vector
184// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000185void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000186 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000187 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000188}
189
Reid Spencer3a1582b2004-11-14 22:20:07 +0000190// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000191// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000192// constraints on modifier/operation pairs have not been violated.
193ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000194
Reid Spencer3a1582b2004-11-14 22:20:07 +0000195 // Keep track of number of operations. We can only specify one
196 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000197 unsigned NumOperations = 0;
198
Reid Spencer3a1582b2004-11-14 22:20:07 +0000199 // Keep track of the number of positional modifiers (a,b,i). Only
200 // one can be specified.
201 unsigned NumPositional = 0;
202
203 // Keep track of which operation was requested
204 ArchiveOperation Operation = NoOperation;
205
Tanya Lattner57bd7962003-12-06 23:01:25 +0000206 for(unsigned i=0; i<Options.size(); ++i) {
207 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000208 case 'd': ++NumOperations; Operation = Delete; break;
209 case 'm': ++NumOperations; Operation = Move ; break;
210 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000211 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000212 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000213 case 't': ++NumOperations; Operation = DisplayTable; break;
214 case 'x': ++NumOperations; Operation = Extract; break;
215 case 'c': Create = true; break;
216 case 'f': TruncateNames = true; break;
Gabor Greifa99be512007-07-05 17:07:56 +0000217 case 'k': DontSkipBitcode = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000218 case 'l': /* accepted but unused */ break;
219 case 'o': OriginalDates = true; break;
220 case 'P': FullPath = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000221 case 's': SymTable = true; break;
222 case 'S': SymTable = false; break;
223 case 'u': OnlyUpdate = true; break;
224 case 'v': Verbose = true; break;
225 case 'V': Verbose = ReallyVerbose = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000226 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000227 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000228 AddAfter = true;
229 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000230 break;
231 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000232 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000233 AddBefore = true;
234 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000235 break;
236 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000237 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000238 InsertBefore = true;
239 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000240 break;
241 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000242 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000243 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000244 break;
245 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000246 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000247 }
248 }
249
Misha Brukman3da94ae2005-04-22 00:00:37 +0000250 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000251 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000252 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000253
254 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000255 getMembers();
256
Reid Spencer3a1582b2004-11-14 22:20:07 +0000257 // Perform various checks on the operation/modifier specification
258 // to make sure we are dealing with a legal request.
259 if (NumOperations == 0)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000260 show_help("You must specify at least one of the operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000261 if (NumOperations > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000262 show_help("Only one operation may be specified");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000263 if (NumPositional > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000264 show_help("You may only specify one of a, b, and i modifiers");
265 if (AddAfter || AddBefore || InsertBefore) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000266 if (Operation != Move && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000267 show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
268 "the 'm' or 'r' operations");
269 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000270 if (OriginalDates && Operation != Extract)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000271 show_help("The 'o' modifier is only applicable to the 'x' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000272 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000273 show_help("The 'f' modifier is only applicable to the 'q' and 'r' "
274 "operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000275 if (OnlyUpdate && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000276 show_help("The 'u' modifier is only applicable to the 'r' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000277 if (Count > 1 && Members.size() > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000278 show_help("Only one member name may be specified with the 'N' modifier");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000279
280 // Return the parsed operation to the caller
281 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000282}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000283
Reid Spencer3a1582b2004-11-14 22:20:07 +0000284// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000285// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000286// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000287bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000288 for (unsigned i = 0; i < Members.size(); i++) {
289 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000290 if (!aPath.set(Members[i]))
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000291 fail(std::string("File member name invalid: ") + Members[i]);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000292 if (checkExistence) {
Michael J. Spencer54453f22011-01-10 02:34:23 +0000293 bool Exists;
294 if (sys::fs::exists(aPath.str(), Exists) || !Exists)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000295 fail(std::string("File does not exist: ") + Members[i]);
Chris Lattner252ad032006-07-28 22:03:44 +0000296 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000297 sys::PathWithStatus PwS(aPath);
298 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000299 if (!si)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000300 fail(Err);
Rafael Espindola8fe960e2013-06-19 14:58:16 +0000301 if (si->isDir)
302 fail(aPath.str() + " Is a directory");
303
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000304 Paths.insert(aPath.str());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000305 } else {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000306 Paths.insert(aPath.str());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000307 }
308 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000309 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000310}
311
Reid Spencerbede5832004-11-16 06:41:09 +0000312// printSymbolTable - print out the archive's symbol table.
313void printSymbolTable() {
Chris Lattner424a04e2010-11-29 23:02:20 +0000314 outs() << "\nArchive Symbol Table:\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000315 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000316 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000317 I != E; ++I ) {
318 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
Chris Lattner424a04e2010-11-29 23:02:20 +0000319 outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000320 }
321}
322
Reid Spencer3a1582b2004-11-14 22:20:07 +0000323// doPrint - Implements the 'p' operation. This function traverses the archive
324// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000325// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000326// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000327bool doPrint(std::string* ErrMsg) {
328 if (buildPaths(false, ErrMsg))
329 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000330 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000331 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000332 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000333 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000334 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
335 if (countDown == 1) {
336 const char* data = reinterpret_cast<const char*>(I->getData());
337
338 // Skip things that don't make sense to print
Rafael Espindola250bfb12013-06-14 23:25:53 +0000339 if (I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000340 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000341 continue;
342
343 if (Verbose)
Chris Lattner424a04e2010-11-29 23:02:20 +0000344 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000345
Chris Lattner44dadff2007-05-06 09:29:57 +0000346 unsigned len = I->getSize();
Chris Lattner424a04e2010-11-29 23:02:20 +0000347 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000348 } else {
349 countDown--;
350 }
351 }
352 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000353 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000354}
355
356// putMode - utility function for printing out the file mode when the 't'
357// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000358void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000359printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000360 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000361 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000362 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000363 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000364 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000365 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000366 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000367 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000368 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000369 outs() << "x";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000370 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000371 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000372}
373
374// doDisplayTable - Implement the 't' operation. This function prints out just
375// the file names of each of the members. However, if verbose mode is requested
376// ('v' modifier) then the file type, permission mode, user, group, size, and
377// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000378bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000379doDisplayTable(std::string* ErrMsg) {
380 if (buildPaths(false, ErrMsg))
381 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000382 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000383 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000384 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000385 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
386 if (Verbose) {
387 // FIXME: Output should be this format:
388 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000389 if (I->isBitcode())
Chris Lattner424a04e2010-11-29 23:02:20 +0000390 outs() << "b";
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 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000394 printMode((mode >> 6) & 007);
395 printMode((mode >> 3) & 007);
396 printMode(mode & 007);
Chris Lattner424a04e2010-11-29 23:02:20 +0000397 outs() << " " << format("%4u", I->getUser());
398 outs() << "/" << format("%4u", I->getGroup());
399 outs() << " " << format("%8u", I->getSize());
400 outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str());
401 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000402 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000403 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000404 }
405 }
406 }
Reid Spencerbede5832004-11-16 06:41:09 +0000407 if (ReallyVerbose)
408 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000409 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000410}
411
412// doExtract - Implement the 'x' operation. This function extracts files back to
Rafael Espindola94bc2462012-08-10 01:57:52 +0000413// the file system.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000414bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000415doExtract(std::string* ErrMsg) {
416 if (buildPaths(false, ErrMsg))
417 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000418 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000419 I != E; ++I ) {
420 if (Paths.empty() ||
421 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
422
423 // Make sure the intervening directories are created
424 if (I->hasPath()) {
425 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000426 dirs.eraseComponent();
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000427 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
Reid Spencere5c9cb52006-08-23 00:39:35 +0000428 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000429 }
430
431 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000432 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
433 std::ios::binary;
Rafael Espindola3baf01b2013-06-19 16:16:13 +0000434 std::ofstream file(I->getPath().str().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000435
436 // Get the data and its length
437 const char* data = reinterpret_cast<const char*>(I->getData());
438 unsigned len = I->getSize();
439
Chris Lattner44dadff2007-05-06 09:29:57 +0000440 // Write the data.
441 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000442 file.close();
443
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000444 sys::PathWithStatus PWS(I->getPath());
445 sys::FileStatus Status = *PWS.getFileStatus();
446
447 // Retain the original mode.
448 Status.mode = I->getMode();
449
Reid Spencer3a1582b2004-11-14 22:20:07 +0000450 // If we're supposed to retain the original modification times, etc. do so
451 // now.
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000452 if (OriginalDates)
453 Status.modTime = I->getModTime();
454
455 PWS.setStatusInfoOnDisk(Status);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000456 }
457 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000458 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000459}
460
461// doDelete - Implement the delete operation. This function deletes zero or more
462// members from the archive. Note that if the count is specified, there should
463// be no more than one path in the Paths list or else this algorithm breaks.
464// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000465bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000466doDelete(std::string* ErrMsg) {
467 if (buildPaths(false, ErrMsg))
468 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000469 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000470 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000471 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000472 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000473 I != E; ) {
474 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
475 if (countDown == 1) {
476 Archive::iterator J = I;
477 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000478 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000479 } else
480 countDown--;
481 } else {
482 ++I;
483 }
484 }
485
486 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000487 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000488 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000489 if (ReallyVerbose)
490 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000491 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000492}
493
494// doMore - Implement the move operation. This function re-arranges just the
495// order of the archive members so that when the archive is written the move
496// of the members is accomplished. Note the use of the RelPos variable to
497// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000498bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000499doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000500 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000501 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000502
503 // By default and convention the place to move members to is the end of the
504 // archive.
505 Archive::iterator moveto_spot = TheArchive->end();
506
507 // However, if the relative positioning modifiers were used, we need to scan
508 // the archive to find the member in question. If we don't find it, its no
509 // crime, we just move to the end.
510 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000511 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000512 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000513 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000514 if (AddAfter) {
515 moveto_spot = I;
516 moveto_spot++;
517 } else {
518 moveto_spot = I;
519 }
520 break;
521 }
522 }
523 }
524
525 // Keep a list of the paths remaining to be moved
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000526 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000527
528 // Scan the archive again, this time looking for the members to move to the
529 // moveto_spot.
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 && !remaining.empty(); ++I ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000532 std::set<std::string>::iterator found =
533 std::find(remaining.begin(),remaining.end(), I->getPath());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000534 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000535 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000536 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000537 remaining.erase(found);
538 }
539 }
540
541 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000542 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000543 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000544 if (ReallyVerbose)
545 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000546 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000547}
548
549// doQuickAppend - Implements the 'q' operation. This function just
550// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000551bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000552doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000553 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000554 if (buildPaths(true, ErrMsg))
555 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000556 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000557 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000558
559 // Append them quickly.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000560 for (std::set<std::string>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000561 PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000562 if (TheArchive->addFileBefore(*PI, TheArchive->end(), ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000563 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000564 }
565
566 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000567 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000568 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000569 if (ReallyVerbose)
570 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000571 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000572}
573
574// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000575// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000576bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000577doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000578
579 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000580 if (buildPaths(true, ErrMsg))
581 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000582 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000583 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000584
585 // Keep track of the paths that remain to be inserted.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000586 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000587
588 // Default the insertion spot to the end of the archive
589 Archive::iterator insert_spot = TheArchive->end();
590
591 // Iterate over the archive contents
592 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
593 I != E && !remaining.empty(); ++I ) {
594
595 // Determine if this archive member matches one of the paths we're trying
596 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000597
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000598 std::set<std::string>::iterator found = remaining.end();
599 for (std::set<std::string>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000600 RE = remaining.end(); RI != RE; ++RI ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000601 std::string compare(*RI);
Reid Spencer0de66b52004-12-02 09:21:55 +0000602 if (TruncateNames && compare.length() > 15) {
603 const char* nm = compare.c_str();
604 unsigned len = compare.length();
605 size_t slashpos = compare.rfind('/');
606 if (slashpos != std::string::npos) {
607 nm += slashpos + 1;
608 len -= slashpos +1;
609 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000610 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000611 len = 15;
612 compare.assign(nm,len);
613 }
Chris Lattner74382b72009-08-23 22:45:37 +0000614 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000615 found = RI;
616 break;
617 }
618 }
619
Reid Spencer3a1582b2004-11-14 22:20:07 +0000620 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000621 std::string Err;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000622 sys::PathWithStatus PwS(*found);
Reid Spencer184e67e2007-04-08 19:59:07 +0000623 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000624 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000625 return true;
Chris Lattner7d8f9b62009-02-05 17:58:39 +0000626 if (!si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000627 if (OnlyUpdate) {
628 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000629 if (si->modTime > I->getModTime())
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000630 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000631 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000632 } else {
633 // Replace the item regardless of time stamp
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000634 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000635 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000636 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000637 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000638 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000639 }
640
641 // Remove it from our "to do" list
642 remaining.erase(found);
643 }
644
645 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000646 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000647 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000648 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000649 insert_spot = I;
650 insert_spot++;
651 }
652 }
653
654 // If we didn't replace all the members, some will remain and need to be
655 // inserted at the previously computed insert-spot.
656 if (!remaining.empty()) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000657 for (std::set<std::string>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000658 PE = remaining.end(); PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000659 if (TheArchive->addFileBefore(*PI, insert_spot, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000660 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000661 }
662 }
663
664 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000665 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000666 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000667 if (ReallyVerbose)
668 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000669 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000670}
671
672// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000673int main(int argc, char **argv) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000674 program_name = argv[0];
Chris Lattnercc14d252009-03-06 05:34:10 +0000675 // Print a stack trace if we signal out.
676 sys::PrintStackTraceOnErrorSignal();
677 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000678 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000679 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000680
Reid Spencer3a1582b2004-11-14 22:20:07 +0000681 // Have the command line options parsed and handle things
682 // like --help and --version.
683 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000684 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000685 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000686 );
687
Reid Spencer3a1582b2004-11-14 22:20:07 +0000688 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000689
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000690 // Do our own parsing of the command line because the CommandLine utility
691 // can't handle the grouped positional parameters without a dash.
692 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000693
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000694 // Check the path name of the archive
695 sys::Path ArchivePath;
696 if (!ArchivePath.set(ArchiveName)) {
697 errs() << argv[0] << ": Archive name invalid: " << ArchiveName << "\n";
698 return 1;
699 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000700
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000701 // Create or open the archive object.
702 bool Exists;
703 if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) {
704 // Produce a warning if we should and we're creating the archive
705 if (!Create)
706 errs() << argv[0] << ": creating " << ArchivePath.str() << "\n";
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000707 TheArchive = Archive::CreateEmpty(ArchivePath.str(), Context);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000708 TheArchive->writeToDisk();
709 } else {
710 std::string Error;
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000711 TheArchive = Archive::OpenAndLoad(ArchivePath.str(), Context, &Error);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000712 if (TheArchive == 0) {
713 errs() << argv[0] << ": error loading '" << ArchivePath.str() << "': "
714 << Error << "!\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000715 return 1;
716 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000717 }
718
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000719 // Make sure we're not fooling ourselves.
720 assert(TheArchive && "Unable to instantiate the archive");
721
722 // Perform the operation
723 std::string ErrMsg;
724 bool haveError = false;
725 switch (Operation) {
726 case Print: haveError = doPrint(&ErrMsg); break;
727 case Delete: haveError = doDelete(&ErrMsg); break;
728 case Move: haveError = doMove(&ErrMsg); break;
729 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
730 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
731 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
732 case Extract: haveError = doExtract(&ErrMsg); break;
733 case NoOperation:
734 errs() << argv[0] << ": No operation was selected.\n";
735 break;
736 }
737 if (haveError) {
738 errs() << argv[0] << ": " << ErrMsg << "\n";
739 return 1;
740 }
741
742 delete TheArchive;
743 TheArchive = 0;
744
Reid Spencer3a1582b2004-11-14 22:20:07 +0000745 // Return result code back to operating system.
746 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000747}